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