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>
46 #include <linux/ledman.h>
49 #include <asm/system.h>
52 #include <asm/uaccess.h>
54 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
55 #include <asm/clock.h>
56 #include <asm/sh_bios.h>
62 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
69 struct uart_port port
;
74 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
75 unsigned int irqs
[SCIx_NR_IRQS
];
77 /* Port pin configuration */
78 void (*init_pins
)(struct uart_port
*port
,
81 /* Port enable callback */
82 void (*enable
)(struct uart_port
*port
);
84 /* Port disable callback */
85 void (*disable
)(struct uart_port
*port
);
88 struct timer_list break_timer
;
91 #if defined(CONFIG_SH_SECUREEDGE5410)
97 static struct sci_port
*kgdb_sci_port
;
100 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
101 static struct sci_port
*serial_console_port
;
104 /* Function prototypes */
105 static void sci_stop_tx(struct uart_port
*port
);
107 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
109 static struct sci_port sci_ports
[SCI_NPORTS
];
110 static struct uart_driver sci_uart_driver
;
112 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && \
113 defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
114 static inline void handle_error(struct uart_port
*port
)
116 /* Clear error flags */
117 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
120 static int get_char(struct uart_port
*port
)
123 unsigned short status
;
126 spin_lock_irqsave(&port
->lock
, flags
);
128 status
= sci_in(port
, SCxSR
);
129 if (status
& SCxSR_ERRORS(port
)) {
133 } while (!(status
& SCxSR_RDxF(port
)));
134 c
= sci_in(port
, SCxRDR
);
135 sci_in(port
, SCxSR
); /* Dummy read */
136 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
137 spin_unlock_irqrestore(&port
->lock
, flags
);
141 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
143 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || defined(CONFIG_SH_KGDB)
144 static void put_char(struct uart_port
*port
, char c
)
147 unsigned short status
;
149 spin_lock_irqsave(&port
->lock
, flags
);
152 status
= sci_in(port
, SCxSR
);
153 } while (!(status
& SCxSR_TDxE(port
)));
155 sci_out(port
, SCxTDR
, c
);
156 sci_in(port
, SCxSR
); /* Dummy read */
157 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
159 spin_unlock_irqrestore(&port
->lock
, flags
);
163 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
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 */
216 #if defined(CONFIG_SH_SECUREEDGE5410)
217 #include <asm/snapgear.h>
218 struct timer_list sci_timer_struct
;
219 static unsigned char sci_dcdstatus
[2];
222 * This subroutine is called when the RS_TIMER goes off. It is used
223 * to monitor the state of the DCD lines - since they have no edge
224 * sensors and interrupt generators.
226 static void sci_timer(unsigned long data
)
229 unsigned char dcdstatus
[2];
231 s
= SECUREEDGE_READ_IOPORT();
232 dcdstatus
[0] = !(s
& 0x10);
233 dcdstatus
[1] = !(s
& 0x1);
235 for (i
= 0; i
< 2; i
++) {
236 if (dcdstatus
[i
] != sci_dcdstatus
[i
]) {
237 if (sci_ports
[i
].open
!= 0) {
238 uart_handle_dcd_change(&sci_ports
[i
].port
, dcdstatus
[i
]);
241 sci_dcdstatus
[i
] = dcdstatus
[i
];
244 sci_timer_struct
.expires
= jiffies
+ HZ
/25;
245 add_timer(&sci_timer_struct
);
251 #ifdef CONFIG_SH_KGDB
252 static int kgdb_sci_getchar(void)
256 /* Keep trying to read a character, this could be neater */
257 while ((c
= get_char(kgdb_sci_port
)) < 0)
263 static inline void kgdb_sci_putchar(int c
)
265 put_char(kgdb_sci_port
, c
);
267 #endif /* CONFIG_SH_KGDB */
269 #if defined(__H8300S__)
270 enum { sci_disable
, sci_enable
};
272 static void h8300_sci_config(struct uart_port
* port
, unsigned int ctrl
)
274 volatile unsigned char *mstpcrl
=(volatile unsigned char *)MSTPCRL
;
275 int ch
= (port
->mapbase
- SMR0
) >> 3;
276 unsigned char mask
= 1 << (ch
+1);
278 if (ctrl
== sci_disable
) {
285 static inline void h8300_sci_enable(struct uart_port
*port
)
287 h8300_sci_config(port
, sci_enable
);
290 static inline void h8300_sci_disable(struct uart_port
*port
)
292 h8300_sci_config(port
, sci_disable
);
296 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF) && \
297 defined(__H8300H__) || defined(__H8300S__)
298 static void sci_init_pins_sci(struct uart_port
* port
, unsigned int cflag
)
300 int ch
= (port
->mapbase
- SMR0
) >> 3;
303 H8300_GPIO_DDR(h8300_sci_pins
[ch
].port
,
304 h8300_sci_pins
[ch
].rx
,
306 H8300_GPIO_DDR(h8300_sci_pins
[ch
].port
,
307 h8300_sci_pins
[ch
].tx
,
311 H8300_SCI_DR(ch
) |= h8300_sci_pins
[ch
].tx
;
314 #define sci_init_pins_sci NULL
317 #if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
318 static void sci_init_pins_irda(struct uart_port
*port
, unsigned int cflag
)
320 unsigned int fcr_val
= 0;
323 fcr_val
|= SCFCR_MCE
;
325 sci_out(port
, SCFCR
, fcr_val
);
328 #define sci_init_pins_irda NULL
332 #define sci_init_pins_scif NULL
335 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
336 #if defined(CONFIG_CPU_SUBTYPE_SH7300) || defined(CONFIG_CPU_SUBTYPE_SH7710)
337 /* SH7300 doesn't use RTS/CTS */
338 static void sci_init_pins_scif(struct uart_port
*port
, unsigned int cflag
)
340 sci_out(port
, SCFCR
, 0);
342 #elif defined(CONFIG_CPU_SH3)
343 /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
344 static void sci_init_pins_scif(struct uart_port
*port
, unsigned int cflag
)
346 unsigned int fcr_val
= 0;
349 /* We need to set SCPCR to enable RTS/CTS */
350 data
= ctrl_inw(SCPCR
);
351 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
352 ctrl_outw(data
& 0x0fcf, SCPCR
);
355 fcr_val
|= SCFCR_MCE
;
357 /* We need to set SCPCR to enable RTS/CTS */
358 data
= ctrl_inw(SCPCR
);
359 /* Clear out SCP7MD1,0, SCP4MD1,0,
360 Set SCP6MD1,0 = {01} (output) */
361 ctrl_outw((data
& 0x0fcf) | 0x1000, SCPCR
);
363 data
= ctrl_inb(SCPDR
);
364 /* Set /RTS2 (bit6) = 0 */
365 ctrl_outb(data
& 0xbf, SCPDR
);
368 sci_out(port
, SCFCR
, fcr_val
);
372 static void sci_init_pins_scif(struct uart_port
*port
, unsigned int cflag
)
374 unsigned int fcr_val
= 0;
376 if (cflag
& CRTSCTS
) {
377 fcr_val
|= SCFCR_MCE
;
379 #ifdef CONFIG_CPU_SUBTYPE_SH7343
381 #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
382 ctrl_outw(0x0080, SCSPTR0
); /* Set RTS = 1 */
384 ctrl_outw(0x0080, SCSPTR2
); /* Set RTS = 1 */
387 sci_out(port
, SCFCR
, fcr_val
);
391 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || defined(CONFIG_CPU_SUBTYPE_SH7780)
392 static inline int scif_txroom(struct uart_port
*port
)
394 return SCIF_TXROOM_MAX
- (sci_in(port
, SCTFDR
) & 0x7f);
397 static inline int scif_rxroom(struct uart_port
*port
)
399 return sci_in(port
, SCRFDR
) & 0x7f;
402 static inline int scif_txroom(struct uart_port
*port
)
404 return SCIF_TXROOM_MAX
- (sci_in(port
, SCFDR
) >> 8);
407 static inline int scif_rxroom(struct uart_port
*port
)
409 return sci_in(port
, SCFDR
) & SCIF_RFDC_MASK
;
412 #endif /* SCIF_ONLY || SCI_AND_SCIF */
414 static inline int sci_txroom(struct uart_port
*port
)
416 return ((sci_in(port
, SCxSR
) & SCI_TDRE
) != 0);
419 static inline int sci_rxroom(struct uart_port
*port
)
421 return ((sci_in(port
, SCxSR
) & SCxSR_RDxF(port
)) != 0);
424 /* ********************************************************************** *
425 * the interrupt related routines *
426 * ********************************************************************** */
428 static void sci_transmit_chars(struct uart_port
*port
)
430 struct circ_buf
*xmit
= &port
->info
->xmit
;
431 unsigned int stopped
= uart_tx_stopped(port
);
432 unsigned short status
;
437 ledman_cmd(LEDMAN_CMD_SET
,
438 port
== &sci_ports
[0].port
? LEDMAN_COM1_TX
: LEDMAN_COM2_TX
);
441 status
= sci_in(port
, SCxSR
);
442 if (!(status
& SCxSR_TDxE(port
))) {
443 ctrl
= sci_in(port
, SCSCR
);
444 if (uart_circ_empty(xmit
)) {
445 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
447 ctrl
|= SCI_CTRL_FLAGS_TIE
;
449 sci_out(port
, SCSCR
, ctrl
);
454 if (port
->type
== PORT_SCIF
)
455 count
= scif_txroom(port
);
458 count
= sci_txroom(port
);
466 } else if (!uart_circ_empty(xmit
) && !stopped
) {
467 c
= xmit
->buf
[xmit
->tail
];
468 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
473 sci_out(port
, SCxTDR
, c
);
476 } while (--count
> 0);
478 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
480 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
481 uart_write_wakeup(port
);
482 if (uart_circ_empty(xmit
)) {
485 ctrl
= sci_in(port
, SCSCR
);
487 #if !defined(SCI_ONLY)
488 if (port
->type
== PORT_SCIF
) {
489 sci_in(port
, SCxSR
); /* Dummy read */
490 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
494 ctrl
|= SCI_CTRL_FLAGS_TIE
;
495 sci_out(port
, SCSCR
, ctrl
);
499 /* On SH3, SCIF may read end-of-break as a space->mark char */
500 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
502 static inline void sci_receive_chars(struct uart_port
*port
)
504 struct sci_port
*sci_port
= (struct sci_port
*)port
;
505 struct tty_struct
*tty
= port
->info
->tty
;
506 int i
, count
, copied
= 0;
507 unsigned short status
;
510 status
= sci_in(port
, SCxSR
);
511 if (!(status
& SCxSR_RDxF(port
)))
515 ledman_cmd(LEDMAN_CMD_SET
,
516 port
== &sci_ports
[0].port
? LEDMAN_COM1_RX
: LEDMAN_COM2_RX
);
520 #if !defined(SCI_ONLY)
521 if (port
->type
== PORT_SCIF
)
522 count
= scif_rxroom(port
);
525 count
= sci_rxroom(port
);
527 /* Don't copy more bytes than there is room for in the buffer */
528 count
= tty_buffer_request_room(tty
, count
);
530 /* If for any reason we can't copy more data, we're done! */
534 if (port
->type
== PORT_SCI
) {
535 char c
= sci_in(port
, SCxRDR
);
536 if (uart_handle_sysrq_char(port
, c
) || sci_port
->break_flag
)
539 tty_insert_flip_char(tty
, c
, TTY_NORMAL
);
542 for (i
=0; i
<count
; i
++) {
543 char c
= sci_in(port
, SCxRDR
);
544 status
= sci_in(port
, SCxSR
);
545 #if defined(CONFIG_CPU_SH3)
546 /* Skip "chars" during break */
547 if (sci_port
->break_flag
) {
549 (status
& SCxSR_FER(port
))) {
554 /* Nonzero => end-of-break */
555 pr_debug("scif: debounce<%02x>\n", c
);
556 sci_port
->break_flag
= 0;
563 #endif /* CONFIG_CPU_SH3 */
564 if (uart_handle_sysrq_char(port
, c
)) {
569 /* Store data and status */
570 if (status
&SCxSR_FER(port
)) {
572 pr_debug("sci: frame error\n");
573 } else if (status
&SCxSR_PER(port
)) {
575 pr_debug("sci: parity error\n");
578 tty_insert_flip_char(tty
, c
, flag
);
582 sci_in(port
, SCxSR
); /* dummy read */
583 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
586 port
->icount
.rx
+= count
;
590 /* Tell the rest of the system the news. New characters! */
591 tty_flip_buffer_push(tty
);
593 sci_in(port
, SCxSR
); /* dummy read */
594 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
598 #define SCI_BREAK_JIFFIES (HZ/20)
599 /* The sci generates interrupts during the break,
600 * 1 per millisecond or so during the break period, for 9600 baud.
601 * So dont bother disabling interrupts.
602 * But dont want more than 1 break event.
603 * Use a kernel timer to periodically poll the rx line until
604 * the break is finished.
606 static void sci_schedule_break_timer(struct sci_port
*port
)
608 port
->break_timer
.expires
= jiffies
+ SCI_BREAK_JIFFIES
;
609 add_timer(&port
->break_timer
);
611 /* Ensure that two consecutive samples find the break over. */
612 static void sci_break_timer(unsigned long data
)
614 struct sci_port
*port
= (struct sci_port
*)data
;
616 if (sci_rxd_in(&port
->port
) == 0) {
617 port
->break_flag
= 1;
618 sci_schedule_break_timer(port
);
619 } else if (port
->break_flag
== 1) {
621 port
->break_flag
= 2;
622 sci_schedule_break_timer(port
);
624 port
->break_flag
= 0;
627 static inline int sci_handle_errors(struct uart_port
*port
)
630 unsigned short status
= sci_in(port
, SCxSR
);
631 struct tty_struct
*tty
= port
->info
->tty
;
633 if (status
& SCxSR_ORER(port
)) {
635 if (tty_insert_flip_char(tty
, 0, TTY_OVERRUN
))
637 pr_debug("sci: overrun error\n");
640 if (status
& SCxSR_FER(port
)) {
641 if (sci_rxd_in(port
) == 0) {
642 /* Notify of BREAK */
643 struct sci_port
*sci_port
= (struct sci_port
*)port
;
645 if (!sci_port
->break_flag
) {
646 sci_port
->break_flag
= 1;
647 sci_schedule_break_timer(sci_port
);
649 /* Do sysrq handling. */
650 if (uart_handle_break(port
))
652 pr_debug("sci: BREAK detected\n");
653 if (tty_insert_flip_char(tty
, 0, TTY_BREAK
))
658 if (tty_insert_flip_char(tty
, 0, TTY_FRAME
))
660 pr_debug("sci: frame error\n");
664 if (status
& SCxSR_PER(port
)) {
666 if (tty_insert_flip_char(tty
, 0, TTY_PARITY
))
668 pr_debug("sci: parity error\n");
672 tty_flip_buffer_push(tty
);
677 static inline int sci_handle_breaks(struct uart_port
*port
)
680 unsigned short status
= sci_in(port
, SCxSR
);
681 struct tty_struct
*tty
= port
->info
->tty
;
682 struct sci_port
*s
= &sci_ports
[port
->line
];
684 if (!s
->break_flag
&& status
& SCxSR_BRK(port
)) {
685 #if defined(CONFIG_CPU_SH3)
689 /* Notify of BREAK */
690 if (tty_insert_flip_char(tty
, 0, TTY_BREAK
))
692 pr_debug("sci: BREAK detected\n");
695 #if defined(SCIF_ORER)
696 /* XXX: Handle SCIF overrun error */
697 if (port
->type
== PORT_SCIF
&& (sci_in(port
, SCLSR
) & SCIF_ORER
) != 0) {
698 sci_out(port
, SCLSR
, 0);
699 if (tty_insert_flip_char(tty
, 0, TTY_OVERRUN
)) {
701 pr_debug("sci: overrun error\n");
707 tty_flip_buffer_push(tty
);
712 static irqreturn_t
sci_rx_interrupt(int irq
, void *port
)
714 /* I think sci_receive_chars has to be called irrespective
715 * of whether the I_IXOFF is set, otherwise, how is the interrupt
718 sci_receive_chars(port
);
723 static irqreturn_t
sci_tx_interrupt(int irq
, void *ptr
)
725 struct uart_port
*port
= ptr
;
727 spin_lock_irq(&port
->lock
);
728 sci_transmit_chars(port
);
729 spin_unlock_irq(&port
->lock
);
734 static irqreturn_t
sci_er_interrupt(int irq
, void *ptr
)
736 struct uart_port
*port
= ptr
;
739 if (port
->type
== PORT_SCI
) {
740 if (sci_handle_errors(port
)) {
741 /* discard character in rx buffer */
743 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
746 #if defined(SCIF_ORER)
747 if((sci_in(port
, SCLSR
) & SCIF_ORER
) != 0) {
748 struct tty_struct
*tty
= port
->info
->tty
;
750 sci_out(port
, SCLSR
, 0);
751 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
752 tty_flip_buffer_push(tty
);
753 pr_debug("scif: overrun error\n");
756 sci_rx_interrupt(irq
, ptr
);
759 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
761 /* Kick the transmission */
762 sci_tx_interrupt(irq
, ptr
);
767 static irqreturn_t
sci_br_interrupt(int irq
, void *ptr
)
769 struct uart_port
*port
= ptr
;
772 sci_handle_breaks(port
);
774 #ifdef CONFIG_SH_KGDB
775 /* Break into the debugger if a break is detected */
779 sci_out(port
, SCxSR
, SCxSR_BREAK_CLEAR(port
));
784 static irqreturn_t
sci_mpxed_interrupt(int irq
, void *ptr
)
786 unsigned short ssr_status
, scr_status
;
787 struct uart_port
*port
= ptr
;
789 ssr_status
= sci_in(port
,SCxSR
);
790 scr_status
= sci_in(port
,SCSCR
);
793 if ((ssr_status
& 0x0020) && (scr_status
& 0x0080))
794 sci_tx_interrupt(irq
, ptr
);
796 if ((ssr_status
& 0x0002) && (scr_status
& 0x0040))
797 sci_rx_interrupt(irq
, ptr
);
798 /* Error Interrupt */
799 if ((ssr_status
& 0x0080) && (scr_status
& 0x0400))
800 sci_er_interrupt(irq
, ptr
);
801 /* Break Interrupt */
802 if ((ssr_status
& 0x0010) && (scr_status
& 0x0200))
803 sci_br_interrupt(irq
, ptr
);
808 #ifdef CONFIG_CPU_FREQ
810 * Here we define a transistion notifier so that we can update all of our
811 * ports' baud rate when the peripheral clock changes.
813 static int sci_notifier(struct notifier_block
*self
,
814 unsigned long phase
, void *p
)
816 struct cpufreq_freqs
*freqs
= p
;
819 if ((phase
== CPUFREQ_POSTCHANGE
) ||
820 (phase
== CPUFREQ_RESUMECHANGE
)){
821 for (i
= 0; i
< SCI_NPORTS
; i
++) {
822 struct uart_port
*port
= &sci_ports
[i
].port
;
826 * Update the uartclk per-port if frequency has
827 * changed, since it will no longer necessarily be
828 * consistent with the old frequency.
830 * Really we want to be able to do something like
831 * uart_change_speed() or something along those lines
832 * here to implicitly reset the per-port baud rate..
834 * Clean this up later..
836 clk
= clk_get("module_clk");
837 port
->uartclk
= clk_get_rate(clk
) * 16;
841 printk(KERN_INFO
"%s: got a postchange notification "
842 "for cpu %d (old %d, new %d)\n",
843 __FUNCTION__
, freqs
->cpu
, freqs
->old
, freqs
->new);
849 static struct notifier_block sci_nb
= { &sci_notifier
, NULL
, 0 };
850 #endif /* CONFIG_CPU_FREQ */
852 static int sci_request_irq(struct sci_port
*port
)
855 irqreturn_t (*handlers
[4])(int irq
, void *ptr
) = {
856 sci_er_interrupt
, sci_rx_interrupt
, sci_tx_interrupt
,
859 const char *desc
[] = { "SCI Receive Error", "SCI Receive Data Full",
860 "SCI Transmit Data Empty", "SCI Break" };
862 if (port
->irqs
[0] == port
->irqs
[1]) {
863 if (!port
->irqs
[0]) {
864 printk(KERN_ERR
"sci: Cannot allocate irq.(IRQ=0)\n");
868 if (request_irq(port
->irqs
[0], sci_mpxed_interrupt
,
869 IRQF_DISABLED
, "sci", port
)) {
870 printk(KERN_ERR
"sci: Cannot allocate irq.\n");
874 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++) {
877 if (request_irq(port
->irqs
[i
], handlers
[i
],
878 IRQF_DISABLED
, desc
[i
], port
)) {
879 printk(KERN_ERR
"sci: Cannot allocate irq.\n");
888 static void sci_free_irq(struct sci_port
*port
)
892 if (port
->irqs
[0] == port
->irqs
[1]) {
894 printk("sci: sci_free_irq error\n");
896 free_irq(port
->irqs
[0], port
);
898 for (i
= 0; i
< ARRAY_SIZE(port
->irqs
); i
++) {
902 free_irq(port
->irqs
[i
], port
);
907 static unsigned int sci_tx_empty(struct uart_port
*port
)
913 static void sci_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
915 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
916 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
917 /* If you have signals for DTR and DCD, please implement here. */
918 #if defined(CONFIG_SH_SECUREEDGE5410)
921 local_irq_save(flags
);
922 switch (port
->line
) {
923 case 1: /* port 1 only */
924 if (mctrl
& TIOCM_DTR
)
925 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0080);
927 SECUREEDGE_WRITE_IOPORT(0x0080, 0x0080);
928 if ((sci_in(port
, SCFCR
) & SCFCR_MCE
) == 0) {
929 if (mctrl
& TIOCM_RTS
)
930 sci_out(port
, SCSPTR
, sci_in(port
, SCSPTR
) & ~0x40);
932 sci_out(port
, SCSPTR
, sci_in(port
, SCSPTR
) | 0x40);
935 case 0: /* port 0 only */
936 if (mctrl
& TIOCM_DTR
)
937 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0200);
939 SECUREEDGE_WRITE_IOPORT(0x0200, 0x0200);
940 if (mctrl
& TIOCM_RTS
)
941 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0100);
943 SECUREEDGE_WRITE_IOPORT(0x0100, 0x0100);
946 local_irq_restore(flags
);
950 static unsigned int sci_get_mctrl(struct uart_port
*port
)
952 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
955 #if defined(CONFIG_SH_SECUREEDGE5410)
956 int rc
= TIOCM_DTR
| TIOCM_RTS
| TIOCM_DSR
;
958 switch (port
->line
) {
959 case 1: /* port 1 only */
961 unsigned short s
= SECUREEDGE_READ_IOPORT();
962 rc
= TIOCM_RTS
|TIOCM_DSR
|TIOCM_CTS
;
964 if ((sci_in(port
, SCFCR
) & SCFCR_MCE
) == 0) {
965 if (sci_in(port
, SCSPTR
) & 0x0040)
967 if (sci_in(port
, SCSPTR
) & 0x0010)
971 if ((s
& 0x0001) == 0)
973 if ((SECUREEDGE_READ_IOPORT() & 0x0080) == 0)
977 case 0: /* port 0 only */
979 unsigned short s
= SECUREEDGE_READ_IOPORT();
982 if ((s
& 0x0010) == 0)
984 if ((s
& 0x0004) == 0)
986 if ((SECUREEDGE_READ_IOPORT() & 0x0200) == 0)
988 if ((SECUREEDGE_READ_IOPORT() & 0x0100) == 0)
995 return TIOCM_DTR
| TIOCM_RTS
| TIOCM_DSR
;
998 static void sci_start_tx(struct uart_port
*port
)
1000 unsigned short ctrl
;
1002 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1003 ctrl
= sci_in(port
, SCSCR
);
1004 ctrl
|= SCI_CTRL_FLAGS_TIE
;
1005 sci_out(port
, SCSCR
, ctrl
);
1008 static void sci_stop_tx(struct uart_port
*port
)
1010 unsigned short ctrl
;
1012 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1013 ctrl
= sci_in(port
, SCSCR
);
1014 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
1015 sci_out(port
, SCSCR
, ctrl
);
1018 static void sci_start_rx(struct uart_port
*port
, unsigned int tty_start
)
1020 unsigned short ctrl
;
1022 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
1023 ctrl
= sci_in(port
, SCSCR
);
1024 ctrl
|= SCI_CTRL_FLAGS_RIE
| SCI_CTRL_FLAGS_REIE
;
1025 sci_out(port
, SCSCR
, ctrl
);
1028 static void sci_stop_rx(struct uart_port
*port
)
1030 unsigned short ctrl
;
1032 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
1033 ctrl
= sci_in(port
, SCSCR
);
1034 ctrl
&= ~(SCI_CTRL_FLAGS_RIE
| SCI_CTRL_FLAGS_REIE
);
1035 sci_out(port
, SCSCR
, ctrl
);
1038 static void sci_enable_ms(struct uart_port
*port
)
1040 /* Nothing here yet .. */
1043 static void sci_break_ctl(struct uart_port
*port
, int break_state
)
1045 /* Nothing here yet .. */
1048 static int sci_startup(struct uart_port
*port
)
1050 struct sci_port
*s
= &sci_ports
[port
->line
];
1052 #if defined(CONFIG_SH_SECUREEDGE5410)
1053 sci_ports
[port
->line
].open
++;
1055 #if defined(__H8300S__)
1056 h8300_sci_enable(port
, sci_enable
);
1063 sci_start_rx(port
, 1);
1068 static void sci_shutdown(struct uart_port
*port
)
1070 struct sci_port
*s
= &sci_ports
[port
->line
];
1076 #if defined(__H8300S__)
1077 h8300_sci_enable(port
, sci_disable
);
1079 #if defined(CONFIG_SH_SECUREEDGE5410)
1080 sci_ports
[port
->line
].open
--;
1086 static void sci_set_termios(struct uart_port
*port
, struct termios
*termios
,
1087 struct termios
*old
)
1089 struct sci_port
*s
= &sci_ports
[port
->line
];
1090 unsigned int status
, baud
, smr_val
;
1091 unsigned long flags
;
1094 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
1102 #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
1103 struct clk
*clk
= clk_get("module_clk");
1104 t
= SCBRR_VALUE(baud
, clk_get_rate(clk
));
1107 t
= SCBRR_VALUE(baud
);
1113 spin_lock_irqsave(&port
->lock
, flags
);
1116 status
= sci_in(port
, SCxSR
);
1117 } while (!(status
& SCxSR_TEND(port
)));
1119 sci_out(port
, SCSCR
, 0x00); /* TE=0, RE=0, CKE1=0 */
1121 #if !defined(SCI_ONLY)
1122 if (port
->type
== PORT_SCIF
)
1123 sci_out(port
, SCFCR
, SCFCR_RFRST
| SCFCR_TFRST
);
1126 smr_val
= sci_in(port
, SCSMR
) & 3;
1127 if ((termios
->c_cflag
& CSIZE
) == CS7
)
1129 if (termios
->c_cflag
& PARENB
)
1131 if (termios
->c_cflag
& PARODD
)
1133 if (termios
->c_cflag
& CSTOPB
)
1136 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1138 sci_out(port
, SCSMR
, smr_val
);
1142 sci_out(port
, SCSMR
, (sci_in(port
, SCSMR
) & ~3) | 1);
1145 sci_out(port
, SCSMR
, sci_in(port
, SCSMR
) & ~3);
1147 sci_out(port
, SCBRR
, t
);
1148 udelay((1000000+(baud
-1)) / baud
); /* Wait one bit interval */
1151 if (likely(s
->init_pins
))
1152 s
->init_pins(port
, termios
->c_cflag
);
1154 sci_out(port
, SCSCR
, SCSCR_INIT(port
));
1156 if ((termios
->c_cflag
& CREAD
) != 0)
1157 sci_start_rx(port
,0);
1159 spin_unlock_irqrestore(&port
->lock
, flags
);
1162 static const char *sci_type(struct uart_port
*port
)
1164 switch (port
->type
) {
1165 case PORT_SCI
: return "sci";
1166 case PORT_SCIF
: return "scif";
1167 case PORT_IRDA
: return "irda";
1173 static void sci_release_port(struct uart_port
*port
)
1175 /* Nothing here yet .. */
1178 static int sci_request_port(struct uart_port
*port
)
1180 /* Nothing here yet .. */
1184 static void sci_config_port(struct uart_port
*port
, int flags
)
1186 struct sci_port
*s
= &sci_ports
[port
->line
];
1188 port
->type
= s
->type
;
1190 switch (port
->type
) {
1192 s
->init_pins
= sci_init_pins_sci
;
1195 s
->init_pins
= sci_init_pins_scif
;
1198 s
->init_pins
= sci_init_pins_irda
;
1202 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1203 if (port
->mapbase
== 0)
1204 port
->mapbase
= onchip_remap(SCIF_ADDR_SH5
, 1024, "SCIF");
1206 port
->membase
= (void __iomem
*)port
->mapbase
;
1210 static int sci_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1212 struct sci_port
*s
= &sci_ports
[port
->line
];
1214 if (ser
->irq
!= s
->irqs
[SCIx_TXI_IRQ
] || ser
->irq
> NR_IRQS
)
1216 if (ser
->baud_base
< 2400)
1217 /* No paper tape reader for Mitch.. */
1223 static struct uart_ops sci_uart_ops
= {
1224 .tx_empty
= sci_tx_empty
,
1225 .set_mctrl
= sci_set_mctrl
,
1226 .get_mctrl
= sci_get_mctrl
,
1227 .start_tx
= sci_start_tx
,
1228 .stop_tx
= sci_stop_tx
,
1229 .stop_rx
= sci_stop_rx
,
1230 .enable_ms
= sci_enable_ms
,
1231 .break_ctl
= sci_break_ctl
,
1232 .startup
= sci_startup
,
1233 .shutdown
= sci_shutdown
,
1234 .set_termios
= sci_set_termios
,
1236 .release_port
= sci_release_port
,
1237 .request_port
= sci_request_port
,
1238 .config_port
= sci_config_port
,
1239 .verify_port
= sci_verify_port
,
1242 static void __init
sci_init_ports(void)
1244 static int first
= 1;
1252 for (i
= 0; i
< SCI_NPORTS
; i
++) {
1253 sci_ports
[i
].port
.ops
= &sci_uart_ops
;
1254 sci_ports
[i
].port
.iotype
= UPIO_MEM
;
1255 sci_ports
[i
].port
.line
= i
;
1256 sci_ports
[i
].port
.fifosize
= 1;
1258 #if defined(__H8300H__) || defined(__H8300S__)
1260 sci_ports
[i
].enable
= h8300_sci_enable
;
1261 sci_ports
[i
].disable
= h8300_sci_disable
;
1263 sci_ports
[i
].port
.uartclk
= CONFIG_CPU_CLOCK
;
1264 #elif defined(CONFIG_SUPERH64)
1265 sci_ports
[i
].port
.uartclk
= current_cpu_data
.module_clock
* 16;
1268 * XXX: We should use a proper SCI/SCIF clock
1271 struct clk
*clk
= clk_get("module_clk");
1272 sci_ports
[i
].port
.uartclk
= clk_get_rate(clk
) * 16;
1277 sci_ports
[i
].break_timer
.data
= (unsigned long)&sci_ports
[i
];
1278 sci_ports
[i
].break_timer
.function
= sci_break_timer
;
1280 init_timer(&sci_ports
[i
].break_timer
);
1284 int __init
early_sci_setup(struct uart_port
*port
)
1286 if (unlikely(port
->line
> SCI_NPORTS
))
1291 sci_ports
[port
->line
].port
.membase
= port
->membase
;
1292 sci_ports
[port
->line
].port
.mapbase
= port
->mapbase
;
1293 sci_ports
[port
->line
].port
.type
= port
->type
;
1298 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1300 * Print a string to the serial port trying not to disturb
1301 * any possible real use of the port...
1303 static void serial_console_write(struct console
*co
, const char *s
,
1306 put_string(serial_console_port
, s
, count
);
1309 static int __init
serial_console_setup(struct console
*co
, char *options
)
1311 struct uart_port
*port
;
1319 * Check whether an invalid uart number has been specified, and
1320 * if so, search for the first available port that does have
1323 if (co
->index
>= SCI_NPORTS
)
1326 serial_console_port
= &sci_ports
[co
->index
];
1327 port
= &serial_console_port
->port
;
1330 * Also need to check port->type, we don't actually have any
1331 * UPIO_PORT ports, but uart_report_port() handily misreports
1332 * it anyways if we don't have a port available by the time this is
1337 if (!port
->membase
|| !port
->mapbase
)
1340 spin_lock_init(&port
->lock
);
1342 port
->type
= serial_console_port
->type
;
1344 if (port
->flags
& UPF_IOREMAP
)
1345 sci_config_port(port
, 0);
1347 if (serial_console_port
->enable
)
1348 serial_console_port
->enable(port
);
1351 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1353 ret
= uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1354 #if defined(__H8300H__) || defined(__H8300S__)
1355 /* disable rx interrupt */
1362 static struct console serial_console
= {
1364 .device
= uart_console_device
,
1365 .write
= serial_console_write
,
1366 .setup
= serial_console_setup
,
1367 .flags
= CON_PRINTBUFFER
,
1369 .data
= &sci_uart_driver
,
1372 static int __init
sci_console_init(void)
1375 register_console(&serial_console
);
1378 console_initcall(sci_console_init
);
1379 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1381 #ifdef CONFIG_SH_KGDB
1383 * FIXME: Most of this can go away.. at the moment, we rely on
1384 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1385 * most of that can easily be done here instead.
1387 * For the time being, just accept the values that were parsed earlier..
1389 static void __init
kgdb_console_get_options(struct uart_port
*port
, int *baud
,
1390 int *parity
, int *bits
)
1393 *parity
= tolower(kgdb_parity
);
1394 *bits
= kgdb_bits
- '0';
1398 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1399 * care of the early-on initialization for kgdb, regardless of whether we
1400 * actually use kgdb as a console or not.
1402 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1404 int __init
kgdb_console_setup(struct console
*co
, char *options
)
1406 struct uart_port
*port
= &sci_ports
[kgdb_portnum
].port
;
1412 spin_lock_init(&port
->lock
);
1414 if (co
->index
!= kgdb_portnum
)
1415 co
->index
= kgdb_portnum
;
1418 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1420 kgdb_console_get_options(port
, &baud
, &parity
, &bits
);
1422 kgdb_getchar
= kgdb_sci_getchar
;
1423 kgdb_putchar
= kgdb_sci_putchar
;
1425 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1427 #endif /* CONFIG_SH_KGDB */
1429 #ifdef CONFIG_SH_KGDB_CONSOLE
1430 static struct console kgdb_console
= {
1432 .write
= kgdb_console_write
,
1433 .setup
= kgdb_console_setup
,
1434 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1436 .data
= &sci_uart_driver
,
1439 /* Register the KGDB console so we get messages (d'oh!) */
1440 static int __init
kgdb_console_init(void)
1443 register_console(&kgdb_console
);
1446 console_initcall(kgdb_console_init
);
1447 #endif /* CONFIG_SH_KGDB_CONSOLE */
1449 #if defined(CONFIG_SH_KGDB_CONSOLE)
1450 #define SCI_CONSOLE &kgdb_console
1451 #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1452 #define SCI_CONSOLE &serial_console
1454 #define SCI_CONSOLE 0
1457 static char banner
[] __initdata
=
1458 KERN_INFO
"SuperH SCI(F) driver initialized\n";
1460 static struct uart_driver sci_uart_driver
= {
1461 .owner
= THIS_MODULE
,
1462 .driver_name
= "sci",
1463 .dev_name
= "ttySC",
1465 .minor
= SCI_MINOR_START
,
1467 .cons
= SCI_CONSOLE
,
1471 * Register a set of serial devices attached to a platform device. The
1472 * list is terminated with a zero flags entry, which means we expect
1473 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1474 * remapping (such as sh64) should also set UPF_IOREMAP.
1476 static int __devinit
sci_probe(struct platform_device
*dev
)
1478 struct plat_sci_port
*p
= dev
->dev
.platform_data
;
1481 for (i
= 0; p
&& p
->flags
!= 0 && i
< SCI_NPORTS
; p
++, i
++) {
1482 struct sci_port
*sciport
= &sci_ports
[i
];
1484 sciport
->port
.mapbase
= p
->mapbase
;
1487 * For the simple (and majority of) cases where we don't need
1488 * to do any remapping, just cast the cookie directly.
1490 if (p
->mapbase
&& !p
->membase
&& !(p
->flags
& UPF_IOREMAP
))
1491 p
->membase
= (void __iomem
*)p
->mapbase
;
1493 sciport
->port
.membase
= p
->membase
;
1495 sciport
->port
.irq
= p
->irqs
[SCIx_TXI_IRQ
];
1496 sciport
->port
.flags
= p
->flags
;
1497 sciport
->port
.dev
= &dev
->dev
;
1499 sciport
->type
= sciport
->port
.type
= p
->type
;
1501 memcpy(&sciport
->irqs
, &p
->irqs
, sizeof(p
->irqs
));
1503 uart_add_one_port(&sci_uart_driver
, &sciport
->port
);
1506 #ifdef CONFIG_CPU_FREQ
1507 cpufreq_register_notifier(&sci_nb
, CPUFREQ_TRANSITION_NOTIFIER
);
1508 dev_info(&dev
->dev
, "sci: CPU frequency notifier registered\n");
1511 #ifdef CONFIG_SH_STANDARD_BIOS
1512 sh_bios_gdb_detach();
1518 static int __devexit
sci_remove(struct platform_device
*dev
)
1522 for (i
= 0; i
< SCI_NPORTS
; i
++)
1523 uart_remove_one_port(&sci_uart_driver
, &sci_ports
[i
].port
);
1528 static int sci_suspend(struct platform_device
*dev
, pm_message_t state
)
1532 for (i
= 0; i
< SCI_NPORTS
; i
++) {
1533 struct sci_port
*p
= &sci_ports
[i
];
1535 if (p
->type
!= PORT_UNKNOWN
&& p
->port
.dev
== &dev
->dev
)
1536 uart_suspend_port(&sci_uart_driver
, &p
->port
);
1542 static int sci_resume(struct platform_device
*dev
)
1546 for (i
= 0; i
< SCI_NPORTS
; i
++) {
1547 struct sci_port
*p
= &sci_ports
[i
];
1549 if (p
->type
!= PORT_UNKNOWN
&& p
->port
.dev
== &dev
->dev
)
1550 uart_resume_port(&sci_uart_driver
, &p
->port
);
1556 static struct platform_driver sci_driver
= {
1558 .remove
= __devexit_p(sci_remove
),
1559 .suspend
= sci_suspend
,
1560 .resume
= sci_resume
,
1563 .owner
= THIS_MODULE
,
1567 static int __init
sci_init(void)
1575 ret
= uart_register_driver(&sci_uart_driver
);
1576 if (likely(ret
== 0)) {
1578 #if defined(CONFIG_SH_SECUREEDGE5410)
1581 init_timer(&sci_timer_struct
);
1582 sci_timer_struct
.function
= sci_timer
;
1583 sci_timer_struct
.data
= 0;
1584 sci_timer_struct
.expires
= jiffies
+ HZ
/25;
1585 add_timer(&sci_timer_struct
);
1587 s
= SECUREEDGE_READ_IOPORT();
1588 sci_dcdstatus
[0] = !(s
& 0x10);
1589 sci_dcdstatus
[1] = !(s
& 0x1);
1592 ret
= platform_driver_register(&sci_driver
);
1594 uart_unregister_driver(&sci_uart_driver
);
1600 static void __exit
sci_exit(void)
1602 #if defined(CONFIG_SH_SECUREEDGE5410)
1603 del_timer(&sci_timer_struct
);
1605 platform_driver_unregister(&sci_driver
);
1606 uart_unregister_driver(&sci_uart_driver
);
1609 module_init(sci_init
);
1610 module_exit(sci_exit
);
1612 MODULE_LICENSE("GPL");