cgroups: fix lockdep subclasses overflow
[linux-2.6/mini2440.git] / drivers / serial / pmac_zilog.c
blobad3488504010f2ebe66055fa3878eae07fcacabb
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)
9 * Derived from drivers/macintosh/macserial.c by Paul Mackerras
10 * and drivers/serial/sunzilog.c by David S. Miller
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...
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.
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.
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
32 * 2004-08-06 Harald Welte <laforge@gnumonks.org>
33 * - Enable BREAK interrupt
34 * - Add support for sysreq
36 * TODO: - Add DMA support
37 * - Defer port shutdown to a few seconds after close
38 * - maybe put something right into uap->clk_divisor
41 #undef DEBUG
42 #undef DEBUG_HARD
43 #undef USE_CTRL_O_SYSRQ
45 #include <linux/module.h>
46 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/major.h>
50 #include <linux/string.h>
51 #include <linux/fcntl.h>
52 #include <linux/mm.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/console.h>
57 #include <linux/slab.h>
58 #include <linux/adb.h>
59 #include <linux/pmu.h>
60 #include <linux/bitops.h>
61 #include <linux/sysrq.h>
62 #include <linux/mutex.h>
63 #include <asm/sections.h>
64 #include <asm/io.h>
65 #include <asm/irq.h>
66 #include <asm/prom.h>
67 #include <asm/machdep.h>
68 #include <asm/pmac_feature.h>
69 #include <asm/dbdma.h>
70 #include <asm/macio.h>
72 #if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
73 #define SUPPORT_SYSRQ
74 #endif
76 #include <linux/serial.h>
77 #include <linux/serial_core.h>
79 #include "pmac_zilog.h"
81 /* Not yet implemented */
82 #undef HAS_DBDMA
84 static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
85 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
86 MODULE_DESCRIPTION("Driver for the PowerMac serial ports.");
87 MODULE_LICENSE("GPL");
89 #define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
91 #ifdef CONFIG_SERIAL_PMACZILOG_TTYS
92 #define PMACZILOG_MAJOR TTY_MAJOR
93 #define PMACZILOG_MINOR 64
94 #define PMACZILOG_NAME "ttyS"
95 #else
96 #define PMACZILOG_MAJOR 204
97 #define PMACZILOG_MINOR 192
98 #define PMACZILOG_NAME "ttyPZ"
99 #endif
103 * For the sake of early serial console, we can do a pre-probe
104 * (optional) of the ports at rather early boot time.
106 static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
107 static int pmz_ports_count;
108 static DEFINE_MUTEX(pmz_irq_mutex);
110 static struct uart_driver pmz_uart_reg = {
111 .owner = THIS_MODULE,
112 .driver_name = PMACZILOG_NAME,
113 .dev_name = PMACZILOG_NAME,
114 .major = PMACZILOG_MAJOR,
115 .minor = PMACZILOG_MINOR,
120 * Load all registers to reprogram the port
121 * This function must only be called when the TX is not busy. The UART
122 * port lock must be held and local interrupts disabled.
124 static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
126 int i;
128 if (ZS_IS_ASLEEP(uap))
129 return;
131 /* Let pending transmits finish. */
132 for (i = 0; i < 1000; i++) {
133 unsigned char stat = read_zsreg(uap, R1);
134 if (stat & ALL_SNT)
135 break;
136 udelay(100);
139 ZS_CLEARERR(uap);
140 zssync(uap);
141 ZS_CLEARFIFO(uap);
142 zssync(uap);
143 ZS_CLEARERR(uap);
145 /* Disable all interrupts. */
146 write_zsreg(uap, R1,
147 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
149 /* Set parity, sync config, stop bits, and clock divisor. */
150 write_zsreg(uap, R4, regs[R4]);
152 /* Set misc. TX/RX control bits. */
153 write_zsreg(uap, R10, regs[R10]);
155 /* Set TX/RX controls sans the enable bits. */
156 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
157 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
159 /* now set R7 "prime" on ESCC */
160 write_zsreg(uap, R15, regs[R15] | EN85C30);
161 write_zsreg(uap, R7, regs[R7P]);
163 /* make sure we use R7 "non-prime" on ESCC */
164 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
166 /* Synchronous mode config. */
167 write_zsreg(uap, R6, regs[R6]);
168 write_zsreg(uap, R7, regs[R7]);
170 /* Disable baud generator. */
171 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
173 /* Clock mode control. */
174 write_zsreg(uap, R11, regs[R11]);
176 /* Lower and upper byte of baud rate generator divisor. */
177 write_zsreg(uap, R12, regs[R12]);
178 write_zsreg(uap, R13, regs[R13]);
180 /* Now rewrite R14, with BRENAB (if set). */
181 write_zsreg(uap, R14, regs[R14]);
183 /* Reset external status interrupts. */
184 write_zsreg(uap, R0, RES_EXT_INT);
185 write_zsreg(uap, R0, RES_EXT_INT);
187 /* Rewrite R3/R5, this time without enables masked. */
188 write_zsreg(uap, R3, regs[R3]);
189 write_zsreg(uap, R5, regs[R5]);
191 /* Rewrite R1, this time without IRQ enabled masked. */
192 write_zsreg(uap, R1, regs[R1]);
194 /* Enable interrupts */
195 write_zsreg(uap, R9, regs[R9]);
199 * We do like sunzilog to avoid disrupting pending Tx
200 * Reprogram the Zilog channel HW registers with the copies found in the
201 * software state struct. If the transmitter is busy, we defer this update
202 * until the next TX complete interrupt. Else, we do it right now.
204 * The UART port lock must be held and local interrupts disabled.
206 static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
208 if (!ZS_REGS_HELD(uap)) {
209 if (ZS_TX_ACTIVE(uap)) {
210 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
211 } else {
212 pmz_debug("pmz: maybe_update_regs: updating\n");
213 pmz_load_zsregs(uap, uap->curregs);
218 static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
220 struct tty_struct *tty = NULL;
221 unsigned char ch, r1, drop, error, flag;
222 int loops = 0;
224 /* The interrupt can be enabled when the port isn't open, typically
225 * that happens when using one port is open and the other closed (stale
226 * interrupt) or when one port is used as a console.
228 if (!ZS_IS_OPEN(uap)) {
229 pmz_debug("pmz: draining input\n");
230 /* Port is closed, drain input data */
231 for (;;) {
232 if ((++loops) > 1000)
233 goto flood;
234 (void)read_zsreg(uap, R1);
235 write_zsreg(uap, R0, ERR_RES);
236 (void)read_zsdata(uap);
237 ch = read_zsreg(uap, R0);
238 if (!(ch & Rx_CH_AV))
239 break;
241 return NULL;
244 /* Sanity check, make sure the old bug is no longer happening */
245 if (uap->port.info == NULL || uap->port.info->port.tty == NULL) {
246 WARN_ON(1);
247 (void)read_zsdata(uap);
248 return NULL;
250 tty = uap->port.info->port.tty;
252 while (1) {
253 error = 0;
254 drop = 0;
256 r1 = read_zsreg(uap, R1);
257 ch = read_zsdata(uap);
259 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
260 write_zsreg(uap, R0, ERR_RES);
261 zssync(uap);
264 ch &= uap->parity_mask;
265 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
266 uap->flags &= ~PMACZILOG_FLAG_BREAK;
269 #if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
270 #ifdef USE_CTRL_O_SYSRQ
271 /* Handle the SysRq ^O Hack */
272 if (ch == '\x0f') {
273 uap->port.sysrq = jiffies + HZ*5;
274 goto next_char;
276 #endif /* USE_CTRL_O_SYSRQ */
277 if (uap->port.sysrq) {
278 int swallow;
279 spin_unlock(&uap->port.lock);
280 swallow = uart_handle_sysrq_char(&uap->port, ch);
281 spin_lock(&uap->port.lock);
282 if (swallow)
283 goto next_char;
285 #endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
287 /* A real serial line, record the character and status. */
288 if (drop)
289 goto next_char;
291 flag = TTY_NORMAL;
292 uap->port.icount.rx++;
294 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
295 error = 1;
296 if (r1 & BRK_ABRT) {
297 pmz_debug("pmz: got break !\n");
298 r1 &= ~(PAR_ERR | CRC_ERR);
299 uap->port.icount.brk++;
300 if (uart_handle_break(&uap->port))
301 goto next_char;
303 else if (r1 & PAR_ERR)
304 uap->port.icount.parity++;
305 else if (r1 & CRC_ERR)
306 uap->port.icount.frame++;
307 if (r1 & Rx_OVR)
308 uap->port.icount.overrun++;
309 r1 &= uap->port.read_status_mask;
310 if (r1 & BRK_ABRT)
311 flag = TTY_BREAK;
312 else if (r1 & PAR_ERR)
313 flag = TTY_PARITY;
314 else if (r1 & CRC_ERR)
315 flag = TTY_FRAME;
318 if (uap->port.ignore_status_mask == 0xff ||
319 (r1 & uap->port.ignore_status_mask) == 0) {
320 tty_insert_flip_char(tty, ch, flag);
322 if (r1 & Rx_OVR)
323 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
324 next_char:
325 /* We can get stuck in an infinite loop getting char 0 when the
326 * line is in a wrong HW state, we break that here.
327 * When that happens, I disable the receive side of the driver.
328 * Note that what I've been experiencing is a real irq loop where
329 * I'm getting flooded regardless of the actual port speed.
330 * Something stange is going on with the HW
332 if ((++loops) > 1000)
333 goto flood;
334 ch = read_zsreg(uap, R0);
335 if (!(ch & Rx_CH_AV))
336 break;
339 return tty;
340 flood:
341 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
342 write_zsreg(uap, R1, uap->curregs[R1]);
343 zssync(uap);
344 dev_err(&uap->dev->ofdev.dev, "pmz: rx irq flood !\n");
345 return tty;
348 static void pmz_status_handle(struct uart_pmac_port *uap)
350 unsigned char status;
352 status = read_zsreg(uap, R0);
353 write_zsreg(uap, R0, RES_EXT_INT);
354 zssync(uap);
356 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
357 if (status & SYNC_HUNT)
358 uap->port.icount.dsr++;
360 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
361 * But it does not tell us which bit has changed, we have to keep
362 * track of this ourselves.
363 * The CTS input is inverted for some reason. -- paulus
365 if ((status ^ uap->prev_status) & DCD)
366 uart_handle_dcd_change(&uap->port,
367 (status & DCD));
368 if ((status ^ uap->prev_status) & CTS)
369 uart_handle_cts_change(&uap->port,
370 !(status & CTS));
372 wake_up_interruptible(&uap->port.info->delta_msr_wait);
375 if (status & BRK_ABRT)
376 uap->flags |= PMACZILOG_FLAG_BREAK;
378 uap->prev_status = status;
381 static void pmz_transmit_chars(struct uart_pmac_port *uap)
383 struct circ_buf *xmit;
385 if (ZS_IS_ASLEEP(uap))
386 return;
387 if (ZS_IS_CONS(uap)) {
388 unsigned char status = read_zsreg(uap, R0);
390 /* TX still busy? Just wait for the next TX done interrupt.
392 * It can occur because of how we do serial console writes. It would
393 * be nice to transmit console writes just like we normally would for
394 * a TTY line. (ie. buffered and TX interrupt driven). That is not
395 * easy because console writes cannot sleep. One solution might be
396 * to poll on enough port->xmit space becomming free. -DaveM
398 if (!(status & Tx_BUF_EMP))
399 return;
402 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
404 if (ZS_REGS_HELD(uap)) {
405 pmz_load_zsregs(uap, uap->curregs);
406 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
409 if (ZS_TX_STOPPED(uap)) {
410 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
411 goto ack_tx_int;
414 if (uap->port.x_char) {
415 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
416 write_zsdata(uap, uap->port.x_char);
417 zssync(uap);
418 uap->port.icount.tx++;
419 uap->port.x_char = 0;
420 return;
423 if (uap->port.info == NULL)
424 goto ack_tx_int;
425 xmit = &uap->port.info->xmit;
426 if (uart_circ_empty(xmit)) {
427 uart_write_wakeup(&uap->port);
428 goto ack_tx_int;
430 if (uart_tx_stopped(&uap->port))
431 goto ack_tx_int;
433 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
434 write_zsdata(uap, xmit->buf[xmit->tail]);
435 zssync(uap);
437 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
438 uap->port.icount.tx++;
440 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
441 uart_write_wakeup(&uap->port);
443 return;
445 ack_tx_int:
446 write_zsreg(uap, R0, RES_Tx_P);
447 zssync(uap);
450 /* Hrm... we register that twice, fixme later.... */
451 static irqreturn_t pmz_interrupt(int irq, void *dev_id)
453 struct uart_pmac_port *uap = dev_id;
454 struct uart_pmac_port *uap_a;
455 struct uart_pmac_port *uap_b;
456 int rc = IRQ_NONE;
457 struct tty_struct *tty;
458 u8 r3;
460 uap_a = pmz_get_port_A(uap);
461 uap_b = uap_a->mate;
463 spin_lock(&uap_a->port.lock);
464 r3 = read_zsreg(uap_a, R3);
466 #ifdef DEBUG_HARD
467 pmz_debug("irq, r3: %x\n", r3);
468 #endif
469 /* Channel A */
470 tty = NULL;
471 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
472 write_zsreg(uap_a, R0, RES_H_IUS);
473 zssync(uap_a);
474 if (r3 & CHAEXT)
475 pmz_status_handle(uap_a);
476 if (r3 & CHARxIP)
477 tty = pmz_receive_chars(uap_a);
478 if (r3 & CHATxIP)
479 pmz_transmit_chars(uap_a);
480 rc = IRQ_HANDLED;
482 spin_unlock(&uap_a->port.lock);
483 if (tty != NULL)
484 tty_flip_buffer_push(tty);
486 if (uap_b->node == NULL)
487 goto out;
489 spin_lock(&uap_b->port.lock);
490 tty = NULL;
491 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
492 write_zsreg(uap_b, R0, RES_H_IUS);
493 zssync(uap_b);
494 if (r3 & CHBEXT)
495 pmz_status_handle(uap_b);
496 if (r3 & CHBRxIP)
497 tty = pmz_receive_chars(uap_b);
498 if (r3 & CHBTxIP)
499 pmz_transmit_chars(uap_b);
500 rc = IRQ_HANDLED;
502 spin_unlock(&uap_b->port.lock);
503 if (tty != NULL)
504 tty_flip_buffer_push(tty);
506 out:
507 #ifdef DEBUG_HARD
508 pmz_debug("irq done.\n");
509 #endif
510 return rc;
514 * Peek the status register, lock not held by caller
516 static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
518 unsigned long flags;
519 u8 status;
521 spin_lock_irqsave(&uap->port.lock, flags);
522 status = read_zsreg(uap, R0);
523 spin_unlock_irqrestore(&uap->port.lock, flags);
525 return status;
529 * Check if transmitter is empty
530 * The port lock is not held.
532 static unsigned int pmz_tx_empty(struct uart_port *port)
534 struct uart_pmac_port *uap = to_pmz(port);
535 unsigned char status;
537 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
538 return TIOCSER_TEMT;
540 status = pmz_peek_status(to_pmz(port));
541 if (status & Tx_BUF_EMP)
542 return TIOCSER_TEMT;
543 return 0;
547 * Set Modem Control (RTS & DTR) bits
548 * The port lock is held and interrupts are disabled.
549 * Note: Shall we really filter out RTS on external ports or
550 * should that be dealt at higher level only ?
552 static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
554 struct uart_pmac_port *uap = to_pmz(port);
555 unsigned char set_bits, clear_bits;
557 /* Do nothing for irda for now... */
558 if (ZS_IS_IRDA(uap))
559 return;
560 /* We get called during boot with a port not up yet */
561 if (ZS_IS_ASLEEP(uap) ||
562 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
563 return;
565 set_bits = clear_bits = 0;
567 if (ZS_IS_INTMODEM(uap)) {
568 if (mctrl & TIOCM_RTS)
569 set_bits |= RTS;
570 else
571 clear_bits |= RTS;
573 if (mctrl & TIOCM_DTR)
574 set_bits |= DTR;
575 else
576 clear_bits |= DTR;
578 /* NOTE: Not subject to 'transmitter active' rule. */
579 uap->curregs[R5] |= set_bits;
580 uap->curregs[R5] &= ~clear_bits;
581 if (ZS_IS_ASLEEP(uap))
582 return;
583 write_zsreg(uap, R5, uap->curregs[R5]);
584 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
585 set_bits, clear_bits, uap->curregs[R5]);
586 zssync(uap);
590 * Get Modem Control bits (only the input ones, the core will
591 * or that with a cached value of the control ones)
592 * The port lock is held and interrupts are disabled.
594 static unsigned int pmz_get_mctrl(struct uart_port *port)
596 struct uart_pmac_port *uap = to_pmz(port);
597 unsigned char status;
598 unsigned int ret;
600 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
601 return 0;
603 status = read_zsreg(uap, R0);
605 ret = 0;
606 if (status & DCD)
607 ret |= TIOCM_CAR;
608 if (status & SYNC_HUNT)
609 ret |= TIOCM_DSR;
610 if (!(status & CTS))
611 ret |= TIOCM_CTS;
613 return ret;
617 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
618 * though for DMA, we will have to do a bit more.
619 * The port lock is held and interrupts are disabled.
621 static void pmz_stop_tx(struct uart_port *port)
623 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
627 * Kick the Tx side.
628 * The port lock is held and interrupts are disabled.
630 static void pmz_start_tx(struct uart_port *port)
632 struct uart_pmac_port *uap = to_pmz(port);
633 unsigned char status;
635 pmz_debug("pmz: start_tx()\n");
637 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
638 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
640 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
641 return;
643 status = read_zsreg(uap, R0);
645 /* TX busy? Just wait for the TX done interrupt. */
646 if (!(status & Tx_BUF_EMP))
647 return;
649 /* Send the first character to jump-start the TX done
650 * IRQ sending engine.
652 if (port->x_char) {
653 write_zsdata(uap, port->x_char);
654 zssync(uap);
655 port->icount.tx++;
656 port->x_char = 0;
657 } else {
658 struct circ_buf *xmit = &port->info->xmit;
660 write_zsdata(uap, xmit->buf[xmit->tail]);
661 zssync(uap);
662 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
663 port->icount.tx++;
665 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
666 uart_write_wakeup(&uap->port);
668 pmz_debug("pmz: start_tx() done.\n");
672 * Stop Rx side, basically disable emitting of
673 * Rx interrupts on the port. We don't disable the rx
674 * side of the chip proper though
675 * The port lock is held.
677 static void pmz_stop_rx(struct uart_port *port)
679 struct uart_pmac_port *uap = to_pmz(port);
681 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
682 return;
684 pmz_debug("pmz: stop_rx()()\n");
686 /* Disable all RX interrupts. */
687 uap->curregs[R1] &= ~RxINT_MASK;
688 pmz_maybe_update_regs(uap);
690 pmz_debug("pmz: stop_rx() done.\n");
694 * Enable modem status change interrupts
695 * The port lock is held.
697 static void pmz_enable_ms(struct uart_port *port)
699 struct uart_pmac_port *uap = to_pmz(port);
700 unsigned char new_reg;
702 if (ZS_IS_IRDA(uap) || uap->node == NULL)
703 return;
704 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
705 if (new_reg != uap->curregs[R15]) {
706 uap->curregs[R15] = new_reg;
708 if (ZS_IS_ASLEEP(uap))
709 return;
710 /* NOTE: Not subject to 'transmitter active' rule. */
711 write_zsreg(uap, R15, uap->curregs[R15]);
716 * Control break state emission
717 * The port lock is not held.
719 static void pmz_break_ctl(struct uart_port *port, int break_state)
721 struct uart_pmac_port *uap = to_pmz(port);
722 unsigned char set_bits, clear_bits, new_reg;
723 unsigned long flags;
725 if (uap->node == NULL)
726 return;
727 set_bits = clear_bits = 0;
729 if (break_state)
730 set_bits |= SND_BRK;
731 else
732 clear_bits |= SND_BRK;
734 spin_lock_irqsave(&port->lock, flags);
736 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
737 if (new_reg != uap->curregs[R5]) {
738 uap->curregs[R5] = new_reg;
740 /* NOTE: Not subject to 'transmitter active' rule. */
741 if (ZS_IS_ASLEEP(uap))
742 return;
743 write_zsreg(uap, R5, uap->curregs[R5]);
746 spin_unlock_irqrestore(&port->lock, flags);
750 * Turn power on or off to the SCC and associated stuff
751 * (port drivers, modem, IR port, etc.)
752 * Returns the number of milliseconds we should wait before
753 * trying to use the port.
755 static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
757 int delay = 0;
758 int rc;
760 if (state) {
761 rc = pmac_call_feature(
762 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
763 pmz_debug("port power on result: %d\n", rc);
764 if (ZS_IS_INTMODEM(uap)) {
765 rc = pmac_call_feature(
766 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
767 delay = 2500; /* wait for 2.5s before using */
768 pmz_debug("modem power result: %d\n", rc);
770 } else {
771 /* TODO: Make that depend on a timer, don't power down
772 * immediately
774 if (ZS_IS_INTMODEM(uap)) {
775 rc = pmac_call_feature(
776 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
777 pmz_debug("port power off result: %d\n", rc);
779 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
781 return delay;
785 * FixZeroBug....Works around a bug in the SCC receving channel.
786 * Inspired from Darwin code, 15 Sept. 2000 -DanM
788 * The following sequence prevents a problem that is seen with O'Hare ASICs
789 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
790 * at the input to the receiver becomes 'stuck' and locks up the receiver.
791 * This problem can occur as a result of a zero bit at the receiver input
792 * coincident with any of the following events:
794 * The SCC is initialized (hardware or software).
795 * A framing error is detected.
796 * The clocking option changes from synchronous or X1 asynchronous
797 * clocking to X16, X32, or X64 asynchronous clocking.
798 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
800 * This workaround attempts to recover from the lockup condition by placing
801 * the SCC in synchronous loopback mode with a fast clock before programming
802 * any of the asynchronous modes.
804 static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
806 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
807 zssync(uap);
808 udelay(10);
809 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
810 zssync(uap);
812 write_zsreg(uap, 4, X1CLK | MONSYNC);
813 write_zsreg(uap, 3, Rx8);
814 write_zsreg(uap, 5, Tx8 | RTS);
815 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
816 write_zsreg(uap, 11, RCBR | TCBR);
817 write_zsreg(uap, 12, 0);
818 write_zsreg(uap, 13, 0);
819 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
820 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
821 write_zsreg(uap, 3, Rx8 | RxENABLE);
822 write_zsreg(uap, 0, RES_EXT_INT);
823 write_zsreg(uap, 0, RES_EXT_INT);
824 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
826 /* The channel should be OK now, but it is probably receiving
827 * loopback garbage.
828 * Switch to asynchronous mode, disable the receiver,
829 * and discard everything in the receive buffer.
831 write_zsreg(uap, 9, NV);
832 write_zsreg(uap, 4, X16CLK | SB_MASK);
833 write_zsreg(uap, 3, Rx8);
835 while (read_zsreg(uap, 0) & Rx_CH_AV) {
836 (void)read_zsreg(uap, 8);
837 write_zsreg(uap, 0, RES_EXT_INT);
838 write_zsreg(uap, 0, ERR_RES);
843 * Real startup routine, powers up the hardware and sets up
844 * the SCC. Returns a delay in ms where you need to wait before
845 * actually using the port, this is typically the internal modem
846 * powerup delay. This routine expect the lock to be taken.
848 static int __pmz_startup(struct uart_pmac_port *uap)
850 int pwr_delay = 0;
852 memset(&uap->curregs, 0, sizeof(uap->curregs));
854 /* Power up the SCC & underlying hardware (modem/irda) */
855 pwr_delay = pmz_set_scc_power(uap, 1);
857 /* Nice buggy HW ... */
858 pmz_fix_zero_bug_scc(uap);
860 /* Reset the channel */
861 uap->curregs[R9] = 0;
862 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
863 zssync(uap);
864 udelay(10);
865 write_zsreg(uap, 9, 0);
866 zssync(uap);
868 /* Clear the interrupt registers */
869 write_zsreg(uap, R1, 0);
870 write_zsreg(uap, R0, ERR_RES);
871 write_zsreg(uap, R0, ERR_RES);
872 write_zsreg(uap, R0, RES_H_IUS);
873 write_zsreg(uap, R0, RES_H_IUS);
875 /* Setup some valid baud rate */
876 uap->curregs[R4] = X16CLK | SB1;
877 uap->curregs[R3] = Rx8;
878 uap->curregs[R5] = Tx8 | RTS;
879 if (!ZS_IS_IRDA(uap))
880 uap->curregs[R5] |= DTR;
881 uap->curregs[R12] = 0;
882 uap->curregs[R13] = 0;
883 uap->curregs[R14] = BRENAB;
885 /* Clear handshaking, enable BREAK interrupts */
886 uap->curregs[R15] = BRKIE;
888 /* Master interrupt enable */
889 uap->curregs[R9] |= NV | MIE;
891 pmz_load_zsregs(uap, uap->curregs);
893 /* Enable receiver and transmitter. */
894 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
895 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
897 /* Remember status for DCD/CTS changes */
898 uap->prev_status = read_zsreg(uap, R0);
901 return pwr_delay;
904 static void pmz_irda_reset(struct uart_pmac_port *uap)
906 uap->curregs[R5] |= DTR;
907 write_zsreg(uap, R5, uap->curregs[R5]);
908 zssync(uap);
909 mdelay(110);
910 uap->curregs[R5] &= ~DTR;
911 write_zsreg(uap, R5, uap->curregs[R5]);
912 zssync(uap);
913 mdelay(10);
917 * This is the "normal" startup routine, using the above one
918 * wrapped with the lock and doing a schedule delay
920 static int pmz_startup(struct uart_port *port)
922 struct uart_pmac_port *uap = to_pmz(port);
923 unsigned long flags;
924 int pwr_delay = 0;
926 pmz_debug("pmz: startup()\n");
928 if (ZS_IS_ASLEEP(uap))
929 return -EAGAIN;
930 if (uap->node == NULL)
931 return -ENODEV;
933 mutex_lock(&pmz_irq_mutex);
935 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
937 /* A console is never powered down. Else, power up and
938 * initialize the chip
940 if (!ZS_IS_CONS(uap)) {
941 spin_lock_irqsave(&port->lock, flags);
942 pwr_delay = __pmz_startup(uap);
943 spin_unlock_irqrestore(&port->lock, flags);
946 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
947 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED, "PowerMac Zilog", uap)) {
948 dev_err(&uap->dev->ofdev.dev,
949 "Unable to register zs interrupt handler.\n");
950 pmz_set_scc_power(uap, 0);
951 mutex_unlock(&pmz_irq_mutex);
952 return -ENXIO;
955 mutex_unlock(&pmz_irq_mutex);
957 /* Right now, we deal with delay by blocking here, I'll be
958 * smarter later on
960 if (pwr_delay != 0) {
961 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
962 msleep(pwr_delay);
965 /* IrDA reset is done now */
966 if (ZS_IS_IRDA(uap))
967 pmz_irda_reset(uap);
969 /* Enable interrupts emission from the chip */
970 spin_lock_irqsave(&port->lock, flags);
971 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
972 if (!ZS_IS_EXTCLK(uap))
973 uap->curregs[R1] |= EXT_INT_ENAB;
974 write_zsreg(uap, R1, uap->curregs[R1]);
975 spin_unlock_irqrestore(&port->lock, flags);
977 pmz_debug("pmz: startup() done.\n");
979 return 0;
982 static void pmz_shutdown(struct uart_port *port)
984 struct uart_pmac_port *uap = to_pmz(port);
985 unsigned long flags;
987 pmz_debug("pmz: shutdown()\n");
989 if (uap->node == NULL)
990 return;
992 mutex_lock(&pmz_irq_mutex);
994 /* Release interrupt handler */
995 free_irq(uap->port.irq, uap);
997 spin_lock_irqsave(&port->lock, flags);
999 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1001 if (!ZS_IS_OPEN(uap->mate))
1002 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1004 /* Disable interrupts */
1005 if (!ZS_IS_ASLEEP(uap)) {
1006 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1007 write_zsreg(uap, R1, uap->curregs[R1]);
1008 zssync(uap);
1011 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1012 spin_unlock_irqrestore(&port->lock, flags);
1013 mutex_unlock(&pmz_irq_mutex);
1014 return;
1017 /* Disable receiver and transmitter. */
1018 uap->curregs[R3] &= ~RxENABLE;
1019 uap->curregs[R5] &= ~TxENABLE;
1021 /* Disable all interrupts and BRK assertion. */
1022 uap->curregs[R5] &= ~SND_BRK;
1023 pmz_maybe_update_regs(uap);
1025 /* Shut the chip down */
1026 pmz_set_scc_power(uap, 0);
1028 spin_unlock_irqrestore(&port->lock, flags);
1030 mutex_unlock(&pmz_irq_mutex);
1032 pmz_debug("pmz: shutdown() done.\n");
1035 /* Shared by TTY driver and serial console setup. The port lock is held
1036 * and local interrupts are disabled.
1038 static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1039 unsigned int iflag, unsigned long baud)
1041 int brg;
1044 /* Switch to external clocking for IrDA high clock rates. That
1045 * code could be re-used for Midi interfaces with different
1046 * multipliers
1048 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1049 uap->curregs[R4] = X1CLK;
1050 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1051 uap->curregs[R14] = 0; /* BRG off */
1052 uap->curregs[R12] = 0;
1053 uap->curregs[R13] = 0;
1054 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1055 } else {
1056 switch (baud) {
1057 case ZS_CLOCK/16: /* 230400 */
1058 uap->curregs[R4] = X16CLK;
1059 uap->curregs[R11] = 0;
1060 uap->curregs[R14] = 0;
1061 break;
1062 case ZS_CLOCK/32: /* 115200 */
1063 uap->curregs[R4] = X32CLK;
1064 uap->curregs[R11] = 0;
1065 uap->curregs[R14] = 0;
1066 break;
1067 default:
1068 uap->curregs[R4] = X16CLK;
1069 uap->curregs[R11] = TCBR | RCBR;
1070 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1071 uap->curregs[R12] = (brg & 255);
1072 uap->curregs[R13] = ((brg >> 8) & 255);
1073 uap->curregs[R14] = BRENAB;
1075 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1078 /* Character size, stop bits, and parity. */
1079 uap->curregs[3] &= ~RxN_MASK;
1080 uap->curregs[5] &= ~TxN_MASK;
1082 switch (cflag & CSIZE) {
1083 case CS5:
1084 uap->curregs[3] |= Rx5;
1085 uap->curregs[5] |= Tx5;
1086 uap->parity_mask = 0x1f;
1087 break;
1088 case CS6:
1089 uap->curregs[3] |= Rx6;
1090 uap->curregs[5] |= Tx6;
1091 uap->parity_mask = 0x3f;
1092 break;
1093 case CS7:
1094 uap->curregs[3] |= Rx7;
1095 uap->curregs[5] |= Tx7;
1096 uap->parity_mask = 0x7f;
1097 break;
1098 case CS8:
1099 default:
1100 uap->curregs[3] |= Rx8;
1101 uap->curregs[5] |= Tx8;
1102 uap->parity_mask = 0xff;
1103 break;
1105 uap->curregs[4] &= ~(SB_MASK);
1106 if (cflag & CSTOPB)
1107 uap->curregs[4] |= SB2;
1108 else
1109 uap->curregs[4] |= SB1;
1110 if (cflag & PARENB)
1111 uap->curregs[4] |= PAR_ENAB;
1112 else
1113 uap->curregs[4] &= ~PAR_ENAB;
1114 if (!(cflag & PARODD))
1115 uap->curregs[4] |= PAR_EVEN;
1116 else
1117 uap->curregs[4] &= ~PAR_EVEN;
1119 uap->port.read_status_mask = Rx_OVR;
1120 if (iflag & INPCK)
1121 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1122 if (iflag & (BRKINT | PARMRK))
1123 uap->port.read_status_mask |= BRK_ABRT;
1125 uap->port.ignore_status_mask = 0;
1126 if (iflag & IGNPAR)
1127 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1128 if (iflag & IGNBRK) {
1129 uap->port.ignore_status_mask |= BRK_ABRT;
1130 if (iflag & IGNPAR)
1131 uap->port.ignore_status_mask |= Rx_OVR;
1134 if ((cflag & CREAD) == 0)
1135 uap->port.ignore_status_mask = 0xff;
1140 * Set the irda codec on the imac to the specified baud rate.
1142 static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1144 u8 cmdbyte;
1145 int t, version;
1147 switch (*baud) {
1148 /* SIR modes */
1149 case 2400:
1150 cmdbyte = 0x53;
1151 break;
1152 case 4800:
1153 cmdbyte = 0x52;
1154 break;
1155 case 9600:
1156 cmdbyte = 0x51;
1157 break;
1158 case 19200:
1159 cmdbyte = 0x50;
1160 break;
1161 case 38400:
1162 cmdbyte = 0x4f;
1163 break;
1164 case 57600:
1165 cmdbyte = 0x4e;
1166 break;
1167 case 115200:
1168 cmdbyte = 0x4d;
1169 break;
1170 /* The FIR modes aren't really supported at this point, how
1171 * do we select the speed ? via the FCR on KeyLargo ?
1173 case 1152000:
1174 cmdbyte = 0;
1175 break;
1176 case 4000000:
1177 cmdbyte = 0;
1178 break;
1179 default: /* 9600 */
1180 cmdbyte = 0x51;
1181 *baud = 9600;
1182 break;
1185 /* Wait for transmitter to drain */
1186 t = 10000;
1187 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1188 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1189 if (--t <= 0) {
1190 dev_err(&uap->dev->ofdev.dev, "transmitter didn't drain\n");
1191 return;
1193 udelay(10);
1196 /* Drain the receiver too */
1197 t = 100;
1198 (void)read_zsdata(uap);
1199 (void)read_zsdata(uap);
1200 (void)read_zsdata(uap);
1201 mdelay(10);
1202 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1203 read_zsdata(uap);
1204 mdelay(10);
1205 if (--t <= 0) {
1206 dev_err(&uap->dev->ofdev.dev, "receiver didn't drain\n");
1207 return;
1211 /* Switch to command mode */
1212 uap->curregs[R5] |= DTR;
1213 write_zsreg(uap, R5, uap->curregs[R5]);
1214 zssync(uap);
1215 mdelay(1);
1217 /* Switch SCC to 19200 */
1218 pmz_convert_to_zs(uap, CS8, 0, 19200);
1219 pmz_load_zsregs(uap, uap->curregs);
1220 mdelay(1);
1222 /* Write get_version command byte */
1223 write_zsdata(uap, 1);
1224 t = 5000;
1225 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1226 if (--t <= 0) {
1227 dev_err(&uap->dev->ofdev.dev,
1228 "irda_setup timed out on get_version byte\n");
1229 goto out;
1231 udelay(10);
1233 version = read_zsdata(uap);
1235 if (version < 4) {
1236 dev_info(&uap->dev->ofdev.dev, "IrDA: dongle version %d not supported\n",
1237 version);
1238 goto out;
1241 /* Send speed mode */
1242 write_zsdata(uap, cmdbyte);
1243 t = 5000;
1244 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1245 if (--t <= 0) {
1246 dev_err(&uap->dev->ofdev.dev,
1247 "irda_setup timed out on speed mode byte\n");
1248 goto out;
1250 udelay(10);
1252 t = read_zsdata(uap);
1253 if (t != cmdbyte)
1254 dev_err(&uap->dev->ofdev.dev,
1255 "irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1257 dev_info(&uap->dev->ofdev.dev, "IrDA setup for %ld bps, dongle version: %d\n",
1258 *baud, version);
1260 (void)read_zsdata(uap);
1261 (void)read_zsdata(uap);
1262 (void)read_zsdata(uap);
1264 out:
1265 /* Switch back to data mode */
1266 uap->curregs[R5] &= ~DTR;
1267 write_zsreg(uap, R5, uap->curregs[R5]);
1268 zssync(uap);
1270 (void)read_zsdata(uap);
1271 (void)read_zsdata(uap);
1272 (void)read_zsdata(uap);
1276 static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1277 struct ktermios *old)
1279 struct uart_pmac_port *uap = to_pmz(port);
1280 unsigned long baud;
1282 pmz_debug("pmz: set_termios()\n");
1284 if (ZS_IS_ASLEEP(uap))
1285 return;
1287 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
1289 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1290 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1291 * about the FIR mode and high speed modes. So these are unused. For
1292 * implementing proper support for these, we should probably add some
1293 * DMA as well, at least on the Rx side, which isn't a simple thing
1294 * at this point.
1296 if (ZS_IS_IRDA(uap)) {
1297 /* Calc baud rate */
1298 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1299 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1300 /* Cet the irda codec to the right rate */
1301 pmz_irda_setup(uap, &baud);
1302 /* Set final baud rate */
1303 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1304 pmz_load_zsregs(uap, uap->curregs);
1305 zssync(uap);
1306 } else {
1307 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1308 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1309 /* Make sure modem status interrupts are correctly configured */
1310 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1311 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1312 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1313 } else {
1314 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1315 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1318 /* Load registers to the chip */
1319 pmz_maybe_update_regs(uap);
1321 uart_update_timeout(port, termios->c_cflag, baud);
1323 pmz_debug("pmz: set_termios() done.\n");
1326 /* The port lock is not held. */
1327 static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1328 struct ktermios *old)
1330 struct uart_pmac_port *uap = to_pmz(port);
1331 unsigned long flags;
1333 spin_lock_irqsave(&port->lock, flags);
1335 /* Disable IRQs on the port */
1336 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1337 write_zsreg(uap, R1, uap->curregs[R1]);
1339 /* Setup new port configuration */
1340 __pmz_set_termios(port, termios, old);
1342 /* Re-enable IRQs on the port */
1343 if (ZS_IS_OPEN(uap)) {
1344 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1345 if (!ZS_IS_EXTCLK(uap))
1346 uap->curregs[R1] |= EXT_INT_ENAB;
1347 write_zsreg(uap, R1, uap->curregs[R1]);
1349 spin_unlock_irqrestore(&port->lock, flags);
1352 static const char *pmz_type(struct uart_port *port)
1354 struct uart_pmac_port *uap = to_pmz(port);
1356 if (ZS_IS_IRDA(uap))
1357 return "Z85c30 ESCC - Infrared port";
1358 else if (ZS_IS_INTMODEM(uap))
1359 return "Z85c30 ESCC - Internal modem";
1360 return "Z85c30 ESCC - Serial port";
1363 /* We do not request/release mappings of the registers here, this
1364 * happens at early serial probe time.
1366 static void pmz_release_port(struct uart_port *port)
1370 static int pmz_request_port(struct uart_port *port)
1372 return 0;
1375 /* These do not need to do anything interesting either. */
1376 static void pmz_config_port(struct uart_port *port, int flags)
1380 /* We do not support letting the user mess with the divisor, IRQ, etc. */
1381 static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1383 return -EINVAL;
1386 #ifdef CONFIG_CONSOLE_POLL
1388 static int pmz_poll_get_char(struct uart_port *port)
1390 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1392 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1393 udelay(5);
1394 return read_zsdata(uap);
1397 static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1399 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1401 /* Wait for the transmit buffer to empty. */
1402 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1403 udelay(5);
1404 write_zsdata(uap, c);
1407 #endif
1409 static struct uart_ops pmz_pops = {
1410 .tx_empty = pmz_tx_empty,
1411 .set_mctrl = pmz_set_mctrl,
1412 .get_mctrl = pmz_get_mctrl,
1413 .stop_tx = pmz_stop_tx,
1414 .start_tx = pmz_start_tx,
1415 .stop_rx = pmz_stop_rx,
1416 .enable_ms = pmz_enable_ms,
1417 .break_ctl = pmz_break_ctl,
1418 .startup = pmz_startup,
1419 .shutdown = pmz_shutdown,
1420 .set_termios = pmz_set_termios,
1421 .type = pmz_type,
1422 .release_port = pmz_release_port,
1423 .request_port = pmz_request_port,
1424 .config_port = pmz_config_port,
1425 .verify_port = pmz_verify_port,
1426 #ifdef CONFIG_CONSOLE_POLL
1427 .poll_get_char = pmz_poll_get_char,
1428 .poll_put_char = pmz_poll_put_char,
1429 #endif
1433 * Setup one port structure after probing, HW is down at this point,
1434 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1435 * register our console before uart_add_one_port() is called
1437 static int __init pmz_init_port(struct uart_pmac_port *uap)
1439 struct device_node *np = uap->node;
1440 const char *conn;
1441 const struct slot_names_prop {
1442 int count;
1443 char name[1];
1444 } *slots;
1445 int len;
1446 struct resource r_ports, r_rxdma, r_txdma;
1449 * Request & map chip registers
1451 if (of_address_to_resource(np, 0, &r_ports))
1452 return -ENODEV;
1453 uap->port.mapbase = r_ports.start;
1454 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1456 uap->control_reg = uap->port.membase;
1457 uap->data_reg = uap->control_reg + 0x10;
1460 * Request & map DBDMA registers
1462 #ifdef HAS_DBDMA
1463 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1464 of_address_to_resource(np, 2, &r_rxdma) == 0)
1465 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1466 #else
1467 memset(&r_txdma, 0, sizeof(struct resource));
1468 memset(&r_rxdma, 0, sizeof(struct resource));
1469 #endif
1470 if (ZS_HAS_DMA(uap)) {
1471 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
1472 if (uap->tx_dma_regs == NULL) {
1473 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1474 goto no_dma;
1476 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
1477 if (uap->rx_dma_regs == NULL) {
1478 iounmap(uap->tx_dma_regs);
1479 uap->tx_dma_regs = NULL;
1480 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1481 goto no_dma;
1483 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1484 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
1486 no_dma:
1489 * Detect port type
1491 if (of_device_is_compatible(np, "cobalt"))
1492 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1493 conn = of_get_property(np, "AAPL,connector", &len);
1494 if (conn && (strcmp(conn, "infrared") == 0))
1495 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1496 uap->port_type = PMAC_SCC_ASYNC;
1497 /* 1999 Powerbook G3 has slot-names property instead */
1498 slots = of_get_property(np, "slot-names", &len);
1499 if (slots && slots->count > 0) {
1500 if (strcmp(slots->name, "IrDA") == 0)
1501 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1502 else if (strcmp(slots->name, "Modem") == 0)
1503 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1505 if (ZS_IS_IRDA(uap))
1506 uap->port_type = PMAC_SCC_IRDA;
1507 if (ZS_IS_INTMODEM(uap)) {
1508 struct device_node* i2c_modem =
1509 of_find_node_by_name(NULL, "i2c-modem");
1510 if (i2c_modem) {
1511 const char* mid =
1512 of_get_property(i2c_modem, "modem-id", NULL);
1513 if (mid) switch(*mid) {
1514 case 0x04 :
1515 case 0x05 :
1516 case 0x07 :
1517 case 0x08 :
1518 case 0x0b :
1519 case 0x0c :
1520 uap->port_type = PMAC_SCC_I2S1;
1522 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1523 mid ? (*mid) : 0);
1524 of_node_put(i2c_modem);
1525 } else {
1526 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1531 * Init remaining bits of "port" structure
1533 uap->port.iotype = UPIO_MEM;
1534 uap->port.irq = irq_of_parse_and_map(np, 0);
1535 uap->port.uartclk = ZS_CLOCK;
1536 uap->port.fifosize = 1;
1537 uap->port.ops = &pmz_pops;
1538 uap->port.type = PORT_PMAC_ZILOG;
1539 uap->port.flags = 0;
1541 /* Setup some valid baud rate information in the register
1542 * shadows so we don't write crap there before baud rate is
1543 * first initialized.
1545 pmz_convert_to_zs(uap, CS8, 0, 9600);
1547 return 0;
1551 * Get rid of a port on module removal
1553 static void pmz_dispose_port(struct uart_pmac_port *uap)
1555 struct device_node *np;
1557 np = uap->node;
1558 iounmap(uap->rx_dma_regs);
1559 iounmap(uap->tx_dma_regs);
1560 iounmap(uap->control_reg);
1561 uap->node = NULL;
1562 of_node_put(np);
1563 memset(uap, 0, sizeof(struct uart_pmac_port));
1567 * Called upon match with an escc node in the devive-tree.
1569 static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
1571 int i;
1573 /* Iterate the pmz_ports array to find a matching entry
1575 for (i = 0; i < MAX_ZS_PORTS; i++)
1576 if (pmz_ports[i].node == mdev->ofdev.node) {
1577 struct uart_pmac_port *uap = &pmz_ports[i];
1579 uap->dev = mdev;
1580 dev_set_drvdata(&mdev->ofdev.dev, uap);
1581 if (macio_request_resources(uap->dev, "pmac_zilog"))
1582 printk(KERN_WARNING "%s: Failed to request resource"
1583 ", port still active\n",
1584 uap->node->name);
1585 else
1586 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1587 return 0;
1589 return -ENODEV;
1593 * That one should not be called, macio isn't really a hotswap device,
1594 * we don't expect one of those serial ports to go away...
1596 static int pmz_detach(struct macio_dev *mdev)
1598 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1600 if (!uap)
1601 return -ENODEV;
1603 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1604 macio_release_resources(uap->dev);
1605 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1607 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1608 uap->dev = NULL;
1610 return 0;
1614 static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
1616 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1617 struct uart_state *state;
1618 unsigned long flags;
1620 if (uap == NULL) {
1621 printk("HRM... pmz_suspend with NULL uap\n");
1622 return 0;
1625 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
1626 return 0;
1628 pmz_debug("suspend, switching to state %d\n", pm_state.event);
1630 state = pmz_uart_reg.state + uap->port.line;
1632 mutex_lock(&pmz_irq_mutex);
1633 mutex_lock(&state->mutex);
1635 spin_lock_irqsave(&uap->port.lock, flags);
1637 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1638 /* Disable receiver and transmitter. */
1639 uap->curregs[R3] &= ~RxENABLE;
1640 uap->curregs[R5] &= ~TxENABLE;
1642 /* Disable all interrupts and BRK assertion. */
1643 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1644 uap->curregs[R5] &= ~SND_BRK;
1645 pmz_load_zsregs(uap, uap->curregs);
1646 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1647 mb();
1650 spin_unlock_irqrestore(&uap->port.lock, flags);
1652 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1653 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1654 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1655 disable_irq(uap->port.irq);
1658 if (ZS_IS_CONS(uap))
1659 uap->port.cons->flags &= ~CON_ENABLED;
1661 /* Shut the chip down */
1662 pmz_set_scc_power(uap, 0);
1664 mutex_unlock(&state->mutex);
1665 mutex_unlock(&pmz_irq_mutex);
1667 pmz_debug("suspend, switching complete\n");
1669 mdev->ofdev.dev.power.power_state = pm_state;
1671 return 0;
1675 static int pmz_resume(struct macio_dev *mdev)
1677 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1678 struct uart_state *state;
1679 unsigned long flags;
1680 int pwr_delay = 0;
1682 if (uap == NULL)
1683 return 0;
1685 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
1686 return 0;
1688 pmz_debug("resume, switching to state 0\n");
1690 state = pmz_uart_reg.state + uap->port.line;
1692 mutex_lock(&pmz_irq_mutex);
1693 mutex_lock(&state->mutex);
1695 spin_lock_irqsave(&uap->port.lock, flags);
1696 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1697 spin_unlock_irqrestore(&uap->port.lock, flags);
1698 goto bail;
1700 pwr_delay = __pmz_startup(uap);
1702 /* Take care of config that may have changed while asleep */
1703 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1705 if (ZS_IS_OPEN(uap)) {
1706 /* Enable interrupts */
1707 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1708 if (!ZS_IS_EXTCLK(uap))
1709 uap->curregs[R1] |= EXT_INT_ENAB;
1710 write_zsreg(uap, R1, uap->curregs[R1]);
1713 spin_unlock_irqrestore(&uap->port.lock, flags);
1715 if (ZS_IS_CONS(uap))
1716 uap->port.cons->flags |= CON_ENABLED;
1718 /* Re-enable IRQ on the controller */
1719 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1720 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1721 enable_irq(uap->port.irq);
1724 bail:
1725 mutex_unlock(&state->mutex);
1726 mutex_unlock(&pmz_irq_mutex);
1728 /* Right now, we deal with delay by blocking here, I'll be
1729 * smarter later on
1731 if (pwr_delay != 0) {
1732 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1733 msleep(pwr_delay);
1736 pmz_debug("resume, switching complete\n");
1738 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
1740 return 0;
1744 * Probe all ports in the system and build the ports array, we register
1745 * with the serial layer at this point, the macio-type probing is only
1746 * used later to "attach" to the sysfs tree so we get power management
1747 * events
1749 static int __init pmz_probe(void)
1751 struct device_node *node_p, *node_a, *node_b, *np;
1752 int count = 0;
1753 int rc;
1756 * Find all escc chips in the system
1758 node_p = of_find_node_by_name(NULL, "escc");
1759 while (node_p) {
1761 * First get channel A/B node pointers
1763 * TODO: Add routines with proper locking to do that...
1765 node_a = node_b = NULL;
1766 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1767 if (strncmp(np->name, "ch-a", 4) == 0)
1768 node_a = of_node_get(np);
1769 else if (strncmp(np->name, "ch-b", 4) == 0)
1770 node_b = of_node_get(np);
1772 if (!node_a && !node_b) {
1773 of_node_put(node_a);
1774 of_node_put(node_b);
1775 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1776 (!node_a) ? 'a' : 'b', node_p->full_name);
1777 goto next;
1781 * Fill basic fields in the port structures
1783 pmz_ports[count].mate = &pmz_ports[count+1];
1784 pmz_ports[count+1].mate = &pmz_ports[count];
1785 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1786 pmz_ports[count].node = node_a;
1787 pmz_ports[count+1].node = node_b;
1788 pmz_ports[count].port.line = count;
1789 pmz_ports[count+1].port.line = count+1;
1792 * Setup the ports for real
1794 rc = pmz_init_port(&pmz_ports[count]);
1795 if (rc == 0 && node_b != NULL)
1796 rc = pmz_init_port(&pmz_ports[count+1]);
1797 if (rc != 0) {
1798 of_node_put(node_a);
1799 of_node_put(node_b);
1800 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1801 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1802 goto next;
1804 count += 2;
1805 next:
1806 node_p = of_find_node_by_name(node_p, "escc");
1808 pmz_ports_count = count;
1810 return 0;
1813 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1815 static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1816 static int __init pmz_console_setup(struct console *co, char *options);
1818 static struct console pmz_console = {
1819 .name = PMACZILOG_NAME,
1820 .write = pmz_console_write,
1821 .device = uart_console_device,
1822 .setup = pmz_console_setup,
1823 .flags = CON_PRINTBUFFER,
1824 .index = -1,
1825 .data = &pmz_uart_reg,
1828 #define PMACZILOG_CONSOLE &pmz_console
1829 #else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1830 #define PMACZILOG_CONSOLE (NULL)
1831 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1834 * Register the driver, console driver and ports with the serial
1835 * core
1837 static int __init pmz_register(void)
1839 int i, rc;
1841 pmz_uart_reg.nr = pmz_ports_count;
1842 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1845 * Register this driver with the serial core
1847 rc = uart_register_driver(&pmz_uart_reg);
1848 if (rc)
1849 return rc;
1852 * Register each port with the serial core
1854 for (i = 0; i < pmz_ports_count; i++) {
1855 struct uart_pmac_port *uport = &pmz_ports[i];
1856 /* NULL node may happen on wallstreet */
1857 if (uport->node != NULL)
1858 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1859 if (rc)
1860 goto err_out;
1863 return 0;
1864 err_out:
1865 while (i-- > 0) {
1866 struct uart_pmac_port *uport = &pmz_ports[i];
1867 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1869 uart_unregister_driver(&pmz_uart_reg);
1870 return rc;
1873 static struct of_device_id pmz_match[] =
1876 .name = "ch-a",
1879 .name = "ch-b",
1883 MODULE_DEVICE_TABLE (of, pmz_match);
1885 static struct macio_driver pmz_driver =
1887 .name = "pmac_zilog",
1888 .match_table = pmz_match,
1889 .probe = pmz_attach,
1890 .remove = pmz_detach,
1891 .suspend = pmz_suspend,
1892 .resume = pmz_resume,
1895 static int __init init_pmz(void)
1897 int rc, i;
1898 printk(KERN_INFO "%s\n", version);
1901 * First, we need to do a direct OF-based probe pass. We
1902 * do that because we want serial console up before the
1903 * macio stuffs calls us back, and since that makes it
1904 * easier to pass the proper number of channels to
1905 * uart_register_driver()
1907 if (pmz_ports_count == 0)
1908 pmz_probe();
1911 * Bail early if no port found
1913 if (pmz_ports_count == 0)
1914 return -ENODEV;
1917 * Now we register with the serial layer
1919 rc = pmz_register();
1920 if (rc) {
1921 printk(KERN_ERR
1922 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
1923 "pmac_zilog: Did another serial driver already claim the minors?\n");
1924 /* effectively "pmz_unprobe()" */
1925 for (i=0; i < pmz_ports_count; i++)
1926 pmz_dispose_port(&pmz_ports[i]);
1927 return rc;
1931 * Then we register the macio driver itself
1933 return macio_register_driver(&pmz_driver);
1936 static void __exit exit_pmz(void)
1938 int i;
1940 /* Get rid of macio-driver (detach from macio) */
1941 macio_unregister_driver(&pmz_driver);
1943 for (i = 0; i < pmz_ports_count; i++) {
1944 struct uart_pmac_port *uport = &pmz_ports[i];
1945 if (uport->node != NULL) {
1946 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1947 pmz_dispose_port(uport);
1950 /* Unregister UART driver */
1951 uart_unregister_driver(&pmz_uart_reg);
1954 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1956 static void pmz_console_putchar(struct uart_port *port, int ch)
1958 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1960 /* Wait for the transmit buffer to empty. */
1961 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1962 udelay(5);
1963 write_zsdata(uap, ch);
1967 * Print a string to the serial port trying not to disturb
1968 * any possible real use of the port...
1970 static void pmz_console_write(struct console *con, const char *s, unsigned int count)
1972 struct uart_pmac_port *uap = &pmz_ports[con->index];
1973 unsigned long flags;
1975 if (ZS_IS_ASLEEP(uap))
1976 return;
1977 spin_lock_irqsave(&uap->port.lock, flags);
1979 /* Turn of interrupts and enable the transmitter. */
1980 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
1981 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
1983 uart_console_write(&uap->port, s, count, pmz_console_putchar);
1985 /* Restore the values in the registers. */
1986 write_zsreg(uap, R1, uap->curregs[1]);
1987 /* Don't disable the transmitter. */
1989 spin_unlock_irqrestore(&uap->port.lock, flags);
1993 * Setup the serial console
1995 static int __init pmz_console_setup(struct console *co, char *options)
1997 struct uart_pmac_port *uap;
1998 struct uart_port *port;
1999 int baud = 38400;
2000 int bits = 8;
2001 int parity = 'n';
2002 int flow = 'n';
2003 unsigned long pwr_delay;
2006 * XServe's default to 57600 bps
2008 if (machine_is_compatible("RackMac1,1")
2009 || machine_is_compatible("RackMac1,2")
2010 || machine_is_compatible("MacRISC4"))
2011 baud = 57600;
2014 * Check whether an invalid uart number has been specified, and
2015 * if so, search for the first available port that does have
2016 * console support.
2018 if (co->index >= pmz_ports_count)
2019 co->index = 0;
2020 uap = &pmz_ports[co->index];
2021 if (uap->node == NULL)
2022 return -ENODEV;
2023 port = &uap->port;
2026 * Mark port as beeing a console
2028 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2031 * Temporary fix for uart layer who didn't setup the spinlock yet
2033 spin_lock_init(&port->lock);
2036 * Enable the hardware
2038 pwr_delay = __pmz_startup(uap);
2039 if (pwr_delay)
2040 mdelay(pwr_delay);
2042 if (options)
2043 uart_parse_options(options, &baud, &parity, &bits, &flow);
2045 return uart_set_options(port, co, baud, parity, bits, flow);
2048 static int __init pmz_console_init(void)
2050 /* Probe ports */
2051 pmz_probe();
2053 /* TODO: Autoprobe console based on OF */
2054 /* pmz_console.index = i; */
2055 register_console(&pmz_console);
2057 return 0;
2060 console_initcall(pmz_console_init);
2061 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2063 module_init(init_pmz);
2064 module_exit(exit_pmz);