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, 2007 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
95 #define SUNZILOG_FLAG_ESCC 0x00000200
96 #define SUNZILOG_FLAG_ISR_HANDLER 0x00000400
100 unsigned char parity_mask;
101 unsigned char prev_status;
109 #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
110 #define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
112 #define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
113 #define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
114 #define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
115 #define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
116 #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
117 #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
118 #define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
119 #define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
120 #define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
122 /* Reading and writing Zilog8530 registers. The delays are to make this
123 * driver work on the Sun4 which needs a settling delay after each chip
124 * register access, other machines handle this in hardware via auxiliary
125 * flip-flops which implement the settle time we do in software.
127 * The port lock must be held and local IRQs must be disabled
128 * when {read,write}_zsreg is invoked.
130 static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
133 unsigned char retval;
135 writeb(reg, &channel->control);
137 retval = readb(&channel->control);
143 static void write_zsreg(struct zilog_channel __iomem *channel,
144 unsigned char reg, unsigned char value)
146 writeb(reg, &channel->control);
148 writeb(value, &channel->control);
152 static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
156 for (i = 0; i < 32; i++) {
157 unsigned char regval;
159 regval = readb(&channel->control);
161 if (regval & Rx_CH_AV)
164 regval = read_zsreg(channel, R1);
165 readb(&channel->data);
168 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
169 writeb(ERR_RES, &channel->control);
176 /* This function must only be called when the TX is not busy. The UART
177 * port lock must be held and local interrupts disabled.
179 static int __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
185 /* Let pending transmits finish. */
186 for (i = 0; i < 1000; i++) {
187 unsigned char stat = read_zsreg(channel, R1);
193 writeb(ERR_RES, &channel->control);
197 sunzilog_clear_fifo(channel);
199 /* Disable all interrupts. */
200 write_zsreg(channel, R1,
201 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
203 /* Set parity, sync config, stop bits, and clock divisor. */
204 write_zsreg(channel, R4, regs[R4]);
206 /* Set misc. TX/RX control bits. */
207 write_zsreg(channel, R10, regs[R10]);
209 /* Set TX/RX controls sans the enable bits. */
210 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
211 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
213 /* Synchronous mode config. */
214 write_zsreg(channel, R6, regs[R6]);
215 write_zsreg(channel, R7, regs[R7]);
217 /* Don't mess with the interrupt vector (R2, unused by us) and
218 * master interrupt control (R9). We make sure this is setup
219 * properly at probe time then never touch it again.
222 /* Disable baud generator. */
223 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
225 /* Clock mode control. */
226 write_zsreg(channel, R11, regs[R11]);
228 /* Lower and upper byte of baud rate generator divisor. */
229 write_zsreg(channel, R12, regs[R12]);
230 write_zsreg(channel, R13, regs[R13]);
232 /* Now rewrite R14, with BRENAB (if set). */
233 write_zsreg(channel, R14, regs[R14]);
235 /* External status interrupt control. */
236 write_zsreg(channel, R15, (regs[R15] | WR7pEN) & ~FIFOEN);
238 /* ESCC Extension Register */
239 r15 = read_zsreg(channel, R15);
241 write_zsreg(channel, R7, regs[R7p]);
243 /* External status interrupt and FIFO control. */
244 write_zsreg(channel, R15, regs[R15] & ~WR7pEN);
247 /* Clear FIFO bit case it is an issue */
248 regs[R15] &= ~FIFOEN;
252 /* Reset external status interrupts. */
253 write_zsreg(channel, R0, RES_EXT_INT); /* First Latch */
254 write_zsreg(channel, R0, RES_EXT_INT); /* Second Latch */
256 /* Rewrite R3/R5, this time without enables masked. */
257 write_zsreg(channel, R3, regs[R3]);
258 write_zsreg(channel, R5, regs[R5]);
260 /* Rewrite R1, this time without IRQ enabled masked. */
261 write_zsreg(channel, R1, regs[R1]);
266 /* Reprogram the Zilog channel HW registers with the copies found in the
267 * software state struct. If the transmitter is busy, we defer this update
268 * until the next TX complete interrupt. Else, we do it right now.
270 * The UART port lock must be held and local interrupts disabled.
272 static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
273 struct zilog_channel __iomem *channel)
275 if (!ZS_REGS_HELD(up)) {
276 if (ZS_TX_ACTIVE(up)) {
277 up->flags |= SUNZILOG_FLAG_REGS_HELD;
279 __load_zsregs(channel, up->curregs);
284 static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
286 unsigned int cur_cflag = up->cflag;
290 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
292 brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
293 up->curregs[R12] = (brg & 0xff);
294 up->curregs[R13] = (brg >> 8) & 0xff;
295 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
298 static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
299 unsigned char ch, int is_break)
301 if (ZS_IS_KEYB(up)) {
302 /* Stop-A is handled by drivers/char/keyboard.c now. */
305 serio_interrupt(&up->serio, ch, 0);
307 } else if (ZS_IS_MOUSE(up)) {
308 int ret = suncore_mouse_baud_detection(ch, is_break);
312 sunzilog_change_mouse_baud(up);
320 serio_interrupt(&up->serio, ch, 0);
327 static struct tty_struct *
328 sunzilog_receive_chars(struct uart_sunzilog_port *up,
329 struct zilog_channel __iomem *channel)
331 struct tty_struct *tty;
332 unsigned char ch, r1, flag;
335 if (up->port.info != NULL && /* Unopened serial console */
336 up->port.info->tty != NULL) /* Keyboard || mouse */
337 tty = up->port.info->tty;
341 r1 = read_zsreg(channel, R1);
342 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
343 writeb(ERR_RES, &channel->control);
348 ch = readb(&channel->control);
351 /* This funny hack depends upon BRK_ABRT not interfering
352 * with the other bits we care about in R1.
357 if (!(ch & Rx_CH_AV))
360 ch = readb(&channel->data);
363 ch &= up->parity_mask;
365 if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
366 sunzilog_kbdms_receive_chars(up, ch, 0);
371 uart_handle_sysrq_char(&up->port, ch);
375 /* A real serial line, record the character and status. */
377 up->port.icount.rx++;
378 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
380 r1 &= ~(PAR_ERR | CRC_ERR);
381 up->port.icount.brk++;
382 if (uart_handle_break(&up->port))
385 else if (r1 & PAR_ERR)
386 up->port.icount.parity++;
387 else if (r1 & CRC_ERR)
388 up->port.icount.frame++;
390 up->port.icount.overrun++;
391 r1 &= up->port.read_status_mask;
394 else if (r1 & PAR_ERR)
396 else if (r1 & CRC_ERR)
399 if (uart_handle_sysrq_char(&up->port, ch))
402 if (up->port.ignore_status_mask == 0xff ||
403 (r1 & up->port.ignore_status_mask) == 0) {
404 tty_insert_flip_char(tty, ch, flag);
407 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
413 static void sunzilog_status_handle(struct uart_sunzilog_port *up,
414 struct zilog_channel __iomem *channel)
416 unsigned char status;
418 status = readb(&channel->control);
421 writeb(RES_EXT_INT, &channel->control);
425 if (status & BRK_ABRT) {
427 sunzilog_kbdms_receive_chars(up, 0, 1);
428 if (ZS_IS_CONS(up)) {
429 /* Wait for BREAK to deassert to avoid potentially
430 * confusing the PROM.
433 status = readb(&channel->control);
435 if (!(status & BRK_ABRT))
443 if (ZS_WANTS_MODEM_STATUS(up)) {
445 up->port.icount.dsr++;
447 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
448 * But it does not tell us which bit has changed, we have to keep
449 * track of this ourselves.
451 if ((status ^ up->prev_status) ^ DCD)
452 uart_handle_dcd_change(&up->port,
454 if ((status ^ up->prev_status) ^ CTS)
455 uart_handle_cts_change(&up->port,
458 wake_up_interruptible(&up->port.info->delta_msr_wait);
461 up->prev_status = status;
464 static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
465 struct zilog_channel __iomem *channel)
467 struct circ_buf *xmit;
469 if (ZS_IS_CONS(up)) {
470 unsigned char status = readb(&channel->control);
473 /* TX still busy? Just wait for the next TX done interrupt.
475 * It can occur because of how we do serial console writes. It would
476 * be nice to transmit console writes just like we normally would for
477 * a TTY line. (ie. buffered and TX interrupt driven). That is not
478 * easy because console writes cannot sleep. One solution might be
479 * to poll on enough port->xmit space becomming free. -DaveM
481 if (!(status & Tx_BUF_EMP))
485 up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
487 if (ZS_REGS_HELD(up)) {
488 __load_zsregs(channel, up->curregs);
489 up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
492 if (ZS_TX_STOPPED(up)) {
493 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
497 if (up->port.x_char) {
498 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
499 writeb(up->port.x_char, &channel->data);
503 up->port.icount.tx++;
508 if (up->port.info == NULL)
510 xmit = &up->port.info->xmit;
511 if (uart_circ_empty(xmit))
514 if (uart_tx_stopped(&up->port))
517 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
518 writeb(xmit->buf[xmit->tail], &channel->data);
522 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
523 up->port.icount.tx++;
525 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
526 uart_write_wakeup(&up->port);
531 writeb(RES_Tx_P, &channel->control);
536 static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
538 struct uart_sunzilog_port *up = dev_id;
541 struct zilog_channel __iomem *channel
542 = ZILOG_CHANNEL_FROM_PORT(&up->port);
543 struct tty_struct *tty;
546 spin_lock(&up->port.lock);
547 r3 = read_zsreg(channel, R3);
551 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
552 writeb(RES_H_IUS, &channel->control);
557 tty = sunzilog_receive_chars(up, channel);
559 sunzilog_status_handle(up, channel);
561 sunzilog_transmit_chars(up, channel);
563 spin_unlock(&up->port.lock);
566 tty_flip_buffer_push(tty);
570 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
572 spin_lock(&up->port.lock);
574 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
575 writeb(RES_H_IUS, &channel->control);
580 tty = sunzilog_receive_chars(up, channel);
582 sunzilog_status_handle(up, channel);
584 sunzilog_transmit_chars(up, channel);
586 spin_unlock(&up->port.lock);
589 tty_flip_buffer_push(tty);
597 /* A convenient way to quickly get R0 status. The caller must _not_ hold the
598 * port lock, it is acquired here.
600 static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
602 struct zilog_channel __iomem *channel;
603 unsigned char status;
605 channel = ZILOG_CHANNEL_FROM_PORT(port);
606 status = readb(&channel->control);
612 /* The port lock is not held. */
613 static unsigned int sunzilog_tx_empty(struct uart_port *port)
616 unsigned char status;
619 spin_lock_irqsave(&port->lock, flags);
621 status = sunzilog_read_channel_status(port);
623 spin_unlock_irqrestore(&port->lock, flags);
625 if (status & Tx_BUF_EMP)
633 /* The port lock is held and interrupts are disabled. */
634 static unsigned int sunzilog_get_mctrl(struct uart_port *port)
636 unsigned char status;
639 status = sunzilog_read_channel_status(port);
652 /* The port lock is held and interrupts are disabled. */
653 static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
655 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
656 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
657 unsigned char set_bits, clear_bits;
659 set_bits = clear_bits = 0;
661 if (mctrl & TIOCM_RTS)
665 if (mctrl & TIOCM_DTR)
670 /* NOTE: Not subject to 'transmitter active' rule. */
671 up->curregs[R5] |= set_bits;
672 up->curregs[R5] &= ~clear_bits;
673 write_zsreg(channel, R5, up->curregs[R5]);
676 /* The port lock is held and interrupts are disabled. */
677 static void sunzilog_stop_tx(struct uart_port *port)
679 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
681 up->flags |= SUNZILOG_FLAG_TX_STOPPED;
684 /* The port lock is held and interrupts are disabled. */
685 static void sunzilog_start_tx(struct uart_port *port)
687 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
688 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
689 unsigned char status;
691 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
692 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
694 status = readb(&channel->control);
697 /* TX busy? Just wait for the TX done interrupt. */
698 if (!(status & Tx_BUF_EMP))
701 /* Send the first character to jump-start the TX done
702 * IRQ sending engine.
705 writeb(port->x_char, &channel->data);
712 struct circ_buf *xmit = &port->info->xmit;
714 writeb(xmit->buf[xmit->tail], &channel->data);
718 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
721 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
722 uart_write_wakeup(&up->port);
726 /* The port lock is held. */
727 static void sunzilog_stop_rx(struct uart_port *port)
729 struct uart_sunzilog_port *up = UART_ZILOG(port);
730 struct zilog_channel __iomem *channel;
735 channel = ZILOG_CHANNEL_FROM_PORT(port);
737 /* Disable all RX interrupts. */
738 up->curregs[R1] &= ~RxINT_MASK;
739 sunzilog_maybe_update_regs(up, channel);
742 /* The port lock is held. */
743 static void sunzilog_enable_ms(struct uart_port *port)
745 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
746 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
747 unsigned char new_reg;
749 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
750 if (new_reg != up->curregs[R15]) {
751 up->curregs[R15] = new_reg;
753 /* NOTE: Not subject to 'transmitter active' rule. */
754 write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
758 /* The port lock is not held. */
759 static void sunzilog_break_ctl(struct uart_port *port, int break_state)
761 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
762 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
763 unsigned char set_bits, clear_bits, new_reg;
766 set_bits = clear_bits = 0;
771 clear_bits |= SND_BRK;
773 spin_lock_irqsave(&port->lock, flags);
775 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
776 if (new_reg != up->curregs[R5]) {
777 up->curregs[R5] = new_reg;
779 /* NOTE: Not subject to 'transmitter active' rule. */
780 write_zsreg(channel, R5, up->curregs[R5]);
783 spin_unlock_irqrestore(&port->lock, flags);
786 static void __sunzilog_startup(struct uart_sunzilog_port *up)
788 struct zilog_channel __iomem *channel;
790 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
791 up->prev_status = readb(&channel->control);
793 /* Enable receiver and transmitter. */
794 up->curregs[R3] |= RxENAB;
795 up->curregs[R5] |= TxENAB;
797 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
798 sunzilog_maybe_update_regs(up, channel);
801 static int sunzilog_startup(struct uart_port *port)
803 struct uart_sunzilog_port *up = UART_ZILOG(port);
809 spin_lock_irqsave(&port->lock, flags);
810 __sunzilog_startup(up);
811 spin_unlock_irqrestore(&port->lock, flags);
816 * The test for ZS_IS_CONS is explained by the following e-mail:
818 * From: Russell King <rmk@arm.linux.org.uk>
819 * Date: Sun, 8 Dec 2002 10:18:38 +0000
821 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
822 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
823 * > and I noticed that something is not right with reference
824 * > counting in this case. It seems that when the console
825 * > is open by kernel initially, this is not accounted
826 * > as an open, and uart_startup is not called.
828 * That is correct. We are unable to call uart_startup when the serial
829 * console is initialised because it may need to allocate memory (as
830 * request_irq does) and the memory allocators may not have been
833 * 1. initialise the port into a state where it can send characters in the
834 * console write method.
836 * 2. don't do the actual hardware shutdown in your shutdown() method (but
837 * do the normal software shutdown - ie, free irqs etc)
840 static void sunzilog_shutdown(struct uart_port *port)
842 struct uart_sunzilog_port *up = UART_ZILOG(port);
843 struct zilog_channel __iomem *channel;
849 spin_lock_irqsave(&port->lock, flags);
851 channel = ZILOG_CHANNEL_FROM_PORT(port);
853 /* Disable receiver and transmitter. */
854 up->curregs[R3] &= ~RxENAB;
855 up->curregs[R5] &= ~TxENAB;
857 /* Disable all interrupts and BRK assertion. */
858 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
859 up->curregs[R5] &= ~SND_BRK;
860 sunzilog_maybe_update_regs(up, channel);
862 spin_unlock_irqrestore(&port->lock, flags);
865 /* Shared by TTY driver and serial console setup. The port lock is held
866 * and local interrupts are disabled.
869 sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
870 unsigned int iflag, int brg)
873 up->curregs[R10] = NRZ;
874 up->curregs[R11] = TCBR | RCBR;
876 /* Program BAUD and clock source. */
877 up->curregs[R4] &= ~XCLK_MASK;
878 up->curregs[R4] |= X16CLK;
879 up->curregs[R12] = brg & 0xff;
880 up->curregs[R13] = (brg >> 8) & 0xff;
881 up->curregs[R14] = BRSRC | BRENAB;
883 /* Character size, stop bits, and parity. */
884 up->curregs[R3] &= ~RxN_MASK;
885 up->curregs[R5] &= ~TxN_MASK;
886 switch (cflag & CSIZE) {
888 up->curregs[R3] |= Rx5;
889 up->curregs[R5] |= Tx5;
890 up->parity_mask = 0x1f;
893 up->curregs[R3] |= Rx6;
894 up->curregs[R5] |= Tx6;
895 up->parity_mask = 0x3f;
898 up->curregs[R3] |= Rx7;
899 up->curregs[R5] |= Tx7;
900 up->parity_mask = 0x7f;
904 up->curregs[R3] |= Rx8;
905 up->curregs[R5] |= Tx8;
906 up->parity_mask = 0xff;
909 up->curregs[R4] &= ~0x0c;
911 up->curregs[R4] |= SB2;
913 up->curregs[R4] |= SB1;
915 up->curregs[R4] |= PAR_ENAB;
917 up->curregs[R4] &= ~PAR_ENAB;
918 if (!(cflag & PARODD))
919 up->curregs[R4] |= PAR_EVEN;
921 up->curregs[R4] &= ~PAR_EVEN;
923 up->port.read_status_mask = Rx_OVR;
925 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
926 if (iflag & (BRKINT | PARMRK))
927 up->port.read_status_mask |= BRK_ABRT;
929 up->port.ignore_status_mask = 0;
931 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
932 if (iflag & IGNBRK) {
933 up->port.ignore_status_mask |= BRK_ABRT;
935 up->port.ignore_status_mask |= Rx_OVR;
938 if ((cflag & CREAD) == 0)
939 up->port.ignore_status_mask = 0xff;
942 /* The port lock is not held. */
944 sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
945 struct ktermios *old)
947 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
951 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
953 spin_lock_irqsave(&up->port.lock, flags);
955 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
957 sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
959 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
960 up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
962 up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
964 up->cflag = termios->c_cflag;
966 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
968 uart_update_timeout(port, termios->c_cflag, baud);
970 spin_unlock_irqrestore(&up->port.lock, flags);
973 static const char *sunzilog_type(struct uart_port *port)
975 struct uart_sunzilog_port *up = UART_ZILOG(port);
977 return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
980 /* We do not request/release mappings of the registers here, this
981 * happens at early serial probe time.
983 static void sunzilog_release_port(struct uart_port *port)
987 static int sunzilog_request_port(struct uart_port *port)
992 /* These do not need to do anything interesting either. */
993 static void sunzilog_config_port(struct uart_port *port, int flags)
997 /* We do not support letting the user mess with the divisor, IRQ, etc. */
998 static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
1003 static struct uart_ops sunzilog_pops = {
1004 .tx_empty = sunzilog_tx_empty,
1005 .set_mctrl = sunzilog_set_mctrl,
1006 .get_mctrl = sunzilog_get_mctrl,
1007 .stop_tx = sunzilog_stop_tx,
1008 .start_tx = sunzilog_start_tx,
1009 .stop_rx = sunzilog_stop_rx,
1010 .enable_ms = sunzilog_enable_ms,
1011 .break_ctl = sunzilog_break_ctl,
1012 .startup = sunzilog_startup,
1013 .shutdown = sunzilog_shutdown,
1014 .set_termios = sunzilog_set_termios,
1015 .type = sunzilog_type,
1016 .release_port = sunzilog_release_port,
1017 .request_port = sunzilog_request_port,
1018 .config_port = sunzilog_config_port,
1019 .verify_port = sunzilog_verify_port,
1022 static struct uart_sunzilog_port *sunzilog_port_table;
1023 static struct zilog_layout __iomem **sunzilog_chip_regs;
1025 static struct uart_sunzilog_port *sunzilog_irq_chain;
1027 static struct uart_driver sunzilog_reg = {
1028 .owner = THIS_MODULE,
1029 .driver_name = "ttyS",
1034 static int __init sunzilog_alloc_tables(void)
1036 struct uart_sunzilog_port *up;
1040 size = NUM_CHANNELS * sizeof(struct uart_sunzilog_port);
1041 sunzilog_port_table = kzalloc(size, GFP_KERNEL);
1042 if (!sunzilog_port_table)
1045 for (i = 0; i < NUM_CHANNELS; i++) {
1046 up = &sunzilog_port_table[i];
1048 spin_lock_init(&up->port.lock);
1051 sunzilog_irq_chain = up;
1053 if (i < NUM_CHANNELS - 1)
1059 size = NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *);
1060 sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
1061 if (!sunzilog_chip_regs) {
1062 kfree(sunzilog_port_table);
1063 sunzilog_irq_chain = NULL;
1070 static void sunzilog_free_tables(void)
1072 kfree(sunzilog_port_table);
1073 sunzilog_irq_chain = NULL;
1074 kfree(sunzilog_chip_regs);
1077 #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
1079 static void sunzilog_putchar(struct uart_port *port, int ch)
1081 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
1082 int loops = ZS_PUT_CHAR_MAX_DELAY;
1084 /* This is a timed polling loop so do not switch the explicit
1085 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1088 unsigned char val = readb(&channel->control);
1089 if (val & Tx_BUF_EMP) {
1096 writeb(ch, &channel->data);
1103 static DEFINE_SPINLOCK(sunzilog_serio_lock);
1105 static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1107 struct uart_sunzilog_port *up = serio->port_data;
1108 unsigned long flags;
1110 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1112 sunzilog_putchar(&up->port, ch);
1114 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1119 static int sunzilog_serio_open(struct serio *serio)
1121 struct uart_sunzilog_port *up = serio->port_data;
1122 unsigned long flags;
1125 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1126 if (!up->serio_open) {
1131 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1136 static void sunzilog_serio_close(struct serio *serio)
1138 struct uart_sunzilog_port *up = serio->port_data;
1139 unsigned long flags;
1141 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1143 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1146 #endif /* CONFIG_SERIO */
1148 #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1150 sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1152 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1153 unsigned long flags;
1156 local_irq_save(flags);
1157 if (up->port.sysrq) {
1159 } else if (oops_in_progress) {
1160 locked = spin_trylock(&up->port.lock);
1162 spin_lock(&up->port.lock);
1164 uart_console_write(&up->port, s, count, sunzilog_putchar);
1168 spin_unlock(&up->port.lock);
1169 local_irq_restore(flags);
1172 static int __init sunzilog_console_setup(struct console *con, char *options)
1174 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1175 unsigned long flags;
1178 if (up->port.type != PORT_SUNZILOG)
1181 printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
1182 (sunzilog_reg.minor - 64) + con->index, con->index);
1184 /* Get firmware console settings. */
1185 sunserial_console_termios(con);
1187 /* Firmware console speed is limited to 150-->38400 baud so
1188 * this hackish cflag thing is OK.
1190 switch (con->cflag & CBAUD) {
1191 case B150: baud = 150; break;
1192 case B300: baud = 300; break;
1193 case B600: baud = 600; break;
1194 case B1200: baud = 1200; break;
1195 case B2400: baud = 2400; break;
1196 case B4800: baud = 4800; break;
1197 default: case B9600: baud = 9600; break;
1198 case B19200: baud = 19200; break;
1199 case B38400: baud = 38400; break;
1202 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1204 spin_lock_irqsave(&up->port.lock, flags);
1206 up->curregs[R15] |= BRKIE;
1207 sunzilog_convert_to_zs(up, con->cflag, 0, brg);
1209 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1210 __sunzilog_startup(up);
1212 spin_unlock_irqrestore(&up->port.lock, flags);
1217 static struct console sunzilog_console_ops = {
1219 .write = sunzilog_console_write,
1220 .device = uart_console_device,
1221 .setup = sunzilog_console_setup,
1222 .flags = CON_PRINTBUFFER,
1224 .data = &sunzilog_reg,
1227 static inline struct console *SUNZILOG_CONSOLE(void)
1229 return &sunzilog_console_ops;
1233 #define SUNZILOG_CONSOLE() (NULL)
1236 static void __devinit sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
1240 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1241 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1244 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1248 up->curregs[R15] |= BRKIE;
1249 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1250 sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1251 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1252 __sunzilog_startup(up);
1256 static void __devinit sunzilog_register_serio(struct uart_sunzilog_port *up)
1258 struct serio *serio = &up->serio;
1260 serio->port_data = up;
1262 serio->id.type = SERIO_RS232;
1263 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1264 serio->id.proto = SERIO_SUNKBD;
1265 strlcpy(serio->name, "zskbd", sizeof(serio->name));
1267 serio->id.proto = SERIO_SUN;
1268 serio->id.extra = 1;
1269 strlcpy(serio->name, "zsms", sizeof(serio->name));
1271 strlcpy(serio->phys,
1272 ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
1273 "zs/serio0" : "zs/serio1"),
1274 sizeof(serio->phys));
1276 serio->write = sunzilog_serio_write;
1277 serio->open = sunzilog_serio_open;
1278 serio->close = sunzilog_serio_close;
1279 serio->dev.parent = up->port.dev;
1281 serio_register_port(serio);
1285 static void __devinit sunzilog_init_hw(struct uart_sunzilog_port *up)
1287 struct zilog_channel __iomem *channel;
1288 unsigned long flags;
1291 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1293 spin_lock_irqsave(&up->port.lock, flags);
1294 if (ZS_IS_CHANNEL_A(up)) {
1295 write_zsreg(channel, R9, FHWRES);
1297 (void) read_zsreg(channel, R0);
1300 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1301 SUNZILOG_FLAG_CONS_MOUSE)) {
1302 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1303 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1304 up->curregs[R3] = RxENAB | Rx8;
1305 up->curregs[R5] = TxENAB | Tx8;
1306 up->curregs[R6] = 0x00; /* SDLC Address */
1307 up->curregs[R7] = 0x7E; /* SDLC Flag */
1308 up->curregs[R9] = NV;
1309 up->curregs[R7p] = 0x00;
1310 sunzilog_init_kbdms(up, up->port.line);
1311 /* Only enable interrupts if an ISR handler available */
1312 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1313 up->curregs[R9] |= MIE;
1314 write_zsreg(channel, R9, up->curregs[R9]);
1316 /* Normal serial TTY. */
1317 up->parity_mask = 0xff;
1318 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1319 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1320 up->curregs[R3] = RxENAB | Rx8;
1321 up->curregs[R5] = TxENAB | Tx8;
1322 up->curregs[R6] = 0x00; /* SDLC Address */
1323 up->curregs[R7] = 0x7E; /* SDLC Flag */
1324 up->curregs[R9] = NV;
1325 up->curregs[R10] = NRZ;
1326 up->curregs[R11] = TCBR | RCBR;
1328 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1329 up->curregs[R12] = (brg & 0xff);
1330 up->curregs[R13] = (brg >> 8) & 0xff;
1331 up->curregs[R14] = BRSRC | BRENAB;
1332 up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
1333 up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
1334 if (__load_zsregs(channel, up->curregs)) {
1335 up->flags |= SUNZILOG_FLAG_ESCC;
1337 /* Only enable interrupts if an ISR handler available */
1338 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1339 up->curregs[R9] |= MIE;
1340 write_zsreg(channel, R9, up->curregs[R9]);
1343 spin_unlock_irqrestore(&up->port.lock, flags);
1346 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1347 SUNZILOG_FLAG_CONS_MOUSE))
1348 sunzilog_register_serio(up);
1352 static int zilog_irq = -1;
1354 static int __devinit zs_probe(struct of_device *op, const struct of_device_id *match)
1357 struct uart_sunzilog_port *up;
1358 struct zilog_layout __iomem *rp;
1363 if (of_find_property(op->node, "keyboard", NULL))
1366 sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
1367 sizeof(struct zilog_layout),
1369 if (!sunzilog_chip_regs[inst])
1372 rp = sunzilog_chip_regs[inst];
1374 if (zilog_irq == -1)
1375 zilog_irq = op->irqs[0];
1377 up = &sunzilog_port_table[inst * 2];
1380 up[0].port.mapbase = op->resource[0].start + 0x00;
1381 up[0].port.membase = (void __iomem *) &rp->channelA;
1382 up[0].port.iotype = UPIO_MEM;
1383 up[0].port.irq = op->irqs[0];
1384 up[0].port.uartclk = ZS_CLOCK;
1385 up[0].port.fifosize = 1;
1386 up[0].port.ops = &sunzilog_pops;
1387 up[0].port.type = PORT_SUNZILOG;
1388 up[0].port.flags = 0;
1389 up[0].port.line = (inst * 2) + 0;
1390 up[0].port.dev = &op->dev;
1391 up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1393 up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
1394 sunzilog_init_hw(&up[0]);
1397 up[1].port.mapbase = op->resource[0].start + 0x04;
1398 up[1].port.membase = (void __iomem *) &rp->channelB;
1399 up[1].port.iotype = UPIO_MEM;
1400 up[1].port.irq = op->irqs[0];
1401 up[1].port.uartclk = ZS_CLOCK;
1402 up[1].port.fifosize = 1;
1403 up[1].port.ops = &sunzilog_pops;
1404 up[1].port.type = PORT_SUNZILOG;
1405 up[1].port.flags = 0;
1406 up[1].port.line = (inst * 2) + 1;
1407 up[1].port.dev = &op->dev;
1410 up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
1411 sunzilog_init_hw(&up[1]);
1413 if (!keyboard_mouse) {
1414 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->node,
1415 &sunzilog_reg, up[0].port.line))
1416 up->flags |= SUNZILOG_FLAG_IS_CONS;
1417 err = uart_add_one_port(&sunzilog_reg, &up[0].port);
1419 of_iounmap(&op->resource[0],
1420 rp, sizeof(struct zilog_layout));
1423 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->node,
1424 &sunzilog_reg, up[1].port.line))
1425 up->flags |= SUNZILOG_FLAG_IS_CONS;
1426 err = uart_add_one_port(&sunzilog_reg, &up[1].port);
1428 uart_remove_one_port(&sunzilog_reg, &up[0].port);
1429 of_iounmap(&op->resource[0],
1430 rp, sizeof(struct zilog_layout));
1434 printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
1437 (unsigned long long) up[0].port.mapbase,
1438 op->irqs[0], sunzilog_type(&up[0].port));
1439 printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
1442 (unsigned long long) up[1].port.mapbase,
1443 op->irqs[0], sunzilog_type(&up[1].port));
1446 dev_set_drvdata(&op->dev, &up[0]);
1453 static void __devexit zs_remove_one(struct uart_sunzilog_port *up)
1455 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
1457 serio_unregister_port(&up->serio);
1460 uart_remove_one_port(&sunzilog_reg, &up->port);
1463 static int __devexit zs_remove(struct of_device *op)
1465 struct uart_sunzilog_port *up = dev_get_drvdata(&op->dev);
1466 struct zilog_layout __iomem *regs;
1468 zs_remove_one(&up[0]);
1469 zs_remove_one(&up[1]);
1471 regs = sunzilog_chip_regs[up[0].port.line / 2];
1472 of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
1474 dev_set_drvdata(&op->dev, NULL);
1479 static struct of_device_id zs_match[] = {
1485 MODULE_DEVICE_TABLE(of, zs_match);
1487 static struct of_platform_driver zs_driver = {
1489 .match_table = zs_match,
1491 .remove = __devexit_p(zs_remove),
1494 static int __init sunzilog_init(void)
1496 struct device_node *dp;
1497 int err, uart_count;
1502 for_each_node_by_name(dp, "zs") {
1504 if (of_find_property(dp, "keyboard", NULL))
1512 err = sunzilog_alloc_tables();
1516 uart_count = (NUM_SUNZILOG * 2) - (2 * num_keybms);
1518 sunzilog_reg.nr = uart_count;
1519 sunzilog_reg.minor = sunserial_current_minor;
1520 err = uart_register_driver(&sunzilog_reg);
1522 goto out_free_tables;
1524 sunzilog_reg.tty_driver->name_base = sunzilog_reg.minor - 64;
1526 sunserial_current_minor += uart_count;
1529 err = of_register_driver(&zs_driver, &of_bus_type);
1531 goto out_unregister_uart;
1533 if (zilog_irq != -1) {
1534 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1535 err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
1536 "zs", sunzilog_irq_chain);
1538 goto out_unregister_driver;
1540 /* Enable Interrupts */
1542 struct zilog_channel __iomem *channel;
1544 /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
1545 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1546 up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
1547 up->curregs[R9] |= MIE;
1548 write_zsreg(channel, R9, up->curregs[R9]);
1556 out_unregister_driver:
1557 of_unregister_driver(&zs_driver);
1559 out_unregister_uart:
1561 uart_unregister_driver(&sunzilog_reg);
1562 sunzilog_reg.cons = NULL;
1566 sunzilog_free_tables();
1570 static void __exit sunzilog_exit(void)
1572 of_unregister_driver(&zs_driver);
1574 if (zilog_irq != -1) {
1575 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1577 /* Disable Interrupts */
1579 struct zilog_channel __iomem *channel;
1581 /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
1582 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1583 up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
1584 up->curregs[R9] &= ~MIE;
1585 write_zsreg(channel, R9, up->curregs[R9]);
1589 free_irq(zilog_irq, sunzilog_irq_chain);
1594 uart_unregister_driver(&sunzilog_reg);
1595 sunzilog_free_tables();
1599 module_init(sunzilog_init);
1600 module_exit(sunzilog_exit);
1602 MODULE_AUTHOR("David S. Miller");
1603 MODULE_DESCRIPTION("Sun Zilog serial port driver");
1604 MODULE_VERSION("2.0");
1605 MODULE_LICENSE("GPL");