zs.c revision 1.26 1 /* $NetBSD: zs.c,v 1.26 2000/11/21 11:41:37 scw 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 * Modified for NetBSD/mvme68k by Jason R. Thorpe <thorpej (at) NetBSD.ORG>
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/device.h>
52 #include <sys/file.h>
53 #include <sys/ioctl.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/tty.h>
57 #include <sys/time.h>
58 #include <sys/syslog.h>
59
60 #include <dev/cons.h>
61 #include <dev/ic/z8530reg.h>
62 #include <machine/z8530var.h>
63
64 #include <machine/cpu.h>
65 #include <machine/bus.h>
66 #include <machine/intr.h>
67
68 #include <mvme68k/dev/zsvar.h>
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 /* XXX Shouldn't hardcode the minor number... */
77 int zs_major = 12;
78
79 /* Flags from zscnprobe() */
80 static int zs_hwflags[NZSC][2];
81
82 /* Default speed for each channel */
83 static int zs_defspeed[NZSC][2] = {
84 { 9600, /* port 1 */
85 9600 }, /* port 2 */
86 { 9600, /* port 3 */
87 9600 }, /* port 4 */
88 };
89
90 static struct zs_chanstate zs_conschan_store;
91 static struct zs_chanstate *zs_conschan;
92
93 u_char zs_init_reg[16] = {
94 0, /* 0: CMD (reset, etc.) */
95 0, /* 1: No interrupts yet. */
96 0x18 + ZSHARD_PRI, /* IVECT */
97 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
98 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
99 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
100 0, /* 6: TXSYNC/SYNCLO */
101 0, /* 7: RXSYNC/SYNCHI */
102 0, /* 8: alias for data port */
103 ZSWR9_MASTER_IE,
104 0, /*10: Misc. TX/RX control bits */
105 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
106 0, /*12: BAUDLO (default=9600) */
107 0, /*13: BAUDHI (default=9600) */
108 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
109 ZSWR15_BREAK_IE,
110 };
111
112
113 /****************************************************************
114 * Autoconfig
115 ****************************************************************/
116
117 /* Definition of the driver for autoconfig. */
118 static int zsc_print __P((void *, const char *name));
119 int zs_getc __P((void *));
120 void zs_putc __P((void *, int));
121
122 #if 0
123 static int zs_get_speed __P((struct zs_chanstate *));
124 #endif
125
126 extern struct cfdriver zsc_cd;
127
128 cons_decl(zsc_pcc);
129
130
131 /*
132 * Configure children of an SCC.
133 */
134 void
135 zs_config(zsc, zs, vector, pclk)
136 struct zsc_softc *zsc;
137 struct zsdevice *zs;
138 int vector, pclk;
139 {
140 struct zsc_attach_args zsc_args;
141 volatile struct zschan *zc;
142 struct zs_chanstate *cs;
143 int zsc_unit, channel, s;
144
145 zsc_unit = zsc->zsc_dev.dv_unit;
146 printf(": Zilog 8530 SCC at vector 0x%x\n", vector);
147
148 /*
149 * Initialize software state for each channel.
150 */
151 for (channel = 0; channel < 2; channel++) {
152 zsc_args.channel = channel;
153 zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
154 cs = &zsc->zsc_cs_store[channel];
155 zsc->zsc_cs[channel] = cs;
156
157 /*
158 * If we're the console, copy the channel state, and
159 * adjust the console channel pointer.
160 */
161 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
162 bcopy(zs_conschan, cs, sizeof(struct zs_chanstate));
163 zs_conschan = cs;
164 } else {
165 zc = (channel == 0) ? &zs->zs_chan_a : &zs->zs_chan_b;
166 cs->cs_reg_csr = zc->zc_csr;
167 cs->cs_reg_data = zc->zc_data;
168 bcopy(zs_init_reg, cs->cs_creg, 16);
169 bcopy(zs_init_reg, cs->cs_preg, 16);
170 cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
171 }
172
173 cs->cs_brg_clk = pclk / 16;
174 cs->cs_creg[2] = cs->cs_preg[2] = vector;
175 zs_set_speed(cs, cs->cs_defspeed);
176 cs->cs_creg[12] = cs->cs_preg[12];
177 cs->cs_creg[13] = cs->cs_preg[13];
178 cs->cs_defcflag = zs_def_cflag;
179
180 /* Make these correspond to cs_defcflag (-crtscts) */
181 cs->cs_rr0_dcd = ZSRR0_DCD;
182 cs->cs_rr0_cts = 0;
183 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
184 cs->cs_wr5_rts = 0;
185
186 cs->cs_channel = channel;
187 cs->cs_private = NULL;
188 cs->cs_ops = &zsops_null;
189
190 /*
191 * Clear the master interrupt enable.
192 * The INTENA is common to both channels,
193 * so just do it on the A channel.
194 * Write the interrupt vector while we're at it.
195 */
196 if (channel == 0) {
197 zs_write_reg(cs, 9, 0);
198 zs_write_reg(cs, 2, vector);
199 }
200
201 /*
202 * Look for a child driver for this channel.
203 * The child attach will setup the hardware.
204 */
205 if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zsc_print)) {
206 /* No sub-driver. Just reset it. */
207 u_char reset = (channel == 0) ?
208 ZSWR9_A_RESET : ZSWR9_B_RESET;
209 s = splzs();
210 zs_write_reg(cs, 9, reset);
211 splx(s);
212 }
213 }
214
215 /*
216 * Allocate a software interrupt cookie.
217 */
218 zsc->zsc_softintr_cookie = softintr_establish(IPL_SOFTSERIAL,
219 (void (*)(void *)) zsc_intr_soft, zsc);
220 #ifdef DEBUG
221 assert(zsc->zsc_softintr_cookie);
222 #endif
223 }
224
225 static int
226 zsc_print(aux, name)
227 void *aux;
228 const char *name;
229 {
230 struct zsc_attach_args *args = aux;
231
232 if (name != NULL)
233 printf("%s: ", name);
234
235 if (args->channel != -1)
236 printf(" channel %d", args->channel);
237
238 return UNCONF;
239 }
240
241 #if defined(MVME162) || defined(MVME172)
242 /*
243 * Our ZS chips each have their own interrupt vector.
244 */
245 int
246 zshard_unshared(arg)
247 void *arg;
248 {
249 struct zsc_softc *zsc = arg;
250 int rval;
251
252 rval = zsc_intr_hard(zsc);
253
254 if ((zsc->zsc_cs[0]->cs_softreq) || (zsc->zsc_cs[1]->cs_softreq))
255 softintr_schedule(zsc->zsc_softintr_cookie);
256
257 return (rval);
258 }
259 #endif
260
261 #ifdef MVME147
262 /*
263 * Our ZS chips all share a common, PCC-vectored interrupt,
264 * so we have to look at all of them on each interrupt.
265 */
266 int
267 zshard_shared(arg)
268 void *arg;
269 {
270 struct zsc_softc *zsc;
271 int unit, rval;
272
273 rval = 0;
274 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
275 zsc = zsc_cd.cd_devs[unit];
276 if (zsc == NULL)
277 continue;
278 rval |= zsc_intr_hard(zsc);
279 if ((zsc->zsc_cs[0]->cs_softreq) ||
280 (zsc->zsc_cs[1]->cs_softreq))
281 softintr_schedule(zsc->zsc_softintr_cookie);
282 }
283 return (rval);
284 }
285 #endif
286
287
288 #if 0
289 /*
290 * Compute the current baud rate given a ZSCC channel.
291 */
292 static int
293 zs_get_speed(cs)
294 struct zs_chanstate *cs;
295 {
296 int tconst;
297
298 tconst = zs_read_reg(cs, 12);
299 tconst |= zs_read_reg(cs, 13) << 8;
300 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
301 }
302 #endif
303
304 /*
305 * MD functions for setting the baud rate and control modes.
306 */
307 int
308 zs_set_speed(cs, bps)
309 struct zs_chanstate *cs;
310 int bps; /* bits per second */
311 {
312 int tconst, real_bps;
313
314 if (bps == 0)
315 return (0);
316
317 #ifdef DIAGNOSTIC
318 if (cs->cs_brg_clk == 0)
319 panic("zs_set_speed");
320 #endif
321
322 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
323 if (tconst < 0)
324 return (EINVAL);
325
326 /* Convert back to make sure we can do it. */
327 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
328
329 /* Allow 2% tolerance WRT the required bps */
330 if (((abs(real_bps - bps) * 1000) / bps) > 20)
331 return (EINVAL);
332
333 cs->cs_preg[12] = tconst;
334 cs->cs_preg[13] = tconst >> 8;
335
336 /* Caller will stuff the pending registers. */
337 return (0);
338 }
339
340 int
341 zs_set_modes(cs, cflag)
342 struct zs_chanstate *cs;
343 int cflag; /* bits per second */
344 {
345 int s;
346
347 /*
348 * Output hardware flow control on the chip is horrendous:
349 * if carrier detect drops, the receiver is disabled, and if
350 * CTS drops, the transmitter is stoped IN MID CHARACTER!
351 * Therefore, NEVER set the HFC bit, and instead use the
352 * status interrupt to detect CTS changes.
353 */
354 s = splzs();
355 cs->cs_rr0_pps = 0;
356 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
357 cs->cs_rr0_dcd = 0;
358 if ((cflag & MDMBUF) == 0)
359 cs->cs_rr0_pps = ZSRR0_DCD;
360 } else
361 cs->cs_rr0_dcd = ZSRR0_DCD;
362 if ((cflag & CRTSCTS) != 0) {
363 cs->cs_wr5_dtr = ZSWR5_DTR;
364 cs->cs_wr5_rts = ZSWR5_RTS;
365 cs->cs_rr0_cts = ZSRR0_CTS;
366 } else if ((cflag & MDMBUF) != 0) {
367 cs->cs_wr5_dtr = 0;
368 cs->cs_wr5_rts = ZSWR5_DTR;
369 cs->cs_rr0_cts = ZSRR0_DCD;
370 } else {
371 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
372 cs->cs_wr5_rts = 0;
373 cs->cs_rr0_cts = 0;
374 }
375 splx(s);
376
377 /* Caller will stuff the pending registers. */
378 return (0);
379 }
380
381
382 /*
383 * Read or write the chip with suitable delays.
384 */
385
386 u_char
387 zs_read_reg(cs, reg)
388 struct zs_chanstate *cs;
389 u_char reg;
390 {
391 u_char val;
392
393 *cs->cs_reg_csr = reg;
394 ZS_DELAY();
395 val = *cs->cs_reg_csr;
396 ZS_DELAY();
397 return val;
398 }
399
400 void
401 zs_write_reg(cs, reg, val)
402 struct zs_chanstate *cs;
403 u_char reg, val;
404 {
405 *cs->cs_reg_csr = reg;
406 ZS_DELAY();
407 *cs->cs_reg_csr = val;
408 ZS_DELAY();
409 }
410
411 u_char zs_read_csr(cs)
412 struct zs_chanstate *cs;
413 {
414 u_char val;
415
416 val = *cs->cs_reg_csr;
417 ZS_DELAY();
418 return val;
419 }
420
421 void zs_write_csr(cs, val)
422 struct zs_chanstate *cs;
423 u_char val;
424 {
425 *cs->cs_reg_csr = val;
426 ZS_DELAY();
427 }
428
429 u_char zs_read_data(cs)
430 struct zs_chanstate *cs;
431 {
432 u_char val;
433
434 val = *cs->cs_reg_data;
435 ZS_DELAY();
436 return val;
437 }
438
439 void zs_write_data(cs, val)
440 struct zs_chanstate *cs;
441 u_char val;
442 {
443 *cs->cs_reg_data = val;
444 ZS_DELAY();
445 }
446
447 /****************************************************************
448 * Console support functions (MVME specific!)
449 ****************************************************************/
450
451 /*
452 * Polled input char.
453 */
454 int
455 zs_getc(arg)
456 void *arg;
457 {
458 struct zs_chanstate *cs = arg;
459 int s, c, rr0, stat;
460
461 s = splhigh();
462 top:
463 /* Wait for a character to arrive. */
464 do {
465 rr0 = *cs->cs_reg_csr;
466 ZS_DELAY();
467 } while ((rr0 & ZSRR0_RX_READY) == 0);
468
469 /* Read error register. */
470 stat = zs_read_reg(cs, 1) & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE);
471 if (stat) {
472 zs_write_csr(cs, ZSM_RESET_ERR);
473 goto top;
474 }
475
476 /* Read character. */
477 c = *cs->cs_reg_data;
478 ZS_DELAY();
479 splx(s);
480
481 return (c);
482 }
483
484 /*
485 * Polled output char.
486 */
487 void
488 zs_putc(arg, c)
489 void *arg;
490 int c;
491 {
492 struct zs_chanstate *cs = arg;
493 int s, rr0;
494
495 s = splhigh();
496 /* Wait for transmitter to become ready. */
497 do {
498 rr0 = *cs->cs_reg_csr;
499 ZS_DELAY();
500 } while ((rr0 & ZSRR0_TX_READY) == 0);
501
502 *cs->cs_reg_data = c;
503 ZS_DELAY();
504 splx(s);
505 }
506
507 /*
508 * Common parts of console init.
509 */
510 void
511 zs_cnconfig(zsc_unit, channel, zs, pclk)
512 int zsc_unit, channel;
513 struct zsdevice *zs;
514 int pclk;
515 {
516 struct zs_chanstate *cs;
517 struct zschan *zc;
518
519 zc = (channel == 0) ? &zs->zs_chan_a : &zs->zs_chan_b;
520
521 /*
522 * Pointer to channel state. Later, the console channel
523 * state is copied into the softc, and the console channel
524 * pointer adjusted to point to the new copy.
525 */
526 zs_conschan = cs = &zs_conschan_store;
527 zs_hwflags[zsc_unit][channel] = ZS_HWFLAG_CONSOLE;
528
529 /* Setup temporary chanstate. */
530 cs->cs_brg_clk = pclk / 16;
531 cs->cs_reg_csr = zc->zc_csr;
532 cs->cs_reg_data = zc->zc_data;
533
534 /* Initialize the pending registers. */
535 bcopy(zs_init_reg, cs->cs_preg, 16);
536 cs->cs_preg[5] |= (ZSWR5_DTR | ZSWR5_RTS);
537
538 #if 0
539 /* XXX: Preserve BAUD rate from boot loader. */
540 /* XXX: Also, why reset the chip here? -gwr */
541 cs->cs_defspeed = zs_get_speed(cs);
542 #else
543 cs->cs_defspeed = 9600; /* XXX */
544 #endif
545 zs_set_speed(cs, cs->cs_defspeed);
546 cs->cs_creg[12] = cs->cs_preg[12];
547 cs->cs_creg[13] = cs->cs_preg[13];
548
549 /* Clear the master interrupt enable. */
550 zs_write_reg(cs, 9, 0);
551
552 /* Reset the whole SCC chip. */
553 zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
554
555 /* Copy "pending" to "current" and H/W. */
556 zs_loadchannelregs(cs);
557 }
558
559 /*
560 * Polled console input putchar.
561 */
562 int
563 zsc_pcccngetc(dev)
564 dev_t dev;
565 {
566 struct zs_chanstate *cs = zs_conschan;
567 int c;
568
569 c = zs_getc(cs);
570 return (c);
571 }
572
573 /*
574 * Polled console output putchar.
575 */
576 void
577 zsc_pcccnputc(dev, c)
578 dev_t dev;
579 int c;
580 {
581 struct zs_chanstate *cs = zs_conschan;
582
583 zs_putc(cs, c);
584 }
585
586 /*
587 * Handle user request to enter kernel debugger.
588 */
589 void
590 zs_abort(cs)
591 struct zs_chanstate *cs;
592 {
593 int rr0;
594
595 /* Wait for end of break to avoid PROM abort. */
596 /* XXX - Limit the wait? */
597 do {
598 rr0 = *cs->cs_reg_csr;
599 ZS_DELAY();
600 } while (rr0 & ZSRR0_BREAK);
601
602 mvme68k_abort("SERIAL LINE ABORT");
603 }
604