blob: ad3488504010f2ebe66055fa3878eae07fcacabb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/module.h>
46#include <linux/tty.h>
47
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>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000062#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
73#define SUPPORT_SYSRQ
74#endif
75
76#include <linux/serial.h>
77#include <linux/serial_core.h>
78
79#include "pmac_zilog.h"
80
81/* Not yet implemented */
82#undef HAS_DBDMA
83
84static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
85MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
86MODULE_DESCRIPTION("Driver for the PowerMac serial ports.");
87MODULE_LICENSE("GPL");
88
89#define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg)
90
David Woodhousee4533b22007-04-05 00:19:43 +100091#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
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * For the sake of early serial console, we can do a pre-probe
104 * (optional) of the ports at rather early boot time.
105 */
106static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
107static int pmz_ports_count;
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000108static DEFINE_MUTEX(pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct uart_driver pmz_uart_reg = {
111 .owner = THIS_MODULE,
David Woodhousee4533b22007-04-05 00:19:43 +1000112 .driver_name = PMACZILOG_NAME,
113 .dev_name = PMACZILOG_NAME,
114 .major = PMACZILOG_MAJOR,
115 .minor = PMACZILOG_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118
119/*
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.
123 */
124static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
125{
126 int i;
127
128 if (ZS_IS_ASLEEP(uap))
129 return;
130
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);
137 }
138
139 ZS_CLEARERR(uap);
140 zssync(uap);
141 ZS_CLEARFIFO(uap);
142 zssync(uap);
143 ZS_CLEARERR(uap);
144
145 /* Disable all interrupts. */
146 write_zsreg(uap, R1,
147 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
148
149 /* Set parity, sync config, stop bits, and clock divisor. */
150 write_zsreg(uap, R4, regs[R4]);
151
152 /* Set misc. TX/RX control bits. */
153 write_zsreg(uap, R10, regs[R10]);
154
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);
158
159 /* now set R7 "prime" on ESCC */
160 write_zsreg(uap, R15, regs[R15] | EN85C30);
161 write_zsreg(uap, R7, regs[R7P]);
162
163 /* make sure we use R7 "non-prime" on ESCC */
164 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
165
166 /* Synchronous mode config. */
167 write_zsreg(uap, R6, regs[R6]);
168 write_zsreg(uap, R7, regs[R7]);
169
170 /* Disable baud generator. */
171 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
172
173 /* Clock mode control. */
174 write_zsreg(uap, R11, regs[R11]);
175
176 /* Lower and upper byte of baud rate generator divisor. */
177 write_zsreg(uap, R12, regs[R12]);
178 write_zsreg(uap, R13, regs[R13]);
179
180 /* Now rewrite R14, with BRENAB (if set). */
181 write_zsreg(uap, R14, regs[R14]);
182
183 /* Reset external status interrupts. */
184 write_zsreg(uap, R0, RES_EXT_INT);
185 write_zsreg(uap, R0, RES_EXT_INT);
186
187 /* Rewrite R3/R5, this time without enables masked. */
188 write_zsreg(uap, R3, regs[R3]);
189 write_zsreg(uap, R5, regs[R5]);
190
191 /* Rewrite R1, this time without IRQ enabled masked. */
192 write_zsreg(uap, R1, regs[R1]);
193
194 /* Enable interrupts */
195 write_zsreg(uap, R9, regs[R9]);
196}
197
198/*
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.
203 *
204 * The UART port lock must be held and local interrupts disabled.
205 */
206static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
207{
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);
214 }
215 }
216}
217
David Howells7d12e782006-10-05 14:55:46 +0100218static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct tty_struct *tty = NULL;
Alan Cox33f0f882006-01-09 20:54:13 -0800221 unsigned char ch, r1, drop, error, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int loops = 0;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* 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.
227 */
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;
240 }
241 return NULL;
242 }
243
244 /* Sanity check, make sure the old bug is no longer happening */
Stephen Rothwell87c25ef2008-07-16 21:54:22 +0100245 if (uap->port.info == NULL || uap->port.info->port.tty == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 WARN_ON(1);
247 (void)read_zsdata(uap);
248 return NULL;
249 }
Stephen Rothwell87c25ef2008-07-16 21:54:22 +0100250 tty = uap->port.info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 while (1) {
253 error = 0;
254 drop = 0;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 r1 = read_zsreg(uap, R1);
257 ch = read_zsdata(uap);
258
259 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
260 write_zsreg(uap, R0, ERR_RES);
261 zssync(uap);
262 }
263
264 ch &= uap->parity_mask;
265 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
266 uap->flags &= ~PMACZILOG_FLAG_BREAK;
267 }
268
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;
275 }
276#endif /* USE_CTRL_O_SYSRQ */
277 if (uap->port.sysrq) {
278 int swallow;
279 spin_unlock(&uap->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100280 swallow = uart_handle_sysrq_char(&uap->port, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 spin_lock(&uap->port.lock);
282 if (swallow)
283 goto next_char;
284 }
285#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
286
287 /* A real serial line, record the character and status. */
288 if (drop)
289 goto next_char;
290
Alan Cox33f0f882006-01-09 20:54:13 -0800291 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 uap->port.icount.rx++;
293
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;
302 }
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)
Alan Cox33f0f882006-01-09 20:54:13 -0800311 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 else if (r1 & PAR_ERR)
Alan Cox33f0f882006-01-09 20:54:13 -0800313 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 else if (r1 & CRC_ERR)
Alan Cox33f0f882006-01-09 20:54:13 -0800315 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 if (uap->port.ignore_status_mask == 0xff ||
319 (r1 & uap->port.ignore_status_mask) == 0) {
Alan Cox33f0f882006-01-09 20:54:13 -0800320 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Alan Cox33f0f882006-01-09 20:54:13 -0800322 if (r1 & Rx_OVR)
323 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 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
331 */
332 if ((++loops) > 1000)
333 goto flood;
334 ch = read_zsreg(uap, R0);
335 if (!(ch & Rx_CH_AV))
336 break;
337 }
338
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;
346}
347
David Howells7d12e782006-10-05 14:55:46 +0100348static void pmz_status_handle(struct uart_pmac_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 unsigned char status;
351
352 status = read_zsreg(uap, R0);
353 write_zsreg(uap, R0, RES_EXT_INT);
354 zssync(uap);
355
356 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
357 if (status & SYNC_HUNT)
358 uap->port.icount.dsr++;
359
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
364 */
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));
371
372 wake_up_interruptible(&uap->port.info->delta_msr_wait);
373 }
374
375 if (status & BRK_ABRT)
376 uap->flags |= PMACZILOG_FLAG_BREAK;
377
378 uap->prev_status = status;
379}
380
381static void pmz_transmit_chars(struct uart_pmac_port *uap)
382{
383 struct circ_buf *xmit;
384
385 if (ZS_IS_ASLEEP(uap))
386 return;
387 if (ZS_IS_CONS(uap)) {
388 unsigned char status = read_zsreg(uap, R0);
389
390 /* TX still busy? Just wait for the next TX done interrupt.
391 *
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
397 */
398 if (!(status & Tx_BUF_EMP))
399 return;
400 }
401
402 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
403
404 if (ZS_REGS_HELD(uap)) {
405 pmz_load_zsregs(uap, uap->curregs);
406 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
407 }
408
409 if (ZS_TX_STOPPED(uap)) {
410 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
411 goto ack_tx_int;
412 }
413
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;
421 }
422
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;
429 }
430 if (uart_tx_stopped(&uap->port))
431 goto ack_tx_int;
432
433 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
434 write_zsdata(uap, xmit->buf[xmit->tail]);
435 zssync(uap);
436
437 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
438 uap->port.icount.tx++;
439
440 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
441 uart_write_wakeup(&uap->port);
442
443 return;
444
445ack_tx_int:
446 write_zsreg(uap, R0, RES_Tx_P);
447 zssync(uap);
448}
449
450/* Hrm... we register that twice, fixme later.... */
David Howells7d12e782006-10-05 14:55:46 +0100451static irqreturn_t pmz_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
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;
459
460 uap_a = pmz_get_port_A(uap);
461 uap_b = uap_a->mate;
462
463 spin_lock(&uap_a->port.lock);
464 r3 = read_zsreg(uap_a, R3);
465
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)
David Howells7d12e782006-10-05 14:55:46 +0100475 pmz_status_handle(uap_a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (r3 & CHARxIP)
David Howells7d12e782006-10-05 14:55:46 +0100477 tty = pmz_receive_chars(uap_a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (r3 & CHATxIP)
479 pmz_transmit_chars(uap_a);
480 rc = IRQ_HANDLED;
481 }
482 spin_unlock(&uap_a->port.lock);
483 if (tty != NULL)
484 tty_flip_buffer_push(tty);
485
486 if (uap_b->node == NULL)
487 goto out;
488
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)
David Howells7d12e782006-10-05 14:55:46 +0100495 pmz_status_handle(uap_b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (r3 & CHBRxIP)
David Howells7d12e782006-10-05 14:55:46 +0100497 tty = pmz_receive_chars(uap_b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (r3 & CHBTxIP)
499 pmz_transmit_chars(uap_b);
500 rc = IRQ_HANDLED;
501 }
502 spin_unlock(&uap_b->port.lock);
503 if (tty != NULL)
504 tty_flip_buffer_push(tty);
505
506 out:
507#ifdef DEBUG_HARD
508 pmz_debug("irq done.\n");
509#endif
510 return rc;
511}
512
513/*
514 * Peek the status register, lock not held by caller
515 */
516static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
517{
518 unsigned long flags;
519 u8 status;
520
521 spin_lock_irqsave(&uap->port.lock, flags);
522 status = read_zsreg(uap, R0);
523 spin_unlock_irqrestore(&uap->port.lock, flags);
524
525 return status;
526}
527
528/*
529 * Check if transmitter is empty
530 * The port lock is not held.
531 */
532static unsigned int pmz_tx_empty(struct uart_port *port)
533{
534 struct uart_pmac_port *uap = to_pmz(port);
535 unsigned char status;
536
537 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
538 return TIOCSER_TEMT;
539
540 status = pmz_peek_status(to_pmz(port));
541 if (status & Tx_BUF_EMP)
542 return TIOCSER_TEMT;
543 return 0;
544}
545
546/*
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 ?
551 */
552static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
553{
554 struct uart_pmac_port *uap = to_pmz(port);
555 unsigned char set_bits, clear_bits;
556
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;
564
565 set_bits = clear_bits = 0;
566
567 if (ZS_IS_INTMODEM(uap)) {
568 if (mctrl & TIOCM_RTS)
569 set_bits |= RTS;
570 else
571 clear_bits |= RTS;
572 }
573 if (mctrl & TIOCM_DTR)
574 set_bits |= DTR;
575 else
576 clear_bits |= DTR;
577
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);
587}
588
589/*
590 * Get Modem Control bits (only the input ones, the core will
591 * or that with a cached value of the control ones)
Russell Kingc5f46442005-06-29 09:42:38 +0100592 * The port lock is held and interrupts are disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594static unsigned int pmz_get_mctrl(struct uart_port *port)
595{
596 struct uart_pmac_port *uap = to_pmz(port);
597 unsigned char status;
598 unsigned int ret;
599
600 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
601 return 0;
602
Russell Kingc5f46442005-06-29 09:42:38 +0100603 status = read_zsreg(uap, R0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
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;
612
613 return ret;
614}
615
616/*
617 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
Russell Kingb129a8c2005-08-31 10:12:14 +0100618 * though for DMA, we will have to do a bit more.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * The port lock is held and interrupts are disabled.
620 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100621static void pmz_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
624}
625
626/*
627 * Kick the Tx side.
628 * The port lock is held and interrupts are disabled.
629 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100630static void pmz_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct uart_pmac_port *uap = to_pmz(port);
633 unsigned char status;
634
635 pmz_debug("pmz: start_tx()\n");
636
637 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
638 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
639
640 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
641 return;
642
643 status = read_zsreg(uap, R0);
644
645 /* TX busy? Just wait for the TX done interrupt. */
646 if (!(status & Tx_BUF_EMP))
647 return;
648
649 /* Send the first character to jump-start the TX done
650 * IRQ sending engine.
651 */
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;
659
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++;
664
665 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
666 uart_write_wakeup(&uap->port);
667 }
668 pmz_debug("pmz: start_tx() done.\n");
669}
670
671/*
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.
676 */
677static void pmz_stop_rx(struct uart_port *port)
678{
679 struct uart_pmac_port *uap = to_pmz(port);
680
681 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
682 return;
683
684 pmz_debug("pmz: stop_rx()()\n");
685
686 /* Disable all RX interrupts. */
687 uap->curregs[R1] &= ~RxINT_MASK;
688 pmz_maybe_update_regs(uap);
689
690 pmz_debug("pmz: stop_rx() done.\n");
691}
692
693/*
694 * Enable modem status change interrupts
695 * The port lock is held.
696 */
697static void pmz_enable_ms(struct uart_port *port)
698{
699 struct uart_pmac_port *uap = to_pmz(port);
700 unsigned char new_reg;
701
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;
707
708 if (ZS_IS_ASLEEP(uap))
709 return;
710 /* NOTE: Not subject to 'transmitter active' rule. */
711 write_zsreg(uap, R15, uap->curregs[R15]);
712 }
713}
714
715/*
716 * Control break state emission
717 * The port lock is not held.
718 */
719static void pmz_break_ctl(struct uart_port *port, int break_state)
720{
721 struct uart_pmac_port *uap = to_pmz(port);
722 unsigned char set_bits, clear_bits, new_reg;
723 unsigned long flags;
724
725 if (uap->node == NULL)
726 return;
727 set_bits = clear_bits = 0;
728
729 if (break_state)
730 set_bits |= SND_BRK;
731 else
732 clear_bits |= SND_BRK;
733
734 spin_lock_irqsave(&port->lock, flags);
735
736 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
737 if (new_reg != uap->curregs[R5]) {
738 uap->curregs[R5] = new_reg;
739
740 /* NOTE: Not subject to 'transmitter active' rule. */
741 if (ZS_IS_ASLEEP(uap))
742 return;
743 write_zsreg(uap, R5, uap->curregs[R5]);
744 }
745
746 spin_unlock_irqrestore(&port->lock, flags);
747}
748
749/*
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.
754 */
755static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
756{
757 int delay = 0;
758 int rc;
759
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);
769 }
770 } else {
771 /* TODO: Make that depend on a timer, don't power down
772 * immediately
773 */
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);
778 }
779 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
780 }
781 return delay;
782}
783
784/*
785 * FixZeroBug....Works around a bug in the SCC receving channel.
786 * Inspired from Darwin code, 15 Sept. 2000 -DanM
787 *
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:
793 *
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.
799 *
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.
803 */
804static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
805{
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);
811
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 */
825
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.
830 */
831 write_zsreg(uap, 9, NV);
832 write_zsreg(uap, 4, X16CLK | SB_MASK);
833 write_zsreg(uap, 3, Rx8);
834
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);
839 }
840}
841
842/*
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.
847 */
848static int __pmz_startup(struct uart_pmac_port *uap)
849{
850 int pwr_delay = 0;
851
852 memset(&uap->curregs, 0, sizeof(uap->curregs));
853
854 /* Power up the SCC & underlying hardware (modem/irda) */
855 pwr_delay = pmz_set_scc_power(uap, 1);
856
857 /* Nice buggy HW ... */
858 pmz_fix_zero_bug_scc(uap);
859
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);
867
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);
874
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;
884
885 /* Clear handshaking, enable BREAK interrupts */
886 uap->curregs[R15] = BRKIE;
887
888 /* Master interrupt enable */
889 uap->curregs[R9] |= NV | MIE;
890
891 pmz_load_zsregs(uap, uap->curregs);
892
893 /* Enable receiver and transmitter. */
894 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
895 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
896
897 /* Remember status for DCD/CTS changes */
898 uap->prev_status = read_zsreg(uap, R0);
899
900
901 return pwr_delay;
902}
903
904static void pmz_irda_reset(struct uart_pmac_port *uap)
905{
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);
914}
915
916/*
917 * This is the "normal" startup routine, using the above one
918 * wrapped with the lock and doing a schedule delay
919 */
920static int pmz_startup(struct uart_port *port)
921{
922 struct uart_pmac_port *uap = to_pmz(port);
923 unsigned long flags;
924 int pwr_delay = 0;
925
926 pmz_debug("pmz: startup()\n");
927
928 if (ZS_IS_ASLEEP(uap))
929 return -EAGAIN;
930 if (uap->node == NULL)
931 return -ENODEV;
932
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000933 mutex_lock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
936
937 /* A console is never powered down. Else, power up and
938 * initialize the chip
939 */
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);
944 }
945
946 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700947 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED, "PowerMac Zilog", uap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dev_err(&uap->dev->ofdev.dev,
949 "Unable to register zs interrupt handler.\n");
950 pmz_set_scc_power(uap, 0);
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000951 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return -ENXIO;
953 }
954
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000955 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* Right now, we deal with delay by blocking here, I'll be
958 * smarter later on
959 */
960 if (pwr_delay != 0) {
961 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
962 msleep(pwr_delay);
963 }
964
965 /* IrDA reset is done now */
966 if (ZS_IS_IRDA(uap))
967 pmz_irda_reset(uap);
968
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);
976
977 pmz_debug("pmz: startup() done.\n");
978
979 return 0;
980}
981
982static void pmz_shutdown(struct uart_port *port)
983{
984 struct uart_pmac_port *uap = to_pmz(port);
985 unsigned long flags;
986
987 pmz_debug("pmz: shutdown()\n");
988
989 if (uap->node == NULL)
990 return;
991
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000992 mutex_lock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* Release interrupt handler */
995 free_irq(uap->port.irq, uap);
996
997 spin_lock_irqsave(&port->lock, flags);
998
999 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1000
1001 if (!ZS_IS_OPEN(uap->mate))
1002 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1003
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);
1009 }
1010
1011 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1012 spin_unlock_irqrestore(&port->lock, flags);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001013 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return;
1015 }
1016
1017 /* Disable receiver and transmitter. */
1018 uap->curregs[R3] &= ~RxENABLE;
1019 uap->curregs[R5] &= ~TxENABLE;
1020
1021 /* Disable all interrupts and BRK assertion. */
1022 uap->curregs[R5] &= ~SND_BRK;
1023 pmz_maybe_update_regs(uap);
1024
1025 /* Shut the chip down */
1026 pmz_set_scc_power(uap, 0);
1027
1028 spin_unlock_irqrestore(&port->lock, flags);
1029
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001030 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 pmz_debug("pmz: shutdown() done.\n");
1033}
1034
1035/* Shared by TTY driver and serial console setup. The port lock is held
1036 * and local interrupts are disabled.
1037 */
1038static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1039 unsigned int iflag, unsigned long baud)
1040{
1041 int brg;
1042
1043
1044 /* Switch to external clocking for IrDA high clock rates. That
1045 * code could be re-used for Midi interfaces with different
1046 * multipliers
1047 */
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;
1074 }
1075 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1076 }
1077
1078 /* Character size, stop bits, and parity. */
1079 uap->curregs[3] &= ~RxN_MASK;
1080 uap->curregs[5] &= ~TxN_MASK;
1081
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;
1104 };
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;
1118
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;
1124
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;
1132 }
1133
1134 if ((cflag & CREAD) == 0)
1135 uap->port.ignore_status_mask = 0xff;
1136}
1137
1138
1139/*
1140 * Set the irda codec on the imac to the specified baud rate.
1141 */
1142static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1143{
1144 u8 cmdbyte;
1145 int t, version;
1146
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 ?
1172 */
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;
1183 }
1184
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;
1192 }
1193 udelay(10);
1194 }
1195
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;
1208 }
1209 }
1210
1211 /* Switch to command mode */
1212 uap->curregs[R5] |= DTR;
1213 write_zsreg(uap, R5, uap->curregs[R5]);
1214 zssync(uap);
1215 mdelay(1);
1216
1217 /* Switch SCC to 19200 */
1218 pmz_convert_to_zs(uap, CS8, 0, 19200);
1219 pmz_load_zsregs(uap, uap->curregs);
1220 mdelay(1);
1221
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;
1230 }
1231 udelay(10);
1232 }
1233 version = read_zsdata(uap);
1234
1235 if (version < 4) {
1236 dev_info(&uap->dev->ofdev.dev, "IrDA: dongle version %d not supported\n",
1237 version);
1238 goto out;
1239 }
1240
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;
1249 }
1250 udelay(10);
1251 }
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);
1256
1257 dev_info(&uap->dev->ofdev.dev, "IrDA setup for %ld bps, dongle version: %d\n",
1258 *baud, version);
1259
1260 (void)read_zsdata(uap);
1261 (void)read_zsdata(uap);
1262 (void)read_zsdata(uap);
1263
1264 out:
1265 /* Switch back to data mode */
1266 uap->curregs[R5] &= ~DTR;
1267 write_zsreg(uap, R5, uap->curregs[R5]);
1268 zssync(uap);
1269
1270 (void)read_zsdata(uap);
1271 (void)read_zsdata(uap);
1272 (void)read_zsdata(uap);
1273}
1274
1275
Alan Cox606d0992006-12-08 02:38:45 -08001276static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1277 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct uart_pmac_port *uap = to_pmz(port);
1280 unsigned long baud;
1281
1282 pmz_debug("pmz: set_termios()\n");
1283
1284 if (ZS_IS_ASLEEP(uap))
1285 return;
1286
Alan Cox606d0992006-12-08 02:38:45 -08001287 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
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.
1295 */
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;
1316 }
1317
1318 /* Load registers to the chip */
1319 pmz_maybe_update_regs(uap);
1320 }
1321 uart_update_timeout(port, termios->c_cflag, baud);
1322
1323 pmz_debug("pmz: set_termios() done.\n");
1324}
1325
1326/* The port lock is not held. */
Alan Cox606d0992006-12-08 02:38:45 -08001327static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1328 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 struct uart_pmac_port *uap = to_pmz(port);
1331 unsigned long flags;
1332
1333 spin_lock_irqsave(&port->lock, flags);
1334
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]);
1338
1339 /* Setup new port configuration */
1340 __pmz_set_termios(port, termios, old);
1341
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]);
1348 }
1349 spin_unlock_irqrestore(&port->lock, flags);
1350}
1351
1352static const char *pmz_type(struct uart_port *port)
1353{
1354 struct uart_pmac_port *uap = to_pmz(port);
1355
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";
1361}
1362
1363/* We do not request/release mappings of the registers here, this
1364 * happens at early serial probe time.
1365 */
1366static void pmz_release_port(struct uart_port *port)
1367{
1368}
1369
1370static int pmz_request_port(struct uart_port *port)
1371{
1372 return 0;
1373}
1374
1375/* These do not need to do anything interesting either. */
1376static void pmz_config_port(struct uart_port *port, int flags)
1377{
1378}
1379
1380/* We do not support letting the user mess with the divisor, IRQ, etc. */
1381static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1382{
1383 return -EINVAL;
1384}
1385
Chris J Arges8c653182008-11-04 12:19:06 +00001386#ifdef CONFIG_CONSOLE_POLL
1387
1388static int pmz_poll_get_char(struct uart_port *port)
1389{
1390 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1391
1392 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1393 udelay(5);
1394 return read_zsdata(uap);
1395}
1396
1397static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1398{
1399 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1400
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);
1405}
1406
1407#endif
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static 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,
Chris J Arges8c653182008-11-04 12:19:06 +00001426#ifdef CONFIG_CONSOLE_POLL
1427 .poll_get_char = pmz_poll_get_char,
1428 .poll_put_char = pmz_poll_put_char,
1429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430};
1431
1432/*
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
1436 */
1437static int __init pmz_init_port(struct uart_pmac_port *uap)
1438{
1439 struct device_node *np = uap->node;
Jeremy Kerr018a3d12006-07-12 15:40:29 +10001440 const char *conn;
1441 const struct slot_names_prop {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int count;
1443 char name[1];
1444 } *slots;
1445 int len;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001446 struct resource r_ports, r_rxdma, r_txdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /*
1449 * Request & map chip registers
1450 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001451 if (of_address_to_resource(np, 0, &r_ports))
1452 return -ENODEV;
1453 uap->port.mapbase = r_ports.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1455
1456 uap->control_reg = uap->port.membase;
1457 uap->data_reg = uap->control_reg + 0x10;
1458
1459 /*
1460 * Request & map DBDMA registers
1461 */
1462#ifdef HAS_DBDMA
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001463 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1464 of_address_to_resource(np, 2, &r_rxdma) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001466#else
1467 memset(&r_txdma, 0, sizeof(struct resource));
1468 memset(&r_rxdma, 0, sizeof(struct resource));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
1470 if (ZS_HAS_DMA(uap)) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001471 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (uap->tx_dma_regs == NULL) {
1473 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1474 goto no_dma;
1475 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001476 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 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;
1482 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001483 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1484 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486no_dma:
1487
1488 /*
1489 * Detect port type
1490 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001491 if (of_device_is_compatible(np, "cobalt"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001493 conn = of_get_property(np, "AAPL,connector", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 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 */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001498 slots = of_get_property(np, "slot-names", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 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;
1504 }
1505 if (ZS_IS_IRDA(uap))
1506 uap->port_type = PMAC_SCC_IRDA;
1507 if (ZS_IS_INTMODEM(uap)) {
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001508 struct device_node* i2c_modem =
1509 of_find_node_by_name(NULL, "i2c-modem");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (i2c_modem) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +10001511 const char* mid =
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001512 of_get_property(i2c_modem, "modem-id", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 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;
1521 }
1522 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1523 mid ? (*mid) : 0);
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001524 of_node_put(i2c_modem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } else {
1526 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1527 }
1528 }
1529
1530 /*
1531 * Init remaining bits of "port" structure
1532 */
Russell King9b4a1612006-02-05 10:48:10 +00001533 uap->port.iotype = UPIO_MEM;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001534 uap->port.irq = irq_of_parse_and_map(np, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 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;
1540
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.
1544 */
1545 pmz_convert_to_zs(uap, CS8, 0, 9600);
1546
1547 return 0;
1548}
1549
1550/*
1551 * Get rid of a port on module removal
1552 */
1553static void pmz_dispose_port(struct uart_pmac_port *uap)
1554{
1555 struct device_node *np;
1556
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));
1564}
1565
1566/*
1567 * Called upon match with an escc node in the devive-tree.
1568 */
Jeff Mahoney5e655772005-07-06 15:44:41 -04001569static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 int i;
1572
1573 /* Iterate the pmz_ports array to find a matching entry
1574 */
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];
1578
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;
1588 }
1589 return -ENODEV;
1590}
1591
1592/*
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...
1595 */
1596static int pmz_detach(struct macio_dev *mdev)
1597{
1598 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1599
1600 if (!uap)
1601 return -ENODEV;
1602
1603 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1604 macio_release_resources(uap->dev);
1605 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1606 }
1607 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1608 uap->dev = NULL;
1609
1610 return 0;
1611}
1612
1613
Pavel Machek0370aff2005-04-16 15:25:35 -07001614static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1617 struct uart_state *state;
1618 unsigned long flags;
1619
1620 if (uap == NULL) {
1621 printk("HRM... pmz_suspend with NULL uap\n");
1622 return 0;
1623 }
1624
Pavel Machekca078ba2005-09-03 15:56:57 -07001625 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627
Olaf Heringaa015422007-08-26 19:10:59 +10001628 pmz_debug("suspend, switching to state %d\n", pm_state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 state = pmz_uart_reg.state + uap->port.line;
1631
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001632 mutex_lock(&pmz_irq_mutex);
Ingo Molnare2862f62006-01-13 21:37:07 +00001633 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 spin_lock_irqsave(&uap->port.lock, flags);
1636
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;
1641
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();
1648 }
1649
1650 spin_unlock_irqrestore(&uap->port.lock, flags);
1651
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);
1656 }
1657
1658 if (ZS_IS_CONS(uap))
1659 uap->port.cons->flags &= ~CON_ENABLED;
1660
1661 /* Shut the chip down */
1662 pmz_set_scc_power(uap, 0);
1663
Ingo Molnare2862f62006-01-13 21:37:07 +00001664 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001665 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 pmz_debug("suspend, switching complete\n");
1668
1669 mdev->ofdev.dev.power.power_state = pm_state;
1670
1671 return 0;
1672}
1673
1674
1675static int pmz_resume(struct macio_dev *mdev)
1676{
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;
1681
1682 if (uap == NULL)
1683 return 0;
1684
Pavel Machekca078ba2005-09-03 15:56:57 -07001685 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return 0;
1687
1688 pmz_debug("resume, switching to state 0\n");
1689
1690 state = pmz_uart_reg.state + uap->port.line;
1691
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001692 mutex_lock(&pmz_irq_mutex);
Ingo Molnare2862f62006-01-13 21:37:07 +00001693 mutex_lock(&state->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
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;
1699 }
1700 pwr_delay = __pmz_startup(uap);
1701
1702 /* Take care of config that may have changed while asleep */
1703 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1704
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]);
1711 }
1712
1713 spin_unlock_irqrestore(&uap->port.lock, flags);
1714
1715 if (ZS_IS_CONS(uap))
1716 uap->port.cons->flags |= CON_ENABLED;
1717
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);
1722 }
1723
1724 bail:
Ingo Molnare2862f62006-01-13 21:37:07 +00001725 mutex_unlock(&state->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001726 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* Right now, we deal with delay by blocking here, I'll be
1729 * smarter later on
1730 */
1731 if (pwr_delay != 0) {
1732 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1733 msleep(pwr_delay);
1734 }
1735
1736 pmz_debug("resume, switching complete\n");
1737
Pavel Machekca078ba2005-09-03 15:56:57 -07001738 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 return 0;
1741}
1742
1743/*
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
1748 */
1749static int __init pmz_probe(void)
1750{
1751 struct device_node *node_p, *node_a, *node_b, *np;
1752 int count = 0;
1753 int rc;
1754
1755 /*
1756 * Find all escc chips in the system
1757 */
1758 node_p = of_find_node_by_name(NULL, "escc");
1759 while (node_p) {
1760 /*
1761 * First get channel A/B node pointers
1762 *
1763 * TODO: Add routines with proper locking to do that...
1764 */
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);
1771 }
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;
1778 }
1779
1780 /*
1781 * Fill basic fields in the port structures
1782 */
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;
1790
1791 /*
1792 * Setup the ports for real
1793 */
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;
1803 }
1804 count += 2;
1805next:
1806 node_p = of_find_node_by_name(node_p, "escc");
1807 }
1808 pmz_ports_count = count;
1809
1810 return 0;
1811}
1812
1813#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1814
1815static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1816static int __init pmz_console_setup(struct console *co, char *options);
1817
1818static struct console pmz_console = {
David Woodhousee4533b22007-04-05 00:19:43 +10001819 .name = PMACZILOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 .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,
1826};
1827
1828#define PMACZILOG_CONSOLE &pmz_console
1829#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1830#define PMACZILOG_CONSOLE (NULL)
1831#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1832
1833/*
1834 * Register the driver, console driver and ports with the serial
1835 * core
1836 */
1837static int __init pmz_register(void)
1838{
1839 int i, rc;
1840
1841 pmz_uart_reg.nr = pmz_ports_count;
1842 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /*
1845 * Register this driver with the serial core
1846 */
1847 rc = uart_register_driver(&pmz_uart_reg);
1848 if (rc)
1849 return rc;
1850
1851 /*
1852 * Register each port with the serial core
1853 */
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;
1861 }
1862
1863 return 0;
1864err_out:
1865 while (i-- > 0) {
1866 struct uart_pmac_port *uport = &pmz_ports[i];
1867 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1868 }
1869 uart_unregister_driver(&pmz_uart_reg);
1870 return rc;
1871}
1872
Jeff Mahoney5e655772005-07-06 15:44:41 -04001873static struct of_device_id pmz_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 {
1876 .name = "ch-a",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 },
1878 {
1879 .name = "ch-b",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 },
1881 {},
1882};
Jeff Mahoney5e655772005-07-06 15:44:41 -04001883MODULE_DEVICE_TABLE (of, pmz_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885static struct macio_driver pmz_driver =
1886{
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,
1893};
1894
1895static int __init init_pmz(void)
1896{
1897 int rc, i;
1898 printk(KERN_INFO "%s\n", version);
1899
1900 /*
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()
1906 */
1907 if (pmz_ports_count == 0)
1908 pmz_probe();
1909
1910 /*
1911 * Bail early if no port found
1912 */
1913 if (pmz_ports_count == 0)
1914 return -ENODEV;
1915
1916 /*
1917 * Now we register with the serial layer
1918 */
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;
1928 }
1929
1930 /*
1931 * Then we register the macio driver itself
1932 */
1933 return macio_register_driver(&pmz_driver);
1934}
1935
1936static void __exit exit_pmz(void)
1937{
1938 int i;
1939
1940 /* Get rid of macio-driver (detach from macio) */
1941 macio_unregister_driver(&pmz_driver);
1942
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);
1948 }
1949 }
1950 /* Unregister UART driver */
1951 uart_unregister_driver(&pmz_uart_reg);
1952}
1953
1954#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1955
Russell Kingd3587882006-03-20 20:00:09 +00001956static void pmz_console_putchar(struct uart_port *port, int ch)
1957{
1958 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1959
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);
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * Print a string to the serial port trying not to disturb
1968 * any possible real use of the port...
1969 */
1970static void pmz_console_write(struct console *con, const char *s, unsigned int count)
1971{
1972 struct uart_pmac_port *uap = &pmz_ports[con->index];
1973 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 if (ZS_IS_ASLEEP(uap))
1976 return;
1977 spin_lock_irqsave(&uap->port.lock, flags);
1978
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);
1982
Russell Kingd3587882006-03-20 20:00:09 +00001983 uart_console_write(&uap->port, s, count, pmz_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* Restore the values in the registers. */
1986 write_zsreg(uap, R1, uap->curregs[1]);
1987 /* Don't disable the transmitter. */
1988
1989 spin_unlock_irqrestore(&uap->port.lock, flags);
1990}
1991
1992/*
1993 * Setup the serial console
1994 */
1995static int __init pmz_console_setup(struct console *co, char *options)
1996{
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;
2004
2005 /*
2006 * XServe's default to 57600 bps
2007 */
2008 if (machine_is_compatible("RackMac1,1")
2009 || machine_is_compatible("RackMac1,2")
2010 || machine_is_compatible("MacRISC4"))
2011 baud = 57600;
2012
2013 /*
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.
2017 */
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;
2024
2025 /*
2026 * Mark port as beeing a console
2027 */
2028 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2029
2030 /*
2031 * Temporary fix for uart layer who didn't setup the spinlock yet
2032 */
2033 spin_lock_init(&port->lock);
2034
2035 /*
2036 * Enable the hardware
2037 */
2038 pwr_delay = __pmz_startup(uap);
2039 if (pwr_delay)
2040 mdelay(pwr_delay);
2041
2042 if (options)
2043 uart_parse_options(options, &baud, &parity, &bits, &flow);
2044
2045 return uart_set_options(port, co, baud, parity, bits, flow);
2046}
2047
2048static int __init pmz_console_init(void)
2049{
2050 /* Probe ports */
2051 pmz_probe();
2052
2053 /* TODO: Autoprobe console based on OF */
2054 /* pmz_console.index = i; */
2055 register_console(&pmz_console);
2056
2057 return 0;
2058
2059}
2060console_initcall(pmz_console_init);
2061#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2062
2063module_init(init_pmz);
2064module_exit(exit_pmz);