zs_hb.c revision 1.17 1 /* $NetBSD: zs_hb.c,v 1.17 2003/07/15 02:59:30 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Zilog Z8530 Dual UART driver (machine-dependent part)
41 *
42 * Runs two serial lines per chip using slave drivers.
43 * Plain tty/async lines use the zs_async slave.
44 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: zs_hb.c,v 1.17 2003/07/15 02:59:30 lukem Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/tty.h>
54 #include <sys/conf.h>
55
56 #include <machine/adrsmap.h>
57 #include <machine/cpu.h>
58 #include <machine/z8530var.h>
59
60 #include <dev/cons.h>
61 #include <dev/ic/z8530reg.h>
62
63 #include <newsmips/dev/hbvar.h>
64
65 #include "zsc.h" /* NZSC */
66 #define NZS NZSC
67
68 /* Make life easier for the initialized arrays here. */
69 #if NZS < 2
70 #undef NZS
71 #define NZS 2
72 #endif
73
74 #define ZSCFLAG_EX 0x01 /* expansion board */
75
76 /*
77 * The news3400 provides a 4.9152 MHz clock to the ZS chips.
78 */
79 #define PCLK (9600 * 512) /* PCLK pin input clock rate */
80 #define PCLK_EX (9600 * 384)
81
82 /*
83 * Define interrupt levels.
84 */
85 #define ZSHARD_PRI 64
86
87 #define ZS_DELAY() {(void)*(volatile char *)INTEN1; delay(2);}
88
89 /* The layout of this is hardware-dependent (padding, order). */
90 struct zschan {
91 volatile u_char zc_csr; /* ctrl,status, and indirect access */
92 volatile u_char zc_data; /* data */
93 };
94 struct zsdevice {
95 /* Yes, they are backwards. */
96 struct zschan zs_chan_b;
97 struct zschan zs_chan_a;
98 };
99
100 extern int zs_def_cflag;
101
102 static struct zsdevice *zsaddr[NZS];
103
104 /* Flags from cninit() */
105 static int zs_hwflags[NZS][2];
106
107 /* Default speed for all channels */
108 static int zs_defspeed = 9600;
109
110 static u_char zs_init_reg[16] = {
111 0, /* 0: CMD (reset, etc.) */
112 0, /* 1: No interrupts yet. */
113 ZSHARD_PRI, /* IVECT */
114 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
115 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
116 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
117 0, /* 6: TXSYNC/SYNCLO */
118 0, /* 7: RXSYNC/SYNCHI */
119 0, /* 8: alias for data port */
120 ZSWR9_MASTER_IE,
121 0, /*10: Misc. TX/RX control bits */
122 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
123 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
124 0, /*13: BAUDHI (default=9600) */
125 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
126 ZSWR15_BREAK_IE,
127 };
128
129 static struct zschan * zs_get_chan_addr __P((int, int));
130 static void zs_hb_delay __P((void));
131 static int zshard_hb __P((void *));
132 static int zs_getc __P((void *));
133 static void zs_putc __P((void *, int));
134
135 struct zschan *
136 zs_get_chan_addr(zs_unit, channel)
137 int zs_unit, channel;
138 {
139 struct zsdevice *addr;
140 struct zschan *zc;
141
142 if (zs_unit >= NZS)
143 return NULL;
144 addr = zsaddr[zs_unit];
145 if (addr == NULL)
146 return NULL;
147 if (channel == 0) {
148 zc = &addr->zs_chan_a;
149 } else {
150 zc = &addr->zs_chan_b;
151 }
152 return (zc);
153 }
154
155 static void
156 zs_hb_delay()
157 {
158
159 ZS_DELAY();
160 }
161
162 /****************************************************************
163 * Autoconfig
164 ****************************************************************/
165
166 /* Definition of the driver for autoconfig. */
167 int zs_hb_match __P((struct device *, struct cfdata *, void *));
168 void zs_hb_attach __P((struct device *, struct device *, void *));
169
170 CFATTACH_DECL(zsc_hb, sizeof(struct zsc_softc),
171 zs_hb_match, zs_hb_attach, NULL, NULL);
172
173 /*
174 * Is the zs chip present?
175 */
176 int
177 zs_hb_match(parent, cf, aux)
178 struct device *parent;
179 struct cfdata *cf;
180 void *aux;
181 {
182 struct hb_attach_args *ha = aux;
183
184 if (strcmp(ha->ha_name, "zsc"))
185 return 0;
186
187 /* This returns -1 on a fault (bus error). */
188 if (hb_badaddr((char *)ha->ha_addr, 1))
189 return 0;
190
191 return 1;
192 }
193
194 /*
195 * Attach a found zs.
196 *
197 * Match slave number to zs unit number, so that misconfiguration will
198 * not set up the keyboard as ttya, etc.
199 */
200 void
201 zs_hb_attach(parent, self, aux)
202 struct device *parent;
203 struct device *self;
204 void *aux;
205 {
206 struct zsc_softc *zsc = (void *)self;
207 struct hb_attach_args *ha = aux;
208 struct zsc_attach_args zsc_args;
209 volatile struct zschan *zc;
210 struct zs_chanstate *cs;
211 int s, zs_unit, channel, intlevel;
212 static int didintr;
213
214 zs_unit = zsc->zsc_dev.dv_unit;
215 intlevel = ha->ha_level;
216 zsaddr[zs_unit] = (void *)ha->ha_addr;
217
218 if (intlevel == -1) {
219 #if 0
220 printf(": interrupt level not configured\n");
221 return;
222 #else
223 printf(": interrupt level not configured; using");
224 intlevel = 1;
225 #endif
226 }
227
228 printf(" level %d\n", intlevel);
229
230 zs_delay = zs_hb_delay;
231
232 /*
233 * Initialize software state for each channel.
234 */
235 for (channel = 0; channel < 2; channel++) {
236 zsc_args.channel = channel;
237 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
238 cs = &zsc->zsc_cs_store[channel];
239 zsc->zsc_cs[channel] = cs;
240
241 simple_lock_init(&cs->cs_lock);
242 cs->cs_channel = channel;
243 cs->cs_private = NULL;
244 cs->cs_ops = &zsops_null;
245 if ((zsc->zsc_dev.dv_cfdata->cf_flags & ZSCFLAG_EX) == 0)
246 cs->cs_brg_clk = PCLK / 16;
247 else
248 cs->cs_brg_clk = PCLK_EX / 16;
249
250 zc = zs_get_chan_addr(zs_unit, channel);
251 cs->cs_reg_csr = &zc->zc_csr;
252 cs->cs_reg_data = &zc->zc_data;
253
254 bcopy(zs_init_reg, cs->cs_creg, 16);
255 bcopy(zs_init_reg, cs->cs_preg, 16);
256
257 /* XXX: Get these from the EEPROM instead? */
258 /* XXX: See the mvme167 code. Better. */
259 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
260 cs->cs_defspeed = zs_get_speed(cs);
261 else
262 cs->cs_defspeed = zs_defspeed;
263 cs->cs_defcflag = zs_def_cflag;
264
265 /* Make these correspond to cs_defcflag (-crtscts) */
266 cs->cs_rr0_dcd = ZSRR0_DCD;
267 cs->cs_rr0_cts = 0;
268 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
269 cs->cs_wr5_rts = 0;
270
271 /*
272 * Clear the master interrupt enable.
273 * The INTENA is common to both channels,
274 * so just do it on the A channel.
275 */
276 if (channel == 0) {
277 zs_write_reg(cs, 9, 0);
278 }
279
280 /*
281 * Look for a child driver for this channel.
282 * The child attach will setup the hardware.
283 */
284 if (!config_found(self, (void *)&zsc_args, zs_print)) {
285 /* No sub-driver. Just reset it. */
286 u_char reset = (channel == 0) ?
287 ZSWR9_A_RESET : ZSWR9_B_RESET;
288 s = splhigh();
289 zs_write_reg(cs, 9, reset);
290 splx(s);
291 }
292 }
293
294 /*
295 * Now safe to install interrupt handlers. Note the arguments
296 * to the interrupt handlers aren't used. Note, we only do this
297 * once since both SCCs interrupt at the same level and vector.
298 */
299 if (!didintr) {
300 didintr = 1;
301
302 zsc->zsc_si = softintr_establish(IPL_SOFTSERIAL, zssoft, zsc);
303 hb_intr_establish(intlevel, INTST1_SCC, IPL_SERIAL,
304 zshard_hb, NULL);
305 }
306 /* XXX; evcnt_attach() ? */
307
308 /*
309 * Set the master interrupt enable and interrupt vector.
310 * (common to both channels, do it on A)
311 */
312 cs = zsc->zsc_cs[0];
313 s = splhigh();
314 /* interrupt vector */
315 zs_write_reg(cs, 2, zs_init_reg[2]);
316 /* master interrupt control (enable) */
317 zs_write_reg(cs, 9, zs_init_reg[9]);
318 splx(s);
319 }
320
321 static int
322 zshard_hb(arg)
323 void *arg;
324 {
325 int rv;
326
327 (void) *(volatile u_char *)SCCVECT;
328 rv = zshard(arg);
329
330 /* XXX news3400 sometimes losts zs interrupt */
331 if (rv)
332 zshard(arg);
333
334 return rv;
335 }
336
337 /*
338 * Polled input char.
339 */
340 int
341 zs_getc(arg)
342 void *arg;
343 {
344 volatile struct zschan *zc = arg;
345 int s, c, rr0;
346
347 s = splhigh();
348 /* Wait for a character to arrive. */
349 do {
350 rr0 = zc->zc_csr;
351 ZS_DELAY();
352 } while ((rr0 & ZSRR0_RX_READY) == 0);
353
354 c = zc->zc_data;
355 ZS_DELAY();
356 splx(s);
357
358 /*
359 * This is used by the kd driver to read scan codes,
360 * so don't translate '\r' ==> '\n' here...
361 */
362 return (c);
363 }
364
365 /*
366 * Polled output char.
367 */
368 void
369 zs_putc(arg, c)
370 void *arg;
371 int c;
372 {
373 volatile struct zschan *zc = arg;
374 int s, rr0;
375
376 s = splhigh();
377 /* Wait for transmitter to become ready. */
378 do {
379 rr0 = zc->zc_csr;
380 ZS_DELAY();
381 } while ((rr0 & ZSRR0_TX_READY) == 0);
382
383 zc->zc_data = c;
384 ZS_DELAY();
385 splx(s);
386 }
387
388 /*****************************************************************/
389
390 static void zscnprobe __P((struct consdev *));
391 static void zscninit __P((struct consdev *));
392 static int zscngetc __P((dev_t));
393 static void zscnputc __P((dev_t, int));
394
395 struct consdev consdev_zs = {
396 zscnprobe,
397 zscninit,
398 zscngetc,
399 zscnputc,
400 nullcnpollc,
401 NULL,
402 NULL,
403 NULL,
404 NODEV,
405 CN_DEAD
406 };
407
408 static void
409 zscnprobe(cn)
410 struct consdev *cn;
411 {
412 }
413
414 static void
415 zscninit(cn)
416 struct consdev *cn;
417 {
418 extern const struct cdevsw zstty_cdevsw;
419
420 cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
421 cn->cn_pri = CN_REMOTE;
422 zs_hwflags[0][0] = ZS_HWFLAG_CONSOLE;
423 }
424
425 static int
426 zscngetc(dev)
427 dev_t dev;
428 {
429
430 return zs_getc((void *)SCCPORT0A);
431 }
432
433 static void
434 zscnputc(dev, c)
435 dev_t dev;
436 int c;
437 {
438
439 zs_putc((void *)SCCPORT0A, c);
440 }
441