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 <asm/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));
384 return !(epsw & EPSW_FLAG_Z);
388 * receive chars from the ring buffer for this serial port
389 * - must do break detection here (not done in the UART)
391 static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
393 struct uart_icount *icount = &port->uart.icount;
394 struct tty_struct *tty = port->uart.info->port.tty;
397 u8 st, ch, push, status, overrun;
399 _enter("%s", port->name);
403 count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
404 count = tty_buffer_request_room(tty, count);
406 if (!tty->low_latency)
407 tty_flip_buffer_push(tty);
412 /* pull chars out of the hat */
414 if (ix == port->rx_inp) {
415 if (push && !tty->low_latency)
416 tty_flip_buffer_push(tty);
420 ch = port->rx_buffer[ix++];
421 st = port->rx_buffer[ix++];
423 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
424 port->uart.icount.rx++;
426 st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
430 /* the UART doesn't detect BREAK, so we have to do that ourselves
431 * - it starts as a framing error on a NUL character
432 * - then we count another two NUL characters before issuing TTY_BREAK
433 * - then we end on a normal char or one that has all the bottom bits
434 * zero and the top bits set
436 switch (port->rx_brk) {
438 /* not breaking at the moment */
442 if (st & SC01STR_FEF && ch == 0) {
449 if (st & SC01STR_FEF && ch == 0) {
451 _proto("Rx Break Detected");
453 if (uart_handle_break(&port->uart))
455 status |= 1 << TTY_BREAK;
461 if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
462 goto try_again; /* still breaking */
464 port->rx_brk = 0; /* end of the break */
476 /* discard char at probable break end */
483 /* handle framing error */
484 if (st & SC01STR_FEF) {
486 /* framing error with NUL char is probably a BREAK */
491 _proto("Rx Framing Error");
493 status |= 1 << TTY_FRAME;
496 /* handle parity error */
497 if (st & SC01STR_PEF) {
498 _proto("Rx Parity Error");
503 /* handle normal char */
505 if (uart_handle_sysrq_char(&port->uart, ch))
507 status = (1 << TTY_NORMAL);
510 /* handle overrun error */
511 if (st & SC01STR_OEF) {
515 _proto("Rx Overrun Error");
521 status &= port->uart.read_status_mask;
523 if (!overrun && !(status & port->uart.ignore_status_mask)) {
526 if (status & (1 << TTY_BREAK))
528 else if (status & (1 << TTY_PARITY))
530 else if (status & (1 << TTY_FRAME))
535 tty_insert_flip_char(tty, ch, flag);
538 /* overrun is special, since it's reported immediately, and doesn't
539 * affect the current character
542 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
546 if (!tty->low_latency)
547 tty_flip_buffer_push(tty);
561 * handle an interrupt from the serial transmission "virtual DMA" driver
562 * - note: the interrupt routine will disable its own interrupts when the Tx
565 static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
567 _enter("%s", port->name);
569 if (uart_tx_stopped(&port->uart) ||
570 uart_circ_empty(&port->uart.info->xmit))
571 mn10300_serial_dis_tx_intr(port);
573 if (uart_circ_chars_pending(&port->uart.info->xmit) < WAKEUP_CHARS)
574 uart_write_wakeup(&port->uart);
578 * deal with a change in the status of the CTS line
580 static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
585 port->uart.icount.cts++;
587 /* flip the CTS state selector flag to interrupt when it changes
589 ctr = *port->_control;
591 *port->_control = ctr;
593 uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
594 wake_up_interruptible(&port->uart.info->delta_msr_wait);
598 * handle a virtual interrupt generated by the lower level "virtual DMA"
599 * routines (irq is the baud timer interrupt)
601 static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
603 struct mn10300_serial_port *port = dev_id;
606 spin_lock(&port->uart.lock);
608 if (port->intr_flags) {
609 _debug("INT %s: %x", port->name, port->intr_flags);
611 if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
612 mn10300_serial_receive_interrupt(port);
614 if (mask_test_and_clear(&port->intr_flags,
615 MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
616 mn10300_serial_transmit_interrupt(port);
619 /* the only modem control line amongst the whole lot is CTS on
621 if (port->type == PORT_MN10300_CTS) {
623 if ((port->tx_cts ^ st) & SC2STR_CTS)
624 mn10300_serial_cts_changed(port, st);
627 spin_unlock(&port->uart.lock);
633 * return indication of whether the hardware transmit buffer is empty
635 static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
637 struct mn10300_serial_port *port =
638 container_of(_port, struct mn10300_serial_port, uart);
640 _enter("%s", port->name);
642 return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
647 * set the modem control lines (we don't have any)
649 static void mn10300_serial_set_mctrl(struct uart_port *_port,
652 struct mn10300_serial_port *port =
653 container_of(_port, struct mn10300_serial_port, uart);
655 _enter("%s,%x", port->name, mctrl);
659 * get the modem control line statuses
661 static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
663 struct mn10300_serial_port *port =
664 container_of(_port, struct mn10300_serial_port, uart);
666 _enter("%s", port->name);
668 if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
669 return TIOCM_CAR | TIOCM_DSR;
671 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
675 * stop transmitting characters
677 static void mn10300_serial_stop_tx(struct uart_port *_port)
679 struct mn10300_serial_port *port =
680 container_of(_port, struct mn10300_serial_port, uart);
682 _enter("%s", port->name);
684 /* disable the virtual DMA */
685 mn10300_serial_dis_tx_intr(port);
689 * start transmitting characters
690 * - jump-start transmission if it has stalled
691 * - enable the serial Tx interrupt (used by the virtual DMA controller)
692 * - force an interrupt to happen if necessary
694 static void mn10300_serial_start_tx(struct uart_port *_port)
696 struct mn10300_serial_port *port =
697 container_of(_port, struct mn10300_serial_port, uart);
703 CIRC_CNT(&port->uart.info->xmit.head,
704 &port->uart.info->xmit.tail,
707 /* kick the virtual DMA controller */
711 if (*port->_status & SC01STR_TBF)
712 x &= ~(GxICR_REQUEST | GxICR_DETECT);
714 x |= GxICR_REQUEST | GxICR_DETECT;
716 _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
717 *port->_control, *port->_intr, *port->_status,
718 *port->_tmxmd, *port->_tmxbr, *port->tx_icr);
725 * transmit a high-priority XON/XOFF character
727 static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
729 struct mn10300_serial_port *port =
730 container_of(_port, struct mn10300_serial_port, uart);
732 _enter("%s,%02x", port->name, ch);
734 if (likely(port->gdbstub)) {
737 mn10300_serial_en_tx_intr(port);
742 * stop receiving characters
743 * - called whilst the port is being closed
745 static void mn10300_serial_stop_rx(struct uart_port *_port)
747 struct mn10300_serial_port *port =
748 container_of(_port, struct mn10300_serial_port, uart);
752 _enter("%s", port->name);
754 ctr = *port->_control;
756 *port->_control = ctr;
758 mn10300_serial_dis_rx_intr(port);
762 * enable modem status interrupts
764 static void mn10300_serial_enable_ms(struct uart_port *_port)
766 struct mn10300_serial_port *port =
767 container_of(_port, struct mn10300_serial_port, uart);
771 _enter("%s", port->name);
773 if (port->type == PORT_MN10300_CTS) {
774 /* want to interrupt when CTS goes low if CTS is now high and
777 port->tx_cts = *port->_status;
779 cts = (port->tx_cts & SC2STR_CTS) ?
780 SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
782 ctr = *port->_control;
785 *port->_control = ctr;
787 mn10300_serial_en_tx_intr(port);
792 * transmit or cease transmitting a break signal
794 static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
796 struct mn10300_serial_port *port =
797 container_of(_port, struct mn10300_serial_port, uart);
799 _enter("%s,%d", port->name, ctl);
802 /* tell the virtual DMA handler to assert BREAK */
804 mn10300_serial_en_tx_intr(port);
807 *port->_control &= ~SC01CTR_BKE;
808 mn10300_serial_en_tx_intr(port);
813 * grab the interrupts and enable the port for reception
815 static int mn10300_serial_startup(struct uart_port *_port)
817 struct mn10300_serial_port *port =
818 container_of(_port, struct mn10300_serial_port, uart);
819 struct mn10300_serial_int *pint;
821 _enter("%s{%d}", port->name, port->gdbstub);
823 if (unlikely(port->gdbstub))
826 /* allocate an Rx buffer for the virtual DMA handler */
827 port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
828 if (!port->rx_buffer)
831 port->rx_inp = port->rx_outp = 0;
833 /* finally, enable the device */
834 *port->_intr = SC01ICR_TI;
835 *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
837 pint = &mn10300_serial_int_tbl[port->rx_irq];
839 pint->vdma = mn10300_serial_vdma_rx_handler;
840 pint = &mn10300_serial_int_tbl[port->tx_irq];
842 pint->vdma = mn10300_serial_vdma_tx_handler;
844 set_intr_level(port->rx_irq, GxICR_LEVEL_1);
845 set_intr_level(port->tx_irq, GxICR_LEVEL_1);
846 set_irq_chip(port->tm_irq, &mn10300_serial_pic);
848 if (request_irq(port->rx_irq, mn10300_serial_interrupt,
849 IRQF_DISABLED, port->rx_name, port) < 0)
852 if (request_irq(port->tx_irq, mn10300_serial_interrupt,
853 IRQF_DISABLED, port->tx_name, port) < 0)
856 if (request_irq(port->tm_irq, mn10300_serial_interrupt,
857 IRQF_DISABLED, port->tm_name, port) < 0)
859 mn10300_serial_mask_ack(port->tm_irq);
864 free_irq(port->tx_irq, port);
866 free_irq(port->rx_irq, port);
868 kfree(port->rx_buffer);
869 port->rx_buffer = NULL;
874 * shutdown the port and release interrupts
876 static void mn10300_serial_shutdown(struct uart_port *_port)
878 struct mn10300_serial_port *port =
879 container_of(_port, struct mn10300_serial_port, uart);
881 _enter("%s", port->name);
883 /* disable the serial port and its baud rate timer */
885 *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
888 if (port->rx_buffer) {
889 void *buf = port->rx_buffer;
890 port->rx_buffer = NULL;
894 /* disable all intrs */
895 free_irq(port->tm_irq, port);
896 free_irq(port->rx_irq, port);
897 free_irq(port->tx_irq, port);
899 *port->rx_icr = GxICR_LEVEL_1;
900 *port->tx_icr = GxICR_LEVEL_1;
904 * this routine is called to set the UART divisor registers to match the
905 * specified baud rate for a serial port.
907 static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
908 struct ktermios *new,
909 struct ktermios *old)
912 unsigned long ioclk = port->ioclk;
914 int baud, bits, xdiv, tmp;
917 u8 div_timer = port->div_timer;
919 _enter("%s{%lu}", port->name, ioclk);
921 /* byte size and parity */
922 cflag = new->c_cflag;
923 switch (cflag & CSIZE) {
924 case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
925 case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
926 default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
929 if (cflag & CSTOPB) {
930 scxctr |= SC01CTR_STB_2BIT;
934 if (cflag & PARENB) {
937 scxctr |= SC01CTR_PB_ODD;
939 else if (cflag & CMSPAR)
940 scxctr |= SC01CTR_PB_FIXED0;
943 scxctr |= SC01CTR_PB_EVEN;
946 /* Determine divisor based on baud rate */
949 if (div_timer == MNSCx_DIV_TIMER_16BIT)
950 scxctr |= SC0CTR_CK_TM8UFLOW_8; /* ( == SC1CTR_CK_TM9UFLOW_8
951 * == SC2CTR_CK_TM10UFLOW) */
952 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
953 scxctr |= SC0CTR_CK_TM2UFLOW_8;
956 baud = uart_get_baud_rate(&port->uart, new, old, 0,
959 _debug("ALT %d [baud %d]", battempt, baud);
962 baud = 9600; /* B0 transition handled in rs_set_termios */
965 baud = 269; /* 134 is really 134.5 */
970 (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
972 _debug("CUSTOM %u", port->uart.custom_divisor);
974 if (div_timer == MNSCx_DIV_TIMER_16BIT) {
975 if (port->uart.custom_divisor <= 65535) {
976 tmxmd = TM8MD_SRC_IOCLK;
977 tmxbr = port->uart.custom_divisor;
978 port->uart.uartclk = ioclk;
981 if (port->uart.custom_divisor / 8 <= 65535) {
982 tmxmd = TM8MD_SRC_IOCLK_8;
983 tmxbr = port->uart.custom_divisor / 8;
984 port->uart.custom_divisor = tmxbr * 8;
985 port->uart.uartclk = ioclk / 8;
988 if (port->uart.custom_divisor / 32 <= 65535) {
989 tmxmd = TM8MD_SRC_IOCLK_32;
990 tmxbr = port->uart.custom_divisor / 32;
991 port->uart.custom_divisor = tmxbr * 32;
992 port->uart.uartclk = ioclk / 32;
996 } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
997 if (port->uart.custom_divisor <= 255) {
998 tmxmd = TM2MD_SRC_IOCLK;
999 tmxbr = port->uart.custom_divisor;
1000 port->uart.uartclk = ioclk;
1003 if (port->uart.custom_divisor / 8 <= 255) {
1004 tmxmd = TM2MD_SRC_IOCLK_8;
1005 tmxbr = port->uart.custom_divisor / 8;
1006 port->uart.custom_divisor = tmxbr * 8;
1007 port->uart.uartclk = ioclk / 8;
1010 if (port->uart.custom_divisor / 32 <= 255) {
1011 tmxmd = TM2MD_SRC_IOCLK_32;
1012 tmxbr = port->uart.custom_divisor / 32;
1013 port->uart.custom_divisor = tmxbr * 32;
1014 port->uart.uartclk = ioclk / 32;
1020 switch (div_timer) {
1021 case MNSCx_DIV_TIMER_16BIT:
1022 port->uart.uartclk = ioclk;
1023 tmxmd = TM8MD_SRC_IOCLK;
1024 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1025 if (tmp > 0 && tmp <= 65535)
1028 port->uart.uartclk = ioclk / 8;
1029 tmxmd = TM8MD_SRC_IOCLK_8;
1030 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1031 if (tmp > 0 && tmp <= 65535)
1034 port->uart.uartclk = ioclk / 32;
1035 tmxmd = TM8MD_SRC_IOCLK_32;
1036 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1037 if (tmp > 0 && tmp <= 65535)
1041 case MNSCx_DIV_TIMER_8BIT:
1042 port->uart.uartclk = ioclk;
1043 tmxmd = TM2MD_SRC_IOCLK;
1044 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1045 if (tmp > 0 && tmp <= 255)
1048 port->uart.uartclk = ioclk / 8;
1049 tmxmd = TM2MD_SRC_IOCLK_8;
1050 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1051 if (tmp > 0 && tmp <= 255)
1054 port->uart.uartclk = ioclk / 32;
1055 tmxmd = TM2MD_SRC_IOCLK_32;
1056 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1057 if (tmp > 0 && tmp <= 255)
1066 /* refuse to change to a baud rate we can't support */
1067 _debug("CAN'T SUPPORT");
1072 new->c_cflag &= ~CBAUD;
1073 new->c_cflag |= (old->c_cflag & CBAUD);
1075 goto try_alternative;
1079 /* as a last resort, if the quotient is zero, default to 9600
1081 new->c_cflag &= ~CBAUD;
1082 new->c_cflag |= B9600;
1084 goto try_alternative;
1087 /* hmmm... can't seem to support 9600 either
1088 * - we could try iterating through the speeds we know about to
1091 new->c_cflag &= ~CBAUD;
1094 if (div_timer == MNSCx_DIV_TIMER_16BIT)
1095 tmxmd = TM8MD_SRC_IOCLK_32;
1096 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
1097 tmxmd = TM2MD_SRC_IOCLK_32;
1100 port->uart.uartclk = ioclk / 32;
1105 _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
1107 /* make the changes */
1108 spin_lock_irqsave(&port->uart.lock, flags);
1110 uart_update_timeout(&port->uart, new->c_cflag, baud);
1112 /* set the timer to produce the required baud rate */
1113 switch (div_timer) {
1114 case MNSCx_DIV_TIMER_16BIT:
1116 *port->_tmxbr = tmxbr;
1117 *port->_tmxmd = TM8MD_INIT_COUNTER;
1118 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1121 case MNSCx_DIV_TIMER_8BIT:
1123 *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
1124 *port->_tmxmd = TM2MD_INIT_COUNTER;
1125 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1129 /* CTS flow control flag and modem status interrupts */
1130 scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
1132 if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
1133 /* want to interrupt when CTS goes low if CTS is now
1134 * high and vice versa
1136 port->tx_cts = *port->_status;
1138 if (port->tx_cts & SC2STR_CTS)
1139 scxctr |= SC2CTR_TWE;
1141 scxctr |= SC2CTR_TWE | SC2CTR_TWS;
1144 /* set up parity check flag */
1145 port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
1146 if (new->c_iflag & INPCK)
1147 port->uart.read_status_mask |=
1148 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1149 if (new->c_iflag & (BRKINT | PARMRK))
1150 port->uart.read_status_mask |= (1 << TTY_BREAK);
1152 /* characters to ignore */
1153 port->uart.ignore_status_mask = 0;
1154 if (new->c_iflag & IGNPAR)
1155 port->uart.ignore_status_mask |=
1156 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1157 if (new->c_iflag & IGNBRK) {
1158 port->uart.ignore_status_mask |= (1 << TTY_BREAK);
1160 * If we're ignoring parity and break indicators,
1161 * ignore overruns to (for real raw support).
1163 if (new->c_iflag & IGNPAR)
1164 port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
1167 /* Ignore all characters if CREAD is not set */
1168 if ((new->c_cflag & CREAD) == 0)
1169 port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
1171 scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1172 *port->_control = scxctr;
1174 spin_unlock_irqrestore(&port->uart.lock, flags);
1178 * set the terminal I/O parameters
1180 static void mn10300_serial_set_termios(struct uart_port *_port,
1181 struct ktermios *new,
1182 struct ktermios *old)
1184 struct mn10300_serial_port *port =
1185 container_of(_port, struct mn10300_serial_port, uart);
1187 _enter("%s,%p,%p", port->name, new, old);
1189 mn10300_serial_change_speed(port, new, old);
1191 /* handle turning off CRTSCTS */
1192 if (!(new->c_cflag & CRTSCTS)) {
1193 u16 ctr = *port->_control;
1195 *port->_control = ctr;
1200 * return description of port type
1202 static const char *mn10300_serial_type(struct uart_port *_port)
1204 struct mn10300_serial_port *port =
1205 container_of(_port, struct mn10300_serial_port, uart);
1207 if (port->uart.type == PORT_MN10300_CTS)
1208 return "MN10300 SIF_CTS";
1210 return "MN10300 SIF";
1214 * release I/O and memory regions in use by port
1216 static void mn10300_serial_release_port(struct uart_port *_port)
1218 struct mn10300_serial_port *port =
1219 container_of(_port, struct mn10300_serial_port, uart);
1221 _enter("%s", port->name);
1223 release_mem_region((unsigned long) port->_iobase, 16);
1227 * request I/O and memory regions for port
1229 static int mn10300_serial_request_port(struct uart_port *_port)
1231 struct mn10300_serial_port *port =
1232 container_of(_port, struct mn10300_serial_port, uart);
1234 _enter("%s", port->name);
1236 request_mem_region((unsigned long) port->_iobase, 16, port->name);
1241 * configure the type and reserve the ports
1243 static void mn10300_serial_config_port(struct uart_port *_port, int type)
1245 struct mn10300_serial_port *port =
1246 container_of(_port, struct mn10300_serial_port, uart);
1248 _enter("%s", port->name);
1250 port->uart.type = PORT_MN10300;
1252 if (port->options & MNSCx_OPT_CTS)
1253 port->uart.type = PORT_MN10300_CTS;
1255 mn10300_serial_request_port(_port);
1259 * verify serial parameters are suitable for this port type
1261 static int mn10300_serial_verify_port(struct uart_port *_port,
1262 struct serial_struct *ss)
1264 struct mn10300_serial_port *port =
1265 container_of(_port, struct mn10300_serial_port, uart);
1266 void *mapbase = (void *) (unsigned long) port->uart.mapbase;
1268 _enter("%s", port->name);
1270 /* these things may not be changed */
1271 if (ss->irq != port->uart.irq ||
1272 ss->port != port->uart.iobase ||
1273 ss->io_type != port->uart.iotype ||
1274 ss->iomem_base != mapbase ||
1275 ss->iomem_reg_shift != port->uart.regshift ||
1276 ss->hub6 != port->uart.hub6 ||
1277 ss->xmit_fifo_size != port->uart.fifosize)
1280 /* type may be changed on a port that supports CTS */
1281 if (ss->type != port->uart.type) {
1282 if (!(port->options & MNSCx_OPT_CTS))
1285 if (ss->type != PORT_MN10300 &&
1286 ss->type != PORT_MN10300_CTS)
1294 * initialise the MN10300 on-chip UARTs
1296 static int __init mn10300_serial_init(void)
1298 struct mn10300_serial_port *port;
1301 printk(KERN_INFO "%s version %s (%s)\n",
1302 serial_name, serial_version, serial_revdate);
1304 #ifdef CONFIG_MN10300_TTYSM2
1305 SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
1308 set_intr_stub(EXCEP_IRQ_LEVEL1, mn10300_serial_vdma_interrupt);
1310 ret = uart_register_driver(&mn10300_serial_driver);
1312 for (i = 0 ; i < NR_PORTS ; i++) {
1313 port = mn10300_serial_ports[i];
1314 if (!port || port->gdbstub)
1317 switch (port->clock_src) {
1318 case MNSCx_CLOCK_SRC_IOCLK:
1319 port->ioclk = MN10300_IOCLK;
1322 #ifdef MN10300_IOBCLK
1323 case MNSCx_CLOCK_SRC_IOBCLK:
1324 port->ioclk = MN10300_IOBCLK;
1331 ret = uart_add_one_port(&mn10300_serial_driver,
1335 _debug("ERROR %d", -ret);
1341 uart_unregister_driver(&mn10300_serial_driver);
1347 __initcall(mn10300_serial_init);
1350 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
1353 * print a string to the serial port without disturbing the real user of the
1355 * - the console must be locked by the caller
1357 static void mn10300_serial_console_write(struct console *co,
1358 const char *s, unsigned count)
1360 struct mn10300_serial_port *port;
1362 u16 scxctr, txicr, tmp;
1365 port = mn10300_serial_ports[co->index];
1367 /* firstly hijack the serial port from the "virtual DMA" controller */
1368 txicr = *port->tx_icr;
1369 *port->tx_icr = GxICR_LEVEL_1;
1370 tmp = *port->tx_icr;
1372 /* the transmitter may be disabled */
1373 scxctr = *port->_control;
1374 if (!(scxctr & SC01CTR_TXE)) {
1375 /* restart the UART clock */
1376 tmxmd = *port->_tmxmd;
1378 switch (port->div_timer) {
1379 case MNSCx_DIV_TIMER_16BIT:
1381 *port->_tmxmd = TM8MD_INIT_COUNTER;
1382 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1385 case MNSCx_DIV_TIMER_8BIT:
1387 *port->_tmxmd = TM2MD_INIT_COUNTER;
1388 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1392 /* enable the transmitter */
1393 *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
1395 } else if (scxctr & SC01CTR_BKE) {
1396 /* stop transmitting BREAK */
1397 *port->_control = (scxctr & ~SC01CTR_BKE);
1400 /* send the chars into the serial port (with LF -> LFCR conversion) */
1401 for (i = 0; i < count; i++) {
1404 while (*port->_status & SC01STR_TBF)
1406 *(u8 *) port->_txb = ch;
1409 while (*port->_status & SC01STR_TBF)
1411 *(u8 *) port->_txb = 0xd;
1415 /* can't let the transmitter be turned off if it's actually
1417 while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
1420 /* disable the transmitter if we re-enabled it */
1421 if (!(scxctr & SC01CTR_TXE))
1422 *port->_control = scxctr;
1424 *port->tx_icr = txicr;
1425 tmp = *port->tx_icr;
1429 * set up a serial port as a console
1430 * - construct a cflag setting for the first rs_open()
1431 * - initialize the serial port
1432 * - return non-zero if we didn't find a serial port.
1434 static int __init mn10300_serial_console_setup(struct console *co,
1437 struct mn10300_serial_port *port;
1438 int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
1440 for (i = 0 ; i < NR_PORTS ; i++) {
1441 port = mn10300_serial_ports[i];
1442 if (port && !port->gdbstub && port->uart.line == co->index)
1449 switch (port->clock_src) {
1450 case MNSCx_CLOCK_SRC_IOCLK:
1451 port->ioclk = MN10300_IOCLK;
1454 #ifdef MN10300_IOBCLK
1455 case MNSCx_CLOCK_SRC_IOBCLK:
1456 port->ioclk = MN10300_IOBCLK;
1464 uart_parse_options(options, &baud, &parity, &bits, &flow);
1466 return uart_set_options(&port->uart, co, baud, parity, bits, flow);
1472 static int __init mn10300_serial_console_init(void)
1474 register_console(&mn10300_serial_console);
1478 console_initcall(mn10300_serial_console_init);