1 /* $NetBSD: zs.c,v 1.83 2008/04/28 20:23:38 martin Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Zilog Z8530 Dual UART driver (machine-dependent part)
35 * Runs two serial lines per chip using slave drivers.
36 * Plain tty/async lines use the zs_async slave.
37 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.83 2008/04/28 20:23:38 martin Exp $");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #include <sys/kernel.h>
55 #include <sys/syslog.h>
59 #include <uvm/uvm_extern.h>
61 #include <machine/autoconf.h>
62 #include <machine/mon.h>
63 #include <machine/z8530var.h>
65 #include <sun3/sun3/machdep.h>
67 #include <sun3/sun3x/obio.h>
69 #include <sun3/sun3/obio.h>
71 #include <sun3/dev/zs_cons.h>
74 #include <dev/ic/z8530reg.h>
77 #include "kbd.h" /* NKBD */
78 #include "zsc.h" /* NZSC */
81 /* Make life easier for the initialized arrays here. */
88 * Some warts needed by z8530tty.c -
89 * The default parity REALLY needs to be the same as the PROM uses,
90 * or you can not see messages done with printf during boot-up...
92 int zs_def_cflag
= (CREAD
| CS8
| HUPCL
);
95 * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
97 #define PCLK (9600 * 512) /* PCLK pin input clock rate */
100 * Define interrupt levels.
102 #define ZSHARD_PRI 6 /* Wired on the CPU board... */
103 #define ZSSOFT_PRI _IPL_SOFT_LEVEL3 /* Want tty pri (4) but this is OK. */
105 #define ZS_DELAY() delay(2)
107 /* The layout of this is hardware-dependent (padding, order). */
109 volatile uint8_t zc_csr
; /* ctrl,status, and indirect access */
111 volatile uint8_t zc_data
; /* data */
115 /* Yes, they are backwards. */
116 struct zschan zs_chan_b
;
117 struct zschan zs_chan_a
;
121 /* Default OBIO addresses. */
122 static int zs_physaddr
[NZS
] = {
126 /* Saved PROM mappings */
127 static struct zsdevice
*zsaddr
[NZS
];
129 /* Flags from cninit() */
130 static int zs_hwflags
[NZS
][2];
132 /* Default speed for each channel */
133 static int zs_defspeed
[NZS
][2] = {
134 { 1200, /* keyboard */
140 static uint8_t zs_init_reg
[16] = {
141 0, /* 0: CMD (reset, etc.) */
142 0, /* 1: No interrupts yet. */
143 0x18 + ZSHARD_PRI
, /* IVECT */
144 ZSWR3_RX_8
| ZSWR3_RX_ENABLE
,
145 ZSWR4_CLK_X16
| ZSWR4_ONESB
| ZSWR4_EVENP
,
146 ZSWR5_TX_8
| ZSWR5_TX_ENABLE
,
147 0, /* 6: TXSYNC/SYNCLO */
148 0, /* 7: RXSYNC/SYNCHI */
149 0, /* 8: alias for data port */
151 0, /*10: Misc. TX/RX control bits */
152 ZSWR11_TXCLK_BAUD
| ZSWR11_RXCLK_BAUD
,
153 ((PCLK
/32)/9600)-2, /*12: BAUDLO (default=9600) */
154 0, /*13: BAUDHI (default=9600) */
155 ZSWR14_BAUD_ENA
| ZSWR14_BAUD_FROM_PCLK
,
160 /* Find PROM mappings (for console support). */
167 for (i
= 0; i
< NZS
; i
++) {
168 if (find_prom_map(zs_physaddr
[i
], PMAP_OBIO
,
169 sizeof(struct zschan
), &va
) == 0)
170 zsaddr
[i
] = (void *)va
;
175 zs_get_chan_addr(int zs_unit
, int channel
)
177 struct zsdevice
*addr
;
182 addr
= zsaddr
[zs_unit
];
186 zc
= &addr
->zs_chan_a
;
188 zc
= &addr
->zs_chan_b
;
194 /****************************************************************
196 ****************************************************************/
198 /* Definition of the driver for autoconfig. */
199 static int zs_match(device_t
, cfdata_t
, void *);
200 static void zs_attach(device_t
, device_t
, void *);
201 static int zs_print(void *, const char *);
203 CFATTACH_DECL_NEW(zsc
, sizeof(struct zsc_softc
),
204 zs_match
, zs_attach
, NULL
, NULL
);
206 static int zshard(void *);
207 static int zs_get_speed(struct zs_chanstate
*);
211 * Is the zs chip present?
214 zs_match(device_t parent
, cfdata_t cf
, void *aux
)
216 struct confargs
*ca
= aux
;
221 * This driver only supports its wired-in mappings,
222 * because the console support depends on those.
224 if (ca
->ca_paddr
== zs_physaddr
[0]) {
226 } else if (ca
->ca_paddr
== zs_physaddr
[1]) {
232 /* Make sure zs_init() found mappings. */
237 /* This returns -1 on a fault (bus error). */
238 if (peek_byte(va
) == -1)
241 /* Default interrupt priority (always splbio==2) */
242 if (ca
->ca_intpri
== -1)
243 ca
->ca_intpri
= ZSHARD_PRI
;
251 * Match slave number to zs unit number, so that misconfiguration will
252 * not set up the keyboard as ttya, etc.
255 zs_attach(device_t parent
, device_t self
, void *aux
)
257 struct zsc_softc
*zsc
= device_private(self
);
258 struct confargs
*ca
= aux
;
259 struct zsc_attach_args zsc_args
;
260 volatile struct zschan
*zc
;
261 struct zs_chanstate
*cs
;
262 int s
, zs_unit
, channel
;
266 zs_unit
= device_unit(self
);
268 aprint_normal(": (softpri %d)\n", ZSSOFT_PRI
);
270 /* Use the mapping setup by the Sun PROM. */
271 if (zsaddr
[zs_unit
] == NULL
)
272 panic("zs_attach: zs%d not mapped", zs_unit
);
275 * Initialize software state for each channel.
277 for (channel
= 0; channel
< 2; channel
++) {
278 zsc_args
.channel
= channel
;
279 zsc_args
.hwflags
= zs_hwflags
[zs_unit
][channel
];
280 cs
= &zsc
->zsc_cs_store
[channel
];
281 zsc
->zsc_cs
[channel
] = cs
;
284 cs
->cs_channel
= channel
;
285 cs
->cs_private
= NULL
;
286 cs
->cs_ops
= &zsops_null
;
287 cs
->cs_brg_clk
= PCLK
/ 16;
289 zc
= zs_get_chan_addr(zs_unit
, channel
);
290 cs
->cs_reg_csr
= &zc
->zc_csr
;
291 cs
->cs_reg_data
= &zc
->zc_data
;
293 memcpy(cs
->cs_creg
, zs_init_reg
, 16);
294 memcpy(cs
->cs_preg
, zs_init_reg
, 16);
296 /* XXX: Get these from the EEPROM instead? */
297 /* XXX: See the mvme167 code. Better. */
298 if (zsc_args
.hwflags
& ZS_HWFLAG_CONSOLE
)
299 cs
->cs_defspeed
= zs_get_speed(cs
);
301 cs
->cs_defspeed
= zs_defspeed
[zs_unit
][channel
];
302 cs
->cs_defcflag
= zs_def_cflag
;
304 /* Make these correspond to cs_defcflag (-crtscts) */
305 cs
->cs_rr0_dcd
= ZSRR0_DCD
;
307 cs
->cs_wr5_dtr
= ZSWR5_DTR
| ZSWR5_RTS
;
311 * Clear the master interrupt enable.
312 * The INTENA is common to both channels,
313 * so just do it on the A channel.
316 zs_write_reg(cs
, 9, 0);
320 * Look for a child driver for this channel.
321 * The child attach will setup the hardware.
323 if (!config_found(self
, (void *)&zsc_args
, zs_print
)) {
324 /* No sub-driver. Just reset it. */
325 uint8_t reset
= (channel
== 0) ?
326 ZSWR9_A_RESET
: ZSWR9_B_RESET
;
328 zs_write_reg(cs
, 9, reset
);
334 * Now safe to install interrupt handlers. Note the arguments
335 * to the interrupt handlers aren't used. Note, we only do this
336 * once since both SCCs interrupt at the same level and vector.
340 isr_add_autovect(zshard
, NULL
, ca
->ca_intpri
);
342 zsc
->zs_si
= softint_establish(SOFTINT_SERIAL
,
343 (void (*)(void *))zsc_intr_soft
, zsc
);
344 /* XXX; evcnt_attach() ? */
347 * Set the master interrupt enable and interrupt vector.
348 * (common to both channels, do it on A)
352 /* interrupt vector */
353 zs_write_reg(cs
, 2, zs_init_reg
[2]);
354 /* master interrupt control (enable) */
355 zs_write_reg(cs
, 9, zs_init_reg
[9]);
359 * XXX: L1A hack - We would like to be able to break into
360 * the debugger during the rest of autoconfiguration, so
361 * lower interrupts just enough to let zs interrupts in.
362 * This is done after both zs devices are attached.
365 (void)spl5(); /* splzs - 1 */
370 zs_print(void *aux
, const char *name
)
372 struct zsc_attach_args
*args
= aux
;
375 aprint_normal("%s: ", name
);
377 if (args
->channel
!= -1)
378 aprint_normal(" channel %d", args
->channel
);
384 * Our ZS chips all share a common, autovectored interrupt,
385 * so we have to look at all of them on each interrupt.
390 struct zsc_softc
*zsc
;
391 int unit
, rval
, softreq
;
394 for (unit
= 0; unit
< zsc_cd
.cd_ndevs
; unit
++) {
395 zsc
= device_lookup_private(&zsc_cd
, unit
);
398 rval
|= zsc_intr_hard(zsc
);
399 softreq
= zsc
->zsc_cs
[0]->cs_softreq
;
400 softreq
|= zsc
->zsc_cs
[1]->cs_softreq
;
402 softint_schedule(zsc
->zs_si
);
409 * Compute the current baud rate given a ZS channel.
412 zs_get_speed(struct zs_chanstate
*cs
)
416 tconst
= zs_read_reg(cs
, 12);
417 tconst
|= zs_read_reg(cs
, 13) << 8;
418 return (TCONST_TO_BPS(cs
->cs_brg_clk
, tconst
));
422 * MD functions for setting the baud rate and control modes.
425 zs_set_speed(struct zs_chanstate
*cs
, int bps
)
427 int tconst
, real_bps
;
433 if (cs
->cs_brg_clk
== 0)
434 panic("zs_set_speed");
437 tconst
= BPS_TO_TCONST(cs
->cs_brg_clk
, bps
);
441 /* Convert back to make sure we can do it. */
442 real_bps
= TCONST_TO_BPS(cs
->cs_brg_clk
, tconst
);
444 /* XXX - Allow some tolerance here? */
448 cs
->cs_preg
[12] = tconst
;
449 cs
->cs_preg
[13] = tconst
>> 8;
451 /* Caller will stuff the pending registers. */
456 zs_set_modes(struct zs_chanstate
*cs
, int cflag
/* bits per second */)
461 * Output hardware flow control on the chip is horrendous:
462 * if carrier detect drops, the receiver is disabled, and if
463 * CTS drops, the transmitter is stoped IN MID CHARACTER!
464 * Therefore, NEVER set the HFC bit, and instead use the
465 * status interrupt to detect CTS changes.
469 if ((cflag
& (CLOCAL
| MDMBUF
)) != 0) {
471 if ((cflag
& MDMBUF
) == 0)
472 cs
->cs_rr0_pps
= ZSRR0_DCD
;
474 cs
->cs_rr0_dcd
= ZSRR0_DCD
;
475 if ((cflag
& CRTSCTS
) != 0) {
476 cs
->cs_wr5_dtr
= ZSWR5_DTR
;
477 cs
->cs_wr5_rts
= ZSWR5_RTS
;
478 cs
->cs_rr0_cts
= ZSRR0_CTS
;
479 } else if ((cflag
& MDMBUF
) != 0) {
481 cs
->cs_wr5_rts
= ZSWR5_DTR
;
482 cs
->cs_rr0_cts
= ZSRR0_DCD
;
484 cs
->cs_wr5_dtr
= ZSWR5_DTR
| ZSWR5_RTS
;
490 /* Caller will stuff the pending registers. */
496 * Read or write the chip with suitable delays.
500 zs_read_reg(struct zs_chanstate
*cs
, uint8_t reg
)
504 *cs
->cs_reg_csr
= reg
;
506 val
= *cs
->cs_reg_csr
;
512 zs_write_reg(struct zs_chanstate
*cs
, uint8_t reg
, uint8_t val
)
514 *cs
->cs_reg_csr
= reg
;
516 *cs
->cs_reg_csr
= val
;
521 zs_read_csr(struct zs_chanstate
*cs
)
525 val
= *cs
->cs_reg_csr
;
531 zs_write_csr(struct zs_chanstate
*cs
, uint8_t val
)
533 *cs
->cs_reg_csr
= val
;
538 zs_read_data(struct zs_chanstate
*cs
)
542 val
= *cs
->cs_reg_data
;
548 zs_write_data(struct zs_chanstate
*cs
, uint8_t val
)
550 *cs
->cs_reg_data
= val
;
554 /****************************************************************
555 * Console support functions (Sun3 specific!)
556 * Note: this code is allowed to know about the layout of
557 * the chip registers, and uses that to keep things simple.
558 * XXX - I think I like the mvme167 code better. -gwr
559 ****************************************************************/
564 * Handle user request to enter kernel debugger.
567 zs_abort(struct zs_chanstate
*cs
)
569 volatile struct zschan
*zc
= zs_conschan
;
572 /* Wait for end of break to avoid PROM abort. */
573 /* XXX - Limit the wait? */
577 } while (rr0
& ZSRR0_BREAK
);
579 /* This is always available on the Sun3. */
589 volatile struct zschan
*zc
= arg
;
593 /* Wait for a character to arrive. */
597 } while ((rr0
& ZSRR0_RX_READY
) == 0);
604 * This is used by the kd driver to read scan codes,
605 * so don't translate '\r' ==> '\n' here...
611 * Polled output char.
614 zs_putc(void *arg
, int c
)
616 volatile struct zschan
*zc
= arg
;
620 /* Wait for transmitter to become ready. */
624 } while ((rr0
& ZSRR0_TX_READY
) == 0);
631 /*****************************************************************/
633 static void zscninit(struct consdev
*);
634 static int zscngetc(dev_t
);
635 static void zscnputc(dev_t
, int);
638 * Console table shared by ttya, ttyb
640 struct consdev consdev_tty
= {
650 zscninit(struct consdev
*cn
)
655 * Polled console input putchar.
660 return (zs_getc(zs_conschan
));
664 * Polled console output putchar.
667 zscnputc(dev_t dev
, int c
)
669 zs_putc(zs_conschan
, c
);
672 /*****************************************************************/
674 static void prom_cninit(struct consdev
*);
675 static int prom_cngetc(dev_t
);
676 static void prom_cnputc(dev_t
, int);
679 * The console is set to this one initially,
680 * which lets us use the PROM until consinit()
681 * is called to select a real console.
683 struct consdev consdev_prom
= {
692 * The console table pointer is statically initialized
693 * to point to the PROM (output only) table, so that
694 * early calls to printf will work.
696 struct consdev
*cn_tab
= &consdev_prom
;
699 nullcnprobe(struct consdev
*cn
)
704 prom_cninit(struct consdev
*cn
)
709 * PROM console input putchar.
710 * (dummy - this is output only)
713 prom_cngetc(dev_t dev
)
719 * PROM console output putchar.
722 prom_cnputc(dev_t dev
, int c
)
724 (*romVectorPtr
->putChar
)(c
& 0x7f);
727 /*****************************************************************/
729 extern struct consdev consdev_kd
;
732 int zs_unit
, channel
;
733 } zstty_conf
[NZS
*2] = {
734 /* XXX: knowledge from the config file here... */
741 static const char *prom_inSrc_name
[] = {
747 * This function replaces sys/dev/cninit.c
748 * Determine which device is the console using
749 * the PROM "input source" and "output sink".
757 int channel
, zs_unit
, zstty_unit
;
758 uint8_t inSource
, outSink
;
759 extern const struct cdevsw zstty_cdevsw
;
761 /* Get the zs driver ready for console duty. */
765 inSource
= *v
->inSource
;
766 outSink
= *v
->outSink
;
767 if (inSource
!= outSink
) {
768 mon_printf("cninit: mismatched PROM output selector\n");
773 mon_printf("cninit: invalid inSource=%d\n", inSource
);
778 case 0: /* keyboard/display */
783 /* Set cn_dev, cn_pri in kd.c */
786 mon_printf("cninit: kdb/display not configured\n");
794 case 3: /* ttyc (rewired keyboard connector) */
795 case 4: /* ttyd (rewired mouse connector) */
796 zstty_unit
= inSource
- 1;
797 zs_unit
= zstty_conf
[zstty_unit
].zs_unit
;
798 channel
= zstty_conf
[zstty_unit
].channel
;
800 cn
->cn_dev
= makedev(cdevsw_lookup_major(&zstty_cdevsw
),
802 cn
->cn_pri
= CN_REMOTE
;
806 /* Now that inSource has been validated, print it. */
807 mon_printf("console is %s\n", prom_inSrc_name
[inSource
]);
809 zc
= zs_get_chan_addr(zs_unit
, channel
);
811 mon_printf("cninit: zs not mapped.\n");
815 zs_hwflags
[zs_unit
][channel
] = ZS_HWFLAG_CONSOLE
;