2 * drivers/serial/sh-sci.c
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 2002, 2003, 2004 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/signal.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/sysrq.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/console.h>
43 #include <linux/bitops.h>
44 #include <linux/generic_serial.h>
46 #ifdef CONFIG_CPU_FREQ
47 #include <linux/notifier.h>
48 #include <linux/cpufreq.h>
51 #include <asm/system.h>
54 #include <asm/uaccess.h>
56 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
57 #include <asm/clock.h>
60 #ifdef CONFIG_SH_STANDARD_BIOS
61 #include <asm/sh_bios.h>
64 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
73 static int kgdb_get_char(struct sci_port *port);
74 static void kgdb_put_char(struct sci_port *port, char c);
75 static void kgdb_handle_error(struct sci_port *port);
76 static struct sci_port *kgdb_sci_port;
77 #endif /* CONFIG_SH_KGDB */
79 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
80 static struct sci_port *serial_console_port = 0;
81 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
83 /* Function prototypes */
84 static void sci_stop_tx(struct uart_port *port);
85 static void sci_start_tx(struct uart_port *port);
86 static void sci_start_rx(struct uart_port *port, unsigned int tty_start);
87 static void sci_stop_rx(struct uart_port *port);
88 static int sci_request_irq(struct sci_port *port);
89 static void sci_free_irq(struct sci_port *port);
91 static struct sci_port sci_ports[];
92 static struct uart_driver sci_uart_driver;
94 #define SCI_NPORTS sci_uart_driver.nr
96 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
98 static void handle_error(struct uart_port *port)
99 { /* Clear error flags */
100 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
103 static int get_char(struct uart_port *port)
106 unsigned short status;
109 local_irq_save(flags);
111 status = sci_in(port, SCxSR);
112 if (status & SCxSR_ERRORS(port)) {
116 } while (!(status & SCxSR_RDxF(port)));
117 c = sci_in(port, SCxRDR);
118 sci_in(port, SCxSR); /* Dummy read */
119 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
120 local_irq_restore(flags);
125 /* Taken from sh-stub.c of GDB 4.18 */
126 static const char hexchars[] = "0123456789abcdef";
128 static __inline__ char highhex(int x)
130 return hexchars[(x >> 4) & 0xf];
133 static __inline__ char lowhex(int x)
135 return hexchars[x & 0xf];
138 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
141 * Send the packet in buffer. The host gets one chance to read it.
142 * This routine does not wait for a positive acknowledge.
145 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
146 static void put_char(struct uart_port *port, char c)
149 unsigned short status;
151 local_irq_save(flags);
154 status = sci_in(port, SCxSR);
155 } while (!(status & SCxSR_TDxE(port)));
157 sci_out(port, SCxTDR, c);
158 sci_in(port, SCxSR); /* Dummy read */
159 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
161 local_irq_restore(flags);
164 static void put_string(struct sci_port *sci_port, const char *buffer, int count)
166 struct uart_port *port = &sci_port->port;
167 const unsigned char *p = buffer;
170 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
174 #ifdef CONFIG_SH_STANDARD_BIOS
175 /* This call only does a trap the first time it is
176 * called, and so is safe to do here unconditionally
178 usegdb |= sh_bios_in_gdb_mode();
180 #ifdef CONFIG_SH_KGDB
181 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
185 /* $<packet info>#<checksum>. */
189 put_char(port, 'O'); /* 'O'utput to console */
192 for (i=0; i<count; i++) { /* Don't use run length encoding */
203 put_char(port, highhex(checksum));
204 put_char(port, lowhex(checksum));
205 } while (get_char(port) != '+');
207 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
208 for (i=0; i<count; i++) {
210 put_char(port, '\r');
211 put_char(port, *p++);
214 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
217 #ifdef CONFIG_SH_KGDB
219 /* Is the SCI ready, ie is there a char waiting? */
220 static int kgdb_is_char_ready(struct sci_port *port)
222 unsigned short status = sci_in(port, SCxSR);
224 if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
225 kgdb_handle_error(port);
227 return (status & SCxSR_RDxF(port));
231 static void kgdb_put_char(struct sci_port *port, char c)
233 unsigned short status;
236 status = sci_in(port, SCxSR);
237 while (!(status & SCxSR_TDxE(port)));
239 sci_out(port, SCxTDR, c);
240 sci_in(port, SCxSR); /* Dummy read */
241 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
244 /* Get a char if there is one, else ret -1 */
245 static int kgdb_get_char(struct sci_port *port)
249 if (kgdb_is_char_ready(port) == 0)
252 c = sci_in(port, SCxRDR);
253 sci_in(port, SCxSR); /* Dummy read */
254 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
260 /* Called from kgdbstub.c to get a character, i.e. is blocking */
261 static int kgdb_sci_getchar(void)
265 /* Keep trying to read a character, this could be neater */
266 while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
271 /* Called from kgdbstub.c to put a character, just a wrapper */
272 static void kgdb_sci_putchar(int c)
275 kgdb_put_char(kgdb_sci_port, c);
278 /* Clear any errors on the SCI */
279 static void kgdb_handle_error(struct sci_port *port)
281 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
284 /* Breakpoint if there's a break sent on the serial port */
285 static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
287 struct sci_port *port = ptr;
288 unsigned short status = sci_in(port, SCxSR);
290 if (status & SCxSR_BRK(port)) {
292 /* Break into the debugger if a break is detected */
296 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
300 #endif /* CONFIG_SH_KGDB */
302 #if defined(__H8300S__)
303 enum { sci_disable, sci_enable };
305 static void h8300_sci_enable(struct uart_port* port, unsigned int ctrl)
307 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
308 int ch = (port->mapbase - SMR0) >> 3;
309 unsigned char mask = 1 << (ch+1);
311 if (ctrl == sci_disable) {
319 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
320 #if defined(__H8300H__) || defined(__H8300S__)
321 static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
323 int ch = (port->mapbase - SMR0) >> 3;
326 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
327 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
329 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
334 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
335 #if defined(CONFIG_CPU_SUBTYPE_SH7300)
336 /* SH7300 doesn't use RTS/CTS */
337 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
339 sci_out(port, SCFCR, 0);
341 #elif defined(CONFIG_CPU_SH3)
342 /* For SH7705, SH7707, SH7709, SH7709A, SH7729 */
343 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
345 unsigned int fcr_val = 0;
348 /* We need to set SCPCR to enable RTS/CTS */
349 data = ctrl_inw(SCPCR);
350 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
351 ctrl_outw(data & 0x0fcf, SCPCR);
354 fcr_val |= SCFCR_MCE;
356 /* We need to set SCPCR to enable RTS/CTS */
357 data = ctrl_inw(SCPCR);
358 /* Clear out SCP7MD1,0, SCP4MD1,0,
359 Set SCP6MD1,0 = {01} (output) */
360 ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
362 data = ctrl_inb(SCPDR);
363 /* Set /RTS2 (bit6) = 0 */
364 ctrl_outb(data & 0xbf, SCPDR);
367 sci_out(port, SCFCR, fcr_val);
370 #if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
371 static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
373 unsigned int fcr_val = 0;
376 fcr_val |= SCFCR_MCE;
378 sci_out(port, SCFCR, fcr_val);
384 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
386 unsigned int fcr_val = 0;
388 if (cflag & CRTSCTS) {
389 fcr_val |= SCFCR_MCE;
391 #ifdef CONFIG_CPU_SUBTYPE_SH7780
392 ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
394 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
397 sci_out(port, SCFCR, fcr_val);
401 #endif /* SCIF_ONLY || SCI_AND_SCIF */
403 /* ********************************************************************** *
404 * the interrupt related routines *
405 * ********************************************************************** */
407 static void sci_transmit_chars(struct uart_port *port)
409 struct circ_buf *xmit = &port->info->xmit;
410 unsigned int stopped = uart_tx_stopped(port);
412 unsigned short status;
416 status = sci_in(port, SCxSR);
417 if (!(status & SCxSR_TDxE(port))) {
418 local_irq_save(flags);
419 ctrl = sci_in(port, SCSCR);
420 if (uart_circ_empty(xmit)) {
421 ctrl &= ~SCI_CTRL_FLAGS_TIE;
423 ctrl |= SCI_CTRL_FLAGS_TIE;
425 sci_out(port, SCSCR, ctrl);
426 local_irq_restore(flags);
430 #if !defined(SCI_ONLY)
431 if (port->type == PORT_SCIF) {
432 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || defined(CONFIG_CPU_SUBTYPE_SH7780)
433 txroom = SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0x7f);
435 txroom = SCIF_TXROOM_MAX - (sci_in(port, SCFDR)>>8);
438 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
441 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
452 } else if (!uart_circ_empty(xmit) && !stopped) {
453 c = xmit->buf[xmit->tail];
454 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
459 sci_out(port, SCxTDR, c);
462 } while (--count > 0);
464 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
466 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
467 uart_write_wakeup(port);
468 if (uart_circ_empty(xmit)) {
471 local_irq_save(flags);
472 ctrl = sci_in(port, SCSCR);
474 #if !defined(SCI_ONLY)
475 if (port->type == PORT_SCIF) {
476 sci_in(port, SCxSR); /* Dummy read */
477 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
481 ctrl |= SCI_CTRL_FLAGS_TIE;
482 sci_out(port, SCSCR, ctrl);
483 local_irq_restore(flags);
487 /* On SH3, SCIF may read end-of-break as a space->mark char */
488 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
490 static inline void sci_receive_chars(struct uart_port *port,
491 struct pt_regs *regs)
493 struct tty_struct *tty = port->info->tty;
494 int i, count, copied = 0;
495 unsigned short status;
498 status = sci_in(port, SCxSR);
499 if (!(status & SCxSR_RDxF(port)))
503 #if !defined(SCI_ONLY)
504 if (port->type == PORT_SCIF) {
505 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || defined(CONFIG_CPU_SUBTYPE_SH7780)
506 count = sci_in(port, SCRFDR) & 0x7f;
508 count = sci_in(port, SCFDR)&SCIF_RFDC_MASK ;
511 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
514 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
517 /* Don't copy more bytes than there is room for in the buffer */
518 count = tty_buffer_request_room(tty, count);
520 /* If for any reason we can't copy more data, we're done! */
524 if (port->type == PORT_SCI) {
525 char c = sci_in(port, SCxRDR);
526 if(((struct sci_port *)port)->break_flag
527 || uart_handle_sysrq_char(port, c, regs)) {
530 tty_insert_flip_char(tty, c, TTY_NORMAL);
533 for (i=0; i<count; i++) {
534 char c = sci_in(port, SCxRDR);
535 status = sci_in(port, SCxSR);
536 #if defined(CONFIG_CPU_SH3)
537 /* Skip "chars" during break */
538 if (((struct sci_port *)port)->break_flag) {
540 (status & SCxSR_FER(port))) {
544 /* Nonzero => end-of-break */
545 pr_debug("scif: debounce<%02x>\n", c);
546 ((struct sci_port *)port)->break_flag = 0;
552 #endif /* CONFIG_CPU_SH3 */
553 if (uart_handle_sysrq_char(port, c, regs)) {
558 /* Store data and status */
559 if (status&SCxSR_FER(port)) {
561 pr_debug("sci: frame error\n");
562 } else if (status&SCxSR_PER(port)) {
564 pr_debug("sci: parity error\n");
567 tty_insert_flip_char(tty, c, flag);
571 sci_in(port, SCxSR); /* dummy read */
572 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
575 port->icount.rx += count;
579 /* Tell the rest of the system the news. New characters! */
580 tty_flip_buffer_push(tty);
582 sci_in(port, SCxSR); /* dummy read */
583 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
587 #define SCI_BREAK_JIFFIES (HZ/20)
588 /* The sci generates interrupts during the break,
589 * 1 per millisecond or so during the break period, for 9600 baud.
590 * So dont bother disabling interrupts.
591 * But dont want more than 1 break event.
592 * Use a kernel timer to periodically poll the rx line until
593 * the break is finished.
595 static void sci_schedule_break_timer(struct sci_port *port)
597 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
598 add_timer(&port->break_timer);
600 /* Ensure that two consecutive samples find the break over. */
601 static void sci_break_timer(unsigned long data)
603 struct sci_port * port = (struct sci_port *)data;
604 if(sci_rxd_in(&port->port) == 0) {
605 port->break_flag = 1;
606 sci_schedule_break_timer(port);
607 } else if(port->break_flag == 1){
609 port->break_flag = 2;
610 sci_schedule_break_timer(port);
611 } else port->break_flag = 0;
614 static inline int sci_handle_errors(struct uart_port *port)
617 unsigned short status = sci_in(port, SCxSR);
618 struct tty_struct *tty = port->info->tty;
620 if (status&SCxSR_ORER(port)) {
622 if(tty_insert_flip_char(tty, 0, TTY_OVERRUN))
624 pr_debug("sci: overrun error\n");
627 if (status&SCxSR_FER(port)) {
628 if (sci_rxd_in(port) == 0) {
629 /* Notify of BREAK */
630 struct sci_port * sci_port = (struct sci_port *)port;
631 if(!sci_port->break_flag) {
632 sci_port->break_flag = 1;
633 sci_schedule_break_timer((struct sci_port *)port);
634 /* Do sysrq handling. */
635 if(uart_handle_break(port))
637 pr_debug("sci: BREAK detected\n");
638 if(tty_insert_flip_char(tty, 0, TTY_BREAK))
644 if(tty_insert_flip_char(tty, 0, TTY_FRAME))
646 pr_debug("sci: frame error\n");
650 if (status&SCxSR_PER(port)) {
651 if(tty_insert_flip_char(tty, 0, TTY_PARITY))
654 pr_debug("sci: parity error\n");
658 tty_flip_buffer_push(tty);
663 static inline int sci_handle_breaks(struct uart_port *port)
666 unsigned short status = sci_in(port, SCxSR);
667 struct tty_struct *tty = port->info->tty;
668 struct sci_port *s = &sci_ports[port->line];
670 if (!s->break_flag && status & SCxSR_BRK(port)) {
671 #if defined(CONFIG_CPU_SH3)
675 /* Notify of BREAK */
676 if(tty_insert_flip_char(tty, 0, TTY_BREAK))
678 pr_debug("sci: BREAK detected\n");
681 #if defined(SCIF_ORER)
682 /* XXX: Handle SCIF overrun error */
683 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
684 sci_out(port, SCLSR, 0);
685 if(tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
687 pr_debug("sci: overrun error\n");
693 tty_flip_buffer_push(tty);
697 static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
699 struct uart_port *port = ptr;
701 /* I think sci_receive_chars has to be called irrespective
702 * of whether the I_IXOFF is set, otherwise, how is the interrupt
705 sci_receive_chars(port, regs);
710 static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
712 struct uart_port *port = ptr;
714 sci_transmit_chars(port);
719 static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
721 struct uart_port *port = ptr;
724 if (port->type == PORT_SCI) {
725 if (sci_handle_errors(port)) {
726 /* discard character in rx buffer */
728 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
731 #if defined(SCIF_ORER)
732 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
733 struct tty_struct *tty = port->info->tty;
735 sci_out(port, SCLSR, 0);
736 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
737 tty_flip_buffer_push(tty);
738 pr_debug("scif: overrun error\n");
741 sci_rx_interrupt(irq, ptr, regs);
744 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
746 /* Kick the transmission */
747 sci_tx_interrupt(irq, ptr, regs);
752 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
754 struct uart_port *port = ptr;
757 sci_handle_breaks(port);
758 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
763 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
765 unsigned short ssr_status, scr_status;
766 struct uart_port *port = ptr;
768 ssr_status = sci_in(port,SCxSR);
769 scr_status = sci_in(port,SCSCR);
772 if ((ssr_status&0x0020) && (scr_status&0x0080))
773 sci_tx_interrupt(irq, ptr, regs);
775 if ((ssr_status&0x0002) && (scr_status&0x0040))
776 sci_rx_interrupt(irq, ptr, regs);
777 /* Error Interrupt */
778 if ((ssr_status&0x0080) && (scr_status&0x0400))
779 sci_er_interrupt(irq, ptr, regs);
780 /* Break Interrupt */
781 if ((ssr_status&0x0010) && (scr_status&0x0200))
782 sci_br_interrupt(irq, ptr, regs);
787 #ifdef CONFIG_CPU_FREQ
789 * Here we define a transistion notifier so that we can update all of our
790 * ports' baud rate when the peripheral clock changes.
792 static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
794 struct cpufreq_freqs *freqs = p;
797 if ((phase == CPUFREQ_POSTCHANGE) ||
798 (phase == CPUFREQ_RESUMECHANGE)){
799 for (i = 0; i < SCI_NPORTS; i++) {
800 struct uart_port *port = &sci_ports[i].port;
804 * Update the uartclk per-port if frequency has
805 * changed, since it will no longer necessarily be
806 * consistent with the old frequency.
808 * Really we want to be able to do something like
809 * uart_change_speed() or something along those lines
810 * here to implicitly reset the per-port baud rate..
812 * Clean this up later..
814 clk = clk_get("module_clk");
815 port->uartclk = clk_get_rate(clk) * 16;
819 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
820 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
826 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
827 #endif /* CONFIG_CPU_FREQ */
829 static int sci_request_irq(struct sci_port *port)
832 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
833 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
836 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
837 "SCI Transmit Data Empty", "SCI Break" };
839 if (port->irqs[0] == port->irqs[1]) {
840 if (!port->irqs[0]) {
841 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
844 if (request_irq(port->irqs[0], sci_mpxed_interrupt, IRQF_DISABLED,
846 printk(KERN_ERR "sci: Cannot allocate irq.\n");
850 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
853 if (request_irq(port->irqs[i], handlers[i], IRQF_DISABLED,
855 printk(KERN_ERR "sci: Cannot allocate irq.\n");
864 static void sci_free_irq(struct sci_port *port)
868 if (port->irqs[0] == port->irqs[1]) {
870 printk("sci: sci_free_irq error\n");
872 free_irq(port->irqs[0], port);
874 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
878 free_irq(port->irqs[i], port);
883 static unsigned int sci_tx_empty(struct uart_port *port)
889 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
891 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
892 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
893 /* If you have signals for DTR and DCD, please implement here. */
896 static unsigned int sci_get_mctrl(struct uart_port *port)
898 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
901 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
904 static void sci_start_tx(struct uart_port *port)
906 struct sci_port *s = &sci_ports[port->line];
908 disable_irq(s->irqs[SCIx_TXI_IRQ]);
909 sci_transmit_chars(port);
910 enable_irq(s->irqs[SCIx_TXI_IRQ]);
913 static void sci_stop_tx(struct uart_port *port)
918 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
919 local_irq_save(flags);
920 ctrl = sci_in(port, SCSCR);
921 ctrl &= ~SCI_CTRL_FLAGS_TIE;
922 sci_out(port, SCSCR, ctrl);
923 local_irq_restore(flags);
926 static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
931 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
932 local_irq_save(flags);
933 ctrl = sci_in(port, SCSCR);
934 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
935 sci_out(port, SCSCR, ctrl);
936 local_irq_restore(flags);
939 static void sci_stop_rx(struct uart_port *port)
944 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
945 local_irq_save(flags);
946 ctrl = sci_in(port, SCSCR);
947 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
948 sci_out(port, SCSCR, ctrl);
949 local_irq_restore(flags);
952 static void sci_enable_ms(struct uart_port *port)
954 /* Nothing here yet .. */
957 static void sci_break_ctl(struct uart_port *port, int break_state)
959 /* Nothing here yet .. */
962 static int sci_startup(struct uart_port *port)
964 struct sci_port *s = &sci_ports[port->line];
966 #if defined(__H8300S__)
967 h8300_sci_enable(port, sci_enable);
972 sci_start_rx(port, 1);
977 static void sci_shutdown(struct uart_port *port)
979 struct sci_port *s = &sci_ports[port->line];
985 #if defined(__H8300S__)
986 h8300_sci_enable(port, sci_disable);
990 static void sci_set_termios(struct uart_port *port, struct termios *termios,
993 struct sci_port *s = &sci_ports[port->line];
994 unsigned int status, baud, smr_val;
998 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1000 spin_lock_irqsave(&port->lock, flags);
1003 status = sci_in(port, SCxSR);
1004 } while (!(status & SCxSR_TEND(port)));
1006 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1008 #if !defined(SCI_ONLY)
1009 if (port->type == PORT_SCIF) {
1010 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1014 smr_val = sci_in(port, SCSMR) & 3;
1015 if ((termios->c_cflag & CSIZE) == CS7)
1017 if (termios->c_cflag & PARENB)
1019 if (termios->c_cflag & PARODD)
1021 if (termios->c_cflag & CSTOPB)
1024 uart_update_timeout(port, termios->c_cflag, baud);
1026 sci_out(port, SCSMR, smr_val);
1034 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
1035 struct clk *clk = clk_get("module_clk");
1036 t = SCBRR_VALUE(baud, clk_get_rate(clk));
1039 t = SCBRR_VALUE(baud);
1047 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1050 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1052 sci_out(port, SCBRR, t);
1053 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1056 if (likely(s->init_pins))
1057 s->init_pins(port, termios->c_cflag);
1059 sci_out(port, SCSCR, SCSCR_INIT(port));
1061 if ((termios->c_cflag & CREAD) != 0)
1062 sci_start_rx(port,0);
1064 spin_unlock_irqrestore(&port->lock, flags);
1067 static const char *sci_type(struct uart_port *port)
1069 switch (port->type) {
1070 case PORT_SCI: return "sci";
1071 case PORT_SCIF: return "scif";
1072 case PORT_IRDA: return "irda";
1078 static void sci_release_port(struct uart_port *port)
1080 /* Nothing here yet .. */
1083 static int sci_request_port(struct uart_port *port)
1085 /* Nothing here yet .. */
1089 static void sci_config_port(struct uart_port *port, int flags)
1091 struct sci_port *s = &sci_ports[port->line];
1093 port->type = s->type;
1095 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1096 if (port->mapbase == 0)
1097 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1099 port->membase = (void *)port->mapbase;
1103 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1105 struct sci_port *s = &sci_ports[port->line];
1107 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1109 if (ser->baud_base < 2400)
1110 /* No paper tape reader for Mitch.. */
1116 static struct uart_ops sci_uart_ops = {
1117 .tx_empty = sci_tx_empty,
1118 .set_mctrl = sci_set_mctrl,
1119 .get_mctrl = sci_get_mctrl,
1120 .start_tx = sci_start_tx,
1121 .stop_tx = sci_stop_tx,
1122 .stop_rx = sci_stop_rx,
1123 .enable_ms = sci_enable_ms,
1124 .break_ctl = sci_break_ctl,
1125 .startup = sci_startup,
1126 .shutdown = sci_shutdown,
1127 .set_termios = sci_set_termios,
1129 .release_port = sci_release_port,
1130 .request_port = sci_request_port,
1131 .config_port = sci_config_port,
1132 .verify_port = sci_verify_port,
1135 static struct sci_port sci_ports[] = {
1136 #if defined(CONFIG_CPU_SUBTYPE_SH7708)
1139 .membase = (void *)0xfffffe80,
1140 .mapbase = 0xfffffe80,
1143 .ops = &sci_uart_ops,
1144 .flags = UPF_BOOT_AUTOCONF,
1150 #elif defined(CONFIG_CPU_SUBTYPE_SH7705)
1153 .membase = (void *)SCIF0,
1157 .ops = &sci_uart_ops,
1158 .flags = UPF_BOOT_AUTOCONF,
1162 .irqs = SH3_IRDA_IRQS,
1163 .init_pins = sci_init_pins_scif,
1167 .membase = (void *)SCIF2,
1171 .ops = &sci_uart_ops,
1172 .flags = UPF_BOOT_AUTOCONF,
1176 .irqs = SH3_SCIF_IRQS,
1177 .init_pins = sci_init_pins_scif,
1179 #elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
1182 .membase = (void *)0xfffffe80,
1183 .mapbase = 0xfffffe80,
1186 .ops = &sci_uart_ops,
1187 .flags = UPF_BOOT_AUTOCONF,
1195 .membase = (void *)0xa4000150,
1196 .mapbase = 0xa4000150,
1199 .ops = &sci_uart_ops,
1200 .flags = UPF_BOOT_AUTOCONF,
1204 .irqs = SH3_SCIF_IRQS,
1205 .init_pins = sci_init_pins_scif,
1209 .membase = (void *)0xa4000140,
1210 .mapbase = 0xa4000140,
1213 .ops = &sci_uart_ops,
1214 .flags = UPF_BOOT_AUTOCONF,
1218 .irqs = SH3_IRDA_IRQS,
1219 .init_pins = sci_init_pins_irda,
1221 #elif defined(CONFIG_CPU_SUBTYPE_SH7300)
1224 .membase = (void *)0xA4430000,
1225 .mapbase = 0xA4430000,
1228 .ops = &sci_uart_ops,
1229 .flags = UPF_BOOT_AUTOCONF,
1233 .irqs = SH7300_SCIF0_IRQS,
1234 .init_pins = sci_init_pins_scif,
1236 #elif defined(CONFIG_CPU_SUBTYPE_SH73180)
1239 .membase = (void *)0xffe00000,
1240 .mapbase = 0xffe00000,
1243 .ops = &sci_uart_ops,
1244 .flags = UPF_BOOT_AUTOCONF,
1248 .irqs = SH73180_SCIF_IRQS,
1249 .init_pins = sci_init_pins_scif,
1251 #elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
1254 .membase = (void *)0xffe80000,
1255 .mapbase = 0xffe80000,
1258 .ops = &sci_uart_ops,
1259 .flags = UPF_BOOT_AUTOCONF,
1263 .irqs = SH4_SCIF_IRQS,
1264 .init_pins = sci_init_pins_scif,
1266 #elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
1269 .membase = (void *)0xffe00000,
1270 .mapbase = 0xffe00000,
1273 .ops = &sci_uart_ops,
1274 .flags = UPF_BOOT_AUTOCONF,
1282 .membase = (void *)0xffe80000,
1283 .mapbase = 0xffe80000,
1286 .ops = &sci_uart_ops,
1287 .flags = UPF_BOOT_AUTOCONF,
1291 .irqs = SH4_SCIF_IRQS,
1292 .init_pins = sci_init_pins_scif,
1294 #elif defined(CONFIG_CPU_SUBTYPE_SH7760)
1297 .membase = (void *)0xfe600000,
1298 .mapbase = 0xfe600000,
1301 .ops = &sci_uart_ops,
1302 .flags = UPF_BOOT_AUTOCONF,
1306 .irqs = SH7760_SCIF0_IRQS,
1307 .init_pins = sci_init_pins_scif,
1311 .membase = (void *)0xfe610000,
1312 .mapbase = 0xfe610000,
1315 .ops = &sci_uart_ops,
1316 .flags = UPF_BOOT_AUTOCONF,
1320 .irqs = SH7760_SCIF1_IRQS,
1321 .init_pins = sci_init_pins_scif,
1325 .membase = (void *)0xfe620000,
1326 .mapbase = 0xfe620000,
1329 .ops = &sci_uart_ops,
1330 .flags = UPF_BOOT_AUTOCONF,
1334 .irqs = SH7760_SCIF2_IRQS,
1335 .init_pins = sci_init_pins_scif,
1337 #elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
1340 .membase = (void *)0xffe00000,
1341 .mapbase = 0xffe00000,
1344 .ops = &sci_uart_ops,
1345 .flags = UPF_BOOT_AUTOCONF,
1349 .irqs = STB1_SCIF1_IRQS,
1350 .init_pins = sci_init_pins_scif,
1354 .membase = (void *)0xffe80000,
1355 .mapbase = 0xffe80000,
1358 .ops = &sci_uart_ops,
1359 .flags = UPF_BOOT_AUTOCONF,
1363 .irqs = SH4_SCIF_IRQS,
1364 .init_pins = sci_init_pins_scif,
1366 #elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1371 .ops = &sci_uart_ops,
1372 .flags = UPF_BOOT_AUTOCONF,
1376 .irqs = SH5_SCIF_IRQS,
1377 .init_pins = sci_init_pins_scif,
1379 #elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
1382 .membase = (void *)0x00ffffb0,
1383 .mapbase = 0x00ffffb0,
1386 .ops = &sci_uart_ops,
1387 .flags = UPF_BOOT_AUTOCONF,
1391 .irqs = H8300H_SCI_IRQS0,
1392 .init_pins = sci_init_pins_sci,
1396 .membase = (void *)0x00ffffb8,
1397 .mapbase = 0x00ffffb8,
1400 .ops = &sci_uart_ops,
1401 .flags = UPF_BOOT_AUTOCONF,
1405 .irqs = H8300H_SCI_IRQS1,
1406 .init_pins = sci_init_pins_sci,
1410 .membase = (void *)0x00ffffc0,
1411 .mapbase = 0x00ffffc0,
1414 .ops = &sci_uart_ops,
1415 .flags = UPF_BOOT_AUTOCONF,
1419 .irqs = H8300H_SCI_IRQS2,
1420 .init_pins = sci_init_pins_sci,
1422 #elif defined(CONFIG_H8S2678)
1425 .membase = (void *)0x00ffff78,
1426 .mapbase = 0x00ffff78,
1429 .ops = &sci_uart_ops,
1430 .flags = UPF_BOOT_AUTOCONF,
1434 .irqs = H8S_SCI_IRQS0,
1435 .init_pins = sci_init_pins_sci,
1439 .membase = (void *)0x00ffff80,
1440 .mapbase = 0x00ffff80,
1443 .ops = &sci_uart_ops,
1444 .flags = UPF_BOOT_AUTOCONF,
1448 .irqs = H8S_SCI_IRQS1,
1449 .init_pins = sci_init_pins_sci,
1453 .membase = (void *)0x00ffff88,
1454 .mapbase = 0x00ffff88,
1457 .ops = &sci_uart_ops,
1458 .flags = UPF_BOOT_AUTOCONF,
1462 .irqs = H8S_SCI_IRQS2,
1463 .init_pins = sci_init_pins_sci,
1465 #elif defined(CONFIG_CPU_SUBTYPE_SH7770)
1468 .membase = (void *)0xff923000,
1469 .mapbase = 0xff923000,
1472 .ops = &sci_uart_ops,
1473 .flags = UPF_BOOT_AUTOCONF,
1477 .irqs = SH7770_SCIF0_IRQS,
1478 .init_pins = sci_init_pins_scif,
1482 .membase = (void *)0xff924000,
1483 .mapbase = 0xff924000,
1486 .ops = &sci_uart_ops,
1487 .flags = UPF_BOOT_AUTOCONF,
1491 .irqs = SH7770_SCIF1_IRQS,
1492 .init_pins = sci_init_pins_scif,
1496 .membase = (void *)0xff925000,
1497 .mapbase = 0xff925000,
1500 .ops = &sci_uart_ops,
1501 .flags = UPF_BOOT_AUTOCONF,
1505 .irqs = SH7770_SCIF2_IRQS,
1506 .init_pins = sci_init_pins_scif,
1508 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
1511 .membase = (void *)0xffe00000,
1512 .mapbase = 0xffe00000,
1515 .ops = &sci_uart_ops,
1516 .flags = UPF_BOOT_AUTOCONF,
1520 .irqs = SH7780_SCIF0_IRQS,
1521 .init_pins = sci_init_pins_scif,
1525 .membase = (void *)0xffe10000,
1526 .mapbase = 0xffe10000,
1529 .ops = &sci_uart_ops,
1530 .flags = UPF_BOOT_AUTOCONF,
1534 .irqs = SH7780_SCIF1_IRQS,
1535 .init_pins = sci_init_pins_scif,
1538 #error "CPU subtype not defined"
1542 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1544 * Print a string to the serial port trying not to disturb
1545 * any possible real use of the port...
1547 static void serial_console_write(struct console *co, const char *s,
1550 put_string(serial_console_port, s, count);
1553 static int __init serial_console_setup(struct console *co, char *options)
1555 struct uart_port *port;
1562 serial_console_port = &sci_ports[co->index];
1563 port = &serial_console_port->port;
1564 port->type = serial_console_port->type;
1566 #ifdef CONFIG_SUPERH64
1567 /* This is especially needed on sh64 to remap the SCIF */
1568 sci_config_port(port, 0);
1572 * We need to set the initial uartclk here, since otherwise it will
1573 * only ever be setup at sci_init() time.
1575 #if defined(__H8300H__) || defined(__H8300S__)
1576 port->uartclk = CONFIG_CPU_CLOCK;
1578 #if defined(__H8300S__)
1579 h8300_sci_enable(port, sci_enable);
1581 #elif defined(CONFIG_SUPERH64)
1582 port->uartclk = current_cpu_data.module_clock * 16;
1585 struct clk *clk = clk_get("module_clk");
1586 port->uartclk = clk_get_rate(clk) * 16;
1591 uart_parse_options(options, &baud, &parity, &bits, &flow);
1593 ret = uart_set_options(port, co, baud, parity, bits, flow);
1594 #if defined(__H8300H__) || defined(__H8300S__)
1595 /* disable rx interrupt */
1602 static struct console serial_console = {
1604 .device = uart_console_device,
1605 .write = serial_console_write,
1606 .setup = serial_console_setup,
1607 .flags = CON_PRINTBUFFER,
1609 .data = &sci_uart_driver,
1612 static int __init sci_console_init(void)
1614 register_console(&serial_console);
1618 console_initcall(sci_console_init);
1619 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1621 #ifdef CONFIG_SH_KGDB
1623 * FIXME: Most of this can go away.. at the moment, we rely on
1624 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1625 * most of that can easily be done here instead.
1627 * For the time being, just accept the values that were parsed earlier..
1629 static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1630 int *parity, int *bits)
1633 *parity = tolower(kgdb_parity);
1634 *bits = kgdb_bits - '0';
1638 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1639 * care of the early-on initialization for kgdb, regardless of whether we
1640 * actually use kgdb as a console or not.
1642 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1644 int __init kgdb_console_setup(struct console *co, char *options)
1646 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1652 if (co->index != kgdb_portnum)
1653 co->index = kgdb_portnum;
1656 uart_parse_options(options, &baud, &parity, &bits, &flow);
1658 kgdb_console_get_options(port, &baud, &parity, &bits);
1660 kgdb_getchar = kgdb_sci_getchar;
1661 kgdb_putchar = kgdb_sci_putchar;
1663 return uart_set_options(port, co, baud, parity, bits, flow);
1665 #endif /* CONFIG_SH_KGDB */
1667 #ifdef CONFIG_SH_KGDB_CONSOLE
1668 static struct console kgdb_console = {
1670 .write = kgdb_console_write,
1671 .setup = kgdb_console_setup,
1672 .flags = CON_PRINTBUFFER | CON_ENABLED,
1674 .data = &sci_uart_driver,
1677 /* Register the KGDB console so we get messages (d'oh!) */
1678 static int __init kgdb_console_init(void)
1680 register_console(&kgdb_console);
1684 console_initcall(kgdb_console_init);
1685 #endif /* CONFIG_SH_KGDB_CONSOLE */
1687 #if defined(CONFIG_SH_KGDB_CONSOLE)
1688 #define SCI_CONSOLE &kgdb_console
1689 #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1690 #define SCI_CONSOLE &serial_console
1692 #define SCI_CONSOLE 0
1695 static char banner[] __initdata =
1696 KERN_INFO "SuperH SCI(F) driver initialized\n";
1698 static struct uart_driver sci_uart_driver = {
1699 .owner = THIS_MODULE,
1700 .driver_name = "sci",
1701 .dev_name = "ttySC",
1703 .minor = SCI_MINOR_START,
1704 .cons = SCI_CONSOLE,
1707 static int __init sci_init(void)
1711 printk("%s", banner);
1713 sci_uart_driver.nr = ARRAY_SIZE(sci_ports);
1715 ret = uart_register_driver(&sci_uart_driver);
1717 for (chan = 0; chan < SCI_NPORTS; chan++) {
1718 struct sci_port *sciport = &sci_ports[chan];
1720 #if defined(__H8300H__) || defined(__H8300S__)
1721 sciport->port.uartclk = CONFIG_CPU_CLOCK;
1722 #elif defined(CONFIG_SUPERH64)
1723 sciport->port.uartclk = current_cpu_data.module_clock * 16;
1725 struct clk *clk = clk_get("module_clk");
1726 sciport->port.uartclk = clk_get_rate(clk) * 16;
1729 uart_add_one_port(&sci_uart_driver, &sciport->port);
1730 sciport->break_timer.data = (unsigned long)sciport;
1731 sciport->break_timer.function = sci_break_timer;
1732 init_timer(&sciport->break_timer);
1736 #ifdef CONFIG_CPU_FREQ
1737 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1738 printk("sci: CPU frequency notifier registered\n");
1741 #ifdef CONFIG_SH_STANDARD_BIOS
1742 sh_bios_gdb_detach();
1748 static void __exit sci_exit(void)
1752 for (chan = 0; chan < SCI_NPORTS; chan++)
1753 uart_remove_one_port(&sci_uart_driver, &sci_ports[chan].port);
1755 uart_unregister_driver(&sci_uart_driver);
1758 module_init(sci_init);
1759 module_exit(sci_exit);