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.
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>
32 struct timbuart_port {
33 struct uart_port port;
34 struct tasklet_struct tasklet;
37 struct platform_device *dev;
40 static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
41 921600, 1843200, 3250000};
43 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
45 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
47 static void timbuart_stop_rx(struct uart_port *port)
49 /* spin lock held by upper layer, disable all RX interrupts */
50 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
51 iowrite32(ier, port->membase + TIMBUART_IER);
54 static void timbuart_stop_tx(struct uart_port *port)
56 /* spinlock held by upper layer, disable TX interrupt */
57 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
58 iowrite32(ier, port->membase + TIMBUART_IER);
61 static void timbuart_start_tx(struct uart_port *port)
63 struct timbuart_port *uart =
64 container_of(port, struct timbuart_port, port);
66 /* do not transfer anything here -> fire off the tasklet */
67 tasklet_schedule(&uart->tasklet);
70 static void timbuart_flush_buffer(struct uart_port *port)
72 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) | TIMBUART_CTRL_FLSHTX;
74 iowrite8(ctl, port->membase + TIMBUART_CTRL);
75 iowrite32(TXBF, port->membase + TIMBUART_ISR);
78 static void timbuart_rx_chars(struct uart_port *port)
80 struct tty_struct *tty = port->info->port.tty;
82 while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
83 u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
85 tty_insert_flip_char(tty, ch, TTY_NORMAL);
88 spin_unlock(&port->lock);
89 tty_flip_buffer_push(port->info->port.tty);
90 spin_lock(&port->lock);
92 dev_dbg(port->dev, "%s - total read %d bytes\n",
93 __func__, port->icount.rx);
96 static void timbuart_tx_chars(struct uart_port *port)
98 struct circ_buf *xmit = &port->info->xmit;
100 while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
101 !uart_circ_empty(xmit)) {
102 iowrite8(xmit->buf[xmit->tail],
103 port->membase + TIMBUART_TXFIFO);
104 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
109 "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
112 ioread8(port->membase + TIMBUART_CTRL),
113 port->mctrl & TIOCM_RTS,
114 ioread8(port->membase + TIMBUART_BAUDRATE));
117 static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
119 struct timbuart_port *uart =
120 container_of(port, struct timbuart_port, port);
121 struct circ_buf *xmit = &port->info->xmit;
123 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
130 timbuart_tx_chars(port);
131 /* clear all TX interrupts */
132 iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
134 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
135 uart_write_wakeup(port);
137 /* Re-enable any tx interrupt */
138 *ier |= uart->last_ier & TXFLAGS;
140 /* enable interrupts if there are chars in the transmit buffer,
141 * Or if we delivered some bytes and want the almost empty interrupt
142 * we wake up the upper layer later when we got the interrupt
143 * to give it some time to go out...
145 if (!uart_circ_empty(xmit))
148 dev_dbg(port->dev, "%s - leaving\n", __func__);
151 void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
154 /* Some RX status is set */
156 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
157 TIMBUART_CTRL_FLSHRX;
158 iowrite8(ctl, port->membase + TIMBUART_CTRL);
159 port->icount.overrun++;
160 } else if (isr & (RXDP))
161 timbuart_rx_chars(port);
163 /* ack all RX interrupts */
164 iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
167 /* always have the RX interrupts enabled */
168 *ier |= RXBAF | RXBF | RXTT;
170 dev_dbg(port->dev, "%s - leaving\n", __func__);
173 void timbuart_tasklet(unsigned long arg)
175 struct timbuart_port *uart = (struct timbuart_port *)arg;
178 spin_lock(&uart->port.lock);
180 isr = ioread32(uart->port.membase + TIMBUART_ISR);
181 dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
184 timbuart_handle_tx_port(&uart->port, isr, &ier);
186 timbuart_mctrl_check(&uart->port, isr, &ier);
189 timbuart_handle_rx_port(&uart->port, isr, &ier);
191 iowrite32(ier, uart->port.membase + TIMBUART_IER);
193 spin_unlock(&uart->port.lock);
194 dev_dbg(uart->port.dev, "%s leaving\n", __func__);
197 static unsigned int timbuart_tx_empty(struct uart_port *port)
199 u32 isr = ioread32(port->membase + TIMBUART_ISR);
201 return (isr & TXBE) ? TIOCSER_TEMT : 0;
204 static unsigned int timbuart_get_mctrl(struct uart_port *port)
206 u8 cts = ioread8(port->membase + TIMBUART_CTRL);
207 dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
209 if (cts & TIMBUART_CTRL_CTS)
210 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
212 return TIOCM_DSR | TIOCM_CAR;
215 static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
217 dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
219 if (mctrl & TIOCM_RTS)
220 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
222 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
225 static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
229 if (isr & CTS_DELTA) {
231 iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
232 cts = timbuart_get_mctrl(port);
233 uart_handle_cts_change(port, cts & TIOCM_CTS);
234 wake_up_interruptible(&port->info->delta_msr_wait);
240 static void timbuart_enable_ms(struct uart_port *port)
245 static void timbuart_break_ctl(struct uart_port *port, int ctl)
250 static int timbuart_startup(struct uart_port *port)
252 struct timbuart_port *uart =
253 container_of(port, struct timbuart_port, port);
255 dev_dbg(port->dev, "%s\n", __func__);
257 iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
258 iowrite32(0x1ff, port->membase + TIMBUART_ISR);
259 /* Enable all but TX interrupts */
260 iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
261 port->membase + TIMBUART_IER);
263 return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
267 static void timbuart_shutdown(struct uart_port *port)
269 struct timbuart_port *uart =
270 container_of(port, struct timbuart_port, port);
271 dev_dbg(port->dev, "%s\n", __func__);
272 free_irq(port->irq, uart);
273 iowrite32(0, port->membase + TIMBUART_IER);
276 static int get_bindex(int baud)
280 for (i = 0; i < ARRAY_SIZE(baudrates); i++)
281 if (baud <= baudrates[i])
287 static void timbuart_set_termios(struct uart_port *port,
288 struct ktermios *termios,
289 struct ktermios *old)
295 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
296 bindex = get_bindex(baud);
297 dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
301 baud = baudrates[bindex];
303 /* The serial layer calls into this once with old = NULL when setting
306 tty_termios_copy_hw(termios, old);
307 tty_termios_encode_baud_rate(termios, baud, baud);
309 spin_lock_irqsave(&port->lock, flags);
310 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
311 uart_update_timeout(port, termios->c_cflag, baud);
312 spin_unlock_irqrestore(&port->lock, flags);
315 static const char *timbuart_type(struct uart_port *port)
317 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
320 /* We do not request/release mappings of the registers here,
321 * currently it's done in the proble function.
323 static void timbuart_release_port(struct uart_port *port)
325 struct platform_device *pdev = to_platform_device(port->dev);
327 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
329 if (port->flags & UPF_IOREMAP) {
330 iounmap(port->membase);
331 port->membase = NULL;
334 release_mem_region(port->mapbase, size);
337 static int timbuart_request_port(struct uart_port *port)
339 struct platform_device *pdev = to_platform_device(port->dev);
341 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
343 if (!request_mem_region(port->mapbase, size, "timb-uart"))
346 if (port->flags & UPF_IOREMAP) {
347 port->membase = ioremap(port->mapbase, size);
348 if (port->membase == NULL) {
349 release_mem_region(port->mapbase, size);
357 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
359 struct timbuart_port *uart = (struct timbuart_port *)devid;
361 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
362 uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
364 /* disable interrupts, the tasklet enables them again */
365 iowrite32(0, uart->port.membase + TIMBUART_IER);
367 /* fire off bottom half */
368 tasklet_schedule(&uart->tasklet);
376 * Configure/autoconfigure the port.
378 static void timbuart_config_port(struct uart_port *port, int flags)
380 if (flags & UART_CONFIG_TYPE) {
381 port->type = PORT_TIMBUART;
382 timbuart_request_port(port);
386 static int timbuart_verify_port(struct uart_port *port,
387 struct serial_struct *ser)
389 /* we don't want the core code to modify any port params */
393 static struct uart_ops timbuart_ops = {
394 .tx_empty = timbuart_tx_empty,
395 .set_mctrl = timbuart_set_mctrl,
396 .get_mctrl = timbuart_get_mctrl,
397 .stop_tx = timbuart_stop_tx,
398 .start_tx = timbuart_start_tx,
399 .flush_buffer = timbuart_flush_buffer,
400 .stop_rx = timbuart_stop_rx,
401 .enable_ms = timbuart_enable_ms,
402 .break_ctl = timbuart_break_ctl,
403 .startup = timbuart_startup,
404 .shutdown = timbuart_shutdown,
405 .set_termios = timbuart_set_termios,
406 .type = timbuart_type,
407 .release_port = timbuart_release_port,
408 .request_port = timbuart_request_port,
409 .config_port = timbuart_config_port,
410 .verify_port = timbuart_verify_port
413 static struct uart_driver timbuart_driver = {
414 .owner = THIS_MODULE,
415 .driver_name = "timberdale_uart",
417 .major = TIMBUART_MAJOR,
418 .minor = TIMBUART_MINOR,
422 static int timbuart_probe(struct platform_device *dev)
425 struct timbuart_port *uart;
426 struct resource *iomem;
428 dev_dbg(&dev->dev, "%s\n", __func__);
430 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
438 uart->port.uartclk = 3250000 * 16;
439 uart->port.fifosize = TIMBUART_FIFO_SIZE;
440 uart->port.regshift = 2;
441 uart->port.iotype = UPIO_MEM;
442 uart->port.ops = &timbuart_ops;
444 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
446 uart->port.dev = &dev->dev;
448 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
453 uart->port.mapbase = iomem->start;
454 uart->port.membase = NULL;
456 uart->port.irq = platform_get_irq(dev, 0);
457 if (uart->port.irq < 0) {
462 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
464 err = uart_register_driver(&timbuart_driver);
468 err = uart_add_one_port(&timbuart_driver, &uart->port);
472 platform_set_drvdata(dev, uart);
477 uart_unregister_driver(&timbuart_driver);
481 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
487 static int timbuart_remove(struct platform_device *dev)
489 struct timbuart_port *uart = platform_get_drvdata(dev);
491 tasklet_kill(&uart->tasklet);
492 uart_remove_one_port(&timbuart_driver, &uart->port);
493 uart_unregister_driver(&timbuart_driver);
499 static struct platform_driver timbuart_platform_driver = {
502 .owner = THIS_MODULE,
504 .probe = timbuart_probe,
505 .remove = timbuart_remove,
508 /*--------------------------------------------------------------------------*/
510 static int __init timbuart_init(void)
512 return platform_driver_register(&timbuart_platform_driver);
515 static void __exit timbuart_exit(void)
517 platform_driver_unregister(&timbuart_platform_driver);
520 module_init(timbuart_init);
521 module_exit(timbuart_exit);
523 MODULE_DESCRIPTION("Timberdale UART driver");
524 MODULE_LICENSE("GPL v2");
525 MODULE_ALIAS("platform:timb-uart");