zs.c revision 1.16 1 /* $NetBSD: zs.c,v 1.16 2001/06/08 00:32:02 matt 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 "opt_ddb.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/device.h>
62 #include <sys/conf.h>
63 #include <sys/file.h>
64 #include <sys/ioctl.h>
65 #include <sys/tty.h>
66 #include <sys/time.h>
67 #include <sys/kernel.h>
68 #include <sys/syslog.h>
69
70 #include <dev/cons.h>
71 #include <dev/ofw/openfirm.h>
72 #include <dev/ic/z8530reg.h>
73
74 #include <machine/z8530var.h>
75 #include <machine/autoconf.h>
76 #include <machine/cpu.h>
77 #include <machine/pio.h>
78
79 /* Are these in a header file anywhere? */
80 /* Booter flags interface */
81 #define ZSMAC_RAW 0x01
82 #define ZSMAC_LOCALTALK 0x02
83
84 #define PCLK (9600 * 384)
85
86 #include "zsc.h" /* get the # of zs chips defined */
87
88 /*
89 * Some warts needed by z8530tty.c -
90 */
91 int zs_def_cflag = (CREAD | CS8 | HUPCL);
92 int zs_major = 12;
93
94 /*
95 * abort detection on console will now timeout after iterating on a loop
96 * the following # of times. Cheep hack. Also, abort detection is turned
97 * off after a timeout (i.e. maybe there's not a terminal hooked up).
98 */
99 #define ZSABORT_DELAY 3000000
100
101 /* The layout of this is hardware-dependent (padding, order). */
102 struct zschan {
103 volatile u_char zc_csr; /* ctrl,status, and indirect access */
104 u_char zc_xxx0[15];
105 volatile u_char zc_data; /* data */
106 u_char zc_xxx1[15];
107 };
108 struct zsdevice {
109 /* Yes, they are backwards. */
110 struct zschan zs_chan_b;
111 struct zschan zs_chan_a;
112 };
113
114 /* Flags from cninit() */
115 static int zs_hwflags[NZSC][2];
116 /* Default speed for each channel */
117 static int zs_defspeed[NZSC][2] = {
118 { 38400, /* tty00 */
119 38400 }, /* tty01 */
120 };
121
122 /* console stuff */
123 void *zs_conschan = 0;
124 #ifdef ZS_CONSOLE_ABORT
125 int zs_cons_canabort = 1;
126 #else
127 int zs_cons_canabort = 0;
128 #endif /* ZS_CONSOLE_ABORT*/
129
130 /* device to which the console is attached--if serial. */
131 /* Mac stuff */
132
133 static int zs_get_speed __P((struct zs_chanstate *));
134
135 /*
136 * Even though zsparam will set up the clock multiples, etc., we
137 * still set them here as: 1) mice & keyboards don't use zsparam,
138 * and 2) the console stuff uses these defaults before device
139 * attach.
140 */
141
142 static u_char zs_init_reg[16] = {
143 0, /* 0: CMD (reset, etc.) */
144 0, /* 1: No interrupts yet. */
145 0, /* IVECT */
146 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
147 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
148 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
149 0, /* 6: TXSYNC/SYNCLO */
150 0, /* 7: RXSYNC/SYNCHI */
151 0, /* 8: alias for data port */
152 ZSWR9_MASTER_IE,
153 0, /*10: Misc. TX/RX control bits */
154 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
155 ((PCLK/32)/38400)-2, /*12: BAUDLO (default=38400) */
156 0, /*13: BAUDHI (default=38400) */
157 ZSWR14_BAUD_ENA,
158 ZSWR15_BREAK_IE,
159 };
160
161 /****************************************************************
162 * Autoconfig
163 ****************************************************************/
164
165 /* Definition of the driver for autoconfig. */
166 static int zsc_match __P((struct device *, struct cfdata *, void *));
167 static void zsc_attach __P((struct device *, struct device *, void *));
168 static int zsc_print __P((void *, const char *name));
169
170 struct cfattach zsc_ca = {
171 sizeof(struct zsc_softc), zsc_match, zsc_attach
172 };
173
174 extern struct cfdriver zsc_cd;
175
176 int zshard __P((void *));
177 int zssoft __P((void *));
178 #ifdef ZS_TXDMA
179 static int zs_txdma_int __P((void *));
180 #endif
181
182 void zscnprobe __P((struct consdev *));
183 void zscninit __P((struct consdev *));
184 int zscngetc __P((dev_t));
185 void zscnputc __P((dev_t, int));
186 void zscnpollc __P((dev_t, int));
187
188 /*
189 * Is the zs chip present?
190 */
191 static int
192 zsc_match(parent, cf, aux)
193 struct device *parent;
194 struct cfdata *cf;
195 void *aux;
196 {
197 struct confargs *ca = aux;
198 int unit = cf->cf_unit;
199
200 if (strcmp(ca->ca_name, "escc") != 0)
201 return 0;
202
203 if (unit > 1)
204 return 0;
205
206 return 1;
207 }
208
209 /*
210 * Attach a found zs.
211 *
212 * Match slave number to zs unit number, so that misconfiguration will
213 * not set up the keyboard as ttya, etc.
214 */
215 static void
216 zsc_attach(parent, self, aux)
217 struct device *parent;
218 struct device *self;
219 void *aux;
220 {
221 struct zsc_softc *zsc = (void *)self;
222 struct confargs *ca = aux;
223 struct zsc_attach_args zsc_args;
224 volatile struct zschan *zc;
225 struct xzs_chanstate *xcs;
226 struct zs_chanstate *cs;
227 struct zsdevice *zsd;
228 int zsc_unit, channel;
229 int s, chip, theflags;
230 int node, intr[2][3];
231 u_int regs[6];
232
233 chip = 0;
234 zsc_unit = zsc->zsc_dev.dv_unit;
235
236 ca->ca_reg[0] += ca->ca_baseaddr;
237 zsd = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
238
239 node = OF_child(ca->ca_node); /* ch-a */
240
241 for (channel = 0; channel < 2; channel++) {
242 if (OF_getprop(node, "AAPL,interrupts",
243 intr[channel], sizeof(intr[0])) == -1 &&
244 OF_getprop(node, "interrupts",
245 intr[channel], sizeof(intr[0])) == -1) {
246 printf(": cannot find interrupt property\n");
247 return;
248 }
249
250 if (OF_getprop(node, "reg", regs, sizeof(regs)) < 24) {
251 printf(": cannot find reg property\n");
252 return;
253 }
254 regs[2] += ca->ca_baseaddr;
255 regs[4] += ca->ca_baseaddr;
256 #ifdef ZS_TXDMA
257 zsc->zsc_txdmareg[channel] = mapiodev(regs[2], regs[3]);
258 zsc->zsc_txdmacmd[channel] =
259 dbdma_alloc(sizeof(dbdma_command_t) * 3);
260 bzero(zsc->zsc_txdmacmd[channel], sizeof(dbdma_command_t) * 3);
261 dbdma_reset(zsc->zsc_txdmareg[channel]);
262 #endif
263 node = OF_peer(node); /* ch-b */
264 }
265
266 printf(": irq %d,%d\n", intr[0][0], intr[1][0]);
267
268 /*
269 * Initialize software state for each channel.
270 */
271 for (channel = 0; channel < 2; channel++) {
272 zsc_args.channel = channel;
273 zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
274 xcs = &zsc->xzsc_xcs_store[channel];
275 cs = &xcs->xzs_cs;
276 zsc->zsc_cs[channel] = cs;
277
278 cs->cs_channel = channel;
279 cs->cs_private = NULL;
280 cs->cs_ops = &zsops_null;
281
282 zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
283
284 cs->cs_reg_csr = &zc->zc_csr;
285 cs->cs_reg_data = &zc->zc_data;
286
287 bcopy(zs_init_reg, cs->cs_creg, 16);
288 bcopy(zs_init_reg, cs->cs_preg, 16);
289
290 /* Current BAUD rate generator clock. */
291 cs->cs_brg_clk = PCLK / 16; /* RTxC is 230400*16, so use 230400 */
292 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
293 cs->cs_defspeed = zs_get_speed(cs);
294 else
295 cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
296 cs->cs_defcflag = zs_def_cflag;
297
298 /* Make these correspond to cs_defcflag (-crtscts) */
299 cs->cs_rr0_dcd = ZSRR0_DCD;
300 cs->cs_rr0_cts = 0;
301 cs->cs_wr5_dtr = ZSWR5_DTR;
302 cs->cs_wr5_rts = 0;
303
304 #ifdef __notyet__
305 cs->cs_slave_type = ZS_SLAVE_NONE;
306 #endif
307
308 /* Define BAUD rate stuff. */
309 xcs->cs_clocks[0].clk = PCLK;
310 xcs->cs_clocks[0].flags = ZSC_RTXBRG | ZSC_RTXDIV;
311 xcs->cs_clocks[1].flags =
312 ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
313 xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
314 xcs->cs_clock_count = 3;
315 if (channel == 0) {
316 theflags = 0; /*mac68k_machine.modem_flags;*/
317 /*xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;*/
318 /*xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;*/
319 xcs->cs_clocks[1].clk = 0;
320 xcs->cs_clocks[2].clk = 0;
321 } else {
322 theflags = 0; /*mac68k_machine.print_flags;*/
323 xcs->cs_clocks[1].flags = ZSC_VARIABLE;
324 /*
325 * Yes, we aren't defining ANY clock source enables for the
326 * printer's DCD clock in. The hardware won't let us
327 * use it. But a clock will freak out the chip, so we
328 * let you set it, telling us to bar interrupts on the line.
329 */
330 /*xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;*/
331 /*xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;*/
332 xcs->cs_clocks[1].clk = 0;
333 xcs->cs_clocks[2].clk = 0;
334 }
335 if (xcs->cs_clocks[1].clk)
336 zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
337 if (xcs->cs_clocks[2].clk)
338 zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
339
340 /* Set defaults in our "extended" chanstate. */
341 xcs->cs_csource = 0;
342 xcs->cs_psource = 0;
343 xcs->cs_cclk_flag = 0; /* Nothing fancy by default */
344 xcs->cs_pclk_flag = 0;
345
346 if (theflags & ZSMAC_RAW) {
347 zsc_args.hwflags |= ZS_HWFLAG_RAW;
348 printf(" (raw defaults)");
349 }
350
351 /*
352 * XXX - This might be better done with a "stub" driver
353 * (to replace zstty) that ignores LocalTalk for now.
354 */
355 if (theflags & ZSMAC_LOCALTALK) {
356 printf(" shielding from LocalTalk");
357 cs->cs_defspeed = 1;
358 cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
359 cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
360 zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
361 zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
362 /*
363 * If we might have LocalTalk, then make sure we have the
364 * Baud rate low-enough to not do any damage.
365 */
366 }
367
368 /*
369 * We used to disable chip interrupts here, but we now
370 * do that in zscnprobe, just in case MacOS left the chip on.
371 */
372
373 xcs->cs_chip = chip;
374
375 /* Stash away a copy of the final H/W flags. */
376 xcs->cs_hwflags = zsc_args.hwflags;
377
378 /*
379 * Look for a child driver for this channel.
380 * The child attach will setup the hardware.
381 */
382 if (!config_found(self, (void *)&zsc_args, zsc_print)) {
383 /* No sub-driver. Just reset it. */
384 u_char reset = (channel == 0) ?
385 ZSWR9_A_RESET : ZSWR9_B_RESET;
386 s = splzs();
387 zs_write_reg(cs, 9, reset);
388 splx(s);
389 }
390 }
391
392 /* XXX - Now safe to install interrupt handlers. */
393 intr_establish(intr[0][0], IST_LEVEL, IPL_TTY, zshard, NULL);
394 intr_establish(intr[1][0], IST_LEVEL, IPL_TTY, zshard, NULL);
395 #ifdef ZS_TXDMA
396 intr_establish(intr[0][1], IST_LEVEL, IPL_TTY, zs_txdma_int, (void *)0);
397 intr_establish(intr[1][1], IST_LEVEL, IPL_TTY, zs_txdma_int, (void *)1);
398 #endif
399
400 /*
401 * Set the master interrupt enable and interrupt vector.
402 * (common to both channels, do it on A)
403 */
404 cs = zsc->zsc_cs[0];
405 s = splzs();
406 /* interrupt vector */
407 zs_write_reg(cs, 2, zs_init_reg[2]);
408 /* master interrupt control (enable) */
409 zs_write_reg(cs, 9, zs_init_reg[9]);
410 splx(s);
411 }
412
413 static int
414 zsc_print(aux, name)
415 void *aux;
416 const char *name;
417 {
418 struct zsc_attach_args *args = aux;
419
420 if (name != NULL)
421 printf("%s: ", name);
422
423 if (args->channel != -1)
424 printf(" channel %d", args->channel);
425
426 return UNCONF;
427 }
428
429 int
430 zsmdioctl(cs, cmd, data)
431 struct zs_chanstate *cs;
432 u_long cmd;
433 caddr_t data;
434 {
435 switch (cmd) {
436 default:
437 return (-1);
438 }
439 return (0);
440 }
441
442 void
443 zsmd_setclock(cs)
444 struct zs_chanstate *cs;
445 {
446 #ifdef NOTYET
447 struct xzs_chanstate *xcs = (void *)cs;
448
449 if (cs->cs_channel != 0)
450 return;
451
452 /*
453 * If the new clock has the external bit set, then select the
454 * external source.
455 */
456 via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
457 #endif
458 }
459
460 static int zssoftpending;
461
462 /*
463 * Our ZS chips all share a common, autovectored interrupt,
464 * so we have to look at all of them on each interrupt.
465 */
466 int
467 zshard(arg)
468 void *arg;
469 {
470 register struct zsc_softc *zsc;
471 register int unit, rval;
472
473 rval = 0;
474 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
475 zsc = zsc_cd.cd_devs[unit];
476 if (zsc == NULL)
477 continue;
478 rval |= zsc_intr_hard(zsc);
479 if ((zsc->zsc_cs[0]->cs_softreq) ||
480 (zsc->zsc_cs[1]->cs_softreq))
481 {
482 /* zsc_req_softint(zsc); */
483 /* We are at splzs here, so no need to lock. */
484 if (zssoftpending == 0) {
485 zssoftpending = 1;
486 setsoftserial();
487 }
488 }
489 }
490 return (rval);
491 }
492
493 /*
494 * Similar scheme as for zshard (look at all of them)
495 */
496 int
497 zssoft(arg)
498 void *arg;
499 {
500 register struct zsc_softc *zsc;
501 register int unit;
502
503 /* This is not the only ISR on this IPL. */
504 if (zssoftpending == 0)
505 return (0);
506
507 /*
508 * The soft intr. bit will be set by zshard only if
509 * the variable zssoftpending is zero.
510 */
511 zssoftpending = 0;
512
513 for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
514 zsc = zsc_cd.cd_devs[unit];
515 if (zsc == NULL)
516 continue;
517 (void) zsc_intr_soft(zsc);
518 }
519 return (1);
520 }
521
522 #ifdef ZS_TXDMA
523 int
524 zs_txdma_int(arg)
525 void *arg;
526 {
527 int ch = (int)arg;
528 struct zsc_softc *zsc;
529 struct zs_chanstate *cs;
530 int unit = 0; /* XXX */
531 extern int zstty_txdma_int();
532
533 zsc = zsc_cd.cd_devs[unit];
534 if (zsc == NULL)
535 panic("zs_txdma_int");
536
537 cs = zsc->zsc_cs[ch];
538 zstty_txdma_int(cs);
539
540 if (cs->cs_softreq) {
541 if (zssoftpending == 0) {
542 zssoftpending = 1;
543 setsoftserial();
544 }
545 }
546 return 1;
547 }
548
549 void
550 zs_dma_setup(cs, pa, len)
551 struct zs_chanstate *cs;
552 caddr_t pa;
553 int len;
554 {
555 struct zsc_softc *zsc;
556 dbdma_command_t *cmdp;
557 int ch = cs->cs_channel;
558
559 zsc = zsc_cd.cd_devs[ch];
560 cmdp = zsc->zsc_txdmacmd[ch];
561
562 DBDMA_BUILD(cmdp, DBDMA_CMD_OUT_LAST, 0, len, kvtop(pa),
563 DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
564 cmdp++;
565 DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
566 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
567
568 __asm __volatile("eieio");
569
570 dbdma_start(zsc->zsc_txdmareg[ch], zsc->zsc_txdmacmd[ch]);
571 }
572 #endif
573
574 /*
575 * Compute the current baud rate given a ZS channel.
576 * XXX Assume internal BRG.
577 */
578 int
579 zs_get_speed(cs)
580 struct zs_chanstate *cs;
581 {
582 int tconst;
583
584 tconst = zs_read_reg(cs, 12);
585 tconst |= zs_read_reg(cs, 13) << 8;
586 return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
587 }
588
589 #ifndef ZS_TOLERANCE
590 #define ZS_TOLERANCE 51
591 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
592 #endif
593
594 /*
595 * Search through the signal sources in the channel, and
596 * pick the best one for the baud rate requested. Return
597 * a -1 if not achievable in tolerance. Otherwise return 0
598 * and fill in the values.
599 *
600 * This routine draws inspiration from the Atari port's zs.c
601 * driver in NetBSD 1.1 which did the same type of source switching.
602 * Tolerance code inspired by comspeed routine in isa/com.c.
603 *
604 * By Bill Studenmund, 1996-05-12
605 */
606 int
607 zs_set_speed(cs, bps)
608 struct zs_chanstate *cs;
609 int bps; /* bits per second */
610 {
611 struct xzs_chanstate *xcs = (void *) cs;
612 int i, tc, tc0 = 0, tc1, s, sf = 0;
613 int src, rate0, rate1, err, tol;
614
615 if (bps == 0)
616 return (0);
617
618 src = -1; /* no valid source yet */
619 tol = ZS_TOLERANCE;
620
621 /*
622 * Step through all the sources and see which one matches
623 * the best. A source has to match BETTER than tol to be chosen.
624 * Thus if two sources give the same error, the first one will be
625 * chosen. Also, allow for the possability that one source might run
626 * both the BRG and the direct divider (i.e. RTxC).
627 */
628 for (i = 0; i < xcs->cs_clock_count; i++) {
629 if (xcs->cs_clocks[i].clk <= 0)
630 continue; /* skip non-existant or bad clocks */
631 if (xcs->cs_clocks[i].flags & ZSC_BRG) {
632 /* check out BRG at /16 */
633 tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
634 if (tc1 >= 0) {
635 rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
636 err = abs(((rate1 - bps)*1000)/bps);
637 if (err < tol) {
638 tol = err;
639 src = i;
640 sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
641 tc0 = tc1;
642 rate0 = rate1;
643 }
644 }
645 }
646 if (xcs->cs_clocks[i].flags & ZSC_DIV) {
647 /*
648 * Check out either /1, /16, /32, or /64
649 * Note: for /1, you'd better be using a synchronized
650 * clock!
651 */
652 int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
653 int b1 = b0 >> 4, e1 = abs(b1-bps);
654 int b2 = b1 >> 1, e2 = abs(b2-bps);
655 int b3 = b2 >> 1, e3 = abs(b3-bps);
656
657 if (e0 < e1 && e0 < e2 && e0 < e3) {
658 err = e0;
659 rate1 = b0;
660 tc1 = ZSWR4_CLK_X1;
661 } else if (e0 > e1 && e1 < e2 && e1 < e3) {
662 err = e1;
663 rate1 = b1;
664 tc1 = ZSWR4_CLK_X16;
665 } else if (e0 > e2 && e1 > e2 && e2 < e3) {
666 err = e2;
667 rate1 = b2;
668 tc1 = ZSWR4_CLK_X32;
669 } else {
670 err = e3;
671 rate1 = b3;
672 tc1 = ZSWR4_CLK_X64;
673 }
674
675 err = (err * 1000)/bps;
676 if (err < tol) {
677 tol = err;
678 src = i;
679 sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
680 tc0 = tc1;
681 rate0 = rate1;
682 }
683 }
684 }
685 #ifdef ZSMACDEBUG
686 zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
687 #endif
688 if (src == -1)
689 return (EINVAL); /* no can do */
690
691 /*
692 * The M.I. layer likes to keep cs_brg_clk current, even though
693 * we are the only ones who should be touching the BRG's rate.
694 *
695 * Note: we are assuming that any ZSC_EXTERN signal source comes in
696 * on the RTxC pin. Correct for the mac68k obio zsc.
697 */
698 if (sf & ZSC_EXTERN)
699 cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
700 else
701 cs->cs_brg_clk = PCLK / 16;
702
703 /*
704 * Now we have a source, so set it up.
705 */
706 s = splzs();
707 xcs->cs_psource = src;
708 xcs->cs_pclk_flag = sf;
709 bps = rate0;
710 if (sf & ZSC_BRG) {
711 cs->cs_preg[4] = ZSWR4_CLK_X16;
712 cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
713 if (sf & ZSC_PCLK) {
714 cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
715 } else {
716 cs->cs_preg[14] = ZSWR14_BAUD_ENA;
717 }
718 tc = tc0;
719 } else {
720 cs->cs_preg[4] = tc0;
721 if (sf & ZSC_RTXDIV) {
722 cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
723 } else {
724 cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
725 }
726 cs->cs_preg[14]= 0;
727 tc = 0xffff;
728 }
729 /* Set the BAUD rate divisor. */
730 cs->cs_preg[12] = tc;
731 cs->cs_preg[13] = tc >> 8;
732 splx(s);
733
734 #ifdef ZSMACDEBUG
735 zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
736 bps, tc, src, sf);
737 zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
738 cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
739 #endif
740
741 cs->cs_preg[5] |= ZSWR5_RTS; /* Make sure the drivers are on! */
742
743 /* Caller will stuff the pending registers. */
744 return (0);
745 }
746
747 int
748 zs_set_modes(cs, cflag)
749 struct zs_chanstate *cs;
750 int cflag; /* bits per second */
751 {
752 struct xzs_chanstate *xcs = (void*)cs;
753 int s;
754
755 /*
756 * Make sure we don't enable hfc on a signal line we're ignoring.
757 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
758 * this code also effectivly turns off ZSWR15_CTS_IE.
759 *
760 * Also, disable DCD interrupts if we've been told to ignore
761 * the DCD pin. Happens on mac68k because the input line for
762 * DCD can also be used as a clock input. (Just set CLOCAL.)
763 *
764 * If someone tries to turn an invalid flow mode on, Just Say No
765 * (Suggested by gwr)
766 */
767 if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
768 return (EINVAL);
769 if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
770 if (cflag & MDMBUF)
771 return (EINVAL);
772 cflag |= CLOCAL;
773 }
774 if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
775 return (EINVAL);
776
777 /*
778 * Output hardware flow control on the chip is horrendous:
779 * if carrier detect drops, the receiver is disabled, and if
780 * CTS drops, the transmitter is stoped IN MID CHARACTER!
781 * Therefore, NEVER set the HFC bit, and instead use the
782 * status interrupt to detect CTS changes.
783 */
784 s = splzs();
785 if ((cflag & (CLOCAL | MDMBUF)) != 0)
786 cs->cs_rr0_dcd = 0;
787 else
788 cs->cs_rr0_dcd = ZSRR0_DCD;
789 /*
790 * The mac hardware only has one output, DTR (HSKo in Mac
791 * parlance). In HFC mode, we use it for the functions
792 * typically served by RTS and DTR on other ports, so we
793 * have to fake the upper layer out some.
794 *
795 * CRTSCTS we use CTS as an input which tells us when to shut up.
796 * We make no effort to shut up the other side of the connection.
797 * DTR is used to hang up the modem.
798 *
799 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
800 * shut up the other side.
801 */
802 if ((cflag & CRTSCTS) != 0) {
803 cs->cs_wr5_dtr = ZSWR5_DTR;
804 cs->cs_wr5_rts = 0;
805 cs->cs_rr0_cts = ZSRR0_CTS;
806 } else if ((cflag & CDTRCTS) != 0) {
807 cs->cs_wr5_dtr = 0;
808 cs->cs_wr5_rts = ZSWR5_DTR;
809 cs->cs_rr0_cts = ZSRR0_CTS;
810 } else if ((cflag & MDMBUF) != 0) {
811 cs->cs_wr5_dtr = 0;
812 cs->cs_wr5_rts = ZSWR5_DTR;
813 cs->cs_rr0_cts = ZSRR0_DCD;
814 } else {
815 cs->cs_wr5_dtr = ZSWR5_DTR;
816 cs->cs_wr5_rts = 0;
817 cs->cs_rr0_cts = 0;
818 }
819 splx(s);
820
821 /* Caller will stuff the pending registers. */
822 return (0);
823 }
824
825
826 /*
827 * Read or write the chip with suitable delays.
828 * MacII hardware has the delay built in.
829 * No need for extra delay. :-) However, some clock-chirped
830 * macs, or zsc's on serial add-on boards might need it.
831 */
832 #define ZS_DELAY()
833
834 u_char
835 zs_read_reg(cs, reg)
836 struct zs_chanstate *cs;
837 u_char reg;
838 {
839 u_char val;
840
841 out8(cs->cs_reg_csr, reg);
842 ZS_DELAY();
843 val = in8(cs->cs_reg_csr);
844 ZS_DELAY();
845 return val;
846 }
847
848 void
849 zs_write_reg(cs, reg, val)
850 struct zs_chanstate *cs;
851 u_char reg, val;
852 {
853 out8(cs->cs_reg_csr, reg);
854 ZS_DELAY();
855 out8(cs->cs_reg_csr, val);
856 ZS_DELAY();
857 }
858
859 u_char zs_read_csr(cs)
860 struct zs_chanstate *cs;
861 {
862 register u_char val;
863
864 val = in8(cs->cs_reg_csr);
865 ZS_DELAY();
866 /* make up for the fact CTS is wired backwards */
867 val ^= ZSRR0_CTS;
868 return val;
869 }
870
871 void zs_write_csr(cs, val)
872 struct zs_chanstate *cs;
873 u_char val;
874 {
875 /* Note, the csr does not write CTS... */
876 out8(cs->cs_reg_csr, val);
877 ZS_DELAY();
878 }
879
880 u_char zs_read_data(cs)
881 struct zs_chanstate *cs;
882 {
883 register u_char val;
884
885 val = in8(cs->cs_reg_data);
886 ZS_DELAY();
887 return val;
888 }
889
890 void zs_write_data(cs, val)
891 struct zs_chanstate *cs;
892 u_char val;
893 {
894 out8(cs->cs_reg_data, val);
895 ZS_DELAY();
896 }
897
898 /****************************************************************
899 * Console support functions (powermac specific!)
900 * Note: this code is allowed to know about the layout of
901 * the chip registers, and uses that to keep things simple.
902 * XXX - I think I like the mvme167 code better. -gwr
903 * XXX - Well :-P :-) -wrs
904 ****************************************************************/
905
906 #define zscnpollc nullcnpollc
907 cons_decl(zs);
908
909 static void zs_putc __P((register volatile struct zschan *, int));
910 static int zs_getc __P((register volatile struct zschan *));
911 extern int zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
912
913 static int stdin, stdout;
914
915 /*
916 * Console functions.
917 */
918
919 /*
920 * zscnprobe is the routine which gets called as the kernel is trying to
921 * figure out where the console should be. Each io driver which might
922 * be the console (as defined in mac68k/conf.c) gets probed. The probe
923 * fills in the consdev structure. Important parts are the device #,
924 * and the console priority. Values are CN_DEAD (don't touch me),
925 * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
926 * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
927 *
928 * As the mac's a bit different, we do extra work here. We mainly check
929 * to see if we have serial echo going on. Also chould check for default
930 * speeds.
931 */
932
933 /*
934 * Polled input char.
935 */
936 int
937 zs_getc(zc)
938 register volatile struct zschan *zc;
939 {
940 register int s, c, rr0;
941
942 s = splhigh();
943 /* Wait for a character to arrive. */
944 do {
945 rr0 = in8(&zc->zc_csr);
946 ZS_DELAY();
947 } while ((rr0 & ZSRR0_RX_READY) == 0);
948
949 c = in8(&zc->zc_data);
950 ZS_DELAY();
951 splx(s);
952
953 /*
954 * This is used by the kd driver to read scan codes,
955 * so don't translate '\r' ==> '\n' here...
956 */
957 return (c);
958 }
959
960 /*
961 * Polled output char.
962 */
963 void
964 zs_putc(zc, c)
965 register volatile struct zschan *zc;
966 int c;
967 {
968 register int s, rr0;
969 register long wait = 0;
970
971 s = splhigh();
972 /* Wait for transmitter to become ready. */
973 do {
974 rr0 = in8(&zc->zc_csr);
975 ZS_DELAY();
976 } while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
977
978 if ((rr0 & ZSRR0_TX_READY) != 0) {
979 out8(&zc->zc_data, c);
980 ZS_DELAY();
981 }
982 splx(s);
983 }
984
985
986 /*
987 * Polled console input putchar.
988 */
989 int
990 zscngetc(dev)
991 dev_t dev;
992 {
993 register volatile struct zschan *zc = zs_conschan;
994 register int c;
995
996 if (zc) {
997 c = zs_getc(zc);
998 } else {
999 char ch = 0;
1000 OF_read(stdin, &ch, 1);
1001 c = ch;
1002 }
1003 return c;
1004 }
1005
1006 /*
1007 * Polled console output putchar.
1008 */
1009 void
1010 zscnputc(dev, c)
1011 dev_t dev;
1012 int c;
1013 {
1014 register volatile struct zschan *zc = zs_conschan;
1015
1016 if (zc) {
1017 zs_putc(zc, c);
1018 } else {
1019 char ch = c;
1020 OF_write(stdout, &ch, 1);
1021 }
1022 }
1023
1024 /*
1025 * Handle user request to enter kernel debugger.
1026 */
1027 void
1028 zs_abort(cs)
1029 struct zs_chanstate *cs;
1030 {
1031 volatile struct zschan *zc = zs_conschan;
1032 int rr0;
1033 register long wait = 0;
1034
1035 if (zs_cons_canabort == 0)
1036 return;
1037
1038 /* Wait for end of break to avoid PROM abort. */
1039 do {
1040 rr0 = in8(&zc->zc_csr);
1041 ZS_DELAY();
1042 } while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
1043
1044 if (wait > ZSABORT_DELAY) {
1045 zs_cons_canabort = 0;
1046 /* If we time out, turn off the abort ability! */
1047 }
1048
1049 #ifdef DDB
1050 Debugger();
1051 #endif
1052 }
1053
1054 extern int ofccngetc __P((dev_t));
1055 extern void ofccnputc __P((dev_t, int));
1056
1057 struct consdev consdev_zs = {
1058 zscnprobe,
1059 zscninit,
1060 zscngetc,
1061 zscnputc,
1062 zscnpollc,
1063 NULL,
1064 };
1065
1066 void
1067 zscnprobe(cp)
1068 struct consdev *cp;
1069 {
1070 int chosen, pkg;
1071 int unit = 0;
1072 char name[16];
1073
1074 if ((chosen = OF_finddevice("/chosen")) == -1)
1075 return;
1076
1077 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
1078 return;
1079 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
1080 return;
1081
1082 if ((pkg = OF_instance_to_package(stdin)) == -1)
1083 return;
1084
1085 bzero(name, sizeof(name));
1086 if (OF_getprop(pkg, "device_type", name, sizeof(name)) == -1)
1087 return;
1088
1089 if (strcmp(name, "serial") != 0)
1090 return;
1091
1092 bzero(name, sizeof(name));
1093 if (OF_getprop(pkg, "name", name, sizeof(name)) == -1)
1094 return;
1095
1096 if (strcmp(name, "ch-b") == 0)
1097 unit = 1;
1098
1099 cp->cn_dev = makedev(zs_major, unit);
1100 cp->cn_pri = CN_REMOTE;
1101 }
1102
1103 void
1104 zscninit(cp)
1105 struct consdev *cp;
1106 {
1107 int escc, escc_ch, obio, zs_offset;
1108 int ch = 0;
1109 u_int32_t reg[5];
1110 char name[16];
1111
1112 if ((escc_ch = OF_instance_to_package(stdin)) == -1)
1113 return;
1114
1115 bzero(name, sizeof(name));
1116 if (OF_getprop(escc_ch, "name", name, sizeof(name)) == -1)
1117 return;
1118
1119 if (strcmp(name, "ch-b") == 0)
1120 ch = 1;
1121
1122 if (OF_getprop(escc_ch, "reg", reg, sizeof(reg)) < 4)
1123 return;
1124 zs_offset = reg[0];
1125
1126 escc = OF_parent(escc_ch);
1127 obio = OF_parent(escc);
1128
1129 if (OF_getprop(obio, "assigned-addresses", reg, sizeof(reg)) < 12)
1130 return;
1131 zs_conschan = (void *)(reg[2] + zs_offset);
1132
1133 zs_hwflags[0][ch] = ZS_HWFLAG_CONSOLE;
1134 }
1135