diff options
author | Richard Röjfors <richard.rojfors.ext@mocean-labs.com> | 2009-06-11 09:05:39 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-06-11 11:51:06 -0400 |
commit | 34aec591847c696339189b070cce2a11f901cfea (patch) | |
tree | b371a780186becf6daba7c229cb74821b020a879 /drivers/serial/timbuart.c | |
parent | e960bf73ddd73714bcfbadb1717e53ecda9924cb (diff) |
serial: Added Timberdale UART driver
Driver for the UART found in the Timberdale FPGA
Signed-off-by: Richard Röjfors <richard.rojfors.ext@mocean-labs.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/serial/timbuart.c')
-rw-r--r-- | drivers/serial/timbuart.c | 520 |
1 files changed, 520 insertions, 0 deletions
diff --git a/drivers/serial/timbuart.c b/drivers/serial/timbuart.c new file mode 100644 index 000000000000..30ba3c4cc180 --- /dev/null +++ b/drivers/serial/timbuart.c | |||
@@ -0,0 +1,520 @@ | |||
1 | /* | ||
2 | * timbuart.c timberdale FPGA UART driver | ||
3 | * Copyright (c) 2009 Intel Corporation | ||
4 | * | ||
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. | ||
8 | * | ||
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. | ||
13 | * | ||
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. | ||
17 | */ | ||
18 | |||
19 | /* Supports: | ||
20 | * Timberdale FPGA UART | ||
21 | */ | ||
22 | |||
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 | |||
30 | #include "timbuart.h" | ||
31 | |||
32 | struct timbuart_port { | ||
33 | struct uart_port port; | ||
34 | struct tasklet_struct tasklet; | ||
35 | int usedma; | ||
36 | u8 last_ier; | ||
37 | struct platform_device *dev; | ||
38 | }; | ||
39 | |||
40 | static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800, | ||
41 | 921600, 1843200, 3250000}; | ||
42 | |||
43 | static void timbuart_mctrl_check(struct uart_port *port, u8 isr, u8 *ier); | ||
44 | |||
45 | static irqreturn_t timbuart_handleinterrupt(int irq, void *devid); | ||
46 | |||
47 | static void timbuart_stop_rx(struct uart_port *port) | ||
48 | { | ||
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); | ||
52 | } | ||
53 | |||
54 | static void timbuart_stop_tx(struct uart_port *port) | ||
55 | { | ||
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); | ||
59 | } | ||
60 | |||
61 | static void timbuart_start_tx(struct uart_port *port) | ||
62 | { | ||
63 | struct timbuart_port *uart = | ||
64 | container_of(port, struct timbuart_port, port); | ||
65 | |||
66 | /* do not transfer anything here -> fire off the tasklet */ | ||
67 | tasklet_schedule(&uart->tasklet); | ||
68 | } | ||
69 | |||
70 | static void timbuart_flush_buffer(struct uart_port *port) | ||
71 | { | ||
72 | u8 ctl = ioread8(port->membase + TIMBUART_CTRL) | TIMBUART_CTRL_FLSHTX; | ||
73 | |||
74 | iowrite8(ctl, port->membase + TIMBUART_CTRL); | ||
75 | iowrite8(TXBF, port->membase + TIMBUART_ISR); | ||
76 | } | ||
77 | |||
78 | static void timbuart_rx_chars(struct uart_port *port) | ||
79 | { | ||
80 | struct tty_struct *tty = port->info->port.tty; | ||
81 | |||
82 | while (ioread8(port->membase + TIMBUART_ISR) & RXDP) { | ||
83 | u8 ch = ioread8(port->membase + TIMBUART_RXFIFO); | ||
84 | port->icount.rx++; | ||
85 | tty_insert_flip_char(tty, ch, TTY_NORMAL); | ||
86 | } | ||
87 | |||
88 | spin_unlock(&port->lock); | ||
89 | tty_flip_buffer_push(port->info->port.tty); | ||
90 | spin_lock(&port->lock); | ||
91 | |||
92 | dev_dbg(port->dev, "%s - total read %d bytes\n", | ||
93 | __func__, port->icount.rx); | ||
94 | } | ||
95 | |||
96 | static void timbuart_tx_chars(struct uart_port *port) | ||
97 | { | ||
98 | struct circ_buf *xmit = &port->info->xmit; | ||
99 | |||
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); | ||
105 | port->icount.tx++; | ||
106 | } | ||
107 | |||
108 | dev_dbg(port->dev, | ||
109 | "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n", | ||
110 | __func__, | ||
111 | port->icount.tx, | ||
112 | ioread8(port->membase + TIMBUART_CTRL), | ||
113 | port->mctrl & TIOCM_RTS, | ||
114 | ioread8(port->membase + TIMBUART_BAUDRATE)); | ||
115 | } | ||
116 | |||
117 | static void timbuart_handle_tx_port(struct uart_port *port, u8 isr, u8 *ier) | ||
118 | { | ||
119 | struct timbuart_port *uart = | ||
120 | container_of(port, struct timbuart_port, port); | ||
121 | struct circ_buf *xmit = &port->info->xmit; | ||
122 | |||
123 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) | ||
124 | return; | ||
125 | |||
126 | if (port->x_char) | ||
127 | return; | ||
128 | |||
129 | if (isr & TXFLAGS) { | ||
130 | timbuart_tx_chars(port); | ||
131 | /* clear all TX interrupts */ | ||
132 | iowrite8(TXFLAGS, port->membase + TIMBUART_ISR); | ||
133 | |||
134 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | ||
135 | uart_write_wakeup(port); | ||
136 | } else | ||
137 | /* Re-enable any tx interrupt */ | ||
138 | *ier |= uart->last_ier & TXFLAGS; | ||
139 | |||
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... | ||
144 | */ | ||
145 | if (!uart_circ_empty(xmit)) | ||
146 | *ier |= TXBAE; | ||
147 | |||
148 | dev_dbg(port->dev, "%s - leaving\n", __func__); | ||
149 | } | ||
150 | |||
151 | void timbuart_handle_rx_port(struct uart_port *port, u8 isr, u8 *ier) | ||
152 | { | ||
153 | if (isr & RXFLAGS) { | ||
154 | /* Some RX status is set */ | ||
155 | if (isr & RXBF) { | ||
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); | ||
162 | |||
163 | /* ack all RX interrupts */ | ||
164 | iowrite8(RXFLAGS, port->membase + TIMBUART_ISR); | ||
165 | } | ||
166 | |||
167 | /* always have the RX interrupts enabled */ | ||
168 | *ier |= RXBAF | RXBF | RXTT; | ||
169 | |||
170 | dev_dbg(port->dev, "%s - leaving\n", __func__); | ||
171 | } | ||
172 | |||
173 | void timbuart_tasklet(unsigned long arg) | ||
174 | { | ||
175 | struct timbuart_port *uart = (struct timbuart_port *)arg; | ||
176 | u8 isr, ier = 0; | ||
177 | |||
178 | spin_lock(&uart->port.lock); | ||
179 | |||
180 | isr = ioread8(uart->port.membase + TIMBUART_ISR); | ||
181 | dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr); | ||
182 | |||
183 | if (!uart->usedma) | ||
184 | timbuart_handle_tx_port(&uart->port, isr, &ier); | ||
185 | |||
186 | timbuart_mctrl_check(&uart->port, isr, &ier); | ||
187 | |||
188 | if (!uart->usedma) | ||
189 | timbuart_handle_rx_port(&uart->port, isr, &ier); | ||
190 | |||
191 | iowrite8(ier, uart->port.membase + TIMBUART_IER); | ||
192 | |||
193 | spin_unlock(&uart->port.lock); | ||
194 | dev_dbg(uart->port.dev, "%s leaving\n", __func__); | ||
195 | } | ||
196 | |||
197 | static unsigned int timbuart_tx_empty(struct uart_port *port) | ||
198 | { | ||
199 | u8 isr = ioread8(port->membase + TIMBUART_ISR); | ||
200 | |||
201 | return (isr & TXBAE) ? TIOCSER_TEMT : 0; | ||
202 | } | ||
203 | |||
204 | static unsigned int timbuart_get_mctrl(struct uart_port *port) | ||
205 | { | ||
206 | u8 cts = ioread8(port->membase + TIMBUART_CTRL); | ||
207 | dev_dbg(port->dev, "%s - cts %x\n", __func__, cts); | ||
208 | |||
209 | if (cts & TIMBUART_CTRL_CTS) | ||
210 | return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; | ||
211 | else | ||
212 | return TIOCM_DSR | TIOCM_CAR; | ||
213 | } | ||
214 | |||
215 | static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl) | ||
216 | { | ||
217 | dev_dbg(port->dev, "%s - %x\n", __func__, mctrl); | ||
218 | |||
219 | if (mctrl & TIOCM_RTS) | ||
220 | iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL); | ||
221 | else | ||
222 | iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL); | ||
223 | } | ||
224 | |||
225 | static void timbuart_mctrl_check(struct uart_port *port, u8 isr, u8 *ier) | ||
226 | { | ||
227 | unsigned int cts; | ||
228 | |||
229 | if (isr & CTS_DELTA) { | ||
230 | /* ack */ | ||
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); | ||
235 | } | ||
236 | |||
237 | *ier |= CTS_DELTA; | ||
238 | } | ||
239 | |||
240 | static void timbuart_enable_ms(struct uart_port *port) | ||
241 | { | ||
242 | /* N/A */ | ||
243 | } | ||
244 | |||
245 | static void timbuart_break_ctl(struct uart_port *port, int ctl) | ||
246 | { | ||
247 | /* N/A */ | ||
248 | } | ||
249 | |||
250 | static int timbuart_startup(struct uart_port *port) | ||
251 | { | ||
252 | struct timbuart_port *uart = | ||
253 | container_of(port, struct timbuart_port, port); | ||
254 | |||
255 | dev_dbg(port->dev, "%s\n", __func__); | ||
256 | |||
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); | ||
262 | |||
263 | return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED, | ||
264 | "timb-uart", uart); | ||
265 | } | ||
266 | |||
267 | static void timbuart_shutdown(struct uart_port *port) | ||
268 | { | ||
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); | ||
274 | } | ||
275 | |||
276 | static int get_bindex(int baud) | ||
277 | { | ||
278 | int i; | ||
279 | |||
280 | for (i = 0; i < ARRAY_SIZE(baudrates); i++) | ||
281 | if (baud == baudrates[i]) | ||
282 | return i; | ||
283 | |||
284 | return -1; | ||
285 | } | ||
286 | |||
287 | static void timbuart_set_termios(struct uart_port *port, | ||
288 | struct ktermios *termios, | ||
289 | struct ktermios *old) | ||
290 | { | ||
291 | unsigned int baud; | ||
292 | short bindex; | ||
293 | unsigned long flags; | ||
294 | |||
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); | ||
298 | |||
299 | if (bindex < 0) { | ||
300 | printk(KERN_ALERT "timbuart: Unsupported baud rate\n"); | ||
301 | } else { | ||
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); | ||
306 | } | ||
307 | } | ||
308 | |||
309 | static const char *timbuart_type(struct uart_port *port) | ||
310 | { | ||
311 | return port->type == PORT_UNKNOWN ? "timbuart" : NULL; | ||
312 | } | ||
313 | |||
314 | /* We do not request/release mappings of the registers here, | ||
315 | * currently it's done in the proble function. | ||
316 | */ | ||
317 | static void timbuart_release_port(struct uart_port *port) | ||
318 | { | ||
319 | struct platform_device *pdev = to_platform_device(port->dev); | ||
320 | int size = | ||
321 | resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0)); | ||
322 | |||
323 | if (port->flags & UPF_IOREMAP) { | ||
324 | iounmap(port->membase); | ||
325 | port->membase = NULL; | ||
326 | } | ||
327 | |||
328 | release_mem_region(port->mapbase, size); | ||
329 | } | ||
330 | |||
331 | static int timbuart_request_port(struct uart_port *port) | ||
332 | { | ||
333 | struct platform_device *pdev = to_platform_device(port->dev); | ||
334 | int size = | ||
335 | resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0)); | ||
336 | |||
337 | if (!request_mem_region(port->mapbase, size, "timb-uart")) | ||
338 | return -EBUSY; | ||
339 | |||
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); | ||
344 | return -ENOMEM; | ||
345 | } | ||
346 | } | ||
347 | |||
348 | return 0; | ||
349 | } | ||
350 | |||
351 | static irqreturn_t timbuart_handleinterrupt(int irq, void *devid) | ||
352 | { | ||
353 | struct timbuart_port *uart = (struct timbuart_port *)devid; | ||
354 | |||
355 | if (ioread8(uart->port.membase + TIMBUART_IPR)) { | ||
356 | uart->last_ier = ioread8(uart->port.membase + TIMBUART_IER); | ||
357 | |||
358 | /* disable interrupts, the tasklet enables them again */ | ||
359 | iowrite8(0, uart->port.membase + TIMBUART_IER); | ||
360 | |||
361 | /* fire off bottom half */ | ||
362 | tasklet_schedule(&uart->tasklet); | ||
363 | |||
364 | return IRQ_HANDLED; | ||
365 | } else | ||
366 | return IRQ_NONE; | ||
367 | } | ||
368 | |||
369 | /* | ||
370 | * Configure/autoconfigure the port. | ||
371 | */ | ||
372 | static void timbuart_config_port(struct uart_port *port, int flags) | ||
373 | { | ||
374 | if (flags & UART_CONFIG_TYPE) { | ||
375 | port->type = PORT_TIMBUART; | ||
376 | timbuart_request_port(port); | ||
377 | } | ||
378 | } | ||
379 | |||
380 | static int timbuart_verify_port(struct uart_port *port, | ||
381 | struct serial_struct *ser) | ||
382 | { | ||
383 | /* we don't want the core code to modify any port params */ | ||
384 | return -EINVAL; | ||
385 | } | ||
386 | |||
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 | ||
405 | }; | ||
406 | |||
407 | static struct uart_driver timbuart_driver = { | ||
408 | .owner = THIS_MODULE, | ||
409 | .driver_name = "timberdale_uart", | ||
410 | .dev_name = "ttyTU", | ||
411 | .major = TIMBUART_MAJOR, | ||
412 | .minor = TIMBUART_MINOR, | ||
413 | .nr = 1 | ||
414 | }; | ||
415 | |||
416 | static int timbuart_probe(struct platform_device *dev) | ||
417 | { | ||
418 | int err; | ||
419 | struct timbuart_port *uart; | ||
420 | struct resource *iomem; | ||
421 | |||
422 | dev_dbg(&dev->dev, "%s\n", __func__); | ||
423 | |||
424 | uart = kzalloc(sizeof(*uart), GFP_KERNEL); | ||
425 | if (!uart) { | ||
426 | err = -EINVAL; | ||
427 | goto err_mem; | ||
428 | } | ||
429 | |||
430 | uart->usedma = 0; | ||
431 | |||
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; | ||
437 | uart->port.irq = 0; | ||
438 | uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; | ||
439 | uart->port.line = 0; | ||
440 | uart->port.dev = &dev->dev; | ||
441 | |||
442 | iomem = platform_get_resource(dev, IORESOURCE_MEM, 0); | ||
443 | if (!iomem) { | ||
444 | err = -ENOMEM; | ||
445 | goto err_register; | ||
446 | } | ||
447 | uart->port.mapbase = iomem->start; | ||
448 | uart->port.membase = NULL; | ||
449 | |||
450 | uart->port.irq = platform_get_irq(dev, 0); | ||
451 | if (uart->port.irq < 0) { | ||
452 | err = -EINVAL; | ||
453 | goto err_register; | ||
454 | } | ||
455 | |||
456 | tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart); | ||
457 | |||
458 | err = uart_register_driver(&timbuart_driver); | ||
459 | if (err) | ||
460 | goto err_register; | ||
461 | |||
462 | err = uart_add_one_port(&timbuart_driver, &uart->port); | ||
463 | if (err) | ||
464 | goto err_add_port; | ||
465 | |||
466 | platform_set_drvdata(dev, uart); | ||
467 | |||
468 | return 0; | ||
469 | |||
470 | err_add_port: | ||
471 | uart_unregister_driver(&timbuart_driver); | ||
472 | err_register: | ||
473 | kfree(uart); | ||
474 | err_mem: | ||
475 | printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n", | ||
476 | err); | ||
477 | |||
478 | return err; | ||
479 | } | ||
480 | |||
481 | static int timbuart_remove(struct platform_device *dev) | ||
482 | { | ||
483 | struct timbuart_port *uart = platform_get_drvdata(dev); | ||
484 | |||
485 | tasklet_kill(&uart->tasklet); | ||
486 | uart_remove_one_port(&timbuart_driver, &uart->port); | ||
487 | uart_unregister_driver(&timbuart_driver); | ||
488 | kfree(uart); | ||
489 | |||
490 | return 0; | ||
491 | } | ||
492 | |||
493 | static struct platform_driver timbuart_platform_driver = { | ||
494 | .driver = { | ||
495 | .name = "timb-uart", | ||
496 | .owner = THIS_MODULE, | ||
497 | }, | ||
498 | .probe = timbuart_probe, | ||
499 | .remove = timbuart_remove, | ||
500 | }; | ||
501 | |||
502 | /*--------------------------------------------------------------------------*/ | ||
503 | |||
504 | static int __init timbuart_init(void) | ||
505 | { | ||
506 | return platform_driver_register(&timbuart_platform_driver); | ||
507 | } | ||
508 | |||
509 | static void __exit timbuart_exit(void) | ||
510 | { | ||
511 | platform_driver_unregister(&timbuart_platform_driver); | ||
512 | } | ||
513 | |||
514 | module_init(timbuart_init); | ||
515 | module_exit(timbuart_exit); | ||
516 | |||
517 | MODULE_DESCRIPTION("Timberdale UART driver"); | ||
518 | MODULE_LICENSE("GPL v2"); | ||
519 | MODULE_ALIAS("platform:timb-uart"); | ||
520 | |||