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