dvbox.c revision 1.1 1 /* $NetBSD: dvbox.c,v 1.1 2011/02/06 18:26:51 tsutsui Exp $ */
2 /* $OpenBSD: dvbox.c,v 1.13 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_dv.c 1.12 93/08/13$
91 *
92 * @(#)grf_dv.c 8.4 (Berkeley) 1/12/94
93 */
94
95 /*
96 * Graphics routines for the DaVinci, HP98730/98731 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/dvboxreg.h>
122
123 struct dvbox_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 dvbox_dio_match(device_t, cfdata_t, void *);
131 void dvbox_dio_attach(device_t, device_t, void *);
132 int dvbox_intio_match(device_t, cfdata_t, void *);
133 void dvbox_intio_attach(device_t, device_t, void *);
134
135 CFATTACH_DECL_NEW(dvbox_dio, sizeof(struct dvbox_softc),
136 dvbox_dio_match, dvbox_dio_attach, NULL, NULL);
137
138 CFATTACH_DECL_NEW(dvbox_intio, sizeof(struct dvbox_softc),
139 dvbox_intio_match, dvbox_intio_attach, NULL, NULL);
140
141 int dvbox_reset(struct diofb *, int, struct diofbreg *);
142 void dvbox_restore(struct diofb *);
143 void dvbox_setcolor(struct diofb *, u_int,
144 u_int8_t, u_int8_t, u_int8_t);
145 int dvbox_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 dvbox_ioctl(void *, void *, u_long, void *, int, struct lwp *);
149
150 struct wsdisplay_accessops dvbox_accessops = {
151 dvbox_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 dvbox_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_DAVINCI) {
178 return (1);
179 }
180
181 return (0);
182 }
183
184 void
185 dvbox_intio_attach(device_t parent, device_t self, void *aux)
186 {
187 struct dvbox_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 dvbox_reset(sc->sc_fb, sc->sc_scode, fbr);
200 }
201
202 diofb_end_attach(self, &dvbox_accessops, sc->sc_fb,
203 sc->sc_scode == conscode, NULL);
204 }
205
206 int
207 dvbox_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_DAVINCI)
213 return (1);
214
215 return (0);
216 }
217
218 void
219 dvbox_dio_attach(device_t parent, device_t self, void *aux)
220 {
221 struct dvbox_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, 0,
234 &bsh)) {
235 aprint_error(": can't map framebuffer\n");
236 return;
237 }
238 fbr = bus_space_vaddr(da->da_bst, bsh);
239 if (dvbox_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, &dvbox_accessops, sc->sc_fb,
246 sc->sc_scode == conscode, NULL);
247 }
248
249 /*
250 * Initialize hardware and display routines.
251 */
252 int
253 dvbox_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 = dvbox_windowmove;
268 dvbox_restore(fb);
269 diofb_fbsetup(fb);
270
271 return (0);
272 }
273
274 /*
275 * Magic initialization code.
276 */
277 void
278 dvbox_restore(struct diofb *fb)
279 {
280 volatile struct dvboxfb *db = (struct dvboxfb *)fb->regkva;
281 u_int i;
282
283 db->regs.id = 0x80;
284 DELAY(100);
285
286 db->regs.interrupt = 0x04;
287 db->en_scan = 0x01;
288 db->fbwen = ~0;
289 db->opwen = ~0;
290 db->fold = 0x01; /* 8bpp */
291 db->drive = 0x01; /* use FB plane */
292 db->rep_rule = DVBOX_DUALROP(RR_COPY);
293 db->alt_rr = DVBOX_DUALROP(RR_COPY);
294 db->zrr = DVBOX_DUALROP(RR_COPY);
295
296 db->fbvenp = 0xFF; /* enable video */
297 db->dispen = 0x01; /* enable display */
298 db->fbvens = 0x0;
299 db->fv_trig = 0x01;
300 DELAY(100);
301 db->vdrive = 0x0;
302 db->zconfig = 0x0;
303
304 while (db->wbusy & 0x01)
305 DELAY(10);
306
307 db->cmapbank = 0;
308
309 db->red0 = 0;
310 db->red1 = 0;
311 db->green0 = 0;
312 db->green1 = 0;
313 db->blue0 = 0;
314 db->blue1 = 0;
315
316 db->panxh = 0;
317 db->panxl = 0;
318 db->panyh = 0;
319 db->panyl = 0;
320 db->zoom = 0;
321 db->cdwidth = 0x50;
322 db->chstart = 0x52;
323 db->cvwidth = 0x22;
324 db->pz_trig = 1;
325
326 /*
327 * Turn on frame buffer, turn on overlay planes, set replacement
328 * rule, enable top overlay plane writes for ite, disable all frame
329 * buffer planes, set byte per pixel, and display frame buffer 0.
330 * Lastly, turn on the box.
331 */
332 db->regs.interrupt = 0x04;
333 db->drive = 0x10;
334 db->rep_rule = DVBOX_DUALROP(RR_COPY);
335 db->opwen = 0x01;
336 db->fbwen = 0x0;
337 db->fold = 0x01;
338 db->vdrive = 0x0;
339 db->dispen = 0x01;
340
341 /*
342 * Video enable top overlay plane.
343 */
344 db->opvenp = 0x01;
345 db->opvens = 0x01;
346
347 /*
348 * Make sure that overlay planes override frame buffer planes.
349 */
350 db->ovly0p = 0x0;
351 db->ovly0s = 0x0;
352 db->ovly1p = 0x0;
353 db->ovly1s = 0x0;
354 db->fv_trig = 0x1;
355 DELAY(100);
356
357 /*
358 * Setup the overlay colormaps. Need to set the 0,1 (black/white)
359 * color for both banks.
360 */
361 db_waitbusy(db);
362 for (i = 0; i <= 1; i++) {
363 db->cmapbank = i;
364 db->rgb[0].red = 0x00;
365 db->rgb[0].green = 0x00;
366 db->rgb[0].blue = 0x00;
367 db->rgb[1].red = 0xff;
368 db->rgb[1].green = 0xff;
369 db->rgb[1].blue = 0xff;
370 }
371 db->cmapbank = 0;
372 db_waitbusy(db);
373 }
374
375 int
376 dvbox_ioctl(void *v, void *vs, u_long cmd, void *data, int flags, struct lwp *l)
377 {
378 struct diofb *fb = v;
379 struct wsdisplay_fbinfo *wdf;
380
381 switch (cmd) {
382 case WSDISPLAYIO_GTYPE:
383 *(u_int *)data = WSDISPLAY_TYPE_DVBOX;
384 return 0;
385 case WSDISPLAYIO_SMODE:
386 fb->mapmode = *(u_int *)data;
387 if (fb->mapmode == WSDISPLAYIO_MODE_EMUL)
388 dvbox_restore(fb);
389 return 0;
390 case WSDISPLAYIO_GINFO:
391 wdf = (void *)data;
392 wdf->width = fb->ri.ri_width;
393 wdf->height = fb->ri.ri_height;
394 wdf->depth = fb->ri.ri_depth;
395 wdf->cmsize = 0; /* XXX */
396 return 0;
397 case WSDISPLAYIO_LINEBYTES:
398 *(u_int *)data = fb->ri.ri_stride;
399 return 0;
400 case WSDISPLAYIO_GETCMAP:
401 case WSDISPLAYIO_PUTCMAP:
402 /* XXX until color support is implemented */
403 return EPASSTHROUGH;
404 case WSDISPLAYIO_GVIDEO:
405 case WSDISPLAYIO_SVIDEO:
406 return EPASSTHROUGH;
407 }
408
409 return EPASSTHROUGH;
410 }
411
412 int
413 dvbox_windowmove(struct diofb *fb, u_int16_t sx, u_int16_t sy,
414 u_int16_t dx, u_int16_t dy, u_int16_t cx, u_int16_t cy, int16_t rop,
415 int16_t planemask)
416 {
417 volatile struct dvboxfb *db = (struct dvboxfb *)fb->regkva;
418
419 if (planemask != 0xff)
420 return (EINVAL);
421
422 db_waitbusy(db);
423
424 db->rep_rule = DVBOX_DUALROP(rop);
425 db->source_y = sy;
426 db->source_x = sx;
427 db->dest_y = dy;
428 db->dest_x = dx;
429 db->wheight = cy;
430 db->wwidth = cx;
431 db->wmove = 1;
432
433 db_waitbusy(db);
434
435 return (0);
436 }
437
438 /*
439 * DaVinci console support
440 */
441
442 int
443 dvboxcnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
444 {
445 bus_space_handle_t bsh;
446 void *va;
447 struct diofbreg *fbr;
448 struct diofb *fb = &diofb_cn;
449 int size;
450
451 if (bus_space_map(bst, addr, PAGE_SIZE, 0, &bsh))
452 return 1;
453 va = bus_space_vaddr(bst, bsh);
454 fbr = va;
455
456 if (badaddr(va) ||
457 (fbr->id != GRFHWID) || (fbr->fbid != GID_DAVINCI)) {
458 bus_space_unmap(bst, bsh, PAGE_SIZE);
459 return 1;
460 }
461
462 size = DIO_SIZE(scode, va);
463
464 bus_space_unmap(bst, bsh, PAGE_SIZE);
465 if (bus_space_map(bst, addr, size, 0, &bsh))
466 return 1;
467 va = bus_space_vaddr(bst, bsh);
468
469 /*
470 * Initialize the framebuffer hardware.
471 */
472 conscode = scode;
473 conaddr = va;
474 dvbox_reset(fb, conscode, (struct diofbreg *)conaddr);
475
476 /*
477 * Initialize the terminal emulator.
478 */
479 diofb_cnattach(fb);
480 return 0;
481 }
482