1 /* MN10300 On-chip serial port UART driver
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 static const char serial_name[] = "MN10300 Serial driver";
13 static const char serial_version[] = "mn10300_serial-1.0";
14 static const char serial_revdate[] = "2007-11-06";
16 #if defined(CONFIG_MN10300_TTYSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20 #include <linux/module.h>
21 #include <linux/serial.h>
22 #include <linux/circ_buf.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/major.h>
31 #include <linux/string.h>
32 #include <linux/ioport.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/console.h>
37 #include <linux/sysrq.h>
39 #include <asm/system.h>
42 #include <asm/bitops.h>
43 #include <asm/serial-regs.h>
44 #include <unit/timex.h>
45 #include "mn10300-serial.h"
47 static inline __attribute__((format(printf, 1, 2)))
48 void no_printk(const char *fmt, ...)
52 #define kenter(FMT, ...) \
53 printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
54 #define _enter(FMT, ...) \
55 no_printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
56 #define kdebug(FMT, ...) \
57 printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
58 #define _debug(FMT, ...) \
59 no_printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
60 #define kproto(FMT, ...) \
61 printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
62 #define _proto(FMT, ...) \
63 no_printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
67 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
68 static void mn10300_serial_console_write(struct console *co,
69 const char *s, unsigned count);
70 static int __init mn10300_serial_console_setup(struct console *co,
73 static struct uart_driver mn10300_serial_driver;
74 static struct console mn10300_serial_console = {
76 .write = mn10300_serial_console_write,
77 .device = uart_console_device,
78 .setup = mn10300_serial_console_setup,
79 .flags = CON_PRINTBUFFER,
81 .data = &mn10300_serial_driver,
85 static struct uart_driver mn10300_serial_driver = {
87 .driver_name = "mn10300-serial",
92 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
93 .cons = &mn10300_serial_console,
97 static unsigned int mn10300_serial_tx_empty(struct uart_port *);
98 static void mn10300_serial_set_mctrl(struct uart_port *, unsigned int mctrl);
99 static unsigned int mn10300_serial_get_mctrl(struct uart_port *);
100 static void mn10300_serial_stop_tx(struct uart_port *);
101 static void mn10300_serial_start_tx(struct uart_port *);
102 static void mn10300_serial_send_xchar(struct uart_port *, char ch);
103 static void mn10300_serial_stop_rx(struct uart_port *);
104 static void mn10300_serial_enable_ms(struct uart_port *);
105 static void mn10300_serial_break_ctl(struct uart_port *, int ctl);
106 static int mn10300_serial_startup(struct uart_port *);
107 static void mn10300_serial_shutdown(struct uart_port *);
108 static void mn10300_serial_set_termios(struct uart_port *,
109 struct ktermios *new,
110 struct ktermios *old);
111 static const char *mn10300_serial_type(struct uart_port *);
112 static void mn10300_serial_release_port(struct uart_port *);
113 static int mn10300_serial_request_port(struct uart_port *);
114 static void mn10300_serial_config_port(struct uart_port *, int);
115 static int mn10300_serial_verify_port(struct uart_port *,
116 struct serial_struct *);
118 static const struct uart_ops mn10300_serial_ops = {
119 .tx_empty = mn10300_serial_tx_empty,
120 .set_mctrl = mn10300_serial_set_mctrl,
121 .get_mctrl = mn10300_serial_get_mctrl,
122 .stop_tx = mn10300_serial_stop_tx,
123 .start_tx = mn10300_serial_start_tx,
124 .send_xchar = mn10300_serial_send_xchar,
125 .stop_rx = mn10300_serial_stop_rx,
126 .enable_ms = mn10300_serial_enable_ms,
127 .break_ctl = mn10300_serial_break_ctl,
128 .startup = mn10300_serial_startup,
129 .shutdown = mn10300_serial_shutdown,
130 .set_termios = mn10300_serial_set_termios,
131 .type = mn10300_serial_type,
132 .release_port = mn10300_serial_release_port,
133 .request_port = mn10300_serial_request_port,
134 .config_port = mn10300_serial_config_port,
135 .verify_port = mn10300_serial_verify_port,
138 static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id);
141 * the first on-chip serial port: ttySM0 (aka SIF0)
143 #ifdef CONFIG_MN10300_TTYSM0
144 struct mn10300_serial_port mn10300_serial_port_sif0 = {
145 .uart.ops = &mn10300_serial_ops,
146 .uart.membase = (void __iomem *) &SC0CTR,
147 .uart.mapbase = (unsigned long) &SC0CTR,
148 .uart.iotype = UPIO_MEM,
150 .uart.uartclk = 0, /* MN10300_IOCLK, */
152 .uart.flags = UPF_BOOT_AUTOCONF,
154 .uart.type = PORT_MN10300,
156 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif0.uart.lock),
160 ._status = (volatile u8 *) &SC0STR,
164 .rx_name = "ttySM0/Rx",
165 .tx_name = "ttySM0/Tx",
166 #ifdef CONFIG_MN10300_TTYSM0_TIMER8
167 .tm_name = "ttySM0/Timer8",
172 .div_timer = MNSCx_DIV_TIMER_16BIT,
173 #else /* CONFIG_MN10300_TTYSM0_TIMER2 */
174 .tm_name = "ttySM0/Timer2",
176 ._tmxbr = (volatile u16 *) &TM2BR,
179 .div_timer = MNSCx_DIV_TIMER_8BIT,
183 .rx_icr = &GxICR(SC0RXIRQ),
184 .tx_icr = &GxICR(SC0TXIRQ),
185 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
187 #ifdef CONFIG_GDBSTUB_ON_TTYSM0
191 #endif /* CONFIG_MN10300_TTYSM0 */
194 * the second on-chip serial port: ttySM1 (aka SIF1)
196 #ifdef CONFIG_MN10300_TTYSM1
197 struct mn10300_serial_port mn10300_serial_port_sif1 = {
198 .uart.ops = &mn10300_serial_ops,
199 .uart.membase = (void __iomem *) &SC1CTR,
200 .uart.mapbase = (unsigned long) &SC1CTR,
201 .uart.iotype = UPIO_MEM,
203 .uart.uartclk = 0, /* MN10300_IOCLK, */
205 .uart.flags = UPF_BOOT_AUTOCONF,
207 .uart.type = PORT_MN10300,
209 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif1.uart.lock),
213 ._status = (volatile u8 *) &SC1STR,
217 .rx_name = "ttySM1/Rx",
218 .tx_name = "ttySM1/Tx",
219 #ifdef CONFIG_MN10300_TTYSM1_TIMER9
220 .tm_name = "ttySM1/Timer9",
225 .div_timer = MNSCx_DIV_TIMER_16BIT,
226 #else /* CONFIG_MN10300_TTYSM1_TIMER3 */
227 .tm_name = "ttySM1/Timer3",
229 ._tmxbr = (volatile u16 *) &TM3BR,
232 .div_timer = MNSCx_DIV_TIMER_8BIT,
236 .rx_icr = &GxICR(SC1RXIRQ),
237 .tx_icr = &GxICR(SC1TXIRQ),
238 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
240 #ifdef CONFIG_GDBSTUB_ON_TTYSM1
244 #endif /* CONFIG_MN10300_TTYSM1 */
247 * the third on-chip serial port: ttySM2 (aka SIF2)
249 #ifdef CONFIG_MN10300_TTYSM2
250 struct mn10300_serial_port mn10300_serial_port_sif2 = {
251 .uart.ops = &mn10300_serial_ops,
252 .uart.membase = (void __iomem *) &SC2CTR,
253 .uart.mapbase = (unsigned long) &SC2CTR,
254 .uart.iotype = UPIO_MEM,
256 .uart.uartclk = 0, /* MN10300_IOCLK, */
258 .uart.flags = UPF_BOOT_AUTOCONF,
260 #ifdef CONFIG_MN10300_TTYSM2_CTS
261 .uart.type = PORT_MN10300_CTS,
263 .uart.type = PORT_MN10300,
266 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif2.uart.lock),
268 .rx_name = "ttySM2/Rx",
269 .tx_name = "ttySM2/Tx",
270 .tm_name = "ttySM2/Timer10",
281 .div_timer = MNSCx_DIV_TIMER_16BIT,
284 .rx_icr = &GxICR(SC2RXIRQ),
285 .tx_icr = &GxICR(SC2TXIRQ),
286 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
287 #ifdef CONFIG_MN10300_TTYSM2_CTS
288 .options = MNSCx_OPT_CTS,
292 #ifdef CONFIG_GDBSTUB_ON_TTYSM2
296 #endif /* CONFIG_MN10300_TTYSM2 */
300 * list of available serial ports
302 struct mn10300_serial_port *mn10300_serial_ports[NR_UARTS + 1] = {
303 #ifdef CONFIG_MN10300_TTYSM0
304 [0] = &mn10300_serial_port_sif0,
306 #ifdef CONFIG_MN10300_TTYSM1
307 [1] = &mn10300_serial_port_sif1,
309 #ifdef CONFIG_MN10300_TTYSM2
310 [2] = &mn10300_serial_port_sif2,
317 * we abuse the serial ports' baud timers' interrupt lines to get the ability
318 * to deliver interrupts to userspace as we use the ports' interrupt lines to
319 * do virtual DMA on account of the ports having no hardware FIFOs
321 * we can generate an interrupt manually in the assembly stubs by writing to
322 * the enable and detect bits in the interrupt control register, so all we need
323 * to do here is disable the interrupt line
325 * note that we can't just leave the line enabled as the baud rate timer *also*
326 * generates interrupts
328 static void mn10300_serial_mask_ack(unsigned int irq)
331 GxICR(irq) = GxICR_LEVEL_6;
332 tmp = GxICR(irq); /* flush write buffer */
335 static void mn10300_serial_nop(unsigned int irq)
339 static struct irq_chip mn10300_serial_pic = {
341 .ack = mn10300_serial_mask_ack,
342 .mask = mn10300_serial_mask_ack,
343 .mask_ack = mn10300_serial_mask_ack,
344 .unmask = mn10300_serial_nop,
345 .end = mn10300_serial_nop,
350 * serial virtual DMA interrupt jump table
352 struct mn10300_serial_int mn10300_serial_int_tbl[NR_IRQS];
354 static void mn10300_serial_dis_tx_intr(struct mn10300_serial_port *port)
357 *port->tx_icr = GxICR_LEVEL_1 | GxICR_DETECT;
361 static void mn10300_serial_en_tx_intr(struct mn10300_serial_port *port)
364 *port->tx_icr = GxICR_LEVEL_1 | GxICR_ENABLE;
368 static void mn10300_serial_dis_rx_intr(struct mn10300_serial_port *port)
371 *port->rx_icr = GxICR_LEVEL_1 | GxICR_DETECT;
376 * multi-bit equivalent of test_and_clear_bit()
378 static int mask_test_and_clear(volatile u8 *ptr, u8 mask)
381 asm volatile(" bclr %1,(%2) \n"
383 : "=d"(epsw) : "d"(mask), "a"(ptr)
385 return !(epsw & EPSW_FLAG_Z);
389 * receive chars from the ring buffer for this serial port
390 * - must do break detection here (not done in the UART)
392 static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
394 struct uart_icount *icount = &port->uart.icount;
395 struct tty_struct *tty = port->uart.state->port.tty;
398 u8 st, ch, push, status, overrun;
400 _enter("%s", port->name);
404 count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
405 count = tty_buffer_request_room(tty, count);
407 if (!tty->low_latency)
408 tty_flip_buffer_push(tty);
413 /* pull chars out of the hat */
415 if (ix == port->rx_inp) {
416 if (push && !tty->low_latency)
417 tty_flip_buffer_push(tty);
421 ch = port->rx_buffer[ix++];
422 st = port->rx_buffer[ix++];
424 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
425 port->uart.icount.rx++;
427 st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
431 /* the UART doesn't detect BREAK, so we have to do that ourselves
432 * - it starts as a framing error on a NUL character
433 * - then we count another two NUL characters before issuing TTY_BREAK
434 * - then we end on a normal char or one that has all the bottom bits
435 * zero and the top bits set
437 switch (port->rx_brk) {
439 /* not breaking at the moment */
443 if (st & SC01STR_FEF && ch == 0) {
450 if (st & SC01STR_FEF && ch == 0) {
452 _proto("Rx Break Detected");
454 if (uart_handle_break(&port->uart))
456 status |= 1 << TTY_BREAK;
462 if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
463 goto try_again; /* still breaking */
465 port->rx_brk = 0; /* end of the break */
477 /* discard char at probable break end */
484 /* handle framing error */
485 if (st & SC01STR_FEF) {
487 /* framing error with NUL char is probably a BREAK */
492 _proto("Rx Framing Error");
494 status |= 1 << TTY_FRAME;
497 /* handle parity error */
498 if (st & SC01STR_PEF) {
499 _proto("Rx Parity Error");
504 /* handle normal char */
506 if (uart_handle_sysrq_char(&port->uart, ch))
508 status = (1 << TTY_NORMAL);
511 /* handle overrun error */
512 if (st & SC01STR_OEF) {
516 _proto("Rx Overrun Error");
522 status &= port->uart.read_status_mask;
524 if (!overrun && !(status & port->uart.ignore_status_mask)) {
527 if (status & (1 << TTY_BREAK))
529 else if (status & (1 << TTY_PARITY))
531 else if (status & (1 << TTY_FRAME))
536 tty_insert_flip_char(tty, ch, flag);
539 /* overrun is special, since it's reported immediately, and doesn't
540 * affect the current character
543 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
547 if (!tty->low_latency)
548 tty_flip_buffer_push(tty);
562 * handle an interrupt from the serial transmission "virtual DMA" driver
563 * - note: the interrupt routine will disable its own interrupts when the Tx
566 static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
568 _enter("%s", port->name);
570 if (!port->uart.state || !port->uart.state->port.tty) {
571 mn10300_serial_dis_tx_intr(port);
575 if (uart_tx_stopped(&port->uart) ||
576 uart_circ_empty(&port->uart.state->xmit))
577 mn10300_serial_dis_tx_intr(port);
579 if (uart_circ_chars_pending(&port->uart.state->xmit) < WAKEUP_CHARS)
580 uart_write_wakeup(&port->uart);
584 * deal with a change in the status of the CTS line
586 static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
591 port->uart.icount.cts++;
593 /* flip the CTS state selector flag to interrupt when it changes
595 ctr = *port->_control;
597 *port->_control = ctr;
599 uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
600 wake_up_interruptible(&port->uart.state->port.delta_msr_wait);
604 * handle a virtual interrupt generated by the lower level "virtual DMA"
605 * routines (irq is the baud timer interrupt)
607 static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
609 struct mn10300_serial_port *port = dev_id;
612 spin_lock(&port->uart.lock);
614 if (port->intr_flags) {
615 _debug("INT %s: %x", port->name, port->intr_flags);
617 if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
618 mn10300_serial_receive_interrupt(port);
620 if (mask_test_and_clear(&port->intr_flags,
621 MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
622 mn10300_serial_transmit_interrupt(port);
625 /* the only modem control line amongst the whole lot is CTS on
627 if (port->type == PORT_MN10300_CTS) {
629 if ((port->tx_cts ^ st) & SC2STR_CTS)
630 mn10300_serial_cts_changed(port, st);
633 spin_unlock(&port->uart.lock);
639 * return indication of whether the hardware transmit buffer is empty
641 static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
643 struct mn10300_serial_port *port =
644 container_of(_port, struct mn10300_serial_port, uart);
646 _enter("%s", port->name);
648 return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
653 * set the modem control lines (we don't have any)
655 static void mn10300_serial_set_mctrl(struct uart_port *_port,
658 struct mn10300_serial_port *port =
659 container_of(_port, struct mn10300_serial_port, uart);
661 _enter("%s,%x", port->name, mctrl);
665 * get the modem control line statuses
667 static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
669 struct mn10300_serial_port *port =
670 container_of(_port, struct mn10300_serial_port, uart);
672 _enter("%s", port->name);
674 if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
675 return TIOCM_CAR | TIOCM_DSR;
677 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
681 * stop transmitting characters
683 static void mn10300_serial_stop_tx(struct uart_port *_port)
685 struct mn10300_serial_port *port =
686 container_of(_port, struct mn10300_serial_port, uart);
688 _enter("%s", port->name);
690 /* disable the virtual DMA */
691 mn10300_serial_dis_tx_intr(port);
695 * start transmitting characters
696 * - jump-start transmission if it has stalled
697 * - enable the serial Tx interrupt (used by the virtual DMA controller)
698 * - force an interrupt to happen if necessary
700 static void mn10300_serial_start_tx(struct uart_port *_port)
702 struct mn10300_serial_port *port =
703 container_of(_port, struct mn10300_serial_port, uart);
709 CIRC_CNT(&port->uart.state->xmit.head,
710 &port->uart.state->xmit.tail,
713 /* kick the virtual DMA controller */
717 if (*port->_status & SC01STR_TBF)
718 x &= ~(GxICR_REQUEST | GxICR_DETECT);
720 x |= GxICR_REQUEST | GxICR_DETECT;
722 _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
723 *port->_control, *port->_intr, *port->_status,
724 *port->_tmxmd, *port->_tmxbr, *port->tx_icr);
731 * transmit a high-priority XON/XOFF character
733 static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
735 struct mn10300_serial_port *port =
736 container_of(_port, struct mn10300_serial_port, uart);
738 _enter("%s,%02x", port->name, ch);
740 if (likely(port->gdbstub)) {
743 mn10300_serial_en_tx_intr(port);
748 * stop receiving characters
749 * - called whilst the port is being closed
751 static void mn10300_serial_stop_rx(struct uart_port *_port)
753 struct mn10300_serial_port *port =
754 container_of(_port, struct mn10300_serial_port, uart);
758 _enter("%s", port->name);
760 ctr = *port->_control;
762 *port->_control = ctr;
764 mn10300_serial_dis_rx_intr(port);
768 * enable modem status interrupts
770 static void mn10300_serial_enable_ms(struct uart_port *_port)
772 struct mn10300_serial_port *port =
773 container_of(_port, struct mn10300_serial_port, uart);
777 _enter("%s", port->name);
779 if (port->type == PORT_MN10300_CTS) {
780 /* want to interrupt when CTS goes low if CTS is now high and
783 port->tx_cts = *port->_status;
785 cts = (port->tx_cts & SC2STR_CTS) ?
786 SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
788 ctr = *port->_control;
791 *port->_control = ctr;
793 mn10300_serial_en_tx_intr(port);
798 * transmit or cease transmitting a break signal
800 static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
802 struct mn10300_serial_port *port =
803 container_of(_port, struct mn10300_serial_port, uart);
805 _enter("%s,%d", port->name, ctl);
808 /* tell the virtual DMA handler to assert BREAK */
810 mn10300_serial_en_tx_intr(port);
813 *port->_control &= ~SC01CTR_BKE;
814 mn10300_serial_en_tx_intr(port);
819 * grab the interrupts and enable the port for reception
821 static int mn10300_serial_startup(struct uart_port *_port)
823 struct mn10300_serial_port *port =
824 container_of(_port, struct mn10300_serial_port, uart);
825 struct mn10300_serial_int *pint;
827 _enter("%s{%d}", port->name, port->gdbstub);
829 if (unlikely(port->gdbstub))
832 /* allocate an Rx buffer for the virtual DMA handler */
833 port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
834 if (!port->rx_buffer)
837 port->rx_inp = port->rx_outp = 0;
839 /* finally, enable the device */
840 *port->_intr = SC01ICR_TI;
841 *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
843 pint = &mn10300_serial_int_tbl[port->rx_irq];
845 pint->vdma = mn10300_serial_vdma_rx_handler;
846 pint = &mn10300_serial_int_tbl[port->tx_irq];
848 pint->vdma = mn10300_serial_vdma_tx_handler;
850 set_intr_level(port->rx_irq, GxICR_LEVEL_1);
851 set_intr_level(port->tx_irq, GxICR_LEVEL_1);
852 set_irq_chip(port->tm_irq, &mn10300_serial_pic);
854 if (request_irq(port->rx_irq, mn10300_serial_interrupt,
855 IRQF_DISABLED, port->rx_name, port) < 0)
858 if (request_irq(port->tx_irq, mn10300_serial_interrupt,
859 IRQF_DISABLED, port->tx_name, port) < 0)
862 if (request_irq(port->tm_irq, mn10300_serial_interrupt,
863 IRQF_DISABLED, port->tm_name, port) < 0)
865 mn10300_serial_mask_ack(port->tm_irq);
870 free_irq(port->tx_irq, port);
872 free_irq(port->rx_irq, port);
874 kfree(port->rx_buffer);
875 port->rx_buffer = NULL;
880 * shutdown the port and release interrupts
882 static void mn10300_serial_shutdown(struct uart_port *_port)
884 struct mn10300_serial_port *port =
885 container_of(_port, struct mn10300_serial_port, uart);
887 _enter("%s", port->name);
889 /* disable the serial port and its baud rate timer */
891 *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
894 if (port->rx_buffer) {
895 void *buf = port->rx_buffer;
896 port->rx_buffer = NULL;
900 /* disable all intrs */
901 free_irq(port->tm_irq, port);
902 free_irq(port->rx_irq, port);
903 free_irq(port->tx_irq, port);
905 *port->rx_icr = GxICR_LEVEL_1;
906 *port->tx_icr = GxICR_LEVEL_1;
910 * this routine is called to set the UART divisor registers to match the
911 * specified baud rate for a serial port.
913 static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
914 struct ktermios *new,
915 struct ktermios *old)
918 unsigned long ioclk = port->ioclk;
920 int baud, bits, xdiv, tmp;
923 u8 div_timer = port->div_timer;
925 _enter("%s{%lu}", port->name, ioclk);
927 /* byte size and parity */
928 cflag = new->c_cflag;
929 switch (cflag & CSIZE) {
930 case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
931 case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
932 default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
935 if (cflag & CSTOPB) {
936 scxctr |= SC01CTR_STB_2BIT;
940 if (cflag & PARENB) {
943 scxctr |= SC01CTR_PB_ODD;
945 else if (cflag & CMSPAR)
946 scxctr |= SC01CTR_PB_FIXED0;
949 scxctr |= SC01CTR_PB_EVEN;
952 /* Determine divisor based on baud rate */
955 if (div_timer == MNSCx_DIV_TIMER_16BIT)
956 scxctr |= SC0CTR_CK_TM8UFLOW_8; /* ( == SC1CTR_CK_TM9UFLOW_8
957 * == SC2CTR_CK_TM10UFLOW) */
958 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
959 scxctr |= SC0CTR_CK_TM2UFLOW_8;
962 baud = uart_get_baud_rate(&port->uart, new, old, 0,
965 _debug("ALT %d [baud %d]", battempt, baud);
968 baud = 9600; /* B0 transition handled in rs_set_termios */
971 baud = 269; /* 134 is really 134.5 */
976 (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
978 _debug("CUSTOM %u", port->uart.custom_divisor);
980 if (div_timer == MNSCx_DIV_TIMER_16BIT) {
981 if (port->uart.custom_divisor <= 65535) {
982 tmxmd = TM8MD_SRC_IOCLK;
983 tmxbr = port->uart.custom_divisor;
984 port->uart.uartclk = ioclk;
987 if (port->uart.custom_divisor / 8 <= 65535) {
988 tmxmd = TM8MD_SRC_IOCLK_8;
989 tmxbr = port->uart.custom_divisor / 8;
990 port->uart.custom_divisor = tmxbr * 8;
991 port->uart.uartclk = ioclk / 8;
994 if (port->uart.custom_divisor / 32 <= 65535) {
995 tmxmd = TM8MD_SRC_IOCLK_32;
996 tmxbr = port->uart.custom_divisor / 32;
997 port->uart.custom_divisor = tmxbr * 32;
998 port->uart.uartclk = ioclk / 32;
1002 } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
1003 if (port->uart.custom_divisor <= 255) {
1004 tmxmd = TM2MD_SRC_IOCLK;
1005 tmxbr = port->uart.custom_divisor;
1006 port->uart.uartclk = ioclk;
1009 if (port->uart.custom_divisor / 8 <= 255) {
1010 tmxmd = TM2MD_SRC_IOCLK_8;
1011 tmxbr = port->uart.custom_divisor / 8;
1012 port->uart.custom_divisor = tmxbr * 8;
1013 port->uart.uartclk = ioclk / 8;
1016 if (port->uart.custom_divisor / 32 <= 255) {
1017 tmxmd = TM2MD_SRC_IOCLK_32;
1018 tmxbr = port->uart.custom_divisor / 32;
1019 port->uart.custom_divisor = tmxbr * 32;
1020 port->uart.uartclk = ioclk / 32;
1026 switch (div_timer) {
1027 case MNSCx_DIV_TIMER_16BIT:
1028 port->uart.uartclk = ioclk;
1029 tmxmd = TM8MD_SRC_IOCLK;
1030 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1031 if (tmp > 0 && tmp <= 65535)
1034 port->uart.uartclk = ioclk / 8;
1035 tmxmd = TM8MD_SRC_IOCLK_8;
1036 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1037 if (tmp > 0 && tmp <= 65535)
1040 port->uart.uartclk = ioclk / 32;
1041 tmxmd = TM8MD_SRC_IOCLK_32;
1042 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1043 if (tmp > 0 && tmp <= 65535)
1047 case MNSCx_DIV_TIMER_8BIT:
1048 port->uart.uartclk = ioclk;
1049 tmxmd = TM2MD_SRC_IOCLK;
1050 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1051 if (tmp > 0 && tmp <= 255)
1054 port->uart.uartclk = ioclk / 8;
1055 tmxmd = TM2MD_SRC_IOCLK_8;
1056 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1057 if (tmp > 0 && tmp <= 255)
1060 port->uart.uartclk = ioclk / 32;
1061 tmxmd = TM2MD_SRC_IOCLK_32;
1062 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1063 if (tmp > 0 && tmp <= 255)
1072 /* refuse to change to a baud rate we can't support */
1073 _debug("CAN'T SUPPORT");
1078 new->c_cflag &= ~CBAUD;
1079 new->c_cflag |= (old->c_cflag & CBAUD);
1081 goto try_alternative;
1085 /* as a last resort, if the quotient is zero, default to 9600
1087 new->c_cflag &= ~CBAUD;
1088 new->c_cflag |= B9600;
1090 goto try_alternative;
1093 /* hmmm... can't seem to support 9600 either
1094 * - we could try iterating through the speeds we know about to
1097 new->c_cflag &= ~CBAUD;
1100 if (div_timer == MNSCx_DIV_TIMER_16BIT)
1101 tmxmd = TM8MD_SRC_IOCLK_32;
1102 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
1103 tmxmd = TM2MD_SRC_IOCLK_32;
1106 port->uart.uartclk = ioclk / 32;
1111 _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
1113 /* make the changes */
1114 spin_lock_irqsave(&port->uart.lock, flags);
1116 uart_update_timeout(&port->uart, new->c_cflag, baud);
1118 /* set the timer to produce the required baud rate */
1119 switch (div_timer) {
1120 case MNSCx_DIV_TIMER_16BIT:
1122 *port->_tmxbr = tmxbr;
1123 *port->_tmxmd = TM8MD_INIT_COUNTER;
1124 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1127 case MNSCx_DIV_TIMER_8BIT:
1129 *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
1130 *port->_tmxmd = TM2MD_INIT_COUNTER;
1131 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1135 /* CTS flow control flag and modem status interrupts */
1136 scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
1138 if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
1139 /* want to interrupt when CTS goes low if CTS is now
1140 * high and vice versa
1142 port->tx_cts = *port->_status;
1144 if (port->tx_cts & SC2STR_CTS)
1145 scxctr |= SC2CTR_TWE;
1147 scxctr |= SC2CTR_TWE | SC2CTR_TWS;
1150 /* set up parity check flag */
1151 port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
1152 if (new->c_iflag & INPCK)
1153 port->uart.read_status_mask |=
1154 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1155 if (new->c_iflag & (BRKINT | PARMRK))
1156 port->uart.read_status_mask |= (1 << TTY_BREAK);
1158 /* characters to ignore */
1159 port->uart.ignore_status_mask = 0;
1160 if (new->c_iflag & IGNPAR)
1161 port->uart.ignore_status_mask |=
1162 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1163 if (new->c_iflag & IGNBRK) {
1164 port->uart.ignore_status_mask |= (1 << TTY_BREAK);
1166 * If we're ignoring parity and break indicators,
1167 * ignore overruns to (for real raw support).
1169 if (new->c_iflag & IGNPAR)
1170 port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
1173 /* Ignore all characters if CREAD is not set */
1174 if ((new->c_cflag & CREAD) == 0)
1175 port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
1177 scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1178 *port->_control = scxctr;
1180 spin_unlock_irqrestore(&port->uart.lock, flags);
1184 * set the terminal I/O parameters
1186 static void mn10300_serial_set_termios(struct uart_port *_port,
1187 struct ktermios *new,
1188 struct ktermios *old)
1190 struct mn10300_serial_port *port =
1191 container_of(_port, struct mn10300_serial_port, uart);
1193 _enter("%s,%p,%p", port->name, new, old);
1195 mn10300_serial_change_speed(port, new, old);
1197 /* handle turning off CRTSCTS */
1198 if (!(new->c_cflag & CRTSCTS)) {
1199 u16 ctr = *port->_control;
1201 *port->_control = ctr;
1206 * return description of port type
1208 static const char *mn10300_serial_type(struct uart_port *_port)
1210 struct mn10300_serial_port *port =
1211 container_of(_port, struct mn10300_serial_port, uart);
1213 if (port->uart.type == PORT_MN10300_CTS)
1214 return "MN10300 SIF_CTS";
1216 return "MN10300 SIF";
1220 * release I/O and memory regions in use by port
1222 static void mn10300_serial_release_port(struct uart_port *_port)
1224 struct mn10300_serial_port *port =
1225 container_of(_port, struct mn10300_serial_port, uart);
1227 _enter("%s", port->name);
1229 release_mem_region((unsigned long) port->_iobase, 16);
1233 * request I/O and memory regions for port
1235 static int mn10300_serial_request_port(struct uart_port *_port)
1237 struct mn10300_serial_port *port =
1238 container_of(_port, struct mn10300_serial_port, uart);
1240 _enter("%s", port->name);
1242 request_mem_region((unsigned long) port->_iobase, 16, port->name);
1247 * configure the type and reserve the ports
1249 static void mn10300_serial_config_port(struct uart_port *_port, int type)
1251 struct mn10300_serial_port *port =
1252 container_of(_port, struct mn10300_serial_port, uart);
1254 _enter("%s", port->name);
1256 port->uart.type = PORT_MN10300;
1258 if (port->options & MNSCx_OPT_CTS)
1259 port->uart.type = PORT_MN10300_CTS;
1261 mn10300_serial_request_port(_port);
1265 * verify serial parameters are suitable for this port type
1267 static int mn10300_serial_verify_port(struct uart_port *_port,
1268 struct serial_struct *ss)
1270 struct mn10300_serial_port *port =
1271 container_of(_port, struct mn10300_serial_port, uart);
1272 void *mapbase = (void *) (unsigned long) port->uart.mapbase;
1274 _enter("%s", port->name);
1276 /* these things may not be changed */
1277 if (ss->irq != port->uart.irq ||
1278 ss->port != port->uart.iobase ||
1279 ss->io_type != port->uart.iotype ||
1280 ss->iomem_base != mapbase ||
1281 ss->iomem_reg_shift != port->uart.regshift ||
1282 ss->hub6 != port->uart.hub6 ||
1283 ss->xmit_fifo_size != port->uart.fifosize)
1286 /* type may be changed on a port that supports CTS */
1287 if (ss->type != port->uart.type) {
1288 if (!(port->options & MNSCx_OPT_CTS))
1291 if (ss->type != PORT_MN10300 &&
1292 ss->type != PORT_MN10300_CTS)
1300 * initialise the MN10300 on-chip UARTs
1302 static int __init mn10300_serial_init(void)
1304 struct mn10300_serial_port *port;
1307 printk(KERN_INFO "%s version %s (%s)\n",
1308 serial_name, serial_version, serial_revdate);
1310 #ifdef CONFIG_MN10300_TTYSM2
1311 SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
1314 set_intr_stub(EXCEP_IRQ_LEVEL1, mn10300_serial_vdma_interrupt);
1316 ret = uart_register_driver(&mn10300_serial_driver);
1318 for (i = 0 ; i < NR_PORTS ; i++) {
1319 port = mn10300_serial_ports[i];
1320 if (!port || port->gdbstub)
1323 switch (port->clock_src) {
1324 case MNSCx_CLOCK_SRC_IOCLK:
1325 port->ioclk = MN10300_IOCLK;
1328 #ifdef MN10300_IOBCLK
1329 case MNSCx_CLOCK_SRC_IOBCLK:
1330 port->ioclk = MN10300_IOBCLK;
1337 ret = uart_add_one_port(&mn10300_serial_driver,
1341 _debug("ERROR %d", -ret);
1347 uart_unregister_driver(&mn10300_serial_driver);
1353 __initcall(mn10300_serial_init);
1356 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
1359 * print a string to the serial port without disturbing the real user of the
1361 * - the console must be locked by the caller
1363 static void mn10300_serial_console_write(struct console *co,
1364 const char *s, unsigned count)
1366 struct mn10300_serial_port *port;
1368 u16 scxctr, txicr, tmp;
1371 port = mn10300_serial_ports[co->index];
1373 /* firstly hijack the serial port from the "virtual DMA" controller */
1374 txicr = *port->tx_icr;
1375 *port->tx_icr = GxICR_LEVEL_1;
1376 tmp = *port->tx_icr;
1378 /* the transmitter may be disabled */
1379 scxctr = *port->_control;
1380 if (!(scxctr & SC01CTR_TXE)) {
1381 /* restart the UART clock */
1382 tmxmd = *port->_tmxmd;
1384 switch (port->div_timer) {
1385 case MNSCx_DIV_TIMER_16BIT:
1387 *port->_tmxmd = TM8MD_INIT_COUNTER;
1388 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1391 case MNSCx_DIV_TIMER_8BIT:
1393 *port->_tmxmd = TM2MD_INIT_COUNTER;
1394 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1398 /* enable the transmitter */
1399 *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
1401 } else if (scxctr & SC01CTR_BKE) {
1402 /* stop transmitting BREAK */
1403 *port->_control = (scxctr & ~SC01CTR_BKE);
1406 /* send the chars into the serial port (with LF -> LFCR conversion) */
1407 for (i = 0; i < count; i++) {
1410 while (*port->_status & SC01STR_TBF)
1412 *(u8 *) port->_txb = ch;
1415 while (*port->_status & SC01STR_TBF)
1417 *(u8 *) port->_txb = 0xd;
1421 /* can't let the transmitter be turned off if it's actually
1423 while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
1426 /* disable the transmitter if we re-enabled it */
1427 if (!(scxctr & SC01CTR_TXE))
1428 *port->_control = scxctr;
1430 *port->tx_icr = txicr;
1431 tmp = *port->tx_icr;
1435 * set up a serial port as a console
1436 * - construct a cflag setting for the first rs_open()
1437 * - initialize the serial port
1438 * - return non-zero if we didn't find a serial port.
1440 static int __init mn10300_serial_console_setup(struct console *co,
1443 struct mn10300_serial_port *port;
1444 int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
1446 for (i = 0 ; i < NR_PORTS ; i++) {
1447 port = mn10300_serial_ports[i];
1448 if (port && !port->gdbstub && port->uart.line == co->index)
1455 switch (port->clock_src) {
1456 case MNSCx_CLOCK_SRC_IOCLK:
1457 port->ioclk = MN10300_IOCLK;
1460 #ifdef MN10300_IOBCLK
1461 case MNSCx_CLOCK_SRC_IOBCLK:
1462 port->ioclk = MN10300_IOBCLK;
1470 uart_parse_options(options, &baud, &parity, &bits, &flow);
1472 return uart_set_options(&port->uart, co, baud, parity, bits, flow);
1478 static int __init mn10300_serial_console_init(void)
1480 register_console(&mn10300_serial_console);
1484 console_initcall(mn10300_serial_console_init);