Import 2.3.10pre1
[davej-history.git] / drivers / macintosh / macserial.c
blob9cf844c217b311f447b520ab1995ccf2571608f9
1 /*
2 * macserial.c: Serial port driver for Power Macintoshes.
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
6 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
7 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8 */
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/timer.h>
15 #include <linux/interrupt.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/major.h>
19 #include <linux/string.h>
20 #include <linux/fcntl.h>
21 #include <linux/mm.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #ifdef CONFIG_SERIAL_CONSOLE
26 #include <linux/console.h>
27 #endif
29 #include <asm/io.h>
30 #include <asm/pgtable.h>
31 #include <asm/irq.h>
32 #include <asm/prom.h>
33 #include <asm/system.h>
34 #include <asm/segment.h>
35 #include <asm/bitops.h>
36 #include <asm/feature.h>
37 #include <asm/adb.h>
38 #include <asm/pmu.h>
39 #ifdef CONFIG_KGDB
40 #include <asm/kgdb.h>
41 #endif
42 #include <asm/init.h>
44 #include "macserial.h"
47 * It would be nice to dynamically allocate everything that
48 * depends on NUM_SERIAL, so we could support any number of
49 * Z8530s, but for now...
51 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
52 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
54 /* On PowerMacs, the hardware takes care of the SCC recovery time,
55 but we need the eieio to make sure that the accesses occur
56 in the order we want. */
57 #define RECOVERY_DELAY eieio()
59 struct mac_zschannel zs_channels[NUM_CHANNELS];
61 struct mac_serial zs_soft[NUM_CHANNELS];
62 int zs_channels_found;
63 struct mac_serial *zs_chain; /* list of all channels */
65 struct tty_struct zs_ttys[NUM_CHANNELS];
67 static int is_powerbook;
69 #ifdef CONFIG_SERIAL_CONSOLE
70 static struct console sercons;
71 #endif
73 #ifdef CONFIG_KGDB
74 struct mac_zschannel *zs_kgdbchan;
75 static unsigned char scc_inittab[] = {
76 9, 0x80, /* reset A side (CHRA) */
77 13, 0, /* set baud rate divisor */
78 12, 1,
79 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
80 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
81 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
82 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
83 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
85 #endif
86 #define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
88 static DECLARE_TASK_QUEUE(tq_serial);
90 static struct tty_driver serial_driver, callout_driver;
91 static int serial_refcount;
93 /* serial subtype definitions */
94 #define SERIAL_TYPE_NORMAL 1
95 #define SERIAL_TYPE_CALLOUT 2
97 /* number of characters left in xmit buffer before we ask for more */
98 #define WAKEUP_CHARS 256
101 * Debugging.
103 #undef SERIAL_DEBUG_INTR
104 #undef SERIAL_DEBUG_OPEN
105 #undef SERIAL_DEBUG_FLOW
106 #undef SERIAL_DEBUG_POWER
107 #undef SERIAL_DEBUG_THROTTLE
108 #undef SERIAL_DEBUG_STOP
109 #undef SERIAL_DEBUG_BAUDS
111 #define RS_STROBE_TIME 10
112 #define RS_ISR_PASS_LIMIT 256
114 #define _INLINE_ inline
116 static void probe_sccs(void);
117 static void change_speed(struct mac_serial *info, struct termios *old);
118 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
119 static void set_scc_power(struct mac_serial * info, int state);
120 static int setup_scc(struct mac_serial * info);
122 static struct tty_struct *serial_table[NUM_CHANNELS];
123 static struct termios *serial_termios[NUM_CHANNELS];
124 static struct termios *serial_termios_locked[NUM_CHANNELS];
126 #ifndef MIN
127 #define MIN(a,b) ((a) < (b) ? (a) : (b))
128 #endif
131 * tmp_buf is used as a temporary buffer by serial_write. We need to
132 * lock it in case the copy_from_user blocks while swapping in a page,
133 * and some other program tries to do a serial write at the same time.
134 * Since the lock will only come under contention when the system is
135 * swapping and available memory is low, it makes sense to share one
136 * buffer across all the serial ports, since it significantly saves
137 * memory if large numbers of serial ports are open.
139 static unsigned char tmp_buf[4096]; /* This is cheating */
140 DECLARE_MUTEX(tmp_buf_sem);
143 static inline int __pmac
144 serial_paranoia_check(struct mac_serial *info,
145 dev_t device, const char *routine)
147 #ifdef SERIAL_PARANOIA_CHECK
148 static const char *badmagic =
149 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
150 static const char *badinfo =
151 "Warning: null mac_serial for (%d, %d) in %s\n";
153 if (!info) {
154 printk(badinfo, MAJOR(device), MINOR(device), routine);
155 return 1;
157 if (info->magic != SERIAL_MAGIC) {
158 printk(badmagic, MAJOR(device), MINOR(device), routine);
159 return 1;
161 #endif
162 return 0;
166 * Reading and writing Z8530 registers.
168 static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
169 unsigned char reg)
171 unsigned char retval;
172 unsigned long flags;
175 * We have to make this atomic.
177 spin_lock_irqsave(&channel->lock, flags);
178 if (reg != 0) {
179 *channel->control = reg;
180 RECOVERY_DELAY;
182 retval = *channel->control;
183 RECOVERY_DELAY;
184 spin_unlock_irqrestore(&channel->lock, flags);
185 return retval;
188 static inline void __pmac write_zsreg(struct mac_zschannel *channel,
189 unsigned char reg, unsigned char value)
191 unsigned long flags;
193 spin_lock_irqsave(&channel->lock, flags);
194 if (reg != 0) {
195 *channel->control = reg;
196 RECOVERY_DELAY;
198 *channel->control = value;
199 RECOVERY_DELAY;
200 spin_unlock_irqrestore(&channel->lock, flags);
201 return;
204 static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
206 unsigned char retval;
208 retval = *channel->data;
209 RECOVERY_DELAY;
210 return retval;
213 static inline void write_zsdata(struct mac_zschannel *channel,
214 unsigned char value)
216 *channel->data = value;
217 RECOVERY_DELAY;
218 return;
221 static inline void load_zsregs(struct mac_zschannel *channel,
222 unsigned char *regs)
224 ZS_CLEARERR(channel);
225 ZS_CLEARFIFO(channel);
226 /* Load 'em up */
227 write_zsreg(channel, R4, regs[R4]);
228 write_zsreg(channel, R10, regs[R10]);
229 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
230 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
231 write_zsreg(channel, R1, regs[R1]);
232 write_zsreg(channel, R9, regs[R9]);
233 write_zsreg(channel, R11, regs[R11]);
234 write_zsreg(channel, R12, regs[R12]);
235 write_zsreg(channel, R13, regs[R13]);
236 write_zsreg(channel, R14, regs[R14]);
237 write_zsreg(channel, R15, regs[R15]);
238 write_zsreg(channel, R3, regs[R3]);
239 write_zsreg(channel, R5, regs[R5]);
240 return;
243 /* Sets or clears DTR/RTS on the requested line */
244 static inline void zs_rtsdtr(struct mac_serial *ss, int set)
246 if (set)
247 ss->curregs[5] |= (RTS | DTR);
248 else
249 ss->curregs[5] &= ~(RTS | DTR);
250 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
251 return;
254 /* Utility routines for the Zilog */
255 static inline int get_zsbaud(struct mac_serial *ss)
257 struct mac_zschannel *channel = ss->zs_channel;
258 int brg;
260 if ((ss->curregs[R11] & TCBR) == 0) {
261 /* higher rates don't use the baud rate generator */
262 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
264 /* The baud rate is split up between two 8-bit registers in
265 * what is termed 'BRG time constant' format in my docs for
266 * the chip, it is a function of the clk rate the chip is
267 * receiving which happens to be constant.
269 brg = (read_zsreg(channel, 13) << 8);
270 brg |= read_zsreg(channel, 12);
271 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
274 /* On receive, this clears errors and the receiver interrupts */
275 static inline void rs_recv_clear(struct mac_zschannel *zsc)
277 write_zsreg(zsc, 0, ERR_RES);
278 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
282 * ----------------------------------------------------------------------
284 * Here starts the interrupt handling routines. All of the following
285 * subroutines are declared as inline and are folded into
286 * rs_interrupt(). They were separated out for readability's sake.
288 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
289 * -----------------------------------------------------------------------
293 * This routine is used by the interrupt handler to schedule
294 * processing in the software interrupt portion of the driver.
296 static _INLINE_ void rs_sched_event(struct mac_serial *info,
297 int event)
299 info->event |= 1 << event;
300 queue_task(&info->tqueue, &tq_serial);
301 mark_bh(MACSERIAL_BH);
304 static _INLINE_ void receive_chars(struct mac_serial *info,
305 struct pt_regs *regs)
307 struct tty_struct *tty = info->tty;
308 unsigned char ch, stat, flag;
310 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
312 stat = read_zsreg(info->zs_channel, R1);
313 ch = read_zsdata(info->zs_channel);
315 #ifdef CONFIG_KGDB
316 if (info->kgdb_channel) {
317 if (ch == 0x03 || ch == '$')
318 breakpoint();
319 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
320 write_zsreg(info->zs_channel, 0, ERR_RES);
321 return;
323 #endif
324 if (!tty)
325 continue;
326 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
327 tty_flip_buffer_push(tty);
329 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
330 static int flip_buf_ovf;
331 ++flip_buf_ovf;
332 printk("FB. overflow: %d\n", flip_buf_ovf);
333 break;
335 tty->flip.count++;
337 static int flip_max_cnt;
338 if (flip_max_cnt < tty->flip.count)
339 flip_max_cnt = tty->flip.count;
341 if (stat & Rx_OVR) {
342 flag = TTY_OVERRUN;
343 } else if (stat & FRM_ERR) {
344 flag = TTY_FRAME;
345 } else if (stat & PAR_ERR) {
346 flag = TTY_PARITY;
347 } else
348 flag = 0;
349 if (flag)
350 /* reset the error indication */
351 write_zsreg(info->zs_channel, 0, ERR_RES);
352 *tty->flip.flag_buf_ptr++ = flag;
353 *tty->flip.char_buf_ptr++ = ch;
355 if (tty)
356 tty_flip_buffer_push(tty);
359 static void transmit_chars(struct mac_serial *info)
361 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
362 return;
363 info->tx_active = 0;
365 if (info->x_char) {
366 /* Send next char */
367 write_zsdata(info->zs_channel, info->x_char);
368 info->x_char = 0;
369 info->tx_active = 1;
370 return;
373 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped) {
374 write_zsreg(info->zs_channel, 0, RES_Tx_P);
375 return;
378 /* Send char */
379 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
380 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
381 info->xmit_cnt--;
382 info->tx_active = 1;
384 if (info->xmit_cnt < WAKEUP_CHARS)
385 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
388 static _INLINE_ void status_handle(struct mac_serial *info)
390 unsigned char status;
392 /* Get status from Read Register 0 */
393 status = read_zsreg(info->zs_channel, 0);
395 /* Check for DCD transitions */
396 if (((status ^ info->read_reg_zero) & DCD) != 0
397 && info->tty && !C_CLOCAL(info->tty)) {
398 if (status & DCD) {
399 wake_up_interruptible(&info->open_wait);
400 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
401 if (info->tty)
402 tty_hangup(info->tty);
406 /* Check for CTS transitions */
407 if (info->tty && C_CRTSCTS(info->tty)) {
409 * For some reason, on the Power Macintosh,
410 * it seems that the CTS bit is 1 when CTS is
411 * *negated* and 0 when it is asserted.
412 * The DCD bit doesn't seem to be inverted
413 * like this.
415 if ((status & CTS) == 0) {
416 if (info->tx_stopped) {
417 #ifdef SERIAL_DEBUG_FLOW
418 printk("CTS up\n");
419 #endif
420 info->tx_stopped = 0;
421 if (!info->tx_active)
422 transmit_chars(info);
424 } else {
425 #ifdef SERIAL_DEBUG_FLOW
426 printk("CTS down\n");
427 #endif
428 info->tx_stopped = 1;
432 /* Clear status condition... */
433 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
434 info->read_reg_zero = status;
438 * This is the serial driver's generic interrupt routine
440 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
442 struct mac_serial *info = (struct mac_serial *) dev_id;
443 unsigned char zs_intreg;
444 int shift;
446 /* NOTE: The read register 3, which holds the irq status,
447 * does so for both channels on each chip. Although
448 * the status value itself must be read from the A
449 * channel and is only valid when read from channel A.
450 * Yes... broken hardware...
452 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
454 if (info->zs_chan_a == info->zs_channel)
455 shift = 3; /* Channel A */
456 else
457 shift = 0; /* Channel B */
459 for (;;) {
460 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
461 #ifdef SERIAL_DEBUG_INTR
462 printk("rs_interrupt: irq %d, zs_intreg 0x%x\n", irq, (int)zs_intreg);
463 #endif
465 if ((zs_intreg & CHAN_IRQMASK) == 0)
466 break;
468 if (!(info->flags & ZILOG_INITIALIZED)) {
469 printk("rs_interrupt: irq %d, port not initialized\n", irq);
470 break;
473 if (zs_intreg & CHBRxIP)
474 receive_chars(info, regs);
475 if (zs_intreg & CHBTxIP)
476 transmit_chars(info);
477 if (zs_intreg & CHBEXT)
478 status_handle(info);
483 * -------------------------------------------------------------------
484 * Here ends the serial interrupt routines.
485 * -------------------------------------------------------------------
489 * ------------------------------------------------------------
490 * rs_stop() and rs_start()
492 * This routines are called before setting or resetting tty->stopped.
493 * ------------------------------------------------------------
495 static void rs_stop(struct tty_struct *tty)
497 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
499 #ifdef SERIAL_DEBUG_STOP
500 printk("rs_stop %ld....\n",
501 tty->ldisc.chars_in_buffer(tty));
502 #endif
504 if (serial_paranoia_check(info, tty->device, "rs_stop"))
505 return;
507 #if 0
508 save_flags(flags); cli();
509 if (info->curregs[5] & TxENAB) {
510 info->curregs[5] &= ~TxENAB;
511 info->pendregs[5] &= ~TxENAB;
512 write_zsreg(info->zs_channel, 5, info->curregs[5]);
514 restore_flags(flags);
515 #endif
518 static void rs_start(struct tty_struct *tty)
520 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
521 unsigned long flags;
523 #ifdef SERIAL_DEBUG_STOP
524 printk("rs_start %ld....\n",
525 tty->ldisc.chars_in_buffer(tty));
526 #endif
528 if (serial_paranoia_check(info, tty->device, "rs_start"))
529 return;
531 save_flags(flags); cli();
532 #if 0
533 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
534 info->curregs[5] |= TxENAB;
535 info->pendregs[5] = info->curregs[5];
536 write_zsreg(info->zs_channel, 5, info->curregs[5]);
538 #else
539 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
540 transmit_chars(info);
542 #endif
543 restore_flags(flags);
547 * This routine is used to handle the "bottom half" processing for the
548 * serial driver, known also the "software interrupt" processing.
549 * This processing is done at the kernel interrupt level, after the
550 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
551 * is where time-consuming activities which can not be done in the
552 * interrupt driver proper are done; the interrupt driver schedules
553 * them using rs_sched_event(), and they get done here.
555 static void do_serial_bh(void)
557 run_task_queue(&tq_serial);
560 static void do_softint(void *private_)
562 struct mac_serial *info = (struct mac_serial *) private_;
563 struct tty_struct *tty;
565 tty = info->tty;
566 if (!tty)
567 return;
569 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
570 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
571 tty->ldisc.write_wakeup)
572 (tty->ldisc.write_wakeup)(tty);
573 wake_up_interruptible(&tty->write_wait);
577 static void rs_timer(void)
581 static int startup(struct mac_serial * info)
583 #ifdef SERIAL_DEBUG_OPEN
584 printk("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
585 #endif
587 if (info->flags & ZILOG_INITIALIZED) {
588 #ifdef SERIAL_DEBUG_OPEN
589 printk(" -> already inited\n");
590 #endif
591 return 0;
594 if (!info->xmit_buf) {
595 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
596 if (!info->xmit_buf)
597 return -ENOMEM;
600 #ifdef SERIAL_DEBUG_OPEN
601 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
602 #endif
604 set_scc_power(info, 1);
606 setup_scc(info);
608 #ifdef SERIAL_DEBUG_OPEN
609 printk("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
610 #endif
612 info->flags |= ZILOG_INITIALIZED;
613 enable_irq(info->irq);
615 return 0;
618 static int setup_scc(struct mac_serial * info)
620 unsigned long flags;
622 #ifdef SERIAL_DEBUG_OPEN
623 printk("setting up ttys%d SCC...\n", info->line);
624 #endif
626 save_flags(flags); cli(); /* Disable interrupts */
629 * Reset the chip.
631 write_zsreg(info->zs_channel, 9,
632 (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
633 udelay(10);
634 write_zsreg(info->zs_channel, 9, 0);
637 * Clear the receive FIFO.
639 ZS_CLEARFIFO(info->zs_channel);
640 info->xmit_fifo_size = 1;
643 * Clear the interrupt registers.
645 write_zsreg(info->zs_channel, 0, ERR_RES);
646 write_zsreg(info->zs_channel, 0, RES_H_IUS);
649 * Turn on RTS and DTR.
651 zs_rtsdtr(info, 1);
654 * Finally, enable sequencing and interrupts
656 info->curregs[1] = (info->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
657 info->pendregs[1] = info->curregs[1];
658 info->curregs[3] |= (RxENABLE | Rx8);
659 info->pendregs[3] = info->curregs[3];
660 info->curregs[5] |= (TxENAB | Tx8);
661 info->pendregs[5] = info->curregs[5];
662 info->curregs[9] |= (NV | MIE);
663 info->pendregs[9] = info->curregs[9];
664 write_zsreg(info->zs_channel, 3, info->curregs[3]);
665 write_zsreg(info->zs_channel, 5, info->curregs[5]);
666 write_zsreg(info->zs_channel, 9, info->curregs[9]);
668 if (info->tty)
669 clear_bit(TTY_IO_ERROR, &info->tty->flags);
670 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
673 * Set the speed of the serial port
675 change_speed(info, 0);
677 /* Save the current value of RR0 */
678 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
680 restore_flags(flags);
682 return 0;
686 * This routine will shutdown a serial port; interrupts are disabled, and
687 * DTR is dropped if the hangup on close termio flag is on.
689 static void shutdown(struct mac_serial * info)
691 #ifdef SERIAL_DEBUG_OPEN
692 printk("Shutting down serial port %d (irq %d)....\n", info->line,
693 info->irq);
694 #endif
696 if (!(info->flags & ZILOG_INITIALIZED)) {
697 #ifdef SERIAL_DEBUG_OPEN
698 printk("(already shutdown)\n");
699 #endif
701 return;
704 disable_irq(info->irq);
706 info->pendregs[1] = info->curregs[1] = 0;
707 write_zsreg(info->zs_channel, 1, 0); /* no interrupts */
709 info->curregs[3] &= ~RxENABLE;
710 info->pendregs[3] = info->curregs[3];
711 write_zsreg(info->zs_channel, 3, info->curregs[3]);
713 info->curregs[5] &= ~TxENAB;
714 if (!info->tty || C_HUPCL(info->tty))
715 info->curregs[5] &= ~(DTR | RTS);
716 info->pendregs[5] = info->curregs[5];
717 write_zsreg(info->zs_channel, 5, info->curregs[5]);
719 if (info->tty)
720 set_bit(TTY_IO_ERROR, &info->tty->flags);
722 set_scc_power(info, 0);
724 if (info->xmit_buf) {
725 free_page((unsigned long) info->xmit_buf);
726 info->xmit_buf = 0;
729 memset(info->curregs, 0, sizeof(info->curregs));
730 memset(info->curregs, 0, sizeof(info->pendregs));
732 info->flags &= ~ZILOG_INITIALIZED;
735 static void set_scc_power(struct mac_serial * info, int state)
737 if (feature_test(info->dev_node, FEATURE_Serial_enable) < 0)
738 return; /* don't have serial power control */
740 /* The timings looks strange but that's the ones MacOS seems
741 to use for the internal modem. I think we can use a lot faster
742 ones, at least whe not using the modem, this should be tested.
744 if (state) {
745 #ifdef SERIAL_DEBUG_POWER
746 printk(KERN_INFO "ttyS%02d: powering up hardware\n", info->line);
747 #endif
748 if (feature_test(info->dev_node, FEATURE_Serial_enable) == 0) {
749 feature_clear(info->dev_node, FEATURE_Serial_reset);
750 mdelay(5);
751 feature_set(info->dev_node, FEATURE_Serial_enable);
753 if (info->zs_chan_a == info->zs_channel)
754 feature_set(info->dev_node, FEATURE_Serial_IO_A);
755 else
756 feature_set(info->dev_node, FEATURE_Serial_IO_B);
757 mdelay(1);
759 if (info->is_cobalt_modem){
760 feature_set(info->dev_node, FEATURE_Modem_Reset);
761 mdelay(15);
762 feature_clear(info->dev_node, FEATURE_Modem_Reset);
763 /* XXX Note the big 250ms, we should probably replace this
764 by something better since we have irqs disabled here
766 mdelay(250);
768 if (info->is_pwbk_ir)
769 pmu_enable_irled(1);
770 } else {
771 #ifdef SERIAL_DEBUG_POWER
772 printk(KERN_INFO "ttyS%02d: shutting down hardware\n", info->line);
773 #endif
774 #ifdef CONFIG_KGDB
775 if (info->kgdb_channel) {
776 #ifdef SERIAL_DEBUG_POWER
777 printk(KERN_INFO " (canceled by KGDB)\n");
778 #endif
779 return;
781 #endif
782 #ifdef CONFIG_XMON
783 if (!info->is_cobalt_modem) {
784 #ifdef SERIAL_DEBUG_POWER
785 printk(KERN_INFO " (canceled by XMON)\n");
786 #endif
787 return;
789 #endif
790 if (info->is_cobalt_modem) {
791 #ifdef SERIAL_DEBUG_POWER
792 printk(KERN_INFO "ttyS%02d: shutting down modem\n", info->line);
793 #endif
794 feature_set(info->dev_node, FEATURE_Modem_Reset);
795 mdelay(15);
796 feature_clear(info->dev_node, FEATURE_Modem_Reset);
797 mdelay(25);
799 if (info->is_pwbk_ir)
800 pmu_enable_irled(0);
802 if (info->zs_chan_a == info->zs_channel) {
803 #ifdef SERIAL_DEBUG_POWER
804 printk(KERN_INFO "ttyS%02d: shutting down SCC channel A\n", info->line);
805 #endif
806 feature_clear(info->dev_node, FEATURE_Serial_IO_A);
807 } else {
808 #ifdef SERIAL_DEBUG_POWER
809 printk(KERN_INFO "ttyS%02d: shutting down SCC channel B\n", info->line);
810 #endif
811 feature_clear(info->dev_node, FEATURE_Serial_IO_B);
813 /* XXX for now, shut down SCC core only on powerbooks */
814 if (is_powerbook
815 && !(feature_test(info->dev_node, FEATURE_Serial_IO_A) ||
816 feature_test(info->dev_node, FEATURE_Serial_IO_B))) {
817 #ifdef SERIAL_DEBUG_POWER
818 printk(KERN_INFO "ttyS%02d: shutting down SCC core\n", info->line);
819 #endif
820 feature_set(info->dev_node, FEATURE_Serial_reset);
821 mdelay(10);
822 feature_clear(info->dev_node, FEATURE_Serial_enable);
823 mdelay(5);
830 * This routine is called to set the UART divisor registers to match
831 * the specified baud rate for a serial port.
833 static void change_speed(struct mac_serial *info, struct termios *old_termios)
835 unsigned short port;
836 unsigned cflag;
837 int bits;
838 int brg, baud;
839 unsigned long flags;
841 if (!info->tty || !info->tty->termios)
842 return;
843 if (!(port = info->port))
844 return;
846 cflag = info->tty->termios->c_cflag;
847 baud = tty_get_baud_rate(info->tty);
848 if (baud == 0) {
849 if (old_termios) {
850 info->tty->termios->c_cflag &= ~CBAUD;
851 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
852 cflag = info->tty->termios->c_cflag;
853 baud = tty_get_baud_rate(info->tty);
855 else
856 baud = info->zs_baud;
858 if (baud > 230400)
859 baud = 230400;
860 else if (baud == 0)
861 baud = 38400;
863 save_flags(flags); cli();
864 info->zs_baud = baud;
865 info->clk_divisor = 16;
867 #ifdef SERIAL_DEBUG_BAUDS
868 printk("set speed to %d bds, ", baud);
869 #endif
871 switch (baud) {
872 case ZS_CLOCK/16: /* 230400 */
873 info->curregs[4] = X16CLK;
874 info->curregs[11] = 0;
875 break;
876 case ZS_CLOCK/32: /* 115200 */
877 info->curregs[4] = X32CLK;
878 info->curregs[11] = 0;
879 break;
880 default:
881 info->curregs[4] = X16CLK;
882 info->curregs[11] = TCBR | RCBR;
883 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
884 info->curregs[12] = (brg & 255);
885 info->curregs[13] = ((brg >> 8) & 255);
886 info->curregs[14] = BRENABL;
889 /* byte size and parity */
890 info->curregs[3] &= ~RxNBITS_MASK;
891 info->curregs[5] &= ~TxNBITS_MASK;
892 switch (cflag & CSIZE) {
893 case CS5:
894 info->curregs[3] |= Rx5;
895 info->curregs[5] |= Tx5;
896 #ifdef SERIAL_DEBUG_BAUDS
897 printk("5 bits, ");
898 #endif
899 bits = 7;
900 break;
901 case CS6:
902 info->curregs[3] |= Rx6;
903 info->curregs[5] |= Tx6;
904 #ifdef SERIAL_DEBUG_BAUDS
905 printk("6 bits, ");
906 #endif
907 bits = 8;
908 break;
909 case CS7:
910 info->curregs[3] |= Rx7;
911 info->curregs[5] |= Tx7;
912 #ifdef SERIAL_DEBUG_BAUDS
913 printk("7 bits, ");
914 #endif
915 bits = 9;
916 break;
917 case CS8:
918 default: /* defaults to 8 bits */
919 info->curregs[3] |= Rx8;
920 info->curregs[5] |= Tx8;
921 #ifdef SERIAL_DEBUG_BAUDS
922 printk("8 bits, ");
923 #endif
924 bits = 10;
925 break;
927 info->pendregs[3] = info->curregs[3];
928 info->pendregs[5] = info->curregs[5];
930 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
931 if (cflag & CSTOPB) {
932 info->curregs[4] |= SB2;
933 bits++;
934 #ifdef SERIAL_DEBUG_BAUDS
935 printk("2 stop, ");
936 #endif
937 } else {
938 info->curregs[4] |= SB1;
939 #ifdef SERIAL_DEBUG_BAUDS
940 printk("1 stop, ");
941 #endif
943 if (cflag & PARENB) {
944 bits++;
945 info->curregs[4] |= PAR_ENA;
946 #ifdef SERIAL_DEBUG_BAUDS
947 printk("parity, ");
948 #endif
950 if (!(cflag & PARODD)) {
951 info->curregs[4] |= PAR_EVEN;
953 info->pendregs[4] = info->curregs[4];
955 if (!(cflag & CLOCAL)) {
956 if (!(info->curregs[15] & DCDIE))
957 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
958 info->curregs[15] |= DCDIE;
959 } else
960 info->curregs[15] &= ~DCDIE;
961 if (cflag & CRTSCTS) {
962 info->curregs[15] |= CTSIE;
963 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
964 info->tx_stopped = 1;
965 } else {
966 info->curregs[15] &= ~CTSIE;
967 info->tx_stopped = 0;
969 info->pendregs[15] = info->curregs[15];
971 /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
972 This code would love a larger HZ and a >1 fifo size, but this is not
973 a priority. The resulting value must be >HZ/2
975 info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
976 info->timeout += HZ/50+1; /* Add .02 seconds of slop */
978 #ifdef SERIAL_DEBUG_BAUDS
979 printk("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ, (int)info->baud_base);
980 #endif
982 /* Load up the new values */
983 load_zsregs(info->zs_channel, info->curregs);
985 restore_flags(flags);
988 static void rs_flush_chars(struct tty_struct *tty)
990 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
991 unsigned long flags;
993 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
994 return;
996 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
997 !info->xmit_buf)
998 return;
1000 /* Enable transmitter */
1001 save_flags(flags); cli();
1002 transmit_chars(info);
1003 restore_flags(flags);
1006 static int rs_write(struct tty_struct * tty, int from_user,
1007 const unsigned char *buf, int count)
1009 int c, total = 0;
1010 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1011 unsigned long flags;
1013 if (serial_paranoia_check(info, tty->device, "rs_write"))
1014 return 0;
1016 if (!tty || !info->xmit_buf)
1017 return 0;
1019 save_flags(flags);
1020 while (1) {
1021 cli();
1022 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1023 SERIAL_XMIT_SIZE - info->xmit_head));
1024 if (c <= 0)
1025 break;
1027 if (from_user) {
1028 down(&tmp_buf_sem);
1029 copy_from_user(tmp_buf, buf, c);
1030 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1031 SERIAL_XMIT_SIZE - info->xmit_head));
1032 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1033 up(&tmp_buf_sem);
1034 } else
1035 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1036 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1037 info->xmit_cnt += c;
1038 restore_flags(flags);
1039 buf += c;
1040 count -= c;
1041 total += c;
1043 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1044 && !info->tx_active)
1045 transmit_chars(info);
1046 restore_flags(flags);
1047 return total;
1050 static int rs_write_room(struct tty_struct *tty)
1052 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1053 int ret;
1055 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1056 return 0;
1057 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1058 if (ret < 0)
1059 ret = 0;
1060 return ret;
1063 static int rs_chars_in_buffer(struct tty_struct *tty)
1065 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1067 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1068 return 0;
1069 return info->xmit_cnt;
1072 static void rs_flush_buffer(struct tty_struct *tty)
1074 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1076 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1077 return;
1078 cli();
1079 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1080 sti();
1081 wake_up_interruptible(&tty->write_wait);
1082 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1083 tty->ldisc.write_wakeup)
1084 (tty->ldisc.write_wakeup)(tty);
1088 * ------------------------------------------------------------
1089 * rs_throttle()
1091 * This routine is called by the upper-layer tty layer to signal that
1092 * incoming characters should be throttled.
1093 * ------------------------------------------------------------
1095 static void rs_throttle(struct tty_struct * tty)
1097 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1098 unsigned long flags;
1099 #ifdef SERIAL_DEBUG_THROTTLE
1100 char buf[64];
1102 printk("throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1103 #endif
1105 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1106 return;
1108 if (I_IXOFF(tty)) {
1109 save_flags(flags); cli();
1110 info->x_char = STOP_CHAR(tty);
1111 if (!info->tx_active)
1112 transmit_chars(info);
1113 restore_flags(flags);
1116 if (C_CRTSCTS(tty)) {
1118 * Here we want to turn off the RTS line. On Macintoshes,
1119 * we only get the DTR line, which goes to both DTR and
1120 * RTS on the modem. RTS doesn't go out to the serial
1121 * port socket. So you should make sure your modem is
1122 * set to ignore DTR if you're using CRTSCTS.
1124 save_flags(flags); cli();
1125 info->curregs[5] &= ~(DTR | RTS);
1126 info->pendregs[5] &= ~(DTR | RTS);
1127 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1128 restore_flags(flags);
1132 static void rs_unthrottle(struct tty_struct * tty)
1134 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1135 unsigned long flags;
1136 #ifdef SERIAL_DEBUG_THROTTLE
1137 char buf[64];
1139 printk("unthrottle %s: %d....\n",tty->ldisc.chars_in_buffer(tty));
1140 #endif
1142 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1143 return;
1145 if (I_IXOFF(tty)) {
1146 save_flags(flags); cli();
1147 if (info->x_char)
1148 info->x_char = 0;
1149 else {
1150 info->x_char = START_CHAR(tty);
1151 if (!info->tx_active)
1152 transmit_chars(info);
1154 restore_flags(flags);
1157 if (C_CRTSCTS(tty)) {
1158 /* Assert RTS and DTR lines */
1159 save_flags(flags); cli();
1160 info->curregs[5] |= DTR | RTS;
1161 info->pendregs[5] |= DTR | RTS;
1162 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1163 restore_flags(flags);
1168 * ------------------------------------------------------------
1169 * rs_ioctl() and friends
1170 * ------------------------------------------------------------
1173 static int get_serial_info(struct mac_serial * info,
1174 struct serial_struct * retinfo)
1176 struct serial_struct tmp;
1178 if (!retinfo)
1179 return -EFAULT;
1180 memset(&tmp, 0, sizeof(tmp));
1181 tmp.type = info->type;
1182 tmp.line = info->line;
1183 tmp.port = info->port;
1184 tmp.irq = info->irq;
1185 tmp.flags = info->flags;
1186 tmp.baud_base = info->baud_base;
1187 tmp.close_delay = info->close_delay;
1188 tmp.closing_wait = info->closing_wait;
1189 tmp.custom_divisor = info->custom_divisor;
1190 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1193 static int set_serial_info(struct mac_serial * info,
1194 struct serial_struct * new_info)
1196 struct serial_struct new_serial;
1197 struct mac_serial old_info;
1198 int retval = 0;
1200 if (!new_info)
1201 return -EFAULT;
1202 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1203 old_info = *info;
1205 if (!capable(CAP_SYS_ADMIN)) {
1206 if ((new_serial.baud_base != info->baud_base) ||
1207 (new_serial.type != info->type) ||
1208 (new_serial.close_delay != info->close_delay) ||
1209 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1210 (info->flags & ~ZILOG_USR_MASK)))
1211 return -EPERM;
1212 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1213 (new_serial.flags & ZILOG_USR_MASK));
1214 info->custom_divisor = new_serial.custom_divisor;
1215 goto check_and_exit;
1218 if (info->count > 1)
1219 return -EBUSY;
1222 * OK, past this point, all the error checking has been done.
1223 * At this point, we start making changes.....
1226 info->baud_base = new_serial.baud_base;
1227 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1228 (new_serial.flags & ZILOG_FLAGS));
1229 info->type = new_serial.type;
1230 info->close_delay = new_serial.close_delay;
1231 info->closing_wait = new_serial.closing_wait;
1233 check_and_exit:
1234 if (info->flags & ZILOG_INITIALIZED)
1235 retval = setup_scc(info);
1236 return retval;
1240 * get_lsr_info - get line status register info
1242 * Purpose: Let user call ioctl() to get info when the UART physically
1243 * is emptied. On bus types like RS485, the transmitter must
1244 * release the bus after transmitting. This must be done when
1245 * the transmit shift register is empty, not be done when the
1246 * transmit holding register is empty. This functionality
1247 * allows an RS485 driver to be written in user space.
1249 static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1251 unsigned char status;
1253 cli();
1254 status = read_zsreg(info->zs_channel, 0);
1255 sti();
1256 put_user(status,value);
1257 return 0;
1260 static int get_modem_info(struct mac_serial *info, unsigned int *value)
1262 unsigned char control, status;
1263 unsigned int result;
1265 cli();
1266 control = info->curregs[5];
1267 status = read_zsreg(info->zs_channel, 0);
1268 sti();
1269 result = ((control & RTS) ? TIOCM_RTS: 0)
1270 | ((control & DTR) ? TIOCM_DTR: 0)
1271 | ((status & DCD) ? TIOCM_CAR: 0)
1272 | ((status & CTS) ? 0: TIOCM_CTS);
1273 put_user(result,value);
1274 return 0;
1277 static int set_modem_info(struct mac_serial *info, unsigned int cmd,
1278 unsigned int *value)
1280 int error;
1281 unsigned int arg, bits;
1283 error = verify_area(VERIFY_READ, value, sizeof(int));
1284 if (error)
1285 return error;
1286 get_user(arg, value);
1287 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1288 cli();
1289 switch (cmd) {
1290 case TIOCMBIS:
1291 info->curregs[5] |= bits;
1292 break;
1293 case TIOCMBIC:
1294 info->curregs[5] &= ~bits;
1295 break;
1296 case TIOCMSET:
1297 info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits;
1298 break;
1299 default:
1300 sti();
1301 return -EINVAL;
1303 info->pendregs[5] = info->curregs[5];
1304 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1305 sti();
1306 return 0;
1310 * rs_break - turn transmit break condition on/off
1312 static void rs_break(struct tty_struct *tty, int break_state)
1314 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1315 unsigned long flags;
1317 if (serial_paranoia_check(info, tty->device, "rs_break"))
1318 return;
1319 if (!info->port)
1320 return;
1322 save_flags(flags); cli();
1323 if (break_state == -1)
1324 info->curregs[5] |= SND_BRK;
1325 else
1326 info->curregs[5] &= ~SND_BRK;
1327 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1328 restore_flags(flags);
1331 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1332 unsigned int cmd, unsigned long arg)
1334 int error;
1335 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1337 #ifdef CONFIG_KGDB
1338 if (info->kgdb_channel)
1339 return -ENODEV;
1340 #endif
1341 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1342 return -ENODEV;
1344 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1345 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1346 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1347 if (tty->flags & (1 << TTY_IO_ERROR))
1348 return -EIO;
1351 switch (cmd) {
1352 case TIOCMGET:
1353 error = verify_area(VERIFY_WRITE, (void *) arg,
1354 sizeof(unsigned int));
1355 if (error)
1356 return error;
1357 return get_modem_info(info, (unsigned int *) arg);
1358 case TIOCMBIS:
1359 case TIOCMBIC:
1360 case TIOCMSET:
1361 return set_modem_info(info, cmd, (unsigned int *) arg);
1362 case TIOCGSERIAL:
1363 error = verify_area(VERIFY_WRITE, (void *) arg,
1364 sizeof(struct serial_struct));
1365 if (error)
1366 return error;
1367 return get_serial_info(info,
1368 (struct serial_struct *) arg);
1369 case TIOCSSERIAL:
1370 return set_serial_info(info,
1371 (struct serial_struct *) arg);
1372 case TIOCSERGETLSR: /* Get line status register */
1373 error = verify_area(VERIFY_WRITE, (void *) arg,
1374 sizeof(unsigned int));
1375 if (error)
1376 return error;
1377 else
1378 return get_lsr_info(info, (unsigned int *) arg);
1380 case TIOCSERGSTRUCT:
1381 error = verify_area(VERIFY_WRITE, (void *) arg,
1382 sizeof(struct mac_serial));
1383 if (error)
1384 return error;
1385 copy_from_user((struct mac_serial *) arg,
1386 info, sizeof(struct mac_serial));
1387 return 0;
1389 default:
1390 return -ENOIOCTLCMD;
1392 return 0;
1395 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1397 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1398 int was_stopped;
1400 if (tty->termios->c_cflag == old_termios->c_cflag)
1401 return;
1402 was_stopped = info->tx_stopped;
1404 change_speed(info, old_termios);
1406 if (was_stopped && !info->tx_stopped) {
1407 tty->hw_stopped = 0;
1408 rs_start(tty);
1413 * ------------------------------------------------------------
1414 * rs_close()
1416 * This routine is called when the serial port gets closed.
1417 * Wait for the last remaining data to be sent.
1418 * ------------------------------------------------------------
1420 static void rs_close(struct tty_struct *tty, struct file * filp)
1422 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1423 unsigned long flags;
1425 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1426 return;
1428 save_flags(flags); cli();
1430 if (tty_hung_up_p(filp)) {
1431 restore_flags(flags);
1432 return;
1435 #ifdef SERIAL_DEBUG_OPEN
1436 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1437 #endif
1438 if ((tty->count == 1) && (info->count != 1)) {
1440 * Uh, oh. tty->count is 1, which means that the tty
1441 * structure will be freed. Info->count should always
1442 * be one in these conditions. If it's greater than
1443 * one, we've got real problems, since it means the
1444 * serial port won't be shutdown.
1446 printk("rs_close: bad serial port count; tty->count is 1, "
1447 "info->count is %d\n", info->count);
1448 info->count = 1;
1450 if (--info->count < 0) {
1451 printk("rs_close: bad serial port count for ttys%d: %d\n",
1452 info->line, info->count);
1453 info->count = 0;
1455 if (info->count) {
1456 restore_flags(flags);
1457 return;
1459 info->flags |= ZILOG_CLOSING;
1461 * Save the termios structure, since this port may have
1462 * separate termios for callout and dialin.
1464 if (info->flags & ZILOG_NORMAL_ACTIVE)
1465 info->normal_termios = *tty->termios;
1466 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1467 info->callout_termios = *tty->termios;
1469 * Now we wait for the transmit buffer to clear; and we notify
1470 * the line discipline to only process XON/XOFF characters.
1472 #ifdef SERIAL_DEBUG_OPEN
1473 printk("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1474 #endif
1475 tty->closing = 1;
1476 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1477 tty_wait_until_sent(tty, info->closing_wait);
1479 * At this point we stop accepting input. To do this, we
1480 * disable the receiver and receive interrupts.
1482 info->curregs[3] &= ~RxENABLE;
1483 info->pendregs[3] = info->curregs[3];
1484 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1485 info->curregs[1] &= ~(0x18); /* disable any rx ints */
1486 info->pendregs[1] = info->curregs[1];
1487 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1488 ZS_CLEARFIFO(info->zs_channel);
1489 if (info->flags & ZILOG_INITIALIZED) {
1491 * Before we drop DTR, make sure the SCC transmitter
1492 * has completely drained.
1494 #ifdef SERIAL_DEBUG_OPEN
1495 printk("waiting end of Rx...\n");
1496 #endif
1497 rs_wait_until_sent(tty, info->timeout);
1500 shutdown(info);
1501 /* restore flags now since shutdown() will have disabled this port's
1502 specific irqs */
1503 restore_flags(flags);
1505 if (tty->driver.flush_buffer)
1506 tty->driver.flush_buffer(tty);
1507 if (tty->ldisc.flush_buffer)
1508 tty->ldisc.flush_buffer(tty);
1509 tty->closing = 0;
1510 info->event = 0;
1511 info->tty = 0;
1513 if (info->blocked_open) {
1514 if (info->close_delay) {
1515 current->state = TASK_INTERRUPTIBLE;
1516 schedule_timeout(info->close_delay);
1518 wake_up_interruptible(&info->open_wait);
1520 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1521 ZILOG_CLOSING);
1522 wake_up_interruptible(&info->close_wait);
1526 * rs_wait_until_sent() --- wait until the transmitter is empty
1528 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1530 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1531 unsigned long orig_jiffies, char_time;
1533 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1534 return;
1536 /* printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
1537 timeout, tty->stopped, info->tx_stopped);
1539 orig_jiffies = jiffies;
1541 * Set the check interval to be 1/5 of the estimated time to
1542 * send a single character, and make it at least 1. The check
1543 * interval should also be less than the timeout.
1545 if (info->timeout <= HZ/50) {
1546 printk("macserial: invalid info->timeout=%d\n", info->timeout);
1547 info->timeout = HZ/50+1;
1550 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1551 char_time = char_time / 5;
1552 if (char_time > HZ) {
1553 printk("macserial: char_time %ld >HZ !!!\n", char_time);
1554 char_time = 1;
1555 } else if (char_time == 0)
1556 char_time = 1;
1557 if (timeout)
1558 char_time = MIN(char_time, timeout);
1559 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1560 current->state = TASK_INTERRUPTIBLE;
1561 schedule_timeout(char_time);
1562 if (signal_pending(current))
1563 break;
1564 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1565 break;
1567 current->state = TASK_RUNNING;
1571 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1573 static void rs_hangup(struct tty_struct *tty)
1575 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1577 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1578 return;
1580 rs_flush_buffer(tty);
1581 shutdown(info);
1582 info->event = 0;
1583 info->count = 0;
1584 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1585 info->tty = 0;
1586 wake_up_interruptible(&info->open_wait);
1590 * ------------------------------------------------------------
1591 * rs_open() and friends
1592 * ------------------------------------------------------------
1594 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1595 struct mac_serial *info)
1597 DECLARE_WAITQUEUE(wait,current);
1598 int retval;
1599 int do_clocal = 0;
1602 * If the device is in the middle of being closed, then block
1603 * until it's done, and then try again.
1605 if (info->flags & ZILOG_CLOSING) {
1606 interruptible_sleep_on(&info->close_wait);
1607 #ifdef SERIAL_DO_RESTART
1608 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1609 -EAGAIN : -ERESTARTSYS);
1610 #else
1611 return -EAGAIN;
1612 #endif
1616 * If this is a callout device, then just make sure the normal
1617 * device isn't being used.
1619 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1620 if (info->flags & ZILOG_NORMAL_ACTIVE)
1621 return -EBUSY;
1622 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1623 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1624 (info->session != current->session))
1625 return -EBUSY;
1626 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1627 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1628 (info->pgrp != current->pgrp))
1629 return -EBUSY;
1630 info->flags |= ZILOG_CALLOUT_ACTIVE;
1631 return 0;
1635 * If non-blocking mode is set, or the port is not enabled,
1636 * then make the check up front and then exit.
1638 if ((filp->f_flags & O_NONBLOCK) ||
1639 (tty->flags & (1 << TTY_IO_ERROR))) {
1640 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1641 return -EBUSY;
1642 info->flags |= ZILOG_NORMAL_ACTIVE;
1643 return 0;
1646 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1647 if (info->normal_termios.c_cflag & CLOCAL)
1648 do_clocal = 1;
1649 } else {
1650 if (tty->termios->c_cflag & CLOCAL)
1651 do_clocal = 1;
1655 * Block waiting for the carrier detect and the line to become
1656 * free (i.e., not in use by the callout). While we are in
1657 * this loop, info->count is dropped by one, so that
1658 * rs_close() knows when to free things. We restore it upon
1659 * exit, either normal or abnormal.
1661 retval = 0;
1662 add_wait_queue(&info->open_wait, &wait);
1663 #ifdef SERIAL_DEBUG_OPEN
1664 printk("block_til_ready before block: ttys%d, count = %d\n",
1665 info->line, info->count);
1666 #endif
1667 cli();
1668 if (!tty_hung_up_p(filp))
1669 info->count--;
1670 sti();
1671 info->blocked_open++;
1672 while (1) {
1673 cli();
1674 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1675 (tty->termios->c_cflag & CBAUD))
1676 zs_rtsdtr(info, 1);
1677 sti();
1678 current->state = TASK_INTERRUPTIBLE;
1679 if (tty_hung_up_p(filp) ||
1680 !(info->flags & ZILOG_INITIALIZED)) {
1681 #ifdef SERIAL_DO_RESTART
1682 if (info->flags & ZILOG_HUP_NOTIFY)
1683 retval = -EAGAIN;
1684 else
1685 retval = -ERESTARTSYS;
1686 #else
1687 retval = -EAGAIN;
1688 #endif
1689 break;
1691 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1692 !(info->flags & ZILOG_CLOSING) &&
1693 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1694 break;
1695 if (signal_pending(current)) {
1696 retval = -ERESTARTSYS;
1697 break;
1699 #ifdef SERIAL_DEBUG_OPEN
1700 printk("block_til_ready blocking: ttys%d, count = %d\n",
1701 info->line, info->count);
1702 #endif
1703 schedule();
1705 current->state = TASK_RUNNING;
1706 remove_wait_queue(&info->open_wait, &wait);
1707 if (!tty_hung_up_p(filp))
1708 info->count++;
1709 info->blocked_open--;
1710 #ifdef SERIAL_DEBUG_OPEN
1711 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1712 info->line, info->count);
1713 #endif
1714 if (retval)
1715 return retval;
1716 info->flags |= ZILOG_NORMAL_ACTIVE;
1717 return 0;
1721 * This routine is called whenever a serial port is opened. It
1722 * enables interrupts for a serial port, linking in its ZILOG structure into
1723 * the IRQ chain. It also performs the serial-specific
1724 * initialization for the tty structure.
1726 static int rs_open(struct tty_struct *tty, struct file * filp)
1728 struct mac_serial *info;
1729 int retval, line;
1731 line = MINOR(tty->device) - tty->driver.minor_start;
1732 if ((line < 0) || (line >= zs_channels_found))
1733 return -ENODEV;
1734 info = zs_soft + line;
1736 #ifdef CONFIG_KGDB
1737 if (info->kgdb_channel)
1738 return -ENODEV;
1739 #endif
1740 if (serial_paranoia_check(info, tty->device, "rs_open"))
1741 return -ENODEV;
1742 #ifdef SERIAL_DEBUG_OPEN
1743 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1744 info->count);
1745 #endif
1747 info->count++;
1748 tty->driver_data = info;
1749 info->tty = tty;
1752 * If the port is the middle of closing, bail out now
1754 if (tty_hung_up_p(filp) ||
1755 (info->flags & ZILOG_CLOSING)) {
1756 if (info->flags & ZILOG_CLOSING)
1757 interruptible_sleep_on(&info->close_wait);
1758 #ifdef SERIAL_DO_RESTART
1759 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1760 -EAGAIN : -ERESTARTSYS);
1761 #else
1762 return -EAGAIN;
1763 #endif
1767 * Start up serial port
1770 retval = startup(info);
1771 if (retval)
1772 return retval;
1774 retval = block_til_ready(tty, filp, info);
1775 if (retval) {
1776 #ifdef SERIAL_DEBUG_OPEN
1777 printk("rs_open returning after block_til_ready with %d\n",
1778 retval);
1779 #endif
1780 return retval;
1783 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1784 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1785 *tty->termios = info->normal_termios;
1786 else
1787 *tty->termios = info->callout_termios;
1788 change_speed(info, 0);
1790 #ifdef CONFIG_SERIAL_CONSOLE
1791 if (sercons.cflag && sercons.index == line) {
1792 tty->termios->c_cflag = sercons.cflag;
1793 sercons.cflag = 0;
1794 change_speed(info, 0);
1796 #endif
1798 info->session = current->session;
1799 info->pgrp = current->pgrp;
1801 #ifdef SERIAL_DEBUG_OPEN
1802 printk("rs_open ttys%d successful...\n", info->line);
1803 #endif
1804 return 0;
1807 /* Finally, routines used to initialize the serial driver. */
1809 static void show_serial_version(void)
1811 printk("PowerMac Z8530 serial driver version 1.01\n");
1814 /* Ask the PROM how many Z8530s we have and initialize their zs_channels */
1815 static void
1816 probe_sccs()
1818 struct device_node *dev, *ch;
1819 struct mac_serial **pp;
1820 int n, lenp;
1822 n = 0;
1823 pp = &zs_chain;
1824 for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
1825 if (n >= NUM_CHANNELS) {
1826 printk("Sorry, can't use %s: no more channels\n",
1827 dev->full_name);
1828 continue;
1830 for (ch = dev->child; ch != 0; ch = ch->sibling) {
1831 if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
1832 printk("Can't use %s: %d addrs %d intrs\n",
1833 ch->full_name, ch->n_addrs, ch->n_intrs);
1834 continue;
1836 zs_channels[n].control = (volatile unsigned char *)
1837 ioremap(ch->addrs[0].address, 0x1000);
1838 zs_channels[n].data = zs_channels[n].control + 0x10;
1839 spin_lock_init(&zs_channels[n].lock);
1840 zs_soft[n].zs_channel = &zs_channels[n];
1841 zs_soft[n].dev_node = ch;
1842 zs_soft[n].irq = ch->intrs[0].line;
1843 zs_soft[n].zs_channel->parent = &zs_soft[n];
1844 zs_soft[n].is_cobalt_modem = device_is_compatible(ch, "cobalt");
1846 /* XXX tested only with wallstreet PowerBook, should do no harm anyway */
1847 zs_soft[n].is_pwbk_ir = (strcmp(get_property(ch, "AAPL,connector",
1848 &lenp), "infrared") == 0);
1850 /* XXX this assumes the prom puts chan A before B */
1851 if (n & 1)
1852 zs_soft[n].zs_chan_a = &zs_channels[n-1];
1853 else
1854 zs_soft[n].zs_chan_a = &zs_channels[n];
1856 *pp = &zs_soft[n];
1857 pp = &zs_soft[n].zs_next;
1858 ++n;
1861 *pp = 0;
1862 zs_channels_found = n;
1865 /* rs_init inits the driver */
1866 int macserial_init(void)
1868 int channel, i;
1869 unsigned long flags;
1870 struct mac_serial *info;
1872 /* Setup base handler, and timer table. */
1873 init_bh(MACSERIAL_BH, do_serial_bh);
1874 timer_table[RS_TIMER].fn = rs_timer;
1875 timer_table[RS_TIMER].expires = 0;
1877 /* Find out how many Z8530 SCCs we have */
1878 if (zs_chain == 0)
1879 probe_sccs();
1881 /* XXX assume it's a powerbook if we have a via-pmu */
1882 is_powerbook = find_devices("via-pmu") != 0;
1884 /* Register the interrupt handler for each one */
1885 save_flags(flags); cli();
1886 for (i = 0; i < zs_channels_found; ++i) {
1887 if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
1888 "SCC", &zs_soft[i]))
1889 printk(KERN_ERR "macserial: can't get irq %d\n",
1890 zs_soft[i].irq);
1891 disable_irq(zs_soft[i].irq);
1893 restore_flags(flags);
1895 show_serial_version();
1897 /* Initialize the tty_driver structure */
1898 /* Not all of this is exactly right for us. */
1900 memset(&serial_driver, 0, sizeof(struct tty_driver));
1901 serial_driver.magic = TTY_DRIVER_MAGIC;
1902 serial_driver.name = "ttyS";
1903 serial_driver.major = TTY_MAJOR;
1904 serial_driver.minor_start = 64;
1905 serial_driver.num = zs_channels_found;
1906 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1907 serial_driver.subtype = SERIAL_TYPE_NORMAL;
1908 serial_driver.init_termios = tty_std_termios;
1910 serial_driver.init_termios.c_cflag =
1911 B38400 | CS8 | CREAD | HUPCL | CLOCAL;
1912 serial_driver.flags = TTY_DRIVER_REAL_RAW;
1913 serial_driver.refcount = &serial_refcount;
1914 serial_driver.table = serial_table;
1915 serial_driver.termios = serial_termios;
1916 serial_driver.termios_locked = serial_termios_locked;
1918 serial_driver.open = rs_open;
1919 serial_driver.close = rs_close;
1920 serial_driver.write = rs_write;
1921 serial_driver.flush_chars = rs_flush_chars;
1922 serial_driver.write_room = rs_write_room;
1923 serial_driver.chars_in_buffer = rs_chars_in_buffer;
1924 serial_driver.flush_buffer = rs_flush_buffer;
1925 serial_driver.ioctl = rs_ioctl;
1926 serial_driver.throttle = rs_throttle;
1927 serial_driver.unthrottle = rs_unthrottle;
1928 serial_driver.set_termios = rs_set_termios;
1929 serial_driver.stop = rs_stop;
1930 serial_driver.start = rs_start;
1931 serial_driver.hangup = rs_hangup;
1932 serial_driver.break_ctl = rs_break;
1933 serial_driver.wait_until_sent = rs_wait_until_sent;
1936 * The callout device is just like normal device except for
1937 * major number and the subtype code.
1939 callout_driver = serial_driver;
1940 callout_driver.name = "cua";
1941 callout_driver.major = TTYAUX_MAJOR;
1942 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1944 if (tty_register_driver(&serial_driver))
1945 panic("Couldn't register serial driver\n");
1946 if (tty_register_driver(&callout_driver))
1947 panic("Couldn't register callout driver\n");
1949 for (channel = 0; channel < zs_channels_found; ++channel) {
1950 #ifdef CONFIG_KGDB
1951 if (zs_soft[channel].kgdb_channel) {
1952 kgdb_interruptible(1);
1953 continue;
1955 #endif
1956 zs_soft[channel].clk_divisor = 16;
1957 /* -- we are not sure the SCC is powered ON at this point
1958 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1960 zs_soft[channel].zs_baud = 38400;
1962 /* If console serial line, then enable interrupts. */
1963 if (zs_soft[channel].is_cons) {
1964 printk("macserial: console line, enabling interrupt %d\n", zs_soft[channel].irq);
1965 panic("macserial: console not supported yet !");
1966 write_zsreg(zs_soft[channel].zs_channel, R1,
1967 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
1968 write_zsreg(zs_soft[channel].zs_channel, R9,
1969 (NV | MIE));
1973 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
1975 unsigned char* connector;
1976 int lenp;
1978 #ifdef CONFIG_KGDB
1979 if (info->kgdb_channel) {
1980 continue;
1982 #endif
1983 info->magic = SERIAL_MAGIC;
1984 info->port = (int) info->zs_channel->control;
1985 info->line = i;
1986 info->tty = 0;
1987 info->custom_divisor = 16;
1988 info->timeout = 0;
1989 info->close_delay = 50;
1990 info->closing_wait = 3000;
1991 info->x_char = 0;
1992 info->event = 0;
1993 info->count = 0;
1994 info->blocked_open = 0;
1995 info->tqueue.routine = do_softint;
1996 info->tqueue.data = info;
1997 info->callout_termios =callout_driver.init_termios;
1998 info->normal_termios = serial_driver.init_termios;
1999 init_waitqueue_head(&info->open_wait);
2000 init_waitqueue_head(&info->close_wait);
2001 info->timeout = HZ;
2002 printk("tty%02d at 0x%08x (irq = %d)", info->line,
2003 info->port, info->irq);
2004 printk(" is a Z8530 ESCC");
2005 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2006 if (connector)
2007 printk(", port = %s", connector);
2008 if (info->is_cobalt_modem)
2009 printk(" (cobalt modem)");
2010 if (info->is_pwbk_ir)
2011 printk(" (powerbook IR)");
2012 printk("\n");
2014 #ifdef CONFIG_KGDB
2015 if (info->kgdb_channel)
2016 continue;
2017 #endif
2018 #ifdef CONFIG_XMON
2019 if (!info->is_cobalt_modem)
2020 continue;
2021 #endif
2022 /* By default, disable the port */
2023 set_scc_power(info, 0);
2026 return 0;
2029 #if 0
2031 * register_serial and unregister_serial allows for serial ports to be
2032 * configured at run-time, to support PCMCIA modems.
2034 /* PowerMac: Unused at this time, just here to make things link. */
2035 int register_serial(struct serial_struct *req)
2037 return -1;
2040 void unregister_serial(int line)
2042 return;
2044 #endif
2047 * ------------------------------------------------------------
2048 * Serial console driver
2049 * ------------------------------------------------------------
2051 #ifdef CONFIG_SERIAL_CONSOLE
2055 * Print a string to the serial port trying not to disturb
2056 * any possible real use of the port...
2058 static void serial_console_write(struct console *co, const char *s,
2059 unsigned count)
2061 struct mac_serial *info = zs_soft + co->index;
2062 int i;
2064 /* Turn of interrupts and enable the transmitter. */
2065 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2066 write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2068 for (i=0; i<count; i++) {
2069 /* Wait for the transmit buffer to empty. */
2070 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2071 eieio();
2074 write_zsdata(info->zs_channel, s[i]);
2075 if (s[i] == 10) {
2076 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2077 == 0)
2078 eieio();
2080 write_zsdata(info->zs_channel, 13);
2084 /* Restore the values in the registers. */
2085 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2086 /* Don't disable the transmitter. */
2090 * Receive character from the serial port
2092 static int serial_console_wait_key(struct console *co)
2094 struct mac_serial *info = zs_soft + co->index;
2095 int val;
2097 /* Turn of interrupts and enable the transmitter. */
2098 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~INT_ALL_Rx);
2099 write_zsreg(info->zs_channel, R3, info->curregs[3] | RxENABLE);
2101 /* Wait for something in the receive buffer. */
2102 while((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0)
2103 eieio();
2104 val = read_zsdata(info->zs_channel);
2106 /* Restore the values in the registers. */
2107 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2108 write_zsreg(info->zs_channel, R3, info->curregs[3]);
2110 return val;
2113 static kdev_t serial_console_device(struct console *c)
2115 return MKDEV(TTY_MAJOR, 64 + c->index);
2119 * Setup initial baud/bits/parity. We do two things here:
2120 * - construct a cflag setting for the first rs_open()
2121 * - initialize the serial port
2122 * Return non-zero if we didn't find a serial port.
2124 __initfunc(static int serial_console_setup(struct console *co, char *options))
2126 struct mac_serial *info = zs_soft + co->index;
2127 int baud = 38400;
2128 int bits = 8;
2129 int parity = 'n';
2130 int cflag = CREAD | HUPCL | CLOCAL;
2131 int brg;
2132 char *s;
2133 long flags;
2135 /* Find out how many Z8530 SCCs we have */
2136 if (zs_chain == 0)
2137 probe_sccs();
2139 if (zs_chain == 0)
2140 return -1;
2142 /* Reset the channel */
2143 write_zsreg(info->zs_channel, R9, CHRA);
2145 if (options) {
2146 baud = simple_strtoul(options, NULL, 10);
2147 s = options;
2148 while(*s >= '0' && *s <= '9')
2149 s++;
2150 if (*s)
2151 parity = *s++;
2152 if (*s)
2153 bits = *s - '0';
2157 * Now construct a cflag setting.
2159 switch(baud) {
2160 case 1200:
2161 cflag |= B1200;
2162 break;
2163 case 2400:
2164 cflag |= B2400;
2165 break;
2166 case 4800:
2167 cflag |= B4800;
2168 break;
2169 case 9600:
2170 cflag |= B9600;
2171 break;
2172 case 19200:
2173 cflag |= B19200;
2174 break;
2175 case 57600:
2176 cflag |= B57600;
2177 break;
2178 case 115200:
2179 cflag |= B115200;
2180 break;
2181 case 38400:
2182 default:
2183 cflag |= B38400;
2184 break;
2186 switch(bits) {
2187 case 7:
2188 cflag |= CS7;
2189 break;
2190 default:
2191 case 8:
2192 cflag |= CS8;
2193 break;
2195 switch(parity) {
2196 case 'o': case 'O':
2197 cflag |= PARENB | PARODD;
2198 break;
2199 case 'e': case 'E':
2200 cflag |= PARENB;
2201 break;
2203 co->cflag = cflag;
2205 save_flags(flags); cli();
2206 memset(info->curregs, 0, sizeof(info->curregs));
2208 info->zs_baud = baud;
2209 info->clk_divisor = 16;
2210 switch (info->zs_baud) {
2211 case ZS_CLOCK/16: /* 230400 */
2212 info->curregs[4] = X16CLK;
2213 info->curregs[11] = 0;
2214 break;
2215 case ZS_CLOCK/32: /* 115200 */
2216 info->curregs[4] = X32CLK;
2217 info->curregs[11] = 0;
2218 break;
2219 default:
2220 info->curregs[4] = X16CLK;
2221 info->curregs[11] = TCBR | RCBR;
2222 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2223 info->curregs[12] = (brg & 255);
2224 info->curregs[13] = ((brg >> 8) & 255);
2225 info->curregs[14] = BRENABL;
2228 /* byte size and parity */
2229 info->curregs[3] &= ~RxNBITS_MASK;
2230 info->curregs[5] &= ~TxNBITS_MASK;
2231 switch (cflag & CSIZE) {
2232 case CS5:
2233 info->curregs[3] |= Rx5;
2234 info->curregs[5] |= Tx5;
2235 break;
2236 case CS6:
2237 info->curregs[3] |= Rx6;
2238 info->curregs[5] |= Tx6;
2239 break;
2240 case CS7:
2241 info->curregs[3] |= Rx7;
2242 info->curregs[5] |= Tx7;
2243 break;
2244 case CS8:
2245 default: /* defaults to 8 bits */
2246 info->curregs[3] |= Rx8;
2247 info->curregs[5] |= Tx8;
2248 break;
2250 info->curregs[5] |= TxENAB | RTS | DTR;
2251 info->pendregs[3] = info->curregs[3];
2252 info->pendregs[5] = info->curregs[5];
2254 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
2255 if (cflag & CSTOPB) {
2256 info->curregs[4] |= SB2;
2257 } else {
2258 info->curregs[4] |= SB1;
2260 if (cflag & PARENB) {
2261 info->curregs[4] |= PAR_ENA;
2262 if (!(cflag & PARODD)) {
2263 info->curregs[4] |= PAR_EVEN;
2266 info->pendregs[4] = info->curregs[4];
2268 if (!(cflag & CLOCAL)) {
2269 if (!(info->curregs[15] & DCDIE))
2270 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2271 info->curregs[15] |= DCDIE;
2272 } else
2273 info->curregs[15] &= ~DCDIE;
2274 if (cflag & CRTSCTS) {
2275 info->curregs[15] |= CTSIE;
2276 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
2277 info->tx_stopped = 1;
2278 } else {
2279 info->curregs[15] &= ~CTSIE;
2280 info->tx_stopped = 0;
2282 info->pendregs[15] = info->curregs[15];
2284 /* Load up the new values */
2285 load_zsregs(info->zs_channel, info->curregs);
2287 restore_flags(flags);
2289 return 0;
2292 static struct console sercons = {
2293 "ttyS",
2294 serial_console_write,
2295 NULL,
2296 serial_console_device,
2297 serial_console_wait_key,
2298 NULL,
2299 serial_console_setup,
2300 CON_PRINTBUFFER,
2303 NULL
2307 * Register console.
2309 __initfunc (long serial_console_init(long kmem_start, long kmem_end))
2311 register_console(&sercons);
2312 return kmem_start;
2314 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2316 #ifdef CONFIG_KGDB
2317 /* These are for receiving and sending characters under the kgdb
2318 * source level kernel debugger.
2320 void putDebugChar(char kgdb_char)
2322 struct mac_zschannel *chan = zs_kgdbchan;
2323 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2324 udelay(5);
2325 write_zsdata(chan, kgdb_char);
2328 char getDebugChar(void)
2330 struct mac_zschannel *chan = zs_kgdbchan;
2331 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2332 eieio(); /*barrier();*/
2333 return read_zsdata(chan);
2336 void kgdb_interruptible(int yes)
2338 struct mac_zschannel *chan = zs_kgdbchan;
2339 int one, nine;
2340 nine = read_zsreg(chan, 9);
2341 if (yes == 1) {
2342 one = EXT_INT_ENAB|INT_ALL_Rx;
2343 nine |= MIE;
2344 printk("turning serial ints on\n");
2345 } else {
2346 one = RxINT_DISAB;
2347 nine &= ~MIE;
2348 printk("turning serial ints off\n");
2350 write_zsreg(chan, 1, one);
2351 write_zsreg(chan, 9, nine);
2354 /* This sets up the serial port we're using, and turns on
2355 * interrupts for that channel, so kgdb is usable once we're done.
2357 static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
2359 int brg;
2360 int i, x;
2361 volatile char *sccc = ms->control;
2362 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
2363 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2364 for (i = 20000; i != 0; --i) {
2365 x = *sccc; eieio();
2367 for (i = 0; i < sizeof(scc_inittab); ++i) {
2368 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2369 i++;
2373 /* This is called at boot time to prime the kgdb serial debugging
2374 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2375 * for /dev/ttyb which is determined in setup_arch() from the
2376 * boot command line flags.
2377 * XXX at the moment probably only channel A will work
2379 __initfunc(void zs_kgdb_hook(int tty_num))
2381 /* Find out how many Z8530 SCCs we have */
2382 if (zs_chain == 0)
2383 probe_sccs();
2385 set_scc_power(&zs_soft[n], 1);
2387 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2388 zs_soft[tty_num].change_needed = 0;
2389 zs_soft[tty_num].clk_divisor = 16;
2390 zs_soft[tty_num].zs_baud = 38400;
2391 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2392 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2394 /* Turn on transmitter/receiver at 8-bits/char */
2395 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2396 printk("KGDB: on channel %d initialized\n", tty_num);
2397 set_debug_traps(); /* init stub */
2399 #endif /* ifdef CONFIG_KGDB */