Import 2.3.18pre1
[davej-history.git] / drivers / macintosh / macserial.c
blob93fe07774b0616f7fafc46b2d93008a16b3e9c5e
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/module.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/interrupt.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/major.h>
20 #include <linux/string.h>
21 #include <linux/fcntl.h>
22 #include <linux/mm.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #ifdef CONFIG_SERIAL_CONSOLE
27 #include <linux/console.h>
28 #endif
30 #include <asm/init.h>
31 #include <asm/io.h>
32 #include <asm/pgtable.h>
33 #include <asm/irq.h>
34 #include <asm/prom.h>
35 #include <asm/system.h>
36 #include <asm/segment.h>
37 #include <asm/bitops.h>
38 #include <asm/feature.h>
39 #include <asm/adb.h>
40 #include <asm/pmu.h>
41 #ifdef CONFIG_KGDB
42 #include <asm/kgdb.h>
43 #endif
45 #include "macserial.h"
48 * It would be nice to dynamically allocate everything that
49 * depends on NUM_SERIAL, so we could support any number of
50 * Z8530s, but for now...
52 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
53 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
55 /* On PowerMacs, the hardware takes care of the SCC recovery time,
56 but we need the eieio to make sure that the accesses occur
57 in the order we want. */
58 #define RECOVERY_DELAY eieio()
60 struct mac_zschannel zs_channels[NUM_CHANNELS];
62 struct mac_serial zs_soft[NUM_CHANNELS];
63 int zs_channels_found;
64 struct mac_serial *zs_chain; /* list of all channels */
66 struct tty_struct zs_ttys[NUM_CHANNELS];
68 static int is_powerbook;
70 #ifdef CONFIG_SERIAL_CONSOLE
71 static struct console sercons;
72 #endif
74 #ifdef CONFIG_KGDB
75 struct mac_zschannel *zs_kgdbchan;
76 static unsigned char scc_inittab[] = {
77 9, 0x80, /* reset A side (CHRA) */
78 13, 0, /* set baud rate divisor */
79 12, 1,
80 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
81 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
82 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
83 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
84 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
86 #endif
87 #define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
89 static DECLARE_TASK_QUEUE(tq_serial);
91 static struct tty_driver serial_driver, callout_driver;
92 static int serial_refcount;
94 /* serial subtype definitions */
95 #define SERIAL_TYPE_NORMAL 1
96 #define SERIAL_TYPE_CALLOUT 2
98 /* number of characters left in xmit buffer before we ask for more */
99 #define WAKEUP_CHARS 256
102 * Debugging.
104 #undef SERIAL_DEBUG_INTR
105 #undef SERIAL_DEBUG_OPEN
106 #undef SERIAL_DEBUG_FLOW
107 #undef SERIAL_DEBUG_POWER
108 #undef SERIAL_DEBUG_THROTTLE
109 #undef SERIAL_DEBUG_STOP
110 #undef SERIAL_DEBUG_BAUDS
112 #define RS_STROBE_TIME 10
113 #define RS_ISR_PASS_LIMIT 256
115 #define _INLINE_ inline
117 static void probe_sccs(void);
118 static void change_speed(struct mac_serial *info, struct termios *old);
119 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
120 static int set_scc_power(struct mac_serial * info, int state);
121 static int setup_scc(struct mac_serial * info);
123 static struct tty_struct *serial_table[NUM_CHANNELS];
124 static struct termios *serial_termios[NUM_CHANNELS];
125 static struct termios *serial_termios_locked[NUM_CHANNELS];
127 #ifndef MIN
128 #define MIN(a,b) ((a) < (b) ? (a) : (b))
129 #endif
132 * tmp_buf is used as a temporary buffer by serial_write. We need to
133 * lock it in case the copy_from_user blocks while swapping in a page,
134 * and some other program tries to do a serial write at the same time.
135 * Since the lock will only come under contention when the system is
136 * swapping and available memory is low, it makes sense to share one
137 * buffer across all the serial ports, since it significantly saves
138 * memory if large numbers of serial ports are open.
140 static unsigned char *tmp_buf;
141 static DECLARE_MUTEX(tmp_buf_sem);
144 static inline int __pmac
145 serial_paranoia_check(struct mac_serial *info,
146 dev_t device, const char *routine)
148 #ifdef SERIAL_PARANOIA_CHECK
149 static const char *badmagic =
150 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
151 static const char *badinfo =
152 "Warning: null mac_serial for (%d, %d) in %s\n";
154 if (!info) {
155 printk(badinfo, MAJOR(device), MINOR(device), routine);
156 return 1;
158 if (info->magic != SERIAL_MAGIC) {
159 printk(badmagic, MAJOR(device), MINOR(device), routine);
160 return 1;
162 #endif
163 return 0;
167 * Reading and writing Z8530 registers.
169 static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
170 unsigned char reg)
172 unsigned char retval;
173 unsigned long flags;
176 * We have to make this atomic.
178 spin_lock_irqsave(&channel->lock, flags);
179 if (reg != 0) {
180 *channel->control = reg;
181 RECOVERY_DELAY;
183 retval = *channel->control;
184 RECOVERY_DELAY;
185 spin_unlock_irqrestore(&channel->lock, flags);
186 return retval;
189 static inline void __pmac write_zsreg(struct mac_zschannel *channel,
190 unsigned char reg, unsigned char value)
192 unsigned long flags;
194 spin_lock_irqsave(&channel->lock, flags);
195 if (reg != 0) {
196 *channel->control = reg;
197 RECOVERY_DELAY;
199 *channel->control = value;
200 RECOVERY_DELAY;
201 spin_unlock_irqrestore(&channel->lock, flags);
202 return;
205 static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
207 unsigned char retval;
209 retval = *channel->data;
210 RECOVERY_DELAY;
211 return retval;
214 static inline void write_zsdata(struct mac_zschannel *channel,
215 unsigned char value)
217 *channel->data = value;
218 RECOVERY_DELAY;
219 return;
222 static inline void load_zsregs(struct mac_zschannel *channel,
223 unsigned char *regs)
225 ZS_CLEARERR(channel);
226 ZS_CLEARFIFO(channel);
227 /* Load 'em up */
228 write_zsreg(channel, R4, regs[R4]);
229 write_zsreg(channel, R10, regs[R10]);
230 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
231 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
232 write_zsreg(channel, R1, regs[R1]);
233 write_zsreg(channel, R9, regs[R9]);
234 write_zsreg(channel, R11, regs[R11]);
235 write_zsreg(channel, R12, regs[R12]);
236 write_zsreg(channel, R13, regs[R13]);
237 write_zsreg(channel, R14, regs[R14]);
238 write_zsreg(channel, R15, regs[R15]);
239 write_zsreg(channel, R3, regs[R3]);
240 write_zsreg(channel, R5, regs[R5]);
241 return;
244 /* Sets or clears DTR/RTS on the requested line */
245 static inline void zs_rtsdtr(struct mac_serial *ss, int set)
247 if (set)
248 ss->curregs[5] |= (RTS | DTR);
249 else
250 ss->curregs[5] &= ~(RTS | DTR);
251 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
252 return;
255 /* Utility routines for the Zilog */
256 static inline int get_zsbaud(struct mac_serial *ss)
258 struct mac_zschannel *channel = ss->zs_channel;
259 int brg;
261 if ((ss->curregs[R11] & TCBR) == 0) {
262 /* higher rates don't use the baud rate generator */
263 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
265 /* The baud rate is split up between two 8-bit registers in
266 * what is termed 'BRG time constant' format in my docs for
267 * the chip, it is a function of the clk rate the chip is
268 * receiving which happens to be constant.
270 brg = (read_zsreg(channel, 13) << 8);
271 brg |= read_zsreg(channel, 12);
272 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
275 /* On receive, this clears errors and the receiver interrupts */
276 static inline void rs_recv_clear(struct mac_zschannel *zsc)
278 write_zsreg(zsc, 0, ERR_RES);
279 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
283 * ----------------------------------------------------------------------
285 * Here starts the interrupt handling routines. All of the following
286 * subroutines are declared as inline and are folded into
287 * rs_interrupt(). They were separated out for readability's sake.
289 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
290 * -----------------------------------------------------------------------
294 * This routine is used by the interrupt handler to schedule
295 * processing in the software interrupt portion of the driver.
297 static _INLINE_ void rs_sched_event(struct mac_serial *info,
298 int event)
300 info->event |= 1 << event;
301 queue_task(&info->tqueue, &tq_serial);
302 mark_bh(MACSERIAL_BH);
305 static _INLINE_ void receive_chars(struct mac_serial *info,
306 struct pt_regs *regs)
308 struct tty_struct *tty = info->tty;
309 unsigned char ch, stat, flag;
311 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
313 stat = read_zsreg(info->zs_channel, R1);
314 ch = read_zsdata(info->zs_channel);
316 #ifdef CONFIG_KGDB
317 if (info->kgdb_channel) {
318 if (ch == 0x03 || ch == '$')
319 breakpoint();
320 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
321 write_zsreg(info->zs_channel, 0, ERR_RES);
322 return;
324 #endif
325 if (!tty)
326 continue;
327 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
328 tty_flip_buffer_push(tty);
330 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
331 static int flip_buf_ovf;
332 if (++flip_buf_ovf <= 1)
333 printk("FB. overflow: %d\n", flip_buf_ovf);
334 break;
336 tty->flip.count++;
338 static int flip_max_cnt;
339 if (flip_max_cnt < tty->flip.count)
340 flip_max_cnt = tty->flip.count;
342 if (stat & Rx_OVR) {
343 flag = TTY_OVERRUN;
344 } else if (stat & FRM_ERR) {
345 flag = TTY_FRAME;
346 } else if (stat & PAR_ERR) {
347 flag = TTY_PARITY;
348 } else
349 flag = 0;
350 if (flag)
351 /* reset the error indication */
352 write_zsreg(info->zs_channel, 0, ERR_RES);
353 *tty->flip.flag_buf_ptr++ = flag;
354 *tty->flip.char_buf_ptr++ = ch;
356 if (tty)
357 tty_flip_buffer_push(tty);
360 static void transmit_chars(struct mac_serial *info)
362 unsigned long flags;
364 save_flags(flags);
365 cli();
366 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
367 goto out;
368 info->tx_active = 0;
370 if (info->x_char) {
371 /* Send next char */
372 write_zsdata(info->zs_channel, info->x_char);
373 info->x_char = 0;
374 info->tx_active = 1;
375 goto out;
378 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped) {
379 write_zsreg(info->zs_channel, 0, RES_Tx_P);
380 goto out;
383 /* Send char */
384 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
385 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
386 info->xmit_cnt--;
387 info->tx_active = 1;
389 if (info->xmit_cnt < WAKEUP_CHARS)
390 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
392 out:
393 restore_flags(flags);
396 static _INLINE_ void status_handle(struct mac_serial *info)
398 unsigned char status;
400 /* Get status from Read Register 0 */
401 status = read_zsreg(info->zs_channel, 0);
403 /* Check for DCD transitions */
404 if (((status ^ info->read_reg_zero) & DCD) != 0
405 && info->tty && !C_CLOCAL(info->tty)) {
406 if (status & DCD) {
407 wake_up_interruptible(&info->open_wait);
408 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
409 if (info->tty)
410 tty_hangup(info->tty);
414 /* Check for CTS transitions */
415 if (info->tty && C_CRTSCTS(info->tty)) {
417 * For some reason, on the Power Macintosh,
418 * it seems that the CTS bit is 1 when CTS is
419 * *negated* and 0 when it is asserted.
420 * The DCD bit doesn't seem to be inverted
421 * like this.
423 if ((status & CTS) == 0) {
424 if (info->tx_stopped) {
425 #ifdef SERIAL_DEBUG_FLOW
426 printk("CTS up\n");
427 #endif
428 info->tx_stopped = 0;
429 if (!info->tx_active)
430 transmit_chars(info);
432 } else {
433 #ifdef SERIAL_DEBUG_FLOW
434 printk("CTS down\n");
435 #endif
436 info->tx_stopped = 1;
440 /* Clear status condition... */
441 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
442 info->read_reg_zero = status;
446 * This is the serial driver's generic interrupt routine
448 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
450 struct mac_serial *info = (struct mac_serial *) dev_id;
451 unsigned char zs_intreg;
452 int shift;
454 /* NOTE: The read register 3, which holds the irq status,
455 * does so for both channels on each chip. Although
456 * the status value itself must be read from the A
457 * channel and is only valid when read from channel A.
458 * Yes... broken hardware...
460 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
462 if (info->zs_chan_a == info->zs_channel)
463 shift = 3; /* Channel A */
464 else
465 shift = 0; /* Channel B */
467 for (;;) {
468 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
469 #ifdef SERIAL_DEBUG_INTR
470 printk("rs_interrupt: irq %d, zs_intreg 0x%x\n", irq, (int)zs_intreg);
471 #endif
473 if ((zs_intreg & CHAN_IRQMASK) == 0)
474 break;
476 if (!(info->flags & ZILOG_INITIALIZED)) {
477 printk("rs_interrupt: irq %d, port not initialized\n", irq);
478 break;
481 if (zs_intreg & CHBRxIP)
482 receive_chars(info, regs);
483 if (zs_intreg & CHBTxIP)
484 transmit_chars(info);
485 if (zs_intreg & CHBEXT)
486 status_handle(info);
491 * -------------------------------------------------------------------
492 * Here ends the serial interrupt routines.
493 * -------------------------------------------------------------------
497 * ------------------------------------------------------------
498 * rs_stop() and rs_start()
500 * This routines are called before setting or resetting tty->stopped.
501 * ------------------------------------------------------------
503 static void rs_stop(struct tty_struct *tty)
505 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
507 #ifdef SERIAL_DEBUG_STOP
508 printk("rs_stop %ld....\n",
509 tty->ldisc.chars_in_buffer(tty));
510 #endif
512 if (serial_paranoia_check(info, tty->device, "rs_stop"))
513 return;
515 #if 0
516 save_flags(flags); cli();
517 if (info->curregs[5] & TxENAB) {
518 info->curregs[5] &= ~TxENAB;
519 info->pendregs[5] &= ~TxENAB;
520 write_zsreg(info->zs_channel, 5, info->curregs[5]);
522 restore_flags(flags);
523 #endif
526 static void rs_start(struct tty_struct *tty)
528 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
529 unsigned long flags;
531 #ifdef SERIAL_DEBUG_STOP
532 printk("rs_start %ld....\n",
533 tty->ldisc.chars_in_buffer(tty));
534 #endif
536 if (serial_paranoia_check(info, tty->device, "rs_start"))
537 return;
539 save_flags(flags); cli();
540 #if 0
541 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
542 info->curregs[5] |= TxENAB;
543 info->pendregs[5] = info->curregs[5];
544 write_zsreg(info->zs_channel, 5, info->curregs[5]);
546 #else
547 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
548 transmit_chars(info);
550 #endif
551 restore_flags(flags);
555 * This routine is used to handle the "bottom half" processing for the
556 * serial driver, known also the "software interrupt" processing.
557 * This processing is done at the kernel interrupt level, after the
558 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
559 * is where time-consuming activities which can not be done in the
560 * interrupt driver proper are done; the interrupt driver schedules
561 * them using rs_sched_event(), and they get done here.
563 static void do_serial_bh(void)
565 run_task_queue(&tq_serial);
568 static void do_softint(void *private_)
570 struct mac_serial *info = (struct mac_serial *) private_;
571 struct tty_struct *tty;
573 tty = info->tty;
574 if (!tty)
575 return;
577 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
578 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
579 tty->ldisc.write_wakeup)
580 (tty->ldisc.write_wakeup)(tty);
581 wake_up_interruptible(&tty->write_wait);
585 static void rs_timer(void)
589 static int startup(struct mac_serial * info, int can_sleep)
591 int delay;
593 #ifdef SERIAL_DEBUG_OPEN
594 printk("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
595 #endif
597 if (info->flags & ZILOG_INITIALIZED) {
598 #ifdef SERIAL_DEBUG_OPEN
599 printk(" -> already inited\n");
600 #endif
601 return 0;
604 if (!info->xmit_buf) {
605 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
606 if (!info->xmit_buf)
607 return -ENOMEM;
610 #ifdef SERIAL_DEBUG_OPEN
611 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
612 #endif
614 delay = set_scc_power(info, 1);
616 setup_scc(info);
618 #ifdef SERIAL_DEBUG_OPEN
619 printk("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
620 #endif
622 info->flags |= ZILOG_INITIALIZED;
623 enable_irq(info->irq);
625 if (delay) {
626 if (can_sleep) {
627 /* we need to wait a bit before using the port */
628 current->state = TASK_INTERRUPTIBLE;
629 schedule_timeout(delay * HZ / 1000);
630 } else
631 mdelay(delay);
634 return 0;
637 static int setup_scc(struct mac_serial * info)
639 unsigned long flags;
641 #ifdef SERIAL_DEBUG_OPEN
642 printk("setting up ttys%d SCC...\n", info->line);
643 #endif
645 save_flags(flags); cli(); /* Disable interrupts */
648 * Reset the chip.
650 write_zsreg(info->zs_channel, 9,
651 (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
652 udelay(10);
653 write_zsreg(info->zs_channel, 9, 0);
656 * Clear the receive FIFO.
658 ZS_CLEARFIFO(info->zs_channel);
659 info->xmit_fifo_size = 1;
662 * Clear the interrupt registers.
664 write_zsreg(info->zs_channel, 0, ERR_RES);
665 write_zsreg(info->zs_channel, 0, RES_H_IUS);
668 * Turn on RTS and DTR.
670 zs_rtsdtr(info, 1);
673 * Finally, enable sequencing and interrupts
675 info->curregs[1] = (info->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
676 info->pendregs[1] = info->curregs[1];
677 info->curregs[3] |= (RxENABLE | Rx8);
678 info->pendregs[3] = info->curregs[3];
679 info->curregs[5] |= (TxENAB | Tx8);
680 info->pendregs[5] = info->curregs[5];
681 info->curregs[9] |= (NV | MIE);
682 info->pendregs[9] = info->curregs[9];
683 write_zsreg(info->zs_channel, 3, info->curregs[3]);
684 write_zsreg(info->zs_channel, 5, info->curregs[5]);
685 write_zsreg(info->zs_channel, 9, info->curregs[9]);
687 if (info->tty)
688 clear_bit(TTY_IO_ERROR, &info->tty->flags);
689 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
692 * Set the speed of the serial port
694 change_speed(info, 0);
696 /* Save the current value of RR0 */
697 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
699 restore_flags(flags);
701 return 0;
705 * This routine will shutdown a serial port; interrupts are disabled, and
706 * DTR is dropped if the hangup on close termio flag is on.
708 static void shutdown(struct mac_serial * info)
710 #ifdef SERIAL_DEBUG_OPEN
711 printk("Shutting down serial port %d (irq %d)....\n", info->line,
712 info->irq);
713 #endif
715 if (!(info->flags & ZILOG_INITIALIZED)) {
716 #ifdef SERIAL_DEBUG_OPEN
717 printk("(already shutdown)\n");
718 #endif
720 return;
723 disable_irq(info->irq);
725 info->pendregs[1] = info->curregs[1] = 0;
726 write_zsreg(info->zs_channel, 1, 0); /* no interrupts */
728 info->curregs[3] &= ~RxENABLE;
729 info->pendregs[3] = info->curregs[3];
730 write_zsreg(info->zs_channel, 3, info->curregs[3]);
732 info->curregs[5] &= ~TxENAB;
733 if (!info->tty || C_HUPCL(info->tty))
734 info->curregs[5] &= ~(DTR | RTS);
735 info->pendregs[5] = info->curregs[5];
736 write_zsreg(info->zs_channel, 5, info->curregs[5]);
738 if (info->tty)
739 set_bit(TTY_IO_ERROR, &info->tty->flags);
741 set_scc_power(info, 0);
743 if (info->xmit_buf) {
744 free_page((unsigned long) info->xmit_buf);
745 info->xmit_buf = 0;
748 memset(info->curregs, 0, sizeof(info->curregs));
749 memset(info->curregs, 0, sizeof(info->pendregs));
751 info->flags &= ~ZILOG_INITIALIZED;
755 * Turn power on or off to the SCC and associated stuff
756 * (port drivers, modem, IR port, etc.)
757 * Returns the number of milliseconds we should wait before
758 * trying to use the port.
760 static int set_scc_power(struct mac_serial * info, int state)
762 int delay = 0;
764 if (feature_test(info->dev_node, FEATURE_Serial_enable) < 0)
765 return 0; /* don't have serial power control */
767 /* The timings looks strange but that's the ones MacOS seems
768 to use for the internal modem. I think we can use a lot faster
769 ones, at least whe not using the modem, this should be tested.
771 if (state) {
772 #ifdef SERIAL_DEBUG_POWER
773 printk(KERN_INFO "ttyS%02d: powering up hardware\n", info->line);
774 #endif
775 if (feature_test(info->dev_node, FEATURE_Serial_enable) == 0) {
776 feature_clear(info->dev_node, FEATURE_Serial_reset);
777 mdelay(5);
778 feature_set(info->dev_node, FEATURE_Serial_enable);
780 if (info->zs_chan_a == info->zs_channel)
781 feature_set(info->dev_node, FEATURE_Serial_IO_A);
782 else
783 feature_set(info->dev_node, FEATURE_Serial_IO_B);
784 delay = 1;
786 if (info->is_cobalt_modem){
787 feature_set(info->dev_node, FEATURE_Modem_Reset);
788 mdelay(5);
789 feature_clear(info->dev_node, FEATURE_Modem_Reset);
790 delay = 1000; /* wait for 1s before using */
792 #ifdef CONFIG_PMAC_PBOOK
793 if (info->is_pwbk_ir)
794 pmu_enable_irled(1);
795 #endif /* CONFIG_PMAC_PBOOK */
796 } else {
797 #ifdef SERIAL_DEBUG_POWER
798 printk(KERN_INFO "ttyS%02d: shutting down hardware\n", info->line);
799 #endif
800 #ifdef CONFIG_KGDB
801 if (info->kgdb_channel) {
802 #ifdef SERIAL_DEBUG_POWER
803 printk(KERN_INFO " (canceled by KGDB)\n");
804 #endif
805 return 0;
807 #endif
808 #ifdef CONFIG_XMON
809 if (!info->is_cobalt_modem) {
810 #ifdef SERIAL_DEBUG_POWER
811 printk(KERN_INFO " (canceled by XMON)\n");
812 #endif
813 return 0;
815 #endif
816 if (info->is_cobalt_modem) {
817 #ifdef SERIAL_DEBUG_POWER
818 printk(KERN_INFO "ttyS%02d: shutting down modem\n", info->line);
819 #endif
820 feature_set(info->dev_node, FEATURE_Modem_Reset);
821 mdelay(15);
822 feature_clear(info->dev_node, FEATURE_Modem_Reset);
823 mdelay(25);
825 #ifdef CONFIG_PMAC_PBOOK
826 if (info->is_pwbk_ir)
827 pmu_enable_irled(0);
828 #endif /* CONFIG_PMAC_PBOOK */
830 if (info->zs_chan_a == info->zs_channel) {
831 #ifdef SERIAL_DEBUG_POWER
832 printk(KERN_INFO "ttyS%02d: shutting down SCC channel A\n", info->line);
833 #endif
834 feature_clear(info->dev_node, FEATURE_Serial_IO_A);
835 } else {
836 #ifdef SERIAL_DEBUG_POWER
837 printk(KERN_INFO "ttyS%02d: shutting down SCC channel B\n", info->line);
838 #endif
839 feature_clear(info->dev_node, FEATURE_Serial_IO_B);
841 /* XXX for now, shut down SCC core only on powerbooks */
842 if (is_powerbook
843 && !(feature_test(info->dev_node, FEATURE_Serial_IO_A) ||
844 feature_test(info->dev_node, FEATURE_Serial_IO_B))) {
845 #ifdef SERIAL_DEBUG_POWER
846 printk(KERN_INFO "ttyS%02d: shutting down SCC core\n", info->line);
847 #endif
848 feature_set(info->dev_node, FEATURE_Serial_reset);
849 mdelay(10);
850 feature_clear(info->dev_node, FEATURE_Serial_enable);
851 mdelay(5);
854 return delay;
859 * This routine is called to set the UART divisor registers to match
860 * the specified baud rate for a serial port.
862 static void change_speed(struct mac_serial *info, struct termios *old_termios)
864 unsigned short port;
865 unsigned cflag;
866 int bits;
867 int brg, baud;
868 unsigned long flags;
870 if (!info->tty || !info->tty->termios)
871 return;
872 if (!(port = info->port))
873 return;
875 cflag = info->tty->termios->c_cflag;
876 baud = tty_get_baud_rate(info->tty);
877 if (baud == 0) {
878 if (old_termios) {
879 info->tty->termios->c_cflag &= ~CBAUD;
880 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
881 cflag = info->tty->termios->c_cflag;
882 baud = tty_get_baud_rate(info->tty);
884 else
885 baud = info->zs_baud;
887 if (baud > 230400)
888 baud = 230400;
889 else if (baud == 0)
890 baud = 38400;
892 save_flags(flags); cli();
893 info->zs_baud = baud;
894 info->clk_divisor = 16;
896 #ifdef SERIAL_DEBUG_BAUDS
897 printk("set speed to %d bds, ", baud);
898 #endif
900 switch (baud) {
901 case ZS_CLOCK/16: /* 230400 */
902 info->curregs[4] = X16CLK;
903 info->curregs[11] = 0;
904 break;
905 case ZS_CLOCK/32: /* 115200 */
906 info->curregs[4] = X32CLK;
907 info->curregs[11] = 0;
908 break;
909 default:
910 info->curregs[4] = X16CLK;
911 info->curregs[11] = TCBR | RCBR;
912 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
913 info->curregs[12] = (brg & 255);
914 info->curregs[13] = ((brg >> 8) & 255);
915 info->curregs[14] = BRENABL;
918 /* byte size and parity */
919 info->curregs[3] &= ~RxNBITS_MASK;
920 info->curregs[5] &= ~TxNBITS_MASK;
921 switch (cflag & CSIZE) {
922 case CS5:
923 info->curregs[3] |= Rx5;
924 info->curregs[5] |= Tx5;
925 #ifdef SERIAL_DEBUG_BAUDS
926 printk("5 bits, ");
927 #endif
928 bits = 7;
929 break;
930 case CS6:
931 info->curregs[3] |= Rx6;
932 info->curregs[5] |= Tx6;
933 #ifdef SERIAL_DEBUG_BAUDS
934 printk("6 bits, ");
935 #endif
936 bits = 8;
937 break;
938 case CS7:
939 info->curregs[3] |= Rx7;
940 info->curregs[5] |= Tx7;
941 #ifdef SERIAL_DEBUG_BAUDS
942 printk("7 bits, ");
943 #endif
944 bits = 9;
945 break;
946 case CS8:
947 default: /* defaults to 8 bits */
948 info->curregs[3] |= Rx8;
949 info->curregs[5] |= Tx8;
950 #ifdef SERIAL_DEBUG_BAUDS
951 printk("8 bits, ");
952 #endif
953 bits = 10;
954 break;
956 info->pendregs[3] = info->curregs[3];
957 info->pendregs[5] = info->curregs[5];
959 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
960 if (cflag & CSTOPB) {
961 info->curregs[4] |= SB2;
962 bits++;
963 #ifdef SERIAL_DEBUG_BAUDS
964 printk("2 stop, ");
965 #endif
966 } else {
967 info->curregs[4] |= SB1;
968 #ifdef SERIAL_DEBUG_BAUDS
969 printk("1 stop, ");
970 #endif
972 if (cflag & PARENB) {
973 bits++;
974 info->curregs[4] |= PAR_ENA;
975 #ifdef SERIAL_DEBUG_BAUDS
976 printk("parity, ");
977 #endif
979 if (!(cflag & PARODD)) {
980 info->curregs[4] |= PAR_EVEN;
982 info->pendregs[4] = info->curregs[4];
984 if (!(cflag & CLOCAL)) {
985 if (!(info->curregs[15] & DCDIE))
986 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
987 info->curregs[15] |= DCDIE;
988 } else
989 info->curregs[15] &= ~DCDIE;
990 if (cflag & CRTSCTS) {
991 info->curregs[15] |= CTSIE;
992 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
993 info->tx_stopped = 1;
994 } else {
995 info->curregs[15] &= ~CTSIE;
996 info->tx_stopped = 0;
998 info->pendregs[15] = info->curregs[15];
1000 /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
1001 This code would love a larger HZ and a >1 fifo size, but this is not
1002 a priority. The resulting value must be >HZ/2
1004 info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
1005 info->timeout += HZ/50+1; /* Add .02 seconds of slop */
1007 #ifdef SERIAL_DEBUG_BAUDS
1008 printk("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ, (int)info->baud_base);
1009 #endif
1011 /* Load up the new values */
1012 load_zsregs(info->zs_channel, info->curregs);
1014 restore_flags(flags);
1017 static void rs_flush_chars(struct tty_struct *tty)
1019 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1021 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1022 return;
1024 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
1025 !info->xmit_buf)
1026 return;
1028 /* Enable transmitter */
1029 transmit_chars(info);
1032 static int rs_write(struct tty_struct * tty, int from_user,
1033 const unsigned char *buf, int count)
1035 int c, ret = 0;
1036 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1037 unsigned long flags;
1039 if (serial_paranoia_check(info, tty->device, "rs_write"))
1040 return 0;
1042 if (!tty || !info->xmit_buf || !tmp_buf)
1043 return 0;
1045 save_flags(flags);
1046 if (from_user) {
1047 down(&tmp_buf_sem);
1048 while (1) {
1049 c = MIN(count,
1050 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1051 SERIAL_XMIT_SIZE - info->xmit_head));
1052 if (c <= 0)
1053 break;
1055 c -= copy_from_user(tmp_buf, buf, c);
1056 if (!c) {
1057 if (!ret)
1058 ret = -EFAULT;
1059 break;
1061 cli();
1062 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1063 SERIAL_XMIT_SIZE - info->xmit_head));
1064 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1065 info->xmit_head = ((info->xmit_head + c) &
1066 (SERIAL_XMIT_SIZE-1));
1067 info->xmit_cnt += c;
1068 restore_flags(flags);
1069 buf += c;
1070 count -= c;
1071 ret += c;
1073 up(&tmp_buf_sem);
1074 } else {
1075 while (1) {
1076 cli();
1077 c = MIN(count,
1078 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1079 SERIAL_XMIT_SIZE - info->xmit_head));
1080 if (c <= 0) {
1081 restore_flags(flags);
1082 break;
1084 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1085 info->xmit_head = ((info->xmit_head + c) &
1086 (SERIAL_XMIT_SIZE-1));
1087 info->xmit_cnt += c;
1088 restore_flags(flags);
1089 buf += c;
1090 count -= c;
1091 ret += c;
1094 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1095 && !info->tx_active)
1096 transmit_chars(info);
1097 restore_flags(flags);
1098 return ret;
1101 static int rs_write_room(struct tty_struct *tty)
1103 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1104 int ret;
1106 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1107 return 0;
1108 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1109 if (ret < 0)
1110 ret = 0;
1111 return ret;
1114 static int rs_chars_in_buffer(struct tty_struct *tty)
1116 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1118 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1119 return 0;
1120 return info->xmit_cnt;
1123 static void rs_flush_buffer(struct tty_struct *tty)
1125 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1127 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1128 return;
1129 cli();
1130 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1131 sti();
1132 wake_up_interruptible(&tty->write_wait);
1133 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1134 tty->ldisc.write_wakeup)
1135 (tty->ldisc.write_wakeup)(tty);
1139 * ------------------------------------------------------------
1140 * rs_throttle()
1142 * This routine is called by the upper-layer tty layer to signal that
1143 * incoming characters should be throttled.
1144 * ------------------------------------------------------------
1146 static void rs_throttle(struct tty_struct * tty)
1148 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1149 unsigned long flags;
1150 #ifdef SERIAL_DEBUG_THROTTLE
1151 char buf[64];
1153 printk("throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1154 #endif
1156 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1157 return;
1159 if (I_IXOFF(tty)) {
1160 save_flags(flags); cli();
1161 info->x_char = STOP_CHAR(tty);
1162 if (!info->tx_active)
1163 transmit_chars(info);
1164 restore_flags(flags);
1167 if (C_CRTSCTS(tty)) {
1169 * Here we want to turn off the RTS line. On Macintoshes,
1170 * we only get the DTR line, which goes to both DTR and
1171 * RTS on the modem. RTS doesn't go out to the serial
1172 * port socket. So you should make sure your modem is
1173 * set to ignore DTR if you're using CRTSCTS.
1175 save_flags(flags); cli();
1176 info->curregs[5] &= ~(DTR | RTS);
1177 info->pendregs[5] &= ~(DTR | RTS);
1178 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1179 restore_flags(flags);
1183 static void rs_unthrottle(struct tty_struct * tty)
1185 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1186 unsigned long flags;
1187 #ifdef SERIAL_DEBUG_THROTTLE
1188 char buf[64];
1190 printk("unthrottle %s: %d....\n",tty->ldisc.chars_in_buffer(tty));
1191 #endif
1193 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1194 return;
1196 if (I_IXOFF(tty)) {
1197 save_flags(flags); cli();
1198 if (info->x_char)
1199 info->x_char = 0;
1200 else {
1201 info->x_char = START_CHAR(tty);
1202 if (!info->tx_active)
1203 transmit_chars(info);
1205 restore_flags(flags);
1208 if (C_CRTSCTS(tty)) {
1209 /* Assert RTS and DTR lines */
1210 save_flags(flags); cli();
1211 info->curregs[5] |= DTR | RTS;
1212 info->pendregs[5] |= DTR | RTS;
1213 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1214 restore_flags(flags);
1219 * ------------------------------------------------------------
1220 * rs_ioctl() and friends
1221 * ------------------------------------------------------------
1224 static int get_serial_info(struct mac_serial * info,
1225 struct serial_struct * retinfo)
1227 struct serial_struct tmp;
1229 if (!retinfo)
1230 return -EFAULT;
1231 memset(&tmp, 0, sizeof(tmp));
1232 tmp.type = info->type;
1233 tmp.line = info->line;
1234 tmp.port = info->port;
1235 tmp.irq = info->irq;
1236 tmp.flags = info->flags;
1237 tmp.baud_base = info->baud_base;
1238 tmp.close_delay = info->close_delay;
1239 tmp.closing_wait = info->closing_wait;
1240 tmp.custom_divisor = info->custom_divisor;
1241 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1242 return -EFAULT;
1243 return 0;
1246 static int set_serial_info(struct mac_serial * info,
1247 struct serial_struct * new_info)
1249 struct serial_struct new_serial;
1250 struct mac_serial old_info;
1251 int retval = 0;
1253 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1254 return -EFAULT;
1255 old_info = *info;
1257 if (!capable(CAP_SYS_ADMIN)) {
1258 if ((new_serial.baud_base != info->baud_base) ||
1259 (new_serial.type != info->type) ||
1260 (new_serial.close_delay != info->close_delay) ||
1261 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1262 (info->flags & ~ZILOG_USR_MASK)))
1263 return -EPERM;
1264 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1265 (new_serial.flags & ZILOG_USR_MASK));
1266 info->custom_divisor = new_serial.custom_divisor;
1267 goto check_and_exit;
1270 if (info->count > 1)
1271 return -EBUSY;
1274 * OK, past this point, all the error checking has been done.
1275 * At this point, we start making changes.....
1278 info->baud_base = new_serial.baud_base;
1279 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1280 (new_serial.flags & ZILOG_FLAGS));
1281 info->type = new_serial.type;
1282 info->close_delay = new_serial.close_delay;
1283 info->closing_wait = new_serial.closing_wait;
1285 check_and_exit:
1286 if (info->flags & ZILOG_INITIALIZED)
1287 retval = setup_scc(info);
1288 return retval;
1292 * get_lsr_info - get line status register info
1294 * Purpose: Let user call ioctl() to get info when the UART physically
1295 * is emptied. On bus types like RS485, the transmitter must
1296 * release the bus after transmitting. This must be done when
1297 * the transmit shift register is empty, not be done when the
1298 * transmit holding register is empty. This functionality
1299 * allows an RS485 driver to be written in user space.
1301 static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1303 unsigned char status;
1304 unsigned long flags;
1306 save_flags(flags); cli();
1307 status = read_zsreg(info->zs_channel, 0);
1308 restore_flags(flags);
1309 status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0;
1310 return put_user(status,value);
1313 static int get_modem_info(struct mac_serial *info, unsigned int *value)
1315 unsigned char control, status;
1316 unsigned int result;
1317 unsigned long flags;
1319 save_flags(flags); cli();
1320 control = info->curregs[5];
1321 status = read_zsreg(info->zs_channel, 0);
1322 restore_flags(flags);
1323 result = ((control & RTS) ? TIOCM_RTS: 0)
1324 | ((control & DTR) ? TIOCM_DTR: 0)
1325 | ((status & DCD) ? TIOCM_CAR: 0)
1326 | ((status & CTS) ? 0: TIOCM_CTS);
1327 return put_user(result,value);
1330 static int set_modem_info(struct mac_serial *info, unsigned int cmd,
1331 unsigned int *value)
1333 int error;
1334 unsigned int arg, bits;
1335 unsigned long flags;
1337 error = get_user(arg, value);
1338 if (error)
1339 return error;
1340 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1341 save_flags(flags); cli();
1342 switch (cmd) {
1343 case TIOCMBIS:
1344 info->curregs[5] |= bits;
1345 break;
1346 case TIOCMBIC:
1347 info->curregs[5] &= ~bits;
1348 break;
1349 case TIOCMSET:
1350 info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits;
1351 break;
1352 default:
1353 restore_flags(flags);
1354 return -EINVAL;
1356 info->pendregs[5] = info->curregs[5];
1357 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1358 restore_flags(flags);
1359 return 0;
1363 * rs_break - turn transmit break condition on/off
1365 static void rs_break(struct tty_struct *tty, int break_state)
1367 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1368 unsigned long flags;
1370 if (serial_paranoia_check(info, tty->device, "rs_break"))
1371 return;
1372 if (!info->port)
1373 return;
1375 save_flags(flags); cli();
1376 if (break_state == -1)
1377 info->curregs[5] |= SND_BRK;
1378 else
1379 info->curregs[5] &= ~SND_BRK;
1380 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1381 restore_flags(flags);
1384 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1385 unsigned int cmd, unsigned long arg)
1387 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1389 #ifdef CONFIG_KGDB
1390 if (info->kgdb_channel)
1391 return -ENODEV;
1392 #endif
1393 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1394 return -ENODEV;
1396 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1397 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) {
1398 if (tty->flags & (1 << TTY_IO_ERROR))
1399 return -EIO;
1402 switch (cmd) {
1403 case TIOCMGET:
1404 return get_modem_info(info, (unsigned int *) arg);
1405 case TIOCMBIS:
1406 case TIOCMBIC:
1407 case TIOCMSET:
1408 return set_modem_info(info, cmd, (unsigned int *) arg);
1409 case TIOCGSERIAL:
1410 return get_serial_info(info,
1411 (struct serial_struct *) arg);
1412 case TIOCSSERIAL:
1413 return set_serial_info(info,
1414 (struct serial_struct *) arg);
1415 case TIOCSERGETLSR: /* Get line status register */
1416 return get_lsr_info(info, (unsigned int *) arg);
1418 case TIOCSERGSTRUCT:
1419 if (copy_to_user((struct mac_serial *) arg,
1420 info, sizeof(struct mac_serial)))
1421 return -EFAULT;
1422 return 0;
1424 default:
1425 return -ENOIOCTLCMD;
1427 return 0;
1430 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1432 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1433 int was_stopped;
1435 if (tty->termios->c_cflag == old_termios->c_cflag)
1436 return;
1437 was_stopped = info->tx_stopped;
1439 change_speed(info, old_termios);
1441 if (was_stopped && !info->tx_stopped) {
1442 tty->hw_stopped = 0;
1443 rs_start(tty);
1448 * ------------------------------------------------------------
1449 * rs_close()
1451 * This routine is called when the serial port gets closed.
1452 * Wait for the last remaining data to be sent.
1453 * ------------------------------------------------------------
1455 static void rs_close(struct tty_struct *tty, struct file * filp)
1457 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1458 unsigned long flags;
1460 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1461 return;
1463 save_flags(flags); cli();
1465 if (tty_hung_up_p(filp)) {
1466 restore_flags(flags);
1467 return;
1470 #ifdef SERIAL_DEBUG_OPEN
1471 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1472 #endif
1473 if ((tty->count == 1) && (info->count != 1)) {
1475 * Uh, oh. tty->count is 1, which means that the tty
1476 * structure will be freed. Info->count should always
1477 * be one in these conditions. If it's greater than
1478 * one, we've got real problems, since it means the
1479 * serial port won't be shutdown.
1481 printk("rs_close: bad serial port count; tty->count is 1, "
1482 "info->count is %d\n", info->count);
1483 info->count = 1;
1485 if (--info->count < 0) {
1486 printk("rs_close: bad serial port count for ttys%d: %d\n",
1487 info->line, info->count);
1488 info->count = 0;
1490 if (info->count) {
1491 restore_flags(flags);
1492 return;
1494 info->flags |= ZILOG_CLOSING;
1496 * Save the termios structure, since this port may have
1497 * separate termios for callout and dialin.
1499 if (info->flags & ZILOG_NORMAL_ACTIVE)
1500 info->normal_termios = *tty->termios;
1501 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1502 info->callout_termios = *tty->termios;
1504 * Now we wait for the transmit buffer to clear; and we notify
1505 * the line discipline to only process XON/XOFF characters.
1507 #ifdef SERIAL_DEBUG_OPEN
1508 printk("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1509 #endif
1510 tty->closing = 1;
1511 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1512 tty_wait_until_sent(tty, info->closing_wait);
1514 * At this point we stop accepting input. To do this, we
1515 * disable the receiver and receive interrupts.
1517 info->curregs[3] &= ~RxENABLE;
1518 info->pendregs[3] = info->curregs[3];
1519 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1520 info->curregs[1] &= ~(0x18); /* disable any rx ints */
1521 info->pendregs[1] = info->curregs[1];
1522 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1523 ZS_CLEARFIFO(info->zs_channel);
1524 if (info->flags & ZILOG_INITIALIZED) {
1526 * Before we drop DTR, make sure the SCC transmitter
1527 * has completely drained.
1529 #ifdef SERIAL_DEBUG_OPEN
1530 printk("waiting end of Rx...\n");
1531 #endif
1532 rs_wait_until_sent(tty, info->timeout);
1535 shutdown(info);
1536 /* restore flags now since shutdown() will have disabled this port's
1537 specific irqs */
1538 restore_flags(flags);
1540 if (tty->driver.flush_buffer)
1541 tty->driver.flush_buffer(tty);
1542 if (tty->ldisc.flush_buffer)
1543 tty->ldisc.flush_buffer(tty);
1544 tty->closing = 0;
1545 info->event = 0;
1546 info->tty = 0;
1548 if (info->blocked_open) {
1549 if (info->close_delay) {
1550 current->state = TASK_INTERRUPTIBLE;
1551 schedule_timeout(info->close_delay);
1553 wake_up_interruptible(&info->open_wait);
1555 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1556 ZILOG_CLOSING);
1557 wake_up_interruptible(&info->close_wait);
1561 * rs_wait_until_sent() --- wait until the transmitter is empty
1563 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1565 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1566 unsigned long orig_jiffies, char_time;
1568 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1569 return;
1571 /* printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
1572 timeout, tty->stopped, info->tx_stopped);
1574 orig_jiffies = jiffies;
1576 * Set the check interval to be 1/5 of the estimated time to
1577 * send a single character, and make it at least 1. The check
1578 * interval should also be less than the timeout.
1580 if (info->timeout <= HZ/50) {
1581 printk("macserial: invalid info->timeout=%d\n", info->timeout);
1582 info->timeout = HZ/50+1;
1585 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1586 char_time = char_time / 5;
1587 if (char_time > HZ) {
1588 printk("macserial: char_time %ld >HZ !!!\n", char_time);
1589 char_time = 1;
1590 } else if (char_time == 0)
1591 char_time = 1;
1592 if (timeout)
1593 char_time = MIN(char_time, timeout);
1594 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1595 current->state = TASK_INTERRUPTIBLE;
1596 schedule_timeout(char_time);
1597 if (signal_pending(current))
1598 break;
1599 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1600 break;
1602 current->state = TASK_RUNNING;
1606 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1608 static void rs_hangup(struct tty_struct *tty)
1610 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1612 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1613 return;
1615 rs_flush_buffer(tty);
1616 shutdown(info);
1617 info->event = 0;
1618 info->count = 0;
1619 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1620 info->tty = 0;
1621 wake_up_interruptible(&info->open_wait);
1625 * ------------------------------------------------------------
1626 * rs_open() and friends
1627 * ------------------------------------------------------------
1629 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1630 struct mac_serial *info)
1632 DECLARE_WAITQUEUE(wait,current);
1633 int retval;
1634 int do_clocal = 0;
1637 * If the device is in the middle of being closed, then block
1638 * until it's done, and then try again.
1640 if (info->flags & ZILOG_CLOSING) {
1641 interruptible_sleep_on(&info->close_wait);
1642 #ifdef SERIAL_DO_RESTART
1643 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1644 -EAGAIN : -ERESTARTSYS);
1645 #else
1646 return -EAGAIN;
1647 #endif
1651 * If this is a callout device, then just make sure the normal
1652 * device isn't being used.
1654 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1655 if (info->flags & ZILOG_NORMAL_ACTIVE)
1656 return -EBUSY;
1657 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1658 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1659 (info->session != current->session))
1660 return -EBUSY;
1661 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1662 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1663 (info->pgrp != current->pgrp))
1664 return -EBUSY;
1665 info->flags |= ZILOG_CALLOUT_ACTIVE;
1666 return 0;
1670 * If non-blocking mode is set, or the port is not enabled,
1671 * then make the check up front and then exit.
1673 if ((filp->f_flags & O_NONBLOCK) ||
1674 (tty->flags & (1 << TTY_IO_ERROR))) {
1675 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1676 return -EBUSY;
1677 info->flags |= ZILOG_NORMAL_ACTIVE;
1678 return 0;
1681 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1682 if (info->normal_termios.c_cflag & CLOCAL)
1683 do_clocal = 1;
1684 } else {
1685 if (tty->termios->c_cflag & CLOCAL)
1686 do_clocal = 1;
1690 * Block waiting for the carrier detect and the line to become
1691 * free (i.e., not in use by the callout). While we are in
1692 * this loop, info->count is dropped by one, so that
1693 * rs_close() knows when to free things. We restore it upon
1694 * exit, either normal or abnormal.
1696 retval = 0;
1697 add_wait_queue(&info->open_wait, &wait);
1698 #ifdef SERIAL_DEBUG_OPEN
1699 printk("block_til_ready before block: ttys%d, count = %d\n",
1700 info->line, info->count);
1701 #endif
1702 cli();
1703 if (!tty_hung_up_p(filp))
1704 info->count--;
1705 sti();
1706 info->blocked_open++;
1707 while (1) {
1708 cli();
1709 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1710 (tty->termios->c_cflag & CBAUD))
1711 zs_rtsdtr(info, 1);
1712 sti();
1713 set_current_state(TASK_INTERRUPTIBLE);
1714 if (tty_hung_up_p(filp) ||
1715 !(info->flags & ZILOG_INITIALIZED)) {
1716 #ifdef SERIAL_DO_RESTART
1717 if (info->flags & ZILOG_HUP_NOTIFY)
1718 retval = -EAGAIN;
1719 else
1720 retval = -ERESTARTSYS;
1721 #else
1722 retval = -EAGAIN;
1723 #endif
1724 break;
1726 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1727 !(info->flags & ZILOG_CLOSING) &&
1728 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1729 break;
1730 if (signal_pending(current)) {
1731 retval = -ERESTARTSYS;
1732 break;
1734 #ifdef SERIAL_DEBUG_OPEN
1735 printk("block_til_ready blocking: ttys%d, count = %d\n",
1736 info->line, info->count);
1737 #endif
1738 schedule();
1740 current->state = TASK_RUNNING;
1741 remove_wait_queue(&info->open_wait, &wait);
1742 if (!tty_hung_up_p(filp))
1743 info->count++;
1744 info->blocked_open--;
1745 #ifdef SERIAL_DEBUG_OPEN
1746 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1747 info->line, info->count);
1748 #endif
1749 if (retval)
1750 return retval;
1751 info->flags |= ZILOG_NORMAL_ACTIVE;
1752 return 0;
1756 * This routine is called whenever a serial port is opened. It
1757 * enables interrupts for a serial port, linking in its ZILOG structure into
1758 * the IRQ chain. It also performs the serial-specific
1759 * initialization for the tty structure.
1761 static int rs_open(struct tty_struct *tty, struct file * filp)
1763 struct mac_serial *info;
1764 int retval, line;
1765 unsigned long page;
1767 line = MINOR(tty->device) - tty->driver.minor_start;
1768 if ((line < 0) || (line >= zs_channels_found))
1769 return -ENODEV;
1770 info = zs_soft + line;
1772 #ifdef CONFIG_KGDB
1773 if (info->kgdb_channel)
1774 return -ENODEV;
1775 #endif
1776 if (serial_paranoia_check(info, tty->device, "rs_open"))
1777 return -ENODEV;
1778 #ifdef SERIAL_DEBUG_OPEN
1779 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1780 info->count);
1781 #endif
1783 info->count++;
1784 tty->driver_data = info;
1785 info->tty = tty;
1787 if (!tmp_buf) {
1788 page = get_free_page(GFP_KERNEL);
1789 if (!page)
1790 return -ENOMEM;
1791 if (tmp_buf)
1792 free_page(page);
1793 else
1794 tmp_buf = (unsigned char *) page;
1798 * If the port is the middle of closing, bail out now
1800 if (tty_hung_up_p(filp) ||
1801 (info->flags & ZILOG_CLOSING)) {
1802 if (info->flags & ZILOG_CLOSING)
1803 interruptible_sleep_on(&info->close_wait);
1804 #ifdef SERIAL_DO_RESTART
1805 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1806 -EAGAIN : -ERESTARTSYS);
1807 #else
1808 return -EAGAIN;
1809 #endif
1813 * Start up serial port
1816 retval = startup(info, 1);
1817 if (retval)
1818 return retval;
1820 retval = block_til_ready(tty, filp, info);
1821 if (retval) {
1822 #ifdef SERIAL_DEBUG_OPEN
1823 printk("rs_open returning after block_til_ready with %d\n",
1824 retval);
1825 #endif
1826 return retval;
1829 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1830 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1831 *tty->termios = info->normal_termios;
1832 else
1833 *tty->termios = info->callout_termios;
1834 change_speed(info, 0);
1836 #ifdef CONFIG_SERIAL_CONSOLE
1837 if (sercons.cflag && sercons.index == line) {
1838 tty->termios->c_cflag = sercons.cflag;
1839 sercons.cflag = 0;
1840 change_speed(info, 0);
1842 #endif
1844 info->session = current->session;
1845 info->pgrp = current->pgrp;
1847 #ifdef SERIAL_DEBUG_OPEN
1848 printk("rs_open ttys%d successful...\n", info->line);
1849 #endif
1850 return 0;
1853 /* Finally, routines used to initialize the serial driver. */
1855 static void show_serial_version(void)
1857 printk("PowerMac Z8530 serial driver version 1.01\n");
1860 /* Ask the PROM how many Z8530s we have and initialize their zs_channels */
1861 static void
1862 probe_sccs()
1864 struct device_node *dev, *ch;
1865 struct mac_serial **pp;
1866 int n, lenp;
1867 char *conn;
1869 n = 0;
1870 pp = &zs_chain;
1871 for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
1872 if (n >= NUM_CHANNELS) {
1873 printk("Sorry, can't use %s: no more channels\n",
1874 dev->full_name);
1875 continue;
1877 for (ch = dev->child; ch != 0; ch = ch->sibling) {
1878 if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
1879 printk("Can't use %s: %d addrs %d intrs\n",
1880 ch->full_name, ch->n_addrs, ch->n_intrs);
1881 continue;
1883 zs_channels[n].control = (volatile unsigned char *)
1884 ioremap(ch->addrs[0].address, 0x1000);
1885 zs_channels[n].data = zs_channels[n].control + 0x10;
1886 spin_lock_init(&zs_channels[n].lock);
1887 zs_soft[n].zs_channel = &zs_channels[n];
1888 zs_soft[n].dev_node = ch;
1889 zs_soft[n].irq = ch->intrs[0].line;
1890 zs_soft[n].zs_channel->parent = &zs_soft[n];
1891 zs_soft[n].is_cobalt_modem = device_is_compatible(ch, "cobalt");
1893 /* XXX tested only with wallstreet PowerBook,
1894 should do no harm anyway */
1895 conn = get_property(ch, "AAPL,connector", &lenp);
1896 zs_soft[n].is_pwbk_ir =
1897 conn && (strcmp(conn, "infrared") == 0);
1899 /* XXX this assumes the prom puts chan A before B */
1900 if (n & 1)
1901 zs_soft[n].zs_chan_a = &zs_channels[n-1];
1902 else
1903 zs_soft[n].zs_chan_a = &zs_channels[n];
1905 *pp = &zs_soft[n];
1906 pp = &zs_soft[n].zs_next;
1907 ++n;
1910 *pp = 0;
1911 zs_channels_found = n;
1914 /* rs_init inits the driver */
1915 int macserial_init(void)
1917 int channel, i;
1918 unsigned long flags;
1919 struct mac_serial *info;
1921 /* Setup base handler, and timer table. */
1922 init_bh(MACSERIAL_BH, do_serial_bh);
1923 timer_table[RS_TIMER].fn = rs_timer;
1924 timer_table[RS_TIMER].expires = 0;
1926 /* Find out how many Z8530 SCCs we have */
1927 if (zs_chain == 0)
1928 probe_sccs();
1930 /* XXX assume it's a powerbook if we have a via-pmu */
1931 is_powerbook = find_devices("via-pmu") != 0;
1933 /* Register the interrupt handler for each one */
1934 save_flags(flags); cli();
1935 for (i = 0; i < zs_channels_found; ++i) {
1936 if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
1937 "SCC", &zs_soft[i]))
1938 printk(KERN_ERR "macserial: can't get irq %d\n",
1939 zs_soft[i].irq);
1940 disable_irq(zs_soft[i].irq);
1942 restore_flags(flags);
1944 show_serial_version();
1946 /* Initialize the tty_driver structure */
1947 /* Not all of this is exactly right for us. */
1949 memset(&serial_driver, 0, sizeof(struct tty_driver));
1950 serial_driver.magic = TTY_DRIVER_MAGIC;
1951 serial_driver.name = "ttyS";
1952 serial_driver.major = TTY_MAJOR;
1953 serial_driver.minor_start = 64;
1954 serial_driver.num = zs_channels_found;
1955 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1956 serial_driver.subtype = SERIAL_TYPE_NORMAL;
1957 serial_driver.init_termios = tty_std_termios;
1959 serial_driver.init_termios.c_cflag =
1960 B38400 | CS8 | CREAD | HUPCL | CLOCAL;
1961 serial_driver.flags = TTY_DRIVER_REAL_RAW;
1962 serial_driver.refcount = &serial_refcount;
1963 serial_driver.table = serial_table;
1964 serial_driver.termios = serial_termios;
1965 serial_driver.termios_locked = serial_termios_locked;
1967 serial_driver.open = rs_open;
1968 serial_driver.close = rs_close;
1969 serial_driver.write = rs_write;
1970 serial_driver.flush_chars = rs_flush_chars;
1971 serial_driver.write_room = rs_write_room;
1972 serial_driver.chars_in_buffer = rs_chars_in_buffer;
1973 serial_driver.flush_buffer = rs_flush_buffer;
1974 serial_driver.ioctl = rs_ioctl;
1975 serial_driver.throttle = rs_throttle;
1976 serial_driver.unthrottle = rs_unthrottle;
1977 serial_driver.set_termios = rs_set_termios;
1978 serial_driver.stop = rs_stop;
1979 serial_driver.start = rs_start;
1980 serial_driver.hangup = rs_hangup;
1981 serial_driver.break_ctl = rs_break;
1982 serial_driver.wait_until_sent = rs_wait_until_sent;
1985 * The callout device is just like normal device except for
1986 * major number and the subtype code.
1988 callout_driver = serial_driver;
1989 callout_driver.name = "cua";
1990 callout_driver.major = TTYAUX_MAJOR;
1991 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1993 if (tty_register_driver(&serial_driver))
1994 panic("Couldn't register serial driver\n");
1995 if (tty_register_driver(&callout_driver))
1996 panic("Couldn't register callout driver\n");
1998 for (channel = 0; channel < zs_channels_found; ++channel) {
1999 #ifdef CONFIG_KGDB
2000 if (zs_soft[channel].kgdb_channel) {
2001 kgdb_interruptible(1);
2002 continue;
2004 #endif
2005 zs_soft[channel].clk_divisor = 16;
2006 /* -- we are not sure the SCC is powered ON at this point
2007 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2009 zs_soft[channel].zs_baud = 38400;
2011 /* If console serial line, then enable interrupts. */
2012 if (zs_soft[channel].is_cons) {
2013 printk("macserial: console line, enabling interrupt %d\n", zs_soft[channel].irq);
2014 panic("macserial: console not supported yet !");
2015 write_zsreg(zs_soft[channel].zs_channel, R1,
2016 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
2017 write_zsreg(zs_soft[channel].zs_channel, R9,
2018 (NV | MIE));
2022 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2024 unsigned char* connector;
2025 int lenp;
2027 #ifdef CONFIG_KGDB
2028 if (info->kgdb_channel) {
2029 continue;
2031 #endif
2032 info->magic = SERIAL_MAGIC;
2033 info->port = (int) info->zs_channel->control;
2034 info->line = i;
2035 info->tty = 0;
2036 info->custom_divisor = 16;
2037 info->timeout = 0;
2038 info->close_delay = 50;
2039 info->closing_wait = 3000;
2040 info->x_char = 0;
2041 info->event = 0;
2042 info->count = 0;
2043 info->blocked_open = 0;
2044 info->tqueue.routine = do_softint;
2045 info->tqueue.data = info;
2046 info->callout_termios =callout_driver.init_termios;
2047 info->normal_termios = serial_driver.init_termios;
2048 init_waitqueue_head(&info->open_wait);
2049 init_waitqueue_head(&info->close_wait);
2050 info->timeout = HZ;
2051 printk("tty%02d at 0x%08x (irq = %d)", info->line,
2052 info->port, info->irq);
2053 printk(" is a Z8530 ESCC");
2054 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2055 if (connector)
2056 printk(", port = %s", connector);
2057 if (info->is_cobalt_modem)
2058 printk(" (cobalt modem)");
2059 if (info->is_pwbk_ir)
2060 printk(" (powerbook IR)");
2061 printk("\n");
2063 #ifdef CONFIG_KGDB
2064 if (info->kgdb_channel)
2065 continue;
2066 #endif
2067 #ifdef CONFIG_XMON
2068 if (!info->is_cobalt_modem)
2069 continue;
2070 #endif
2071 /* By default, disable the port */
2072 set_scc_power(info, 0);
2075 return 0;
2078 #ifdef MODULE
2079 int init_module(void)
2081 macserial_init();
2082 return 0;
2085 void cleanup_module(void)
2087 int i;
2088 unsigned long flags;
2089 struct mac_serial *info;
2091 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2092 set_scc_power(info, 0);
2093 save_flags(flags); cli();
2094 for (i = 0; i < zs_channels_found; ++i)
2095 free_irq(zs_soft[i].irq, &zs_soft[i]);
2096 restore_flags(flags);
2097 tty_unregister_driver(&callout_driver);
2098 tty_unregister_driver(&serial_driver);
2100 #endif /* MODULE */
2102 #if 0
2104 * register_serial and unregister_serial allows for serial ports to be
2105 * configured at run-time, to support PCMCIA modems.
2107 /* PowerMac: Unused at this time, just here to make things link. */
2108 int register_serial(struct serial_struct *req)
2110 return -1;
2113 void unregister_serial(int line)
2115 return;
2117 #endif
2120 * ------------------------------------------------------------
2121 * Serial console driver
2122 * ------------------------------------------------------------
2124 #ifdef CONFIG_SERIAL_CONSOLE
2128 * Print a string to the serial port trying not to disturb
2129 * any possible real use of the port...
2131 static void serial_console_write(struct console *co, const char *s,
2132 unsigned count)
2134 struct mac_serial *info = zs_soft + co->index;
2135 int i;
2137 /* Turn of interrupts and enable the transmitter. */
2138 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2139 write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2141 for (i=0; i<count; i++) {
2142 /* Wait for the transmit buffer to empty. */
2143 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2144 eieio();
2147 write_zsdata(info->zs_channel, s[i]);
2148 if (s[i] == 10) {
2149 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2150 == 0)
2151 eieio();
2153 write_zsdata(info->zs_channel, 13);
2157 /* Restore the values in the registers. */
2158 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2159 /* Don't disable the transmitter. */
2163 * Receive character from the serial port
2165 static int serial_console_wait_key(struct console *co)
2167 struct mac_serial *info = zs_soft + co->index;
2168 int val;
2170 /* Turn of interrupts and enable the transmitter. */
2171 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~INT_ALL_Rx);
2172 write_zsreg(info->zs_channel, R3, info->curregs[3] | RxENABLE);
2174 /* Wait for something in the receive buffer. */
2175 while((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0)
2176 eieio();
2177 val = read_zsdata(info->zs_channel);
2179 /* Restore the values in the registers. */
2180 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2181 write_zsreg(info->zs_channel, R3, info->curregs[3]);
2183 return val;
2186 static kdev_t serial_console_device(struct console *c)
2188 return MKDEV(TTY_MAJOR, 64 + c->index);
2192 * Setup initial baud/bits/parity. We do two things here:
2193 * - construct a cflag setting for the first rs_open()
2194 * - initialize the serial port
2195 * Return non-zero if we didn't find a serial port.
2197 static int __init serial_console_setup(struct console *co, char *options)
2199 struct mac_serial *info = zs_soft + co->index;
2200 int baud = 38400;
2201 int bits = 8;
2202 int parity = 'n';
2203 int cflag = CREAD | HUPCL | CLOCAL;
2204 int brg;
2205 char *s;
2206 long flags;
2208 /* Find out how many Z8530 SCCs we have */
2209 if (zs_chain == 0)
2210 probe_sccs();
2212 if (zs_chain == 0)
2213 return -1;
2215 /* Reset the channel */
2216 write_zsreg(info->zs_channel, R9, CHRA);
2218 if (options) {
2219 baud = simple_strtoul(options, NULL, 10);
2220 s = options;
2221 while(*s >= '0' && *s <= '9')
2222 s++;
2223 if (*s)
2224 parity = *s++;
2225 if (*s)
2226 bits = *s - '0';
2230 * Now construct a cflag setting.
2232 switch(baud) {
2233 case 1200:
2234 cflag |= B1200;
2235 break;
2236 case 2400:
2237 cflag |= B2400;
2238 break;
2239 case 4800:
2240 cflag |= B4800;
2241 break;
2242 case 9600:
2243 cflag |= B9600;
2244 break;
2245 case 19200:
2246 cflag |= B19200;
2247 break;
2248 case 57600:
2249 cflag |= B57600;
2250 break;
2251 case 115200:
2252 cflag |= B115200;
2253 break;
2254 case 38400:
2255 default:
2256 cflag |= B38400;
2257 break;
2259 switch(bits) {
2260 case 7:
2261 cflag |= CS7;
2262 break;
2263 default:
2264 case 8:
2265 cflag |= CS8;
2266 break;
2268 switch(parity) {
2269 case 'o': case 'O':
2270 cflag |= PARENB | PARODD;
2271 break;
2272 case 'e': case 'E':
2273 cflag |= PARENB;
2274 break;
2276 co->cflag = cflag;
2278 save_flags(flags); cli();
2279 memset(info->curregs, 0, sizeof(info->curregs));
2281 info->zs_baud = baud;
2282 info->clk_divisor = 16;
2283 switch (info->zs_baud) {
2284 case ZS_CLOCK/16: /* 230400 */
2285 info->curregs[4] = X16CLK;
2286 info->curregs[11] = 0;
2287 break;
2288 case ZS_CLOCK/32: /* 115200 */
2289 info->curregs[4] = X32CLK;
2290 info->curregs[11] = 0;
2291 break;
2292 default:
2293 info->curregs[4] = X16CLK;
2294 info->curregs[11] = TCBR | RCBR;
2295 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2296 info->curregs[12] = (brg & 255);
2297 info->curregs[13] = ((brg >> 8) & 255);
2298 info->curregs[14] = BRENABL;
2301 /* byte size and parity */
2302 info->curregs[3] &= ~RxNBITS_MASK;
2303 info->curregs[5] &= ~TxNBITS_MASK;
2304 switch (cflag & CSIZE) {
2305 case CS5:
2306 info->curregs[3] |= Rx5;
2307 info->curregs[5] |= Tx5;
2308 break;
2309 case CS6:
2310 info->curregs[3] |= Rx6;
2311 info->curregs[5] |= Tx6;
2312 break;
2313 case CS7:
2314 info->curregs[3] |= Rx7;
2315 info->curregs[5] |= Tx7;
2316 break;
2317 case CS8:
2318 default: /* defaults to 8 bits */
2319 info->curregs[3] |= Rx8;
2320 info->curregs[5] |= Tx8;
2321 break;
2323 info->curregs[5] |= TxENAB | RTS | DTR;
2324 info->pendregs[3] = info->curregs[3];
2325 info->pendregs[5] = info->curregs[5];
2327 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
2328 if (cflag & CSTOPB) {
2329 info->curregs[4] |= SB2;
2330 } else {
2331 info->curregs[4] |= SB1;
2333 if (cflag & PARENB) {
2334 info->curregs[4] |= PAR_ENA;
2335 if (!(cflag & PARODD)) {
2336 info->curregs[4] |= PAR_EVEN;
2339 info->pendregs[4] = info->curregs[4];
2341 if (!(cflag & CLOCAL)) {
2342 if (!(info->curregs[15] & DCDIE))
2343 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2344 info->curregs[15] |= DCDIE;
2345 } else
2346 info->curregs[15] &= ~DCDIE;
2347 if (cflag & CRTSCTS) {
2348 info->curregs[15] |= CTSIE;
2349 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
2350 info->tx_stopped = 1;
2351 } else {
2352 info->curregs[15] &= ~CTSIE;
2353 info->tx_stopped = 0;
2355 info->pendregs[15] = info->curregs[15];
2357 /* Load up the new values */
2358 load_zsregs(info->zs_channel, info->curregs);
2360 restore_flags(flags);
2362 return 0;
2365 static struct console sercons = {
2366 "ttyS",
2367 serial_console_write,
2368 NULL,
2369 serial_console_device,
2370 serial_console_wait_key,
2371 NULL,
2372 serial_console_setup,
2373 CON_PRINTBUFFER,
2376 NULL
2380 * Register console.
2382 long __init serial_console_init(long kmem_start, long kmem_end)
2384 register_console(&sercons);
2385 return kmem_start;
2387 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2389 #ifdef CONFIG_KGDB
2390 /* These are for receiving and sending characters under the kgdb
2391 * source level kernel debugger.
2393 void putDebugChar(char kgdb_char)
2395 struct mac_zschannel *chan = zs_kgdbchan;
2396 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2397 udelay(5);
2398 write_zsdata(chan, kgdb_char);
2401 char getDebugChar(void)
2403 struct mac_zschannel *chan = zs_kgdbchan;
2404 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2405 eieio(); /*barrier();*/
2406 return read_zsdata(chan);
2409 void kgdb_interruptible(int yes)
2411 struct mac_zschannel *chan = zs_kgdbchan;
2412 int one, nine;
2413 nine = read_zsreg(chan, 9);
2414 if (yes == 1) {
2415 one = EXT_INT_ENAB|INT_ALL_Rx;
2416 nine |= MIE;
2417 printk("turning serial ints on\n");
2418 } else {
2419 one = RxINT_DISAB;
2420 nine &= ~MIE;
2421 printk("turning serial ints off\n");
2423 write_zsreg(chan, 1, one);
2424 write_zsreg(chan, 9, nine);
2427 /* This sets up the serial port we're using, and turns on
2428 * interrupts for that channel, so kgdb is usable once we're done.
2430 static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
2432 int brg;
2433 int i, x;
2434 volatile char *sccc = ms->control;
2435 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
2436 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2437 for (i = 20000; i != 0; --i) {
2438 x = *sccc; eieio();
2440 for (i = 0; i < sizeof(scc_inittab); ++i) {
2441 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2442 i++;
2446 /* This is called at boot time to prime the kgdb serial debugging
2447 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2448 * for /dev/ttyb which is determined in setup_arch() from the
2449 * boot command line flags.
2450 * XXX at the moment probably only channel A will work
2452 void __init zs_kgdb_hook(int tty_num)
2454 /* Find out how many Z8530 SCCs we have */
2455 if (zs_chain == 0)
2456 probe_sccs();
2458 set_scc_power(&zs_soft[n], 1);
2460 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2461 zs_soft[tty_num].change_needed = 0;
2462 zs_soft[tty_num].clk_divisor = 16;
2463 zs_soft[tty_num].zs_baud = 38400;
2464 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2465 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2467 /* Turn on transmitter/receiver at 8-bits/char */
2468 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2469 printk("KGDB: on channel %d initialized\n", tty_num);
2470 set_debug_traps(); /* init stub */
2472 #endif /* ifdef CONFIG_KGDB */