2 * drivers/serial/sh-sci.c
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 2002 - 2006 Paul Mundt
8 * based off of the old drivers/char/sh-sci.c by:
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/timer.h>
26 #include <linux/interrupt.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/major.h>
31 #include <linux/string.h>
32 #include <linux/sysrq.h>
33 #include <linux/ioport.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/console.h>
38 #include <linux/platform_device.h>
40 #ifdef CONFIG_CPU_FREQ
41 #include <linux/notifier.h>
42 #include <linux/cpufreq.h>
45 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
46 #include <asm/clock.h>
47 #include <asm/sh_bios.h>
53 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
60 struct uart_port port;
65 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
66 unsigned int irqs[SCIx_NR_IRQS];
68 /* Port pin configuration */
69 void (*init_pins)(struct uart_port *port,
72 /* Port enable callback */
73 void (*enable)(struct uart_port *port);
75 /* Port disable callback */
76 void (*disable)(struct uart_port *port);
79 struct timer_list break_timer;
84 static struct sci_port *kgdb_sci_port;
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 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
100 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
101 static inline void handle_error(struct uart_port *port)
103 /* Clear error flags */
104 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
107 static int get_char(struct uart_port *port)
110 unsigned short status;
113 spin_lock_irqsave(&port->lock, flags);
115 status = sci_in(port, SCxSR);
116 if (status & SCxSR_ERRORS(port)) {
120 } while (!(status & SCxSR_RDxF(port)));
121 c = sci_in(port, SCxRDR);
122 sci_in(port, SCxSR); /* Dummy read */
123 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
124 spin_unlock_irqrestore(&port->lock, flags);
128 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
130 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
131 static void put_char(struct uart_port *port, char c)
134 unsigned short status;
136 spin_lock_irqsave(&port->lock, flags);
139 status = sci_in(port, SCxSR);
140 } while (!(status & SCxSR_TDxE(port)));
142 sci_out(port, SCxTDR, c);
143 sci_in(port, SCxSR); /* Dummy read */
144 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
146 spin_unlock_irqrestore(&port->lock, flags);
150 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
151 static void put_string(struct sci_port *sci_port, const char *buffer, int count)
153 struct uart_port *port = &sci_port->port;
154 const unsigned char *p = buffer;
157 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
161 #ifdef CONFIG_SH_STANDARD_BIOS
162 /* This call only does a trap the first time it is
163 * called, and so is safe to do here unconditionally
165 usegdb |= sh_bios_in_gdb_mode();
167 #ifdef CONFIG_SH_KGDB
168 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
172 /* $<packet info>#<checksum>. */
176 put_char(port, 'O'); /* 'O'utput to console */
179 for (i=0; i<count; i++) { /* Don't use run length encoding */
190 put_char(port, highhex(checksum));
191 put_char(port, lowhex(checksum));
192 } while (get_char(port) != '+');
194 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
195 for (i=0; i<count; i++) {
197 put_char(port, '\r');
198 put_char(port, *p++);
201 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
203 #ifdef CONFIG_SH_KGDB
204 static int kgdb_sci_getchar(void)
208 /* Keep trying to read a character, this could be neater */
209 while ((c = get_char(kgdb_sci_port)) < 0)
215 static inline void kgdb_sci_putchar(int c)
217 put_char(kgdb_sci_port, c);
219 #endif /* CONFIG_SH_KGDB */
221 #if defined(__H8300S__)
222 enum { sci_disable, sci_enable };
224 static void h8300_sci_config(struct uart_port* port, unsigned int ctrl)
226 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
227 int ch = (port->mapbase - SMR0) >> 3;
228 unsigned char mask = 1 << (ch+1);
230 if (ctrl == sci_disable) {
237 static inline void h8300_sci_enable(struct uart_port *port)
239 h8300_sci_config(port, sci_enable);
242 static inline void h8300_sci_disable(struct uart_port *port)
244 h8300_sci_config(port, sci_disable);
248 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF) && \
249 defined(__H8300H__) || defined(__H8300S__)
250 static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
252 int ch = (port->mapbase - SMR0) >> 3;
255 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
256 h8300_sci_pins[ch].rx,
258 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
259 h8300_sci_pins[ch].tx,
263 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
266 #define sci_init_pins_sci NULL
269 #if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
270 static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
272 unsigned int fcr_val = 0;
275 fcr_val |= SCFCR_MCE;
277 sci_out(port, SCFCR, fcr_val);
280 #define sci_init_pins_irda NULL
284 #define sci_init_pins_scif NULL
287 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
288 #if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7710)
289 /* SH7300 doesn't use RTS/CTS */
290 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
292 sci_out(port, SCFCR, 0);
294 #elif defined(CONFIG_CPU_SH3)
295 /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
296 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
298 unsigned int fcr_val = 0;
301 /* We need to set SCPCR to enable RTS/CTS */
302 data = ctrl_inw(SCPCR);
303 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
304 ctrl_outw(data & 0x0fcf, SCPCR);
307 fcr_val |= SCFCR_MCE;
309 /* We need to set SCPCR to enable RTS/CTS */
310 data = ctrl_inw(SCPCR);
311 /* Clear out SCP7MD1,0, SCP4MD1,0,
312 Set SCP6MD1,0 = {01} (output) */
313 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
315 data = ctrl_inb(SCPDR);
316 /* Set /RTS2 (bit6) = 0 */
317 ctrl_outb(data & 0xbf, SCPDR);
320 sci_out(port, SCFCR, fcr_val);
324 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
326 unsigned int fcr_val = 0;
328 if (cflag & CRTSCTS) {
329 fcr_val |= SCFCR_MCE;
331 #ifdef CONFIG_CPU_SUBTYPE_SH7343
333 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
334 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
336 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
339 sci_out(port, SCFCR, fcr_val);
343 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || defined(CONFIG_CPU_SUBTYPE_SH7780)
344 static inline int scif_txroom(struct uart_port *port)
346 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0x7f);
349 static inline int scif_rxroom(struct uart_port *port)
351 return sci_in(port, SCRFDR) & 0x7f;
354 static inline int scif_txroom(struct uart_port *port)
356 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
359 static inline int scif_rxroom(struct uart_port *port)
361 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
364 #endif /* SCIF_ONLY || SCI_AND_SCIF */
366 static inline int sci_txroom(struct uart_port *port)
368 return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
371 static inline int sci_rxroom(struct uart_port *port)
373 return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
376 /* ********************************************************************** *
377 * the interrupt related routines *
378 * ********************************************************************** */
380 static void sci_transmit_chars(struct uart_port *port)
382 struct circ_buf *xmit = &port->info->xmit;
383 unsigned int stopped = uart_tx_stopped(port);
384 unsigned short status;
388 status = sci_in(port, SCxSR);
389 if (!(status & SCxSR_TDxE(port))) {
390 ctrl = sci_in(port, SCSCR);
391 if (uart_circ_empty(xmit)) {
392 ctrl &= ~SCI_CTRL_FLAGS_TIE;
394 ctrl |= SCI_CTRL_FLAGS_TIE;
396 sci_out(port, SCSCR, ctrl);
401 if (port->type == PORT_SCIF)
402 count = scif_txroom(port);
405 count = sci_txroom(port);
413 } else if (!uart_circ_empty(xmit) && !stopped) {
414 c = xmit->buf[xmit->tail];
415 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
420 sci_out(port, SCxTDR, c);
423 } while (--count > 0);
425 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
427 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
428 uart_write_wakeup(port);
429 if (uart_circ_empty(xmit)) {
432 ctrl = sci_in(port, SCSCR);
434 #if !defined(SCI_ONLY)
435 if (port->type == PORT_SCIF) {
436 sci_in(port, SCxSR); /* Dummy read */
437 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
441 ctrl |= SCI_CTRL_FLAGS_TIE;
442 sci_out(port, SCSCR, ctrl);
446 /* On SH3, SCIF may read end-of-break as a space->mark char */
447 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
449 static inline void sci_receive_chars(struct uart_port *port,
450 struct pt_regs *regs)
452 struct sci_port *sci_port = (struct sci_port *)port;
453 struct tty_struct *tty = port->info->tty;
454 int i, count, copied = 0;
455 unsigned short status;
458 status = sci_in(port, SCxSR);
459 if (!(status & SCxSR_RDxF(port)))
463 #if !defined(SCI_ONLY)
464 if (port->type == PORT_SCIF)
465 count = scif_rxroom(port);
468 count = sci_rxroom(port);
470 /* Don't copy more bytes than there is room for in the buffer */
471 count = tty_buffer_request_room(tty, count);
473 /* If for any reason we can't copy more data, we're done! */
477 if (port->type == PORT_SCI) {
478 char c = sci_in(port, SCxRDR);
479 if (uart_handle_sysrq_char(port, c, regs) || sci_port->break_flag)
482 tty_insert_flip_char(tty, c, TTY_NORMAL);
485 for (i=0; i<count; i++) {
486 char c = sci_in(port, SCxRDR);
487 status = sci_in(port, SCxSR);
488 #if defined(CONFIG_CPU_SH3)
489 /* Skip "chars" during break */
490 if (sci_port->break_flag) {
492 (status & SCxSR_FER(port))) {
497 /* Nonzero => end-of-break */
498 pr_debug("scif: debounce<%02x>\n", c);
499 sci_port->break_flag = 0;
506 #endif /* CONFIG_CPU_SH3 */
507 if (uart_handle_sysrq_char(port, c, regs)) {
512 /* Store data and status */
513 if (status&SCxSR_FER(port)) {
515 pr_debug("sci: frame error\n");
516 } else if (status&SCxSR_PER(port)) {
518 pr_debug("sci: parity error\n");
521 tty_insert_flip_char(tty, c, flag);
525 sci_in(port, SCxSR); /* dummy read */
526 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
529 port->icount.rx += count;
533 /* Tell the rest of the system the news. New characters! */
534 tty_flip_buffer_push(tty);
536 sci_in(port, SCxSR); /* dummy read */
537 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
541 #define SCI_BREAK_JIFFIES (HZ/20)
542 /* The sci generates interrupts during the break,
543 * 1 per millisecond or so during the break period, for 9600 baud.
544 * So dont bother disabling interrupts.
545 * But dont want more than 1 break event.
546 * Use a kernel timer to periodically poll the rx line until
547 * the break is finished.
549 static void sci_schedule_break_timer(struct sci_port *port)
551 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
552 add_timer(&port->break_timer);
554 /* Ensure that two consecutive samples find the break over. */
555 static void sci_break_timer(unsigned long data)
557 struct sci_port *port = (struct sci_port *)data;
559 if (sci_rxd_in(&port->port) == 0) {
560 port->break_flag = 1;
561 sci_schedule_break_timer(port);
562 } else if (port->break_flag == 1) {
564 port->break_flag = 2;
565 sci_schedule_break_timer(port);
567 port->break_flag = 0;
570 static inline int sci_handle_errors(struct uart_port *port)
573 unsigned short status = sci_in(port, SCxSR);
574 struct tty_struct *tty = port->info->tty;
576 if (status & SCxSR_ORER(port)) {
578 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
580 pr_debug("sci: overrun error\n");
583 if (status & SCxSR_FER(port)) {
584 if (sci_rxd_in(port) == 0) {
585 /* Notify of BREAK */
586 struct sci_port *sci_port = (struct sci_port *)port;
588 if (!sci_port->break_flag) {
589 sci_port->break_flag = 1;
590 sci_schedule_break_timer(sci_port);
592 /* Do sysrq handling. */
593 if (uart_handle_break(port))
595 pr_debug("sci: BREAK detected\n");
596 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
601 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
603 pr_debug("sci: frame error\n");
607 if (status & SCxSR_PER(port)) {
609 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
611 pr_debug("sci: parity error\n");
615 tty_flip_buffer_push(tty);
620 static inline int sci_handle_breaks(struct uart_port *port)
623 unsigned short status = sci_in(port, SCxSR);
624 struct tty_struct *tty = port->info->tty;
625 struct sci_port *s = &sci_ports[port->line];
627 if (!s->break_flag && status & SCxSR_BRK(port)) {
628 #if defined(CONFIG_CPU_SH3)
632 /* Notify of BREAK */
633 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
635 pr_debug("sci: BREAK detected\n");
638 #if defined(SCIF_ORER)
639 /* XXX: Handle SCIF overrun error */
640 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
641 sci_out(port, SCLSR, 0);
642 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
644 pr_debug("sci: overrun error\n");
650 tty_flip_buffer_push(tty);
655 static irqreturn_t sci_rx_interrupt(int irq, void *port, struct pt_regs *regs)
657 /* I think sci_receive_chars has to be called irrespective
658 * of whether the I_IXOFF is set, otherwise, how is the interrupt
661 sci_receive_chars(port, regs);
666 static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
668 struct uart_port *port = ptr;
670 spin_lock_irq(&port->lock);
671 sci_transmit_chars(port);
672 spin_unlock_irq(&port->lock);
677 static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
679 struct uart_port *port = ptr;
682 if (port->type == PORT_SCI) {
683 if (sci_handle_errors(port)) {
684 /* discard character in rx buffer */
686 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
689 #if defined(SCIF_ORER)
690 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
691 struct tty_struct *tty = port->info->tty;
693 sci_out(port, SCLSR, 0);
694 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
695 tty_flip_buffer_push(tty);
696 pr_debug("scif: overrun error\n");
699 sci_rx_interrupt(irq, ptr, regs);
702 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
704 /* Kick the transmission */
705 sci_tx_interrupt(irq, ptr, regs);
710 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
712 struct uart_port *port = ptr;
715 sci_handle_breaks(port);
717 #ifdef CONFIG_SH_KGDB
718 /* Break into the debugger if a break is detected */
722 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
727 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
729 unsigned short ssr_status, scr_status;
730 struct uart_port *port = ptr;
732 ssr_status = sci_in(port,SCxSR);
733 scr_status = sci_in(port,SCSCR);
736 if ((ssr_status & 0x0020) && (scr_status & 0x0080))
737 sci_tx_interrupt(irq, ptr, regs);
739 if ((ssr_status & 0x0002) && (scr_status & 0x0040))
740 sci_rx_interrupt(irq, ptr, regs);
741 /* Error Interrupt */
742 if ((ssr_status & 0x0080) && (scr_status & 0x0400))
743 sci_er_interrupt(irq, ptr, regs);
744 /* Break Interrupt */
745 if ((ssr_status & 0x0010) && (scr_status & 0x0200))
746 sci_br_interrupt(irq, ptr, regs);
751 #ifdef CONFIG_CPU_FREQ
753 * Here we define a transistion notifier so that we can update all of our
754 * ports' baud rate when the peripheral clock changes.
756 static int sci_notifier(struct notifier_block *self,
757 unsigned long phase, void *p)
759 struct cpufreq_freqs *freqs = p;
762 if ((phase == CPUFREQ_POSTCHANGE) ||
763 (phase == CPUFREQ_RESUMECHANGE)){
764 for (i = 0; i < SCI_NPORTS; i++) {
765 struct uart_port *port = &sci_ports[i].port;
769 * Update the uartclk per-port if frequency has
770 * changed, since it will no longer necessarily be
771 * consistent with the old frequency.
773 * Really we want to be able to do something like
774 * uart_change_speed() or something along those lines
775 * here to implicitly reset the per-port baud rate..
777 * Clean this up later..
779 clk = clk_get("module_clk");
780 port->uartclk = clk_get_rate(clk) * 16;
784 printk(KERN_INFO "%s: got a postchange notification "
785 "for cpu %d (old %d, new %d)\n",
786 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
792 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
793 #endif /* CONFIG_CPU_FREQ */
795 static int sci_request_irq(struct sci_port *port)
798 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
799 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
802 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
803 "SCI Transmit Data Empty", "SCI Break" };
805 if (port->irqs[0] == port->irqs[1]) {
806 if (!port->irqs[0]) {
807 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
811 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
812 SA_INTERRUPT, "sci", port)) {
813 printk(KERN_ERR "sci: Cannot allocate irq.\n");
817 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
820 if (request_irq(port->irqs[i], handlers[i],
821 SA_INTERRUPT, desc[i], port)) {
822 printk(KERN_ERR "sci: Cannot allocate irq.\n");
831 static void sci_free_irq(struct sci_port *port)
835 if (port->irqs[0] == port->irqs[1]) {
837 printk("sci: sci_free_irq error\n");
839 free_irq(port->irqs[0], port);
841 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
845 free_irq(port->irqs[i], port);
850 static unsigned int sci_tx_empty(struct uart_port *port)
856 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
858 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
859 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
860 /* If you have signals for DTR and DCD, please implement here. */
863 static unsigned int sci_get_mctrl(struct uart_port *port)
865 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
868 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
871 static void sci_start_tx(struct uart_port *port)
875 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
876 ctrl = sci_in(port, SCSCR);
877 ctrl |= SCI_CTRL_FLAGS_TIE;
878 sci_out(port, SCSCR, ctrl);
881 static void sci_stop_tx(struct uart_port *port)
885 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
886 ctrl = sci_in(port, SCSCR);
887 ctrl &= ~SCI_CTRL_FLAGS_TIE;
888 sci_out(port, SCSCR, ctrl);
891 static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
895 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
896 ctrl = sci_in(port, SCSCR);
897 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
898 sci_out(port, SCSCR, ctrl);
901 static void sci_stop_rx(struct uart_port *port)
905 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
906 ctrl = sci_in(port, SCSCR);
907 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
908 sci_out(port, SCSCR, ctrl);
911 static void sci_enable_ms(struct uart_port *port)
913 /* Nothing here yet .. */
916 static void sci_break_ctl(struct uart_port *port, int break_state)
918 /* Nothing here yet .. */
921 static int sci_startup(struct uart_port *port)
923 struct sci_port *s = &sci_ports[port->line];
930 sci_start_rx(port, 1);
935 static void sci_shutdown(struct uart_port *port)
937 struct sci_port *s = &sci_ports[port->line];
947 static void sci_set_termios(struct uart_port *port, struct termios *termios,
950 struct sci_port *s = &sci_ports[port->line];
951 unsigned int status, baud, smr_val;
955 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
963 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
964 struct clk *clk = clk_get("module_clk");
965 t = SCBRR_VALUE(baud, clk_get_rate(clk));
968 t = SCBRR_VALUE(baud);
974 spin_lock_irqsave(&port->lock, flags);
977 status = sci_in(port, SCxSR);
978 } while (!(status & SCxSR_TEND(port)));
980 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
982 #if !defined(SCI_ONLY)
983 if (port->type == PORT_SCIF)
984 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
987 smr_val = sci_in(port, SCSMR) & 3;
988 if ((termios->c_cflag & CSIZE) == CS7)
990 if (termios->c_cflag & PARENB)
992 if (termios->c_cflag & PARODD)
994 if (termios->c_cflag & CSTOPB)
997 uart_update_timeout(port, termios->c_cflag, baud);
999 sci_out(port, SCSMR, smr_val);
1003 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1006 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1008 sci_out(port, SCBRR, t);
1009 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1012 if (likely(s->init_pins))
1013 s->init_pins(port, termios->c_cflag);
1015 sci_out(port, SCSCR, SCSCR_INIT(port));
1017 if ((termios->c_cflag & CREAD) != 0)
1018 sci_start_rx(port,0);
1020 spin_unlock_irqrestore(&port->lock, flags);
1023 static const char *sci_type(struct uart_port *port)
1025 switch (port->type) {
1026 case PORT_SCI: return "sci";
1027 case PORT_SCIF: return "scif";
1028 case PORT_IRDA: return "irda";
1034 static void sci_release_port(struct uart_port *port)
1036 /* Nothing here yet .. */
1039 static int sci_request_port(struct uart_port *port)
1041 /* Nothing here yet .. */
1045 static void sci_config_port(struct uart_port *port, int flags)
1047 struct sci_port *s = &sci_ports[port->line];
1049 port->type = s->type;
1051 switch (port->type) {
1053 s->init_pins = sci_init_pins_sci;
1056 s->init_pins = sci_init_pins_scif;
1059 s->init_pins = sci_init_pins_irda;
1063 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1064 if (port->mapbase == 0)
1065 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1067 port->membase = (void __iomem *)port->mapbase;
1071 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1073 struct sci_port *s = &sci_ports[port->line];
1075 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1077 if (ser->baud_base < 2400)
1078 /* No paper tape reader for Mitch.. */
1084 static struct uart_ops sci_uart_ops = {
1085 .tx_empty = sci_tx_empty,
1086 .set_mctrl = sci_set_mctrl,
1087 .get_mctrl = sci_get_mctrl,
1088 .start_tx = sci_start_tx,
1089 .stop_tx = sci_stop_tx,
1090 .stop_rx = sci_stop_rx,
1091 .enable_ms = sci_enable_ms,
1092 .break_ctl = sci_break_ctl,
1093 .startup = sci_startup,
1094 .shutdown = sci_shutdown,
1095 .set_termios = sci_set_termios,
1097 .release_port = sci_release_port,
1098 .request_port = sci_request_port,
1099 .config_port = sci_config_port,
1100 .verify_port = sci_verify_port,
1103 static void __init sci_init_ports(void)
1105 static int first = 1;
1113 for (i = 0; i < SCI_NPORTS; i++) {
1114 sci_ports[i].port.ops = &sci_uart_ops;
1115 sci_ports[i].port.iotype = UPIO_MEM;
1116 sci_ports[i].port.line = i;
1117 sci_ports[i].port.fifosize = 1;
1119 #if defined(__H8300H__) || defined(__H8300S__)
1121 sci_ports[i].enable = h8300_sci_enable;
1122 sci_ports[i].disable = h8300_sci_disable;
1124 sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
1125 #elif defined(CONFIG_SUPERH64)
1126 sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
1129 * XXX: We should use a proper SCI/SCIF clock
1132 struct clk *clk = clk_get("module_clk");
1133 sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
1138 sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
1139 sci_ports[i].break_timer.function = sci_break_timer;
1141 init_timer(&sci_ports[i].break_timer);
1145 int __init early_sci_setup(struct uart_port *port)
1147 if (unlikely(port->line > SCI_NPORTS))
1152 sci_ports[port->line].port.membase = port->membase;
1153 sci_ports[port->line].port.mapbase = port->mapbase;
1154 sci_ports[port->line].port.type = port->type;
1159 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1161 * Print a string to the serial port trying not to disturb
1162 * any possible real use of the port...
1164 static void serial_console_write(struct console *co, const char *s,
1167 put_string(serial_console_port, s, count);
1170 static int __init serial_console_setup(struct console *co, char *options)
1172 struct uart_port *port;
1180 * Check whether an invalid uart number has been specified, and
1181 * if so, search for the first available port that does have
1184 if (co->index >= SCI_NPORTS)
1187 serial_console_port = &sci_ports[co->index];
1188 port = &serial_console_port->port;
1191 * Also need to check port->type, we don't actually have any
1192 * UPIO_PORT ports, but uart_report_port() handily misreports
1193 * it anyways if we don't have a port available by the time this is
1198 if (!port->membase || !port->mapbase)
1201 spin_lock_init(&port->lock);
1203 port->type = serial_console_port->type;
1205 if (port->flags & UPF_IOREMAP)
1206 sci_config_port(port, 0);
1208 if (serial_console_port->enable)
1209 serial_console_port->enable(port);
1212 uart_parse_options(options, &baud, &parity, &bits, &flow);
1214 ret = uart_set_options(port, co, baud, parity, bits, flow);
1215 #if defined(__H8300H__) || defined(__H8300S__)
1216 /* disable rx interrupt */
1223 static struct console serial_console = {
1225 .device = uart_console_device,
1226 .write = serial_console_write,
1227 .setup = serial_console_setup,
1228 .flags = CON_PRINTBUFFER,
1230 .data = &sci_uart_driver,
1233 static int __init sci_console_init(void)
1236 register_console(&serial_console);
1239 console_initcall(sci_console_init);
1240 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1242 #ifdef CONFIG_SH_KGDB
1244 * FIXME: Most of this can go away.. at the moment, we rely on
1245 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1246 * most of that can easily be done here instead.
1248 * For the time being, just accept the values that were parsed earlier..
1250 static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1251 int *parity, int *bits)
1254 *parity = tolower(kgdb_parity);
1255 *bits = kgdb_bits - '0';
1259 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1260 * care of the early-on initialization for kgdb, regardless of whether we
1261 * actually use kgdb as a console or not.
1263 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1265 int __init kgdb_console_setup(struct console *co, char *options)
1267 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1273 spin_lock_init(&port->lock);
1275 if (co->index != kgdb_portnum)
1276 co->index = kgdb_portnum;
1279 uart_parse_options(options, &baud, &parity, &bits, &flow);
1281 kgdb_console_get_options(port, &baud, &parity, &bits);
1283 kgdb_getchar = kgdb_sci_getchar;
1284 kgdb_putchar = kgdb_sci_putchar;
1286 return uart_set_options(port, co, baud, parity, bits, flow);
1288 #endif /* CONFIG_SH_KGDB */
1290 #ifdef CONFIG_SH_KGDB_CONSOLE
1291 static struct console kgdb_console = {
1293 .write = kgdb_console_write,
1294 .setup = kgdb_console_setup,
1295 .flags = CON_PRINTBUFFER | CON_ENABLED,
1297 .data = &sci_uart_driver,
1300 /* Register the KGDB console so we get messages (d'oh!) */
1301 static int __init kgdb_console_init(void)
1304 register_console(&kgdb_console);
1307 console_initcall(kgdb_console_init);
1308 #endif /* CONFIG_SH_KGDB_CONSOLE */
1310 #if defined(CONFIG_SH_KGDB_CONSOLE)
1311 #define SCI_CONSOLE &kgdb_console
1312 #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1313 #define SCI_CONSOLE &serial_console
1315 #define SCI_CONSOLE 0
1318 static char banner[] __initdata =
1319 KERN_INFO "SuperH SCI(F) driver initialized\n";
1321 static struct uart_driver sci_uart_driver = {
1322 .owner = THIS_MODULE,
1323 .driver_name = "sci",
1324 .dev_name = "ttySC",
1326 .minor = SCI_MINOR_START,
1328 .cons = SCI_CONSOLE,
1332 * Register a set of serial devices attached to a platform device. The
1333 * list is terminated with a zero flags entry, which means we expect
1334 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1335 * remapping (such as sh64) should also set UPF_IOREMAP.
1337 static int __devinit sci_probe(struct platform_device *dev)
1339 struct plat_sci_port *p = dev->dev.platform_data;
1342 for (i = 0; p && p->flags != 0 && i < SCI_NPORTS; p++, i++) {
1343 struct sci_port *sciport = &sci_ports[i];
1345 sciport->port.mapbase = p->mapbase;
1348 * For the simple (and majority of) cases where we don't need
1349 * to do any remapping, just cast the cookie directly.
1351 if (p->mapbase && !p->membase && !(p->flags & UPF_IOREMAP))
1352 p->membase = (void __iomem *)p->mapbase;
1354 sciport->port.membase = p->membase;
1356 sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
1357 sciport->port.flags = p->flags;
1358 sciport->port.dev = &dev->dev;
1360 sciport->type = sciport->port.type = p->type;
1362 memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
1364 uart_add_one_port(&sci_uart_driver, &sciport->port);
1367 #ifdef CONFIG_CPU_FREQ
1368 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1369 dev_info(&dev->dev, "sci: CPU frequency notifier registered\n");
1372 #ifdef CONFIG_SH_STANDARD_BIOS
1373 sh_bios_gdb_detach();
1379 static int __devexit sci_remove(struct platform_device *dev)
1383 for (i = 0; i < SCI_NPORTS; i++)
1384 uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
1389 static int sci_suspend(struct platform_device *dev, pm_message_t state)
1393 for (i = 0; i < SCI_NPORTS; i++) {
1394 struct sci_port *p = &sci_ports[i];
1396 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1397 uart_suspend_port(&sci_uart_driver, &p->port);
1403 static int sci_resume(struct platform_device *dev)
1407 for (i = 0; i < SCI_NPORTS; i++) {
1408 struct sci_port *p = &sci_ports[i];
1410 if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
1411 uart_resume_port(&sci_uart_driver, &p->port);
1417 static struct platform_driver sci_driver = {
1419 .remove = __devexit_p(sci_remove),
1420 .suspend = sci_suspend,
1421 .resume = sci_resume,
1424 .owner = THIS_MODULE,
1428 static int __init sci_init(void)
1436 ret = uart_register_driver(&sci_uart_driver);
1437 if (likely(ret == 0)) {
1438 ret = platform_driver_register(&sci_driver);
1440 uart_unregister_driver(&sci_uart_driver);
1446 static void __exit sci_exit(void)
1448 platform_driver_unregister(&sci_driver);
1449 uart_unregister_driver(&sci_uart_driver);
1452 module_init(sci_init);
1453 module_exit(sci_exit);
1455 MODULE_LICENSE("GPL");