zs.c revision 1.14 1 /* $NetBSD: zs.c,v 1.14 2003/02/10 11:38:30 tsutsui 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 */
45
46 /*
47 * news68k/dev/zs.c - based on {newsmips,x68k,mvme68k}/dev/zs.c
48 */
49
50 #include "opt_ddb.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/tty.h>
57 #include <sys/lock.h>
58
59 #include <machine/cpu.h>
60 #include <machine/z8530var.h>
61
62 #include <dev/cons.h>
63 #include <dev/ic/z8530reg.h>
64
65 #include <news68k/dev/hbvar.h>
66
67 int zs_getc(void *);
68 void zs_putc(void *, int);
69
70 /*
71 * Some warts needed by z8530tty.c -
72 * The default parity REALLY needs to be the same as the PROM uses,
73 * or you can not see messages done with printf during boot-up...
74 */
75 int zs_def_cflag = (CREAD | CS8 | HUPCL);
76
77 /*
78 * The news68k machines use three different clocks for the ZS chips.
79 */
80 #define NPCLK 3
81 #define PCLK0 (9600 * 416) /* news1700: 3.9936MHz */
82 #define PCLK1 (9600 * 512) /* news1200: 4.9152MHz */
83 #define PCLK2 (9600 * 384) /* external: 3.6864MHz */
84
85 static const u_int pclk[NPCLK] = {
86 PCLK0,
87 PCLK1,
88 PCLK2,
89 };
90
91 /*
92 * Define interrupt levels.
93 */
94 #define ZSHARD_PRI 5
95 #define ZS_IVECT 64
96
97 #define ZS_DELAY() /* delay(2) */
98
99 /* The layout of this is hardware-dependent (padding, order). */
100 struct zschan {
101 volatile u_char zc_csr; /* ctrl,status, and indirect access */
102 volatile u_char zc_data; /* data */
103 };
104 struct zsdevice {
105 /* Yes, they are backwards. */
106 struct zschan zs_chan_b;
107 struct zschan zs_chan_a;
108 };
109
110 static u_char zs_sir;
111
112 /* Default speed for all channels */
113 static int zs_defspeed = 9600;
114
115 /* console status from cninit */
116 static struct zs_chanstate zs_conschan_store;
117 static struct zs_chanstate *zs_conschan;
118 static struct zschan *zc_cons;
119
120 static u_char zs_init_reg[16] = {
121 0, /* 0: CMD (reset, etc.) */
122 0, /* 1: No interrupts yet. */
123 ZS_IVECT, /* IVECT */
124 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
125 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
126 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
127 0, /* 6: TXSYNC/SYNCLO */
128 0, /* 7: RXSYNC/SYNCHI */
129 0, /* 8: alias for data port */
130 ZSWR9_MASTER_IE,
131 0, /*10: Misc. TX/RX control bits */
132 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
133 BPS_TO_TCONST((PCLK0/16), 9600), /*12: BAUDLO (default=9600) */
134 0, /*13: BAUDHI (default=9600) */
135 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
136 ZSWR15_BREAK_IE,
137 };
138
139
140 /****************************************************************
141 * Autoconfig
142 ****************************************************************/
143
144 /* Definition of the driver for autoconfig. */
145 static int zs_match(struct device *, struct cfdata *, void *);
146 static void zs_attach(struct device *, struct device *, void *);
147 static int zs_print(void *, const char *name);
148
149 CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
150 zs_match, zs_attach, NULL, NULL);
151
152 extern struct cfdriver zsc_cd;
153
154 static int zshard(void *);
155 void zssoft(void *);
156 #if 0
157 static int zs_get_speed(struct zs_chanstate *);
158 #endif
159
160 /*
161 * Is the zs chip present?
162 */
163 static int
164 zs_match(parent, cf, aux)
165 struct device *parent;
166 struct cfdata *cf;
167 void *aux;
168 {
169 struct hb_attach_args *ha = aux;
170 u_int addr;
171
172 if (strcmp(ha->ha_name, "zsc"))
173 return 0;
174
175 /* XXX no default address */
176 if (ha->ha_address == (u_int)-1)
177 return 0;
178
179 addr = IIOV(ha->ha_address);
180 /* This returns -1 on a fault (bus error). */
181 if (badaddr((void *)addr, 1))
182 return 0;
183
184 return 1;
185 }
186
187 /*
188 * Attach a found zs.
189 */
190 static void
191 zs_attach(parent, self, aux)
192 struct device *parent;
193 struct device *self;
194 void *aux;
195 {
196 struct zsc_softc *zsc = (void *) self;
197 struct cfdata *cf = self->dv_cfdata;
198 struct hb_attach_args *ha = aux;
199 struct zsc_attach_args zsc_args;
200 struct zsdevice *zs;
201 struct zschan *zc;
202 struct zs_chanstate *cs;
203 int s, channel, clk;
204
205 zs = (void *)IIOV(ha->ha_address);
206
207 clk = cf->cf_flags;
208 if (clk < 0 || clk >= NPCLK)
209 clk = 0;
210
211 printf("\n");
212
213 /*
214 * Initialize software state for each channel.
215 */
216 for (channel = 0; channel < 2; channel++) {
217 zsc_args.channel = channel;
218 cs = &zsc->zsc_cs_store[channel];
219 simple_lock_init(&cs->cs_lock);
220
221 zsc->zsc_cs[channel] = cs;
222 zc = (channel == 0) ? &zs->zs_chan_a : &zs->zs_chan_b;
223
224 if (ha->ha_vect != -1)
225 zs_init_reg[2] = ha->ha_vect;
226
227 if (zc == zc_cons) {
228 memcpy(cs, zs_conschan, sizeof(struct zs_chanstate));
229 zs_conschan = cs;
230 zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
231 } else {
232 cs->cs_reg_csr = &zc->zc_csr;
233 cs->cs_reg_data = &zc->zc_data;
234 memcpy(cs->cs_creg, zs_init_reg, 16);
235 memcpy(cs->cs_preg, zs_init_reg, 16);
236 cs->cs_defspeed = zs_defspeed;
237 zsc_args.hwflags = 0;
238 }
239
240 cs->cs_defcflag = zs_def_cflag;
241
242 cs->cs_channel = channel;
243 cs->cs_private = NULL;
244 cs->cs_ops = &zsops_null;
245 cs->cs_brg_clk = pclk[clk] / 16;
246
247 /* Make these correspond to cs_defcflag (-crtscts) */
248 cs->cs_rr0_dcd = ZSRR0_DCD;
249 cs->cs_rr0_cts = 0;
250 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
251 cs->cs_wr5_rts = 0;
252
253 /*
254 * Clear the master interrupt enable.
255 * The INTENA is common to both channels,
256 * so just do it on the A channel.
257 */
258 if (channel == 0) {
259 s = splhigh();
260 zs_write_reg(cs, 9, 0);
261 splx(s);
262 }
263
264 /*
265 * Look for a child driver for this channel.
266 * The child attach will setup the hardware.
267 */
268 if (!config_found(self, (void *)&zsc_args, zs_print)) {
269 /* No sub-driver. Just reset it. */
270 u_char reset = (channel == 0) ?
271 ZSWR9_A_RESET : ZSWR9_B_RESET;
272 s = splhigh();
273 zs_write_reg(cs, 9, reset);
274 splx(s);
275 }
276 }
277
278 /*
279 * Now safe to install interrupt handlers.
280 */
281 hb_intr_establish(zs_init_reg[2], zshard, ZSHARD_PRI, zsc);
282
283 /*
284 * Set the master interrupt enable and interrupt vector.
285 * (common to both channels, do it on A)
286 */
287 cs = zsc->zsc_cs[0];
288 s = splhigh();
289 /* interrupt vector */
290 zs_write_reg(cs, 2, zs_init_reg[2]);
291 /* master interrupt control (enable) */
292 zs_write_reg(cs, 9, zs_init_reg[9]);
293 splx(s);
294
295 if (zs_sir == 0)
296 zs_sir = allocate_sir(zssoft, zsc);
297 }
298
299 static int
300 zs_print(aux, name)
301 void *aux;
302 const char *name;
303 {
304 struct zsc_attach_args *args = aux;
305
306 if (name != NULL)
307 aprint_normal("%s: ", name);
308
309 if (args->channel != -1)
310 aprint_normal(" channel %d", args->channel);
311
312 return UNCONF;
313 }
314
315 /*
316 * For news68k-port, we don't use autovectored interrupt.
317 * We do not need to look at all of the zs chips.
318 */
319 static int
320 zshard(arg)
321 void *arg;
322 {
323 struct zsc_softc *zsc = arg;
324 int rval;
325
326 rval = zsc_intr_hard(zsc);
327
328 /* We are at splzs here, so no need to lock. */
329 if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq) {
330 setsoftint(zs_sir);
331 }
332
333 return (rval);
334 }
335
336 /*
337 * Shared among the all chips. We have to look at all of them.
338 */
339 void
340 zssoft(arg)
341 void *arg;
342 {
343 struct zsc_softc *zsc;
344 int s, unit;
345
346 /* Make sure we call the tty layer at spltty. */
347 s = spltty();
348 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
349 zsc = zsc_cd.cd_devs[unit];
350 if (zsc == NULL)
351 continue;
352 (void) zsc_intr_soft(zsc);
353 }
354 splx(s);
355 }
356
357 /*
358 * Compute the current baud rate given a ZS channel.
359 */
360 #if 0
361 static int
362 zs_get_speed(cs)
363 struct zs_chanstate *cs;
364 {
365 int tconst;
366
367 tconst = zs_read_reg(cs, 12);
368 tconst |= zs_read_reg(cs, 13) << 8;
369 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
370 }
371 #endif
372
373 /*
374 * MD functions for setting the baud rate and control modes.
375 */
376 int
377 zs_set_speed(cs, bps)
378 struct zs_chanstate *cs;
379 int bps; /* bits per second */
380 {
381 int tconst, real_bps;
382
383 if (bps == 0)
384 return (0);
385
386 #ifdef DIAGNOSTIC
387 if (cs->cs_brg_clk == 0)
388 panic("zs_set_speed");
389 #endif
390
391 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
392 if (tconst < 0)
393 return (EINVAL);
394
395 /* Convert back to make sure we can do it. */
396 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
397
398 /* XXX - Allow some tolerance here? */
399 if (real_bps != bps)
400 return (EINVAL);
401
402 cs->cs_preg[12] = tconst;
403 cs->cs_preg[13] = tconst >> 8;
404
405 /* Caller will stuff the pending registers. */
406 return (0);
407 }
408
409 int
410 zs_set_modes(cs, cflag)
411 struct zs_chanstate *cs;
412 int cflag; /* bits per second */
413 {
414 int s;
415
416 /*
417 * Output hardware flow control on the chip is horrendous:
418 * if carrier detect drops, the receiver is disabled, and if
419 * CTS drops, the transmitter is stoped IN MID CHARACTER!
420 * Therefore, NEVER set the HFC bit, and instead use the
421 * status interrupt to detect CTS changes.
422 */
423 s = splzs();
424 cs->cs_rr0_pps = 0;
425 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
426 cs->cs_rr0_dcd = 0;
427 if ((cflag & MDMBUF) == 0)
428 cs->cs_rr0_pps = ZSRR0_DCD;
429 } else
430 cs->cs_rr0_dcd = ZSRR0_DCD;
431 if ((cflag & CRTSCTS) != 0) {
432 cs->cs_wr5_dtr = ZSWR5_DTR;
433 cs->cs_wr5_rts = ZSWR5_RTS;
434 cs->cs_rr0_cts = ZSRR0_CTS;
435 } else if ((cflag & MDMBUF) != 0) {
436 cs->cs_wr5_dtr = 0;
437 cs->cs_wr5_rts = ZSWR5_DTR;
438 cs->cs_rr0_cts = ZSRR0_DCD;
439 } else {
440 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
441 cs->cs_wr5_rts = 0;
442 cs->cs_rr0_cts = 0;
443 }
444 splx(s);
445
446 /* Caller will stuff the pending registers. */
447 return (0);
448 }
449
450
451 /*
452 * Read or write the chip with suitable delays.
453 */
454
455 u_char
456 zs_read_reg(cs, reg)
457 struct zs_chanstate *cs;
458 u_char reg;
459 {
460 u_char val;
461
462 *cs->cs_reg_csr = reg;
463 ZS_DELAY();
464 val = *cs->cs_reg_csr;
465 ZS_DELAY();
466 return val;
467 }
468
469 void
470 zs_write_reg(cs, reg, val)
471 struct zs_chanstate *cs;
472 u_char reg, val;
473 {
474 *cs->cs_reg_csr = reg;
475 ZS_DELAY();
476 *cs->cs_reg_csr = val;
477 ZS_DELAY();
478 }
479
480 u_char
481 zs_read_csr(cs)
482 struct zs_chanstate *cs;
483 {
484 u_char val;
485
486 val = *cs->cs_reg_csr;
487 ZS_DELAY();
488 return val;
489 }
490
491 void
492 zs_write_csr(cs, val)
493 struct zs_chanstate *cs;
494 u_char val;
495 {
496 *cs->cs_reg_csr = val;
497 ZS_DELAY();
498 }
499
500 u_char
501 zs_read_data(cs)
502 struct zs_chanstate *cs;
503 {
504 u_char val;
505
506 val = *cs->cs_reg_data;
507 ZS_DELAY();
508 return val;
509 }
510
511 void
512 zs_write_data(cs, val)
513 struct zs_chanstate *cs;
514 u_char val;
515 {
516 *cs->cs_reg_data = val;
517 ZS_DELAY();
518 }
519
520 void
521 zs_abort(cs)
522 struct zs_chanstate *cs;
523 {
524 #ifdef DDB
525 Debugger();
526 #endif
527 }
528
529 /*
530 * Polled input char.
531 */
532 int
533 zs_getc(arg)
534 void *arg;
535 {
536 struct zs_chanstate *cs = arg;
537 int s, c, rr0;
538
539 s = splhigh();
540 /* Wait for a character to arrive. */
541 do {
542 rr0 = *cs->cs_reg_csr;
543 ZS_DELAY();
544 } while ((rr0 & ZSRR0_RX_READY) == 0);
545
546 c = *cs->cs_reg_data;
547 ZS_DELAY();
548 splx(s);
549
550 return c;
551 }
552
553 /*
554 * Polled output char.
555 */
556 void
557 zs_putc(arg, c)
558 void *arg;
559 int c;
560 {
561 struct zs_chanstate *cs = arg;
562 int s, rr0;
563
564 s = splhigh();
565 /* Wait for transmitter to become ready. */
566 do {
567 rr0 = *cs->cs_reg_csr;
568 ZS_DELAY();
569 } while ((rr0 & ZSRR0_TX_READY) == 0);
570
571 *cs->cs_reg_data = c;
572 ZS_DELAY();
573 splx(s);
574 }
575
576 /*****************************************************************/
577
578 static void zscnprobe(struct consdev *);
579 static void zscninit(struct consdev *);
580 static int zscngetc(dev_t);
581 static void zscnputc(dev_t, int);
582
583 struct consdev consdev_zs = {
584 zscnprobe,
585 zscninit,
586 zscngetc,
587 zscnputc,
588 nullcnpollc,
589 NULL,
590 };
591
592 static void
593 zscnprobe(cn)
594 struct consdev *cn;
595 {
596 extern const struct cdevsw zstty_cdevsw;
597 extern int tty00_is_console;
598
599 cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
600 if (tty00_is_console)
601 cn->cn_pri = CN_REMOTE;
602 else
603 cn->cn_pri = CN_NORMAL;
604 }
605
606 static void
607 zscninit(cn)
608 struct consdev *cn;
609 {
610 struct zs_chanstate *cs;
611
612 extern volatile u_char *sccport0a;
613
614 zc_cons = (struct zschan *)sccport0a; /* XXX */
615
616 zs_conschan = cs = &zs_conschan_store;
617
618 /* Setup temporary chanstate. */
619 cs->cs_reg_csr = &zc_cons->zc_csr;
620 cs->cs_reg_data = &zc_cons->zc_data;
621
622 /* Initialize the pending registers. */
623 memcpy(cs->cs_preg, zs_init_reg, 16);
624 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
625
626 cs->cs_preg[12] = BPS_TO_TCONST(pclk[systype] / 16, 9600); /* XXX */
627 cs->cs_preg[13] = 0;
628 cs->cs_defspeed = 9600;
629
630 /* Clear the master interrupt enable. */
631 zs_write_reg(cs, 9, 0);
632
633 /* Reset the whole SCC chip. */
634 zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
635
636 /* Copy "pending" to "current" and H/W */
637 zs_loadchannelregs(cs);
638 }
639
640 static int
641 zscngetc(dev)
642 dev_t dev;
643 {
644 return zs_getc((void *)zs_conschan);
645 }
646
647 static void
648 zscnputc(dev, c)
649 dev_t dev;
650 int c;
651 {
652 zs_putc((void *)zs_conschan, c);
653 }
654