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