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