x86, ioapic: Fix io_apic_redir_entries to return the number of entries.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / timbuart.c
blob786ba85c170ba622d638a010fc27b1c1213142a3
1 /*
2 * timbuart.c timberdale FPGA UART driver
3 * Copyright (c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Supports:
20 * Timberdale FPGA UART
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/serial_core.h>
26 #include <linux/kernel.h>
27 #include <linux/platform_device.h>
28 #include <linux/ioport.h>
29 #include <linux/slab.h>
31 #include "timbuart.h"
33 struct timbuart_port {
34 struct uart_port port;
35 struct tasklet_struct tasklet;
36 int usedma;
37 u32 last_ier;
38 struct platform_device *dev;
41 static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
42 921600, 1843200, 3250000};
44 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
46 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
48 static void timbuart_stop_rx(struct uart_port *port)
50 /* spin lock held by upper layer, disable all RX interrupts */
51 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
52 iowrite32(ier, port->membase + TIMBUART_IER);
55 static void timbuart_stop_tx(struct uart_port *port)
57 /* spinlock held by upper layer, disable TX interrupt */
58 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
59 iowrite32(ier, port->membase + TIMBUART_IER);
62 static void timbuart_start_tx(struct uart_port *port)
64 struct timbuart_port *uart =
65 container_of(port, struct timbuart_port, port);
67 /* do not transfer anything here -> fire off the tasklet */
68 tasklet_schedule(&uart->tasklet);
71 static void timbuart_flush_buffer(struct uart_port *port)
73 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) | TIMBUART_CTRL_FLSHTX;
75 iowrite8(ctl, port->membase + TIMBUART_CTRL);
76 iowrite32(TXBF, port->membase + TIMBUART_ISR);
79 static void timbuart_rx_chars(struct uart_port *port)
81 struct tty_struct *tty = port->state->port.tty;
83 while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
84 u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
85 port->icount.rx++;
86 tty_insert_flip_char(tty, ch, TTY_NORMAL);
89 spin_unlock(&port->lock);
90 tty_flip_buffer_push(port->state->port.tty);
91 spin_lock(&port->lock);
93 dev_dbg(port->dev, "%s - total read %d bytes\n",
94 __func__, port->icount.rx);
97 static void timbuart_tx_chars(struct uart_port *port)
99 struct circ_buf *xmit = &port->state->xmit;
101 while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
102 !uart_circ_empty(xmit)) {
103 iowrite8(xmit->buf[xmit->tail],
104 port->membase + TIMBUART_TXFIFO);
105 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
106 port->icount.tx++;
109 dev_dbg(port->dev,
110 "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
111 __func__,
112 port->icount.tx,
113 ioread8(port->membase + TIMBUART_CTRL),
114 port->mctrl & TIOCM_RTS,
115 ioread8(port->membase + TIMBUART_BAUDRATE));
118 static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
120 struct timbuart_port *uart =
121 container_of(port, struct timbuart_port, port);
122 struct circ_buf *xmit = &port->state->xmit;
124 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
125 return;
127 if (port->x_char)
128 return;
130 if (isr & TXFLAGS) {
131 timbuart_tx_chars(port);
132 /* clear all TX interrupts */
133 iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
135 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
136 uart_write_wakeup(port);
137 } else
138 /* Re-enable any tx interrupt */
139 *ier |= uart->last_ier & TXFLAGS;
141 /* enable interrupts if there are chars in the transmit buffer,
142 * Or if we delivered some bytes and want the almost empty interrupt
143 * we wake up the upper layer later when we got the interrupt
144 * to give it some time to go out...
146 if (!uart_circ_empty(xmit))
147 *ier |= TXBAE;
149 dev_dbg(port->dev, "%s - leaving\n", __func__);
152 void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
154 if (isr & RXFLAGS) {
155 /* Some RX status is set */
156 if (isr & RXBF) {
157 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
158 TIMBUART_CTRL_FLSHRX;
159 iowrite8(ctl, port->membase + TIMBUART_CTRL);
160 port->icount.overrun++;
161 } else if (isr & (RXDP))
162 timbuart_rx_chars(port);
164 /* ack all RX interrupts */
165 iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
168 /* always have the RX interrupts enabled */
169 *ier |= RXBAF | RXBF | RXTT;
171 dev_dbg(port->dev, "%s - leaving\n", __func__);
174 void timbuart_tasklet(unsigned long arg)
176 struct timbuart_port *uart = (struct timbuart_port *)arg;
177 u32 isr, ier = 0;
179 spin_lock(&uart->port.lock);
181 isr = ioread32(uart->port.membase + TIMBUART_ISR);
182 dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
184 if (!uart->usedma)
185 timbuart_handle_tx_port(&uart->port, isr, &ier);
187 timbuart_mctrl_check(&uart->port, isr, &ier);
189 if (!uart->usedma)
190 timbuart_handle_rx_port(&uart->port, isr, &ier);
192 iowrite32(ier, uart->port.membase + TIMBUART_IER);
194 spin_unlock(&uart->port.lock);
195 dev_dbg(uart->port.dev, "%s leaving\n", __func__);
198 static unsigned int timbuart_tx_empty(struct uart_port *port)
200 u32 isr = ioread32(port->membase + TIMBUART_ISR);
202 return (isr & TXBE) ? TIOCSER_TEMT : 0;
205 static unsigned int timbuart_get_mctrl(struct uart_port *port)
207 u8 cts = ioread8(port->membase + TIMBUART_CTRL);
208 dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
210 if (cts & TIMBUART_CTRL_CTS)
211 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
212 else
213 return TIOCM_DSR | TIOCM_CAR;
216 static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
218 dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
220 if (mctrl & TIOCM_RTS)
221 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
222 else
223 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
226 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
228 unsigned int cts;
230 if (isr & CTS_DELTA) {
231 /* ack */
232 iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
233 cts = timbuart_get_mctrl(port);
234 uart_handle_cts_change(port, cts & TIOCM_CTS);
235 wake_up_interruptible(&port->state->port.delta_msr_wait);
238 *ier |= CTS_DELTA;
241 static void timbuart_enable_ms(struct uart_port *port)
243 /* N/A */
246 static void timbuart_break_ctl(struct uart_port *port, int ctl)
248 /* N/A */
251 static int timbuart_startup(struct uart_port *port)
253 struct timbuart_port *uart =
254 container_of(port, struct timbuart_port, port);
256 dev_dbg(port->dev, "%s\n", __func__);
258 iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
259 iowrite32(0x1ff, port->membase + TIMBUART_ISR);
260 /* Enable all but TX interrupts */
261 iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
262 port->membase + TIMBUART_IER);
264 return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
265 "timb-uart", uart);
268 static void timbuart_shutdown(struct uart_port *port)
270 struct timbuart_port *uart =
271 container_of(port, struct timbuart_port, port);
272 dev_dbg(port->dev, "%s\n", __func__);
273 free_irq(port->irq, uart);
274 iowrite32(0, port->membase + TIMBUART_IER);
277 static int get_bindex(int baud)
279 int i;
281 for (i = 0; i < ARRAY_SIZE(baudrates); i++)
282 if (baud <= baudrates[i])
283 return i;
285 return -1;
288 static void timbuart_set_termios(struct uart_port *port,
289 struct ktermios *termios,
290 struct ktermios *old)
292 unsigned int baud;
293 short bindex;
294 unsigned long flags;
296 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
297 bindex = get_bindex(baud);
298 dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
300 if (bindex < 0)
301 bindex = 0;
302 baud = baudrates[bindex];
304 /* The serial layer calls into this once with old = NULL when setting
305 up initially */
306 if (old)
307 tty_termios_copy_hw(termios, old);
308 tty_termios_encode_baud_rate(termios, baud, baud);
310 spin_lock_irqsave(&port->lock, flags);
311 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
312 uart_update_timeout(port, termios->c_cflag, baud);
313 spin_unlock_irqrestore(&port->lock, flags);
316 static const char *timbuart_type(struct uart_port *port)
318 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
321 /* We do not request/release mappings of the registers here,
322 * currently it's done in the proble function.
324 static void timbuart_release_port(struct uart_port *port)
326 struct platform_device *pdev = to_platform_device(port->dev);
327 int size =
328 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
330 if (port->flags & UPF_IOREMAP) {
331 iounmap(port->membase);
332 port->membase = NULL;
335 release_mem_region(port->mapbase, size);
338 static int timbuart_request_port(struct uart_port *port)
340 struct platform_device *pdev = to_platform_device(port->dev);
341 int size =
342 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
344 if (!request_mem_region(port->mapbase, size, "timb-uart"))
345 return -EBUSY;
347 if (port->flags & UPF_IOREMAP) {
348 port->membase = ioremap(port->mapbase, size);
349 if (port->membase == NULL) {
350 release_mem_region(port->mapbase, size);
351 return -ENOMEM;
355 return 0;
358 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
360 struct timbuart_port *uart = (struct timbuart_port *)devid;
362 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
363 uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
365 /* disable interrupts, the tasklet enables them again */
366 iowrite32(0, uart->port.membase + TIMBUART_IER);
368 /* fire off bottom half */
369 tasklet_schedule(&uart->tasklet);
371 return IRQ_HANDLED;
372 } else
373 return IRQ_NONE;
377 * Configure/autoconfigure the port.
379 static void timbuart_config_port(struct uart_port *port, int flags)
381 if (flags & UART_CONFIG_TYPE) {
382 port->type = PORT_TIMBUART;
383 timbuart_request_port(port);
387 static int timbuart_verify_port(struct uart_port *port,
388 struct serial_struct *ser)
390 /* we don't want the core code to modify any port params */
391 return -EINVAL;
394 static struct uart_ops timbuart_ops = {
395 .tx_empty = timbuart_tx_empty,
396 .set_mctrl = timbuart_set_mctrl,
397 .get_mctrl = timbuart_get_mctrl,
398 .stop_tx = timbuart_stop_tx,
399 .start_tx = timbuart_start_tx,
400 .flush_buffer = timbuart_flush_buffer,
401 .stop_rx = timbuart_stop_rx,
402 .enable_ms = timbuart_enable_ms,
403 .break_ctl = timbuart_break_ctl,
404 .startup = timbuart_startup,
405 .shutdown = timbuart_shutdown,
406 .set_termios = timbuart_set_termios,
407 .type = timbuart_type,
408 .release_port = timbuart_release_port,
409 .request_port = timbuart_request_port,
410 .config_port = timbuart_config_port,
411 .verify_port = timbuart_verify_port
414 static struct uart_driver timbuart_driver = {
415 .owner = THIS_MODULE,
416 .driver_name = "timberdale_uart",
417 .dev_name = "ttyTU",
418 .major = TIMBUART_MAJOR,
419 .minor = TIMBUART_MINOR,
420 .nr = 1
423 static int timbuart_probe(struct platform_device *dev)
425 int err, irq;
426 struct timbuart_port *uart;
427 struct resource *iomem;
429 dev_dbg(&dev->dev, "%s\n", __func__);
431 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
432 if (!uart) {
433 err = -EINVAL;
434 goto err_mem;
437 uart->usedma = 0;
439 uart->port.uartclk = 3250000 * 16;
440 uart->port.fifosize = TIMBUART_FIFO_SIZE;
441 uart->port.regshift = 2;
442 uart->port.iotype = UPIO_MEM;
443 uart->port.ops = &timbuart_ops;
444 uart->port.irq = 0;
445 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
446 uart->port.line = 0;
447 uart->port.dev = &dev->dev;
449 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
450 if (!iomem) {
451 err = -ENOMEM;
452 goto err_register;
454 uart->port.mapbase = iomem->start;
455 uart->port.membase = NULL;
457 irq = platform_get_irq(dev, 0);
458 if (irq < 0) {
459 err = -EINVAL;
460 goto err_register;
462 uart->port.irq = irq;
464 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
466 err = uart_register_driver(&timbuart_driver);
467 if (err)
468 goto err_register;
470 err = uart_add_one_port(&timbuart_driver, &uart->port);
471 if (err)
472 goto err_add_port;
474 platform_set_drvdata(dev, uart);
476 return 0;
478 err_add_port:
479 uart_unregister_driver(&timbuart_driver);
480 err_register:
481 kfree(uart);
482 err_mem:
483 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
484 err);
486 return err;
489 static int timbuart_remove(struct platform_device *dev)
491 struct timbuart_port *uart = platform_get_drvdata(dev);
493 tasklet_kill(&uart->tasklet);
494 uart_remove_one_port(&timbuart_driver, &uart->port);
495 uart_unregister_driver(&timbuart_driver);
496 kfree(uart);
498 return 0;
501 static struct platform_driver timbuart_platform_driver = {
502 .driver = {
503 .name = "timb-uart",
504 .owner = THIS_MODULE,
506 .probe = timbuart_probe,
507 .remove = timbuart_remove,
510 /*--------------------------------------------------------------------------*/
512 static int __init timbuart_init(void)
514 return platform_driver_register(&timbuart_platform_driver);
517 static void __exit timbuart_exit(void)
519 platform_driver_unregister(&timbuart_platform_driver);
522 module_init(timbuart_init);
523 module_exit(timbuart_exit);
525 MODULE_DESCRIPTION("Timberdale UART driver");
526 MODULE_LICENSE("GPL v2");
527 MODULE_ALIAS("platform:timb-uart");