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