1 /* sunzilog.c: Zilog serial driver for Sparc systems.
3 * Driver for Zilog serial chips found on Sun workstations and
4 * servers. This driver could actually be made more generic.
6 * This is based on the old drivers/sbus/char/zs.c code. A lot
7 * of code has been simply moved over directly from there but
8 * much has been rewritten. Credits therefore go out to Eddie
9 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
12 * Copyright (C) 2002, 2006 David S. Miller (davem@davemloft.net)
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>
32 #include <linux/serio.h>
34 #include <linux/init.h>
39 #include <asm/of_device.h>
41 #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
45 #include <linux/serial_core.h>
50 /* On 32-bit sparcs we need to delay after register accesses
51 * to accommodate sun4 systems, but we do not need to flush writes.
52 * On 64-bit sparc we only need to flush single writes to ensure
55 #ifndef CONFIG_SPARC64
56 #define ZSDELAY() udelay(5)
57 #define ZSDELAY_LONG() udelay(20)
58 #define ZS_WSYNC(channel) do { } while (0)
61 #define ZSDELAY_LONG()
62 #define ZS_WSYNC(__channel) \
63 readb(&((__channel)->control))
66 static int num_sunzilog;
67 #define NUM_SUNZILOG num_sunzilog
68 #define NUM_CHANNELS (NUM_SUNZILOG * 2)
70 #define ZS_CLOCK 4915200 /* Zilog input clock rate. */
71 #define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
74 * We wrap our port structure around the generic uart_port.
76 struct uart_sunzilog_port {
77 struct uart_port port;
79 /* IRQ servicing chain. */
80 struct uart_sunzilog_port *next;
82 /* Current values of Zilog write registers. */
83 unsigned char curregs[NUM_ZSREGS];
86 #define SUNZILOG_FLAG_CONS_KEYB 0x00000001
87 #define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
88 #define SUNZILOG_FLAG_IS_CONS 0x00000004
89 #define SUNZILOG_FLAG_IS_KGDB 0x00000008
90 #define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
91 #define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
92 #define SUNZILOG_FLAG_REGS_HELD 0x00000040
93 #define SUNZILOG_FLAG_TX_STOPPED 0x00000080
94 #define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
98 unsigned char parity_mask;
99 unsigned char prev_status;
107 #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
108 #define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
110 #define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
111 #define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
112 #define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
113 #define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
114 #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
115 #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
116 #define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
117 #define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
118 #define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
120 /* Reading and writing Zilog8530 registers. The delays are to make this
121 * driver work on the Sun4 which needs a settling delay after each chip
122 * register access, other machines handle this in hardware via auxiliary
123 * flip-flops which implement the settle time we do in software.
125 * The port lock must be held and local IRQs must be disabled
126 * when {read,write}_zsreg is invoked.
128 static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
131 unsigned char retval;
133 writeb(reg, &channel->control);
135 retval = readb(&channel->control);
141 static void write_zsreg(struct zilog_channel __iomem *channel,
142 unsigned char reg, unsigned char value)
144 writeb(reg, &channel->control);
146 writeb(value, &channel->control);
150 static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
154 for (i = 0; i < 32; i++) {
155 unsigned char regval;
157 regval = readb(&channel->control);
159 if (regval & Rx_CH_AV)
162 regval = read_zsreg(channel, R1);
163 readb(&channel->data);
166 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
167 writeb(ERR_RES, &channel->control);
174 /* This function must only be called when the TX is not busy. The UART
175 * port lock must be held and local interrupts disabled.
177 static void __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
181 /* Let pending transmits finish. */
182 for (i = 0; i < 1000; i++) {
183 unsigned char stat = read_zsreg(channel, R1);
189 writeb(ERR_RES, &channel->control);
193 sunzilog_clear_fifo(channel);
195 /* Disable all interrupts. */
196 write_zsreg(channel, R1,
197 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
199 /* Set parity, sync config, stop bits, and clock divisor. */
200 write_zsreg(channel, R4, regs[R4]);
202 /* Set misc. TX/RX control bits. */
203 write_zsreg(channel, R10, regs[R10]);
205 /* Set TX/RX controls sans the enable bits. */
206 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
207 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
209 /* Synchronous mode config. */
210 write_zsreg(channel, R6, regs[R6]);
211 write_zsreg(channel, R7, regs[R7]);
213 /* Don't mess with the interrupt vector (R2, unused by us) and
214 * master interrupt control (R9). We make sure this is setup
215 * properly at probe time then never touch it again.
218 /* Disable baud generator. */
219 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
221 /* Clock mode control. */
222 write_zsreg(channel, R11, regs[R11]);
224 /* Lower and upper byte of baud rate generator divisor. */
225 write_zsreg(channel, R12, regs[R12]);
226 write_zsreg(channel, R13, regs[R13]);
228 /* Now rewrite R14, with BRENAB (if set). */
229 write_zsreg(channel, R14, regs[R14]);
231 /* External status interrupt control. */
232 write_zsreg(channel, R15, regs[R15]);
234 /* Reset external status interrupts. */
235 write_zsreg(channel, R0, RES_EXT_INT);
236 write_zsreg(channel, R0, RES_EXT_INT);
238 /* Rewrite R3/R5, this time without enables masked. */
239 write_zsreg(channel, R3, regs[R3]);
240 write_zsreg(channel, R5, regs[R5]);
242 /* Rewrite R1, this time without IRQ enabled masked. */
243 write_zsreg(channel, R1, regs[R1]);
246 /* Reprogram the Zilog channel HW registers with the copies found in the
247 * software state struct. If the transmitter is busy, we defer this update
248 * until the next TX complete interrupt. Else, we do it right now.
250 * The UART port lock must be held and local interrupts disabled.
252 static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
253 struct zilog_channel __iomem *channel)
255 if (!ZS_REGS_HELD(up)) {
256 if (ZS_TX_ACTIVE(up)) {
257 up->flags |= SUNZILOG_FLAG_REGS_HELD;
259 __load_zsregs(channel, up->curregs);
264 static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
266 unsigned int cur_cflag = up->cflag;
270 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
272 brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
273 up->curregs[R12] = (brg & 0xff);
274 up->curregs[R13] = (brg >> 8) & 0xff;
275 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
278 static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
279 unsigned char ch, int is_break)
281 if (ZS_IS_KEYB(up)) {
282 /* Stop-A is handled by drivers/char/keyboard.c now. */
285 serio_interrupt(&up->serio, ch, 0);
287 } else if (ZS_IS_MOUSE(up)) {
288 int ret = suncore_mouse_baud_detection(ch, is_break);
292 sunzilog_change_mouse_baud(up);
300 serio_interrupt(&up->serio, ch, 0);
307 static struct tty_struct *
308 sunzilog_receive_chars(struct uart_sunzilog_port *up,
309 struct zilog_channel __iomem *channel)
311 struct tty_struct *tty;
312 unsigned char ch, r1, flag;
315 if (up->port.info != NULL && /* Unopened serial console */
316 up->port.info->tty != NULL) /* Keyboard || mouse */
317 tty = up->port.info->tty;
321 r1 = read_zsreg(channel, R1);
322 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
323 writeb(ERR_RES, &channel->control);
328 ch = readb(&channel->control);
331 /* This funny hack depends upon BRK_ABRT not interfering
332 * with the other bits we care about in R1.
337 if (!(ch & Rx_CH_AV))
340 ch = readb(&channel->data);
343 ch &= up->parity_mask;
345 if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
346 sunzilog_kbdms_receive_chars(up, ch, 0);
351 uart_handle_sysrq_char(&up->port, ch);
355 /* A real serial line, record the character and status. */
357 up->port.icount.rx++;
358 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
360 r1 &= ~(PAR_ERR | CRC_ERR);
361 up->port.icount.brk++;
362 if (uart_handle_break(&up->port))
365 else if (r1 & PAR_ERR)
366 up->port.icount.parity++;
367 else if (r1 & CRC_ERR)
368 up->port.icount.frame++;
370 up->port.icount.overrun++;
371 r1 &= up->port.read_status_mask;
374 else if (r1 & PAR_ERR)
376 else if (r1 & CRC_ERR)
379 if (uart_handle_sysrq_char(&up->port, ch))
382 if (up->port.ignore_status_mask == 0xff ||
383 (r1 & up->port.ignore_status_mask) == 0) {
384 tty_insert_flip_char(tty, ch, flag);
387 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
393 static void sunzilog_status_handle(struct uart_sunzilog_port *up,
394 struct zilog_channel __iomem *channel)
396 unsigned char status;
398 status = readb(&channel->control);
401 writeb(RES_EXT_INT, &channel->control);
405 if (status & BRK_ABRT) {
407 sunzilog_kbdms_receive_chars(up, 0, 1);
408 if (ZS_IS_CONS(up)) {
409 /* Wait for BREAK to deassert to avoid potentially
410 * confusing the PROM.
413 status = readb(&channel->control);
415 if (!(status & BRK_ABRT))
423 if (ZS_WANTS_MODEM_STATUS(up)) {
425 up->port.icount.dsr++;
427 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
428 * But it does not tell us which bit has changed, we have to keep
429 * track of this ourselves.
431 if ((status ^ up->prev_status) ^ DCD)
432 uart_handle_dcd_change(&up->port,
434 if ((status ^ up->prev_status) ^ CTS)
435 uart_handle_cts_change(&up->port,
438 wake_up_interruptible(&up->port.info->delta_msr_wait);
441 up->prev_status = status;
444 static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
445 struct zilog_channel __iomem *channel)
447 struct circ_buf *xmit;
449 if (ZS_IS_CONS(up)) {
450 unsigned char status = readb(&channel->control);
453 /* TX still busy? Just wait for the next TX done interrupt.
455 * It can occur because of how we do serial console writes. It would
456 * be nice to transmit console writes just like we normally would for
457 * a TTY line. (ie. buffered and TX interrupt driven). That is not
458 * easy because console writes cannot sleep. One solution might be
459 * to poll on enough port->xmit space becomming free. -DaveM
461 if (!(status & Tx_BUF_EMP))
465 up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
467 if (ZS_REGS_HELD(up)) {
468 __load_zsregs(channel, up->curregs);
469 up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
472 if (ZS_TX_STOPPED(up)) {
473 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
477 if (up->port.x_char) {
478 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
479 writeb(up->port.x_char, &channel->data);
483 up->port.icount.tx++;
488 if (up->port.info == NULL)
490 xmit = &up->port.info->xmit;
491 if (uart_circ_empty(xmit))
494 if (uart_tx_stopped(&up->port))
497 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
498 writeb(xmit->buf[xmit->tail], &channel->data);
502 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
503 up->port.icount.tx++;
505 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
506 uart_write_wakeup(&up->port);
511 writeb(RES_Tx_P, &channel->control);
516 static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
518 struct uart_sunzilog_port *up = dev_id;
521 struct zilog_channel __iomem *channel
522 = ZILOG_CHANNEL_FROM_PORT(&up->port);
523 struct tty_struct *tty;
526 spin_lock(&up->port.lock);
527 r3 = read_zsreg(channel, R3);
531 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
532 writeb(RES_H_IUS, &channel->control);
537 tty = sunzilog_receive_chars(up, channel);
539 sunzilog_status_handle(up, channel);
541 sunzilog_transmit_chars(up, channel);
543 spin_unlock(&up->port.lock);
546 tty_flip_buffer_push(tty);
550 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
552 spin_lock(&up->port.lock);
554 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
555 writeb(RES_H_IUS, &channel->control);
560 tty = sunzilog_receive_chars(up, channel);
562 sunzilog_status_handle(up, channel);
564 sunzilog_transmit_chars(up, channel);
566 spin_unlock(&up->port.lock);
569 tty_flip_buffer_push(tty);
577 /* A convenient way to quickly get R0 status. The caller must _not_ hold the
578 * port lock, it is acquired here.
580 static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
582 struct zilog_channel __iomem *channel;
583 unsigned char status;
585 channel = ZILOG_CHANNEL_FROM_PORT(port);
586 status = readb(&channel->control);
592 /* The port lock is not held. */
593 static unsigned int sunzilog_tx_empty(struct uart_port *port)
596 unsigned char status;
599 spin_lock_irqsave(&port->lock, flags);
601 status = sunzilog_read_channel_status(port);
603 spin_unlock_irqrestore(&port->lock, flags);
605 if (status & Tx_BUF_EMP)
613 /* The port lock is held and interrupts are disabled. */
614 static unsigned int sunzilog_get_mctrl(struct uart_port *port)
616 unsigned char status;
619 status = sunzilog_read_channel_status(port);
632 /* The port lock is held and interrupts are disabled. */
633 static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
635 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
636 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
637 unsigned char set_bits, clear_bits;
639 set_bits = clear_bits = 0;
641 if (mctrl & TIOCM_RTS)
645 if (mctrl & TIOCM_DTR)
650 /* NOTE: Not subject to 'transmitter active' rule. */
651 up->curregs[R5] |= set_bits;
652 up->curregs[R5] &= ~clear_bits;
653 write_zsreg(channel, R5, up->curregs[R5]);
656 /* The port lock is held and interrupts are disabled. */
657 static void sunzilog_stop_tx(struct uart_port *port)
659 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
661 up->flags |= SUNZILOG_FLAG_TX_STOPPED;
664 /* The port lock is held and interrupts are disabled. */
665 static void sunzilog_start_tx(struct uart_port *port)
667 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
668 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
669 unsigned char status;
671 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
672 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
674 status = readb(&channel->control);
677 /* TX busy? Just wait for the TX done interrupt. */
678 if (!(status & Tx_BUF_EMP))
681 /* Send the first character to jump-start the TX done
682 * IRQ sending engine.
685 writeb(port->x_char, &channel->data);
692 struct circ_buf *xmit = &port->info->xmit;
694 writeb(xmit->buf[xmit->tail], &channel->data);
698 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
701 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
702 uart_write_wakeup(&up->port);
706 /* The port lock is held. */
707 static void sunzilog_stop_rx(struct uart_port *port)
709 struct uart_sunzilog_port *up = UART_ZILOG(port);
710 struct zilog_channel __iomem *channel;
715 channel = ZILOG_CHANNEL_FROM_PORT(port);
717 /* Disable all RX interrupts. */
718 up->curregs[R1] &= ~RxINT_MASK;
719 sunzilog_maybe_update_regs(up, channel);
722 /* The port lock is held. */
723 static void sunzilog_enable_ms(struct uart_port *port)
725 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
726 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
727 unsigned char new_reg;
729 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
730 if (new_reg != up->curregs[R15]) {
731 up->curregs[R15] = new_reg;
733 /* NOTE: Not subject to 'transmitter active' rule. */
734 write_zsreg(channel, R15, up->curregs[R15]);
738 /* The port lock is not held. */
739 static void sunzilog_break_ctl(struct uart_port *port, int break_state)
741 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
742 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
743 unsigned char set_bits, clear_bits, new_reg;
746 set_bits = clear_bits = 0;
751 clear_bits |= SND_BRK;
753 spin_lock_irqsave(&port->lock, flags);
755 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
756 if (new_reg != up->curregs[R5]) {
757 up->curregs[R5] = new_reg;
759 /* NOTE: Not subject to 'transmitter active' rule. */
760 write_zsreg(channel, R5, up->curregs[R5]);
763 spin_unlock_irqrestore(&port->lock, flags);
766 static void __sunzilog_startup(struct uart_sunzilog_port *up)
768 struct zilog_channel __iomem *channel;
770 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
771 up->prev_status = readb(&channel->control);
773 /* Enable receiver and transmitter. */
774 up->curregs[R3] |= RxENAB;
775 up->curregs[R5] |= TxENAB;
777 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
778 sunzilog_maybe_update_regs(up, channel);
781 static int sunzilog_startup(struct uart_port *port)
783 struct uart_sunzilog_port *up = UART_ZILOG(port);
789 spin_lock_irqsave(&port->lock, flags);
790 __sunzilog_startup(up);
791 spin_unlock_irqrestore(&port->lock, flags);
796 * The test for ZS_IS_CONS is explained by the following e-mail:
798 * From: Russell King <rmk@arm.linux.org.uk>
799 * Date: Sun, 8 Dec 2002 10:18:38 +0000
801 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
802 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
803 * > and I noticed that something is not right with reference
804 * > counting in this case. It seems that when the console
805 * > is open by kernel initially, this is not accounted
806 * > as an open, and uart_startup is not called.
808 * That is correct. We are unable to call uart_startup when the serial
809 * console is initialised because it may need to allocate memory (as
810 * request_irq does) and the memory allocators may not have been
813 * 1. initialise the port into a state where it can send characters in the
814 * console write method.
816 * 2. don't do the actual hardware shutdown in your shutdown() method (but
817 * do the normal software shutdown - ie, free irqs etc)
820 static void sunzilog_shutdown(struct uart_port *port)
822 struct uart_sunzilog_port *up = UART_ZILOG(port);
823 struct zilog_channel __iomem *channel;
829 spin_lock_irqsave(&port->lock, flags);
831 channel = ZILOG_CHANNEL_FROM_PORT(port);
833 /* Disable receiver and transmitter. */
834 up->curregs[R3] &= ~RxENAB;
835 up->curregs[R5] &= ~TxENAB;
837 /* Disable all interrupts and BRK assertion. */
838 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
839 up->curregs[R5] &= ~SND_BRK;
840 sunzilog_maybe_update_regs(up, channel);
842 spin_unlock_irqrestore(&port->lock, flags);
845 /* Shared by TTY driver and serial console setup. The port lock is held
846 * and local interrupts are disabled.
849 sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
850 unsigned int iflag, int brg)
853 up->curregs[R10] = NRZ;
854 up->curregs[R11] = TCBR | RCBR;
856 /* Program BAUD and clock source. */
857 up->curregs[R4] &= ~XCLK_MASK;
858 up->curregs[R4] |= X16CLK;
859 up->curregs[R12] = brg & 0xff;
860 up->curregs[R13] = (brg >> 8) & 0xff;
861 up->curregs[R14] = BRSRC | BRENAB;
863 /* Character size, stop bits, and parity. */
864 up->curregs[3] &= ~RxN_MASK;
865 up->curregs[5] &= ~TxN_MASK;
866 switch (cflag & CSIZE) {
868 up->curregs[3] |= Rx5;
869 up->curregs[5] |= Tx5;
870 up->parity_mask = 0x1f;
873 up->curregs[3] |= Rx6;
874 up->curregs[5] |= Tx6;
875 up->parity_mask = 0x3f;
878 up->curregs[3] |= Rx7;
879 up->curregs[5] |= Tx7;
880 up->parity_mask = 0x7f;
884 up->curregs[3] |= Rx8;
885 up->curregs[5] |= Tx8;
886 up->parity_mask = 0xff;
889 up->curregs[4] &= ~0x0c;
891 up->curregs[4] |= SB2;
893 up->curregs[4] |= SB1;
895 up->curregs[4] |= PAR_ENAB;
897 up->curregs[4] &= ~PAR_ENAB;
898 if (!(cflag & PARODD))
899 up->curregs[4] |= PAR_EVEN;
901 up->curregs[4] &= ~PAR_EVEN;
903 up->port.read_status_mask = Rx_OVR;
905 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
906 if (iflag & (BRKINT | PARMRK))
907 up->port.read_status_mask |= BRK_ABRT;
909 up->port.ignore_status_mask = 0;
911 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
912 if (iflag & IGNBRK) {
913 up->port.ignore_status_mask |= BRK_ABRT;
915 up->port.ignore_status_mask |= Rx_OVR;
918 if ((cflag & CREAD) == 0)
919 up->port.ignore_status_mask = 0xff;
922 /* The port lock is not held. */
924 sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
925 struct ktermios *old)
927 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
931 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
933 spin_lock_irqsave(&up->port.lock, flags);
935 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
937 sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
939 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
940 up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
942 up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
944 up->cflag = termios->c_cflag;
946 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
948 uart_update_timeout(port, termios->c_cflag, baud);
950 spin_unlock_irqrestore(&up->port.lock, flags);
953 static const char *sunzilog_type(struct uart_port *port)
958 /* We do not request/release mappings of the registers here, this
959 * happens at early serial probe time.
961 static void sunzilog_release_port(struct uart_port *port)
965 static int sunzilog_request_port(struct uart_port *port)
970 /* These do not need to do anything interesting either. */
971 static void sunzilog_config_port(struct uart_port *port, int flags)
975 /* We do not support letting the user mess with the divisor, IRQ, etc. */
976 static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
981 static struct uart_ops sunzilog_pops = {
982 .tx_empty = sunzilog_tx_empty,
983 .set_mctrl = sunzilog_set_mctrl,
984 .get_mctrl = sunzilog_get_mctrl,
985 .stop_tx = sunzilog_stop_tx,
986 .start_tx = sunzilog_start_tx,
987 .stop_rx = sunzilog_stop_rx,
988 .enable_ms = sunzilog_enable_ms,
989 .break_ctl = sunzilog_break_ctl,
990 .startup = sunzilog_startup,
991 .shutdown = sunzilog_shutdown,
992 .set_termios = sunzilog_set_termios,
993 .type = sunzilog_type,
994 .release_port = sunzilog_release_port,
995 .request_port = sunzilog_request_port,
996 .config_port = sunzilog_config_port,
997 .verify_port = sunzilog_verify_port,
1000 static struct uart_sunzilog_port *sunzilog_port_table;
1001 static struct zilog_layout __iomem **sunzilog_chip_regs;
1003 static struct uart_sunzilog_port *sunzilog_irq_chain;
1005 static struct uart_driver sunzilog_reg = {
1006 .owner = THIS_MODULE,
1007 .driver_name = "ttyS",
1012 static int __init sunzilog_alloc_tables(void)
1014 struct uart_sunzilog_port *up;
1018 size = NUM_CHANNELS * sizeof(struct uart_sunzilog_port);
1019 sunzilog_port_table = kzalloc(size, GFP_KERNEL);
1020 if (!sunzilog_port_table)
1023 for (i = 0; i < NUM_CHANNELS; i++) {
1024 up = &sunzilog_port_table[i];
1026 spin_lock_init(&up->port.lock);
1029 sunzilog_irq_chain = up;
1031 if (i < NUM_CHANNELS - 1)
1037 size = NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *);
1038 sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
1039 if (!sunzilog_chip_regs) {
1040 kfree(sunzilog_port_table);
1041 sunzilog_irq_chain = NULL;
1048 static void sunzilog_free_tables(void)
1050 kfree(sunzilog_port_table);
1051 sunzilog_irq_chain = NULL;
1052 kfree(sunzilog_chip_regs);
1055 #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
1057 static void sunzilog_putchar(struct uart_port *port, int ch)
1059 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
1060 int loops = ZS_PUT_CHAR_MAX_DELAY;
1062 /* This is a timed polling loop so do not switch the explicit
1063 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1066 unsigned char val = readb(&channel->control);
1067 if (val & Tx_BUF_EMP) {
1074 writeb(ch, &channel->data);
1081 static DEFINE_SPINLOCK(sunzilog_serio_lock);
1083 static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1085 struct uart_sunzilog_port *up = serio->port_data;
1086 unsigned long flags;
1088 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1090 sunzilog_putchar(&up->port, ch);
1092 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1097 static int sunzilog_serio_open(struct serio *serio)
1099 struct uart_sunzilog_port *up = serio->port_data;
1100 unsigned long flags;
1103 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1104 if (!up->serio_open) {
1109 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1114 static void sunzilog_serio_close(struct serio *serio)
1116 struct uart_sunzilog_port *up = serio->port_data;
1117 unsigned long flags;
1119 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1121 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1124 #endif /* CONFIG_SERIO */
1126 #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1128 sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1130 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1131 unsigned long flags;
1133 spin_lock_irqsave(&up->port.lock, flags);
1134 uart_console_write(&up->port, s, count, sunzilog_putchar);
1136 spin_unlock_irqrestore(&up->port.lock, flags);
1139 static int __init sunzilog_console_setup(struct console *con, char *options)
1141 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1142 unsigned long flags;
1145 if (up->port.type != PORT_SUNZILOG)
1148 printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
1149 (sunzilog_reg.minor - 64) + con->index, con->index);
1151 /* Get firmware console settings. */
1152 sunserial_console_termios(con);
1154 /* Firmware console speed is limited to 150-->38400 baud so
1155 * this hackish cflag thing is OK.
1157 switch (con->cflag & CBAUD) {
1158 case B150: baud = 150; break;
1159 case B300: baud = 300; break;
1160 case B600: baud = 600; break;
1161 case B1200: baud = 1200; break;
1162 case B2400: baud = 2400; break;
1163 case B4800: baud = 4800; break;
1164 default: case B9600: baud = 9600; break;
1165 case B19200: baud = 19200; break;
1166 case B38400: baud = 38400; break;
1169 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1171 spin_lock_irqsave(&up->port.lock, flags);
1173 up->curregs[R15] = BRKIE;
1174 sunzilog_convert_to_zs(up, con->cflag, 0, brg);
1176 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1177 __sunzilog_startup(up);
1179 spin_unlock_irqrestore(&up->port.lock, flags);
1184 static struct console sunzilog_console_ops = {
1186 .write = sunzilog_console_write,
1187 .device = uart_console_device,
1188 .setup = sunzilog_console_setup,
1189 .flags = CON_PRINTBUFFER,
1191 .data = &sunzilog_reg,
1194 static inline struct console *SUNZILOG_CONSOLE(void)
1198 if (con_is_present())
1201 for (i = 0; i < NUM_CHANNELS; i++) {
1202 int this_minor = sunzilog_reg.minor + i;
1204 if ((this_minor - 64) == (serial_console - 1))
1207 if (i == NUM_CHANNELS)
1210 sunzilog_console_ops.index = i;
1211 sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
1213 return &sunzilog_console_ops;
1217 #define SUNZILOG_CONSOLE() (NULL)
1220 static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
1224 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1225 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1228 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1232 up->curregs[R15] = BRKIE;
1233 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1234 sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1235 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1236 __sunzilog_startup(up);
1240 static void __init sunzilog_register_serio(struct uart_sunzilog_port *up)
1242 struct serio *serio = &up->serio;
1244 serio->port_data = up;
1246 serio->id.type = SERIO_RS232;
1247 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1248 serio->id.proto = SERIO_SUNKBD;
1249 strlcpy(serio->name, "zskbd", sizeof(serio->name));
1251 serio->id.proto = SERIO_SUN;
1252 serio->id.extra = 1;
1253 strlcpy(serio->name, "zsms", sizeof(serio->name));
1255 strlcpy(serio->phys,
1256 ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
1257 "zs/serio0" : "zs/serio1"),
1258 sizeof(serio->phys));
1260 serio->write = sunzilog_serio_write;
1261 serio->open = sunzilog_serio_open;
1262 serio->close = sunzilog_serio_close;
1263 serio->dev.parent = up->port.dev;
1265 serio_register_port(serio);
1269 static void __devinit sunzilog_init_hw(struct uart_sunzilog_port *up)
1271 struct zilog_channel __iomem *channel;
1272 unsigned long flags;
1275 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1277 spin_lock_irqsave(&up->port.lock, flags);
1278 if (ZS_IS_CHANNEL_A(up)) {
1279 write_zsreg(channel, R9, FHWRES);
1281 (void) read_zsreg(channel, R0);
1284 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1285 SUNZILOG_FLAG_CONS_MOUSE)) {
1286 sunzilog_init_kbdms(up, up->port.line);
1287 up->curregs[R9] |= (NV | MIE);
1288 write_zsreg(channel, R9, up->curregs[R9]);
1290 /* Normal serial TTY. */
1291 up->parity_mask = 0xff;
1292 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1293 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1294 up->curregs[R3] = RxENAB | Rx8;
1295 up->curregs[R5] = TxENAB | Tx8;
1296 up->curregs[R9] = NV | MIE;
1297 up->curregs[R10] = NRZ;
1298 up->curregs[R11] = TCBR | RCBR;
1300 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1301 up->curregs[R12] = (brg & 0xff);
1302 up->curregs[R13] = (brg >> 8) & 0xff;
1303 up->curregs[R14] = BRSRC | BRENAB;
1304 __load_zsregs(channel, up->curregs);
1305 write_zsreg(channel, R9, up->curregs[R9]);
1308 spin_unlock_irqrestore(&up->port.lock, flags);
1311 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1312 SUNZILOG_FLAG_CONS_MOUSE))
1313 sunzilog_register_serio(up);
1317 static int zilog_irq = -1;
1319 static int __devinit zs_probe(struct of_device *op, const struct of_device_id *match)
1322 struct uart_sunzilog_port *up;
1323 struct zilog_layout __iomem *rp;
1328 if (of_find_property(op->node, "keyboard", NULL))
1331 sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
1332 sizeof(struct zilog_layout),
1334 if (!sunzilog_chip_regs[inst])
1337 rp = sunzilog_chip_regs[inst];
1339 if (zilog_irq == -1)
1340 zilog_irq = op->irqs[0];
1342 up = &sunzilog_port_table[inst * 2];
1345 up[0].port.mapbase = op->resource[0].start + 0x00;
1346 up[0].port.membase = (void __iomem *) &rp->channelA;
1347 up[0].port.iotype = UPIO_MEM;
1348 up[0].port.irq = op->irqs[0];
1349 up[0].port.uartclk = ZS_CLOCK;
1350 up[0].port.fifosize = 1;
1351 up[0].port.ops = &sunzilog_pops;
1352 up[0].port.type = PORT_SUNZILOG;
1353 up[0].port.flags = 0;
1354 up[0].port.line = (inst * 2) + 0;
1355 up[0].port.dev = &op->dev;
1356 up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1358 up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
1359 sunzilog_init_hw(&up[0]);
1362 up[1].port.mapbase = op->resource[0].start + 0x04;
1363 up[1].port.membase = (void __iomem *) &rp->channelB;
1364 up[1].port.iotype = UPIO_MEM;
1365 up[1].port.irq = op->irqs[0];
1366 up[1].port.uartclk = ZS_CLOCK;
1367 up[1].port.fifosize = 1;
1368 up[1].port.ops = &sunzilog_pops;
1369 up[1].port.type = PORT_SUNZILOG;
1370 up[1].port.flags = 0;
1371 up[1].port.line = (inst * 2) + 1;
1372 up[1].port.dev = &op->dev;
1375 up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
1376 sunzilog_init_hw(&up[1]);
1378 if (!keyboard_mouse) {
1379 err = uart_add_one_port(&sunzilog_reg, &up[0].port);
1381 of_iounmap(&op->resource[0],
1382 rp, sizeof(struct zilog_layout));
1385 err = uart_add_one_port(&sunzilog_reg, &up[1].port);
1387 uart_remove_one_port(&sunzilog_reg, &up[0].port);
1388 of_iounmap(&op->resource[0],
1389 rp, sizeof(struct zilog_layout));
1393 printk(KERN_INFO "%s: Keyboard at MMIO %lx (irq = %d) "
1395 op->dev.bus_id, up[0].port.mapbase, op->irqs[0]);
1396 printk(KERN_INFO "%s: Mouse at MMIO %lx (irq = %d) "
1398 op->dev.bus_id, up[1].port.mapbase, op->irqs[0]);
1401 dev_set_drvdata(&op->dev, &up[0]);
1408 static void __devexit zs_remove_one(struct uart_sunzilog_port *up)
1410 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
1412 serio_unregister_port(&up->serio);
1415 uart_remove_one_port(&sunzilog_reg, &up->port);
1418 static int __devexit zs_remove(struct of_device *op)
1420 struct uart_sunzilog_port *up = dev_get_drvdata(&op->dev);
1421 struct zilog_layout __iomem *regs;
1423 zs_remove_one(&up[0]);
1424 zs_remove_one(&up[1]);
1426 regs = sunzilog_chip_regs[up[0].port.line / 2];
1427 of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
1429 dev_set_drvdata(&op->dev, NULL);
1434 static struct of_device_id zs_match[] = {
1440 MODULE_DEVICE_TABLE(of, zs_match);
1442 static struct of_platform_driver zs_driver = {
1444 .match_table = zs_match,
1446 .remove = __devexit_p(zs_remove),
1449 static int __init sunzilog_init(void)
1451 struct device_node *dp;
1452 int err, uart_count;
1457 for_each_node_by_name(dp, "zs") {
1459 if (of_find_property(dp, "keyboard", NULL))
1467 err = sunzilog_alloc_tables();
1471 uart_count = (NUM_SUNZILOG * 2) - (2 * num_keybms);
1473 sunzilog_reg.nr = uart_count;
1474 sunzilog_reg.minor = sunserial_current_minor;
1475 err = uart_register_driver(&sunzilog_reg);
1477 goto out_free_tables;
1479 sunzilog_reg.tty_driver->name_base = sunzilog_reg.minor - 64;
1480 sunzilog_reg.cons = SUNZILOG_CONSOLE();
1482 sunserial_current_minor += uart_count;
1485 err = of_register_driver(&zs_driver, &of_bus_type);
1487 goto out_unregister_uart;
1489 if (zilog_irq != -1) {
1490 err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
1491 "zs", sunzilog_irq_chain);
1493 goto out_unregister_driver;
1499 out_unregister_driver:
1500 of_unregister_driver(&zs_driver);
1502 out_unregister_uart:
1504 uart_unregister_driver(&sunzilog_reg);
1505 sunzilog_reg.cons = NULL;
1509 sunzilog_free_tables();
1513 static void __exit sunzilog_exit(void)
1515 of_unregister_driver(&zs_driver);
1517 if (zilog_irq != -1) {
1518 free_irq(zilog_irq, sunzilog_irq_chain);
1523 uart_unregister_driver(&sunzilog_reg);
1524 sunzilog_free_tables();
1528 module_init(sunzilog_init);
1529 module_exit(sunzilog_exit);
1531 MODULE_AUTHOR("David S. Miller");
1532 MODULE_DESCRIPTION("Sun Zilog serial port driver");
1533 MODULE_VERSION("2.0");
1534 MODULE_LICENSE("GPL");