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