46 #if defined(CONFIG_SERIAL_ZS_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
53 #include <linux/errno.h>
59 #include <linux/kernel.h>
60 #include <linux/module.h>
62 #include <linux/serial.h>
63 #include <linux/serial_core.h>
66 #include <linux/tty.h>
68 #include <linux/types.h>
84 static char zs_name[]
__initdata =
"DECstation Z85C30 serial driver version ";
96 #define ZS_CHAN_IO_SIZE 8
97 #define ZS_CHAN_IO_STRIDE 4
98 #define ZS_CHAN_IO_OFFSET 1
100 #define ZS_CLOCK 7372800
102 #define to_zport(uport) container_of(uport, struct zs_port, port)
136 static void recovery_delay(
void)
141 static u8 read_zsreg(
struct zs_port *zport,
int reg)
147 writeb(reg & 0xf, control);
151 retval =
readb(control);
156 static void write_zsreg(
struct zs_port *zport,
int reg,
u8 value)
161 writeb(reg & 0xf, control);
170 static u8 read_zsdata(
struct zs_port *zport)
176 retval =
readb(data);
181 static void write_zsdata(
struct zs_port *zport,
u8 value)
195 struct zs_port *zport;
204 for (j = 0; j < 16; j++)
205 printk(
"W%-2d = 0x%02x\t", j, zport->regs[j]);
207 for (j = 0; j < 16; j++)
208 printk(
"R%-2d = 0x%02x\t", j, read_zsreg(zport, j));
215 static void zs_spin_lock_cond_irq(
spinlock_t *lock,
int irq)
223 static void zs_spin_unlock_cond_irq(
spinlock_t *lock,
int irq)
226 spin_unlock_irq(lock);
231 static int zs_receive_drain(
struct zs_port *zport)
235 while ((read_zsreg(zport,
R0) &
Rx_CH_AV) && --loops)
240 static int zs_transmit_drain(
struct zs_port *zport,
int irq)
242 struct zs_scc *
scc = zport->scc;
245 while (!(read_zsreg(zport,
R0) &
Tx_BUF_EMP) && --loops) {
246 zs_spin_unlock_cond_irq(&scc->zlock, irq);
248 zs_spin_lock_cond_irq(&scc->zlock, irq);
253 static int zs_line_drain(
struct zs_port *zport,
int irq)
255 struct zs_scc *scc = zport->scc;
258 while (!(read_zsreg(zport,
R1) &
ALL_SNT) && --loops) {
259 zs_spin_unlock_cond_irq(&scc->zlock, irq);
261 zs_spin_lock_cond_irq(&scc->zlock, irq);
267 static void load_zsregs(
struct zs_port *zport,
u8 *
regs,
int irq)
270 zs_line_drain(zport, irq);
273 write_zsreg(zport,
R5, regs[5] & ~
TxENAB);
274 write_zsreg(zport,
R4, regs[4]);
275 write_zsreg(zport,
R9, regs[9]);
276 write_zsreg(zport,
R1, regs[1]);
277 write_zsreg(zport,
R2, regs[2]);
278 write_zsreg(zport,
R10, regs[10]);
280 write_zsreg(zport,
R11, regs[11]);
281 write_zsreg(zport,
R12, regs[12]);
282 write_zsreg(zport,
R13, regs[13]);
283 write_zsreg(zport,
R14, regs[14]);
284 write_zsreg(zport,
R15, regs[15]);
286 write_zsreg(zport,
R3, regs[3]);
288 write_zsreg(zport,
R5, regs[5]);
307 static unsigned int zs_tx_empty(
struct uart_port *uport)
309 struct zs_port *zport =
to_zport(uport);
310 struct zs_scc *scc = zport->scc;
315 status = read_zsreg(zport,
R1);
316 spin_unlock_irqrestore(&scc->zlock, flags);
321 static unsigned int zs_raw_get_ab_mctrl(
struct zs_port *zport_a,
322 struct zs_port *zport_b)
324 u8 status_a, status_b;
327 status_a = read_zsreg(zport_a,
R0);
328 status_b = read_zsreg(zport_b,
R0);
338 static unsigned int zs_raw_get_mctrl(
struct zs_port *zport)
340 struct zs_port *zport_a = &zport->scc->zport[
ZS_CHAN_A];
342 return zport != zport_a ? zs_raw_get_ab_mctrl(zport_a, zport) : 0;
345 static unsigned int zs_raw_xor_mctrl(
struct zs_port *zport)
347 struct zs_port *zport_a = &zport->scc->zport[
ZS_CHAN_A];
348 unsigned int mmask, mctrl,
delta;
351 if (zport == zport_a)
354 mask_a = zport_a->regs[15];
355 mask_b = zport->regs[15];
362 mctrl = zport->mctrl;
365 mctrl |= zs_raw_get_ab_mctrl(zport_a, zport) & mmask;
368 delta = mctrl ^ zport->mctrl;
370 zport->mctrl = mctrl;
375 static unsigned int zs_get_mctrl(
struct uart_port *uport)
377 struct zs_port *zport =
to_zport(uport);
378 struct zs_scc *scc = zport->scc;
381 spin_lock(&scc->zlock);
382 mctrl = zs_raw_get_mctrl(zport);
383 spin_unlock(&scc->zlock);
388 static void zs_set_mctrl(
struct uart_port *uport,
unsigned int mctrl)
390 struct zs_port *zport =
to_zport(uport);
391 struct zs_scc *scc = zport->scc;
392 struct zs_port *zport_a = &scc->zport[
ZS_CHAN_A];
395 spin_lock(&scc->zlock);
396 if (zport != zport_a) {
398 zport_a->regs[5] |=
DTR;
400 zport_a->regs[5] &= ~
DTR;
402 zport_a->regs[5] |=
RTS;
404 zport_a->regs[5] &= ~
RTS;
405 write_zsreg(zport_a,
R5, zport_a->regs[5]);
409 oldloop = zport->regs[14];
415 if (newloop != oldloop) {
416 zport->regs[14] = newloop;
417 write_zsreg(zport,
R14, zport->regs[14]);
419 spin_unlock(&scc->zlock);
422 static void zs_raw_stop_tx(
struct zs_port *zport)
425 zport->tx_stopped = 1;
428 static void zs_stop_tx(
struct uart_port *uport)
430 struct zs_port *zport =
to_zport(uport);
431 struct zs_scc *scc = zport->scc;
433 spin_lock(&scc->zlock);
434 zs_raw_stop_tx(zport);
435 spin_unlock(&scc->zlock);
438 static void zs_raw_transmit_chars(
struct zs_port *);
440 static void zs_start_tx(
struct uart_port *uport)
442 struct zs_port *zport =
to_zport(uport);
443 struct zs_scc *scc = zport->scc;
445 spin_lock(&scc->zlock);
446 if (zport->tx_stopped) {
447 zs_transmit_drain(zport, 0);
448 zport->tx_stopped = 0;
449 zs_raw_transmit_chars(zport);
451 spin_unlock(&scc->zlock);
454 static void zs_stop_rx(
struct uart_port *uport)
456 struct zs_port *zport =
to_zport(uport);
457 struct zs_scc *scc = zport->scc;
458 struct zs_port *zport_a = &scc->zport[
ZS_CHAN_A];
460 spin_lock(&scc->zlock);
461 zport->regs[15] &= ~
BRKIE;
465 if (zport != zport_a) {
468 write_zsreg(zport_a,
R15, zport_a->regs[15]);
469 if (!(zport_a->regs[15] &
BRKIE)) {
471 write_zsreg(zport_a,
R1, zport_a->regs[1]);
483 write_zsreg(zport,
R15, zport->regs[15]);
484 write_zsreg(zport,
R1, zport->regs[1]);
485 spin_unlock(&scc->zlock);
488 static void zs_enable_ms(
struct uart_port *uport)
490 struct zs_port *zport =
to_zport(uport);
491 struct zs_scc *scc = zport->scc;
492 struct zs_port *zport_a = &scc->zport[
ZS_CHAN_A];
494 if (zport == zport_a)
497 spin_lock(&scc->zlock);
510 zs_raw_xor_mctrl(zport);
512 write_zsreg(zport_a,
R1, zport_a->regs[1]);
513 write_zsreg(zport_a,
R15, zport_a->regs[15]);
514 write_zsreg(zport,
R15, zport->regs[15]);
515 spin_unlock(&scc->zlock);
518 static void zs_break_ctl(
struct uart_port *uport,
int break_state)
520 struct zs_port *zport =
to_zport(uport);
521 struct zs_scc *scc = zport->scc;
525 if (break_state == -1)
529 write_zsreg(zport,
R5, zport->regs[5]);
530 spin_unlock_irqrestore(&scc->zlock, flags);
537 #define Rx_BRK 0x0100
538 #define Rx_SYS 0x0200
540 static void zs_receive_chars(
struct zs_port *zport)
543 struct zs_scc *scc = zport->scc;
548 for (count = 16;
count; count--) {
549 spin_lock(&scc->zlock);
551 spin_unlock(&scc->zlock);
555 spin_lock(&scc->zlock);
557 ch = read_zsdata(zport);
558 spin_unlock(&scc->zlock);
567 status |= zport->tty_break;
570 zport->tty_break = 0;
574 spin_lock(&scc->zlock);
576 spin_unlock(&scc->zlock);
594 else if (status & FRM_ERR)
596 else if (status & PAR_ERR)
609 static void zs_raw_transmit_chars(
struct zs_port *zport)
614 if (zport->port.x_char) {
615 write_zsdata(zport, zport->port.x_char);
616 zport->port.icount.tx++;
617 zport->port.x_char = 0;
623 zs_raw_stop_tx(zport);
628 write_zsdata(zport, xmit->
buf[xmit->
tail]);
630 zport->port.icount.tx++;
637 zs_raw_stop_tx(zport);
640 static void zs_transmit_chars(
struct zs_port *zport)
642 struct zs_scc *scc = zport->scc;
644 spin_lock(&scc->zlock);
645 zs_raw_transmit_chars(zport);
646 spin_unlock(&scc->zlock);
649 static void zs_status_handle(
struct zs_port *zport,
struct zs_port *zport_a)
652 struct zs_scc *scc = zport->scc;
656 spin_lock(&scc->zlock);
659 status = read_zsreg(zport,
R0);
661 if (zport->regs[15] &
BRKIE) {
663 if (brk && !zport->brk) {
664 spin_unlock(&scc->zlock);
665 if (uart_handle_break(uport))
666 zport->tty_break =
Rx_SYS;
668 zport->tty_break =
Rx_BRK;
669 spin_lock(&scc->zlock);
674 if (zport != zport_a) {
675 delta = zs_raw_xor_mctrl(zport);
676 spin_unlock(&scc->zlock);
680 zport->
mctrl & TIOCM_CTS);
681 if (delta & TIOCM_CAR)
683 zport->
mctrl & TIOCM_CAR);
684 if (delta & TIOCM_RNG)
686 if (delta & TIOCM_DSR)
692 spin_lock(&scc->zlock);
698 spin_unlock(&scc->zlock);
706 struct zs_scc *scc =
dev_id;
707 struct zs_port *zport_a = &scc->zport[
ZS_CHAN_A];
708 struct zs_port *zport_b = &scc->zport[
ZS_CHAN_B];
720 for (count = 16;
count; count--) {
721 spin_lock(&scc->zlock);
722 zs_intreg = read_zsreg(zport_a,
R3);
723 spin_unlock(&scc->zlock);
733 zs_receive_chars(zport_b);
735 zs_receive_chars(zport_a);
737 zs_status_handle(zport_b, zport_a);
739 zs_status_handle(zport_a, zport_a);
741 zs_transmit_chars(zport_b);
743 zs_transmit_chars(zport_a);
755 static int zs_startup(
struct uart_port *uport)
757 struct zs_port *zport =
to_zport(uport);
758 struct zs_scc *scc = zport->scc;
764 if (irq_guard == 1) {
778 zs_receive_drain(zport);
791 zport->regs[15] |=
BRKIE;
792 write_zsreg(zport,
R1, zport->regs[1]);
793 write_zsreg(zport,
R3, zport->regs[3]);
794 write_zsreg(zport,
R5, zport->regs[5]);
795 write_zsreg(zport,
R15, zport->regs[15]);
798 zport->mctrl = zs_raw_get_mctrl(zport);
799 zport->brk = read_zsreg(zport,
R0) &
BRK_ABRT;
801 zport->tx_stopped = 1;
803 spin_unlock_irqrestore(&scc->zlock, flags);
808 static void zs_shutdown(
struct uart_port *uport)
810 struct zs_port *zport =
to_zport(uport);
811 struct zs_scc *scc = zport->scc;
817 zport->regs[3] &= ~RxENABLE;
818 write_zsreg(zport,
R5, zport->regs[5]);
819 write_zsreg(zport,
R3, zport->regs[3]);
821 spin_unlock_irqrestore(&scc->zlock, flags);
829 static void zs_reset(
struct zs_port *zport)
831 struct zs_scc *scc = zport->scc;
837 if (!scc->initialised) {
839 read_zsreg(zport,
R0);
841 zs_line_drain(zport, irq);
844 write_zsreg(zport,
R9, 0);
845 scc->initialised = 1;
847 load_zsregs(zport, zport->regs, irq);
848 spin_unlock_irqrestore(&scc->zlock, flags);
854 struct zs_port *zport =
to_zport(uport);
855 struct zs_scc *scc = zport->scc;
856 struct zs_port *zport_a = &scc->zport[
ZS_CHAN_A];
858 unsigned int baud, brg;
869 zport->regs[3] |=
Rx5;
870 zport->regs[5] |=
Tx5;
873 zport->regs[3] |=
Rx6;
874 zport->regs[5] |=
Tx6;
877 zport->regs[3] |=
Rx7;
878 zport->regs[5] |=
Tx7;
882 zport->regs[3] |=
Rx8;
883 zport->regs[5] |=
Tx8;
890 zport->regs[4] |=
SB2;
892 zport->regs[4] |=
SB1;
897 switch (zport->clk_mode) {
908 zport->regs[4] |=
X1CLK;
915 uport->
uartclk / zport->clk_mode / 4);
918 zport->regs[12] = brg & 0xff;
919 zport->regs[13] = (brg >> 8) & 0xff;
941 zport->regs[3] &= ~RxENABLE;
943 if (zport != zport_a) {
945 zport->regs[15] |=
DCDIE;
947 zport->regs[15] &= ~
DCDIE;
949 zport->regs[15] |=
CTSIE;
951 zport->regs[15] &= ~
CTSIE;
952 zs_raw_xor_mctrl(zport);
956 load_zsregs(zport, zport->regs, irq);
958 spin_unlock_irqrestore(&scc->zlock, flags);
967 unsigned int oldstate)
969 struct zs_port *zport =
to_zport(uport);
974 zport->regs[5] &= ~
TxENAB;
975 write_zsreg(zport,
R5, zport->regs[5]);
979 static const char *zs_type(
struct uart_port *uport)
984 static void zs_release_port(
struct uart_port *uport)
991 static int zs_map_port(
struct uart_port *uport)
1003 static int zs_request_port(
struct uart_port *uport)
1011 ret = zs_map_port(uport);
1019 static void zs_config_port(
struct uart_port *uport,
int flags)
1021 struct zs_port *zport =
to_zport(uport);
1024 if (zs_request_port(uport))
1035 struct zs_port *zport =
to_zport(uport);
1040 if (ser->
irq != uport->
irq)
1049 .tx_empty = zs_tx_empty,
1050 .set_mctrl = zs_set_mctrl,
1051 .get_mctrl = zs_get_mctrl,
1052 .stop_tx = zs_stop_tx,
1053 .start_tx = zs_start_tx,
1054 .stop_rx = zs_stop_rx,
1055 .enable_ms = zs_enable_ms,
1056 .break_ctl = zs_break_ctl,
1057 .startup = zs_startup,
1058 .shutdown = zs_shutdown,
1059 .set_termios = zs_set_termios,
1062 .release_port = zs_release_port,
1063 .request_port = zs_request_port,
1064 .config_port = zs_config_port,
1065 .verify_port = zs_verify_port,
1071 static int __init zs_probe_sccs(
void)
1099 for (chip = 0; chip < n_chips; chip++) {
1102 struct zs_port *zport = &zs_sccs[
chip].zport[
side];
1105 zport->scc = &zs_sccs[
chip];
1106 zport->clk_mode = 16;
1113 uport->
ops = &zs_ops;
1114 uport->
line = chip * ZS_NUM_CHAN +
side;
1119 for (i = 0; i < ZS_NUM_REGS; i++)
1120 zport->regs[i] = zs_init_regs[i];
1128 #ifdef CONFIG_SERIAL_ZS_CONSOLE
1129 static void zs_console_putchar(
struct uart_port *uport,
int ch)
1131 struct zs_port *zport =
to_zport(uport);
1132 struct zs_scc *scc = zport->scc;
1134 unsigned long flags;
1138 if (zs_transmit_drain(zport, irq))
1139 write_zsdata(zport, ch);
1140 spin_unlock_irqrestore(&scc->zlock, flags);
1147 static void zs_console_write(
struct console *co,
const char *
s,
1151 struct zs_port *zport = &zs_sccs[
chip].zport[
side];
1152 struct zs_scc *scc = zport->scc;
1153 unsigned long flags;
1159 txint = zport->regs[1];
1160 txenb = zport->regs[5];
1162 zport->regs[1] = txint & ~TxINT_ENAB;
1163 write_zsreg(zport,
R1, zport->regs[1]);
1166 zport->regs[5] = txenb |
TxENAB;
1167 write_zsreg(zport,
R5, zport->regs[5]);
1169 spin_unlock_irqrestore(&scc->zlock, flags);
1176 zs_line_drain(zport, irq);
1177 if (!(txenb & TxENAB)) {
1178 zport->regs[5] &= ~TxENAB;
1179 write_zsreg(zport,
R5, zport->regs[5]);
1181 if (txint & TxINT_ENAB) {
1183 write_zsreg(zport,
R1, zport->regs[1]);
1185 spin_unlock_irqrestore(&scc->zlock, flags);
1197 struct zs_port *zport = &zs_sccs[
chip].zport[
side];
1205 ret = zs_map_port(uport);
1210 zs_pm(uport, 0, -1);
1218 static struct console zs_console = {
1220 .write = zs_console_write,
1222 .setup = zs_console_setup,
1231 static int __init zs_serial_console_init(
void)
1235 ret = zs_probe_sccs();
1245 #define SERIAL_ZS_CONSOLE &zs_console
1247 #define SERIAL_ZS_CONSOLE NULL
1252 .driver_name =
"serial",
1261 static int __init zs_init(
void)
1265 pr_info(
"%s%s\n", zs_name, zs_version);
1268 ret = zs_probe_sccs();
1278 struct zs_port *zport = &scc->zport[i %
ZS_NUM_CHAN];
1288 static void __exit zs_exit(
void)
1292 for (i =
ZS_NUM_SCCS * ZS_NUM_CHAN - 1; i >= 0; i--) {
1294 struct zs_port *zport = &scc->zport[i %
ZS_NUM_CHAN];