IB/core: Set alternate port number when initializing QP attributes
[sfrench/cifs-2.6.git] / drivers / serial / pmac_zilog.c
1 /*
2  * linux/drivers/serial/pmac_zilog.c
3  * 
4  * Driver for PowerMac Z85c30 based ESCC cell found in the
5  * "macio" ASICs of various PowerMac models
6  * 
7  * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
8  *
9  * Derived from drivers/macintosh/macserial.c by Paul Mackerras
10  * and drivers/serial/sunzilog.c by David S. Miller
11  *
12  * Hrm... actually, I ripped most of sunzilog (Thanks David !) and
13  * adapted special tweaks needed for us. I don't think it's worth
14  * merging back those though. The DMA code still has to get in
15  * and once done, I expect that driver to remain fairly stable in
16  * the long term, unless we change the driver model again...
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  * 2004-08-06 Harald Welte <laforge@gnumonks.org>
33  *      - Enable BREAK interrupt
34  *      - Add support for sysreq
35  *
36  * TODO:   - Add DMA support
37  *         - Defer port shutdown to a few seconds after close
38  *         - maybe put something right into uap->clk_divisor
39  */
40
41 #undef DEBUG
42 #undef DEBUG_HARD
43 #undef USE_CTRL_O_SYSRQ
44
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/tty.h>
48
49 #include <linux/tty_flip.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/mm.h>
54 #include <linux/kernel.h>
55 #include <linux/delay.h>
56 #include <linux/init.h>
57 #include <linux/console.h>
58 #include <linux/slab.h>
59 #include <linux/adb.h>
60 #include <linux/pmu.h>
61 #include <linux/bitops.h>
62 #include <linux/sysrq.h>
63 #include <linux/mutex.h>
64 #include <asm/sections.h>
65 #include <asm/io.h>
66 #include <asm/irq.h>
67 #include <asm/prom.h>
68 #include <asm/machdep.h>
69 #include <asm/pmac_feature.h>
70 #include <asm/dbdma.h>
71 #include <asm/macio.h>
72
73 #if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
74 #define SUPPORT_SYSRQ
75 #endif
76
77 #include <linux/serial.h>
78 #include <linux/serial_core.h>
79
80 #include "pmac_zilog.h"
81
82 /* Not yet implemented */
83 #undef HAS_DBDMA
84
85 static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
86 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
87 MODULE_DESCRIPTION("Driver for the PowerMac serial ports.");
88 MODULE_LICENSE("GPL");
89
90 #define PWRDBG(fmt, arg...)     printk(KERN_DEBUG fmt , ## arg)
91
92
93 /*
94  * For the sake of early serial console, we can do a pre-probe
95  * (optional) of the ports at rather early boot time.
96  */
97 static struct uart_pmac_port    pmz_ports[MAX_ZS_PORTS];
98 static int                      pmz_ports_count;
99 static DEFINE_MUTEX(pmz_irq_mutex);
100
101 static struct uart_driver pmz_uart_reg = {
102         .owner          =       THIS_MODULE,
103         .driver_name    =       "ttyS",
104         .dev_name       =       "ttyS",
105         .major          =       TTY_MAJOR,
106 };
107
108
109 /* 
110  * Load all registers to reprogram the port
111  * This function must only be called when the TX is not busy.  The UART
112  * port lock must be held and local interrupts disabled.
113  */
114 static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
115 {
116         int i;
117
118         if (ZS_IS_ASLEEP(uap))
119                 return;
120
121         /* Let pending transmits finish.  */
122         for (i = 0; i < 1000; i++) {
123                 unsigned char stat = read_zsreg(uap, R1);
124                 if (stat & ALL_SNT)
125                         break;
126                 udelay(100);
127         }
128
129         ZS_CLEARERR(uap);
130         zssync(uap);
131         ZS_CLEARFIFO(uap);
132         zssync(uap);
133         ZS_CLEARERR(uap);
134
135         /* Disable all interrupts.  */
136         write_zsreg(uap, R1,
137                     regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
138
139         /* Set parity, sync config, stop bits, and clock divisor.  */
140         write_zsreg(uap, R4, regs[R4]);
141
142         /* Set misc. TX/RX control bits.  */
143         write_zsreg(uap, R10, regs[R10]);
144
145         /* Set TX/RX controls sans the enable bits.  */
146         write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
147         write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
148
149         /* now set R7 "prime" on ESCC */
150         write_zsreg(uap, R15, regs[R15] | EN85C30);
151         write_zsreg(uap, R7, regs[R7P]);
152
153         /* make sure we use R7 "non-prime" on ESCC */
154         write_zsreg(uap, R15, regs[R15] & ~EN85C30);
155
156         /* Synchronous mode config.  */
157         write_zsreg(uap, R6, regs[R6]);
158         write_zsreg(uap, R7, regs[R7]);
159
160         /* Disable baud generator.  */
161         write_zsreg(uap, R14, regs[R14] & ~BRENAB);
162
163         /* Clock mode control.  */
164         write_zsreg(uap, R11, regs[R11]);
165
166         /* Lower and upper byte of baud rate generator divisor.  */
167         write_zsreg(uap, R12, regs[R12]);
168         write_zsreg(uap, R13, regs[R13]);
169         
170         /* Now rewrite R14, with BRENAB (if set).  */
171         write_zsreg(uap, R14, regs[R14]);
172
173         /* Reset external status interrupts.  */
174         write_zsreg(uap, R0, RES_EXT_INT);
175         write_zsreg(uap, R0, RES_EXT_INT);
176
177         /* Rewrite R3/R5, this time without enables masked.  */
178         write_zsreg(uap, R3, regs[R3]);
179         write_zsreg(uap, R5, regs[R5]);
180
181         /* Rewrite R1, this time without IRQ enabled masked.  */
182         write_zsreg(uap, R1, regs[R1]);
183
184         /* Enable interrupts */
185         write_zsreg(uap, R9, regs[R9]);
186 }
187
188 /* 
189  * We do like sunzilog to avoid disrupting pending Tx
190  * Reprogram the Zilog channel HW registers with the copies found in the
191  * software state struct.  If the transmitter is busy, we defer this update
192  * until the next TX complete interrupt.  Else, we do it right now.
193  *
194  * The UART port lock must be held and local interrupts disabled.
195  */
196 static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
197 {
198         if (!ZS_REGS_HELD(uap)) {
199                 if (ZS_TX_ACTIVE(uap)) {
200                         uap->flags |= PMACZILOG_FLAG_REGS_HELD;
201                 } else {
202                         pmz_debug("pmz: maybe_update_regs: updating\n");
203                         pmz_load_zsregs(uap, uap->curregs);
204                 }
205         }
206 }
207
208 static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap,
209                                             struct pt_regs *regs)
210 {
211         struct tty_struct *tty = NULL;
212         unsigned char ch, r1, drop, error, flag;
213         int loops = 0;
214
215         /* The interrupt can be enabled when the port isn't open, typically
216          * that happens when using one port is open and the other closed (stale
217          * interrupt) or when one port is used as a console.
218          */
219         if (!ZS_IS_OPEN(uap)) {
220                 pmz_debug("pmz: draining input\n");
221                 /* Port is closed, drain input data */
222                 for (;;) {
223                         if ((++loops) > 1000)
224                                 goto flood;
225                         (void)read_zsreg(uap, R1);
226                         write_zsreg(uap, R0, ERR_RES);
227                         (void)read_zsdata(uap);
228                         ch = read_zsreg(uap, R0);
229                         if (!(ch & Rx_CH_AV))
230                                 break;
231                 }
232                 return NULL;
233         }
234
235         /* Sanity check, make sure the old bug is no longer happening */
236         if (uap->port.info == NULL || uap->port.info->tty == NULL) {
237                 WARN_ON(1);
238                 (void)read_zsdata(uap);
239                 return NULL;
240         }
241         tty = uap->port.info->tty;
242
243         while (1) {
244                 error = 0;
245                 drop = 0;
246
247                 r1 = read_zsreg(uap, R1);
248                 ch = read_zsdata(uap);
249
250                 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
251                         write_zsreg(uap, R0, ERR_RES);
252                         zssync(uap);
253                 }
254
255                 ch &= uap->parity_mask;
256                 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
257                         uap->flags &= ~PMACZILOG_FLAG_BREAK;
258                 }
259
260 #if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
261 #ifdef USE_CTRL_O_SYSRQ
262                 /* Handle the SysRq ^O Hack */
263                 if (ch == '\x0f') {
264                         uap->port.sysrq = jiffies + HZ*5;
265                         goto next_char;
266                 }
267 #endif /* USE_CTRL_O_SYSRQ */
268                 if (uap->port.sysrq) {
269                         int swallow;
270                         spin_unlock(&uap->port.lock);
271                         swallow = uart_handle_sysrq_char(&uap->port, ch, regs);
272                         spin_lock(&uap->port.lock);
273                         if (swallow)
274                                 goto next_char;
275                 }
276 #endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
277
278                 /* A real serial line, record the character and status.  */
279                 if (drop)
280                         goto next_char;
281
282                 flag = TTY_NORMAL;
283                 uap->port.icount.rx++;
284
285                 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
286                         error = 1;
287                         if (r1 & BRK_ABRT) {
288                                 pmz_debug("pmz: got break !\n");
289                                 r1 &= ~(PAR_ERR | CRC_ERR);
290                                 uap->port.icount.brk++;
291                                 if (uart_handle_break(&uap->port))
292                                         goto next_char;
293                         }
294                         else if (r1 & PAR_ERR)
295                                 uap->port.icount.parity++;
296                         else if (r1 & CRC_ERR)
297                                 uap->port.icount.frame++;
298                         if (r1 & Rx_OVR)
299                                 uap->port.icount.overrun++;
300                         r1 &= uap->port.read_status_mask;
301                         if (r1 & BRK_ABRT)
302                                 flag = TTY_BREAK;
303                         else if (r1 & PAR_ERR)
304                                 flag = TTY_PARITY;
305                         else if (r1 & CRC_ERR)
306                                 flag = TTY_FRAME;
307                 }
308
309                 if (uap->port.ignore_status_mask == 0xff ||
310                     (r1 & uap->port.ignore_status_mask) == 0) {
311                         tty_insert_flip_char(tty, ch, flag);
312                 }
313                 if (r1 & Rx_OVR)
314                         tty_insert_flip_char(tty, 0, TTY_OVERRUN);
315         next_char:
316                 /* We can get stuck in an infinite loop getting char 0 when the
317                  * line is in a wrong HW state, we break that here.
318                  * When that happens, I disable the receive side of the driver.
319                  * Note that what I've been experiencing is a real irq loop where
320                  * I'm getting flooded regardless of the actual port speed.
321                  * Something stange is going on with the HW
322                  */
323                 if ((++loops) > 1000)
324                         goto flood;
325                 ch = read_zsreg(uap, R0);
326                 if (!(ch & Rx_CH_AV))
327                         break;
328         }
329
330         return tty;
331  flood:
332         uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
333         write_zsreg(uap, R1, uap->curregs[R1]);
334         zssync(uap);
335         dev_err(&uap->dev->ofdev.dev, "pmz: rx irq flood !\n");
336         return tty;
337 }
338
339 static void pmz_status_handle(struct uart_pmac_port *uap, struct pt_regs *regs)
340 {
341         unsigned char status;
342
343         status = read_zsreg(uap, R0);
344         write_zsreg(uap, R0, RES_EXT_INT);
345         zssync(uap);
346
347         if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
348                 if (status & SYNC_HUNT)
349                         uap->port.icount.dsr++;
350
351                 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
352                  * But it does not tell us which bit has changed, we have to keep
353                  * track of this ourselves.
354                  * The CTS input is inverted for some reason.  -- paulus
355                  */
356                 if ((status ^ uap->prev_status) & DCD)
357                         uart_handle_dcd_change(&uap->port,
358                                                (status & DCD));
359                 if ((status ^ uap->prev_status) & CTS)
360                         uart_handle_cts_change(&uap->port,
361                                                !(status & CTS));
362
363                 wake_up_interruptible(&uap->port.info->delta_msr_wait);
364         }
365
366         if (status & BRK_ABRT)
367                 uap->flags |= PMACZILOG_FLAG_BREAK;
368
369         uap->prev_status = status;
370 }
371
372 static void pmz_transmit_chars(struct uart_pmac_port *uap)
373 {
374         struct circ_buf *xmit;
375
376         if (ZS_IS_ASLEEP(uap))
377                 return;
378         if (ZS_IS_CONS(uap)) {
379                 unsigned char status = read_zsreg(uap, R0);
380
381                 /* TX still busy?  Just wait for the next TX done interrupt.
382                  *
383                  * It can occur because of how we do serial console writes.  It would
384                  * be nice to transmit console writes just like we normally would for
385                  * a TTY line. (ie. buffered and TX interrupt driven).  That is not
386                  * easy because console writes cannot sleep.  One solution might be
387                  * to poll on enough port->xmit space becomming free.  -DaveM
388                  */
389                 if (!(status & Tx_BUF_EMP))
390                         return;
391         }
392
393         uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
394
395         if (ZS_REGS_HELD(uap)) {
396                 pmz_load_zsregs(uap, uap->curregs);
397                 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
398         }
399
400         if (ZS_TX_STOPPED(uap)) {
401                 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
402                 goto ack_tx_int;
403         }
404
405         if (uap->port.x_char) {
406                 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
407                 write_zsdata(uap, uap->port.x_char);
408                 zssync(uap);
409                 uap->port.icount.tx++;
410                 uap->port.x_char = 0;
411                 return;
412         }
413
414         if (uap->port.info == NULL)
415                 goto ack_tx_int;
416         xmit = &uap->port.info->xmit;
417         if (uart_circ_empty(xmit)) {
418                 uart_write_wakeup(&uap->port);
419                 goto ack_tx_int;
420         }
421         if (uart_tx_stopped(&uap->port))
422                 goto ack_tx_int;
423
424         uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
425         write_zsdata(uap, xmit->buf[xmit->tail]);
426         zssync(uap);
427
428         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
429         uap->port.icount.tx++;
430
431         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
432                 uart_write_wakeup(&uap->port);
433
434         return;
435
436 ack_tx_int:
437         write_zsreg(uap, R0, RES_Tx_P);
438         zssync(uap);
439 }
440
441 /* Hrm... we register that twice, fixme later.... */
442 static irqreturn_t pmz_interrupt(int irq, void *dev_id, struct pt_regs *regs)
443 {
444         struct uart_pmac_port *uap = dev_id;
445         struct uart_pmac_port *uap_a;
446         struct uart_pmac_port *uap_b;
447         int rc = IRQ_NONE;
448         struct tty_struct *tty;
449         u8 r3;
450
451         uap_a = pmz_get_port_A(uap);
452         uap_b = uap_a->mate;
453        
454         spin_lock(&uap_a->port.lock);
455         r3 = read_zsreg(uap_a, R3);
456
457 #ifdef DEBUG_HARD
458         pmz_debug("irq, r3: %x\n", r3);
459 #endif
460         /* Channel A */
461         tty = NULL;
462         if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
463                 write_zsreg(uap_a, R0, RES_H_IUS);
464                 zssync(uap_a);          
465                 if (r3 & CHAEXT)
466                         pmz_status_handle(uap_a, regs);
467                 if (r3 & CHARxIP)
468                         tty = pmz_receive_chars(uap_a, regs);
469                 if (r3 & CHATxIP)
470                         pmz_transmit_chars(uap_a);
471                 rc = IRQ_HANDLED;
472         }
473         spin_unlock(&uap_a->port.lock);
474         if (tty != NULL)
475                 tty_flip_buffer_push(tty);
476
477         if (uap_b->node == NULL)
478                 goto out;
479
480         spin_lock(&uap_b->port.lock);
481         tty = NULL;
482         if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
483                 write_zsreg(uap_b, R0, RES_H_IUS);
484                 zssync(uap_b);
485                 if (r3 & CHBEXT)
486                         pmz_status_handle(uap_b, regs);
487                 if (r3 & CHBRxIP)
488                         tty = pmz_receive_chars(uap_b, regs);
489                 if (r3 & CHBTxIP)
490                         pmz_transmit_chars(uap_b);
491                 rc = IRQ_HANDLED;
492         }
493         spin_unlock(&uap_b->port.lock);
494         if (tty != NULL)
495                 tty_flip_buffer_push(tty);
496
497  out:
498 #ifdef DEBUG_HARD
499         pmz_debug("irq done.\n");
500 #endif
501         return rc;
502 }
503
504 /*
505  * Peek the status register, lock not held by caller
506  */
507 static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
508 {
509         unsigned long flags;
510         u8 status;
511         
512         spin_lock_irqsave(&uap->port.lock, flags);
513         status = read_zsreg(uap, R0);
514         spin_unlock_irqrestore(&uap->port.lock, flags);
515
516         return status;
517 }
518
519 /* 
520  * Check if transmitter is empty
521  * The port lock is not held.
522  */
523 static unsigned int pmz_tx_empty(struct uart_port *port)
524 {
525         struct uart_pmac_port *uap = to_pmz(port);
526         unsigned char status;
527
528         if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
529                 return TIOCSER_TEMT;
530
531         status = pmz_peek_status(to_pmz(port));
532         if (status & Tx_BUF_EMP)
533                 return TIOCSER_TEMT;
534         return 0;
535 }
536
537 /* 
538  * Set Modem Control (RTS & DTR) bits
539  * The port lock is held and interrupts are disabled.
540  * Note: Shall we really filter out RTS on external ports or
541  * should that be dealt at higher level only ?
542  */
543 static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
544 {
545         struct uart_pmac_port *uap = to_pmz(port);
546         unsigned char set_bits, clear_bits;
547
548         /* Do nothing for irda for now... */
549         if (ZS_IS_IRDA(uap))
550                 return;
551         /* We get called during boot with a port not up yet */
552         if (ZS_IS_ASLEEP(uap) ||
553             !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
554                 return;
555
556         set_bits = clear_bits = 0;
557
558         if (ZS_IS_INTMODEM(uap)) {
559                 if (mctrl & TIOCM_RTS)
560                         set_bits |= RTS;
561                 else
562                         clear_bits |= RTS;
563         }
564         if (mctrl & TIOCM_DTR)
565                 set_bits |= DTR;
566         else
567                 clear_bits |= DTR;
568
569         /* NOTE: Not subject to 'transmitter active' rule.  */ 
570         uap->curregs[R5] |= set_bits;
571         uap->curregs[R5] &= ~clear_bits;
572         if (ZS_IS_ASLEEP(uap))
573                 return;
574         write_zsreg(uap, R5, uap->curregs[R5]);
575         pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
576                   set_bits, clear_bits, uap->curregs[R5]);
577         zssync(uap);
578 }
579
580 /* 
581  * Get Modem Control bits (only the input ones, the core will
582  * or that with a cached value of the control ones)
583  * The port lock is held and interrupts are disabled.
584  */
585 static unsigned int pmz_get_mctrl(struct uart_port *port)
586 {
587         struct uart_pmac_port *uap = to_pmz(port);
588         unsigned char status;
589         unsigned int ret;
590
591         if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
592                 return 0;
593
594         status = read_zsreg(uap, R0);
595
596         ret = 0;
597         if (status & DCD)
598                 ret |= TIOCM_CAR;
599         if (status & SYNC_HUNT)
600                 ret |= TIOCM_DSR;
601         if (!(status & CTS))
602                 ret |= TIOCM_CTS;
603
604         return ret;
605 }
606
607 /* 
608  * Stop TX side. Dealt like sunzilog at next Tx interrupt,
609  * though for DMA, we will have to do a bit more.
610  * The port lock is held and interrupts are disabled.
611  */
612 static void pmz_stop_tx(struct uart_port *port)
613 {
614         to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
615 }
616
617 /* 
618  * Kick the Tx side.
619  * The port lock is held and interrupts are disabled.
620  */
621 static void pmz_start_tx(struct uart_port *port)
622 {
623         struct uart_pmac_port *uap = to_pmz(port);
624         unsigned char status;
625
626         pmz_debug("pmz: start_tx()\n");
627
628         uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
629         uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
630
631         if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
632                 return;
633
634         status = read_zsreg(uap, R0);
635
636         /* TX busy?  Just wait for the TX done interrupt.  */
637         if (!(status & Tx_BUF_EMP))
638                 return;
639
640         /* Send the first character to jump-start the TX done
641          * IRQ sending engine.
642          */
643         if (port->x_char) {
644                 write_zsdata(uap, port->x_char);
645                 zssync(uap);
646                 port->icount.tx++;
647                 port->x_char = 0;
648         } else {
649                 struct circ_buf *xmit = &port->info->xmit;
650
651                 write_zsdata(uap, xmit->buf[xmit->tail]);
652                 zssync(uap);
653                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
654                 port->icount.tx++;
655
656                 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
657                         uart_write_wakeup(&uap->port);
658         }
659         pmz_debug("pmz: start_tx() done.\n");
660 }
661
662 /* 
663  * Stop Rx side, basically disable emitting of
664  * Rx interrupts on the port. We don't disable the rx
665  * side of the chip proper though
666  * The port lock is held.
667  */
668 static void pmz_stop_rx(struct uart_port *port)
669 {
670         struct uart_pmac_port *uap = to_pmz(port);
671
672         if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
673                 return;
674
675         pmz_debug("pmz: stop_rx()()\n");
676
677         /* Disable all RX interrupts.  */
678         uap->curregs[R1] &= ~RxINT_MASK;
679         pmz_maybe_update_regs(uap);
680
681         pmz_debug("pmz: stop_rx() done.\n");
682 }
683
684 /* 
685  * Enable modem status change interrupts
686  * The port lock is held.
687  */
688 static void pmz_enable_ms(struct uart_port *port)
689 {
690         struct uart_pmac_port *uap = to_pmz(port);
691         unsigned char new_reg;
692
693         if (ZS_IS_IRDA(uap) || uap->node == NULL)
694                 return;
695         new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
696         if (new_reg != uap->curregs[R15]) {
697                 uap->curregs[R15] = new_reg;
698
699                 if (ZS_IS_ASLEEP(uap))
700                         return;
701                 /* NOTE: Not subject to 'transmitter active' rule.  */ 
702                 write_zsreg(uap, R15, uap->curregs[R15]);
703         }
704 }
705
706 /* 
707  * Control break state emission
708  * The port lock is not held.
709  */
710 static void pmz_break_ctl(struct uart_port *port, int break_state)
711 {
712         struct uart_pmac_port *uap = to_pmz(port);
713         unsigned char set_bits, clear_bits, new_reg;
714         unsigned long flags;
715
716         if (uap->node == NULL)
717                 return;
718         set_bits = clear_bits = 0;
719
720         if (break_state)
721                 set_bits |= SND_BRK;
722         else
723                 clear_bits |= SND_BRK;
724
725         spin_lock_irqsave(&port->lock, flags);
726
727         new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
728         if (new_reg != uap->curregs[R5]) {
729                 uap->curregs[R5] = new_reg;
730
731                 /* NOTE: Not subject to 'transmitter active' rule.  */ 
732                 if (ZS_IS_ASLEEP(uap))
733                         return;
734                 write_zsreg(uap, R5, uap->curregs[R5]);
735         }
736
737         spin_unlock_irqrestore(&port->lock, flags);
738 }
739
740 /*
741  * Turn power on or off to the SCC and associated stuff
742  * (port drivers, modem, IR port, etc.)
743  * Returns the number of milliseconds we should wait before
744  * trying to use the port.
745  */
746 static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
747 {
748         int delay = 0;
749         int rc;
750
751         if (state) {
752                 rc = pmac_call_feature(
753                         PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
754                 pmz_debug("port power on result: %d\n", rc);
755                 if (ZS_IS_INTMODEM(uap)) {
756                         rc = pmac_call_feature(
757                                 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
758                         delay = 2500;   /* wait for 2.5s before using */
759                         pmz_debug("modem power result: %d\n", rc);
760                 }
761         } else {
762                 /* TODO: Make that depend on a timer, don't power down
763                  * immediately
764                  */
765                 if (ZS_IS_INTMODEM(uap)) {
766                         rc = pmac_call_feature(
767                                 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
768                         pmz_debug("port power off result: %d\n", rc);
769                 }
770                 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
771         }
772         return delay;
773 }
774
775 /*
776  * FixZeroBug....Works around a bug in the SCC receving channel.
777  * Inspired from Darwin code, 15 Sept. 2000  -DanM
778  *
779  * The following sequence prevents a problem that is seen with O'Hare ASICs
780  * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
781  * at the input to the receiver becomes 'stuck' and locks up the receiver.
782  * This problem can occur as a result of a zero bit at the receiver input
783  * coincident with any of the following events:
784  *
785  *      The SCC is initialized (hardware or software).
786  *      A framing error is detected.
787  *      The clocking option changes from synchronous or X1 asynchronous
788  *              clocking to X16, X32, or X64 asynchronous clocking.
789  *      The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
790  *
791  * This workaround attempts to recover from the lockup condition by placing
792  * the SCC in synchronous loopback mode with a fast clock before programming
793  * any of the asynchronous modes.
794  */
795 static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
796 {
797         write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
798         zssync(uap);
799         udelay(10);
800         write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
801         zssync(uap);
802
803         write_zsreg(uap, 4, X1CLK | MONSYNC);
804         write_zsreg(uap, 3, Rx8);
805         write_zsreg(uap, 5, Tx8 | RTS);
806         write_zsreg(uap, 9, NV);        /* Didn't we already do this? */
807         write_zsreg(uap, 11, RCBR | TCBR);
808         write_zsreg(uap, 12, 0);
809         write_zsreg(uap, 13, 0);
810         write_zsreg(uap, 14, (LOOPBAK | BRSRC));
811         write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
812         write_zsreg(uap, 3, Rx8 | RxENABLE);
813         write_zsreg(uap, 0, RES_EXT_INT);
814         write_zsreg(uap, 0, RES_EXT_INT);
815         write_zsreg(uap, 0, RES_EXT_INT);       /* to kill some time */
816
817         /* The channel should be OK now, but it is probably receiving
818          * loopback garbage.
819          * Switch to asynchronous mode, disable the receiver,
820          * and discard everything in the receive buffer.
821          */
822         write_zsreg(uap, 9, NV);
823         write_zsreg(uap, 4, X16CLK | SB_MASK);
824         write_zsreg(uap, 3, Rx8);
825
826         while (read_zsreg(uap, 0) & Rx_CH_AV) {
827                 (void)read_zsreg(uap, 8);
828                 write_zsreg(uap, 0, RES_EXT_INT);
829                 write_zsreg(uap, 0, ERR_RES);
830         }
831 }
832
833 /*
834  * Real startup routine, powers up the hardware and sets up
835  * the SCC. Returns a delay in ms where you need to wait before
836  * actually using the port, this is typically the internal modem
837  * powerup delay. This routine expect the lock to be taken.
838  */
839 static int __pmz_startup(struct uart_pmac_port *uap)
840 {
841         int pwr_delay = 0;
842
843         memset(&uap->curregs, 0, sizeof(uap->curregs));
844
845         /* Power up the SCC & underlying hardware (modem/irda) */
846         pwr_delay = pmz_set_scc_power(uap, 1);
847
848         /* Nice buggy HW ... */
849         pmz_fix_zero_bug_scc(uap);
850
851         /* Reset the channel */
852         uap->curregs[R9] = 0;
853         write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
854         zssync(uap);
855         udelay(10);
856         write_zsreg(uap, 9, 0);
857         zssync(uap);
858
859         /* Clear the interrupt registers */
860         write_zsreg(uap, R1, 0);
861         write_zsreg(uap, R0, ERR_RES);
862         write_zsreg(uap, R0, ERR_RES);
863         write_zsreg(uap, R0, RES_H_IUS);
864         write_zsreg(uap, R0, RES_H_IUS);
865
866         /* Setup some valid baud rate */
867         uap->curregs[R4] = X16CLK | SB1;
868         uap->curregs[R3] = Rx8;
869         uap->curregs[R5] = Tx8 | RTS;
870         if (!ZS_IS_IRDA(uap))
871                 uap->curregs[R5] |= DTR;
872         uap->curregs[R12] = 0;
873         uap->curregs[R13] = 0;
874         uap->curregs[R14] = BRENAB;
875
876         /* Clear handshaking, enable BREAK interrupts */
877         uap->curregs[R15] = BRKIE;
878
879         /* Master interrupt enable */
880         uap->curregs[R9] |= NV | MIE;
881
882         pmz_load_zsregs(uap, uap->curregs);
883
884         /* Enable receiver and transmitter.  */
885         write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
886         write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
887
888         /* Remember status for DCD/CTS changes */
889         uap->prev_status = read_zsreg(uap, R0);
890
891
892         return pwr_delay;
893 }
894
895 static void pmz_irda_reset(struct uart_pmac_port *uap)
896 {
897         uap->curregs[R5] |= DTR;
898         write_zsreg(uap, R5, uap->curregs[R5]);
899         zssync(uap);
900         mdelay(110);
901         uap->curregs[R5] &= ~DTR;
902         write_zsreg(uap, R5, uap->curregs[R5]);
903         zssync(uap);
904         mdelay(10);
905 }
906
907 /*
908  * This is the "normal" startup routine, using the above one
909  * wrapped with the lock and doing a schedule delay
910  */
911 static int pmz_startup(struct uart_port *port)
912 {
913         struct uart_pmac_port *uap = to_pmz(port);
914         unsigned long flags;
915         int pwr_delay = 0;
916
917         pmz_debug("pmz: startup()\n");
918
919         if (ZS_IS_ASLEEP(uap))
920                 return -EAGAIN;
921         if (uap->node == NULL)
922                 return -ENODEV;
923
924         mutex_lock(&pmz_irq_mutex);
925
926         uap->flags |= PMACZILOG_FLAG_IS_OPEN;
927
928         /* A console is never powered down. Else, power up and
929          * initialize the chip
930          */
931         if (!ZS_IS_CONS(uap)) {
932                 spin_lock_irqsave(&port->lock, flags);
933                 pwr_delay = __pmz_startup(uap);
934                 spin_unlock_irqrestore(&port->lock, flags);
935         }       
936
937         pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
938         if (request_irq(uap->port.irq, pmz_interrupt, SA_SHIRQ, "PowerMac Zilog", uap)) {
939                 dev_err(&uap->dev->ofdev.dev,
940                         "Unable to register zs interrupt handler.\n");
941                 pmz_set_scc_power(uap, 0);
942                 mutex_unlock(&pmz_irq_mutex);
943                 return -ENXIO;
944         }
945
946         mutex_unlock(&pmz_irq_mutex);
947
948         /* Right now, we deal with delay by blocking here, I'll be
949          * smarter later on
950          */
951         if (pwr_delay != 0) {
952                 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
953                 msleep(pwr_delay);
954         }
955
956         /* IrDA reset is done now */
957         if (ZS_IS_IRDA(uap))
958                 pmz_irda_reset(uap);
959
960         /* Enable interrupts emission from the chip */
961         spin_lock_irqsave(&port->lock, flags);
962         uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
963         if (!ZS_IS_EXTCLK(uap))
964                 uap->curregs[R1] |= EXT_INT_ENAB;
965         write_zsreg(uap, R1, uap->curregs[R1]);
966         spin_unlock_irqrestore(&port->lock, flags);
967
968         pmz_debug("pmz: startup() done.\n");
969
970         return 0;
971 }
972
973 static void pmz_shutdown(struct uart_port *port)
974 {
975         struct uart_pmac_port *uap = to_pmz(port);
976         unsigned long flags;
977
978         pmz_debug("pmz: shutdown()\n");
979
980         if (uap->node == NULL)
981                 return;
982
983         mutex_lock(&pmz_irq_mutex);
984
985         /* Release interrupt handler */
986         free_irq(uap->port.irq, uap);
987
988         spin_lock_irqsave(&port->lock, flags);
989
990         uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
991
992         if (!ZS_IS_OPEN(uap->mate))
993                 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
994
995         /* Disable interrupts */
996         if (!ZS_IS_ASLEEP(uap)) {
997                 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
998                 write_zsreg(uap, R1, uap->curregs[R1]);
999                 zssync(uap);
1000         }
1001
1002         if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1003                 spin_unlock_irqrestore(&port->lock, flags);
1004                 mutex_unlock(&pmz_irq_mutex);
1005                 return;
1006         }
1007
1008         /* Disable receiver and transmitter.  */
1009         uap->curregs[R3] &= ~RxENABLE;
1010         uap->curregs[R5] &= ~TxENABLE;
1011
1012         /* Disable all interrupts and BRK assertion.  */
1013         uap->curregs[R5] &= ~SND_BRK;
1014         pmz_maybe_update_regs(uap);
1015
1016         /* Shut the chip down */
1017         pmz_set_scc_power(uap, 0);
1018
1019         spin_unlock_irqrestore(&port->lock, flags);
1020
1021         mutex_unlock(&pmz_irq_mutex);
1022
1023         pmz_debug("pmz: shutdown() done.\n");
1024 }
1025
1026 /* Shared by TTY driver and serial console setup.  The port lock is held
1027  * and local interrupts are disabled.
1028  */
1029 static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1030                               unsigned int iflag, unsigned long baud)
1031 {
1032         int brg;
1033
1034
1035         /* Switch to external clocking for IrDA high clock rates. That
1036          * code could be re-used for Midi interfaces with different
1037          * multipliers
1038          */
1039         if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1040                 uap->curregs[R4] = X1CLK;
1041                 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1042                 uap->curregs[R14] = 0; /* BRG off */
1043                 uap->curregs[R12] = 0;
1044                 uap->curregs[R13] = 0;
1045                 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1046         } else {
1047                 switch (baud) {
1048                 case ZS_CLOCK/16:       /* 230400 */
1049                         uap->curregs[R4] = X16CLK;
1050                         uap->curregs[R11] = 0;
1051                         uap->curregs[R14] = 0;
1052                         break;
1053                 case ZS_CLOCK/32:       /* 115200 */
1054                         uap->curregs[R4] = X32CLK;
1055                         uap->curregs[R11] = 0;
1056                         uap->curregs[R14] = 0;
1057                         break;
1058                 default:
1059                         uap->curregs[R4] = X16CLK;
1060                         uap->curregs[R11] = TCBR | RCBR;
1061                         brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1062                         uap->curregs[R12] = (brg & 255);
1063                         uap->curregs[R13] = ((brg >> 8) & 255);
1064                         uap->curregs[R14] = BRENAB;
1065                 }
1066                 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1067         }
1068
1069         /* Character size, stop bits, and parity. */
1070         uap->curregs[3] &= ~RxN_MASK;
1071         uap->curregs[5] &= ~TxN_MASK;
1072
1073         switch (cflag & CSIZE) {
1074         case CS5:
1075                 uap->curregs[3] |= Rx5;
1076                 uap->curregs[5] |= Tx5;
1077                 uap->parity_mask = 0x1f;
1078                 break;
1079         case CS6:
1080                 uap->curregs[3] |= Rx6;
1081                 uap->curregs[5] |= Tx6;
1082                 uap->parity_mask = 0x3f;
1083                 break;
1084         case CS7:
1085                 uap->curregs[3] |= Rx7;
1086                 uap->curregs[5] |= Tx7;
1087                 uap->parity_mask = 0x7f;
1088                 break;
1089         case CS8:
1090         default:
1091                 uap->curregs[3] |= Rx8;
1092                 uap->curregs[5] |= Tx8;
1093                 uap->parity_mask = 0xff;
1094                 break;
1095         };
1096         uap->curregs[4] &= ~(SB_MASK);
1097         if (cflag & CSTOPB)
1098                 uap->curregs[4] |= SB2;
1099         else
1100                 uap->curregs[4] |= SB1;
1101         if (cflag & PARENB)
1102                 uap->curregs[4] |= PAR_ENAB;
1103         else
1104                 uap->curregs[4] &= ~PAR_ENAB;
1105         if (!(cflag & PARODD))
1106                 uap->curregs[4] |= PAR_EVEN;
1107         else
1108                 uap->curregs[4] &= ~PAR_EVEN;
1109
1110         uap->port.read_status_mask = Rx_OVR;
1111         if (iflag & INPCK)
1112                 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1113         if (iflag & (BRKINT | PARMRK))
1114                 uap->port.read_status_mask |= BRK_ABRT;
1115
1116         uap->port.ignore_status_mask = 0;
1117         if (iflag & IGNPAR)
1118                 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1119         if (iflag & IGNBRK) {
1120                 uap->port.ignore_status_mask |= BRK_ABRT;
1121                 if (iflag & IGNPAR)
1122                         uap->port.ignore_status_mask |= Rx_OVR;
1123         }
1124
1125         if ((cflag & CREAD) == 0)
1126                 uap->port.ignore_status_mask = 0xff;
1127 }
1128
1129
1130 /*
1131  * Set the irda codec on the imac to the specified baud rate.
1132  */
1133 static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1134 {
1135         u8 cmdbyte;
1136         int t, version;
1137
1138         switch (*baud) {
1139         /* SIR modes */
1140         case 2400:
1141                 cmdbyte = 0x53;
1142                 break;
1143         case 4800:
1144                 cmdbyte = 0x52;
1145                 break;
1146         case 9600:
1147                 cmdbyte = 0x51;
1148                 break;
1149         case 19200:
1150                 cmdbyte = 0x50;
1151                 break;
1152         case 38400:
1153                 cmdbyte = 0x4f;
1154                 break;
1155         case 57600:
1156                 cmdbyte = 0x4e;
1157                 break;
1158         case 115200:
1159                 cmdbyte = 0x4d;
1160                 break;
1161         /* The FIR modes aren't really supported at this point, how
1162          * do we select the speed ? via the FCR on KeyLargo ?
1163          */
1164         case 1152000:
1165                 cmdbyte = 0;
1166                 break;
1167         case 4000000:
1168                 cmdbyte = 0;
1169                 break;
1170         default: /* 9600 */
1171                 cmdbyte = 0x51;
1172                 *baud = 9600;
1173                 break;
1174         }
1175
1176         /* Wait for transmitter to drain */
1177         t = 10000;
1178         while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1179                || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1180                 if (--t <= 0) {
1181                         dev_err(&uap->dev->ofdev.dev, "transmitter didn't drain\n");
1182                         return;
1183                 }
1184                 udelay(10);
1185         }
1186
1187         /* Drain the receiver too */
1188         t = 100;
1189         (void)read_zsdata(uap);
1190         (void)read_zsdata(uap);
1191         (void)read_zsdata(uap);
1192         mdelay(10);
1193         while (read_zsreg(uap, R0) & Rx_CH_AV) {
1194                 read_zsdata(uap);
1195                 mdelay(10);
1196                 if (--t <= 0) {
1197                         dev_err(&uap->dev->ofdev.dev, "receiver didn't drain\n");
1198                         return;
1199                 }
1200         }
1201
1202         /* Switch to command mode */
1203         uap->curregs[R5] |= DTR;
1204         write_zsreg(uap, R5, uap->curregs[R5]);
1205         zssync(uap);
1206         mdelay(1);
1207
1208         /* Switch SCC to 19200 */
1209         pmz_convert_to_zs(uap, CS8, 0, 19200);          
1210         pmz_load_zsregs(uap, uap->curregs);
1211         mdelay(1);
1212
1213         /* Write get_version command byte */
1214         write_zsdata(uap, 1);
1215         t = 5000;
1216         while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1217                 if (--t <= 0) {
1218                         dev_err(&uap->dev->ofdev.dev,
1219                                 "irda_setup timed out on get_version byte\n");
1220                         goto out;
1221                 }
1222                 udelay(10);
1223         }
1224         version = read_zsdata(uap);
1225
1226         if (version < 4) {
1227                 dev_info(&uap->dev->ofdev.dev, "IrDA: dongle version %d not supported\n",
1228                          version);
1229                 goto out;
1230         }
1231
1232         /* Send speed mode */
1233         write_zsdata(uap, cmdbyte);
1234         t = 5000;
1235         while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1236                 if (--t <= 0) {
1237                         dev_err(&uap->dev->ofdev.dev,
1238                                 "irda_setup timed out on speed mode byte\n");
1239                         goto out;
1240                 }
1241                 udelay(10);
1242         }
1243         t = read_zsdata(uap);
1244         if (t != cmdbyte)
1245                 dev_err(&uap->dev->ofdev.dev,
1246                         "irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1247
1248         dev_info(&uap->dev->ofdev.dev, "IrDA setup for %ld bps, dongle version: %d\n",
1249                  *baud, version);
1250
1251         (void)read_zsdata(uap);
1252         (void)read_zsdata(uap);
1253         (void)read_zsdata(uap);
1254
1255  out:
1256         /* Switch back to data mode */
1257         uap->curregs[R5] &= ~DTR;
1258         write_zsreg(uap, R5, uap->curregs[R5]);
1259         zssync(uap);
1260
1261         (void)read_zsdata(uap);
1262         (void)read_zsdata(uap);
1263         (void)read_zsdata(uap);
1264 }
1265
1266
1267 static void __pmz_set_termios(struct uart_port *port, struct termios *termios,
1268                               struct termios *old)
1269 {
1270         struct uart_pmac_port *uap = to_pmz(port);
1271         unsigned long baud;
1272
1273         pmz_debug("pmz: set_termios()\n");
1274
1275         if (ZS_IS_ASLEEP(uap))
1276                 return;
1277
1278         memcpy(&uap->termios_cache, termios, sizeof(struct termios));
1279
1280         /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1281          * on the IR dongle. Note that the IRTTY driver currently doesn't know
1282          * about the FIR mode and high speed modes. So these are unused. For
1283          * implementing proper support for these, we should probably add some
1284          * DMA as well, at least on the Rx side, which isn't a simple thing
1285          * at this point.
1286          */
1287         if (ZS_IS_IRDA(uap)) {
1288                 /* Calc baud rate */
1289                 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1290                 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1291                 /* Cet the irda codec to the right rate */
1292                 pmz_irda_setup(uap, &baud);
1293                 /* Set final baud rate */
1294                 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1295                 pmz_load_zsregs(uap, uap->curregs);
1296                 zssync(uap);
1297         } else {
1298                 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1299                 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1300                 /* Make sure modem status interrupts are correctly configured */
1301                 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1302                         uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1303                         uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1304                 } else {
1305                         uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1306                         uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1307                 }
1308
1309                 /* Load registers to the chip */
1310                 pmz_maybe_update_regs(uap);
1311         }
1312         uart_update_timeout(port, termios->c_cflag, baud);
1313
1314         pmz_debug("pmz: set_termios() done.\n");
1315 }
1316
1317 /* The port lock is not held.  */
1318 static void pmz_set_termios(struct uart_port *port, struct termios *termios,
1319                             struct termios *old)
1320 {
1321         struct uart_pmac_port *uap = to_pmz(port);
1322         unsigned long flags;
1323
1324         spin_lock_irqsave(&port->lock, flags);  
1325
1326         /* Disable IRQs on the port */
1327         uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1328         write_zsreg(uap, R1, uap->curregs[R1]);
1329
1330         /* Setup new port configuration */
1331         __pmz_set_termios(port, termios, old);
1332
1333         /* Re-enable IRQs on the port */
1334         if (ZS_IS_OPEN(uap)) {
1335                 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1336                 if (!ZS_IS_EXTCLK(uap))
1337                         uap->curregs[R1] |= EXT_INT_ENAB;
1338                 write_zsreg(uap, R1, uap->curregs[R1]);
1339         }
1340         spin_unlock_irqrestore(&port->lock, flags);
1341 }
1342
1343 static const char *pmz_type(struct uart_port *port)
1344 {
1345         struct uart_pmac_port *uap = to_pmz(port);
1346
1347         if (ZS_IS_IRDA(uap))
1348                 return "Z85c30 ESCC - Infrared port";
1349         else if (ZS_IS_INTMODEM(uap))
1350                 return "Z85c30 ESCC - Internal modem";
1351         return "Z85c30 ESCC - Serial port";
1352 }
1353
1354 /* We do not request/release mappings of the registers here, this
1355  * happens at early serial probe time.
1356  */
1357 static void pmz_release_port(struct uart_port *port)
1358 {
1359 }
1360
1361 static int pmz_request_port(struct uart_port *port)
1362 {
1363         return 0;
1364 }
1365
1366 /* These do not need to do anything interesting either.  */
1367 static void pmz_config_port(struct uart_port *port, int flags)
1368 {
1369 }
1370
1371 /* We do not support letting the user mess with the divisor, IRQ, etc. */
1372 static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1373 {
1374         return -EINVAL;
1375 }
1376
1377 static struct uart_ops pmz_pops = {
1378         .tx_empty       =       pmz_tx_empty,
1379         .set_mctrl      =       pmz_set_mctrl,
1380         .get_mctrl      =       pmz_get_mctrl,
1381         .stop_tx        =       pmz_stop_tx,
1382         .start_tx       =       pmz_start_tx,
1383         .stop_rx        =       pmz_stop_rx,
1384         .enable_ms      =       pmz_enable_ms,
1385         .break_ctl      =       pmz_break_ctl,
1386         .startup        =       pmz_startup,
1387         .shutdown       =       pmz_shutdown,
1388         .set_termios    =       pmz_set_termios,
1389         .type           =       pmz_type,
1390         .release_port   =       pmz_release_port,
1391         .request_port   =       pmz_request_port,
1392         .config_port    =       pmz_config_port,
1393         .verify_port    =       pmz_verify_port,
1394 };
1395
1396 /*
1397  * Setup one port structure after probing, HW is down at this point,
1398  * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1399  * register our console before uart_add_one_port() is called
1400  */
1401 static int __init pmz_init_port(struct uart_pmac_port *uap)
1402 {
1403         struct device_node *np = uap->node;
1404         char *conn;
1405         struct slot_names_prop {
1406                 int     count;
1407                 char    name[1];
1408         } *slots;
1409         int len;
1410         struct resource r_ports, r_rxdma, r_txdma;
1411
1412         /*
1413          * Request & map chip registers
1414          */
1415         if (of_address_to_resource(np, 0, &r_ports))
1416                 return -ENODEV;
1417         uap->port.mapbase = r_ports.start;
1418         uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1419       
1420         uap->control_reg = uap->port.membase;
1421         uap->data_reg = uap->control_reg + 0x10;
1422         
1423         /*
1424          * Request & map DBDMA registers
1425          */
1426 #ifdef HAS_DBDMA
1427         if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1428             of_address_to_resource(np, 2, &r_rxdma) == 0)
1429                 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1430 #else
1431         memset(&r_txdma, 0, sizeof(struct resource));
1432         memset(&r_rxdma, 0, sizeof(struct resource));
1433 #endif  
1434         if (ZS_HAS_DMA(uap)) {
1435                 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
1436                 if (uap->tx_dma_regs == NULL) { 
1437                         uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1438                         goto no_dma;
1439                 }
1440                 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
1441                 if (uap->rx_dma_regs == NULL) { 
1442                         iounmap(uap->tx_dma_regs);
1443                         uap->tx_dma_regs = NULL;
1444                         uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1445                         goto no_dma;
1446                 }
1447                 uap->tx_dma_irq = np->intrs[1].line;
1448                 uap->rx_dma_irq = np->intrs[2].line;
1449         }
1450 no_dma:
1451
1452         /*
1453          * Detect port type
1454          */
1455         if (device_is_compatible(np, "cobalt"))
1456                 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1457         conn = get_property(np, "AAPL,connector", &len);
1458         if (conn && (strcmp(conn, "infrared") == 0))
1459                 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1460         uap->port_type = PMAC_SCC_ASYNC;
1461         /* 1999 Powerbook G3 has slot-names property instead */
1462         slots = (struct slot_names_prop *)get_property(np, "slot-names", &len);
1463         if (slots && slots->count > 0) {
1464                 if (strcmp(slots->name, "IrDA") == 0)
1465                         uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1466                 else if (strcmp(slots->name, "Modem") == 0)
1467                         uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1468         }
1469         if (ZS_IS_IRDA(uap))
1470                 uap->port_type = PMAC_SCC_IRDA;
1471         if (ZS_IS_INTMODEM(uap)) {
1472                 struct device_node* i2c_modem = find_devices("i2c-modem");
1473                 if (i2c_modem) {
1474                         char* mid = get_property(i2c_modem, "modem-id", NULL);
1475                         if (mid) switch(*mid) {
1476                         case 0x04 :
1477                         case 0x05 :
1478                         case 0x07 :
1479                         case 0x08 :
1480                         case 0x0b :
1481                         case 0x0c :
1482                                 uap->port_type = PMAC_SCC_I2S1;
1483                         }
1484                         printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1485                                 mid ? (*mid) : 0);
1486                 } else {
1487                         printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1488                 }
1489         }
1490
1491         /*
1492          * Init remaining bits of "port" structure
1493          */
1494         uap->port.iotype = UPIO_MEM;
1495         uap->port.irq = np->intrs[0].line;
1496         uap->port.uartclk = ZS_CLOCK;
1497         uap->port.fifosize = 1;
1498         uap->port.ops = &pmz_pops;
1499         uap->port.type = PORT_PMAC_ZILOG;
1500         uap->port.flags = 0;
1501
1502         /* Setup some valid baud rate information in the register
1503          * shadows so we don't write crap there before baud rate is
1504          * first initialized.
1505          */
1506         pmz_convert_to_zs(uap, CS8, 0, 9600);
1507
1508         return 0;
1509 }
1510
1511 /*
1512  * Get rid of a port on module removal
1513  */
1514 static void pmz_dispose_port(struct uart_pmac_port *uap)
1515 {
1516         struct device_node *np;
1517
1518         np = uap->node;
1519         iounmap(uap->rx_dma_regs);
1520         iounmap(uap->tx_dma_regs);
1521         iounmap(uap->control_reg);
1522         uap->node = NULL;
1523         of_node_put(np);
1524         memset(uap, 0, sizeof(struct uart_pmac_port));
1525 }
1526
1527 /*
1528  * Called upon match with an escc node in the devive-tree.
1529  */
1530 static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
1531 {
1532         int i;
1533         
1534         /* Iterate the pmz_ports array to find a matching entry
1535          */
1536         for (i = 0; i < MAX_ZS_PORTS; i++)
1537                 if (pmz_ports[i].node == mdev->ofdev.node) {
1538                         struct uart_pmac_port *uap = &pmz_ports[i];
1539
1540                         uap->dev = mdev;
1541                         dev_set_drvdata(&mdev->ofdev.dev, uap);
1542                         if (macio_request_resources(uap->dev, "pmac_zilog"))
1543                                 printk(KERN_WARNING "%s: Failed to request resource"
1544                                        ", port still active\n",
1545                                        uap->node->name);
1546                         else
1547                                 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;                            
1548                         return 0;
1549                 }
1550         return -ENODEV;
1551 }
1552
1553 /*
1554  * That one should not be called, macio isn't really a hotswap device,
1555  * we don't expect one of those serial ports to go away...
1556  */
1557 static int pmz_detach(struct macio_dev *mdev)
1558 {
1559         struct uart_pmac_port   *uap = dev_get_drvdata(&mdev->ofdev.dev);
1560         
1561         if (!uap)
1562                 return -ENODEV;
1563
1564         if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1565                 macio_release_resources(uap->dev);
1566                 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1567         }
1568         dev_set_drvdata(&mdev->ofdev.dev, NULL);
1569         uap->dev = NULL;
1570         
1571         return 0;
1572 }
1573
1574
1575 static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
1576 {
1577         struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1578         struct uart_state *state;
1579         unsigned long flags;
1580
1581         if (uap == NULL) {
1582                 printk("HRM... pmz_suspend with NULL uap\n");
1583                 return 0;
1584         }
1585
1586         if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
1587                 return 0;
1588
1589         pmz_debug("suspend, switching to state %d\n", pm_state);
1590
1591         state = pmz_uart_reg.state + uap->port.line;
1592
1593         mutex_lock(&pmz_irq_mutex);
1594         mutex_lock(&state->mutex);
1595
1596         spin_lock_irqsave(&uap->port.lock, flags);
1597
1598         if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1599                 /* Disable receiver and transmitter.  */
1600                 uap->curregs[R3] &= ~RxENABLE;
1601                 uap->curregs[R5] &= ~TxENABLE;
1602
1603                 /* Disable all interrupts and BRK assertion.  */
1604                 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1605                 uap->curregs[R5] &= ~SND_BRK;
1606                 pmz_load_zsregs(uap, uap->curregs);
1607                 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1608                 mb();
1609         }
1610
1611         spin_unlock_irqrestore(&uap->port.lock, flags);
1612
1613         if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1614                 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1615                         pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1616                         disable_irq(uap->port.irq);
1617                 }
1618
1619         if (ZS_IS_CONS(uap))
1620                 uap->port.cons->flags &= ~CON_ENABLED;
1621
1622         /* Shut the chip down */
1623         pmz_set_scc_power(uap, 0);
1624
1625         mutex_unlock(&state->mutex);
1626         mutex_unlock(&pmz_irq_mutex);
1627
1628         pmz_debug("suspend, switching complete\n");
1629
1630         mdev->ofdev.dev.power.power_state = pm_state;
1631
1632         return 0;
1633 }
1634
1635
1636 static int pmz_resume(struct macio_dev *mdev)
1637 {
1638         struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1639         struct uart_state *state;
1640         unsigned long flags;
1641         int pwr_delay = 0;
1642
1643         if (uap == NULL)
1644                 return 0;
1645
1646         if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
1647                 return 0;
1648         
1649         pmz_debug("resume, switching to state 0\n");
1650
1651         state = pmz_uart_reg.state + uap->port.line;
1652
1653         mutex_lock(&pmz_irq_mutex);
1654         mutex_lock(&state->mutex);
1655
1656         spin_lock_irqsave(&uap->port.lock, flags);
1657         if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1658                 spin_unlock_irqrestore(&uap->port.lock, flags);
1659                 goto bail;
1660         }
1661         pwr_delay = __pmz_startup(uap);
1662
1663         /* Take care of config that may have changed while asleep */
1664         __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1665
1666         if (ZS_IS_OPEN(uap)) {
1667                 /* Enable interrupts */         
1668                 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1669                 if (!ZS_IS_EXTCLK(uap))
1670                         uap->curregs[R1] |= EXT_INT_ENAB;
1671                 write_zsreg(uap, R1, uap->curregs[R1]);
1672         }
1673
1674         spin_unlock_irqrestore(&uap->port.lock, flags);
1675
1676         if (ZS_IS_CONS(uap))
1677                 uap->port.cons->flags |= CON_ENABLED;
1678
1679         /* Re-enable IRQ on the controller */
1680         if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1681                 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1682                 enable_irq(uap->port.irq);
1683         }
1684
1685  bail:
1686         mutex_unlock(&state->mutex);
1687         mutex_unlock(&pmz_irq_mutex);
1688
1689         /* Right now, we deal with delay by blocking here, I'll be
1690          * smarter later on
1691          */
1692         if (pwr_delay != 0) {
1693                 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1694                 msleep(pwr_delay);
1695         }
1696
1697         pmz_debug("resume, switching complete\n");
1698
1699         mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
1700
1701         return 0;
1702 }
1703
1704 /*
1705  * Probe all ports in the system and build the ports array, we register
1706  * with the serial layer at this point, the macio-type probing is only
1707  * used later to "attach" to the sysfs tree so we get power management
1708  * events
1709  */
1710 static int __init pmz_probe(void)
1711 {
1712         struct device_node      *node_p, *node_a, *node_b, *np;
1713         int                     count = 0;
1714         int                     rc;
1715
1716         /*
1717          * Find all escc chips in the system
1718          */
1719         node_p = of_find_node_by_name(NULL, "escc");
1720         while (node_p) {
1721                 /*
1722                  * First get channel A/B node pointers
1723                  * 
1724                  * TODO: Add routines with proper locking to do that...
1725                  */
1726                 node_a = node_b = NULL;
1727                 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1728                         if (strncmp(np->name, "ch-a", 4) == 0)
1729                                 node_a = of_node_get(np);
1730                         else if (strncmp(np->name, "ch-b", 4) == 0)
1731                                 node_b = of_node_get(np);
1732                 }
1733                 if (!node_a && !node_b) {
1734                         of_node_put(node_a);
1735                         of_node_put(node_b);
1736                         printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1737                                 (!node_a) ? 'a' : 'b', node_p->full_name);
1738                         goto next;
1739                 }
1740
1741                 /*
1742                  * Fill basic fields in the port structures
1743                  */
1744                 pmz_ports[count].mate           = &pmz_ports[count+1];
1745                 pmz_ports[count+1].mate         = &pmz_ports[count];
1746                 pmz_ports[count].flags          = PMACZILOG_FLAG_IS_CHANNEL_A;
1747                 pmz_ports[count].node           = node_a;
1748                 pmz_ports[count+1].node         = node_b;
1749                 pmz_ports[count].port.line      = count;
1750                 pmz_ports[count+1].port.line    = count+1;
1751
1752                 /*
1753                  * Setup the ports for real
1754                  */
1755                 rc = pmz_init_port(&pmz_ports[count]);
1756                 if (rc == 0 && node_b != NULL)
1757                         rc = pmz_init_port(&pmz_ports[count+1]);
1758                 if (rc != 0) {
1759                         of_node_put(node_a);
1760                         of_node_put(node_b);
1761                         memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1762                         memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1763                         goto next;
1764                 }
1765                 count += 2;
1766 next:
1767                 node_p = of_find_node_by_name(node_p, "escc");
1768         }
1769         pmz_ports_count = count;
1770
1771         return 0;
1772 }
1773
1774 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1775
1776 static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1777 static int __init pmz_console_setup(struct console *co, char *options);
1778
1779 static struct console pmz_console = {
1780         .name   =       "ttyS",
1781         .write  =       pmz_console_write,
1782         .device =       uart_console_device,
1783         .setup  =       pmz_console_setup,
1784         .flags  =       CON_PRINTBUFFER,
1785         .index  =       -1,
1786         .data   =       &pmz_uart_reg,
1787 };
1788
1789 #define PMACZILOG_CONSOLE       &pmz_console
1790 #else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1791 #define PMACZILOG_CONSOLE       (NULL)
1792 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1793
1794 /*
1795  * Register the driver, console driver and ports with the serial
1796  * core
1797  */
1798 static int __init pmz_register(void)
1799 {
1800         int i, rc;
1801         
1802         pmz_uart_reg.nr = pmz_ports_count;
1803         pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1804         pmz_uart_reg.minor = 64;
1805
1806         /*
1807          * Register this driver with the serial core
1808          */
1809         rc = uart_register_driver(&pmz_uart_reg);
1810         if (rc)
1811                 return rc;
1812
1813         /*
1814          * Register each port with the serial core
1815          */
1816         for (i = 0; i < pmz_ports_count; i++) {
1817                 struct uart_pmac_port *uport = &pmz_ports[i];
1818                 /* NULL node may happen on wallstreet */
1819                 if (uport->node != NULL)
1820                         rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1821                 if (rc)
1822                         goto err_out;
1823         }
1824
1825         return 0;
1826 err_out:
1827         while (i-- > 0) {
1828                 struct uart_pmac_port *uport = &pmz_ports[i];
1829                 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1830         }
1831         uart_unregister_driver(&pmz_uart_reg);
1832         return rc;
1833 }
1834
1835 static struct of_device_id pmz_match[] = 
1836 {
1837         {
1838         .name           = "ch-a",
1839         },
1840         {
1841         .name           = "ch-b",
1842         },
1843         {},
1844 };
1845 MODULE_DEVICE_TABLE (of, pmz_match);
1846
1847 static struct macio_driver pmz_driver = 
1848 {
1849         .name           = "pmac_zilog",
1850         .match_table    = pmz_match,
1851         .probe          = pmz_attach,
1852         .remove         = pmz_detach,
1853         .suspend        = pmz_suspend,
1854         .resume         = pmz_resume,
1855 };
1856
1857 static int __init init_pmz(void)
1858 {
1859         int rc, i;
1860         printk(KERN_INFO "%s\n", version);
1861
1862         /* 
1863          * First, we need to do a direct OF-based probe pass. We
1864          * do that because we want serial console up before the
1865          * macio stuffs calls us back, and since that makes it
1866          * easier to pass the proper number of channels to
1867          * uart_register_driver()
1868          */
1869         if (pmz_ports_count == 0)
1870                 pmz_probe();
1871
1872         /*
1873          * Bail early if no port found
1874          */
1875         if (pmz_ports_count == 0)
1876                 return -ENODEV;
1877
1878         /*
1879          * Now we register with the serial layer
1880          */
1881         rc = pmz_register();
1882         if (rc) {
1883                 printk(KERN_ERR 
1884                         "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
1885                         "pmac_zilog: Did another serial driver already claim the minors?\n"); 
1886                 /* effectively "pmz_unprobe()" */
1887                 for (i=0; i < pmz_ports_count; i++)
1888                         pmz_dispose_port(&pmz_ports[i]);
1889                 return rc;
1890         }
1891         
1892         /*
1893          * Then we register the macio driver itself
1894          */
1895         return macio_register_driver(&pmz_driver);
1896 }
1897
1898 static void __exit exit_pmz(void)
1899 {
1900         int i;
1901
1902         /* Get rid of macio-driver (detach from macio) */
1903         macio_unregister_driver(&pmz_driver);
1904
1905         for (i = 0; i < pmz_ports_count; i++) {
1906                 struct uart_pmac_port *uport = &pmz_ports[i];
1907                 if (uport->node != NULL) {
1908                         uart_remove_one_port(&pmz_uart_reg, &uport->port);
1909                         pmz_dispose_port(uport);
1910                 }
1911         }
1912         /* Unregister UART driver */
1913         uart_unregister_driver(&pmz_uart_reg);
1914 }
1915
1916 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1917
1918 static void pmz_console_putchar(struct uart_port *port, int ch)
1919 {
1920         struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1921
1922         /* Wait for the transmit buffer to empty. */
1923         while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1924                 udelay(5);
1925         write_zsdata(uap, ch);
1926 }
1927
1928 /*
1929  * Print a string to the serial port trying not to disturb
1930  * any possible real use of the port...
1931  */
1932 static void pmz_console_write(struct console *con, const char *s, unsigned int count)
1933 {
1934         struct uart_pmac_port *uap = &pmz_ports[con->index];
1935         unsigned long flags;
1936
1937         if (ZS_IS_ASLEEP(uap))
1938                 return;
1939         spin_lock_irqsave(&uap->port.lock, flags);
1940
1941         /* Turn of interrupts and enable the transmitter. */
1942         write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
1943         write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
1944
1945         uart_console_write(&uap->port, s, count, pmz_console_putchar);
1946
1947         /* Restore the values in the registers. */
1948         write_zsreg(uap, R1, uap->curregs[1]);
1949         /* Don't disable the transmitter. */
1950
1951         spin_unlock_irqrestore(&uap->port.lock, flags);
1952 }
1953
1954 /*
1955  * Setup the serial console
1956  */
1957 static int __init pmz_console_setup(struct console *co, char *options)
1958 {
1959         struct uart_pmac_port *uap;
1960         struct uart_port *port;
1961         int baud = 38400;
1962         int bits = 8;
1963         int parity = 'n';
1964         int flow = 'n';
1965         unsigned long pwr_delay;
1966
1967         /*
1968          * XServe's default to 57600 bps
1969          */
1970         if (machine_is_compatible("RackMac1,1")
1971             || machine_is_compatible("RackMac1,2")
1972             || machine_is_compatible("MacRISC4"))
1973                 baud = 57600;
1974
1975         /*
1976          * Check whether an invalid uart number has been specified, and
1977          * if so, search for the first available port that does have
1978          * console support.
1979          */
1980         if (co->index >= pmz_ports_count)
1981                 co->index = 0;
1982         uap = &pmz_ports[co->index];
1983         if (uap->node == NULL)
1984                 return -ENODEV;
1985         port = &uap->port;
1986
1987         /*
1988          * Mark port as beeing a console
1989          */
1990         uap->flags |= PMACZILOG_FLAG_IS_CONS;
1991
1992         /*
1993          * Temporary fix for uart layer who didn't setup the spinlock yet
1994          */
1995         spin_lock_init(&port->lock);
1996
1997         /*
1998          * Enable the hardware
1999          */
2000         pwr_delay = __pmz_startup(uap);
2001         if (pwr_delay)
2002                 mdelay(pwr_delay);
2003         
2004         if (options)
2005                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2006
2007         return uart_set_options(port, co, baud, parity, bits, flow);
2008 }
2009
2010 static int __init pmz_console_init(void)
2011 {
2012         /* Probe ports */
2013         pmz_probe();
2014
2015         /* TODO: Autoprobe console based on OF */
2016         /* pmz_console.index = i; */
2017         register_console(&pmz_console);
2018
2019         return 0;
2020
2021 }
2022 console_initcall(pmz_console_init);
2023 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2024
2025 module_init(init_pmz);
2026 module_exit(exit_pmz);