Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / macintosh / macserial.c
blob6ed94158d4e139f4185a1547f823e48724799bee
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)
9 * Receive DMA code by Takashi Oe <toe@unlserve.unl.edu>.
11 * $Id: macserial.c,v 1.24.2.4 1999/10/19 04:36:42 paulus Exp $
14 #include <linux/config.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/interrupt.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/major.h>
24 #include <linux/string.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #ifdef CONFIG_SERIAL_CONSOLE
31 #include <linux/console.h>
32 #endif
33 #include <linux/slab.h>
35 #include <asm/init.h>
36 #include <asm/io.h>
37 #include <asm/pgtable.h>
38 #include <asm/irq.h>
39 #include <asm/prom.h>
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/bitops.h>
43 #include <asm/feature.h>
44 #include <linux/adb.h>
45 #include <linux/pmu.h>
46 #ifdef CONFIG_KGDB
47 #include <asm/kgdb.h>
48 #endif
49 #include <asm/dbdma.h>
51 #include "macserial.h"
53 #ifdef CONFIG_PMAC_PBOOK
54 static int serial_notify_sleep(struct pmu_sleep_notifier *self, int when);
55 static struct pmu_sleep_notifier serial_sleep_notifier = {
56 serial_notify_sleep,
57 SLEEP_LEVEL_MISC,
59 #endif
61 #define SUPPORT_SERIAL_DMA
64 * It would be nice to dynamically allocate everything that
65 * depends on NUM_SERIAL, so we could support any number of
66 * Z8530s, but for now...
68 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
69 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
71 /* On PowerMacs, the hardware takes care of the SCC recovery time,
72 but we need the eieio to make sure that the accesses occur
73 in the order we want. */
74 #define RECOVERY_DELAY eieio()
76 struct mac_zschannel zs_channels[NUM_CHANNELS];
78 struct mac_serial zs_soft[NUM_CHANNELS];
79 int zs_channels_found;
80 struct mac_serial *zs_chain; /* list of all channels */
82 struct tty_struct zs_ttys[NUM_CHANNELS];
84 static int is_powerbook;
86 #ifdef CONFIG_SERIAL_CONSOLE
87 static struct console sercons;
88 #endif
90 #ifdef CONFIG_KGDB
91 struct mac_zschannel *zs_kgdbchan;
92 static unsigned char scc_inittab[] = {
93 9, 0x80, /* reset A side (CHRA) */
94 13, 0, /* set baud rate divisor */
95 12, 1,
96 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
97 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
98 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
99 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
100 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
102 #endif
103 #define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
105 static DECLARE_TASK_QUEUE(tq_serial);
107 static struct tty_driver serial_driver, callout_driver;
108 static int serial_refcount;
110 /* serial subtype definitions */
111 #define SERIAL_TYPE_NORMAL 1
112 #define SERIAL_TYPE_CALLOUT 2
114 /* number of characters left in xmit buffer before we ask for more */
115 #define WAKEUP_CHARS 256
118 * Debugging.
120 #undef SERIAL_DEBUG_INTR
121 #undef SERIAL_DEBUG_OPEN
122 #undef SERIAL_DEBUG_FLOW
123 #undef SERIAL_DEBUG_POWER
124 #undef SERIAL_DEBUG_THROTTLE
125 #undef SERIAL_DEBUG_STOP
126 #undef SERIAL_DEBUG_BAUDS
128 #define RS_STROBE_TIME 10
129 #define RS_ISR_PASS_LIMIT 256
131 #define _INLINE_ inline
133 #ifdef SERIAL_DEBUG_OPEN
134 #define OPNDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
135 #else
136 #define OPNDBG(fmt, arg...) do { } while (0)
137 #endif
138 #ifdef SERIAL_DEBUG_POWER
139 #define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
140 #else
141 #define PWRDBG(fmt, arg...) do { } while (0)
142 #endif
143 #ifdef SERIAL_DEBUG_BAUDS
144 #define BAUDBG(fmt, arg...) printk(fmt , ## arg)
145 #else
146 #define BAUDBG(fmt, arg...) do { } while (0)
147 #endif
149 static void probe_sccs(void);
150 static void change_speed(struct mac_serial *info, struct termios *old);
151 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
152 static int set_scc_power(struct mac_serial * info, int state);
153 static int setup_scc(struct mac_serial * info);
154 static void dbdma_reset(volatile struct dbdma_regs *dma);
155 static void dbdma_flush(volatile struct dbdma_regs *dma);
156 static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs);
157 static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs);
158 static void dma_init(struct mac_serial * info);
159 static void rxdma_start(struct mac_serial * info, int current);
160 static void rxdma_to_tty(struct mac_serial * info);
162 static struct tty_struct *serial_table[NUM_CHANNELS];
163 static struct termios *serial_termios[NUM_CHANNELS];
164 static struct termios *serial_termios_locked[NUM_CHANNELS];
166 #ifndef MIN
167 #define MIN(a,b) ((a) < (b) ? (a) : (b))
168 #endif
171 * tmp_buf is used as a temporary buffer by serial_write. We need to
172 * lock it in case the copy_from_user blocks while swapping in a page,
173 * and some other program tries to do a serial write at the same time.
174 * Since the lock will only come under contention when the system is
175 * swapping and available memory is low, it makes sense to share one
176 * buffer across all the serial ports, since it significantly saves
177 * memory if large numbers of serial ports are open.
179 static unsigned char *tmp_buf;
180 static DECLARE_MUTEX(tmp_buf_sem);
183 static inline int __pmac
184 serial_paranoia_check(struct mac_serial *info,
185 dev_t device, const char *routine)
187 #ifdef SERIAL_PARANOIA_CHECK
188 static const char *badmagic =
189 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
190 static const char *badinfo =
191 "Warning: null mac_serial for (%d, %d) in %s\n";
193 if (!info) {
194 printk(badinfo, MAJOR(device), MINOR(device), routine);
195 return 1;
197 if (info->magic != SERIAL_MAGIC) {
198 printk(badmagic, MAJOR(device), MINOR(device), routine);
199 return 1;
201 #endif
202 return 0;
206 * Reading and writing Z8530 registers.
208 static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
209 unsigned char reg)
211 unsigned char retval;
212 unsigned long flags;
215 * We have to make this atomic.
217 spin_lock_irqsave(&channel->lock, flags);
218 if (reg != 0) {
219 *channel->control = reg;
220 RECOVERY_DELAY;
222 retval = *channel->control;
223 RECOVERY_DELAY;
224 spin_unlock_irqrestore(&channel->lock, flags);
225 return retval;
228 static inline void __pmac write_zsreg(struct mac_zschannel *channel,
229 unsigned char reg, unsigned char value)
231 unsigned long flags;
233 spin_lock_irqsave(&channel->lock, flags);
234 if (reg != 0) {
235 *channel->control = reg;
236 RECOVERY_DELAY;
238 *channel->control = value;
239 RECOVERY_DELAY;
240 spin_unlock_irqrestore(&channel->lock, flags);
241 return;
244 static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
246 unsigned char retval;
248 retval = *channel->data;
249 RECOVERY_DELAY;
250 return retval;
253 static inline void write_zsdata(struct mac_zschannel *channel,
254 unsigned char value)
256 *channel->data = value;
257 RECOVERY_DELAY;
258 return;
261 static inline void load_zsregs(struct mac_zschannel *channel,
262 unsigned char *regs)
264 ZS_CLEARERR(channel);
265 ZS_CLEARFIFO(channel);
266 /* Load 'em up */
267 write_zsreg(channel, R4, regs[R4]);
268 write_zsreg(channel, R10, regs[R10]);
269 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
270 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
271 write_zsreg(channel, R1, regs[R1]);
272 write_zsreg(channel, R9, regs[R9]);
273 write_zsreg(channel, R11, regs[R11]);
274 write_zsreg(channel, R12, regs[R12]);
275 write_zsreg(channel, R13, regs[R13]);
276 write_zsreg(channel, R14, regs[R14]);
277 write_zsreg(channel, R15, regs[R15]);
278 write_zsreg(channel, R3, regs[R3]);
279 write_zsreg(channel, R5, regs[R5]);
280 return;
283 /* Sets or clears DTR/RTS on the requested line */
284 static inline void zs_rtsdtr(struct mac_serial *ss, int set)
286 if (set)
287 ss->curregs[5] |= (RTS | DTR);
288 else
289 ss->curregs[5] &= ~(RTS | DTR);
290 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
291 return;
294 /* Utility routines for the Zilog */
295 static inline int get_zsbaud(struct mac_serial *ss)
297 struct mac_zschannel *channel = ss->zs_channel;
298 int brg;
300 if ((ss->curregs[R11] & TCBR) == 0) {
301 /* higher rates don't use the baud rate generator */
302 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
304 /* The baud rate is split up between two 8-bit registers in
305 * what is termed 'BRG time constant' format in my docs for
306 * the chip, it is a function of the clk rate the chip is
307 * receiving which happens to be constant.
309 brg = (read_zsreg(channel, 13) << 8);
310 brg |= read_zsreg(channel, 12);
311 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
314 /* On receive, this clears errors and the receiver interrupts */
315 static inline void rs_recv_clear(struct mac_zschannel *zsc)
317 write_zsreg(zsc, 0, ERR_RES);
318 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
322 * Reset a Descriptor-Based DMA channel.
324 static void dbdma_reset(volatile struct dbdma_regs *dma)
326 int i;
328 out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
331 * Yes this looks peculiar, but apparently it needs to be this
332 * way on some machines. (We need to make sure the DBDMA
333 * engine has actually got the write above and responded
334 * to it. - paulus)
336 for (i = 200; i > 0; --i)
337 if (ld_le32(&dma->status) & RUN)
338 udelay(1);
342 * Tells a DBDMA channel to stop and write any buffered data
343 * it might have to memory.
345 static _INLINE_ void dbdma_flush(volatile struct dbdma_regs *dma)
347 int i = 0;
349 out_le32(&dma->control, (FLUSH << 16) | FLUSH);
350 while (((in_le32(&dma->status) & FLUSH) != 0) && (i++ < 100))
351 udelay(1);
355 * ----------------------------------------------------------------------
357 * Here starts the interrupt handling routines. All of the following
358 * subroutines are declared as inline and are folded into
359 * rs_interrupt(). They were separated out for readability's sake.
361 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
362 * -----------------------------------------------------------------------
366 * This routine is used by the interrupt handler to schedule
367 * processing in the software interrupt portion of the driver.
369 static _INLINE_ void rs_sched_event(struct mac_serial *info,
370 int event)
372 info->event |= 1 << event;
373 queue_task(&info->tqueue, &tq_serial);
374 mark_bh(MACSERIAL_BH);
377 /* Work out the flag value for a z8530 status value. */
378 static _INLINE_ int stat_to_flag(int stat)
380 int flag;
382 if (stat & Rx_OVR) {
383 flag = TTY_OVERRUN;
384 } else if (stat & FRM_ERR) {
385 flag = TTY_FRAME;
386 } else if (stat & PAR_ERR) {
387 flag = TTY_PARITY;
388 } else
389 flag = 0;
390 return flag;
393 static _INLINE_ void receive_chars(struct mac_serial *info,
394 struct pt_regs *regs)
396 struct tty_struct *tty = info->tty;
397 unsigned char ch, stat, flag;
399 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
401 stat = read_zsreg(info->zs_channel, R1);
402 ch = read_zsdata(info->zs_channel);
404 #ifdef CONFIG_KGDB
405 if (info->kgdb_channel) {
406 if (ch == 0x03 || ch == '$')
407 breakpoint();
408 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
409 write_zsreg(info->zs_channel, 0, ERR_RES);
410 return;
412 #endif
413 if (!tty)
414 continue;
415 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
416 tty_flip_buffer_push(tty);
418 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
419 static int flip_buf_ovf;
420 if (++flip_buf_ovf <= 1)
421 printk("FB. overflow: %d\n", flip_buf_ovf);
422 break;
424 tty->flip.count++;
426 static int flip_max_cnt;
427 if (flip_max_cnt < tty->flip.count)
428 flip_max_cnt = tty->flip.count;
430 flag = stat_to_flag(stat);
431 if (flag)
432 /* reset the error indication */
433 write_zsreg(info->zs_channel, 0, ERR_RES);
434 *tty->flip.flag_buf_ptr++ = flag;
435 *tty->flip.char_buf_ptr++ = ch;
437 if (tty)
438 tty_flip_buffer_push(tty);
441 static void transmit_chars(struct mac_serial *info)
443 unsigned long flags;
445 save_flags(flags);
446 cli();
447 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
448 goto out;
449 info->tx_active = 0;
451 if (info->x_char) {
452 /* Send next char */
453 write_zsdata(info->zs_channel, info->x_char);
454 info->x_char = 0;
455 info->tx_active = 1;
456 goto out;
459 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped) {
460 write_zsreg(info->zs_channel, 0, RES_Tx_P);
461 goto out;
464 /* Send char */
465 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
466 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
467 info->xmit_cnt--;
468 info->tx_active = 1;
470 if (info->xmit_cnt < WAKEUP_CHARS)
471 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
473 out:
474 restore_flags(flags);
477 static _INLINE_ void status_handle(struct mac_serial *info)
479 unsigned char status;
481 /* Get status from Read Register 0 */
482 status = read_zsreg(info->zs_channel, 0);
484 /* Check for DCD transitions */
485 if (((status ^ info->read_reg_zero) & DCD) != 0
486 && info->tty && !C_CLOCAL(info->tty)) {
487 if (status & DCD) {
488 wake_up_interruptible(&info->open_wait);
489 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
490 if (info->tty)
491 tty_hangup(info->tty);
495 /* Check for CTS transitions */
496 if (info->tty && C_CRTSCTS(info->tty)) {
498 * For some reason, on the Power Macintosh,
499 * it seems that the CTS bit is 1 when CTS is
500 * *negated* and 0 when it is asserted.
501 * The DCD bit doesn't seem to be inverted
502 * like this.
504 if ((status & CTS) == 0) {
505 if (info->tx_stopped) {
506 #ifdef SERIAL_DEBUG_FLOW
507 printk("CTS up\n");
508 #endif
509 info->tx_stopped = 0;
510 if (!info->tx_active)
511 transmit_chars(info);
513 } else {
514 #ifdef SERIAL_DEBUG_FLOW
515 printk("CTS down\n");
516 #endif
517 info->tx_stopped = 1;
521 /* Clear status condition... */
522 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
523 info->read_reg_zero = status;
526 static _INLINE_ void receive_special_dma(struct mac_serial *info)
528 unsigned char stat, flag;
529 volatile struct dbdma_regs *rd = &info->rx->dma;
530 int where = RX_BUF_SIZE;
532 spin_lock(&info->rx_dma_lock);
533 if ((ld_le32(&rd->status) & ACTIVE) != 0)
534 dbdma_flush(rd);
535 if (in_le32(&rd->cmdptr)
536 == virt_to_bus(info->rx_cmds[info->rx_cbuf] + 1))
537 where -= in_le16(&info->rx->res_count);
538 where--;
540 stat = read_zsreg(info->zs_channel, R1);
542 flag = stat_to_flag(stat);
543 if (flag) {
544 info->rx_flag_buf[info->rx_cbuf][where] = flag;
545 /* reset the error indication */
546 write_zsreg(info->zs_channel, 0, ERR_RES);
549 spin_unlock(&info->rx_dma_lock);
553 * This is the serial driver's generic interrupt routine
555 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
557 struct mac_serial *info = (struct mac_serial *) dev_id;
558 unsigned char zs_intreg;
559 int shift;
561 if (!(info->flags & ZILOG_INITIALIZED)) {
562 printk("rs_interrupt: irq %d, port not initialized\n", irq);
563 disable_irq(irq);
564 return;
567 /* NOTE: The read register 3, which holds the irq status,
568 * does so for both channels on each chip. Although
569 * the status value itself must be read from the A
570 * channel and is only valid when read from channel A.
571 * Yes... broken hardware...
573 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
575 if (info->zs_chan_a == info->zs_channel)
576 shift = 3; /* Channel A */
577 else
578 shift = 0; /* Channel B */
580 for (;;) {
581 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
582 #ifdef SERIAL_DEBUG_INTR
583 printk("rs_interrupt: irq %d, zs_intreg 0x%x\n",
584 irq, (int)zs_intreg);
585 #endif
587 if ((zs_intreg & CHAN_IRQMASK) == 0)
588 break;
590 if (zs_intreg & CHBRxIP) {
591 /* If we are doing DMA, we only ask for interrupts
592 on characters with errors or special conditions. */
593 if (info->dma_initted)
594 receive_special_dma(info);
595 else
596 receive_chars(info, regs);
598 if (zs_intreg & CHBTxIP)
599 transmit_chars(info);
600 if (zs_intreg & CHBEXT)
601 status_handle(info);
605 /* Transmit DMA interrupt - not used at present */
606 static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs)
611 * Receive DMA interrupt.
613 static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs)
615 struct mac_serial *info = (struct mac_serial *) dev_id;
616 volatile struct dbdma_cmd *cd;
618 if (!info->dma_initted)
619 return;
620 spin_lock(&info->rx_dma_lock);
621 /* First, confirm that this interrupt is, indeed, coming */
622 /* from Rx DMA */
623 cd = info->rx_cmds[info->rx_cbuf] + 2;
624 if ((in_le16(&cd->xfer_status) & (RUN | ACTIVE)) != (RUN | ACTIVE)) {
625 spin_unlock(&info->rx_dma_lock);
626 return;
628 if (info->rx_fbuf != RX_NO_FBUF) {
629 info->rx_cbuf = info->rx_fbuf;
630 if (++info->rx_fbuf == info->rx_nbuf)
631 info->rx_fbuf = 0;
632 if (info->rx_fbuf == info->rx_ubuf)
633 info->rx_fbuf = RX_NO_FBUF;
635 spin_unlock(&info->rx_dma_lock);
639 * -------------------------------------------------------------------
640 * Here ends the serial interrupt routines.
641 * -------------------------------------------------------------------
645 * ------------------------------------------------------------
646 * rs_stop() and rs_start()
648 * This routines are called before setting or resetting tty->stopped.
649 * ------------------------------------------------------------
651 static void rs_stop(struct tty_struct *tty)
653 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
655 #ifdef SERIAL_DEBUG_STOP
656 printk("rs_stop %ld....\n",
657 tty->ldisc.chars_in_buffer(tty));
658 #endif
660 if (serial_paranoia_check(info, tty->device, "rs_stop"))
661 return;
663 #if 0
664 save_flags(flags); cli();
665 if (info->curregs[5] & TxENAB) {
666 info->curregs[5] &= ~TxENAB;
667 info->pendregs[5] &= ~TxENAB;
668 write_zsreg(info->zs_channel, 5, info->curregs[5]);
670 restore_flags(flags);
671 #endif
674 static void rs_start(struct tty_struct *tty)
676 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
677 unsigned long flags;
679 #ifdef SERIAL_DEBUG_STOP
680 printk("rs_start %ld....\n",
681 tty->ldisc.chars_in_buffer(tty));
682 #endif
684 if (serial_paranoia_check(info, tty->device, "rs_start"))
685 return;
687 save_flags(flags); cli();
688 #if 0
689 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
690 info->curregs[5] |= TxENAB;
691 info->pendregs[5] = info->curregs[5];
692 write_zsreg(info->zs_channel, 5, info->curregs[5]);
694 #else
695 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
696 transmit_chars(info);
698 #endif
699 restore_flags(flags);
703 * This routine is used to handle the "bottom half" processing for the
704 * serial driver, known also the "software interrupt" processing.
705 * This processing is done at the kernel interrupt level, after the
706 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
707 * is where time-consuming activities which can not be done in the
708 * interrupt driver proper are done; the interrupt driver schedules
709 * them using rs_sched_event(), and they get done here.
711 static void do_serial_bh(void)
713 run_task_queue(&tq_serial);
716 static void do_softint(void *private_)
718 struct mac_serial *info = (struct mac_serial *) private_;
719 struct tty_struct *tty;
721 tty = info->tty;
722 if (!tty)
723 return;
725 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
726 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
727 tty->ldisc.write_wakeup)
728 (tty->ldisc.write_wakeup)(tty);
729 wake_up_interruptible(&tty->write_wait);
733 static int startup(struct mac_serial * info, int can_sleep)
735 int delay;
737 OPNDBG("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
739 if (info->flags & ZILOG_INITIALIZED) {
740 OPNDBG(" -> already inited\n");
741 return 0;
744 if (!info->xmit_buf) {
745 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
746 if (!info->xmit_buf)
747 return -ENOMEM;
750 OPNDBG("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
752 delay = set_scc_power(info, 1);
754 setup_scc(info);
756 OPNDBG("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
758 info->flags |= ZILOG_INITIALIZED;
759 enable_irq(info->irq);
760 if (info->dma_initted) {
761 enable_irq(info->rx_dma_irq);
764 if (delay) {
765 if (can_sleep) {
766 /* we need to wait a bit before using the port */
767 current->state = TASK_INTERRUPTIBLE;
768 schedule_timeout(delay * HZ / 1000);
769 } else
770 mdelay(delay);
773 return 0;
776 static _INLINE_ void rxdma_start(struct mac_serial * info, int current)
778 volatile struct dbdma_regs *rd = &info->rx->dma;
779 volatile struct dbdma_cmd *cd = info->rx_cmds[current];
781 //printk(KERN_DEBUG "SCC: rxdma_start\n");
783 st_le32(&rd->cmdptr, virt_to_bus(cd));
784 out_le32(&rd->control, (RUN << 16) | RUN);
787 static void rxdma_to_tty(struct mac_serial *info)
789 struct tty_struct *tty = info->tty;
790 volatile struct dbdma_regs *rd = &info->rx->dma;
791 unsigned long flags;
792 int residue, available, space, do_queue;
794 if (!tty)
795 return;
797 do_queue = 0;
798 spin_lock_irqsave(&info->rx_dma_lock, flags);
799 more:
800 space = TTY_FLIPBUF_SIZE - tty->flip.count;
801 if (!space) {
802 do_queue++;
803 goto out;
805 residue = 0;
806 if (info->rx_ubuf == info->rx_cbuf) {
807 if ((ld_le32(&rd->status) & ACTIVE) != 0) {
808 dbdma_flush(rd);
809 if (in_le32(&rd->cmdptr)
810 == virt_to_bus(info->rx_cmds[info->rx_cbuf]+1))
811 residue = in_le16(&info->rx->res_count);
814 available = RX_BUF_SIZE - residue - info->rx_done_bytes;
815 if (available > space)
816 available = space;
817 if (available) {
818 memcpy(tty->flip.char_buf_ptr,
819 info->rx_char_buf[info->rx_ubuf] + info->rx_done_bytes,
820 available);
821 memcpy(tty->flip.flag_buf_ptr,
822 info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
823 available);
824 tty->flip.char_buf_ptr += available;
825 tty->flip.count += available;
826 tty->flip.flag_buf_ptr += available;
827 memset(info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
828 0, available);
829 info->rx_done_bytes += available;
830 do_queue++;
832 if (info->rx_done_bytes == RX_BUF_SIZE) {
833 volatile struct dbdma_cmd *cd = info->rx_cmds[info->rx_ubuf];
835 if (info->rx_ubuf == info->rx_cbuf)
836 goto out;
837 /* mark rx_char_buf[rx_ubuf] free */
838 st_le16(&cd->command, DBDMA_NOP);
839 cd++;
840 st_le32(&cd->cmd_dep, 0);
841 st_le32((unsigned int *)&cd->res_count, 0);
842 cd++;
843 st_le16(&cd->xfer_status, 0);
845 if (info->rx_fbuf == RX_NO_FBUF) {
846 info->rx_fbuf = info->rx_ubuf;
847 if (!(ld_le32(&rd->status) & ACTIVE)) {
848 dbdma_reset(&info->rx->dma);
849 rxdma_start(info, info->rx_ubuf);
850 info->rx_cbuf = info->rx_ubuf;
853 info->rx_done_bytes = 0;
854 if (++info->rx_ubuf == info->rx_nbuf)
855 info->rx_ubuf = 0;
856 if (info->rx_fbuf == info->rx_ubuf)
857 info->rx_fbuf = RX_NO_FBUF;
858 goto more;
860 out:
861 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
862 if (do_queue)
863 queue_task(&tty->flip.tqueue, &tq_timer);
866 static void poll_rxdma(void *private_)
868 struct mac_serial *info = (struct mac_serial *) private_;
869 unsigned long flags;
871 rxdma_to_tty(info);
872 spin_lock_irqsave(&info->rx_dma_lock, flags);
873 mod_timer(&info->poll_dma_timer, RX_DMA_TIMER);
874 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
877 static void dma_init(struct mac_serial * info)
879 int i, size;
880 volatile struct dbdma_cmd *cd;
881 unsigned char *p;
883 info->rx_nbuf = 8;
885 /* various mem set up */
886 size = sizeof(struct dbdma_cmd) * (3 * info->rx_nbuf + 2)
887 + (RX_BUF_SIZE * 2 + sizeof(*info->rx_cmds)
888 + sizeof(*info->rx_char_buf) + sizeof(*info->rx_flag_buf))
889 * info->rx_nbuf;
890 info->dma_priv = kmalloc(size, GFP_KERNEL | GFP_DMA);
891 if (info->dma_priv == NULL)
892 return;
893 memset(info->dma_priv, 0, size);
895 info->rx_cmds = (volatile struct dbdma_cmd **)info->dma_priv;
896 info->rx_char_buf = (unsigned char **) (info->rx_cmds + info->rx_nbuf);
897 info->rx_flag_buf = info->rx_char_buf + info->rx_nbuf;
898 p = (unsigned char *) (info->rx_flag_buf + info->rx_nbuf);
899 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
900 info->rx_char_buf[i] = p;
901 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
902 info->rx_flag_buf[i] = p;
904 /* a bit of DMA programming */
905 cd = info->rx_cmds[0] = (volatile struct dbdma_cmd *) DBDMA_ALIGN(p);
906 st_le16(&cd->command, DBDMA_NOP);
907 cd++;
908 st_le16(&cd->req_count, RX_BUF_SIZE);
909 st_le16(&cd->command, INPUT_MORE);
910 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[0]));
911 cd++;
912 st_le16(&cd->req_count, 4);
913 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
914 st_le32(&cd->phy_addr, virt_to_bus(cd-2));
915 st_le32(&cd->cmd_dep, DBDMA_STOP);
916 for (i = 1; i < info->rx_nbuf; i++) {
917 info->rx_cmds[i] = ++cd;
918 st_le16(&cd->command, DBDMA_NOP);
919 cd++;
920 st_le16(&cd->req_count, RX_BUF_SIZE);
921 st_le16(&cd->command, INPUT_MORE);
922 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[i]));
923 cd++;
924 st_le16(&cd->req_count, 4);
925 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
926 st_le32(&cd->phy_addr, virt_to_bus(cd-2));
927 st_le32(&cd->cmd_dep, DBDMA_STOP);
929 cd++;
930 st_le16(&cd->command, DBDMA_NOP | BR_ALWAYS);
931 st_le32(&cd->cmd_dep, virt_to_bus(info->rx_cmds[0]));
933 /* setup DMA to our liking */
934 dbdma_reset(&info->rx->dma);
935 st_le32(&info->rx->dma.intr_sel, 0x10001);
936 st_le32(&info->rx->dma.br_sel, 0x10001);
937 out_le32(&info->rx->dma.wait_sel, 0x10001);
939 /* set various flags */
940 info->rx_ubuf = 0;
941 info->rx_cbuf = 0;
942 info->rx_fbuf = info->rx_ubuf + 1;
943 if (info->rx_fbuf == info->rx_nbuf)
944 info->rx_fbuf = RX_NO_FBUF;
945 info->rx_done_bytes = 0;
947 /* setup polling */
948 init_timer(&info->poll_dma_timer);
949 info->poll_dma_timer.function = (void *)&poll_rxdma;
950 info->poll_dma_timer.data = (unsigned long)info;
952 info->dma_initted = 1;
955 static int setup_scc(struct mac_serial * info)
957 unsigned long flags;
959 OPNDBG("setting up ttys%d SCC...\n", info->line);
961 save_flags(flags); cli(); /* Disable interrupts */
964 * Reset the chip.
966 write_zsreg(info->zs_channel, 9,
967 (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
968 udelay(10);
969 write_zsreg(info->zs_channel, 9, 0);
972 * Clear the receive FIFO.
974 ZS_CLEARFIFO(info->zs_channel);
975 info->xmit_fifo_size = 1;
978 * Reset DMAs
980 if (info->has_dma)
981 dma_init(info);
984 * Clear the interrupt registers.
986 write_zsreg(info->zs_channel, 0, ERR_RES);
987 write_zsreg(info->zs_channel, 0, RES_H_IUS);
990 * Turn on RTS and DTR.
992 if (!info->is_irda)
993 zs_rtsdtr(info, 1);
996 * Finally, enable sequencing and interrupts
998 if (!info->dma_initted) {
999 /* interrupt on ext/status changes, all received chars,
1000 transmit ready */
1001 info->curregs[1] = (info->curregs[1] & ~0x18)
1002 | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
1003 } else {
1004 /* interrupt on ext/status changes, W/Req pin is
1005 receive DMA request */
1006 info->curregs[1] = (info->curregs[1] & ~(0x18 | TxINT_ENAB))
1007 | (EXT_INT_ENAB | WT_RDY_RT | WT_FN_RDYFN);
1008 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1009 /* enable W/Req pin */
1010 info->curregs[1] |= WT_RDY_ENAB;
1011 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1012 /* enable interrupts on transmit ready and receive errors */
1013 info->curregs[1] |= INT_ERR_Rx | TxINT_ENAB;
1015 info->pendregs[1] = info->curregs[1];
1016 info->curregs[3] |= (RxENABLE | Rx8);
1017 info->pendregs[3] = info->curregs[3];
1018 info->curregs[5] |= (TxENAB | Tx8);
1019 info->pendregs[5] = info->curregs[5];
1020 info->curregs[9] |= (NV | MIE);
1021 info->pendregs[9] = info->curregs[9];
1022 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1023 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1024 write_zsreg(info->zs_channel, 9, info->curregs[9]);
1026 if (info->tty)
1027 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1028 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1031 * Set the speed of the serial port
1033 change_speed(info, 0);
1035 /* Save the current value of RR0 */
1036 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1038 restore_flags(flags);
1040 if (info->dma_initted) {
1041 spin_lock_irqsave(&info->rx_dma_lock, flags);
1042 rxdma_start(info, 0);
1043 info->poll_dma_timer.expires = RX_DMA_TIMER;
1044 add_timer(&info->poll_dma_timer);
1045 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
1048 return 0;
1052 * This routine will shutdown a serial port; interrupts are disabled, and
1053 * DTR is dropped if the hangup on close termio flag is on.
1055 static void shutdown(struct mac_serial * info)
1057 OPNDBG("Shutting down serial port %d (irq %d)....\n", info->line,
1058 info->irq);
1060 if (!(info->flags & ZILOG_INITIALIZED)) {
1061 OPNDBG("(already shutdown)\n");
1062 return;
1065 if (info->has_dma) {
1066 del_timer(&info->poll_dma_timer);
1067 dbdma_reset(info->tx_dma);
1068 dbdma_reset(&info->rx->dma);
1069 disable_irq(info->tx_dma_irq);
1070 disable_irq(info->rx_dma_irq);
1072 disable_irq(info->irq);
1074 info->pendregs[1] = info->curregs[1] = 0;
1075 write_zsreg(info->zs_channel, 1, 0); /* no interrupts */
1077 info->curregs[3] &= ~RxENABLE;
1078 info->pendregs[3] = info->curregs[3];
1079 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1081 info->curregs[5] &= ~TxENAB;
1082 if (!info->tty || C_HUPCL(info->tty))
1083 info->curregs[5] &= ~(DTR | RTS);
1084 info->pendregs[5] = info->curregs[5];
1085 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1087 if (info->tty)
1088 set_bit(TTY_IO_ERROR, &info->tty->flags);
1090 set_scc_power(info, 0);
1092 if (info->xmit_buf) {
1093 free_page((unsigned long) info->xmit_buf);
1094 info->xmit_buf = 0;
1097 if (info->has_dma && info->dma_priv) {
1098 kfree(info->dma_priv);
1099 info->dma_priv = NULL;
1100 info->dma_initted = 0;
1103 memset(info->curregs, 0, sizeof(info->curregs));
1104 memset(info->curregs, 0, sizeof(info->pendregs));
1106 info->flags &= ~ZILOG_INITIALIZED;
1110 * Turn power on or off to the SCC and associated stuff
1111 * (port drivers, modem, IR port, etc.)
1112 * Returns the number of milliseconds we should wait before
1113 * trying to use the port.
1115 static int set_scc_power(struct mac_serial * info, int state)
1117 int delay = 0;
1119 if (feature_test(info->dev_node, FEATURE_Serial_enable) < 0)
1120 return 0; /* don't have serial power control */
1122 /* The timings looks strange but that's the ones MacOS seems
1123 to use for the internal modem. I think we can use a lot faster
1124 ones, at least whe not using the modem, this should be tested.
1126 if (state) {
1127 PWRDBG("ttyS%02d: powering up hardware\n", info->line);
1128 if (feature_test(info->dev_node, FEATURE_Serial_enable) == 0) {
1129 feature_set(info->dev_node, FEATURE_Serial_enable);
1130 mdelay(10);
1131 feature_set(info->dev_node, FEATURE_Serial_reset);
1132 mdelay(15);
1133 feature_clear(info->dev_node, FEATURE_Serial_reset);
1134 mdelay(10);
1136 if (info->zs_chan_a == info->zs_channel)
1137 feature_set(info->dev_node, FEATURE_Serial_IO_A);
1138 else
1139 feature_set(info->dev_node, FEATURE_Serial_IO_B);
1140 delay = 10;
1141 if (info->is_cobalt_modem){
1142 mdelay(300);
1143 feature_set(info->dev_node, FEATURE_Modem_power);
1144 mdelay(5);
1145 feature_clear(info->dev_node, FEATURE_Modem_power);
1146 mdelay(10);
1147 feature_set(info->dev_node, FEATURE_Modem_power);
1148 delay = 2500; /* wait for 2.5s before using */
1150 #ifdef CONFIG_PMAC_PBOOK
1151 if (info->is_irda)
1152 pmu_enable_irled(1);
1153 #endif /* CONFIG_PMAC_PBOOK */
1154 } else {
1155 PWRDBG("ttyS%02d: shutting down hardware\n", info->line);
1156 if (info->is_cobalt_modem) {
1157 PWRDBG("ttyS%02d: shutting down modem\n", info->line);
1158 feature_clear(info->dev_node, FEATURE_Modem_power);
1159 mdelay(10);
1161 #ifdef CONFIG_PMAC_PBOOK
1162 if (info->is_irda)
1163 pmu_enable_irled(0);
1164 #endif /* CONFIG_PMAC_PBOOK */
1166 if (info->zs_chan_a == info->zs_channel && !info->is_irda) {
1167 PWRDBG("ttyS%02d: shutting down SCC channel A\n", info->line);
1168 feature_clear(info->dev_node, FEATURE_Serial_IO_A);
1169 } else if (!info->is_irda) {
1170 PWRDBG("ttyS%02d: shutting down SCC channel B\n", info->line);
1171 feature_clear(info->dev_node, FEATURE_Serial_IO_B);
1173 /* XXX for now, shut down SCC core only on powerbooks */
1174 if (is_powerbook
1175 && !(feature_test(info->dev_node, FEATURE_Serial_IO_A) ||
1176 feature_test(info->dev_node, FEATURE_Serial_IO_B))) {
1177 PWRDBG("ttyS%02d: shutting down SCC core\n", info->line);
1178 feature_set(info->dev_node, FEATURE_Serial_reset);
1179 mdelay(15);
1180 feature_clear(info->dev_node, FEATURE_Serial_reset);
1181 mdelay(25);
1182 feature_clear(info->dev_node, FEATURE_Serial_enable);
1183 mdelay(5);
1186 return delay;
1189 static void irda_rts_pulses(struct mac_serial *info, int w)
1191 unsigned long flags;
1193 udelay(w);
1194 save_flags(flags); cli();
1195 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1196 udelay(2);
1197 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1198 udelay(8);
1199 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1200 udelay(4);
1201 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1202 restore_flags(flags);
1206 * Set the irda codec on the imac to the specified baud rate.
1208 static void irda_setup(struct mac_serial *info)
1210 int code, speed, t;
1211 unsigned long flags;
1213 speed = info->tty->termios->c_cflag & CBAUD;
1214 if (speed < B2400 || speed > B115200)
1215 return;
1216 code = 0x4d + B115200 - speed;
1218 /* disable serial interrupts and receive DMA */
1219 write_zsreg(info->zs_channel, 1, info->curregs[1] & ~0x9f);
1221 /* wait for transmitter to drain */
1222 t = 10000;
1223 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0
1224 || (read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1225 if (--t <= 0) {
1226 printk(KERN_ERR "transmitter didn't drain\n");
1227 return;
1229 udelay(10);
1231 udelay(100);
1233 /* set to 8 bits, no parity, 19200 baud, RTS on, DTR off */
1234 write_zsreg(info->zs_channel, 4, X16CLK | SB1);
1235 write_zsreg(info->zs_channel, 11, TCBR | RCBR);
1236 t = BPS_TO_BRG(19200, ZS_CLOCK/16);
1237 write_zsreg(info->zs_channel, 12, t);
1238 write_zsreg(info->zs_channel, 13, t >> 8);
1239 write_zsreg(info->zs_channel, 14, BRENABL);
1240 write_zsreg(info->zs_channel, 3, Rx8 | RxENABLE);
1241 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1243 /* set TxD low for ~104us and pulse RTS */
1244 udelay(1000);
1245 save_flags(flags); cli();
1246 write_zsdata(info->zs_channel, 0xfe);
1247 irda_rts_pulses(info, 150);
1248 restore_flags(flags);
1249 irda_rts_pulses(info, 180);
1250 irda_rts_pulses(info, 50);
1251 udelay(100);
1253 /* assert DTR, wait 30ms, talk to the chip */
1254 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS | DTR);
1255 udelay(30000);
1256 while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV)
1257 read_zsdata(info->zs_channel);
1259 write_zsdata(info->zs_channel, 1);
1260 t = 1000;
1261 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1262 if (--t <= 0) {
1263 printk(KERN_ERR "irda_setup timed out on 1st byte\n");
1264 goto out;
1266 udelay(10);
1268 t = read_zsdata(info->zs_channel);
1269 if (t != 4)
1270 printk(KERN_ERR "irda_setup 1st byte = %x\n", t);
1272 write_zsdata(info->zs_channel, code);
1273 t = 1000;
1274 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1275 if (--t <= 0) {
1276 printk(KERN_ERR "irda_setup timed out on 2nd byte\n");
1277 goto out;
1279 udelay(10);
1281 t = read_zsdata(info->zs_channel);
1282 if (t != code)
1283 printk(KERN_ERR "irda_setup 2nd byte = %x (%x)\n", t, code);
1285 /* Drop DTR again and do some more RTS pulses */
1286 out:
1287 udelay(100);
1288 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1289 irda_rts_pulses(info, 80);
1291 /* We should be right to go now. We assume that load_zsregs
1292 will get called soon to load up the correct baud rate etc. */
1293 info->curregs[5] = (info->curregs[5] | RTS) & ~DTR;
1294 info->pendregs[5] = info->curregs[5];
1298 * This routine is called to set the UART divisor registers to match
1299 * the specified baud rate for a serial port.
1301 static void change_speed(struct mac_serial *info, struct termios *old_termios)
1303 unsigned cflag;
1304 int bits;
1305 int brg, baud;
1306 unsigned long flags;
1308 if (!info->tty || !info->tty->termios)
1309 return;
1311 cflag = info->tty->termios->c_cflag;
1312 baud = tty_get_baud_rate(info->tty);
1313 if (baud == 0) {
1314 if (old_termios) {
1315 info->tty->termios->c_cflag &= ~CBAUD;
1316 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1317 cflag = info->tty->termios->c_cflag;
1318 baud = tty_get_baud_rate(info->tty);
1320 else
1321 baud = info->zs_baud;
1323 if (baud > 230400)
1324 baud = 230400;
1325 else if (baud == 0)
1326 baud = 38400;
1328 save_flags(flags); cli();
1329 info->zs_baud = baud;
1330 info->clk_divisor = 16;
1332 BAUDBG(KERN_DEBUG "set speed to %d bds, ", baud);
1334 switch (baud) {
1335 case ZS_CLOCK/16: /* 230400 */
1336 info->curregs[4] = X16CLK;
1337 info->curregs[11] = 0;
1338 break;
1339 case ZS_CLOCK/32: /* 115200 */
1340 info->curregs[4] = X32CLK;
1341 info->curregs[11] = 0;
1342 break;
1343 default:
1344 info->curregs[4] = X16CLK;
1345 info->curregs[11] = TCBR | RCBR;
1346 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
1347 info->curregs[12] = (brg & 255);
1348 info->curregs[13] = ((brg >> 8) & 255);
1349 info->curregs[14] = BRENABL;
1352 /* byte size and parity */
1353 info->curregs[3] &= ~RxNBITS_MASK;
1354 info->curregs[5] &= ~TxNBITS_MASK;
1355 switch (cflag & CSIZE) {
1356 case CS5:
1357 info->curregs[3] |= Rx5;
1358 info->curregs[5] |= Tx5;
1359 BAUDBG("5 bits, ");
1360 bits = 7;
1361 break;
1362 case CS6:
1363 info->curregs[3] |= Rx6;
1364 info->curregs[5] |= Tx6;
1365 BAUDBG("6 bits, ");
1366 bits = 8;
1367 break;
1368 case CS7:
1369 info->curregs[3] |= Rx7;
1370 info->curregs[5] |= Tx7;
1371 BAUDBG("7 bits, ");
1372 bits = 9;
1373 break;
1374 case CS8:
1375 default: /* defaults to 8 bits */
1376 info->curregs[3] |= Rx8;
1377 info->curregs[5] |= Tx8;
1378 BAUDBG("8 bits, ");
1379 bits = 10;
1380 break;
1382 info->pendregs[3] = info->curregs[3];
1383 info->pendregs[5] = info->curregs[5];
1385 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
1386 if (cflag & CSTOPB) {
1387 info->curregs[4] |= SB2;
1388 bits++;
1389 BAUDBG("2 stop, ");
1390 } else {
1391 info->curregs[4] |= SB1;
1392 BAUDBG("1 stop, ");
1394 if (cflag & PARENB) {
1395 bits++;
1396 info->curregs[4] |= PAR_ENA;
1397 BAUDBG("parity, ");
1399 if (!(cflag & PARODD)) {
1400 info->curregs[4] |= PAR_EVEN;
1402 info->pendregs[4] = info->curregs[4];
1404 if (!(cflag & CLOCAL)) {
1405 if (!(info->curregs[15] & DCDIE))
1406 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1407 info->curregs[15] |= DCDIE;
1408 } else
1409 info->curregs[15] &= ~DCDIE;
1410 if (cflag & CRTSCTS) {
1411 info->curregs[15] |= CTSIE;
1412 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
1413 info->tx_stopped = 1;
1414 } else {
1415 info->curregs[15] &= ~CTSIE;
1416 info->tx_stopped = 0;
1418 info->pendregs[15] = info->curregs[15];
1420 /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
1421 This code would love a larger HZ and a >1 fifo size, but this is not
1422 a priority. The resulting value must be >HZ/2
1424 info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
1425 info->timeout += HZ/50+1; /* Add .02 seconds of slop */
1427 BAUDBG("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ,
1428 (int)info->baud_base);
1430 /* set the irda codec to the right rate */
1431 if (info->is_irda)
1432 irda_setup(info);
1434 /* Load up the new values */
1435 load_zsregs(info->zs_channel, info->curregs);
1437 restore_flags(flags);
1440 static void rs_flush_chars(struct tty_struct *tty)
1442 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1444 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1445 return;
1447 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
1448 !info->xmit_buf)
1449 return;
1451 /* Enable transmitter */
1452 transmit_chars(info);
1455 static int rs_write(struct tty_struct * tty, int from_user,
1456 const unsigned char *buf, int count)
1458 int c, ret = 0;
1459 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1460 unsigned long flags;
1462 if (serial_paranoia_check(info, tty->device, "rs_write"))
1463 return 0;
1465 if (!tty || !info->xmit_buf || !tmp_buf)
1466 return 0;
1468 if (from_user) {
1469 down(&tmp_buf_sem);
1470 while (1) {
1471 c = MIN(count,
1472 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1473 SERIAL_XMIT_SIZE - info->xmit_head));
1474 if (c <= 0)
1475 break;
1477 c -= copy_from_user(tmp_buf, buf, c);
1478 if (!c) {
1479 if (!ret)
1480 ret = -EFAULT;
1481 break;
1483 save_flags(flags);
1484 cli();
1485 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1486 SERIAL_XMIT_SIZE - info->xmit_head));
1487 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1488 info->xmit_head = ((info->xmit_head + c) &
1489 (SERIAL_XMIT_SIZE-1));
1490 info->xmit_cnt += c;
1491 restore_flags(flags);
1492 buf += c;
1493 count -= c;
1494 ret += c;
1496 up(&tmp_buf_sem);
1497 } else {
1498 while (1) {
1499 save_flags(flags);
1500 cli();
1501 c = MIN(count,
1502 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1503 SERIAL_XMIT_SIZE - info->xmit_head));
1504 if (c <= 0) {
1505 restore_flags(flags);
1506 break;
1508 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1509 info->xmit_head = ((info->xmit_head + c) &
1510 (SERIAL_XMIT_SIZE-1));
1511 info->xmit_cnt += c;
1512 restore_flags(flags);
1513 buf += c;
1514 count -= c;
1515 ret += c;
1518 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1519 && !info->tx_active)
1520 transmit_chars(info);
1521 return ret;
1524 static int rs_write_room(struct tty_struct *tty)
1526 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1527 int ret;
1529 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1530 return 0;
1531 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1532 if (ret < 0)
1533 ret = 0;
1534 return ret;
1537 static int rs_chars_in_buffer(struct tty_struct *tty)
1539 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1541 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1542 return 0;
1543 return info->xmit_cnt;
1546 static void rs_flush_buffer(struct tty_struct *tty)
1548 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1549 unsigned long flags;
1551 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1552 return;
1553 save_flags(flags); cli();
1554 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1555 restore_flags(flags);
1556 wake_up_interruptible(&tty->write_wait);
1557 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1558 tty->ldisc.write_wakeup)
1559 (tty->ldisc.write_wakeup)(tty);
1563 * ------------------------------------------------------------
1564 * rs_throttle()
1566 * This routine is called by the upper-layer tty layer to signal that
1567 * incoming characters should be throttled.
1568 * ------------------------------------------------------------
1570 static void rs_throttle(struct tty_struct * tty)
1572 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1573 unsigned long flags;
1574 #ifdef SERIAL_DEBUG_THROTTLE
1575 printk("throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1576 #endif
1578 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1579 return;
1581 if (I_IXOFF(tty)) {
1582 save_flags(flags); cli();
1583 info->x_char = STOP_CHAR(tty);
1584 if (!info->tx_active)
1585 transmit_chars(info);
1586 restore_flags(flags);
1589 if (C_CRTSCTS(tty)) {
1591 * Here we want to turn off the RTS line. On Macintoshes,
1592 * we only get the DTR line, which goes to both DTR and
1593 * RTS on the modem. RTS doesn't go out to the serial
1594 * port socket. So you should make sure your modem is
1595 * set to ignore DTR if you're using CRTSCTS.
1597 save_flags(flags); cli();
1598 info->curregs[5] &= ~(DTR | RTS);
1599 info->pendregs[5] &= ~(DTR | RTS);
1600 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1601 restore_flags(flags);
1605 static void rs_unthrottle(struct tty_struct * tty)
1607 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1608 unsigned long flags;
1609 #ifdef SERIAL_DEBUG_THROTTLE
1610 printk("unthrottle %s: %d....\n",tty->ldisc.chars_in_buffer(tty));
1611 #endif
1613 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1614 return;
1616 if (I_IXOFF(tty)) {
1617 save_flags(flags); cli();
1618 if (info->x_char)
1619 info->x_char = 0;
1620 else {
1621 info->x_char = START_CHAR(tty);
1622 if (!info->tx_active)
1623 transmit_chars(info);
1625 restore_flags(flags);
1628 if (C_CRTSCTS(tty)) {
1629 /* Assert RTS and DTR lines */
1630 save_flags(flags); cli();
1631 info->curregs[5] |= DTR | RTS;
1632 info->pendregs[5] |= DTR | RTS;
1633 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1634 restore_flags(flags);
1639 * ------------------------------------------------------------
1640 * rs_ioctl() and friends
1641 * ------------------------------------------------------------
1644 static int get_serial_info(struct mac_serial * info,
1645 struct serial_struct * retinfo)
1647 struct serial_struct tmp;
1649 if (!retinfo)
1650 return -EFAULT;
1651 memset(&tmp, 0, sizeof(tmp));
1652 tmp.type = info->type;
1653 tmp.line = info->line;
1654 tmp.port = info->port;
1655 tmp.irq = info->irq;
1656 tmp.flags = info->flags;
1657 tmp.baud_base = info->baud_base;
1658 tmp.close_delay = info->close_delay;
1659 tmp.closing_wait = info->closing_wait;
1660 tmp.custom_divisor = info->custom_divisor;
1661 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1662 return -EFAULT;
1663 return 0;
1666 static int set_serial_info(struct mac_serial * info,
1667 struct serial_struct * new_info)
1669 struct serial_struct new_serial;
1670 struct mac_serial old_info;
1671 int retval = 0;
1673 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1674 return -EFAULT;
1675 old_info = *info;
1677 if (!capable(CAP_SYS_ADMIN)) {
1678 if ((new_serial.baud_base != info->baud_base) ||
1679 (new_serial.type != info->type) ||
1680 (new_serial.close_delay != info->close_delay) ||
1681 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1682 (info->flags & ~ZILOG_USR_MASK)))
1683 return -EPERM;
1684 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1685 (new_serial.flags & ZILOG_USR_MASK));
1686 info->custom_divisor = new_serial.custom_divisor;
1687 goto check_and_exit;
1690 if (info->count > 1)
1691 return -EBUSY;
1694 * OK, past this point, all the error checking has been done.
1695 * At this point, we start making changes.....
1698 info->baud_base = new_serial.baud_base;
1699 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1700 (new_serial.flags & ZILOG_FLAGS));
1701 info->type = new_serial.type;
1702 info->close_delay = new_serial.close_delay;
1703 info->closing_wait = new_serial.closing_wait;
1705 check_and_exit:
1706 if (info->flags & ZILOG_INITIALIZED)
1707 retval = setup_scc(info);
1708 return retval;
1712 * get_lsr_info - get line status register info
1714 * Purpose: Let user call ioctl() to get info when the UART physically
1715 * is emptied. On bus types like RS485, the transmitter must
1716 * release the bus after transmitting. This must be done when
1717 * the transmit shift register is empty, not be done when the
1718 * transmit holding register is empty. This functionality
1719 * allows an RS485 driver to be written in user space.
1721 static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1723 unsigned char status;
1724 unsigned long flags;
1726 save_flags(flags); cli();
1727 status = read_zsreg(info->zs_channel, 0);
1728 restore_flags(flags);
1729 status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0;
1730 return put_user(status,value);
1733 static int get_modem_info(struct mac_serial *info, unsigned int *value)
1735 unsigned char control, status;
1736 unsigned int result;
1737 unsigned long flags;
1739 save_flags(flags); cli();
1740 control = info->curregs[5];
1741 status = read_zsreg(info->zs_channel, 0);
1742 restore_flags(flags);
1743 result = ((control & RTS) ? TIOCM_RTS: 0)
1744 | ((control & DTR) ? TIOCM_DTR: 0)
1745 | ((status & DCD) ? TIOCM_CAR: 0)
1746 | ((status & CTS) ? 0: TIOCM_CTS);
1747 return put_user(result,value);
1750 static int set_modem_info(struct mac_serial *info, unsigned int cmd,
1751 unsigned int *value)
1753 int error;
1754 unsigned int arg, bits;
1755 unsigned long flags;
1757 error = get_user(arg, value);
1758 if (error)
1759 return error;
1760 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1761 save_flags(flags); cli();
1762 switch (cmd) {
1763 case TIOCMBIS:
1764 info->curregs[5] |= bits;
1765 break;
1766 case TIOCMBIC:
1767 info->curregs[5] &= ~bits;
1768 break;
1769 case TIOCMSET:
1770 info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits;
1771 break;
1772 default:
1773 restore_flags(flags);
1774 return -EINVAL;
1776 info->pendregs[5] = info->curregs[5];
1777 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1778 restore_flags(flags);
1779 return 0;
1783 * rs_break - turn transmit break condition on/off
1785 static void rs_break(struct tty_struct *tty, int break_state)
1787 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1788 unsigned long flags;
1790 if (serial_paranoia_check(info, tty->device, "rs_break"))
1791 return;
1793 save_flags(flags); cli();
1794 if (break_state == -1)
1795 info->curregs[5] |= SND_BRK;
1796 else
1797 info->curregs[5] &= ~SND_BRK;
1798 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1799 restore_flags(flags);
1802 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1803 unsigned int cmd, unsigned long arg)
1805 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1807 #ifdef CONFIG_KGDB
1808 if (info->kgdb_channel)
1809 return -ENODEV;
1810 #endif
1811 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1812 return -ENODEV;
1814 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1815 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) {
1816 if (tty->flags & (1 << TTY_IO_ERROR))
1817 return -EIO;
1820 switch (cmd) {
1821 case TIOCMGET:
1822 return get_modem_info(info, (unsigned int *) arg);
1823 case TIOCMBIS:
1824 case TIOCMBIC:
1825 case TIOCMSET:
1826 return set_modem_info(info, cmd, (unsigned int *) arg);
1827 case TIOCGSERIAL:
1828 return get_serial_info(info,
1829 (struct serial_struct *) arg);
1830 case TIOCSSERIAL:
1831 return set_serial_info(info,
1832 (struct serial_struct *) arg);
1833 case TIOCSERGETLSR: /* Get line status register */
1834 return get_lsr_info(info, (unsigned int *) arg);
1836 case TIOCSERGSTRUCT:
1837 if (copy_to_user((struct mac_serial *) arg,
1838 info, sizeof(struct mac_serial)))
1839 return -EFAULT;
1840 return 0;
1842 default:
1843 return -ENOIOCTLCMD;
1845 return 0;
1848 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1850 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1851 int was_stopped;
1853 if (tty->termios->c_cflag == old_termios->c_cflag)
1854 return;
1855 was_stopped = info->tx_stopped;
1857 change_speed(info, old_termios);
1859 if (was_stopped && !info->tx_stopped) {
1860 tty->hw_stopped = 0;
1861 rs_start(tty);
1866 * ------------------------------------------------------------
1867 * rs_close()
1869 * This routine is called when the serial port gets closed.
1870 * Wait for the last remaining data to be sent.
1871 * ------------------------------------------------------------
1873 static void rs_close(struct tty_struct *tty, struct file * filp)
1875 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1876 unsigned long flags;
1878 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1879 return;
1881 save_flags(flags); cli();
1883 if (tty_hung_up_p(filp)) {
1884 MOD_DEC_USE_COUNT;
1885 restore_flags(flags);
1886 return;
1889 OPNDBG("rs_close ttys%d, count = %d\n", info->line, info->count);
1890 if ((tty->count == 1) && (info->count != 1)) {
1892 * Uh, oh. tty->count is 1, which means that the tty
1893 * structure will be freed. Info->count should always
1894 * be one in these conditions. If it's greater than
1895 * one, we've got real problems, since it means the
1896 * serial port won't be shutdown.
1898 printk("rs_close: bad serial port count; tty->count is 1, "
1899 "info->count is %d\n", info->count);
1900 info->count = 1;
1902 if (--info->count < 0) {
1903 printk("rs_close: bad serial port count for ttys%d: %d\n",
1904 info->line, info->count);
1905 info->count = 0;
1907 if (info->count) {
1908 MOD_DEC_USE_COUNT;
1909 restore_flags(flags);
1910 return;
1912 info->flags |= ZILOG_CLOSING;
1914 * Save the termios structure, since this port may have
1915 * separate termios for callout and dialin.
1917 if (info->flags & ZILOG_NORMAL_ACTIVE)
1918 info->normal_termios = *tty->termios;
1919 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1920 info->callout_termios = *tty->termios;
1922 * Now we wait for the transmit buffer to clear; and we notify
1923 * the line discipline to only process XON/XOFF characters.
1925 OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1926 tty->closing = 1;
1927 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) {
1928 restore_flags(flags);
1929 tty_wait_until_sent(tty, info->closing_wait);
1930 save_flags(flags); cli();
1934 * At this point we stop accepting input. To do this, we
1935 * disable the receiver and receive interrupts.
1937 info->curregs[3] &= ~RxENABLE;
1938 info->pendregs[3] = info->curregs[3];
1939 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1940 info->curregs[1] &= ~(0x18); /* disable any rx ints */
1941 info->pendregs[1] = info->curregs[1];
1942 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1943 ZS_CLEARFIFO(info->zs_channel);
1944 if (info->flags & ZILOG_INITIALIZED) {
1946 * Before we drop DTR, make sure the SCC transmitter
1947 * has completely drained.
1949 OPNDBG("waiting end of Rx...\n");
1950 restore_flags(flags);
1951 rs_wait_until_sent(tty, info->timeout);
1952 save_flags(flags); cli();
1955 shutdown(info);
1956 /* restore flags now since shutdown() will have disabled this port's
1957 specific irqs */
1958 restore_flags(flags);
1960 if (tty->driver.flush_buffer)
1961 tty->driver.flush_buffer(tty);
1962 if (tty->ldisc.flush_buffer)
1963 tty->ldisc.flush_buffer(tty);
1964 tty->closing = 0;
1965 info->event = 0;
1966 info->tty = 0;
1968 if (info->blocked_open) {
1969 if (info->close_delay) {
1970 current->state = TASK_INTERRUPTIBLE;
1971 schedule_timeout(info->close_delay);
1973 wake_up_interruptible(&info->open_wait);
1975 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1976 ZILOG_CLOSING);
1977 wake_up_interruptible(&info->close_wait);
1978 MOD_DEC_USE_COUNT;
1982 * rs_wait_until_sent() --- wait until the transmitter is empty
1984 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1986 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1987 unsigned long orig_jiffies, char_time;
1989 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1990 return;
1992 /* printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
1993 timeout, tty->stopped, info->tx_stopped);
1995 orig_jiffies = jiffies;
1997 * Set the check interval to be 1/5 of the estimated time to
1998 * send a single character, and make it at least 1. The check
1999 * interval should also be less than the timeout.
2001 if (info->timeout <= HZ/50) {
2002 printk("macserial: invalid info->timeout=%d\n", info->timeout);
2003 info->timeout = HZ/50+1;
2006 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2007 char_time = char_time / 5;
2008 if (char_time > HZ) {
2009 printk("macserial: char_time %ld >HZ !!!\n", char_time);
2010 char_time = 1;
2011 } else if (char_time == 0)
2012 char_time = 1;
2013 if (timeout)
2014 char_time = MIN(char_time, timeout);
2015 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
2016 current->state = TASK_INTERRUPTIBLE;
2017 schedule_timeout(char_time);
2018 if (signal_pending(current))
2019 break;
2020 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2021 break;
2023 current->state = TASK_RUNNING;
2027 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2029 static void rs_hangup(struct tty_struct *tty)
2031 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
2033 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2034 return;
2036 rs_flush_buffer(tty);
2037 shutdown(info);
2038 info->event = 0;
2039 info->count = 0;
2040 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
2041 info->tty = 0;
2042 wake_up_interruptible(&info->open_wait);
2046 * ------------------------------------------------------------
2047 * rs_open() and friends
2048 * ------------------------------------------------------------
2050 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2051 struct mac_serial *info)
2053 DECLARE_WAITQUEUE(wait,current);
2054 int retval;
2055 int do_clocal = 0;
2058 * If the device is in the middle of being closed, then block
2059 * until it's done, and then try again.
2061 if (info->flags & ZILOG_CLOSING) {
2062 interruptible_sleep_on(&info->close_wait);
2063 #ifdef SERIAL_DO_RESTART
2064 return ((info->flags & ZILOG_HUP_NOTIFY) ?
2065 -EAGAIN : -ERESTARTSYS);
2066 #else
2067 return -EAGAIN;
2068 #endif
2072 * If this is a callout device, then just make sure the normal
2073 * device isn't being used.
2075 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2076 if (info->flags & ZILOG_NORMAL_ACTIVE)
2077 return -EBUSY;
2078 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
2079 (info->flags & ZILOG_SESSION_LOCKOUT) &&
2080 (info->session != current->session))
2081 return -EBUSY;
2082 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
2083 (info->flags & ZILOG_PGRP_LOCKOUT) &&
2084 (info->pgrp != current->pgrp))
2085 return -EBUSY;
2086 info->flags |= ZILOG_CALLOUT_ACTIVE;
2087 return 0;
2091 * If non-blocking mode is set, or the port is not enabled,
2092 * then make the check up front and then exit.
2094 if ((filp->f_flags & O_NONBLOCK) ||
2095 (tty->flags & (1 << TTY_IO_ERROR))) {
2096 if (info->flags & ZILOG_CALLOUT_ACTIVE)
2097 return -EBUSY;
2098 info->flags |= ZILOG_NORMAL_ACTIVE;
2099 return 0;
2102 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
2103 if (info->normal_termios.c_cflag & CLOCAL)
2104 do_clocal = 1;
2105 } else {
2106 if (tty->termios->c_cflag & CLOCAL)
2107 do_clocal = 1;
2111 * Block waiting for the carrier detect and the line to become
2112 * free (i.e., not in use by the callout). While we are in
2113 * this loop, info->count is dropped by one, so that
2114 * rs_close() knows when to free things. We restore it upon
2115 * exit, either normal or abnormal.
2117 retval = 0;
2118 add_wait_queue(&info->open_wait, &wait);
2119 OPNDBG("block_til_ready before block: ttys%d, count = %d\n",
2120 info->line, info->count);
2121 cli();
2122 if (!tty_hung_up_p(filp))
2123 info->count--;
2124 sti();
2125 info->blocked_open++;
2126 while (1) {
2127 cli();
2128 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
2129 (tty->termios->c_cflag & CBAUD) &&
2130 !info->is_irda)
2131 zs_rtsdtr(info, 1);
2132 sti();
2133 set_current_state(TASK_INTERRUPTIBLE);
2134 if (tty_hung_up_p(filp) ||
2135 !(info->flags & ZILOG_INITIALIZED)) {
2136 #ifdef SERIAL_DO_RESTART
2137 if (info->flags & ZILOG_HUP_NOTIFY)
2138 retval = -EAGAIN;
2139 else
2140 retval = -ERESTARTSYS;
2141 #else
2142 retval = -EAGAIN;
2143 #endif
2144 break;
2146 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
2147 !(info->flags & ZILOG_CLOSING) &&
2148 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
2149 break;
2150 if (signal_pending(current)) {
2151 retval = -ERESTARTSYS;
2152 break;
2154 OPNDBG("block_til_ready blocking: ttys%d, count = %d\n",
2155 info->line, info->count);
2156 schedule();
2158 current->state = TASK_RUNNING;
2159 remove_wait_queue(&info->open_wait, &wait);
2160 if (!tty_hung_up_p(filp))
2161 info->count++;
2162 info->blocked_open--;
2163 OPNDBG("block_til_ready after blocking: ttys%d, count = %d\n",
2164 info->line, info->count);
2165 if (retval)
2166 return retval;
2167 info->flags |= ZILOG_NORMAL_ACTIVE;
2168 return 0;
2172 * This routine is called whenever a serial port is opened. It
2173 * enables interrupts for a serial port, linking in its ZILOG structure into
2174 * the IRQ chain. It also performs the serial-specific
2175 * initialization for the tty structure.
2177 static int rs_open(struct tty_struct *tty, struct file * filp)
2179 struct mac_serial *info;
2180 int retval, line;
2181 unsigned long page;
2183 MOD_INC_USE_COUNT;
2184 line = MINOR(tty->device) - tty->driver.minor_start;
2185 if ((line < 0) || (line >= zs_channels_found)) {
2186 MOD_DEC_USE_COUNT;
2187 return -ENODEV;
2189 info = zs_soft + line;
2191 #ifdef CONFIG_KGDB
2192 if (info->kgdb_channel) {
2193 MOD_DEC_USE_COUNT;
2194 return -ENODEV;
2196 #endif
2197 if (serial_paranoia_check(info, tty->device, "rs_open"))
2198 return -ENODEV;
2199 OPNDBG("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2200 info->count);
2202 info->count++;
2203 tty->driver_data = info;
2204 info->tty = tty;
2206 if (!tmp_buf) {
2207 page = get_free_page(GFP_KERNEL);
2208 if (!page)
2209 return -ENOMEM;
2210 if (tmp_buf)
2211 free_page(page);
2212 else
2213 tmp_buf = (unsigned char *) page;
2217 * If the port is the middle of closing, bail out now
2219 if (tty_hung_up_p(filp) ||
2220 (info->flags & ZILOG_CLOSING)) {
2221 if (info->flags & ZILOG_CLOSING)
2222 interruptible_sleep_on(&info->close_wait);
2223 #ifdef SERIAL_DO_RESTART
2224 return ((info->flags & ZILOG_HUP_NOTIFY) ?
2225 -EAGAIN : -ERESTARTSYS);
2226 #else
2227 return -EAGAIN;
2228 #endif
2232 * Start up serial port
2235 retval = startup(info, 1);
2236 if (retval)
2237 return retval;
2239 retval = block_til_ready(tty, filp, info);
2240 if (retval) {
2241 OPNDBG("rs_open returning after block_til_ready with %d\n",
2242 retval);
2243 return retval;
2246 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
2247 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2248 *tty->termios = info->normal_termios;
2249 else
2250 *tty->termios = info->callout_termios;
2251 change_speed(info, 0);
2253 #ifdef CONFIG_SERIAL_CONSOLE
2254 if (sercons.cflag && sercons.index == line) {
2255 tty->termios->c_cflag = sercons.cflag;
2256 sercons.cflag = 0;
2257 change_speed(info, 0);
2259 #endif
2261 info->session = current->session;
2262 info->pgrp = current->pgrp;
2264 OPNDBG("rs_open ttys%d successful...\n", info->line);
2265 return 0;
2268 /* Finally, routines used to initialize the serial driver. */
2270 static void show_serial_version(void)
2272 printk("PowerMac Z8530 serial driver version 2.0\n");
2276 * Initialize one channel, both the mac_serial and mac_zschannel
2277 * structs. We use the dev_node field of the mac_serial struct.
2279 static void
2280 chan_init(struct mac_serial *zss, struct mac_zschannel *zs_chan,
2281 struct mac_zschannel *zs_chan_a)
2283 struct device_node *ch = zss->dev_node;
2284 char *conn;
2285 int len;
2286 struct slot_names_prop {
2287 int count;
2288 char name[1];
2289 } *slots;
2291 zss->irq = ch->intrs[0].line;
2292 zss->has_dma = 0;
2293 #if !defined(CONFIG_KGDB) && defined(SUPPORT_SERIAL_DMA)
2294 if (ch->n_addrs >= 3 && ch->n_intrs == 3)
2295 zss->has_dma = 1;
2296 #endif
2297 zss->dma_initted = 0;
2299 zs_chan->control = (volatile unsigned char *)
2300 ioremap(ch->addrs[0].address, 0x1000);
2301 zs_chan->data = zs_chan->control + 0x10;
2302 spin_lock_init(&zs_chan->lock);
2303 zs_chan->parent = zss;
2304 zss->zs_channel = zs_chan;
2305 zss->zs_chan_a = zs_chan_a;
2307 /* setup misc varariables */
2308 zss->kgdb_channel = 0;
2309 zss->is_cobalt_modem = device_is_compatible(ch, "cobalt");
2311 /* XXX tested only with wallstreet PowerBook,
2312 should do no harm anyway */
2313 conn = get_property(ch, "AAPL,connector", &len);
2314 zss->is_irda = conn && (strcmp(conn, "infrared") == 0);
2315 /* 1999 Powerbook G3 has slot-names property instead */
2316 slots = (struct slot_names_prop *)get_property(ch, "slot-names", &len);
2317 if (slots && slots->count > 0 && strcmp(slots->name, "IrDA") == 0)
2318 zss->is_irda = 1;
2320 if (zss->has_dma) {
2321 zss->dma_priv = NULL;
2322 /* it seems that the last two addresses are the
2323 DMA controllers */
2324 zss->tx_dma = (volatile struct dbdma_regs *)
2325 ioremap(ch->addrs[ch->n_addrs - 2].address, 0x100);
2326 zss->rx = (volatile struct mac_dma *)
2327 ioremap(ch->addrs[ch->n_addrs - 1].address, 0x100);
2328 zss->tx_dma_irq = ch->intrs[1].line;
2329 zss->rx_dma_irq = ch->intrs[2].line;
2330 spin_lock_init(&zss->rx_dma_lock);
2334 /* Ask the PROM how many Z8530s we have and initialize their zs_channels */
2335 static void
2336 probe_sccs()
2338 struct device_node *dev, *ch;
2339 struct mac_serial **pp;
2340 int n, chip, nchan;
2341 struct mac_zschannel *zs_chan;
2342 int chan_a_index;
2344 n = 0;
2345 pp = &zs_chain;
2346 zs_chan = zs_channels;
2347 for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
2348 nchan = 0;
2349 chip = n;
2350 if (n >= NUM_CHANNELS) {
2351 printk("Sorry, can't use %s: no more channels\n",
2352 dev->full_name);
2353 continue;
2355 chan_a_index = 0;
2356 for (ch = dev->child; ch != 0; ch = ch->sibling) {
2357 if (nchan >= 2) {
2358 printk(KERN_WARNING "SCC: Only 2 channels per "
2359 "chip are supported\n");
2360 break;
2362 if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
2363 printk("Can't use %s: %d addrs %d intrs\n",
2364 ch->full_name, ch->n_addrs, ch->n_intrs);
2365 continue;
2368 /* The channel with the higher address
2369 will be the A side. */
2370 if (nchan > 0 &&
2371 ch->addrs[0].address
2372 > zs_soft[n-1].dev_node->addrs[0].address)
2373 chan_a_index = 1;
2375 /* minimal initialization for now */
2376 zs_soft[n].dev_node = ch;
2377 *pp = &zs_soft[n];
2378 pp = &zs_soft[n].zs_next;
2379 ++nchan;
2380 ++n;
2382 if (nchan == 0)
2383 continue;
2385 /* set up A side */
2386 chan_init(&zs_soft[chip + chan_a_index], zs_chan, zs_chan);
2387 ++zs_chan;
2389 /* set up B side, if it exists */
2390 if (nchan > 1)
2391 chan_init(&zs_soft[chip + 1 - chan_a_index],
2392 zs_chan, zs_chan - 1);
2393 ++zs_chan;
2395 *pp = 0;
2397 zs_channels_found = n;
2398 #ifdef CONFIG_PMAC_PBOOK
2399 if (n)
2400 pmu_register_sleep_notifier(&serial_sleep_notifier);
2401 #endif /* CONFIG_PMAC_PBOOK */
2404 /* rs_init inits the driver */
2405 int macserial_init(void)
2407 int channel, i;
2408 unsigned long flags;
2409 struct mac_serial *info;
2411 /* Setup base handler, and timer table. */
2412 init_bh(MACSERIAL_BH, do_serial_bh);
2414 /* Find out how many Z8530 SCCs we have */
2415 if (zs_chain == 0)
2416 probe_sccs();
2418 /* XXX assume it's a powerbook if we have a via-pmu */
2419 is_powerbook = find_devices("via-pmu") != 0;
2421 /* Register the interrupt handler for each one */
2422 save_flags(flags); cli();
2423 for (i = 0; i < zs_channels_found; ++i) {
2424 if (zs_soft[i].has_dma) {
2425 if (request_irq(zs_soft[i].tx_dma_irq, rs_txdma_irq, 0,
2426 "SCC-txdma", &zs_soft[i]))
2427 printk(KERN_ERR "macserial: can't get irq %d\n",
2428 zs_soft[i].tx_dma_irq);
2429 disable_irq(zs_soft[i].tx_dma_irq);
2430 if (request_irq(zs_soft[i].rx_dma_irq, rs_rxdma_irq, 0,
2431 "SCC-rxdma", &zs_soft[i]))
2432 printk(KERN_ERR "macserial: can't get irq %d\n",
2433 zs_soft[i].rx_dma_irq);
2434 disable_irq(zs_soft[i].rx_dma_irq);
2436 if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
2437 "SCC", &zs_soft[i]))
2438 printk(KERN_ERR "macserial: can't get irq %d\n",
2439 zs_soft[i].irq);
2440 disable_irq(zs_soft[i].irq);
2442 restore_flags(flags);
2444 show_serial_version();
2446 /* Initialize the tty_driver structure */
2447 /* Not all of this is exactly right for us. */
2449 memset(&serial_driver, 0, sizeof(struct tty_driver));
2450 serial_driver.magic = TTY_DRIVER_MAGIC;
2451 #ifdef CONFIG_DEVFS_FS
2452 serial_driver.name = "tts/%d";
2453 #else
2454 serial_driver.name = "ttyS";
2455 #endif /* CONFIG_DEVFS_FS */
2456 serial_driver.major = TTY_MAJOR;
2457 serial_driver.minor_start = 64;
2458 serial_driver.num = zs_channels_found;
2459 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2460 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2461 serial_driver.init_termios = tty_std_termios;
2463 serial_driver.init_termios.c_cflag =
2464 B38400 | CS8 | CREAD | HUPCL | CLOCAL;
2465 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2466 serial_driver.refcount = &serial_refcount;
2467 serial_driver.table = serial_table;
2468 serial_driver.termios = serial_termios;
2469 serial_driver.termios_locked = serial_termios_locked;
2471 serial_driver.open = rs_open;
2472 serial_driver.close = rs_close;
2473 serial_driver.write = rs_write;
2474 serial_driver.flush_chars = rs_flush_chars;
2475 serial_driver.write_room = rs_write_room;
2476 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2477 serial_driver.flush_buffer = rs_flush_buffer;
2478 serial_driver.ioctl = rs_ioctl;
2479 serial_driver.throttle = rs_throttle;
2480 serial_driver.unthrottle = rs_unthrottle;
2481 serial_driver.set_termios = rs_set_termios;
2482 serial_driver.stop = rs_stop;
2483 serial_driver.start = rs_start;
2484 serial_driver.hangup = rs_hangup;
2485 serial_driver.break_ctl = rs_break;
2486 serial_driver.wait_until_sent = rs_wait_until_sent;
2489 * The callout device is just like normal device except for
2490 * major number and the subtype code.
2492 callout_driver = serial_driver;
2493 #ifdef CONFIG_DEVFS_FS
2494 callout_driver.name = "cua/%d";
2495 #else
2496 callout_driver.name = "cua";
2497 #endif /* CONFIG_DEVFS_FS */
2498 callout_driver.major = TTYAUX_MAJOR;
2499 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2501 if (tty_register_driver(&serial_driver))
2502 panic("Couldn't register serial driver\n");
2503 if (tty_register_driver(&callout_driver))
2504 panic("Couldn't register callout driver\n");
2506 for (channel = 0; channel < zs_channels_found; ++channel) {
2507 #ifdef CONFIG_KGDB
2508 if (zs_soft[channel].kgdb_channel) {
2509 kgdb_interruptible(1);
2510 continue;
2512 #endif
2513 zs_soft[channel].clk_divisor = 16;
2514 /* -- we are not sure the SCC is powered ON at this point
2515 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2517 zs_soft[channel].zs_baud = 38400;
2519 /* If console serial line, then enable interrupts. */
2520 if (zs_soft[channel].is_cons) {
2521 printk("macserial: console line, enabling interrupt %d\n", zs_soft[channel].irq);
2522 panic("macserial: console not supported yet !");
2523 write_zsreg(zs_soft[channel].zs_channel, R1,
2524 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
2525 write_zsreg(zs_soft[channel].zs_channel, R9,
2526 (NV | MIE));
2530 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2532 unsigned char* connector;
2533 int lenp;
2535 #ifdef CONFIG_KGDB
2536 if (info->kgdb_channel) {
2537 continue;
2539 #endif
2540 info->magic = SERIAL_MAGIC;
2541 info->port = (int) info->zs_channel->control;
2542 info->line = i;
2543 info->tty = 0;
2544 info->custom_divisor = 16;
2545 info->timeout = 0;
2546 info->close_delay = 50;
2547 info->closing_wait = 3000;
2548 info->x_char = 0;
2549 info->event = 0;
2550 info->count = 0;
2551 info->blocked_open = 0;
2552 info->tqueue.routine = do_softint;
2553 info->tqueue.data = info;
2554 info->callout_termios =callout_driver.init_termios;
2555 info->normal_termios = serial_driver.init_termios;
2556 init_waitqueue_head(&info->open_wait);
2557 init_waitqueue_head(&info->close_wait);
2558 info->timeout = HZ;
2559 printk("tty%02d at 0x%08x (irq = %d)", info->line,
2560 info->port, info->irq);
2561 printk(" is a Z8530 ESCC");
2562 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2563 if (connector)
2564 printk(", port = %s", connector);
2565 if (info->is_cobalt_modem)
2566 printk(" (cobalt modem)");
2567 if (info->is_irda)
2568 printk(" (IrDA)");
2569 printk("\n");
2571 #ifndef CONFIG_XMON
2572 #ifdef CONFIG_KGDB
2573 if (!info->kgdb_channel)
2574 #endif /* CONFIG_KGDB */
2575 /* By default, disable the port */
2576 set_scc_power(info, 0);
2577 #endif /* CONFIG_XMON */
2579 tmp_buf = 0;
2581 return 0;
2584 #ifdef MODULE
2585 int init_module(void)
2587 macserial_init();
2588 return 0;
2591 void cleanup_module(void)
2593 int i;
2594 unsigned long flags;
2595 struct mac_serial *info;
2597 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2598 set_scc_power(info, 0);
2599 save_flags(flags); cli();
2600 for (i = 0; i < zs_channels_found; ++i) {
2601 free_irq(zs_soft[i].irq, &zs_soft[i]);
2602 if (zs_soft[i].has_dma) {
2603 free_irq(zs_soft[i].tx_dma_irq, &zs_soft[i]);
2604 free_irq(zs_soft[i].rx_dma_irq, &zs_soft[i]);
2607 restore_flags(flags);
2608 tty_unregister_driver(&callout_driver);
2609 tty_unregister_driver(&serial_driver);
2611 if (tmp_buf) {
2612 free_page((unsigned long) tmp_buf);
2613 tmp_buf = 0;
2616 #ifdef CONFIG_PMAC_PBOOK
2617 if (zs_channels_found)
2618 pmu_unregister_sleep_notifier(&serial_sleep_notifier);
2619 #endif /* CONFIG_PMAC_PBOOK */
2621 #endif /* MODULE */
2623 #if 0
2625 * register_serial and unregister_serial allows for serial ports to be
2626 * configured at run-time, to support PCMCIA modems.
2628 /* PowerMac: Unused at this time, just here to make things link. */
2629 int register_serial(struct serial_struct *req)
2631 return -1;
2634 void unregister_serial(int line)
2636 return;
2638 #endif
2641 * ------------------------------------------------------------
2642 * Serial console driver
2643 * ------------------------------------------------------------
2645 #ifdef CONFIG_SERIAL_CONSOLE
2648 * Print a string to the serial port trying not to disturb
2649 * any possible real use of the port...
2651 static void serial_console_write(struct console *co, const char *s,
2652 unsigned count)
2654 struct mac_serial *info = zs_soft + co->index;
2655 int i;
2657 /* Turn of interrupts and enable the transmitter. */
2658 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2659 write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2661 for (i=0; i<count; i++) {
2662 /* Wait for the transmit buffer to empty. */
2663 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2664 eieio();
2667 write_zsdata(info->zs_channel, s[i]);
2668 if (s[i] == 10) {
2669 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2670 == 0)
2671 eieio();
2673 write_zsdata(info->zs_channel, 13);
2677 /* Restore the values in the registers. */
2678 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2679 /* Don't disable the transmitter. */
2683 * Receive character from the serial port
2685 static int serial_console_wait_key(struct console *co)
2687 struct mac_serial *info = zs_soft + co->index;
2688 int val;
2690 /* Turn of interrupts and enable the transmitter. */
2691 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~INT_ALL_Rx);
2692 write_zsreg(info->zs_channel, R3, info->curregs[3] | RxENABLE);
2694 /* Wait for something in the receive buffer. */
2695 while((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0)
2696 eieio();
2697 val = read_zsdata(info->zs_channel);
2699 /* Restore the values in the registers. */
2700 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2701 write_zsreg(info->zs_channel, R3, info->curregs[3]);
2703 return val;
2706 static kdev_t serial_console_device(struct console *c)
2708 return MKDEV(TTY_MAJOR, 64 + c->index);
2712 * Setup initial baud/bits/parity. We do two things here:
2713 * - construct a cflag setting for the first rs_open()
2714 * - initialize the serial port
2715 * Return non-zero if we didn't find a serial port.
2717 static int __init serial_console_setup(struct console *co, char *options)
2719 struct mac_serial *info;
2720 int baud = 38400;
2721 int bits = 8;
2722 int parity = 'n';
2723 int cflag = CREAD | HUPCL | CLOCAL;
2724 int brg;
2725 char *s;
2726 long flags;
2728 /* Find out how many Z8530 SCCs we have */
2729 if (zs_chain == 0)
2730 probe_sccs();
2732 if (zs_chain == 0)
2733 return -1;
2735 /* Do we have the device asked for? */
2736 if (co->index >= zs_channels_found)
2737 return -1;
2738 info = zs_soft + co->index;
2740 set_scc_power(info, 1);
2742 /* Reset the channel */
2743 write_zsreg(info->zs_channel, R9, CHRA);
2745 if (options) {
2746 baud = simple_strtoul(options, NULL, 10);
2747 s = options;
2748 while(*s >= '0' && *s <= '9')
2749 s++;
2750 if (*s)
2751 parity = *s++;
2752 if (*s)
2753 bits = *s - '0';
2757 * Now construct a cflag setting.
2759 switch(baud) {
2760 case 1200:
2761 cflag |= B1200;
2762 break;
2763 case 2400:
2764 cflag |= B2400;
2765 break;
2766 case 4800:
2767 cflag |= B4800;
2768 break;
2769 case 9600:
2770 cflag |= B9600;
2771 break;
2772 case 19200:
2773 cflag |= B19200;
2774 break;
2775 case 57600:
2776 cflag |= B57600;
2777 break;
2778 case 115200:
2779 cflag |= B115200;
2780 break;
2781 case 38400:
2782 default:
2783 cflag |= B38400;
2784 break;
2786 switch(bits) {
2787 case 7:
2788 cflag |= CS7;
2789 break;
2790 default:
2791 case 8:
2792 cflag |= CS8;
2793 break;
2795 switch(parity) {
2796 case 'o': case 'O':
2797 cflag |= PARENB | PARODD;
2798 break;
2799 case 'e': case 'E':
2800 cflag |= PARENB;
2801 break;
2803 co->cflag = cflag;
2805 save_flags(flags); cli();
2806 memset(info->curregs, 0, sizeof(info->curregs));
2808 info->zs_baud = baud;
2809 info->clk_divisor = 16;
2810 switch (info->zs_baud) {
2811 case ZS_CLOCK/16: /* 230400 */
2812 info->curregs[4] = X16CLK;
2813 info->curregs[11] = 0;
2814 break;
2815 case ZS_CLOCK/32: /* 115200 */
2816 info->curregs[4] = X32CLK;
2817 info->curregs[11] = 0;
2818 break;
2819 default:
2820 info->curregs[4] = X16CLK;
2821 info->curregs[11] = TCBR | RCBR;
2822 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2823 info->curregs[12] = (brg & 255);
2824 info->curregs[13] = ((brg >> 8) & 255);
2825 info->curregs[14] = BRENABL;
2828 /* byte size and parity */
2829 info->curregs[3] &= ~RxNBITS_MASK;
2830 info->curregs[5] &= ~TxNBITS_MASK;
2831 switch (cflag & CSIZE) {
2832 case CS5:
2833 info->curregs[3] |= Rx5;
2834 info->curregs[5] |= Tx5;
2835 break;
2836 case CS6:
2837 info->curregs[3] |= Rx6;
2838 info->curregs[5] |= Tx6;
2839 break;
2840 case CS7:
2841 info->curregs[3] |= Rx7;
2842 info->curregs[5] |= Tx7;
2843 break;
2844 case CS8:
2845 default: /* defaults to 8 bits */
2846 info->curregs[3] |= Rx8;
2847 info->curregs[5] |= Tx8;
2848 break;
2850 info->curregs[5] |= TxENAB | RTS | DTR;
2851 info->pendregs[3] = info->curregs[3];
2852 info->pendregs[5] = info->curregs[5];
2854 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
2855 if (cflag & CSTOPB) {
2856 info->curregs[4] |= SB2;
2857 } else {
2858 info->curregs[4] |= SB1;
2860 if (cflag & PARENB) {
2861 info->curregs[4] |= PAR_ENA;
2862 if (!(cflag & PARODD)) {
2863 info->curregs[4] |= PAR_EVEN;
2866 info->pendregs[4] = info->curregs[4];
2868 if (!(cflag & CLOCAL)) {
2869 if (!(info->curregs[15] & DCDIE))
2870 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2871 info->curregs[15] |= DCDIE;
2872 } else
2873 info->curregs[15] &= ~DCDIE;
2874 if (cflag & CRTSCTS) {
2875 info->curregs[15] |= CTSIE;
2876 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
2877 info->tx_stopped = 1;
2878 } else {
2879 info->curregs[15] &= ~CTSIE;
2880 info->tx_stopped = 0;
2882 info->pendregs[15] = info->curregs[15];
2884 /* Load up the new values */
2885 load_zsregs(info->zs_channel, info->curregs);
2887 restore_flags(flags);
2889 return 0;
2892 static struct console sercons = {
2893 name: "ttyS",
2894 write: serial_console_write,
2895 device: serial_console_device,
2896 wait_key: serial_console_wait_key,
2897 setup: serial_console_setup,
2898 flags: CON_PRINTBUFFER,
2899 index: -1,
2903 * Register console.
2905 void __init mac_scc_console_init(void)
2907 register_console(&sercons);
2909 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2911 #ifdef CONFIG_KGDB
2912 /* These are for receiving and sending characters under the kgdb
2913 * source level kernel debugger.
2915 void putDebugChar(char kgdb_char)
2917 struct mac_zschannel *chan = zs_kgdbchan;
2918 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2919 udelay(5);
2920 write_zsdata(chan, kgdb_char);
2923 char getDebugChar(void)
2925 struct mac_zschannel *chan = zs_kgdbchan;
2926 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2927 eieio(); /*barrier();*/
2928 return read_zsdata(chan);
2931 void kgdb_interruptible(int yes)
2933 struct mac_zschannel *chan = zs_kgdbchan;
2934 int one, nine;
2935 nine = read_zsreg(chan, 9);
2936 if (yes == 1) {
2937 one = EXT_INT_ENAB|INT_ALL_Rx;
2938 nine |= MIE;
2939 printk("turning serial ints on\n");
2940 } else {
2941 one = RxINT_DISAB;
2942 nine &= ~MIE;
2943 printk("turning serial ints off\n");
2945 write_zsreg(chan, 1, one);
2946 write_zsreg(chan, 9, nine);
2949 /* This sets up the serial port we're using, and turns on
2950 * interrupts for that channel, so kgdb is usable once we're done.
2952 static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
2954 int brg;
2955 int i, x;
2956 volatile char *sccc = ms->control;
2957 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
2958 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2959 for (i = 20000; i != 0; --i) {
2960 x = *sccc; eieio();
2962 for (i = 0; i < sizeof(scc_inittab); ++i) {
2963 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2964 i++;
2968 /* This is called at boot time to prime the kgdb serial debugging
2969 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2970 * for /dev/ttyb which is determined in setup_arch() from the
2971 * boot command line flags.
2972 * XXX at the moment probably only channel A will work
2974 void __init zs_kgdb_hook(int tty_num)
2976 /* Find out how many Z8530 SCCs we have */
2977 if (zs_chain == 0)
2978 probe_sccs();
2980 set_scc_power(&zs_soft[tty_num], 1);
2982 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2983 zs_soft[tty_num].change_needed = 0;
2984 zs_soft[tty_num].clk_divisor = 16;
2985 zs_soft[tty_num].zs_baud = 38400;
2986 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2988 /* Turn on transmitter/receiver at 8-bits/char */
2989 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2990 printk("KGDB: on channel %d initialized\n", tty_num);
2991 set_debug_traps(); /* init stub */
2993 #endif /* ifdef CONFIG_KGDB */
2995 #ifdef CONFIG_PMAC_PBOOK
2997 * notify clients before sleep and reset bus afterwards
3000 serial_notify_sleep(struct pmu_sleep_notifier *self, int when)
3002 int i;
3004 switch (when) {
3005 case PBOOK_SLEEP_REQUEST:
3006 case PBOOK_SLEEP_REJECT:
3007 break;
3009 case PBOOK_SLEEP_NOW:
3010 for (i=0; i<zs_channels_found; i++) {
3011 struct mac_serial *info = &zs_soft[i];
3012 if (info->flags & ZILOG_INITIALIZED) {
3013 shutdown(info);
3014 info->flags |= ZILOG_SLEEPING;
3017 break;
3018 case PBOOK_WAKE:
3019 for (i=0; i<zs_channels_found; i++) {
3020 struct mac_serial *info = &zs_soft[i];
3021 if (info->flags & ZILOG_SLEEPING) {
3022 info->flags &= ~ZILOG_SLEEPING;
3023 startup(info, 0);
3026 break;
3028 return PBOOK_SLEEP_OK;
3030 #endif /* CONFIG_PMAC_PBOOK */