blob: f020de1cdd5035155bbb4f3c95ba847e81b1c52b [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>
Finn Thainec9cbe02009-11-17 20:04:44 +110066
67#ifdef CONFIG_PPC_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/prom.h>
69#include <asm/machdep.h>
70#include <asm/pmac_feature.h>
71#include <asm/dbdma.h>
72#include <asm/macio.h>
Finn Thainec9cbe02009-11-17 20:04:44 +110073#else
74#include <linux/platform_device.h>
75#define of_machine_is_compatible(x) (0)
76#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
79#define SUPPORT_SYSRQ
80#endif
81
82#include <linux/serial.h>
83#include <linux/serial_core.h>
84
85#include "pmac_zilog.h"
86
87/* Not yet implemented */
88#undef HAS_DBDMA
89
90static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
91MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
Finn Thainec9cbe02009-11-17 20:04:44 +110092MODULE_DESCRIPTION("Driver for the Mac and PowerMac serial ports.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093MODULE_LICENSE("GPL");
94
David Woodhousee4533b22007-04-05 00:19:43 +100095#ifdef CONFIG_SERIAL_PMACZILOG_TTYS
96#define PMACZILOG_MAJOR TTY_MAJOR
97#define PMACZILOG_MINOR 64
98#define PMACZILOG_NAME "ttyS"
99#else
100#define PMACZILOG_MAJOR 204
101#define PMACZILOG_MINOR 192
102#define PMACZILOG_NAME "ttyPZ"
103#endif
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * For the sake of early serial console, we can do a pre-probe
108 * (optional) of the ports at rather early boot time.
109 */
110static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
111static int pmz_ports_count;
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000112static DEFINE_MUTEX(pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static struct uart_driver pmz_uart_reg = {
115 .owner = THIS_MODULE,
David Woodhousee4533b22007-04-05 00:19:43 +1000116 .driver_name = PMACZILOG_NAME,
117 .dev_name = PMACZILOG_NAME,
118 .major = PMACZILOG_MAJOR,
119 .minor = PMACZILOG_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122
123/*
124 * Load all registers to reprogram the port
125 * This function must only be called when the TX is not busy. The UART
126 * port lock must be held and local interrupts disabled.
127 */
128static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
129{
130 int i;
131
132 if (ZS_IS_ASLEEP(uap))
133 return;
134
135 /* Let pending transmits finish. */
136 for (i = 0; i < 1000; i++) {
137 unsigned char stat = read_zsreg(uap, R1);
138 if (stat & ALL_SNT)
139 break;
140 udelay(100);
141 }
142
143 ZS_CLEARERR(uap);
144 zssync(uap);
145 ZS_CLEARFIFO(uap);
146 zssync(uap);
147 ZS_CLEARERR(uap);
148
149 /* Disable all interrupts. */
150 write_zsreg(uap, R1,
151 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
152
153 /* Set parity, sync config, stop bits, and clock divisor. */
154 write_zsreg(uap, R4, regs[R4]);
155
156 /* Set misc. TX/RX control bits. */
157 write_zsreg(uap, R10, regs[R10]);
158
159 /* Set TX/RX controls sans the enable bits. */
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100160 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
161 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* now set R7 "prime" on ESCC */
164 write_zsreg(uap, R15, regs[R15] | EN85C30);
165 write_zsreg(uap, R7, regs[R7P]);
166
167 /* make sure we use R7 "non-prime" on ESCC */
168 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
169
170 /* Synchronous mode config. */
171 write_zsreg(uap, R6, regs[R6]);
172 write_zsreg(uap, R7, regs[R7]);
173
174 /* Disable baud generator. */
175 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
176
177 /* Clock mode control. */
178 write_zsreg(uap, R11, regs[R11]);
179
180 /* Lower and upper byte of baud rate generator divisor. */
181 write_zsreg(uap, R12, regs[R12]);
182 write_zsreg(uap, R13, regs[R13]);
183
184 /* Now rewrite R14, with BRENAB (if set). */
185 write_zsreg(uap, R14, regs[R14]);
186
187 /* Reset external status interrupts. */
188 write_zsreg(uap, R0, RES_EXT_INT);
189 write_zsreg(uap, R0, RES_EXT_INT);
190
191 /* Rewrite R3/R5, this time without enables masked. */
192 write_zsreg(uap, R3, regs[R3]);
193 write_zsreg(uap, R5, regs[R5]);
194
195 /* Rewrite R1, this time without IRQ enabled masked. */
196 write_zsreg(uap, R1, regs[R1]);
197
198 /* Enable interrupts */
199 write_zsreg(uap, R9, regs[R9]);
200}
201
202/*
203 * We do like sunzilog to avoid disrupting pending Tx
204 * Reprogram the Zilog channel HW registers with the copies found in the
205 * software state struct. If the transmitter is busy, we defer this update
206 * until the next TX complete interrupt. Else, we do it right now.
207 *
208 * The UART port lock must be held and local interrupts disabled.
209 */
210static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
211{
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100212 if (!ZS_REGS_HELD(uap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ZS_TX_ACTIVE(uap)) {
214 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
215 } else {
216 pmz_debug("pmz: maybe_update_regs: updating\n");
217 pmz_load_zsregs(uap, uap->curregs);
218 }
219 }
220}
221
David Howells7d12e782006-10-05 14:55:46 +0100222static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct tty_struct *tty = NULL;
Alan Cox33f0f882006-01-09 20:54:13 -0800225 unsigned char ch, r1, drop, error, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int loops = 0;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* The interrupt can be enabled when the port isn't open, typically
229 * that happens when using one port is open and the other closed (stale
230 * interrupt) or when one port is used as a console.
231 */
232 if (!ZS_IS_OPEN(uap)) {
233 pmz_debug("pmz: draining input\n");
234 /* Port is closed, drain input data */
235 for (;;) {
236 if ((++loops) > 1000)
237 goto flood;
238 (void)read_zsreg(uap, R1);
239 write_zsreg(uap, R0, ERR_RES);
240 (void)read_zsdata(uap);
241 ch = read_zsreg(uap, R0);
242 if (!(ch & Rx_CH_AV))
243 break;
244 }
245 return NULL;
246 }
247
248 /* Sanity check, make sure the old bug is no longer happening */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700249 if (uap->port.state == NULL || uap->port.state->port.tty == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 WARN_ON(1);
251 (void)read_zsdata(uap);
252 return NULL;
253 }
Alan Coxebd2c8f2009-09-19 13:13:28 -0700254 tty = uap->port.state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 while (1) {
257 error = 0;
258 drop = 0;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 r1 = read_zsreg(uap, R1);
261 ch = read_zsdata(uap);
262
263 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
264 write_zsreg(uap, R0, ERR_RES);
265 zssync(uap);
266 }
267
268 ch &= uap->parity_mask;
269 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
270 uap->flags &= ~PMACZILOG_FLAG_BREAK;
271 }
272
273#if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
274#ifdef USE_CTRL_O_SYSRQ
275 /* Handle the SysRq ^O Hack */
276 if (ch == '\x0f') {
277 uap->port.sysrq = jiffies + HZ*5;
278 goto next_char;
279 }
280#endif /* USE_CTRL_O_SYSRQ */
281 if (uap->port.sysrq) {
282 int swallow;
283 spin_unlock(&uap->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100284 swallow = uart_handle_sysrq_char(&uap->port, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_lock(&uap->port.lock);
286 if (swallow)
287 goto next_char;
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
290
291 /* A real serial line, record the character and status. */
292 if (drop)
293 goto next_char;
294
Alan Cox33f0f882006-01-09 20:54:13 -0800295 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 uap->port.icount.rx++;
297
298 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
299 error = 1;
300 if (r1 & BRK_ABRT) {
301 pmz_debug("pmz: got break !\n");
302 r1 &= ~(PAR_ERR | CRC_ERR);
303 uap->port.icount.brk++;
304 if (uart_handle_break(&uap->port))
305 goto next_char;
306 }
307 else if (r1 & PAR_ERR)
308 uap->port.icount.parity++;
309 else if (r1 & CRC_ERR)
310 uap->port.icount.frame++;
311 if (r1 & Rx_OVR)
312 uap->port.icount.overrun++;
313 r1 &= uap->port.read_status_mask;
314 if (r1 & BRK_ABRT)
Alan Cox33f0f882006-01-09 20:54:13 -0800315 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else if (r1 & PAR_ERR)
Alan Cox33f0f882006-01-09 20:54:13 -0800317 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 else if (r1 & CRC_ERR)
Alan Cox33f0f882006-01-09 20:54:13 -0800319 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 if (uap->port.ignore_status_mask == 0xff ||
323 (r1 & uap->port.ignore_status_mask) == 0) {
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100324 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Alan Cox33f0f882006-01-09 20:54:13 -0800326 if (r1 & Rx_OVR)
327 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 next_char:
329 /* We can get stuck in an infinite loop getting char 0 when the
330 * line is in a wrong HW state, we break that here.
331 * When that happens, I disable the receive side of the driver.
332 * Note that what I've been experiencing is a real irq loop where
333 * I'm getting flooded regardless of the actual port speed.
334 * Something stange is going on with the HW
335 */
336 if ((++loops) > 1000)
337 goto flood;
338 ch = read_zsreg(uap, R0);
339 if (!(ch & Rx_CH_AV))
340 break;
341 }
342
343 return tty;
344 flood:
345 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
346 write_zsreg(uap, R1, uap->curregs[R1]);
347 zssync(uap);
Finn Thainec9cbe02009-11-17 20:04:44 +1100348 pmz_error("pmz: rx irq flood !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return tty;
350}
351
David Howells7d12e782006-10-05 14:55:46 +0100352static void pmz_status_handle(struct uart_pmac_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unsigned char status;
355
356 status = read_zsreg(uap, R0);
357 write_zsreg(uap, R0, RES_EXT_INT);
358 zssync(uap);
359
360 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
361 if (status & SYNC_HUNT)
362 uap->port.icount.dsr++;
363
364 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
365 * But it does not tell us which bit has changed, we have to keep
366 * track of this ourselves.
367 * The CTS input is inverted for some reason. -- paulus
368 */
369 if ((status ^ uap->prev_status) & DCD)
370 uart_handle_dcd_change(&uap->port,
371 (status & DCD));
372 if ((status ^ uap->prev_status) & CTS)
373 uart_handle_cts_change(&uap->port,
374 !(status & CTS));
375
Alan Coxbdc04e32009-09-19 13:13:31 -0700376 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 if (status & BRK_ABRT)
380 uap->flags |= PMACZILOG_FLAG_BREAK;
381
382 uap->prev_status = status;
383}
384
385static void pmz_transmit_chars(struct uart_pmac_port *uap)
386{
387 struct circ_buf *xmit;
388
389 if (ZS_IS_ASLEEP(uap))
390 return;
391 if (ZS_IS_CONS(uap)) {
392 unsigned char status = read_zsreg(uap, R0);
393
394 /* TX still busy? Just wait for the next TX done interrupt.
395 *
396 * It can occur because of how we do serial console writes. It would
397 * be nice to transmit console writes just like we normally would for
398 * a TTY line. (ie. buffered and TX interrupt driven). That is not
399 * easy because console writes cannot sleep. One solution might be
400 * to poll on enough port->xmit space becomming free. -DaveM
401 */
402 if (!(status & Tx_BUF_EMP))
403 return;
404 }
405
406 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
407
408 if (ZS_REGS_HELD(uap)) {
409 pmz_load_zsregs(uap, uap->curregs);
410 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
411 }
412
413 if (ZS_TX_STOPPED(uap)) {
414 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
415 goto ack_tx_int;
416 }
417
Benjamin Herrenschmidt02ab8512010-01-10 17:51:42 +0000418 /* Under some circumstances, we see interrupts reported for
419 * a closed channel. The interrupt mask in R1 is clear, but
420 * R3 still signals the interrupts and we see them when taking
421 * an interrupt for the other channel (this could be a qemu
422 * bug but since the ESCC doc doesn't specify precsiely whether
423 * R3 interrup status bits are masked by R1 interrupt enable
424 * bits, better safe than sorry). --BenH.
425 */
426 if (!ZS_IS_OPEN(uap))
427 goto ack_tx_int;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (uap->port.x_char) {
430 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
431 write_zsdata(uap, uap->port.x_char);
432 zssync(uap);
433 uap->port.icount.tx++;
434 uap->port.x_char = 0;
435 return;
436 }
437
Alan Coxebd2c8f2009-09-19 13:13:28 -0700438 if (uap->port.state == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto ack_tx_int;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700440 xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (uart_circ_empty(xmit)) {
442 uart_write_wakeup(&uap->port);
443 goto ack_tx_int;
444 }
445 if (uart_tx_stopped(&uap->port))
446 goto ack_tx_int;
447
448 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
449 write_zsdata(uap, xmit->buf[xmit->tail]);
450 zssync(uap);
451
452 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
453 uap->port.icount.tx++;
454
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(&uap->port);
457
458 return;
459
460ack_tx_int:
461 write_zsreg(uap, R0, RES_Tx_P);
462 zssync(uap);
463}
464
465/* Hrm... we register that twice, fixme later.... */
David Howells7d12e782006-10-05 14:55:46 +0100466static irqreturn_t pmz_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct uart_pmac_port *uap = dev_id;
469 struct uart_pmac_port *uap_a;
470 struct uart_pmac_port *uap_b;
471 int rc = IRQ_NONE;
472 struct tty_struct *tty;
473 u8 r3;
474
475 uap_a = pmz_get_port_A(uap);
476 uap_b = uap_a->mate;
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100477
478 spin_lock(&uap_a->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 r3 = read_zsreg(uap_a, R3);
480
481#ifdef DEBUG_HARD
482 pmz_debug("irq, r3: %x\n", r3);
483#endif
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100484 /* Channel A */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 tty = NULL;
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100486 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 write_zsreg(uap_a, R0, RES_H_IUS);
488 zssync(uap_a);
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100489 if (r3 & CHAEXT)
490 pmz_status_handle(uap_a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (r3 & CHARxIP)
David Howells7d12e782006-10-05 14:55:46 +0100492 tty = pmz_receive_chars(uap_a);
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100493 if (r3 & CHATxIP)
494 pmz_transmit_chars(uap_a);
495 rc = IRQ_HANDLED;
496 }
497 spin_unlock(&uap_a->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (tty != NULL)
499 tty_flip_buffer_push(tty);
500
501 if (uap_b->node == NULL)
502 goto out;
503
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100504 spin_lock(&uap_b->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 tty = NULL;
506 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
507 write_zsreg(uap_b, R0, RES_H_IUS);
508 zssync(uap_b);
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100509 if (r3 & CHBEXT)
510 pmz_status_handle(uap_b);
511 if (r3 & CHBRxIP)
512 tty = pmz_receive_chars(uap_b);
513 if (r3 & CHBTxIP)
514 pmz_transmit_chars(uap_b);
515 rc = IRQ_HANDLED;
516 }
517 spin_unlock(&uap_b->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (tty != NULL)
519 tty_flip_buffer_push(tty);
520
521 out:
522#ifdef DEBUG_HARD
523 pmz_debug("irq done.\n");
524#endif
525 return rc;
526}
527
528/*
529 * Peek the status register, lock not held by caller
530 */
531static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
532{
533 unsigned long flags;
534 u8 status;
535
536 spin_lock_irqsave(&uap->port.lock, flags);
537 status = read_zsreg(uap, R0);
538 spin_unlock_irqrestore(&uap->port.lock, flags);
539
540 return status;
541}
542
543/*
544 * Check if transmitter is empty
545 * The port lock is not held.
546 */
547static unsigned int pmz_tx_empty(struct uart_port *port)
548{
549 struct uart_pmac_port *uap = to_pmz(port);
550 unsigned char status;
551
552 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
553 return TIOCSER_TEMT;
554
555 status = pmz_peek_status(to_pmz(port));
556 if (status & Tx_BUF_EMP)
557 return TIOCSER_TEMT;
558 return 0;
559}
560
561/*
562 * Set Modem Control (RTS & DTR) bits
563 * The port lock is held and interrupts are disabled.
564 * Note: Shall we really filter out RTS on external ports or
565 * should that be dealt at higher level only ?
566 */
567static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
568{
569 struct uart_pmac_port *uap = to_pmz(port);
570 unsigned char set_bits, clear_bits;
571
572 /* Do nothing for irda for now... */
573 if (ZS_IS_IRDA(uap))
574 return;
575 /* We get called during boot with a port not up yet */
576 if (ZS_IS_ASLEEP(uap) ||
577 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
578 return;
579
580 set_bits = clear_bits = 0;
581
582 if (ZS_IS_INTMODEM(uap)) {
583 if (mctrl & TIOCM_RTS)
584 set_bits |= RTS;
585 else
586 clear_bits |= RTS;
587 }
588 if (mctrl & TIOCM_DTR)
589 set_bits |= DTR;
590 else
591 clear_bits |= DTR;
592
593 /* NOTE: Not subject to 'transmitter active' rule. */
594 uap->curregs[R5] |= set_bits;
595 uap->curregs[R5] &= ~clear_bits;
596 if (ZS_IS_ASLEEP(uap))
597 return;
598 write_zsreg(uap, R5, uap->curregs[R5]);
599 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
600 set_bits, clear_bits, uap->curregs[R5]);
601 zssync(uap);
602}
603
604/*
605 * Get Modem Control bits (only the input ones, the core will
606 * or that with a cached value of the control ones)
Russell Kingc5f46442005-06-29 09:42:38 +0100607 * The port lock is held and interrupts are disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
609static unsigned int pmz_get_mctrl(struct uart_port *port)
610{
611 struct uart_pmac_port *uap = to_pmz(port);
612 unsigned char status;
613 unsigned int ret;
614
615 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
616 return 0;
617
Russell Kingc5f46442005-06-29 09:42:38 +0100618 status = read_zsreg(uap, R0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 ret = 0;
621 if (status & DCD)
622 ret |= TIOCM_CAR;
623 if (status & SYNC_HUNT)
624 ret |= TIOCM_DSR;
625 if (!(status & CTS))
626 ret |= TIOCM_CTS;
627
628 return ret;
629}
630
631/*
632 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
Russell Kingb129a8c2005-08-31 10:12:14 +0100633 * though for DMA, we will have to do a bit more.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * The port lock is held and interrupts are disabled.
635 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100636static void pmz_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
639}
640
641/*
642 * Kick the Tx side.
643 * The port lock is held and interrupts are disabled.
644 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100645static void pmz_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct uart_pmac_port *uap = to_pmz(port);
648 unsigned char status;
649
650 pmz_debug("pmz: start_tx()\n");
651
652 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
653 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
654
655 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
656 return;
657
658 status = read_zsreg(uap, R0);
659
660 /* TX busy? Just wait for the TX done interrupt. */
661 if (!(status & Tx_BUF_EMP))
662 return;
663
664 /* Send the first character to jump-start the TX done
665 * IRQ sending engine.
666 */
667 if (port->x_char) {
668 write_zsdata(uap, port->x_char);
669 zssync(uap);
670 port->icount.tx++;
671 port->x_char = 0;
672 } else {
Alan Coxebd2c8f2009-09-19 13:13:28 -0700673 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 write_zsdata(uap, xmit->buf[xmit->tail]);
676 zssync(uap);
677 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
678 port->icount.tx++;
679
680 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
681 uart_write_wakeup(&uap->port);
682 }
683 pmz_debug("pmz: start_tx() done.\n");
684}
685
686/*
687 * Stop Rx side, basically disable emitting of
688 * Rx interrupts on the port. We don't disable the rx
689 * side of the chip proper though
690 * The port lock is held.
691 */
692static void pmz_stop_rx(struct uart_port *port)
693{
694 struct uart_pmac_port *uap = to_pmz(port);
695
696 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
697 return;
698
699 pmz_debug("pmz: stop_rx()()\n");
700
701 /* Disable all RX interrupts. */
702 uap->curregs[R1] &= ~RxINT_MASK;
703 pmz_maybe_update_regs(uap);
704
705 pmz_debug("pmz: stop_rx() done.\n");
706}
707
708/*
709 * Enable modem status change interrupts
710 * The port lock is held.
711 */
712static void pmz_enable_ms(struct uart_port *port)
713{
714 struct uart_pmac_port *uap = to_pmz(port);
715 unsigned char new_reg;
716
717 if (ZS_IS_IRDA(uap) || uap->node == NULL)
718 return;
719 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
720 if (new_reg != uap->curregs[R15]) {
721 uap->curregs[R15] = new_reg;
722
723 if (ZS_IS_ASLEEP(uap))
724 return;
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100725 /* NOTE: Not subject to 'transmitter active' rule. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 write_zsreg(uap, R15, uap->curregs[R15]);
727 }
728}
729
730/*
731 * Control break state emission
732 * The port lock is not held.
733 */
734static void pmz_break_ctl(struct uart_port *port, int break_state)
735{
736 struct uart_pmac_port *uap = to_pmz(port);
737 unsigned char set_bits, clear_bits, new_reg;
738 unsigned long flags;
739
740 if (uap->node == NULL)
741 return;
742 set_bits = clear_bits = 0;
743
744 if (break_state)
745 set_bits |= SND_BRK;
746 else
747 clear_bits |= SND_BRK;
748
749 spin_lock_irqsave(&port->lock, flags);
750
751 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
752 if (new_reg != uap->curregs[R5]) {
753 uap->curregs[R5] = new_reg;
754
Finn Thain1f7b5ff2009-11-04 00:40:23 +1100755 /* NOTE: Not subject to 'transmitter active' rule. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ZS_IS_ASLEEP(uap))
757 return;
758 write_zsreg(uap, R5, uap->curregs[R5]);
759 }
760
761 spin_unlock_irqrestore(&port->lock, flags);
762}
763
Finn Thainec9cbe02009-11-17 20:04:44 +1100764#ifdef CONFIG_PPC_PMAC
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * Turn power on or off to the SCC and associated stuff
768 * (port drivers, modem, IR port, etc.)
769 * Returns the number of milliseconds we should wait before
770 * trying to use the port.
771 */
772static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
773{
774 int delay = 0;
775 int rc;
776
777 if (state) {
778 rc = pmac_call_feature(
779 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
780 pmz_debug("port power on result: %d\n", rc);
781 if (ZS_IS_INTMODEM(uap)) {
782 rc = pmac_call_feature(
783 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
784 delay = 2500; /* wait for 2.5s before using */
785 pmz_debug("modem power result: %d\n", rc);
786 }
787 } else {
788 /* TODO: Make that depend on a timer, don't power down
789 * immediately
790 */
791 if (ZS_IS_INTMODEM(uap)) {
792 rc = pmac_call_feature(
793 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
794 pmz_debug("port power off result: %d\n", rc);
795 }
796 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
797 }
798 return delay;
799}
800
Finn Thainec9cbe02009-11-17 20:04:44 +1100801#else
802
803static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
804{
805 return 0;
806}
807
808#endif /* !CONFIG_PPC_PMAC */
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * FixZeroBug....Works around a bug in the SCC receving channel.
812 * Inspired from Darwin code, 15 Sept. 2000 -DanM
813 *
814 * The following sequence prevents a problem that is seen with O'Hare ASICs
815 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
816 * at the input to the receiver becomes 'stuck' and locks up the receiver.
817 * This problem can occur as a result of a zero bit at the receiver input
818 * coincident with any of the following events:
819 *
820 * The SCC is initialized (hardware or software).
821 * A framing error is detected.
822 * The clocking option changes from synchronous or X1 asynchronous
823 * clocking to X16, X32, or X64 asynchronous clocking.
824 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
825 *
826 * This workaround attempts to recover from the lockup condition by placing
827 * the SCC in synchronous loopback mode with a fast clock before programming
828 * any of the asynchronous modes.
829 */
830static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
831{
832 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
833 zssync(uap);
834 udelay(10);
835 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
836 zssync(uap);
837
838 write_zsreg(uap, 4, X1CLK | MONSYNC);
839 write_zsreg(uap, 3, Rx8);
840 write_zsreg(uap, 5, Tx8 | RTS);
841 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
842 write_zsreg(uap, 11, RCBR | TCBR);
843 write_zsreg(uap, 12, 0);
844 write_zsreg(uap, 13, 0);
845 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
846 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
847 write_zsreg(uap, 3, Rx8 | RxENABLE);
848 write_zsreg(uap, 0, RES_EXT_INT);
849 write_zsreg(uap, 0, RES_EXT_INT);
850 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
851
852 /* The channel should be OK now, but it is probably receiving
853 * loopback garbage.
854 * Switch to asynchronous mode, disable the receiver,
855 * and discard everything in the receive buffer.
856 */
857 write_zsreg(uap, 9, NV);
858 write_zsreg(uap, 4, X16CLK | SB_MASK);
859 write_zsreg(uap, 3, Rx8);
860
861 while (read_zsreg(uap, 0) & Rx_CH_AV) {
862 (void)read_zsreg(uap, 8);
863 write_zsreg(uap, 0, RES_EXT_INT);
864 write_zsreg(uap, 0, ERR_RES);
865 }
866}
867
868/*
869 * Real startup routine, powers up the hardware and sets up
870 * the SCC. Returns a delay in ms where you need to wait before
871 * actually using the port, this is typically the internal modem
872 * powerup delay. This routine expect the lock to be taken.
873 */
874static int __pmz_startup(struct uart_pmac_port *uap)
875{
876 int pwr_delay = 0;
877
878 memset(&uap->curregs, 0, sizeof(uap->curregs));
879
880 /* Power up the SCC & underlying hardware (modem/irda) */
881 pwr_delay = pmz_set_scc_power(uap, 1);
882
883 /* Nice buggy HW ... */
884 pmz_fix_zero_bug_scc(uap);
885
886 /* Reset the channel */
887 uap->curregs[R9] = 0;
888 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
889 zssync(uap);
890 udelay(10);
891 write_zsreg(uap, 9, 0);
892 zssync(uap);
893
894 /* Clear the interrupt registers */
895 write_zsreg(uap, R1, 0);
896 write_zsreg(uap, R0, ERR_RES);
897 write_zsreg(uap, R0, ERR_RES);
898 write_zsreg(uap, R0, RES_H_IUS);
899 write_zsreg(uap, R0, RES_H_IUS);
900
901 /* Setup some valid baud rate */
902 uap->curregs[R4] = X16CLK | SB1;
903 uap->curregs[R3] = Rx8;
904 uap->curregs[R5] = Tx8 | RTS;
905 if (!ZS_IS_IRDA(uap))
906 uap->curregs[R5] |= DTR;
907 uap->curregs[R12] = 0;
908 uap->curregs[R13] = 0;
909 uap->curregs[R14] = BRENAB;
910
911 /* Clear handshaking, enable BREAK interrupts */
912 uap->curregs[R15] = BRKIE;
913
914 /* Master interrupt enable */
915 uap->curregs[R9] |= NV | MIE;
916
917 pmz_load_zsregs(uap, uap->curregs);
918
919 /* Enable receiver and transmitter. */
920 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
921 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
922
923 /* Remember status for DCD/CTS changes */
924 uap->prev_status = read_zsreg(uap, R0);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return pwr_delay;
927}
928
929static void pmz_irda_reset(struct uart_pmac_port *uap)
930{
931 uap->curregs[R5] |= DTR;
932 write_zsreg(uap, R5, uap->curregs[R5]);
933 zssync(uap);
934 mdelay(110);
935 uap->curregs[R5] &= ~DTR;
936 write_zsreg(uap, R5, uap->curregs[R5]);
937 zssync(uap);
938 mdelay(10);
939}
940
941/*
942 * This is the "normal" startup routine, using the above one
943 * wrapped with the lock and doing a schedule delay
944 */
945static int pmz_startup(struct uart_port *port)
946{
947 struct uart_pmac_port *uap = to_pmz(port);
948 unsigned long flags;
949 int pwr_delay = 0;
950
951 pmz_debug("pmz: startup()\n");
952
953 if (ZS_IS_ASLEEP(uap))
954 return -EAGAIN;
955 if (uap->node == NULL)
956 return -ENODEV;
957
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000958 mutex_lock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
961
962 /* A console is never powered down. Else, power up and
963 * initialize the chip
964 */
965 if (!ZS_IS_CONS(uap)) {
966 spin_lock_irqsave(&port->lock, flags);
967 pwr_delay = __pmz_startup(uap);
968 spin_unlock_irqrestore(&port->lock, flags);
969 }
970
971 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
Finn Thainec9cbe02009-11-17 20:04:44 +1100972 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED,
973 "SCC", uap)) {
974 pmz_error("Unable to register zs interrupt handler.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 pmz_set_scc_power(uap, 0);
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000976 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENXIO;
978 }
979
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000980 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* Right now, we deal with delay by blocking here, I'll be
983 * smarter later on
984 */
985 if (pwr_delay != 0) {
986 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
987 msleep(pwr_delay);
988 }
989
990 /* IrDA reset is done now */
991 if (ZS_IS_IRDA(uap))
992 pmz_irda_reset(uap);
993
994 /* Enable interrupts emission from the chip */
995 spin_lock_irqsave(&port->lock, flags);
996 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
997 if (!ZS_IS_EXTCLK(uap))
998 uap->curregs[R1] |= EXT_INT_ENAB;
999 write_zsreg(uap, R1, uap->curregs[R1]);
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001000 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 pmz_debug("pmz: startup() done.\n");
1003
1004 return 0;
1005}
1006
1007static void pmz_shutdown(struct uart_port *port)
1008{
1009 struct uart_pmac_port *uap = to_pmz(port);
1010 unsigned long flags;
1011
1012 pmz_debug("pmz: shutdown()\n");
1013
1014 if (uap->node == NULL)
1015 return;
1016
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001017 mutex_lock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* Release interrupt handler */
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001020 free_irq(uap->port.irq, uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 spin_lock_irqsave(&port->lock, flags);
1023
1024 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1025
1026 if (!ZS_IS_OPEN(uap->mate))
1027 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1028
1029 /* Disable interrupts */
1030 if (!ZS_IS_ASLEEP(uap)) {
1031 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1032 write_zsreg(uap, R1, uap->curregs[R1]);
1033 zssync(uap);
1034 }
1035
1036 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1037 spin_unlock_irqrestore(&port->lock, flags);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001038 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return;
1040 }
1041
1042 /* Disable receiver and transmitter. */
1043 uap->curregs[R3] &= ~RxENABLE;
1044 uap->curregs[R5] &= ~TxENABLE;
1045
1046 /* Disable all interrupts and BRK assertion. */
1047 uap->curregs[R5] &= ~SND_BRK;
1048 pmz_maybe_update_regs(uap);
1049
1050 /* Shut the chip down */
1051 pmz_set_scc_power(uap, 0);
1052
1053 spin_unlock_irqrestore(&port->lock, flags);
1054
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001055 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 pmz_debug("pmz: shutdown() done.\n");
1058}
1059
1060/* Shared by TTY driver and serial console setup. The port lock is held
1061 * and local interrupts are disabled.
1062 */
1063static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1064 unsigned int iflag, unsigned long baud)
1065{
1066 int brg;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Switch to external clocking for IrDA high clock rates. That
1069 * code could be re-used for Midi interfaces with different
1070 * multipliers
1071 */
1072 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1073 uap->curregs[R4] = X1CLK;
1074 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1075 uap->curregs[R14] = 0; /* BRG off */
1076 uap->curregs[R12] = 0;
1077 uap->curregs[R13] = 0;
1078 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1079 } else {
1080 switch (baud) {
1081 case ZS_CLOCK/16: /* 230400 */
1082 uap->curregs[R4] = X16CLK;
1083 uap->curregs[R11] = 0;
1084 uap->curregs[R14] = 0;
1085 break;
1086 case ZS_CLOCK/32: /* 115200 */
1087 uap->curregs[R4] = X32CLK;
1088 uap->curregs[R11] = 0;
1089 uap->curregs[R14] = 0;
1090 break;
1091 default:
1092 uap->curregs[R4] = X16CLK;
1093 uap->curregs[R11] = TCBR | RCBR;
1094 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1095 uap->curregs[R12] = (brg & 255);
1096 uap->curregs[R13] = ((brg >> 8) & 255);
1097 uap->curregs[R14] = BRENAB;
1098 }
1099 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1100 }
1101
1102 /* Character size, stop bits, and parity. */
1103 uap->curregs[3] &= ~RxN_MASK;
1104 uap->curregs[5] &= ~TxN_MASK;
1105
1106 switch (cflag & CSIZE) {
1107 case CS5:
1108 uap->curregs[3] |= Rx5;
1109 uap->curregs[5] |= Tx5;
1110 uap->parity_mask = 0x1f;
1111 break;
1112 case CS6:
1113 uap->curregs[3] |= Rx6;
1114 uap->curregs[5] |= Tx6;
1115 uap->parity_mask = 0x3f;
1116 break;
1117 case CS7:
1118 uap->curregs[3] |= Rx7;
1119 uap->curregs[5] |= Tx7;
1120 uap->parity_mask = 0x7f;
1121 break;
1122 case CS8:
1123 default:
1124 uap->curregs[3] |= Rx8;
1125 uap->curregs[5] |= Tx8;
1126 uap->parity_mask = 0xff;
1127 break;
1128 };
1129 uap->curregs[4] &= ~(SB_MASK);
1130 if (cflag & CSTOPB)
1131 uap->curregs[4] |= SB2;
1132 else
1133 uap->curregs[4] |= SB1;
1134 if (cflag & PARENB)
1135 uap->curregs[4] |= PAR_ENAB;
1136 else
1137 uap->curregs[4] &= ~PAR_ENAB;
1138 if (!(cflag & PARODD))
1139 uap->curregs[4] |= PAR_EVEN;
1140 else
1141 uap->curregs[4] &= ~PAR_EVEN;
1142
1143 uap->port.read_status_mask = Rx_OVR;
1144 if (iflag & INPCK)
1145 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1146 if (iflag & (BRKINT | PARMRK))
1147 uap->port.read_status_mask |= BRK_ABRT;
1148
1149 uap->port.ignore_status_mask = 0;
1150 if (iflag & IGNPAR)
1151 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1152 if (iflag & IGNBRK) {
1153 uap->port.ignore_status_mask |= BRK_ABRT;
1154 if (iflag & IGNPAR)
1155 uap->port.ignore_status_mask |= Rx_OVR;
1156 }
1157
1158 if ((cflag & CREAD) == 0)
1159 uap->port.ignore_status_mask = 0xff;
1160}
1161
1162
1163/*
1164 * Set the irda codec on the imac to the specified baud rate.
1165 */
1166static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1167{
1168 u8 cmdbyte;
1169 int t, version;
1170
1171 switch (*baud) {
1172 /* SIR modes */
1173 case 2400:
1174 cmdbyte = 0x53;
1175 break;
1176 case 4800:
1177 cmdbyte = 0x52;
1178 break;
1179 case 9600:
1180 cmdbyte = 0x51;
1181 break;
1182 case 19200:
1183 cmdbyte = 0x50;
1184 break;
1185 case 38400:
1186 cmdbyte = 0x4f;
1187 break;
1188 case 57600:
1189 cmdbyte = 0x4e;
1190 break;
1191 case 115200:
1192 cmdbyte = 0x4d;
1193 break;
1194 /* The FIR modes aren't really supported at this point, how
1195 * do we select the speed ? via the FCR on KeyLargo ?
1196 */
1197 case 1152000:
1198 cmdbyte = 0;
1199 break;
1200 case 4000000:
1201 cmdbyte = 0;
1202 break;
1203 default: /* 9600 */
1204 cmdbyte = 0x51;
1205 *baud = 9600;
1206 break;
1207 }
1208
1209 /* Wait for transmitter to drain */
1210 t = 10000;
1211 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1212 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1213 if (--t <= 0) {
Finn Thainec9cbe02009-11-17 20:04:44 +11001214 pmz_error("transmitter didn't drain\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return;
1216 }
1217 udelay(10);
1218 }
1219
1220 /* Drain the receiver too */
1221 t = 100;
1222 (void)read_zsdata(uap);
1223 (void)read_zsdata(uap);
1224 (void)read_zsdata(uap);
1225 mdelay(10);
1226 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1227 read_zsdata(uap);
1228 mdelay(10);
1229 if (--t <= 0) {
Finn Thainec9cbe02009-11-17 20:04:44 +11001230 pmz_error("receiver didn't drain\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233 }
1234
1235 /* Switch to command mode */
1236 uap->curregs[R5] |= DTR;
1237 write_zsreg(uap, R5, uap->curregs[R5]);
1238 zssync(uap);
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001239 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Switch SCC to 19200 */
1242 pmz_convert_to_zs(uap, CS8, 0, 19200);
1243 pmz_load_zsregs(uap, uap->curregs);
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001244 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* Write get_version command byte */
1247 write_zsdata(uap, 1);
1248 t = 5000;
1249 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1250 if (--t <= 0) {
Finn Thainec9cbe02009-11-17 20:04:44 +11001251 pmz_error("irda_setup timed out on get_version byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out;
1253 }
1254 udelay(10);
1255 }
1256 version = read_zsdata(uap);
1257
1258 if (version < 4) {
Finn Thainec9cbe02009-11-17 20:04:44 +11001259 pmz_info("IrDA: dongle version %d not supported\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto out;
1261 }
1262
1263 /* Send speed mode */
1264 write_zsdata(uap, cmdbyte);
1265 t = 5000;
1266 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1267 if (--t <= 0) {
Finn Thainec9cbe02009-11-17 20:04:44 +11001268 pmz_error("irda_setup timed out on speed mode byte\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto out;
1270 }
1271 udelay(10);
1272 }
1273 t = read_zsdata(uap);
1274 if (t != cmdbyte)
Finn Thainec9cbe02009-11-17 20:04:44 +11001275 pmz_error("irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Finn Thainec9cbe02009-11-17 20:04:44 +11001277 pmz_info("IrDA setup for %ld bps, dongle version: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *baud, version);
1279
1280 (void)read_zsdata(uap);
1281 (void)read_zsdata(uap);
1282 (void)read_zsdata(uap);
1283
1284 out:
1285 /* Switch back to data mode */
1286 uap->curregs[R5] &= ~DTR;
1287 write_zsreg(uap, R5, uap->curregs[R5]);
1288 zssync(uap);
1289
1290 (void)read_zsdata(uap);
1291 (void)read_zsdata(uap);
1292 (void)read_zsdata(uap);
1293}
1294
1295
Alan Cox606d0992006-12-08 02:38:45 -08001296static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1297 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct uart_pmac_port *uap = to_pmz(port);
1300 unsigned long baud;
1301
1302 pmz_debug("pmz: set_termios()\n");
1303
1304 if (ZS_IS_ASLEEP(uap))
1305 return;
1306
Alan Cox606d0992006-12-08 02:38:45 -08001307 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1310 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1311 * about the FIR mode and high speed modes. So these are unused. For
1312 * implementing proper support for these, we should probably add some
1313 * DMA as well, at least on the Rx side, which isn't a simple thing
1314 * at this point.
1315 */
1316 if (ZS_IS_IRDA(uap)) {
1317 /* Calc baud rate */
1318 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1319 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1320 /* Cet the irda codec to the right rate */
1321 pmz_irda_setup(uap, &baud);
1322 /* Set final baud rate */
1323 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1324 pmz_load_zsregs(uap, uap->curregs);
1325 zssync(uap);
1326 } else {
1327 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1328 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1329 /* Make sure modem status interrupts are correctly configured */
1330 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1331 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1332 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1333 } else {
1334 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1335 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1336 }
1337
1338 /* Load registers to the chip */
1339 pmz_maybe_update_regs(uap);
1340 }
1341 uart_update_timeout(port, termios->c_cflag, baud);
1342
1343 pmz_debug("pmz: set_termios() done.\n");
1344}
1345
1346/* The port lock is not held. */
Alan Cox606d0992006-12-08 02:38:45 -08001347static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1348 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct uart_pmac_port *uap = to_pmz(port);
1351 unsigned long flags;
1352
1353 spin_lock_irqsave(&port->lock, flags);
1354
1355 /* Disable IRQs on the port */
1356 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1357 write_zsreg(uap, R1, uap->curregs[R1]);
1358
1359 /* Setup new port configuration */
1360 __pmz_set_termios(port, termios, old);
1361
1362 /* Re-enable IRQs on the port */
1363 if (ZS_IS_OPEN(uap)) {
1364 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1365 if (!ZS_IS_EXTCLK(uap))
1366 uap->curregs[R1] |= EXT_INT_ENAB;
1367 write_zsreg(uap, R1, uap->curregs[R1]);
1368 }
1369 spin_unlock_irqrestore(&port->lock, flags);
1370}
1371
1372static const char *pmz_type(struct uart_port *port)
1373{
1374 struct uart_pmac_port *uap = to_pmz(port);
1375
1376 if (ZS_IS_IRDA(uap))
1377 return "Z85c30 ESCC - Infrared port";
1378 else if (ZS_IS_INTMODEM(uap))
1379 return "Z85c30 ESCC - Internal modem";
1380 return "Z85c30 ESCC - Serial port";
1381}
1382
1383/* We do not request/release mappings of the registers here, this
1384 * happens at early serial probe time.
1385 */
1386static void pmz_release_port(struct uart_port *port)
1387{
1388}
1389
1390static int pmz_request_port(struct uart_port *port)
1391{
1392 return 0;
1393}
1394
1395/* These do not need to do anything interesting either. */
1396static void pmz_config_port(struct uart_port *port, int flags)
1397{
1398}
1399
1400/* We do not support letting the user mess with the divisor, IRQ, etc. */
1401static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1402{
1403 return -EINVAL;
1404}
1405
Chris J Arges8c653182008-11-04 12:19:06 +00001406#ifdef CONFIG_CONSOLE_POLL
1407
1408static int pmz_poll_get_char(struct uart_port *port)
1409{
1410 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1411
1412 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1413 udelay(5);
1414 return read_zsdata(uap);
1415}
1416
1417static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1418{
1419 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1420
1421 /* Wait for the transmit buffer to empty. */
1422 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1423 udelay(5);
1424 write_zsdata(uap, c);
1425}
1426
Finn Thainec9cbe02009-11-17 20:04:44 +11001427#endif /* CONFIG_CONSOLE_POLL */
Chris J Arges8c653182008-11-04 12:19:06 +00001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429static struct uart_ops pmz_pops = {
1430 .tx_empty = pmz_tx_empty,
1431 .set_mctrl = pmz_set_mctrl,
1432 .get_mctrl = pmz_get_mctrl,
1433 .stop_tx = pmz_stop_tx,
1434 .start_tx = pmz_start_tx,
1435 .stop_rx = pmz_stop_rx,
1436 .enable_ms = pmz_enable_ms,
1437 .break_ctl = pmz_break_ctl,
1438 .startup = pmz_startup,
1439 .shutdown = pmz_shutdown,
1440 .set_termios = pmz_set_termios,
1441 .type = pmz_type,
1442 .release_port = pmz_release_port,
1443 .request_port = pmz_request_port,
1444 .config_port = pmz_config_port,
1445 .verify_port = pmz_verify_port,
Chris J Arges8c653182008-11-04 12:19:06 +00001446#ifdef CONFIG_CONSOLE_POLL
1447 .poll_get_char = pmz_poll_get_char,
1448 .poll_put_char = pmz_poll_put_char,
1449#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450};
1451
Finn Thainec9cbe02009-11-17 20:04:44 +11001452#ifdef CONFIG_PPC_PMAC
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/*
1455 * Setup one port structure after probing, HW is down at this point,
1456 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1457 * register our console before uart_add_one_port() is called
1458 */
1459static int __init pmz_init_port(struct uart_pmac_port *uap)
1460{
1461 struct device_node *np = uap->node;
Jeremy Kerr018a3d12006-07-12 15:40:29 +10001462 const char *conn;
1463 const struct slot_names_prop {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int count;
1465 char name[1];
1466 } *slots;
1467 int len;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001468 struct resource r_ports, r_rxdma, r_txdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 /*
1471 * Request & map chip registers
1472 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001473 if (of_address_to_resource(np, 0, &r_ports))
1474 return -ENODEV;
1475 uap->port.mapbase = r_ports.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 uap->control_reg = uap->port.membase;
1479 uap->data_reg = uap->control_reg + 0x10;
1480
1481 /*
1482 * Request & map DBDMA registers
1483 */
1484#ifdef HAS_DBDMA
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001485 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1486 of_address_to_resource(np, 2, &r_rxdma) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001488#else
1489 memset(&r_txdma, 0, sizeof(struct resource));
1490 memset(&r_rxdma, 0, sizeof(struct resource));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491#endif
1492 if (ZS_HAS_DMA(uap)) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001493 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (uap->tx_dma_regs == NULL) {
1495 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1496 goto no_dma;
1497 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001498 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (uap->rx_dma_regs == NULL) {
1500 iounmap(uap->tx_dma_regs);
1501 uap->tx_dma_regs = NULL;
1502 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1503 goto no_dma;
1504 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001505 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1506 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508no_dma:
1509
1510 /*
1511 * Detect port type
1512 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001513 if (of_device_is_compatible(np, "cobalt"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001515 conn = of_get_property(np, "AAPL,connector", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (conn && (strcmp(conn, "infrared") == 0))
1517 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1518 uap->port_type = PMAC_SCC_ASYNC;
1519 /* 1999 Powerbook G3 has slot-names property instead */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001520 slots = of_get_property(np, "slot-names", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (slots && slots->count > 0) {
1522 if (strcmp(slots->name, "IrDA") == 0)
1523 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1524 else if (strcmp(slots->name, "Modem") == 0)
1525 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1526 }
1527 if (ZS_IS_IRDA(uap))
1528 uap->port_type = PMAC_SCC_IRDA;
1529 if (ZS_IS_INTMODEM(uap)) {
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001530 struct device_node* i2c_modem =
1531 of_find_node_by_name(NULL, "i2c-modem");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (i2c_modem) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +10001533 const char* mid =
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001534 of_get_property(i2c_modem, "modem-id", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (mid) switch(*mid) {
1536 case 0x04 :
1537 case 0x05 :
1538 case 0x07 :
1539 case 0x08 :
1540 case 0x0b :
1541 case 0x0c :
1542 uap->port_type = PMAC_SCC_I2S1;
1543 }
1544 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1545 mid ? (*mid) : 0);
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001546 of_node_put(i2c_modem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 } else {
1548 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1549 }
1550 }
1551
1552 /*
1553 * Init remaining bits of "port" structure
1554 */
Russell King9b4a1612006-02-05 10:48:10 +00001555 uap->port.iotype = UPIO_MEM;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001556 uap->port.irq = irq_of_parse_and_map(np, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 uap->port.uartclk = ZS_CLOCK;
1558 uap->port.fifosize = 1;
1559 uap->port.ops = &pmz_pops;
1560 uap->port.type = PORT_PMAC_ZILOG;
1561 uap->port.flags = 0;
1562
Benjamin Herrenschmidtf08b7e92009-03-30 17:34:04 +00001563 /*
1564 * Fixup for the port on Gatwick for which the device-tree has
1565 * missing interrupts. Normally, the macio_dev would contain
1566 * fixed up interrupt info, but we use the device-tree directly
1567 * here due to early probing so we need the fixup too.
1568 */
1569 if (uap->port.irq == NO_IRQ &&
1570 np->parent && np->parent->parent &&
1571 of_device_is_compatible(np->parent->parent, "gatwick")) {
1572 /* IRQs on gatwick are offset by 64 */
1573 uap->port.irq = irq_create_mapping(NULL, 64 + 15);
1574 uap->tx_dma_irq = irq_create_mapping(NULL, 64 + 4);
1575 uap->rx_dma_irq = irq_create_mapping(NULL, 64 + 5);
1576 }
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* Setup some valid baud rate information in the register
1579 * shadows so we don't write crap there before baud rate is
1580 * first initialized.
1581 */
1582 pmz_convert_to_zs(uap, CS8, 0, 9600);
1583
1584 return 0;
1585}
1586
1587/*
1588 * Get rid of a port on module removal
1589 */
1590static void pmz_dispose_port(struct uart_pmac_port *uap)
1591{
1592 struct device_node *np;
1593
1594 np = uap->node;
1595 iounmap(uap->rx_dma_regs);
1596 iounmap(uap->tx_dma_regs);
1597 iounmap(uap->control_reg);
1598 uap->node = NULL;
1599 of_node_put(np);
1600 memset(uap, 0, sizeof(struct uart_pmac_port));
1601}
1602
1603/*
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001604 * Called upon match with an escc node in the device-tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Jeff Mahoney5e655772005-07-06 15:44:41 -04001606static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 int i;
1609
1610 /* Iterate the pmz_ports array to find a matching entry
1611 */
1612 for (i = 0; i < MAX_ZS_PORTS; i++)
1613 if (pmz_ports[i].node == mdev->ofdev.node) {
1614 struct uart_pmac_port *uap = &pmz_ports[i];
1615
1616 uap->dev = mdev;
1617 dev_set_drvdata(&mdev->ofdev.dev, uap);
1618 if (macio_request_resources(uap->dev, "pmac_zilog"))
1619 printk(KERN_WARNING "%s: Failed to request resource"
1620 ", port still active\n",
1621 uap->node->name);
1622 else
1623 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1624 return 0;
1625 }
1626 return -ENODEV;
1627}
1628
1629/*
1630 * That one should not be called, macio isn't really a hotswap device,
1631 * we don't expect one of those serial ports to go away...
1632 */
1633static int pmz_detach(struct macio_dev *mdev)
1634{
1635 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1636
1637 if (!uap)
1638 return -ENODEV;
1639
1640 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1641 macio_release_resources(uap->dev);
1642 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1643 }
1644 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1645 uap->dev = NULL;
1646
1647 return 0;
1648}
1649
1650
Pavel Machek0370aff2005-04-16 15:25:35 -07001651static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1654 struct uart_state *state;
1655 unsigned long flags;
1656
1657 if (uap == NULL) {
1658 printk("HRM... pmz_suspend with NULL uap\n");
1659 return 0;
1660 }
1661
Pavel Machekca078ba2005-09-03 15:56:57 -07001662 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664
Olaf Heringaa015422007-08-26 19:10:59 +10001665 pmz_debug("suspend, switching to state %d\n", pm_state.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 state = pmz_uart_reg.state + uap->port.line;
1668
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001669 mutex_lock(&pmz_irq_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07001670 mutex_lock(&state->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 spin_lock_irqsave(&uap->port.lock, flags);
1673
1674 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1675 /* Disable receiver and transmitter. */
1676 uap->curregs[R3] &= ~RxENABLE;
1677 uap->curregs[R5] &= ~TxENABLE;
1678
1679 /* Disable all interrupts and BRK assertion. */
1680 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1681 uap->curregs[R5] &= ~SND_BRK;
1682 pmz_load_zsregs(uap, uap->curregs);
1683 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1684 mb();
1685 }
1686
1687 spin_unlock_irqrestore(&uap->port.lock, flags);
1688
1689 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1690 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1691 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1692 disable_irq(uap->port.irq);
1693 }
1694
1695 if (ZS_IS_CONS(uap))
1696 uap->port.cons->flags &= ~CON_ENABLED;
1697
1698 /* Shut the chip down */
1699 pmz_set_scc_power(uap, 0);
1700
Alan Coxa2bceae2009-09-19 13:13:31 -07001701 mutex_unlock(&state->port.mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001702 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 pmz_debug("suspend, switching complete\n");
1705
1706 mdev->ofdev.dev.power.power_state = pm_state;
1707
1708 return 0;
1709}
1710
1711
1712static int pmz_resume(struct macio_dev *mdev)
1713{
1714 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1715 struct uart_state *state;
1716 unsigned long flags;
1717 int pwr_delay = 0;
1718
1719 if (uap == NULL)
1720 return 0;
1721
Pavel Machekca078ba2005-09-03 15:56:57 -07001722 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724
1725 pmz_debug("resume, switching to state 0\n");
1726
1727 state = pmz_uart_reg.state + uap->port.line;
1728
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001729 mutex_lock(&pmz_irq_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07001730 mutex_lock(&state->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 spin_lock_irqsave(&uap->port.lock, flags);
1733 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1734 spin_unlock_irqrestore(&uap->port.lock, flags);
1735 goto bail;
1736 }
1737 pwr_delay = __pmz_startup(uap);
1738
1739 /* Take care of config that may have changed while asleep */
1740 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1741
1742 if (ZS_IS_OPEN(uap)) {
1743 /* Enable interrupts */
1744 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1745 if (!ZS_IS_EXTCLK(uap))
1746 uap->curregs[R1] |= EXT_INT_ENAB;
1747 write_zsreg(uap, R1, uap->curregs[R1]);
1748 }
1749
1750 spin_unlock_irqrestore(&uap->port.lock, flags);
1751
1752 if (ZS_IS_CONS(uap))
1753 uap->port.cons->flags |= CON_ENABLED;
1754
1755 /* Re-enable IRQ on the controller */
1756 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1757 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1758 enable_irq(uap->port.irq);
1759 }
1760
1761 bail:
Alan Coxa2bceae2009-09-19 13:13:31 -07001762 mutex_unlock(&state->port.mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001763 mutex_unlock(&pmz_irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 /* Right now, we deal with delay by blocking here, I'll be
1766 * smarter later on
1767 */
1768 if (pwr_delay != 0) {
1769 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1770 msleep(pwr_delay);
1771 }
1772
1773 pmz_debug("resume, switching complete\n");
1774
Pavel Machekca078ba2005-09-03 15:56:57 -07001775 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 return 0;
1778}
1779
1780/*
1781 * Probe all ports in the system and build the ports array, we register
1782 * with the serial layer at this point, the macio-type probing is only
1783 * used later to "attach" to the sysfs tree so we get power management
1784 * events
1785 */
1786static int __init pmz_probe(void)
1787{
1788 struct device_node *node_p, *node_a, *node_b, *np;
1789 int count = 0;
1790 int rc;
1791
1792 /*
1793 * Find all escc chips in the system
1794 */
1795 node_p = of_find_node_by_name(NULL, "escc");
1796 while (node_p) {
1797 /*
1798 * First get channel A/B node pointers
1799 *
1800 * TODO: Add routines with proper locking to do that...
1801 */
1802 node_a = node_b = NULL;
1803 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1804 if (strncmp(np->name, "ch-a", 4) == 0)
1805 node_a = of_node_get(np);
1806 else if (strncmp(np->name, "ch-b", 4) == 0)
1807 node_b = of_node_get(np);
1808 }
1809 if (!node_a && !node_b) {
1810 of_node_put(node_a);
1811 of_node_put(node_b);
1812 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1813 (!node_a) ? 'a' : 'b', node_p->full_name);
1814 goto next;
1815 }
1816
1817 /*
1818 * Fill basic fields in the port structures
1819 */
1820 pmz_ports[count].mate = &pmz_ports[count+1];
1821 pmz_ports[count+1].mate = &pmz_ports[count];
1822 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1823 pmz_ports[count].node = node_a;
1824 pmz_ports[count+1].node = node_b;
1825 pmz_ports[count].port.line = count;
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001826 pmz_ports[count+1].port.line = count+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /*
1829 * Setup the ports for real
1830 */
1831 rc = pmz_init_port(&pmz_ports[count]);
1832 if (rc == 0 && node_b != NULL)
1833 rc = pmz_init_port(&pmz_ports[count+1]);
1834 if (rc != 0) {
1835 of_node_put(node_a);
1836 of_node_put(node_b);
1837 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1838 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1839 goto next;
1840 }
1841 count += 2;
1842next:
1843 node_p = of_find_node_by_name(node_p, "escc");
1844 }
1845 pmz_ports_count = count;
1846
1847 return 0;
1848}
1849
Finn Thainec9cbe02009-11-17 20:04:44 +11001850#else
1851
1852extern struct platform_device scc_a_pdev, scc_b_pdev;
1853
1854static int __init pmz_init_port(struct uart_pmac_port *uap)
1855{
1856 struct resource *r_ports;
1857 int irq;
1858
1859 r_ports = platform_get_resource(uap->node, IORESOURCE_MEM, 0);
1860 irq = platform_get_irq(uap->node, 0);
1861 if (!r_ports || !irq)
1862 return -ENODEV;
1863
1864 uap->port.mapbase = r_ports->start;
1865 uap->port.membase = (unsigned char __iomem *) r_ports->start;
1866 uap->port.iotype = UPIO_MEM;
1867 uap->port.irq = irq;
1868 uap->port.uartclk = ZS_CLOCK;
1869 uap->port.fifosize = 1;
1870 uap->port.ops = &pmz_pops;
1871 uap->port.type = PORT_PMAC_ZILOG;
1872 uap->port.flags = 0;
1873
1874 uap->control_reg = uap->port.membase;
1875 uap->data_reg = uap->control_reg + 4;
1876 uap->port_type = 0;
1877
1878 pmz_convert_to_zs(uap, CS8, 0, 9600);
1879
1880 return 0;
1881}
1882
1883static int __init pmz_probe(void)
1884{
1885 int err;
1886
1887 pmz_ports_count = 0;
1888
1889 pmz_ports[0].mate = &pmz_ports[1];
1890 pmz_ports[0].port.line = 0;
1891 pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1892 pmz_ports[0].node = &scc_a_pdev;
1893 err = pmz_init_port(&pmz_ports[0]);
1894 if (err)
1895 return err;
1896 pmz_ports_count++;
1897
1898 pmz_ports[1].mate = &pmz_ports[0];
1899 pmz_ports[1].port.line = 1;
1900 pmz_ports[1].flags = 0;
1901 pmz_ports[1].node = &scc_b_pdev;
1902 err = pmz_init_port(&pmz_ports[1]);
1903 if (err)
1904 return err;
1905 pmz_ports_count++;
1906
1907 return 0;
1908}
1909
1910static void pmz_dispose_port(struct uart_pmac_port *uap)
1911{
1912 memset(uap, 0, sizeof(struct uart_pmac_port));
1913}
1914
1915static int __init pmz_attach(struct platform_device *pdev)
1916{
1917 int i;
1918
1919 for (i = 0; i < pmz_ports_count; i++)
1920 if (pmz_ports[i].node == pdev)
1921 return 0;
1922 return -ENODEV;
1923}
1924
1925static int __exit pmz_detach(struct platform_device *pdev)
1926{
1927 return 0;
1928}
1929
1930#endif /* !CONFIG_PPC_PMAC */
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1933
1934static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1935static int __init pmz_console_setup(struct console *co, char *options);
1936
1937static struct console pmz_console = {
David Woodhousee4533b22007-04-05 00:19:43 +10001938 .name = PMACZILOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 .write = pmz_console_write,
1940 .device = uart_console_device,
1941 .setup = pmz_console_setup,
1942 .flags = CON_PRINTBUFFER,
1943 .index = -1,
1944 .data = &pmz_uart_reg,
1945};
1946
1947#define PMACZILOG_CONSOLE &pmz_console
1948#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1949#define PMACZILOG_CONSOLE (NULL)
1950#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1951
1952/*
1953 * Register the driver, console driver and ports with the serial
1954 * core
1955 */
1956static int __init pmz_register(void)
1957{
1958 int i, rc;
1959
1960 pmz_uart_reg.nr = pmz_ports_count;
1961 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /*
1964 * Register this driver with the serial core
1965 */
1966 rc = uart_register_driver(&pmz_uart_reg);
1967 if (rc)
1968 return rc;
1969
1970 /*
1971 * Register each port with the serial core
1972 */
1973 for (i = 0; i < pmz_ports_count; i++) {
1974 struct uart_pmac_port *uport = &pmz_ports[i];
1975 /* NULL node may happen on wallstreet */
1976 if (uport->node != NULL)
1977 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1978 if (rc)
1979 goto err_out;
1980 }
1981
1982 return 0;
1983err_out:
1984 while (i-- > 0) {
1985 struct uart_pmac_port *uport = &pmz_ports[i];
1986 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1987 }
1988 uart_unregister_driver(&pmz_uart_reg);
1989 return rc;
1990}
1991
Finn Thainec9cbe02009-11-17 20:04:44 +11001992#ifdef CONFIG_PPC_PMAC
1993
Jeff Mahoney5e655772005-07-06 15:44:41 -04001994static struct of_device_id pmz_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 {
Finn Thain1f7b5ff2009-11-04 00:40:23 +11001997 .name = "ch-a",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 },
1999 {
Finn Thain1f7b5ff2009-11-04 00:40:23 +11002000 .name = "ch-b",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 },
2002 {},
2003};
Jeff Mahoney5e655772005-07-06 15:44:41 -04002004MODULE_DEVICE_TABLE (of, pmz_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Finn Thain1f7b5ff2009-11-04 00:40:23 +11002006static struct macio_driver pmz_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 .name = "pmac_zilog",
2008 .match_table = pmz_match,
2009 .probe = pmz_attach,
2010 .remove = pmz_detach,
2011 .suspend = pmz_suspend,
Finn Thain1f7b5ff2009-11-04 00:40:23 +11002012 .resume = pmz_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013};
2014
Finn Thainec9cbe02009-11-17 20:04:44 +11002015#else
2016
2017static struct platform_driver pmz_driver = {
2018 .remove = __exit_p(pmz_detach),
2019 .driver = {
2020 .name = "scc",
2021 .owner = THIS_MODULE,
2022 },
2023};
2024
2025#endif /* !CONFIG_PPC_PMAC */
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027static int __init init_pmz(void)
2028{
2029 int rc, i;
2030 printk(KERN_INFO "%s\n", version);
2031
2032 /*
2033 * First, we need to do a direct OF-based probe pass. We
2034 * do that because we want serial console up before the
2035 * macio stuffs calls us back, and since that makes it
2036 * easier to pass the proper number of channels to
2037 * uart_register_driver()
2038 */
2039 if (pmz_ports_count == 0)
2040 pmz_probe();
2041
2042 /*
2043 * Bail early if no port found
2044 */
2045 if (pmz_ports_count == 0)
2046 return -ENODEV;
2047
2048 /*
2049 * Now we register with the serial layer
2050 */
2051 rc = pmz_register();
2052 if (rc) {
2053 printk(KERN_ERR
2054 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
2055 "pmac_zilog: Did another serial driver already claim the minors?\n");
2056 /* effectively "pmz_unprobe()" */
2057 for (i=0; i < pmz_ports_count; i++)
2058 pmz_dispose_port(&pmz_ports[i]);
2059 return rc;
2060 }
Finn Thain1f7b5ff2009-11-04 00:40:23 +11002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Then we register the macio driver itself
2064 */
Finn Thainec9cbe02009-11-17 20:04:44 +11002065#ifdef CONFIG_PPC_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return macio_register_driver(&pmz_driver);
Finn Thainec9cbe02009-11-17 20:04:44 +11002067#else
2068 return platform_driver_probe(&pmz_driver, pmz_attach);
2069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072static void __exit exit_pmz(void)
2073{
2074 int i;
2075
Finn Thainec9cbe02009-11-17 20:04:44 +11002076#ifdef CONFIG_PPC_PMAC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /* Get rid of macio-driver (detach from macio) */
2078 macio_unregister_driver(&pmz_driver);
Finn Thainec9cbe02009-11-17 20:04:44 +11002079#else
2080 platform_driver_unregister(&pmz_driver);
2081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 for (i = 0; i < pmz_ports_count; i++) {
2084 struct uart_pmac_port *uport = &pmz_ports[i];
2085 if (uport->node != NULL) {
2086 uart_remove_one_port(&pmz_uart_reg, &uport->port);
2087 pmz_dispose_port(uport);
2088 }
2089 }
2090 /* Unregister UART driver */
2091 uart_unregister_driver(&pmz_uart_reg);
2092}
2093
2094#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
2095
Russell Kingd3587882006-03-20 20:00:09 +00002096static void pmz_console_putchar(struct uart_port *port, int ch)
2097{
2098 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
2099
2100 /* Wait for the transmit buffer to empty. */
2101 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
2102 udelay(5);
2103 write_zsdata(uap, ch);
2104}
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106/*
2107 * Print a string to the serial port trying not to disturb
2108 * any possible real use of the port...
2109 */
2110static void pmz_console_write(struct console *con, const char *s, unsigned int count)
2111{
2112 struct uart_pmac_port *uap = &pmz_ports[con->index];
2113 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 if (ZS_IS_ASLEEP(uap))
2116 return;
2117 spin_lock_irqsave(&uap->port.lock, flags);
2118
2119 /* Turn of interrupts and enable the transmitter. */
2120 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
2121 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
2122
Russell Kingd3587882006-03-20 20:00:09 +00002123 uart_console_write(&uap->port, s, count, pmz_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Restore the values in the registers. */
2126 write_zsreg(uap, R1, uap->curregs[1]);
2127 /* Don't disable the transmitter. */
2128
2129 spin_unlock_irqrestore(&uap->port.lock, flags);
2130}
2131
2132/*
2133 * Setup the serial console
2134 */
2135static int __init pmz_console_setup(struct console *co, char *options)
2136{
2137 struct uart_pmac_port *uap;
2138 struct uart_port *port;
2139 int baud = 38400;
2140 int bits = 8;
2141 int parity = 'n';
2142 int flow = 'n';
2143 unsigned long pwr_delay;
2144
2145 /*
2146 * XServe's default to 57600 bps
2147 */
Grant Likely71a157e2010-02-01 21:34:14 -07002148 if (of_machine_is_compatible("RackMac1,1")
2149 || of_machine_is_compatible("RackMac1,2")
2150 || of_machine_is_compatible("MacRISC4"))
Finn Thain1f7b5ff2009-11-04 00:40:23 +11002151 baud = 57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 /*
2154 * Check whether an invalid uart number has been specified, and
2155 * if so, search for the first available port that does have
2156 * console support.
2157 */
2158 if (co->index >= pmz_ports_count)
2159 co->index = 0;
2160 uap = &pmz_ports[co->index];
2161 if (uap->node == NULL)
2162 return -ENODEV;
2163 port = &uap->port;
2164
2165 /*
2166 * Mark port as beeing a console
2167 */
2168 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2169
2170 /*
2171 * Temporary fix for uart layer who didn't setup the spinlock yet
2172 */
2173 spin_lock_init(&port->lock);
2174
2175 /*
2176 * Enable the hardware
2177 */
2178 pwr_delay = __pmz_startup(uap);
2179 if (pwr_delay)
2180 mdelay(pwr_delay);
2181
2182 if (options)
2183 uart_parse_options(options, &baud, &parity, &bits, &flow);
2184
2185 return uart_set_options(port, co, baud, parity, bits, flow);
2186}
2187
2188static int __init pmz_console_init(void)
2189{
2190 /* Probe ports */
2191 pmz_probe();
2192
2193 /* TODO: Autoprobe console based on OF */
2194 /* pmz_console.index = i; */
2195 register_console(&pmz_console);
2196
2197 return 0;
2198
2199}
2200console_initcall(pmz_console_init);
2201#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2202
2203module_init(init_pmz);
2204module_exit(exit_pmz);