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