Merge branch 'adm8668'
[openwrt/svn-archive/archive.git] / target / linux / adm8668 / files / arch / mips / adm8668 / serial.c
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * ADM8668 serial driver, totally ripped the source from BCM63xx and changed
7 * all the registers to fit our hardware, and removed all the features that
8 * I didn't know because our GPL'd serial driver was way lame.
9 *
10 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
11 * Derived directly from bcm63xx_uart
12 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
13 *
14 */
15
16 #if defined(CONFIG_SERIAL_ADM8668_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17 #define SUPPORT_SYSRQ
18 #endif
19
20 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/console.h>
26 #include <linux/clk.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/sysrq.h>
30 #include <linux/serial.h>
31 #include <linux/serial_core.h>
32 #include <adm8668.h>
33
34 #define ADM8668_NR_UARTS 1
35
36 static struct uart_port ports[ADM8668_NR_UARTS];
37
38 /*
39 * handy uart register accessor
40 */
41 static inline unsigned int adm_uart_readl(struct uart_port *port,
42 unsigned int offset)
43 {
44 return (*(volatile unsigned int *)(port->membase + offset));
45 }
46
47 static inline void adm_uart_writel(struct uart_port *port,
48 unsigned int value, unsigned int offset)
49 {
50 (*((volatile unsigned int *)(port->membase + offset))) = value;
51 }
52
53 /*
54 * serial core request to check if uart tx fifo is empty
55 */
56 static unsigned int adm_uart_tx_empty(struct uart_port *port)
57 {
58 /* we always wait for completion, no buffer is made... */
59 return 1;
60 }
61
62 /*
63 * serial core request to set RTS and DTR pin state and loopback mode
64 */
65 static void adm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
66 {
67 }
68
69 /*
70 * serial core request to return RI, CTS, DCD and DSR pin state
71 */
72 static unsigned int adm_uart_get_mctrl(struct uart_port *port)
73 {
74 return 0;
75 }
76
77 /*
78 * serial core request to disable tx ASAP (used for flow control)
79 */
80 static void adm_uart_stop_tx(struct uart_port *port)
81 {
82 unsigned int val;
83
84 val = adm_uart_readl(port, UART_CR_REG);
85 val &= ~(UART_TX_INT_EN);
86 adm_uart_writel(port, val, UART_CR_REG);
87 }
88
89 /*
90 * serial core request to (re)enable tx
91 */
92 static void adm_uart_start_tx(struct uart_port *port)
93 {
94 unsigned int val;
95
96 val = adm_uart_readl(port, UART_CR_REG);
97 val |= UART_TX_INT_EN;
98 adm_uart_writel(port, val, UART_CR_REG);
99 }
100
101 /*
102 * serial core request to stop rx, called before port shutdown
103 */
104 static void adm_uart_stop_rx(struct uart_port *port)
105 {
106 unsigned int val;
107
108 val = adm_uart_readl(port, UART_CR_REG);
109 val &= ~UART_RX_INT_EN;
110 adm_uart_writel(port, val, UART_CR_REG);
111 }
112
113 /*
114 * serial core request to enable modem status interrupt reporting
115 */
116 static void adm_uart_enable_ms(struct uart_port *port)
117 {
118 }
119
120 /*
121 * serial core request to start/stop emitting break char
122 */
123 static void adm_uart_break_ctl(struct uart_port *port, int ctl)
124 {
125 }
126
127 /*
128 * return port type in string format
129 */
130 static const char *adm_uart_type(struct uart_port *port)
131 {
132 return (port->type == PORT_ADM8668) ? "adm8668_uart" : NULL;
133 }
134
135 /*
136 * read all chars in rx fifo and send them to core
137 */
138 static void adm_uart_do_rx(struct uart_port *port)
139 {
140 struct tty_struct *tty;
141 unsigned int max_count;
142
143 /* limit number of char read in interrupt, should not be
144 * higher than fifo size anyway since we're much faster than
145 * serial port */
146 max_count = 32;
147 tty = port->state->port.tty;
148 do {
149 unsigned int iestat, c, cstat;
150 char flag;
151
152 /* get overrun/fifo empty information from ier
153 * register */
154 iestat = adm_uart_readl(port, UART_FR_REG);
155 if (iestat & UART_RX_FIFO_EMPTY)
156 break;
157
158 /* recieve status */
159 cstat = adm_uart_readl(port, UART_RSR_REG);
160 /* clear errors */
161 adm_uart_writel(port, cstat, UART_RSR_REG);
162
163 c = adm_uart_readl(port, UART_DR_REG);
164 port->icount.rx++;
165 flag = TTY_NORMAL;
166
167 if (unlikely((cstat & UART_RX_STATUS_MASK))) {
168 /* do stats first */
169 if (cstat & UART_BREAK_ERR) {
170 port->icount.brk++;
171 if (uart_handle_break(port))
172 continue;
173 }
174
175 if (cstat & UART_PARITY_ERR)
176 port->icount.parity++;
177 if (cstat & UART_FRAMING_ERR)
178 port->icount.frame++;
179 if (cstat & UART_OVERRUN_ERR) {
180 port->icount.overrun++;
181 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
182 }
183
184 /* update flag wrt read_status_mask */
185 cstat &= port->read_status_mask;
186 if (cstat & UART_BREAK_ERR)
187 flag = TTY_BREAK;
188 if (cstat & UART_FRAMING_ERR)
189 flag = TTY_FRAME;
190 if (cstat & UART_PARITY_ERR)
191 flag = TTY_PARITY;
192 }
193
194 if (uart_handle_sysrq_char(port, c))
195 continue;
196
197 /* fixthis */
198 if ((cstat & port->ignore_status_mask) == 0)
199 tty_insert_flip_char(tty, c, flag);
200 } while (max_count--);
201
202 tty_flip_buffer_push(tty);
203 }
204
205 /*
206 * fill tx fifo with chars to send, stop when fifo is about to be full
207 * or when all chars have been sent.
208 */
209 static void adm_uart_do_tx(struct uart_port *port)
210 {
211 struct circ_buf *xmit;
212
213 if (port->x_char) {
214 adm_uart_writel(port, port->x_char, UART_DR_REG);
215 port->icount.tx++;
216 port->x_char = 0;
217 return;
218 }
219
220 if (uart_tx_stopped(port))
221 adm_uart_stop_tx(port);
222
223 xmit = &port->state->xmit;
224
225 if (uart_circ_empty(xmit))
226 goto txq_empty;
227 do
228 {
229 while ((adm_uart_readl(port, UART_FR_REG) & UART_TX_FIFO_FULL) != 0)
230 ;
231 adm_uart_writel(port, xmit->buf[xmit->tail], UART_DR_REG);
232 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
233 port->icount.tx++;
234 if (uart_circ_empty(xmit))
235 break;
236 } while (1);
237
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port);
240
241 if (uart_circ_empty(xmit))
242 goto txq_empty;
243
244 return;
245
246 txq_empty:
247 adm_uart_stop_tx(port);
248 }
249
250 /*
251 * process uart interrupt
252 */
253 static irqreturn_t adm_uart_interrupt(int irq, void *dev_id)
254 {
255 struct uart_port *port;
256 unsigned int irqstat;
257
258 port = dev_id;
259 spin_lock(&port->lock);
260
261 irqstat = adm_uart_readl(port, UART_IIR_REG);
262
263 if (irqstat & (UART_RX_INT|UART_RX_TIMEOUT_INT)) {
264 adm_uart_do_rx(port);
265 }
266
267 if (irqstat & UART_TX_INT) {
268 adm_uart_do_tx(port);
269 }
270 spin_unlock(&port->lock);
271 return IRQ_HANDLED;
272 }
273
274 /*
275 * enable rx & tx operation on uart
276 */
277 static void adm_uart_enable(struct uart_port *port)
278 {
279 unsigned int val;
280
281 val = adm_uart_readl(port, UART_CR_REG);
282 // BREAK_INT too
283 val |= (UART_RX_INT_EN | UART_RX_TIMEOUT_INT_EN);
284 adm_uart_writel(port, val, UART_CR_REG);
285 }
286
287 /*
288 * disable rx & tx operation on uart
289 */
290 static void adm_uart_disable(struct uart_port *port)
291 {
292 unsigned int val;
293
294 val = adm_uart_readl(port, UART_CR_REG);
295 val &= ~(UART_TX_INT_EN | UART_RX_INT_EN | UART_RX_TIMEOUT_INT_EN);
296 adm_uart_writel(port, val, UART_CR_REG);
297 }
298
299 /*
300 * clear all unread data in rx fifo and unsent data in tx fifo
301 */
302 static void adm_uart_flush(struct uart_port *port)
303 {
304 /* read any pending char to make sure all irq status are
305 * cleared */
306 (void)adm_uart_readl(port, UART_DR_REG);
307 }
308
309 /*
310 * serial core request to initialize uart and start rx operation
311 */
312 static int adm_uart_startup(struct uart_port *port)
313 {
314 int ret;
315
316 /* clear any pending external input interrupt */
317 (void)adm_uart_readl(port, UART_IIR_REG);
318
319 /* register irq and enable rx interrupts */
320 ret = request_irq(port->irq, adm_uart_interrupt, 0,
321 adm_uart_type(port), port);
322 if (ret)
323 return ret;
324
325 adm_uart_enable(port);
326
327 return 0;
328 }
329
330 /*
331 * serial core request to flush & disable uart
332 */
333 static void adm_uart_shutdown(struct uart_port *port)
334 {
335 unsigned long flags;
336
337 spin_lock_irqsave(&port->lock, flags);
338 // adm_uart_writel(port, 0, UART_CR_REG);
339 spin_unlock_irqrestore(&port->lock, flags);
340
341 adm_uart_disable(port);
342 adm_uart_flush(port);
343 free_irq(port->irq, port);
344 }
345
346 /*
347 * serial core request to change current uart setting
348 */
349 static void adm_uart_set_termios(struct uart_port *port,
350 struct ktermios *new,
351 struct ktermios *old)
352 {
353 port->ignore_status_mask = 0;
354 uart_update_timeout(port, new->c_cflag, 115200);
355 }
356
357 /*
358 * serial core request to claim uart iomem
359 */
360 static int adm_uart_request_port(struct uart_port *port)
361 {
362 unsigned int size = 0xf;
363 if (!request_mem_region(port->mapbase, size, "adm8668")) {
364 dev_err(port->dev, "Memory region busy\n");
365 return -EBUSY;
366 }
367
368 port->membase = ioremap(port->mapbase, size);
369 if (!port->membase) {
370 dev_err(port->dev, "Unable to map registers\n");
371 release_mem_region(port->mapbase, size);
372 return -EBUSY;
373 }
374 return 0;
375 }
376
377 /*
378 * serial core request to release uart iomem
379 */
380 static void adm_uart_release_port(struct uart_port *port)
381 {
382 release_mem_region(port->mapbase, 0xf);
383 iounmap(port->membase);
384 }
385
386 /*
387 * serial core request to do any port required autoconfiguration
388 */
389 static void adm_uart_config_port(struct uart_port *port, int flags)
390 {
391 if (flags & UART_CONFIG_TYPE) {
392 if (adm_uart_request_port(port))
393 return;
394 port->type = PORT_ADM8668;
395 }
396 }
397
398 /*
399 * serial core request to check that port information in serinfo are
400 * suitable
401 */
402 static int adm_uart_verify_port(struct uart_port *port,
403 struct serial_struct *serinfo)
404 {
405 if (port->type != PORT_ADM8668)
406 return -EINVAL;
407 if (port->irq != serinfo->irq)
408 return -EINVAL;
409 if (port->iotype != serinfo->io_type)
410 return -EINVAL;
411 if (port->mapbase != (unsigned long)serinfo->iomem_base)
412 return -EINVAL;
413 return 0;
414 }
415
416 /* serial core callbacks */
417 static struct uart_ops adm_uart_ops = {
418 .tx_empty = adm_uart_tx_empty,
419 .get_mctrl = adm_uart_get_mctrl,
420 .set_mctrl = adm_uart_set_mctrl,
421 .start_tx = adm_uart_start_tx,
422 .stop_tx = adm_uart_stop_tx,
423 .stop_rx = adm_uart_stop_rx,
424 .enable_ms = adm_uart_enable_ms,
425 .break_ctl = adm_uart_break_ctl,
426 .startup = adm_uart_startup,
427 .shutdown = adm_uart_shutdown,
428 .set_termios = adm_uart_set_termios,
429 .type = adm_uart_type,
430 .release_port = adm_uart_release_port,
431 .request_port = adm_uart_request_port,
432 .config_port = adm_uart_config_port,
433 .verify_port = adm_uart_verify_port,
434 };
435
436 #ifdef CONFIG_SERIAL_ADM8668_CONSOLE
437 static inline void wait_for_xmitr(struct uart_port *port)
438 {
439 while ((adm_uart_readl(port, UART_FR_REG) & UART_TX_FIFO_FULL) != 0)
440 ;
441 }
442
443 /*
444 * output given char
445 */
446 static void adm_console_putchar(struct uart_port *port, int ch)
447 {
448 wait_for_xmitr(port);
449 adm_uart_writel(port, ch, UART_DR_REG);
450 }
451
452 /*
453 * console core request to output given string
454 */
455 static void adm_console_write(struct console *co, const char *s,
456 unsigned int count)
457 {
458 struct uart_port *port;
459 unsigned long flags;
460 int locked;
461
462 port = &ports[co->index];
463
464 local_irq_save(flags);
465 if (port->sysrq) {
466 /* adm_uart_interrupt() already took the lock */
467 locked = 0;
468 } else if (oops_in_progress) {
469 locked = spin_trylock(&port->lock);
470 } else {
471 spin_lock(&port->lock);
472 locked = 1;
473 }
474
475 /* call helper to deal with \r\n */
476 uart_console_write(port, s, count, adm_console_putchar);
477
478 /* and wait for char to be transmitted */
479 wait_for_xmitr(port);
480
481 if (locked)
482 spin_unlock(&port->lock);
483 local_irq_restore(flags);
484 }
485
486 /*
487 * console core request to setup given console, find matching uart
488 * port and setup it.
489 */
490 static int adm_console_setup(struct console *co, char *options)
491 {
492 struct uart_port *port;
493 int baud = 115200;
494 int bits = 8;
495 int parity = 'n';
496 int flow = 'n';
497
498 if (co->index < 0 || co->index >= ADM8668_NR_UARTS)
499 return -EINVAL;
500 port = &ports[co->index];
501 if (!port->membase)
502 return -ENODEV;
503
504 if (options)
505 uart_parse_options(options, &baud, &parity, &bits, &flow);
506
507 return uart_set_options(port, co, baud, parity, bits, flow);
508 }
509
510 static struct uart_driver adm_uart_driver;
511
512 static struct console adm8668_console = {
513 .name = "ttyS",
514 .write = adm_console_write,
515 .device = uart_console_device,
516 .setup = adm_console_setup,
517 .flags = CON_PRINTBUFFER,
518 .index = -1,
519 .data = &adm_uart_driver,
520 };
521
522 static int __init adm8668_console_init(void)
523 {
524 register_console(&adm8668_console);
525 return 0;
526 }
527
528 console_initcall(adm8668_console_init);
529
530 #define ADM8668_CONSOLE (&adm8668_console)
531 #else
532 #define ADM8668_CONSOLE NULL
533 #endif /* CONFIG_SERIAL_ADM8668_CONSOLE */
534
535 static struct uart_driver adm_uart_driver = {
536 .owner = THIS_MODULE,
537 .driver_name = "adm8668_uart",
538 .dev_name = "ttyS",
539 .major = TTY_MAJOR,
540 .minor = 64,
541 .nr = 1,
542 .cons = ADM8668_CONSOLE,
543 };
544
545 /*
546 * platform driver probe/remove callback
547 */
548 static int __devinit adm_uart_probe(struct platform_device *pdev)
549 {
550 struct resource *res_mem, *res_irq;
551 struct uart_port *port;
552 int ret;
553
554 if (pdev->id < 0 || pdev->id >= ADM8668_NR_UARTS)
555 return -EINVAL;
556
557 if (ports[pdev->id].membase)
558 return -EBUSY;
559
560 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
561 if (!res_mem)
562 return -ENODEV;
563
564 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
565 if (!res_irq)
566 return -ENODEV;
567
568 port = &ports[pdev->id];
569 memset(port, 0, sizeof(*port));
570 port->iotype = UPIO_MEM;
571 port->mapbase = res_mem->start;
572 port->irq = res_irq->start;
573 port->ops = &adm_uart_ops;
574 port->flags = UPF_BOOT_AUTOCONF;
575 port->dev = &pdev->dev;
576 port->fifosize = 8;
577 port->uartclk = ADM8668_UARTCLK_FREQ;
578
579 ret = uart_add_one_port(&adm_uart_driver, port);
580 if (ret) {
581 ports[pdev->id].membase = 0;
582 return ret;
583 }
584 platform_set_drvdata(pdev, port);
585 return 0;
586 }
587
588 static int __devexit adm_uart_remove(struct platform_device *pdev)
589 {
590 struct uart_port *port;
591
592 port = platform_get_drvdata(pdev);
593 uart_remove_one_port(&adm_uart_driver, port);
594 platform_set_drvdata(pdev, NULL);
595 /* mark port as free */
596 ports[pdev->id].membase = 0;
597 return 0;
598 }
599
600 /*
601 * platform driver stuff
602 */
603 static struct platform_driver adm_uart_platform_driver = {
604 .probe = adm_uart_probe,
605 .remove = __devexit_p(adm_uart_remove),
606 .driver = {
607 .owner = THIS_MODULE,
608 .name = "adm8668_uart",
609 },
610 };
611
612 static int __init adm_uart_init(void)
613 {
614 int ret;
615
616 ret = uart_register_driver(&adm_uart_driver);
617 if (ret)
618 return ret;
619
620 ret = platform_driver_register(&adm_uart_platform_driver);
621 if (ret)
622 uart_unregister_driver(&adm_uart_driver);
623
624 return ret;
625 }
626
627 static void __exit adm_uart_exit(void)
628 {
629 platform_driver_unregister(&adm_uart_platform_driver);
630 uart_unregister_driver(&adm_uart_driver);
631 }
632
633 module_init(adm_uart_init);
634 module_exit(adm_uart_exit);
635
636 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
637 MODULE_DESCRIPTION("ADM8668 integrated uart driver");
638 MODULE_LICENSE("GPL");