1 /* $NetBSD: pcdisplay.c,v 1.47 2021/08/07 16:19:12 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pcdisplay.c,v 1.47 2021/08/07 16:19:12 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/malloc.h>
37 #include <sys/bus.h>
38
39 #include <dev/isa/isavar.h>
40
41 #include <dev/ic/mc6845reg.h>
42 #include <dev/ic/pcdisplayvar.h>
43 #include <dev/isa/pcdisplayvar.h>
44
45 #include <dev/ic/pcdisplay.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49
50 #include "pcweasel.h"
51 #if NPCWEASEL > 0
52 #include <dev/isa/weaselreg.h>
53 #include <dev/isa/weaselvar.h>
54 #endif
55
56 struct pcdisplay_config {
57 struct pcdisplayscreen pcs;
58 struct pcdisplay_handle dc_ph;
59 int mono;
60 };
61
62 struct pcdisplay_softc {
63 struct pcdisplay_config *sc_dc;
64 int nscreens;
65 #if NPCWEASEL > 0
66 struct weasel_handle sc_weasel;
67 #endif
68 };
69
70 static int pcdisplayconsole, pcdisplay_console_attached;
71 static struct pcdisplay_config pcdisplay_console_dc;
72
73 int pcdisplay_match(device_t, cfdata_t, void *);
74 void pcdisplay_attach(device_t, device_t, void *);
75
76 static int pcdisplay_is_console(bus_space_tag_t);
77 static int pcdisplay_probe_col(bus_space_tag_t, bus_space_tag_t);
78 static int pcdisplay_probe_mono(bus_space_tag_t, bus_space_tag_t);
79 static void pcdisplay_init(struct pcdisplay_config *,
80 bus_space_tag_t, bus_space_tag_t,
81 int);
82 static int pcdisplay_allocattr(void *, int, int, int, long *);
83
84 CFATTACH_DECL_NEW(pcdisplay, sizeof(struct pcdisplay_softc),
85 pcdisplay_match, pcdisplay_attach, NULL, NULL);
86
87 const struct wsdisplay_emulops pcdisplay_emulops = {
88 pcdisplay_cursor,
89 pcdisplay_mapchar,
90 pcdisplay_putchar,
91 pcdisplay_copycols,
92 pcdisplay_erasecols,
93 pcdisplay_copyrows,
94 pcdisplay_eraserows,
95 pcdisplay_allocattr,
96 NULL, /* replaceattr */
97 };
98
99 const struct wsscreen_descr pcdisplay_scr = {
100 "80x25", 80, 25,
101 &pcdisplay_emulops,
102 0, 0, /* no font support */
103 WSSCREEN_REVERSE, /* that's minimal... */
104 NULL, /* modecookie */
105 };
106
107 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
108 &pcdisplay_scr,
109 };
110
111 const struct wsscreen_list pcdisplay_screenlist = {
112 sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
113 _pcdisplay_scrlist
114 };
115
116 static int pcdisplay_ioctl(void *, void *, u_long, void *, int, struct lwp *);
117 static paddr_t pcdisplay_mmap(void *, void *, off_t, int);
118 static int pcdisplay_alloc_screen(void *, const struct wsscreen_descr *,
119 void **, int *, int *, long *);
120 static void pcdisplay_free_screen(void *, void *);
121 static int pcdisplay_show_screen(void *, void *, int,
122 void (*) (void *, int, int), void *);
123
124 const struct wsdisplay_accessops pcdisplay_accessops = {
125 pcdisplay_ioctl,
126 pcdisplay_mmap,
127 pcdisplay_alloc_screen,
128 pcdisplay_free_screen,
129 pcdisplay_show_screen,
130 NULL, /* load_font */
131 NULL, /* pollc */
132 NULL, /* scroll */
133 };
134
135 static int
pcdisplay_probe_col(bus_space_tag_t iot,bus_space_tag_t memt)136 pcdisplay_probe_col(bus_space_tag_t iot, bus_space_tag_t memt)
137 {
138 bus_space_handle_t memh, ioh_6845;
139 u_int16_t oldval, val;
140
141 if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
142 return (0);
143 oldval = bus_space_read_2(memt, memh, 0);
144 bus_space_write_2(memt, memh, 0, 0xa55a);
145 val = bus_space_read_2(memt, memh, 0);
146 bus_space_write_2(memt, memh, 0, oldval);
147 bus_space_unmap(memt, memh, 0x8000);
148 if (val != 0xa55a)
149 return (0);
150
151 if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
152 return (0);
153 bus_space_unmap(iot, ioh_6845, 0x10);
154
155 return (1);
156 }
157
158 static int
pcdisplay_probe_mono(bus_space_tag_t iot,bus_space_tag_t memt)159 pcdisplay_probe_mono(bus_space_tag_t iot, bus_space_tag_t memt)
160 {
161 bus_space_handle_t memh, ioh_6845;
162 u_int16_t oldval, val;
163
164 if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
165 return (0);
166 oldval = bus_space_read_2(memt, memh, 0);
167 bus_space_write_2(memt, memh, 0, 0xa55a);
168 val = bus_space_read_2(memt, memh, 0);
169 bus_space_write_2(memt, memh, 0, oldval);
170 bus_space_unmap(memt, memh, 0x8000);
171 if (val != 0xa55a)
172 return (0);
173
174 if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
175 return (0);
176 bus_space_unmap(iot, ioh_6845, 0x10);
177
178 return (1);
179 }
180
181 static void
pcdisplay_init(struct pcdisplay_config * dc,bus_space_tag_t iot,bus_space_tag_t memt,int mono)182 pcdisplay_init(struct pcdisplay_config *dc, bus_space_tag_t iot,
183 bus_space_tag_t memt, int mono)
184 {
185 struct pcdisplay_handle *ph = &dc->dc_ph;
186 int cpos;
187
188 ph->ph_iot = iot;
189 ph->ph_memt = memt;
190 dc->mono = mono;
191
192 if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
193 BUS_SPACE_MAP_CACHEABLE, &ph->ph_memh))
194 panic("pcdisplay_init: cannot map memory");
195 if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
196 0, &ph->ph_ioh_6845))
197 panic("pcdisplay_init: cannot map io");
198
199 /*
200 * initialize the only screen
201 */
202 dc->pcs.hdl = ph;
203 dc->pcs.type = &pcdisplay_scr;
204 dc->pcs.active = 1;
205 dc->pcs.mem = NULL;
206
207 cpos = pcdisplay_6845_read(ph, cursorh) << 8;
208 cpos |= pcdisplay_6845_read(ph, cursorl);
209
210 /* make sure we have a valid cursor position */
211 if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
212 cpos = 0;
213
214 dc->pcs.dispoffset = 0;
215
216 dc->pcs.cursorrow = cpos / pcdisplay_scr.ncols;
217 dc->pcs.cursorcol = cpos % pcdisplay_scr.ncols;
218 pcdisplay_cursor_init(&dc->pcs, 1);
219 }
220
221 int
pcdisplay_match(device_t parent,cfdata_t match,void * aux)222 pcdisplay_match(device_t parent, cfdata_t match, void *aux)
223 {
224 struct isa_attach_args *ia = aux;
225 int mono;
226
227 if (ISA_DIRECT_CONFIG(ia))
228 return (0);
229
230 /* If values are hardwired to something that they can't be, punt. */
231 if (ia->ia_nio < 1 ||
232 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
233 ia->ia_io[0].ir_addr != 0x3d0 &&
234 ia->ia_io[0].ir_addr != 0x3b0))
235 return (0);
236
237 if (ia->ia_niomem < 1 ||
238 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
239 ia->ia_iomem[0].ir_addr != 0xb8000 &&
240 ia->ia_iomem[0].ir_addr != 0xb0000))
241 return (0);
242 if (ia->ia_iomem[0].ir_size != 0 &&
243 ia->ia_iomem[0].ir_size != 0x8000)
244 return (0);
245
246 if (ia->ia_nirq > 0 &&
247 ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ)
248 return (0);
249
250 if (ia->ia_ndrq > 0 &&
251 ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)
252 return (0);
253
254 if (pcdisplay_is_console(ia->ia_iot))
255 mono = pcdisplay_console_dc.mono;
256 else if (ia->ia_io[0].ir_addr != 0x3b0 &&
257 ia->ia_iomem[0].ir_addr != 0xb0000 &&
258 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
259 mono = 0;
260 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
261 ia->ia_iomem[0].ir_addr != 0xb8000 &&
262 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
263 mono = 1;
264 else
265 return (0);
266
267 ia->ia_nio = 1;
268 ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
269 ia->ia_io[0].ir_size = 0x10;
270
271 ia->ia_niomem = 1;
272 ia->ia_iomem[0].ir_addr = mono ? 0xb0000 : 0xb8000;
273 ia->ia_iomem[0].ir_size = 0x8000;
274
275 ia->ia_nirq = 0;
276 ia->ia_ndrq = 0;
277
278 return (1);
279 }
280
281 void
pcdisplay_attach(device_t parent,device_t self,void * aux)282 pcdisplay_attach(device_t parent, device_t self, void *aux)
283 {
284 struct isa_attach_args *ia = aux;
285 struct pcdisplay_softc *sc = device_private(self);
286 int console;
287 struct pcdisplay_config *dc;
288 struct wsemuldisplaydev_attach_args aa;
289
290 printf("\n");
291
292 console = pcdisplay_is_console(ia->ia_iot);
293
294 if (console) {
295 dc = &pcdisplay_console_dc;
296 sc->nscreens = 1;
297 pcdisplay_console_attached = 1;
298 } else {
299 dc = malloc(sizeof(struct pcdisplay_config),
300 M_DEVBUF, M_WAITOK);
301 if (ia->ia_io[0].ir_addr != 0x3b0 &&
302 ia->ia_iomem[0].ir_addr != 0xb0000 &&
303 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
304 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
305 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
306 ia->ia_iomem[0].ir_addr != 0xb8000 &&
307 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
308 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
309 else
310 panic("pcdisplay_attach: display disappeared");
311 }
312 sc->sc_dc = dc;
313
314 #if NPCWEASEL > 0
315 /*
316 * If the display is monochrome, check to see if we have
317 * a PC-Weasel, and initialize its special features.
318 */
319 if (dc->mono) {
320 sc->sc_weasel.wh_st = dc->dc_ph.ph_memt;
321 sc->sc_weasel.wh_sh = dc->dc_ph.ph_memh;
322 sc->sc_weasel.wh_parent = self;
323 weasel_isa_init(&sc->sc_weasel);
324 }
325 #endif /* NPCWEASEL > 0 */
326
327 aa.console = console;
328 aa.scrdata = &pcdisplay_screenlist;
329 aa.accessops = &pcdisplay_accessops;
330 aa.accesscookie = sc;
331
332 config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);
333 }
334
335
336 int
pcdisplay_cnattach(bus_space_tag_t iot,bus_space_tag_t memt)337 pcdisplay_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
338 {
339 int mono;
340
341 if (pcdisplay_probe_col(iot, memt))
342 mono = 0;
343 else if (pcdisplay_probe_mono(iot, memt))
344 mono = 1;
345 else
346 return (ENXIO);
347
348 pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
349
350 wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
351 pcdisplay_console_dc.pcs.cursorcol,
352 pcdisplay_console_dc.pcs.cursorrow,
353 FG_LIGHTGREY | BG_BLACK);
354
355 pcdisplayconsole = 1;
356 return (0);
357 }
358
359 static int
pcdisplay_is_console(bus_space_tag_t iot)360 pcdisplay_is_console(bus_space_tag_t iot)
361 {
362 if (pcdisplayconsole &&
363 !pcdisplay_console_attached &&
364 bus_space_is_equal(iot, pcdisplay_console_dc.dc_ph.ph_iot))
365 return (1);
366 return (0);
367 }
368
369 static int
pcdisplay_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)370 pcdisplay_ioctl(void *v, void *vs, u_long cmd,
371 void *data, int flag, struct lwp *l)
372 {
373 /*
374 * XXX "do something!"
375 */
376 return (EPASSTHROUGH);
377 }
378
379 static paddr_t
pcdisplay_mmap(void * v,void * vs,off_t offset,int prot)380 pcdisplay_mmap(void *v, void *vs, off_t offset, int prot)
381 {
382 return (-1);
383 }
384
385 static int
pcdisplay_alloc_screen(void * v,const struct wsscreen_descr * type,void ** cookiep,int * curxp,int * curyp,long * defattrp)386 pcdisplay_alloc_screen(void *v, const struct wsscreen_descr *type,
387 void **cookiep, int *curxp, int *curyp, long *defattrp)
388 {
389 struct pcdisplay_softc *sc = v;
390
391 if (sc->nscreens > 0)
392 return (ENOMEM);
393
394 *cookiep = sc->sc_dc;
395 *curxp = 0;
396 *curyp = 0;
397 *defattrp = FG_LIGHTGREY | BG_BLACK;
398 sc->nscreens++;
399 return (0);
400 }
401
402 static void
pcdisplay_free_screen(void * v,void * cookie)403 pcdisplay_free_screen(void *v, void *cookie)
404 {
405 struct pcdisplay_softc *sc = v;
406
407 if (sc->sc_dc == &pcdisplay_console_dc)
408 panic("pcdisplay_free_screen: console");
409
410 sc->nscreens--;
411 }
412
413 static int
pcdisplay_show_screen(void * v,void * cookie,int waitok,void (* cb)(void *,int,int),void * cbarg)414 pcdisplay_show_screen(void *v, void *cookie, int waitok,
415 void (*cb)(void *, int, int), void *cbarg)
416 {
417 #ifdef DIAGNOSTIC
418 struct pcdisplay_softc *sc = v;
419
420 if (cookie != sc->sc_dc)
421 panic("pcdisplay_show_screen: bad screen");
422 #endif
423 return (0);
424 }
425
426 static int
pcdisplay_allocattr(void * id,int fg,int bg,int flags,long * attrp)427 pcdisplay_allocattr(void *id, int fg, int bg, int flags, long *attrp)
428 {
429 if (flags & WSATTR_REVERSE)
430 *attrp = FG_BLACK | BG_LIGHTGREY;
431 else
432 *attrp = FG_LIGHTGREY | BG_BLACK;
433 return (0);
434 }
435