15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
19 #include <linux/tty.h>
22 #include <linux/string.h>
23 #include <linux/ptrace.h>
25 #include <linux/slab.h>
27 #include <linux/serial.h>
32 #include <linux/serio.h>
40 #include <asm/setup.h>
42 #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
46 #include <linux/serial_core.h>
56 #ifndef CONFIG_SPARC64
57 #define ZSDELAY() udelay(5)
58 #define ZSDELAY_LONG() udelay(20)
59 #define ZS_WSYNC(channel) do { } while (0)
62 #define ZSDELAY_LONG()
63 #define ZS_WSYNC(__channel) \
64 readb(&((__channel)->control))
67 #define ZS_CLOCK 4915200
68 #define ZS_CLOCK_DIVISOR 16
83 #define SUNZILOG_FLAG_CONS_KEYB 0x00000001
84 #define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
85 #define SUNZILOG_FLAG_IS_CONS 0x00000004
86 #define SUNZILOG_FLAG_IS_KGDB 0x00000008
87 #define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
88 #define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
89 #define SUNZILOG_FLAG_REGS_HELD 0x00000040
90 #define SUNZILOG_FLAG_TX_STOPPED 0x00000080
91 #define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
92 #define SUNZILOG_FLAG_ESCC 0x00000200
93 #define SUNZILOG_FLAG_ISR_HANDLER 0x00000400
106 static void sunzilog_putchar(
struct uart_port *
port,
int ch);
108 #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
109 #define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
111 #define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
112 #define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
113 #define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
114 #define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
115 #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
116 #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
117 #define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
118 #define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
119 #define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
134 writeb(reg, &channel->control);
136 retval =
readb(&channel->control);
143 unsigned char reg,
unsigned char value)
145 writeb(reg, &channel->control);
147 writeb(value, &channel->control);
155 for (i = 0; i < 32; i++) {
158 regval =
readb(&channel->control);
163 regval = read_zsreg(channel,
R1);
164 readb(&channel->data);
185 for (i = 0; i < 1000; i++) {
186 unsigned char stat = read_zsreg(channel,
R1);
196 sunzilog_clear_fifo(channel);
199 write_zsreg(channel,
R1,
203 write_zsreg(channel,
R4, regs[
R4]);
206 write_zsreg(channel,
R10, regs[
R10]);
213 write_zsreg(channel,
R6, regs[
R6]);
214 write_zsreg(channel,
R7, regs[
R7]);
225 write_zsreg(channel,
R11, regs[
R11]);
228 write_zsreg(channel,
R12, regs[
R12]);
229 write_zsreg(channel,
R13, regs[
R13]);
232 write_zsreg(channel,
R14, regs[
R14]);
238 r15 = read_zsreg(channel,
R15);
240 write_zsreg(channel,
R7, regs[
R7p]);
256 write_zsreg(channel,
R3, regs[
R3]);
257 write_zsreg(channel,
R5, regs[
R5]);
260 write_zsreg(channel,
R1, regs[
R1]);
278 __load_zsregs(channel, up->
curregs);
285 unsigned int cur_cflag = up->
cflag;
298 unsigned char ch,
int is_break)
311 sunzilog_change_mouse_baud(up);
331 unsigned char ch,
r1,
flag;
336 tty = up->
port.state->port.tty;
340 r1 = read_zsreg(channel,
R1);
347 ch =
readb(&channel->control);
356 if (!(ch & Rx_CH_AV))
359 ch =
readb(&channel->data);
365 sunzilog_kbdms_receive_chars(up, ch, 0);
376 up->
port.icount.rx++;
380 up->
port.icount.brk++;
381 if (uart_handle_break(&up->
port))
385 up->
port.icount.parity++;
387 up->
port.icount.frame++;
389 up->
port.icount.overrun++;
390 r1 &= up->
port.read_status_mask;
393 else if (r1 & PAR_ERR)
395 else if (r1 & CRC_ERR)
401 if (up->
port.ignore_status_mask == 0xff ||
402 (r1 & up->
port.ignore_status_mask) == 0) {
403 tty_insert_flip_char(tty, ch, flag);
417 status =
readb(&channel->control);
424 if (status & BRK_ABRT) {
426 sunzilog_kbdms_receive_chars(up, 0, 1);
432 status =
readb(&channel->control);
434 if (!(status & BRK_ABRT))
444 up->
port.icount.dsr++;
469 unsigned char status =
readb(&channel->control);
487 __load_zsregs(channel, up->
curregs);
496 if (up->
port.x_char) {
502 up->
port.icount.tx++;
509 xmit = &up->
port.state->xmit;
513 if (uart_tx_stopped(&up->
port))
522 up->
port.icount.tx++;
545 spin_lock(&up->
port.lock);
546 r3 = read_zsreg(channel,
R3);
556 tty = sunzilog_receive_chars(up, channel);
558 sunzilog_status_handle(up, channel);
560 sunzilog_transmit_chars(up, channel);
562 spin_unlock(&up->
port.lock);
571 spin_lock(&up->
port.lock);
579 tty = sunzilog_receive_chars(up, channel);
581 sunzilog_status_handle(up, channel);
583 sunzilog_transmit_chars(up, channel);
585 spin_unlock(&up->
port.lock);
612 static unsigned int sunzilog_tx_empty(
struct uart_port *
port)
620 status = sunzilog_read_channel_status(port);
622 spin_unlock_irqrestore(&port->
lock, flags);
633 static unsigned int sunzilog_get_mctrl(
struct uart_port *port)
638 status = sunzilog_read_channel_status(port);
652 static void sunzilog_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
658 set_bits = clear_bits = 0;
676 static void sunzilog_stop_tx(
struct uart_port *port)
684 static void sunzilog_start_tx(
struct uart_port *port)
726 static void sunzilog_stop_rx(
struct uart_port *port)
738 sunzilog_maybe_update_regs(up, channel);
742 static void sunzilog_enable_ms(
struct uart_port *port)
746 unsigned char new_reg;
758 static void sunzilog_break_ctl(
struct uart_port *port,
int break_state)
762 unsigned char set_bits, clear_bits, new_reg;
765 set_bits = clear_bits = 0;
782 spin_unlock_irqrestore(&port->
lock, flags);
797 sunzilog_maybe_update_regs(up, channel);
800 static int sunzilog_startup(
struct uart_port *port)
809 __sunzilog_startup(up);
810 spin_unlock_irqrestore(&port->
lock, flags);
839 static void sunzilog_shutdown(
struct uart_port *port)
859 sunzilog_maybe_update_regs(up, channel);
861 spin_unlock_irqrestore(&port->
lock, flags);
869 unsigned int iflag,
int brg)
885 switch (cflag &
CSIZE) {
922 up->
port.read_status_mask = Rx_OVR;
926 up->
port.read_status_mask |= BRK_ABRT;
928 up->
port.ignore_status_mask = 0;
937 if ((cflag &
CREAD) == 0)
938 up->
port.ignore_status_mask = 0xff;
956 sunzilog_convert_to_zs(up, termios->
c_cflag, termios->
c_iflag, brg);
969 spin_unlock_irqrestore(&up->
port.lock, flags);
972 static const char *sunzilog_type(
struct uart_port *port)
982 static void sunzilog_release_port(
struct uart_port *port)
986 static int sunzilog_request_port(
struct uart_port *port)
992 static void sunzilog_config_port(
struct uart_port *port,
int flags)
1002 #ifdef CONFIG_CONSOLE_POLL
1003 static int sunzilog_get_poll_char(
struct uart_port *port)
1005 unsigned char ch,
r1;
1011 r1 = read_zsreg(channel,
R1);
1012 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
1027 if (!(ch & Rx_CH_AV))
1037 static void sunzilog_put_poll_char(
struct uart_port *port,
1042 sunzilog_putchar(&up->
port, ch);
1046 static struct uart_ops sunzilog_pops = {
1047 .tx_empty = sunzilog_tx_empty,
1048 .set_mctrl = sunzilog_set_mctrl,
1049 .get_mctrl = sunzilog_get_mctrl,
1050 .stop_tx = sunzilog_stop_tx,
1051 .start_tx = sunzilog_start_tx,
1052 .stop_rx = sunzilog_stop_rx,
1053 .enable_ms = sunzilog_enable_ms,
1054 .break_ctl = sunzilog_break_ctl,
1055 .startup = sunzilog_startup,
1056 .shutdown = sunzilog_shutdown,
1057 .set_termios = sunzilog_set_termios,
1058 .type = sunzilog_type,
1059 .release_port = sunzilog_release_port,
1060 .request_port = sunzilog_request_port,
1061 .config_port = sunzilog_config_port,
1062 .verify_port = sunzilog_verify_port,
1063 #ifdef CONFIG_CONSOLE_POLL
1064 .poll_get_char = sunzilog_get_poll_char,
1065 .poll_put_char = sunzilog_put_poll_char,
1069 static int uart_chip_count;
1077 .driver_name =
"sunzilog",
1082 static int __init sunzilog_alloc_tables(
int num_sunzilog)
1090 sunzilog_port_table = kzalloc(size,
GFP_KERNEL);
1091 if (!sunzilog_port_table)
1095 up = &sunzilog_port_table[
i];
1100 sunzilog_irq_chain =
up;
1102 if (i < num_channels - 1)
1109 sunzilog_chip_regs = kzalloc(size,
GFP_KERNEL);
1110 if (!sunzilog_chip_regs) {
1111 kfree(sunzilog_port_table);
1112 sunzilog_irq_chain =
NULL;
1119 static void sunzilog_free_tables(
void)
1121 kfree(sunzilog_port_table);
1122 sunzilog_irq_chain =
NULL;
1123 kfree(sunzilog_chip_regs);
1126 #define ZS_PUT_CHAR_MAX_DELAY 2000
1128 static void sunzilog_putchar(
struct uart_port *port,
int ch)
1138 if (val & Tx_BUF_EMP) {
1154 static int sunzilog_serio_write(
struct serio *
serio,
unsigned char ch)
1157 unsigned long flags;
1161 sunzilog_putchar(&up->
port, ch);
1163 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1168 static int sunzilog_serio_open(
struct serio *
serio)
1171 unsigned long flags;
1175 if (!up->serio_open) {
1180 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1185 static void sunzilog_serio_close(
struct serio *serio)
1188 unsigned long flags;
1192 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1197 #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1199 sunzilog_console_write(
struct console *
con,
const char *
s,
unsigned int count)
1202 unsigned long flags;
1206 if (up->
port.sysrq) {
1209 locked = spin_trylock(&up->
port.lock);
1211 spin_lock(&up->
port.lock);
1217 spin_unlock(&up->
port.lock);
1224 unsigned long flags;
1240 case B150: baud = 150;
break;
1241 case B300: baud = 300;
break;
1242 case B600: baud = 600;
break;
1243 case B1200: baud = 1200;
break;
1244 case B2400: baud = 2400;
break;
1245 case B4800: baud = 4800;
break;
1246 default:
case B9600: baud = 9600;
break;
1247 case B19200: baud = 19200;
break;
1248 case B38400: baud = 38400;
break;
1256 sunzilog_convert_to_zs(up, con->
cflag, 0, brg);
1259 __sunzilog_startup(up);
1261 spin_unlock_irqrestore(&up->
port.lock, flags);
1266 static struct console sunzilog_console_ops = {
1268 .write = sunzilog_console_write,
1270 .setup = sunzilog_console_setup,
1273 .data = &sunzilog_reg,
1278 return &sunzilog_console_ops;
1282 #define SUNZILOG_CONSOLE() (NULL)
1299 sunzilog_convert_to_zs(up, up->
cflag, 0, brg);
1301 __sunzilog_startup(up);
1307 struct serio *serio = &up->serio;
1317 serio->
id.extra = 1;
1322 "zs/serio0" :
"zs/serio1"),
1323 sizeof(serio->
phys));
1325 serio->
write = sunzilog_serio_write;
1326 serio->
open = sunzilog_serio_open;
1327 serio->
close = sunzilog_serio_close;
1328 serio->
dev.parent = up->
port.dev;
1337 unsigned long flags;
1346 (
void) read_zsreg(channel,
R0);
1359 sunzilog_init_kbdms(up);
1383 if (__load_zsregs(channel, up->
curregs)) {
1392 spin_unlock_irqrestore(&up->
port.lock, flags);
1397 sunzilog_register_serio(up);
1401 static int zilog_irq;
1405 static int kbm_inst, uart_inst;
1409 int keyboard_mouse = 0;
1417 inst = uart_chip_count + kbm_inst;
1424 if (!sunzilog_chip_regs[inst])
1427 rp = sunzilog_chip_regs[inst];
1432 up = &sunzilog_port_table[inst * 2];
1440 up[0].
port.fifosize = 1;
1441 up[0].
port.ops = &sunzilog_pops;
1443 up[0].
port.flags = 0;
1444 up[0].
port.line = (inst * 2) + 0;
1449 sunzilog_init_hw(&up[0]);
1457 up[1].
port.fifosize = 1;
1458 up[1].
port.ops = &sunzilog_pops;
1460 up[1].
port.flags = 0;
1461 up[1].
port.line = (inst * 2) + 1;
1466 sunzilog_init_hw(&up[1]);
1468 if (!keyboard_mouse) {
1470 &sunzilog_reg, up[0].
port.line,
1480 &sunzilog_reg, up[1].
port.line,
1495 (
unsigned long long) up[0].
port.mapbase,
1496 op->
archdata.irqs[0], sunzilog_type(&up[0].port));
1500 (
unsigned long long) up[1].port.mapbase,
1501 op->
archdata.irqs[0], sunzilog_type(&up[1].port));
1525 zs_remove_one(&up[0]);
1526 zs_remove_one(&up[1]);
1528 regs = sunzilog_chip_regs[up[0].
port.line / 2];
1548 .of_match_table = zs_match,
1554 static int __init sunzilog_init(
void)
1559 int num_sunzilog = 0;
1561 for_each_node_by_name(dp,
"zs") {
1568 err = sunzilog_alloc_tables(num_sunzilog);
1572 uart_chip_count = num_sunzilog - num_keybms;
1575 uart_chip_count * 2);
1577 goto out_free_tables;
1582 goto out_unregister_uart;
1587 "zs", sunzilog_irq_chain);
1589 goto out_unregister_driver;
1607 out_unregister_driver:
1610 out_unregister_uart:
1617 sunzilog_free_tables();
1621 static void __exit sunzilog_exit(
void)
1640 free_irq(zilog_irq, sunzilog_irq_chain);
1644 if (sunzilog_reg.
nr) {
1646 sunzilog_free_tables();