zs.c revision 1.25 1 /* $NetBSD: zs.c,v 1.25 2005/01/10 17:01:22 chs Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Zilog Z8530 Dual UART driver (machine-dependent part)
41 *
42 * Runs two serial lines per chip using slave drivers.
43 * Plain tty/async lines use the zs_async slave.
44 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45 */
46
47 /* This was snarfed from the netbsd sparc/dev/zs.c at version 1.56
48 * and then updated to reflect changes in 1.59
49 * by Darrin B Jewell <jewell (at) mit.edu> Mon Mar 30 20:24:46 1998
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.25 2005/01/10 17:01:22 chs Exp $");
54
55 #include "opt_ddb.h"
56 #include "opt_kgdb.h"
57 #include "opt_serial.h"
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/conf.h>
62 #include <sys/device.h>
63 #include <sys/file.h>
64 #include <sys/ioctl.h>
65 #include <sys/kernel.h>
66 #include <sys/proc.h>
67 #include <sys/tty.h>
68 #include <sys/time.h>
69 #include <sys/syslog.h>
70
71 #include <machine/autoconf.h>
72 #include <machine/cpu.h>
73 #include <machine/psl.h>
74
75 #include <dev/cons.h>
76
77 #include <dev/ic/z8530reg.h>
78 #include <machine/z8530var.h>
79
80 #include <next68k/next68k/isr.h>
81
82 #include <next68k/dev/intiovar.h>
83 #include <next68k/dev/zs_cons.h>
84
85 #include "zsc.h" /* NZSC */
86
87 #if (NZSC < 0)
88 #error "No serial controllers?"
89 #endif
90
91 /*
92 * Some warts needed by z8530tty.c -
93 * The default parity REALLY needs to be the same as the PROM uses,
94 * or you can not see messages done with printf during boot-up...
95 */
96 int zs_def_cflag = (CREAD | CS8 | HUPCL);
97
98 /*
99 * The NeXT provides a 3.686400 MHz clock to the ZS chips.
100 */
101 #define PCLK (9600 * 384) /* PCLK pin input clock rate */
102
103 #define ZS_DELAY() delay(2)
104
105 /* The layout of this is hardware-dependent (padding, order). */
106 struct zschan {
107 volatile u_char zc_csr; /* ctrl,status, and indirect access */
108 u_char zc_xxx0;
109 volatile u_char zc_data; /* data */
110 u_char zc_xxx1;
111 };
112
113 static char *zsaddr[NZSC];
114
115 /* Flags from cninit() */
116 static int zs_hwflags[NZSC][2];
117
118 /* Default speed for each channel */
119 static int zs_defspeed[NZSC][2] = {
120 { 9600, /* ttya */
121 9600 }, /* ttyb */
122 };
123
124 static u_char zs_init_reg[16] = {
125 0, /* 0: CMD (reset, etc.) */
126 0, /* 1: No interrupts yet. */
127 0x18 + NEXT_I_IPL(NEXT_I_SCC), /* 2: IVECT */
128 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
129 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
130 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
131 0, /* 6: TXSYNC/SYNCLO */
132 0, /* 7: RXSYNC/SYNCHI */
133 0, /* 8: alias for data port */
134 ZSWR9_MASTER_IE,
135 0, /*10: Misc. TX/RX control bits */
136 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
137 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
138 0, /*13: BAUDHI (default=9600) */
139 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
140 ZSWR15_BREAK_IE,
141 };
142
143 struct zschan *
144 zs_get_chan_addr(int zs_unit, int channel)
145 {
146 char *addr;
147 struct zschan *zc;
148
149 if (zs_unit >= NZSC)
150 return (NULL);
151 addr = zsaddr[zs_unit];
152 if (addr == NULL)
153 return (NULL);
154 if (channel == 0) {
155 /* handle the fact the ports are intertwined. */
156 zc = (struct zschan *)(addr+1);
157 } else {
158 zc = (struct zschan *)(addr);
159 }
160 return (zc);
161 }
162
163
164 /****************************************************************
165 * Autoconfig
166 ****************************************************************/
167
168 /* Definition of the driver for autoconfig. */
169 static int zs_match(struct device *, struct cfdata *, void *);
170 static void zs_attach(struct device *, struct device *, void *);
171 static int zs_print(void *, const char *);
172
173 extern int zs_getc(void *);
174 extern void zs_putc(void *, int);
175
176 CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
177 zs_match, zs_attach, NULL, NULL);
178
179 extern struct cfdriver zsc_cd;
180
181 /* Interrupt handlers. */
182 static int zshard(void *);
183 static void zssoft(void *);
184
185 static int zs_get_speed(struct zs_chanstate *);
186
187 /*
188 * Is the zs chip present?
189 */
190 static int
191 zs_match(struct device *parent, struct cfdata *cf, void *aux)
192 {
193 struct intio_attach_args *ia = (struct intio_attach_args *)aux;
194
195 if (zsaddr[cf->cf_unit] == NULL)
196 return(0);
197
198 ia->ia_addr = (void *)zsaddr[cf->cf_unit];
199
200 return(1);
201 }
202
203 /*
204 * Attach a found zs.
205 *
206 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
207 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
208 */
209 static void
210 zs_attach(struct device *parent, struct device *self, void *aux)
211 {
212 struct zsc_softc *zsc = (void *) self;
213 struct zsc_attach_args zsc_args;
214 volatile struct zschan *zc;
215 struct zs_chanstate *cs;
216 int s, zs_unit, channel;
217
218 printf("\n");
219
220 zs_unit = zsc->zsc_dev.dv_unit;
221
222 if (zs_unit == 0) {
223 zsaddr[0] = (void *)IIOV(NEXT_P_SCC);
224 }
225
226 if (zsaddr[zs_unit] == NULL)
227 panic("zs_attach: zs%d not mapped", zs_unit);
228
229 /*
230 * Initialize software state for each channel.
231 */
232 for (channel = 0; channel < 2; channel++) {
233 zsc_args.channel = channel;
234 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
235 cs = &zsc->zsc_cs_store[channel];
236 zsc->zsc_cs[channel] = cs;
237
238 simple_lock_init(&cs->cs_lock);
239 cs->cs_channel = channel;
240 cs->cs_private = NULL;
241 cs->cs_ops = &zsops_null;
242 cs->cs_brg_clk = PCLK / 16;
243
244 zc = zs_get_chan_addr(zs_unit, channel);
245 cs->cs_reg_csr = &zc->zc_csr;
246 cs->cs_reg_data = &zc->zc_data;
247
248 bcopy(zs_init_reg, cs->cs_creg, 16);
249 bcopy(zs_init_reg, cs->cs_preg, 16);
250
251 /* XXX: Get these from the PROM properties! */
252 /* XXX: See the mvme167 code. Better. */
253 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
254 cs->cs_defspeed = zs_get_speed(cs);
255 else
256 cs->cs_defspeed = zs_defspeed[zs_unit][channel];
257 cs->cs_defcflag = zs_def_cflag;
258
259 /* Make these correspond to cs_defcflag (-crtscts) */
260 cs->cs_rr0_dcd = ZSRR0_DCD;
261 cs->cs_rr0_cts = 0;
262 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
263 cs->cs_wr5_rts = 0;
264
265 /*
266 * Clear the master interrupt enable.
267 * The INTENA is common to both channels,
268 * so just do it on the A channel.
269 */
270 if (channel == 0) {
271 zs_write_reg(cs, 9, 0);
272 }
273
274 /*
275 * Look for a child driver for this channel.
276 * The child attach will setup the hardware.
277 */
278 if (!config_found(self, (void *)&zsc_args, zs_print)) {
279 /* No sub-driver. Just reset it. */
280 u_char reset = (channel == 0) ?
281 ZSWR9_A_RESET : ZSWR9_B_RESET;
282 s = splzs();
283 zs_write_reg(cs, 9, reset);
284 splx(s);
285 }
286 }
287
288 isrlink_autovec(zshard, NULL, NEXT_I_IPL(NEXT_I_SCC), 0, NULL);
289 INTR_ENABLE(NEXT_I_SCC);
290
291 {
292 int sir;
293 sir = allocate_sir(zssoft, zsc);
294 if (sir != SIR_SERIAL) {
295 panic("Unexpected zssoft sir");
296 }
297 }
298
299 /*
300 * Set the master interrupt enable and interrupt vector.
301 * (common to both channels, do it on A)
302 */
303 cs = zsc->zsc_cs[0];
304 s = splhigh();
305 /* interrupt vector */
306 zs_write_reg(cs, 2, zs_init_reg[2]);
307 /* master interrupt control (enable) */
308 zs_write_reg(cs, 9, zs_init_reg[9]);
309 splx(s);
310 }
311
312 static int
313 zs_print(void *aux, const char *name)
314 {
315 struct zsc_attach_args *args = aux;
316
317 if (name != NULL)
318 aprint_normal("%s: ", name);
319
320 if (args->channel != -1)
321 aprint_normal(" channel %d", args->channel);
322
323 return (UNCONF);
324 }
325
326 static volatile int zssoftpending;
327
328 /*
329 * Our ZS chips all share a common, autovectored interrupt,
330 * so we have to look at all of them on each interrupt.
331 */
332 static int
333 zshard(void *arg)
334 {
335 struct zsc_softc *zsc;
336 int unit, rr3, rval, softreq;
337
338 if (!INTR_OCCURRED(NEXT_I_SCC))
339 return 0;
340
341 rval = softreq = 0;
342 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
343 zsc = zsc_cd.cd_devs[unit];
344 if (zsc == NULL)
345 continue;
346 rr3 = zsc_intr_hard(zsc);
347 /* Count up the interrupts. */
348 if (rr3) {
349 rval |= rr3;
350 zsc->zsc_intrcnt.ev_count++;
351 }
352 softreq |= zsc->zsc_cs[0]->cs_softreq;
353 softreq |= zsc->zsc_cs[1]->cs_softreq;
354 }
355
356 /* We are at splzs here, so no need to lock. */
357 if (softreq && (zssoftpending == 0)) {
358 zssoftpending = 1;
359 setsoftserial();
360 }
361 return(1);
362 }
363
364 /*
365 * Similar scheme as for zshard (look at all of them)
366 */
367 static void
368 zssoft(void *arg)
369 {
370 struct zsc_softc *zsc;
371 int s, unit;
372
373 /* This is not the only ISR on this IPL. */
374 if (zssoftpending == 0)
375 panic("zssoft not pending");
376
377 /*
378 * The soft intr. bit will be set by zshard only if
379 * the variable zssoftpending is zero. The order of
380 * these next two statements prevents our clearing
381 * the soft intr bit just after zshard has set it.
382 */
383 /* ienab_bic(IE_ZSSOFT); */
384 zssoftpending = 0;
385
386 /* Make sure we call the tty layer at spltty. */
387 s = spltty();
388 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
389 zsc = zsc_cd.cd_devs[unit];
390 if (zsc == NULL)
391 continue;
392 (void)zsc_intr_soft(zsc);
393 }
394 splx(s);
395 }
396
397
398 /*
399 * Compute the current baud rate given a ZS channel.
400 */
401 static int
402 zs_get_speed(struct zs_chanstate *cs)
403 {
404 int tconst;
405
406 tconst = zs_read_reg(cs, 12);
407 tconst |= zs_read_reg(cs, 13) << 8;
408 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
409 }
410
411 /*
412 * MD functions for setting the baud rate and control modes.
413 */
414 int
415 zs_set_speed(struct zs_chanstate *cs, int bps)
416 {
417 int tconst, real_bps;
418
419 if (bps == 0)
420 return (0);
421
422 #ifdef DIAGNOSTIC
423 if (cs->cs_brg_clk == 0)
424 panic("zs_set_speed");
425 #endif
426
427 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
428 if (tconst < 0)
429 return (EINVAL);
430
431 /* Convert back to make sure we can do it. */
432 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
433
434 /* XXX - Allow some tolerance here? */
435 if (real_bps != bps)
436 return (EINVAL);
437
438 cs->cs_preg[12] = tconst;
439 cs->cs_preg[13] = tconst >> 8;
440
441 /* Caller will stuff the pending registers. */
442 return (0);
443 }
444
445 int
446 zs_set_modes(struct zs_chanstate *cs, int cflag)
447 {
448 int s;
449
450 /*
451 * Output hardware flow control on the chip is horrendous:
452 * if carrier detect drops, the receiver is disabled, and if
453 * CTS drops, the transmitter is stoped IN MID CHARACTER!
454 * Therefore, NEVER set the HFC bit, and instead use the
455 * status interrupt to detect CTS changes.
456 */
457 s = splzs();
458 cs->cs_rr0_pps = 0;
459 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
460 cs->cs_rr0_dcd = 0;
461 if ((cflag & MDMBUF) == 0)
462 cs->cs_rr0_pps = ZSRR0_DCD;
463 } else
464 cs->cs_rr0_dcd = ZSRR0_DCD;
465 if ((cflag & CRTSCTS) != 0) {
466 cs->cs_wr5_dtr = ZSWR5_DTR;
467 cs->cs_wr5_rts = ZSWR5_RTS;
468 cs->cs_rr0_cts = ZSRR0_CTS;
469 } else if ((cflag & CDTRCTS) != 0) {
470 cs->cs_wr5_dtr = 0;
471 cs->cs_wr5_rts = ZSWR5_DTR;
472 cs->cs_rr0_cts = ZSRR0_CTS;
473 } else if ((cflag & MDMBUF) != 0) {
474 cs->cs_wr5_dtr = 0;
475 cs->cs_wr5_rts = ZSWR5_DTR;
476 cs->cs_rr0_cts = ZSRR0_DCD;
477 } else {
478 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
479 cs->cs_wr5_rts = 0;
480 cs->cs_rr0_cts = 0;
481 }
482 splx(s);
483
484 /* Caller will stuff the pending registers. */
485 return (0);
486 }
487
488 /*
489 * Read or write the chip with suitable delays.
490 */
491
492 u_char
493 zs_read_reg(struct zs_chanstate *cs, u_char reg)
494 {
495 u_char val;
496
497 *cs->cs_reg_csr = reg;
498 ZS_DELAY();
499 val = *cs->cs_reg_csr;
500 ZS_DELAY();
501 return (val);
502 }
503
504 void
505 zs_write_reg(struct zs_chanstate *cs, u_char reg, u_char val)
506 {
507 *cs->cs_reg_csr = reg;
508 ZS_DELAY();
509 *cs->cs_reg_csr = val;
510 ZS_DELAY();
511 }
512
513 u_char
514 zs_read_csr(struct zs_chanstate *cs)
515 {
516 u_char val;
517
518 val = *cs->cs_reg_csr;
519 ZS_DELAY();
520 return (val);
521 }
522
523 void
524 zs_write_csr(struct zs_chanstate *cs, u_char val)
525 {
526 *cs->cs_reg_csr = val;
527 ZS_DELAY();
528 }
529
530 u_char
531 zs_read_data(struct zs_chanstate *cs)
532 {
533 u_char val;
534
535 val = *cs->cs_reg_data;
536 ZS_DELAY();
537 return (val);
538 }
539
540 void
541 zs_write_data(struct zs_chanstate *cs, u_char val)
542 {
543 *cs->cs_reg_data = val;
544 ZS_DELAY();
545 }
546
547 /****************************************************************
548 * Console support functions (Sun specific!)
549 * Note: this code is allowed to know about the layout of
550 * the chip registers, and uses that to keep things simple.
551 * XXX - I think I like the mvme167 code better. -gwr
552 ****************************************************************/
553
554 extern void Debugger(void);
555 void *zs_conschan;
556 int zs_consunit = 0;
557
558 /*
559 * Handle user request to enter kernel debugger.
560 */
561 void
562 zs_abort(struct zs_chanstate *cs)
563 {
564 #if defined(ZS_CONSOLE_ABORT)
565 volatile struct zschan *zc = zs_conschan;
566 int rr0;
567
568 /* Wait for end of break to avoid PROM abort. */
569 /* XXX - Limit the wait? */
570 do {
571 rr0 = zc->zc_csr;
572 ZS_DELAY();
573 } while (rr0 & ZSRR0_BREAK);
574
575 #if defined(KGDB)
576 zskgdb(cs);
577 #elif defined(DDB)
578 Debugger();
579 #else
580 /* XXX eventually, drop into next rom monitor here */
581 printf("stopping on keyboard abort not supported without DDB or KGDB\n");
582 #endif
583 #else /* !ZS_CONSOLE_ABORT */
584 return;
585 #endif
586 }
587
588 /*
589 * Polled input char.
590 */
591 int
592 zs_getc(void *arg)
593 {
594 volatile struct zschan *zc = arg;
595 int s, c, rr0;
596
597 s = splhigh();
598 /* Wait for a character to arrive. */
599 do {
600 rr0 = zc->zc_csr;
601 ZS_DELAY();
602 } while ((rr0 & ZSRR0_RX_READY) == 0);
603
604 c = zc->zc_data;
605 ZS_DELAY();
606 splx(s);
607
608 /*
609 * This is used by the kd driver to read scan codes,
610 * so don't translate '\r' ==> '\n' here...
611 */
612 return (c);
613 }
614
615 /*
616 * Polled output char.
617 */
618 void
619 zs_putc(void *arg, int c)
620 {
621 volatile struct zschan *zc = arg;
622 int s, rr0;
623
624 s = splhigh();
625 /* Wait for transmitter to become ready. */
626 do {
627 rr0 = zc->zc_csr;
628 ZS_DELAY();
629 } while ((rr0 & ZSRR0_TX_READY) == 0);
630
631
632 zc->zc_data = c;
633 ZS_DELAY();
634
635 splx(s);
636 }
637
638 /*****************************************************************/
639
640 void zscninit(struct consdev *);
641 int zscngetc(dev_t);
642 void zscnputc(dev_t, int);
643 void zscnprobe(struct consdev *);
644
645 void
646 zscnprobe(struct consdev *cp)
647 {
648 extern const struct cdevsw zstty_cdevsw;
649 int maj;
650
651 maj = cdevsw_lookup_major(&zstty_cdevsw);
652 if (maj != -1) {
653 #ifdef SERCONSOLE
654 cp->cn_pri = CN_REMOTE;
655 #else
656 cp->cn_pri = CN_NORMAL; /* Lower than CN_INTERNAL */
657 #endif
658 zs_consunit = 0;
659 zsaddr[0] = (void *)IIOV(NEXT_P_SCC);
660 cp->cn_dev = makedev(maj, zs_consunit);
661 zs_conschan = zs_get_chan_addr(0, zs_consunit);
662 } else {
663 cp->cn_pri = CN_DEAD;
664 }
665 }
666
667 void
668 zscninit(struct consdev *cn)
669 {
670 struct zs_chanstate xcs;
671 struct zs_chanstate *cs;
672 volatile struct zschan *zc;
673 int tconst, s;
674
675 zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
676
677 /* Setup temporary chanstate. */
678 memset(&xcs, 0, sizeof(xcs));
679 cs = &xcs;
680 zc = zs_conschan;
681 cs->cs_reg_csr = &zc->zc_csr;
682 cs->cs_reg_data = &zc->zc_data;
683 cs->cs_channel = zs_consunit;
684 cs->cs_brg_clk = PCLK / 16;
685
686 memcpy(cs->cs_preg, zs_init_reg, 16);
687 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
688 cs->cs_preg[15] = ZSWR15_BREAK_IE;
689
690 tconst = BPS_TO_TCONST(cs->cs_brg_clk, zs_defspeed[0][zs_consunit]);
691 cs->cs_preg[12] = tconst;
692 cs->cs_preg[13] = tconst >> 8;
693
694 /*
695 * can't use zs_set_speed as we haven't set up the
696 * signal sources, and it's not worth it for now
697 */
698
699 cs->cs_preg[9] &= ~ZSWR9_MASTER_IE;
700 /* no interrupts until later, after attach. */
701
702 s = splhigh();
703 zs_loadchannelregs(cs);
704 splx(s);
705
706 printf("\nNetBSD/next68k console\n");
707 }
708
709 /*
710 * Polled console input putchar.
711 */
712 int
713 zscngetc(dev_t dev)
714 {
715 return (zs_getc(zs_conschan));
716 }
717
718 /*
719 * Polled console output putchar.
720 */
721 void
722 zscnputc(dev_t dev, int c)
723 {
724 zs_putc(zs_conschan, c);
725 }
726
727 /*****************************************************************/
728