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/workqueue.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/major.h>
25 #include <linux/string.h>
26 #include <linux/fcntl.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #ifdef CONFIG_SERIAL_CONSOLE
32 #include <linux/console.h>
34 #include <linux/slab.h>
36 #include <asm/sections.h>
38 #include <asm/pgtable.h>
41 #include <asm/system.h>
42 #include <asm/segment.h>
43 #include <asm/bitops.h>
44 #include <asm/machdep.h>
45 #include <asm/pmac_feature.h>
46 #include <linux/adb.h>
47 #include <linux/pmu.h>
51 #include <asm/dbdma.h>
53 #include "macserial.h"
55 #ifdef CONFIG_PMAC_PBOOK
56 static int serial_notify_sleep(struct pmu_sleep_notifier *self, int when);
57 static struct pmu_sleep_notifier serial_sleep_notifier = {
63 #define SUPPORT_SERIAL_DMA
64 #define MACSERIAL_VERSION "2.0"
67 * It would be nice to dynamically allocate everything that
68 * depends on NUM_SERIAL, so we could support any number of
69 * Z8530s, but for now...
71 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
72 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
74 /* On PowerMacs, the hardware takes care of the SCC recovery time,
75 but we need the eieio to make sure that the accesses occur
76 in the order we want. */
77 #define RECOVERY_DELAY eieio()
79 static struct tty_driver *serial_driver;
81 struct mac_zschannel zs_channels[NUM_CHANNELS];
83 struct mac_serial zs_soft[NUM_CHANNELS];
84 int zs_channels_found;
85 struct mac_serial *zs_chain; /* list of all channels */
87 struct tty_struct zs_ttys[NUM_CHANNELS];
89 static int is_powerbook;
91 #ifdef CONFIG_SERIAL_CONSOLE
92 static struct console sercons;
96 struct mac_zschannel *zs_kgdbchan;
97 static unsigned char scc_inittab[] = {
98 9, 0x80, /* reset A side (CHRA) */
99 13, 0, /* set baud rate divisor */
101 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
102 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
103 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
104 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
105 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
108 #define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
110 /* serial subtype definitions */
111 #define SERIAL_TYPE_NORMAL 1
113 /* number of characters left in xmit buffer before we ask for more */
114 #define WAKEUP_CHARS 256
119 #undef SERIAL_DEBUG_INTR
120 #undef SERIAL_DEBUG_OPEN
121 #undef SERIAL_DEBUG_FLOW
122 #undef SERIAL_DEBUG_POWER
123 #undef SERIAL_DEBUG_THROTTLE
124 #undef SERIAL_DEBUG_STOP
125 #undef SERIAL_DEBUG_BAUDS
127 #define RS_STROBE_TIME 10
128 #define RS_ISR_PASS_LIMIT 256
130 #define _INLINE_ inline
132 #ifdef SERIAL_DEBUG_OPEN
133 #define OPNDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
135 #define OPNDBG(fmt, arg...) do { } while (0)
137 #ifdef SERIAL_DEBUG_POWER
138 #define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
140 #define PWRDBG(fmt, arg...) do { } while (0)
142 #ifdef SERIAL_DEBUG_BAUDS
143 #define BAUDBG(fmt, arg...) printk(fmt , ## arg)
145 #define BAUDBG(fmt, arg...) do { } while (0)
148 static void probe_sccs(void);
149 static void change_speed(struct mac_serial *info, struct termios *old);
150 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
151 static int set_scc_power(struct mac_serial * info, int state);
152 static int setup_scc(struct mac_serial * info);
153 static void dbdma_reset(volatile struct dbdma_regs *dma);
154 static void dbdma_flush(volatile struct dbdma_regs *dma);
155 static irqreturn_t rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs);
156 static irqreturn_t rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs);
157 static void dma_init(struct mac_serial * info);
158 static void rxdma_start(struct mac_serial * info, int current);
159 static void rxdma_to_tty(struct mac_serial * info);
162 #define MIN(a,b) ((a) < (b) ? (a) : (b))
166 * tmp_buf is used as a temporary buffer by serial_write. We need to
167 * lock it in case the copy_from_user blocks while swapping in a page,
168 * and some other program tries to do a serial write at the same time.
169 * Since the lock will only come under contention when the system is
170 * swapping and available memory is low, it makes sense to share one
171 * buffer across all the serial ports, since it significantly saves
172 * memory if large numbers of serial ports are open.
174 static unsigned char *tmp_buf;
175 static DECLARE_MUTEX(tmp_buf_sem);
178 static inline int __pmac
179 serial_paranoia_check(struct mac_serial *info,
180 char *name, const char *routine)
182 #ifdef SERIAL_PARANOIA_CHECK
183 static const char badmagic[] = KERN_WARNING
184 "Warning: bad magic number for serial struct %s in %s\n";
185 static const char badinfo[] = KERN_WARNING
186 "Warning: null mac_serial for %s in %s\n";
189 printk(badinfo, name, routine);
192 if (info->magic != SERIAL_MAGIC) {
193 printk(badmagic, name, routine);
201 * Reading and writing Z8530 registers.
203 static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
206 unsigned char retval;
210 * We have to make this atomic.
212 spin_lock_irqsave(&channel->lock, flags);
214 *channel->control = reg;
217 retval = *channel->control;
219 spin_unlock_irqrestore(&channel->lock, flags);
223 static inline void __pmac write_zsreg(struct mac_zschannel *channel,
224 unsigned char reg, unsigned char value)
228 spin_lock_irqsave(&channel->lock, flags);
230 *channel->control = reg;
233 *channel->control = value;
235 spin_unlock_irqrestore(&channel->lock, flags);
239 static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
241 unsigned char retval;
243 retval = *channel->data;
248 static inline void write_zsdata(struct mac_zschannel *channel,
251 *channel->data = value;
256 static inline void load_zsregs(struct mac_zschannel *channel,
259 ZS_CLEARERR(channel);
260 ZS_CLEARFIFO(channel);
262 write_zsreg(channel, R4, regs[R4]);
263 write_zsreg(channel, R10, regs[R10]);
264 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
265 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
266 write_zsreg(channel, R1, regs[R1]);
267 write_zsreg(channel, R9, regs[R9]);
268 write_zsreg(channel, R11, regs[R11]);
269 write_zsreg(channel, R12, regs[R12]);
270 write_zsreg(channel, R13, regs[R13]);
271 write_zsreg(channel, R14, regs[R14]);
272 write_zsreg(channel, R15, regs[R15]);
273 write_zsreg(channel, R3, regs[R3]);
274 write_zsreg(channel, R5, regs[R5]);
278 /* Sets or clears DTR/RTS on the requested line */
279 static inline void zs_rtsdtr(struct mac_serial *ss, int set)
282 ss->curregs[5] |= (RTS | DTR);
284 ss->curregs[5] &= ~(RTS | DTR);
285 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
289 /* Utility routines for the Zilog */
290 static inline int get_zsbaud(struct mac_serial *ss)
292 struct mac_zschannel *channel = ss->zs_channel;
295 if ((ss->curregs[R11] & TCBR) == 0) {
296 /* higher rates don't use the baud rate generator */
297 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
299 /* The baud rate is split up between two 8-bit registers in
300 * what is termed 'BRG time constant' format in my docs for
301 * the chip, it is a function of the clk rate the chip is
302 * receiving which happens to be constant.
304 brg = (read_zsreg(channel, 13) << 8);
305 brg |= read_zsreg(channel, 12);
306 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
309 /* On receive, this clears errors and the receiver interrupts */
310 static inline void rs_recv_clear(struct mac_zschannel *zsc)
312 write_zsreg(zsc, 0, ERR_RES);
313 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
317 * Reset a Descriptor-Based DMA channel.
319 static void dbdma_reset(volatile struct dbdma_regs *dma)
323 out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
326 * Yes this looks peculiar, but apparently it needs to be this
327 * way on some machines. (We need to make sure the DBDMA
328 * engine has actually got the write above and responded
331 for (i = 200; i > 0; --i)
332 if (ld_le32(&dma->status) & RUN)
337 * Tells a DBDMA channel to stop and write any buffered data
338 * it might have to memory.
340 static _INLINE_ void dbdma_flush(volatile struct dbdma_regs *dma)
344 out_le32(&dma->control, (FLUSH << 16) | FLUSH);
345 while (((in_le32(&dma->status) & FLUSH) != 0) && (i++ < 100))
350 * ----------------------------------------------------------------------
352 * Here starts the interrupt handling routines. All of the following
353 * subroutines are declared as inline and are folded into
354 * rs_interrupt(). They were separated out for readability's sake.
356 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
357 * -----------------------------------------------------------------------
361 * This routine is used by the interrupt handler to schedule
362 * processing in the software interrupt portion of the driver.
364 static _INLINE_ void rs_sched_event(struct mac_serial *info,
367 info->event |= 1 << event;
368 schedule_work(&info->tqueue);
371 /* Work out the flag value for a z8530 status value. */
372 static _INLINE_ int stat_to_flag(int stat)
378 } else if (stat & FRM_ERR) {
380 } else if (stat & PAR_ERR) {
387 static _INLINE_ void receive_chars(struct mac_serial *info,
388 struct pt_regs *regs)
390 struct tty_struct *tty = info->tty;
391 unsigned char ch, stat, flag;
393 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
395 stat = read_zsreg(info->zs_channel, R1);
396 ch = read_zsdata(info->zs_channel);
399 if (info->kgdb_channel) {
400 if (ch == 0x03 || ch == '$')
402 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
403 write_zsreg(info->zs_channel, 0, ERR_RES);
409 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
410 tty_flip_buffer_push(tty);
412 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
413 static int flip_buf_ovf;
414 if (++flip_buf_ovf <= 1)
415 printk(KERN_WARNING "FB. overflow: %d\n",
421 static int flip_max_cnt;
422 if (flip_max_cnt < tty->flip.count)
423 flip_max_cnt = tty->flip.count;
425 flag = stat_to_flag(stat);
427 /* reset the error indication */
428 write_zsreg(info->zs_channel, 0, ERR_RES);
429 *tty->flip.flag_buf_ptr++ = flag;
430 *tty->flip.char_buf_ptr++ = ch;
433 tty_flip_buffer_push(tty);
436 static void transmit_chars(struct mac_serial *info)
438 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
442 if (info->x_char && !info->power_wait) {
444 write_zsdata(info->zs_channel, info->x_char);
450 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped
451 || info->power_wait) {
452 write_zsreg(info->zs_channel, 0, RES_Tx_P);
457 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
458 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
462 if (info->xmit_cnt < WAKEUP_CHARS)
463 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
466 static void powerup_done(unsigned long data)
468 struct mac_serial *info = (struct mac_serial *) data;
471 spin_lock_irqsave(&info->lock, flags);
472 info->power_wait = 0;
473 transmit_chars(info);
474 spin_unlock_irqrestore(&info->lock, 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)) {
488 wake_up_interruptible(&info->open_wait);
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
504 if ((status & CTS) == 0) {
505 if (info->tx_stopped) {
506 #ifdef SERIAL_DEBUG_FLOW
507 printk(KERN_DEBUG "CTS up\n");
509 info->tx_stopped = 0;
510 if (!info->tx_active)
511 transmit_chars(info);
514 #ifdef SERIAL_DEBUG_FLOW
515 printk(KERN_DEBUG "CTS down\n");
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)
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);
540 stat = read_zsreg(info->zs_channel, R1);
542 flag = stat_to_flag(stat);
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 irqreturn_t 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;
563 if (!(info->flags & ZILOG_INITIALIZED)) {
564 printk(KERN_WARNING "rs_interrupt: irq %d, port not "
565 "initialized\n", irq);
570 /* NOTE: The read register 3, which holds the irq status,
571 * does so for both channels on each chip. Although
572 * the status value itself must be read from the A
573 * channel and is only valid when read from channel A.
574 * Yes... broken hardware...
576 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
578 if (info->zs_chan_a == info->zs_channel)
579 shift = 3; /* Channel A */
581 shift = 0; /* Channel B */
583 spin_lock_irqsave(&info->lock, flags);
585 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
586 #ifdef SERIAL_DEBUG_INTR
587 printk(KERN_DEBUG "rs_interrupt: irq %d, zs_intreg 0x%x\n",
588 irq, (int)zs_intreg);
591 if ((zs_intreg & CHAN_IRQMASK) == 0)
595 if (zs_intreg & CHBRxIP) {
596 /* If we are doing DMA, we only ask for interrupts
597 on characters with errors or special conditions. */
598 if (info->dma_initted)
599 receive_special_dma(info);
601 receive_chars(info, regs);
603 if (zs_intreg & CHBTxIP)
604 transmit_chars(info);
605 if (zs_intreg & CHBEXT)
608 spin_unlock_irqrestore(&info->lock, flags);
609 return IRQ_RETVAL(handled);
612 /* Transmit DMA interrupt - not used at present */
613 static irqreturn_t rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs)
619 * Receive DMA interrupt.
621 static irqreturn_t rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs)
623 struct mac_serial *info = (struct mac_serial *) dev_id;
624 volatile struct dbdma_cmd *cd;
626 if (!info->dma_initted)
628 spin_lock(&info->rx_dma_lock);
629 /* First, confirm that this interrupt is, indeed, coming */
631 cd = info->rx_cmds[info->rx_cbuf] + 2;
632 if ((in_le16(&cd->xfer_status) & (RUN | ACTIVE)) != (RUN | ACTIVE)) {
633 spin_unlock(&info->rx_dma_lock);
636 if (info->rx_fbuf != RX_NO_FBUF) {
637 info->rx_cbuf = info->rx_fbuf;
638 if (++info->rx_fbuf == info->rx_nbuf)
640 if (info->rx_fbuf == info->rx_ubuf)
641 info->rx_fbuf = RX_NO_FBUF;
643 spin_unlock(&info->rx_dma_lock);
648 * -------------------------------------------------------------------
649 * Here ends the serial interrupt routines.
650 * -------------------------------------------------------------------
654 * ------------------------------------------------------------
655 * rs_stop() and rs_start()
657 * This routines are called before setting or resetting tty->stopped.
658 * ------------------------------------------------------------
660 static void rs_stop(struct tty_struct *tty)
662 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
664 #ifdef SERIAL_DEBUG_STOP
665 printk(KERN_DEBUG "rs_stop %ld....\n",
666 tty->ldisc.chars_in_buffer(tty));
669 if (serial_paranoia_check(info, tty->name, "rs_stop"))
673 spin_lock_irqsave(&info->lock, flags);
674 if (info->curregs[5] & TxENAB) {
675 info->curregs[5] &= ~TxENAB;
676 info->pendregs[5] &= ~TxENAB;
677 write_zsreg(info->zs_channel, 5, info->curregs[5]);
679 spin_unlock_irqrestore(&info->lock, flags);
683 static void rs_start(struct tty_struct *tty)
685 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
688 #ifdef SERIAL_DEBUG_STOP
689 printk(KERN_DEBUG "rs_start %ld....\n",
690 tty->ldisc.chars_in_buffer(tty));
693 if (serial_paranoia_check(info, tty->name, "rs_start"))
696 spin_lock_irqsave(&info->lock, flags);
698 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
699 info->curregs[5] |= TxENAB;
700 info->pendregs[5] = info->curregs[5];
701 write_zsreg(info->zs_channel, 5, info->curregs[5]);
704 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
705 transmit_chars(info);
708 spin_unlock_irqrestore(&info->lock, flags);
711 static void do_softint(void *private_)
713 struct mac_serial *info = (struct mac_serial *) private_;
714 struct tty_struct *tty;
720 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
721 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
722 tty->ldisc.write_wakeup)
723 (tty->ldisc.write_wakeup)(tty);
724 wake_up_interruptible(&tty->write_wait);
728 static int startup(struct mac_serial * info)
732 OPNDBG("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
734 if (info->flags & ZILOG_INITIALIZED) {
735 OPNDBG(" -> already inited\n");
739 if (!info->xmit_buf) {
740 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
745 OPNDBG("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
747 delay = set_scc_power(info, 1);
755 spin_lock_irqsave(&info->lock, flags);
756 info->power_wait = 1;
757 mod_timer(&info->powerup_timer,
758 jiffies + (delay * HZ + 999) / 1000);
759 spin_unlock_irqrestore(&info->lock, flags);
762 OPNDBG("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
764 info->flags |= ZILOG_INITIALIZED;
765 enable_irq(info->irq);
766 if (info->dma_initted) {
767 enable_irq(info->rx_dma_irq);
773 static _INLINE_ void rxdma_start(struct mac_serial * info, int current)
775 volatile struct dbdma_regs *rd = &info->rx->dma;
776 volatile struct dbdma_cmd *cd = info->rx_cmds[current];
778 //printk(KERN_DEBUG "SCC: rxdma_start\n");
780 st_le32(&rd->cmdptr, virt_to_bus(cd));
781 out_le32(&rd->control, (RUN << 16) | RUN);
784 static void rxdma_to_tty(struct mac_serial *info)
786 struct tty_struct *tty = info->tty;
787 volatile struct dbdma_regs *rd = &info->rx->dma;
789 int residue, available, space, do_queue;
795 spin_lock_irqsave(&info->rx_dma_lock, flags);
797 space = TTY_FLIPBUF_SIZE - tty->flip.count;
803 if (info->rx_ubuf == info->rx_cbuf) {
804 if ((ld_le32(&rd->status) & ACTIVE) != 0) {
806 if (in_le32(&rd->cmdptr)
807 == virt_to_bus(info->rx_cmds[info->rx_cbuf]+1))
808 residue = in_le16(&info->rx->res_count);
811 available = RX_BUF_SIZE - residue - info->rx_done_bytes;
812 if (available > space)
815 memcpy(tty->flip.char_buf_ptr,
816 info->rx_char_buf[info->rx_ubuf] + info->rx_done_bytes,
818 memcpy(tty->flip.flag_buf_ptr,
819 info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
821 tty->flip.char_buf_ptr += available;
822 tty->flip.count += available;
823 tty->flip.flag_buf_ptr += available;
824 memset(info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
826 info->rx_done_bytes += available;
829 if (info->rx_done_bytes == RX_BUF_SIZE) {
830 volatile struct dbdma_cmd *cd = info->rx_cmds[info->rx_ubuf];
832 if (info->rx_ubuf == info->rx_cbuf)
834 /* mark rx_char_buf[rx_ubuf] free */
835 st_le16(&cd->command, DBDMA_NOP);
837 st_le32(&cd->cmd_dep, 0);
838 st_le32((unsigned int *)&cd->res_count, 0);
840 st_le16(&cd->xfer_status, 0);
842 if (info->rx_fbuf == RX_NO_FBUF) {
843 info->rx_fbuf = info->rx_ubuf;
844 if (!(ld_le32(&rd->status) & ACTIVE)) {
845 dbdma_reset(&info->rx->dma);
846 rxdma_start(info, info->rx_ubuf);
847 info->rx_cbuf = info->rx_ubuf;
850 info->rx_done_bytes = 0;
851 if (++info->rx_ubuf == info->rx_nbuf)
853 if (info->rx_fbuf == info->rx_ubuf)
854 info->rx_fbuf = RX_NO_FBUF;
858 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
860 tty_flip_buffer_push(tty);
863 static void poll_rxdma(unsigned long private_)
865 struct mac_serial *info = (struct mac_serial *) private_;
869 spin_lock_irqsave(&info->rx_dma_lock, flags);
870 mod_timer(&info->poll_dma_timer, RX_DMA_TIMER);
871 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
874 static void dma_init(struct mac_serial * info)
877 volatile struct dbdma_cmd *cd;
882 /* various mem set up */
883 size = sizeof(struct dbdma_cmd) * (3 * info->rx_nbuf + 2)
884 + (RX_BUF_SIZE * 2 + sizeof(*info->rx_cmds)
885 + sizeof(*info->rx_char_buf) + sizeof(*info->rx_flag_buf))
887 info->dma_priv = kmalloc(size, GFP_KERNEL | GFP_DMA);
888 if (info->dma_priv == NULL)
890 memset(info->dma_priv, 0, size);
892 info->rx_cmds = (volatile struct dbdma_cmd **)info->dma_priv;
893 info->rx_char_buf = (unsigned char **) (info->rx_cmds + info->rx_nbuf);
894 info->rx_flag_buf = info->rx_char_buf + info->rx_nbuf;
895 p = (unsigned char *) (info->rx_flag_buf + info->rx_nbuf);
896 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
897 info->rx_char_buf[i] = p;
898 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
899 info->rx_flag_buf[i] = p;
901 /* a bit of DMA programming */
902 cd = info->rx_cmds[0] = (volatile struct dbdma_cmd *) DBDMA_ALIGN(p);
903 st_le16(&cd->command, DBDMA_NOP);
905 st_le16(&cd->req_count, RX_BUF_SIZE);
906 st_le16(&cd->command, INPUT_MORE);
907 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[0]));
909 st_le16(&cd->req_count, 4);
910 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
911 st_le32(&cd->phy_addr, virt_to_bus(cd-2));
912 st_le32(&cd->cmd_dep, DBDMA_STOP);
913 for (i = 1; i < info->rx_nbuf; i++) {
914 info->rx_cmds[i] = ++cd;
915 st_le16(&cd->command, DBDMA_NOP);
917 st_le16(&cd->req_count, RX_BUF_SIZE);
918 st_le16(&cd->command, INPUT_MORE);
919 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[i]));
921 st_le16(&cd->req_count, 4);
922 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
923 st_le32(&cd->phy_addr, virt_to_bus(cd-2));
924 st_le32(&cd->cmd_dep, DBDMA_STOP);
927 st_le16(&cd->command, DBDMA_NOP | BR_ALWAYS);
928 st_le32(&cd->cmd_dep, virt_to_bus(info->rx_cmds[0]));
930 /* setup DMA to our liking */
931 dbdma_reset(&info->rx->dma);
932 st_le32(&info->rx->dma.intr_sel, 0x10001);
933 st_le32(&info->rx->dma.br_sel, 0x10001);
934 out_le32(&info->rx->dma.wait_sel, 0x10001);
936 /* set various flags */
939 info->rx_fbuf = info->rx_ubuf + 1;
940 if (info->rx_fbuf == info->rx_nbuf)
941 info->rx_fbuf = RX_NO_FBUF;
942 info->rx_done_bytes = 0;
945 init_timer(&info->poll_dma_timer);
946 info->poll_dma_timer.function = (void *)&poll_rxdma;
947 info->poll_dma_timer.data = (unsigned long)info;
949 info->dma_initted = 1;
953 * FixZeroBug....Works around a bug in the SCC receving channel.
954 * Taken from Darwin code, 15 Sept. 2000 -DanM
956 * The following sequence prevents a problem that is seen with O'Hare ASICs
957 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
958 * at the input to the receiver becomes 'stuck' and locks up the receiver.
959 * This problem can occur as a result of a zero bit at the receiver input
960 * coincident with any of the following events:
962 * The SCC is initialized (hardware or software).
963 * A framing error is detected.
964 * The clocking option changes from synchronous or X1 asynchronous
965 * clocking to X16, X32, or X64 asynchronous clocking.
966 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
968 * This workaround attempts to recover from the lockup condition by placing
969 * the SCC in synchronous loopback mode with a fast clock before programming
970 * any of the asynchronous modes.
972 static void fix_zero_bug_scc(struct mac_serial * info)
974 write_zsreg(info->zs_channel, 9,
975 (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
977 write_zsreg(info->zs_channel, 9,
978 ((info->zs_channel == info->zs_chan_a? CHRA: CHRB) | NV));
980 write_zsreg(info->zs_channel, 4, (X1CLK | EXTSYNC));
982 /* I think this is wrong....but, I just copying code....
984 write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
986 write_zsreg(info->zs_channel, 5, (8 & ~TxENAB));
987 write_zsreg(info->zs_channel, 9, NV); /* Didn't we already do this? */
988 write_zsreg(info->zs_channel, 11, (RCBR | TCBR));
989 write_zsreg(info->zs_channel, 12, 0);
990 write_zsreg(info->zs_channel, 13, 0);
991 write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR));
992 write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR | BRENABL));
993 write_zsreg(info->zs_channel, 3, (8 | RxENABLE));
994 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
995 write_zsreg(info->zs_channel, 0, RES_EXT_INT); /* to kill some time */
997 /* The channel should be OK now, but it is probably receiving
999 * Switch to asynchronous mode, disable the receiver,
1000 * and discard everything in the receive buffer.
1002 write_zsreg(info->zs_channel, 9, NV);
1003 write_zsreg(info->zs_channel, 4, PAR_ENA);
1004 write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
1006 while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV) {
1007 (void)read_zsreg(info->zs_channel, 8);
1008 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
1009 write_zsreg(info->zs_channel, 0, ERR_RES);
1013 static int setup_scc(struct mac_serial * info)
1015 unsigned long flags;
1017 OPNDBG("setting up ttyS%d SCC...\n", info->line);
1019 spin_lock_irqsave(&info->lock, flags);
1021 /* Nice buggy HW ... */
1022 fix_zero_bug_scc(info);
1027 write_zsreg(info->zs_channel, 9,
1028 (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
1030 write_zsreg(info->zs_channel, 9, 0);
1033 * Clear the receive FIFO.
1035 ZS_CLEARFIFO(info->zs_channel);
1036 info->xmit_fifo_size = 1;
1045 * Clear the interrupt registers.
1047 write_zsreg(info->zs_channel, 0, ERR_RES);
1048 write_zsreg(info->zs_channel, 0, RES_H_IUS);
1051 * Turn on RTS and DTR.
1057 * Finally, enable sequencing and interrupts
1059 if (!info->dma_initted) {
1060 /* interrupt on ext/status changes, all received chars,
1062 info->curregs[1] = (info->curregs[1] & ~0x18)
1063 | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
1065 /* interrupt on ext/status changes, W/Req pin is
1066 receive DMA request */
1067 info->curregs[1] = (info->curregs[1] & ~(0x18 | TxINT_ENAB))
1068 | (EXT_INT_ENAB | WT_RDY_RT | WT_FN_RDYFN);
1069 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1070 /* enable W/Req pin */
1071 info->curregs[1] |= WT_RDY_ENAB;
1072 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1073 /* enable interrupts on transmit ready and receive errors */
1074 info->curregs[1] |= INT_ERR_Rx | TxINT_ENAB;
1076 info->pendregs[1] = info->curregs[1];
1077 info->curregs[3] |= (RxENABLE | Rx8);
1078 info->pendregs[3] = info->curregs[3];
1079 info->curregs[5] |= (TxENAB | Tx8);
1080 info->pendregs[5] = info->curregs[5];
1081 info->curregs[9] |= (NV | MIE);
1082 info->pendregs[9] = info->curregs[9];
1083 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1084 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1085 write_zsreg(info->zs_channel, 9, info->curregs[9]);
1088 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1089 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1091 spin_unlock_irqrestore(&info->lock, flags);
1094 * Set the speed of the serial port
1096 change_speed(info, 0);
1098 /* Save the current value of RR0 */
1099 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1101 if (info->dma_initted) {
1102 spin_lock_irqsave(&info->rx_dma_lock, flags);
1103 rxdma_start(info, 0);
1104 info->poll_dma_timer.expires = RX_DMA_TIMER;
1105 add_timer(&info->poll_dma_timer);
1106 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
1113 * This routine will shutdown a serial port; interrupts are disabled, and
1114 * DTR is dropped if the hangup on close termio flag is on.
1116 static void shutdown(struct mac_serial * info)
1118 OPNDBG("Shutting down serial port %d (irq %d)....\n", info->line,
1121 if (!(info->flags & ZILOG_INITIALIZED)) {
1122 OPNDBG("(already shutdown)\n");
1126 if (info->has_dma) {
1127 del_timer(&info->poll_dma_timer);
1128 dbdma_reset(info->tx_dma);
1129 dbdma_reset(&info->rx->dma);
1130 disable_irq(info->tx_dma_irq);
1131 disable_irq(info->rx_dma_irq);
1133 disable_irq(info->irq);
1135 info->pendregs[1] = info->curregs[1] = 0;
1136 write_zsreg(info->zs_channel, 1, 0); /* no interrupts */
1138 info->curregs[3] &= ~RxENABLE;
1139 info->pendregs[3] = info->curregs[3];
1140 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1142 info->curregs[5] &= ~TxENAB;
1143 if (!info->tty || C_HUPCL(info->tty))
1144 info->curregs[5] &= ~DTR;
1145 info->pendregs[5] = info->curregs[5];
1146 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1149 set_bit(TTY_IO_ERROR, &info->tty->flags);
1151 set_scc_power(info, 0);
1153 if (info->xmit_buf) {
1154 free_page((unsigned long) info->xmit_buf);
1158 if (info->has_dma && info->dma_priv) {
1159 kfree(info->dma_priv);
1160 info->dma_priv = NULL;
1161 info->dma_initted = 0;
1164 memset(info->curregs, 0, sizeof(info->curregs));
1165 memset(info->pendregs, 0, sizeof(info->pendregs));
1167 info->flags &= ~ZILOG_INITIALIZED;
1171 * Turn power on or off to the SCC and associated stuff
1172 * (port drivers, modem, IR port, etc.)
1173 * Returns the number of milliseconds we should wait before
1174 * trying to use the port.
1176 static int set_scc_power(struct mac_serial * info, int state)
1181 PWRDBG("ttyS%d: powering up hardware\n", info->line);
1183 PMAC_FTR_SCC_ENABLE,
1184 info->dev_node, info->port_type, 1);
1185 if (info->is_internal_modem) {
1187 PMAC_FTR_MODEM_ENABLE,
1188 info->dev_node, 0, 1);
1189 delay = 2500; /* wait for 2.5s before using */
1190 } else if (info->is_irda)
1191 mdelay(50); /* Do better here once the problems
1192 * with blocking have been ironed out
1195 /* TODO: Make that depend on a timer, don't power down
1198 PWRDBG("ttyS%d: shutting down hardware\n", info->line);
1199 if (info->is_internal_modem) {
1200 PWRDBG("ttyS%d: shutting down modem\n", info->line);
1202 PMAC_FTR_MODEM_ENABLE,
1203 info->dev_node, 0, 0);
1206 PMAC_FTR_SCC_ENABLE,
1207 info->dev_node, info->port_type, 0);
1212 static void irda_rts_pulses(struct mac_serial *info, int w)
1215 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1217 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1219 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1221 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1225 * Set the irda codec on the imac to the specified baud rate.
1227 static void irda_setup(struct mac_serial *info)
1231 speed = info->tty->termios->c_cflag & CBAUD;
1232 if (speed < B2400 || speed > B115200)
1234 code = 0x4d + B115200 - speed;
1236 /* disable serial interrupts and receive DMA */
1237 write_zsreg(info->zs_channel, 1, info->curregs[1] & ~0x9f);
1239 /* wait for transmitter to drain */
1241 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0
1242 || (read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1244 printk(KERN_ERR "transmitter didn't drain\n");
1251 /* set to 8 bits, no parity, 19200 baud, RTS on, DTR off */
1252 write_zsreg(info->zs_channel, 4, X16CLK | SB1);
1253 write_zsreg(info->zs_channel, 11, TCBR | RCBR);
1254 t = BPS_TO_BRG(19200, ZS_CLOCK/16);
1255 write_zsreg(info->zs_channel, 12, t);
1256 write_zsreg(info->zs_channel, 13, t >> 8);
1257 write_zsreg(info->zs_channel, 14, BRENABL);
1258 write_zsreg(info->zs_channel, 3, Rx8 | RxENABLE);
1259 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1261 /* set TxD low for ~104us and pulse RTS */
1263 write_zsdata(info->zs_channel, 0xfe);
1264 irda_rts_pulses(info, 150);
1265 irda_rts_pulses(info, 180);
1266 irda_rts_pulses(info, 50);
1269 /* assert DTR, wait 30ms, talk to the chip */
1270 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS | DTR);
1272 while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV)
1273 read_zsdata(info->zs_channel);
1275 write_zsdata(info->zs_channel, 1);
1277 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1279 printk(KERN_ERR "irda_setup timed out on 1st byte\n");
1284 t = read_zsdata(info->zs_channel);
1286 printk(KERN_ERR "irda_setup 1st byte = %x\n", t);
1288 write_zsdata(info->zs_channel, code);
1290 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1292 printk(KERN_ERR "irda_setup timed out on 2nd byte\n");
1297 t = read_zsdata(info->zs_channel);
1299 printk(KERN_ERR "irda_setup 2nd byte = %x (%x)\n", t, code);
1301 /* Drop DTR again and do some more RTS pulses */
1304 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1305 irda_rts_pulses(info, 80);
1307 /* We should be right to go now. We assume that load_zsregs
1308 will get called soon to load up the correct baud rate etc. */
1309 info->curregs[5] = (info->curregs[5] | RTS) & ~DTR;
1310 info->pendregs[5] = info->curregs[5];
1314 * This routine is called to set the UART divisor registers to match
1315 * the specified baud rate for a serial port.
1317 static void change_speed(struct mac_serial *info, struct termios *old_termios)
1322 unsigned long flags;
1324 if (!info->tty || !info->tty->termios)
1327 cflag = info->tty->termios->c_cflag;
1328 baud = tty_get_baud_rate(info->tty);
1331 info->tty->termios->c_cflag &= ~CBAUD;
1332 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1333 cflag = info->tty->termios->c_cflag;
1334 baud = tty_get_baud_rate(info->tty);
1337 baud = info->zs_baud;
1344 spin_lock_irqsave(&info->lock, flags);
1345 info->zs_baud = baud;
1346 info->clk_divisor = 16;
1348 BAUDBG(KERN_DEBUG "set speed to %d bds, ", baud);
1351 case ZS_CLOCK/16: /* 230400 */
1352 info->curregs[4] = X16CLK;
1353 info->curregs[11] = 0;
1355 case ZS_CLOCK/32: /* 115200 */
1356 info->curregs[4] = X32CLK;
1357 info->curregs[11] = 0;
1360 info->curregs[4] = X16CLK;
1361 info->curregs[11] = TCBR | RCBR;
1362 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
1363 info->curregs[12] = (brg & 255);
1364 info->curregs[13] = ((brg >> 8) & 255);
1365 info->curregs[14] = BRENABL;
1368 /* byte size and parity */
1369 info->curregs[3] &= ~RxNBITS_MASK;
1370 info->curregs[5] &= ~TxNBITS_MASK;
1371 switch (cflag & CSIZE) {
1373 info->curregs[3] |= Rx5;
1374 info->curregs[5] |= Tx5;
1379 info->curregs[3] |= Rx6;
1380 info->curregs[5] |= Tx6;
1385 info->curregs[3] |= Rx7;
1386 info->curregs[5] |= Tx7;
1391 default: /* defaults to 8 bits */
1392 info->curregs[3] |= Rx8;
1393 info->curregs[5] |= Tx8;
1398 info->pendregs[3] = info->curregs[3];
1399 info->pendregs[5] = info->curregs[5];
1401 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
1402 if (cflag & CSTOPB) {
1403 info->curregs[4] |= SB2;
1407 info->curregs[4] |= SB1;
1410 if (cflag & PARENB) {
1412 info->curregs[4] |= PAR_ENA;
1415 if (!(cflag & PARODD)) {
1416 info->curregs[4] |= PAR_EVEN;
1418 info->pendregs[4] = info->curregs[4];
1420 if (!(cflag & CLOCAL)) {
1421 if (!(info->curregs[15] & DCDIE))
1422 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1423 info->curregs[15] |= DCDIE;
1425 info->curregs[15] &= ~DCDIE;
1426 if (cflag & CRTSCTS) {
1427 info->curregs[15] |= CTSIE;
1428 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
1429 info->tx_stopped = 1;
1431 info->curregs[15] &= ~CTSIE;
1432 info->tx_stopped = 0;
1434 info->pendregs[15] = info->curregs[15];
1436 /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
1437 This code would love a larger HZ and a >1 fifo size, but this is not
1438 a priority. The resulting value must be >HZ/2
1440 info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
1441 info->timeout += HZ/50+1; /* Add .02 seconds of slop */
1443 BAUDBG("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ,
1444 (int)info->baud_base);
1446 /* set the irda codec to the right rate */
1450 /* Load up the new values */
1451 load_zsregs(info->zs_channel, info->curregs);
1453 spin_unlock_irqrestore(&info->lock, flags);
1456 static void rs_flush_chars(struct tty_struct *tty)
1458 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1459 unsigned long flags;
1461 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1464 spin_lock_irqsave(&info->lock, flags);
1465 if (!(info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
1467 /* Enable transmitter */
1468 transmit_chars(info);
1469 spin_unlock_irqrestore(&info->lock, flags);
1472 static int rs_write(struct tty_struct * tty, int from_user,
1473 const unsigned char *buf, int count)
1476 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1477 unsigned long flags;
1479 if (serial_paranoia_check(info, tty->name, "rs_write"))
1482 if (!tty || !info->xmit_buf || !tmp_buf)
1489 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1490 SERIAL_XMIT_SIZE - info->xmit_head));
1494 c -= copy_from_user(tmp_buf, buf, c);
1500 spin_lock_irqsave(&info->lock, flags);
1501 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1502 SERIAL_XMIT_SIZE - info->xmit_head));
1503 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1504 info->xmit_head = ((info->xmit_head + c) &
1505 (SERIAL_XMIT_SIZE-1));
1506 info->xmit_cnt += c;
1507 spin_unlock_irqrestore(&info->lock, flags);
1515 spin_lock_irqsave(&info->lock, flags);
1517 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1518 SERIAL_XMIT_SIZE - info->xmit_head));
1520 spin_unlock_irqrestore(&info->lock, flags);
1523 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1524 info->xmit_head = ((info->xmit_head + c) &
1525 (SERIAL_XMIT_SIZE-1));
1526 info->xmit_cnt += c;
1527 spin_unlock_irqrestore(&info->lock, flags);
1533 spin_lock_irqsave(&info->lock, flags);
1534 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1535 && !info->tx_active)
1536 transmit_chars(info);
1537 spin_unlock_irqrestore(&info->lock, flags);
1541 static int rs_write_room(struct tty_struct *tty)
1543 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1546 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1548 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1554 static int rs_chars_in_buffer(struct tty_struct *tty)
1556 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1558 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1560 return info->xmit_cnt;
1563 static void rs_flush_buffer(struct tty_struct *tty)
1565 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1566 unsigned long flags;
1568 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1570 spin_lock_irqsave(&info->lock, flags);
1571 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1572 spin_unlock_irqrestore(&info->lock, flags);
1573 wake_up_interruptible(&tty->write_wait);
1574 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1575 tty->ldisc.write_wakeup)
1576 (tty->ldisc.write_wakeup)(tty);
1580 * ------------------------------------------------------------
1583 * This routine is called by the upper-layer tty layer to signal that
1584 * incoming characters should be throttled.
1585 * ------------------------------------------------------------
1587 static void rs_throttle(struct tty_struct * tty)
1589 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1590 unsigned long flags;
1591 #ifdef SERIAL_DEBUG_THROTTLE
1592 printk(KERN_DEBUG "throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1595 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1599 spin_lock_irqsave(&info->lock, flags);
1600 info->x_char = STOP_CHAR(tty);
1601 if (!info->tx_active)
1602 transmit_chars(info);
1603 spin_unlock_irqrestore(&info->lock, flags);
1606 if (C_CRTSCTS(tty)) {
1608 * Here we want to turn off the RTS line. On Macintoshes,
1609 * the external serial ports using a DIN-8 or DIN-9
1610 * connector only have the DTR line (which is usually
1611 * wired to both RTS and DTR on an external modem in
1612 * the cable). RTS doesn't go out to the serial port
1613 * socket, it acts as an output enable for the transmit
1614 * data line. So in this case we don't drop RTS.
1616 * Macs with internal modems generally do have both RTS
1617 * and DTR wired to the modem, so in that case we do
1620 if (info->is_internal_modem) {
1621 spin_lock_irqsave(&info->lock, flags);
1622 info->curregs[5] &= ~RTS;
1623 info->pendregs[5] &= ~RTS;
1624 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1625 spin_unlock_irqrestore(&info->lock, flags);
1630 if (tty->termios->c_cflag & CDTRCTS) {
1631 spin_lock_irqsave(&info->lock, flags);
1632 info->curregs[5] &= ~DTR;
1633 info->pendregs[5] &= ~DTR;
1634 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1635 spin_unlock_irqrestore(&info->lock, flags);
1637 #endif /* CDTRCTS */
1640 static void rs_unthrottle(struct tty_struct * tty)
1642 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1643 unsigned long flags;
1644 #ifdef SERIAL_DEBUG_THROTTLE
1645 printk(KERN_DEBUG "unthrottle %s: %d....\n",
1646 tty->ldisc.chars_in_buffer(tty));
1649 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1653 spin_lock_irqsave(&info->lock, flags);
1657 info->x_char = START_CHAR(tty);
1658 if (!info->tx_active)
1659 transmit_chars(info);
1661 spin_unlock_irqrestore(&info->lock, flags);
1664 if (C_CRTSCTS(tty) && info->is_internal_modem) {
1665 /* Assert RTS line */
1666 spin_lock_irqsave(&info->lock, flags);
1667 info->curregs[5] |= RTS;
1668 info->pendregs[5] |= RTS;
1669 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1670 spin_unlock_irqrestore(&info->lock, flags);
1674 if (tty->termios->c_cflag & CDTRCTS) {
1675 /* Assert DTR line */
1676 spin_lock_irqsave(&info->lock, flags);
1677 info->curregs[5] |= DTR;
1678 info->pendregs[5] |= DTR;
1679 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1680 spin_unlock_irqrestore(&info->lock, flags);
1686 * ------------------------------------------------------------
1687 * rs_ioctl() and friends
1688 * ------------------------------------------------------------
1691 static int get_serial_info(struct mac_serial * info,
1692 struct serial_struct __user * retinfo)
1694 struct serial_struct tmp;
1698 memset(&tmp, 0, sizeof(tmp));
1699 tmp.type = info->type;
1700 tmp.line = info->line;
1701 tmp.port = info->port;
1702 tmp.irq = info->irq;
1703 tmp.flags = info->flags;
1704 tmp.baud_base = info->baud_base;
1705 tmp.close_delay = info->close_delay;
1706 tmp.closing_wait = info->closing_wait;
1707 tmp.custom_divisor = info->custom_divisor;
1708 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1713 static int set_serial_info(struct mac_serial * info,
1714 struct serial_struct __user * new_info)
1716 struct serial_struct new_serial;
1717 struct mac_serial old_info;
1720 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1724 if (!capable(CAP_SYS_ADMIN)) {
1725 if ((new_serial.baud_base != info->baud_base) ||
1726 (new_serial.type != info->type) ||
1727 (new_serial.close_delay != info->close_delay) ||
1728 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1729 (info->flags & ~ZILOG_USR_MASK)))
1731 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1732 (new_serial.flags & ZILOG_USR_MASK));
1733 info->custom_divisor = new_serial.custom_divisor;
1734 goto check_and_exit;
1737 if (info->count > 1)
1741 * OK, past this point, all the error checking has been done.
1742 * At this point, we start making changes.....
1745 info->baud_base = new_serial.baud_base;
1746 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1747 (new_serial.flags & ZILOG_FLAGS));
1748 info->type = new_serial.type;
1749 info->close_delay = new_serial.close_delay;
1750 info->closing_wait = new_serial.closing_wait;
1753 if (info->flags & ZILOG_INITIALIZED)
1754 retval = setup_scc(info);
1759 * get_lsr_info - get line status register info
1761 * Purpose: Let user call ioctl() to get info when the UART physically
1762 * is emptied. On bus types like RS485, the transmitter must
1763 * release the bus after transmitting. This must be done when
1764 * the transmit shift register is empty, not be done when the
1765 * transmit holding register is empty. This functionality
1766 * allows an RS485 driver to be written in user space.
1768 static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1770 unsigned char status;
1771 unsigned long flags;
1773 spin_lock_irqsave(&info->lock, flags);
1774 status = read_zsreg(info->zs_channel, 0);
1775 spin_unlock_irqrestore(&info->lock, flags);
1776 status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0;
1777 return put_user(status,value);
1780 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1782 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1783 unsigned char control, status;
1784 unsigned long flags;
1787 if (info->kgdb_channel)
1790 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1793 if (tty->flags & (1 << TTY_IO_ERROR))
1796 spin_lock_irqsave(&info->lock, flags);
1797 control = info->curregs[5];
1798 status = read_zsreg(info->zs_channel, 0);
1799 spin_unlock_irqrestore(&info->lock, flags);
1800 return ((control & RTS) ? TIOCM_RTS: 0)
1801 | ((control & DTR) ? TIOCM_DTR: 0)
1802 | ((status & DCD) ? TIOCM_CAR: 0)
1803 | ((status & CTS) ? 0: TIOCM_CTS);
1806 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1807 unsigned int set, unsigned int clear)
1809 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1810 unsigned int arg, bits;
1811 unsigned long flags;
1814 if (info->kgdb_channel)
1817 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1820 if (tty->flags & (1 << TTY_IO_ERROR))
1823 spin_lock_irqsave(&info->lock, flags);
1824 if (set & TIOCM_RTS)
1825 info->curregs[5] |= RTS;
1826 if (set & TIOCM_DTR)
1827 info->curregs[5] |= DTR;
1828 if (clear & TIOCM_RTS)
1829 info->curregs[5] &= ~RTS;
1830 if (clear & TIOCM_DTR)
1831 info->curregs[5] &= ~DTR;
1833 info->pendregs[5] = info->curregs[5];
1834 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1835 spin_unlock_irqrestore(&info->lock, flags);
1840 * rs_break - turn transmit break condition on/off
1842 static void rs_break(struct tty_struct *tty, int break_state)
1844 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1845 unsigned long flags;
1847 if (serial_paranoia_check(info, tty->name, "rs_break"))
1850 spin_lock_irqsave(&info->lock, flags);
1851 if (break_state == -1)
1852 info->curregs[5] |= SND_BRK;
1854 info->curregs[5] &= ~SND_BRK;
1855 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1856 spin_unlock_irqrestore(&info->lock, flags);
1859 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1860 unsigned int cmd, unsigned long arg)
1862 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1865 if (info->kgdb_channel)
1868 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1871 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1872 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) {
1873 if (tty->flags & (1 << TTY_IO_ERROR))
1879 return get_serial_info(info,
1880 (struct serial_struct __user *) arg);
1882 return set_serial_info(info,
1883 (struct serial_struct __user *) arg);
1884 case TIOCSERGETLSR: /* Get line status register */
1885 return get_lsr_info(info, (unsigned int *) arg);
1887 case TIOCSERGSTRUCT:
1888 if (copy_to_user((struct mac_serial __user *) arg,
1889 info, sizeof(struct mac_serial)))
1894 return -ENOIOCTLCMD;
1899 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1901 struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1904 if (tty->termios->c_cflag == old_termios->c_cflag)
1906 was_stopped = info->tx_stopped;
1908 change_speed(info, old_termios);
1910 if (was_stopped && !info->tx_stopped) {
1911 tty->hw_stopped = 0;
1917 * ------------------------------------------------------------
1920 * This routine is called when the serial port gets closed.
1921 * Wait for the last remaining data to be sent.
1922 * ------------------------------------------------------------
1924 static void rs_close(struct tty_struct *tty, struct file * filp)
1926 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1927 unsigned long flags;
1929 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1932 spin_lock_irqsave(&info->lock, flags);
1934 if (tty_hung_up_p(filp)) {
1935 spin_unlock_irqrestore(&info->lock, flags);
1939 OPNDBG("rs_close ttyS%d, count = %d\n", info->line, info->count);
1940 if ((tty->count == 1) && (info->count != 1)) {
1942 * Uh, oh. tty->count is 1, which means that the tty
1943 * structure will be freed. Info->count should always
1944 * be one in these conditions. If it's greater than
1945 * one, we've got real problems, since it means the
1946 * serial port won't be shutdown.
1948 printk(KERN_ERR "rs_close: bad serial port count; tty->count "
1949 "is 1, info->count is %d\n", info->count);
1952 if (--info->count < 0) {
1953 printk(KERN_ERR "rs_close: bad serial port count for "
1954 "ttyS%d: %d\n", info->line, info->count);
1958 spin_unlock_irqrestore(&info->lock, flags);
1961 info->flags |= ZILOG_CLOSING;
1963 * Now we wait for the transmit buffer to clear; and we notify
1964 * the line discipline to only process XON/XOFF characters.
1966 OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1968 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) {
1969 spin_unlock_irqrestore(&info->lock, flags);
1970 tty_wait_until_sent(tty, info->closing_wait);
1971 spin_lock_irqsave(&info->lock, flags);
1975 * At this point we stop accepting input. To do this, we
1976 * disable the receiver and receive interrupts.
1978 info->curregs[3] &= ~RxENABLE;
1979 info->pendregs[3] = info->curregs[3];
1980 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1981 info->curregs[1] &= ~(0x18); /* disable any rx ints */
1982 info->pendregs[1] = info->curregs[1];
1983 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1984 ZS_CLEARFIFO(info->zs_channel);
1985 if (info->flags & ZILOG_INITIALIZED) {
1987 * Before we drop DTR, make sure the SCC transmitter
1988 * has completely drained.
1990 OPNDBG("waiting end of Rx...\n");
1991 spin_unlock_irqrestore(&info->lock, flags);
1992 rs_wait_until_sent(tty, info->timeout);
1993 spin_lock_irqsave(&info->lock, flags);
1997 /* restore flags now since shutdown() will have disabled this port's
1999 spin_unlock_irqrestore(&info->lock, flags);
2001 if (tty->driver->flush_buffer)
2002 tty->driver->flush_buffer(tty);
2003 if (tty->ldisc.flush_buffer)
2004 tty->ldisc.flush_buffer(tty);
2009 if (info->blocked_open) {
2010 if (info->close_delay) {
2011 current->state = TASK_INTERRUPTIBLE;
2012 schedule_timeout(info->close_delay);
2014 wake_up_interruptible(&info->open_wait);
2016 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
2017 wake_up_interruptible(&info->close_wait);
2021 * rs_wait_until_sent() --- wait until the transmitter is empty
2023 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2025 struct mac_serial *info = (struct mac_serial *) tty->driver_data;
2026 unsigned long orig_jiffies, char_time;
2028 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2031 /* printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
2032 timeout, tty->stopped, info->tx_stopped);
2034 orig_jiffies = jiffies;
2036 * Set the check interval to be 1/5 of the estimated time to
2037 * send a single character, and make it at least 1. The check
2038 * interval should also be less than the timeout.
2040 if (info->timeout <= HZ/50) {
2041 printk(KERN_INFO "macserial: invalid info->timeout=%d\n",
2043 info->timeout = HZ/50+1;
2046 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2047 char_time = char_time / 5;
2048 if (char_time > HZ) {
2049 printk(KERN_WARNING "macserial: char_time %ld >HZ !!!\n",
2052 } else if (char_time == 0)
2055 char_time = MIN(char_time, timeout);
2056 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
2057 current->state = TASK_INTERRUPTIBLE;
2058 schedule_timeout(char_time);
2059 if (signal_pending(current))
2061 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2064 current->state = TASK_RUNNING;
2068 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2070 static void rs_hangup(struct tty_struct *tty)
2072 struct mac_serial * info = (struct mac_serial *)tty->driver_data;
2074 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
2077 rs_flush_buffer(tty);
2081 info->flags &= ~ZILOG_NORMAL_ACTIVE;
2083 wake_up_interruptible(&info->open_wait);
2087 * ------------------------------------------------------------
2088 * rs_open() and friends
2089 * ------------------------------------------------------------
2091 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2092 struct mac_serial *info)
2094 DECLARE_WAITQUEUE(wait,current);
2099 * If the device is in the middle of being closed, then block
2100 * until it's done, and then try again.
2102 if (info->flags & ZILOG_CLOSING) {
2103 interruptible_sleep_on(&info->close_wait);
2108 * If non-blocking mode is set, or the port is not enabled,
2109 * then make the check up front and then exit.
2111 if ((filp->f_flags & O_NONBLOCK) ||
2112 (tty->flags & (1 << TTY_IO_ERROR))) {
2113 info->flags |= ZILOG_NORMAL_ACTIVE;
2117 if (tty->termios->c_cflag & CLOCAL)
2121 * Block waiting for the carrier detect and the line to become
2122 * free (i.e., not in use by the callout). While we are in
2123 * this loop, info->count is dropped by one, so that
2124 * rs_close() knows when to free things. We restore it upon
2125 * exit, either normal or abnormal.
2128 add_wait_queue(&info->open_wait, &wait);
2129 OPNDBG("block_til_ready before block: ttyS%d, count = %d\n",
2130 info->line, info->count);
2131 spin_lock_irq(&info->lock);
2132 if (!tty_hung_up_p(filp))
2134 spin_unlock_irq(&info->lock);
2135 info->blocked_open++;
2137 spin_lock_irq(&info->lock);
2138 if ((tty->termios->c_cflag & CBAUD) &&
2141 spin_unlock_irq(&info->lock);
2142 set_current_state(TASK_INTERRUPTIBLE);
2143 if (tty_hung_up_p(filp) ||
2144 !(info->flags & ZILOG_INITIALIZED)) {
2148 if (!(info->flags & ZILOG_CLOSING) &&
2149 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
2151 if (signal_pending(current)) {
2152 retval = -ERESTARTSYS;
2155 OPNDBG("block_til_ready blocking: ttyS%d, count = %d\n",
2156 info->line, info->count);
2159 current->state = TASK_RUNNING;
2160 remove_wait_queue(&info->open_wait, &wait);
2161 if (!tty_hung_up_p(filp))
2163 info->blocked_open--;
2164 OPNDBG("block_til_ready after blocking: ttyS%d, count = %d\n",
2165 info->line, info->count);
2168 info->flags |= ZILOG_NORMAL_ACTIVE;
2173 * This routine is called whenever a serial port is opened. It
2174 * enables interrupts for a serial port, linking in its ZILOG structure into
2175 * the IRQ chain. It also performs the serial-specific
2176 * initialization for the tty structure.
2178 static int rs_open(struct tty_struct *tty, struct file * filp)
2180 struct mac_serial *info;
2185 if ((line < 0) || (line >= zs_channels_found)) {
2188 info = zs_soft + line;
2191 if (info->kgdb_channel) {
2195 if (serial_paranoia_check(info, tty->name, "rs_open"))
2197 OPNDBG("rs_open %s, count = %d, tty=%p\n", tty->name,
2201 tty->driver_data = info;
2205 page = get_zeroed_page(GFP_KERNEL);
2211 tmp_buf = (unsigned char *) page;
2215 * If the port is the middle of closing, bail out now
2217 if (tty_hung_up_p(filp) ||
2218 (info->flags & ZILOG_CLOSING)) {
2219 if (info->flags & ZILOG_CLOSING)
2220 interruptible_sleep_on(&info->close_wait);
2225 * Start up serial port
2228 retval = startup(info);
2232 retval = block_til_ready(tty, filp, info);
2234 OPNDBG("rs_open returning after block_til_ready with %d\n",
2239 #ifdef CONFIG_SERIAL_CONSOLE
2240 if (sercons.cflag && sercons.index == line) {
2241 tty->termios->c_cflag = sercons.cflag;
2243 change_speed(info, 0);
2247 OPNDBG("rs_open %s successful...\n", tty->name);
2251 /* Finally, routines used to initialize the serial driver. */
2253 static void show_serial_version(void)
2255 printk(KERN_INFO "PowerMac Z8530 serial driver version " MACSERIAL_VERSION "\n");
2259 * Initialize one channel, both the mac_serial and mac_zschannel
2260 * structs. We use the dev_node field of the mac_serial struct.
2263 chan_init(struct mac_serial *zss, struct mac_zschannel *zs_chan,
2264 struct mac_zschannel *zs_chan_a)
2266 struct device_node *ch = zss->dev_node;
2269 struct slot_names_prop {
2274 zss->irq = ch->intrs[0].line;
2276 #if !defined(CONFIG_KGDB) && defined(SUPPORT_SERIAL_DMA)
2277 if (ch->n_addrs >= 3 && ch->n_intrs == 3)
2280 zss->dma_initted = 0;
2282 zs_chan->control = (volatile unsigned char *)
2283 ioremap(ch->addrs[0].address, 0x1000);
2284 zs_chan->data = zs_chan->control + 0x10;
2285 spin_lock_init(&zs_chan->lock);
2286 zs_chan->parent = zss;
2287 zss->zs_channel = zs_chan;
2288 zss->zs_chan_a = zs_chan_a;
2290 /* setup misc varariables */
2291 zss->kgdb_channel = 0;
2293 /* For now, we assume you either have a slot-names property
2294 * with "Modem" in it, or your channel is compatible with
2295 * "cobalt". Might need additional fixups
2297 zss->is_internal_modem = device_is_compatible(ch, "cobalt");
2298 conn = get_property(ch, "AAPL,connector", &len);
2299 zss->is_irda = conn && (strcmp(conn, "infrared") == 0);
2300 zss->port_type = PMAC_SCC_ASYNC;
2301 /* 1999 Powerbook G3 has slot-names property instead */
2302 slots = (struct slot_names_prop *)get_property(ch, "slot-names", &len);
2303 if (slots && slots->count > 0) {
2304 if (strcmp(slots->name, "IrDA") == 0)
2306 else if (strcmp(slots->name, "Modem") == 0)
2307 zss->is_internal_modem = 1;
2310 zss->port_type = PMAC_SCC_IRDA;
2311 if (zss->is_internal_modem) {
2312 struct device_node* i2c_modem = find_devices("i2c-modem");
2314 char* mid = get_property(i2c_modem, "modem-id", NULL);
2315 if (mid) switch(*mid) {
2322 zss->port_type = PMAC_SCC_I2S1;
2324 printk(KERN_INFO "macserial: i2c-modem detected, id: %d\n",
2327 printk(KERN_INFO "macserial: serial modem detected\n");
2331 while (zss->has_dma) {
2332 zss->dma_priv = NULL;
2333 /* it seems that the last two addresses are the
2335 zss->tx_dma = (volatile struct dbdma_regs *)
2336 ioremap(ch->addrs[ch->n_addrs - 2].address, 0x100);
2337 zss->rx = (volatile struct mac_dma *)
2338 ioremap(ch->addrs[ch->n_addrs - 1].address, 0x100);
2339 zss->tx_dma_irq = ch->intrs[1].line;
2340 zss->rx_dma_irq = ch->intrs[2].line;
2341 spin_lock_init(&zss->rx_dma_lock);
2345 init_timer(&zss->powerup_timer);
2346 zss->powerup_timer.function = powerup_done;
2347 zss->powerup_timer.data = (unsigned long) zss;
2352 * /proc fs routines. TODO: Add status lines & error stats
2355 line_info(char *buf, struct mac_serial *info)
2358 unsigned char* connector;
2361 ret += sprintf(buf, "%d: port:0x%X irq:%d", info->line, info->port, info->irq);
2363 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2365 ret+=sprintf(buf+ret," con:%s ", connector);
2366 if (info->is_internal_modem) {
2368 ret+=sprintf(buf+ret," con:");
2369 ret+=sprintf(buf+ret,"%s", " (internal modem)");
2371 if (info->is_irda) {
2373 ret+=sprintf(buf+ret," con:");
2374 ret+=sprintf(buf+ret,"%s", " (IrDA)");
2376 ret+=sprintf(buf+ret,"\n");
2381 int macserial_read_proc(char *page, char **start, off_t off, int count,
2382 int *eof, void *data)
2386 struct mac_serial *info;
2388 len += sprintf(page, "serinfo:1.0 driver:" MACSERIAL_VERSION "\n");
2389 for (info = zs_chain; info && len < 4000; info = info->zs_next) {
2390 l = line_info(page + len, info);
2392 if (len+begin > off+count)
2394 if (len+begin < off) {
2401 if (off >= len+begin)
2403 *start = page + (off-begin);
2404 return ((count < begin+len-off) ? count : begin+len-off);
2407 /* Ask the PROM how many Z8530s we have and initialize their zs_channels */
2411 struct device_node *dev, *ch;
2412 struct mac_serial **pp;
2414 struct mac_zschannel *zs_chan;
2419 zs_chan = zs_channels;
2420 for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
2423 if (n >= NUM_CHANNELS) {
2424 printk(KERN_WARNING "Sorry, can't use %s: no more "
2425 "channels\n", dev->full_name);
2429 for (ch = dev->child; ch != 0; ch = ch->sibling) {
2431 printk(KERN_WARNING "SCC: Only 2 channels per "
2432 "chip are supported\n");
2435 if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
2436 printk("Can't use %s: %d addrs %d intrs\n",
2437 ch->full_name, ch->n_addrs, ch->n_intrs);
2441 /* The channel with the higher address
2442 will be the A side. */
2444 ch->addrs[0].address
2445 > zs_soft[n-1].dev_node->addrs[0].address)
2448 /* minimal initialization for now */
2449 zs_soft[n].dev_node = ch;
2451 pp = &zs_soft[n].zs_next;
2459 if (chan_init(&zs_soft[chip + chan_a_index], zs_chan, zs_chan))
2463 /* set up B side, if it exists */
2465 if (chan_init(&zs_soft[chip + 1 - chan_a_index],
2466 zs_chan, zs_chan - 1))
2472 zs_channels_found = n;
2473 #ifdef CONFIG_PMAC_PBOOK
2475 pmu_register_sleep_notifier(&serial_sleep_notifier);
2476 #endif /* CONFIG_PMAC_PBOOK */
2479 static struct tty_operations serial_ops = {
2483 .flush_chars = rs_flush_chars,
2484 .write_room = rs_write_room,
2485 .chars_in_buffer = rs_chars_in_buffer,
2486 .flush_buffer = rs_flush_buffer,
2488 .throttle = rs_throttle,
2489 .unthrottle = rs_unthrottle,
2490 .set_termios = rs_set_termios,
2493 .hangup = rs_hangup,
2494 .break_ctl = rs_break,
2495 .wait_until_sent = rs_wait_until_sent,
2496 .read_proc = macserial_read_proc,
2497 .tiocmget = rs_tiocmget,
2498 .tiocmset = rs_tiocmset,
2501 static int macserial_init(void)
2504 struct mac_serial *info;
2506 /* Find out how many Z8530 SCCs we have */
2510 serial_driver = alloc_tty_driver(zs_channels_found);
2514 /* XXX assume it's a powerbook if we have a via-pmu
2516 * This is OK for core99 machines as well.
2518 is_powerbook = find_devices("via-pmu") != 0;
2520 /* Register the interrupt handler for each one
2521 * We also request the OF resources here as probe_sccs()
2522 * might be called too early for that
2524 for (i = 0; i < zs_channels_found; ++i) {
2525 struct device_node* ch = zs_soft[i].dev_node;
2526 if (!request_OF_resource(ch, 0, NULL)) {
2527 printk(KERN_ERR "macserial: can't request IO resource !\n");
2528 put_tty_driver(serial_driver);
2531 if (zs_soft[i].has_dma) {
2532 if (!request_OF_resource(ch, ch->n_addrs - 2, " (tx dma)")) {
2533 printk(KERN_ERR "macserial: can't request TX DMA resource !\n");
2534 zs_soft[i].has_dma = 0;
2537 if (!request_OF_resource(ch, ch->n_addrs - 1, " (rx dma)")) {
2538 release_OF_resource(ch, ch->n_addrs - 2);
2539 printk(KERN_ERR "macserial: can't request RX DMA resource !\n");
2540 zs_soft[i].has_dma = 0;
2543 if (request_irq(zs_soft[i].tx_dma_irq, rs_txdma_irq, 0,
2544 "SCC-txdma", &zs_soft[i]))
2545 printk(KERN_ERR "macserial: can't get irq %d\n",
2546 zs_soft[i].tx_dma_irq);
2547 disable_irq(zs_soft[i].tx_dma_irq);
2548 if (request_irq(zs_soft[i].rx_dma_irq, rs_rxdma_irq, 0,
2549 "SCC-rxdma", &zs_soft[i]))
2550 printk(KERN_ERR "macserial: can't get irq %d\n",
2551 zs_soft[i].rx_dma_irq);
2552 disable_irq(zs_soft[i].rx_dma_irq);
2555 if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
2556 "SCC", &zs_soft[i]))
2557 printk(KERN_ERR "macserial: can't get irq %d\n",
2559 disable_irq(zs_soft[i].irq);
2562 show_serial_version();
2564 /* Initialize the tty_driver structure */
2565 /* Not all of this is exactly right for us. */
2567 serial_driver->owner = THIS_MODULE;
2568 serial_driver->driver_name = "macserial";
2569 serial_driver->devfs_name = "tts/";
2570 serial_driver->name = "ttyS";
2571 serial_driver->major = TTY_MAJOR;
2572 serial_driver->minor_start = 64;
2573 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2574 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2575 serial_driver->init_termios = tty_std_termios;
2576 serial_driver->init_termios.c_cflag =
2577 B38400 | CS8 | CREAD | HUPCL | CLOCAL;
2578 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2579 tty_set_operations(serial_driver, &serial_ops);
2581 if (tty_register_driver(serial_driver))
2582 printk(KERN_ERR "Error: couldn't register serial driver\n");
2584 for (channel = 0; channel < zs_channels_found; ++channel) {
2586 if (zs_soft[channel].kgdb_channel) {
2587 kgdb_interruptible(1);
2591 zs_soft[channel].clk_divisor = 16;
2592 /* -- we are not sure the SCC is powered ON at this point
2593 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2595 zs_soft[channel].zs_baud = 38400;
2597 /* If console serial line, then enable interrupts. */
2598 if (zs_soft[channel].is_cons) {
2599 printk(KERN_INFO "macserial: console line, enabling "
2600 "interrupt %d\n", zs_soft[channel].irq);
2601 panic("macserial: console not supported yet !");
2602 write_zsreg(zs_soft[channel].zs_channel, R1,
2603 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
2604 write_zsreg(zs_soft[channel].zs_channel, R9,
2609 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2611 unsigned char* connector;
2615 if (info->kgdb_channel) {
2619 info->magic = SERIAL_MAGIC;
2620 info->port = (int) info->zs_channel->control;
2623 info->custom_divisor = 16;
2625 info->close_delay = 50;
2626 info->closing_wait = 3000;
2630 info->blocked_open = 0;
2631 INIT_WORK(&info->tqueue, do_softint, info);
2632 spin_lock_init(&info->lock);
2633 init_waitqueue_head(&info->open_wait);
2634 init_waitqueue_head(&info->close_wait);
2636 printk(KERN_INFO "tty%02d at 0x%08x (irq = %d)", info->line,
2637 info->port, info->irq);
2638 printk(" is a Z8530 ESCC");
2639 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2641 printk(", port = %s", connector);
2642 if (info->is_internal_modem)
2643 printk(" (internal modem)");
2653 void macserial_cleanup(void)
2656 unsigned long flags;
2657 struct mac_serial *info;
2659 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2660 set_scc_power(info, 0);
2661 spin_lock_irqsave(&info->lock, flags);
2662 for (i = 0; i < zs_channels_found; ++i) {
2663 free_irq(zs_soft[i].irq, &zs_soft[i]);
2664 if (zs_soft[i].has_dma) {
2665 free_irq(zs_soft[i].tx_dma_irq, &zs_soft[i]);
2666 free_irq(zs_soft[i].rx_dma_irq, &zs_soft[i]);
2668 release_OF_resource(zs_soft[i].dev_node, 0);
2669 if (zs_soft[i].has_dma) {
2670 struct device_node* ch = zs_soft[i].dev_node;
2671 release_OF_resource(ch, ch->n_addrs - 2);
2672 release_OF_resource(ch, ch->n_addrs - 1);
2675 spin_unlock_irqrestore(&info->lock, flags);
2676 tty_unregister_driver(serial_driver);
2677 put_tty_driver(serial_driver);
2680 free_page((unsigned long) tmp_buf);
2684 #ifdef CONFIG_PMAC_PBOOK
2685 if (zs_channels_found)
2686 pmu_unregister_sleep_notifier(&serial_sleep_notifier);
2687 #endif /* CONFIG_PMAC_PBOOK */
2690 module_init(macserial_init);
2691 module_exit(macserial_cleanup);
2692 MODULE_LICENSE("GPL");
2696 * register_serial and unregister_serial allows for serial ports to be
2697 * configured at run-time, to support PCMCIA modems.
2699 /* PowerMac: Unused at this time, just here to make things link. */
2700 int register_serial(struct serial_struct *req)
2705 void unregister_serial(int line)
2712 * ------------------------------------------------------------
2713 * Serial console driver
2714 * ------------------------------------------------------------
2716 #ifdef CONFIG_SERIAL_CONSOLE
2719 * Print a string to the serial port trying not to disturb
2720 * any possible real use of the port...
2722 static void serial_console_write(struct console *co, const char *s,
2725 struct mac_serial *info = zs_soft + co->index;
2728 /* Turn of interrupts and enable the transmitter. */
2729 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2730 write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2732 for (i=0; i<count; i++) {
2733 /* Wait for the transmit buffer to empty. */
2734 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2738 write_zsdata(info->zs_channel, s[i]);
2740 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2744 write_zsdata(info->zs_channel, 13);
2748 /* Restore the values in the registers. */
2749 write_zsreg(info->zs_channel, R1, info->curregs[1]);
2750 /* Don't disable the transmitter. */
2753 static struct tty_driver *serial_driver;
2755 static struct tty_driver *serial_console_device(struct console *c, int *index)
2758 return serial_driver;
2762 * Setup initial baud/bits/parity. We do two things here:
2763 * - construct a cflag setting for the first rs_open()
2764 * - initialize the serial port
2765 * Return non-zero if we didn't find a serial port.
2767 static int __init serial_console_setup(struct console *co, char *options)
2769 struct mac_serial *info;
2773 int cflag = CREAD | HUPCL | CLOCAL;
2778 /* Find out how many Z8530 SCCs we have */
2785 /* Do we have the device asked for? */
2786 if (co->index >= zs_channels_found)
2788 info = zs_soft + co->index;
2790 set_scc_power(info, 1);
2792 /* Reset the channel */
2793 write_zsreg(info->zs_channel, R9, CHRA);
2796 baud = simple_strtoul(options, NULL, 10);
2798 while(*s >= '0' && *s <= '9')
2807 * Now construct a cflag setting.
2847 cflag |= PARENB | PARODD;
2855 spin_lock_irqsave(&info->lock, flags);
2856 memset(info->curregs, 0, sizeof(info->curregs));
2858 info->zs_baud = baud;
2859 info->clk_divisor = 16;
2860 switch (info->zs_baud) {
2861 case ZS_CLOCK/16: /* 230400 */
2862 info->curregs[4] = X16CLK;
2863 info->curregs[11] = 0;
2865 case ZS_CLOCK/32: /* 115200 */
2866 info->curregs[4] = X32CLK;
2867 info->curregs[11] = 0;
2870 info->curregs[4] = X16CLK;
2871 info->curregs[11] = TCBR | RCBR;
2872 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2873 info->curregs[12] = (brg & 255);
2874 info->curregs[13] = ((brg >> 8) & 255);
2875 info->curregs[14] = BRENABL;
2878 /* byte size and parity */
2879 info->curregs[3] &= ~RxNBITS_MASK;
2880 info->curregs[5] &= ~TxNBITS_MASK;
2881 switch (cflag & CSIZE) {
2883 info->curregs[3] |= Rx5;
2884 info->curregs[5] |= Tx5;
2887 info->curregs[3] |= Rx6;
2888 info->curregs[5] |= Tx6;
2891 info->curregs[3] |= Rx7;
2892 info->curregs[5] |= Tx7;
2895 default: /* defaults to 8 bits */
2896 info->curregs[3] |= Rx8;
2897 info->curregs[5] |= Tx8;
2900 info->curregs[5] |= TxENAB | RTS | DTR;
2901 info->pendregs[3] = info->curregs[3];
2902 info->pendregs[5] = info->curregs[5];
2904 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
2905 if (cflag & CSTOPB) {
2906 info->curregs[4] |= SB2;
2908 info->curregs[4] |= SB1;
2910 if (cflag & PARENB) {
2911 info->curregs[4] |= PAR_ENA;
2912 if (!(cflag & PARODD)) {
2913 info->curregs[4] |= PAR_EVEN;
2916 info->pendregs[4] = info->curregs[4];
2918 if (!(cflag & CLOCAL)) {
2919 if (!(info->curregs[15] & DCDIE))
2920 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2921 info->curregs[15] |= DCDIE;
2923 info->curregs[15] &= ~DCDIE;
2924 if (cflag & CRTSCTS) {
2925 info->curregs[15] |= CTSIE;
2926 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
2927 info->tx_stopped = 1;
2929 info->curregs[15] &= ~CTSIE;
2930 info->tx_stopped = 0;
2932 info->pendregs[15] = info->curregs[15];
2934 /* Load up the new values */
2935 load_zsregs(info->zs_channel, info->curregs);
2937 spin_unlock_irqrestore(&info->lock, flags);
2942 static struct console sercons = {
2944 .write = serial_console_write,
2945 .device = serial_console_device,
2946 .setup = serial_console_setup,
2947 .flags = CON_PRINTBUFFER,
2954 static void __init mac_scc_console_init(void)
2956 register_console(&sercons);
2958 console_initcall(mac_scc_console_init);
2960 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2963 /* These are for receiving and sending characters under the kgdb
2964 * source level kernel debugger.
2966 void putDebugChar(char kgdb_char)
2968 struct mac_zschannel *chan = zs_kgdbchan;
2969 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2971 write_zsdata(chan, kgdb_char);
2974 char getDebugChar(void)
2976 struct mac_zschannel *chan = zs_kgdbchan;
2977 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2978 eieio(); /*barrier();*/
2979 return read_zsdata(chan);
2982 void kgdb_interruptible(int yes)
2984 struct mac_zschannel *chan = zs_kgdbchan;
2986 nine = read_zsreg(chan, 9);
2988 one = EXT_INT_ENAB|INT_ALL_Rx;
2990 printk("turning serial ints on\n");
2994 printk("turning serial ints off\n");
2996 write_zsreg(chan, 1, one);
2997 write_zsreg(chan, 9, nine);
3000 /* This sets up the serial port we're using, and turns on
3001 * interrupts for that channel, so kgdb is usable once we're done.
3003 static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
3007 volatile char *sccc = ms->control;
3008 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
3009 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
3010 for (i = 20000; i != 0; --i) {
3013 for (i = 0; i < sizeof(scc_inittab); ++i) {
3014 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
3019 /* This is called at boot time to prime the kgdb serial debugging
3020 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
3021 * for /dev/ttyb which is determined in setup_arch() from the
3022 * boot command line flags.
3023 * XXX at the moment probably only channel A will work
3025 void __init zs_kgdb_hook(int tty_num)
3027 /* Find out how many Z8530 SCCs we have */
3031 set_scc_power(&zs_soft[tty_num], 1);
3033 zs_kgdbchan = zs_soft[tty_num].zs_channel;
3034 zs_soft[tty_num].change_needed = 0;
3035 zs_soft[tty_num].clk_divisor = 16;
3036 zs_soft[tty_num].zs_baud = 38400;
3037 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
3039 /* Turn on transmitter/receiver at 8-bits/char */
3040 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
3041 printk("KGDB: on channel %d initialized\n", tty_num);
3042 set_debug_traps(); /* init stub */
3044 #endif /* ifdef CONFIG_KGDB */
3046 #ifdef CONFIG_PMAC_PBOOK
3048 * notify clients before sleep and reset bus afterwards
3051 serial_notify_sleep(struct pmu_sleep_notifier *self, int when)
3056 case PBOOK_SLEEP_REQUEST:
3057 case PBOOK_SLEEP_REJECT:
3060 case PBOOK_SLEEP_NOW:
3061 for (i=0; i<zs_channels_found; i++) {
3062 struct mac_serial *info = &zs_soft[i];
3063 if (info->flags & ZILOG_INITIALIZED) {
3065 info->flags |= ZILOG_SLEEPING;
3070 for (i=0; i<zs_channels_found; i++) {
3071 struct mac_serial *info = &zs_soft[i];
3072 if (info->flags & ZILOG_SLEEPING) {
3073 info->flags &= ~ZILOG_SLEEPING;
3079 return PBOOK_SLEEP_OK;
3081 #endif /* CONFIG_PMAC_PBOOK */