VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / macintosh / macserial.c
1 /*
2  * macserial.c: Serial port driver for Power Macintoshes.
3  *
4  * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5  *
6  * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
7  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8  *
9  * Receive DMA code by Takashi Oe <toe@unlserve.unl.edu>.
10  *
11  * $Id: macserial.c,v 1.24.2.4 1999/10/19 04:36:42 paulus Exp $
12  */
13
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>
27 #include <linux/mm.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>
33 #endif
34 #include <linux/slab.h>
35
36 #include <asm/sections.h>
37 #include <asm/io.h>
38 #include <asm/pgtable.h>
39 #include <asm/irq.h>
40 #include <asm/prom.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>
48 #ifdef CONFIG_KGDB
49 #include <asm/kgdb.h>
50 #endif
51 #include <asm/dbdma.h>
52
53 #include "macserial.h"
54
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 = {
58         serial_notify_sleep,
59         SLEEP_LEVEL_MISC,
60 };
61 #endif
62
63 #define SUPPORT_SERIAL_DMA
64 #define MACSERIAL_VERSION       "2.0"
65
66 /*
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...
70  */
71 #define NUM_SERIAL      2               /* Max number of ZS chips supported */
72 #define NUM_CHANNELS    (NUM_SERIAL * 2)        /* 2 channels per chip */
73
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()
78
79 static struct tty_driver *serial_driver;
80
81 struct mac_zschannel zs_channels[NUM_CHANNELS];
82
83 struct mac_serial zs_soft[NUM_CHANNELS];
84 int zs_channels_found;
85 struct mac_serial *zs_chain;    /* list of all channels */
86
87 struct tty_struct zs_ttys[NUM_CHANNELS];
88
89 static int is_powerbook;
90
91 #ifdef CONFIG_SERIAL_CONSOLE
92 static struct console sercons;
93 #endif
94
95 #ifdef CONFIG_KGDB
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 */
100         12, 1,
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)*/
106 };
107 #endif
108 #define ZS_CLOCK         3686400        /* Z8530 RTxC input clock rate */
109
110 /* serial subtype definitions */
111 #define SERIAL_TYPE_NORMAL      1
112
113 /* number of characters left in xmit buffer before we ask for more */
114 #define WAKEUP_CHARS 256
115
116 /*
117  * Debugging.
118  */
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
126
127 #define RS_STROBE_TIME 10
128 #define RS_ISR_PASS_LIMIT 256
129
130 #define _INLINE_ inline
131
132 #ifdef SERIAL_DEBUG_OPEN
133 #define OPNDBG(fmt, arg...)     printk(KERN_DEBUG fmt , ## arg)
134 #else
135 #define OPNDBG(fmt, arg...)     do { } while (0)
136 #endif
137 #ifdef SERIAL_DEBUG_POWER
138 #define PWRDBG(fmt, arg...)     printk(KERN_DEBUG fmt , ## arg)
139 #else
140 #define PWRDBG(fmt, arg...)     do { } while (0)
141 #endif
142 #ifdef SERIAL_DEBUG_BAUDS
143 #define BAUDBG(fmt, arg...)     printk(fmt , ## arg)
144 #else
145 #define BAUDBG(fmt, arg...)     do { } while (0)
146 #endif
147
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);
160
161 /*
162  * tmp_buf is used as a temporary buffer by serial_write.  We need to
163  * lock it in case the copy_from_user blocks while swapping in a page,
164  * and some other program tries to do a serial write at the same time.
165  * Since the lock will only come under contention when the system is
166  * swapping and available memory is low, it makes sense to share one
167  * buffer across all the serial ports, since it significantly saves
168  * memory if large numbers of serial ports are open.
169  */
170 static unsigned char *tmp_buf;
171 static DECLARE_MUTEX(tmp_buf_sem);
172
173
174 static inline int __pmac
175 serial_paranoia_check(struct mac_serial *info,
176                       char *name, const char *routine)
177 {
178 #ifdef SERIAL_PARANOIA_CHECK
179         static const char badmagic[] = KERN_WARNING
180                 "Warning: bad magic number for serial struct %s in %s\n";
181         static const char badinfo[] = KERN_WARNING
182                 "Warning: null mac_serial for %s in %s\n";
183
184         if (!info) {
185                 printk(badinfo, name, routine);
186                 return 1;
187         }
188         if (info->magic != SERIAL_MAGIC) {
189                 printk(badmagic, name, routine);
190                 return 1;
191         }
192 #endif
193         return 0;
194 }
195
196 /* 
197  * Reading and writing Z8530 registers.
198  */
199 static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
200                                               unsigned char reg)
201 {
202         unsigned char retval;
203         unsigned long flags;
204
205         /*
206          * We have to make this atomic.
207          */
208         spin_lock_irqsave(&channel->lock, flags);
209         if (reg != 0) {
210                 *channel->control = reg;
211                 RECOVERY_DELAY;
212         }
213         retval = *channel->control;
214         RECOVERY_DELAY;
215         spin_unlock_irqrestore(&channel->lock, flags);
216         return retval;
217 }
218
219 static inline void __pmac write_zsreg(struct mac_zschannel *channel,
220                                       unsigned char reg, unsigned char value)
221 {
222         unsigned long flags;
223
224         spin_lock_irqsave(&channel->lock, flags);
225         if (reg != 0) {
226                 *channel->control = reg;
227                 RECOVERY_DELAY;
228         }
229         *channel->control = value;
230         RECOVERY_DELAY;
231         spin_unlock_irqrestore(&channel->lock, flags);
232         return;
233 }
234
235 static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
236 {
237         unsigned char retval;
238
239         retval = *channel->data;
240         RECOVERY_DELAY;
241         return retval;
242 }
243
244 static inline void write_zsdata(struct mac_zschannel *channel,
245                                 unsigned char value)
246 {
247         *channel->data = value;
248         RECOVERY_DELAY;
249         return;
250 }
251
252 static inline void load_zsregs(struct mac_zschannel *channel,
253                                unsigned char *regs)
254 {
255         ZS_CLEARERR(channel);
256         ZS_CLEARFIFO(channel);
257         /* Load 'em up */
258         write_zsreg(channel, R4, regs[R4]);
259         write_zsreg(channel, R10, regs[R10]);
260         write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
261         write_zsreg(channel, R5, regs[R5] & ~TxENAB);
262         write_zsreg(channel, R1, regs[R1]);
263         write_zsreg(channel, R9, regs[R9]);
264         write_zsreg(channel, R11, regs[R11]);
265         write_zsreg(channel, R12, regs[R12]);
266         write_zsreg(channel, R13, regs[R13]);
267         write_zsreg(channel, R14, regs[R14]);
268         write_zsreg(channel, R15, regs[R15]);
269         write_zsreg(channel, R3, regs[R3]);
270         write_zsreg(channel, R5, regs[R5]);
271         return;
272 }
273
274 /* Sets or clears DTR/RTS on the requested line */
275 static inline void zs_rtsdtr(struct mac_serial *ss, int set)
276 {
277         if (set)
278                 ss->curregs[5] |= (RTS | DTR);
279         else
280                 ss->curregs[5] &= ~(RTS | DTR);
281         write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
282         return;
283 }
284
285 /* Utility routines for the Zilog */
286 static inline int get_zsbaud(struct mac_serial *ss)
287 {
288         struct mac_zschannel *channel = ss->zs_channel;
289         int brg;
290
291         if ((ss->curregs[R11] & TCBR) == 0) {
292                 /* higher rates don't use the baud rate generator */
293                 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
294         }
295         /* The baud rate is split up between two 8-bit registers in
296          * what is termed 'BRG time constant' format in my docs for
297          * the chip, it is a function of the clk rate the chip is
298          * receiving which happens to be constant.
299          */
300         brg = (read_zsreg(channel, 13) << 8);
301         brg |= read_zsreg(channel, 12);
302         return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
303 }
304
305 /* On receive, this clears errors and the receiver interrupts */
306 static inline void rs_recv_clear(struct mac_zschannel *zsc)
307 {
308         write_zsreg(zsc, 0, ERR_RES);
309         write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
310 }
311
312 /*
313  * Reset a Descriptor-Based DMA channel.
314  */
315 static void dbdma_reset(volatile struct dbdma_regs *dma)
316 {
317         int i;
318
319         out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
320
321         /*
322          * Yes this looks peculiar, but apparently it needs to be this
323          * way on some machines.  (We need to make sure the DBDMA
324          * engine has actually got the write above and responded
325          * to it. - paulus)
326          */
327         for (i = 200; i > 0; --i)
328                 if (ld_le32(&dma->status) & RUN)
329                         udelay(1);
330 }
331
332 /*
333  * Tells a DBDMA channel to stop and write any buffered data
334  * it might have to memory.
335  */
336 static _INLINE_ void dbdma_flush(volatile struct dbdma_regs *dma)
337 {
338         int i = 0;
339
340         out_le32(&dma->control, (FLUSH << 16) | FLUSH);
341         while (((in_le32(&dma->status) & FLUSH) != 0) && (i++ < 100))
342                 udelay(1);
343 }
344
345 /*
346  * ----------------------------------------------------------------------
347  *
348  * Here starts the interrupt handling routines.  All of the following
349  * subroutines are declared as inline and are folded into
350  * rs_interrupt().  They were separated out for readability's sake.
351  *
352  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
353  * -----------------------------------------------------------------------
354  */
355
356 /*
357  * This routine is used by the interrupt handler to schedule
358  * processing in the software interrupt portion of the driver.
359  */
360 static _INLINE_ void rs_sched_event(struct mac_serial *info,
361                                   int event)
362 {
363         info->event |= 1 << event;
364         schedule_work(&info->tqueue);
365 }
366
367 /* Work out the flag value for a z8530 status value. */
368 static _INLINE_ int stat_to_flag(int stat)
369 {
370         int flag;
371
372         if (stat & Rx_OVR) {
373                 flag = TTY_OVERRUN;
374         } else if (stat & FRM_ERR) {
375                 flag = TTY_FRAME;
376         } else if (stat & PAR_ERR) {
377                 flag = TTY_PARITY;
378         } else
379                 flag = 0;
380         return flag;
381 }
382
383 static _INLINE_ void receive_chars(struct mac_serial *info,
384                                    struct pt_regs *regs)
385 {
386         struct tty_struct *tty = info->tty;
387         unsigned char ch, stat, flag;
388
389         while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
390
391                 stat = read_zsreg(info->zs_channel, R1);
392                 ch = read_zsdata(info->zs_channel);
393
394 #ifdef CONFIG_KGDB
395                 if (info->kgdb_channel) {
396                         if (ch == 0x03 || ch == '$')
397                                 breakpoint();
398                         if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
399                                 write_zsreg(info->zs_channel, 0, ERR_RES);
400                         return;
401                 }
402 #endif
403                 if (!tty)
404                         continue;
405                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
406                         tty_flip_buffer_push(tty);
407
408                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
409                         static int flip_buf_ovf;
410                         if (++flip_buf_ovf <= 1)
411                                 printk(KERN_WARNING "FB. overflow: %d\n",
412                                                     flip_buf_ovf);
413                         break;
414                 }
415                 tty->flip.count++;
416                 {
417                         static int flip_max_cnt;
418                         if (flip_max_cnt < tty->flip.count)
419                                 flip_max_cnt = tty->flip.count;
420                 }
421                 flag = stat_to_flag(stat);
422                 if (flag)
423                         /* reset the error indication */
424                         write_zsreg(info->zs_channel, 0, ERR_RES);
425                 *tty->flip.flag_buf_ptr++ = flag;
426                 *tty->flip.char_buf_ptr++ = ch;
427         }
428         if (tty)
429                 tty_flip_buffer_push(tty);
430 }
431
432 static void transmit_chars(struct mac_serial *info)
433 {
434         if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
435                 return;
436         info->tx_active = 0;
437
438         if (info->x_char && !info->power_wait) {
439                 /* Send next char */
440                 write_zsdata(info->zs_channel, info->x_char);
441                 info->x_char = 0;
442                 info->tx_active = 1;
443                 return;
444         }
445
446         if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped
447             || info->power_wait) {
448                 write_zsreg(info->zs_channel, 0, RES_Tx_P);
449                 return;
450         }
451
452         /* Send char */
453         write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
454         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
455         info->xmit_cnt--;
456         info->tx_active = 1;
457
458         if (info->xmit_cnt < WAKEUP_CHARS)
459                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
460 }
461
462 static void powerup_done(unsigned long data)
463 {
464         struct mac_serial *info = (struct mac_serial *) data;
465         unsigned long flags;
466
467         spin_lock_irqsave(&info->lock, flags);
468         info->power_wait = 0;
469         transmit_chars(info);
470         spin_unlock_irqrestore(&info->lock, flags);
471 }
472
473 static _INLINE_ void status_handle(struct mac_serial *info)
474 {
475         unsigned char status;
476
477         /* Get status from Read Register 0 */
478         status = read_zsreg(info->zs_channel, 0);
479
480         /* Check for DCD transitions */
481         if (((status ^ info->read_reg_zero) & DCD) != 0
482             && info->tty && !C_CLOCAL(info->tty)) {
483                 if (status & DCD) {
484                         wake_up_interruptible(&info->open_wait);
485                 } else {
486                         if (info->tty)
487                                 tty_hangup(info->tty);
488                 }
489         }
490
491         /* Check for CTS transitions */
492         if (info->tty && C_CRTSCTS(info->tty)) {
493                 /*
494                  * For some reason, on the Power Macintosh,
495                  * it seems that the CTS bit is 1 when CTS is
496                  * *negated* and 0 when it is asserted.
497                  * The DCD bit doesn't seem to be inverted
498                  * like this.
499                  */
500                 if ((status & CTS) == 0) {
501                         if (info->tx_stopped) {
502 #ifdef SERIAL_DEBUG_FLOW
503                                 printk(KERN_DEBUG "CTS up\n");
504 #endif
505                                 info->tx_stopped = 0;
506                                 if (!info->tx_active)
507                                         transmit_chars(info);
508                         }
509                 } else {
510 #ifdef SERIAL_DEBUG_FLOW
511                         printk(KERN_DEBUG "CTS down\n");
512 #endif
513                         info->tx_stopped = 1;
514                 }
515         }
516
517         /* Clear status condition... */
518         write_zsreg(info->zs_channel, 0, RES_EXT_INT);
519         info->read_reg_zero = status;
520 }
521
522 static _INLINE_ void receive_special_dma(struct mac_serial *info)
523 {
524         unsigned char stat, flag;
525         volatile struct dbdma_regs *rd = &info->rx->dma;
526         int where = RX_BUF_SIZE;
527
528         spin_lock(&info->rx_dma_lock);
529         if ((ld_le32(&rd->status) & ACTIVE) != 0)
530                 dbdma_flush(rd);
531         if (in_le32(&rd->cmdptr)
532             == virt_to_bus(info->rx_cmds[info->rx_cbuf] + 1))
533                 where -= in_le16(&info->rx->res_count);
534         where--;
535
536         stat = read_zsreg(info->zs_channel, R1);
537
538         flag = stat_to_flag(stat);
539         if (flag) {
540                 info->rx_flag_buf[info->rx_cbuf][where] = flag;
541                 /* reset the error indication */
542                 write_zsreg(info->zs_channel, 0, ERR_RES);
543         }
544
545         spin_unlock(&info->rx_dma_lock);
546 }
547
548 /*
549  * This is the serial driver's generic interrupt routine
550  */
551 static irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
552 {
553         struct mac_serial *info = (struct mac_serial *) dev_id;
554         unsigned char zs_intreg;
555         int shift;
556         unsigned long flags;
557         int handled = 0;
558
559         if (!(info->flags & ZILOG_INITIALIZED)) {
560                 printk(KERN_WARNING "rs_interrupt: irq %d, port not "
561                                     "initialized\n", irq);
562                 disable_irq(irq);
563                 return IRQ_NONE;
564         }
565
566         /* NOTE: The read register 3, which holds the irq status,
567          *       does so for both channels on each chip.  Although
568          *       the status value itself must be read from the A
569          *       channel and is only valid when read from channel A.
570          *       Yes... broken hardware...
571          */
572 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
573
574         if (info->zs_chan_a == info->zs_channel)
575                 shift = 3;      /* Channel A */
576         else
577                 shift = 0;      /* Channel B */
578
579         spin_lock_irqsave(&info->lock, flags);
580         for (;;) {
581                 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
582 #ifdef SERIAL_DEBUG_INTR
583                 printk(KERN_DEBUG "rs_interrupt: irq %d, zs_intreg 0x%x\n",
584                        irq, (int)zs_intreg);
585 #endif
586
587                 if ((zs_intreg & CHAN_IRQMASK) == 0)
588                         break;
589                 handled = 1;
590
591                 if (zs_intreg & CHBRxIP) {
592                         /* If we are doing DMA, we only ask for interrupts
593                            on characters with errors or special conditions. */
594                         if (info->dma_initted)
595                                 receive_special_dma(info);
596                         else
597                                 receive_chars(info, regs);
598                 }
599                 if (zs_intreg & CHBTxIP)
600                         transmit_chars(info);
601                 if (zs_intreg & CHBEXT)
602                         status_handle(info);
603         }
604         spin_unlock_irqrestore(&info->lock, flags);
605         return IRQ_RETVAL(handled);
606 }
607
608 /* Transmit DMA interrupt - not used at present */
609 static irqreturn_t rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs)
610 {
611         return IRQ_HANDLED;
612 }
613
614 /*
615  * Receive DMA interrupt.
616  */
617 static irqreturn_t rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs)
618 {
619         struct mac_serial *info = (struct mac_serial *) dev_id;
620         volatile struct dbdma_cmd *cd;
621
622         if (!info->dma_initted)
623                 return IRQ_NONE;
624         spin_lock(&info->rx_dma_lock);
625         /* First, confirm that this interrupt is, indeed, coming */
626         /* from Rx DMA */
627         cd = info->rx_cmds[info->rx_cbuf] + 2;
628         if ((in_le16(&cd->xfer_status) & (RUN | ACTIVE)) != (RUN | ACTIVE)) {
629                 spin_unlock(&info->rx_dma_lock);
630                 return IRQ_NONE;
631         }
632         if (info->rx_fbuf != RX_NO_FBUF) {
633                 info->rx_cbuf = info->rx_fbuf;
634                 if (++info->rx_fbuf == info->rx_nbuf)
635                         info->rx_fbuf = 0;
636                 if (info->rx_fbuf == info->rx_ubuf)
637                         info->rx_fbuf = RX_NO_FBUF;
638         }
639         spin_unlock(&info->rx_dma_lock);
640         return IRQ_HANDLED;
641 }
642
643 /*
644  * -------------------------------------------------------------------
645  * Here ends the serial interrupt routines.
646  * -------------------------------------------------------------------
647  */
648
649 /*
650  * ------------------------------------------------------------
651  * rs_stop() and rs_start()
652  *
653  * This routines are called before setting or resetting tty->stopped.
654  * ------------------------------------------------------------
655  */
656 static void rs_stop(struct tty_struct *tty)
657 {
658         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
659
660 #ifdef SERIAL_DEBUG_STOP
661         printk(KERN_DEBUG "rs_stop %ld....\n",
662                tty->ldisc.chars_in_buffer(tty));
663 #endif
664
665         if (serial_paranoia_check(info, tty->name, "rs_stop"))
666                 return;
667
668 #if 0
669         spin_lock_irqsave(&info->lock, flags);
670         if (info->curregs[5] & TxENAB) {
671                 info->curregs[5] &= ~TxENAB;
672                 info->pendregs[5] &= ~TxENAB;
673                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
674         }
675         spin_unlock_irqrestore(&info->lock, flags);
676 #endif
677 }
678
679 static void rs_start(struct tty_struct *tty)
680 {
681         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
682         unsigned long flags;
683
684 #ifdef SERIAL_DEBUG_STOP
685         printk(KERN_DEBUG "rs_start %ld....\n", 
686                tty->ldisc.chars_in_buffer(tty));
687 #endif
688
689         if (serial_paranoia_check(info, tty->name, "rs_start"))
690                 return;
691
692         spin_lock_irqsave(&info->lock, flags);
693 #if 0
694         if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
695                 info->curregs[5] |= TxENAB;
696                 info->pendregs[5] = info->curregs[5];
697                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
698         }
699 #else
700         if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
701                 transmit_chars(info);
702         }
703 #endif
704         spin_unlock_irqrestore(&info->lock, flags);
705 }
706
707 static void do_softint(void *private_)
708 {
709         struct mac_serial       *info = (struct mac_serial *) private_;
710         struct tty_struct       *tty;
711
712         tty = info->tty;
713         if (!tty)
714                 return;
715
716         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
717                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
718                     tty->ldisc.write_wakeup)
719                         (tty->ldisc.write_wakeup)(tty);
720                 wake_up_interruptible(&tty->write_wait);
721         }
722 }
723
724 static int startup(struct mac_serial * info)
725 {
726         int delay;
727
728         OPNDBG("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
729  
730         if (info->flags & ZILOG_INITIALIZED) {
731                 OPNDBG(" -> already inited\n");
732                 return 0;
733         }
734
735         if (!info->xmit_buf) {
736                 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
737                 if (!info->xmit_buf)
738                         return -ENOMEM;
739         }
740
741         OPNDBG("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
742
743         delay = set_scc_power(info, 1);
744
745         setup_scc(info);
746
747         if (delay) {
748                 unsigned long flags;
749
750                 /* delay is in ms */
751                 spin_lock_irqsave(&info->lock, flags);
752                 info->power_wait = 1;
753                 mod_timer(&info->powerup_timer,
754                           jiffies + (delay * HZ + 999) / 1000);
755                 spin_unlock_irqrestore(&info->lock, flags);
756         }
757
758         OPNDBG("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
759
760         info->flags |= ZILOG_INITIALIZED;
761         enable_irq(info->irq);
762         if (info->dma_initted) {
763                 enable_irq(info->rx_dma_irq);
764         }
765
766         return 0;
767 }
768
769 static _INLINE_ void rxdma_start(struct mac_serial * info, int current)
770 {
771         volatile struct dbdma_regs *rd = &info->rx->dma;
772         volatile struct dbdma_cmd *cd = info->rx_cmds[current];
773
774 //printk(KERN_DEBUG "SCC: rxdma_start\n");
775
776         st_le32(&rd->cmdptr, virt_to_bus(cd));
777         out_le32(&rd->control, (RUN << 16) | RUN);
778 }
779
780 static void rxdma_to_tty(struct mac_serial *info)
781 {
782         struct tty_struct       *tty = info->tty;
783         volatile struct dbdma_regs *rd = &info->rx->dma;
784         unsigned long flags;
785         int residue, available, space, do_queue;
786
787         if (!tty)
788                 return;
789
790         do_queue = 0;
791         spin_lock_irqsave(&info->rx_dma_lock, flags);
792 more:
793         space = TTY_FLIPBUF_SIZE - tty->flip.count;
794         if (!space) {
795                 do_queue++;
796                 goto out;
797         }
798         residue = 0;
799         if (info->rx_ubuf == info->rx_cbuf) {
800                 if ((ld_le32(&rd->status) & ACTIVE) != 0) {
801                         dbdma_flush(rd);
802                         if (in_le32(&rd->cmdptr)
803                             == virt_to_bus(info->rx_cmds[info->rx_cbuf]+1))
804                                 residue = in_le16(&info->rx->res_count);
805                 }
806         }
807         available = RX_BUF_SIZE - residue - info->rx_done_bytes;
808         if (available > space)
809                 available = space;
810         if (available) {
811                 memcpy(tty->flip.char_buf_ptr,
812                        info->rx_char_buf[info->rx_ubuf] + info->rx_done_bytes,
813                        available);
814                 memcpy(tty->flip.flag_buf_ptr,
815                        info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
816                        available);
817                 tty->flip.char_buf_ptr += available;
818                 tty->flip.count += available;
819                 tty->flip.flag_buf_ptr += available;
820                 memset(info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
821                        0, available);
822                 info->rx_done_bytes += available;
823                 do_queue++;
824         }
825         if (info->rx_done_bytes == RX_BUF_SIZE) {
826                 volatile struct dbdma_cmd *cd = info->rx_cmds[info->rx_ubuf];
827
828                 if (info->rx_ubuf == info->rx_cbuf)
829                         goto out;
830                 /* mark rx_char_buf[rx_ubuf] free */
831                 st_le16(&cd->command, DBDMA_NOP);
832                 cd++;
833                 st_le32(&cd->cmd_dep, 0);
834                 st_le32((unsigned int *)&cd->res_count, 0);
835                 cd++;
836                 st_le16(&cd->xfer_status, 0);
837
838                 if (info->rx_fbuf == RX_NO_FBUF) {
839                         info->rx_fbuf = info->rx_ubuf;
840                         if (!(ld_le32(&rd->status) & ACTIVE)) {
841                                 dbdma_reset(&info->rx->dma);
842                                 rxdma_start(info, info->rx_ubuf);
843                                 info->rx_cbuf = info->rx_ubuf;
844                         }
845                 }
846                 info->rx_done_bytes = 0;
847                 if (++info->rx_ubuf == info->rx_nbuf)
848                         info->rx_ubuf = 0;
849                 if (info->rx_fbuf == info->rx_ubuf)
850                         info->rx_fbuf = RX_NO_FBUF;
851                 goto more;
852         }
853 out:
854         spin_unlock_irqrestore(&info->rx_dma_lock, flags);
855         if (do_queue)
856                 tty_flip_buffer_push(tty);
857 }
858
859 static void poll_rxdma(unsigned long private_)
860 {
861         struct mac_serial       *info = (struct mac_serial *) private_;
862         unsigned long flags;
863
864         rxdma_to_tty(info);
865         spin_lock_irqsave(&info->rx_dma_lock, flags);
866         mod_timer(&info->poll_dma_timer, RX_DMA_TIMER);
867         spin_unlock_irqrestore(&info->rx_dma_lock, flags);
868 }
869
870 static void dma_init(struct mac_serial * info)
871 {
872         int i, size;
873         volatile struct dbdma_cmd *cd;
874         unsigned char *p;
875
876         info->rx_nbuf = 8;
877
878         /* various mem set up */
879         size = sizeof(struct dbdma_cmd) * (3 * info->rx_nbuf + 2)
880                 + (RX_BUF_SIZE * 2 + sizeof(*info->rx_cmds)
881                    + sizeof(*info->rx_char_buf) + sizeof(*info->rx_flag_buf))
882                 * info->rx_nbuf;
883         info->dma_priv = kmalloc(size, GFP_KERNEL | GFP_DMA);
884         if (info->dma_priv == NULL)
885                 return;
886         memset(info->dma_priv, 0, size);
887
888         info->rx_cmds = (volatile struct dbdma_cmd **)info->dma_priv;
889         info->rx_char_buf = (unsigned char **) (info->rx_cmds + info->rx_nbuf);
890         info->rx_flag_buf = info->rx_char_buf + info->rx_nbuf;
891         p = (unsigned char *) (info->rx_flag_buf + info->rx_nbuf);
892         for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
893                 info->rx_char_buf[i] = p;
894         for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
895                 info->rx_flag_buf[i] = p;
896
897         /* a bit of DMA programming */
898         cd = info->rx_cmds[0] = (volatile struct dbdma_cmd *) DBDMA_ALIGN(p);
899         st_le16(&cd->command, DBDMA_NOP);
900         cd++;
901         st_le16(&cd->req_count, RX_BUF_SIZE);
902         st_le16(&cd->command, INPUT_MORE);
903         st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[0]));
904         cd++;
905         st_le16(&cd->req_count, 4);
906         st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
907         st_le32(&cd->phy_addr, virt_to_bus(cd-2));
908         st_le32(&cd->cmd_dep, DBDMA_STOP);
909         for (i = 1; i < info->rx_nbuf; i++) {
910                 info->rx_cmds[i] = ++cd;
911                 st_le16(&cd->command, DBDMA_NOP);
912                 cd++;
913                 st_le16(&cd->req_count, RX_BUF_SIZE);
914                 st_le16(&cd->command, INPUT_MORE);
915                 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[i]));
916                 cd++;
917                 st_le16(&cd->req_count, 4);
918                 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
919                 st_le32(&cd->phy_addr, virt_to_bus(cd-2));
920                 st_le32(&cd->cmd_dep, DBDMA_STOP);
921         }
922         cd++;
923         st_le16(&cd->command, DBDMA_NOP | BR_ALWAYS);
924         st_le32(&cd->cmd_dep, virt_to_bus(info->rx_cmds[0]));
925
926         /* setup DMA to our liking */
927         dbdma_reset(&info->rx->dma);
928         st_le32(&info->rx->dma.intr_sel, 0x10001);
929         st_le32(&info->rx->dma.br_sel, 0x10001);
930         out_le32(&info->rx->dma.wait_sel, 0x10001);
931
932         /* set various flags */
933         info->rx_ubuf = 0;
934         info->rx_cbuf = 0;
935         info->rx_fbuf = info->rx_ubuf + 1;
936         if (info->rx_fbuf == info->rx_nbuf)
937                 info->rx_fbuf = RX_NO_FBUF;
938         info->rx_done_bytes = 0;
939
940         /* setup polling */
941         init_timer(&info->poll_dma_timer);
942         info->poll_dma_timer.function = (void *)&poll_rxdma;
943         info->poll_dma_timer.data = (unsigned long)info;
944
945         info->dma_initted = 1;
946 }
947
948 /*
949  * FixZeroBug....Works around a bug in the SCC receving channel.
950  * Taken from Darwin code, 15 Sept. 2000  -DanM
951  *
952  * The following sequence prevents a problem that is seen with O'Hare ASICs
953  * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
954  * at the input to the receiver becomes 'stuck' and locks up the receiver.
955  * This problem can occur as a result of a zero bit at the receiver input
956  * coincident with any of the following events:
957  *
958  *      The SCC is initialized (hardware or software).
959  *      A framing error is detected.
960  *      The clocking option changes from synchronous or X1 asynchronous
961  *              clocking to X16, X32, or X64 asynchronous clocking.
962  *      The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
963  *
964  * This workaround attempts to recover from the lockup condition by placing
965  * the SCC in synchronous loopback mode with a fast clock before programming
966  * any of the asynchronous modes.
967  */
968 static void fix_zero_bug_scc(struct mac_serial * info)
969 {
970         write_zsreg(info->zs_channel, 9,
971                     (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
972         udelay(10);
973         write_zsreg(info->zs_channel, 9,
974                     ((info->zs_channel == info->zs_chan_a? CHRA: CHRB) | NV));
975
976         write_zsreg(info->zs_channel, 4, (X1CLK | EXTSYNC));
977
978         /* I think this is wrong....but, I just copying code....
979         */
980         write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
981
982         write_zsreg(info->zs_channel, 5, (8 & ~TxENAB));
983         write_zsreg(info->zs_channel, 9, NV);   /* Didn't we already do this? */
984         write_zsreg(info->zs_channel, 11, (RCBR | TCBR));
985         write_zsreg(info->zs_channel, 12, 0);
986         write_zsreg(info->zs_channel, 13, 0);
987         write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR));
988         write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR | BRENABL));
989         write_zsreg(info->zs_channel, 3, (8 | RxENABLE));
990         write_zsreg(info->zs_channel, 0, RES_EXT_INT);
991         write_zsreg(info->zs_channel, 0, RES_EXT_INT);  /* to kill some time */
992
993         /* The channel should be OK now, but it is probably receiving
994          * loopback garbage.
995          * Switch to asynchronous mode, disable the receiver,
996          * and discard everything in the receive buffer.
997          */
998         write_zsreg(info->zs_channel, 9, NV);
999         write_zsreg(info->zs_channel, 4, PAR_ENA);
1000         write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
1001
1002         while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV) {
1003                 (void)read_zsreg(info->zs_channel, 8);
1004                 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
1005                 write_zsreg(info->zs_channel, 0, ERR_RES);
1006         }
1007 }
1008
1009 static int setup_scc(struct mac_serial * info)
1010 {
1011         unsigned long flags;
1012
1013         OPNDBG("setting up ttyS%d SCC...\n", info->line);
1014
1015         spin_lock_irqsave(&info->lock, flags);
1016
1017         /* Nice buggy HW ... */
1018         fix_zero_bug_scc(info);
1019
1020         /*
1021          * Reset the chip.
1022          */
1023         write_zsreg(info->zs_channel, 9,
1024                     (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
1025         udelay(10);
1026         write_zsreg(info->zs_channel, 9, 0);
1027
1028         /*
1029          * Clear the receive FIFO.
1030          */
1031         ZS_CLEARFIFO(info->zs_channel);
1032         info->xmit_fifo_size = 1;
1033
1034         /*
1035          * Reset DMAs
1036          */
1037         if (info->has_dma)
1038                 dma_init(info);
1039
1040         /*
1041          * Clear the interrupt registers.
1042          */
1043         write_zsreg(info->zs_channel, 0, ERR_RES);
1044         write_zsreg(info->zs_channel, 0, RES_H_IUS);
1045
1046         /*
1047          * Turn on RTS and DTR.
1048          */
1049         if (!info->is_irda)
1050                 zs_rtsdtr(info, 1);
1051
1052         /*
1053          * Finally, enable sequencing and interrupts
1054          */
1055         if (!info->dma_initted) {
1056                 /* interrupt on ext/status changes, all received chars,
1057                    transmit ready */
1058                 info->curregs[1] = (info->curregs[1] & ~0x18)
1059                                 | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
1060         } else {
1061                 /* interrupt on ext/status changes, W/Req pin is
1062                    receive DMA request */
1063                 info->curregs[1] = (info->curregs[1] & ~(0x18 | TxINT_ENAB))
1064                                 | (EXT_INT_ENAB | WT_RDY_RT | WT_FN_RDYFN);
1065                 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1066                 /* enable W/Req pin */
1067                 info->curregs[1] |= WT_RDY_ENAB;
1068                 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1069                 /* enable interrupts on transmit ready and receive errors */
1070                 info->curregs[1] |= INT_ERR_Rx | TxINT_ENAB;
1071         }
1072         info->pendregs[1] = info->curregs[1];
1073         info->curregs[3] |= (RxENABLE | Rx8);
1074         info->pendregs[3] = info->curregs[3];
1075         info->curregs[5] |= (TxENAB | Tx8);
1076         info->pendregs[5] = info->curregs[5];
1077         info->curregs[9] |= (NV | MIE);
1078         info->pendregs[9] = info->curregs[9];
1079         write_zsreg(info->zs_channel, 3, info->curregs[3]);
1080         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1081         write_zsreg(info->zs_channel, 9, info->curregs[9]);
1082
1083         if (info->tty)
1084                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1085         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1086
1087         spin_unlock_irqrestore(&info->lock, flags);
1088
1089         /*
1090          * Set the speed of the serial port
1091          */
1092         change_speed(info, 0);
1093
1094         /* Save the current value of RR0 */
1095         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1096
1097         if (info->dma_initted) {
1098                 spin_lock_irqsave(&info->rx_dma_lock, flags);
1099                 rxdma_start(info, 0);
1100                 info->poll_dma_timer.expires = RX_DMA_TIMER;
1101                 add_timer(&info->poll_dma_timer);
1102                 spin_unlock_irqrestore(&info->rx_dma_lock, flags);
1103         }
1104
1105         return 0;
1106 }
1107
1108 /*
1109  * This routine will shutdown a serial port; interrupts are disabled, and
1110  * DTR is dropped if the hangup on close termio flag is on.
1111  */
1112 static void shutdown(struct mac_serial * info)
1113 {
1114         OPNDBG("Shutting down serial port %d (irq %d)....\n", info->line,
1115                info->irq);
1116
1117         if (!(info->flags & ZILOG_INITIALIZED)) {
1118                 OPNDBG("(already shutdown)\n");
1119                 return;
1120         }
1121
1122         if (info->has_dma) {
1123                 del_timer(&info->poll_dma_timer);
1124                 dbdma_reset(info->tx_dma);
1125                 dbdma_reset(&info->rx->dma);
1126                 disable_irq(info->tx_dma_irq);
1127                 disable_irq(info->rx_dma_irq);
1128         }
1129         disable_irq(info->irq);
1130
1131         info->pendregs[1] = info->curregs[1] = 0;
1132         write_zsreg(info->zs_channel, 1, 0);    /* no interrupts */
1133
1134         info->curregs[3] &= ~RxENABLE;
1135         info->pendregs[3] = info->curregs[3];
1136         write_zsreg(info->zs_channel, 3, info->curregs[3]);
1137
1138         info->curregs[5] &= ~TxENAB;
1139         if (!info->tty || C_HUPCL(info->tty))
1140                 info->curregs[5] &= ~DTR;
1141         info->pendregs[5] = info->curregs[5];
1142         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1143
1144         if (info->tty)
1145                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1146
1147         set_scc_power(info, 0);
1148
1149         if (info->xmit_buf) {
1150                 free_page((unsigned long) info->xmit_buf);
1151                 info->xmit_buf = 0;
1152         }
1153
1154         if (info->has_dma && info->dma_priv) {
1155                 kfree(info->dma_priv);
1156                 info->dma_priv = NULL;
1157                 info->dma_initted = 0;
1158         }
1159
1160         memset(info->curregs, 0, sizeof(info->curregs));
1161         memset(info->pendregs, 0, sizeof(info->pendregs));
1162
1163         info->flags &= ~ZILOG_INITIALIZED;
1164 }
1165
1166 /*
1167  * Turn power on or off to the SCC and associated stuff
1168  * (port drivers, modem, IR port, etc.)
1169  * Returns the number of milliseconds we should wait before
1170  * trying to use the port.
1171  */
1172 static int set_scc_power(struct mac_serial * info, int state)
1173 {
1174         int delay = 0;
1175
1176         if (state) {
1177                 PWRDBG("ttyS%d: powering up hardware\n", info->line);
1178                 pmac_call_feature(
1179                         PMAC_FTR_SCC_ENABLE,
1180                         info->dev_node, info->port_type, 1);
1181                 if (info->is_internal_modem) {
1182                         pmac_call_feature(
1183                                 PMAC_FTR_MODEM_ENABLE,
1184                                 info->dev_node, 0, 1);
1185                         delay = 2500;   /* wait for 2.5s before using */
1186                 } else if (info->is_irda)
1187                         mdelay(50);     /* Do better here once the problems
1188                                          * with blocking have been ironed out
1189                                          */
1190         } else {
1191                 /* TODO: Make that depend on a timer, don't power down
1192                  * immediately
1193                  */
1194                 PWRDBG("ttyS%d: shutting down hardware\n", info->line);
1195                 if (info->is_internal_modem) {
1196                         PWRDBG("ttyS%d: shutting down modem\n", info->line);
1197                         pmac_call_feature(
1198                                 PMAC_FTR_MODEM_ENABLE,
1199                                 info->dev_node, 0, 0);
1200                 }
1201                 pmac_call_feature(
1202                         PMAC_FTR_SCC_ENABLE,
1203                         info->dev_node, info->port_type, 0);
1204         }
1205         return delay;
1206 }
1207
1208 static void irda_rts_pulses(struct mac_serial *info, int w)
1209 {
1210         udelay(w);
1211         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1212         udelay(2);
1213         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1214         udelay(8);
1215         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1216         udelay(4);
1217         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1218 }
1219
1220 /*
1221  * Set the irda codec on the imac to the specified baud rate.
1222  */
1223 static void irda_setup(struct mac_serial *info)
1224 {
1225         int code, speed, t;
1226
1227         speed = info->tty->termios->c_cflag & CBAUD;
1228         if (speed < B2400 || speed > B115200)
1229                 return;
1230         code = 0x4d + B115200 - speed;
1231
1232         /* disable serial interrupts and receive DMA */
1233         write_zsreg(info->zs_channel, 1, info->curregs[1] & ~0x9f);
1234
1235         /* wait for transmitter to drain */
1236         t = 10000;
1237         while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0
1238                || (read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1239                 if (--t <= 0) {
1240                         printk(KERN_ERR "transmitter didn't drain\n");
1241                         return;
1242                 }
1243                 udelay(10);
1244         }
1245         udelay(100);
1246
1247         /* set to 8 bits, no parity, 19200 baud, RTS on, DTR off */
1248         write_zsreg(info->zs_channel, 4, X16CLK | SB1);
1249         write_zsreg(info->zs_channel, 11, TCBR | RCBR);
1250         t = BPS_TO_BRG(19200, ZS_CLOCK/16);
1251         write_zsreg(info->zs_channel, 12, t);
1252         write_zsreg(info->zs_channel, 13, t >> 8);
1253         write_zsreg(info->zs_channel, 14, BRENABL);
1254         write_zsreg(info->zs_channel, 3, Rx8 | RxENABLE);
1255         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1256
1257         /* set TxD low for ~104us and pulse RTS */
1258         udelay(1000);
1259         write_zsdata(info->zs_channel, 0xfe);
1260         irda_rts_pulses(info, 150);
1261         irda_rts_pulses(info, 180);
1262         irda_rts_pulses(info, 50);
1263         udelay(100);
1264
1265         /* assert DTR, wait 30ms, talk to the chip */
1266         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS | DTR);
1267         mdelay(30);
1268         while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV)
1269                 read_zsdata(info->zs_channel);
1270
1271         write_zsdata(info->zs_channel, 1);
1272         t = 1000;
1273         while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1274                 if (--t <= 0) {
1275                         printk(KERN_ERR "irda_setup timed out on 1st byte\n");
1276                         goto out;
1277                 }
1278                 udelay(10);
1279         }
1280         t = read_zsdata(info->zs_channel);
1281         if (t != 4)
1282                 printk(KERN_ERR "irda_setup 1st byte = %x\n", t);
1283
1284         write_zsdata(info->zs_channel, code);
1285         t = 1000;
1286         while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1287                 if (--t <= 0) {
1288                         printk(KERN_ERR "irda_setup timed out on 2nd byte\n");
1289                         goto out;
1290                 }
1291                 udelay(10);
1292         }
1293         t = read_zsdata(info->zs_channel);
1294         if (t != code)
1295                 printk(KERN_ERR "irda_setup 2nd byte = %x (%x)\n", t, code);
1296
1297         /* Drop DTR again and do some more RTS pulses */
1298  out:
1299         udelay(100);
1300         write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1301         irda_rts_pulses(info, 80);
1302
1303         /* We should be right to go now.  We assume that load_zsregs
1304            will get called soon to load up the correct baud rate etc. */
1305         info->curregs[5] = (info->curregs[5] | RTS) & ~DTR;
1306         info->pendregs[5] = info->curregs[5];
1307 }
1308
1309 /*
1310  * This routine is called to set the UART divisor registers to match
1311  * the specified baud rate for a serial port.
1312  */
1313 static void change_speed(struct mac_serial *info, struct termios *old_termios)
1314 {
1315         unsigned cflag;
1316         int     bits;
1317         int     brg, baud;
1318         unsigned long flags;
1319
1320         if (!info->tty || !info->tty->termios)
1321                 return;
1322
1323         cflag = info->tty->termios->c_cflag;
1324         baud = tty_get_baud_rate(info->tty);
1325         if (baud == 0) {
1326                 if (old_termios) {
1327                         info->tty->termios->c_cflag &= ~CBAUD;
1328                         info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1329                         cflag = info->tty->termios->c_cflag;
1330                         baud = tty_get_baud_rate(info->tty);
1331                 }
1332                 else
1333                         baud = info->zs_baud;
1334         }
1335         if (baud > 230400)
1336                 baud = 230400;
1337         else if (baud == 0)
1338                 baud = 38400;
1339
1340         spin_lock_irqsave(&info->lock, flags);
1341         info->zs_baud = baud;
1342         info->clk_divisor = 16;
1343
1344         BAUDBG(KERN_DEBUG "set speed to %d bds, ", baud);
1345
1346         switch (baud) {
1347         case ZS_CLOCK/16:       /* 230400 */
1348                 info->curregs[4] = X16CLK;
1349                 info->curregs[11] = 0;
1350                 break;
1351         case ZS_CLOCK/32:       /* 115200 */
1352                 info->curregs[4] = X32CLK;
1353                 info->curregs[11] = 0;
1354                 break;
1355         default:
1356                 info->curregs[4] = X16CLK;
1357                 info->curregs[11] = TCBR | RCBR;
1358                 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
1359                 info->curregs[12] = (brg & 255);
1360                 info->curregs[13] = ((brg >> 8) & 255);
1361                 info->curregs[14] = BRENABL;
1362         }
1363
1364         /* byte size and parity */
1365         info->curregs[3] &= ~RxNBITS_MASK;
1366         info->curregs[5] &= ~TxNBITS_MASK;
1367         switch (cflag & CSIZE) {
1368         case CS5:
1369                 info->curregs[3] |= Rx5;
1370                 info->curregs[5] |= Tx5;
1371                 BAUDBG("5 bits, ");
1372                 bits = 7;
1373                 break;
1374         case CS6:
1375                 info->curregs[3] |= Rx6;
1376                 info->curregs[5] |= Tx6;
1377                 BAUDBG("6 bits, ");
1378                 bits = 8;
1379                 break;
1380         case CS7:
1381                 info->curregs[3] |= Rx7;
1382                 info->curregs[5] |= Tx7;
1383                 BAUDBG("7 bits, ");
1384                 bits = 9;
1385                 break;
1386         case CS8:
1387         default: /* defaults to 8 bits */
1388                 info->curregs[3] |= Rx8;
1389                 info->curregs[5] |= Tx8;
1390                 BAUDBG("8 bits, ");
1391                 bits = 10;
1392                 break;
1393         }
1394         info->pendregs[3] = info->curregs[3];
1395         info->pendregs[5] = info->curregs[5];
1396
1397         info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
1398         if (cflag & CSTOPB) {
1399                 info->curregs[4] |= SB2;
1400                 bits++;
1401                 BAUDBG("2 stop, ");
1402         } else {
1403                 info->curregs[4] |= SB1;
1404                 BAUDBG("1 stop, ");
1405         }
1406         if (cflag & PARENB) {
1407                 bits++;
1408                 info->curregs[4] |= PAR_ENA;
1409                 BAUDBG("parity, ");
1410         }
1411         if (!(cflag & PARODD)) {
1412                 info->curregs[4] |= PAR_EVEN;
1413         }
1414         info->pendregs[4] = info->curregs[4];
1415
1416         if (!(cflag & CLOCAL)) {
1417                 if (!(info->curregs[15] & DCDIE))
1418                         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1419                 info->curregs[15] |= DCDIE;
1420         } else
1421                 info->curregs[15] &= ~DCDIE;
1422         if (cflag & CRTSCTS) {
1423                 info->curregs[15] |= CTSIE;
1424                 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
1425                         info->tx_stopped = 1;
1426         } else {
1427                 info->curregs[15] &= ~CTSIE;
1428                 info->tx_stopped = 0;
1429         }
1430         info->pendregs[15] = info->curregs[15];
1431
1432         /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
1433            This code would love a larger HZ and a >1 fifo size, but this is not
1434            a priority. The resulting value must be >HZ/2
1435          */
1436         info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
1437         info->timeout += HZ/50+1;       /* Add .02 seconds of slop */
1438
1439         BAUDBG("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ,
1440                (int)info->baud_base);
1441
1442         /* set the irda codec to the right rate */
1443         if (info->is_irda)
1444                 irda_setup(info);
1445
1446         /* Load up the new values */
1447         load_zsregs(info->zs_channel, info->curregs);
1448
1449         spin_unlock_irqrestore(&info->lock, flags);
1450 }
1451
1452 static void rs_flush_chars(struct tty_struct *tty)
1453 {
1454         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1455         unsigned long flags;
1456
1457         if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1458                 return;
1459
1460         spin_lock_irqsave(&info->lock, flags);
1461         if (!(info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
1462               !info->xmit_buf))
1463                 /* Enable transmitter */
1464                 transmit_chars(info);
1465         spin_unlock_irqrestore(&info->lock, flags);
1466 }
1467
1468 static int rs_write(struct tty_struct * tty, int from_user,
1469                     const unsigned char *buf, int count)
1470 {
1471         int     c, ret = 0;
1472         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1473         unsigned long flags;
1474
1475         if (serial_paranoia_check(info, tty->name, "rs_write"))
1476                 return 0;
1477
1478         if (!tty || !info->xmit_buf || !tmp_buf)
1479                 return 0;
1480
1481         if (from_user) {
1482                 down(&tmp_buf_sem);
1483                 while (1) {
1484                         c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1485                                                   SERIAL_XMIT_SIZE - info->xmit_head));
1486                         if (c <= 0)
1487                                 break;
1488
1489                         c -= copy_from_user(tmp_buf, buf, c);
1490                         if (!c) {
1491                                 if (!ret)
1492                                         ret = -EFAULT;
1493                                 break;
1494                         }
1495                         spin_lock_irqsave(&info->lock, flags);
1496                         c = min_t(int, c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1497                                               SERIAL_XMIT_SIZE - info->xmit_head));
1498                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1499                         info->xmit_head = ((info->xmit_head + c) &
1500                                            (SERIAL_XMIT_SIZE-1));
1501                         info->xmit_cnt += c;
1502                         spin_unlock_irqrestore(&info->lock, flags);
1503                         buf += c;
1504                         count -= c;
1505                         ret += c;
1506                 }
1507                 up(&tmp_buf_sem);
1508         } else {
1509                 while (1) {
1510                         spin_lock_irqsave(&info->lock, flags);
1511                         c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1512                                                   SERIAL_XMIT_SIZE - info->xmit_head));
1513                         if (c <= 0) {
1514                                 spin_unlock_irqrestore(&info->lock, flags);
1515                                 break;
1516                         }
1517                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1518                         info->xmit_head = ((info->xmit_head + c) &
1519                                            (SERIAL_XMIT_SIZE-1));
1520                         info->xmit_cnt += c;
1521                         spin_unlock_irqrestore(&info->lock, flags);
1522                         buf += c;
1523                         count -= c;
1524                         ret += c;
1525                 }
1526         }
1527         spin_lock_irqsave(&info->lock, flags);
1528         if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1529             && !info->tx_active)
1530                 transmit_chars(info);
1531         spin_unlock_irqrestore(&info->lock, flags);
1532         return ret;
1533 }
1534
1535 static int rs_write_room(struct tty_struct *tty)
1536 {
1537         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1538         int     ret;
1539
1540         if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1541                 return 0;
1542         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1543         if (ret < 0)
1544                 ret = 0;
1545         return ret;
1546 }
1547
1548 static int rs_chars_in_buffer(struct tty_struct *tty)
1549 {
1550         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1551
1552         if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1553                 return 0;
1554         return info->xmit_cnt;
1555 }
1556
1557 static void rs_flush_buffer(struct tty_struct *tty)
1558 {
1559         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1560         unsigned long flags;
1561
1562         if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1563                 return;
1564         spin_lock_irqsave(&info->lock, flags);
1565         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1566         spin_unlock_irqrestore(&info->lock, flags);
1567         wake_up_interruptible(&tty->write_wait);
1568         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1569             tty->ldisc.write_wakeup)
1570                 (tty->ldisc.write_wakeup)(tty);
1571 }
1572
1573 /*
1574  * ------------------------------------------------------------
1575  * rs_throttle()
1576  * 
1577  * This routine is called by the upper-layer tty layer to signal that
1578  * incoming characters should be throttled.
1579  * ------------------------------------------------------------
1580  */
1581 static void rs_throttle(struct tty_struct * tty)
1582 {
1583         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1584         unsigned long flags;
1585 #ifdef SERIAL_DEBUG_THROTTLE
1586         printk(KERN_DEBUG "throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1587 #endif
1588
1589         if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1590                 return;
1591
1592         if (I_IXOFF(tty)) {
1593                 spin_lock_irqsave(&info->lock, flags);
1594                 info->x_char = STOP_CHAR(tty);
1595                 if (!info->tx_active)
1596                         transmit_chars(info);
1597                 spin_unlock_irqrestore(&info->lock, flags);
1598         }
1599
1600         if (C_CRTSCTS(tty)) {
1601                 /*
1602                  * Here we want to turn off the RTS line.  On Macintoshes,
1603                  * the external serial ports using a DIN-8 or DIN-9
1604                  * connector only have the DTR line (which is usually
1605                  * wired to both RTS and DTR on an external modem in
1606                  * the cable).  RTS doesn't go out to the serial port
1607                  * socket, it acts as an output enable for the transmit
1608                  * data line.  So in this case we don't drop RTS.
1609                  *
1610                  * Macs with internal modems generally do have both RTS
1611                  * and DTR wired to the modem, so in that case we do
1612                  * drop RTS.
1613                  */
1614                 if (info->is_internal_modem) {
1615                         spin_lock_irqsave(&info->lock, flags);
1616                         info->curregs[5] &= ~RTS;
1617                         info->pendregs[5] &= ~RTS;
1618                         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1619                         spin_unlock_irqrestore(&info->lock, flags);
1620                 }
1621         }
1622         
1623 #ifdef CDTRCTS
1624         if (tty->termios->c_cflag & CDTRCTS) {
1625                 spin_lock_irqsave(&info->lock, flags);
1626                 info->curregs[5] &= ~DTR;
1627                 info->pendregs[5] &= ~DTR;
1628                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1629                 spin_unlock_irqrestore(&info->lock, flags);
1630         }
1631 #endif /* CDTRCTS */
1632 }
1633
1634 static void rs_unthrottle(struct tty_struct * tty)
1635 {
1636         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1637         unsigned long flags;
1638 #ifdef SERIAL_DEBUG_THROTTLE
1639         printk(KERN_DEBUG "unthrottle %s: %d....\n",
1640                         tty->ldisc.chars_in_buffer(tty));
1641 #endif
1642
1643         if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1644                 return;
1645
1646         if (I_IXOFF(tty)) {
1647                 spin_lock_irqsave(&info->lock, flags);
1648                 if (info->x_char)
1649                         info->x_char = 0;
1650                 else {
1651                         info->x_char = START_CHAR(tty);
1652                         if (!info->tx_active)
1653                                 transmit_chars(info);
1654                 }
1655                 spin_unlock_irqrestore(&info->lock, flags);
1656         }
1657
1658         if (C_CRTSCTS(tty) && info->is_internal_modem) {
1659                 /* Assert RTS line */
1660                 spin_lock_irqsave(&info->lock, flags);
1661                 info->curregs[5] |= RTS;
1662                 info->pendregs[5] |= RTS;
1663                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1664                 spin_unlock_irqrestore(&info->lock, flags);
1665         }
1666
1667 #ifdef CDTRCTS
1668         if (tty->termios->c_cflag & CDTRCTS) {
1669                 /* Assert DTR line */
1670                 spin_lock_irqsave(&info->lock, flags);
1671                 info->curregs[5] |= DTR;
1672                 info->pendregs[5] |= DTR;
1673                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1674                 spin_unlock_irqrestore(&info->lock, flags);
1675         }
1676 #endif
1677 }
1678
1679 /*
1680  * ------------------------------------------------------------
1681  * rs_ioctl() and friends
1682  * ------------------------------------------------------------
1683  */
1684
1685 static int get_serial_info(struct mac_serial * info,
1686                            struct serial_struct __user * retinfo)
1687 {
1688         struct serial_struct tmp;
1689   
1690         if (!retinfo)
1691                 return -EFAULT;
1692         memset(&tmp, 0, sizeof(tmp));
1693         tmp.type = info->type;
1694         tmp.line = info->line;
1695         tmp.port = info->port;
1696         tmp.irq = info->irq;
1697         tmp.flags = info->flags;
1698         tmp.baud_base = info->baud_base;
1699         tmp.close_delay = info->close_delay;
1700         tmp.closing_wait = info->closing_wait;
1701         tmp.custom_divisor = info->custom_divisor;
1702         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1703                 return -EFAULT;
1704         return 0;
1705 }
1706
1707 static int set_serial_info(struct mac_serial * info,
1708                            struct serial_struct __user * new_info)
1709 {
1710         struct serial_struct new_serial;
1711         struct mac_serial old_info;
1712         int                     retval = 0;
1713
1714         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1715                 return -EFAULT;
1716         old_info = *info;
1717
1718         if (!capable(CAP_SYS_ADMIN)) {
1719                 if ((new_serial.baud_base != info->baud_base) ||
1720                     (new_serial.type != info->type) ||
1721                     (new_serial.close_delay != info->close_delay) ||
1722                     ((new_serial.flags & ~ZILOG_USR_MASK) !=
1723                      (info->flags & ~ZILOG_USR_MASK)))
1724                         return -EPERM;
1725                 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1726                                (new_serial.flags & ZILOG_USR_MASK));
1727                 info->custom_divisor = new_serial.custom_divisor;
1728                 goto check_and_exit;
1729         }
1730
1731         if (info->count > 1)
1732                 return -EBUSY;
1733
1734         /*
1735          * OK, past this point, all the error checking has been done.
1736          * At this point, we start making changes.....
1737          */
1738
1739         info->baud_base = new_serial.baud_base;
1740         info->flags = ((info->flags & ~ZILOG_FLAGS) |
1741                         (new_serial.flags & ZILOG_FLAGS));
1742         info->type = new_serial.type;
1743         info->close_delay = new_serial.close_delay;
1744         info->closing_wait = new_serial.closing_wait;
1745
1746 check_and_exit:
1747         if (info->flags & ZILOG_INITIALIZED)
1748                 retval = setup_scc(info);
1749         return retval;
1750 }
1751
1752 /*
1753  * get_lsr_info - get line status register info
1754  *
1755  * Purpose: Let user call ioctl() to get info when the UART physically
1756  *          is emptied.  On bus types like RS485, the transmitter must
1757  *          release the bus after transmitting. This must be done when
1758  *          the transmit shift register is empty, not be done when the
1759  *          transmit holding register is empty.  This functionality
1760  *          allows an RS485 driver to be written in user space. 
1761  */
1762 static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1763 {
1764         unsigned char status;
1765         unsigned long flags;
1766
1767         spin_lock_irqsave(&info->lock, flags);
1768         status = read_zsreg(info->zs_channel, 0);
1769         spin_unlock_irqrestore(&info->lock, flags);
1770         status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0;
1771         return put_user(status,value);
1772 }
1773
1774 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1775 {
1776         struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1777         unsigned char control, status;
1778         unsigned long flags;
1779
1780 #ifdef CONFIG_KGDB
1781         if (info->kgdb_channel)
1782                 return -ENODEV;
1783 #endif
1784         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1785                 return -ENODEV;
1786
1787         if (tty->flags & (1 << TTY_IO_ERROR))
1788                 return -EIO;
1789
1790         spin_lock_irqsave(&info->lock, flags);
1791         control = info->curregs[5];
1792         status = read_zsreg(info->zs_channel, 0);
1793         spin_unlock_irqrestore(&info->lock, flags);
1794         return    ((control & RTS) ? TIOCM_RTS: 0)
1795                 | ((control & DTR) ? TIOCM_DTR: 0)
1796                 | ((status  & DCD) ? TIOCM_CAR: 0)
1797                 | ((status  & CTS) ? 0: TIOCM_CTS);
1798 }
1799
1800 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1801                        unsigned int set, unsigned int clear)
1802 {
1803         struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1804         unsigned int arg, bits;
1805         unsigned long flags;
1806
1807 #ifdef CONFIG_KGDB
1808         if (info->kgdb_channel)
1809                 return -ENODEV;
1810 #endif
1811         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1812                 return -ENODEV;
1813
1814         if (tty->flags & (1 << TTY_IO_ERROR))
1815                 return -EIO;
1816
1817         spin_lock_irqsave(&info->lock, flags);
1818         if (set & TIOCM_RTS)
1819                 info->curregs[5] |= RTS;
1820         if (set & TIOCM_DTR)
1821                 info->curregs[5] |= DTR;
1822         if (clear & TIOCM_RTS)
1823                 info->curregs[5] &= ~RTS;
1824         if (clear & TIOCM_DTR)
1825                 info->curregs[5] &= ~DTR;
1826
1827         info->pendregs[5] = info->curregs[5];
1828         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1829         spin_unlock_irqrestore(&info->lock, flags);
1830         return 0;
1831 }
1832
1833 /*
1834  * rs_break - turn transmit break condition on/off
1835  */
1836 static void rs_break(struct tty_struct *tty, int break_state)
1837 {
1838         struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1839         unsigned long flags;
1840
1841         if (serial_paranoia_check(info, tty->name, "rs_break"))
1842                 return;
1843
1844         spin_lock_irqsave(&info->lock, flags);
1845         if (break_state == -1)
1846                 info->curregs[5] |= SND_BRK;
1847         else
1848                 info->curregs[5] &= ~SND_BRK;
1849         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1850         spin_unlock_irqrestore(&info->lock, flags);
1851 }
1852
1853 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1854                     unsigned int cmd, unsigned long arg)
1855 {
1856         struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1857
1858 #ifdef CONFIG_KGDB
1859         if (info->kgdb_channel)
1860                 return -ENODEV;
1861 #endif
1862         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1863                 return -ENODEV;
1864
1865         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1866             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) {
1867                 if (tty->flags & (1 << TTY_IO_ERROR))
1868                     return -EIO;
1869         }
1870
1871         switch (cmd) {
1872                 case TIOCGSERIAL:
1873                         return get_serial_info(info,
1874                                         (struct serial_struct __user *) arg);
1875                 case TIOCSSERIAL:
1876                         return set_serial_info(info,
1877                                         (struct serial_struct __user *) arg);
1878                 case TIOCSERGETLSR: /* Get line status register */
1879                         return get_lsr_info(info, (unsigned int *) arg);
1880
1881                 case TIOCSERGSTRUCT:
1882                         if (copy_to_user((struct mac_serial __user *) arg,
1883                                          info, sizeof(struct mac_serial)))
1884                                 return -EFAULT;
1885                         return 0;
1886
1887                 default:
1888                         return -ENOIOCTLCMD;
1889                 }
1890         return 0;
1891 }
1892
1893 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1894 {
1895         struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1896         int was_stopped;
1897
1898         if (tty->termios->c_cflag == old_termios->c_cflag)
1899                 return;
1900         was_stopped = info->tx_stopped;
1901
1902         change_speed(info, old_termios);
1903
1904         if (was_stopped && !info->tx_stopped) {
1905                 tty->hw_stopped = 0;
1906                 rs_start(tty);
1907         }
1908 }
1909
1910 /*
1911  * ------------------------------------------------------------
1912  * rs_close()
1913  * 
1914  * This routine is called when the serial port gets closed.
1915  * Wait for the last remaining data to be sent.
1916  * ------------------------------------------------------------
1917  */
1918 static void rs_close(struct tty_struct *tty, struct file * filp)
1919 {
1920         struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1921         unsigned long flags;
1922
1923         if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1924                 return;
1925
1926         spin_lock_irqsave(&info->lock, flags);
1927
1928         if (tty_hung_up_p(filp)) {
1929                 spin_unlock_irqrestore(&info->lock, flags);
1930                 return;
1931         }
1932
1933         OPNDBG("rs_close ttyS%d, count = %d\n", info->line, info->count);
1934         if ((tty->count == 1) && (info->count != 1)) {
1935                 /*
1936                  * Uh, oh.  tty->count is 1, which means that the tty
1937                  * structure will be freed.  Info->count should always
1938                  * be one in these conditions.  If it's greater than
1939                  * one, we've got real problems, since it means the
1940                  * serial port won't be shutdown.
1941                  */
1942                 printk(KERN_ERR "rs_close: bad serial port count; tty->count "
1943                                 "is 1, info->count is %d\n", info->count);
1944                 info->count = 1;
1945         }
1946         if (--info->count < 0) {
1947                 printk(KERN_ERR "rs_close: bad serial port count for "
1948                                 "ttyS%d: %d\n", info->line, info->count);
1949                 info->count = 0;
1950         }
1951         if (info->count) {
1952                 spin_unlock_irqrestore(&info->lock, flags);
1953                 return;
1954         }
1955         info->flags |= ZILOG_CLOSING;
1956         /*
1957          * Now we wait for the transmit buffer to clear; and we notify 
1958          * the line discipline to only process XON/XOFF characters.
1959          */
1960         OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1961         tty->closing = 1;
1962         if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) {
1963                 spin_unlock_irqrestore(&info->lock, flags);
1964                 tty_wait_until_sent(tty, info->closing_wait);
1965                 spin_lock_irqsave(&info->lock, flags);
1966         }
1967
1968         /*
1969          * At this point we stop accepting input.  To do this, we
1970          * disable the receiver and receive interrupts.
1971          */
1972         info->curregs[3] &= ~RxENABLE;
1973         info->pendregs[3] = info->curregs[3];
1974         write_zsreg(info->zs_channel, 3, info->curregs[3]);
1975         info->curregs[1] &= ~(0x18);    /* disable any rx ints */
1976         info->pendregs[1] = info->curregs[1];
1977         write_zsreg(info->zs_channel, 1, info->curregs[1]);
1978         ZS_CLEARFIFO(info->zs_channel);
1979         if (info->flags & ZILOG_INITIALIZED) {
1980                 /*
1981                  * Before we drop DTR, make sure the SCC transmitter
1982                  * has completely drained.
1983                  */
1984                 OPNDBG("waiting end of Rx...\n");
1985                 spin_unlock_irqrestore(&info->lock, flags);
1986                 rs_wait_until_sent(tty, info->timeout);
1987                 spin_lock_irqsave(&info->lock, flags);
1988         }
1989
1990         shutdown(info);
1991         /* restore flags now since shutdown() will have disabled this port's
1992            specific irqs */
1993         spin_unlock_irqrestore(&info->lock, flags);
1994
1995         if (tty->driver->flush_buffer)
1996                 tty->driver->flush_buffer(tty);
1997         if (tty->ldisc.flush_buffer)
1998                 tty->ldisc.flush_buffer(tty);
1999         tty->closing = 0;
2000         info->event = 0;
2001         info->tty = 0;
2002
2003         if (info->blocked_open) {
2004                 if (info->close_delay) {
2005                         current->state = TASK_INTERRUPTIBLE;
2006                         schedule_timeout(info->close_delay);
2007                 }
2008                 wake_up_interruptible(&info->open_wait);
2009         }
2010         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
2011         wake_up_interruptible(&info->close_wait);
2012 }
2013
2014 /*
2015  * rs_wait_until_sent() --- wait until the transmitter is empty
2016  */
2017 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2018 {
2019         struct mac_serial *info = (struct mac_serial *) tty->driver_data;
2020         unsigned long orig_jiffies, char_time;
2021
2022         if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2023                 return;
2024
2025 /*      printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
2026                         timeout, tty->stopped, info->tx_stopped);
2027 */
2028         orig_jiffies = jiffies;
2029         /*
2030          * Set the check interval to be 1/5 of the estimated time to
2031          * send a single character, and make it at least 1.  The check
2032          * interval should also be less than the timeout.
2033          */
2034         if (info->timeout <= HZ/50) {
2035                 printk(KERN_INFO "macserial: invalid info->timeout=%d\n",
2036                                     info->timeout);
2037                 info->timeout = HZ/50+1;
2038         }
2039
2040         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2041         char_time = char_time / 5;
2042         if (char_time > HZ) {
2043                 printk(KERN_WARNING "macserial: char_time %ld >HZ !!!\n",
2044                                     char_time);
2045                 char_time = 1;
2046         } else if (char_time == 0)
2047                 char_time = 1;
2048         if (timeout)
2049                 char_time = min_t(unsigned long, char_time, timeout);
2050         while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
2051                 current->state = TASK_INTERRUPTIBLE;
2052                 schedule_timeout(char_time);
2053                 if (signal_pending(current))
2054                         break;
2055                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2056                         break;
2057         }
2058         current->state = TASK_RUNNING;
2059 }
2060
2061 /*
2062  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2063  */
2064 static void rs_hangup(struct tty_struct *tty)
2065 {
2066         struct mac_serial * info = (struct mac_serial *)tty->driver_data;
2067
2068         if (serial_paranoia_check(info, tty->name, "rs_hangup"))
2069                 return;
2070
2071         rs_flush_buffer(tty);
2072         shutdown(info);
2073         info->event = 0;
2074         info->count = 0;
2075         info->flags &= ~ZILOG_NORMAL_ACTIVE;
2076         info->tty = 0;
2077         wake_up_interruptible(&info->open_wait);
2078 }
2079
2080 /*
2081  * ------------------------------------------------------------
2082  * rs_open() and friends
2083  * ------------------------------------------------------------
2084  */
2085 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2086                            struct mac_serial *info)
2087 {
2088         DECLARE_WAITQUEUE(wait,current);
2089         int             retval;
2090         int             do_clocal = 0;
2091
2092         /*
2093          * If the device is in the middle of being closed, then block
2094          * until it's done, and then try again.
2095          */
2096         if (info->flags & ZILOG_CLOSING) {
2097                 interruptible_sleep_on(&info->close_wait);
2098                 return -EAGAIN;
2099         }
2100
2101         /*
2102          * If non-blocking mode is set, or the port is not enabled,
2103          * then make the check up front and then exit.
2104          */
2105         if ((filp->f_flags & O_NONBLOCK) ||
2106             (tty->flags & (1 << TTY_IO_ERROR))) {
2107                 info->flags |= ZILOG_NORMAL_ACTIVE;
2108                 return 0;
2109         }
2110
2111         if (tty->termios->c_cflag & CLOCAL)
2112                 do_clocal = 1;
2113
2114         /*
2115          * Block waiting for the carrier detect and the line to become
2116          * free (i.e., not in use by the callout).  While we are in
2117          * this loop, info->count is dropped by one, so that
2118          * rs_close() knows when to free things.  We restore it upon
2119          * exit, either normal or abnormal.
2120          */
2121         retval = 0;
2122         add_wait_queue(&info->open_wait, &wait);
2123         OPNDBG("block_til_ready before block: ttyS%d, count = %d\n",
2124                info->line, info->count);
2125         spin_lock_irq(&info->lock);
2126         if (!tty_hung_up_p(filp)) 
2127                 info->count--;
2128         spin_unlock_irq(&info->lock);
2129         info->blocked_open++;
2130         while (1) {
2131                 spin_lock_irq(&info->lock);
2132                 if ((tty->termios->c_cflag & CBAUD) &&
2133                     !info->is_irda)
2134                         zs_rtsdtr(info, 1);
2135                 spin_unlock_irq(&info->lock);
2136                 set_current_state(TASK_INTERRUPTIBLE);
2137                 if (tty_hung_up_p(filp) ||
2138                     !(info->flags & ZILOG_INITIALIZED)) {
2139                         retval = -EAGAIN;
2140                         break;
2141                 }
2142                 if (!(info->flags & ZILOG_CLOSING) &&
2143                     (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
2144                         break;
2145                 if (signal_pending(current)) {
2146                         retval = -ERESTARTSYS;
2147                         break;
2148                 }
2149                 OPNDBG("block_til_ready blocking: ttyS%d, count = %d\n",
2150                        info->line, info->count);
2151                 schedule();
2152         }
2153         current->state = TASK_RUNNING;
2154         remove_wait_queue(&info->open_wait, &wait);
2155         if (!tty_hung_up_p(filp))
2156                 info->count++;
2157         info->blocked_open--;
2158         OPNDBG("block_til_ready after blocking: ttyS%d, count = %d\n",
2159                info->line, info->count);
2160         if (retval)
2161                 return retval;
2162         info->flags |= ZILOG_NORMAL_ACTIVE;
2163         return 0;
2164 }
2165
2166 /*
2167  * This routine is called whenever a serial port is opened.  It
2168  * enables interrupts for a serial port, linking in its ZILOG structure into
2169  * the IRQ chain.   It also performs the serial-specific
2170  * initialization for the tty structure.
2171  */
2172 static int rs_open(struct tty_struct *tty, struct file * filp)
2173 {
2174         struct mac_serial       *info;
2175         int                     retval, line;
2176         unsigned long           page;
2177
2178         line = tty->index;
2179         if ((line < 0) || (line >= zs_channels_found)) {
2180                 return -ENODEV;
2181         }
2182         info = zs_soft + line;
2183
2184 #ifdef CONFIG_KGDB
2185         if (info->kgdb_channel) {
2186                 return -ENODEV;
2187         }
2188 #endif
2189         if (serial_paranoia_check(info, tty->name, "rs_open"))
2190                 return -ENODEV;
2191         OPNDBG("rs_open %s, count = %d, tty=%p\n", tty->name,
2192                info->count, tty);
2193
2194         info->count++;
2195         tty->driver_data = info;
2196         info->tty = tty;
2197
2198         if (!tmp_buf) {
2199                 page = get_zeroed_page(GFP_KERNEL);
2200                 if (!page)
2201                         return -ENOMEM;
2202                 if (tmp_buf)
2203                         free_page(page);
2204                 else
2205                         tmp_buf = (unsigned char *) page;
2206         }
2207
2208         /*
2209          * If the port is the middle of closing, bail out now
2210          */
2211         if (tty_hung_up_p(filp) ||
2212             (info->flags & ZILOG_CLOSING)) {
2213                 if (info->flags & ZILOG_CLOSING)
2214                         interruptible_sleep_on(&info->close_wait);
2215                 return -EAGAIN;
2216         }
2217
2218         /*
2219          * Start up serial port
2220          */
2221
2222         retval = startup(info);
2223         if (retval)
2224                 return retval;
2225
2226         retval = block_til_ready(tty, filp, info);
2227         if (retval) {
2228                 OPNDBG("rs_open returning after block_til_ready with %d\n",
2229                         retval);
2230                 return retval;
2231         }
2232
2233 #ifdef CONFIG_SERIAL_CONSOLE
2234         if (sercons.cflag && sercons.index == line) {
2235                 tty->termios->c_cflag = sercons.cflag;
2236                 sercons.cflag = 0;
2237                 change_speed(info, 0);
2238         }
2239 #endif
2240
2241         OPNDBG("rs_open %s successful...\n", tty->name);
2242         return 0;
2243 }
2244
2245 /* Finally, routines used to initialize the serial driver. */
2246
2247 static void show_serial_version(void)
2248 {
2249         printk(KERN_INFO "PowerMac Z8530 serial driver version " MACSERIAL_VERSION "\n");
2250 }
2251
2252 /*
2253  * Initialize one channel, both the mac_serial and mac_zschannel
2254  * structs.  We use the dev_node field of the mac_serial struct.
2255  */
2256 static int
2257 chan_init(struct mac_serial *zss, struct mac_zschannel *zs_chan,
2258           struct mac_zschannel *zs_chan_a)
2259 {
2260         struct device_node *ch = zss->dev_node;
2261         char *conn;
2262         int len;
2263         struct slot_names_prop {
2264                 int     count;
2265                 char    name[1];
2266         } *slots;
2267
2268         zss->irq = ch->intrs[0].line;
2269         zss->has_dma = 0;
2270 #if !defined(CONFIG_KGDB) && defined(SUPPORT_SERIAL_DMA)
2271         if (ch->n_addrs >= 3 && ch->n_intrs == 3)
2272                 zss->has_dma = 1;
2273 #endif
2274         zss->dma_initted = 0;
2275
2276         zs_chan->control = (volatile unsigned char *)
2277                 ioremap(ch->addrs[0].address, 0x1000);
2278         zs_chan->data = zs_chan->control + 0x10;
2279         spin_lock_init(&zs_chan->lock);
2280         zs_chan->parent = zss;
2281         zss->zs_channel = zs_chan;
2282         zss->zs_chan_a = zs_chan_a;
2283
2284         /* setup misc varariables */
2285         zss->kgdb_channel = 0;
2286
2287         /* For now, we assume you either have a slot-names property
2288          * with "Modem" in it, or your channel is compatible with
2289          * "cobalt". Might need additional fixups
2290          */
2291         zss->is_internal_modem = device_is_compatible(ch, "cobalt");
2292         conn = get_property(ch, "AAPL,connector", &len);
2293         zss->is_irda = conn && (strcmp(conn, "infrared") == 0);
2294         zss->port_type = PMAC_SCC_ASYNC;
2295         /* 1999 Powerbook G3 has slot-names property instead */
2296         slots = (struct slot_names_prop *)get_property(ch, "slot-names", &len);
2297         if (slots && slots->count > 0) {
2298                 if (strcmp(slots->name, "IrDA") == 0)
2299                         zss->is_irda = 1;
2300                 else if (strcmp(slots->name, "Modem") == 0)
2301                         zss->is_internal_modem = 1;
2302         }
2303         if (zss->is_irda)
2304                 zss->port_type = PMAC_SCC_IRDA;
2305         if (zss->is_internal_modem) {
2306                 struct device_node* i2c_modem = find_devices("i2c-modem");
2307                 if (i2c_modem) {
2308                         char* mid = get_property(i2c_modem, "modem-id", NULL);
2309                         if (mid) switch(*mid) {
2310                         case 0x04 :
2311                         case 0x05 :
2312                         case 0x07 :
2313                         case 0x08 :
2314                         case 0x0b :
2315                         case 0x0c :
2316                                 zss->port_type = PMAC_SCC_I2S1;
2317                         }
2318                         printk(KERN_INFO "macserial: i2c-modem detected, id: %d\n",
2319                                 mid ? (*mid) : 0);
2320                 } else {
2321                         printk(KERN_INFO "macserial: serial modem detected\n");
2322                 }
2323         }
2324
2325         while (zss->has_dma) {
2326                 zss->dma_priv = NULL;
2327                 /* it seems that the last two addresses are the
2328                    DMA controllers */
2329                 zss->tx_dma = (volatile struct dbdma_regs *)
2330                         ioremap(ch->addrs[ch->n_addrs - 2].address, 0x100);
2331                 zss->rx = (volatile struct mac_dma *)
2332                         ioremap(ch->addrs[ch->n_addrs - 1].address, 0x100);
2333                 zss->tx_dma_irq = ch->intrs[1].line;
2334                 zss->rx_dma_irq = ch->intrs[2].line;
2335                 spin_lock_init(&zss->rx_dma_lock);
2336                 break;
2337         }
2338
2339         init_timer(&zss->powerup_timer);
2340         zss->powerup_timer.function = powerup_done;
2341         zss->powerup_timer.data = (unsigned long) zss;
2342         return 0;
2343 }
2344
2345 /*
2346  * /proc fs routines. TODO: Add status lines & error stats
2347  */
2348 static inline int
2349 line_info(char *buf, struct mac_serial *info)
2350 {
2351         int             ret=0;
2352         unsigned char* connector;
2353         int lenp;
2354
2355         ret += sprintf(buf, "%d: port:0x%X irq:%d", info->line, info->port, info->irq);
2356
2357         connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2358         if (connector)
2359                 ret+=sprintf(buf+ret," con:%s ", connector);
2360         if (info->is_internal_modem) {
2361                 if (!connector)
2362                         ret+=sprintf(buf+ret," con:");
2363                 ret+=sprintf(buf+ret,"%s", " (internal modem)");
2364         }
2365         if (info->is_irda) {
2366                 if (!connector)
2367                         ret+=sprintf(buf+ret," con:");
2368                 ret+=sprintf(buf+ret,"%s", " (IrDA)");
2369         }
2370         ret+=sprintf(buf+ret,"\n");
2371
2372         return ret;
2373 }
2374
2375 int macserial_read_proc(char *page, char **start, off_t off, int count,
2376                  int *eof, void *data)
2377 {
2378         int l, len = 0;
2379         off_t   begin = 0;
2380         struct mac_serial *info;
2381
2382         len += sprintf(page, "serinfo:1.0 driver:" MACSERIAL_VERSION "\n");
2383         for (info = zs_chain; info && len < 4000; info = info->zs_next) {
2384                 l = line_info(page + len, info);
2385                 len += l;
2386                 if (len+begin > off+count)
2387                         goto done;
2388                 if (len+begin < off) {
2389                         begin += len;
2390                         len = 0;
2391                 }
2392         }
2393         *eof = 1;
2394 done:
2395         if (off >= len+begin)
2396                 return 0;
2397         *start = page + (off-begin);
2398         return ((count < begin+len-off) ? count : begin+len-off);
2399 }
2400
2401 /* Ask the PROM how many Z8530s we have and initialize their zs_channels */
2402 static void
2403 probe_sccs(void)
2404 {
2405         struct device_node *dev, *ch;
2406         struct mac_serial **pp;
2407         int n, chip, nchan;
2408         struct mac_zschannel *zs_chan;
2409         int chan_a_index;
2410
2411         n = 0;
2412         pp = &zs_chain;
2413         zs_chan = zs_channels;
2414         for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
2415                 nchan = 0;
2416                 chip = n;
2417                 if (n >= NUM_CHANNELS) {
2418                         printk(KERN_WARNING "Sorry, can't use %s: no more "
2419                                             "channels\n", dev->full_name);
2420                         continue;
2421                 }
2422                 chan_a_index = 0;
2423                 for (ch = dev->child; ch != 0; ch = ch->sibling) {
2424                         if (nchan >= 2) {
2425                                 printk(KERN_WARNING "SCC: Only 2 channels per "
2426                                         "chip are supported\n");
2427                                 break;
2428                         }
2429                         if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
2430                                 printk("Can't use %s: %d addrs %d intrs\n",
2431                                       ch->full_name, ch->n_addrs, ch->n_intrs);
2432                                 continue;
2433                         }
2434
2435                         /* The channel with the higher address
2436                            will be the A side. */
2437                         if (nchan > 0 &&
2438                             ch->addrs[0].address
2439                             > zs_soft[n-1].dev_node->addrs[0].address)
2440                                 chan_a_index = 1;
2441
2442                         /* minimal initialization for now */
2443                         zs_soft[n].dev_node = ch;
2444                         *pp = &zs_soft[n];
2445                         pp = &zs_soft[n].zs_next;
2446                         ++nchan;
2447                         ++n;
2448                 }
2449                 if (nchan == 0)
2450                         continue;
2451
2452                 /* set up A side */
2453                 if (chan_init(&zs_soft[chip + chan_a_index], zs_chan, zs_chan))
2454                         continue;
2455                 ++zs_chan;
2456
2457                 /* set up B side, if it exists */
2458                 if (nchan > 1)
2459                         if (chan_init(&zs_soft[chip + 1 - chan_a_index],
2460                                   zs_chan, zs_chan - 1))
2461                                 continue;
2462                 ++zs_chan;
2463         }
2464         *pp = 0;
2465
2466         zs_channels_found = n;
2467 #ifdef CONFIG_PMAC_PBOOK
2468         if (n)
2469                 pmu_register_sleep_notifier(&serial_sleep_notifier);
2470 #endif /* CONFIG_PMAC_PBOOK */
2471 }
2472
2473 static struct tty_operations serial_ops = {
2474         .open = rs_open,
2475         .close = rs_close,
2476         .write = rs_write,
2477         .flush_chars = rs_flush_chars,
2478         .write_room = rs_write_room,
2479         .chars_in_buffer = rs_chars_in_buffer,
2480         .flush_buffer = rs_flush_buffer,
2481         .ioctl = rs_ioctl,
2482         .throttle = rs_throttle,
2483         .unthrottle = rs_unthrottle,
2484         .set_termios = rs_set_termios,
2485         .stop = rs_stop,
2486         .start = rs_start,
2487         .hangup = rs_hangup,
2488         .break_ctl = rs_break,
2489         .wait_until_sent = rs_wait_until_sent,
2490         .read_proc = macserial_read_proc,
2491         .tiocmget = rs_tiocmget,
2492         .tiocmset = rs_tiocmset,
2493 };
2494
2495 static int macserial_init(void)
2496 {
2497         int channel, i;
2498         struct mac_serial *info;
2499
2500         /* Find out how many Z8530 SCCs we have */
2501         if (zs_chain == 0)
2502                 probe_sccs();
2503
2504         serial_driver = alloc_tty_driver(zs_channels_found);
2505         if (!serial_driver)
2506                 return -ENOMEM;
2507
2508         /* XXX assume it's a powerbook if we have a via-pmu
2509          * 
2510          * This is OK for core99 machines as well.
2511          */
2512         is_powerbook = find_devices("via-pmu") != 0;
2513
2514         /* Register the interrupt handler for each one
2515          * We also request the OF resources here as probe_sccs()
2516          * might be called too early for that
2517          */
2518         for (i = 0; i < zs_channels_found; ++i) {
2519                 struct device_node* ch = zs_soft[i].dev_node;
2520                 if (!request_OF_resource(ch, 0, NULL)) {
2521                         printk(KERN_ERR "macserial: can't request IO resource !\n");
2522                         put_tty_driver(serial_driver);
2523                         return -ENODEV;
2524                 }
2525                 if (zs_soft[i].has_dma) {
2526                         if (!request_OF_resource(ch, ch->n_addrs - 2, " (tx dma)")) {
2527                                 printk(KERN_ERR "macserial: can't request TX DMA resource !\n");
2528                                 zs_soft[i].has_dma = 0;
2529                                 goto no_dma;
2530                         }
2531                         if (!request_OF_resource(ch, ch->n_addrs - 1, " (rx dma)")) {
2532                                 release_OF_resource(ch, ch->n_addrs - 2);
2533                                 printk(KERN_ERR "macserial: can't request RX DMA resource !\n");
2534                                 zs_soft[i].has_dma = 0;
2535                                 goto no_dma;
2536                         }
2537                         if (request_irq(zs_soft[i].tx_dma_irq, rs_txdma_irq, 0,
2538                                         "SCC-txdma", &zs_soft[i]))
2539                                 printk(KERN_ERR "macserial: can't get irq %d\n",
2540                                        zs_soft[i].tx_dma_irq);
2541                         disable_irq(zs_soft[i].tx_dma_irq);
2542                         if (request_irq(zs_soft[i].rx_dma_irq, rs_rxdma_irq, 0,
2543                                         "SCC-rxdma", &zs_soft[i]))
2544                                 printk(KERN_ERR "macserial: can't get irq %d\n",
2545                                        zs_soft[i].rx_dma_irq);
2546                         disable_irq(zs_soft[i].rx_dma_irq);
2547                 }
2548 no_dma:         
2549                 if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
2550                                 "SCC", &zs_soft[i]))
2551                         printk(KERN_ERR "macserial: can't get irq %d\n",
2552                                zs_soft[i].irq);
2553                 disable_irq(zs_soft[i].irq);
2554         }
2555
2556         show_serial_version();
2557
2558         /* Initialize the tty_driver structure */
2559         /* Not all of this is exactly right for us. */
2560
2561         serial_driver->owner = THIS_MODULE;
2562         serial_driver->driver_name = "macserial";
2563         serial_driver->devfs_name = "tts/";
2564         serial_driver->name = "ttyS";
2565         serial_driver->major = TTY_MAJOR;
2566         serial_driver->minor_start = 64;
2567         serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2568         serial_driver->subtype = SERIAL_TYPE_NORMAL;
2569         serial_driver->init_termios = tty_std_termios;
2570         serial_driver->init_termios.c_cflag =
2571                 B38400 | CS8 | CREAD | HUPCL | CLOCAL;
2572         serial_driver->flags = TTY_DRIVER_REAL_RAW;
2573         tty_set_operations(serial_driver, &serial_ops);
2574
2575         if (tty_register_driver(serial_driver))
2576                 printk(KERN_ERR "Error: couldn't register serial driver\n");
2577
2578         for (channel = 0; channel < zs_channels_found; ++channel) {
2579 #ifdef CONFIG_KGDB
2580                 if (zs_soft[channel].kgdb_channel) {
2581                         kgdb_interruptible(1);
2582                         continue;
2583                 }
2584 #endif
2585                 zs_soft[channel].clk_divisor = 16;
2586 /* -- we are not sure the SCC is powered ON at this point
2587                 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2588 */
2589                 zs_soft[channel].zs_baud = 38400;
2590
2591                 /* If console serial line, then enable interrupts. */
2592                 if (zs_soft[channel].is_cons) {
2593                         printk(KERN_INFO "macserial: console line, enabling "
2594                                         "interrupt %d\n", zs_soft[channel].irq);
2595                         panic("macserial: console not supported yet !");
2596                         write_zsreg(zs_soft[channel].zs_channel, R1,
2597                                     (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
2598                         write_zsreg(zs_soft[channel].zs_channel, R9,
2599                                     (NV | MIE));
2600                 }
2601         }
2602
2603         for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2604         {
2605                 unsigned char* connector;
2606                 int lenp;
2607
2608 #ifdef CONFIG_KGDB
2609                 if (info->kgdb_channel) {
2610                         continue;
2611                 }
2612 #endif
2613                 info->magic = SERIAL_MAGIC;
2614                 info->port = (int) info->zs_channel->control;
2615                 info->line = i;
2616                 info->tty = 0;
2617                 info->custom_divisor = 16;
2618                 info->timeout = 0;
2619                 info->close_delay = 50;
2620                 info->closing_wait = 3000;
2621                 info->x_char = 0;
2622                 info->event = 0;
2623                 info->count = 0;
2624                 info->blocked_open = 0;
2625                 INIT_WORK(&info->tqueue, do_softint, info);
2626                 spin_lock_init(&info->lock);
2627                 init_waitqueue_head(&info->open_wait);
2628                 init_waitqueue_head(&info->close_wait);
2629                 info->timeout = HZ;
2630                 printk(KERN_INFO "tty%02d at 0x%08x (irq = %d)", info->line, 
2631                         info->port, info->irq);
2632                 printk(" is a Z8530 ESCC");
2633                 connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2634                 if (connector)
2635                         printk(", port = %s", connector);
2636                 if (info->is_internal_modem)
2637                         printk(" (internal modem)");
2638                 if (info->is_irda)
2639                         printk(" (IrDA)");
2640                 printk("\n");
2641         }
2642         tmp_buf = 0;
2643
2644         return 0;
2645 }
2646
2647 void macserial_cleanup(void)
2648 {
2649         int i;
2650         unsigned long flags;
2651         struct mac_serial *info;
2652
2653         for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2654                 set_scc_power(info, 0);
2655         spin_lock_irqsave(&info->lock, flags);
2656         for (i = 0; i < zs_channels_found; ++i) {
2657                 free_irq(zs_soft[i].irq, &zs_soft[i]);
2658                 if (zs_soft[i].has_dma) {
2659                         free_irq(zs_soft[i].tx_dma_irq, &zs_soft[i]);
2660                         free_irq(zs_soft[i].rx_dma_irq, &zs_soft[i]);
2661                 }
2662                 release_OF_resource(zs_soft[i].dev_node, 0);
2663                 if (zs_soft[i].has_dma) {
2664                         struct device_node* ch = zs_soft[i].dev_node;
2665                         release_OF_resource(ch, ch->n_addrs - 2);
2666                         release_OF_resource(ch, ch->n_addrs - 1);
2667                 }
2668         }
2669         spin_unlock_irqrestore(&info->lock, flags);
2670         tty_unregister_driver(serial_driver);
2671         put_tty_driver(serial_driver);
2672
2673         if (tmp_buf) {
2674                 free_page((unsigned long) tmp_buf);
2675                 tmp_buf = 0;
2676         }
2677
2678 #ifdef CONFIG_PMAC_PBOOK
2679         if (zs_channels_found)
2680                 pmu_unregister_sleep_notifier(&serial_sleep_notifier);
2681 #endif /* CONFIG_PMAC_PBOOK */
2682 }
2683
2684 module_init(macserial_init);
2685 module_exit(macserial_cleanup);
2686 MODULE_LICENSE("GPL");
2687
2688 #if 0
2689 /*
2690  * register_serial and unregister_serial allows for serial ports to be
2691  * configured at run-time, to support PCMCIA modems.
2692  */
2693 /* PowerMac: Unused at this time, just here to make things link. */
2694 int register_serial(struct serial_struct *req)
2695 {
2696         return -1;
2697 }
2698
2699 void unregister_serial(int line)
2700 {
2701         return;
2702 }
2703 #endif
2704
2705 /*
2706  * ------------------------------------------------------------
2707  * Serial console driver
2708  * ------------------------------------------------------------
2709  */
2710 #ifdef CONFIG_SERIAL_CONSOLE
2711
2712 /*
2713  *      Print a string to the serial port trying not to disturb
2714  *      any possible real use of the port...
2715  */
2716 static void serial_console_write(struct console *co, const char *s,
2717                                  unsigned count)
2718 {
2719         struct mac_serial *info = zs_soft + co->index;
2720         int i;
2721
2722         /* Turn of interrupts and enable the transmitter. */
2723         write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2724         write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2725
2726         for (i=0; i<count; i++) {
2727                 /* Wait for the transmit buffer to empty. */
2728                 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2729                         eieio();
2730                 }
2731
2732                 write_zsdata(info->zs_channel, s[i]);
2733                 if (s[i] == 10) {
2734                         while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2735                                 == 0)
2736                                 eieio();
2737
2738                         write_zsdata(info->zs_channel, 13);
2739                 }
2740         }
2741
2742         /* Restore the values in the registers. */
2743         write_zsreg(info->zs_channel, R1, info->curregs[1]);
2744         /* Don't disable the transmitter. */
2745 }
2746
2747 static struct tty_driver *serial_driver;
2748
2749 static struct tty_driver *serial_console_device(struct console *c, int *index)
2750 {
2751         *index = c->index;
2752         return serial_driver;
2753 }
2754
2755 /*
2756  *      Setup initial baud/bits/parity. We do two things here:
2757  *      - construct a cflag setting for the first rs_open()
2758  *      - initialize the serial port
2759  *      Return non-zero if we didn't find a serial port.
2760  */
2761 static int __init serial_console_setup(struct console *co, char *options)
2762 {
2763         struct mac_serial *info;
2764         int     baud = 38400;
2765         int     bits = 8;
2766         int     parity = 'n';
2767         int     cflag = CREAD | HUPCL | CLOCAL;
2768         int     brg;
2769         char    *s;
2770         long    flags;
2771
2772         /* Find out how many Z8530 SCCs we have */
2773         if (zs_chain == 0)
2774                 probe_sccs();
2775
2776         if (zs_chain == 0)
2777                 return -1;
2778
2779         /* Do we have the device asked for? */
2780         if (co->index >= zs_channels_found)
2781                 return -1;
2782         info = zs_soft + co->index;
2783
2784         set_scc_power(info, 1);
2785
2786         /* Reset the channel */
2787         write_zsreg(info->zs_channel, R9, CHRA);
2788
2789         if (options) {
2790                 baud = simple_strtoul(options, NULL, 10);
2791                 s = options;
2792                 while(*s >= '0' && *s <= '9')
2793                         s++;
2794                 if (*s)
2795                         parity = *s++;
2796                 if (*s)
2797                         bits   = *s - '0';
2798         }
2799
2800         /*
2801          *      Now construct a cflag setting.
2802          */
2803         switch(baud) {
2804         case 1200:
2805                 cflag |= B1200;
2806                 break;
2807         case 2400:
2808                 cflag |= B2400;
2809                 break;
2810         case 4800:
2811                 cflag |= B4800;
2812                 break;
2813         case 9600:
2814                 cflag |= B9600;
2815                 break;
2816         case 19200:
2817                 cflag |= B19200;
2818                 break;
2819         case 57600:
2820                 cflag |= B57600;
2821                 break;
2822         case 115200:
2823                 cflag |= B115200;
2824                 break;
2825         case 38400:
2826         default:
2827                 cflag |= B38400;
2828                 break;
2829         }
2830         switch(bits) {
2831         case 7:
2832                 cflag |= CS7;
2833                 break;
2834         default:
2835         case 8:
2836                 cflag |= CS8;
2837                 break;
2838         }
2839         switch(parity) {
2840         case 'o': case 'O':
2841                 cflag |= PARENB | PARODD;
2842                 break;
2843         case 'e': case 'E':
2844                 cflag |= PARENB;
2845                 break;
2846         }
2847         co->cflag = cflag;
2848
2849         spin_lock_irqsave(&info->lock, flags);
2850         memset(info->curregs, 0, sizeof(info->curregs));
2851
2852         info->zs_baud = baud;
2853         info->clk_divisor = 16;
2854         switch (info->zs_baud) {
2855         case ZS_CLOCK/16:       /* 230400 */
2856                 info->curregs[4] = X16CLK;
2857                 info->curregs[11] = 0;
2858                 break;
2859         case ZS_CLOCK/32:       /* 115200 */
2860                 info->curregs[4] = X32CLK;
2861                 info->curregs[11] = 0;
2862                 break;
2863         default:
2864                 info->curregs[4] = X16CLK;
2865                 info->curregs[11] = TCBR | RCBR;
2866                 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2867                 info->curregs[12] = (brg & 255);
2868                 info->curregs[13] = ((brg >> 8) & 255);
2869                 info->curregs[14] = BRENABL;
2870         }
2871
2872         /* byte size and parity */
2873         info->curregs[3] &= ~RxNBITS_MASK;
2874         info->curregs[5] &= ~TxNBITS_MASK;
2875         switch (cflag & CSIZE) {
2876         case CS5:
2877                 info->curregs[3] |= Rx5;
2878                 info->curregs[5] |= Tx5;
2879                 break;
2880         case CS6:
2881                 info->curregs[3] |= Rx6;
2882                 info->curregs[5] |= Tx6;
2883                 break;
2884         case CS7:
2885                 info->curregs[3] |= Rx7;
2886                 info->curregs[5] |= Tx7;
2887                 break;
2888         case CS8:
2889         default: /* defaults to 8 bits */
2890                 info->curregs[3] |= Rx8;
2891                 info->curregs[5] |= Tx8;
2892                 break;
2893         }
2894         info->curregs[5] |= TxENAB | RTS | DTR;
2895         info->pendregs[3] = info->curregs[3];
2896         info->pendregs[5] = info->curregs[5];
2897
2898         info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
2899         if (cflag & CSTOPB) {
2900                 info->curregs[4] |= SB2;
2901         } else {
2902                 info->curregs[4] |= SB1;
2903         }
2904         if (cflag & PARENB) {
2905                 info->curregs[4] |= PAR_ENA;
2906                 if (!(cflag & PARODD)) {
2907                         info->curregs[4] |= PAR_EVEN;
2908                 }
2909         }
2910         info->pendregs[4] = info->curregs[4];
2911
2912         if (!(cflag & CLOCAL)) {
2913                 if (!(info->curregs[15] & DCDIE))
2914                         info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2915                 info->curregs[15] |= DCDIE;
2916         } else
2917                 info->curregs[15] &= ~DCDIE;
2918         if (cflag & CRTSCTS) {
2919                 info->curregs[15] |= CTSIE;
2920                 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
2921                         info->tx_stopped = 1;
2922         } else {
2923                 info->curregs[15] &= ~CTSIE;
2924                 info->tx_stopped = 0;
2925         }
2926         info->pendregs[15] = info->curregs[15];
2927
2928         /* Load up the new values */
2929         load_zsregs(info->zs_channel, info->curregs);
2930
2931         spin_unlock_irqrestore(&info->lock, flags);
2932
2933         return 0;
2934 }
2935
2936 static struct console sercons = {
2937         .name           = "ttyS",
2938         .write          = serial_console_write,
2939         .device         = serial_console_device,
2940         .setup          = serial_console_setup,
2941         .flags          = CON_PRINTBUFFER,
2942         .index          = -1,
2943 };
2944
2945 /*
2946  *      Register console.
2947  */
2948 static void __init mac_scc_console_init(void)
2949 {
2950         register_console(&sercons);
2951 }
2952 console_initcall(mac_scc_console_init);
2953
2954 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2955
2956 #ifdef CONFIG_KGDB
2957 /* These are for receiving and sending characters under the kgdb
2958  * source level kernel debugger.
2959  */
2960 void putDebugChar(char kgdb_char)
2961 {
2962         struct mac_zschannel *chan = zs_kgdbchan;
2963         while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2964                 udelay(5);
2965         write_zsdata(chan, kgdb_char);
2966 }
2967
2968 char getDebugChar(void)
2969 {
2970         struct mac_zschannel *chan = zs_kgdbchan;
2971         while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2972                 eieio(); /*barrier();*/
2973         return read_zsdata(chan);
2974 }
2975
2976 void kgdb_interruptible(int yes)
2977 {
2978         struct mac_zschannel *chan = zs_kgdbchan;
2979         int one, nine;
2980         nine = read_zsreg(chan, 9);
2981         if (yes == 1) {
2982                 one = EXT_INT_ENAB|INT_ALL_Rx;
2983                 nine |= MIE;
2984                 printk("turning serial ints on\n");
2985         } else {
2986                 one = RxINT_DISAB;
2987                 nine &= ~MIE;
2988                 printk("turning serial ints off\n");
2989         }
2990         write_zsreg(chan, 1, one);
2991         write_zsreg(chan, 9, nine);
2992 }
2993
2994 /* This sets up the serial port we're using, and turns on
2995  * interrupts for that channel, so kgdb is usable once we're done.
2996  */
2997 static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
2998 {
2999         int brg;
3000         int i, x;
3001         volatile char *sccc = ms->control;
3002         brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
3003         printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
3004         for (i = 20000; i != 0; --i) {
3005                 x = *sccc; eieio();
3006         }
3007         for (i = 0; i < sizeof(scc_inittab); ++i) {
3008                 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
3009                 i++;
3010         }
3011 }
3012
3013 /* This is called at boot time to prime the kgdb serial debugging
3014  * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
3015  * for /dev/ttyb which is determined in setup_arch() from the
3016  * boot command line flags.
3017  * XXX at the moment probably only channel A will work
3018  */
3019 void __init zs_kgdb_hook(int tty_num)
3020 {
3021         /* Find out how many Z8530 SCCs we have */
3022         if (zs_chain == 0)
3023                 probe_sccs();
3024
3025         set_scc_power(&zs_soft[tty_num], 1);
3026
3027         zs_kgdbchan = zs_soft[tty_num].zs_channel;
3028         zs_soft[tty_num].change_needed = 0;
3029         zs_soft[tty_num].clk_divisor = 16;
3030         zs_soft[tty_num].zs_baud = 38400;
3031         zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
3032
3033         /* Turn on transmitter/receiver at 8-bits/char */
3034         kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
3035         printk("KGDB: on channel %d initialized\n", tty_num);
3036         set_debug_traps(); /* init stub */
3037 }
3038 #endif /* ifdef CONFIG_KGDB */
3039
3040 #ifdef CONFIG_PMAC_PBOOK
3041 /*
3042  * notify clients before sleep and reset bus afterwards
3043  */
3044 int
3045 serial_notify_sleep(struct pmu_sleep_notifier *self, int when)
3046 {
3047         int i;
3048
3049         switch (when) {
3050         case PBOOK_SLEEP_REQUEST:
3051         case PBOOK_SLEEP_REJECT:
3052                 break;
3053
3054         case PBOOK_SLEEP_NOW:
3055                 for (i=0; i<zs_channels_found; i++) {
3056                         struct mac_serial *info = &zs_soft[i];
3057                         if (info->flags & ZILOG_INITIALIZED) {
3058                                 shutdown(info);
3059                                 info->flags |= ZILOG_SLEEPING;
3060                         }
3061                 }
3062                 break;
3063         case PBOOK_WAKE:
3064                 for (i=0; i<zs_channels_found; i++) {
3065                         struct mac_serial *info = &zs_soft[i];
3066                         if (info->flags & ZILOG_SLEEPING) {
3067                                 info->flags &= ~ZILOG_SLEEPING;
3068                                 startup(info);
3069                         }
3070                 }
3071                 break;
3072         }
3073         return PBOOK_SLEEP_OK;
3074 }
3075 #endif /* CONFIG_PMAC_PBOOK */