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