1 /* $NetBSD: zs.c,v 1.23 2008/04/28 20:23:28 martin Exp $ */
4 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross and Wayne Knowles
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.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.23 2008/04/28 20:23:28 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 <machine/mainboard.h>
60 #include <machine/autoconf.h>
61 #include <machine/prom.h>
62 #include <machine/z8530var.h>
65 #include <dev/ic/z8530reg.h>
68 #include "zsc.h" /* NZSC */
71 /* Make life easier for the initialized arrays here. */
78 * Some warts needed by z8530tty.c -
79 * The default parity REALLY needs to be the same as the PROM uses,
80 * or you can not see messages done with printf during boot-up...
82 int zs_def_cflag
= (CREAD
| CS8
| HUPCL
);
85 #define PCLK 10000000 /* PCLK pin input clock rate */
88 #define ZS_DEFSPEED 9600
92 * Define interrupt levels.
96 /* Register recovery time is 3.5 to 4 PCLK Cycles */
97 #define ZS_RECOVERY 1 /* 1us = 10 PCLK Cycles */
98 #define ZS_DELAY() delay(ZS_RECOVERY)
100 /* The layout of this is hardware-dependent (padding, order). */
103 volatile uint8_t zc_csr
; /* ctrl,status, and indirect access */
105 volatile uint8_t zc_data
; /* data */
108 /* Yes, they are backwards. */
109 struct zschan zs_chan_b
;
110 struct zschan zs_chan_a
;
113 /* Return the byte offset of element within a structure */
114 #define OFFSET(struct_def, el) ((size_t)&((struct_def *)0)->el)
116 #define ZS_CHAN_A OFFSET(struct zsdevice, zs_chan_a)
117 #define ZS_CHAN_B OFFSET(struct zsdevice, zs_chan_b)
118 #define ZS_REG_CSR OFFSET(struct zschan, zc_csr)
119 #define ZS_REG_DATA OFFSET(struct zschan, zc_data)
120 static int zs_chan_offset
[] = {ZS_CHAN_A
, ZS_CHAN_B
};
122 /* Flags from cninit() */
123 static int zs_hwflags
[NZS
][2];
125 /* Default speed for all channels */
126 static int zs_defspeed
= ZS_DEFSPEED
;
127 static volatile int zssoftpending
;
129 static uint8_t zs_init_reg
[16] = {
130 0, /* 0: CMD (reset, etc.) */
131 0, /* 1: No interrupts yet. */
132 ZSHARD_PRI
, /* 2: IVECT */
133 ZSWR3_RX_8
| ZSWR3_RX_ENABLE
,
134 ZSWR4_CLK_X16
| ZSWR4_ONESB
,
135 ZSWR5_TX_8
| ZSWR5_TX_ENABLE
,
136 0, /* 6: TXSYNC/SYNCLO */
137 0, /* 7: RXSYNC/SYNCHI */
138 0, /* 8: alias for data port */
140 0, /*10: Misc. TX/RX control bits */
141 ZSWR11_TXCLK_BAUD
| ZSWR11_RXCLK_BAUD
| ZSWR11_TRXC_OUT_ENA
,
142 BPS_TO_TCONST(PCLK
/16, ZS_DEFSPEED
), /*12: BAUDLO (default=9600) */
143 0, /*13: BAUDHI (default=9600) */
144 ZSWR14_BAUD_ENA
| ZSWR14_BAUD_FROM_PCLK
,
149 /****************************************************************
151 ****************************************************************/
153 /* Definition of the driver for autoconfig. */
154 static int zs_match(device_t
, cfdata_t
, void *);
155 static void zs_attach(device_t
, device_t
, void *);
156 static int zs_print(void *, const char *name
);
158 CFATTACH_DECL_NEW(zsc
, sizeof(struct zsc_softc
),
159 zs_match
, zs_attach
, NULL
, NULL
);
161 static int zshard(void *);
163 static int zs_get_speed(struct zs_chanstate
*);
164 struct zschan
*zs_get_chan_addr(int zs_unit
, int channel
);
166 void zs_putc(void *, int);
169 * Is the zs chip present?
172 zs_match(device_t parent
, cfdata_t cf
, void *aux
)
174 struct confargs
*ca
= aux
;
177 if (strcmp(ca
->ca_name
, "zsc"))
180 va
= (void *)cf
->cf_addr
;
182 /* This returns -1 on a fault (bus error). */
191 * Match slave number to zs unit number, so that misconfiguration will
192 * not set up the keyboard as ttya, etc.
195 zs_attach(device_t parent
, device_t self
, void *aux
)
197 struct zsc_softc
*zsc
= device_private(self
);
198 struct confargs
*ca
= aux
;
199 struct zsc_attach_args zsc_args
;
200 struct zs_chanstate
*cs
;
201 struct zs_channel
*ch
;
202 int zs_unit
, channel
, s
;
205 zsc
->zsc_bustag
= ca
->ca_bustag
;
206 if (bus_space_map(ca
->ca_bustag
, ca
->ca_addr
,
207 sizeof(struct zsdevice
),
208 BUS_SPACE_MAP_LINEAR
,
209 &zsc
->zsc_base
) != 0) {
210 aprint_error(": cannot map registers\n");
214 zs_unit
= device_unit(self
);
218 * Initialize software state for each channel.
220 for (channel
= 0; channel
< 2; channel
++) {
221 zsc_args
.channel
= channel
;
222 zsc_args
.hwflags
= zs_hwflags
[zs_unit
][channel
];
223 ch
= &zsc
->zsc_cs_store
[channel
];
224 cs
= zsc
->zsc_cs
[channel
] = (struct zs_chanstate
*)ch
;
227 cs
->cs_reg_csr
= NULL
;
228 cs
->cs_reg_data
= NULL
;
229 cs
->cs_channel
= channel
;
230 cs
->cs_private
= NULL
;
231 cs
->cs_ops
= &zsops_null
;
232 cs
->cs_brg_clk
= PCLK
/ 16;
234 if (bus_space_subregion(ca
->ca_bustag
, zsc
->zsc_base
,
235 zs_chan_offset
[channel
],
236 sizeof(struct zschan
),
237 &ch
->cs_regs
) != 0) {
238 aprint_error_dev(self
, ": cannot map regs\n");
241 ch
->cs_bustag
= ca
->ca_bustag
;
243 memcpy(cs
->cs_creg
, zs_init_reg
, 16);
244 memcpy(cs
->cs_preg
, zs_init_reg
, 16);
246 if (zsc_args
.hwflags
& ZS_HWFLAG_CONSOLE
)
247 cs
->cs_defspeed
= zs_get_speed(cs
);
249 cs
->cs_defspeed
= zs_defspeed
;
250 cs
->cs_defcflag
= zs_def_cflag
;
252 /* Make these correspond to cs_defcflag (-crtscts) */
253 cs
->cs_rr0_dcd
= ZSRR0_DCD
;
255 cs
->cs_wr5_dtr
= ZSWR5_DTR
| ZSWR5_RTS
;
259 * Clear the master interrupt enable.
260 * The INTENA is common to both channels,
261 * so just do it on the A channel.
264 zs_write_reg(cs
, 9, 0);
267 * Look for a child driver for this channel.
268 * The child attach will setup the hardware.
270 if (!config_found(self
, (void *)&zsc_args
, zs_print
)) {
271 /* No sub-driver. Just reset it. */
272 uint8_t reset
= (channel
== 0) ?
273 ZSWR9_A_RESET
: ZSWR9_B_RESET
;
276 zs_write_reg(cs
, 9, reset
);
282 zsc
->sc_si
= softint_establish(SOFTINT_SERIAL
, zssoft
, zsc
);
283 bus_intr_establish(zsc
->zsc_bustag
, SYS_INTR_SCC0
, 0, 0, zshard
, NULL
);
285 evcnt_attach_dynamic(&zsc
->zs_intrcnt
, EVCNT_TYPE_INTR
, NULL
,
286 device_xname(self
), "intr");
289 * Set the master interrupt enable and interrupt vector.
290 * (common to both channels, do it on A)
294 /* interrupt vector */
295 zs_write_reg(cs
, 2, zs_init_reg
[2]);
296 /* master interrupt control (enable) */
297 zs_write_reg(cs
, 9, zs_init_reg
[9]);
302 zs_print(void *aux
, const char *name
)
304 struct zsc_attach_args
*args
= aux
;
307 aprint_normal("%s: ", name
);
309 if (args
->channel
!= -1)
310 aprint_normal(" channel %d", args
->channel
);
316 * Our ZS chips all share a common, autovectored interrupt,
317 * so we have to look at all of them on each interrupt.
322 struct zsc_softc
*zsc
;
323 int unit
, rval
, softreq
;
326 for (unit
= 0; unit
< zsc_cd
.cd_ndevs
; unit
++) {
327 zsc
= device_lookup_private(&zsc_cd
, unit
);
330 rval
|= zsc_intr_hard(zsc
);
331 softreq
= zsc
->zsc_cs
[0]->cs_softreq
;
332 softreq
|= zsc
->zsc_cs
[1]->cs_softreq
;
333 if (softreq
&& (zssoftpending
== 0)) {
335 softint_schedule(zsc
->sc_si
);
337 zsc
->zs_intrcnt
.ev_count
++;
343 * Similar scheme as for zshard (look at all of them)
348 struct zsc_softc
*zsc
;
351 /* This is not the only ISR on this IPL. */
352 if (zssoftpending
== 0)
356 * The soft intr. bit will be set by zshard only if
357 * the variable zssoftpending is zero. The order of
358 * these next two statements prevents our clearing
359 * the soft intr bit just after zshard has set it.
361 /*isr_soft_clear(ZSSOFT_PRI);*/
364 /* Make sure we call the tty layer at spltty. */
366 for (unit
= 0; unit
< zsc_cd
.cd_ndevs
; unit
++) {
367 zsc
= device_lookup_private(&zsc_cd
, unit
);
370 (void)zsc_intr_soft(zsc
);
378 * Compute the current baud rate given a ZS channel.
381 zs_get_speed(struct zs_chanstate
*cs
)
385 tconst
= zs_read_reg(cs
, 12);
386 tconst
|= zs_read_reg(cs
, 13) << 8;
387 return (TCONST_TO_BPS(cs
->cs_brg_clk
, tconst
));
391 * MD functions for setting the baud rate and control modes.
394 zs_set_speed(struct zs_chanstate
*cs
, int bps
)
396 int tconst
, real_bps
;
399 while (!(zs_read_csr(cs
) & ZSRR0_TX_READY
))
402 /* Wait for transmit buffer to empty */
408 if (cs
->cs_brg_clk
== 0)
409 panic("zs_set_speed");
412 tconst
= BPS_TO_TCONST(cs
->cs_brg_clk
, bps
);
416 /* Convert back to make sure we can do it. */
417 real_bps
= TCONST_TO_BPS(cs
->cs_brg_clk
, tconst
);
419 /* XXX - Allow some tolerance here? */
425 cs
->cs_preg
[12] = tconst
;
426 cs
->cs_preg
[13] = tconst
>> 8;
428 /* Caller will stuff the pending registers. */
433 zs_set_modes(struct zs_chanstate
*cs
, int cflag
)
438 * Output hardware flow control on the chip is horrendous:
439 * if carrier detect drops, the receiver is disabled, and if
440 * CTS drops, the transmitter is stoped IN MID CHARACTER!
441 * Therefore, NEVER set the HFC bit, and instead use the
442 * status interrupt to detect CTS changes.
446 if ((cflag
& (CLOCAL
| MDMBUF
)) != 0) {
448 if ((cflag
& MDMBUF
) == 0)
449 cs
->cs_rr0_pps
= ZSRR0_DCD
;
451 cs
->cs_rr0_dcd
= ZSRR0_DCD
;
452 if ((cflag
& CRTSCTS
) != 0) {
453 cs
->cs_wr5_dtr
= ZSWR5_DTR
;
454 cs
->cs_wr5_rts
= ZSWR5_RTS
;
455 cs
->cs_rr0_cts
= ZSRR0_CTS
;
456 } else if ((cflag
& MDMBUF
) != 0) {
458 cs
->cs_wr5_rts
= ZSWR5_DTR
;
459 cs
->cs_rr0_cts
= ZSRR0_DCD
;
461 cs
->cs_wr5_dtr
= ZSWR5_DTR
| ZSWR5_RTS
;
467 /* Caller will stuff the pending registers. */
473 * Read or write the chip with suitable delays.
477 zs_read_reg(struct zs_chanstate
*cs
, uint8_t reg
)
480 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
482 bus_space_write_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
, reg
);
484 val
= bus_space_read_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
);
490 zs_write_reg(struct zs_chanstate
*cs
, uint8_t reg
, uint8_t val
)
492 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
494 bus_space_write_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
, reg
);
496 bus_space_write_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
, val
);
501 zs_read_csr(struct zs_chanstate
*cs
)
503 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
506 val
= bus_space_read_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
);
512 zs_write_csr(struct zs_chanstate
*cs
, uint8_t val
)
514 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
516 bus_space_write_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_CSR
, val
);
521 zs_read_data(struct zs_chanstate
*cs
)
523 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
526 val
= bus_space_read_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_DATA
);
532 zs_write_data(struct zs_chanstate
*cs
, uint8_t val
)
534 struct zs_channel
*zsc
= (struct zs_channel
*)cs
;
536 bus_space_write_1(zsc
->cs_bustag
, zsc
->cs_regs
, ZS_REG_DATA
, val
);
541 zs_abort(struct zs_chanstate
*cs
)
552 /*********************************************************/
553 /* Polled character I/O functions for console and KGDB */
554 /*********************************************************/
557 zs_get_chan_addr(int zs_unit
, int channel
)
559 struct zsdevice
*addr
;
565 addr
= (struct zsdevice
*) ZS0_ADDR
;
568 zc
= &addr
->zs_chan_a
;
570 zc
= &addr
->zs_chan_b
;
578 volatile struct zschan
*zc
= arg
;
583 /* Wait for a character to arrive. */
587 } while ((rr0
& ZSRR0_RX_READY
) == 0);
597 * Polled output char.
600 zs_putc(void *arg
, int c
)
602 volatile struct zschan
*zc
= arg
;
607 /* Wait for transmitter to become ready. */
611 } while ((rr0
& ZSRR0_TX_READY
) == 0);
619 /***************************************************************/
621 static void zscnprobe(struct consdev
*);
622 static void zscninit(struct consdev
*);
623 static int zscngetc(dev_t
);
624 static void zscnputc(dev_t
, int);
625 static void zscnpollc(dev_t
, int);
627 static int cons_port
;
629 struct consdev consdev_zs
= {
638 zscnprobe(struct consdev
*cn
)
643 zscninit(struct consdev
*cn
)
645 extern const struct cdevsw zstty_cdevsw
;
647 cons_port
= prom_getconsole();
648 cn
->cn_dev
= makedev(cdevsw_lookup_major(&zstty_cdevsw
), cons_port
);
649 cn
->cn_pri
= CN_REMOTE
;
650 zs_hwflags
[0][cons_port
] = ZS_HWFLAG_CONSOLE
;
658 zs
= zs_get_chan_addr(0, cons_port
);
663 zscnputc(dev_t dev
, int c
)
667 zs
= zs_get_chan_addr(0, cons_port
);
672 zscnpollc(dev_t dev
, int on
)