rbox.c revision 1.1 1 /* $NetBSD: rbox.c,v 1.1 2011/02/06 18:26:51 tsutsui Exp $ */
2 /* $OpenBSD: rbox.c,v 1.14 2006/08/11 18:33:13 miod Exp $ */
3
4 /*
5 * Copyright (c) 2005, Miodrag Vallat
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
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /*-
29 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
30 * All rights reserved.
31 *
32 * This code is derived from software contributed to The NetBSD Foundation
33 * by Jason R. Thorpe.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
45 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 * POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 /*
58 * Copyright (c) 1988 University of Utah.
59 * Copyright (c) 1990, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * This code is derived from software contributed to Berkeley by
63 * the Systems Programming Group of the University of Utah Computer
64 * Science Department.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * from: Utah $Hdr: grf_rb.c 1.15 93/08/13$
91 *
92 * @(#)grf_rb.c 8.4 (Berkeley) 1/12/94
93 */
94
95 /*
96 * Graphics routines for the Renaissance, HP98720 Graphics system.
97 */
98
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/conf.h>
102 #include <sys/device.h>
103 #include <sys/proc.h>
104 #include <sys/ioctl.h>
105 #include <sys/bus.h>
106 #include <sys/cpu.h>
107
108 #include <machine/autoconf.h>
109
110 #include <hp300/dev/dioreg.h>
111 #include <hp300/dev/diovar.h>
112 #include <hp300/dev/diodevs.h>
113 #include <hp300/dev/intiovar.h>
114
115 #include <dev/wscons/wsconsio.h>
116 #include <dev/wscons/wsdisplayvar.h>
117 #include <dev/rasops/rasops.h>
118
119 #include <hp300/dev/diofbreg.h>
120 #include <hp300/dev/diofbvar.h>
121 #include <hp300/dev/rboxreg.h>
122
123 struct rbox_softc {
124 device_t sc_dev;
125 struct diofb *sc_fb;
126 struct diofb sc_fb_store;
127 int sc_scode;
128 };
129
130 int rbox_dio_match(device_t, cfdata_t, void *);
131 void rbox_dio_attach(device_t, device_t, void *);
132 int rbox_intio_match(device_t, cfdata_t, void *);
133 void rbox_intio_attach(device_t, device_t, void *);
134
135 CFATTACH_DECL_NEW(rbox_dio, sizeof(struct rbox_softc),
136 rbox_dio_match, rbox_dio_attach, NULL, NULL);
137
138 CFATTACH_DECL_NEW(rbox_intio, sizeof(struct rbox_softc),
139 rbox_intio_match, rbox_intio_attach, NULL, NULL);
140
141 int rbox_reset(struct diofb *, int, struct diofbreg *);
142 void rbox_restore(struct diofb *);
143 void rbox_setcolor(struct diofb *, u_int,
144 u_int8_t, u_int8_t, u_int8_t);
145 int rbox_windowmove(struct diofb *, u_int16_t, u_int16_t, u_int16_t,
146 u_int16_t, u_int16_t, u_int16_t, int16_t, int16_t);
147
148 int rbox_ioctl(void *, void *, u_long, void *, int, struct lwp *);
149
150 struct wsdisplay_accessops rbox_accessops = {
151 rbox_ioctl,
152 diofb_mmap,
153 diofb_alloc_screen,
154 diofb_free_screen,
155 diofb_show_screen,
156 NULL, /* load_font */
157 };
158
159 /*
160 * Attachment glue
161 */
162
163 int
164 rbox_intio_match(device_t parent, cfdata_t cf, void *aux)
165 {
166 struct intio_attach_args *ia = aux;
167 struct diofbreg *fbr;
168
169 if (strcmp("fb", ia->ia_modname) != 0)
170 return 0;
171
172 fbr = (struct diofbreg *)ia->ia_addr;
173
174 if (badaddr((void *)fbr))
175 return (0);
176
177 if (fbr->id == GRFHWID && fbr->fbid == GID_RENAISSANCE) {
178 return (1);
179 }
180
181 return (0);
182 }
183
184 void
185 rbox_intio_attach(device_t parent, device_t self, void *aux)
186 {
187 struct rbox_softc *sc = device_private(self);
188 struct intio_attach_args *ia = aux;
189 struct diofbreg *fbr;
190
191 sc->sc_dev = self;
192 fbr = (struct diofbreg *)ia->ia_addr;
193 sc->sc_scode = CONSCODE_INTERNAL;
194
195 if (sc->sc_scode == conscode) {
196 sc->sc_fb = &diofb_cn;
197 } else {
198 sc->sc_fb = &sc->sc_fb_store;
199 rbox_reset(sc->sc_fb, sc->sc_scode, fbr);
200 }
201
202 diofb_end_attach(self, &rbox_accessops, sc->sc_fb,
203 sc->sc_scode == conscode, NULL);
204 }
205
206 int
207 rbox_dio_match(device_t parent, cfdata_t cf, void *aux)
208 {
209 struct dio_attach_args *da = aux;
210
211 if (da->da_id == DIO_DEVICE_ID_FRAMEBUFFER &&
212 da->da_secid == DIO_DEVICE_SECID_RENAISSANCE)
213 return (1);
214
215 return (0);
216 }
217
218 void
219 rbox_dio_attach(device_t parent, device_t self, void *aux)
220 {
221 struct rbox_softc *sc = device_private(self);
222 struct dio_attach_args *da = aux;
223 bus_space_handle_t bsh;
224 struct diofbreg *fbr;
225
226 sc->sc_dev = self;
227 sc->sc_scode = da->da_scode;
228 if (sc->sc_scode == conscode) {
229 fbr = (struct diofbreg *)conaddr; /* already mapped */
230 sc->sc_fb = &diofb_cn;
231 } else {
232 sc->sc_fb = &sc->sc_fb_store;
233 if (bus_space_map(da->da_bst, da->da_addr, da->da_size,
234 0, &bsh)) {
235 aprint_error(": can't map framebuffer\n");
236 return;
237 }
238 fbr = bus_space_vaddr(da->da_bst, bsh);
239 if (rbox_reset(sc->sc_fb, sc->sc_scode, fbr) != 0) {
240 printf(": can't map framebuffer\n");
241 return;
242 }
243 }
244
245 diofb_end_attach(self, &rbox_accessops, sc->sc_fb,
246 sc->sc_scode == conscode, NULL);
247 }
248
249 /*
250 * Initialize hardware and display routines.
251 */
252 int
253 rbox_reset(struct diofb *fb, int scode, struct diofbreg *fbr)
254 {
255 int rc;
256
257 if ((rc = diofb_fbinquire(fb, scode, fbr)) != 0)
258 return (rc);
259
260 /*
261 * Restrict the framebuffer to a monochrome view for now, until
262 * I know better how to detect and frob overlay planes, and
263 * setup a proper colormap. -- miod
264 */
265 fb->planes = fb->planemask = 1;
266
267 fb->bmv = rbox_windowmove;
268 rbox_restore(fb);
269 diofb_fbsetup(fb);
270
271 return (0);
272 }
273
274 void
275 rbox_restore(struct diofb *fb)
276 {
277 volatile struct rboxfb *rb = (struct rboxfb *)fb->regkva;
278 u_int i;
279
280 rb_waitbusy(rb);
281
282 rb->regs.id = GRFHWID; /* trigger reset */
283 DELAY(1000);
284
285 rb->regs.interrupt = 0x04;
286 rb->video_enable = 0x01;
287 rb->drive = 0x01;
288 rb->vdrive = 0x0;
289
290 rb->opwen = 0xFF;
291
292 /*
293 * Clear color map
294 */
295 rb_waitbusy(fb->regkva);
296 for (i = 0; i < 16; i++) {
297 *(fb->regkva + 0x63c3 + i*4) = 0x0;
298 *(fb->regkva + 0x6403 + i*4) = 0x0;
299 *(fb->regkva + 0x6803 + i*4) = 0x0;
300 *(fb->regkva + 0x6c03 + i*4) = 0x0;
301 *(fb->regkva + 0x73c3 + i*4) = 0x0;
302 *(fb->regkva + 0x7403 + i*4) = 0x0;
303 *(fb->regkva + 0x7803 + i*4) = 0x0;
304 *(fb->regkva + 0x7c03 + i*4) = 0x0;
305 }
306
307 rb->rep_rule = RBOX_DUALROP(RR_COPY);
308
309 /*
310 * I cannot figure out how to make the blink planes stop. So, we
311 * must set both colormaps so that when the planes blink, and
312 * the secondary colormap is active, we still get text.
313 */
314 CM1RED(fb)[0x00].value = 0x00;
315 CM1GRN(fb)[0x00].value = 0x00;
316 CM1BLU(fb)[0x00].value = 0x00;
317 CM1RED(fb)[0x01].value = 0xFF;
318 CM1GRN(fb)[0x01].value = 0xFF;
319 CM1BLU(fb)[0x01].value = 0xFF;
320
321 CM2RED(fb)[0x00].value = 0x00;
322 CM2GRN(fb)[0x00].value = 0x00;
323 CM2BLU(fb)[0x00].value = 0x00;
324 CM2RED(fb)[0x01].value = 0xFF;
325 CM2GRN(fb)[0x01].value = 0xFF;
326 CM2BLU(fb)[0x01].value = 0xFF;
327
328 rb->blink = 0x00;
329 rb->write_enable = 0x01;
330 rb->opwen = 0x00;
331
332 /* enable display */
333 rb->display_enable = 0x01;
334 }
335
336 int
337 rbox_ioctl(void *v, void *vs, u_long cmd, void *data, int flags, struct lwp *l)
338 {
339 struct diofb *fb = v;
340 struct wsdisplay_fbinfo *wdf;
341
342 switch (cmd) {
343 case WSDISPLAYIO_GTYPE:
344 *(u_int *)data = WSDISPLAY_TYPE_RBOX;
345 return 0;
346 case WSDISPLAYIO_SMODE:
347 fb->mapmode = *(u_int *)data;
348 if (fb->mapmode == WSDISPLAYIO_MODE_EMUL)
349 rbox_restore(fb);
350 return 0;
351 case WSDISPLAYIO_GINFO:
352 wdf = (void *)data;
353 wdf->width = fb->ri.ri_width;
354 wdf->height = fb->ri.ri_height;
355 wdf->depth = fb->ri.ri_depth;
356 wdf->cmsize = 0; /* XXX */
357 return 0;
358 case WSDISPLAYIO_LINEBYTES:
359 *(u_int *)data = fb->ri.ri_stride;
360 return 0;
361 case WSDISPLAYIO_GETCMAP:
362 case WSDISPLAYIO_PUTCMAP:
363 /* XXX until color support is implemented */
364 return EPASSTHROUGH;
365 case WSDISPLAYIO_GVIDEO:
366 case WSDISPLAYIO_SVIDEO:
367 return EPASSTHROUGH;
368 }
369
370 return EPASSTHROUGH;
371 }
372
373 int
374 rbox_windowmove(struct diofb *fb, u_int16_t sx, u_int16_t sy,
375 u_int16_t dx, u_int16_t dy, u_int16_t cx, u_int16_t cy, int16_t rop,
376 int16_t planemask)
377 {
378 volatile struct rboxfb *rb = (struct rboxfb *)fb->regkva;
379
380 if (planemask != 0xff)
381 return (EINVAL);
382
383 rb_waitbusy(rb);
384
385 rb->rep_rule = RBOX_DUALROP(rop);
386 rb->source_y = sy;
387 rb->source_x = sx;
388 rb->dest_y = dy;
389 rb->dest_x = dx;
390 rb->wheight = cy;
391 rb->wwidth = cx;
392 rb->wmove = 1;
393
394 rb_waitbusy(rb);
395
396 return (0);
397 }
398
399 /*
400 * Renaissance console support
401 */
402 int
403 rboxcnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
404 {
405 bus_space_handle_t bsh;
406 void *va;
407 struct diofbreg *fbr;
408 struct diofb *fb = &diofb_cn;
409 int size;
410
411 if (bus_space_map(bst, addr, PAGE_SIZE, 0, &bsh))
412 return 1;
413 va = bus_space_vaddr(bst, bsh);
414 fbr = va;
415
416 if (badaddr(va) ||
417 (fbr->id != GRFHWID) || (fbr->fbid != GID_RENAISSANCE)) {
418 bus_space_unmap(bst, bsh, PAGE_SIZE);
419 return 1;
420 }
421
422 size = DIO_SIZE(scode, va);
423
424 bus_space_unmap(bst, bsh, PAGE_SIZE);
425 if (bus_space_map(bst, addr, size, 0, &bsh))
426 return 1;
427 va = bus_space_vaddr(bst, bsh);
428
429 /*
430 * Initialize the framebuffer hardware.
431 */
432 conscode = scode;
433 conaddr = va;
434 rbox_reset(fb, conscode, (struct diofbreg *)conaddr);
435
436 /*
437 * Initialize the terminal emulator.
438 */
439 diofb_cnattach(fb);
440 return 0;
441 }
442