1 /* $NetBSD: zs_hb.c,v 1.24 2008/03/29 19:15:35 tsutsui 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_hb.c,v 1.24 2008/03/29 19:15:35 tsutsui Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
51 #include <machine/adrsmap.h>
52 #include <machine/z8530var.h>
55 #include <dev/ic/z8530reg.h>
57 #include <newsmips/dev/hbvar.h>
59 #include "zsc.h" /* NZSC */
62 /* Make life easier for the initialized arrays here. */
68 #define ZSCFLAG_EX 0x01 /* expansion board */
71 * The news3400 provides a 4.9152 MHz clock to the ZS chips.
73 #define PCLK (9600 * 512) /* PCLK pin input clock rate */
74 #define PCLK_EX (9600 * 384)
77 * Define interrupt levels.
81 #define ZS_DELAY() {(void)*(volatile char *)INTEN1; delay(2);}
83 /* The layout of this is hardware-dependent (padding, order). */
85 volatile uint8_t zc_csr
; /* ctrl,status, and indirect access */
86 volatile uint8_t zc_data
; /* data */
89 /* Yes, they are backwards. */
90 struct zschan zs_chan_b
;
91 struct zschan zs_chan_a
;
94 extern int zs_def_cflag
;
96 static struct zsdevice
*zsaddr
[NZS
];
98 /* Flags from cninit() */
99 static int zs_hwflags
[NZS
][2];
101 /* Default speed for all channels */
102 static int zs_defspeed
= 9600;
104 static uint8_t zs_init_reg
[16] = {
105 0, /* 0: CMD (reset, etc.) */
106 0, /* 1: No interrupts yet. */
107 ZSHARD_PRI
, /* IVECT */
108 ZSWR3_RX_8
| ZSWR3_RX_ENABLE
,
109 ZSWR4_CLK_X16
| ZSWR4_ONESB
| ZSWR4_EVENP
,
110 ZSWR5_TX_8
| ZSWR5_TX_ENABLE
,
111 0, /* 6: TXSYNC/SYNCLO */
112 0, /* 7: RXSYNC/SYNCHI */
113 0, /* 8: alias for data port */
115 0, /*10: Misc. TX/RX control bits */
116 ZSWR11_TXCLK_BAUD
| ZSWR11_RXCLK_BAUD
,
117 ((PCLK
/32)/9600)-2, /*12: BAUDLO (default=9600) */
118 0, /*13: BAUDHI (default=9600) */
119 ZSWR14_BAUD_ENA
| ZSWR14_BAUD_FROM_PCLK
,
123 static struct zschan
* zs_get_chan_addr(int, int);
124 static void zs_hb_delay(void);
125 static int zshard_hb(void *);
126 static int zs_getc(void *);
127 static void zs_putc(void *, int);
130 zs_get_chan_addr(int zs_unit
, int channel
)
132 struct zsdevice
*addr
;
137 addr
= zsaddr
[zs_unit
];
141 zc
= &addr
->zs_chan_a
;
143 zc
= &addr
->zs_chan_b
;
155 /****************************************************************
157 ****************************************************************/
159 /* Definition of the driver for autoconfig. */
160 int zs_hb_match(device_t
, cfdata_t
, void *);
161 void zs_hb_attach(device_t
, device_t
, void *);
163 CFATTACH_DECL_NEW(zsc_hb
, sizeof(struct zsc_softc
),
164 zs_hb_match
, zs_hb_attach
, NULL
, NULL
);
167 * Is the zs chip present?
170 zs_hb_match(device_t parent
, cfdata_t cf
, void *aux
)
172 struct hb_attach_args
*ha
= aux
;
174 if (strcmp(ha
->ha_name
, "zsc"))
177 /* This returns -1 on a fault (bus error). */
178 if (hb_badaddr((char *)ha
->ha_addr
, 1))
187 * Match slave number to zs unit number, so that misconfiguration will
188 * not set up the keyboard as ttya, etc.
191 zs_hb_attach(device_t parent
, device_t self
, void *aux
)
193 struct zsc_softc
*zsc
= device_private(self
);
194 struct hb_attach_args
*ha
= aux
;
195 struct zsc_attach_args zsc_args
;
196 volatile struct zschan
*zc
;
197 struct zs_chanstate
*cs
;
198 int s
, zs_unit
, channel
, intlevel
;
202 zs_unit
= device_unit(self
);
203 intlevel
= ha
->ha_level
;
204 zsaddr
[zs_unit
] = (void *)ha
->ha_addr
;
206 if (intlevel
== -1) {
208 aprint_error(": interrupt level not configured\n");
211 aprint_error(": interrupt level not configured; using");
216 aprint_error(" level %d\n", intlevel
);
218 zs_delay
= zs_hb_delay
;
221 * Initialize software state for each channel.
223 for (channel
= 0; channel
< 2; channel
++) {
224 zsc_args
.channel
= channel
;
225 zsc_args
.hwflags
= zs_hwflags
[zs_unit
][channel
];
226 cs
= &zsc
->zsc_cs_store
[channel
];
227 zsc
->zsc_cs
[channel
] = cs
;
230 cs
->cs_channel
= channel
;
231 cs
->cs_private
= NULL
;
232 cs
->cs_ops
= &zsops_null
;
233 if ((device_cfdata(self
)->cf_flags
& ZSCFLAG_EX
) == 0)
234 cs
->cs_brg_clk
= PCLK
/ 16;
236 cs
->cs_brg_clk
= PCLK_EX
/ 16;
238 zc
= zs_get_chan_addr(zs_unit
, channel
);
239 cs
->cs_reg_csr
= &zc
->zc_csr
;
240 cs
->cs_reg_data
= &zc
->zc_data
;
242 memcpy(cs
->cs_creg
, zs_init_reg
, 16);
243 memcpy(cs
->cs_preg
, zs_init_reg
, 16);
245 /* XXX: Get these from the EEPROM instead? */
246 /* XXX: See the mvme167 code. Better. */
247 if (zsc_args
.hwflags
& ZS_HWFLAG_CONSOLE
)
248 cs
->cs_defspeed
= zs_get_speed(cs
);
250 cs
->cs_defspeed
= zs_defspeed
;
251 cs
->cs_defcflag
= zs_def_cflag
;
253 /* Make these correspond to cs_defcflag (-crtscts) */
254 cs
->cs_rr0_dcd
= ZSRR0_DCD
;
256 cs
->cs_wr5_dtr
= ZSWR5_DTR
| ZSWR5_RTS
;
260 * Clear the master interrupt enable.
261 * The INTENA is common to both channels,
262 * so just do it on the A channel.
265 zs_write_reg(cs
, 9, 0);
269 * Look for a child driver for this channel.
270 * The child attach will setup the hardware.
272 if (!config_found(self
, (void *)&zsc_args
, zs_print
)) {
273 /* No sub-driver. Just reset it. */
274 uint8_t reset
= (channel
== 0) ?
275 ZSWR9_A_RESET
: ZSWR9_B_RESET
;
277 zs_write_reg(cs
, 9, reset
);
283 * Now safe to install interrupt handlers. Note the arguments
284 * to the interrupt handlers aren't used. Note, we only do this
285 * once since both SCCs interrupt at the same level and vector.
290 zsc
->zsc_si
= softint_establish(SOFTINT_SERIAL
, zssoft
, zsc
);
291 hb_intr_establish(intlevel
, INTST1_SCC
, IPL_SERIAL
,
294 /* XXX; evcnt_attach() ? */
297 * Set the master interrupt enable and interrupt vector.
298 * (common to both channels, do it on A)
302 /* interrupt vector */
303 zs_write_reg(cs
, 2, zs_init_reg
[2]);
304 /* master interrupt control (enable) */
305 zs_write_reg(cs
, 9, zs_init_reg
[9]);
314 (void) *(volatile u_char
*)SCCVECT
;
317 /* XXX news3400 sometimes losts zs interrupt */
330 volatile struct zschan
*zc
= arg
;
334 /* Wait for a character to arrive. */
338 } while ((rr0
& ZSRR0_RX_READY
) == 0);
345 * This is used by the kd driver to read scan codes,
346 * so don't translate '\r' ==> '\n' here...
352 * Polled output char.
355 zs_putc(void *arg
, int c
)
357 volatile struct zschan
*zc
= arg
;
361 /* Wait for transmitter to become ready. */
365 } while ((rr0
& ZSRR0_TX_READY
) == 0);
372 /*****************************************************************/
374 static void zscnprobe(struct consdev
*);
375 static void zscninit(struct consdev
*);
376 static int zscngetc(dev_t
);
377 static void zscnputc(dev_t
, int);
379 struct consdev consdev_zs
= {
393 zscnprobe(struct consdev
*cn
)
398 zscninit(struct consdev
*cn
)
400 extern const struct cdevsw zstty_cdevsw
;
402 cn
->cn_dev
= makedev(cdevsw_lookup_major(&zstty_cdevsw
), 0);
403 cn
->cn_pri
= CN_REMOTE
;
404 zs_hwflags
[0][0] = ZS_HWFLAG_CONSOLE
;
411 return zs_getc((void *)SCCPORT0A
);
415 zscnputc(dev_t dev
, int c
)
418 zs_putc((void *)SCCPORT0A
, c
);