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