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