xref: /netbsd-src/sys/arch/evbppc/explora/dev/fb_elb.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /*	$NetBSD: fb_elb.c,v 1.14 2020/11/21 15:42:20 thorpej 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.14 2020/11/21 15:42:20 thorpej 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/kmem.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 <sys/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 	device_t sc_dev;
62 	struct fb_dev *sc_fb;
63 	int sc_nscreens;
64 };
65 
66 static int	fb_elb_probe(device_t, cfdata_t, void *);
67 static void	fb_elb_attach(device_t, device_t, 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 	__arraycount(scrlist), scrlist
112 };
113 
114 CFATTACH_DECL_NEW(fb_elb, sizeof(struct fb_elb_softc),
115     fb_elb_probe, fb_elb_attach, NULL, NULL);
116 
117 static int
118 fb_elb_probe(device_t parent, cfdata_t 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(device_t parent, device_t self, void *aux)
130 {
131 	struct fb_elb_softc *sc = device_private(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 	sc->sc_dev = self;
139 
140 	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);
141 
142 	if (is_console) {
143 		sc->sc_fb = &console_dev;
144 		sc->sc_fb->fb_ri.ri_flg &= ~RI_NO_AUTO;
145 	} else {
146 		sc->sc_fb = kmem_zalloc(sizeof(struct fb_dev), KM_SLEEP);
147 	}
148 
149 	sc->sc_fb->fb_iot = eaa->elb_bt;
150 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB,
151 	    BUS_SPACE_MAP_LINEAR, &ioh);
152 	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
153 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
154 	    0, &sc->sc_fb->fb_ioh);
155 
156 	fb_init(sc->sc_fb, !is_console);
157 
158 	ri = &sc->sc_fb->fb_ri;
159 
160 	printf(": %d x %d\n", ri->ri_rows, ri->ri_cols);
161 
162 	waa.console = is_console;
163 	waa.scrdata = &screenlist;
164 	waa.accessops = &accessops;
165 	waa.accesscookie = sc;
166 
167 	config_found(self, &waa, wsemuldisplaydevprint);
168 }
169 
170 static void
171 fb_init(struct fb_dev *fb, int full)
172 {
173 	struct rasops_info *ri = &fb->fb_ri;
174 
175 	if (full) {
176 		s3_init(fb, &ri->ri_width, &ri->ri_height);
177 		ri->ri_depth = 8;
178 		ri->ri_stride = ri->ri_width;
179 		ri->ri_bits = fb->fb_vram;
180 		ri->ri_flg = RI_CENTER;
181 		if (ri == &console_dev.fb_ri)
182 			ri->ri_flg |= RI_NO_AUTO;
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, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
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, void *vs, off_t offset, int prot)
234 {
235 	struct fb_elb_softc *sc = v;
236 
237 	if (offset < 0 || offset >= SIZE_FB)
238 		return -1;
239 
240 	return bus_space_mmap(sc->sc_fb->fb_iot, BASE_FB, offset, prot,
241 	    BUS_SPACE_MAP_LINEAR);
242 }
243 
244 static int
245 fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
246     int *ccolp, int *crowp, long *attrp)
247 {
248 	struct fb_elb_softc *sc = v;
249 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
250 
251 	if (sc->sc_nscreens > 0)
252 		return ENOMEM;
253 
254 	*cookiep = ri;
255 	*ccolp = *crowp = 0;
256 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
257 	sc->sc_nscreens++;
258 
259 	return(0);
260 }
261 
262 static void
263 fb_free_screen(void *v, void *cookie)
264 {
265 	struct fb_elb_softc *sc = v;
266 
267 	if (sc->sc_fb == &console_dev)
268 		panic("fb_free_screen: freeing console");
269 
270 	sc->sc_nscreens--;
271 }
272 
273 static int
274 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
275     void *cbarg)
276 {
277 	return(0);
278 }
279 
280 void
281 fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
282 {
283 	struct rasops_info *ri = &console_dev.fb_ri;
284 	long defattr;
285 
286 	console_dev.fb_iot = iot;
287 	console_dev.fb_ioh = iobase;
288 	console_dev.fb_vram = vram;
289 
290 	fb_init(&console_dev, 1);
291 
292 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
293 
294 	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
295 }
296 
297 static void
298 fb_eraserows(void *v, int row, int nrows, long attr)
299 {
300 	struct rasops_info *ri = v;
301 	struct fb_dev *fb = ri->ri_hw;
302 
303 	row *= ri->ri_font->fontheight;
304 	nrows *= ri->ri_font->fontheight;
305 
306 	s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f);
307 }
308 
309 static void
310 fb_erasecols(void *v, int row, int startcol, int ncols, long attr)
311 {
312 	struct rasops_info *ri = v;
313 	struct fb_dev *fb = ri->ri_hw;
314 
315 	row *= ri->ri_font->fontheight;
316 	startcol *= ri->ri_font->fontwidth;
317 	ncols *= ri->ri_font->fontwidth;
318 
319 	s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight,
320 	    (attr >> 16)&0x0f, 0x0f);
321 }
322 
323 static void
324 fb_copyrows(void *v, int srcrow, int dstrow, int nrows)
325 {
326 	struct rasops_info *ri = v;
327 	struct fb_dev *fb = ri->ri_hw;
328 
329 	srcrow *= ri->ri_font->fontheight;
330 	dstrow *= ri->ri_font->fontheight;
331 	nrows *= ri->ri_font->fontheight;
332 
333 	s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f);
334 }
335 
336 static void
337 fb_copycols(void *v, int row, int srccol, int dstcol, int ncols)
338 {
339 	struct rasops_info *ri = v;
340 	struct fb_dev *fb = ri->ri_hw;
341 
342 	row *= ri->ri_font->fontheight;
343 	srccol *= ri->ri_font->fontwidth;
344 	dstcol *= ri->ri_font->fontwidth;
345 	ncols *= ri->ri_font->fontwidth;
346 
347 	s3_copy(fb, srccol, row, dstcol, row,
348 	    ncols, ri->ri_font->fontheight, 0x0f);
349 }
350 
351 /*
352  * S3 support routines
353  */
354 
355 #define S3_CRTC_INDEX		0x83d4
356 #define S3_CRTC_DATA		0x83d5
357 
358 #define S3_DAC_RD_INDEX		0x83c7
359 #define S3_DAC_WR_INDEX		0x83c8
360 #define S3_DAC_DATA		0x83c9
361 
362 #define S3_CUR_Y		0x82e8
363 #define S3_CUR_X		0x86e8
364 #define S3_DESTY_AXSTP		0x8ae8
365 #define S3_DESTX_DIASTP		0x8ee8
366 #define S3_MAJ_AXIS_PCNT	0x96e8
367 #define S3_GP_STAT		0x9ae8
368 #define S3_CMD			0x9ae8
369 #define S3_BKGD_COLOR		0xa2e8
370 #define S3_FRGD_COLOR		0xa6e8
371 #define S3_WRT_MASK		0xaae8
372 #define S3_RD_MASK		0xaee8
373 #define S3_BKGD_MIX		0xb6e8
374 #define S3_FRGD_MIX		0xbae8
375 #define S3_MULTIFUNC_CNTL	0xbee8
376 
377 #define S3_GP_STAT_FIFO_1	0x0080
378 #define S3_GP_STAT_BSY		0x0200
379 
380 #define S3_CMD_BITBLT		0xc001
381 #define S3_CMD_RECT		0x4001
382 #define S3_INC_Y		0x0080
383 #define S3_INC_X		0x0020
384 #define S3_DRAW			0x0010
385 #define S3_MULTI		0x0002
386 
387 #define S3_CSRC_BKGDCOL		0x0000
388 #define S3_CSRC_FRGDCOL		0x0020
389 #define S3_CSRC_DISPMEM		0x0060
390 #define S3_MIX_NEW		0x0007
391 
392 #define CMAP_SIZE		256
393 
394 static u_int8_t default_cmap[] = {
395 	/* black */		  0,   0,   0,
396 	/* blue */		  0,   0, 192,
397 	/* green */		  0, 192,   0,
398 	/* cyan */		  0, 192, 192,
399 	/* red */		192,   0,   0,
400 	/* magenta */		192,   0, 192,
401 	/* brown */		192, 192,   0,
402 	/* lightgrey */		212, 208, 200,
403 	/* darkgrey */		200, 192, 188,
404 	/* lightblue */		  0,   0, 255,
405 	/* lightgreen */	  0, 255,   0,
406 	/* lightcyan */		  0, 255, 255,
407 	/* lightred */		255,   0,   0,
408 	/* lightmagenta */	255,   0, 255,
409 	/* yellow */		255, 255,   0,
410 	/* white */		255, 255, 255,
411 };
412 
413 static void
414 s3_init(struct fb_dev *fb, int *width, int *height)
415 {
416 	int i, j, w, h;
417 	bus_space_tag_t iot = fb->fb_iot;
418 	bus_space_handle_t ioh = fb->fb_ioh;
419 
420 	/* Initialize colormap */
421 
422 	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);
423 
424 	for (i = j = 0; i < CMAP_SIZE*3; i++) {
425 		bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2);
426 		j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]);
427 	}
428 
429 	/* Retrieve frame buffer geometry */
430 
431 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
432 	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
433 
434 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
435 	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
436 
437 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
438 	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
439 
440 	h += (i << 7) & 0x100;
441 	h += (i << 3) & 0x200;
442 
443 	*width = (w+1) << 3;
444 	*height = h+1;
445 }
446 
447 static void
448 s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y,
449     int width, int height, int mask)
450 {
451 	bus_space_tag_t iot = fb->fb_iot;
452 	bus_space_handle_t ioh = fb->fb_ioh;
453 	u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW;
454 
455 	if (src_x > dest_x)
456 		cmd |= S3_INC_X;
457 	else {
458 		src_x += width-1;
459 		dest_x += width-1;
460 	}
461 
462 	if (src_y > dest_y)
463 		cmd |= S3_INC_Y;
464 	else {
465 		src_y += height-1;
466 		dest_y += height-1;
467 	}
468 
469 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
470 		;
471 
472 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW);
473 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
474 	bus_space_write_2(iot, ioh, S3_CUR_X, src_x);
475 	bus_space_write_2(iot, ioh, S3_CUR_Y, src_y);
476 	bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x);
477 	bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y);
478 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
479 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
480 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
481 
482 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
483 		;
484 }
485 
486 static void
487 s3_fill(struct fb_dev *fb, int x, int y, int width, int height,
488     int color, int mask)
489 {
490 	bus_space_tag_t iot = fb->fb_iot;
491 	bus_space_handle_t ioh = fb->fb_ioh;
492 	u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW;
493 
494 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
495 		;
496 
497 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW);
498 	bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color);
499 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
500 	bus_space_write_2(iot, ioh, S3_CUR_X, x);
501 	bus_space_write_2(iot, ioh, S3_CUR_Y, y);
502 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
503 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
504 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
505 
506 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
507 		;
508 }
509