xref: /netbsd-src/sys/arch/luna68k/dev/lunafb.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /* $NetBSD: lunafb.c,v 1.25 2011/07/27 14:17:54 tsutsui 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.25 2011/07/27 14:17:54 tsutsui 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 #include "ioconf.h"
59 
60 struct bt454 {
61 	volatile uint8_t bt_addr;	/* map address register */
62 	volatile uint8_t bt_cmap;	/* colormap data register */
63 };
64 
65 struct bt458 {
66 	volatile uint8_t bt_addr;	/* map address register */
67 	uint8_t          pad0[3];
68 	volatile uint8_t bt_cmap;	/* colormap data register */
69 	uint8_t          pad1[3];
70 	volatile uint8_t bt_ctrl;	/* control register */
71 	uint8_t          pad2[3];
72 	volatile uint8_t bt_omap;	/* overlay (cursor) map register */
73 	uint8_t          pad3[3];
74 };
75 
76 #define	OMFB_RFCNT	0xB1000000	/* video h-origin/v-origin */
77 #define	OMFB_PLANEMASK	0xB1040000	/* planemask register */
78 #define	OMFB_FB_WADDR	0xB1080008	/* common plane */
79 #define	OMFB_FB_RADDR	0xB10C0008	/* plane #0 */
80 #define	OMFB_ROPFUNC	0xB12C0000	/* ROP function code */
81 #define	OMFB_RAMDAC	0xC1100000	/* Bt454/Bt458 RAMDAC */
82 #define	OMFB_SIZE	(0xB1300000 - 0xB1080000 + PAGE_SIZE)
83 
84 struct om_hwdevconfig {
85 	int	dc_wid;			/* width of frame buffer */
86 	int	dc_ht;			/* height of frame buffer */
87 	int	dc_depth;		/* depth, bits per pixel */
88 	int	dc_rowbytes;		/* bytes in a FB scan line */
89 	int	dc_cmsize;		/* colormap size */
90 	vaddr_t	dc_videobase;		/* base of flat frame buffer */
91 	struct raster	dc_raster;	/* raster description */
92 	struct rcons	dc_rcons;	/* raster blitter control info */
93 };
94 
95 struct hwcmap {
96 #define CMAP_SIZE 256
97 	uint8_t r[CMAP_SIZE];
98 	uint8_t g[CMAP_SIZE];
99 	uint8_t b[CMAP_SIZE];
100 };
101 
102 struct omfb_softc {
103 	device_t sc_dev;		/* base device */
104 	struct om_hwdevconfig *sc_dc;	/* device configuration */
105 	struct hwcmap sc_cmap;		/* software copy of colormap */
106 	int nscreens;
107 };
108 
109 static int  omgetcmap(struct omfb_softc *, struct wsdisplay_cmap *);
110 static int  omsetcmap(struct omfb_softc *, struct wsdisplay_cmap *);
111 
112 static struct om_hwdevconfig omfb_console_dc;
113 static void omfb_getdevconfig(paddr_t, struct om_hwdevconfig *);
114 
115 extern struct wsdisplay_emulops omfb_emulops;
116 
117 static struct wsscreen_descr omfb_stdscreen = {
118 	"std", 0, 0,
119 	&omfb_emulops,
120 	0, 0,
121 	0
122 };
123 
124 static const struct wsscreen_descr *_omfb_scrlist[] = {
125 	&omfb_stdscreen,
126 };
127 
128 static const struct wsscreen_list omfb_screenlist = {
129 	sizeof(_omfb_scrlist) / sizeof(struct wsscreen_descr *), _omfb_scrlist
130 };
131 
132 static int   omfbioctl(void *, void *, u_long, void *, int, struct lwp *);
133 static paddr_t omfbmmap(void *, void *, off_t, int);
134 static int   omfb_alloc_screen(void *, const struct wsscreen_descr *,
135 			       void **, int *, int *, long *);
136 static void  omfb_free_screen(void *, void *);
137 static int   omfb_show_screen(void *, void *, int,
138 			      void (*) (void *, int, int), void *);
139 
140 static const struct wsdisplay_accessops omfb_accessops = {
141 	omfbioctl,
142 	omfbmmap,
143 	omfb_alloc_screen,
144 	omfb_free_screen,
145 	omfb_show_screen,
146 	0 /* load_font */
147 };
148 
149 static int  omfbmatch(device_t, cfdata_t, void *);
150 static void omfbattach(device_t, device_t, void *);
151 
152 CFATTACH_DECL_NEW(fb, sizeof(struct omfb_softc),
153     omfbmatch, omfbattach, NULL, NULL);
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(device_t parent, cfdata_t 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(device_t parent, device_t self, void *args)
179 {
180 	struct omfb_softc *sc = device_private(self);
181 	struct wsemuldisplaydev_attach_args waa;
182 
183 	sc->sc_dev = self;
184 
185 	if (omfb_console) {
186 		sc->sc_dc = &omfb_console_dc;
187 		sc->nscreens = 1;
188 	} else {
189 		sc->sc_dc = malloc(sizeof(struct om_hwdevconfig),
190 		    M_DEVBUF, M_WAITOK);
191 		omfb_getdevconfig(OMFB_FB_WADDR, sc->sc_dc);
192 	}
193 	aprint_normal(": %d x %d, %dbpp\n", sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
194 	    sc->sc_dc->dc_depth);
195 
196 	/* WHITE on BLACK */
197 	memset(&sc->sc_cmap, 255, sizeof(struct hwcmap));
198 	sc->sc_cmap.r[0] = sc->sc_cmap.g[0] = sc->sc_cmap.b[0] = 0;
199 
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 = WSDISPLAY_TYPE_LUNA;
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_LINEBYTES:
242 		*(u_int *)data = dc->dc_rowbytes;
243 		return 0;
244 
245 	case WSDISPLAYIO_GETCMAP:
246 		return omgetcmap(sc, (struct wsdisplay_cmap *)data);
247 
248 	case WSDISPLAYIO_PUTCMAP:
249 		return omsetcmap(sc, (struct wsdisplay_cmap *)data);
250 
251 	case WSDISPLAYIO_SVIDEO:
252 	case WSDISPLAYIO_GVIDEO:
253 	case WSDISPLAYIO_GCURPOS:
254 	case WSDISPLAYIO_SCURPOS:
255 	case WSDISPLAYIO_GCURMAX:
256 	case WSDISPLAYIO_GCURSOR:
257 	case WSDISPLAYIO_SCURSOR:
258 		break;
259 	}
260 	return EPASSTHROUGH;
261 }
262 
263 /*
264  * Return the address that would map the given device at the given
265  * offset, allowing for the given protection, or return -1 for error.
266  */
267 static paddr_t
268 omfbmmap(void *v, void *vs, off_t offset, int prot)
269 {
270 	struct omfb_softc *sc = v;
271 	struct om_hwdevconfig *dc = sc->sc_dc;
272 	paddr_t cookie = -1;
273 
274 #if 0	/* XXX: quick workaround to make X.Org mono server work */
275 	if (offset >= 0 && offset < OMFB_SIZE)
276 		cookie = m68k_btop(m68k_trunc_page(dc->dc_videobase) + offset);
277 #else
278 	if (offset >= 0 && offset < dc->dc_rowbytes * dc->dc_ht)
279 		cookie = m68k_btop(m68k_trunc_page(OMFB_FB_RADDR) + offset);
280 #endif
281 
282 	return cookie;
283 }
284 
285 static int
286 omgetcmap(struct omfb_softc *sc, struct wsdisplay_cmap *p)
287 {
288 	u_int index = p->index, count = p->count;
289 	int cmsize, error;
290 
291 	cmsize = sc->sc_dc->dc_cmsize;
292 	if (index >= cmsize || count > cmsize - index)
293 		return EINVAL;
294 
295 	error = copyout(&sc->sc_cmap.r[index], p->red, count);
296 	if (error)
297 		return error;
298 	error = copyout(&sc->sc_cmap.g[index], p->green, count);
299 	if (error)
300 		return error;
301 	error = copyout(&sc->sc_cmap.b[index], p->blue, count);
302 	return error;
303 }
304 
305 static int
306 omsetcmap(struct omfb_softc *sc, struct wsdisplay_cmap *p)
307 {
308 	struct hwcmap cmap;
309 	u_int index = p->index, count = p->count;
310 	int cmsize, i, error;
311 
312 	cmsize = sc->sc_dc->dc_cmsize;
313 	if (index >= cmsize || (index + count) > cmsize)
314 		return (EINVAL);
315 
316 	error = copyin(p->red, &cmap.r[index], count);
317 	if (error)
318 		return error;
319 	error = copyin(p->green, &cmap.g[index], count);
320 	if (error)
321 		return error;
322 	error = copyin(p->blue, &cmap.b[index], count);
323 	if (error)
324 		return error;
325 
326 	memcpy(&sc->sc_cmap.r[index], &cmap.r[index], count);
327 	memcpy(&sc->sc_cmap.g[index], &cmap.g[index], count);
328 	memcpy(&sc->sc_cmap.b[index], &cmap.b[index], count);
329 	if (hwplanemask == 0x0f) {
330 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
331 		odac->bt_addr = index;
332 		for (i = index; i < index + count; i++) {
333 			odac->bt_cmap = sc->sc_cmap.r[i];
334 			odac->bt_cmap = sc->sc_cmap.g[i];
335 			odac->bt_cmap = sc->sc_cmap.b[i];
336 		}
337 	} else if (hwplanemask == 0xff) {
338 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
339 		ndac->bt_addr = index;
340 		for (i = index; i < index + count; i++) {
341 			ndac->bt_cmap = sc->sc_cmap.r[i];
342 			ndac->bt_cmap = sc->sc_cmap.g[i];
343 			ndac->bt_cmap = sc->sc_cmap.b[i];
344 		}
345 	}
346 	return 0;
347 }
348 
349 static void
350 omfb_getdevconfig(paddr_t paddr, struct om_hwdevconfig *dc)
351 {
352 	int bpp, i;
353 	struct raster *rap;
354 	struct rcons *rcp;
355 	union {
356 		struct { short h, v; } p;
357 		uint32_t u;
358 	} rfcnt;
359 
360 	switch (hwplanemask) {
361 	case 0xff:
362 		bpp = 8;	/* XXX check monochrome bit in DIPSW */
363 		break;
364 	default:
365 	case 0x0f:
366 #if 0
367 		/*
368 		 * XXX
369 		 * experiment resulted in WHITE on SKYBLUE after Xorg server
370 		 * touches pallete. Disable 4bpp for now.
371 		 */
372 		bpp = 4;	/* XXX check monochrome bit in DIPSW */
373 		break;
374 #endif
375 	case 1:
376 		bpp = 1;
377 		break;
378 	}
379 	dc->dc_wid = 1280;
380 	dc->dc_ht = 1024;
381 	dc->dc_depth = bpp;
382 	dc->dc_rowbytes = 2048 / 8;
383 	dc->dc_cmsize = (bpp == 1) ? 0 : 1 << bpp;
384 	dc->dc_videobase = paddr;
385 
386 	/* WHITE on BLACK */
387 	if (hwplanemask == 0x0f) {
388 		/* XXX Need Bt454 more initialization */
389 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
390 		odac->bt_addr = 0;
391 		odac->bt_cmap = 0;
392 		odac->bt_cmap = 0;
393 		odac->bt_cmap = 0;
394 		for (i = 1; i < 16; i++) {
395 			odac->bt_cmap = 255;
396 			odac->bt_cmap = 255;
397 			odac->bt_cmap = 255;
398 		}
399 	} else if (hwplanemask == 0xff) {
400 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
401 
402 		ndac->bt_addr = 0x04;
403 		ndac->bt_ctrl = 0xff; /* all planes will be read */
404 		ndac->bt_ctrl = 0x00; /* all planes have non-blink */
405 		ndac->bt_ctrl = 0x43; /* pallete enabled, ovly plane */
406 		ndac->bt_ctrl = 0x00; /* no test mode */
407 		ndac->bt_addr = 0;
408 		ndac->bt_cmap = 0;
409 		ndac->bt_cmap = 0;
410 		ndac->bt_cmap = 0;
411 		for (i = 1; i < 256; i++) {
412 			ndac->bt_cmap = 255;
413 			ndac->bt_cmap = 255;
414 			ndac->bt_cmap = 255;
415 		}
416 	}
417 
418 	/* adjust h/v orgin on screen */
419 	rfcnt.p.h = 7;
420 	rfcnt.p.v = -27;
421 	/* single write of 0x007ffe6 */
422 	*(volatile uint32_t *)OMFB_RFCNT = rfcnt.u;
423 
424 	/* clear the screen */
425 	*(volatile uint32_t *)OMFB_PLANEMASK = 0xff;
426 	((volatile uint32_t *)OMFB_ROPFUNC)[5] = ~0;	/* ROP copy */
427 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes/sizeof(uint32_t); i++)
428 		*((volatile uint32_t *)dc->dc_videobase + i) = 0;
429 	*(volatile uint32_t *)OMFB_PLANEMASK = 0x01;
430 
431 	/* initialize the raster */
432 	rap = &dc->dc_raster;
433 	rap->width = dc->dc_wid;
434 	rap->height = dc->dc_ht;
435 	rap->depth = dc->dc_depth;
436 	rap->linelongs = dc->dc_rowbytes / sizeof(uint32_t);
437 	rap->pixels = (uint32_t *)dc->dc_videobase;
438 
439 	/* initialize the raster console blitter */
440 	rcp = &dc->dc_rcons;
441 	rcp->rc_sp = rap;
442 	rcp->rc_crow = rcp->rc_ccol = -1;
443 	rcp->rc_crowp = &rcp->rc_crow;
444 	rcp->rc_ccolp = &rcp->rc_ccol;
445 	rcons_init(rcp, 34, 80);
446 
447 	omfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
448 	omfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
449 }
450 
451 static int
452 omfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
453     int *curxp, int *curyp, long *attrp)
454 {
455 	struct omfb_softc *sc = v;
456 	long defattr;
457 
458 	if (sc->nscreens > 0)
459 		return ENOMEM;
460 
461 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
462 	*curxp = 0;
463 	*curyp = 0;
464 	(*omfb_emulops.allocattr)(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
465 	*attrp = defattr;
466 	sc->nscreens++;
467 	return 0;
468 }
469 
470 static void
471 omfb_free_screen(void *v, void *cookie)
472 {
473 	struct omfb_softc *sc = v;
474 
475 	if (sc->sc_dc == &omfb_console_dc)
476 		panic("omfb_free_screen: console");
477 
478 	sc->nscreens--;
479 }
480 
481 static int
482 omfb_show_screen(void *v, void *cookie, int waitok,
483     void (*cb)(void *, int, int), void *cbarg)
484 {
485 
486 	return 0;
487 }
488