2 * drivers/serial/sh-sci.c
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 2002 - 2008 Paul Mundt
7 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
9 * based off of the old drivers/char/sh-sci.c by:
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * Removed SH7300 support (Jul 2007).
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
22 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/sysrq.h>
38 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/console.h>
43 #include <linux/platform_device.h>
44 #include <linux/serial_sci.h>
45 #include <linux/notifier.h>
46 #include <linux/cpufreq.h>
47 #include <linux/clk.h>
48 #include <linux/ctype.h>
49 #include <linux/err.h>
52 #include <asm/clock.h>
53 #include <asm/sh_bios.h>
59 struct uart_port port;
64 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
65 unsigned int irqs[SCIx_NR_IRQS];
67 /* Port pin configuration */
68 void (*init_pins)(struct uart_port *port,
71 /* Port enable callback */
72 void (*enable)(struct uart_port *port);
74 /* Port disable callback */
75 void (*disable)(struct uart_port *port);
78 struct timer_list break_timer;
81 #ifdef CONFIG_HAVE_CLK
87 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
88 static struct sci_port *serial_console_port;
91 /* Function prototypes */
92 static void sci_stop_tx(struct uart_port *port);
94 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
96 static struct sci_port sci_ports[SCI_NPORTS];
97 static struct uart_driver sci_uart_driver;
99 static inline struct sci_port *
100 to_sci_port(struct uart_port *uart)
102 return container_of(uart, struct sci_port, port);
105 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
106 static inline void handle_error(struct uart_port *port)
108 /* Clear error flags */
109 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
112 static int sci_poll_get_char(struct uart_port *port)
114 unsigned short status;
118 status = sci_in(port, SCxSR);
119 if (status & SCxSR_ERRORS(port)) {
123 } while (!(status & SCxSR_RDxF(port)));
125 c = sci_in(port, SCxRDR);
129 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
134 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
136 unsigned short status;
139 status = sci_in(port, SCxSR);
140 } while (!(status & SCxSR_TDxE(port)));
142 sci_in(port, SCxSR); /* Dummy read */
143 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
144 sci_out(port, SCxTDR, c);
146 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
148 #if defined(__H8300S__)
149 enum { sci_disable, sci_enable };
151 static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
153 volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
154 int ch = (port->mapbase - SMR0) >> 3;
155 unsigned char mask = 1 << (ch+1);
157 if (ctrl == sci_disable)
163 static inline void h8300_sci_enable(struct uart_port *port)
165 h8300_sci_config(port, sci_enable);
168 static inline void h8300_sci_disable(struct uart_port *port)
170 h8300_sci_config(port, sci_disable);
174 #if defined(__H8300H__) || defined(__H8300S__)
175 static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
177 int ch = (port->mapbase - SMR0) >> 3;
180 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
181 h8300_sci_pins[ch].rx,
183 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
184 h8300_sci_pins[ch].tx,
188 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
191 #define sci_init_pins_sci NULL
194 #if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
195 static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
197 unsigned int fcr_val = 0;
200 fcr_val |= SCFCR_MCE;
202 sci_out(port, SCFCR, fcr_val);
205 #define sci_init_pins_irda NULL
208 #if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
209 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
211 unsigned int fcr_val = 0;
213 set_sh771x_scif_pfc(port);
215 fcr_val |= SCFCR_MCE;
216 sci_out(port, SCFCR, fcr_val);
218 #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
219 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
221 unsigned int fcr_val = 0;
224 if (cflag & CRTSCTS) {
226 if (port->mapbase == 0xa4430000) { /* SCIF0 */
227 /* Clear PTCR bit 9-2; enable all scif pins but sck */
228 data = ctrl_inw(PORT_PTCR);
229 ctrl_outw((data & 0xfc03), PORT_PTCR);
230 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
231 /* Clear PVCR bit 9-2 */
232 data = ctrl_inw(PORT_PVCR);
233 ctrl_outw((data & 0xfc03), PORT_PVCR);
235 fcr_val |= SCFCR_MCE;
237 if (port->mapbase == 0xa4430000) { /* SCIF0 */
238 /* Clear PTCR bit 5-2; enable only tx and rx */
239 data = ctrl_inw(PORT_PTCR);
240 ctrl_outw((data & 0xffc3), PORT_PTCR);
241 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
242 /* Clear PVCR bit 5-2 */
243 data = ctrl_inw(PORT_PVCR);
244 ctrl_outw((data & 0xffc3), PORT_PVCR);
247 sci_out(port, SCFCR, fcr_val);
249 #elif defined(CONFIG_CPU_SH3)
250 /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
251 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
253 unsigned int fcr_val = 0;
256 /* We need to set SCPCR to enable RTS/CTS */
257 data = ctrl_inw(SCPCR);
258 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
259 ctrl_outw(data & 0x0fcf, SCPCR);
262 fcr_val |= SCFCR_MCE;
264 /* We need to set SCPCR to enable RTS/CTS */
265 data = ctrl_inw(SCPCR);
266 /* Clear out SCP7MD1,0, SCP4MD1,0,
267 Set SCP6MD1,0 = {01} (output) */
268 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
270 data = ctrl_inb(SCPDR);
271 /* Set /RTS2 (bit6) = 0 */
272 ctrl_outb(data & 0xbf, SCPDR);
275 sci_out(port, SCFCR, fcr_val);
277 #elif defined(CONFIG_CPU_SUBTYPE_SH7722)
278 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
280 unsigned int fcr_val = 0;
283 if (port->mapbase == 0xffe00000) {
284 data = ctrl_inw(PSCR);
287 fcr_val |= SCFCR_MCE;
291 ctrl_outw(data, PSCR);
293 /* SCIF1 and SCIF2 should be setup by board code */
295 sci_out(port, SCFCR, fcr_val);
297 #elif defined(CONFIG_CPU_SUBTYPE_SH7723)
298 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
300 /* Nothing to do here.. */
301 sci_out(port, SCFCR, 0);
305 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
307 unsigned int fcr_val = 0;
309 if (cflag & CRTSCTS) {
310 fcr_val |= SCFCR_MCE;
312 #if defined(CONFIG_CPU_SUBTYPE_SH7343) || defined(CONFIG_CPU_SUBTYPE_SH7366)
314 #elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
315 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
316 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
317 defined(CONFIG_CPU_SUBTYPE_SHX3)
318 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
320 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
323 sci_out(port, SCFCR, fcr_val);
327 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
328 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
329 defined(CONFIG_CPU_SUBTYPE_SH7785)
330 static inline int scif_txroom(struct uart_port *port)
332 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
335 static inline int scif_rxroom(struct uart_port *port)
337 return sci_in(port, SCRFDR) & 0xff;
339 #elif defined(CONFIG_CPU_SUBTYPE_SH7763)
340 static inline int scif_txroom(struct uart_port *port)
342 if ((port->mapbase == 0xffe00000) ||
343 (port->mapbase == 0xffe08000)) {
345 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
348 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
352 static inline int scif_rxroom(struct uart_port *port)
354 if ((port->mapbase == 0xffe00000) ||
355 (port->mapbase == 0xffe08000)) {
357 return sci_in(port, SCRFDR) & 0xff;
360 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
364 static inline int scif_txroom(struct uart_port *port)
366 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
369 static inline int scif_rxroom(struct uart_port *port)
371 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
375 static inline int sci_txroom(struct uart_port *port)
377 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
380 static inline int sci_rxroom(struct uart_port *port)
382 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
385 /* ********************************************************************** *
386 * the interrupt related routines *
387 * ********************************************************************** */
389 static void sci_transmit_chars(struct uart_port *port)
391 struct circ_buf *xmit = &port->info->xmit;
392 unsigned int stopped = uart_tx_stopped(port);
393 unsigned short status;
397 status = sci_in(port, SCxSR);
398 if (!(status & SCxSR_TDxE(port))) {
399 ctrl = sci_in(port, SCSCR);
400 if (uart_circ_empty(xmit))
401 ctrl &= ~SCI_CTRL_FLAGS_TIE;
403 ctrl |= SCI_CTRL_FLAGS_TIE;
404 sci_out(port, SCSCR, ctrl);
408 if (port->type == PORT_SCI)
409 count = sci_txroom(port);
411 count = scif_txroom(port);
419 } else if (!uart_circ_empty(xmit) && !stopped) {
420 c = xmit->buf[xmit->tail];
421 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
426 sci_out(port, SCxTDR, c);
429 } while (--count > 0);
431 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
433 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
434 uart_write_wakeup(port);
435 if (uart_circ_empty(xmit)) {
438 ctrl = sci_in(port, SCSCR);
440 if (port->type != PORT_SCI) {
441 sci_in(port, SCxSR); /* Dummy read */
442 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
445 ctrl |= SCI_CTRL_FLAGS_TIE;
446 sci_out(port, SCSCR, ctrl);
450 /* On SH3, SCIF may read end-of-break as a space->mark char */
451 #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
453 static inline void sci_receive_chars(struct uart_port *port)
455 struct sci_port *sci_port = to_sci_port(port);
456 struct tty_struct *tty = port->info->port.tty;
457 int i, count, copied = 0;
458 unsigned short status;
461 status = sci_in(port, SCxSR);
462 if (!(status & SCxSR_RDxF(port)))
466 if (port->type == PORT_SCI)
467 count = sci_rxroom(port);
469 count = scif_rxroom(port);
471 /* Don't copy more bytes than there is room for in the buffer */
472 count = tty_buffer_request_room(tty, count);
474 /* If for any reason we can't copy more data, we're done! */
478 if (port->type == PORT_SCI) {
479 char c = sci_in(port, SCxRDR);
480 if (uart_handle_sysrq_char(port, c) ||
481 sci_port->break_flag)
484 tty_insert_flip_char(tty, c, TTY_NORMAL);
486 for (i = 0; i < count; i++) {
487 char c = sci_in(port, SCxRDR);
488 status = sci_in(port, SCxSR);
489 #if defined(CONFIG_CPU_SH3)
490 /* Skip "chars" during break */
491 if (sci_port->break_flag) {
493 (status & SCxSR_FER(port))) {
498 /* Nonzero => end-of-break */
499 dev_dbg(port->dev, "debounce<%02x>\n", c);
500 sci_port->break_flag = 0;
507 #endif /* CONFIG_CPU_SH3 */
508 if (uart_handle_sysrq_char(port, c)) {
513 /* Store data and status */
514 if (status&SCxSR_FER(port)) {
516 dev_notice(port->dev, "frame error\n");
517 } else if (status&SCxSR_PER(port)) {
519 dev_notice(port->dev, "parity error\n");
523 tty_insert_flip_char(tty, c, flag);
527 sci_in(port, SCxSR); /* dummy read */
528 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
531 port->icount.rx += count;
535 /* Tell the rest of the system the news. New characters! */
536 tty_flip_buffer_push(tty);
538 sci_in(port, SCxSR); /* dummy read */
539 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
543 #define SCI_BREAK_JIFFIES (HZ/20)
544 /* The sci generates interrupts during the break,
545 * 1 per millisecond or so during the break period, for 9600 baud.
546 * So dont bother disabling interrupts.
547 * But dont want more than 1 break event.
548 * Use a kernel timer to periodically poll the rx line until
549 * the break is finished.
551 static void sci_schedule_break_timer(struct sci_port *port)
553 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
554 add_timer(&port->break_timer);
556 /* Ensure that two consecutive samples find the break over. */
557 static void sci_break_timer(unsigned long data)
559 struct sci_port *port = (struct sci_port *)data;
561 if (sci_rxd_in(&port->port) == 0) {
562 port->break_flag = 1;
563 sci_schedule_break_timer(port);
564 } else if (port->break_flag == 1) {
566 port->break_flag = 2;
567 sci_schedule_break_timer(port);
569 port->break_flag = 0;
572 static inline int sci_handle_errors(struct uart_port *port)
575 unsigned short status = sci_in(port, SCxSR);
576 struct tty_struct *tty = port->info->port.tty;
578 if (status & SCxSR_ORER(port)) {
580 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
583 dev_notice(port->dev, "overrun error");
586 if (status & SCxSR_FER(port)) {
587 if (sci_rxd_in(port) == 0) {
588 /* Notify of BREAK */
589 struct sci_port *sci_port = to_sci_port(port);
591 if (!sci_port->break_flag) {
592 sci_port->break_flag = 1;
593 sci_schedule_break_timer(sci_port);
595 /* Do sysrq handling. */
596 if (uart_handle_break(port))
599 dev_dbg(port->dev, "BREAK detected\n");
601 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
607 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
610 dev_notice(port->dev, "frame error\n");
614 if (status & SCxSR_PER(port)) {
616 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
619 dev_notice(port->dev, "parity error");
623 tty_flip_buffer_push(tty);
628 static inline int sci_handle_breaks(struct uart_port *port)
631 unsigned short status = sci_in(port, SCxSR);
632 struct tty_struct *tty = port->info->port.tty;
633 struct sci_port *s = &sci_ports[port->line];
635 if (uart_handle_break(port))
638 if (!s->break_flag && status & SCxSR_BRK(port)) {
639 #if defined(CONFIG_CPU_SH3)
643 /* Notify of BREAK */
644 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
647 dev_dbg(port->dev, "BREAK detected\n");
650 #if defined(SCIF_ORER)
651 /* XXX: Handle SCIF overrun error */
652 if (port->type != PORT_SCI && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
653 sci_out(port, SCLSR, 0);
654 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
656 dev_notice(port->dev, "overrun error\n");
662 tty_flip_buffer_push(tty);
667 static irqreturn_t sci_rx_interrupt(int irq, void *port)
669 /* I think sci_receive_chars has to be called irrespective
670 * of whether the I_IXOFF is set, otherwise, how is the interrupt
673 sci_receive_chars(port);
678 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
680 struct uart_port *port = ptr;
682 spin_lock_irq(&port->lock);
683 sci_transmit_chars(port);
684 spin_unlock_irq(&port->lock);
689 static irqreturn_t sci_er_interrupt(int irq, void *ptr)
691 struct uart_port *port = ptr;
694 if (port->type == PORT_SCI) {
695 if (sci_handle_errors(port)) {
696 /* discard character in rx buffer */
698 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
701 #if defined(SCIF_ORER)
702 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
703 struct tty_struct *tty = port->info->port.tty;
705 sci_out(port, SCLSR, 0);
706 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
707 tty_flip_buffer_push(tty);
708 dev_notice(port->dev, "overrun error\n");
711 sci_rx_interrupt(irq, ptr);
714 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
716 /* Kick the transmission */
717 sci_tx_interrupt(irq, ptr);
722 static irqreturn_t sci_br_interrupt(int irq, void *ptr)
724 struct uart_port *port = ptr;
727 sci_handle_breaks(port);
728 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
733 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
735 unsigned short ssr_status, scr_status;
736 struct uart_port *port = ptr;
737 irqreturn_t ret = IRQ_NONE;
739 ssr_status = sci_in(port, SCxSR);
740 scr_status = sci_in(port, SCSCR);
743 if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
744 ret = sci_tx_interrupt(irq, ptr);
746 if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
747 ret = sci_rx_interrupt(irq, ptr);
748 /* Error Interrupt */
749 if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
750 ret = sci_er_interrupt(irq, ptr);
751 /* Break Interrupt */
752 if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
753 ret = sci_br_interrupt(irq, ptr);
758 #ifdef CONFIG_HAVE_CLK
760 * Here we define a transistion notifier so that we can update all of our
761 * ports' baud rate when the peripheral clock changes.
763 static int sci_notifier(struct notifier_block *self,
764 unsigned long phase, void *p)
768 if ((phase == CPUFREQ_POSTCHANGE) ||
769 (phase == CPUFREQ_RESUMECHANGE))
770 for (i = 0; i < SCI_NPORTS; i++) {
771 struct sci_port *s = &sci_ports[i];
772 s->port.uartclk = clk_get_rate(s->clk);
778 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
781 static int sci_request_irq(struct sci_port *port)
784 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
785 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
788 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
789 "SCI Transmit Data Empty", "SCI Break" };
791 if (port->irqs[0] == port->irqs[1]) {
792 if (unlikely(!port->irqs[0]))
795 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
796 IRQF_DISABLED, "sci", port)) {
797 dev_err(port->port.dev, "Can't allocate IRQ\n");
801 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
802 if (unlikely(!port->irqs[i]))
805 if (request_irq(port->irqs[i], handlers[i],
806 IRQF_DISABLED, desc[i], port)) {
807 dev_err(port->port.dev, "Can't allocate IRQ\n");
816 static void sci_free_irq(struct sci_port *port)
820 if (port->irqs[0] == port->irqs[1])
821 free_irq(port->irqs[0], port);
823 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
827 free_irq(port->irqs[i], port);
832 static unsigned int sci_tx_empty(struct uart_port *port)
838 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
840 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
841 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
842 /* If you have signals for DTR and DCD, please implement here. */
845 static unsigned int sci_get_mctrl(struct uart_port *port)
847 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
850 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
853 static void sci_start_tx(struct uart_port *port)
857 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
858 ctrl = sci_in(port, SCSCR);
859 ctrl |= SCI_CTRL_FLAGS_TIE;
860 sci_out(port, SCSCR, ctrl);
863 static void sci_stop_tx(struct uart_port *port)
867 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
868 ctrl = sci_in(port, SCSCR);
869 ctrl &= ~SCI_CTRL_FLAGS_TIE;
870 sci_out(port, SCSCR, ctrl);
873 static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
877 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
878 ctrl = sci_in(port, SCSCR);
879 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
880 sci_out(port, SCSCR, ctrl);
883 static void sci_stop_rx(struct uart_port *port)
887 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
888 ctrl = sci_in(port, SCSCR);
889 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
890 sci_out(port, SCSCR, ctrl);
893 static void sci_enable_ms(struct uart_port *port)
895 /* Nothing here yet .. */
898 static void sci_break_ctl(struct uart_port *port, int break_state)
900 /* Nothing here yet .. */
903 static int sci_startup(struct uart_port *port)
905 struct sci_port *s = &sci_ports[port->line];
910 #ifdef CONFIG_HAVE_CLK
911 s->clk = clk_get(NULL, "module_clk");
916 sci_start_rx(port, 1);
921 static void sci_shutdown(struct uart_port *port)
923 struct sci_port *s = &sci_ports[port->line];
932 #ifdef CONFIG_HAVE_CLK
938 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
939 struct ktermios *old)
941 struct sci_port *s = &sci_ports[port->line];
942 unsigned int status, baud, smr_val;
945 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
947 t = SCBRR_VALUE(baud, port->uartclk);
950 status = sci_in(port, SCxSR);
951 } while (!(status & SCxSR_TEND(port)));
953 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
955 if (port->type != PORT_SCI)
956 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
958 smr_val = sci_in(port, SCSMR) & 3;
959 if ((termios->c_cflag & CSIZE) == CS7)
961 if (termios->c_cflag & PARENB)
963 if (termios->c_cflag & PARODD)
965 if (termios->c_cflag & CSTOPB)
968 uart_update_timeout(port, termios->c_cflag, baud);
970 sci_out(port, SCSMR, smr_val);
974 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
977 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
979 sci_out(port, SCBRR, t);
980 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
983 if (likely(s->init_pins))
984 s->init_pins(port, termios->c_cflag);
986 sci_out(port, SCSCR, SCSCR_INIT(port));
988 if ((termios->c_cflag & CREAD) != 0)
989 sci_start_rx(port, 0);
992 static const char *sci_type(struct uart_port *port)
994 switch (port->type) {
1008 static void sci_release_port(struct uart_port *port)
1010 /* Nothing here yet .. */
1013 static int sci_request_port(struct uart_port *port)
1015 /* Nothing here yet .. */
1019 static void sci_config_port(struct uart_port *port, int flags)
1021 struct sci_port *s = &sci_ports[port->line];
1023 port->type = s->type;
1025 switch (port->type) {
1027 s->init_pins = sci_init_pins_sci;
1031 s->init_pins = sci_init_pins_scif;
1034 s->init_pins = sci_init_pins_irda;
1038 if (port->flags & UPF_IOREMAP && !port->membase) {
1039 #if defined(CONFIG_SUPERH64)
1040 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1041 port->membase = (void __iomem *)port->mapbase;
1043 port->membase = ioremap_nocache(port->mapbase, 0x40);
1046 dev_err(port->dev, "can't remap port#%d\n", port->line);
1050 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1052 struct sci_port *s = &sci_ports[port->line];
1054 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
1056 if (ser->baud_base < 2400)
1057 /* No paper tape reader for Mitch.. */
1063 static struct uart_ops sci_uart_ops = {
1064 .tx_empty = sci_tx_empty,
1065 .set_mctrl = sci_set_mctrl,
1066 .get_mctrl = sci_get_mctrl,
1067 .start_tx = sci_start_tx,
1068 .stop_tx = sci_stop_tx,
1069 .stop_rx = sci_stop_rx,
1070 .enable_ms = sci_enable_ms,
1071 .break_ctl = sci_break_ctl,
1072 .startup = sci_startup,
1073 .shutdown = sci_shutdown,
1074 .set_termios = sci_set_termios,
1076 .release_port = sci_release_port,
1077 .request_port = sci_request_port,
1078 .config_port = sci_config_port,
1079 .verify_port = sci_verify_port,
1080 #ifdef CONFIG_CONSOLE_POLL
1081 .poll_get_char = sci_poll_get_char,
1082 .poll_put_char = sci_poll_put_char,
1086 static void __init sci_init_ports(void)
1088 static int first = 1;
1096 for (i = 0; i < SCI_NPORTS; i++) {
1097 sci_ports[i].port.ops = &sci_uart_ops;
1098 sci_ports[i].port.iotype = UPIO_MEM;
1099 sci_ports[i].port.line = i;
1100 sci_ports[i].port.fifosize = 1;
1102 #if defined(__H8300H__) || defined(__H8300S__)
1104 sci_ports[i].enable = h8300_sci_enable;
1105 sci_ports[i].disable = h8300_sci_disable;
1107 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1108 #elif defined(CONFIG_HAVE_CLK)
1110 * XXX: We should use a proper SCI/SCIF clock
1113 struct clk *clk = clk_get(NULL, "module_clk");
1114 sci_ports[i].port.uartclk = clk_get_rate(clk);
1118 #error "Need a valid uartclk"
1121 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1122 sci_ports[i].break_timer.function = sci_break_timer;
1124 init_timer(&sci_ports[i].break_timer);
1128 int __init early_sci_setup(struct uart_port *port)
1130 if (unlikely(port->line > SCI_NPORTS))
1135 sci_ports[port->line].port.membase = port->membase;
1136 sci_ports[port->line].port.mapbase = port->mapbase;
1137 sci_ports[port->line].port.type = port->type;
1142 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1144 * Print a string to the serial port trying not to disturb
1145 * any possible real use of the port...
1147 static void serial_console_write(struct console *co, const char *s,
1150 struct uart_port *port = &serial_console_port->port;
1153 for (i = 0; i < count; i++) {
1155 sci_poll_put_char(port, '\r');
1157 sci_poll_put_char(port, *s++);
1161 static int __init serial_console_setup(struct console *co, char *options)
1163 struct uart_port *port;
1171 * Check whether an invalid uart number has been specified, and
1172 * if so, search for the first available port that does have
1175 if (co->index >= SCI_NPORTS)
1178 serial_console_port = &sci_ports[co->index];
1179 port = &serial_console_port->port;
1182 * Also need to check port->type, we don't actually have any
1183 * UPIO_PORT ports, but uart_report_port() handily misreports
1184 * it anyways if we don't have a port available by the time this is
1189 if (!port->membase || !port->mapbase)
1192 port->type = serial_console_port->type;
1194 #ifdef CONFIG_HAVE_CLK
1195 if (!serial_console_port->clk)
1196 serial_console_port->clk = clk_get(NULL, "module_clk");
1199 if (port->flags & UPF_IOREMAP)
1200 sci_config_port(port, 0);
1202 if (serial_console_port->enable)
1203 serial_console_port->enable(port);
1206 uart_parse_options(options, &baud, &parity, &bits, &flow);
1208 ret = uart_set_options(port, co, baud, parity, bits, flow);
1209 #if defined(__H8300H__) || defined(__H8300S__)
1210 /* disable rx interrupt */
1217 static struct console serial_console = {
1219 .device = uart_console_device,
1220 .write = serial_console_write,
1221 .setup = serial_console_setup,
1222 .flags = CON_PRINTBUFFER,
1224 .data = &sci_uart_driver,
1227 static int __init sci_console_init(void)
1230 register_console(&serial_console);
1233 console_initcall(sci_console_init);
1234 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1236 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1237 #define SCI_CONSOLE (&serial_console)
1239 #define SCI_CONSOLE 0
1242 static char banner[] __initdata =
1243 KERN_INFO "SuperH SCI(F) driver initialized\n";
1245 static struct uart_driver sci_uart_driver = {
1246 .owner = THIS_MODULE,
1247 .driver_name = "sci",
1248 .dev_name = "ttySC",
1250 .minor = SCI_MINOR_START,
1252 .cons = SCI_CONSOLE,
1256 * Register a set of serial devices attached to a platform device. The
1257 * list is terminated with a zero flags entry, which means we expect
1258 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1259 * remapping (such as sh64) should also set UPF_IOREMAP.
1261 static int __devinit sci_probe(struct platform_device *dev)
1263 struct plat_sci_port *p = dev->dev.platform_data;
1264 int i, ret = -EINVAL;
1266 for (i = 0; p && p->flags != 0; p++, i++) {
1267 struct sci_port *sciport = &sci_ports[i];
1270 if (unlikely(i == SCI_NPORTS)) {
1271 dev_notice(&dev->dev, "Attempting to register port "
1272 "%d when only %d are available.\n",
1274 dev_notice(&dev->dev, "Consider bumping "
1275 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1279 sciport->port.mapbase = p->mapbase;
1281 if (p->mapbase && !p->membase) {
1282 if (p->flags & UPF_IOREMAP) {
1283 p->membase = ioremap_nocache(p->mapbase, 0x40);
1284 if (IS_ERR(p->membase)) {
1285 ret = PTR_ERR(p->membase);
1290 * For the simple (and majority of) cases
1291 * where we don't need to do any remapping,
1292 * just cast the cookie directly.
1294 p->membase = (void __iomem *)p->mapbase;
1298 sciport->port.membase = p->membase;
1300 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1301 sciport->port.flags = p->flags;
1302 sciport->port.dev = &dev->dev;
1304 sciport->type = sciport->port.type = p->type;
1306 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1308 uart_add_one_port(&sci_uart_driver, &sciport->port);
1311 #ifdef CONFIG_HAVE_CLK
1312 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1315 #ifdef CONFIG_SH_STANDARD_BIOS
1316 sh_bios_gdb_detach();
1322 for (i = i - 1; i >= 0; i--)
1323 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1328 static int __devexit sci_remove(struct platform_device *dev)
1332 #ifdef CONFIG_HAVE_CLK
1333 cpufreq_unregister_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1336 for (i = 0; i < SCI_NPORTS; i++)
1337 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1342 static int sci_suspend(struct platform_device *dev, pm_message_t state)
1346 for (i = 0; i < SCI_NPORTS; i++) {
1347 struct sci_port *p = &sci_ports[i];
1349 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1350 uart_suspend_port(&sci_uart_driver, &p->port);
1356 static int sci_resume(struct platform_device *dev)
1360 for (i = 0; i < SCI_NPORTS; i++) {
1361 struct sci_port *p = &sci_ports[i];
1363 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1364 uart_resume_port(&sci_uart_driver, &p->port);
1370 static struct platform_driver sci_driver = {
1372 .remove = __devexit_p(sci_remove),
1373 .suspend = sci_suspend,
1374 .resume = sci_resume,
1377 .owner = THIS_MODULE,
1381 static int __init sci_init(void)
1389 ret = uart_register_driver(&sci_uart_driver);
1390 if (likely(ret == 0)) {
1391 ret = platform_driver_register(&sci_driver);
1393 uart_unregister_driver(&sci_uart_driver);
1399 static void __exit sci_exit(void)
1401 platform_driver_unregister(&sci_driver);
1402 uart_unregister_driver(&sci_uart_driver);
1405 module_init(sci_init);
1406 module_exit(sci_exit);
1408 MODULE_LICENSE("GPL");
1409 MODULE_ALIAS("platform:sh-sci");