pcdisplay.c revision 1.30 1 /* $NetBSD: pcdisplay.c,v 1.30 2006/09/03 06:46:22 christos 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.30 2006/09/03 06:46:22 christos 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 <machine/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 device sc_dev;
64 struct pcdisplay_config *sc_dc;
65 int nscreens;
66 #if NPCWEASEL > 0
67 struct weasel_handle sc_weasel;
68 #endif
69 };
70
71 static int pcdisplayconsole, pcdisplay_console_attached;
72 static struct pcdisplay_config pcdisplay_console_dc;
73
74 int pcdisplay_match(struct device *, struct cfdata *, void *);
75 void pcdisplay_attach(struct device *, struct device *, void *);
76
77 static int pcdisplay_is_console(bus_space_tag_t);
78 static int pcdisplay_probe_col(bus_space_tag_t, bus_space_tag_t);
79 static int pcdisplay_probe_mono(bus_space_tag_t, bus_space_tag_t);
80 static void pcdisplay_init(struct pcdisplay_config *,
81 bus_space_tag_t, bus_space_tag_t,
82 int);
83 static int pcdisplay_allocattr(void *, int, int, int, long *);
84
85 CFATTACH_DECL(pcdisplay, sizeof(struct pcdisplay_softc),
86 pcdisplay_match, pcdisplay_attach, NULL, NULL);
87
88 const struct wsdisplay_emulops pcdisplay_emulops = {
89 pcdisplay_cursor,
90 pcdisplay_mapchar,
91 pcdisplay_putchar,
92 pcdisplay_copycols,
93 pcdisplay_erasecols,
94 pcdisplay_copyrows,
95 pcdisplay_eraserows,
96 pcdisplay_allocattr,
97 NULL, /* replaceattr */
98 };
99
100 const struct wsscreen_descr pcdisplay_scr = {
101 "80x25", 80, 25,
102 &pcdisplay_emulops,
103 0, 0, /* no font support */
104 WSSCREEN_REVERSE, /* that's minimal... */
105 NULL, /* modecookie */
106 };
107
108 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
109 &pcdisplay_scr,
110 };
111
112 const struct wsscreen_list pcdisplay_screenlist = {
113 sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
114 _pcdisplay_scrlist
115 };
116
117 static int pcdisplay_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
118 static paddr_t pcdisplay_mmap(void *, void *, off_t, int);
119 static int pcdisplay_alloc_screen(void *, const struct wsscreen_descr *,
120 void **, int *, int *, long *);
121 static void pcdisplay_free_screen(void *, void *);
122 static int pcdisplay_show_screen(void *, void *, int,
123 void (*) (void *, int, int), void *);
124
125 const struct wsdisplay_accessops pcdisplay_accessops = {
126 pcdisplay_ioctl,
127 pcdisplay_mmap,
128 pcdisplay_alloc_screen,
129 pcdisplay_free_screen,
130 pcdisplay_show_screen,
131 NULL, /* load_font */
132 NULL, /* pollc */
133 NULL, /* scroll */
134 };
135
136 static int
137 pcdisplay_probe_col(iot, memt)
138 bus_space_tag_t iot, memt;
139 {
140 bus_space_handle_t memh, ioh_6845;
141 u_int16_t oldval, val;
142
143 if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
144 return (0);
145 oldval = bus_space_read_2(memt, memh, 0);
146 bus_space_write_2(memt, memh, 0, 0xa55a);
147 val = bus_space_read_2(memt, memh, 0);
148 bus_space_write_2(memt, memh, 0, oldval);
149 bus_space_unmap(memt, memh, 0x8000);
150 if (val != 0xa55a)
151 return (0);
152
153 if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
154 return (0);
155 bus_space_unmap(iot, ioh_6845, 0x10);
156
157 return (1);
158 }
159
160 static int
161 pcdisplay_probe_mono(iot, memt)
162 bus_space_tag_t iot, memt;
163 {
164 bus_space_handle_t memh, ioh_6845;
165 u_int16_t oldval, val;
166
167 if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
168 return (0);
169 oldval = bus_space_read_2(memt, memh, 0);
170 bus_space_write_2(memt, memh, 0, 0xa55a);
171 val = bus_space_read_2(memt, memh, 0);
172 bus_space_write_2(memt, memh, 0, oldval);
173 bus_space_unmap(memt, memh, 0x8000);
174 if (val != 0xa55a)
175 return (0);
176
177 if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
178 return (0);
179 bus_space_unmap(iot, ioh_6845, 0x10);
180
181 return (1);
182 }
183
184 static void
185 pcdisplay_init(dc, iot, memt, mono)
186 struct pcdisplay_config *dc;
187 bus_space_tag_t iot, memt;
188 int mono;
189 {
190 struct pcdisplay_handle *ph = &dc->dc_ph;
191 int cpos;
192
193 ph->ph_iot = iot;
194 ph->ph_memt = memt;
195 dc->mono = mono;
196
197 if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
198 0, &ph->ph_memh))
199 panic("pcdisplay_init: cannot map memory");
200 if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
201 0, &ph->ph_ioh_6845))
202 panic("pcdisplay_init: cannot map io");
203
204 /*
205 * initialize the only screen
206 */
207 dc->pcs.hdl = ph;
208 dc->pcs.type = &pcdisplay_scr;
209 dc->pcs.active = 1;
210 dc->pcs.mem = NULL;
211
212 cpos = pcdisplay_6845_read(ph, cursorh) << 8;
213 cpos |= pcdisplay_6845_read(ph, cursorl);
214
215 /* make sure we have a valid cursor position */
216 if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
217 cpos = 0;
218
219 dc->pcs.dispoffset = 0;
220
221 dc->pcs.cursorrow = cpos / pcdisplay_scr.ncols;
222 dc->pcs.cursorcol = cpos % pcdisplay_scr.ncols;
223 pcdisplay_cursor_init(&dc->pcs, 1);
224 }
225
226 int
227 pcdisplay_match(parent, match, aux)
228 struct device *parent;
229 struct cfdata *match;
230 void *aux;
231 {
232 struct isa_attach_args *ia = aux;
233 int mono;
234
235 if (ISA_DIRECT_CONFIG(ia))
236 return (0);
237
238 /* If values are hardwired to something that they can't be, punt. */
239 if (ia->ia_nio < 1 ||
240 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
241 ia->ia_io[0].ir_addr != 0x3d0 &&
242 ia->ia_io[0].ir_addr != 0x3b0))
243 return (0);
244
245 if (ia->ia_niomem < 1 ||
246 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
247 ia->ia_iomem[0].ir_addr != 0xb8000 &&
248 ia->ia_iomem[0].ir_addr != 0xb0000))
249 return (0);
250 if (ia->ia_iomem[0].ir_size != 0 &&
251 ia->ia_iomem[0].ir_size != 0x8000)
252 return (0);
253
254 if (ia->ia_nirq > 0 &&
255 ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ)
256 return (0);
257
258 if (ia->ia_ndrq > 0 &&
259 ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)
260 return (0);
261
262 if (pcdisplay_is_console(ia->ia_iot))
263 mono = pcdisplay_console_dc.mono;
264 else if (ia->ia_io[0].ir_addr != 0x3b0 &&
265 ia->ia_iomem[0].ir_addr != 0xb0000 &&
266 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
267 mono = 0;
268 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
269 ia->ia_iomem[0].ir_addr != 0xb8000 &&
270 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
271 mono = 1;
272 else
273 return (0);
274
275 ia->ia_nio = 1;
276 ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
277 ia->ia_io[0].ir_size = 0x10;
278
279 ia->ia_niomem = 1;
280 ia->ia_iomem[0].ir_size = mono ? 0xb0000 : 0xb8000;
281 ia->ia_iomem[0].ir_size = 0x8000;
282
283 ia->ia_nirq = 0;
284 ia->ia_ndrq = 0;
285
286 return (1);
287 }
288
289 void
290 pcdisplay_attach(parent, self, aux)
291 struct device *parent, *self;
292 void *aux;
293 {
294 struct isa_attach_args *ia = aux;
295 struct pcdisplay_softc *sc = (struct pcdisplay_softc *)self;
296 int console;
297 struct pcdisplay_config *dc;
298 struct wsemuldisplaydev_attach_args aa;
299
300 printf("\n");
301
302 console = pcdisplay_is_console(ia->ia_iot);
303
304 if (console) {
305 dc = &pcdisplay_console_dc;
306 sc->nscreens = 1;
307 pcdisplay_console_attached = 1;
308 } else {
309 dc = malloc(sizeof(struct pcdisplay_config),
310 M_DEVBUF, M_WAITOK);
311 if (ia->ia_io[0].ir_addr != 0x3b0 &&
312 ia->ia_iomem[0].ir_addr != 0xb0000 &&
313 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
314 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
315 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
316 ia->ia_iomem[0].ir_addr != 0xb8000 &&
317 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
318 pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
319 else
320 panic("pcdisplay_attach: display disappeared");
321 }
322 sc->sc_dc = dc;
323
324 #if NPCWEASEL > 0
325 /*
326 * If the display is monochrome, check to see if we have
327 * a PC-Weasel, and initialize its special features.
328 */
329 if (dc->mono) {
330 sc->sc_weasel.wh_st = dc->dc_ph.ph_memt;
331 sc->sc_weasel.wh_sh = dc->dc_ph.ph_memh;
332 sc->sc_weasel.wh_parent = &sc->sc_dev;
333 weasel_isa_init(&sc->sc_weasel);
334 }
335 #endif /* NPCWEASEL > 0 */
336
337 aa.console = console;
338 aa.scrdata = &pcdisplay_screenlist;
339 aa.accessops = &pcdisplay_accessops;
340 aa.accesscookie = sc;
341
342 config_found(self, &aa, wsemuldisplaydevprint);
343 }
344
345
346 int
347 pcdisplay_cnattach(iot, memt)
348 bus_space_tag_t iot, memt;
349 {
350 int mono;
351
352 if (pcdisplay_probe_col(iot, memt))
353 mono = 0;
354 else if (pcdisplay_probe_mono(iot, memt))
355 mono = 1;
356 else
357 return (ENXIO);
358
359 pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
360
361 wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
362 pcdisplay_console_dc.pcs.cursorcol,
363 pcdisplay_console_dc.pcs.cursorrow,
364 FG_LIGHTGREY | BG_BLACK);
365
366 pcdisplayconsole = 1;
367 return (0);
368 }
369
370 static int
371 pcdisplay_is_console(iot)
372 bus_space_tag_t iot;
373 {
374 if (pcdisplayconsole &&
375 !pcdisplay_console_attached &&
376 iot == pcdisplay_console_dc.dc_ph.ph_iot)
377 return (1);
378 return (0);
379 }
380
381 static int
382 pcdisplay_ioctl(v, vs, cmd, data, flag, l)
383 void *v;
384 void *vs;
385 u_long cmd;
386 caddr_t data;
387 int flag;
388 struct lwp *l;
389 {
390 /*
391 * XXX "do something!"
392 */
393 return (EPASSTHROUGH);
394 }
395
396 static paddr_t
397 pcdisplay_mmap(v, vs, offset, prot)
398 void *v;
399 void *vs;
400 off_t offset;
401 int prot;
402 {
403 return (-1);
404 }
405
406 static int
407 pcdisplay_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
408 void *v;
409 const struct wsscreen_descr *type;
410 void **cookiep;
411 int *curxp, *curyp;
412 long *defattrp;
413 {
414 struct pcdisplay_softc *sc = v;
415
416 if (sc->nscreens > 0)
417 return (ENOMEM);
418
419 *cookiep = sc->sc_dc;
420 *curxp = 0;
421 *curyp = 0;
422 *defattrp = FG_LIGHTGREY | BG_BLACK;
423 sc->nscreens++;
424 return (0);
425 }
426
427 static void
428 pcdisplay_free_screen(v, cookie)
429 void *v;
430 void *cookie;
431 {
432 struct pcdisplay_softc *sc = v;
433
434 if (sc->sc_dc == &pcdisplay_console_dc)
435 panic("pcdisplay_free_screen: console");
436
437 sc->nscreens--;
438 }
439
440 static int
441 pcdisplay_show_screen(v, cookie, waitok, cb, cbarg)
442 void *v;
443 void *cookie;
444 int waitok;
445 void (*cb)(void *, int, int);
446 void *cbarg;
447 {
448 #ifdef DIAGNOSTIC
449 struct pcdisplay_softc *sc = v;
450
451 if (cookie != sc->sc_dc)
452 panic("pcdisplay_show_screen: bad screen");
453 #endif
454 return (0);
455 }
456
457 static int
458 pcdisplay_allocattr(id, fg, bg, flags, attrp)
459 void *id;
460 int fg, bg;
461 int flags;
462 long *attrp;
463 {
464 if (flags & WSATTR_REVERSE)
465 *attrp = FG_BLACK | BG_LIGHTGREY;
466 else
467 *attrp = FG_LIGHTGREY | BG_BLACK;
468 return (0);
469 }
470