zs.c revision 1.1 1 /* $NetBSD: zs.c,v 1.1 1996/05/18 18:54:37 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon Ross
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Zilog Z8530 Dual UART driver (machine-dependent part)
35 *
36 * Runs two serial lines per chip using slave drivers.
37 * Plain tty/async lines use the zs_async slave.
38 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/device.h>
45 #include <sys/conf.h>
46 #include <sys/file.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52
53 #include <dev/cons.h>
54 #include "z8530reg.h"
55 #include <machine/z8530var.h>
56
57 #include <machine/autoconf.h>
58 #include <machine/cpu.h>
59
60 /*
61 * XXX: Hard code this to make console init easier...
62 */
63 #define NZSC 1 /* XXX */
64
65 /*
66 * Define interrupt levels.
67 */
68 #define ZSHARD_PRI 6 /* Wired on the CPU board... */
69 #define ZSSOFT_PRI 3 /* Want tty pri (4) but this is OK. */
70
71 /* The layout of this is hardware-dependent (padding, order). */
72 struct zschan {
73 volatile u_char zc_csr; /* ctrl,status, and indirect access */
74 u_char zc_xxx0;
75 u_char zc_xxx1;
76 u_char zc_xxx2;
77 volatile u_char zc_data; /* data */
78 u_char zc_xxx3;
79 u_char zc_xxx4;
80 u_char zc_xxx5;
81 };
82 /*
83 * The zsdevice structure is not used on the mac68k port as the
84 * chip is wired up weird. Channel B & A are interspursed with
85 * the data & control bytes
86 struct zsdevice {
87 /! Yes, they are backwards. !/
88 struct zschan zs_chan_b;
89 struct zschan zs_chan_a;
90 };
91 */
92
93 /* Saved PROM mappings */
94 static char *zsaddr[NZSC]; /* See zs_init() */
95 /* Flags from cninit() */
96 static int zs_hwflags[NZSC][2];
97 /* Default speed for each channel */
98 static int zs_defspeed[NZSC][2] = {
99 { 9600, /* tty00 */
100 9600 }, /* tty01 */
101 };
102 /* console stuff */
103 void *zs_conschan = 0;
104 int zs_consunit;
105 /* device that the console is attached to--if serial. */
106 dev_t mac68k_zsdev;
107 /* Mac stuff, some vestages of old mac serial driver here */
108 volatile unsigned char *sccA = 0;
109
110 static struct zschan *zs_get_chan_addr __P((int zsc_unit, int channel));
111 void zs_init __P((void));
112
113 static struct zschan *
114 zs_get_chan_addr(zsc_unit, channel)
115 int zsc_unit, channel;
116 {
117 char *addr;
118 struct zschan *zc;
119
120 if (zsc_unit >= NZSC)
121 return NULL;
122 addr = zsaddr[zsc_unit];
123 if (addr == NULL)
124 return NULL;
125 if (channel == 0) {
126 zc = (struct zschan *)(addr +2);
127 /* handle the fact the ports are intertwined. */
128 } else {
129 zc = (struct zschan *)(addr);
130 }
131 return (zc);
132 }
133
134
135 /* Find PROM mappings (for console support). */
136 static int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
137
138 void
139 zs_init()
140 {
141 if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
142 panic("Moved zs0 address after attached!");
143 zsaddr[0] = (char *) sccA;
144 zsinited = 1;
145 if (zs_conschan != 0){ /* we might have moved io under the console */
146 zs_conschan = zs_get_chan_addr(0, zs_consunit);
147 /* so recalc the console port */
148 }
149 }
150
151
152 /*
153 * Even though zsparam will set up the clock multiples, etc., we
154 * still set them here as: 1) mice & keyboards don't use zsparam,
155 * and 2) the console stuff uses these defaults before device
156 * attach.
157 */
158
159 static u_char zs_init_reg[16] = {
160 0, /* 0: CMD (reset, etc.) */
161 ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE,
162 0x18 + ZSHARD_PRI, /* IVECT */
163 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
164 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
165 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
166 0, /* 6: TXSYNC/SYNCLO */
167 0, /* 7: RXSYNC/SYNCHI */
168 0, /* 8: alias for data port */
169 ZSWR9_MASTER_IE,
170 0, /*10: Misc. TX/RX control bits */
171 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
172 14, /*12: BAUDLO (default=9600) */
173 0, /*13: BAUDHI (default=9600) */
174 ZSWR14_BAUD_ENA,
175 ZSWR15_BREAK_IE | ZSWR15_DCD_IE | ZSWR15_CTS_IE,
176 };
177
178
179 /****************************************************************
180 * Autoconfig
181 ****************************************************************/
182
183 /* Definition of the driver for autoconfig. */
184 static int zsc_match __P((struct device *, void *, void *));
185 static void zsc_attach __P((struct device *, struct device *, void *));
186 static int zsc_print __P((void *aux, char *name));
187
188 struct cfattach zsc_ca = {
189 sizeof(struct zsc_softc), zsc_match, zsc_attach
190 };
191
192 struct cfdriver zsc_cd = {
193 NULL, "zsc", DV_DULL
194 };
195
196 int zshard __P((void *));
197 int zssoft __P((void *));
198
199
200 /*
201 * Is the zs chip present?
202 */
203 static int
204 zsc_match(parent, vcf, aux)
205 struct device *parent;
206 void *vcf;
207 void *aux;
208 {
209 return 1;
210 }
211
212 static int
213 zsc_print(aux, name)
214 void *aux;
215 char *name;
216 {
217 struct zsc_attach_args *args = aux;
218
219 if (name != NULL)
220 printf("%s: ", name);
221
222 if (args->channel != -1)
223 printf(" channel %d", args->channel);
224
225 return UNCONF;
226 }
227
228 /*
229 * Attach a found zs.
230 *
231 * Match slave number to zs unit number, so that misconfiguration will
232 * not set up the keyboard as ttya, etc.
233 */
234 static void
235 zsc_attach(parent, self, aux)
236 struct device *parent;
237 struct device *self;
238 void *aux;
239 {
240 struct zsc_softc *zsc = (void *) self;
241 struct zsc_attach_args zsc_args;
242 volatile struct zschan *zc;
243 struct zs_chanstate *cs;
244 int zsc_unit, channel;
245 int reset, s, chip;
246
247 if (!zsinited) zs_init();
248 zsinited = 2;
249
250 zsc_unit = zsc->zsc_dev.dv_unit;
251
252 /* Make sure everything's inited ok. */
253 if (zsaddr[zsc_unit] == NULL)
254 panic("zs_attach: zs%d not mapped\n", zsc_unit);
255
256 /*
257 * Initialize software state for each channel.
258 */
259 for (channel = 0; channel < 2; channel++) {
260 cs = &zsc->zsc_cs[channel];
261
262 zc = zs_get_chan_addr(zsc_unit, channel);
263 cs->cs_reg_csr = &zc->zc_csr;
264 cs->cs_reg_data = &zc->zc_data;
265
266 cs->cs_channel = channel;
267 cs->cs_private = NULL;
268 cs->cs_ops = &zsops_null;
269
270 /* Define BAUD rate clock for the MI code. */
271 cs->cs_pclk_div16 = mac68k_machine.sccClkConst*2;
272 cs->cs_csource = 0;
273 cs->cs_psource = 0;
274
275 cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
276
277 bcopy(zs_init_reg, cs->cs_creg, 16);
278 bcopy(zs_init_reg, cs->cs_preg, 16);
279
280 /*
281 * Clear the master interrupt enable.
282 * The INTENA is common to both channels,
283 * so just do it on the A channel.
284 */
285 if (channel == 0) {
286 zs_write_reg(cs, 9, 0);
287
288 chip = zs_checkchip(cs);
289 printf(" chip type %d \n",chip);
290 }
291 cs->cs_chip = chip;
292
293 /*
294 * Look for a child driver for this channel.
295 * The child attach will setup the hardware.
296 */
297 zsc_args.channel = channel;
298 zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
299 if (!config_found(self, (void *) &zsc_args, zsc_print)) {
300 /* No sub-driver. Just reset it. */
301 reset = (channel == 0) ?
302 ZSWR9_A_RESET : ZSWR9_B_RESET;
303 s = splzs();
304 zs_write_reg(cs, 9, reset);
305 splx(s);
306 }
307 }
308
309 /*
310 * Set the master interrupt enable and interrupt vector.
311 * (common to both channels, do it on A)
312 */
313 cs = &zsc->zsc_cs[0];
314 s = splzs();
315 /* interrupt vector */
316 zs_write_reg(cs, 2, zs_init_reg[2]);
317 /* master interrupt control (enable) */
318 zs_write_reg(cs, 9, zs_init_reg[9]);
319 splx(s);
320 }
321
322 void
323 zstty_mdattach(zsc, zst, cs, tp)
324 struct zsc_softc *zsc;
325 struct zstty_softc *zst;
326 struct zs_chanstate *cs;
327 struct tty *tp;
328 {
329 zst->zst_resetdef = 0;
330 cs->cs_clock_count = 3; /* internal + externals */
331 cs->cs_cclk_flag = 0; /* Not doing anything fancy by default */
332 cs->cs_pclk_flag = 0;
333 cs->cs_clocks[0].clk = mac68k_machine.sccClkConst*32;
334 cs->cs_clocks[0].flags = ZSC_RTXBRG; /* allowing divide by 16 will
335 melt the driver! */
336 cs->cs_clocks[1].clk = 0;
337 cs->cs_clocks[1].flags = ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
338 cs->cs_clocks[2].clk = 0;
339 cs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
340
341 /* For the mac, we have rtscts = check CTS for output control, no
342 * input control. mdmbuf means check DCD for output, and use DTR
343 * for input control. mdmbuf & rtscts means use CTS for output
344 * control, and DTR for input control. */
345
346 zst->zst_hwimasks[1] = 0;
347 zst->zst_hwimasks[2] = ZSWR5_DTR;
348 zst->zst_hwimasks[3] = ZSWR5_DTR;
349 }
350
351 int
352 zsmdioctl(tp, com, data, flag, p)
353 struct tty *tp;
354 u_long com;
355 caddr_t data;
356 int flag;
357 struct proc *p;
358 {
359 return (-1);
360 }
361
362 void
363 zsmd_setclock(cs)
364 struct zs_chanstate *cs;
365 {
366 if (cs->cs_pclk_flag & ZSC_EXTERN) {
367 /* XXX need to set the via! */
368 }
369 }
370
371 int
372 zshard(arg)
373 void *arg;
374 {
375 struct zsc_softc *zsc;
376 int unit, rval;
377 #ifdef ZSMACDEBUG
378 itecnputc(mac68k_zsdev, 'Z');
379 #endif
380
381 rval = 0;
382 unit = zsc_cd.cd_ndevs;
383 while (--unit >= 0) {
384 zsc = zsc_cd.cd_devs[unit];
385 if (zsc != NULL) {
386 rval |= zsc_intr_hard(zsc);
387 }
388 }
389 #ifdef ZSMACDEBUG
390 itecnputc(mac68k_zsdev, '\n');
391 #endif
392 return (rval);
393 }
394
395 int zssoftpending;
396
397 void
398 zsc_req_softint(zsc)
399 struct zsc_softc *zsc;
400 {
401 if (zssoftpending == 0) {
402 /* We are at splzs here, so no need to lock. */
403 zssoftpending = ZSSOFT_PRI;
404 /* isr_soft_request(ZSSOFT_PRI); */
405 setsoftserial();
406 }
407 }
408
409 int
410 zssoft(arg)
411 void *arg;
412 {
413 struct zsc_softc *zsc;
414 int unit;
415
416 /* This is not the only ISR on this IPL. */
417 if (zssoftpending == 0)
418 return (0);
419
420 /*
421 * The soft intr. bit will be set by zshard only if
422 * the variable zssoftpending is zero. The order of
423 * these next two statements prevents our clearing
424 * the soft intr bit just after zshard has set it.
425 */
426 /* isr_soft_clear(ZSSOFT_PRI); */
427 zssoftpending = 0;
428
429 /* Do ttya/ttyb first, because they go faster. */
430 unit = zsc_cd.cd_ndevs;
431 while (--unit >= 0) {
432 zsc = zsc_cd.cd_devs[unit];
433 if (zsc != NULL) {
434 (void) zsc_intr_soft(zsc);
435 }
436 }
437 return (1);
438 }
439
440
441 /*
442 * Read or write the chip with suitable delays.
443 */
444 #define ZS_DELAY()
445 /*
446 * MacII hardware has the delay built in. No need for extra delay. :-)
447 */
448
449 u_char
450 zs_read_reg(cs, reg)
451 struct zs_chanstate *cs;
452 u_char reg;
453 {
454 u_char val;
455
456 *cs->cs_reg_csr = reg;
457 ZS_DELAY();
458 val = *cs->cs_reg_csr;
459 ZS_DELAY();
460 return val;
461 }
462
463 void
464 zs_write_reg(cs, reg, val)
465 struct zs_chanstate *cs;
466 u_char reg, val;
467 {
468 *cs->cs_reg_csr = reg;
469 ZS_DELAY();
470 *cs->cs_reg_csr = val;
471 ZS_DELAY();
472 }
473
474 u_char zs_read_csr(cs)
475 struct zs_chanstate *cs;
476 {
477 register u_char v;
478
479 v = (*cs->cs_reg_csr) ^ ZSRR0_CTS;
480 /* make up for the fact CTS is wired backwards */
481 ZS_DELAY();
482 return v;
483 }
484
485 u_char zs_read_data(cs)
486 struct zs_chanstate *cs;
487 {
488 register u_char v;
489
490 v = *cs->cs_reg_data;
491 ZS_DELAY();
492 return v;
493 }
494
495 void zs_write_csr(cs, val)
496 struct zs_chanstate *cs;
497 u_char val;
498 {
499 *cs->cs_reg_csr = val;
500 ZS_DELAY();
501 }
502
503 void zs_write_data(cs, val)
504 struct zs_chanstate *cs;
505 u_char val;
506 {
507 *cs->cs_reg_data = val;
508 ZS_DELAY();
509 }
510
511 /****************************************************************
512 * Console support functions (Originally Sun3 specific!)
513 * Now works w/ just mac68k port!
514 ****************************************************************/
515
516 #define zscnpollc nullcnpollc
517 cons_decl(zs);
518
519 static void zs_putc __P((register volatile struct zschan *, int));
520 static int zs_getc __P((register volatile struct zschan *));
521 static void zscnsetup __P((void));
522 extern int zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
523
524 /*
525 * Console functions.
526 */
527
528 /*
529 * This code modled after the zs_setparam routine in zskgdb
530 * It sets the console unit to a known state so we can output
531 * correctly.
532 */
533 static void
534 zscnsetup()
535 {
536 struct zs_chanstate cs;
537 struct zschan *zc;
538 int tconst, s;
539
540 /* Setup temporary chanstate. */
541 bzero((caddr_t)&cs, sizeof(cs));
542 zc = zs_conschan;
543 cs.cs_reg_csr = &zc->zc_csr;
544 cs.cs_reg_data = &zc->zc_data;
545 cs.cs_channel = zs_consunit;
546
547 bcopy(zs_init_reg, cs.cs_preg, 16);
548 tconst = BPS_TO_TCONST(mac68k_machine.sccClkConst*2, zs_defspeed[0][zs_consunit]);
549 cs.cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
550 cs.cs_preg[1] = 0; /* don't enable interrupts */
551 cs.cs_preg[12] = tconst;
552 cs.cs_preg[13] = tconst >> 8;
553
554 s = splhigh();
555 zs_loadchannelregs(&cs);
556 splx(s);
557 }
558
559 /*
560 * zscnprobe is the routine which gets called as the kernel is trying to
561 * figure out where the console should be. Each io driver which might
562 * be the console (as defined in mac68k/conf.c) gets probed. The probe
563 * fills in the consdev structure. Important parts are the device #,
564 * and the console priority. Values are CN_DEAD (don't touch me),
565 * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
566 * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
567 *
568 * As the mac's a bit different, we do extra work here. We mainly check
569 * to see if we have serial echo going on, and if the tty's are supposed
570 * to default to raw or not.
571 */
572 void
573 zscnprobe(struct consdev * cp)
574 {
575 extern u_long IOBase;
576 int maj, unit;
577
578 for (maj = 0; maj < nchrdev; maj++) {
579 if (cdevsw[maj].d_open == zsopen) {
580 break;
581 }
582 }
583 if (maj == nchrdev) {
584 /* no console entry for us */
585 if (mac68k_machine.serial_boot_echo) {
586 mac68k_set_io_offsets(IOBase);
587 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
588 zs_consunit = 1;
589 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
590 zs_init();
591 zscnsetup();
592 }
593 return;
594 }
595
596 cp->cn_pri = CN_NORMAL; /* Lower than CN_INTERNAL */
597 if (mac68k_machine.serial_console != 0) {
598 cp->cn_pri = CN_REMOTE; /* Higher than CN_INTERNAL */
599 mac68k_machine.serial_boot_echo =0;
600 }
601
602 unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
603 zs_consunit = unit;
604
605 mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
606
607 if (mac68k_machine.serial_boot_echo) {
608 /*
609 * at this point, we know that we don't have a serial
610 * console, but are doing echo
611 */
612 mac68k_set_io_offsets(IOBase);
613 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
614 zs_consunit = 1;
615 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
616 zs_init();
617 zscnsetup();
618 }
619 return;
620 }
621
622 void
623 zscninit(struct consdev * cp)
624 {
625 extern u_long IOBase;
626 int chan = minor(cp->cn_dev & 1);
627
628 mac68k_set_io_offsets(IOBase);
629 zs_conschan = (struct zschan *) -1;
630 zs_consunit = chan;
631 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE | ZS_HWFLAG_CONABRT;
632 zs_init();
633 /*
634 * zsinit will set up the addresses of the scc. It will also, if
635 * zs_conschan != 0, calculate the new address of the conschan for
636 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
637 * number. :-)
638 */
639 zscnsetup();
640 printf("Whee! we are the console!\n");
641 }
642
643
644 /*
645 * Polled input char.
646 */
647 static int
648 zs_getc(zc)
649 register volatile struct zschan *zc;
650 {
651 register int s, c, rr0;
652
653 s = splhigh();
654 /* Wait for a character to arrive. */
655 do {
656 rr0 = zc->zc_csr;
657 ZS_DELAY();
658 } while ((rr0 & ZSRR0_RX_READY) == 0);
659
660 c = zc->zc_data;
661 ZS_DELAY();
662 splx(s);
663
664 /*
665 * This is used by the kd driver to read scan codes,
666 * so don't translate '\r' ==> '\n' here...
667 */
668 return (c);
669 }
670
671 /*
672 * Polled output char.
673 */
674 static void
675 zs_putc(zc, c)
676 register volatile struct zschan *zc;
677 int c;
678 {
679 register int s, rr0;
680 register long wait = 0;
681
682 s = splhigh();
683 /* Wait for transmitter to become ready. */
684 do {
685 rr0 = zc->zc_csr;
686 ZS_DELAY();
687 } while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
688
689 if ((rr0 & ZSRR0_TX_READY) != 0) {
690 zc->zc_data = c;
691 ZS_DELAY();
692 }
693 splx(s);
694 }
695
696
697 /*
698 * Polled console input putchar.
699 */
700 int
701 zscngetc(dev)
702 dev_t dev;
703 {
704 register volatile struct zschan *zc = zs_conschan;
705 register int c;
706
707 c = zs_getc(zc);
708 return (c);
709 }
710
711 /*
712 * Polled console output putchar.
713 */
714 void
715 zscnputc(dev, c)
716 dev_t dev;
717 int c;
718 {
719 register volatile struct zschan *zc = zs_conschan;
720
721 zs_putc(zc, c);
722 }
723
724
725
726 /*
727 * Handle user request to enter kernel debugger.
728 */
729 void
730 zs_abort(zst)
731 register struct zstty_softc *zst;
732 {
733 register volatile struct zschan *zc = zs_conschan;
734 int rr0;
735 register long wait = 0;
736
737 /* Wait for end of break to avoid PROM abort. */
738 /* XXX - Limit the wait? */
739 do {
740 rr0 = zc->zc_csr;
741 ZS_DELAY();
742 } while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
743
744 if (wait > ZSABORT_DELAY) {
745 if (zst != NULL) zst->zst_hwflags &= ~ZS_HWFLAG_CONABRT;
746 /* If we time out, turn off the abort ability! */
747 }
748
749 /* XXX - Always available, but may be the PROM monitor. */
750 Debugger();
751 }
752