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