zs.c revision 1.46 1 /* $NetBSD: zs.c,v 1.46 2005/01/15 16:00:59 chs 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.46 2005/01/15 16:00:59 chs 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;
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 extern volatile unsigned char *sccA;
149
150 int zs_cn_check_speed(int);
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(int zsc_unit, int channel)
180 {
181 char *addr;
182 struct zschan *zc;
183
184 if (zsc_unit >= NZSC)
185 return NULL;
186 addr = zsaddr[zsc_unit];
187 if (addr == NULL)
188 return NULL;
189 if (channel == 0) {
190 zc = (struct zschan *)(addr + 2);
191 /* handle the fact the ports are intertwined. */
192 } else {
193 zc = (struct zschan *)(addr);
194 }
195 return (zc);
196 }
197
198
199 /* Find PROM mappings (for console support). */
200 int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
201
202 void
203 zs_init()
204 {
205 if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
206 panic("Moved zs0 address after attached!");
207 zsaddr[0] = (char *) sccA;
208 zsinited = 1;
209 if (zs_conschan != 0){ /* we might have moved io under the console */
210 zs_conschan = zs_get_chan_addr(0, zs_consunit);
211 /* so recalc the console port */
212 }
213 }
214
215
216 /****************************************************************
217 * Autoconfig
218 ****************************************************************/
219
220 /* Definition of the driver for autoconfig. */
221 static int zsc_match(struct device *, struct cfdata *, void *);
222 static void zsc_attach(struct device *, struct device *, void *);
223 static int zsc_print(void *, const char *);
224
225 CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
226 zsc_match, zsc_attach, NULL, NULL);
227
228 extern struct cfdriver zsc_cd;
229
230 int zshard(void *);
231 int zssoft(void *);
232
233 /*
234 * Is the zs chip present?
235 */
236 static int
237 zsc_match(struct device *parent, struct cfdata *cf, void *aux)
238 {
239 return 1;
240 }
241
242 /*
243 * Attach a found zs.
244 *
245 * Match slave number to zs unit number, so that misconfiguration will
246 * not set up the keyboard as ttya, etc.
247 */
248 static void
249 zsc_attach(struct device *parent, struct device *self, void *aux)
250 {
251 struct zsc_softc *zsc = (void *) self;
252 struct zsc_attach_args zsc_args;
253 volatile struct zschan *zc;
254 struct xzs_chanstate *xcs;
255 struct zs_chanstate *cs;
256 int zsc_unit, channel;
257 int s, chip, theflags;
258
259 if (!zsinited)
260 zs_init();
261 zsinited = 2;
262
263 zsc_unit = zsc->zsc_dev.dv_unit;
264
265 /* Make sure everything's inited ok. */
266 if (zsaddr[zsc_unit] == NULL)
267 panic("zs_attach: zs%d not mapped", zsc_unit);
268
269 chip = 0; /* We'll deal with chip types post 1.2 */
270 printf(" chip type %d \n",chip);
271
272 /*
273 * Initialize software state for each channel.
274 */
275 for (channel = 0; channel < 2; channel++) {
276 zsc_args.channel = channel;
277 zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
278 xcs = &zsc->xzsc_xcs_store[channel];
279 cs = &xcs->xzs_cs;
280 zsc->zsc_cs[channel] = cs;
281
282 simple_lock_init(&cs->cs_lock);
283 cs->cs_channel = channel;
284 cs->cs_private = NULL;
285 cs->cs_ops = &zsops_null;
286
287 zc = zs_get_chan_addr(zsc_unit, channel);
288 cs->cs_reg_csr = &zc->zc_csr;
289 cs->cs_reg_data = &zc->zc_data;
290
291 memcpy(cs->cs_creg, zs_init_reg, 16);
292 memcpy(cs->cs_preg, zs_init_reg, 16);
293
294 /* Current BAUD rate generator clock. */
295 cs->cs_brg_clk = PCLK / 16; /* RTxC is 230400*16, so use 230400 */
296 cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
297 cs->cs_defcflag = zs_def_cflag;
298
299 /* Make these correspond to cs_defcflag (-crtscts) */
300 cs->cs_rr0_dcd = ZSRR0_DCD;
301 cs->cs_rr0_cts = 0;
302 cs->cs_wr5_dtr = ZSWR5_DTR;
303 cs->cs_wr5_rts = 0;
304
305 #ifdef __notyet__
306 cs->cs_slave_type = ZS_SLAVE_NONE;
307 #endif
308
309 /* Define BAUD rate stuff. */
310 xcs->cs_clocks[0].clk = PCLK;
311 xcs->cs_clocks[0].flags = ZSC_RTXBRG | ZSC_RTXDIV;
312 xcs->cs_clocks[1].flags =
313 ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
314 xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
315 xcs->cs_clock_count = 3;
316 if (channel == 0) {
317 theflags = mac68k_machine.modem_flags;
318 xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
319 xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
320 } else {
321 theflags = mac68k_machine.print_flags;
322 xcs->cs_clocks[1].flags = ZSC_VARIABLE;
323 /*
324 * Yes, we aren't defining ANY clock source enables for the
325 * printer's DCD clock in. The hardware won't let us
326 * use it. But a clock will freak out the chip, so we
327 * let you set it, telling us to bar interrupts on the line.
328 */
329 xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
330 xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
331 }
332 if (xcs->cs_clocks[1].clk)
333 zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
334 if (xcs->cs_clocks[2].clk)
335 zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
336
337 printf("zsc%d channel %d: d_speed %6d DCD clk %ld CTS clk %ld",
338 zsc_unit, channel, cs->cs_defspeed,
339 xcs->cs_clocks[1].clk, xcs->cs_clocks[2].clk);
340
341 /* Set defaults in our "extended" chanstate. */
342 xcs->cs_csource = 0;
343 xcs->cs_psource = 0;
344 xcs->cs_cclk_flag = 0; /* Nothing fancy by default */
345 xcs->cs_pclk_flag = 0;
346
347 if (theflags & ZSMAC_RAW) {
348 zsc_args.hwflags |= ZS_HWFLAG_RAW;
349 printf(" (raw defaults)");
350 }
351
352 /*
353 * XXX - This might be better done with a "stub" driver
354 * (to replace zstty) that ignores LocalTalk for now.
355 */
356 if (theflags & ZSMAC_LOCALTALK) {
357 printf(" shielding from LocalTalk");
358 cs->cs_defspeed = 1;
359 cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
360 cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
361 zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
362 zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
363 /*
364 * If we might have LocalTalk, then make sure we have the
365 * Baud rate low-enough to not do any damage.
366 */
367 }
368
369 /*
370 * We used to disable chip interrupts here, but we now
371 * do that in zscnprobe, just in case MacOS left the chip on.
372 */
373
374 xcs->cs_chip = chip;
375
376 /* Stash away a copy of the final H/W flags. */
377 xcs->cs_hwflags = zsc_args.hwflags;
378
379 printf("\n");
380
381 /*
382 * Look for a child driver for this channel.
383 * The child attach will setup the hardware.
384 */
385 if (!config_found(self, (void *)&zsc_args, zsc_print)) {
386 /* No sub-driver. Just reset it. */
387 u_char reset = (channel == 0) ?
388 ZSWR9_A_RESET : ZSWR9_B_RESET;
389 s = splzs();
390 zs_write_reg(cs, 9, reset);
391 splx(s);
392 }
393 }
394
395 if (current_mac_model->class == MACH_CLASSAV) {
396 add_psc_lev4_intr(PSCINTR_SCCA, zshard, zsc);
397 add_psc_lev4_intr(PSCINTR_SCCB, zshard, zsc);
398 } else {
399 intr_establish(zshard, zsc, ZSHARD_PRI);
400 }
401
402 /* Now safe to enable interrupts. */
403
404 /*
405 * Set the master interrupt enable and interrupt vector.
406 * (common to both channels, do it on A)
407 */
408 cs = zsc->zsc_cs[0];
409 s = splzs();
410 /* interrupt vector */
411 zs_write_reg(cs, 2, zs_init_reg[2]);
412 /* master interrupt control (enable) */
413 zs_write_reg(cs, 9, zs_init_reg[9]);
414 splx(s);
415 }
416
417 static int
418 zsc_print(void *aux, const char *name)
419 {
420 struct zsc_attach_args *args = aux;
421
422 if (name != NULL)
423 aprint_normal("%s: ", name);
424
425 if (args->channel != -1)
426 aprint_normal(" channel %d", args->channel);
427
428 return UNCONF;
429 }
430
431 int
432 zsmdioctl(struct zs_chanstate *cs, u_long cmd, caddr_t data)
433 {
434 switch (cmd) {
435 default:
436 return (EPASSTHROUGH);
437 }
438 return (0);
439 }
440
441 void
442 zsmd_setclock(struct zs_chanstate *cs)
443 {
444 struct xzs_chanstate *xcs = (void *)cs;
445
446 if (cs->cs_channel != 0)
447 return;
448
449 /*
450 * If the new clock has the external bit set, then select the
451 * external source.
452 */
453 via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
454 }
455
456 static int zssoftpending;
457
458 /*
459 * Do the minimum work to pull data off of the chip and queue it up
460 * for later processing.
461 */
462 int
463 zshard(void *arg)
464 {
465 struct zsc_softc *zsc = (struct zsc_softc *)arg;
466 int rval;
467
468 if (zsc == NULL)
469 return 0;
470
471 rval = zsc_intr_hard(zsc);
472 if ((zsc->zsc_cs[0]->cs_softreq) || (zsc->zsc_cs[1]->cs_softreq)) {
473 /* zsc_req_softint(zsc); */
474 /* We are at splzs here, so no need to lock. */
475 if (zssoftpending == 0) {
476 zssoftpending = 1;
477 setsoftserial();
478 }
479 }
480 return (rval);
481 }
482
483 /*
484 * Look at all of the zsc softint queues.
485 */
486 int
487 zssoft(void *arg)
488 {
489 struct zsc_softc *zsc;
490 int unit;
491
492 /* This is not the only ISR on this IPL. */
493 if (zssoftpending == 0)
494 return (0);
495
496 /*
497 * The soft intr. bit will be set by zshard only if
498 * the variable zssoftpending is zero.
499 */
500 zssoftpending = 0;
501
502 for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
503 zsc = zsc_cd.cd_devs[unit];
504 if (zsc == NULL)
505 continue;
506 (void) zsc_intr_soft(zsc);
507 }
508 return (1);
509 }
510
511
512 #ifndef ZS_TOLERANCE
513 #define ZS_TOLERANCE 51
514 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
515 #endif
516
517 /*
518 * check out a rate for acceptability from the internal clock
519 * source. Used in console config to validate a requested
520 * default speed. Placed here so that all the speed checking code is
521 * in one place.
522 *
523 * != 0 means ok.
524 */
525 int
526 zs_cn_check_speed(int bps)
527 {
528 int tc, rate;
529
530 tc = BPS_TO_TCONST(PCLK / 16, bps);
531 if (tc < 0)
532 return 0;
533 rate = TCONST_TO_BPS(PCLK / 16, tc);
534 if (ZS_TOLERANCE > abs(((rate - bps)*1000)/bps))
535 return 1;
536 else
537 return 0;
538 }
539
540 /*
541 * Search through the signal sources in the channel, and
542 * pick the best one for the baud rate requested. Return
543 * a -1 if not achievable in tolerance. Otherwise return 0
544 * and fill in the values.
545 *
546 * This routine draws inspiration from the Atari port's zs.c
547 * driver in NetBSD 1.1 which did the same type of source switching.
548 * Tolerance code inspired by comspeed routine in isa/com.c.
549 *
550 * By Bill Studenmund, 1996-05-12
551 */
552 int
553 zs_set_speed(struct zs_chanstate *cs, int bps)
554 {
555 struct xzs_chanstate *xcs = (void *) cs;
556 int i, tc, tc0 = 0, tc1, s, sf = 0;
557 int src, rate0, rate1, err, tol;
558
559 if (bps == 0)
560 return (0);
561
562 src = -1; /* no valid source yet */
563 tol = ZS_TOLERANCE;
564
565 /*
566 * Step through all the sources and see which one matches
567 * the best. A source has to match BETTER than tol to be chosen.
568 * Thus if two sources give the same error, the first one will be
569 * chosen. Also, allow for the possability that one source might run
570 * both the BRG and the direct divider (i.e. RTxC).
571 */
572 for (i=0; i < xcs->cs_clock_count; i++) {
573 if (xcs->cs_clocks[i].clk <= 0)
574 continue; /* skip non-existent or bad clocks */
575 if (xcs->cs_clocks[i].flags & ZSC_BRG) {
576 /* check out BRG at /16 */
577 tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
578 if (tc1 >= 0) {
579 rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
580 err = abs(((rate1 - bps)*1000)/bps);
581 if (err < tol) {
582 tol = err;
583 src = i;
584 sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
585 tc0 = tc1;
586 rate0 = rate1;
587 }
588 }
589 }
590 if (xcs->cs_clocks[i].flags & ZSC_DIV) {
591 /*
592 * Check out either /1, /16, /32, or /64
593 * Note: for /1, you'd better be using a synchronized
594 * clock!
595 */
596 int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
597 int b1 = b0 >> 4, e1 = abs(b1-bps);
598 int b2 = b1 >> 1, e2 = abs(b2-bps);
599 int b3 = b2 >> 1, e3 = abs(b3-bps);
600
601 if (e0 < e1 && e0 < e2 && e0 < e3) {
602 err = e0;
603 rate1 = b0;
604 tc1 = ZSWR4_CLK_X1;
605 } else if (e0 > e1 && e1 < e2 && e1 < e3) {
606 err = e1;
607 rate1 = b1;
608 tc1 = ZSWR4_CLK_X16;
609 } else if (e0 > e2 && e1 > e2 && e2 < e3) {
610 err = e2;
611 rate1 = b2;
612 tc1 = ZSWR4_CLK_X32;
613 } else {
614 err = e3;
615 rate1 = b3;
616 tc1 = ZSWR4_CLK_X64;
617 }
618
619 err = (err * 1000)/bps;
620 if (err < tol) {
621 tol = err;
622 src = i;
623 sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
624 tc0 = tc1;
625 rate0 = rate1;
626 }
627 }
628 }
629 #ifdef ZSMACDEBUG
630 zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
631 #endif
632 if (src == -1)
633 return (EINVAL); /* no can do */
634
635 /*
636 * The M.I. layer likes to keep cs_brg_clk current, even though
637 * we are the only ones who should be touching the BRG's rate.
638 *
639 * Note: we are assuming that any ZSC_EXTERN signal source comes in
640 * on the RTxC pin. Correct for the mac68k obio zsc.
641 */
642 if (sf & ZSC_EXTERN)
643 cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
644 else
645 cs->cs_brg_clk = PCLK / 16;
646
647 /*
648 * Now we have a source, so set it up.
649 */
650 s = splzs();
651 xcs->cs_psource = src;
652 xcs->cs_pclk_flag = sf;
653 bps = rate0;
654 if (sf & ZSC_BRG) {
655 cs->cs_preg[4] = ZSWR4_CLK_X16;
656 cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
657 if (sf & ZSC_PCLK) {
658 cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
659 } else {
660 cs->cs_preg[14] = ZSWR14_BAUD_ENA;
661 }
662 tc = tc0;
663 } else {
664 cs->cs_preg[4] = tc0;
665 if (sf & ZSC_RTXDIV) {
666 cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
667 } else {
668 cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
669 }
670 cs->cs_preg[14]= 0;
671 tc = 0xffff;
672 }
673 /* Set the BAUD rate divisor. */
674 cs->cs_preg[12] = tc;
675 cs->cs_preg[13] = tc >> 8;
676 splx(s);
677
678 #ifdef ZSMACDEBUG
679 zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
680 bps, tc, src, sf);
681 zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
682 cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
683 #endif
684
685 cs->cs_preg[5] |= ZSWR5_RTS; /* Make sure the drivers are on! */
686
687 /* Caller will stuff the pending registers. */
688 return (0);
689 }
690
691 int
692 zs_set_modes(struct zs_chanstate *cs, int cflag)
693 {
694 struct xzs_chanstate *xcs = (void*)cs;
695 int s;
696
697 /*
698 * Make sure we don't enable hfc on a signal line we're ignoring.
699 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
700 * this code also effectivly turns off ZSWR15_CTS_IE.
701 *
702 * Also, disable DCD interrupts if we've been told to ignore
703 * the DCD pin. Happens on mac68k because the input line for
704 * DCD can also be used as a clock input. (Just set CLOCAL.)
705 *
706 * If someone tries to turn an invalid flow mode on, Just Say No
707 * (Suggested by gwr)
708 */
709 if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
710 return (EINVAL);
711 cs->cs_rr0_pps = 0;
712 if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
713 if (cflag & MDMBUF)
714 return (EINVAL);
715 cflag |= CLOCAL;
716 } else {
717 /*
718 * cs->cs_rr0_pps indicates which bit MAY be used for pps.
719 * Enable only if nothing else will want the interrupt and
720 * it's ok to enable interrupts on this line.
721 */
722 if ((cflag & (CLOCAL | MDMBUF)) == CLOCAL)
723 cs->cs_rr0_pps = ZSRR0_DCD;
724 }
725 if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
726 return (EINVAL);
727
728 /*
729 * Output hardware flow control on the chip is horrendous:
730 * if carrier detect drops, the receiver is disabled, and if
731 * CTS drops, the transmitter is stoped IN MID CHARACTER!
732 * Therefore, NEVER set the HFC bit, and instead use the
733 * status interrupt to detect CTS changes.
734 */
735 s = splzs();
736 if ((cflag & (CLOCAL | MDMBUF)) != 0)
737 cs->cs_rr0_dcd = 0;
738 else
739 cs->cs_rr0_dcd = ZSRR0_DCD;
740 /*
741 * The mac hardware only has one output, DTR (HSKo in Mac
742 * parlance). In HFC mode, we use it for the functions
743 * typically served by RTS and DTR on other ports, so we
744 * have to fake the upper layer out some.
745 *
746 * CRTSCTS we use CTS as an input which tells us when to shut up.
747 * We make no effort to shut up the other side of the connection.
748 * DTR is used to hang up the modem.
749 *
750 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
751 * shut up the other side.
752 */
753 if ((cflag & CRTSCTS) != 0) {
754 cs->cs_wr5_dtr = ZSWR5_DTR;
755 cs->cs_wr5_rts = 0;
756 cs->cs_rr0_cts = ZSRR0_CTS;
757 } else if ((cflag & CDTRCTS) != 0) {
758 cs->cs_wr5_dtr = 0;
759 cs->cs_wr5_rts = ZSWR5_DTR;
760 cs->cs_rr0_cts = ZSRR0_CTS;
761 } else if ((cflag & MDMBUF) != 0) {
762 cs->cs_wr5_dtr = 0;
763 cs->cs_wr5_rts = ZSWR5_DTR;
764 cs->cs_rr0_cts = ZSRR0_DCD;
765 } else {
766 cs->cs_wr5_dtr = ZSWR5_DTR;
767 cs->cs_wr5_rts = 0;
768 cs->cs_rr0_cts = 0;
769 }
770 splx(s);
771
772 /* Caller will stuff the pending registers. */
773 return (0);
774 }
775
776
777 /*
778 * Read or write the chip with suitable delays.
779 * MacII hardware has the delay built in.
780 * No need for extra delay. :-) However, some clock-chirped
781 * macs, or zsc's on serial add-on boards might need it.
782 */
783 #define ZS_DELAY()
784
785 u_char
786 zs_read_reg(struct zs_chanstate *cs, u_char reg)
787 {
788 u_char val;
789
790 *cs->cs_reg_csr = reg;
791 ZS_DELAY();
792 val = *cs->cs_reg_csr;
793 ZS_DELAY();
794 return val;
795 }
796
797 void
798 zs_write_reg(struct zs_chanstate *cs, u_char reg, u_char val)
799 {
800 *cs->cs_reg_csr = reg;
801 ZS_DELAY();
802 *cs->cs_reg_csr = val;
803 ZS_DELAY();
804 }
805
806 u_char
807 zs_read_csr(struct zs_chanstate *cs)
808 {
809 u_char val;
810
811 val = *cs->cs_reg_csr;
812 ZS_DELAY();
813 /* make up for the fact CTS is wired backwards */
814 val ^= ZSRR0_CTS;
815 return val;
816 }
817
818 void
819 zs_write_csr(struct zs_chanstate *cs, u_char val)
820 {
821 /* Note, the csr does not write CTS... */
822 *cs->cs_reg_csr = val;
823 ZS_DELAY();
824 }
825
826 u_char
827 zs_read_data(struct zs_chanstate *cs)
828 {
829 u_char val;
830
831 val = *cs->cs_reg_data;
832 ZS_DELAY();
833 return val;
834 }
835
836 void
837 zs_write_data(struct zs_chanstate *cs, u_char val)
838 {
839 *cs->cs_reg_data = val;
840 ZS_DELAY();
841 }
842
843 /****************************************************************
844 * Console support functions (mac68k specific!)
845 * Note: this code is allowed to know about the layout of
846 * the chip registers, and uses that to keep things simple.
847 * XXX - I think I like the mvme167 code better. -gwr
848 * XXX - Well :-P :-) -wrs
849 ****************************************************************/
850
851 #define zscnpollc nullcnpollc
852 cons_decl(zs);
853
854 static void zscnsetup(void);
855
856 /*
857 * Console functions.
858 */
859
860 /*
861 * This code modled after the zs_setparam routine in zskgdb
862 * It sets the console unit to a known state so we can output
863 * correctly.
864 */
865 static void
866 zscnsetup(void)
867 {
868 struct xzs_chanstate xcs;
869 struct zs_chanstate *cs;
870 struct zschan *zc;
871 int tconst, s;
872
873 /* Setup temporary chanstate. */
874 memset(&xcs, 0, sizeof(xcs));
875 cs = &xcs.xzs_cs;
876 zc = zs_conschan;
877 cs->cs_reg_csr = &zc->zc_csr;
878 cs->cs_reg_data = &zc->zc_data;
879 cs->cs_channel = zs_consunit;
880 cs->cs_brg_clk = PCLK / 16;
881
882 memcpy(cs->cs_preg, zs_init_reg, 16);
883 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
884 cs->cs_preg[15] = ZSWR15_BREAK_IE;
885 tconst = BPS_TO_TCONST(cs->cs_brg_clk,
886 zs_defspeed[0][zs_consunit]);
887 cs->cs_preg[12] = tconst;
888 cs->cs_preg[13] = tconst >> 8;
889 /* can't use zs_set_speed as we haven't set up the
890 * signal sources, and it's not worth it for now
891 */
892
893 /*
894 * As zs_loadchannelregs doesn't touch reg 9 (interrupt control),
895 * we won't accidentally turn on interrupts below
896 */
897 s = splhigh();
898 zs_loadchannelregs(cs);
899 splx(s);
900 }
901
902 /*
903 * zscnprobe is the routine which gets called as the kernel is trying to
904 * figure out where the console should be. Each io driver which might
905 * be the console (as defined in mac68k/conf.c) gets probed. The probe
906 * fills in the consdev structure. Important parts are the device #,
907 * and the console priority. Values are CN_DEAD (don't touch me),
908 * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
909 * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
910 *
911 * As the mac's a bit different, we do extra work here. We mainly check
912 * to see if we have serial echo going on. Also chould check for default
913 * speeds.
914 */
915 void
916 zscnprobe(struct consdev * cp)
917 {
918 extern u_long IOBase;
919 int maj, unit, i;
920 extern const struct cdevsw zstty_cdevsw;
921
922 maj = cdevsw_lookup_major(&zstty_cdevsw);
923 if (maj != -1) {
924 cp->cn_pri = CN_NORMAL; /* Lower than CN_INTERNAL */
925 if (mac68k_machine.serial_console != 0) {
926 cp->cn_pri = CN_REMOTE; /* Higher than CN_INTERNAL */
927 mac68k_machine.serial_boot_echo =0;
928 }
929
930 unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
931 zs_consunit = unit;
932 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
933
934 mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
935 }
936 if (mac68k_machine.serial_boot_echo) {
937 /*
938 * at this point, we know that we don't have a serial
939 * console, but are doing echo
940 */
941 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
942 zs_consunit = 1;
943 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
944 }
945
946 if ((i = mac68k_machine.modem_d_speed) > 0) {
947 if (zs_cn_check_speed(i))
948 zs_defspeed[0][0] = i;
949 }
950 if ((i = mac68k_machine.print_d_speed) > 0) {
951 if (zs_cn_check_speed(i))
952 zs_defspeed[0][1] = i;
953 }
954 mac68k_set_io_offsets(IOBase);
955 zs_init();
956 /*
957 * zsinit will set up the addresses of the scc. It will also, if
958 * zs_conschan != 0, calculate the new address of the conschan for
959 * unit zs_consunit. So if we are (or think we are) going to use the
960 * chip for console I/O, we just set up the internal addresses for it.
961 *
962 * Now turn off interrupts for the chip. Note: using sccA to get at
963 * the chip is the only vestage of the NetBSD 1.0 ser driver. :-)
964 */
965 unit = sccA[2]; /* reset reg. access */
966 unit = sccA[0];
967 sccA[2] = 9; sccA[2] = 0; /* write 0 to reg. 9, clearing MIE */
968 sccA[2] = ZSWR0_CLR_INTR; unit = sccA[2]; /* reset any pending ints. */
969 sccA[0] = ZSWR0_CLR_INTR; unit = sccA[0];
970
971 if (mac68k_machine.serial_boot_echo)
972 zscnsetup();
973 return;
974 }
975
976 void
977 zscninit(struct consdev *cp)
978 {
979
980 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
981 /*
982 * zsinit will set up the addresses of the scc. It will also, if
983 * zs_conschan != 0, calculate the new address of the conschan for
984 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
985 * number. :-)
986 */
987 zscnsetup();
988 printf("\nNetBSD/mac68k console\n");
989 }
990
991
992 /*
993 * Polled input char.
994 */
995 int
996 zs_getc(void *arg)
997 {
998 volatile struct zschan *zc = arg;
999 int s, c, rr0;
1000
1001 s = splhigh();
1002 /* Wait for a character to arrive. */
1003 do {
1004 rr0 = zc->zc_csr;
1005 ZS_DELAY();
1006 } while ((rr0 & ZSRR0_RX_READY) == 0);
1007
1008 c = zc->zc_data;
1009 ZS_DELAY();
1010 splx(s);
1011
1012 /*
1013 * This is used by the kd driver to read scan codes,
1014 * so don't translate '\r' ==> '\n' here...
1015 */
1016 return (c);
1017 }
1018
1019 /*
1020 * Polled output char.
1021 */
1022 void
1023 zs_putc(void *arg, int c)
1024 {
1025 volatile struct zschan *zc = arg;
1026 int s, rr0;
1027 long wait = 0;
1028
1029 s = splhigh();
1030 /* Wait for transmitter to become ready. */
1031 do {
1032 rr0 = zc->zc_csr;
1033 ZS_DELAY();
1034 } while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
1035
1036 if ((rr0 & ZSRR0_TX_READY) != 0) {
1037 zc->zc_data = c;
1038 ZS_DELAY();
1039 }
1040 splx(s);
1041 }
1042
1043
1044 /*
1045 * Polled console input putchar.
1046 */
1047 int
1048 zscngetc(dev_t dev)
1049 {
1050 struct zschan *zc = zs_conschan;
1051 int c;
1052
1053 c = zs_getc(zc);
1054 return (c);
1055 }
1056
1057 /*
1058 * Polled console output putchar.
1059 */
1060 void
1061 zscnputc(dev_t dev, int c)
1062 {
1063 struct zschan *zc = zs_conschan;
1064
1065 zs_putc(zc, c);
1066 }
1067
1068
1069
1070 /*
1071 * Handle user request to enter kernel debugger.
1072 */
1073 void
1074 zs_abort(struct zs_chanstate *cs)
1075 {
1076 volatile struct zschan *zc = zs_conschan;
1077 int rr0;
1078 long wait = 0;
1079
1080 if (zs_cons_canabort == 0)
1081 return;
1082
1083 /* Wait for end of break to avoid PROM abort. */
1084 do {
1085 rr0 = zc->zc_csr;
1086 ZS_DELAY();
1087 } while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
1088
1089 if (wait > ZSABORT_DELAY) {
1090 zs_cons_canabort = 0;
1091 /* If we time out, turn off the abort ability! */
1092 }
1093
1094 #ifdef DDB
1095 Debugger();
1096 #endif
1097 }
1098