zs.c revision 1.4 1 /* $NetBSD: zs.c,v 1.4 2000/08/29 11:25:08 wdk Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 2000 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 and Wayne Knowles
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 */
45
46 #include "opt_ddb.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/device.h>
52 #include <sys/file.h>
53 #include <sys/ioctl.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/tty.h>
57 #include <sys/time.h>
58 #include <sys/syslog.h>
59
60 #include <machine/cpu.h>
61 #include <machine/mainboard.h>
62 #include <machine/autoconf.h>
63 #include <machine/z8530var.h>
64
65 #include <dev/cons.h>
66 #include <dev/ic/z8530reg.h>
67
68 #include "zsc.h" /* NZSC */
69 #define NZS NZSC
70
71 /* Make life easier for the initialized arrays here. */
72 #if NZS < 2
73 #undef NZS
74 #define NZS 2
75 #endif
76
77 extern void Debugger __P((void));
78
79 /*
80 * Some warts needed by z8530tty.c -
81 * The default parity REALLY needs to be the same as the PROM uses,
82 * or you can not see messages done with printf during boot-up...
83 */
84 int zs_def_cflag = (CREAD | CS8 | HUPCL);
85 int zs_major = 1;
86
87 /*
88 * 10MHz PCLK
89 */
90 #define PCLK 10000000 /* PCLK pin input clock rate */
91
92 /*
93 * Define interrupt levels.
94 */
95 #define ZSHARD_PRI 64
96
97 #define ZS_DELAY() delay(2);
98
99 /* The layout of this is hardware-dependent (padding, order). */
100 struct zschan {
101 u_char pad1[3];
102 volatile u_char zc_csr; /* ctrl,status, and indirect access */
103 u_char pad2[3];
104 volatile u_char zc_data; /* data */
105 };
106 struct zsdevice {
107 /* Yes, they are backwards. */
108 struct zschan zs_chan_b;
109 struct zschan zs_chan_a;
110 };
111
112 /* Flags from cninit() */
113 static int zs_hwflags[NZS][2];
114
115 /* Default speed for all channels */
116 static int zs_defspeed = 9600;
117
118 static u_char zs_init_reg[16] = {
119 0, /* 0: CMD (reset, etc.) */
120 0, /* 1: No interrupts yet. */
121 ZSHARD_PRI, /* IVECT */
122 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
123 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
124 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
125 0, /* 6: TXSYNC/SYNCLO */
126 0, /* 7: RXSYNC/SYNCHI */
127 0, /* 8: alias for data port */
128 ZSWR9_MASTER_IE,
129 0, /*10: Misc. TX/RX control bits */
130 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
131 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
132 0, /*13: BAUDHI (default=9600) */
133 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
134 ZSWR15_BREAK_IE,
135 };
136
137
138 struct zschan *
139 zs_get_chan_addr(zs_unit, channel)
140 int zs_unit, channel;
141 {
142 struct zsdevice *addr;
143 struct zschan *zc;
144
145 if (zs_unit >= NZS)
146 return NULL;
147
148 addr = (struct zsdevice *) ZS0_ADDR;
149
150 if (channel == 0) {
151 zc = &addr->zs_chan_a;
152 } else {
153 zc = &addr->zs_chan_b;
154 }
155 return (zc);
156 }
157
158
159 /****************************************************************
160 * Autoconfig
161 ****************************************************************/
162
163 /* Definition of the driver for autoconfig. */
164 static int zs_match __P((struct device *, struct cfdata *, void *));
165 static void zs_attach __P((struct device *, struct device *, void *));
166 static int zs_print __P((void *, const char *name));
167
168 struct cfattach zsc_ca = {
169 sizeof(struct zsc_softc), zs_match, zs_attach
170 };
171
172 extern struct cfdriver zsc_cd;
173
174 static int zshard __P((void *));
175 static void zssoft __P((void *));
176 static int zs_get_speed __P((struct zs_chanstate *));
177
178
179 /*
180 * Is the zs chip present?
181 */
182 static int
183 zs_match(parent, cf, aux)
184 struct device *parent;
185 struct cfdata *cf;
186 void *aux;
187 {
188 struct confargs *ca = aux;
189 void *va;
190
191 if (strcmp(ca->ca_name, "zsc"))
192 return 0;
193
194 va = (void *)cf->cf_addr;
195
196 /* This returns -1 on a fault (bus error). */
197 if (badaddr(va, 1))
198 return 0;
199 return 1;
200 }
201
202 /*
203 * Attach a found zs.
204 *
205 * Match slave number to zs unit number, so that misconfiguration will
206 * not set up the keyboard as ttya, etc.
207 */
208 static void
209 zs_attach(parent, self, aux)
210 struct device *parent;
211 struct device *self;
212 void *aux;
213 {
214 struct zsc_softc *zsc = (void *) self;
215 struct confargs *ca = aux;
216 struct zsc_attach_args zsc_args;
217 struct zsdevice *zsd;
218 struct zschan *zc;
219 struct zs_chanstate *cs;
220 int s, zs_unit, channel;
221
222 zsc->zsc_bustag = ca->ca_bustag;
223 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
224 sizeof(struct zsdevice),
225 BUS_SPACE_MAP_LINEAR,
226 &zsc->zsc_base) != 0) {
227 printf(": cannot map registers\n");
228 return;
229 }
230 zsd = (struct zsdevice *)zsc->zsc_base;
231
232 zs_unit = zsc->zsc_dev.dv_unit;
233 printf("\n");
234
235 /*
236 * Initialize software state for each channel.
237 */
238 for (channel = 0; channel < 2; channel++) {
239 zsc_args.channel = channel;
240 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
241 cs = &zsc->zsc_cs_store[channel];
242 zsc->zsc_cs[channel] = cs;
243
244 cs->cs_channel = channel;
245 cs->cs_private = NULL;
246 cs->cs_ops = &zsops_null;
247 cs->cs_brg_clk = PCLK / 16;
248
249 zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
250
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 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
258 cs->cs_defspeed = zs_get_speed(cs);
259 else
260 cs->cs_defspeed = zs_defspeed;
261 cs->cs_defcflag = zs_def_cflag;
262
263 /* Make these correspond to cs_defcflag (-crtscts) */
264 cs->cs_rr0_dcd = ZSRR0_DCD;
265 cs->cs_rr0_cts = 0;
266 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
267 cs->cs_wr5_rts = 0;
268
269 /*
270 * Clear the master interrupt enable.
271 * The INTENA is common to both channels,
272 * so just do it on the A channel.
273 */
274 if (channel == 0) {
275 zs_write_reg(cs, 9, 0);
276 }
277 /*
278 * Look for a child driver for this channel.
279 * The child attach will setup the hardware.
280 */
281 if (!config_found(self, (void *)&zsc_args, zs_print)) {
282 /* No sub-driver. Just reset it. */
283 u_char reset = (channel == 0) ?
284 ZSWR9_A_RESET : ZSWR9_B_RESET;
285
286 s = splhigh();
287 zs_write_reg(cs, 9, reset);
288 splx(s);
289 }
290 }
291
292 /* bus_intr_establish(zssoft, NULL, ZSSOFT_PRI); */
293 bus_intr_establish(zsc->zsc_bustag, SYS_INTR_SCC0, 0, 0, zshard, NULL);
294
295 evcnt_attach_dynamic(&zsc->zs_intrcnt, EVCNT_TYPE_INTR, NULL,
296 self->dv_xname, "intr");
297
298 /*
299 * Set the master interrupt enable and interrupt vector.
300 * (common to both channels, do it on A)
301 */
302 cs = zsc->zsc_cs[0];
303 s = splhigh();
304 /* interrupt vector */
305 zs_write_reg(cs, 2, zs_init_reg[2]);
306 /* master interrupt control (enable) */
307 zs_write_reg(cs, 9, zs_init_reg[9]);
308 splx(s);
309 }
310
311 static int
312 zs_print(aux, name)
313 void *aux;
314 const char *name;
315 {
316 struct zsc_attach_args *args = aux;
317
318 if (name != NULL)
319 printf("%s: ", name);
320
321 if (args->channel != -1)
322 printf(" channel %d", args->channel);
323
324 return UNCONF;
325 }
326
327 static volatile int zssoftpending;
328
329 /*
330 * Our ZS chips all share a common, autovectored interrupt,
331 * so we have to look at all of them on each interrupt.
332 */
333 static int
334 zshard(arg)
335 void *arg;
336 {
337 register struct zsc_softc *zsc;
338 register int unit, rval, softreq;
339
340 rval = softreq = 0;
341 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
342 zsc = zsc_cd.cd_devs[unit];
343 if (zsc == NULL)
344 continue;
345 rval |= zsc_intr_hard(zsc);
346 softreq |= zsc->zsc_cs[0]->cs_softreq;
347 softreq |= zsc->zsc_cs[1]->cs_softreq;
348 zsc->zs_intrcnt.ev_count++;
349 }
350
351 /* We are at splzs here, so no need to lock. */
352 if (softreq && (zssoftpending == 0)) {
353 zssoftpending = 1;
354 zssoft(arg); /*isr_soft_request(ZSSOFT_PRI);*/
355 }
356 return 0;
357 }
358
359 /*
360 * Similar scheme as for zshard (look at all of them)
361 */
362 static void
363 zssoft(arg)
364 void *arg;
365 {
366 register struct zsc_softc *zsc;
367 register int s, unit;
368
369 /* This is not the only ISR on this IPL. */
370 if (zssoftpending == 0)
371 return;
372
373 /*
374 * The soft intr. bit will be set by zshard only if
375 * the variable zssoftpending is zero. The order of
376 * these next two statements prevents our clearing
377 * the soft intr bit just after zshard has set it.
378 */
379 /*isr_soft_clear(ZSSOFT_PRI);*/
380 /*zssoftpending = 0;*/
381
382 /* Make sure we call the tty layer at spltty. */
383 s = spltty();
384 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
385 zsc = zsc_cd.cd_devs[unit];
386 if (zsc == NULL)
387 continue;
388 (void) zsc_intr_soft(zsc);
389 }
390 splx(s);
391 zssoftpending = 0;
392 return;
393 }
394
395
396 /*
397 * Compute the current baud rate given a ZS channel.
398 */
399 static int
400 zs_get_speed(cs)
401 struct zs_chanstate *cs;
402 {
403 int tconst;
404
405 tconst = zs_read_reg(cs, 12);
406 tconst |= zs_read_reg(cs, 13) << 8;
407 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
408 }
409
410 /*
411 * MD functions for setting the baud rate and control modes.
412 */
413 int
414 zs_set_speed(cs, bps)
415 struct zs_chanstate *cs;
416 int bps; /* bits per second */
417 {
418 int tconst, real_bps;
419
420 /* Wait for transmit buffer to empty */
421 while (!(zs_read_csr(cs) & ZSRR0_TX_READY))
422 {/*nop*/}
423
424 if (bps == 0)
425 return (0);
426
427 #ifdef DIAGNOSTIC
428 if (cs->cs_brg_clk == 0)
429 panic("zs_set_speed");
430 #endif
431
432 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
433 if (tconst < 0)
434 return (EINVAL);
435
436 /* Convert back to make sure we can do it. */
437 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
438
439 /* XXX - Allow some tolerance here? */
440 #if 0
441 if (real_bps != bps)
442 return (EINVAL);
443 #endif
444
445 cs->cs_preg[12] = tconst;
446 cs->cs_preg[13] = tconst >> 8;
447
448 /* Caller will stuff the pending registers. */
449 return (0);
450 }
451
452 int
453 zs_set_modes(cs, cflag)
454 struct zs_chanstate *cs;
455 int cflag; /* bits per second */
456 {
457 int s;
458
459 /*
460 * Output hardware flow control on the chip is horrendous:
461 * if carrier detect drops, the receiver is disabled, and if
462 * CTS drops, the transmitter is stoped IN MID CHARACTER!
463 * Therefore, NEVER set the HFC bit, and instead use the
464 * status interrupt to detect CTS changes.
465 */
466 s = splzs();
467 cs->cs_rr0_pps = 0;
468 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
469 cs->cs_rr0_dcd = 0;
470 if ((cflag & MDMBUF) == 0)
471 cs->cs_rr0_pps = ZSRR0_DCD;
472 } else
473 cs->cs_rr0_dcd = ZSRR0_DCD;
474 if ((cflag & CRTSCTS) != 0) {
475 cs->cs_wr5_dtr = ZSWR5_DTR;
476 cs->cs_wr5_rts = ZSWR5_RTS;
477 cs->cs_rr0_cts = ZSRR0_CTS;
478 } else if ((cflag & MDMBUF) != 0) {
479 cs->cs_wr5_dtr = 0;
480 cs->cs_wr5_rts = ZSWR5_DTR;
481 cs->cs_rr0_cts = ZSRR0_DCD;
482 } else {
483 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
484 cs->cs_wr5_rts = 0;
485 cs->cs_rr0_cts = 0;
486 }
487 splx(s);
488
489 /* Caller will stuff the pending registers. */
490 return (0);
491 }
492
493
494 /*
495 * Read or write the chip with suitable delays.
496 */
497
498 u_char
499 zs_read_reg(cs, reg)
500 struct zs_chanstate *cs;
501 u_char reg;
502 {
503 u_char val;
504
505 *cs->cs_reg_csr = reg;
506 ZS_DELAY();
507 val = *cs->cs_reg_csr;
508 ZS_DELAY();
509 return val;
510 }
511
512 void
513 zs_write_reg(cs, reg, val)
514 struct zs_chanstate *cs;
515 u_char reg, val;
516 {
517 *cs->cs_reg_csr = reg;
518 ZS_DELAY();
519 *cs->cs_reg_csr = val;
520 ZS_DELAY();
521 }
522
523 u_char zs_read_csr(cs)
524 struct zs_chanstate *cs;
525 {
526 register u_char val;
527
528 val = *cs->cs_reg_csr;
529 ZS_DELAY();
530 return val;
531 }
532
533 void zs_write_csr(cs, val)
534 struct zs_chanstate *cs;
535 u_char val;
536 {
537 *cs->cs_reg_csr = val;
538 ZS_DELAY();
539 }
540
541 u_char zs_read_data(cs)
542 struct zs_chanstate *cs;
543 {
544 register u_char val;
545
546 val = *cs->cs_reg_data;
547 ZS_DELAY();
548 return val;
549 }
550
551 void zs_write_data(cs, val)
552 struct zs_chanstate *cs;
553 u_char val;
554 {
555 *cs->cs_reg_data = val;
556 ZS_DELAY();
557 }
558
559 void
560 zs_abort(cs)
561 struct zs_chanstate *cs;
562 {
563 #ifdef DDB
564 Debugger();
565 #endif
566 }
567
568 /*
569 * Polled input char.
570 */
571 int
572 zs_getc(arg)
573 void *arg;
574 {
575 register volatile struct zschan *zc = arg;
576 register int s, c, rr0;
577
578 s = splhigh();
579 /* Wait for a character to arrive. */
580 do {
581 rr0 = zc->zc_csr;
582 ZS_DELAY();
583 } while ((rr0 & ZSRR0_RX_READY) == 0);
584
585 c = zc->zc_data;
586 ZS_DELAY();
587 splx(s);
588
589 /*
590 * This is used by the kd driver to read scan codes,
591 * so don't translate '\r' ==> '\n' here...
592 */
593 return (c);
594 }
595
596 /*
597 * Polled output char.
598 */
599 void
600 zs_putc(arg, c)
601 void *arg;
602 int c;
603 {
604 register volatile struct zschan *zc = arg;
605 register int s, rr0;
606
607 s = splhigh();
608 /* Wait for transmitter to become ready. */
609 do {
610 rr0 = zc->zc_csr;
611 ZS_DELAY();
612 } while ((rr0 & ZSRR0_TX_READY) == 0);
613
614 zc->zc_data = c;
615 ZS_DELAY();
616 splx(s);
617 }
618
619 /*****************************************************************/
620
621 static void zscnprobe __P((struct consdev *));
622 static void zscninit __P((struct consdev *));
623 static int zscngetc __P((dev_t));
624 static void zscnputc __P((dev_t, int));
625 static void zscnpollc __P((dev_t, int));
626
627 static int cons_port;
628 extern int prom_getconsole __P((void));
629
630 struct consdev consdev_zs = {
631 zscnprobe,
632 zscninit,
633 zscngetc,
634 zscnputc,
635 zscnpollc
636 };
637
638 void
639 zscnprobe(cn)
640 struct consdev *cn;
641 {
642 }
643
644 void
645 zscninit(cn)
646 struct consdev *cn;
647 {
648 cons_port = prom_getconsole();
649 cn->cn_dev = makedev(zs_major, cons_port);
650 cn->cn_pri = CN_REMOTE;
651 zs_hwflags[0][cons_port] = ZS_HWFLAG_CONSOLE;
652 }
653
654 int
655 zscngetc(dev)
656 dev_t dev;
657 {
658 struct zschan *zs;
659
660 zs = zs_get_chan_addr(0, cons_port);
661 return zs_getc(zs);
662 }
663
664 void
665 zscnputc(dev, c)
666 dev_t dev;
667 int c;
668 {
669 struct zschan *zs;
670
671 zs = zs_get_chan_addr(0, cons_port);
672 zs_putc(zs, c);
673 }
674
675 void
676 zscnpollc(dev, on)
677 dev_t dev;
678 int on;
679 {
680 }
681