1 /* $NetBSD: dz_vsbus.c,v 1.47 2022/12/12 18:22:32 jakllsch Exp $ */
2 /*
3 * Copyright (c) 1998 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: dz_vsbus.c,v 1.47 2022/12/12 18:22:32 jakllsch Exp $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/cpu.h>
34 #include <sys/device.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/proc.h>
38 #include <sys/tty.h>
39
40 #include <dev/cons.h>
41
42 #include <machine/sid.h>
43 #include <machine/vsbus.h>
44 #include <machine/scb.h>
45
46 #include <arch/vax/vax/gencons.h>
47
48 #include <dev/dec/dzreg.h>
49 #include <dev/dec/dzvar.h>
50
51 #include "ioconf.h"
52 #include "locators.h"
53 #include "dzkbd.h"
54 #include "dzms.h"
55 #include "opt_cputype.h"
56
57 #if NDZKBD > 0 || NDZMS > 0
58 #include <dev/dec/dzkbdvar.h>
59
60 #if 0
61 static struct dz_linestate dz_conslinestate = { NULL, -1, NULL, NULL, NULL };
62 #endif
63 #endif
64
65 static int dz_vsbus_match(device_t, cfdata_t, void *);
66 static void dz_vsbus_attach(device_t, device_t, void *);
67
68 static vaddr_t dz_regs; /* Used for console */
69
70 CFATTACH_DECL_NEW(dz_vsbus, sizeof(struct dz_softc),
71 dz_vsbus_match, dz_vsbus_attach, NULL, NULL);
72
73 #define REG(name) short name; short X##name##X;
74 static volatile struct ss_dz {/* base address of DZ-controller: 0x200A0000 */
75 REG(csr); /* 00 Csr: control/status register */
76 REG(rbuf); /* 04 Rbuf/Lpr: receive buffer/line param reg. */
77 REG(tcr); /* 08 Tcr: transmit console register */
78 REG(tdr); /* 0C Msr/Tdr: modem status reg/transmit data reg */
79 REG(lpr0); /* 10 Lpr0: */
80 REG(lpr1); /* 14 Lpr0: */
81 REG(lpr2); /* 18 Lpr0: */
82 REG(lpr3); /* 1C Lpr0: */
83 } *dz;
84 #undef REG
85
86 cons_decl(dz);
87
88 #if NDZKBD > 0 || NDZMS > 0
89 static int
dz_print(void * aux,const char * name)90 dz_print(void *aux, const char *name)
91 {
92 #if 0
93 #if NDZKBD > 0 || NDZMS > 0
94 struct dz_attach_args *dz_args = aux;
95 if (name == NULL) {
96 aprint_normal (" line %d", dz_args->line);
97 if (dz_args->hwflags & DZ_HWFLAG_CONSOLE)
98 aprint_normal (" (console)");
99 }
100 return (QUIET);
101 #else
102 if (name)
103 aprint_normal ("lkc at %s", name);
104 return (UNCONF);
105 #endif
106 #endif
107 return (UNCONF);
108 }
109 #endif
110
111 static int
dz_vsbus_match(device_t parent,cfdata_t cf,void * aux)112 dz_vsbus_match(device_t parent, cfdata_t cf, void *aux)
113 {
114 struct vsbus_attach_args * const va = aux;
115 volatile struct ss_dz *dzP;
116 short i;
117
118 #if VAX53 || VAX49 || VAXANY
119 if (vax_boardtype == VAX_BTYP_53 || vax_boardtype == VAX_BTYP_49)
120 if (cf->cf_loc[VSBUSCF_CSR] != DZ_CSR_KA49)
121 return 0; /* Ugly */
122 #endif
123
124 dzP = (struct ss_dz *)va->va_addr;
125 i = dzP->tcr;
126 dzP->csr = DZ_CSR_MSE|DZ_CSR_TXIE;
127 dzP->tcr = 0;
128 DELAY(1000);
129 dzP->tcr = 1;
130 DELAY(100000);
131 dzP->tcr = i;
132
133 /* If the device doesn't exist, no interrupt has been generated */
134 return 1;
135 }
136
137 static void
dz_vsbus_attach(device_t parent,device_t self,void * aux)138 dz_vsbus_attach(device_t parent, device_t self, void *aux)
139 {
140 struct dz_softc * const sc = device_private(self);
141 struct vsbus_attach_args * const va = aux;
142 #if NDZKBD > 0
143 extern const struct cdevsw dz_cdevsw;
144 #endif
145 #if NDZKBD > 0 || NDZMS > 0
146 struct dzkm_attach_args daa;
147 #endif
148 int s, consline;
149
150 sc->sc_dev = self;
151 /*
152 * XXX - This is evil and ugly, but...
153 * due to the nature of how bus_space_* works on VAX, this will
154 * be perfectly good until everything is converted.
155 */
156 if (cn_tab->cn_dev != makedev(cdevsw_lookup_major(&dz_cdevsw), 0)) {
157 dz_regs = vax_map_physmem(va->va_paddr, 1);
158 consline = -1;
159 } else
160 consline = minor(cn_tab->cn_dev);
161 sc->sc_ioh = dz_regs;
162 sc->sc_dr.dr_csr = 0;
163 sc->sc_dr.dr_rbuf = 4;
164 sc->sc_dr.dr_dtr = 9;
165 sc->sc_dr.dr_break = 13;
166 sc->sc_dr.dr_tbuf = 12;
167 sc->sc_dr.dr_tcr = 8;
168 sc->sc_dr.dr_dcd = 13;
169 sc->sc_dr.dr_ring = 13;
170
171 sc->sc_dr.dr_firstreg = 0;
172 sc->sc_dr.dr_winsize = 14;
173
174 sc->sc_type = DZ_DZV;
175
176 sc->sc_dsr = 0x0f; /* XXX check if VS has modem ctrl bits */
177
178 scb_vecalloc(va->va_cvec, dzxint, sc, SCB_ISTACK, &sc->sc_tintrcnt);
179 scb_vecalloc(va->va_cvec - 4, dzrint, sc, SCB_ISTACK, &sc->sc_rintrcnt);
180
181 aprint_normal("\n");
182 aprint_normal_dev(self, "4 lines");
183
184 dzattach(sc, NULL, consline);
185 DELAY(10000);
186
187 if (consline != -1)
188 cn_set_magic("\033D"); /* set VAX DDB escape sequence */
189
190 #if NDZKBD > 0
191 /* Don't touch this port if this is the console */
192 if (cn_tab->cn_dev != makedev(cdevsw_lookup_major(&dz_cdevsw), 0)) {
193 dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8)
194 | DZ_LPR_8_BIT_CHAR;
195 daa.daa_line = 0;
196 daa.daa_flags =
197 (cn_tab->cn_pri == CN_INTERNAL ? DZKBD_CONSOLE : 0);
198 config_found(self, &daa, dz_print, CFARGS_NONE);
199 }
200 #endif
201 #if NDZMS > 0
202 dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8) | DZ_LPR_8_BIT_CHAR \
203 | DZ_LPR_PARENB | DZ_LPR_OPAR | 1 /* line */;
204 daa.daa_line = 1;
205 daa.daa_flags = 0;
206 config_found(self, &daa, dz_print, CFARGS_NONE);
207 #endif
208 s = spltty();
209 dzrint(sc);
210 dzxint(sc);
211 splx(s);
212 }
213
214 int
dzcngetc(dev_t dev)215 dzcngetc(dev_t dev)
216 {
217 int c = 0, s;
218 int mino = minor(dev);
219 u_short rbuf;
220
221 s = spltty();
222 do {
223 while ((dz->csr & 0x80) == 0)
224 ; /* Wait for char */
225 rbuf = dz->rbuf;
226 if (((rbuf >> 8) & 3) != mino)
227 continue;
228 c = rbuf & 0x7f;
229 } while (c == 17 || c == 19); /* ignore XON/XOFF */
230 splx(s);
231
232 if (c == 13)
233 c = 10;
234
235 return (c);
236 }
237
238 void
dzcnprobe(struct consdev * cndev)239 dzcnprobe(struct consdev *cndev)
240 {
241 extern vaddr_t iospace;
242 int diagcons;
243 paddr_t ioaddr = 0x200A0000;
244 extern const struct cdevsw dz_cdevsw;
245
246 switch (vax_boardtype) {
247 case VAX_BTYP_410:
248 case VAX_BTYP_420:
249 case VAX_BTYP_43:
250 diagcons = (vax_confdata & 0x20 ? 3 : 0);
251 break;
252
253 case VAX_BTYP_46:
254 case VAX_BTYP_48:
255 diagcons = (vax_confdata & 0x100 ? 3 : 0);
256 break;
257
258 case VAX_BTYP_49:
259 ioaddr = DZ_CSR_KA49;
260 diagcons = (vax_confdata & 8 ? 3 : 0);
261 break;
262
263 case VAX_BTYP_53:
264 ioaddr = DZ_CSR_KA49;
265 diagcons = 3;
266 break;
267
268 default:
269 cndev->cn_pri = CN_DEAD;
270 return;
271 }
272 if (diagcons)
273 cndev->cn_pri = CN_REMOTE;
274 else
275 cndev->cn_pri = CN_NORMAL;
276 cndev->cn_dev = makedev(cdevsw_lookup_major(&dz_cdevsw), diagcons);
277 dz_regs = iospace;
278 dz = (void *)dz_regs;
279 ioaccess(iospace, ioaddr, 1);
280 dz->csr = 0; /* Disable scanning until initting is done */
281 dz->tcr = (1 << minor(cndev->cn_dev)); /* Turn on xmitter */
282 dz->csr = 0x20; /* Turn scanning back on */
283 }
284
285 void
dzcninit(struct consdev * cndev)286 dzcninit(struct consdev *cndev)
287 {
288 dz = (void*)dz_regs;
289
290 dz->csr = 0; /* Disable scanning until initting is done */
291 dz->tcr = (1 << minor(cndev->cn_dev)); /* Turn on xmitter */
292 dz->csr = 0x20; /* Turn scanning back on */
293 }
294
295
296 void
dzcnputc(dev_t dev,int ch)297 dzcnputc(dev_t dev, int ch)
298 {
299 int timeout = 1<<15; /* don't hang the machine! */
300 int mino = minor(dev);
301 int s;
302 u_short tcr;
303
304 if (mfpr(PR_MAPEN) == 0)
305 return;
306
307 s = spltty();
308 tcr = dz->tcr; /* remember which lines to scan */
309 dz->tcr = (1 << mino);
310
311 while ((dz->csr & 0x8000) == 0) /* Wait until ready */
312 if (--timeout < 0)
313 break;
314 dz->tdr = ch; /* Put the character */
315 timeout = 1<<15;
316 while ((dz->csr & 0x8000) == 0) /* Wait until ready */
317 if (--timeout < 0)
318 break;
319
320 dz->tcr = tcr;
321 splx(s);
322 }
323
324 void
dzcnpollc(dev_t dev,int pollflag)325 dzcnpollc(dev_t dev, int pollflag)
326 {
327 static u_char mask;
328
329 if (pollflag)
330 mask = vsbus_setmask(0);
331 else
332 vsbus_setmask(mask);
333 }
334
335 #if NDZKBD > 0 || NDZMS > 0
336 int
dzgetc(struct dz_linestate * ls)337 dzgetc(struct dz_linestate *ls)
338 {
339 int line = ls->dz_line;
340 int s;
341 u_short rbuf;
342
343 s = spltty();
344 for (;;) {
345 for(; (dz->csr & DZ_CSR_RX_DONE) == 0;);
346 rbuf = dz->rbuf;
347 if (((rbuf >> 8) & 3) == line) {
348 splx(s);
349 return (rbuf & 0xff);
350 }
351 }
352 }
353
354 void
dzputc(struct dz_linestate * ls,int ch)355 dzputc(struct dz_linestate *ls, int ch)
356 {
357 int line;
358 u_short tcr;
359 int s;
360 extern const struct cdevsw dz_cdevsw;
361
362 /* if the dz has already been attached, the MI
363 driver will do the transmitting: */
364 if (ls && ls->dz_sc) {
365 s = spltty();
366 line = ls->dz_line;
367 putc(ch, &ls->dz_tty->t_outq);
368 tcr = dz->tcr;
369 if (!(tcr & (1 << line)))
370 dz->tcr = tcr | (1 << line);
371 dzxint(ls->dz_sc);
372 splx(s);
373 return;
374 }
375
376 /* use dzcnputc to do the transmitting: */
377 dzcnputc(makedev(cdevsw_lookup_major(&dz_cdevsw), 0), ch);
378 }
379 #endif /* NDZKBD > 0 || NDZMS > 0 */
380