Blackfin arch: fix bug: kernel prints out error message twice
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / ip22zilog.c
blobf3257f708ef91161bb10469ac97c09f998eadf3d
1 /*
2 * Driver for Zilog serial chips found on SGI workstations and
3 * servers. This driver could actually be made more generic.
5 * This is based on the drivers/serial/sunzilog.c code as of 2.6.0-test7 and the
6 * old drivers/sgi/char/sgiserial.c code which itself is based of the original
7 * drivers/sbus/char/zs.c code. A lot of code has been simply moved over
8 * directly from there but much has been rewritten. Credits therefore go out
9 * to David S. Miller, Eddie C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell
10 * for their work there.
12 * Copyright (C) 2002 Ralf Baechle (ralf@linux-mips.org)
13 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/delay.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/major.h>
22 #include <linux/string.h>
23 #include <linux/ptrace.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/circ_buf.h>
27 #include <linux/serial.h>
28 #include <linux/sysrq.h>
29 #include <linux/console.h>
30 #include <linux/spinlock.h>
31 #include <linux/init.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/sgialib.h>
36 #include <asm/sgi/ioc.h>
37 #include <asm/sgi/hpc3.h>
38 #include <asm/sgi/ip22.h>
40 #if defined(CONFIG_SERIAL_IP22_ZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
41 #define SUPPORT_SYSRQ
42 #endif
44 #include <linux/serial_core.h>
46 #include "ip22zilog.h"
48 void ip22_do_break(void);
51 * On IP22 we need to delay after register accesses but we do not need to
52 * flush writes.
54 #define ZSDELAY() udelay(5)
55 #define ZSDELAY_LONG() udelay(20)
56 #define ZS_WSYNC(channel) do { } while (0)
58 #define NUM_IP22ZILOG 1
59 #define NUM_CHANNELS (NUM_IP22ZILOG * 2)
61 #define ZS_CLOCK 3672000 /* Zilog input clock rate. */
62 #define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
65 * We wrap our port structure around the generic uart_port.
67 struct uart_ip22zilog_port {
68 struct uart_port port;
70 /* IRQ servicing chain. */
71 struct uart_ip22zilog_port *next;
73 /* Current values of Zilog write registers. */
74 unsigned char curregs[NUM_ZSREGS];
76 unsigned int flags;
77 #define IP22ZILOG_FLAG_IS_CONS 0x00000004
78 #define IP22ZILOG_FLAG_IS_KGDB 0x00000008
79 #define IP22ZILOG_FLAG_MODEM_STATUS 0x00000010
80 #define IP22ZILOG_FLAG_IS_CHANNEL_A 0x00000020
81 #define IP22ZILOG_FLAG_REGS_HELD 0x00000040
82 #define IP22ZILOG_FLAG_TX_STOPPED 0x00000080
83 #define IP22ZILOG_FLAG_TX_ACTIVE 0x00000100
85 unsigned int cflag;
87 /* L1-A keyboard break state. */
88 int kbd_id;
89 int l1_down;
91 unsigned char parity_mask;
92 unsigned char prev_status;
95 #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel *)((PORT)->membase))
96 #define UART_ZILOG(PORT) ((struct uart_ip22zilog_port *)(PORT))
97 #define IP22ZILOG_GET_CURR_REG(PORT, REGNUM) \
98 (UART_ZILOG(PORT)->curregs[REGNUM])
99 #define IP22ZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL) \
100 ((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
101 #define ZS_IS_CONS(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CONS)
102 #define ZS_IS_KGDB(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_KGDB)
103 #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & IP22ZILOG_FLAG_MODEM_STATUS)
104 #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CHANNEL_A)
105 #define ZS_REGS_HELD(UP) ((UP)->flags & IP22ZILOG_FLAG_REGS_HELD)
106 #define ZS_TX_STOPPED(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_STOPPED)
107 #define ZS_TX_ACTIVE(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_ACTIVE)
109 /* Reading and writing Zilog8530 registers. The delays are to make this
110 * driver work on the IP22 which needs a settling delay after each chip
111 * register access, other machines handle this in hardware via auxiliary
112 * flip-flops which implement the settle time we do in software.
114 * The port lock must be held and local IRQs must be disabled
115 * when {read,write}_zsreg is invoked.
117 static unsigned char read_zsreg(struct zilog_channel *channel,
118 unsigned char reg)
120 unsigned char retval;
122 writeb(reg, &channel->control);
123 ZSDELAY();
124 retval = readb(&channel->control);
125 ZSDELAY();
127 return retval;
130 static void write_zsreg(struct zilog_channel *channel,
131 unsigned char reg, unsigned char value)
133 writeb(reg, &channel->control);
134 ZSDELAY();
135 writeb(value, &channel->control);
136 ZSDELAY();
139 static void ip22zilog_clear_fifo(struct zilog_channel *channel)
141 int i;
143 for (i = 0; i < 32; i++) {
144 unsigned char regval;
146 regval = readb(&channel->control);
147 ZSDELAY();
148 if (regval & Rx_CH_AV)
149 break;
151 regval = read_zsreg(channel, R1);
152 readb(&channel->data);
153 ZSDELAY();
155 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
156 writeb(ERR_RES, &channel->control);
157 ZSDELAY();
158 ZS_WSYNC(channel);
163 /* This function must only be called when the TX is not busy. The UART
164 * port lock must be held and local interrupts disabled.
166 static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
168 int i;
170 /* Let pending transmits finish. */
171 for (i = 0; i < 1000; i++) {
172 unsigned char stat = read_zsreg(channel, R1);
173 if (stat & ALL_SNT)
174 break;
175 udelay(100);
178 writeb(ERR_RES, &channel->control);
179 ZSDELAY();
180 ZS_WSYNC(channel);
182 ip22zilog_clear_fifo(channel);
184 /* Disable all interrupts. */
185 write_zsreg(channel, R1,
186 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
188 /* Set parity, sync config, stop bits, and clock divisor. */
189 write_zsreg(channel, R4, regs[R4]);
191 /* Set misc. TX/RX control bits. */
192 write_zsreg(channel, R10, regs[R10]);
194 /* Set TX/RX controls sans the enable bits. */
195 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
196 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
198 /* Synchronous mode config. */
199 write_zsreg(channel, R6, regs[R6]);
200 write_zsreg(channel, R7, regs[R7]);
202 /* Don't mess with the interrupt vector (R2, unused by us) and
203 * master interrupt control (R9). We make sure this is setup
204 * properly at probe time then never touch it again.
207 /* Disable baud generator. */
208 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
210 /* Clock mode control. */
211 write_zsreg(channel, R11, regs[R11]);
213 /* Lower and upper byte of baud rate generator divisor. */
214 write_zsreg(channel, R12, regs[R12]);
215 write_zsreg(channel, R13, regs[R13]);
217 /* Now rewrite R14, with BRENAB (if set). */
218 write_zsreg(channel, R14, regs[R14]);
220 /* External status interrupt control. */
221 write_zsreg(channel, R15, regs[R15]);
223 /* Reset external status interrupts. */
224 write_zsreg(channel, R0, RES_EXT_INT);
225 write_zsreg(channel, R0, RES_EXT_INT);
227 /* Rewrite R3/R5, this time without enables masked. */
228 write_zsreg(channel, R3, regs[R3]);
229 write_zsreg(channel, R5, regs[R5]);
231 /* Rewrite R1, this time without IRQ enabled masked. */
232 write_zsreg(channel, R1, regs[R1]);
235 /* Reprogram the Zilog channel HW registers with the copies found in the
236 * software state struct. If the transmitter is busy, we defer this update
237 * until the next TX complete interrupt. Else, we do it right now.
239 * The UART port lock must be held and local interrupts disabled.
241 static void ip22zilog_maybe_update_regs(struct uart_ip22zilog_port *up,
242 struct zilog_channel *channel)
244 if (!ZS_REGS_HELD(up)) {
245 if (ZS_TX_ACTIVE(up)) {
246 up->flags |= IP22ZILOG_FLAG_REGS_HELD;
247 } else {
248 __load_zsregs(channel, up->curregs);
253 static void ip22zilog_receive_chars(struct uart_ip22zilog_port *up,
254 struct zilog_channel *channel)
256 struct tty_struct *tty = up->port.info->tty; /* XXX info==NULL? */
258 while (1) {
259 unsigned char ch, r1, flag;
261 r1 = read_zsreg(channel, R1);
262 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
263 writeb(ERR_RES, &channel->control);
264 ZSDELAY();
265 ZS_WSYNC(channel);
268 ch = readb(&channel->control);
269 ZSDELAY();
271 /* This funny hack depends upon BRK_ABRT not interfering
272 * with the other bits we care about in R1.
274 if (ch & BRK_ABRT)
275 r1 |= BRK_ABRT;
277 ch = readb(&channel->data);
278 ZSDELAY();
280 ch &= up->parity_mask;
282 if (ZS_IS_CONS(up) && (r1 & BRK_ABRT)) {
283 /* Wait for BREAK to deassert to avoid potentially
284 * confusing the PROM.
286 while (1) {
287 ch = readb(&channel->control);
288 ZSDELAY();
289 if (!(ch & BRK_ABRT))
290 break;
292 ip22_do_break();
293 return;
296 /* A real serial line, record the character and status. */
297 flag = TTY_NORMAL;
298 up->port.icount.rx++;
299 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
300 if (r1 & BRK_ABRT) {
301 r1 &= ~(PAR_ERR | CRC_ERR);
302 up->port.icount.brk++;
303 if (uart_handle_break(&up->port))
304 goto next_char;
306 else if (r1 & PAR_ERR)
307 up->port.icount.parity++;
308 else if (r1 & CRC_ERR)
309 up->port.icount.frame++;
310 if (r1 & Rx_OVR)
311 up->port.icount.overrun++;
312 r1 &= up->port.read_status_mask;
313 if (r1 & BRK_ABRT)
314 flag = TTY_BREAK;
315 else if (r1 & PAR_ERR)
316 flag = TTY_PARITY;
317 else if (r1 & CRC_ERR)
318 flag = TTY_FRAME;
320 if (uart_handle_sysrq_char(&up->port, ch))
321 goto next_char;
323 if (up->port.ignore_status_mask == 0xff ||
324 (r1 & up->port.ignore_status_mask) == 0)
325 tty_insert_flip_char(tty, ch, flag);
327 if (r1 & Rx_OVR)
328 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
329 next_char:
330 ch = readb(&channel->control);
331 ZSDELAY();
332 if (!(ch & Rx_CH_AV))
333 break;
336 tty_flip_buffer_push(tty);
339 static void ip22zilog_status_handle(struct uart_ip22zilog_port *up,
340 struct zilog_channel *channel)
342 unsigned char status;
344 status = readb(&channel->control);
345 ZSDELAY();
347 writeb(RES_EXT_INT, &channel->control);
348 ZSDELAY();
349 ZS_WSYNC(channel);
351 if (ZS_WANTS_MODEM_STATUS(up)) {
352 if (status & SYNC)
353 up->port.icount.dsr++;
355 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
356 * But it does not tell us which bit has changed, we have to keep
357 * track of this ourselves.
359 if ((status & DCD) ^ up->prev_status)
360 uart_handle_dcd_change(&up->port,
361 (status & DCD));
362 if ((status & CTS) ^ up->prev_status)
363 uart_handle_cts_change(&up->port,
364 (status & CTS));
366 wake_up_interruptible(&up->port.info->delta_msr_wait);
369 up->prev_status = status;
372 static void ip22zilog_transmit_chars(struct uart_ip22zilog_port *up,
373 struct zilog_channel *channel)
375 struct circ_buf *xmit;
377 if (ZS_IS_CONS(up)) {
378 unsigned char status = readb(&channel->control);
379 ZSDELAY();
381 /* TX still busy? Just wait for the next TX done interrupt.
383 * It can occur because of how we do serial console writes. It would
384 * be nice to transmit console writes just like we normally would for
385 * a TTY line. (ie. buffered and TX interrupt driven). That is not
386 * easy because console writes cannot sleep. One solution might be
387 * to poll on enough port->xmit space becomming free. -DaveM
389 if (!(status & Tx_BUF_EMP))
390 return;
393 up->flags &= ~IP22ZILOG_FLAG_TX_ACTIVE;
395 if (ZS_REGS_HELD(up)) {
396 __load_zsregs(channel, up->curregs);
397 up->flags &= ~IP22ZILOG_FLAG_REGS_HELD;
400 if (ZS_TX_STOPPED(up)) {
401 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
402 goto ack_tx_int;
405 if (up->port.x_char) {
406 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
407 writeb(up->port.x_char, &channel->data);
408 ZSDELAY();
409 ZS_WSYNC(channel);
411 up->port.icount.tx++;
412 up->port.x_char = 0;
413 return;
416 if (up->port.info == NULL)
417 goto ack_tx_int;
418 xmit = &up->port.info->xmit;
419 if (uart_circ_empty(xmit))
420 goto ack_tx_int;
421 if (uart_tx_stopped(&up->port))
422 goto ack_tx_int;
424 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
425 writeb(xmit->buf[xmit->tail], &channel->data);
426 ZSDELAY();
427 ZS_WSYNC(channel);
429 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
430 up->port.icount.tx++;
432 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
433 uart_write_wakeup(&up->port);
435 return;
437 ack_tx_int:
438 writeb(RES_Tx_P, &channel->control);
439 ZSDELAY();
440 ZS_WSYNC(channel);
443 static irqreturn_t ip22zilog_interrupt(int irq, void *dev_id)
445 struct uart_ip22zilog_port *up = dev_id;
447 while (up) {
448 struct zilog_channel *channel
449 = ZILOG_CHANNEL_FROM_PORT(&up->port);
450 unsigned char r3;
452 spin_lock(&up->port.lock);
453 r3 = read_zsreg(channel, R3);
455 /* Channel A */
456 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
457 writeb(RES_H_IUS, &channel->control);
458 ZSDELAY();
459 ZS_WSYNC(channel);
461 if (r3 & CHARxIP)
462 ip22zilog_receive_chars(up, channel);
463 if (r3 & CHAEXT)
464 ip22zilog_status_handle(up, channel);
465 if (r3 & CHATxIP)
466 ip22zilog_transmit_chars(up, channel);
468 spin_unlock(&up->port.lock);
470 /* Channel B */
471 up = up->next;
472 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
474 spin_lock(&up->port.lock);
475 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
476 writeb(RES_H_IUS, &channel->control);
477 ZSDELAY();
478 ZS_WSYNC(channel);
480 if (r3 & CHBRxIP)
481 ip22zilog_receive_chars(up, channel);
482 if (r3 & CHBEXT)
483 ip22zilog_status_handle(up, channel);
484 if (r3 & CHBTxIP)
485 ip22zilog_transmit_chars(up, channel);
487 spin_unlock(&up->port.lock);
489 up = up->next;
492 return IRQ_HANDLED;
495 /* A convenient way to quickly get R0 status. The caller must _not_ hold the
496 * port lock, it is acquired here.
498 static __inline__ unsigned char ip22zilog_read_channel_status(struct uart_port *port)
500 struct zilog_channel *channel;
501 unsigned char status;
503 channel = ZILOG_CHANNEL_FROM_PORT(port);
504 status = readb(&channel->control);
505 ZSDELAY();
507 return status;
510 /* The port lock is not held. */
511 static unsigned int ip22zilog_tx_empty(struct uart_port *port)
513 unsigned long flags;
514 unsigned char status;
515 unsigned int ret;
517 spin_lock_irqsave(&port->lock, flags);
519 status = ip22zilog_read_channel_status(port);
521 spin_unlock_irqrestore(&port->lock, flags);
523 if (status & Tx_BUF_EMP)
524 ret = TIOCSER_TEMT;
525 else
526 ret = 0;
528 return ret;
531 /* The port lock is held and interrupts are disabled. */
532 static unsigned int ip22zilog_get_mctrl(struct uart_port *port)
534 unsigned char status;
535 unsigned int ret;
537 status = ip22zilog_read_channel_status(port);
539 ret = 0;
540 if (status & DCD)
541 ret |= TIOCM_CAR;
542 if (status & SYNC)
543 ret |= TIOCM_DSR;
544 if (status & CTS)
545 ret |= TIOCM_CTS;
547 return ret;
550 /* The port lock is held and interrupts are disabled. */
551 static void ip22zilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
553 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
554 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
555 unsigned char set_bits, clear_bits;
557 set_bits = clear_bits = 0;
559 if (mctrl & TIOCM_RTS)
560 set_bits |= RTS;
561 else
562 clear_bits |= RTS;
563 if (mctrl & TIOCM_DTR)
564 set_bits |= DTR;
565 else
566 clear_bits |= DTR;
568 /* NOTE: Not subject to 'transmitter active' rule. */
569 up->curregs[R5] |= set_bits;
570 up->curregs[R5] &= ~clear_bits;
571 write_zsreg(channel, R5, up->curregs[R5]);
574 /* The port lock is held and interrupts are disabled. */
575 static void ip22zilog_stop_tx(struct uart_port *port)
577 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
579 up->flags |= IP22ZILOG_FLAG_TX_STOPPED;
582 /* The port lock is held and interrupts are disabled. */
583 static void ip22zilog_start_tx(struct uart_port *port)
585 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
586 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
587 unsigned char status;
589 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
590 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
592 status = readb(&channel->control);
593 ZSDELAY();
595 /* TX busy? Just wait for the TX done interrupt. */
596 if (!(status & Tx_BUF_EMP))
597 return;
599 /* Send the first character to jump-start the TX done
600 * IRQ sending engine.
602 if (port->x_char) {
603 writeb(port->x_char, &channel->data);
604 ZSDELAY();
605 ZS_WSYNC(channel);
607 port->icount.tx++;
608 port->x_char = 0;
609 } else {
610 struct circ_buf *xmit = &port->info->xmit;
612 writeb(xmit->buf[xmit->tail], &channel->data);
613 ZSDELAY();
614 ZS_WSYNC(channel);
616 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
617 port->icount.tx++;
619 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
620 uart_write_wakeup(&up->port);
624 /* The port lock is held and interrupts are disabled. */
625 static void ip22zilog_stop_rx(struct uart_port *port)
627 struct uart_ip22zilog_port *up = UART_ZILOG(port);
628 struct zilog_channel *channel;
630 if (ZS_IS_CONS(up))
631 return;
633 channel = ZILOG_CHANNEL_FROM_PORT(port);
635 /* Disable all RX interrupts. */
636 up->curregs[R1] &= ~RxINT_MASK;
637 ip22zilog_maybe_update_regs(up, channel);
640 /* The port lock is held. */
641 static void ip22zilog_enable_ms(struct uart_port *port)
643 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
644 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
645 unsigned char new_reg;
647 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
648 if (new_reg != up->curregs[R15]) {
649 up->curregs[R15] = new_reg;
651 /* NOTE: Not subject to 'transmitter active' rule. */
652 write_zsreg(channel, R15, up->curregs[R15]);
656 /* The port lock is not held. */
657 static void ip22zilog_break_ctl(struct uart_port *port, int break_state)
659 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
660 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
661 unsigned char set_bits, clear_bits, new_reg;
662 unsigned long flags;
664 set_bits = clear_bits = 0;
666 if (break_state)
667 set_bits |= SND_BRK;
668 else
669 clear_bits |= SND_BRK;
671 spin_lock_irqsave(&port->lock, flags);
673 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
674 if (new_reg != up->curregs[R5]) {
675 up->curregs[R5] = new_reg;
677 /* NOTE: Not subject to 'transmitter active' rule. */
678 write_zsreg(channel, R5, up->curregs[R5]);
681 spin_unlock_irqrestore(&port->lock, flags);
684 static void __ip22zilog_startup(struct uart_ip22zilog_port *up)
686 struct zilog_channel *channel;
688 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
689 up->prev_status = readb(&channel->control);
691 /* Enable receiver and transmitter. */
692 up->curregs[R3] |= RxENAB;
693 up->curregs[R5] |= TxENAB;
695 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
696 ip22zilog_maybe_update_regs(up, channel);
699 static int ip22zilog_startup(struct uart_port *port)
701 struct uart_ip22zilog_port *up = UART_ZILOG(port);
702 unsigned long flags;
704 if (ZS_IS_CONS(up))
705 return 0;
707 spin_lock_irqsave(&port->lock, flags);
708 __ip22zilog_startup(up);
709 spin_unlock_irqrestore(&port->lock, flags);
710 return 0;
714 * The test for ZS_IS_CONS is explained by the following e-mail:
715 *****
716 * From: Russell King <rmk@arm.linux.org.uk>
717 * Date: Sun, 8 Dec 2002 10:18:38 +0000
719 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
720 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
721 * > and I noticed that something is not right with reference
722 * > counting in this case. It seems that when the console
723 * > is open by kernel initially, this is not accounted
724 * > as an open, and uart_startup is not called.
726 * That is correct. We are unable to call uart_startup when the serial
727 * console is initialised because it may need to allocate memory (as
728 * request_irq does) and the memory allocators may not have been
729 * initialised.
731 * 1. initialise the port into a state where it can send characters in the
732 * console write method.
734 * 2. don't do the actual hardware shutdown in your shutdown() method (but
735 * do the normal software shutdown - ie, free irqs etc)
736 *****
738 static void ip22zilog_shutdown(struct uart_port *port)
740 struct uart_ip22zilog_port *up = UART_ZILOG(port);
741 struct zilog_channel *channel;
742 unsigned long flags;
744 if (ZS_IS_CONS(up))
745 return;
747 spin_lock_irqsave(&port->lock, flags);
749 channel = ZILOG_CHANNEL_FROM_PORT(port);
751 /* Disable receiver and transmitter. */
752 up->curregs[R3] &= ~RxENAB;
753 up->curregs[R5] &= ~TxENAB;
755 /* Disable all interrupts and BRK assertion. */
756 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
757 up->curregs[R5] &= ~SND_BRK;
758 ip22zilog_maybe_update_regs(up, channel);
760 spin_unlock_irqrestore(&port->lock, flags);
763 /* Shared by TTY driver and serial console setup. The port lock is held
764 * and local interrupts are disabled.
766 static void
767 ip22zilog_convert_to_zs(struct uart_ip22zilog_port *up, unsigned int cflag,
768 unsigned int iflag, int brg)
771 up->curregs[R10] = NRZ;
772 up->curregs[R11] = TCBR | RCBR;
774 /* Program BAUD and clock source. */
775 up->curregs[R4] &= ~XCLK_MASK;
776 up->curregs[R4] |= X16CLK;
777 up->curregs[R12] = brg & 0xff;
778 up->curregs[R13] = (brg >> 8) & 0xff;
779 up->curregs[R14] = BRENAB;
781 /* Character size, stop bits, and parity. */
782 up->curregs[3] &= ~RxN_MASK;
783 up->curregs[5] &= ~TxN_MASK;
784 switch (cflag & CSIZE) {
785 case CS5:
786 up->curregs[3] |= Rx5;
787 up->curregs[5] |= Tx5;
788 up->parity_mask = 0x1f;
789 break;
790 case CS6:
791 up->curregs[3] |= Rx6;
792 up->curregs[5] |= Tx6;
793 up->parity_mask = 0x3f;
794 break;
795 case CS7:
796 up->curregs[3] |= Rx7;
797 up->curregs[5] |= Tx7;
798 up->parity_mask = 0x7f;
799 break;
800 case CS8:
801 default:
802 up->curregs[3] |= Rx8;
803 up->curregs[5] |= Tx8;
804 up->parity_mask = 0xff;
805 break;
807 up->curregs[4] &= ~0x0c;
808 if (cflag & CSTOPB)
809 up->curregs[4] |= SB2;
810 else
811 up->curregs[4] |= SB1;
812 if (cflag & PARENB)
813 up->curregs[4] |= PAR_ENAB;
814 else
815 up->curregs[4] &= ~PAR_ENAB;
816 if (!(cflag & PARODD))
817 up->curregs[4] |= PAR_EVEN;
818 else
819 up->curregs[4] &= ~PAR_EVEN;
821 up->port.read_status_mask = Rx_OVR;
822 if (iflag & INPCK)
823 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
824 if (iflag & (BRKINT | PARMRK))
825 up->port.read_status_mask |= BRK_ABRT;
827 up->port.ignore_status_mask = 0;
828 if (iflag & IGNPAR)
829 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
830 if (iflag & IGNBRK) {
831 up->port.ignore_status_mask |= BRK_ABRT;
832 if (iflag & IGNPAR)
833 up->port.ignore_status_mask |= Rx_OVR;
836 if ((cflag & CREAD) == 0)
837 up->port.ignore_status_mask = 0xff;
840 /* The port lock is not held. */
841 static void
842 ip22zilog_set_termios(struct uart_port *port, struct ktermios *termios,
843 struct ktermios *old)
845 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
846 unsigned long flags;
847 int baud, brg;
849 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
851 spin_lock_irqsave(&up->port.lock, flags);
853 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
855 ip22zilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
857 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
858 up->flags |= IP22ZILOG_FLAG_MODEM_STATUS;
859 else
860 up->flags &= ~IP22ZILOG_FLAG_MODEM_STATUS;
862 up->cflag = termios->c_cflag;
864 ip22zilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
865 uart_update_timeout(port, termios->c_cflag, baud);
867 spin_unlock_irqrestore(&up->port.lock, flags);
870 static const char *ip22zilog_type(struct uart_port *port)
872 return "IP22-Zilog";
875 /* We do not request/release mappings of the registers here, this
876 * happens at early serial probe time.
878 static void ip22zilog_release_port(struct uart_port *port)
882 static int ip22zilog_request_port(struct uart_port *port)
884 return 0;
887 /* These do not need to do anything interesting either. */
888 static void ip22zilog_config_port(struct uart_port *port, int flags)
892 /* We do not support letting the user mess with the divisor, IRQ, etc. */
893 static int ip22zilog_verify_port(struct uart_port *port, struct serial_struct *ser)
895 return -EINVAL;
898 static struct uart_ops ip22zilog_pops = {
899 .tx_empty = ip22zilog_tx_empty,
900 .set_mctrl = ip22zilog_set_mctrl,
901 .get_mctrl = ip22zilog_get_mctrl,
902 .stop_tx = ip22zilog_stop_tx,
903 .start_tx = ip22zilog_start_tx,
904 .stop_rx = ip22zilog_stop_rx,
905 .enable_ms = ip22zilog_enable_ms,
906 .break_ctl = ip22zilog_break_ctl,
907 .startup = ip22zilog_startup,
908 .shutdown = ip22zilog_shutdown,
909 .set_termios = ip22zilog_set_termios,
910 .type = ip22zilog_type,
911 .release_port = ip22zilog_release_port,
912 .request_port = ip22zilog_request_port,
913 .config_port = ip22zilog_config_port,
914 .verify_port = ip22zilog_verify_port,
917 static struct uart_ip22zilog_port *ip22zilog_port_table;
918 static struct zilog_layout **ip22zilog_chip_regs;
920 static struct uart_ip22zilog_port *ip22zilog_irq_chain;
921 static int zilog_irq = -1;
923 static void * __init alloc_one_table(unsigned long size)
925 return kzalloc(size, GFP_KERNEL);
928 static void __init ip22zilog_alloc_tables(void)
930 ip22zilog_port_table = (struct uart_ip22zilog_port *)
931 alloc_one_table(NUM_CHANNELS * sizeof(struct uart_ip22zilog_port));
932 ip22zilog_chip_regs = (struct zilog_layout **)
933 alloc_one_table(NUM_IP22ZILOG * sizeof(struct zilog_layout *));
935 if (ip22zilog_port_table == NULL || ip22zilog_chip_regs == NULL) {
936 panic("IP22-Zilog: Cannot allocate IP22-Zilog tables.");
940 /* Get the address of the registers for IP22-Zilog instance CHIP. */
941 static struct zilog_layout * __init get_zs(int chip)
943 unsigned long base;
945 if (chip < 0 || chip >= NUM_IP22ZILOG) {
946 panic("IP22-Zilog: Illegal chip number %d in get_zs.", chip);
949 /* Not probe-able, hard code it. */
950 base = (unsigned long) &sgioc->uart;
952 zilog_irq = SGI_SERIAL_IRQ;
953 request_mem_region(base, 8, "IP22-Zilog");
955 return (struct zilog_layout *) base;
958 #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
960 #ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
961 static void ip22zilog_put_char(struct uart_port *port, int ch)
963 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
964 int loops = ZS_PUT_CHAR_MAX_DELAY;
966 /* This is a timed polling loop so do not switch the explicit
967 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
969 do {
970 unsigned char val = readb(&channel->control);
971 if (val & Tx_BUF_EMP) {
972 ZSDELAY();
973 break;
975 udelay(5);
976 } while (--loops);
978 writeb(ch, &channel->data);
979 ZSDELAY();
980 ZS_WSYNC(channel);
983 static void
984 ip22zilog_console_write(struct console *con, const char *s, unsigned int count)
986 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
987 unsigned long flags;
989 spin_lock_irqsave(&up->port.lock, flags);
990 uart_console_write(&up->port, s, count, ip22zilog_put_char);
991 udelay(2);
992 spin_unlock_irqrestore(&up->port.lock, flags);
995 void
996 ip22serial_console_termios(struct console *con, char *options)
998 int baud = 9600, bits = 8, cflag;
999 int parity = 'n';
1000 int flow = 'n';
1002 if (options)
1003 uart_parse_options(options, &baud, &parity, &bits, &flow);
1005 cflag = CREAD | HUPCL | CLOCAL;
1007 switch (baud) {
1008 case 150: cflag |= B150; break;
1009 case 300: cflag |= B300; break;
1010 case 600: cflag |= B600; break;
1011 case 1200: cflag |= B1200; break;
1012 case 2400: cflag |= B2400; break;
1013 case 4800: cflag |= B4800; break;
1014 case 9600: cflag |= B9600; break;
1015 case 19200: cflag |= B19200; break;
1016 case 38400: cflag |= B38400; break;
1017 default: baud = 9600; cflag |= B9600; break;
1020 con->cflag = cflag | CS8; /* 8N1 */
1022 uart_update_timeout(&ip22zilog_port_table[con->index].port, cflag, baud);
1025 static int __init ip22zilog_console_setup(struct console *con, char *options)
1027 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1028 unsigned long flags;
1029 int baud, brg;
1031 printk("Console: ttyS%d (IP22-Zilog)\n", con->index);
1033 /* Get firmware console settings. */
1034 ip22serial_console_termios(con, options);
1036 /* Firmware console speed is limited to 150-->38400 baud so
1037 * this hackish cflag thing is OK.
1039 switch (con->cflag & CBAUD) {
1040 case B150: baud = 150; break;
1041 case B300: baud = 300; break;
1042 case B600: baud = 600; break;
1043 case B1200: baud = 1200; break;
1044 case B2400: baud = 2400; break;
1045 case B4800: baud = 4800; break;
1046 default: case B9600: baud = 9600; break;
1047 case B19200: baud = 19200; break;
1048 case B38400: baud = 38400; break;
1051 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1053 spin_lock_irqsave(&up->port.lock, flags);
1055 up->curregs[R15] = BRKIE;
1056 ip22zilog_convert_to_zs(up, con->cflag, 0, brg);
1058 __ip22zilog_startup(up);
1060 spin_unlock_irqrestore(&up->port.lock, flags);
1062 return 0;
1065 static struct uart_driver ip22zilog_reg;
1067 static struct console ip22zilog_console = {
1068 .name = "ttyS",
1069 .write = ip22zilog_console_write,
1070 .device = uart_console_device,
1071 .setup = ip22zilog_console_setup,
1072 .flags = CON_PRINTBUFFER,
1073 .index = -1,
1074 .data = &ip22zilog_reg,
1076 #endif /* CONFIG_SERIAL_IP22_ZILOG_CONSOLE */
1078 static struct uart_driver ip22zilog_reg = {
1079 .owner = THIS_MODULE,
1080 .driver_name = "serial",
1081 .dev_name = "ttyS",
1082 .major = TTY_MAJOR,
1083 .minor = 64,
1084 .nr = NUM_CHANNELS,
1085 #ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
1086 .cons = &ip22zilog_console,
1087 #endif
1090 static void __init ip22zilog_prepare(void)
1092 struct uart_ip22zilog_port *up;
1093 struct zilog_layout *rp;
1094 int channel, chip;
1097 * Temporary fix.
1099 for (channel = 0; channel < NUM_CHANNELS; channel++)
1100 spin_lock_init(&ip22zilog_port_table[channel].port.lock);
1102 ip22zilog_irq_chain = &ip22zilog_port_table[NUM_CHANNELS - 1];
1103 up = &ip22zilog_port_table[0];
1104 for (channel = NUM_CHANNELS - 1 ; channel > 0; channel--)
1105 up[channel].next = &up[channel - 1];
1106 up[channel].next = NULL;
1108 for (chip = 0; chip < NUM_IP22ZILOG; chip++) {
1109 if (!ip22zilog_chip_regs[chip]) {
1110 ip22zilog_chip_regs[chip] = rp = get_zs(chip);
1112 up[(chip * 2) + 0].port.membase = (char *) &rp->channelB;
1113 up[(chip * 2) + 1].port.membase = (char *) &rp->channelA;
1115 /* In theory mapbase is the physical address ... */
1116 up[(chip * 2) + 0].port.mapbase =
1117 (unsigned long) ioremap((unsigned long) &rp->channelB, 8);
1118 up[(chip * 2) + 1].port.mapbase =
1119 (unsigned long) ioremap((unsigned long) &rp->channelA, 8);
1122 /* Channel A */
1123 up[(chip * 2) + 0].port.iotype = UPIO_MEM;
1124 up[(chip * 2) + 0].port.irq = zilog_irq;
1125 up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1126 up[(chip * 2) + 0].port.fifosize = 1;
1127 up[(chip * 2) + 0].port.ops = &ip22zilog_pops;
1128 up[(chip * 2) + 0].port.type = PORT_IP22ZILOG;
1129 up[(chip * 2) + 0].port.flags = 0;
1130 up[(chip * 2) + 0].port.line = (chip * 2) + 0;
1131 up[(chip * 2) + 0].flags = 0;
1133 /* Channel B */
1134 up[(chip * 2) + 1].port.iotype = UPIO_MEM;
1135 up[(chip * 2) + 1].port.irq = zilog_irq;
1136 up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1137 up[(chip * 2) + 1].port.fifosize = 1;
1138 up[(chip * 2) + 1].port.ops = &ip22zilog_pops;
1139 up[(chip * 2) + 1].port.type = PORT_IP22ZILOG;
1140 up[(chip * 2) + 1].port.line = (chip * 2) + 1;
1141 up[(chip * 2) + 1].flags |= IP22ZILOG_FLAG_IS_CHANNEL_A;
1145 static void __init ip22zilog_init_hw(void)
1147 int i;
1149 for (i = 0; i < NUM_CHANNELS; i++) {
1150 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1151 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1152 unsigned long flags;
1153 int baud, brg;
1155 spin_lock_irqsave(&up->port.lock, flags);
1157 if (ZS_IS_CHANNEL_A(up)) {
1158 write_zsreg(channel, R9, FHWRES);
1159 ZSDELAY_LONG();
1160 (void) read_zsreg(channel, R0);
1163 /* Normal serial TTY. */
1164 up->parity_mask = 0xff;
1165 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1166 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1167 up->curregs[R3] = RxENAB | Rx8;
1168 up->curregs[R5] = TxENAB | Tx8;
1169 up->curregs[R9] = NV | MIE;
1170 up->curregs[R10] = NRZ;
1171 up->curregs[R11] = TCBR | RCBR;
1172 baud = 9600;
1173 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1174 up->curregs[R12] = (brg & 0xff);
1175 up->curregs[R13] = (brg >> 8) & 0xff;
1176 up->curregs[R14] = BRENAB;
1177 __load_zsregs(channel, up->curregs);
1178 /* set master interrupt enable */
1179 write_zsreg(channel, R9, up->curregs[R9]);
1181 spin_unlock_irqrestore(&up->port.lock, flags);
1185 static int __init ip22zilog_ports_init(void)
1187 int ret;
1189 printk(KERN_INFO "Serial: IP22 Zilog driver (%d chips).\n", NUM_IP22ZILOG);
1191 ip22zilog_prepare();
1193 if (request_irq(zilog_irq, ip22zilog_interrupt, 0,
1194 "IP22-Zilog", ip22zilog_irq_chain)) {
1195 panic("IP22-Zilog: Unable to register zs interrupt handler.\n");
1198 ip22zilog_init_hw();
1200 ret = uart_register_driver(&ip22zilog_reg);
1201 if (ret == 0) {
1202 int i;
1204 for (i = 0; i < NUM_CHANNELS; i++) {
1205 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1207 uart_add_one_port(&ip22zilog_reg, &up->port);
1211 return ret;
1214 static int __init ip22zilog_init(void)
1216 /* IP22 Zilog setup is hard coded, no probing to do. */
1217 ip22zilog_alloc_tables();
1218 ip22zilog_ports_init();
1220 return 0;
1223 static void __exit ip22zilog_exit(void)
1225 int i;
1226 struct uart_ip22zilog_port *up;
1228 for (i = 0; i < NUM_CHANNELS; i++) {
1229 up = &ip22zilog_port_table[i];
1231 uart_remove_one_port(&ip22zilog_reg, &up->port);
1234 /* Free IO mem */
1235 up = &ip22zilog_port_table[0];
1236 for (i = 0; i < NUM_IP22ZILOG; i++) {
1237 if (up[(i * 2) + 0].port.mapbase) {
1238 iounmap((void*)up[(i * 2) + 0].port.mapbase);
1239 up[(i * 2) + 0].port.mapbase = 0;
1241 if (up[(i * 2) + 1].port.mapbase) {
1242 iounmap((void*)up[(i * 2) + 1].port.mapbase);
1243 up[(i * 2) + 1].port.mapbase = 0;
1247 uart_unregister_driver(&ip22zilog_reg);
1250 module_init(ip22zilog_init);
1251 module_exit(ip22zilog_exit);
1253 /* David wrote it but I'm to blame for the bugs ... */
1254 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1255 MODULE_DESCRIPTION("SGI Zilog serial port driver");
1256 MODULE_LICENSE("GPL");