zs.c revision 1.15 1 /* $NetBSD: zs.c,v 1.15 2000/06/07 17:37:07 tsubai 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 struct xzs_chanstate *xcs = (void *)cs;
447
448 if (cs->cs_channel != 0)
449 return;
450
451 /*
452 * If the new clock has the external bit set, then select the
453 * external source.
454 */
455 /*via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);*/
456 }
457
458 static int zssoftpending;
459
460 /*
461 * Our ZS chips all share a common, autovectored interrupt,
462 * so we have to look at all of them on each interrupt.
463 */
464 int
465 zshard(arg)
466 void *arg;
467 {
468 register struct zsc_softc *zsc;
469 register int unit, rval;
470
471 rval = 0;
472 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
473 zsc = zsc_cd.cd_devs[unit];
474 if (zsc == NULL)
475 continue;
476 rval |= zsc_intr_hard(zsc);
477 if ((zsc->zsc_cs[0]->cs_softreq) ||
478 (zsc->zsc_cs[1]->cs_softreq))
479 {
480 /* zsc_req_softint(zsc); */
481 /* We are at splzs here, so no need to lock. */
482 if (zssoftpending == 0) {
483 zssoftpending = 1;
484 setsoftserial();
485 }
486 }
487 }
488 return (rval);
489 }
490
491 /*
492 * Similar scheme as for zshard (look at all of them)
493 */
494 int
495 zssoft(arg)
496 void *arg;
497 {
498 register struct zsc_softc *zsc;
499 register int unit;
500
501 /* This is not the only ISR on this IPL. */
502 if (zssoftpending == 0)
503 return (0);
504
505 /*
506 * The soft intr. bit will be set by zshard only if
507 * the variable zssoftpending is zero.
508 */
509 zssoftpending = 0;
510
511 for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
512 zsc = zsc_cd.cd_devs[unit];
513 if (zsc == NULL)
514 continue;
515 (void) zsc_intr_soft(zsc);
516 }
517 return (1);
518 }
519
520 #ifdef ZS_TXDMA
521 int
522 zs_txdma_int(arg)
523 void *arg;
524 {
525 int ch = (int)arg;
526 struct zsc_softc *zsc;
527 struct zs_chanstate *cs;
528 int unit = 0; /* XXX */
529 extern int zstty_txdma_int();
530
531 zsc = zsc_cd.cd_devs[unit];
532 if (zsc == NULL)
533 panic("zs_txdma_int");
534
535 cs = zsc->zsc_cs[ch];
536 zstty_txdma_int(cs);
537
538 if (cs->cs_softreq) {
539 if (zssoftpending == 0) {
540 zssoftpending = 1;
541 setsoftserial();
542 }
543 }
544 return 1;
545 }
546
547 void
548 zs_dma_setup(cs, pa, len)
549 struct zs_chanstate *cs;
550 caddr_t pa;
551 int len;
552 {
553 struct zsc_softc *zsc;
554 dbdma_command_t *cmdp;
555 int ch = cs->cs_channel;
556
557 zsc = zsc_cd.cd_devs[ch];
558 cmdp = zsc->zsc_txdmacmd[ch];
559
560 DBDMA_BUILD(cmdp, DBDMA_CMD_OUT_LAST, 0, len, kvtop(pa),
561 DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
562 cmdp++;
563 DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
564 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
565
566 __asm __volatile("eieio");
567
568 dbdma_start(zsc->zsc_txdmareg[ch], zsc->zsc_txdmacmd[ch]);
569 }
570 #endif
571
572 /*
573 * Compute the current baud rate given a ZS channel.
574 * XXX Assume internal BRG.
575 */
576 int
577 zs_get_speed(cs)
578 struct zs_chanstate *cs;
579 {
580 int tconst;
581
582 tconst = zs_read_reg(cs, 12);
583 tconst |= zs_read_reg(cs, 13) << 8;
584 return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
585 }
586
587 #ifndef ZS_TOLERANCE
588 #define ZS_TOLERANCE 51
589 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
590 #endif
591
592 /*
593 * Search through the signal sources in the channel, and
594 * pick the best one for the baud rate requested. Return
595 * a -1 if not achievable in tolerance. Otherwise return 0
596 * and fill in the values.
597 *
598 * This routine draws inspiration from the Atari port's zs.c
599 * driver in NetBSD 1.1 which did the same type of source switching.
600 * Tolerance code inspired by comspeed routine in isa/com.c.
601 *
602 * By Bill Studenmund, 1996-05-12
603 */
604 int
605 zs_set_speed(cs, bps)
606 struct zs_chanstate *cs;
607 int bps; /* bits per second */
608 {
609 struct xzs_chanstate *xcs = (void *) cs;
610 int i, tc, tc0 = 0, tc1, s, sf = 0;
611 int src, rate0, rate1, err, tol;
612
613 if (bps == 0)
614 return (0);
615
616 src = -1; /* no valid source yet */
617 tol = ZS_TOLERANCE;
618
619 /*
620 * Step through all the sources and see which one matches
621 * the best. A source has to match BETTER than tol to be chosen.
622 * Thus if two sources give the same error, the first one will be
623 * chosen. Also, allow for the possability that one source might run
624 * both the BRG and the direct divider (i.e. RTxC).
625 */
626 for (i = 0; i < xcs->cs_clock_count; i++) {
627 if (xcs->cs_clocks[i].clk <= 0)
628 continue; /* skip non-existant or bad clocks */
629 if (xcs->cs_clocks[i].flags & ZSC_BRG) {
630 /* check out BRG at /16 */
631 tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
632 if (tc1 >= 0) {
633 rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
634 err = abs(((rate1 - bps)*1000)/bps);
635 if (err < tol) {
636 tol = err;
637 src = i;
638 sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
639 tc0 = tc1;
640 rate0 = rate1;
641 }
642 }
643 }
644 if (xcs->cs_clocks[i].flags & ZSC_DIV) {
645 /*
646 * Check out either /1, /16, /32, or /64
647 * Note: for /1, you'd better be using a synchronized
648 * clock!
649 */
650 int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
651 int b1 = b0 >> 4, e1 = abs(b1-bps);
652 int b2 = b1 >> 1, e2 = abs(b2-bps);
653 int b3 = b2 >> 1, e3 = abs(b3-bps);
654
655 if (e0 < e1 && e0 < e2 && e0 < e3) {
656 err = e0;
657 rate1 = b0;
658 tc1 = ZSWR4_CLK_X1;
659 } else if (e0 > e1 && e1 < e2 && e1 < e3) {
660 err = e1;
661 rate1 = b1;
662 tc1 = ZSWR4_CLK_X16;
663 } else if (e0 > e2 && e1 > e2 && e2 < e3) {
664 err = e2;
665 rate1 = b2;
666 tc1 = ZSWR4_CLK_X32;
667 } else {
668 err = e3;
669 rate1 = b3;
670 tc1 = ZSWR4_CLK_X64;
671 }
672
673 err = (err * 1000)/bps;
674 if (err < tol) {
675 tol = err;
676 src = i;
677 sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
678 tc0 = tc1;
679 rate0 = rate1;
680 }
681 }
682 }
683 #ifdef ZSMACDEBUG
684 zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
685 #endif
686 if (src == -1)
687 return (EINVAL); /* no can do */
688
689 /*
690 * The M.I. layer likes to keep cs_brg_clk current, even though
691 * we are the only ones who should be touching the BRG's rate.
692 *
693 * Note: we are assuming that any ZSC_EXTERN signal source comes in
694 * on the RTxC pin. Correct for the mac68k obio zsc.
695 */
696 if (sf & ZSC_EXTERN)
697 cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
698 else
699 cs->cs_brg_clk = PCLK / 16;
700
701 /*
702 * Now we have a source, so set it up.
703 */
704 s = splzs();
705 xcs->cs_psource = src;
706 xcs->cs_pclk_flag = sf;
707 bps = rate0;
708 if (sf & ZSC_BRG) {
709 cs->cs_preg[4] = ZSWR4_CLK_X16;
710 cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
711 if (sf & ZSC_PCLK) {
712 cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
713 } else {
714 cs->cs_preg[14] = ZSWR14_BAUD_ENA;
715 }
716 tc = tc0;
717 } else {
718 cs->cs_preg[4] = tc0;
719 if (sf & ZSC_RTXDIV) {
720 cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
721 } else {
722 cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
723 }
724 cs->cs_preg[14]= 0;
725 tc = 0xffff;
726 }
727 /* Set the BAUD rate divisor. */
728 cs->cs_preg[12] = tc;
729 cs->cs_preg[13] = tc >> 8;
730 splx(s);
731
732 #ifdef ZSMACDEBUG
733 zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
734 bps, tc, src, sf);
735 zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
736 cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
737 #endif
738
739 cs->cs_preg[5] |= ZSWR5_RTS; /* Make sure the drivers are on! */
740
741 /* Caller will stuff the pending registers. */
742 return (0);
743 }
744
745 int
746 zs_set_modes(cs, cflag)
747 struct zs_chanstate *cs;
748 int cflag; /* bits per second */
749 {
750 struct xzs_chanstate *xcs = (void*)cs;
751 int s;
752
753 /*
754 * Make sure we don't enable hfc on a signal line we're ignoring.
755 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
756 * this code also effectivly turns off ZSWR15_CTS_IE.
757 *
758 * Also, disable DCD interrupts if we've been told to ignore
759 * the DCD pin. Happens on mac68k because the input line for
760 * DCD can also be used as a clock input. (Just set CLOCAL.)
761 *
762 * If someone tries to turn an invalid flow mode on, Just Say No
763 * (Suggested by gwr)
764 */
765 if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
766 return (EINVAL);
767 if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
768 if (cflag & MDMBUF)
769 return (EINVAL);
770 cflag |= CLOCAL;
771 }
772 if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
773 return (EINVAL);
774
775 /*
776 * Output hardware flow control on the chip is horrendous:
777 * if carrier detect drops, the receiver is disabled, and if
778 * CTS drops, the transmitter is stoped IN MID CHARACTER!
779 * Therefore, NEVER set the HFC bit, and instead use the
780 * status interrupt to detect CTS changes.
781 */
782 s = splzs();
783 if ((cflag & (CLOCAL | MDMBUF)) != 0)
784 cs->cs_rr0_dcd = 0;
785 else
786 cs->cs_rr0_dcd = ZSRR0_DCD;
787 /*
788 * The mac hardware only has one output, DTR (HSKo in Mac
789 * parlance). In HFC mode, we use it for the functions
790 * typically served by RTS and DTR on other ports, so we
791 * have to fake the upper layer out some.
792 *
793 * CRTSCTS we use CTS as an input which tells us when to shut up.
794 * We make no effort to shut up the other side of the connection.
795 * DTR is used to hang up the modem.
796 *
797 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
798 * shut up the other side.
799 */
800 if ((cflag & CRTSCTS) != 0) {
801 cs->cs_wr5_dtr = ZSWR5_DTR;
802 cs->cs_wr5_rts = 0;
803 cs->cs_rr0_cts = ZSRR0_CTS;
804 } else if ((cflag & CDTRCTS) != 0) {
805 cs->cs_wr5_dtr = 0;
806 cs->cs_wr5_rts = ZSWR5_DTR;
807 cs->cs_rr0_cts = ZSRR0_CTS;
808 } else if ((cflag & MDMBUF) != 0) {
809 cs->cs_wr5_dtr = 0;
810 cs->cs_wr5_rts = ZSWR5_DTR;
811 cs->cs_rr0_cts = ZSRR0_DCD;
812 } else {
813 cs->cs_wr5_dtr = ZSWR5_DTR;
814 cs->cs_wr5_rts = 0;
815 cs->cs_rr0_cts = 0;
816 }
817 splx(s);
818
819 /* Caller will stuff the pending registers. */
820 return (0);
821 }
822
823
824 /*
825 * Read or write the chip with suitable delays.
826 * MacII hardware has the delay built in.
827 * No need for extra delay. :-) However, some clock-chirped
828 * macs, or zsc's on serial add-on boards might need it.
829 */
830 #define ZS_DELAY()
831
832 u_char
833 zs_read_reg(cs, reg)
834 struct zs_chanstate *cs;
835 u_char reg;
836 {
837 u_char val;
838
839 out8(cs->cs_reg_csr, reg);
840 ZS_DELAY();
841 val = in8(cs->cs_reg_csr);
842 ZS_DELAY();
843 return val;
844 }
845
846 void
847 zs_write_reg(cs, reg, val)
848 struct zs_chanstate *cs;
849 u_char reg, val;
850 {
851 out8(cs->cs_reg_csr, reg);
852 ZS_DELAY();
853 out8(cs->cs_reg_csr, val);
854 ZS_DELAY();
855 }
856
857 u_char zs_read_csr(cs)
858 struct zs_chanstate *cs;
859 {
860 register u_char val;
861
862 val = in8(cs->cs_reg_csr);
863 ZS_DELAY();
864 /* make up for the fact CTS is wired backwards */
865 val ^= ZSRR0_CTS;
866 return val;
867 }
868
869 void zs_write_csr(cs, val)
870 struct zs_chanstate *cs;
871 u_char val;
872 {
873 /* Note, the csr does not write CTS... */
874 out8(cs->cs_reg_csr, val);
875 ZS_DELAY();
876 }
877
878 u_char zs_read_data(cs)
879 struct zs_chanstate *cs;
880 {
881 register u_char val;
882
883 val = in8(cs->cs_reg_data);
884 ZS_DELAY();
885 return val;
886 }
887
888 void zs_write_data(cs, val)
889 struct zs_chanstate *cs;
890 u_char val;
891 {
892 out8(cs->cs_reg_data, val);
893 ZS_DELAY();
894 }
895
896 /****************************************************************
897 * Console support functions (powermac specific!)
898 * Note: this code is allowed to know about the layout of
899 * the chip registers, and uses that to keep things simple.
900 * XXX - I think I like the mvme167 code better. -gwr
901 * XXX - Well :-P :-) -wrs
902 ****************************************************************/
903
904 #define zscnpollc nullcnpollc
905 cons_decl(zs);
906
907 static void zs_putc __P((register volatile struct zschan *, int));
908 static int zs_getc __P((register volatile struct zschan *));
909 extern int zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
910
911 static int stdin, stdout;
912
913 /*
914 * Console functions.
915 */
916
917 /*
918 * zscnprobe is the routine which gets called as the kernel is trying to
919 * figure out where the console should be. Each io driver which might
920 * be the console (as defined in mac68k/conf.c) gets probed. The probe
921 * fills in the consdev structure. Important parts are the device #,
922 * and the console priority. Values are CN_DEAD (don't touch me),
923 * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
924 * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
925 *
926 * As the mac's a bit different, we do extra work here. We mainly check
927 * to see if we have serial echo going on. Also chould check for default
928 * speeds.
929 */
930
931 /*
932 * Polled input char.
933 */
934 int
935 zs_getc(zc)
936 register volatile struct zschan *zc;
937 {
938 register int s, c, rr0;
939
940 s = splhigh();
941 /* Wait for a character to arrive. */
942 do {
943 rr0 = in8(&zc->zc_csr);
944 ZS_DELAY();
945 } while ((rr0 & ZSRR0_RX_READY) == 0);
946
947 c = in8(&zc->zc_data);
948 ZS_DELAY();
949 splx(s);
950
951 /*
952 * This is used by the kd driver to read scan codes,
953 * so don't translate '\r' ==> '\n' here...
954 */
955 return (c);
956 }
957
958 /*
959 * Polled output char.
960 */
961 void
962 zs_putc(zc, c)
963 register volatile struct zschan *zc;
964 int c;
965 {
966 register int s, rr0;
967 register long wait = 0;
968
969 s = splhigh();
970 /* Wait for transmitter to become ready. */
971 do {
972 rr0 = in8(&zc->zc_csr);
973 ZS_DELAY();
974 } while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
975
976 if ((rr0 & ZSRR0_TX_READY) != 0) {
977 out8(&zc->zc_data, c);
978 ZS_DELAY();
979 }
980 splx(s);
981 }
982
983
984 /*
985 * Polled console input putchar.
986 */
987 int
988 zscngetc(dev)
989 dev_t dev;
990 {
991 register volatile struct zschan *zc = zs_conschan;
992 register int c;
993
994 if (zc) {
995 c = zs_getc(zc);
996 } else {
997 char ch = 0;
998 OF_read(stdin, &ch, 1);
999 c = ch;
1000 }
1001 return c;
1002 }
1003
1004 /*
1005 * Polled console output putchar.
1006 */
1007 void
1008 zscnputc(dev, c)
1009 dev_t dev;
1010 int c;
1011 {
1012 register volatile struct zschan *zc = zs_conschan;
1013
1014 if (zc) {
1015 zs_putc(zc, c);
1016 } else {
1017 char ch = c;
1018 OF_write(stdout, &ch, 1);
1019 }
1020 }
1021
1022 /*
1023 * Handle user request to enter kernel debugger.
1024 */
1025 void
1026 zs_abort(cs)
1027 struct zs_chanstate *cs;
1028 {
1029 volatile struct zschan *zc = zs_conschan;
1030 int rr0;
1031 register long wait = 0;
1032
1033 if (zs_cons_canabort == 0)
1034 return;
1035
1036 /* Wait for end of break to avoid PROM abort. */
1037 do {
1038 rr0 = in8(&zc->zc_csr);
1039 ZS_DELAY();
1040 } while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
1041
1042 if (wait > ZSABORT_DELAY) {
1043 zs_cons_canabort = 0;
1044 /* If we time out, turn off the abort ability! */
1045 }
1046
1047 #ifdef DDB
1048 Debugger();
1049 #endif
1050 }
1051
1052 extern int ofccngetc __P((dev_t));
1053 extern void ofccnputc __P((dev_t, int));
1054
1055 struct consdev consdev_zs = {
1056 zscnprobe,
1057 zscninit,
1058 zscngetc,
1059 zscnputc,
1060 zscnpollc,
1061 NULL,
1062 };
1063
1064 void
1065 zscnprobe(cp)
1066 struct consdev *cp;
1067 {
1068 int chosen, pkg;
1069 int unit = 0;
1070 char name[16];
1071
1072 if ((chosen = OF_finddevice("/chosen")) == -1)
1073 return;
1074
1075 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
1076 return;
1077 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
1078 return;
1079
1080 if ((pkg = OF_instance_to_package(stdin)) == -1)
1081 return;
1082
1083 bzero(name, sizeof(name));
1084 if (OF_getprop(pkg, "device_type", name, sizeof(name)) == -1)
1085 return;
1086
1087 if (strcmp(name, "serial") != 0)
1088 return;
1089
1090 bzero(name, sizeof(name));
1091 if (OF_getprop(pkg, "name", name, sizeof(name)) == -1)
1092 return;
1093
1094 if (strcmp(name, "ch-b") == 0)
1095 unit = 1;
1096
1097 cp->cn_dev = makedev(zs_major, unit);
1098 cp->cn_pri = CN_REMOTE;
1099 }
1100
1101 void
1102 zscninit(cp)
1103 struct consdev *cp;
1104 {
1105 int escc, escc_ch, obio, zs_offset;
1106 int ch = 0;
1107 u_int32_t reg[5];
1108 char name[16];
1109
1110 if ((escc_ch = OF_instance_to_package(stdin)) == -1)
1111 return;
1112
1113 bzero(name, sizeof(name));
1114 if (OF_getprop(escc_ch, "name", name, sizeof(name)) == -1)
1115 return;
1116
1117 if (strcmp(name, "ch-b") == 0)
1118 ch = 1;
1119
1120 if (OF_getprop(escc_ch, "reg", reg, sizeof(reg)) < 4)
1121 return;
1122 zs_offset = reg[0];
1123
1124 escc = OF_parent(escc_ch);
1125 obio = OF_parent(escc);
1126
1127 if (OF_getprop(obio, "assigned-addresses", reg, sizeof(reg)) < 12)
1128 return;
1129 zs_conschan = (void *)(reg[2] + zs_offset);
1130
1131 zs_hwflags[0][ch] = ZS_HWFLAG_CONSOLE;
1132 }
1133