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/malloc.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_DEBUG_KERNEL_WITH_GDB_STUB
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 void sci_hungup(void *ptr
);
75 static void sci_close(void *ptr
);
76 static int sci_chars_in_buffer(void *ptr
);
77 static int sci_init_drivers(void);
79 static struct tty_driver sci_driver
, sci_callout_driver
;
81 static struct sci_port sci_ports
[SCI_NPORTS
] = SCI_INIT
;
82 static struct tty_struct
*sci_table
[SCI_NPORTS
] = { NULL
, };
83 static struct termios
*sci_termios
[SCI_NPORTS
];
84 static struct termios
*sci_termios_locked
[SCI_NPORTS
];
90 MODULE_PARM(sci_debug
, "i");
93 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
95 static void put_char(struct sci_port
*port
, char c
)
98 unsigned short status
;
103 status
= sci_in(port
, SCxSR
);
104 while (!(status
& SCxSR_TDxE(port
)));
106 sci_out(port
, SCxTDR
, c
);
107 sci_in(port
, SCxSR
); /* Dummy read */
108 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
110 restore_flags(flags
);
113 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
115 static void handle_error(struct sci_port
*port
)
116 { /* Clear error flags */
117 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
120 static int get_char(struct sci_port
*port
)
123 unsigned short status
;
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 restore_flags(flags
);
142 /* Taken from sh-stub.c of GDB 4.18 */
143 static const char hexchars
[] = "0123456789abcdef";
145 static __inline__
char highhex(int x
)
147 return hexchars
[(x
>> 4) & 0xf];
150 static __inline__
char lowhex(int x
)
152 return hexchars
[x
& 0xf];
158 * Send the packet in buffer. The host gets one chance to read it.
159 * This routine does not wait for a positive acknowledge.
162 static void put_string(struct sci_port
*port
,
163 const char *buffer
, int count
)
166 const unsigned char *p
= buffer
;
167 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
170 /* This call only does a trap the first time it is
171 * called, and so is safe to do here unconditionally
173 if (sh_bios_in_gdb_mode()) {
174 /* $<packet info>#<checksum>. */
178 put_char(port
, 'O'); /* 'O'utput to console */
181 for (i
=0; i
<count
; i
++) { /* Don't use run length encoding */
192 put_char(port
, highhex(checksum
));
193 put_char(port
, lowhex(checksum
));
194 } while (get_char(port
) != '+');
197 for (i
=0; i
<count
; i
++) {
199 put_char(port
, '\r');
200 put_char(port
, *p
++);
206 static struct real_driver sci_real_driver
= {
207 sci_disable_tx_interrupts
,
208 sci_enable_tx_interrupts
,
209 sci_disable_rx_interrupts
,
210 sci_enable_rx_interrupts
,
213 sci_set_real_termios
,
220 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
221 static void sci_init_pins_sci(struct sci_port
* port
, unsigned int cflag
)
226 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
228 /* For SH7707, SH7709, SH7709A, SH7729 */
229 static void sci_init_pins_scif(struct sci_port
* port
, unsigned int cflag
)
231 unsigned int fcr_val
= 0;
236 /* We need to set SCPCR to enable RTS/CTS */
237 data
= ctrl_inw(SCPCR
);
238 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
239 ctrl_outw(data
&0x0fcf, SCPCR
);
242 fcr_val
|= SCFCR_MCE
;
246 /* We need to set SCPCR to enable RTS/CTS */
247 data
= ctrl_inw(SCPCR
);
248 /* Clear out SCP7MD1,0, SCP4MD1,0,
249 Set SCP6MD1,0 = {01} (output) */
250 ctrl_outw((data
&0x0fcf)|0x1000, SCPCR
);
252 data
= ctrl_inb(SCPDR
);
253 /* Set /RTS2 (bit6) = 0 */
254 ctrl_outb(data
&0xbf, SCPDR
);
256 sci_out(port
, SCFCR
, fcr_val
);
259 static void sci_init_pins_irda(struct sci_port
* port
, unsigned int cflag
)
261 unsigned int fcr_val
= 0;
264 fcr_val
|= SCFCR_MCE
;
266 sci_out(port
, SCFCR
, fcr_val
);
272 static void sci_init_pins_scif(struct sci_port
* port
, unsigned int cflag
)
274 unsigned int fcr_val
= 0;
276 if (cflag
& CRTSCTS
) {
277 fcr_val
|= SCFCR_MCE
;
279 ctrl_outw(0x0080, SCSPTR2
); /* Set RTS = 1 */
281 sci_out(port
, SCFCR
, fcr_val
);
285 #endif /* SCIF_ONLY || SCI_AND_SCIF */
287 static void sci_setsignals(struct sci_port
*port
, int dtr
, int rts
)
289 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
290 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
291 /* If you have signals for DTR and DCD, please implement here. */
295 static int sci_getsignals(struct sci_port
*port
)
297 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
300 return TIOCM_DTR
|TIOCM_RTS
|TIOCM_DSR
;
302 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
303 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
304 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
305 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
306 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
307 ((i_stat & IP_RI) ?TIOCM_RNG:0)
311 static void sci_set_baud(struct sci_port
*port
, int baud
)
338 printk(KERN_INFO
"sci: unsupported baud rate: %d, using 115200 instead.\n", baud
);
345 sci_setsignals (port
, 1, -1);
347 sci_out(port
, SCSMR
, (sci_in(port
, SCSMR
) & ~3) | 1);
350 sci_out(port
, SCSMR
, sci_in(port
, SCSMR
) & ~3);
352 sci_out(port
, SCBRR
, t
);
353 udelay((1000000+(baud
-1)) / baud
); /* Wait one bit interval */
355 sci_setsignals (port
, 0, -1);
359 static void sci_set_termios_cflag(struct sci_port
*port
, int cflag
, int baud
)
362 unsigned int smr_val
;
365 status
= sci_in(port
, SCxSR
);
366 while (!(status
& SCxSR_TEND(port
)));
368 sci_out(port
, SCSCR
, 0x00); /* TE=0, RE=0, CKE1=0 */
370 if (port
->type
== PORT_SCIF
) {
371 sci_out(port
, SCFCR
, SCFCR_RFRST
| SCFCR_TFRST
);
374 smr_val
= sci_in(port
, SCSMR
) & 3;
375 if ((cflag
& CSIZE
) == CS7
)
383 sci_out(port
, SCSMR
, smr_val
);
384 sci_set_baud(port
, baud
);
386 port
->init_pins(port
, cflag
);
387 sci_out(port
, SCSCR
, SCSCR_INIT(port
));
390 static int sci_set_real_termios(void *ptr
)
392 struct sci_port
*port
= ptr
;
394 if (port
->old_cflag
!= port
->gs
.tty
->termios
->c_cflag
) {
395 port
->old_cflag
= port
->gs
.tty
->termios
->c_cflag
;
396 sci_set_termios_cflag(port
, port
->old_cflag
, port
->gs
.baud
);
397 sci_enable_rx_interrupts(port
);
400 /* Tell line discipline whether we will do input cooking */
401 if (I_OTHER(port
->gs
.tty
))
402 clear_bit(TTY_HW_COOK_IN
, &port
->gs
.tty
->flags
);
404 set_bit(TTY_HW_COOK_IN
, &port
->gs
.tty
->flags
);
406 /* Tell line discipline whether we will do output cooking.
407 * If OPOST is set and no other output flags are set then we can do output
408 * processing. Even if only *one* other flag in the O_OTHER group is set
409 * we do cooking in software.
411 if (O_OPOST(port
->gs
.tty
) && !O_OTHER(port
->gs
.tty
))
412 set_bit(TTY_HW_COOK_OUT
, &port
->gs
.tty
->flags
);
414 clear_bit(TTY_HW_COOK_OUT
, &port
->gs
.tty
->flags
);
419 /* ********************************************************************** *
420 * the interrupt related routines *
421 * ********************************************************************** */
424 * This routine is used by the interrupt handler to schedule
425 * processing in the software interrupt portion of the driver.
427 static inline void sci_sched_event(struct sci_port
*port
, int event
)
429 port
->event
|= 1 << event
;
430 queue_task(&port
->tqueue
, &tq_immediate
);
431 mark_bh(IMMEDIATE_BH
);
434 static void sci_transmit_chars(struct sci_port
*port
)
439 unsigned short status
;
443 status
= sci_in(port
, SCxSR
);
444 if (!(status
& SCxSR_TDxE(port
))) {
446 ctrl
= sci_in(port
, SCSCR
);
447 if (port
->gs
.xmit_cnt
== 0) {
448 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
449 port
->gs
.flags
&= ~GS_TX_INTEN
;
451 ctrl
|= SCI_CTRL_FLAGS_TIE
;
452 sci_out(port
, SCSCR
, ctrl
);
453 restore_flags(flags
);
458 count
= port
->gs
.xmit_cnt
;
459 if (port
->type
== PORT_SCIF
) {
460 txroom
= 16 - (sci_in(port
, SCFDR
)>>8);
462 txroom
= (sci_in(port
, SCxSR
) & SCI_TDRE
)?1:0;
467 /* Don't copy pas the end of the source buffer */
468 if (count
> SERIAL_XMIT_SIZE
- port
->gs
.xmit_tail
)
469 count
= SERIAL_XMIT_SIZE
- port
->gs
.xmit_tail
;
471 /* If for one reason or another, we can't copy more data, we're done! */
475 for (i
=0; i
<count
; i
++) {
476 c
= port
->gs
.xmit_buf
[port
->gs
.xmit_tail
+ i
];
477 sci_out(port
, SCxTDR
, c
);
479 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
481 port
->icount
.tx
+= count
;
483 /* Update the kernel buffer end */
484 port
->gs
.xmit_tail
= (port
->gs
.xmit_tail
+ count
) & (SERIAL_XMIT_SIZE
-1);
486 /* This one last. (this is essential)
487 It would allow others to start putting more data into the buffer! */
488 port
->gs
.xmit_cnt
-= count
;
491 if (port
->gs
.xmit_cnt
<= port
->gs
.wakeup_chars
)
492 sci_sched_event(port
, SCI_EVENT_WRITE_WAKEUP
);
495 ctrl
= sci_in(port
, SCSCR
);
496 if (port
->gs
.xmit_cnt
== 0) {
497 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
498 port
->gs
.flags
&= ~GS_TX_INTEN
;
500 if (port
->type
== PORT_SCIF
) {
501 sci_in(port
, SCxSR
); /* Dummy read */
502 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
504 ctrl
|= SCI_CTRL_FLAGS_TIE
;
506 sci_out(port
, SCSCR
, ctrl
);
507 restore_flags(flags
);
510 static inline void sci_receive_chars(struct sci_port
*port
)
513 struct tty_struct
*tty
;
515 unsigned short status
;
517 status
= sci_in(port
, SCxSR
);
518 if (!(status
& SCxSR_RDxF(port
)))
523 if (port
->type
== PORT_SCIF
) {
524 count
= sci_in(port
, SCFDR
)&0x001f;
526 count
= (sci_in(port
, SCxSR
)&SCxSR_RDxF(port
))?1:0;
529 /* Don't copy more bytes than there is room for in the buffer */
530 if (tty
->flip
.count
+ count
> TTY_FLIPBUF_SIZE
)
531 count
= TTY_FLIPBUF_SIZE
- tty
->flip
.count
;
533 /* If for one reason or another, we can't copy more data, we're done! */
537 if (port
->type
== PORT_SCI
) {
538 tty
->flip
.char_buf_ptr
[0] = sci_in(port
, SCxRDR
);
539 tty
->flip
.flag_buf_ptr
[0] = TTY_NORMAL
;
541 for (i
=0; i
<count
; i
++) {
542 tty
->flip
.char_buf_ptr
[i
] = sci_in(port
, SCxRDR
);
543 status
= sci_in(port
, SCxSR
);
544 if (status
&SCxSR_FER(port
)) {
545 tty
->flip
.flag_buf_ptr
[i
] = TTY_FRAME
;
546 dprintk("sci: frame error\n");
547 } else if (status
&SCxSR_PER(port
)) {
548 tty
->flip
.flag_buf_ptr
[i
] = TTY_PARITY
;
549 dprintk("sci: parity error\n");
551 tty
->flip
.flag_buf_ptr
[i
] = TTY_NORMAL
;
556 sci_in(port
, SCxSR
); /* dummy read */
557 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
559 /* Update the kernel buffer end */
560 tty
->flip
.count
+= count
;
561 tty
->flip
.char_buf_ptr
+= count
;
562 tty
->flip
.flag_buf_ptr
+= count
;
565 port
->icount
.rx
+= count
;
569 /* Tell the rest of the system the news. New characters! */
570 tty_flip_buffer_push(tty
);
573 static inline int sci_handle_errors(struct sci_port
*port
)
576 unsigned short status
= sci_in(port
, SCxSR
);
577 struct tty_struct
*tty
= port
->gs
.tty
;
579 if (status
&SCxSR_ORER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
582 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
583 dprintk("sci: overrun error\n");
586 if (status
&SCxSR_FER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
587 if (sci_rxd_in(port
) == 0) {
588 /* Notify of BREAK */
590 *tty
->flip
.flag_buf_ptr
++ = TTY_BREAK
;
591 dprintk("sci: BREAK detected\n");
596 *tty
->flip
.flag_buf_ptr
++ = TTY_FRAME
;
597 dprintk("sci: frame error\n");
601 if (status
&SCxSR_PER(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
604 *tty
->flip
.flag_buf_ptr
++ = TTY_PARITY
;
605 dprintk("sci: parity error\n");
609 tty
->flip
.count
+= copied
;
610 tty_flip_buffer_push(tty
);
616 static inline int sci_handle_breaks(struct sci_port
*port
)
619 unsigned short status
= sci_in(port
, SCxSR
);
620 struct tty_struct
*tty
= port
->gs
.tty
;
622 if (status
&SCxSR_BRK(port
) && tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
623 /* Notify of BREAK */
625 *tty
->flip
.flag_buf_ptr
++ = TTY_BREAK
;
626 dprintk("sci: BREAK detected\n");
629 #if defined(CONFIG_CPU_SUBTYPE_SH7750)
630 /* XXX: Handle SCIF overrun error */
631 if (port
->type
== PORT_SCIF
&& (ctrl_inw(SCLSR2
) & SCIF_ORER
) != 0) {
632 ctrl_outw(0, SCLSR2
);
633 if(tty
->flip
.count
<TTY_FLIPBUF_SIZE
) {
635 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
636 dprintk("sci: overrun error\n");
642 tty
->flip
.count
+= copied
;
643 tty_flip_buffer_push(tty
);
649 static void sci_rx_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
651 struct sci_port
*port
= ptr
;
653 if (port
->gs
.flags
& GS_ACTIVE
)
654 if (!(port
->gs
.flags
& SCI_RX_THROTTLE
)) {
655 sci_receive_chars(port
);
658 sci_disable_rx_interrupts(port
);
661 static void sci_tx_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
663 struct sci_port
*port
= ptr
;
665 if (port
->gs
.flags
& GS_ACTIVE
)
666 sci_transmit_chars(port
);
668 sci_disable_tx_interrupts(port
);
672 static void sci_er_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
674 struct sci_port
*port
= ptr
;
677 if (port
->type
== PORT_SCI
) {
678 if(sci_handle_errors(port
)) {
679 /* discard character in rx buffer */
681 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
685 sci_rx_interrupt(irq
, ptr
, regs
);
687 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
689 /* Kick the transmission */
690 sci_tx_interrupt(irq
, ptr
, regs
);
693 static void sci_br_interrupt(int irq
, void *ptr
, struct pt_regs
*regs
)
695 struct sci_port
*port
= ptr
;
698 sci_handle_breaks(port
);
699 sci_out(port
, SCxSR
, SCxSR_BREAK_CLEAR(port
));
702 static void do_softint(void *private_
)
704 struct sci_port
*port
= (struct sci_port
*) private_
;
705 struct tty_struct
*tty
;
711 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP
, &port
->event
)) {
712 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
713 tty
->ldisc
.write_wakeup
)
714 (tty
->ldisc
.write_wakeup
)(tty
);
715 wake_up_interruptible(&tty
->write_wait
);
719 /* ********************************************************************** *
720 * Here are the routines that actually *
721 * interface with the generic_serial driver *
722 * ********************************************************************** */
724 static void sci_disable_tx_interrupts(void *ptr
)
726 struct sci_port
*port
= ptr
;
730 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
732 ctrl
= sci_in(port
, SCSCR
);
733 ctrl
&= ~SCI_CTRL_FLAGS_TIE
;
734 sci_out(port
, SCSCR
, ctrl
);
735 restore_flags(flags
);
738 static void sci_enable_tx_interrupts(void *ptr
)
740 struct sci_port
*port
= ptr
;
742 disable_irq(port
->irqs
[SCIx_TXI_IRQ
]);
743 sci_transmit_chars(port
);
744 enable_irq(port
->irqs
[SCIx_TXI_IRQ
]);
747 static void sci_disable_rx_interrupts(void * ptr
)
749 struct sci_port
*port
= ptr
;
753 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
755 ctrl
= sci_in(port
, SCSCR
);
756 ctrl
&= ~SCI_CTRL_FLAGS_RIE
;
757 sci_out(port
, SCSCR
, ctrl
);
758 restore_flags(flags
);
761 static void sci_enable_rx_interrupts(void * ptr
)
763 struct sci_port
*port
= ptr
;
767 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
769 ctrl
= sci_in(port
, SCSCR
);
770 ctrl
|= SCI_CTRL_FLAGS_RIE
;
771 sci_out(port
, SCSCR
, ctrl
);
772 restore_flags(flags
);
775 static int sci_get_CD(void * ptr
)
777 /* If you have signal for CD (Carrier Detect), please change here. */
781 static int sci_chars_in_buffer(void * ptr
)
783 struct sci_port
*port
= ptr
;
785 if (port
->type
== PORT_SCIF
) {
786 return (sci_in(port
, SCFDR
) >> 8) + ((sci_in(port
, SCxSR
) & SCxSR_TEND(port
))? 0: 1);
788 return (sci_in(port
, SCxSR
) & SCxSR_TEND(port
))? 0: 1;
792 static void sci_shutdown_port(void * ptr
)
794 struct sci_port
*port
= ptr
;
796 port
->gs
.flags
&= ~ GS_ACTIVE
;
797 if (port
->gs
.tty
&& port
->gs
.tty
->termios
->c_cflag
& HUPCL
)
798 sci_setsignals(port
, 0, 0);
801 /* ********************************************************************** *
802 * Here are the routines that actually *
803 * interface with the rest of the system *
804 * ********************************************************************** */
806 static int sci_open(struct tty_struct
* tty
, struct file
* filp
)
808 struct sci_port
*port
;
811 line
= MINOR(tty
->device
) - SCI_MINOR_START
;
813 if ((line
< 0) || (line
>= SCI_NPORTS
))
816 port
= &sci_ports
[line
];
818 tty
->driver_data
= port
;
823 port
->tqueue
.routine
= do_softint
;
824 port
->tqueue
.data
= port
;
827 * Start up serial port
829 retval
= gs_init_port(&port
->gs
);
835 port
->gs
.flags
|= GS_ACTIVE
;
836 sci_setsignals(port
, 1,1);
838 if (port
->gs
.count
== 1) {
842 retval
= block_til_ready(port
, filp
);
850 if ((port
->gs
.count
== 1) && (port
->gs
.flags
& ASYNC_SPLIT_TERMIOS
)) {
851 if (tty
->driver
.subtype
== SERIAL_TYPE_NORMAL
)
852 *tty
->termios
= port
->gs
.normal_termios
;
854 *tty
->termios
= port
->gs
.callout_termios
;
855 sci_set_real_termios(port
);
858 #ifdef CONFIG_SERIAL_CONSOLE
859 if (sercons
.cflag
&& sercons
.index
== line
) {
860 tty
->termios
->c_cflag
= sercons
.cflag
;
861 port
->gs
.baud
= sercons_baud
;
863 sci_set_real_termios(port
);
867 sci_enable_rx_interrupts(port
);
869 port
->gs
.session
= current
->session
;
870 port
->gs
.pgrp
= current
->pgrp
;
875 static void sci_hungup(void *ptr
)
880 static void sci_close(void *ptr
)
885 static int sci_ioctl(struct tty_struct
* tty
, struct file
* filp
,
886 unsigned int cmd
, unsigned long arg
)
889 struct sci_port
*port
= tty
->driver_data
;
895 rc
= put_user(((tty
->termios
->c_cflag
& CLOCAL
) ? 1 : 0),
896 (unsigned int *) arg
);
899 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
900 sizeof(int))) == 0) {
901 get_user(ival
, (unsigned int *) arg
);
902 tty
->termios
->c_cflag
=
903 (tty
->termios
->c_cflag
& ~CLOCAL
) |
908 if ((rc
= verify_area(VERIFY_WRITE
, (void *) arg
,
909 sizeof(struct serial_struct
))) == 0)
910 gs_getserial(&port
->gs
, (struct serial_struct
*) arg
);
913 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
914 sizeof(struct serial_struct
))) == 0)
915 rc
= gs_setserial(&port
->gs
,
916 (struct serial_struct
*) arg
);
919 if ((rc
= verify_area(VERIFY_WRITE
, (void *) arg
,
920 sizeof(unsigned int))) == 0) {
921 ival
= sci_getsignals(port
);
922 put_user(ival
, (unsigned int *) arg
);
926 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
927 sizeof(unsigned int))) == 0) {
928 get_user(ival
, (unsigned int *) arg
);
929 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 1 : -1),
930 ((ival
& TIOCM_RTS
) ? 1 : -1));
934 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
935 sizeof(unsigned int))) == 0) {
936 get_user(ival
, (unsigned int *) arg
);
937 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 0 : -1),
938 ((ival
& TIOCM_RTS
) ? 0 : -1));
942 if ((rc
= verify_area(VERIFY_READ
, (void *) arg
,
943 sizeof(unsigned int))) == 0) {
944 get_user(ival
, (unsigned int *)arg
);
945 sci_setsignals(port
, ((ival
& TIOCM_DTR
) ? 1 : 0),
946 ((ival
& TIOCM_RTS
) ? 1 : 0));
958 static void sci_throttle(struct tty_struct
* tty
)
960 struct sci_port
*port
= (struct sci_port
*)tty
->driver_data
;
962 /* If the port is using any type of input flow
963 * control then throttle the port.
965 if ((tty
->termios
->c_cflag
& CRTSCTS
) || (I_IXOFF(tty
)) )
966 port
->gs
.flags
|= SCI_RX_THROTTLE
;
969 static void sci_unthrottle(struct tty_struct
* tty
)
971 struct sci_port
*port
= (struct sci_port
*)tty
->driver_data
;
973 /* Always unthrottle even if flow control is not enabled on
974 * this port in case we disabled flow control while the port
977 port
->gs
.flags
&= ~SCI_RX_THROTTLE
;
981 #ifdef CONFIG_PROC_FS
982 static int sci_read_proc(char *page
, char **start
, off_t off
, int count
,
983 int *eof
, void *data
)
986 struct sci_port
*port
;
989 len
+= sprintf(page
, "sciinfo:0.1\n");
990 for (i
= 0; i
< SCI_NPORTS
&& len
< 4000; i
++) {
991 port
= &sci_ports
[i
];
992 len
+= sprintf(page
+len
, "%d: uart:%s address: %08x", i
,
993 (port
->type
== PORT_SCI
) ? "SCI" : "SCIF",
995 len
+= sprintf(page
+len
, " baud:%d", port
->gs
.baud
);
996 len
+= sprintf(page
+len
, " tx:%d rx:%d",
997 port
->icount
.tx
, port
->icount
.rx
);
999 if (port
->icount
.frame
)
1000 len
+= sprintf(page
+len
, " fe:%d", port
->icount
.frame
);
1001 if (port
->icount
.parity
)
1002 len
+= sprintf(page
+len
, " pe:%d", port
->icount
.parity
);
1003 if (port
->icount
.brk
)
1004 len
+= sprintf(page
+len
, " brk:%d", port
->icount
.brk
);
1005 if (port
->icount
.overrun
)
1006 len
+= sprintf(page
+len
, " oe:%d", port
->icount
.overrun
);
1007 len
+= sprintf(page
+len
, "\n");
1013 /* ********************************************************************** *
1014 * Here are the initialization routines. *
1015 * ********************************************************************** */
1017 static int sci_init_drivers(void)
1020 struct sci_port
*port
;
1022 memset(&sci_driver
, 0, sizeof(sci_driver
));
1023 sci_driver
.magic
= TTY_DRIVER_MAGIC
;
1024 sci_driver
.driver_name
= "sci";
1025 sci_driver
.name
= "ttySC";
1026 sci_driver
.major
= SCI_MAJOR
;
1027 sci_driver
.minor_start
= SCI_MINOR_START
;
1028 sci_driver
.num
= SCI_NPORTS
;
1029 sci_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
1030 sci_driver
.subtype
= SERIAL_TYPE_NORMAL
;
1031 sci_driver
.init_termios
= tty_std_termios
;
1032 sci_driver
.init_termios
.c_cflag
=
1033 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
| CRTSCTS
;
1034 sci_driver
.flags
= TTY_DRIVER_REAL_RAW
;
1035 sci_driver
.refcount
= &sci_refcount
;
1036 sci_driver
.table
= sci_table
;
1037 sci_driver
.termios
= sci_termios
;
1038 sci_driver
.termios_locked
= sci_termios_locked
;
1040 sci_driver
.open
= sci_open
;
1041 sci_driver
.close
= gs_close
;
1042 sci_driver
.write
= gs_write
;
1043 sci_driver
.put_char
= gs_put_char
;
1044 sci_driver
.flush_chars
= gs_flush_chars
;
1045 sci_driver
.write_room
= gs_write_room
;
1046 sci_driver
.chars_in_buffer
= gs_chars_in_buffer
;
1047 sci_driver
.flush_buffer
= gs_flush_buffer
;
1048 sci_driver
.ioctl
= sci_ioctl
;
1049 sci_driver
.throttle
= sci_throttle
;
1050 sci_driver
.unthrottle
= sci_unthrottle
;
1051 sci_driver
.set_termios
= gs_set_termios
;
1052 sci_driver
.stop
= gs_stop
;
1053 sci_driver
.start
= gs_start
;
1054 sci_driver
.hangup
= gs_hangup
;
1055 #ifdef CONFIG_PROC_FS
1056 sci_driver
.read_proc
= sci_read_proc
;
1059 sci_callout_driver
= sci_driver
;
1060 sci_callout_driver
.name
= "cusc";
1061 sci_callout_driver
.major
= SCI_MAJOR
+1;
1062 sci_callout_driver
.subtype
= SERIAL_TYPE_CALLOUT
;
1063 sci_callout_driver
.read_proc
= NULL
;
1065 if ((error
= tty_register_driver(&sci_driver
))) {
1066 printk(KERN_ERR
"sci: Couldn't register SCI driver, error = %d\n",
1070 if ((error
= tty_register_driver(&sci_callout_driver
))) {
1071 tty_unregister_driver(&sci_driver
);
1072 printk(KERN_ERR
"sci: Couldn't register SCI callout driver, error = %d\n",
1077 for (port
= &sci_ports
[0]; port
< &sci_ports
[SCI_NPORTS
]; port
++) {
1078 port
->gs
.callout_termios
= sci_callout_driver
.init_termios
;
1079 port
->gs
.normal_termios
= sci_driver
.init_termios
;
1080 port
->gs
.magic
= SCI_MAGIC
;
1081 port
->gs
.close_delay
= HZ
/2;
1082 port
->gs
.closing_wait
= 30 * HZ
;
1083 port
->gs
.rd
= &sci_real_driver
;
1084 init_waitqueue_head(&port
->gs
.open_wait
);
1085 init_waitqueue_head(&port
->gs
.close_wait
);
1086 port
->old_cflag
= 0;
1087 port
->icount
.cts
= port
->icount
.dsr
=
1088 port
->icount
.rng
= port
->icount
.dcd
= 0;
1089 port
->icount
.rx
= port
->icount
.tx
= 0;
1090 port
->icount
.frame
= port
->icount
.parity
= 0;
1091 port
->icount
.overrun
= port
->icount
.brk
= 0;
1097 int __init
sci_init(void)
1099 struct sci_port
*port
;
1101 void (*handlers
[4])(int irq
, void *ptr
, struct pt_regs
*regs
) = {
1102 sci_er_interrupt
, sci_rx_interrupt
, sci_tx_interrupt
,
1106 printk("SuperH SCI(F) driver initialized\n");
1108 for (j
=0; j
<SCI_NPORTS
; j
++) {
1109 port
= &sci_ports
[j
];
1110 printk("ttySC%d at 0x%08x is a %s\n", j
, port
->base
,
1111 (port
->type
== PORT_SCI
) ? "SCI" : "SCIF");
1112 for (i
=0; i
<4; i
++) {
1113 if (!port
->irqs
[i
]) continue;
1114 if (request_irq(port
->irqs
[i
], handlers
[i
], SA_INTERRUPT
,
1116 printk(KERN_ERR
"sci: Cannot allocate irq.\n");
1124 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
1125 sh_bios_gdb_detach();
1127 return 0; /* Return -EIO when not detected */
1130 module_init(sci_init
);
1136 void cleanup_module(void)
1140 for (i
=SCI_ERI_IRQ
; i
<SCI_TEI_IRQ
; i
++) /* XXX: irq_end?? */
1143 tty_unregister_driver(&sci_driver
);
1144 tty_unregister_driver(&sci_callout_driver
);
1147 #include "generic_serial.c"
1150 #ifdef CONFIG_SERIAL_CONSOLE
1152 * Print a string to the serial port trying not to disturb
1153 * any possible real use of the port...
1155 static void serial_console_write(struct console
*co
, const char *s
,
1158 put_string(sercons_port
, s
, count
);
1162 * Receive character from the serial port
1164 static int serial_console_wait_key(struct console
*co
)
1166 /* Not implemented yet */
1170 static kdev_t
serial_console_device(struct console
*c
)
1172 return MKDEV(SCI_MAJOR
, SCI_MINOR_START
+ c
->index
);
1176 * Setup initial baud/bits/parity. We do two things here:
1177 * - construct a cflag setting for the first rs_open()
1178 * - initialize the serial port
1179 * Return non-zero if we didn't find a serial port.
1181 static int __init
serial_console_setup(struct console
*co
, char *options
)
1186 int cflag
= CREAD
| HUPCL
| CLOCAL
;
1189 sercons_port
= &sci_ports
[co
->index
];
1192 baud
= simple_strtoul(options
, NULL
, 10);
1194 while(*s
>= '0' && *s
<= '9')
1196 if (*s
) parity
= *s
++;
1197 if (*s
) bits
= *s
- '0';
1201 * Now construct a cflag setting.
1241 sercons_baud
= baud
;
1243 sci_set_termios_cflag(sercons_port
, cflag
, baud
);
1244 sercons_port
->old_cflag
= cflag
;
1249 static struct console sercons
= {
1251 write
: serial_console_write
,
1252 device
: serial_console_device
,
1253 wait_key
: serial_console_wait_key
,
1254 setup
: serial_console_setup
,
1255 flags
: CON_PRINTBUFFER
,
1263 #ifdef CONFIG_SH_EARLY_PRINTK
1264 extern void sh_console_unregister (void);
1267 void __init
sci_console_init(void)
1269 register_console(&sercons
);
1270 #ifdef CONFIG_SH_EARLY_PRINTK
1271 /* Now that the real console is available, unregister the one we
1272 * used while first booting.
1274 sh_console_unregister();
1277 #endif /* CONFIG_SERIAL_CONSOLE */