zs.c revision 1.22 1 /* $NetBSD: zs.c,v 1.22 2008/03/29 19:15:34 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 2000 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 and Wayne Knowles
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 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.22 2008/03/29 19:15:34 tsutsui Exp $");
48
49 #include "opt_ddb.h"
50 #include "opt_kgdb.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/file.h>
57 #include <sys/ioctl.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60 #include <sys/tty.h>
61 #include <sys/time.h>
62 #include <sys/syslog.h>
63 #include <sys/cpu.h>
64 #include <sys/intr.h>
65
66 #include <machine/mainboard.h>
67 #include <machine/autoconf.h>
68 #include <machine/prom.h>
69 #include <machine/z8530var.h>
70
71 #include <dev/cons.h>
72 #include <dev/ic/z8530reg.h>
73
74 #include "ioconf.h"
75 #include "zsc.h" /* NZSC */
76 #define NZS NZSC
77
78 /* Make life easier for the initialized arrays here. */
79 #if NZS < 2
80 #undef NZS
81 #define NZS 2
82 #endif
83
84 /*
85 * Some warts needed by z8530tty.c -
86 * The default parity REALLY needs to be the same as the PROM uses,
87 * or you can not see messages done with printf during boot-up...
88 */
89 int zs_def_cflag = (CREAD | CS8 | HUPCL);
90
91
92 #define PCLK 10000000 /* PCLK pin input clock rate */
93
94 #ifndef ZS_DEFSPEED
95 #define ZS_DEFSPEED 9600
96 #endif
97
98 /*
99 * Define interrupt levels.
100 */
101 #define ZSHARD_PRI 64
102
103 /* Register recovery time is 3.5 to 4 PCLK Cycles */
104 #define ZS_RECOVERY 1 /* 1us = 10 PCLK Cycles */
105 #define ZS_DELAY() delay(ZS_RECOVERY)
106
107 /* The layout of this is hardware-dependent (padding, order). */
108 struct zschan {
109 uint8_t pad1[3];
110 volatile uint8_t zc_csr; /* ctrl,status, and indirect access */
111 uint8_t pad2[3];
112 volatile uint8_t zc_data; /* data */
113 };
114 struct zsdevice {
115 /* Yes, they are backwards. */
116 struct zschan zs_chan_b;
117 struct zschan zs_chan_a;
118 };
119
120 /* Return the byte offset of element within a structure */
121 #define OFFSET(struct_def, el) ((size_t)&((struct_def *)0)->el)
122
123 #define ZS_CHAN_A OFFSET(struct zsdevice, zs_chan_a)
124 #define ZS_CHAN_B OFFSET(struct zsdevice, zs_chan_b)
125 #define ZS_REG_CSR OFFSET(struct zschan, zc_csr)
126 #define ZS_REG_DATA OFFSET(struct zschan, zc_data)
127 static int zs_chan_offset[] = {ZS_CHAN_A, ZS_CHAN_B};
128
129 /* Flags from cninit() */
130 static int zs_hwflags[NZS][2];
131
132 /* Default speed for all channels */
133 static int zs_defspeed = ZS_DEFSPEED;
134 static volatile int zssoftpending;
135
136 static uint8_t zs_init_reg[16] = {
137 0, /* 0: CMD (reset, etc.) */
138 0, /* 1: No interrupts yet. */
139 ZSHARD_PRI, /* 2: IVECT */
140 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
141 ZSWR4_CLK_X16 | ZSWR4_ONESB,
142 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
143 0, /* 6: TXSYNC/SYNCLO */
144 0, /* 7: RXSYNC/SYNCHI */
145 0, /* 8: alias for data port */
146 ZSWR9_MASTER_IE,
147 0, /*10: Misc. TX/RX control bits */
148 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD | ZSWR11_TRXC_OUT_ENA,
149 BPS_TO_TCONST(PCLK/16, ZS_DEFSPEED), /*12: BAUDLO (default=9600) */
150 0, /*13: BAUDHI (default=9600) */
151 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
152 ZSWR15_BREAK_IE,
153 };
154
155
156 /****************************************************************
157 * Autoconfig
158 ****************************************************************/
159
160 /* Definition of the driver for autoconfig. */
161 static int zs_match(device_t, cfdata_t, void *);
162 static void zs_attach(device_t, device_t, void *);
163 static int zs_print(void *, const char *name);
164
165 CFATTACH_DECL_NEW(zsc, sizeof(struct zsc_softc),
166 zs_match, zs_attach, NULL, NULL);
167
168 static int zshard(void *);
169 void zssoft(void *);
170 static int zs_get_speed(struct zs_chanstate *);
171 struct zschan *zs_get_chan_addr(int zs_unit, int channel);
172 int zs_getc(void *);
173 void zs_putc(void *, int);
174
175 /*
176 * Is the zs chip present?
177 */
178 static int
179 zs_match(device_t parent, cfdata_t cf, void *aux)
180 {
181 struct confargs *ca = aux;
182 void *va;
183
184 if (strcmp(ca->ca_name, "zsc"))
185 return 0;
186
187 va = (void *)cf->cf_addr;
188
189 /* This returns -1 on a fault (bus error). */
190 if (badaddr(va, 1))
191 return 0;
192 return 1;
193 }
194
195 /*
196 * Attach a found zs.
197 *
198 * Match slave number to zs unit number, so that misconfiguration will
199 * not set up the keyboard as ttya, etc.
200 */
201 static void
202 zs_attach(device_t parent, device_t self, void *aux)
203 {
204 struct zsc_softc *zsc = device_private(self);
205 struct confargs *ca = aux;
206 struct zsc_attach_args zsc_args;
207 struct zs_chanstate *cs;
208 struct zs_channel *ch;
209 int zs_unit, channel, s;
210
211 zsc->zsc_dev = self;
212 zsc->zsc_bustag = ca->ca_bustag;
213 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
214 sizeof(struct zsdevice),
215 BUS_SPACE_MAP_LINEAR,
216 &zsc->zsc_base) != 0) {
217 aprint_error(": cannot map registers\n");
218 return;
219 }
220
221 zs_unit = device_unit(self);
222 aprint_normal("\n");
223
224 /*
225 * Initialize software state for each channel.
226 */
227 for (channel = 0; channel < 2; channel++) {
228 zsc_args.channel = channel;
229 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
230 ch = &zsc->zsc_cs_store[channel];
231 cs = zsc->zsc_cs[channel] = (struct zs_chanstate *)ch;
232
233 zs_lock_init(cs);
234 cs->cs_reg_csr = NULL;
235 cs->cs_reg_data = NULL;
236 cs->cs_channel = channel;
237 cs->cs_private = NULL;
238 cs->cs_ops = &zsops_null;
239 cs->cs_brg_clk = PCLK / 16;
240
241 if (bus_space_subregion(ca->ca_bustag, zsc->zsc_base,
242 zs_chan_offset[channel],
243 sizeof(struct zschan),
244 &ch->cs_regs) != 0) {
245 aprint_error_dev(self, ": cannot map regs\n");
246 return;
247 }
248 ch->cs_bustag = ca->ca_bustag;
249
250 memcpy(cs->cs_creg, zs_init_reg, 16);
251 memcpy(cs->cs_preg, zs_init_reg, 16);
252
253 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
254 cs->cs_defspeed = zs_get_speed(cs);
255 else
256 cs->cs_defspeed = zs_defspeed;
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 * Look for a child driver for this channel.
275 * The child attach will setup the hardware.
276 */
277 if (!config_found(self, (void *)&zsc_args, zs_print)) {
278 /* No sub-driver. Just reset it. */
279 uint8_t reset = (channel == 0) ?
280 ZSWR9_A_RESET : ZSWR9_B_RESET;
281
282 s = splhigh();
283 zs_write_reg(cs, 9, reset);
284 splx(s);
285 }
286 }
287
288
289 zsc->sc_si = softint_establish(SOFTINT_SERIAL, zssoft, zsc);
290 bus_intr_establish(zsc->zsc_bustag, SYS_INTR_SCC0, 0, 0, zshard, NULL);
291
292 evcnt_attach_dynamic(&zsc->zs_intrcnt, EVCNT_TYPE_INTR, NULL,
293 device_xname(self), "intr");
294
295 /*
296 * Set the master interrupt enable and interrupt vector.
297 * (common to both channels, do it on A)
298 */
299 cs = zsc->zsc_cs[0];
300 s = splhigh();
301 /* interrupt vector */
302 zs_write_reg(cs, 2, zs_init_reg[2]);
303 /* master interrupt control (enable) */
304 zs_write_reg(cs, 9, zs_init_reg[9]);
305 splx(s);
306 }
307
308 static int
309 zs_print(void *aux, const char *name)
310 {
311 struct zsc_attach_args *args = aux;
312
313 if (name != NULL)
314 aprint_normal("%s: ", name);
315
316 if (args->channel != -1)
317 aprint_normal(" channel %d", args->channel);
318
319 return UNCONF;
320 }
321
322 /*
323 * Our ZS chips all share a common, autovectored interrupt,
324 * so we have to look at all of them on each interrupt.
325 */
326 static int
327 zshard(void *arg)
328 {
329 struct zsc_softc *zsc;
330 int unit, rval, softreq;
331
332 rval = 0;
333 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
334 zsc = device_private(zsc_cd.cd_devs[unit]);
335 if (zsc == NULL)
336 continue;
337 rval |= zsc_intr_hard(zsc);
338 softreq = zsc->zsc_cs[0]->cs_softreq;
339 softreq |= zsc->zsc_cs[1]->cs_softreq;
340 if (softreq && (zssoftpending == 0)) {
341 zssoftpending = 1;
342 softint_schedule(zsc->sc_si);
343 }
344 zsc->zs_intrcnt.ev_count++;
345 }
346 return rval;
347 }
348
349 /*
350 * Similar scheme as for zshard (look at all of them)
351 */
352 void
353 zssoft(void *arg)
354 {
355 struct zsc_softc *zsc;
356 int s, unit;
357
358 /* This is not the only ISR on this IPL. */
359 if (zssoftpending == 0)
360 return;
361
362 /*
363 * The soft intr. bit will be set by zshard only if
364 * the variable zssoftpending is zero. The order of
365 * these next two statements prevents our clearing
366 * the soft intr bit just after zshard has set it.
367 */
368 /*isr_soft_clear(ZSSOFT_PRI);*/
369 zssoftpending = 0;
370
371 /* Make sure we call the tty layer at spltty. */
372 s = spltty();
373 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
374 zsc = device_private(zsc_cd.cd_devs[unit]);
375 if (zsc == NULL)
376 continue;
377 (void)zsc_intr_soft(zsc);
378 }
379 splx(s);
380 return;
381 }
382
383
384 /*
385 * Compute the current baud rate given a ZS channel.
386 */
387 static int
388 zs_get_speed(struct zs_chanstate *cs)
389 {
390 int tconst;
391
392 tconst = zs_read_reg(cs, 12);
393 tconst |= zs_read_reg(cs, 13) << 8;
394 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
395 }
396
397 /*
398 * MD functions for setting the baud rate and control modes.
399 */
400 int
401 zs_set_speed(struct zs_chanstate *cs, int bps)
402 {
403 int tconst, real_bps;
404
405 #if 0
406 while (!(zs_read_csr(cs) & ZSRR0_TX_READY))
407 {/*nop*/}
408 #endif
409 /* Wait for transmit buffer to empty */
410 if (bps == 0) {
411 return (0);
412 }
413
414 #ifdef DIAGNOSTIC
415 if (cs->cs_brg_clk == 0)
416 panic("zs_set_speed");
417 #endif
418
419 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
420 if (tconst < 0)
421 return (EINVAL);
422
423 /* Convert back to make sure we can do it. */
424 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
425
426 /* XXX - Allow some tolerance here? */
427 #if 0
428 if (real_bps != bps)
429 return (EINVAL);
430 #endif
431
432 cs->cs_preg[12] = tconst;
433 cs->cs_preg[13] = tconst >> 8;
434
435 /* Caller will stuff the pending registers. */
436 return (0);
437 }
438
439 int
440 zs_set_modes(struct zs_chanstate *cs, int cflag)
441 {
442 int s;
443
444 /*
445 * Output hardware flow control on the chip is horrendous:
446 * if carrier detect drops, the receiver is disabled, and if
447 * CTS drops, the transmitter is stoped IN MID CHARACTER!
448 * Therefore, NEVER set the HFC bit, and instead use the
449 * status interrupt to detect CTS changes.
450 */
451 s = splzs();
452 cs->cs_rr0_pps = 0;
453 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
454 cs->cs_rr0_dcd = 0;
455 if ((cflag & MDMBUF) == 0)
456 cs->cs_rr0_pps = ZSRR0_DCD;
457 } else
458 cs->cs_rr0_dcd = ZSRR0_DCD;
459 if ((cflag & CRTSCTS) != 0) {
460 cs->cs_wr5_dtr = ZSWR5_DTR;
461 cs->cs_wr5_rts = ZSWR5_RTS;
462 cs->cs_rr0_cts = ZSRR0_CTS;
463 } else if ((cflag & MDMBUF) != 0) {
464 cs->cs_wr5_dtr = 0;
465 cs->cs_wr5_rts = ZSWR5_DTR;
466 cs->cs_rr0_cts = ZSRR0_DCD;
467 } else {
468 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
469 cs->cs_wr5_rts = 0;
470 cs->cs_rr0_cts = 0;
471 }
472 splx(s);
473
474 /* Caller will stuff the pending registers. */
475 return (0);
476 }
477
478
479 /*
480 * Read or write the chip with suitable delays.
481 */
482
483 uint8_t
484 zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
485 {
486 uint8_t val;
487 struct zs_channel *zsc = (struct zs_channel *)cs;
488
489 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
490 ZS_DELAY();
491 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
492 ZS_DELAY();
493 return val;
494 }
495
496 void
497 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
498 {
499 struct zs_channel *zsc = (struct zs_channel *)cs;
500
501 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
502 ZS_DELAY();
503 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
504 ZS_DELAY();
505 }
506
507 uint8_t
508 zs_read_csr(struct zs_chanstate *cs)
509 {
510 struct zs_channel *zsc = (struct zs_channel *)cs;
511 uint8_t val;
512
513 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
514 ZS_DELAY();
515 return val;
516 }
517
518 void
519 zs_write_csr(struct zs_chanstate *cs, uint8_t val)
520 {
521 struct zs_channel *zsc = (struct zs_channel *)cs;
522
523 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
524 ZS_DELAY();
525 }
526
527 uint8_t
528 zs_read_data(struct zs_chanstate *cs)
529 {
530 struct zs_channel *zsc = (struct zs_channel *)cs;
531 uint8_t val;
532
533 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA);
534 ZS_DELAY();
535 return val;
536 }
537
538 void
539 zs_write_data(struct zs_chanstate *cs, uint8_t val)
540 {
541 struct zs_channel *zsc = (struct zs_channel *)cs;
542
543 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA, val);
544 ZS_DELAY();
545 }
546
547 void
548 zs_abort(struct zs_chanstate *cs)
549 {
550
551 #if defined(KGDB)
552 zskgdb(cs);
553 #elif defined(DDB)
554 Debugger();
555 #endif
556 }
557
558
559 /*********************************************************/
560 /* Polled character I/O functions for console and KGDB */
561 /*********************************************************/
562
563 struct zschan *
564 zs_get_chan_addr(int zs_unit, int channel)
565 {
566 struct zsdevice *addr;
567 struct zschan *zc;
568
569 if (zs_unit >= NZS)
570 return NULL;
571
572 addr = (struct zsdevice *) ZS0_ADDR;
573
574 if (channel == 0) {
575 zc = &addr->zs_chan_a;
576 } else {
577 zc = &addr->zs_chan_b;
578 }
579 return (zc);
580 }
581
582 int
583 zs_getc(void *arg)
584 {
585 volatile struct zschan *zc = arg;
586 int s, c;
587 uint8_t rr0;
588
589 s = splhigh();
590 /* Wait for a character to arrive. */
591 do {
592 rr0 = zc->zc_csr;
593 ZS_DELAY();
594 } while ((rr0 & ZSRR0_RX_READY) == 0);
595
596 c = zc->zc_data;
597 ZS_DELAY();
598 splx(s);
599
600 return (c);
601 }
602
603 /*
604 * Polled output char.
605 */
606 void
607 zs_putc(void *arg, int c)
608 {
609 volatile struct zschan *zc = arg;
610 int s;
611 uint8_t rr0;
612
613 s = splhigh();
614 /* Wait for transmitter to become ready. */
615 do {
616 rr0 = zc->zc_csr;
617 ZS_DELAY();
618 } while ((rr0 & ZSRR0_TX_READY) == 0);
619
620 zc->zc_data = c;
621 wbflush();
622 ZS_DELAY();
623 splx(s);
624 }
625
626 /***************************************************************/
627
628 static void zscnprobe(struct consdev *);
629 static void zscninit(struct consdev *);
630 static int zscngetc(dev_t);
631 static void zscnputc(dev_t, int);
632 static void zscnpollc(dev_t, int);
633
634 static int cons_port;
635
636 struct consdev consdev_zs = {
637 zscnprobe,
638 zscninit,
639 zscngetc,
640 zscnputc,
641 zscnpollc
642 };
643
644 void
645 zscnprobe(struct consdev *cn)
646 {
647 }
648
649 void
650 zscninit(struct consdev *cn)
651 {
652 extern const struct cdevsw zstty_cdevsw;
653
654 cons_port = prom_getconsole();
655 cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), cons_port);
656 cn->cn_pri = CN_REMOTE;
657 zs_hwflags[0][cons_port] = ZS_HWFLAG_CONSOLE;
658 }
659
660 int
661 zscngetc(dev_t dev)
662 {
663 struct zschan *zs;
664
665 zs = zs_get_chan_addr(0, cons_port);
666 return zs_getc(zs);
667 }
668
669 void
670 zscnputc(dev_t dev, int c)
671 {
672 struct zschan *zs;
673
674 zs = zs_get_chan_addr(0, cons_port);
675 zs_putc(zs, c);
676 }
677
678 void
679 zscnpollc(dev_t dev, int on)
680 {
681 }
682