zs.c revision 1.43 1 /* $NetBSD: zs.c,v 1.43 2003/07/15 02:43:18 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1996-1998 Bill Studenmund
5 * Copyright (c) 1995 Gordon W. Ross
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 * 4. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Gordon Ross
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Zilog Z8530 Dual UART driver (machine-dependent part)
36 *
37 * Runs two serial lines per chip using slave drivers.
38 * Plain tty/async lines use the zs_async slave.
39 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
40 * Other ports use their own mice & keyboard slaves.
41 *
42 * Credits & history:
43 *
44 * With NetBSD 1.1, port-mac68k started using a port of the port-sparc
45 * (port-sun3?) zs.c driver (which was in turn based on code in the
46 * Berkeley 4.4 Lite release). Bill Studenmund did the port, with
47 * help from Allen Briggs and Gordon Ross <gwr (at) netbsd.org>. Noud de
48 * Brouwer field-tested the driver at a local ISP.
49 *
50 * Bill Studenmund and Gordon Ross then ported the machine-independant
51 * z8530 driver to work with port-mac68k. NetBSD 1.2 contained an
52 * intermediate version (mac68k using a local, patched version of
53 * the m.i. drivers), with NetBSD 1.3 containing a full version.
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.43 2003/07/15 02:43:18 lukem Exp $");
58
59 #include "opt_ddb.h"
60 #include "opt_mac68k.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/proc.h>
65 #include <sys/device.h>
66 #include <sys/conf.h>
67 #include <sys/file.h>
68 #include <sys/ioctl.h>
69 #include <sys/tty.h>
70 #include <sys/time.h>
71 #include <sys/kernel.h>
72 #include <sys/syslog.h>
73
74 #include <machine/autoconf.h>
75 #include <machine/cpu.h>
76 #include <machine/psc.h>
77 #include <machine/viareg.h>
78
79 #include <dev/cons.h>
80 #include <dev/ic/z8530reg.h>
81 #include <machine/z8530var.h>
82 #include <mac68k/dev/zs_cons.h>
83
84 /* Are these in a header file anywhere? */
85 /* Booter flags interface */
86 #define ZSMAC_RAW 0x01
87 #define ZSMAC_LOCALTALK 0x02
88
89 #define PCLK (9600 * 384)
90
91 #include "zsc.h" /* get the # of zs chips defined */
92
93 /*
94 * Some warts needed by z8530tty.c -
95 */
96 int zs_def_cflag = (CREAD | CS8 | HUPCL);
97
98 /*
99 * abort detection on console will now timeout after iterating on a loop
100 * the following # of times. Cheep hack. Also, abort detection is turned
101 * off after a timeout (i.e. maybe there's not a terminal hooked up).
102 */
103 #define ZSABORT_DELAY 3000000
104
105 /*
106 * Define interrupt levels.
107 */
108 #define ZSHARD_PRI 4 /* Wired on the CPU board... */
109 /*
110 * Serial port cards with zs chips on them are actually at the
111 * NuBus interrupt level, which is lower than 4. But blocking
112 * level 4 interrupts will block those interrupts too, so level
113 * 4 is fine.
114 */
115
116 /* The layout of this is hardware-dependent (padding, order). */
117 struct zschan {
118 volatile u_char zc_csr; /* ctrl,status, and indirect access */
119 u_char zc_xxx0;
120 u_char zc_xxx1; /* part of the other channel lives here! */
121 u_char zc_xxx2; /* Yea Apple! */
122 volatile u_char zc_data; /* data */
123 u_char zc_xxx3;
124 u_char zc_xxx4;
125 u_char zc_xxx5;
126 };
127
128 /* Saved PROM mappings */
129 static char *zsaddr[NZSC]; /* See zs_init() */
130 /* Flags from cninit() */
131 static int zs_hwflags[NZSC][2];
132 /* Default speed for each channel */
133 static int zs_defspeed[NZSC][2] = {
134 { 9600, /* tty00 */
135 9600 }, /* tty01 */
136 };
137 /* console stuff */
138 void *zs_conschan = 0;
139 int zs_consunit;
140 #ifdef ZS_CONSOLE_ABORT
141 int zs_cons_canabort = 1;
142 #else
143 int zs_cons_canabort = 0;
144 #endif /* ZS_CONSOLE_ABORT*/
145 /* device to which the console is attached--if serial. */
146 dev_t mac68k_zsdev;
147 /* Mac stuff */
148 volatile unsigned char *sccA = 0;
149
150 int zs_cn_check_speed __P((int bps));
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 0, /* 1: No interrupts yet. */
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 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
173 0, /*13: BAUDHI (default=9600) */
174 ZSWR14_BAUD_ENA,
175 ZSWR15_BREAK_IE,
176 };
177
178 struct zschan *
179 zs_get_chan_addr(zsc_unit, channel)
180 int zsc_unit, channel;
181 {
182 char *addr;
183 struct zschan *zc;
184
185 if (zsc_unit >= NZSC)
186 return NULL;
187 addr = zsaddr[zsc_unit];
188 if (addr == NULL)
189 return NULL;
190 if (channel == 0) {
191 zc = (struct zschan *)(addr + 2);
192 /* handle the fact the ports are intertwined. */
193 } else {
194 zc = (struct zschan *)(addr);
195 }
196 return (zc);
197 }
198
199
200 /* Find PROM mappings (for console support). */
201 int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
202
203 void
204 zs_init()
205 {
206 if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
207 panic("Moved zs0 address after attached!");
208 zsaddr[0] = (char *) sccA;
209 zsinited = 1;
210 if (zs_conschan != 0){ /* we might have moved io under the console */
211 zs_conschan = zs_get_chan_addr(0, zs_consunit);
212 /* so recalc the console port */
213 }
214 }
215
216
217 /****************************************************************
218 * Autoconfig
219 ****************************************************************/
220
221 /* Definition of the driver for autoconfig. */
222 static int zsc_match __P((struct device *, struct cfdata *, void *));
223 static void zsc_attach __P((struct device *, struct device *, void *));
224 static int zsc_print __P((void *, const char *name));
225
226 CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
227 zsc_match, zsc_attach, NULL, NULL);
228
229 extern struct cfdriver zsc_cd;
230
231 int zshard __P((void *));
232 int zssoft __P((void *));
233
234
235 /*
236 * Is the zs chip present?
237 */
238 static int
239 zsc_match(parent, cf, aux)
240 struct device *parent;
241 struct cfdata *cf;
242 void *aux;
243 {
244 return 1;
245 }
246
247 /*
248 * Attach a found zs.
249 *
250 * Match slave number to zs unit number, so that misconfiguration will
251 * not set up the keyboard as ttya, etc.
252 */
253 static void
254 zsc_attach(parent, self, aux)
255 struct device *parent;
256 struct device *self;
257 void *aux;
258 {
259 struct zsc_softc *zsc = (void *) self;
260 struct zsc_attach_args zsc_args;
261 volatile struct zschan *zc;
262 struct xzs_chanstate *xcs;
263 struct zs_chanstate *cs;
264 int zsc_unit, channel;
265 int s, chip, theflags;
266
267 if (!zsinited)
268 zs_init();
269 zsinited = 2;
270
271 zsc_unit = zsc->zsc_dev.dv_unit;
272
273 /* Make sure everything's inited ok. */
274 if (zsaddr[zsc_unit] == NULL)
275 panic("zs_attach: zs%d not mapped", zsc_unit);
276
277 chip = 0; /* We'll deal with chip types post 1.2 */
278 printf(" chip type %d \n",chip);
279
280 /*
281 * Initialize software state for each channel.
282 */
283 for (channel = 0; channel < 2; channel++) {
284 zsc_args.channel = channel;
285 zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
286 xcs = &zsc->xzsc_xcs_store[channel];
287 cs = &xcs->xzs_cs;
288 zsc->zsc_cs[channel] = cs;
289
290 simple_lock_init(&cs->cs_lock);
291 cs->cs_channel = channel;
292 cs->cs_private = NULL;
293 cs->cs_ops = &zsops_null;
294
295 zc = zs_get_chan_addr(zsc_unit, channel);
296 cs->cs_reg_csr = &zc->zc_csr;
297 cs->cs_reg_data = &zc->zc_data;
298
299 bcopy(zs_init_reg, cs->cs_creg, 16);
300 bcopy(zs_init_reg, cs->cs_preg, 16);
301
302 /* Current BAUD rate generator clock. */
303 cs->cs_brg_clk = PCLK / 16; /* RTxC is 230400*16, so use 230400 */
304 cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
305 cs->cs_defcflag = zs_def_cflag;
306
307 /* Make these correspond to cs_defcflag (-crtscts) */
308 cs->cs_rr0_dcd = ZSRR0_DCD;
309 cs->cs_rr0_cts = 0;
310 cs->cs_wr5_dtr = ZSWR5_DTR;
311 cs->cs_wr5_rts = 0;
312
313 #ifdef __notyet__
314 cs->cs_slave_type = ZS_SLAVE_NONE;
315 #endif
316
317 /* Define BAUD rate stuff. */
318 xcs->cs_clocks[0].clk = PCLK;
319 xcs->cs_clocks[0].flags = ZSC_RTXBRG | ZSC_RTXDIV;
320 xcs->cs_clocks[1].flags =
321 ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
322 xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
323 xcs->cs_clock_count = 3;
324 if (channel == 0) {
325 theflags = mac68k_machine.modem_flags;
326 xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
327 xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
328 } else {
329 theflags = mac68k_machine.print_flags;
330 xcs->cs_clocks[1].flags = ZSC_VARIABLE;
331 /*
332 * Yes, we aren't defining ANY clock source enables for the
333 * printer's DCD clock in. The hardware won't let us
334 * use it. But a clock will freak out the chip, so we
335 * let you set it, telling us to bar interrupts on the line.
336 */
337 xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
338 xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
339 }
340 if (xcs->cs_clocks[1].clk)
341 zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
342 if (xcs->cs_clocks[2].clk)
343 zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
344
345 printf("zsc%d channel %d: d_speed %6d DCD clk %ld CTS clk %ld",
346 zsc_unit, channel, cs->cs_defspeed,
347 xcs->cs_clocks[1].clk, xcs->cs_clocks[2].clk);
348
349 /* Set defaults in our "extended" chanstate. */
350 xcs->cs_csource = 0;
351 xcs->cs_psource = 0;
352 xcs->cs_cclk_flag = 0; /* Nothing fancy by default */
353 xcs->cs_pclk_flag = 0;
354
355 if (theflags & ZSMAC_RAW) {
356 zsc_args.hwflags |= ZS_HWFLAG_RAW;
357 printf(" (raw defaults)");
358 }
359
360 /*
361 * XXX - This might be better done with a "stub" driver
362 * (to replace zstty) that ignores LocalTalk for now.
363 */
364 if (theflags & ZSMAC_LOCALTALK) {
365 printf(" shielding from LocalTalk");
366 cs->cs_defspeed = 1;
367 cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
368 cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
369 zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
370 zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
371 /*
372 * If we might have LocalTalk, then make sure we have the
373 * Baud rate low-enough to not do any damage.
374 */
375 }
376
377 /*
378 * We used to disable chip interrupts here, but we now
379 * do that in zscnprobe, just in case MacOS left the chip on.
380 */
381
382 xcs->cs_chip = chip;
383
384 /* Stash away a copy of the final H/W flags. */
385 xcs->cs_hwflags = zsc_args.hwflags;
386
387 printf("\n");
388
389 /*
390 * Look for a child driver for this channel.
391 * The child attach will setup the hardware.
392 */
393 if (!config_found(self, (void *)&zsc_args, zsc_print)) {
394 /* No sub-driver. Just reset it. */
395 u_char reset = (channel == 0) ?
396 ZSWR9_A_RESET : ZSWR9_B_RESET;
397 s = splzs();
398 zs_write_reg(cs, 9, reset);
399 splx(s);
400 }
401 }
402
403 if (current_mac_model->class == MACH_CLASSAV) {
404 add_psc_lev4_intr(PSCINTR_SCCA, zshard, zsc);
405 add_psc_lev4_intr(PSCINTR_SCCB, zshard, zsc);
406 } else {
407 intr_establish(zshard, zsc, ZSHARD_PRI);
408 }
409
410 /* Now safe to enable interrupts. */
411
412 /*
413 * Set the master interrupt enable and interrupt vector.
414 * (common to both channels, do it on A)
415 */
416 cs = zsc->zsc_cs[0];
417 s = splzs();
418 /* interrupt vector */
419 zs_write_reg(cs, 2, zs_init_reg[2]);
420 /* master interrupt control (enable) */
421 zs_write_reg(cs, 9, zs_init_reg[9]);
422 splx(s);
423 }
424
425 static int
426 zsc_print(aux, name)
427 void *aux;
428 const char *name;
429 {
430 struct zsc_attach_args *args = aux;
431
432 if (name != NULL)
433 aprint_normal("%s: ", name);
434
435 if (args->channel != -1)
436 aprint_normal(" channel %d", args->channel);
437
438 return UNCONF;
439 }
440
441 int
442 zsmdioctl(cs, cmd, data)
443 struct zs_chanstate *cs;
444 u_long cmd;
445 caddr_t data;
446 {
447 switch (cmd) {
448 default:
449 return (EPASSTHROUGH);
450 }
451 return (0);
452 }
453
454 void
455 zsmd_setclock(cs)
456 struct zs_chanstate *cs;
457 {
458 struct xzs_chanstate *xcs = (void *)cs;
459
460 if (cs->cs_channel != 0)
461 return;
462
463 /*
464 * If the new clock has the external bit set, then select the
465 * external source.
466 */
467 via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
468 }
469
470 static int zssoftpending;
471
472 /*
473 * Do the minimum work to pull data off of the chip and queue it up
474 * for later processing.
475 */
476 int
477 zshard(arg)
478 void *arg;
479 {
480 struct zsc_softc *zsc = (struct zsc_softc *)arg;
481 int rval;
482
483 if (zsc == NULL)
484 return 0;
485
486 rval = zsc_intr_hard(zsc);
487 if ((zsc->zsc_cs[0]->cs_softreq) || (zsc->zsc_cs[1]->cs_softreq)) {
488 /* zsc_req_softint(zsc); */
489 /* We are at splzs here, so no need to lock. */
490 if (zssoftpending == 0) {
491 zssoftpending = 1;
492 setsoftserial();
493 }
494 }
495 return (rval);
496 }
497
498 /*
499 * Look at all of the zsc softint queues.
500 */
501 int
502 zssoft(arg)
503 void *arg;
504 {
505 struct zsc_softc *zsc;
506 int unit;
507
508 /* This is not the only ISR on this IPL. */
509 if (zssoftpending == 0)
510 return (0);
511
512 /*
513 * The soft intr. bit will be set by zshard only if
514 * the variable zssoftpending is zero.
515 */
516 zssoftpending = 0;
517
518 for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
519 zsc = zsc_cd.cd_devs[unit];
520 if (zsc == NULL)
521 continue;
522 (void) zsc_intr_soft(zsc);
523 }
524 return (1);
525 }
526
527
528 #ifndef ZS_TOLERANCE
529 #define ZS_TOLERANCE 51
530 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
531 #endif
532
533 /*
534 * check out a rate for acceptability from the internal clock
535 * source. Used in console config to validate a requested
536 * default speed. Placed here so that all the speed checking code is
537 * in one place.
538 *
539 * != 0 means ok.
540 */
541 int
542 zs_cn_check_speed(bps)
543 int bps; /* target rate */
544 {
545 int tc, rate;
546
547 tc = BPS_TO_TCONST(PCLK / 16, bps);
548 if (tc < 0)
549 return 0;
550 rate = TCONST_TO_BPS(PCLK / 16, tc);
551 if (ZS_TOLERANCE > abs(((rate - bps)*1000)/bps))
552 return 1;
553 else
554 return 0;
555 }
556
557 /*
558 * Search through the signal sources in the channel, and
559 * pick the best one for the baud rate requested. Return
560 * a -1 if not achievable in tolerance. Otherwise return 0
561 * and fill in the values.
562 *
563 * This routine draws inspiration from the Atari port's zs.c
564 * driver in NetBSD 1.1 which did the same type of source switching.
565 * Tolerance code inspired by comspeed routine in isa/com.c.
566 *
567 * By Bill Studenmund, 1996-05-12
568 */
569 int
570 zs_set_speed(cs, bps)
571 struct zs_chanstate *cs;
572 int bps; /* bits per second */
573 {
574 struct xzs_chanstate *xcs = (void *) cs;
575 int i, tc, tc0 = 0, tc1, s, sf = 0;
576 int src, rate0, rate1, err, tol;
577
578 if (bps == 0)
579 return (0);
580
581 src = -1; /* no valid source yet */
582 tol = ZS_TOLERANCE;
583
584 /*
585 * Step through all the sources and see which one matches
586 * the best. A source has to match BETTER than tol to be chosen.
587 * Thus if two sources give the same error, the first one will be
588 * chosen. Also, allow for the possability that one source might run
589 * both the BRG and the direct divider (i.e. RTxC).
590 */
591 for (i=0; i < xcs->cs_clock_count; i++) {
592 if (xcs->cs_clocks[i].clk <= 0)
593 continue; /* skip non-existent or bad clocks */
594 if (xcs->cs_clocks[i].flags & ZSC_BRG) {
595 /* check out BRG at /16 */
596 tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
597 if (tc1 >= 0) {
598 rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
599 err = abs(((rate1 - bps)*1000)/bps);
600 if (err < tol) {
601 tol = err;
602 src = i;
603 sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
604 tc0 = tc1;
605 rate0 = rate1;
606 }
607 }
608 }
609 if (xcs->cs_clocks[i].flags & ZSC_DIV) {
610 /*
611 * Check out either /1, /16, /32, or /64
612 * Note: for /1, you'd better be using a synchronized
613 * clock!
614 */
615 int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
616 int b1 = b0 >> 4, e1 = abs(b1-bps);
617 int b2 = b1 >> 1, e2 = abs(b2-bps);
618 int b3 = b2 >> 1, e3 = abs(b3-bps);
619
620 if (e0 < e1 && e0 < e2 && e0 < e3) {
621 err = e0;
622 rate1 = b0;
623 tc1 = ZSWR4_CLK_X1;
624 } else if (e0 > e1 && e1 < e2 && e1 < e3) {
625 err = e1;
626 rate1 = b1;
627 tc1 = ZSWR4_CLK_X16;
628 } else if (e0 > e2 && e1 > e2 && e2 < e3) {
629 err = e2;
630 rate1 = b2;
631 tc1 = ZSWR4_CLK_X32;
632 } else {
633 err = e3;
634 rate1 = b3;
635 tc1 = ZSWR4_CLK_X64;
636 }
637
638 err = (err * 1000)/bps;
639 if (err < tol) {
640 tol = err;
641 src = i;
642 sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
643 tc0 = tc1;
644 rate0 = rate1;
645 }
646 }
647 }
648 #ifdef ZSMACDEBUG
649 zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
650 #endif
651 if (src == -1)
652 return (EINVAL); /* no can do */
653
654 /*
655 * The M.I. layer likes to keep cs_brg_clk current, even though
656 * we are the only ones who should be touching the BRG's rate.
657 *
658 * Note: we are assuming that any ZSC_EXTERN signal source comes in
659 * on the RTxC pin. Correct for the mac68k obio zsc.
660 */
661 if (sf & ZSC_EXTERN)
662 cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
663 else
664 cs->cs_brg_clk = PCLK / 16;
665
666 /*
667 * Now we have a source, so set it up.
668 */
669 s = splzs();
670 xcs->cs_psource = src;
671 xcs->cs_pclk_flag = sf;
672 bps = rate0;
673 if (sf & ZSC_BRG) {
674 cs->cs_preg[4] = ZSWR4_CLK_X16;
675 cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
676 if (sf & ZSC_PCLK) {
677 cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
678 } else {
679 cs->cs_preg[14] = ZSWR14_BAUD_ENA;
680 }
681 tc = tc0;
682 } else {
683 cs->cs_preg[4] = tc0;
684 if (sf & ZSC_RTXDIV) {
685 cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
686 } else {
687 cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
688 }
689 cs->cs_preg[14]= 0;
690 tc = 0xffff;
691 }
692 /* Set the BAUD rate divisor. */
693 cs->cs_preg[12] = tc;
694 cs->cs_preg[13] = tc >> 8;
695 splx(s);
696
697 #ifdef ZSMACDEBUG
698 zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
699 bps, tc, src, sf);
700 zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
701 cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
702 #endif
703
704 cs->cs_preg[5] |= ZSWR5_RTS; /* Make sure the drivers are on! */
705
706 /* Caller will stuff the pending registers. */
707 return (0);
708 }
709
710 int
711 zs_set_modes(cs, cflag)
712 struct zs_chanstate *cs;
713 int cflag; /* bits per second */
714 {
715 struct xzs_chanstate *xcs = (void*)cs;
716 int s;
717
718 /*
719 * Make sure we don't enable hfc on a signal line we're ignoring.
720 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
721 * this code also effectivly turns off ZSWR15_CTS_IE.
722 *
723 * Also, disable DCD interrupts if we've been told to ignore
724 * the DCD pin. Happens on mac68k because the input line for
725 * DCD can also be used as a clock input. (Just set CLOCAL.)
726 *
727 * If someone tries to turn an invalid flow mode on, Just Say No
728 * (Suggested by gwr)
729 */
730 if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
731 return (EINVAL);
732 cs->cs_rr0_pps = 0;
733 if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
734 if (cflag & MDMBUF)
735 return (EINVAL);
736 cflag |= CLOCAL;
737 } else {
738 /*
739 * cs->cs_rr0_pps indicates which bit MAY be used for pps.
740 * Enable only if nothing else will want the interrupt and
741 * it's ok to enable interrupts on this line.
742 */
743 if ((cflag & (CLOCAL | MDMBUF)) == CLOCAL)
744 cs->cs_rr0_pps = ZSRR0_DCD;
745 }
746 if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
747 return (EINVAL);
748
749 /*
750 * Output hardware flow control on the chip is horrendous:
751 * if carrier detect drops, the receiver is disabled, and if
752 * CTS drops, the transmitter is stoped IN MID CHARACTER!
753 * Therefore, NEVER set the HFC bit, and instead use the
754 * status interrupt to detect CTS changes.
755 */
756 s = splzs();
757 if ((cflag & (CLOCAL | MDMBUF)) != 0)
758 cs->cs_rr0_dcd = 0;
759 else
760 cs->cs_rr0_dcd = ZSRR0_DCD;
761 /*
762 * The mac hardware only has one output, DTR (HSKo in Mac
763 * parlance). In HFC mode, we use it for the functions
764 * typically served by RTS and DTR on other ports, so we
765 * have to fake the upper layer out some.
766 *
767 * CRTSCTS we use CTS as an input which tells us when to shut up.
768 * We make no effort to shut up the other side of the connection.
769 * DTR is used to hang up the modem.
770 *
771 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
772 * shut up the other side.
773 */
774 if ((cflag & CRTSCTS) != 0) {
775 cs->cs_wr5_dtr = ZSWR5_DTR;
776 cs->cs_wr5_rts = 0;
777 cs->cs_rr0_cts = ZSRR0_CTS;
778 } else if ((cflag & CDTRCTS) != 0) {
779 cs->cs_wr5_dtr = 0;
780 cs->cs_wr5_rts = ZSWR5_DTR;
781 cs->cs_rr0_cts = ZSRR0_CTS;
782 } else if ((cflag & MDMBUF) != 0) {
783 cs->cs_wr5_dtr = 0;
784 cs->cs_wr5_rts = ZSWR5_DTR;
785 cs->cs_rr0_cts = ZSRR0_DCD;
786 } else {
787 cs->cs_wr5_dtr = ZSWR5_DTR;
788 cs->cs_wr5_rts = 0;
789 cs->cs_rr0_cts = 0;
790 }
791 splx(s);
792
793 /* Caller will stuff the pending registers. */
794 return (0);
795 }
796
797
798 /*
799 * Read or write the chip with suitable delays.
800 * MacII hardware has the delay built in.
801 * No need for extra delay. :-) However, some clock-chirped
802 * macs, or zsc's on serial add-on boards might need it.
803 */
804 #define ZS_DELAY()
805
806 u_char
807 zs_read_reg(cs, reg)
808 struct zs_chanstate *cs;
809 u_char reg;
810 {
811 u_char val;
812
813 *cs->cs_reg_csr = reg;
814 ZS_DELAY();
815 val = *cs->cs_reg_csr;
816 ZS_DELAY();
817 return val;
818 }
819
820 void
821 zs_write_reg(cs, reg, val)
822 struct zs_chanstate *cs;
823 u_char reg, val;
824 {
825 *cs->cs_reg_csr = reg;
826 ZS_DELAY();
827 *cs->cs_reg_csr = val;
828 ZS_DELAY();
829 }
830
831 u_char zs_read_csr(cs)
832 struct zs_chanstate *cs;
833 {
834 u_char val;
835
836 val = *cs->cs_reg_csr;
837 ZS_DELAY();
838 /* make up for the fact CTS is wired backwards */
839 val ^= ZSRR0_CTS;
840 return val;
841 }
842
843 void zs_write_csr(cs, val)
844 struct zs_chanstate *cs;
845 u_char val;
846 {
847 /* Note, the csr does not write CTS... */
848 *cs->cs_reg_csr = val;
849 ZS_DELAY();
850 }
851
852 u_char zs_read_data(cs)
853 struct zs_chanstate *cs;
854 {
855 u_char val;
856
857 val = *cs->cs_reg_data;
858 ZS_DELAY();
859 return val;
860 }
861
862 void zs_write_data(cs, val)
863 struct zs_chanstate *cs;
864 u_char val;
865 {
866 *cs->cs_reg_data = val;
867 ZS_DELAY();
868 }
869
870 /****************************************************************
871 * Console support functions (mac68k specific!)
872 * Note: this code is allowed to know about the layout of
873 * the chip registers, and uses that to keep things simple.
874 * XXX - I think I like the mvme167 code better. -gwr
875 * XXX - Well :-P :-) -wrs
876 ****************************************************************/
877
878 #define zscnpollc nullcnpollc
879 cons_decl(zs);
880
881 static void zscnsetup __P((void));
882
883 /*
884 * Console functions.
885 */
886
887 /*
888 * This code modled after the zs_setparam routine in zskgdb
889 * It sets the console unit to a known state so we can output
890 * correctly.
891 */
892 static void
893 zscnsetup()
894 {
895 struct xzs_chanstate xcs;
896 struct zs_chanstate *cs;
897 struct zschan *zc;
898 int tconst, s;
899
900 /* Setup temporary chanstate. */
901 bzero((caddr_t)&xcs, sizeof(xcs));
902 cs = &xcs.xzs_cs;
903 zc = zs_conschan;
904 cs->cs_reg_csr = &zc->zc_csr;
905 cs->cs_reg_data = &zc->zc_data;
906 cs->cs_channel = zs_consunit;
907 cs->cs_brg_clk = PCLK / 16;
908
909 bcopy(zs_init_reg, cs->cs_preg, 16);
910 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
911 cs->cs_preg[15] = ZSWR15_BREAK_IE;
912 tconst = BPS_TO_TCONST(cs->cs_brg_clk,
913 zs_defspeed[0][zs_consunit]);
914 cs->cs_preg[12] = tconst;
915 cs->cs_preg[13] = tconst >> 8;
916 /* can't use zs_set_speed as we haven't set up the
917 * signal sources, and it's not worth it for now
918 */
919
920 /*
921 * As zs_loadchannelregs doesn't touch reg 9 (interrupt control),
922 * we won't accidentally turn on interrupts below
923 */
924 s = splhigh();
925 zs_loadchannelregs(cs);
926 splx(s);
927 }
928
929 /*
930 * zscnprobe is the routine which gets called as the kernel is trying to
931 * figure out where the console should be. Each io driver which might
932 * be the console (as defined in mac68k/conf.c) gets probed. The probe
933 * fills in the consdev structure. Important parts are the device #,
934 * and the console priority. Values are CN_DEAD (don't touch me),
935 * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
936 * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
937 *
938 * As the mac's a bit different, we do extra work here. We mainly check
939 * to see if we have serial echo going on. Also chould check for default
940 * speeds.
941 */
942 void
943 zscnprobe(struct consdev * cp)
944 {
945 extern u_long IOBase;
946 int maj, unit, i;
947 extern const struct cdevsw zstty_cdevsw;
948
949 maj = cdevsw_lookup_major(&zstty_cdevsw);
950 if (maj != -1) {
951 cp->cn_pri = CN_NORMAL; /* Lower than CN_INTERNAL */
952 if (mac68k_machine.serial_console != 0) {
953 cp->cn_pri = CN_REMOTE; /* Higher than CN_INTERNAL */
954 mac68k_machine.serial_boot_echo =0;
955 }
956
957 unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
958 zs_consunit = unit;
959 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
960
961 mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
962 }
963 if (mac68k_machine.serial_boot_echo) {
964 /*
965 * at this point, we know that we don't have a serial
966 * console, but are doing echo
967 */
968 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
969 zs_consunit = 1;
970 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
971 }
972
973 if ((i = mac68k_machine.modem_d_speed) > 0) {
974 if (zs_cn_check_speed(i))
975 zs_defspeed[0][0] = i;
976 }
977 if ((i = mac68k_machine.print_d_speed) > 0) {
978 if (zs_cn_check_speed(i))
979 zs_defspeed[0][1] = i;
980 }
981 mac68k_set_io_offsets(IOBase);
982 zs_init();
983 /*
984 * zsinit will set up the addresses of the scc. It will also, if
985 * zs_conschan != 0, calculate the new address of the conschan for
986 * unit zs_consunit. So if we are (or think we are) going to use the
987 * chip for console I/O, we just set up the internal addresses for it.
988 *
989 * Now turn off interrupts for the chip. Note: using sccA to get at
990 * the chip is the only vestage of the NetBSD 1.0 ser driver. :-)
991 */
992 unit = sccA[2]; /* reset reg. access */
993 unit = sccA[0];
994 sccA[2] = 9; sccA[2] = 0; /* write 0 to reg. 9, clearing MIE */
995 sccA[2] = ZSWR0_CLR_INTR; unit = sccA[2]; /* reset any pending ints. */
996 sccA[0] = ZSWR0_CLR_INTR; unit = sccA[0];
997
998 if (mac68k_machine.serial_boot_echo)
999 zscnsetup();
1000 return;
1001 }
1002
1003 void
1004 zscninit(struct consdev * cp)
1005 {
1006
1007 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
1008 /*
1009 * zsinit will set up the addresses of the scc. It will also, if
1010 * zs_conschan != 0, calculate the new address of the conschan for
1011 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
1012 * number. :-)
1013 */
1014 zscnsetup();
1015 printf("\nNetBSD/mac68k console\n");
1016 }
1017
1018
1019 /*
1020 * Polled input char.
1021 */
1022 int
1023 zs_getc(arg)
1024 void *arg;
1025 {
1026 volatile struct zschan *zc = arg;
1027 int s, c, rr0;
1028
1029 s = splhigh();
1030 /* Wait for a character to arrive. */
1031 do {
1032 rr0 = zc->zc_csr;
1033 ZS_DELAY();
1034 } while ((rr0 & ZSRR0_RX_READY) == 0);
1035
1036 c = zc->zc_data;
1037 ZS_DELAY();
1038 splx(s);
1039
1040 /*
1041 * This is used by the kd driver to read scan codes,
1042 * so don't translate '\r' ==> '\n' here...
1043 */
1044 return (c);
1045 }
1046
1047 /*
1048 * Polled output char.
1049 */
1050 void
1051 zs_putc(arg, c)
1052 void *arg;
1053 int c;
1054 {
1055 volatile struct zschan *zc = arg;
1056 int s, rr0;
1057 long wait = 0;
1058
1059 s = splhigh();
1060 /* Wait for transmitter to become ready. */
1061 do {
1062 rr0 = zc->zc_csr;
1063 ZS_DELAY();
1064 } while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
1065
1066 if ((rr0 & ZSRR0_TX_READY) != 0) {
1067 zc->zc_data = c;
1068 ZS_DELAY();
1069 }
1070 splx(s);
1071 }
1072
1073
1074 /*
1075 * Polled console input putchar.
1076 */
1077 int
1078 zscngetc(dev)
1079 dev_t dev;
1080 {
1081 struct zschan *zc = zs_conschan;
1082 int c;
1083
1084 c = zs_getc(zc);
1085 return (c);
1086 }
1087
1088 /*
1089 * Polled console output putchar.
1090 */
1091 void
1092 zscnputc(dev, c)
1093 dev_t dev;
1094 int c;
1095 {
1096 struct zschan *zc = zs_conschan;
1097
1098 zs_putc(zc, c);
1099 }
1100
1101
1102
1103 /*
1104 * Handle user request to enter kernel debugger.
1105 */
1106 void
1107 zs_abort(cs)
1108 struct zs_chanstate *cs;
1109 {
1110 volatile struct zschan *zc = zs_conschan;
1111 int rr0;
1112 long wait = 0;
1113
1114 if (zs_cons_canabort == 0)
1115 return;
1116
1117 /* Wait for end of break to avoid PROM abort. */
1118 do {
1119 rr0 = zc->zc_csr;
1120 ZS_DELAY();
1121 } while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
1122
1123 if (wait > ZSABORT_DELAY) {
1124 zs_cons_canabort = 0;
1125 /* If we time out, turn off the abort ability! */
1126 }
1127
1128 #ifdef DDB
1129 Debugger();
1130 #endif
1131 }
1132
1133