2 * decserial.c: Serial port driver for IOASIC DECstations.
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000,2001 Maciej W. Rozycki <macro@ds2.pg.gda.pl>
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
16 * Note: for IOASIC systems the wiring is as follows:
19 * DIN-7 MJ-4 signal SCC
24 * DB-25 MMJ-6 signal SCC
31 * 12 DSRS(DCE) -> ~A.CTS (*)
36 * 23 DSRS(DTE) <- ~B.RTS
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
42 #include <linux/config.h>
43 #include <linux/errno.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
54 #include <linux/kernel.h>
55 #include <linux/delay.h>
56 #include <linux/init.h>
57 #include <linux/ioport.h>
58 #ifdef CONFIG_SERIAL_CONSOLE
59 #include <linux/console.h>
63 #include <asm/pgtable.h>
65 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
68 #include <asm/wbflush.h>
69 #include <asm/bootinfo.h>
70 #ifdef CONFIG_DECSTATION
71 #include <asm/dec/interrupts.h>
72 #include <asm/dec/machtype.h>
73 #include <asm/dec/tc.h>
74 #include <asm/dec/ioasic_addrs.h>
76 #ifdef CONFIG_BAGET_MIPS
77 #include <asm/baget/baget.h>
78 unsigned long system_base
;
83 #ifdef CONFIG_MAGIC_SYSRQ
84 #include <linux/sysrq.h>
90 * It would be nice to dynamically allocate everything that
91 * depends on NUM_SERIAL, so we could support any number of
92 * Z8530s, but for now...
94 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
95 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
96 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
97 /* Number of channel A in the chip */
98 #define ZS_CHAN_IO_SIZE 8
99 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
101 #define RECOVERY_DELAY udelay(2)
106 int channel_a_offset
;
107 int channel_b_offset
;
112 static struct zs_parms
*zs_parms
;
114 #ifdef CONFIG_DECSTATION
115 static struct zs_parms ds_parms
= {
118 channel_a_offset
: 1,
119 channel_b_offset
: 9,
124 #ifdef CONFIG_BAGET_MIPS
125 static struct zs_parms baget_parms
= {
128 channel_a_offset
: 9,
129 channel_b_offset
: 1,
135 #ifdef CONFIG_DECSTATION
136 #define DS_BUS_PRESENT (IOASIC)
138 #define DS_BUS_PRESENT 0
141 #ifdef CONFIG_BAGET_MIPS
142 #define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
144 #define BAGET_BUS_PRESENT 0
147 #define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
149 struct dec_zschannel zs_channels
[NUM_CHANNELS
];
150 struct dec_serial zs_soft
[NUM_CHANNELS
];
151 int zs_channels_found
;
152 struct dec_serial
*zs_chain
; /* list of all channels */
154 struct tty_struct zs_ttys
[NUM_CHANNELS
];
156 #ifdef CONFIG_SERIAL_CONSOLE
157 static struct console sercons
;
159 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
161 static unsigned long break_pressed
; /* break, really ... */
164 static unsigned char zs_init_regs
[16] __initdata
= {
169 (X16CLK
| SB1
), /* write 4 */
171 0, 0, 0, /* write 6, 7, 8 */
173 (NRZ
), /* write 10 */
174 (TCBR
| RCBR
), /* write 11 */
175 0, 0, /* BRG time constant, write 12 + 13 */
176 (BRSRC
| BRENABL
), /* write 14 */
180 DECLARE_TASK_QUEUE(tq_zs_serial
);
182 static struct tty_driver
*serial_driver
;
184 /* serial subtype definitions */
185 #define SERIAL_TYPE_NORMAL 1
187 /* number of characters left in xmit buffer before we ask for more */
188 #define WAKEUP_CHARS 256
193 #undef SERIAL_DEBUG_INTR
194 #undef SERIAL_DEBUG_OPEN
195 #undef SERIAL_DEBUG_FLOW
196 #undef SERIAL_DEBUG_THROTTLE
197 #undef SERIAL_PARANOIA_CHECK
201 #ifdef SERIAL_DEBUG_THROTTLE
202 #define _tty_name(tty,buf) tty_name(tty,buf)
205 #define RS_STROBE_TIME 10
206 #define RS_ISR_PASS_LIMIT 256
208 #define _INLINE_ inline
210 static void probe_sccs(void);
211 static void change_speed(struct dec_serial
*info
);
212 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
215 #define MIN(a,b) ((a) < (b) ? (a) : (b))
219 * tmp_buf is used as a temporary buffer by serial_write. We need to
220 * lock it in case the copy_from_user blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
227 static unsigned char tmp_buf
[4096]; /* This is cheating */
228 static DECLARE_MUTEX(tmp_buf_sem
);
230 static inline int serial_paranoia_check(struct dec_serial
*info
,
231 char *name
, const char *routine
)
233 #ifdef SERIAL_PARANOIA_CHECK
234 static const char *badmagic
=
235 "Warning: bad magic number for serial struct %s in %s\n";
236 static const char *badinfo
=
237 "Warning: null mac_serial for %s in %s\n";
240 printk(badinfo
, name
, routine
);
243 if (info
->magic
!= SERIAL_MAGIC
) {
244 printk(badmagic
, name
, routine
);
252 * This is used to figure out the divisor speeds and the timeouts
254 static int baud_table
[] = {
255 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
256 9600, 19200, 38400, 57600, 115200, 0 };
259 * Reading and writing Z8530 registers.
261 static inline unsigned char read_zsreg(struct dec_zschannel
*channel
,
264 unsigned char retval
;
267 *channel
->control
= reg
& 0xf;
268 wbflush(); RECOVERY_DELAY
;
270 retval
= *channel
->control
;
275 static inline void write_zsreg(struct dec_zschannel
*channel
,
276 unsigned char reg
, unsigned char value
)
279 *channel
->control
= reg
& 0xf;
280 wbflush(); RECOVERY_DELAY
;
282 *channel
->control
= value
;
283 wbflush(); RECOVERY_DELAY
;
287 static inline unsigned char read_zsdata(struct dec_zschannel
*channel
)
289 unsigned char retval
;
291 retval
= *channel
->data
;
296 static inline void write_zsdata(struct dec_zschannel
*channel
,
299 *channel
->data
= value
;
300 wbflush(); RECOVERY_DELAY
;
304 static inline void load_zsregs(struct dec_zschannel
*channel
,
307 /* ZS_CLEARERR(channel);
308 ZS_CLEARFIFO(channel); */
310 write_zsreg(channel
, R4
, regs
[R4
]);
311 write_zsreg(channel
, R3
, regs
[R3
] & ~RxENABLE
);
312 write_zsreg(channel
, R5
, regs
[R5
] & ~TxENAB
);
313 write_zsreg(channel
, R9
, regs
[R9
]);
314 write_zsreg(channel
, R1
, regs
[R1
]);
315 write_zsreg(channel
, R2
, regs
[R2
]);
316 write_zsreg(channel
, R10
, regs
[R10
]);
317 write_zsreg(channel
, R11
, regs
[R11
]);
318 write_zsreg(channel
, R12
, regs
[R12
]);
319 write_zsreg(channel
, R13
, regs
[R13
]);
320 write_zsreg(channel
, R14
, regs
[R14
]);
321 write_zsreg(channel
, R15
, regs
[R15
]);
322 write_zsreg(channel
, R3
, regs
[R3
]);
323 write_zsreg(channel
, R5
, regs
[R5
]);
327 /* Sets or clears DTR/RTS on the requested line */
328 static inline void zs_rtsdtr(struct dec_serial
*info
, int which
, int set
)
333 save_flags(flags
); cli();
334 if (info
->zs_channel
!= info
->zs_chan_a
) {
336 info
->zs_chan_a
->curregs
[5] |= (which
& (RTS
| DTR
));
338 info
->zs_chan_a
->curregs
[5] &= ~(which
& (RTS
| DTR
));
340 write_zsreg(info
->zs_chan_a
, 5, info
->zs_chan_a
->curregs
[5]);
342 restore_flags(flags
);
345 /* Utility routines for the Zilog */
346 static inline int get_zsbaud(struct dec_serial
*ss
)
348 struct dec_zschannel
*channel
= ss
->zs_channel
;
351 /* The baud rate is split up between two 8-bit registers in
352 * what is termed 'BRG time constant' format in my docs for
353 * the chip, it is a function of the clk rate the chip is
354 * receiving which happens to be constant.
356 brg
= (read_zsreg(channel
, 13) << 8);
357 brg
|= read_zsreg(channel
, 12);
358 return BRG_TO_BPS(brg
, (zs_parms
->clock
/(ss
->clk_divisor
)));
361 /* On receive, this clears errors and the receiver interrupts */
362 static inline void rs_recv_clear(struct dec_zschannel
*zsc
)
364 write_zsreg(zsc
, 0, ERR_RES
);
365 write_zsreg(zsc
, 0, RES_H_IUS
); /* XXX this is unnecessary */
369 * ----------------------------------------------------------------------
371 * Here starts the interrupt handling routines. All of the following
372 * subroutines are declared as inline and are folded into
373 * rs_interrupt(). They were separated out for readability's sake.
375 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
376 * -----------------------------------------------------------------------
379 static int tty_break
; /* Set whenever BREAK condition is detected. */
382 * This routine is used by the interrupt handler to schedule
383 * processing in the software interrupt portion of the driver.
385 static _INLINE_
void rs_sched_event(struct dec_serial
*info
,
388 info
->event
|= 1 << event
;
389 queue_task(&info
->tqueue
, &tq_zs_serial
);
393 static _INLINE_
void receive_chars(struct dec_serial
*info
,
394 struct pt_regs
*regs
)
396 struct tty_struct
*tty
= info
->tty
;
397 unsigned char ch
, stat
, flag
;
399 while ((read_zsreg(info
->zs_channel
, R0
) & Rx_CH_AV
) != 0) {
401 stat
= read_zsreg(info
->zs_channel
, R1
);
402 ch
= read_zsdata(info
->zs_channel
);
404 if (!tty
&& !info
->hook
&& !info
->hook
->rx_char
)
409 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
410 if (info
->line
== sercons
.index
) {
411 if (!break_pressed
) {
412 break_pressed
= jiffies
;
419 if (info
->flags
& ZILOG_SAK
)
424 } else if (stat
& FRM_ERR
) {
426 } else if (stat
& PAR_ERR
) {
431 /* reset the error indication */
432 write_zsreg(info
->zs_channel
, R0
, ERR_RES
);
435 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
436 if (break_pressed
&& info
->line
== sercons
.index
) {
438 time_before(jiffies
, break_pressed
+ HZ
*5)) {
439 handle_sysrq(ch
, regs
, NULL
);
447 if (info
->hook
&& info
->hook
->rx_char
) {
448 (*info
->hook
->rx_char
)(ch
, flag
);
452 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
453 static int flip_buf_ovf
;
459 static int flip_max_cnt
;
460 if (flip_max_cnt
< tty
->flip
.count
)
461 flip_max_cnt
= tty
->flip
.count
;
464 *tty
->flip
.flag_buf_ptr
++ = flag
;
465 *tty
->flip
.char_buf_ptr
++ = ch
;
469 tty_flip_buffer_push(tty
);
472 static void transmit_chars(struct dec_serial
*info
)
474 if ((read_zsreg(info
->zs_channel
, R0
) & Tx_BUF_EMP
) == 0)
480 write_zsdata(info
->zs_channel
, info
->x_char
);
486 if ((info
->xmit_cnt
<= 0) || (info
->tty
&& info
->tty
->stopped
)
487 || info
->tx_stopped
) {
488 write_zsreg(info
->zs_channel
, R0
, RES_Tx_P
);
492 write_zsdata(info
->zs_channel
, info
->xmit_buf
[info
->xmit_tail
++]);
493 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
497 if (info
->xmit_cnt
< WAKEUP_CHARS
)
498 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
501 static _INLINE_
void status_handle(struct dec_serial
*info
)
505 /* Get status from Read Register 0 */
506 stat
= read_zsreg(info
->zs_channel
, R0
);
508 if (stat
& BRK_ABRT
) {
509 #ifdef SERIAL_DEBUG_INTR
510 printk("handling break....");
515 if (info
->zs_channel
!= info
->zs_chan_a
) {
517 /* FIXEM: Check for DCD transitions */
518 if (((stat
^ info
->read_reg_zero
) & DCD
) != 0
519 && info
->tty
&& !C_CLOCAL(info
->tty
)) {
521 wake_up_interruptible(&info
->open_wait
);
523 tty_hangup(info
->tty
);
527 /* Check for CTS transitions */
528 if (info
->tty
&& C_CRTSCTS(info
->tty
)) {
529 if ((stat
& CTS
) != 0) {
530 if (info
->tx_stopped
) {
531 info
->tx_stopped
= 0;
532 if (!info
->tx_active
)
533 transmit_chars(info
);
536 info
->tx_stopped
= 1;
542 /* Clear status condition... */
543 write_zsreg(info
->zs_channel
, R0
, RES_EXT_INT
);
544 info
->read_reg_zero
= stat
;
548 * This is the serial driver's generic interrupt routine
550 void rs_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
552 struct dec_serial
*info
= (struct dec_serial
*) dev_id
;
553 unsigned char zs_intreg
;
556 /* NOTE: The read register 3, which holds the irq status,
557 * does so for both channels on each chip. Although
558 * the status value itself must be read from the A
559 * channel and is only valid when read from channel A.
560 * Yes... broken hardware...
562 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
564 if (info
->zs_chan_a
== info
->zs_channel
)
565 shift
= 3; /* Channel A */
567 shift
= 0; /* Channel B */
570 zs_intreg
= read_zsreg(info
->zs_chan_a
, R3
) >> shift
;
571 if ((zs_intreg
& CHAN_IRQMASK
) == 0)
574 if (zs_intreg
& CHBRxIP
) {
575 receive_chars(info
, regs
);
577 if (zs_intreg
& CHBTxIP
) {
578 transmit_chars(info
);
580 if (zs_intreg
& CHBEXT
) {
585 /* Why do we need this ? */
586 write_zsreg(info
->zs_channel
, 0, RES_H_IUS
);
590 void zs_dump (void) {
592 for (i
= 0; i
< zs_channels_found
; i
++) {
593 struct dec_zschannel
*ch
= &zs_channels
[i
];
594 if ((long)ch
->control
== UNI_IO_BASE
+UNI_SCC1A_CTRL
) {
595 for (j
= 0; j
< 15; j
++) {
596 printk("W%d = 0x%x\t",
597 j
, (int)ch
->curregs
[j
]);
599 for (j
= 0; j
< 15; j
++) {
600 printk("R%d = 0x%x\t",
601 j
, (int)read_zsreg(ch
,j
));
610 * -------------------------------------------------------------------
611 * Here ends the serial interrupt routines.
612 * -------------------------------------------------------------------
616 * ------------------------------------------------------------
617 * rs_stop() and rs_start()
619 * This routines are called before setting or resetting tty->stopped.
620 * ------------------------------------------------------------
622 static void rs_stop(struct tty_struct
*tty
)
624 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
627 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
631 save_flags(flags
); cli();
632 if (info
->zs_channel
->curregs
[5] & TxENAB
) {
633 info
->zs_channel
->curregs
[5] &= ~TxENAB
;
634 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
636 restore_flags(flags
);
640 static void rs_start(struct tty_struct
*tty
)
642 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
645 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
648 save_flags(flags
); cli();
650 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(info
->zs_channel
->curregs
[5] & TxENAB
)) {
651 info
->zs_channel
->curregs
[5] |= TxENAB
;
652 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
655 if (info
->xmit_cnt
&& info
->xmit_buf
&& !info
->tx_active
) {
656 transmit_chars(info
);
659 restore_flags(flags
);
663 * This routine is used to handle the "bottom half" processing for the
664 * serial driver, known also the "software interrupt" processing.
665 * This processing is done at the kernel interrupt level, after the
666 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
667 * is where time-consuming activities which can not be done in the
668 * interrupt driver proper are done; the interrupt driver schedules
669 * them using rs_sched_event(), and they get done here.
671 static void do_serial_bh(void)
673 run_task_queue(&tq_zs_serial
);
676 static void do_softint(void *private_
)
678 struct dec_serial
*info
= (struct dec_serial
*) private_
;
679 struct tty_struct
*tty
;
685 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
686 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
687 tty
->ldisc
.write_wakeup
)
688 (tty
->ldisc
.write_wakeup
)(tty
);
689 wake_up_interruptible(&tty
->write_wait
);
693 int zs_startup(struct dec_serial
* info
)
697 if (info
->flags
& ZILOG_INITIALIZED
)
700 if (!info
->xmit_buf
) {
701 info
->xmit_buf
= (unsigned char *) get_zeroed_page(GFP_KERNEL
);
706 save_flags(flags
); cli();
708 #ifdef SERIAL_DEBUG_OPEN
709 printk("starting up ttyS%02d (irq %d)...", info
->line
, info
->irq
);
713 * Clear the receive FIFO.
715 ZS_CLEARFIFO(info
->zs_channel
);
716 info
->xmit_fifo_size
= 1;
719 * Clear the interrupt registers.
721 write_zsreg(info
->zs_channel
, 0, ERR_RES
);
722 write_zsreg(info
->zs_channel
, 0, RES_H_IUS
);
725 * Turn on RTS and DTR.
727 zs_rtsdtr(info
, RTS
| DTR
, 1);
730 * Finally, enable sequencing and interrupts
732 info
->zs_channel
->curregs
[1] = (info
->zs_channel
->curregs
[1] & ~0x18) | (EXT_INT_ENAB
| INT_ALL_Rx
| TxINT_ENAB
);
733 info
->zs_channel
->curregs
[3] |= (RxENABLE
| Rx8
);
734 info
->zs_channel
->curregs
[5] |= (TxENAB
| Tx8
);
735 info
->zs_channel
->curregs
[15] |= (DCDIE
| CTSIE
| TxUIE
| BRKIE
);
736 info
->zs_channel
->curregs
[9] |= (VIS
| MIE
);
737 write_zsreg(info
->zs_channel
, 1, info
->zs_channel
->curregs
[1]);
738 write_zsreg(info
->zs_channel
, 3, info
->zs_channel
->curregs
[3]);
739 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
740 write_zsreg(info
->zs_channel
, 15, info
->zs_channel
->curregs
[15]);
741 write_zsreg(info
->zs_channel
, 9, info
->zs_channel
->curregs
[9]);
744 * And clear the interrupt registers again for luck.
746 write_zsreg(info
->zs_channel
, 0, ERR_RES
);
747 write_zsreg(info
->zs_channel
, 0, RES_H_IUS
);
750 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
751 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
754 * Set the speed of the serial port
758 /* Save the current value of RR0 */
759 info
->read_reg_zero
= read_zsreg(info
->zs_channel
, 0);
761 info
->flags
|= ZILOG_INITIALIZED
;
762 restore_flags(flags
);
767 * This routine will shutdown a serial port; interrupts are disabled, and
768 * DTR is dropped if the hangup on close termio flag is on.
770 static void shutdown(struct dec_serial
* info
)
774 if (!(info
->flags
& ZILOG_INITIALIZED
))
777 #ifdef SERIAL_DEBUG_OPEN
778 printk("Shutting down serial port %d (irq %d)....", info
->line
,
782 save_flags(flags
); cli(); /* Disable interrupts */
784 if (info
->xmit_buf
) {
785 free_page((unsigned long) info
->xmit_buf
);
789 info
->zs_channel
->curregs
[1] = 0;
790 write_zsreg(info
->zs_channel
, 1, info
->zs_channel
->curregs
[1]); /* no interrupts */
792 info
->zs_channel
->curregs
[3] &= ~RxENABLE
;
793 write_zsreg(info
->zs_channel
, 3, info
->zs_channel
->curregs
[3]);
795 info
->zs_channel
->curregs
[5] &= ~TxENAB
;
796 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
797 if (!info
->tty
|| C_HUPCL(info
->tty
)) {
798 zs_rtsdtr(info
, RTS
| DTR
, 0);
802 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
804 info
->flags
&= ~ZILOG_INITIALIZED
;
805 restore_flags(flags
);
809 * This routine is called to set the UART divisor registers to match
810 * the specified baud rate for a serial port.
812 static void change_speed(struct dec_serial
*info
)
820 if (!info
->tty
|| !info
->tty
->termios
)
822 cflag
= info
->tty
->termios
->c_cflag
;
826 cflag
= info
->hook
->cflags
;
832 if (i
< 1 || i
> 2) {
834 info
->tty
->termios
->c_cflag
&= ~CBAUDEX
;
836 info
->hook
->cflags
&= ~CBAUDEX
;
841 save_flags(flags
); cli();
842 info
->zs_baud
= baud_table
[i
];
843 info
->clk_divisor
= 16;
845 info
->zs_channel
->curregs
[4] = X16CLK
;
846 brg
= BPS_TO_BRG(info
->zs_baud
, zs_parms
->clock
/info
->clk_divisor
);
847 info
->zs_channel
->curregs
[12] = (brg
& 255);
848 info
->zs_channel
->curregs
[13] = ((brg
>> 8) & 255);
849 zs_rtsdtr(info
, DTR
, 1);
851 zs_rtsdtr(info
, RTS
| DTR
, 0);
855 /* byte size and parity */
856 info
->zs_channel
->curregs
[3] &= ~RxNBITS_MASK
;
857 info
->zs_channel
->curregs
[5] &= ~TxNBITS_MASK
;
858 switch (cflag
& CSIZE
) {
861 info
->zs_channel
->curregs
[3] |= Rx5
;
862 info
->zs_channel
->curregs
[5] |= Tx5
;
866 info
->zs_channel
->curregs
[3] |= Rx6
;
867 info
->zs_channel
->curregs
[5] |= Tx6
;
871 info
->zs_channel
->curregs
[3] |= Rx7
;
872 info
->zs_channel
->curregs
[5] |= Tx7
;
875 default: /* defaults to 8 bits */
877 info
->zs_channel
->curregs
[3] |= Rx8
;
878 info
->zs_channel
->curregs
[5] |= Tx8
;
882 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
) / info
->zs_baud
);
883 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
885 info
->zs_channel
->curregs
[4] &= ~(SB_MASK
| PAR_ENA
| PAR_EVEN
);
886 if (cflag
& CSTOPB
) {
887 info
->zs_channel
->curregs
[4] |= SB2
;
889 info
->zs_channel
->curregs
[4] |= SB1
;
891 if (cflag
& PARENB
) {
892 info
->zs_channel
->curregs
[4] |= PAR_ENA
;
894 if (!(cflag
& PARODD
)) {
895 info
->zs_channel
->curregs
[4] |= PAR_EVEN
;
898 if (!(cflag
& CLOCAL
)) {
899 if (!(info
->zs_channel
->curregs
[15] & DCDIE
))
900 info
->read_reg_zero
= read_zsreg(info
->zs_channel
, 0);
901 info
->zs_channel
->curregs
[15] |= DCDIE
;
903 info
->zs_channel
->curregs
[15] &= ~DCDIE
;
904 if (cflag
& CRTSCTS
) {
905 info
->zs_channel
->curregs
[15] |= CTSIE
;
906 if ((read_zsreg(info
->zs_channel
, 0) & CTS
) == 0)
907 info
->tx_stopped
= 1;
909 info
->zs_channel
->curregs
[15] &= ~CTSIE
;
910 info
->tx_stopped
= 0;
913 /* Load up the new values */
914 load_zsregs(info
->zs_channel
, info
->zs_channel
->curregs
);
916 restore_flags(flags
);
919 static void rs_flush_chars(struct tty_struct
*tty
)
921 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
924 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
927 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| info
->tx_stopped
||
931 /* Enable transmitter */
932 save_flags(flags
); cli();
933 transmit_chars(info
);
934 restore_flags(flags
);
937 static int rs_write(struct tty_struct
* tty
, int from_user
,
938 const unsigned char *buf
, int count
)
941 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
944 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
947 if (!tty
|| !info
->xmit_buf
)
953 c
= MIN(count
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
954 SERIAL_XMIT_SIZE
- info
->xmit_head
));
960 copy_from_user(tmp_buf
, buf
, c
);
961 c
= MIN(c
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
962 SERIAL_XMIT_SIZE
- info
->xmit_head
));
963 memcpy(info
->xmit_buf
+ info
->xmit_head
, tmp_buf
, c
);
966 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
967 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
969 restore_flags(flags
);
975 if (info
->xmit_cnt
&& !tty
->stopped
&& !info
->tx_stopped
977 transmit_chars(info
);
978 restore_flags(flags
);
982 static int rs_write_room(struct tty_struct
*tty
)
984 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
987 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
989 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
995 static int rs_chars_in_buffer(struct tty_struct
*tty
)
997 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
999 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
1001 return info
->xmit_cnt
;
1004 static void rs_flush_buffer(struct tty_struct
*tty
)
1006 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
1008 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
1011 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1013 wake_up_interruptible(&tty
->write_wait
);
1014 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1015 tty
->ldisc
.write_wakeup
)
1016 (tty
->ldisc
.write_wakeup
)(tty
);
1020 * ------------------------------------------------------------
1023 * This routine is called by the upper-layer tty layer to signal that
1024 * incoming characters should be throttled.
1025 * ------------------------------------------------------------
1027 static void rs_throttle(struct tty_struct
* tty
)
1029 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
1030 unsigned long flags
;
1032 #ifdef SERIAL_DEBUG_THROTTLE
1035 printk("throttle %s: %d....\n", _tty_name(tty
, buf
),
1036 tty
->ldisc
.chars_in_buffer(tty
));
1039 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
1043 save_flags(flags
); cli();
1044 info
->x_char
= STOP_CHAR(tty
);
1045 if (!info
->tx_active
)
1046 transmit_chars(info
);
1047 restore_flags(flags
);
1050 if (C_CRTSCTS(tty
)) {
1051 zs_rtsdtr(info
, RTS
, 0);
1055 static void rs_unthrottle(struct tty_struct
* tty
)
1057 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
1058 unsigned long flags
;
1060 #ifdef SERIAL_DEBUG_THROTTLE
1063 printk("unthrottle %s: %d....\n", _tty_name(tty
, buf
),
1064 tty
->ldisc
.chars_in_buffer(tty
));
1067 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
1071 save_flags(flags
); cli();
1075 info
->x_char
= START_CHAR(tty
);
1076 if (!info
->tx_active
)
1077 transmit_chars(info
);
1079 restore_flags(flags
);
1082 if (C_CRTSCTS(tty
)) {
1083 zs_rtsdtr(info
, RTS
, 1);
1088 * ------------------------------------------------------------
1089 * rs_ioctl() and friends
1090 * ------------------------------------------------------------
1093 static int get_serial_info(struct dec_serial
* info
,
1094 struct serial_struct
* retinfo
)
1096 struct serial_struct tmp
;
1100 memset(&tmp
, 0, sizeof(tmp
));
1101 tmp
.type
= info
->type
;
1102 tmp
.line
= info
->line
;
1103 tmp
.port
= info
->port
;
1104 tmp
.irq
= info
->irq
;
1105 tmp
.flags
= info
->flags
;
1106 tmp
.baud_base
= info
->baud_base
;
1107 tmp
.close_delay
= info
->close_delay
;
1108 tmp
.closing_wait
= info
->closing_wait
;
1109 tmp
.custom_divisor
= info
->custom_divisor
;
1110 return copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)) ? -EFAULT
: 0;
1113 static int set_serial_info(struct dec_serial
* info
,
1114 struct serial_struct
* new_info
)
1116 struct serial_struct new_serial
;
1117 struct dec_serial old_info
;
1122 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
1125 if (!capable(CAP_SYS_ADMIN
)) {
1126 if ((new_serial
.baud_base
!= info
->baud_base
) ||
1127 (new_serial
.type
!= info
->type
) ||
1128 (new_serial
.close_delay
!= info
->close_delay
) ||
1129 ((new_serial
.flags
& ~ZILOG_USR_MASK
) !=
1130 (info
->flags
& ~ZILOG_USR_MASK
)))
1132 info
->flags
= ((info
->flags
& ~ZILOG_USR_MASK
) |
1133 (new_serial
.flags
& ZILOG_USR_MASK
));
1134 info
->custom_divisor
= new_serial
.custom_divisor
;
1135 goto check_and_exit
;
1138 if (info
->count
> 1)
1142 * OK, past this point, all the error checking has been done.
1143 * At this point, we start making changes.....
1146 info
->baud_base
= new_serial
.baud_base
;
1147 info
->flags
= ((info
->flags
& ~ZILOG_FLAGS
) |
1148 (new_serial
.flags
& ZILOG_FLAGS
));
1149 info
->type
= new_serial
.type
;
1150 info
->close_delay
= new_serial
.close_delay
;
1151 info
->closing_wait
= new_serial
.closing_wait
;
1154 retval
= zs_startup(info
);
1159 * get_lsr_info - get line status register info
1161 * Purpose: Let user call ioctl() to get info when the UART physically
1162 * is emptied. On bus types like RS485, the transmitter must
1163 * release the bus after transmitting. This must be done when
1164 * the transmit shift register is empty, not be done when the
1165 * transmit holding register is empty. This functionality
1166 * allows an RS485 driver to be written in user space.
1168 static int get_lsr_info(struct dec_serial
* info
, unsigned int *value
)
1170 unsigned char status
;
1173 status
= read_zsreg(info
->zs_channel
, 0);
1175 put_user(status
,value
);
1179 static int rs_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1181 struct dec_serial
* info
= (struct dec_serial
*)tty
->driver_data
;
1182 unsigned char control
, status_a
, status_b
;
1183 unsigned int result
;
1188 if (serial_paranoia_check(info
, tty
->name
, __FUNCTION__
))
1191 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1194 if (info
->zs_channel
== info
->zs_chan_a
)
1198 control
= info
->zs_chan_a
->curregs
[5];
1199 status_a
= read_zsreg(info
->zs_chan_a
, 0);
1200 status_b
= read_zsreg(info
->zs_channel
, 0);
1202 result
= ((control
& RTS
) ? TIOCM_RTS
: 0)
1203 | ((control
& DTR
) ? TIOCM_DTR
: 0)
1204 | ((status_b
& DCD
) ? TIOCM_CAR
: 0)
1205 | ((status_a
& DCD
) ? TIOCM_RNG
: 0)
1206 | ((status_a
& SYNC_HUNT
) ? TIOCM_DSR
: 0)
1207 | ((status_b
& CTS
) ? TIOCM_CTS
: 0);
1212 static int rs_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1213 unsigned int set
, unsigned int clear
)
1215 struct dec_serial
* info
= (struct dec_serial
*)tty
->driver_data
;
1217 unsigned int arg
, bits
;
1222 if (serial_paranoia_check(info
, tty
->name
, __FUNCTION__
))
1225 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1228 if (info
->zs_channel
== info
->zs_chan_a
)
1232 if (set
& TIOCM_RTS
)
1233 info
->zs_chan_a
->curregs
[5] |= RTS
;
1234 if (set
& TIOCM_DTR
)
1235 info
->zs_chan_a
->curregs
[5] |= DTR
;
1236 if (clear
& TIOCM_RTS
)
1237 info
->zs_chan_a
->curregs
[5] &= ~RTS
;
1238 if (clear
& TIOCM_DTR
)
1239 info
->zs_chan_a
->curregs
[5] &= ~DTR
;
1240 write_zsreg(info
->zs_chan_a
, 5, info
->zs_chan_a
->curregs
[5]);
1246 * rs_break - turn transmit break condition on/off
1248 static void rs_break(struct tty_struct
*tty
, int break_state
)
1250 struct dec_serial
*info
= (struct dec_serial
*) tty
->driver_data
;
1251 unsigned long flags
;
1253 if (serial_paranoia_check(info
, tty
->name
, "rs_break"))
1258 save_flags(flags
); cli();
1259 if (break_state
== -1)
1260 info
->zs_channel
->curregs
[5] |= SND_BRK
;
1262 info
->zs_channel
->curregs
[5] &= ~SND_BRK
;
1263 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
1264 restore_flags(flags
);
1267 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1268 unsigned int cmd
, unsigned long arg
)
1271 struct dec_serial
* info
= (struct dec_serial
*)tty
->driver_data
;
1276 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1279 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1280 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1281 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1282 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1288 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1289 sizeof(struct serial_struct
));
1292 return get_serial_info(info
,
1293 (struct serial_struct
*) arg
);
1295 return set_serial_info(info
,
1296 (struct serial_struct
*) arg
);
1297 case TIOCSERGETLSR
: /* Get line status register */
1298 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1299 sizeof(unsigned int));
1303 return get_lsr_info(info
, (unsigned int *) arg
);
1305 case TIOCSERGSTRUCT
:
1306 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1307 sizeof(struct dec_serial
));
1310 copy_from_user((struct dec_serial
*) arg
,
1311 info
, sizeof(struct dec_serial
));
1315 return -ENOIOCTLCMD
;
1320 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1322 struct dec_serial
*info
= (struct dec_serial
*)tty
->driver_data
;
1325 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1327 was_stopped
= info
->tx_stopped
;
1331 if (was_stopped
&& !info
->tx_stopped
)
1336 * ------------------------------------------------------------
1339 * This routine is called when the serial port gets closed.
1340 * Wait for the last remaining data to be sent.
1341 * ------------------------------------------------------------
1343 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1345 struct dec_serial
* info
= (struct dec_serial
*)tty
->driver_data
;
1346 unsigned long flags
;
1348 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1351 save_flags(flags
); cli();
1353 if (tty_hung_up_p(filp
)) {
1354 restore_flags(flags
);
1358 #ifdef SERIAL_DEBUG_OPEN
1359 printk("rs_close ttyS%02d, count = %d\n", info
->line
, info
->count
);
1361 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1363 * Uh, oh. tty->count is 1, which means that the tty
1364 * structure will be freed. Info->count should always
1365 * be one in these conditions. If it's greater than
1366 * one, we've got real problems, since it means the
1367 * serial port won't be shutdown.
1369 printk("rs_close: bad serial port count; tty->count is 1, "
1370 "info->count is %d\n", info
->count
);
1373 if (--info
->count
< 0) {
1374 printk("rs_close: bad serial port count for ttyS%02d: %d\n",
1375 info
->line
, info
->count
);
1379 restore_flags(flags
);
1382 info
->flags
|= ZILOG_CLOSING
;
1384 * Now we wait for the transmit buffer to clear; and we notify
1385 * the line discipline to only process XON/XOFF characters.
1388 if (info
->closing_wait
!= ZILOG_CLOSING_WAIT_NONE
)
1389 tty_wait_until_sent(tty
, info
->closing_wait
);
1391 * At this point we stop accepting input. To do this, we
1392 * disable the receiver and receive interrupts.
1394 info
->zs_channel
->curregs
[3] &= ~RxENABLE
;
1395 write_zsreg(info
->zs_channel
, 3, info
->zs_channel
->curregs
[3]);
1396 info
->zs_channel
->curregs
[1] = 0; /* disable any rx ints */
1397 write_zsreg(info
->zs_channel
, 1, info
->zs_channel
->curregs
[1]);
1398 ZS_CLEARFIFO(info
->zs_channel
);
1399 if (info
->flags
& ZILOG_INITIALIZED
) {
1401 * Before we drop DTR, make sure the SCC transmitter
1402 * has completely drained.
1404 rs_wait_until_sent(tty
, info
->timeout
);
1408 if (tty
->driver
->flush_buffer
)
1409 tty
->driver
->flush_buffer(tty
);
1410 if (tty
->ldisc
.flush_buffer
)
1411 tty
->ldisc
.flush_buffer(tty
);
1415 if (info
->blocked_open
) {
1416 if (info
->close_delay
) {
1417 current
->state
= TASK_INTERRUPTIBLE
;
1418 schedule_timeout(info
->close_delay
);
1420 wake_up_interruptible(&info
->open_wait
);
1422 info
->flags
&= ~(ZILOG_NORMAL_ACTIVE
|ZILOG_CLOSING
);
1423 wake_up_interruptible(&info
->close_wait
);
1424 restore_flags(flags
);
1428 * rs_wait_until_sent() --- wait until the transmitter is empty
1430 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1432 struct dec_serial
*info
= (struct dec_serial
*) tty
->driver_data
;
1433 unsigned long orig_jiffies
, char_time
;
1435 if (serial_paranoia_check(info
, tty
->name
, "rs_wait_until_sent"))
1438 orig_jiffies
= jiffies
;
1440 * Set the check interval to be 1/5 of the estimated time to
1441 * send a single character, and make it at least 1. The check
1442 * interval should also be less than the timeout.
1444 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
1445 char_time
= char_time
/ 5;
1449 char_time
= MIN(char_time
, timeout
);
1450 while ((read_zsreg(info
->zs_channel
, 1) & Tx_BUF_EMP
) == 0) {
1451 current
->state
= TASK_INTERRUPTIBLE
;
1452 schedule_timeout(char_time
);
1453 if (signal_pending(current
))
1455 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1458 current
->state
= TASK_RUNNING
;
1462 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1464 void rs_hangup(struct tty_struct
*tty
)
1466 struct dec_serial
* info
= (struct dec_serial
*)tty
->driver_data
;
1468 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1471 rs_flush_buffer(tty
);
1475 info
->flags
&= ~ZILOG_NORMAL_ACTIVE
;
1477 wake_up_interruptible(&info
->open_wait
);
1481 * ------------------------------------------------------------
1482 * rs_open() and friends
1483 * ------------------------------------------------------------
1485 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1486 struct dec_serial
*info
)
1488 DECLARE_WAITQUEUE(wait
, current
);
1493 * If the device is in the middle of being closed, then block
1494 * until it's done, and then try again.
1496 if (info
->flags
& ZILOG_CLOSING
) {
1497 interruptible_sleep_on(&info
->close_wait
);
1498 #ifdef SERIAL_DO_RESTART
1499 return ((info
->flags
& ZILOG_HUP_NOTIFY
) ?
1500 -EAGAIN
: -ERESTARTSYS
);
1507 * If this is a callout device, then just make sure the normal
1508 * device isn't being used.
1512 * If non-blocking mode is set, or the port is not enabled,
1513 * then make the check up front and then exit.
1515 if ((filp
->f_flags
& O_NONBLOCK
) ||
1516 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1517 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1521 if (tty
->termios
->c_cflag
& CLOCAL
)
1525 * Block waiting for the carrier detect and the line to become
1526 * free (i.e., not in use by the callout). While we are in
1527 * this loop, info->count is dropped by one, so that
1528 * rs_close() knows when to free things. We restore it upon
1529 * exit, either normal or abnormal.
1532 add_wait_queue(&info
->open_wait
, &wait
);
1533 #ifdef SERIAL_DEBUG_OPEN
1534 printk("block_til_ready before block: ttyS%02d, count = %d\n",
1535 info
->line
, info
->count
);
1538 if (!tty_hung_up_p(filp
))
1541 info
->blocked_open
++;
1544 if (tty
->termios
->c_cflag
& CBAUD
)
1545 zs_rtsdtr(info
, RTS
| DTR
, 1);
1547 set_current_state(TASK_INTERRUPTIBLE
);
1548 if (tty_hung_up_p(filp
) ||
1549 !(info
->flags
& ZILOG_INITIALIZED
)) {
1550 #ifdef SERIAL_DO_RESTART
1551 if (info
->flags
& ZILOG_HUP_NOTIFY
)
1554 retval
= -ERESTARTSYS
;
1560 if (!(info
->flags
& ZILOG_CLOSING
) &&
1561 (do_clocal
|| (read_zsreg(info
->zs_channel
, 0) & DCD
)))
1563 if (signal_pending(current
)) {
1564 retval
= -ERESTARTSYS
;
1567 #ifdef SERIAL_DEBUG_OPEN
1568 printk("block_til_ready blocking: ttyS%02d, count = %d\n",
1569 info
->line
, info
->count
);
1573 current
->state
= TASK_RUNNING
;
1574 remove_wait_queue(&info
->open_wait
, &wait
);
1575 if (!tty_hung_up_p(filp
))
1577 info
->blocked_open
--;
1578 #ifdef SERIAL_DEBUG_OPEN
1579 printk("block_til_ready after blocking: ttyS%02d, count = %d\n",
1580 info
->line
, info
->count
);
1584 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1589 * This routine is called whenever a serial port is opened. It
1590 * enables interrupts for a serial port, linking in its ZILOG structure into
1591 * the IRQ chain. It also performs the serial-specific
1592 * initialization for the tty structure.
1594 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1596 struct dec_serial
*info
;
1600 if ((line
< 0) || (line
>= zs_channels_found
))
1602 info
= zs_soft
+ line
;
1607 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1609 #ifdef SERIAL_DEBUG_OPEN
1610 printk("rs_open %s, count = %d\n", tty
->name
, info
->count
);
1614 tty
->driver_data
= info
;
1618 * If the port is the middle of closing, bail out now
1620 if (tty_hung_up_p(filp
) ||
1621 (info
->flags
& ZILOG_CLOSING
)) {
1622 if (info
->flags
& ZILOG_CLOSING
)
1623 interruptible_sleep_on(&info
->close_wait
);
1624 #ifdef SERIAL_DO_RESTART
1625 return ((info
->flags
& ZILOG_HUP_NOTIFY
) ?
1626 -EAGAIN
: -ERESTARTSYS
);
1633 * Start up serial port
1635 retval
= zs_startup(info
);
1639 retval
= block_til_ready(tty
, filp
, info
);
1641 #ifdef SERIAL_DEBUG_OPEN
1642 printk("rs_open returning after block_til_ready with %d\n",
1648 #ifdef CONFIG_SERIAL_CONSOLE
1649 if (sercons
.cflag
&& sercons
.index
== line
) {
1650 tty
->termios
->c_cflag
= sercons
.cflag
;
1656 #ifdef SERIAL_DEBUG_OPEN
1657 printk("rs_open %s successful...", tty
->name
);
1659 /* tty->low_latency = 1; */
1663 /* Finally, routines used to initialize the serial driver. */
1665 static void __init
show_serial_version(void)
1667 printk("DECstation Z8530 serial driver version 0.05\n");
1670 /* Initialize Z8530s zs_channels
1673 static void __init
probe_sccs(void)
1675 struct dec_serial
**pp
;
1676 int i
, n
, n_chips
= 0, n_channels
, chip
, channel
;
1679 * did we get here by accident?
1682 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1687 * When serial console is activated, tc_init has not been called yet
1688 * and system_base is undefined. Unfortunately we have to hardcode
1689 * system_base for this case :-(. HK
1691 switch(mips_machtype
) {
1692 #ifdef CONFIG_DECSTATION
1693 case MACH_DS5000_2X0
:
1694 system_base
= 0xbf800000;
1696 zs_parms
= &ds_parms
;
1698 case MACH_DS5000_1XX
:
1699 system_base
= 0xbc000000;
1701 zs_parms
= &ds_parms
;
1703 case MACH_DS5000_XX
:
1704 system_base
= 0xbc000000;
1706 zs_parms
= &ds_parms
;
1709 #ifdef CONFIG_BAGET_MIPS
1711 system_base
= UNI_IO_BASE
;
1713 zs_parms
= &baget_parms
;
1714 zs_init_regs
[2] = 0x8;
1718 panic("zs: unsupported bus");
1721 panic("zs: uninitialized parms");
1727 for (chip
= 0; chip
< n_chips
; chip
++) {
1728 for (channel
= 0; channel
<= 1; channel
++) {
1730 * The sccs reside on the high byte of the 16 bit IOBUS
1732 zs_channels
[n_channels
].control
=
1733 (volatile unsigned char *)system_base
+
1734 (0 == chip
? zs_parms
->scc0
: zs_parms
->scc1
) +
1735 (0 == channel
? zs_parms
->channel_a_offset
:
1736 zs_parms
->channel_b_offset
);
1737 zs_channels
[n_channels
].data
=
1738 zs_channels
[n_channels
].control
+ 4;
1740 #ifndef CONFIG_SERIAL_CONSOLE
1742 * We're called early and memory managment isn't up, yet.
1743 * Thus check_region would fail.
1745 if (check_region((unsigned long)
1746 zs_channels
[n_channels
].control
,
1747 ZS_CHAN_IO_SIZE
) < 0) {
1748 panic("SCC I/O region is not free");
1750 request_region((unsigned long)
1751 zs_channels
[n_channels
].control
,
1752 ZS_CHAN_IO_SIZE
, "SCC");
1754 zs_soft
[n_channels
].zs_channel
= &zs_channels
[n_channels
];
1755 zs_soft
[n_channels
].irq
= zs_parms
->irq
;
1758 * Identification of channel A. Location of channel A
1759 * inside chip depends on mapping of internal address
1760 * the chip decodes channels by.
1761 * CHANNEL_A_NR returns either 0 (in case of
1762 * DECstations) or 1 (in case of Baget).
1764 if (CHANNEL_A_NR
== channel
)
1765 zs_soft
[n_channels
].zs_chan_a
=
1766 &zs_channels
[n_channels
+1-2*CHANNEL_A_NR
];
1768 zs_soft
[n_channels
].zs_chan_a
=
1769 &zs_channels
[n_channels
];
1771 *pp
= &zs_soft
[n_channels
];
1772 pp
= &zs_soft
[n_channels
].zs_next
;
1778 zs_channels_found
= n_channels
;
1780 for (n
= 0; n
< zs_channels_found
; n
++) {
1781 for (i
= 0; i
< 16; i
++) {
1782 zs_soft
[n
].zs_channel
->curregs
[i
] = zs_init_regs
[i
];
1786 /* save_and_cli(flags);
1787 for (n = 0; n < zs_channels_found; n++) {
1788 if (((int)zs_channels[n].control & 0xf) == 1) {
1789 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1791 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1793 load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1795 restore_flags(flags); */
1798 static struct tty_operations serial_ops
= {
1802 .flush_chars
= rs_flush_chars
,
1803 .write_room
= rs_write_room
,
1804 .chars_in_buffer
= rs_chars_in_buffer
,
1805 .flush_buffer
= rs_flush_buffer
,
1807 .throttle
= rs_throttle
,
1808 .unthrottle
= rs_unthrottle
,
1809 .set_termios
= rs_set_termios
,
1812 .hangup
= rs_hangup
,
1813 .break_ctl
= rs_break
,
1814 .wait_until_sent
= rs_wait_until_sent
,
1815 .tiocmget
= rs_tiocmget
,
1816 .tiocmset
= rs_tiocmset
,
1819 /* zs_init inits the driver */
1820 int __init
zs_init(void)
1823 unsigned long flags
;
1824 struct dec_serial
*info
;
1829 /* Setup base handler, and timer table. */
1830 init_bh(SERIAL_BH
, do_serial_bh
);
1832 /* Find out how many Z8530 SCCs we have */
1836 serial_driver
= alloc_tty_driver(zs_channels_found
);
1840 show_serial_version();
1842 /* Initialize the tty_driver structure */
1843 /* Not all of this is exactly right for us. */
1845 serial_driver
->owner
= THIS_MODULE
;
1846 serial_driver
->devfs_name
= "tts/";
1847 serial_driver
->name
= "ttyS";
1848 serial_driver
->major
= TTY_MAJOR
;
1849 serial_driver
->minor_start
= 64;
1850 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1851 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1852 serial_driver
->init_termios
= tty_std_termios
;
1853 serial_driver
->init_termios
.c_cflag
=
1854 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1855 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
1856 tty_set_operations(serial_driver
, &serial_ops
);
1858 if (tty_register_driver(serial_driver
))
1859 panic("Couldn't register serial driver\n");
1861 save_flags(flags
); cli();
1863 for (channel
= 0; channel
< zs_channels_found
; ++channel
) {
1864 if (zs_soft
[channel
].hook
&&
1865 zs_soft
[channel
].hook
->init_channel
)
1866 (*zs_soft
[channel
].hook
->init_channel
)
1867 (&zs_soft
[channel
]);
1869 zs_soft
[channel
].clk_divisor
= 16;
1870 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1872 if (request_irq(zs_parms
->irq
, rs_interrupt
, SA_SHIRQ
,
1873 "SCC", &zs_soft
[channel
]))
1874 printk(KERN_ERR
"decserial: can't get irq %d\n",
1878 for (info
= zs_chain
, i
= 0; info
; info
= info
->zs_next
, i
++)
1880 if (info
->hook
&& info
->hook
->init_info
) {
1881 (*info
->hook
->init_info
)(info
);
1884 info
->magic
= SERIAL_MAGIC
;
1885 info
->port
= (int) info
->zs_channel
->control
;
1888 info
->custom_divisor
= 16;
1889 info
->close_delay
= 50;
1890 info
->closing_wait
= 3000;
1894 info
->blocked_open
= 0;
1895 info
->tqueue
.routine
= do_softint
;
1896 info
->tqueue
.data
= info
;
1897 init_waitqueue_head(&info
->open_wait
);
1898 init_waitqueue_head(&info
->close_wait
);
1899 printk("ttyS%02d at 0x%08x (irq = %d)", info
->line
,
1900 info
->port
, info
->irq
);
1901 printk(" is a Z85C30 SCC\n");
1902 tty_register_device(serial_driver
, info
->line
, NULL
);
1905 restore_flags(flags
);
1911 * register_serial and unregister_serial allows for serial ports to be
1912 * configured at run-time, to support PCMCIA modems.
1914 /* PowerMac: Unused at this time, just here to make things link. */
1915 int register_serial(struct serial_struct
*req
)
1920 void unregister_serial(int line
)
1926 * polling I/O routines
1929 zs_poll_tx_char(struct dec_serial
*info
, unsigned char ch
)
1931 struct dec_zschannel
*chan
= info
->zs_channel
;
1936 // int nine = read_zsreg(chan, R9);
1939 // write_zsreg(chan, R9, nine & ~MIE);
1943 while (!(*(chan
->control
) & Tx_BUF_EMP
) && --loops
)
1954 // write_zsreg(chan, R9, nine);
1965 zs_poll_rx_char(struct dec_serial
*info
)
1967 struct dec_zschannel
*chan
= info
->zs_channel
;
1973 while((read_zsreg(chan
, 0) & Rx_CH_AV
) == 0)
1977 ret
= read_zsdata(chan
);
1986 unsigned int register_zs_hook(unsigned int channel
, struct zs_hook
*hook
)
1988 struct dec_serial
*info
= &zs_soft
[channel
];
1991 printk("%s: line %d has already a hook registered\n",
1992 __FUNCTION__
, channel
);
2001 if (!(info
->flags
& ZILOG_INITIALIZED
))
2004 hook
->poll_rx_char
= zs_poll_rx_char
;
2005 hook
->poll_tx_char
= zs_poll_tx_char
;
2011 unsigned int unregister_zs_hook(unsigned int channel
)
2013 struct dec_serial
*info
= &zs_soft
[channel
];
2019 printk("%s: trying to unregister hook on line %d,"
2020 " but none is registered\n", __FUNCTION__
, channel
);
2026 * ------------------------------------------------------------
2027 * Serial console driver
2028 * ------------------------------------------------------------
2030 #ifdef CONFIG_SERIAL_CONSOLE
2034 * Print a string to the serial port trying not to disturb
2035 * any possible real use of the port...
2037 static void serial_console_write(struct console
*co
, const char *s
,
2040 struct dec_serial
*info
;
2043 info
= zs_soft
+ co
->index
;
2045 for (i
= 0; i
< count
; i
++, s
++) {
2047 zs_poll_tx_char(info
, '\r');
2048 zs_poll_tx_char(info
, *s
);
2052 static struct tty_driver
*serial_console_device(struct console
*c
, int *index
)
2055 return serial_driver
;
2059 * Setup initial baud/bits/parity. We do two things here:
2060 * - construct a cflag setting for the first rs_open()
2061 * - initialize the serial port
2062 * Return non-zero if we didn't find a serial port.
2064 static int __init
serial_console_setup(struct console
*co
, char *options
)
2066 struct dec_serial
*info
;
2070 int cflag
= CREAD
| HUPCL
| CLOCAL
;
2072 unsigned long flags
;
2077 info
= zs_soft
+ co
->index
;
2085 baud
= simple_strtoul(options
, NULL
, 10);
2087 while(*s
>= '0' && *s
<= '9')
2096 * Now construct a cflag setting.
2144 save_and_cli(flags
);
2147 * Turn on RTS and DTR.
2149 zs_rtsdtr(info
, RTS
| DTR
, 1);
2152 * Finally, enable sequencing
2154 info
->zs_channel
->curregs
[3] |= (RxENABLE
| Rx8
);
2155 info
->zs_channel
->curregs
[5] |= (TxENAB
| Tx8
);
2156 info
->zs_channel
->curregs
[9] |= (VIS
);
2157 write_zsreg(info
->zs_channel
, 3, info
->zs_channel
->curregs
[3]);
2158 write_zsreg(info
->zs_channel
, 5, info
->zs_channel
->curregs
[5]);
2159 write_zsreg(info
->zs_channel
, 9, info
->zs_channel
->curregs
[9]);
2162 * Clear the interrupt registers.
2164 write_zsreg(info
->zs_channel
, 0, ERR_RES
);
2165 write_zsreg(info
->zs_channel
, 0, RES_H_IUS
);
2168 * Set the speed of the serial port
2172 /* Save the current value of RR0 */
2173 info
->read_reg_zero
= read_zsreg(info
->zs_channel
, 0);
2175 zs_soft
[co
->index
].clk_divisor
= 16;
2176 zs_soft
[co
->index
].zs_baud
= get_zsbaud(&zs_soft
[co
->index
]);
2178 restore_flags(flags
);
2183 static struct console sercons
= {
2185 .write
= serial_console_write
,
2186 .device
= serial_console_device
,
2187 .setup
= serial_console_setup
,
2188 .flags
= CON_PRINTBUFFER
,
2195 void __init
zs_serial_console_init(void)
2197 register_console(&sercons
);
2199 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2202 struct dec_zschannel
*zs_kgdbchan
;
2203 static unsigned char scc_inittab
[] = {
2204 9, 0x80, /* reset A side (CHRA) */
2205 13, 0, /* set baud rate divisor */
2207 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2208 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2209 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2210 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2211 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2214 /* These are for receiving and sending characters under the kgdb
2215 * source level kernel debugger.
2217 void putDebugChar(char kgdb_char
)
2219 struct dec_zschannel
*chan
= zs_kgdbchan
;
2220 while ((read_zsreg(chan
, 0) & Tx_BUF_EMP
) == 0)
2222 write_zsdata(chan
, kgdb_char
);
2224 char getDebugChar(void)
2226 struct dec_zschannel
*chan
= zs_kgdbchan
;
2227 while((read_zsreg(chan
, 0) & Rx_CH_AV
) == 0)
2228 eieio(); /*barrier();*/
2229 return read_zsdata(chan
);
2231 void kgdb_interruptible(int yes
)
2233 struct dec_zschannel
*chan
= zs_kgdbchan
;
2235 nine
= read_zsreg(chan
, 9);
2237 one
= EXT_INT_ENAB
|INT_ALL_Rx
;
2239 printk("turning serial ints on\n");
2243 printk("turning serial ints off\n");
2245 write_zsreg(chan
, 1, one
);
2246 write_zsreg(chan
, 9, nine
);
2249 static int kgdbhook_init_channel(struct dec_serial
* info
)
2254 static void kgdbhook_init_info(struct dec_serial
* info
)
2258 static void kgdbhook_rx_char(struct dec_serial
* info
,
2259 unsigned char ch
, unsigned char stat
)
2261 if (ch
== 0x03 || ch
== '$')
2263 if (stat
& (Rx_OVR
|FRM_ERR
|PAR_ERR
))
2264 write_zsreg(info
->zs_channel
, 0, ERR_RES
);
2267 /* This sets up the serial port we're using, and turns on
2268 * interrupts for that channel, so kgdb is usable once we're done.
2270 static inline void kgdb_chaninit(struct dec_zschannel
*ms
, int intson
, int bps
)
2274 volatile char *sccc
= ms
->control
;
2275 brg
= BPS_TO_BRG(bps
, zs_parms
->clock
/16);
2276 printk("setting bps on kgdb line to %d [brg=%x]\n", bps
, brg
);
2277 for (i
= 20000; i
!= 0; --i
) {
2280 for (i
= 0; i
< sizeof(scc_inittab
); ++i
) {
2281 write_zsreg(ms
, scc_inittab
[i
], scc_inittab
[i
+1]);
2285 /* This is called at boot time to prime the kgdb serial debugging
2286 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2287 * for /dev/ttyb which is determined in setup_arch() from the
2288 * boot command line flags.
2290 struct zs_hook zs_kgdbhook
= {
2291 init_channel
: kgdbhook_init_channel
,
2292 init_info
: kgdbhook_init_info
,
2293 cflags
: B38400
|CS8
|CLOCAL
,
2294 rx_char
: kgdbhook_rx_char
,
2297 void __init
zs_kgdb_hook(int tty_num
)
2299 /* Find out how many Z8530 SCCs we have */
2302 zs_soft
[tty_num
].zs_channel
= &zs_channels
[tty_num
];
2303 zs_kgdbchan
= zs_soft
[tty_num
].zs_channel
;
2304 zs_soft
[tty_num
].change_needed
= 0;
2305 zs_soft
[tty_num
].clk_divisor
= 16;
2306 zs_soft
[tty_num
].zs_baud
= 38400;
2307 zs_soft
[tty_num
].hook
= &zs_kgdbhook
; /* This runs kgdb */
2308 /* Turn on transmitter/receiver at 8-bits/char */
2309 kgdb_chaninit(zs_soft
[tty_num
].zs_channel
, 1, 38400);
2310 printk("KGDB: on channel %d initialized\n", tty_num
);
2311 set_debug_traps(); /* init stub */
2313 #endif /* ifdef CONFIG_KGDB */