Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / tty / serial / timbuart.c
blobe213535c9ede837354b5290fc953ff4812209446
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>
30 #include <linux/module.h>
32 #include "timbuart.h"
34 struct timbuart_port {
35 struct uart_port port;
36 struct tasklet_struct tasklet;
37 int usedma;
38 u32 last_ier;
39 struct platform_device *dev;
42 static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
43 921600, 1843200, 3250000};
45 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
47 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
49 static void timbuart_stop_rx(struct uart_port *port)
51 /* spin lock held by upper layer, disable all RX interrupts */
52 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
53 iowrite32(ier, port->membase + TIMBUART_IER);
56 static void timbuart_stop_tx(struct uart_port *port)
58 /* spinlock held by upper layer, disable TX interrupt */
59 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
60 iowrite32(ier, port->membase + TIMBUART_IER);
63 static void timbuart_start_tx(struct uart_port *port)
65 struct timbuart_port *uart =
66 container_of(port, struct timbuart_port, port);
68 /* do not transfer anything here -> fire off the tasklet */
69 tasklet_schedule(&uart->tasklet);
72 static unsigned int timbuart_tx_empty(struct uart_port *port)
74 u32 isr = ioread32(port->membase + TIMBUART_ISR);
76 return (isr & TXBE) ? TIOCSER_TEMT : 0;
79 static void timbuart_flush_buffer(struct uart_port *port)
81 if (!timbuart_tx_empty(port)) {
82 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
83 TIMBUART_CTRL_FLSHTX;
85 iowrite8(ctl, port->membase + TIMBUART_CTRL);
86 iowrite32(TXBF, port->membase + TIMBUART_ISR);
90 static void timbuart_rx_chars(struct uart_port *port)
92 struct tty_struct *tty = port->state->port.tty;
94 while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
95 u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
96 port->icount.rx++;
97 tty_insert_flip_char(tty, ch, TTY_NORMAL);
100 spin_unlock(&port->lock);
101 tty_flip_buffer_push(port->state->port.tty);
102 spin_lock(&port->lock);
104 dev_dbg(port->dev, "%s - total read %d bytes\n",
105 __func__, port->icount.rx);
108 static void timbuart_tx_chars(struct uart_port *port)
110 struct circ_buf *xmit = &port->state->xmit;
112 while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
113 !uart_circ_empty(xmit)) {
114 iowrite8(xmit->buf[xmit->tail],
115 port->membase + TIMBUART_TXFIFO);
116 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
117 port->icount.tx++;
120 dev_dbg(port->dev,
121 "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
122 __func__,
123 port->icount.tx,
124 ioread8(port->membase + TIMBUART_CTRL),
125 port->mctrl & TIOCM_RTS,
126 ioread8(port->membase + TIMBUART_BAUDRATE));
129 static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
131 struct timbuart_port *uart =
132 container_of(port, struct timbuart_port, port);
133 struct circ_buf *xmit = &port->state->xmit;
135 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
136 return;
138 if (port->x_char)
139 return;
141 if (isr & TXFLAGS) {
142 timbuart_tx_chars(port);
143 /* clear all TX interrupts */
144 iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
146 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
147 uart_write_wakeup(port);
148 } else
149 /* Re-enable any tx interrupt */
150 *ier |= uart->last_ier & TXFLAGS;
152 /* enable interrupts if there are chars in the transmit buffer,
153 * Or if we delivered some bytes and want the almost empty interrupt
154 * we wake up the upper layer later when we got the interrupt
155 * to give it some time to go out...
157 if (!uart_circ_empty(xmit))
158 *ier |= TXBAE;
160 dev_dbg(port->dev, "%s - leaving\n", __func__);
163 void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
165 if (isr & RXFLAGS) {
166 /* Some RX status is set */
167 if (isr & RXBF) {
168 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
169 TIMBUART_CTRL_FLSHRX;
170 iowrite8(ctl, port->membase + TIMBUART_CTRL);
171 port->icount.overrun++;
172 } else if (isr & (RXDP))
173 timbuart_rx_chars(port);
175 /* ack all RX interrupts */
176 iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
179 /* always have the RX interrupts enabled */
180 *ier |= RXBAF | RXBF | RXTT;
182 dev_dbg(port->dev, "%s - leaving\n", __func__);
185 void timbuart_tasklet(unsigned long arg)
187 struct timbuart_port *uart = (struct timbuart_port *)arg;
188 u32 isr, ier = 0;
190 spin_lock(&uart->port.lock);
192 isr = ioread32(uart->port.membase + TIMBUART_ISR);
193 dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
195 if (!uart->usedma)
196 timbuart_handle_tx_port(&uart->port, isr, &ier);
198 timbuart_mctrl_check(&uart->port, isr, &ier);
200 if (!uart->usedma)
201 timbuart_handle_rx_port(&uart->port, isr, &ier);
203 iowrite32(ier, uart->port.membase + TIMBUART_IER);
205 spin_unlock(&uart->port.lock);
206 dev_dbg(uart->port.dev, "%s leaving\n", __func__);
209 static unsigned int timbuart_get_mctrl(struct uart_port *port)
211 u8 cts = ioread8(port->membase + TIMBUART_CTRL);
212 dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
214 if (cts & TIMBUART_CTRL_CTS)
215 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
216 else
217 return TIOCM_DSR | TIOCM_CAR;
220 static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
222 dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
224 if (mctrl & TIOCM_RTS)
225 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
226 else
227 iowrite8(0, port->membase + TIMBUART_CTRL);
230 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
232 unsigned int cts;
234 if (isr & CTS_DELTA) {
235 /* ack */
236 iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
237 cts = timbuart_get_mctrl(port);
238 uart_handle_cts_change(port, cts & TIOCM_CTS);
239 wake_up_interruptible(&port->state->port.delta_msr_wait);
242 *ier |= CTS_DELTA;
245 static void timbuart_enable_ms(struct uart_port *port)
247 /* N/A */
250 static void timbuart_break_ctl(struct uart_port *port, int ctl)
252 /* N/A */
255 static int timbuart_startup(struct uart_port *port)
257 struct timbuart_port *uart =
258 container_of(port, struct timbuart_port, port);
260 dev_dbg(port->dev, "%s\n", __func__);
262 iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
263 iowrite32(0x1ff, port->membase + TIMBUART_ISR);
264 /* Enable all but TX interrupts */
265 iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
266 port->membase + TIMBUART_IER);
268 return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
269 "timb-uart", uart);
272 static void timbuart_shutdown(struct uart_port *port)
274 struct timbuart_port *uart =
275 container_of(port, struct timbuart_port, port);
276 dev_dbg(port->dev, "%s\n", __func__);
277 free_irq(port->irq, uart);
278 iowrite32(0, port->membase + TIMBUART_IER);
281 static int get_bindex(int baud)
283 int i;
285 for (i = 0; i < ARRAY_SIZE(baudrates); i++)
286 if (baud <= baudrates[i])
287 return i;
289 return -1;
292 static void timbuart_set_termios(struct uart_port *port,
293 struct ktermios *termios,
294 struct ktermios *old)
296 unsigned int baud;
297 short bindex;
298 unsigned long flags;
300 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
301 bindex = get_bindex(baud);
302 dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
304 if (bindex < 0)
305 bindex = 0;
306 baud = baudrates[bindex];
308 /* The serial layer calls into this once with old = NULL when setting
309 up initially */
310 if (old)
311 tty_termios_copy_hw(termios, old);
312 tty_termios_encode_baud_rate(termios, baud, baud);
314 spin_lock_irqsave(&port->lock, flags);
315 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
316 uart_update_timeout(port, termios->c_cflag, baud);
317 spin_unlock_irqrestore(&port->lock, flags);
320 static const char *timbuart_type(struct uart_port *port)
322 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
325 /* We do not request/release mappings of the registers here,
326 * currently it's done in the proble function.
328 static void timbuart_release_port(struct uart_port *port)
330 struct platform_device *pdev = to_platform_device(port->dev);
331 int size =
332 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
334 if (port->flags & UPF_IOREMAP) {
335 iounmap(port->membase);
336 port->membase = NULL;
339 release_mem_region(port->mapbase, size);
342 static int timbuart_request_port(struct uart_port *port)
344 struct platform_device *pdev = to_platform_device(port->dev);
345 int size =
346 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
348 if (!request_mem_region(port->mapbase, size, "timb-uart"))
349 return -EBUSY;
351 if (port->flags & UPF_IOREMAP) {
352 port->membase = ioremap(port->mapbase, size);
353 if (port->membase == NULL) {
354 release_mem_region(port->mapbase, size);
355 return -ENOMEM;
359 return 0;
362 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
364 struct timbuart_port *uart = (struct timbuart_port *)devid;
366 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
367 uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
369 /* disable interrupts, the tasklet enables them again */
370 iowrite32(0, uart->port.membase + TIMBUART_IER);
372 /* fire off bottom half */
373 tasklet_schedule(&uart->tasklet);
375 return IRQ_HANDLED;
376 } else
377 return IRQ_NONE;
381 * Configure/autoconfigure the port.
383 static void timbuart_config_port(struct uart_port *port, int flags)
385 if (flags & UART_CONFIG_TYPE) {
386 port->type = PORT_TIMBUART;
387 timbuart_request_port(port);
391 static int timbuart_verify_port(struct uart_port *port,
392 struct serial_struct *ser)
394 /* we don't want the core code to modify any port params */
395 return -EINVAL;
398 static struct uart_ops timbuart_ops = {
399 .tx_empty = timbuart_tx_empty,
400 .set_mctrl = timbuart_set_mctrl,
401 .get_mctrl = timbuart_get_mctrl,
402 .stop_tx = timbuart_stop_tx,
403 .start_tx = timbuart_start_tx,
404 .flush_buffer = timbuart_flush_buffer,
405 .stop_rx = timbuart_stop_rx,
406 .enable_ms = timbuart_enable_ms,
407 .break_ctl = timbuart_break_ctl,
408 .startup = timbuart_startup,
409 .shutdown = timbuart_shutdown,
410 .set_termios = timbuart_set_termios,
411 .type = timbuart_type,
412 .release_port = timbuart_release_port,
413 .request_port = timbuart_request_port,
414 .config_port = timbuart_config_port,
415 .verify_port = timbuart_verify_port
418 static struct uart_driver timbuart_driver = {
419 .owner = THIS_MODULE,
420 .driver_name = "timberdale_uart",
421 .dev_name = "ttyTU",
422 .major = TIMBUART_MAJOR,
423 .minor = TIMBUART_MINOR,
424 .nr = 1
427 static int __devinit timbuart_probe(struct platform_device *dev)
429 int err, irq;
430 struct timbuart_port *uart;
431 struct resource *iomem;
433 dev_dbg(&dev->dev, "%s\n", __func__);
435 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
436 if (!uart) {
437 err = -EINVAL;
438 goto err_mem;
441 uart->usedma = 0;
443 uart->port.uartclk = 3250000 * 16;
444 uart->port.fifosize = TIMBUART_FIFO_SIZE;
445 uart->port.regshift = 2;
446 uart->port.iotype = UPIO_MEM;
447 uart->port.ops = &timbuart_ops;
448 uart->port.irq = 0;
449 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
450 uart->port.line = 0;
451 uart->port.dev = &dev->dev;
453 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
454 if (!iomem) {
455 err = -ENOMEM;
456 goto err_register;
458 uart->port.mapbase = iomem->start;
459 uart->port.membase = NULL;
461 irq = platform_get_irq(dev, 0);
462 if (irq < 0) {
463 err = -EINVAL;
464 goto err_register;
466 uart->port.irq = irq;
468 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
470 err = uart_register_driver(&timbuart_driver);
471 if (err)
472 goto err_register;
474 err = uart_add_one_port(&timbuart_driver, &uart->port);
475 if (err)
476 goto err_add_port;
478 platform_set_drvdata(dev, uart);
480 return 0;
482 err_add_port:
483 uart_unregister_driver(&timbuart_driver);
484 err_register:
485 kfree(uart);
486 err_mem:
487 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
488 err);
490 return err;
493 static int __devexit timbuart_remove(struct platform_device *dev)
495 struct timbuart_port *uart = platform_get_drvdata(dev);
497 tasklet_kill(&uart->tasklet);
498 uart_remove_one_port(&timbuart_driver, &uart->port);
499 uart_unregister_driver(&timbuart_driver);
500 kfree(uart);
502 return 0;
505 static struct platform_driver timbuart_platform_driver = {
506 .driver = {
507 .name = "timb-uart",
508 .owner = THIS_MODULE,
510 .probe = timbuart_probe,
511 .remove = __devexit_p(timbuart_remove),
514 /*--------------------------------------------------------------------------*/
516 static int __init timbuart_init(void)
518 return platform_driver_register(&timbuart_platform_driver);
521 static void __exit timbuart_exit(void)
523 platform_driver_unregister(&timbuart_platform_driver);
526 module_init(timbuart_init);
527 module_exit(timbuart_exit);
529 MODULE_DESCRIPTION("Timberdale UART driver");
530 MODULE_LICENSE("GPL v2");
531 MODULE_ALIAS("platform:timb-uart");