pcdisplay.c revision 1.6 1 1.6 drochner /* $NetBSD: pcdisplay.c,v 1.6 1999/01/11 21:35:54 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.3 drochner pcdisplay_mapchar,
86 1.2 drochner pcdisplay_putchar,
87 1.1 drochner pcdisplay_copycols,
88 1.1 drochner pcdisplay_erasecols,
89 1.1 drochner pcdisplay_copyrows,
90 1.1 drochner pcdisplay_eraserows,
91 1.1 drochner pcdisplay_alloc_attr
92 1.1 drochner };
93 1.1 drochner
94 1.1 drochner const struct wsscreen_descr pcdisplay_scr = {
95 1.1 drochner "80x25", 80, 25,
96 1.1 drochner &pcdisplay_emulops,
97 1.1 drochner 0, 0, /* no font support */
98 1.1 drochner WSSCREEN_REVERSE /* that's minimal... */
99 1.1 drochner };
100 1.1 drochner
101 1.1 drochner const struct wsscreen_descr *_pcdisplay_scrlist[] = {
102 1.1 drochner &pcdisplay_scr,
103 1.1 drochner };
104 1.1 drochner
105 1.1 drochner const struct wsscreen_list pcdisplay_screenlist = {
106 1.1 drochner sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
107 1.1 drochner _pcdisplay_scrlist
108 1.1 drochner };
109 1.1 drochner
110 1.1 drochner static int pcdisplay_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
111 1.1 drochner static int pcdisplay_mmap __P((void *, off_t, int));
112 1.1 drochner static int pcdisplay_alloc_screen __P((void *, const struct wsscreen_descr *,
113 1.1 drochner void **, int *, int *, long *));
114 1.1 drochner static void pcdisplay_free_screen __P((void *, void *));
115 1.1 drochner static void pcdisplay_show_screen __P((void *, 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.6 drochner 0 /* 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.4 drochner
209 1.4 drochner dc->pcs.dispoffset = 0;
210 1.1 drochner
211 1.1 drochner dc->pcs.vc_crow = cpos / pcdisplay_scr.ncols;
212 1.1 drochner dc->pcs.vc_ccol = cpos % pcdisplay_scr.ncols;
213 1.1 drochner dc->pcs.cursoron = 1;
214 1.1 drochner }
215 1.1 drochner
216 1.1 drochner int
217 1.1 drochner pcdisplay_match(parent, match, aux)
218 1.1 drochner struct device *parent;
219 1.1 drochner struct cfdata *match;
220 1.1 drochner void *aux;
221 1.1 drochner {
222 1.1 drochner struct isa_attach_args *ia = aux;
223 1.1 drochner int mono;
224 1.1 drochner
225 1.1 drochner /* If values are hardwired to something that they can't be, punt. */
226 1.1 drochner if ((ia->ia_iobase != IOBASEUNK &&
227 1.1 drochner ia->ia_iobase != 0x3d0 &&
228 1.1 drochner ia->ia_iobase != 0x3b0) ||
229 1.1 drochner /* ia->ia_iosize != 0 || XXX isa.c */
230 1.1 drochner (ia->ia_maddr != MADDRUNK &&
231 1.1 drochner ia->ia_maddr != 0xb8000 &&
232 1.1 drochner ia->ia_maddr != 0xb0000) ||
233 1.1 drochner (ia->ia_msize != 0 && ia->ia_msize != 0x8000) ||
234 1.1 drochner ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
235 1.1 drochner return (0);
236 1.1 drochner
237 1.1 drochner if (pcdisplay_is_console(ia->ia_iot))
238 1.1 drochner mono = pcdisplay_console_dc.mono;
239 1.1 drochner else if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
240 1.1 drochner pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
241 1.1 drochner mono = 0;
242 1.1 drochner else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
243 1.1 drochner pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
244 1.1 drochner mono = 1;
245 1.1 drochner else
246 1.1 drochner return (0);
247 1.1 drochner
248 1.1 drochner ia->ia_iobase = mono ? 0x3b0 : 0x3d0;
249 1.1 drochner ia->ia_iosize = 0x10;
250 1.1 drochner ia->ia_maddr = mono ? 0xb0000 : 0xb8000;
251 1.1 drochner ia->ia_msize = 0x8000;
252 1.1 drochner return (1);
253 1.1 drochner }
254 1.1 drochner
255 1.1 drochner void
256 1.1 drochner pcdisplay_attach(parent, self, aux)
257 1.1 drochner struct device *parent, *self;
258 1.1 drochner void *aux;
259 1.1 drochner {
260 1.1 drochner struct isa_attach_args *ia = aux;
261 1.1 drochner struct pcdisplay_softc *sc = (struct pcdisplay_softc *)self;
262 1.1 drochner int console;
263 1.1 drochner struct pcdisplay_config *dc;
264 1.1 drochner struct wsemuldisplaydev_attach_args aa;
265 1.1 drochner
266 1.1 drochner printf("\n");
267 1.1 drochner
268 1.1 drochner console = pcdisplay_is_console(ia->ia_iot);
269 1.1 drochner
270 1.1 drochner if (console) {
271 1.1 drochner dc = &pcdisplay_console_dc;
272 1.1 drochner sc->nscreens = 1;
273 1.1 drochner pcdisplay_console_attached = 1;
274 1.1 drochner } else {
275 1.1 drochner dc = malloc(sizeof(struct pcdisplay_config),
276 1.1 drochner M_DEVBUF, M_WAITOK);
277 1.1 drochner if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
278 1.1 drochner pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
279 1.1 drochner pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
280 1.1 drochner else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
281 1.1 drochner pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
282 1.1 drochner pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
283 1.1 drochner else
284 1.1 drochner panic("pcdisplay_attach: display disappeared");
285 1.1 drochner }
286 1.1 drochner sc->sc_dc = dc;
287 1.1 drochner
288 1.1 drochner aa.console = console;
289 1.1 drochner aa.scrdata = &pcdisplay_screenlist;
290 1.1 drochner aa.accessops = &pcdisplay_accessops;
291 1.1 drochner aa.accesscookie = sc;
292 1.1 drochner
293 1.1 drochner config_found(self, &aa, wsemuldisplaydevprint);
294 1.1 drochner }
295 1.1 drochner
296 1.1 drochner
297 1.1 drochner int
298 1.1 drochner pcdisplay_cnattach(iot, memt)
299 1.1 drochner bus_space_tag_t iot, memt;
300 1.1 drochner {
301 1.1 drochner int mono;
302 1.1 drochner
303 1.1 drochner if (pcdisplay_probe_col(iot, memt))
304 1.1 drochner mono = 0;
305 1.1 drochner else if (pcdisplay_probe_mono(iot, memt))
306 1.1 drochner mono = 1;
307 1.1 drochner else
308 1.1 drochner return (ENXIO);
309 1.1 drochner
310 1.1 drochner pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
311 1.1 drochner
312 1.1 drochner wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
313 1.1 drochner pcdisplay_console_dc.pcs.vc_ccol,
314 1.1 drochner pcdisplay_console_dc.pcs.vc_crow,
315 1.1 drochner FG_LIGHTGREY | BG_BLACK);
316 1.1 drochner
317 1.1 drochner pcdisplayconsole = 1;
318 1.1 drochner return (0);
319 1.1 drochner }
320 1.1 drochner
321 1.1 drochner static int
322 1.1 drochner pcdisplay_is_console(iot)
323 1.1 drochner bus_space_tag_t iot;
324 1.1 drochner {
325 1.1 drochner if (pcdisplayconsole &&
326 1.1 drochner !pcdisplay_console_attached &&
327 1.1 drochner iot == pcdisplay_console_dc.dc_ph.ph_iot)
328 1.1 drochner return (1);
329 1.1 drochner return (0);
330 1.1 drochner }
331 1.1 drochner
332 1.1 drochner static int
333 1.1 drochner pcdisplay_ioctl(v, cmd, data, flag, p)
334 1.1 drochner void *v;
335 1.1 drochner u_long cmd;
336 1.1 drochner caddr_t data;
337 1.1 drochner int flag;
338 1.1 drochner struct proc *p;
339 1.1 drochner {
340 1.1 drochner /*
341 1.1 drochner * XXX "do something!"
342 1.1 drochner */
343 1.1 drochner return (-1);
344 1.1 drochner }
345 1.1 drochner
346 1.1 drochner static int
347 1.1 drochner pcdisplay_mmap(v, offset, prot)
348 1.1 drochner void *v;
349 1.1 drochner off_t offset;
350 1.1 drochner int prot;
351 1.1 drochner {
352 1.1 drochner return (-1);
353 1.1 drochner }
354 1.1 drochner
355 1.1 drochner static int
356 1.1 drochner pcdisplay_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
357 1.1 drochner void *v;
358 1.1 drochner const struct wsscreen_descr *type;
359 1.1 drochner void **cookiep;
360 1.1 drochner int *curxp, *curyp;
361 1.1 drochner long *defattrp;
362 1.1 drochner {
363 1.1 drochner struct pcdisplay_softc *sc = v;
364 1.1 drochner
365 1.1 drochner if (sc->nscreens > 0)
366 1.1 drochner return (ENOMEM);
367 1.1 drochner
368 1.1 drochner *cookiep = sc->sc_dc;
369 1.1 drochner *curxp = 0;
370 1.1 drochner *curyp = 0;
371 1.1 drochner *defattrp = FG_LIGHTGREY | BG_BLACK;
372 1.1 drochner sc->nscreens++;
373 1.1 drochner return (0);
374 1.1 drochner }
375 1.1 drochner
376 1.1 drochner static void
377 1.1 drochner pcdisplay_free_screen(v, cookie)
378 1.1 drochner void *v;
379 1.1 drochner void *cookie;
380 1.1 drochner {
381 1.1 drochner struct pcdisplay_softc *sc = v;
382 1.1 drochner
383 1.1 drochner if (sc->sc_dc == &pcdisplay_console_dc)
384 1.1 drochner panic("pcdisplay_free_screen: console");
385 1.1 drochner
386 1.1 drochner sc->nscreens--;
387 1.1 drochner }
388 1.1 drochner
389 1.1 drochner static void
390 1.1 drochner pcdisplay_show_screen(v, cookie)
391 1.1 drochner void *v;
392 1.1 drochner void *cookie;
393 1.1 drochner {
394 1.1 drochner #ifdef DIAGNOSTIC
395 1.1 drochner struct pcdisplay_softc *sc = v;
396 1.1 drochner
397 1.1 drochner if (cookie != sc->sc_dc)
398 1.1 drochner panic("pcdisplay_show_screen: bad screen");
399 1.1 drochner #endif
400 1.1 drochner }
401 1.1 drochner
402 1.1 drochner static int
403 1.1 drochner pcdisplay_alloc_attr(id, fg, bg, flags, attrp)
404 1.1 drochner void *id;
405 1.1 drochner int fg, bg;
406 1.1 drochner int flags;
407 1.1 drochner long *attrp;
408 1.1 drochner {
409 1.1 drochner if (flags & WSATTR_REVERSE)
410 1.1 drochner *attrp = FG_BLACK | BG_LIGHTGREY;
411 1.1 drochner else
412 1.1 drochner *attrp = FG_LIGHTGREY | BG_BLACK;
413 1.1 drochner return (0);
414 1.1 drochner }
415