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, u8 isr, u8 *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 u8 ier = ioread8(port->membase + TIMBUART_IER) & ~RXFLAGS;
51 iowrite8(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 u8 ier = ioread8(port->membase + TIMBUART_IER) & ~TXBAE;
58 iowrite8(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 iowrite8(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 (ioread8(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 (!(ioread8(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, u8 isr, u8 *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 iowrite8(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, u8 isr, u8 *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 iowrite8(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 = ioread8(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 iowrite8(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 u8 isr = ioread8(port->membase + TIMBUART_ISR);
201 return (isr & TXBAE) ? 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, u8 isr, u8 *ier)
229 if (isr & CTS_DELTA) {
231 iowrite8(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 iowrite8(0xff, port->membase + TIMBUART_ISR);
259 /* Enable all but TX interrupts */
260 iowrite8(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 iowrite8(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);
300 printk(KERN_ALERT "timbuart: Unsupported baud rate\n");
302 spin_lock_irqsave(&port->lock, flags);
303 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
304 uart_update_timeout(port, termios->c_cflag, baud);
305 spin_unlock_irqrestore(&port->lock, flags);
309 static const char *timbuart_type(struct uart_port *port)
311 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
314 /* We do not request/release mappings of the registers here,
315 * currently it's done in the proble function.
317 static void timbuart_release_port(struct uart_port *port)
319 struct platform_device *pdev = to_platform_device(port->dev);
321 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
323 if (port->flags & UPF_IOREMAP) {
324 iounmap(port->membase);
325 port->membase = NULL;
328 release_mem_region(port->mapbase, size);
331 static int timbuart_request_port(struct uart_port *port)
333 struct platform_device *pdev = to_platform_device(port->dev);
335 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
337 if (!request_mem_region(port->mapbase, size, "timb-uart"))
340 if (port->flags & UPF_IOREMAP) {
341 port->membase = ioremap(port->mapbase, size);
342 if (port->membase == NULL) {
343 release_mem_region(port->mapbase, size);
351 static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
353 struct timbuart_port *uart = (struct timbuart_port *)devid;
355 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
356 uart->last_ier = ioread8(uart->port.membase + TIMBUART_IER);
358 /* disable interrupts, the tasklet enables them again */
359 iowrite8(0, uart->port.membase + TIMBUART_IER);
361 /* fire off bottom half */
362 tasklet_schedule(&uart->tasklet);
370 * Configure/autoconfigure the port.
372 static void timbuart_config_port(struct uart_port *port, int flags)
374 if (flags & UART_CONFIG_TYPE) {
375 port->type = PORT_TIMBUART;
376 timbuart_request_port(port);
380 static int timbuart_verify_port(struct uart_port *port,
381 struct serial_struct *ser)
383 /* we don't want the core code to modify any port params */
387 static struct uart_ops timbuart_ops = {
388 .tx_empty = timbuart_tx_empty,
389 .set_mctrl = timbuart_set_mctrl,
390 .get_mctrl = timbuart_get_mctrl,
391 .stop_tx = timbuart_stop_tx,
392 .start_tx = timbuart_start_tx,
393 .flush_buffer = timbuart_flush_buffer,
394 .stop_rx = timbuart_stop_rx,
395 .enable_ms = timbuart_enable_ms,
396 .break_ctl = timbuart_break_ctl,
397 .startup = timbuart_startup,
398 .shutdown = timbuart_shutdown,
399 .set_termios = timbuart_set_termios,
400 .type = timbuart_type,
401 .release_port = timbuart_release_port,
402 .request_port = timbuart_request_port,
403 .config_port = timbuart_config_port,
404 .verify_port = timbuart_verify_port
407 static struct uart_driver timbuart_driver = {
408 .owner = THIS_MODULE,
409 .driver_name = "timberdale_uart",
411 .major = TIMBUART_MAJOR,
412 .minor = TIMBUART_MINOR,
416 static int timbuart_probe(struct platform_device *dev)
419 struct timbuart_port *uart;
420 struct resource *iomem;
422 dev_dbg(&dev->dev, "%s\n", __func__);
424 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
432 uart->port.uartclk = 3250000 * 16;
433 uart->port.fifosize = TIMBUART_FIFO_SIZE;
434 uart->port.regshift = 2;
435 uart->port.iotype = UPIO_MEM;
436 uart->port.ops = &timbuart_ops;
438 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
440 uart->port.dev = &dev->dev;
442 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
447 uart->port.mapbase = iomem->start;
448 uart->port.membase = NULL;
450 uart->port.irq = platform_get_irq(dev, 0);
451 if (uart->port.irq < 0) {
456 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
458 err = uart_register_driver(&timbuart_driver);
462 err = uart_add_one_port(&timbuart_driver, &uart->port);
466 platform_set_drvdata(dev, uart);
471 uart_unregister_driver(&timbuart_driver);
475 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
481 static int timbuart_remove(struct platform_device *dev)
483 struct timbuart_port *uart = platform_get_drvdata(dev);
485 tasklet_kill(&uart->tasklet);
486 uart_remove_one_port(&timbuart_driver, &uart->port);
487 uart_unregister_driver(&timbuart_driver);
493 static struct platform_driver timbuart_platform_driver = {
496 .owner = THIS_MODULE,
498 .probe = timbuart_probe,
499 .remove = timbuart_remove,
502 /*--------------------------------------------------------------------------*/
504 static int __init timbuart_init(void)
506 return platform_driver_register(&timbuart_platform_driver);
509 static void __exit timbuart_exit(void)
511 platform_driver_unregister(&timbuart_platform_driver);
514 module_init(timbuart_init);
515 module_exit(timbuart_exit);
517 MODULE_DESCRIPTION("Timberdale UART driver");
518 MODULE_LICENSE("GPL v2");
519 MODULE_ALIAS("platform:timb-uart");