xref: /netbsd/sys/arch/evbppc/explora/dev/fb_elb.c (revision 6550d01e)
1 /*	$NetBSD: fb_elb.c,v 1.11 2010/05/15 08:53:26 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
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>
33 __KERNEL_RCSID(0, "$NetBSD: fb_elb.c,v 1.11 2010/05/15 08:53:26 tsutsui Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/rasops/rasops.h>
45 
46 #include <machine/explora.h>
47 #include <machine/bus.h>
48 
49 #include <evbppc/explora/dev/elbvar.h>
50 
51 #define FB_NPORTS		65536
52 
53 struct fb_dev {
54 	void *fb_vram;
55 	bus_space_tag_t fb_iot;
56 	bus_space_handle_t fb_ioh;
57 	struct rasops_info fb_ri;
58 };
59 
60 struct fb_elb_softc {
61 	struct device sc_dev;
62 	struct fb_dev *sc_fb;
63 	int sc_nscreens;
64 };
65 
66 static int	fb_elb_probe(struct device *, struct cfdata *, void *);
67 static void	fb_elb_attach(struct device *, struct device *, void *);
68 void		fb_cnattach(bus_space_tag_t, bus_addr_t, void *);
69 static void	fb_init(struct fb_dev *, int);
70 static int	fb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
71 static paddr_t	fb_mmap(void *, void *, off_t, int);
72 static int	fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
73                     int *, int *, long *);
74 static void	fb_free_screen(void *, void *);
75 static int	fb_show_screen(void *, void *, int, void (*)(void *, int, int),
76                     void *);
77 
78 static void	fb_eraserows(void *, int, int, long);
79 static void	fb_erasecols(void *, int, int, int, long);
80 static void	fb_copyrows(void *, int, int, int);
81 static void	fb_copycols(void *, int, int, int, int);
82 
83 static void	s3_init(struct fb_dev *, int *, int *);
84 static void	s3_copy(struct fb_dev *, int, int, int, int, int, int, int);
85 static void	s3_fill(struct fb_dev *, int, int, int, int, int, int);
86 
87 static struct fb_dev console_dev;
88 
89 static struct wsdisplay_accessops accessops = {
90 	fb_ioctl,
91 	fb_mmap,
92 	fb_alloc_screen,
93 	fb_free_screen,
94 	fb_show_screen,
95 	NULL
96 };
97 
98 static struct wsscreen_descr stdscreen = {
99 	"std",
100 	0, 0,
101 	0,
102 	0, 0,
103 	0
104 };
105 
106 static const struct wsscreen_descr *scrlist[] = {
107 	&stdscreen
108 };
109 
110 static struct wsscreen_list screenlist = {
111 	sizeof(scrlist)/sizeof(scrlist[0]), scrlist
112 };
113 
114 CFATTACH_DECL(fb_elb, sizeof(struct fb_elb_softc),
115     fb_elb_probe, fb_elb_attach, NULL, NULL);
116 
117 static int
118 fb_elb_probe(struct device *parent, struct cfdata *cf, void *aux)
119 {
120 	struct elb_attach_args *oaa = aux;
121 
122 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
123 		return 0;
124 
125 	return (1);
126 }
127 
128 static void
129 fb_elb_attach(struct device *parent, struct device *self, void *aux)
130 {
131 	struct fb_elb_softc *sc = (void *)self;
132 	struct elb_attach_args *eaa = aux;
133 	struct wsemuldisplaydev_attach_args waa;
134 	struct rasops_info *ri;
135 	bus_space_handle_t ioh;
136 	int is_console;
137 
138 	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);
139 
140 	if (is_console) {
141 		sc->sc_fb = &console_dev;
142 		sc->sc_fb->fb_ri.ri_flg &= ~RI_NO_AUTO;
143 	} else {
144 		sc->sc_fb = malloc(sizeof(struct fb_dev), M_DEVBUF, M_WAITOK);
145 		memset(sc->sc_fb, 0, sizeof(struct fb_dev));
146 	}
147 
148 	sc->sc_fb->fb_iot = eaa->elb_bt;
149 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB,
150 	    BUS_SPACE_MAP_LINEAR, &ioh);
151 	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
152 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
153 	    0, &sc->sc_fb->fb_ioh);
154 
155 	fb_init(sc->sc_fb, !is_console);
156 
157 	ri = &sc->sc_fb->fb_ri;
158 
159 	printf(": %d x %d\n", ri->ri_rows, ri->ri_cols);
160 
161 	waa.console = is_console;
162 	waa.scrdata = &screenlist;
163 	waa.accessops = &accessops;
164 	waa.accesscookie = sc;
165 
166 	config_found(self, &waa, wsemuldisplaydevprint);
167 }
168 
169 static void
170 fb_init(struct fb_dev *fb, int full)
171 {
172 	struct rasops_info *ri = &fb->fb_ri;
173 
174 	if (full) {
175 		s3_init(fb, &ri->ri_width, &ri->ri_height);
176 		ri->ri_depth = 8;
177 		ri->ri_stride = ri->ri_width;
178 		ri->ri_bits = fb->fb_vram;
179 		ri->ri_flg = RI_CENTER;
180 		if (ri == &console_dev.fb_ri)
181 			ri->ri_flg |= RI_NO_AUTO;
182 
183 		rasops_init(ri, 500, 500);
184 	} else {
185 		ri->ri_origbits = fb->fb_vram;	/*XXX*/
186 		rasops_reconfig(ri, 500, 500);
187 	}
188 
189 	/* Replace the copy/erase ops. */
190 	ri->ri_hw = fb;
191 	ri->ri_ops.eraserows = fb_eraserows;
192 	ri->ri_ops.erasecols = fb_erasecols;
193 	ri->ri_ops.copyrows = fb_copyrows;
194 	ri->ri_ops.copycols = fb_copycols;
195 
196 	stdscreen.nrows = ri->ri_rows;
197 	stdscreen.ncols = ri->ri_cols;
198 	stdscreen.textops = &ri->ri_ops;
199 	stdscreen.capabilities = ri->ri_caps;
200 }
201 
202 static int
203 fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
204 {
205 	struct fb_elb_softc *sc = v;
206 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
207 	struct wsdisplay_fbinfo *wdf;
208 
209 	switch (cmd) {
210 	case WSDISPLAYIO_GTYPE:
211 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
212 		return(0);
213 
214 	case WSDISPLAYIO_GINFO:
215 		wdf = (void *)data;
216 		wdf->height = ri->ri_height;
217 		wdf->width = ri->ri_width;
218 		wdf->depth = ri->ri_depth;
219 		wdf->cmsize = 16; /*XXX*/
220 		return(0);
221 
222 	case WSDISPLAYIO_SVIDEO:
223 	case WSDISPLAYIO_GETCMAP:
224 	case WSDISPLAYIO_PUTCMAP:
225 		break;
226 	}
227 
228 	return(EPASSTHROUGH);
229 }
230 
231 static paddr_t
232 fb_mmap(void *v, void *vs, off_t offset, int prot)
233 {
234 	struct fb_elb_softc *sc = v;
235 
236 	if (offset < 0 || offset >= SIZE_FB)
237 		return -1;
238 
239 	return bus_space_mmap(sc->sc_fb->fb_iot, BASE_FB, offset, prot,
240 	    BUS_SPACE_MAP_LINEAR);
241 }
242 
243 static int
244 fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
245     int *ccolp, int *crowp, long *attrp)
246 {
247 	struct fb_elb_softc *sc = v;
248 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
249 
250 	if (sc->sc_nscreens > 0)
251 		return ENOMEM;
252 
253 	*cookiep = ri;
254 	*ccolp = *crowp = 0;
255 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
256 	sc->sc_nscreens++;
257 
258 	return(0);
259 }
260 
261 static void
262 fb_free_screen(void *v, void *cookie)
263 {
264 	struct fb_elb_softc *sc = v;
265 
266 	if (sc->sc_fb == &console_dev)
267 		panic("fb_free_screen: freeing console");
268 
269 	sc->sc_nscreens--;
270 }
271 
272 static int
273 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
274     void *cbarg)
275 {
276 	return(0);
277 }
278 
279 void
280 fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
281 {
282 	struct rasops_info *ri = &console_dev.fb_ri;
283 	long defattr;
284 
285 	console_dev.fb_iot = iot;
286 	console_dev.fb_ioh = iobase;
287 	console_dev.fb_vram = vram;
288 
289 	fb_init(&console_dev, 1);
290 
291 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
292 
293 	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
294 }
295 
296 static void
297 fb_eraserows(void *v, int row, int nrows, long attr)
298 {
299 	struct rasops_info *ri = v;
300 	struct fb_dev *fb = ri->ri_hw;
301 
302 	row *= ri->ri_font->fontheight;
303 	nrows *= ri->ri_font->fontheight;
304 
305 	s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f);
306 }
307 
308 static void
309 fb_erasecols(void *v, int row, int startcol, int ncols, long attr)
310 {
311 	struct rasops_info *ri = v;
312 	struct fb_dev *fb = ri->ri_hw;
313 
314 	row *= ri->ri_font->fontheight;
315 	startcol *= ri->ri_font->fontwidth;
316 	ncols *= ri->ri_font->fontwidth;
317 
318 	s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight,
319 	    (attr >> 16)&0x0f, 0x0f);
320 }
321 
322 static void
323 fb_copyrows(void *v, int srcrow, int dstrow, int nrows)
324 {
325 	struct rasops_info *ri = v;
326 	struct fb_dev *fb = ri->ri_hw;
327 
328 	srcrow *= ri->ri_font->fontheight;
329 	dstrow *= ri->ri_font->fontheight;
330 	nrows *= ri->ri_font->fontheight;
331 
332 	s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f);
333 }
334 
335 static void
336 fb_copycols(void *v, int row, int srccol, int dstcol, int ncols)
337 {
338 	struct rasops_info *ri = v;
339 	struct fb_dev *fb = ri->ri_hw;
340 
341 	row *= ri->ri_font->fontheight;
342 	srccol *= ri->ri_font->fontwidth;
343 	dstcol *= ri->ri_font->fontwidth;
344 	ncols *= ri->ri_font->fontwidth;
345 
346 	s3_copy(fb, srccol, row, dstcol, row,
347 	    ncols, ri->ri_font->fontheight, 0x0f);
348 }
349 
350 /*
351  * S3 support routines
352  */
353 
354 #define S3_CRTC_INDEX		0x83d4
355 #define S3_CRTC_DATA		0x83d5
356 
357 #define S3_DAC_RD_INDEX		0x83c7
358 #define S3_DAC_WR_INDEX		0x83c8
359 #define S3_DAC_DATA		0x83c9
360 
361 #define S3_CUR_Y		0x82e8
362 #define S3_CUR_X		0x86e8
363 #define S3_DESTY_AXSTP		0x8ae8
364 #define S3_DESTX_DIASTP		0x8ee8
365 #define S3_MAJ_AXIS_PCNT	0x96e8
366 #define S3_GP_STAT		0x9ae8
367 #define S3_CMD			0x9ae8
368 #define S3_BKGD_COLOR		0xa2e8
369 #define S3_FRGD_COLOR		0xa6e8
370 #define S3_WRT_MASK		0xaae8
371 #define S3_RD_MASK		0xaee8
372 #define S3_BKGD_MIX		0xb6e8
373 #define S3_FRGD_MIX		0xbae8
374 #define S3_MULTIFUNC_CNTL	0xbee8
375 
376 #define S3_GP_STAT_FIFO_1	0x0080
377 #define S3_GP_STAT_BSY		0x0200
378 
379 #define S3_CMD_BITBLT		0xc001
380 #define S3_CMD_RECT		0x4001
381 #define S3_INC_Y		0x0080
382 #define S3_INC_X		0x0020
383 #define S3_DRAW			0x0010
384 #define S3_MULTI		0x0002
385 
386 #define S3_CSRC_BKGDCOL		0x0000
387 #define S3_CSRC_FRGDCOL		0x0020
388 #define S3_CSRC_DISPMEM		0x0060
389 #define S3_MIX_NEW		0x0007
390 
391 #define CMAP_SIZE		256
392 
393 static u_int8_t default_cmap[] = {
394 	/* black */		  0,   0,   0,
395 	/* blue */		  0,   0, 192,
396 	/* green */		  0, 192,   0,
397 	/* cyan */		  0, 192, 192,
398 	/* red */		192,   0,   0,
399 	/* magenta */		192,   0, 192,
400 	/* brown */		192, 192,   0,
401 	/* lightgrey */		212, 208, 200,
402 	/* darkgrey */		200, 192, 188,
403 	/* lightblue */		  0,   0, 255,
404 	/* lightgreen */	  0, 255,   0,
405 	/* lightcyan */		  0, 255, 255,
406 	/* lightred */		255,   0,   0,
407 	/* lightmagenta */	255,   0, 255,
408 	/* yellow */		255, 255,   0,
409 	/* white */		255, 255, 255,
410 };
411 
412 static void
413 s3_init(struct fb_dev *fb, int *width, int *height)
414 {
415 	int i, j, w, h;
416 	bus_space_tag_t iot = fb->fb_iot;
417 	bus_space_handle_t ioh = fb->fb_ioh;
418 
419 	/* Initialize colormap */
420 
421 	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);
422 
423 	for (i = j = 0; i < CMAP_SIZE*3; i++) {
424 		bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2);
425 		j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]);
426 	}
427 
428 	/* Retrieve frame buffer geometry */
429 
430 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
431 	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
432 
433 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
434 	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
435 
436 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
437 	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
438 
439 	h += (i << 7) & 0x100;
440 	h += (i << 3) & 0x200;
441 
442 	*width = (w+1) << 3;
443 	*height = h+1;
444 }
445 
446 static void
447 s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y,
448     int width, int height, int mask)
449 {
450 	bus_space_tag_t iot = fb->fb_iot;
451 	bus_space_handle_t ioh = fb->fb_ioh;
452 	u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW;
453 
454 	if (src_x > dest_x)
455 		cmd |= S3_INC_X;
456 	else {
457 		src_x += width-1;
458 		dest_x += width-1;
459 	}
460 
461 	if (src_y > dest_y)
462 		cmd |= S3_INC_Y;
463 	else {
464 		src_y += height-1;
465 		dest_y += height-1;
466 	}
467 
468 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
469 		;
470 
471 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW);
472 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
473 	bus_space_write_2(iot, ioh, S3_CUR_X, src_x);
474 	bus_space_write_2(iot, ioh, S3_CUR_Y, src_y);
475 	bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x);
476 	bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y);
477 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
478 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
479 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
480 
481 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
482 		;
483 }
484 
485 static void
486 s3_fill(struct fb_dev *fb, int x, int y, int width, int height,
487     int color, int mask)
488 {
489 	bus_space_tag_t iot = fb->fb_iot;
490 	bus_space_handle_t ioh = fb->fb_ioh;
491 	u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW;
492 
493 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
494 		;
495 
496 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW);
497 	bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color);
498 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
499 	bus_space_write_2(iot, ioh, S3_CUR_X, x);
500 	bus_space_write_2(iot, ioh, S3_CUR_Y, y);
501 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
502 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
503 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
504 
505 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
506 		;
507 }
508