xref: /netbsd/sys/arch/luna68k/dev/lunafb.c (revision c4a72b64)
1 /* $NetBSD: lunafb.c,v 1.10 2002/10/02 05:31:46 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tohru Nishimura.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
40 
41 __KERNEL_RCSID(0, "$NetBSD: lunafb.c,v 1.10 2002/10/02 05:31:46 thorpej Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #include <sys/malloc.h>
49 #include <sys/mman.h>
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/errno.h>
53 #include <sys/buf.h>
54 
55 #include <uvm/uvm_extern.h>
56 
57 #include <dev/rcons/raster.h>
58 #include <dev/wscons/wsconsio.h>
59 #include <dev/wscons/wscons_raster.h>
60 #include <dev/wscons/wsdisplayvar.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/autoconf.h>
64 
65 struct bt454 {
66 	u_int8_t bt_addr;		/* map address register */
67 	u_int8_t bt_cmap;		/* colormap data register */
68 };
69 
70 struct bt458 {
71 	u_int8_t bt_addr;		/* map address register */
72 		unsigned :24;
73 	u_int8_t bt_cmap;		/* colormap data register */
74 		unsigned :24;
75 	u_int8_t bt_ctrl;		/* control register */
76 		unsigned :24;
77 	u_int8_t bt_omap;		/* overlay (cursor) map register */
78 		unsigned :24;
79 };
80 
81 #define	OMFB_RFCNT	0xB1000000	/* video h-origin/v-origin */
82 #define	OMFB_PLANEMASK	0xB1040000	/* planemask register */
83 #define	OMFB_FB_WADDR	0xB1080008	/* common plane */
84 #define	OMFB_FB_RADDR	0xB10C0008	/* plane #0 */
85 #define	OMFB_ROPFUNC	0xB12C0000	/* ROP function code */
86 #define	OMFB_RAMDAC	0xC1100000	/* Bt454/Bt458 RAMDAC */
87 #define	OMFB_SIZE	(0xB1300000 - 0xB1080000 + NBPG)
88 
89 struct om_hwdevconfig {
90 	int	dc_wid;			/* width of frame buffer */
91 	int	dc_ht;			/* height of frame buffer */
92 	int	dc_depth;		/* depth, bits per pixel */
93 	int	dc_rowbytes;		/* bytes in a FB scan line */
94 	int	dc_cmsize;		/* colormap size */
95 	vaddr_t	dc_videobase;		/* base of flat frame buffer */
96 	struct raster	dc_raster;	/* raster description */
97 	struct rcons	dc_rcons;	/* raster blitter control info */
98 };
99 
100 struct hwcmap {
101 #define CMAP_SIZE 256
102 	u_int8_t r[CMAP_SIZE];
103 	u_int8_t g[CMAP_SIZE];
104 	u_int8_t b[CMAP_SIZE];
105 };
106 
107 struct omfb_softc {
108 	struct device sc_dev;		/* base device */
109 	struct om_hwdevconfig *sc_dc;	/* device configuration */
110 	struct hwcmap sc_cmap;		/* software copy of colormap */
111 	int nscreens;
112 };
113 
114 static int  omgetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *));
115 static int  omsetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *));
116 
117 static struct om_hwdevconfig omfb_console_dc;
118 static void omfb_getdevconfig __P((paddr_t, struct om_hwdevconfig *));
119 
120 extern struct wsdisplay_emulops omfb_emulops;
121 
122 static struct wsscreen_descr omfb_stdscreen = {
123 	"std", 0, 0,
124 	&omfb_emulops,
125 	0, 0,
126 	0
127 };
128 
129 static const struct wsscreen_descr *_omfb_scrlist[] = {
130 	&omfb_stdscreen,
131 };
132 
133 static const struct wsscreen_list omfb_screenlist = {
134 	sizeof(_omfb_scrlist) / sizeof(struct wsscreen_descr *), _omfb_scrlist
135 };
136 
137 static int   omfbioctl __P((void *, u_long, caddr_t, int, struct proc *));
138 static paddr_t omfbmmap __P((void *, off_t, int));
139 static int   omfb_alloc_screen __P((void *, const struct wsscreen_descr *,
140 				      void **, int *, int *, long *));
141 static void  omfb_free_screen __P((void *, void *));
142 static int   omfb_show_screen __P((void *, void *, int,
143 				void (*) (void *, int, int), void *));
144 
145 static const struct wsdisplay_accessops omfb_accessops = {
146 	omfbioctl,
147 	omfbmmap,
148 	omfb_alloc_screen,
149 	omfb_free_screen,
150 	omfb_show_screen,
151 	0 /* load_font */
152 };
153 
154 static int  omfbmatch __P((struct device *, struct cfdata *, void *));
155 static void omfbattach __P((struct device *, struct device *, void *));
156 
157 CFATTACH_DECL(fb, sizeof(struct omfb_softc),
158     omfbmatch, omfbattach, NULL, NULL);
159 extern struct cfdriver fb_cd;
160 
161 extern int hwplanemask;	/* hardware planemask; retrieved at boot */
162 
163 static int omfb_console;
164 int  omfb_cnattach __P((void));
165 
166 static int
167 omfbmatch(parent, cf, aux)
168 	struct device *parent;
169 	struct cfdata *cf;
170 	void *aux;
171 {
172 	struct mainbus_attach_args *ma = aux;
173 
174 	if (strcmp(ma->ma_name, fb_cd.cd_name))
175 		return (0);
176 #if 0	/* XXX badaddr() bombs if no framebuffer is installed */
177 	if (badaddr((caddr_t)ma->ma_addr, 4))
178 		return (0);
179 #else
180 	if (hwplanemask == 0)
181 		return (0);
182 #endif
183 	return (1);
184 }
185 
186 static void
187 omfbattach(parent, self, args)
188 	struct device *parent, *self;
189 	void *args;
190 {
191 	struct omfb_softc *sc = (struct omfb_softc *)self;
192 	struct wsemuldisplaydev_attach_args waa;
193 
194 	if (omfb_console) {
195 		sc->sc_dc = &omfb_console_dc;
196 		sc->nscreens = 1;
197 	}
198 	else {
199 		sc->sc_dc = (struct om_hwdevconfig *)
200 		    malloc(sizeof(struct om_hwdevconfig), M_DEVBUF, M_WAITOK);
201 		omfb_getdevconfig(OMFB_FB_WADDR, sc->sc_dc);
202 	}
203 	printf(": %d x %d, %dbpp\n", sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
204 	    sc->sc_dc->dc_depth);
205 
206 #if 0	/* WHITE on BLACK */
207 	cm = &sc->sc_cmap;
208 	memset(cm, 255, sizeof(struct hwcmap));
209 	cm->r[0] = cm->g[0] = cm->b[0] = 0;
210 #endif
211 	waa.console = omfb_console;
212 	waa.scrdata = &omfb_screenlist;
213 	waa.accessops = &omfb_accessops;
214 	waa.accesscookie = sc;
215 
216 	config_found(self, &waa, wsemuldisplaydevprint);
217 }
218 
219 /* EXPORT */ int
220 omfb_cnattach()
221 {
222 	struct om_hwdevconfig *dc = &omfb_console_dc;
223 	long defattr;
224 
225 	omfb_getdevconfig(OMFB_FB_WADDR, dc);
226 	(*omfb_emulops.allocattr)(&dc->dc_rcons, 0, 0, 0, &defattr);
227 	wsdisplay_cnattach(&omfb_stdscreen, &dc->dc_rcons, 0, 0, defattr);
228 	omfb_console = 1;
229 	return (0);
230 }
231 
232 static int
233 omfbioctl(v, cmd, data, flag, p)
234 	void *v;
235 	u_long cmd;
236 	caddr_t data;
237 	int flag;
238 	struct proc *p;
239 {
240 	struct omfb_softc *sc = v;
241 	struct om_hwdevconfig *dc = sc->sc_dc;
242 
243 	switch (cmd) {
244 	case WSDISPLAYIO_GTYPE:
245 		*(u_int *)data = 0x19990927;
246 		return (0);
247 
248 	case WSDISPLAYIO_GINFO:
249 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
250 		wsd_fbip->height = dc->dc_ht;
251 		wsd_fbip->width = dc->dc_wid;
252 		wsd_fbip->depth = dc->dc_depth;
253 		wsd_fbip->cmsize = dc->dc_cmsize;
254 #undef fbt
255 		return (0);
256 
257 	case WSDISPLAYIO_GETCMAP:
258 		return omgetcmap(sc, (struct wsdisplay_cmap *)data);
259 
260 	case WSDISPLAYIO_PUTCMAP:
261 		return omsetcmap(sc, (struct wsdisplay_cmap *)data);
262 
263 	case WSDISPLAYIO_SVIDEO:
264 	case WSDISPLAYIO_GVIDEO:
265 	case WSDISPLAYIO_GCURPOS:
266 	case WSDISPLAYIO_SCURPOS:
267 	case WSDISPLAYIO_GCURMAX:
268 	case WSDISPLAYIO_GCURSOR:
269 	case WSDISPLAYIO_SCURSOR:
270 		break;
271 	}
272 	return (EPASSTHROUGH);
273 }
274 
275 /*
276  * Return the address that would map the given device at the given
277  * offset, allowing for the given protection, or return -1 for error.
278  */
279 static paddr_t
280 omfbmmap(v, offset, prot)
281 	void *v;
282 	off_t offset;
283 	int prot;
284 {
285 	struct omfb_softc *sc = v;
286 
287 	if (offset >= OMFB_SIZE || offset < 0)
288 		return (-1);
289 	return m68k_btop(m68k_trunc_page(sc->sc_dc->dc_videobase) + offset);
290 }
291 
292 static int
293 omgetcmap(sc, p)
294 	struct omfb_softc *sc;
295 	struct wsdisplay_cmap *p;
296 {
297 	u_int index = p->index, count = p->count;
298         int cmsize;
299 
300 	cmsize = sc->sc_dc->dc_cmsize;
301 	if (index >= cmsize || count > cmsize - index)
302 		return (EINVAL);
303 
304 	if (!uvm_useracc(p->red, count, B_WRITE) ||
305 	    !uvm_useracc(p->green, count, B_WRITE) ||
306 	    !uvm_useracc(p->blue, count, B_WRITE))
307 		return (EFAULT);
308 
309 	copyout(&sc->sc_cmap.r[index], p->red, count);
310 	copyout(&sc->sc_cmap.g[index], p->green, count);
311 	copyout(&sc->sc_cmap.b[index], p->blue, count);
312 
313 	return (0);
314 }
315 
316 static int
317 omsetcmap(sc, p)
318 	struct omfb_softc *sc;
319 	struct wsdisplay_cmap *p;
320 {
321 	u_int index = p->index, count = p->count;
322         int cmsize, i;
323 
324 	cmsize = sc->sc_dc->dc_cmsize;
325 	if (index >= cmsize || (index + count) > cmsize)
326 		return (EINVAL);
327 
328 	if (!uvm_useracc(p->red, count, B_READ) ||
329 	    !uvm_useracc(p->green, count, B_READ) ||
330 	    !uvm_useracc(p->blue, count, B_READ))
331 		return (EFAULT);
332 
333 	copyin(p->red, &sc->sc_cmap.r[index], count);
334 	copyin(p->green, &sc->sc_cmap.g[index], count);
335 	copyin(p->blue, &sc->sc_cmap.b[index], count);
336 
337 	if (hwplanemask == 0x0f) {
338 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
339 		odac->bt_addr = index;
340 		for (i = index; i < count; i++) {
341 			odac->bt_cmap = sc->sc_cmap.r[i];
342 			odac->bt_cmap = sc->sc_cmap.g[i];
343 			odac->bt_cmap = sc->sc_cmap.b[i];
344 		}
345 	}
346 	else if (hwplanemask == 0xff) {
347 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
348 		ndac->bt_addr = index;
349 		for (i = index; i < count; i++) {
350 			ndac->bt_cmap = sc->sc_cmap.r[i];
351 			ndac->bt_cmap = sc->sc_cmap.g[i];
352 			ndac->bt_cmap = sc->sc_cmap.b[i];
353 		}
354 	}
355 	return (0);
356 }
357 
358 static void
359 omfb_getdevconfig(paddr, dc)
360 	paddr_t paddr;
361 	struct om_hwdevconfig *dc;
362 {
363 	int bpp, i;
364 	struct raster *rap;
365 	struct rcons *rcp;
366 	union {
367 		struct { short h, v; } p;
368 		u_int32_t u;
369 	} rfcnt;
370 
371 	switch (hwplanemask) {
372 	case 0xff:
373 		bpp = 8;	/* XXX check monochrome bit in DIPSW */
374 		break;
375 	default:
376 	case 0x0f:
377 		bpp = 4;	/* XXX check monochrome bit in DIPSW */
378 		break;
379 	case 1:
380 		bpp = 1;
381 		break;
382 	}
383 	dc->dc_wid = 1280;
384 	dc->dc_ht = 1024;
385 	dc->dc_depth = bpp;
386 	dc->dc_rowbytes = 2048 / 8;
387 	dc->dc_cmsize = (bpp == 1) ? 0 : 1 << bpp;
388 	dc->dc_videobase = paddr;
389 
390 #if 0 /* WHITE on BLACK XXX experiment resulted in WHITE on SKYBLUE... */
391 	if (hwplanemask == 0x0f) {
392 		/* XXX Need Bt454 initialization */
393 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
394 		odac->bt_addr = 0;
395 		odac->bt_cmap = 0;
396 		odac->bt_cmap = 0;
397 		odac->bt_cmap = 0;
398 		for (i = 1; i < 16; i++) {
399 			odac->bt_cmap = 255;
400 			odac->bt_cmap = 255;
401 			odac->bt_cmap = 255;
402 		}
403 	}
404 	else if (hwplanemask == 0xff) {
405 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
406 
407 		ndac->bt_addr = 0x04;
408 		ndac->bt_ctrl = 0xff; /* all planes will be read */
409 		ndac->bt_ctrl = 0x00; /* all planes have non-blink */
410 		ndac->bt_ctrl = 0x43; /* pallete enabled, ovly plane */
411 		ndac->bt_ctrl = 0x00; /* no test mode */
412 		ndac->bt_addr = 0;
413 		ndac->bt_cmap = 0;
414 		ndac->bt_cmap = 0;
415 		ndac->bt_cmap = 0;
416 		for (i = 1; i < 256; i++) {
417 			ndac->bt_cmap = 255;
418 			ndac->bt_cmap = 255;
419 			ndac->bt_cmap = 255;
420 		}
421 	}
422 #endif
423 
424 	/* adjust h/v orgin on screen */
425 	rfcnt.p.h = 7;
426 	rfcnt.p.v = -27;
427 	*(u_int32_t *)OMFB_RFCNT = rfcnt.u; /* single write of 0x007ffe6 */
428 
429 	/* clear the screen */
430 	*(u_int32_t *)OMFB_PLANEMASK = 0xff;
431 	((u_int32_t *)OMFB_ROPFUNC)[5] = ~0;	/* ROP copy */
432 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes/sizeof(u_int32_t); i++)
433 		*((u_int32_t *)dc->dc_videobase + i) = 0;
434 	*(u_int32_t *)OMFB_PLANEMASK = 0x01;
435 
436 	/* initialize the raster */
437 	rap = &dc->dc_raster;
438 	rap->width = dc->dc_wid;
439 	rap->height = dc->dc_ht;
440 	rap->depth = dc->dc_depth;
441 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
442 	rap->pixels = (u_int32_t *)dc->dc_videobase;
443 
444 	/* initialize the raster console blitter */
445 	rcp = &dc->dc_rcons;
446 	rcp->rc_sp = rap;
447 	rcp->rc_crow = rcp->rc_ccol = -1;
448 	rcp->rc_crowp = &rcp->rc_crow;
449 	rcp->rc_ccolp = &rcp->rc_ccol;
450 	rcons_init(rcp, 34, 80);
451 
452 	omfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
453 	omfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
454 }
455 
456 static int
457 omfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
458 	void *v;
459 	const struct wsscreen_descr *type;
460 	void **cookiep;
461 	int *curxp, *curyp;
462 	long *attrp;
463 {
464 	struct omfb_softc *sc = v;
465 	long defattr;
466 
467 	if (sc->nscreens > 0)
468 		return (ENOMEM);
469 
470 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
471 	*curxp = 0;
472 	*curyp = 0;
473 	(*omfb_emulops.allocattr)(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
474 	*attrp = defattr;
475 	sc->nscreens++;
476 	return (0);
477 }
478 
479 static void
480 omfb_free_screen(v, cookie)
481 	void *v;
482 	void *cookie;
483 {
484 	struct omfb_softc *sc = v;
485 
486 	if (sc->sc_dc == &omfb_console_dc)
487 		panic("omfb_free_screen: console");
488 
489 	sc->nscreens--;
490 }
491 
492 static int
493 omfb_show_screen(v, cookie, waitok, cb, cbarg)
494 	void *v;
495 	void *cookie;
496 	int waitok;
497 	void (*cb) __P((void *, int, int));
498 	void *cbarg;
499 {
500 	return 0;
501 }
502