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