No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / pmax / ibus / dz_ibus.c
blob4ae3fc2d1acc042f509592139abd1d034e589244
1 /* $NetBSD: dz_ibus.c,v 1.8 2008/03/29 15:59:57 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1998 Ludd, University of Lule}, Sweden.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed at Ludd, University of
47 * Lule}, Sweden and its contributors.
48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: dz_ibus.c,v 1.8 2008/03/29 15:59:57 tsutsui Exp $");
66 #include "dzkbd.h"
67 #include "dzms.h"
69 #include <sys/param.h>
70 #include <sys/proc.h>
71 #include <sys/systm.h>
72 #include <sys/ioctl.h>
73 #include <sys/tty.h>
74 #include <sys/file.h>
75 #include <sys/conf.h>
76 #include <sys/device.h>
77 #include <sys/reboot.h>
79 #include <machine/bus.h>
81 #include <dev/cons.h>
83 #include <dev/tc/tcvar.h> /* tc_addr_t */
85 #include <dev/dec/dzreg.h>
86 #include <dev/dec/dzvar.h>
87 #include <dev/dec/dzkbdvar.h>
89 #include <pmax/ibus/ibusvar.h>
90 #include <pmax/pmax/pmaxtype.h>
91 #include <pmax/pmax/cons.h>
93 #define DZ_LINE_KBD 0
94 #define DZ_LINE_MOUSE 1
95 #define DZ_LINE_CONSOLE 2
96 #define DZ_LINE_AUX 3
98 int dz_ibus_match(device_t, cfdata_t, void *);
99 void dz_ibus_attach(device_t, device_t, void *);
100 int dz_ibus_intr(void *);
101 void dz_ibus_cnsetup(paddr_t);
102 int dz_ibus_cngetc(dev_t);
103 void dz_ibus_cnputc(dev_t, int);
104 void dz_ibus_cnpollc(dev_t, int);
105 int dz_ibus_getmajor(void);
106 int dz_ibus_print(void *, const char *);
108 int dzgetc(struct dz_linestate *);
109 void dzputc(struct dz_linestate *, int);
111 CFATTACH_DECL_NEW(dz_ibus, sizeof(struct dz_softc),
112 dz_ibus_match, dz_ibus_attach, NULL, NULL);
114 struct consdev dz_ibus_consdev = {
115 NULL, NULL, dz_ibus_cngetc, dz_ibus_cnputc,
116 dz_ibus_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
119 struct dzregs {
120 uint16_t csr; /* 00 Csr: control/status */
121 uint16_t p0[3];
123 uint16_t rbuf; /* 08 Rbuf/Lpr: receive buffer/line param */
124 uint16_t p1[3];
126 uint16_t tcr; /* 10 Tcr: transmit console */
127 uint16_t p2[3];
129 uint16_t tdr; /* 18 Msr/Tdr: modem status reg/xmit data */
130 uint16_t p3[3];
131 } volatile *dzcn;
133 int dz_ibus_iscn;
134 int dz_ibus_consln = -1;
137 dz_ibus_match(device_t parent, cfdata_t cf, void *aux)
139 struct ibus_attach_args *iba;
141 iba = aux;
143 if (strcmp(iba->ia_name, "dc") != 0 &&
144 strcmp(iba->ia_name, "mdc") != 0 &&
145 strcmp(iba->ia_name, "dc7085") != 0)
146 return (0);
148 if (badaddr((void *)iba->ia_addr, 2))
149 return (0);
151 return (1);
154 void
155 dz_ibus_attach(device_t parent, device_t self, void *aux)
157 struct ibus_attach_args *iba = aux;
158 struct dz_softc *sc = device_private(self);
159 volatile struct dzregs *dz;
160 struct dzkm_attach_args daa;
161 int i;
164 DELAY(100000);
166 sc->sc_dev = self;
169 * XXX - This is evil and ugly, but... due to the nature of how
170 * bus_space_* works on pmax it will do for the time being.
172 sc->sc_ioh = (bus_space_handle_t)MIPS_PHYS_TO_KSEG1(iba->ia_addr);
174 sc->sc_dr.dr_csr = 0;
175 sc->sc_dr.dr_rbuf = 8;
176 sc->sc_dr.dr_dtr = 17;
177 sc->sc_dr.dr_break = 25;
178 sc->sc_dr.dr_tbuf = 24;
179 sc->sc_dr.dr_tcr = 16;
180 sc->sc_dr.dr_dcd = 25;
181 sc->sc_dr.dr_ring = 24;
183 sc->sc_dr.dr_firstreg = 0;
184 sc->sc_dr.dr_winsize = sizeof(struct dzregs);
186 sc->sc_type = DZ_DZV;
188 dz = (volatile struct dzregs *)sc->sc_ioh;
189 i = dz->tcr;
190 dz->csr = DZ_CSR_MSE | DZ_CSR_TXIE;
191 dz->tcr = 0;
192 wbflush();
193 DELAY(1000);
194 dz->tcr = 1;
195 wbflush();
196 DELAY(100000);
197 dz->tcr = i;
198 wbflush();
200 sc->sc_dsr = 0x0f; /* XXX check if VS has modem ctrl bits */
202 aprint_normal(": DC-7085, 4 lines");
203 ibus_intr_establish(parent, (void *)iba->ia_cookie, IPL_TTY,
204 dz_ibus_intr, sc);
205 dzattach(sc, NULL, dz_ibus_consln);
206 DELAY(10000);
208 if (systype == DS_PMAX || systype == DS_3MAX) {
209 #if NDZKBD > 0
210 if (!dz_ibus_iscn)
211 dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8) |
212 DZ_LPR_8_BIT_CHAR | DZ_LINE_KBD;
213 daa.daa_line = DZ_LINE_KBD;
214 daa.daa_flags = (dz_ibus_iscn ? 0 : DZKBD_CONSOLE);
215 config_found(self, &daa, dz_ibus_print);
216 #endif
217 #if NDZMS > 0
218 dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8) |
219 DZ_LPR_8_BIT_CHAR | DZ_LPR_PARENB | DZ_LPR_OPAR |
220 DZ_LINE_MOUSE;
221 daa.daa_line = DZ_LINE_MOUSE;
222 daa.daa_flags = 0;
223 config_found(self, &daa, dz_ibus_print);
224 #endif
229 dz_ibus_getmajor(void)
231 extern const struct cdevsw dz_cdevsw;
232 static int cache = -1;
234 if (cache != -1)
235 return (cache);
237 return (cache = cdevsw_lookup_major(&dz_cdevsw));
241 dz_ibus_intr(void *cookie)
243 struct dz_softc *sc;
244 volatile struct dzregs *dzr;
245 unsigned csr;
247 sc = cookie;
248 dzr = (volatile struct dzregs *)sc->sc_ioh;
250 while (((csr = dzr->csr) & (DZ_CSR_RX_DONE | DZ_CSR_TX_READY)) != 0) {
251 if ((csr & DZ_CSR_RX_DONE) != 0)
252 dzrint(sc);
253 if ((csr & DZ_CSR_TX_READY) != 0)
254 dzxint(sc);
257 return (0);
260 void
261 dz_ibus_cnsetup(paddr_t addr)
264 dzcn = (void *)MIPS_PHYS_TO_KSEG1(addr);
267 void
268 dz_ibus_cnattach(int line)
271 switch (line) {
272 case 0:
273 line = DZ_LINE_KBD;
274 break;
275 case 4:
276 line = DZ_LINE_CONSOLE;
277 break;
278 default:
279 line = DZ_LINE_AUX;
280 break;
283 dz_ibus_iscn = 1;
284 dz_ibus_consln = line;
286 /* Disable scanning until init is done. */
287 dzcn->csr = 0;
288 wbflush();
289 DELAY(1000);
291 /* Turn on transmitter for the console. */
292 dzcn->tcr = (1 << line);
293 wbflush();
294 DELAY(1000);
296 /* Turn scanning back on. */
297 dzcn->csr = 0x20;
298 wbflush();
299 DELAY(1000);
302 * Point the console at the DZ-11.
304 cn_tab = &dz_ibus_consdev;
305 cn_tab->cn_pri = CN_REMOTE;
306 cn_tab->cn_dev = makedev(dz_ibus_getmajor(), line);
310 dz_ibus_cngetc(dev_t dev)
312 int c, line, s;
313 uint16_t rbuf;
315 c = 0;
316 line = minor(dev);
317 s = spltty();
319 do {
320 while ((dzcn->csr & DZ_CSR_RX_DONE) == 0)
321 DELAY(10);
322 DELAY(10);
323 rbuf = dzcn->rbuf;
324 if (((rbuf >> 8) & 3) != line)
325 continue;
326 c = rbuf & 0x7f;
327 } while (c == 17 || c == 19); /* ignore XON/XOFF */
329 splx(s);
330 if (c == 13)
331 c = 10;
332 return (c);
335 void
336 dz_ibus_cnputc(dev_t dev, int ch)
338 int timeout, s;
339 uint16_t tcr;
341 s = spltty();
343 /* Don't hang the machine! */
344 timeout = 1 << 15;
346 /* Remember which lines to scan */
347 tcr = dzcn->tcr;
348 dzcn->tcr = (1 << minor(dev));
349 wbflush();
350 DELAY(10);
352 /* Wait until ready */
353 while ((dzcn->csr & 0x8000) == 0)
354 if (--timeout < 0)
355 break;
356 DELAY(10);
358 /* Put the character */
359 dzcn->tdr = ch;
360 timeout = 1 << 15;
361 wbflush();
362 DELAY(10);
364 /* Wait until ready */
365 while ((dzcn->csr & 0x8000) == 0)
366 if (--timeout < 0)
367 break;
369 DELAY(10);
370 dzcn->tcr = tcr;
371 wbflush();
372 DELAY(10);
373 splx(s);
376 void
377 dz_ibus_cnpollc(dev_t dev, int pollflag)
382 #if NDZKBD > 0 || NDZMS > 0
384 dz_ibus_print(void *aux, const char *pnp)
386 struct dzkm_attach_args *daa;
388 daa = aux;
389 if (pnp != NULL)
390 aprint_normal("lkkbd/vsms at %s", pnp);
391 aprint_normal(" line %d", daa->daa_line);
392 return (UNCONF);
396 dzgetc(struct dz_linestate *ls)
398 volatile struct dzregs *dzr;
399 int line, s;
400 uint16_t rbuf;
402 if (ls == NULL) {
404 * dzkbd is the only thing that should put us here.
406 line = DZ_LINE_KBD;
407 dzr = dzcn;
408 } else {
409 line = ls->dz_line;
410 dzr = (volatile struct dzregs *)ls->dz_sc->sc_ioh;
413 s = spltty();
414 for (;;) {
415 while ((dzr->csr & DZ_CSR_RX_DONE) == 0)
416 DELAY(10);
417 DELAY(10);
418 rbuf = dzr->rbuf;
419 DELAY(10);
420 if (((rbuf >> 8) & 3) == line)
421 return (rbuf & 0xff);
423 splx(s);
426 void
427 dzputc(struct dz_linestate *ls, int ch)
429 volatile struct dzregs *dzr;
430 int line;
431 uint16_t tcr;
432 int s;
435 * If the dz has already been attached, the MI driver will do the
436 * transmitting:
438 if (ls != NULL && ls->dz_sc != NULL) {
439 line = ls->dz_line;
440 dzr = (volatile struct dzregs *)ls->dz_sc->sc_ioh;
441 s = spltty();
442 putc(ch, &ls->dz_tty->t_outq);
443 tcr = dzr->tcr;
444 if ((tcr & (1 << line)) == 0) {
445 dzr->tcr = tcr | (1 << line);
446 wbflush();
447 DELAY(10);
449 dzxint(ls->dz_sc);
450 splx(s);
451 return;
454 /* Use dzcnputc to do the transmitting. */
455 dz_ibus_cnputc(makedev(dz_ibus_getmajor(), DZ_LINE_KBD), ch);
457 #endif /* NDZKBD > 0 || NDZMS > 0 */