pcdisplay.c revision 1.40.18.1 1 /* $NetBSD: pcdisplay.c,v 1.40.18.1 2012/11/20 03:02:10 tls 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.40.18.1 2012/11/20 03:02:10 tls 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
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
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
182 pcdisplay_init(struct pcdisplay_config *dc, bus_space_tag_t iot, bus_space_tag_t memt, int mono)
183 {
184 struct pcdisplay_handle *ph = &dc->dc_ph;
185 int cpos;
186
187 ph->ph_iot = iot;
188 ph->ph_memt = memt;
189 dc->mono = mono;
190
191 if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
192 0, &ph->ph_memh))
193 panic("pcdisplay_init: cannot map memory");
194 if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
195 0, &ph->ph_ioh_6845))
196 panic("pcdisplay_init: cannot map io");
197
198 /*
199 * initialize the only screen
200 */
201 dc->pcs.hdl = ph;
202 dc->pcs.type = &pcdisplay_scr;
203 dc->pcs.active = 1;
204 dc->pcs.mem = NULL;
205
206 cpos = pcdisplay_6845_read(ph, cursorh) << 8;
207 cpos |= pcdisplay_6845_read(ph, cursorl);
208
209 /* make sure we have a valid cursor position */
210 if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
211 cpos = 0;
212
213 dc->pcs.dispoffset = 0;
214
215 dc->pcs.cursorrow = cpos / pcdisplay_scr.ncols;
216 dc->pcs.cursorcol = cpos % pcdisplay_scr.ncols;
217 pcdisplay_cursor_init(&dc->pcs, 1);
218 }
219
220 int
221 pcdisplay_match(device_t parent, cfdata_t match, void *aux)
222 {
223 struct isa_attach_args *ia = aux;
224 int mono;
225
226 if (ISA_DIRECT_CONFIG(ia))
227 return (0);
228
229 /* If values are hardwired to something that they can't be, punt. */
230 if (ia->ia_nio < 1 ||
231 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
232 ia->ia_io[0].ir_addr != 0x3d0 &&
233 ia->ia_io[0].ir_addr != 0x3b0))
234 return (0);
235
236 if (ia->ia_niomem < 1 ||
237 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
238 ia->ia_iomem[0].ir_addr != 0xb8000 &&
239 ia->ia_iomem[0].ir_addr != 0xb0000))
240 return (0);
241 if (ia->ia_iomem[0].ir_size != 0 &&
242 ia->ia_iomem[0].ir_size != 0x8000)
243 return (0);
244
245 if (ia->ia_nirq > 0 &&
246 ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ)
247 return (0);
248
249 if (ia->ia_ndrq > 0 &&
250 ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)
251 return (0);
252
253 if (pcdisplay_is_console(ia->ia_iot))
254 mono = pcdisplay_console_dc.mono;
255 else if (ia->ia_io[0].ir_addr != 0x3b0 &&
256 ia->ia_iomem[0].ir_addr != 0xb0000 &&
257 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
258 mono = 0;
259 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
260 ia->ia_iomem[0].ir_addr != 0xb8000 &&
261 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
262 mono = 1;
263 else
264 return (0);
265
266 ia->ia_nio = 1;
267 ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
268 ia->ia_io[0].ir_size = 0x10;
269
270 ia->ia_niomem = 1;
271 ia->ia_iomem[0].ir_size = mono ? 0xb0000 : 0xb8000;
272 ia->ia_iomem[0].ir_size = 0x8000;
273
274 ia->ia_nirq = 0;
275 ia->ia_ndrq = 0;
276
277 return (1);
278 }
279
280 void
281 pcdisplay_attach(device_t parent, device_t self, void *aux)
282 {
283 struct isa_attach_args *ia = aux;
284 struct pcdisplay_softc *sc = device_private(self);
285 int console;
286 struct pcdisplay_config *dc;
287 struct wsemuldisplaydev_attach_args aa;
288
289 printf("\n");
290
291 console = pcdisplay_is_console(ia->ia_iot);
292
293 if (console) {
294 dc = &pcdisplay_console_dc;
295 sc->nscreens = 1;
296 pcdisplay_console_attached = 1;
297 } else {
298 dc = malloc(sizeof(struct pcdisplay_config),
299 M_DEVBUF, M_WAITOK);
300 if (ia->ia_io[0].ir_addr != 0x3b0 &&
301 ia->ia_iomem[0].ir_addr != 0xb0000 &&
302 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
303 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
304 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
305 ia->ia_iomem[0].ir_addr != 0xb8000 &&
306 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
307 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
308 else
309 panic("pcdisplay_attach: display disappeared");
310 }
311 sc->sc_dc = dc;
312
313 #if NPCWEASEL > 0
314 /*
315 * If the display is monochrome, check to see if we have
316 * a PC-Weasel, and initialize its special features.
317 */
318 if (dc->mono) {
319 sc->sc_weasel.wh_st = dc->dc_ph.ph_memt;
320 sc->sc_weasel.wh_sh = dc->dc_ph.ph_memh;
321 sc->sc_weasel.wh_parent = self;
322 weasel_isa_init(&sc->sc_weasel);
323 }
324 #endif /* NPCWEASEL > 0 */
325
326 aa.console = console;
327 aa.scrdata = &pcdisplay_screenlist;
328 aa.accessops = &pcdisplay_accessops;
329 aa.accesscookie = sc;
330
331 config_found(self, &aa, wsemuldisplaydevprint);
332 }
333
334
335 int
336 pcdisplay_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
337 {
338 int mono;
339
340 if (pcdisplay_probe_col(iot, memt))
341 mono = 0;
342 else if (pcdisplay_probe_mono(iot, memt))
343 mono = 1;
344 else
345 return (ENXIO);
346
347 pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
348
349 wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
350 pcdisplay_console_dc.pcs.cursorcol,
351 pcdisplay_console_dc.pcs.cursorrow,
352 FG_LIGHTGREY | BG_BLACK);
353
354 pcdisplayconsole = 1;
355 return (0);
356 }
357
358 static int
359 pcdisplay_is_console(bus_space_tag_t iot)
360 {
361 if (pcdisplayconsole &&
362 !pcdisplay_console_attached &&
363 bus_space_is_equal(iot, pcdisplay_console_dc.dc_ph.ph_iot))
364 return (1);
365 return (0);
366 }
367
368 static int
369 pcdisplay_ioctl(void *v, void *vs, u_long cmd,
370 void *data, int flag, struct lwp *l)
371 {
372 /*
373 * XXX "do something!"
374 */
375 return (EPASSTHROUGH);
376 }
377
378 static paddr_t
379 pcdisplay_mmap(void *v, void *vs, off_t offset,
380 int prot)
381 {
382 return (-1);
383 }
384
385 static int
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
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
414 pcdisplay_show_screen(void *v, void *cookie,
415 int waitok, void (*cb)(void *, int, int),
416 void *cbarg)
417 {
418 #ifdef DIAGNOSTIC
419 struct pcdisplay_softc *sc = v;
420
421 if (cookie != sc->sc_dc)
422 panic("pcdisplay_show_screen: bad screen");
423 #endif
424 return (0);
425 }
426
427 static int
428 pcdisplay_allocattr(void *id, int fg, int bg,
429 int flags, long *attrp)
430 {
431 if (flags & WSATTR_REVERSE)
432 *attrp = FG_BLACK | BG_LIGHTGREY;
433 else
434 *attrp = FG_LIGHTGREY | BG_BLACK;
435 return (0);
436 }
437