1 /* $Id: sh-sci.c,v 1.40 2000/04/15 06:57:29 gniibe Exp $
3 * linux/drivers/char/sh-sci.c
5 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 1999, 2000 Niibe Yutaka
7 * Copyright (C) 2000 Sugioka Toshinobu
8 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
10 * TTY code is based on sx.c (Specialix SX driver) by:
12 * (C) 1998 R.E.Wolff@BitWizard.nl
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/signal.h>
20 #include <linux/sched.h>
21 #include <linux/timer.h>
22 #include <linux/interrupt.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
29 #include <linux/ptrace.h>
30 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #ifdef CONFIG_SERIAL_CONSOLE
36 #include <linux/console.h>
39 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <asm/bitops.h>
45 #include <linux/generic_serial.h>
47 #ifdef CONFIG_SH_STANDARD_BIOS
48 #include <asm/sh_bios.h>
53 #ifdef CONFIG_SERIAL_CONSOLE
54 static struct console sercons
;
55 static struct sci_port
* sercons_port
=0;
56 static int sercons_baud
;
59 /* Function prototypes */
60 static void sci_init_pins_sci(struct sci_port
* port
, unsigned int cflag
);
62 static void sci_init_pins_scif(struct sci_port
* port
, unsigned int cflag
);
64 static void sci_init_pins_irda(struct sci_port
* port
, unsigned int cflag
);
67 static void sci_disable_tx_interrupts(void *ptr
);
68 static void sci_enable_tx_interrupts(void *ptr
);
69 static void sci_disable_rx_interrupts(void *ptr
);
70 static void sci_enable_rx_interrupts(void *ptr
);
71 static int sci_get_CD(void *ptr
);
72 static void sci_shutdown_port(void *ptr
);
73 static int sci_set_real_termios(void *ptr
);
74 static int sci_chars_in_buffer(void *ptr
);
75 static int sci_request_irq(struct sci_port
*port
);
76 static void sci_free_irq(struct sci_port
*port
);
77 static int sci_init_drivers(void);
79 static struct tty_driver
*sci_driver
;
81 static struct sci_port sci_ports
[SCI_NPORTS
] = SCI_INIT
;
83 static int sci_debug
= 0;
86 MODULE_PARM(sci_debug
, "i");
89 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
91 #ifdef CONFIG_SERIAL_CONSOLE
92 static void put_char(struct sci_port
*port
, char c
)
95 unsigned short status
;
100 status
= sci_in(port
, SCxSR
);
101 while (!(status
& SCxSR_TDxE(port
)));
103 sci_out(port
, SCxTDR
, c
);
104 sci_in(port
, SCxSR
); /* Dummy read */
105 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
107 restore_flags(flags
);
111 #ifdef CONFIG_SH_STANDARD_BIOS
113 static void handle_error(struct sci_port
*port
)
114 { /* Clear error flags */
115 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
118 static int get_char(struct sci_port
*port
)
121 unsigned short status
;
126 status
= sci_in(port
, SCxSR
);
127 if (status
& SCxSR_ERRORS(port
)) {
131 } while (!(status
& SCxSR_RDxF(port
)));
132 c
= sci_in(port
, SCxRDR
);
133 sci_in(port
, SCxSR
); /* Dummy read */
134 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
135 restore_flags(flags
);
140 /* Taken from sh-stub.c of GDB 4.18 */
141 static const char hexchars
[] = "0123456789abcdef";
143 static __inline__
char highhex(int x
)
145 return hexchars
[(x
>> 4) & 0xf];
148 static __inline__
char lowhex(int x
)
150 return hexchars
[x
& 0xf];
156 * Send the packet in buffer. The host gets one chance to read it.
157 * This routine does not wait for a positive acknowledge.
160 #ifdef CONFIG_SERIAL_CONSOLE
161 static void put_string(struct sci_port
*port
, const char *buffer
, int count
)
164 const unsigned char *p
= buffer
;
165 #ifdef CONFIG_SH_STANDARD_BIOS
168 /* This call only does a trap the first time it is
169 * called, and so is safe to do here unconditionally
171 if (sh_bios_in_gdb_mode()) {
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
) != '+');
195 for (i
=0; i
<count
; i
++) {
197 put_char(port
, '\r');
198 put_char(port
, *p
++);
204 static struct real_driver sci_real_driver
= {
205 sci_disable_tx_interrupts
,
206 sci_enable_tx_interrupts
,
207 sci_disable_rx_interrupts
,
208 sci_enable_rx_interrupts
,
211 sci_set_real_termios
,
216 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
217 static void sci_init_pins_sci(struct sci_port
* port
, unsigned int cflag
)
222 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
224 /* For SH7707, SH7709, SH7709A, SH7729 */
225 static void sci_init_pins_scif(struct sci_port
* port
, unsigned int cflag
)
227 unsigned int fcr_val
= 0;
232 /* We need to set SCPCR to enable RTS/CTS */
233 data
= ctrl_inw(SCPCR
);
234 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
235 ctrl_outw(data
&0x0fcf, SCPCR
);
238 fcr_val
|= SCFCR_MCE
;
242 /* We need to set SCPCR to enable RTS/CTS */
243 data
= ctrl_inw(SCPCR
);
244 /* Clear out SCP7MD1,0, SCP4MD1,0,
245 Set SCP6MD1,0 = {01} (output) */
246 ctrl_outw((data
&0x0fcf)|0x1000, SCPCR
);
248 data
= ctrl_inb(SCPDR
);
249 /* Set /RTS2 (bit6) = 0 */
250 ctrl_outb(data
&0xbf, SCPDR
);
252 sci_out(port
, SCFCR
, fcr_val
);
255 static void sci_init_pins_irda(struct sci_port
* port
, unsigned int cflag
)
257 unsigned int fcr_val
= 0;
260 fcr_val
|= SCFCR_MCE
;
262 sci_out(port
, SCFCR
, fcr_val
);
268 static void sci_init_pins_scif(struct sci_port
* port
, unsigned int cflag
)
270 unsigned int fcr_val
= 0;
272 if (cflag
& CRTSCTS
) {
273 fcr_val
|= SCFCR_MCE
;
275 ctrl_outw(0x0080, SCSPTR2
); /* Set RTS = 1 */
277 sci_out(port
, SCFCR
, fcr_val
);
281 #endif /* SCIF_ONLY || SCI_AND_SCIF */
283 static void sci_setsignals(struct sci_port
*port
, int dtr
, int rts
)
285 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
286 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
287 /* If you have signals for DTR and DCD, please implement here. */
291 static int sci_getsignals(struct sci_port
*port
)
293 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
296 return TIOCM_DTR
|TIOCM_RTS
|TIOCM_DSR
;
298 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
299 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
300 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
301 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
302 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
303 ((i_stat & IP_RI) ?TIOCM_RNG:0)
307 static void sci_set_baud(struct sci_port
*port
, int baud
)
334 printk(KERN_INFO
"sci: unsupported baud rate: %d, using 115200 instead.\n", baud
);
341 sci_setsignals (port
, 1, -1);
343 sci_out(port
, SCSMR
, (sci_in(port
, SCSMR
) & ~3) | 1);
346 sci_out(port
, SCSMR
, sci_in(port
, SCSMR
) & ~3);
348 sci_out(port
, SCBRR
, t
);
349 udelay((1000000+(baud
-1)) / baud
); /* Wait one bit interval */
351 sci_setsignals (port
, 0, -1);
355 static void sci_set_termios_cflag(struct sci_port
*port
, int cflag
, int baud
)
358 unsigned int smr_val
;
361 status
= sci_in(port
, SCxSR
);
362 while (!(status
& SCxSR_TEND(port
)));
364 sci_out(port
, SCSCR
, 0x00); /* TE=0, RE=0, CKE1=0 */
366 if (port
->type
== PORT_SCIF
) {
367 sci_out(port
, SCFCR
, SCFCR_RFRST
| SCFCR_TFRST
);
370 smr_val
= sci_in(port
, SCSMR
) & 3;
371 if ((cflag
& CSIZE
) == CS7
)
379 sci_out(port
, SCSMR
, smr_val
);
380 sci_set_baud(port
, baud
);
382 port
->init_pins(port
, cflag
);
383 sci_out(port
, SCSCR
, SCSCR_INIT(port
));
386 static int sci_set_real_termios(void *ptr
)
388 struct sci_port
*port
= ptr
;
390 if (port
->old_cflag
!= port
->gs
.tty
->termios
->c_cflag
) {
391 port
->old_cflag
= port
->gs
.tty
->termios
->c_cflag
;
392 sci_set_termios_cflag(port
, port
->old_cflag
, port
->gs
.baud
);
393 sci_enable_rx_interrupts(port
);
396 /* Tell line discipline whether we will do input cooking */
397 if (I_OTHER(port
->gs
.tty
))
398 clear_bit(TTY_HW_COOK_IN
, &port
->gs
.tty
->flags
);
400 set_bit(TTY_HW_COOK_IN
, &port
->gs
.tty
->flags
);
402 /* Tell line discipline whether we will do output cooking.
403 * If OPOST is set and no other output flags are set then we can do output
404 * processing. Even if only *one* other flag in the O_OTHER group is set
405 * we do cooking in software.
407 if (O_OPOST(port
->gs
.tty
) && !O_OTHER(port
->gs
.tty
))
408 set_bit(TTY_HW_COOK_OUT
, &port
->gs
.tty
->flags
);
410 clear_bit(TTY_HW_COOK_OUT
, &port
->gs
.tty
->flags
);
415 /* ********************************************************************** *
416 * the interrupt related routines *
417 * ********************************************************************** */
420 * This routine is used by the interrupt handler to schedule
421 * processing in the software interrupt portion of the driver.
423 static inline void sci_sched_event(struct sci_port
*port
, int event
)
425 port
->event
|= 1 << event
;
426 schedule_work(&port
->tqueue
);
429 static void sci_transmit_chars(struct sci_port
*port
)
434 unsigned short status
;
438 status
= sci_in(port
, SCxSR
);
439 if (!(status
& SCxSR_TDxE(port
))) {
441 ctrl
= sci_in(port
, SCSCR
);
442 if (port
->gs
.xmit_cnt
== 0) {
443 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
444 port
->gs
.flags
&= ~GS_TX_INTEN
;
446 ctrl
|= SCI_CTRL_FLAGS_TIE
;
447 sci_out(port
, SCSCR
, ctrl
);
448 restore_flags(flags
);
453 count
= port
->gs
.xmit_cnt
;
454 if (port
->type
== PORT_SCIF
) {
455 txroom
= 16 - (sci_in(port
, SCFDR
)>>8);
457 txroom
= (sci_in(port
, SCxSR
) & SCI_TDRE
)?1:0;
462 /* Don't copy pas the end of the source buffer */
463 if (count
> SERIAL_XMIT_SIZE
- port
->gs
.xmit_tail
)
464 count
= SERIAL_XMIT_SIZE
- port
->gs
.xmit_tail
;
466 /* If for one reason or another, we can't copy more data, we're done! */
470 for (i
=0; i
<count
; i
++) {
471 c
= port
->gs
.xmit_buf
[port
->gs
.xmit_tail
+ i
];
472 sci_out(port
, SCxTDR
, c
);
474 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
476 port
->icount
.tx
+= count
;
478 /* Update the kernel buffer end */
479 port
->gs
.xmit_tail
= (port
->gs
.xmit_tail
+ count
) & (SERIAL_XMIT_SIZE
-1);
481 /* This one last. (this is essential)
482 It would allow others to start putting more data into the buffer! */
483 port
->gs
.xmit_cnt
-= count
;
486 if (port
->gs
.xmit_cnt
<= port
->gs
.wakeup_chars
)
487 sci_sched_event(port
, SCI_EVENT_WRITE_WAKEUP
);
490 ctrl
= sci_in(port
, SCSCR
);
491 if (port
->gs
.xmit_cnt
== 0) {
492 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
493 port
->gs
.flags
&= ~GS_TX_INTEN
;
495 if (port
->type
== PORT_SCIF
) {
496 sci_in(port
, SCxSR
); /* Dummy read */
497 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
499 ctrl
|= SCI_CTRL_FLAGS_TIE
;
501 sci_out(port
, SCSCR
, ctrl
);
502 restore_flags(flags
);
505 static inline void sci_receive_chars(struct sci_port
*port
)
508 struct tty_struct
*tty
;
510 unsigned short status
;
512 status
= sci_in(port
, SCxSR
);
513 if (!(status
& SCxSR_RDxF(port
)))
518 if (port
->type
== PORT_SCIF
) {
519 count
= sci_in(port
, SCFDR
)&0x001f;
521 count
= (sci_in(port
, SCxSR
)&SCxSR_RDxF(port
))?1:0;
524 /* Don't copy more bytes than there is room for in the buffer */
525 if (tty
->flip
.count
+ count
> TTY_FLIPBUF_SIZE
)
526 count
= TTY_FLIPBUF_SIZE
- tty
->flip
.count
;
528 /* If for one reason or another, we can't copy more data, we're done! */
532 if (port
->type
== PORT_SCI
) {
533 tty
->flip
.char_buf_ptr
[0] = sci_in(port
, SCxRDR
);
534 tty
->flip
.flag_buf_ptr
[0] = TTY_NORMAL
;
536 for (i
=0; i
<count
; i
++) {
537 tty
->flip
.char_buf_ptr
[i
] = sci_in(port
, SCxRDR
);
538 status
= sci_in(port
, SCxSR
);
539 if (status
&SCxSR_FER(port
)) {
540 tty
->flip
.flag_buf_ptr
[i
] = TTY_FRAME
;
541 dprintk("sci: frame error\n");
542 } else if (status
&SCxSR_PER(port
)) {
543 tty
->flip
.flag_buf_ptr
[i
] = TTY_PARITY
;
544 dprintk("sci: parity error\n");
546 tty
->flip
.flag_buf_ptr
[i
] = TTY_NORMAL
;
551 sci_in(port
, SCxSR
); /* dummy read */
552 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
554 /* Update the kernel buffer end */
555 tty
->flip
.count
+= count
;
556 tty
->flip
.char_buf_ptr
+= count
;
557 tty
->flip
.flag_buf_ptr
+= count
;
560 port
->icount
.rx
+= count
;
564 /* Tell the rest of the system the news. New characters! */
565 tty_flip_buffer_push(tty
);
568 static inline int sci_handle_errors(struct sci_port
*port
)
571 unsigned short status
= sci_in(port
, SCxSR
);
572 struct tty_struct
*tty
= port
->gs
.tty
;
574 if (status
&SCxSR_ORER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
577 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
578 dprintk("sci: overrun error\n");
581 if (status
&SCxSR_FER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
582 if (sci_rxd_in(port
) == 0) {
583 /* Notify of BREAK */
585 *tty
->flip
.flag_buf_ptr
++ = TTY_BREAK
;
586 dprintk("sci: BREAK detected\n");
591 *tty
->flip
.flag_buf_ptr
++ = TTY_FRAME
;
592 dprintk("sci: frame error\n");
596 if (status
&SCxSR_PER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
599 *tty
->flip
.flag_buf_ptr
++ = TTY_PARITY
;
600 dprintk("sci: parity error\n");
604 tty
->flip
.count
+= copied
;
605 tty_flip_buffer_push(tty
);
611 static inline int sci_handle_breaks(struct sci_port
*port
)
614 unsigned short status
= sci_in(port
, SCxSR
);
615 struct tty_struct
*tty
= port
->gs
.tty
;
617 if (status
&SCxSR_BRK(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
618 /* Notify of BREAK */
620 *tty
->flip
.flag_buf_ptr
++ = TTY_BREAK
;
621 dprintk("sci: BREAK detected\n");
624 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1)
625 /* XXX: Handle SCIF overrun error */
626 if (port
->type
== PORT_SCIF
&& (sci_in(port
, SCLSR
) & SCIF_ORER
) != 0) {
627 sci_out(port
, SCLSR
, 0);
628 if(tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
630 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
631 dprintk("sci: overrun error\n");
637 tty
->flip
.count
+= copied
;
638 tty_flip_buffer_push(tty
);
644 static void sci_rx_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
646 struct sci_port
*port
= ptr
;
648 if (port
->gs
.flags
& GS_ACTIVE
)
649 if (!(port
->gs
.flags
& SCI_RX_THROTTLE
)) {
650 sci_receive_chars(port
);
653 sci_disable_rx_interrupts(port
);
656 static void sci_tx_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
658 struct sci_port
*port
= ptr
;
660 if (port
->gs
.flags
& GS_ACTIVE
)
661 sci_transmit_chars(port
);
663 sci_disable_tx_interrupts(port
);
667 static void sci_er_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
669 struct sci_port
*port
= ptr
;
672 if (port
->type
== PORT_SCI
) {
673 if(sci_handle_errors(port
)) {
674 /* discard character in rx buffer */
676 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
680 sci_rx_interrupt(irq
, ptr
, regs
);
682 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
684 /* Kick the transmission */
685 sci_tx_interrupt(irq
, ptr
, regs
);
688 static void sci_br_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
690 struct sci_port
*port
= ptr
;
693 sci_handle_breaks(port
);
694 sci_out(port
, SCxSR
, SCxSR_BREAK_CLEAR(port
));
697 static void do_softint(void *private_
)
699 struct sci_port
*port
= (struct sci_port
*) private_
;
700 struct tty_struct
*tty
;
706 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP
, &port
->event
)) {
707 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
708 tty
->ldisc
.write_wakeup
)
709 (tty
->ldisc
.write_wakeup
)(tty
);
710 wake_up_interruptible(&tty
->write_wait
);
714 /* ********************************************************************** *
715 * Here are the routines that actually *
716 * interface with the generic_serial driver *
717 * ********************************************************************** */
719 static void sci_disable_tx_interrupts(void *ptr
)
721 struct sci_port
*port
= ptr
;
725 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
727 ctrl
= sci_in(port
, SCSCR
);
728 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
729 sci_out(port
, SCSCR
, ctrl
);
730 restore_flags(flags
);
733 static void sci_enable_tx_interrupts(void *ptr
)
735 struct sci_port
*port
= ptr
;
737 disable_irq(port
->irqs
[SCIx_TXI_IRQ
]);
738 sci_transmit_chars(port
);
739 enable_irq(port
->irqs
[SCIx_TXI_IRQ
]);
742 static void sci_disable_rx_interrupts(void * ptr
)
744 struct sci_port
*port
= ptr
;
748 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
750 ctrl
= sci_in(port
, SCSCR
);
751 ctrl
&= ~SCI_CTRL_FLAGS_RIE
;
752 sci_out(port
, SCSCR
, ctrl
);
753 restore_flags(flags
);
756 static void sci_enable_rx_interrupts(void * ptr
)
758 struct sci_port
*port
= ptr
;
762 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
764 ctrl
= sci_in(port
, SCSCR
);
765 ctrl
|= SCI_CTRL_FLAGS_RIE
;
766 sci_out(port
, SCSCR
, ctrl
);
767 restore_flags(flags
);
770 static int sci_get_CD(void * ptr
)
772 /* If you have signal for CD (Carrier Detect), please change here. */
776 static int sci_chars_in_buffer(void * ptr
)
778 struct sci_port
*port
= ptr
;
780 if (port
->type
== PORT_SCIF
) {
781 return (sci_in(port
, SCFDR
) >> 8) + ((sci_in(port
, SCxSR
) & SCxSR_TEND(port
))? 0: 1);
783 return (sci_in(port
, SCxSR
) & SCxSR_TEND(port
))? 0: 1;
787 static void sci_shutdown_port(void * ptr
)
789 struct sci_port
*port
= ptr
;
791 port
->gs
.flags
&= ~ GS_ACTIVE
;
792 if (port
->gs
.tty
&& port
->gs
.tty
->termios
->c_cflag
& HUPCL
)
793 sci_setsignals(port
, 0, 0);
797 /* ********************************************************************** *
798 * Here are the routines that actually *
799 * interface with the rest of the system *
800 * ********************************************************************** */
802 static int sci_open(struct tty_struct
* tty
, struct file
* filp
)
804 struct sci_port
*port
;
809 if ((line
< 0) || (line
>= SCI_NPORTS
))
812 port
= &sci_ports
[line
];
814 tty
->driver_data
= port
;
819 INIT_WORK(&port
->tqueue
, do_softint
, port
);
822 * Start up serial port
824 retval
= gs_init_port(&port
->gs
);
829 port
->gs
.flags
|= GS_ACTIVE
;
830 sci_setsignals(port
, 1,1);
832 if (port
->gs
.count
== 1) {
833 retval
= sci_request_irq(port
);
836 retval
= gs_block_til_ready(port
, filp
);
842 #ifdef CONFIG_SERIAL_CONSOLE
843 if (sercons
.cflag
&& sercons
.index
== line
) {
844 tty
->termios
->c_cflag
= sercons
.cflag
;
845 port
->gs
.baud
= sercons_baud
;
847 sci_set_real_termios(port
);
851 sci_enable_rx_interrupts(port
);
862 static int sci_ioctl(struct tty_struct
* tty
, struct file
* filp
,
863 unsigned int cmd
, unsigned long arg
)
866 struct sci_port
*port
= tty
->driver_data
;
872 rc
= put_user(((tty
->termios
->c_cflag
& CLOCAL
) ? 1 : 0),
873 (unsigned int *) arg
);
876 if ((rc
= get_user(ival
, (unsigned int *) arg
)) == 0)
877 tty
->termios
->c_cflag
=
878 (tty
->termios
->c_cflag
& ~CLOCAL
) |
882 if ((rc
= verify_area(VERIFY_WRITE
, (void *) arg
,
883 sizeof(struct serial_struct
))) == 0)
884 rc
= gs_getserial(&port
->gs
, (struct serial_struct
*) arg
);
887 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
888 sizeof(struct serial_struct
))) == 0)
889 rc
= gs_setserial(&port
->gs
,
890 (struct serial_struct
*) arg
);
893 ival
= sci_getsignals(port
);
894 rc
= put_user(ival
, (unsigned int *) arg
);
897 if ((rc
= get_user(ival
, (unsigned int *) arg
)) == 0)
898 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 1 : -1),
899 ((ival
& TIOCM_RTS
) ? 1 : -1));
902 if ((rc
= get_user(ival
, (unsigned int *) arg
)) == 0)
903 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 0 : -1),
904 ((ival
& TIOCM_RTS
) ? 0 : -1));
907 if ((rc
= get_user(ival
, (unsigned int *)arg
)) == 0)
908 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 1 : 0),
909 ((ival
& TIOCM_RTS
) ? 1 : 0));
920 static void sci_throttle(struct tty_struct
* tty
)
922 struct sci_port
*port
= (struct sci_port
*)tty
->driver_data
;
924 /* If the port is using any type of input flow
925 * control then throttle the port.
927 if ((tty
->termios
->c_cflag
& CRTSCTS
) || (I_IXOFF(tty
)) )
928 port
->gs
.flags
|= SCI_RX_THROTTLE
;
931 static void sci_unthrottle(struct tty_struct
* tty
)
933 struct sci_port
*port
= (struct sci_port
*)tty
->driver_data
;
935 /* Always unthrottle even if flow control is not enabled on
936 * this port in case we disabled flow control while the port
939 port
->gs
.flags
&= ~SCI_RX_THROTTLE
;
943 #ifdef CONFIG_PROC_FS
944 static int sci_read_proc(char *page
, char **start
, off_t off
, int count
,
945 int *eof
, void *data
)
948 struct sci_port
*port
;
951 len
+= sprintf(page
, "sciinfo:0.1\n");
952 for (i
= 0; i
< SCI_NPORTS
&& len
< 4000; i
++) {
953 port
= &sci_ports
[i
];
954 len
+= sprintf(page
+len
, "%d: uart:%s address: %08x", i
,
955 (port
->type
== PORT_SCI
) ? "SCI" : "SCIF",
957 len
+= sprintf(page
+len
, " baud:%d", port
->gs
.baud
);
958 len
+= sprintf(page
+len
, " tx:%d rx:%d",
959 port
->icount
.tx
, port
->icount
.rx
);
961 if (port
->icount
.frame
)
962 len
+= sprintf(page
+len
, " fe:%d", port
->icount
.frame
);
963 if (port
->icount
.parity
)
964 len
+= sprintf(page
+len
, " pe:%d", port
->icount
.parity
);
965 if (port
->icount
.brk
)
966 len
+= sprintf(page
+len
, " brk:%d", port
->icount
.brk
);
967 if (port
->icount
.overrun
)
968 len
+= sprintf(page
+len
, " oe:%d", port
->icount
.overrun
);
969 len
+= sprintf(page
+len
, "\n");
975 static struct tty_operations sci_ops
= {
979 .put_char
= gs_put_char
,
980 .flush_chars
= gs_flush_chars
,
981 .write_room
= gs_write_room
,
982 .chars_in_buffer
= gs_chars_in_buffer
,
983 .flush_buffer
= gs_flush_buffer
,
985 .throttle
= sci_throttle
,
986 .unthrottle
= sci_unthrottle
,
987 .set_termios
= gs_set_termios
,
991 #ifdef CONFIG_PROC_FS
992 .read_proc
= sci_read_proc
,
996 /* ********************************************************************** *
997 * Here are the initialization routines. *
998 * ********************************************************************** */
1000 static int sci_init_drivers(void)
1003 struct sci_port
*port
;
1004 sci_driver
= alloc_tty_driver(SCI_NPORTS
);
1008 sci_driver
->owner
= THIS_MODULE
;
1009 sci_driver
->driver_name
= "sci";
1010 sci_driver
->name
= "ttySC";
1011 sci_driver
->devfs_name
= "ttsc/";
1012 sci_driver
->major
= SCI_MAJOR
;
1013 sci_driver
->minor_start
= SCI_MINOR_START
;
1014 sci_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1015 sci_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1016 sci_driver
->init_termios
= tty_std_termios
;
1017 sci_driver
->init_termios
.c_cflag
=
1018 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
| CRTSCTS
;
1019 sci_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1020 tty_set_operations(sci_driver
, &sci_ops
);
1021 if ((error
= tty_register_driver(sci_driver
))) {
1022 printk(KERN_ERR
"sci: Couldn't register SCI driver, error = %d\n",
1024 put_tty_driver(sci_driver
);
1028 for (port
= &sci_ports
[0]; port
< &sci_ports
[SCI_NPORTS
]; port
++) {
1029 port
->gs
.magic
= SCI_MAGIC
;
1030 port
->gs
.close_delay
= HZ
/2;
1031 port
->gs
.closing_wait
= 30 * HZ
;
1032 port
->gs
.rd
= &sci_real_driver
;
1033 init_waitqueue_head(&port
->gs
.open_wait
);
1034 init_waitqueue_head(&port
->gs
.close_wait
);
1035 port
->old_cflag
= 0;
1036 port
->icount
.cts
= port
->icount
.dsr
=
1037 port
->icount
.rng
= port
->icount
.dcd
= 0;
1038 port
->icount
.rx
= port
->icount
.tx
= 0;
1039 port
->icount
.frame
= port
->icount
.parity
= 0;
1040 port
->icount
.overrun
= port
->icount
.brk
= 0;
1046 static int sci_request_irq(struct sci_port
*port
)
1049 void (*handlers
[4])(int irq
, void *ptr
, struct pt_regs
*regs
) = {
1050 sci_er_interrupt
, sci_rx_interrupt
, sci_tx_interrupt
,
1054 for (i
=0; i
<4; i
++) {
1055 if (!port
->irqs
[i
]) continue;
1056 if (request_irq(port
->irqs
[i
], handlers
[i
], SA_INTERRUPT
,
1058 printk(KERN_ERR
"sci: Cannot allocate irq.\n");
1065 static void sci_free_irq(struct sci_port
*port
)
1069 for (i
=0; i
<4; i
++) {
1070 if (!port
->irqs
[i
]) continue;
1071 free_irq(port
->irqs
[i
], port
);
1075 static char banner
[] __initdata
=
1076 KERN_INFO
"SuperH SCI(F) driver initialized\n";
1078 int __init
sci_init(void)
1080 struct sci_port
*port
;
1083 printk("%s", banner
);
1085 for (j
=0; j
<SCI_NPORTS
; j
++) {
1086 port
= &sci_ports
[j
];
1087 printk(KERN_INFO
"ttySC%d at 0x%08x is a %s\n", j
, port
->base
,
1088 (port
->type
== PORT_SCI
) ? "SCI" : "SCIF");
1093 #ifdef CONFIG_SH_STANDARD_BIOS
1094 sh_bios_gdb_detach();
1096 return 0; /* Return -EIO when not detected */
1099 module_init(sci_init
);
1105 void cleanup_module(void)
1107 tty_unregister_driver(sci_driver
);
1108 put_tty_driver(sci_driver
);
1111 #include "generic_serial.c"
1114 #ifdef CONFIG_SERIAL_CONSOLE
1116 * Print a string to the serial port trying not to disturb
1117 * any possible real use of the port...
1119 static void serial_console_write(struct console
*co
, const char *s
,
1122 put_string(sercons_port
, s
, count
);
1125 static struct tty_driver
*serial_console_device(struct console
*c
, int *index
)
1132 * Setup initial baud/bits/parity. We do two things here:
1133 * - construct a cflag setting for the first rs_open()
1134 * - initialize the serial port
1135 * Return non-zero if we didn't find a serial port.
1137 static int __init
serial_console_setup(struct console
*co
, char *options
)
1142 int cflag
= CREAD
| HUPCL
| CLOCAL
;
1145 sercons_port
= &sci_ports
[co
->index
];
1148 baud
= simple_strtoul(options
, NULL
, 10);
1150 while(*s
>= '0' && *s
<= '9')
1152 if (*s
) parity
= *s
++;
1153 if (*s
) bits
= *s
- '0';
1157 * Now construct a cflag setting.
1197 sercons_baud
= baud
;
1199 sci_set_termios_cflag(sercons_port
, cflag
, baud
);
1200 sercons_port
->old_cflag
= cflag
;
1205 static struct console sercons
= {
1207 .write
= serial_console_write
,
1208 .device
= serial_console_device
,
1209 .setup
= serial_console_setup
,
1210 .flags
= CON_PRINTBUFFER
,
1218 #ifdef CONFIG_SH_EARLY_PRINTK
1219 extern void sh_console_unregister (void);
1222 static int __init
sci_console_init(void)
1224 register_console(&sercons
);
1225 #ifdef CONFIG_SH_EARLY_PRINTK
1226 /* Now that the real console is available, unregister the one we
1227 * used while first booting.
1229 sh_console_unregister();
1233 console_initcall(sci_console_init
);
1235 #endif /* CONFIG_SERIAL_CONSOLE */