1/*	$NetBSD: fb_elb.c,v 1.20 2021/08/07 16:18:52 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.20 2021/08/07 16:18:52 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#define CMAP_SIZE		256
53
54struct fb_dev {
55	void *fb_vram;
56	bus_space_tag_t fb_iot;
57	bus_space_handle_t fb_ioh;
58	struct rasops_info fb_ri;
59};
60
61struct fb_cmap {
62	uint8_t r;
63	uint8_t g;
64	uint8_t b;
65};
66
67struct fb_elb_softc {
68	device_t sc_dev;
69	struct fb_dev *sc_fb;
70	bus_addr_t sc_fbbase;
71	bus_size_t sc_fbsize;
72	int sc_nscreens;
73	int sc_mode;
74	struct fb_cmap sc_cmap[CMAP_SIZE];
75};
76
77/*
78 * We assume that rasops_cmap is compatible to sc_cmap.
79 */
80CTASSERT(sizeof(rasops_cmap) == CMAP_SIZE * 3);
81
82void		fb_cnattach(bus_space_tag_t, bus_addr_t, void *);
83
84static int	fb_elb_probe(device_t, cfdata_t, void *);
85static void	fb_elb_attach(device_t, device_t, void *);
86
87static void	fb_init(struct fb_dev *, int, int);
88static void	fb_initcmap(struct fb_elb_softc *);
89
90static int	fb_getcmap(struct fb_elb_softc *, struct wsdisplay_cmap *);
91static int	fb_putcmap(struct fb_elb_softc *,
92		    const struct wsdisplay_cmap *);
93
94static int	fb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
95static paddr_t	fb_mmap(void *, void *, off_t, int);
96static int	fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
97                    int *, int *, long *);
98static void	fb_free_screen(void *, void *);
99static int	fb_show_screen(void *, void *, int, void (*)(void *, int, int),
100                    void *);
101
102static void	fb_eraserows(void *, int, int, long);
103static void	fb_erasecols(void *, int, int, int, long);
104static void	fb_copyrows(void *, int, int, int);
105static void	fb_copycols(void *, int, int, int, int);
106
107static void	s3_getgeometry(struct fb_dev *, int *, int *);
108static void	s3_putcmap(struct fb_dev *, const struct fb_cmap *);
109static void	s3_copy(struct fb_dev *, int, int, int, int, int, int, int);
110static void	s3_fill(struct fb_dev *, int, int, int, int, int, int);
111
112static struct fb_dev console_dev;
113
114static struct wsdisplay_accessops accessops = {
115	fb_ioctl,
116	fb_mmap,
117	fb_alloc_screen,
118	fb_free_screen,
119	fb_show_screen,
120	NULL
121};
122
123static struct wsscreen_descr stdscreen = {
124	"std",
125	0, 0,
126	0,
127	0, 0,
128	0
129};
130
131static const struct wsscreen_descr *scrlist[] = {
132	&stdscreen
133};
134
135static struct wsscreen_list screenlist = {
136	__arraycount(scrlist), scrlist
137};
138
139void
140fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
141{
142	struct rasops_info *ri = &console_dev.fb_ri;
143	long defattr;
144	int width, height;
145
146	console_dev.fb_iot = iot;
147	console_dev.fb_ioh = iobase;
148	console_dev.fb_vram = vram;
149
150	s3_getgeometry(&console_dev, &width, &height);
151
152	fb_init(&console_dev, width, height);
153
154	s3_putcmap(&console_dev, (const struct fb_cmap*)rasops_cmap);
155
156	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
157
158	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
159}
160
161CFATTACH_DECL_NEW(fb_elb, sizeof(struct fb_elb_softc),
162    fb_elb_probe, fb_elb_attach, NULL, NULL);
163
164static int
165fb_elb_probe(device_t parent, cfdata_t cf, void *aux)
166{
167	struct elb_attach_args *oaa = aux;
168
169	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
170		return 0;
171
172	return 1;
173}
174
175static void
176fb_elb_attach(device_t parent, device_t self, void *aux)
177{
178	struct fb_elb_softc *sc = device_private(self);
179	struct elb_attach_args *eaa = aux;
180	struct wsemuldisplaydev_attach_args waa;
181	bus_space_handle_t ioh;
182	int is_console, width, height;
183
184	sc->sc_dev = self;
185
186	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);
187
188	if (is_console)
189		sc->sc_fb = &console_dev;
190	else
191		sc->sc_fb = kmem_zalloc(sizeof(struct fb_dev), KM_SLEEP);
192
193	sc->sc_fb->fb_iot = eaa->elb_bt;
194	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
195	    0, &sc->sc_fb->fb_ioh);
196
197	s3_getgeometry(sc->sc_fb, &width, &height);
198
199	sc->sc_fbbase = eaa->elb_base;
200	sc->sc_fbsize = width * height;
201	bus_space_map(sc->sc_fb->fb_iot, sc->sc_fbbase, sc->sc_fbsize,
202	    BUS_SPACE_MAP_LINEAR, &ioh);
203	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
204
205	if (!is_console)
206		fb_init(sc->sc_fb, width, height);
207	fb_initcmap(sc);
208
209	printf(": %dx%d 8bpp\n", width, height);
210
211	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
212	waa.console = is_console;
213	waa.scrdata = &screenlist;
214	waa.accessops = &accessops;
215	waa.accesscookie = sc;
216
217	config_found(self, &waa, wsemuldisplaydevprint, CFARGS_NONE);
218}
219
220static void
221fb_init(struct fb_dev *fb, int width, int height)
222{
223	struct rasops_info *ri = &fb->fb_ri;
224
225	ri->ri_width = width;
226	ri->ri_height = height;
227	ri->ri_depth = 8;
228	ri->ri_stride = ri->ri_width;
229	ri->ri_bits = fb->fb_vram;
230	ri->ri_flg = RI_CENTER | RI_CLEAR;
231	if (ri == &console_dev.fb_ri)
232		ri->ri_flg |= RI_NO_AUTO;
233
234	rasops_init(ri, 500, 500);
235
236	/* Replace the copy/erase ops. */
237	ri->ri_hw = fb;
238	ri->ri_ops.eraserows = fb_eraserows;
239	ri->ri_ops.erasecols = fb_erasecols;
240	ri->ri_ops.copyrows = fb_copyrows;
241	ri->ri_ops.copycols = fb_copycols;
242
243	stdscreen.nrows = ri->ri_rows;
244	stdscreen.ncols = ri->ri_cols;
245	stdscreen.textops = &ri->ri_ops;
246	stdscreen.capabilities = ri->ri_caps;
247}
248
249static void
250fb_initcmap(struct fb_elb_softc *sc)
251{
252
253	memcpy(&sc->sc_cmap, rasops_cmap, sizeof(sc->sc_cmap));
254	s3_putcmap(sc->sc_fb, sc->sc_cmap);
255}
256
257static int
258fb_getcmap(struct fb_elb_softc *sc, struct wsdisplay_cmap *p)
259{
260	u_int index = p->index, count = p->count;
261	uint8_t buf[CMAP_SIZE * 3];
262	int error, i;
263
264	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
265		return EINVAL;
266
267	for (i = 0; i < count; i++) {
268		buf[i]             = sc->sc_cmap[index + i].r;
269		buf[i + count]     = sc->sc_cmap[index + i].g;
270		buf[i + 2 * count] = sc->sc_cmap[index + i].b;
271	}
272
273	if ((error = copyout(&buf[0],         p->red,   count)) != 0 ||
274	    (error = copyout(&buf[count],     p->green, count)) != 0 ||
275	    (error = copyout(&buf[2 * count], p->blue,  count)) != 0)
276		return error;
277
278	return 0;
279}
280
281static int
282fb_putcmap(struct fb_elb_softc *sc, const struct wsdisplay_cmap *p)
283{
284	u_int index = p->index, count = p->count;
285	uint8_t buf[CMAP_SIZE * 3];
286	int error, i;
287
288	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
289		return EINVAL;
290
291	if ((error = copyin(p->red,   &buf[0],         count)) != 0 ||
292	    (error = copyin(p->green, &buf[count],     count)) != 0 ||
293	    (error = copyin(p->blue,  &buf[2 * count], count)) != 0)
294		return error;
295
296	for (i = 0; i < count; i++) {
297		sc->sc_cmap[index + i].r = buf[i];
298		sc->sc_cmap[index + i].g = buf[i + count];
299		sc->sc_cmap[index + i].b = buf[i + 2 * count];
300	}
301
302	s3_putcmap(sc->sc_fb, sc->sc_cmap);
303
304	return 0;
305}
306
307static int
308fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
309{
310	struct fb_elb_softc *sc = v;
311	struct rasops_info *ri = &sc->sc_fb->fb_ri;
312	struct wsdisplay_fbinfo *wdf;
313	int new_mode;
314
315	switch (cmd) {
316	case WSDISPLAYIO_GTYPE:
317		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
318		return 0;
319
320	case WSDISPLAYIO_GINFO:
321		wdf = (void *)data;
322		wdf->height = ri->ri_height;
323		wdf->width = ri->ri_width;
324		wdf->depth = ri->ri_depth;
325		wdf->cmsize = CMAP_SIZE;
326		return 0;
327
328	case WSDISPLAYIO_LINEBYTES:
329		*(u_int *)data = ri->ri_stride;
330		return 0;
331
332	case WSDISPLAYIO_GETCMAP:
333		return fb_getcmap(sc, (struct wsdisplay_cmap *)data);
334
335	case WSDISPLAYIO_PUTCMAP:
336		return fb_putcmap(sc, (struct wsdisplay_cmap *)data);
337
338	case WSDISPLAYIO_SMODE:
339		new_mode = *(int *)data;
340		if (new_mode != sc->sc_mode) {
341			sc->sc_mode = new_mode;
342			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
343				/* XXX */
344				memset(sc->sc_fb->fb_vram, 0, sc->sc_fbsize);
345
346				fb_initcmap(sc);
347			}
348		}
349		return 0;
350
351	default:
352		break;
353	}
354
355	return EPASSTHROUGH;
356}
357
358static paddr_t
359fb_mmap(void *v, void *vs, off_t offset, int prot)
360{
361	struct fb_elb_softc *sc = v;
362
363	if (offset < 0 || offset >= sc->sc_fbsize)
364		return -1;
365
366	return bus_space_mmap(sc->sc_fb->fb_iot, sc->sc_fbbase, offset, prot,
367	    BUS_SPACE_MAP_LINEAR);
368}
369
370static int
371fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
372    int *ccolp, int *crowp, long *attrp)
373{
374	struct fb_elb_softc *sc = v;
375	struct rasops_info *ri = &sc->sc_fb->fb_ri;
376
377	if (sc->sc_nscreens > 0)
378		return ENOMEM;
379
380	*cookiep = ri;
381	*ccolp = *crowp = 0;
382	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
383	sc->sc_nscreens++;
384
385	return 0;
386}
387
388static void
389fb_free_screen(void *v, void *cookie)
390{
391	struct fb_elb_softc *sc = v;
392
393	if (sc->sc_fb == &console_dev)
394		panic("fb_free_screen: freeing console");
395
396	sc->sc_nscreens--;
397}
398
399static int
400fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
401    void *cbarg)
402{
403
404	return 0;
405}
406
407static void
408fb_eraserows(void *v, int row, int nrows, long attr)
409{
410	struct rasops_info *ri = v;
411	struct fb_dev *fb = ri->ri_hw;
412
413	row *= ri->ri_font->fontheight;
414	nrows *= ri->ri_font->fontheight;
415
416	s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f);
417}
418
419static void
420fb_erasecols(void *v, int row, int startcol, int ncols, long attr)
421{
422	struct rasops_info *ri = v;
423	struct fb_dev *fb = ri->ri_hw;
424
425	row *= ri->ri_font->fontheight;
426	startcol *= ri->ri_font->fontwidth;
427	ncols *= ri->ri_font->fontwidth;
428
429	s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight,
430	    (attr >> 16)&0x0f, 0x0f);
431}
432
433static void
434fb_copyrows(void *v, int srcrow, int dstrow, int nrows)
435{
436	struct rasops_info *ri = v;
437	struct fb_dev *fb = ri->ri_hw;
438
439	srcrow *= ri->ri_font->fontheight;
440	dstrow *= ri->ri_font->fontheight;
441	nrows *= ri->ri_font->fontheight;
442
443	s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f);
444}
445
446static void
447fb_copycols(void *v, int row, int srccol, int dstcol, int ncols)
448{
449	struct rasops_info *ri = v;
450	struct fb_dev *fb = ri->ri_hw;
451
452	row *= ri->ri_font->fontheight;
453	srccol *= ri->ri_font->fontwidth;
454	dstcol *= ri->ri_font->fontwidth;
455	ncols *= ri->ri_font->fontwidth;
456
457	s3_copy(fb, srccol, row, dstcol, row,
458	    ncols, ri->ri_font->fontheight, 0x0f);
459}
460
461/*
462 * S3 support routines
463 */
464
465#define S3_CRTC_INDEX		0x83d4
466#define S3_CRTC_DATA		0x83d5
467
468#define S3_DAC_RD_INDEX		0x83c7
469#define S3_DAC_WR_INDEX		0x83c8
470#define S3_DAC_DATA		0x83c9
471
472#define S3_CUR_Y		0x82e8
473#define S3_CUR_X		0x86e8
474#define S3_DESTY_AXSTP		0x8ae8
475#define S3_DESTX_DIASTP		0x8ee8
476#define S3_MAJ_AXIS_PCNT	0x96e8
477#define S3_GP_STAT		0x9ae8
478#define S3_CMD			0x9ae8
479#define S3_BKGD_COLOR		0xa2e8
480#define S3_FRGD_COLOR		0xa6e8
481#define S3_WRT_MASK		0xaae8
482#define S3_RD_MASK		0xaee8
483#define S3_BKGD_MIX		0xb6e8
484#define S3_FRGD_MIX		0xbae8
485#define S3_MULTIFUNC_CNTL	0xbee8
486
487#define S3_GP_STAT_FIFO_1	0x0080
488#define S3_GP_STAT_BSY		0x0200
489
490#define S3_CMD_BITBLT		0xc001
491#define S3_CMD_RECT		0x4001
492#define S3_INC_Y		0x0080
493#define S3_INC_X		0x0020
494#define S3_DRAW			0x0010
495#define S3_MULTI		0x0002
496
497#define S3_CSRC_BKGDCOL		0x0000
498#define S3_CSRC_FRGDCOL		0x0020
499#define S3_CSRC_DISPMEM		0x0060
500#define S3_MIX_NEW		0x0007
501
502static void
503s3_getgeometry(struct fb_dev *fb, int *width, int *height)
504{
505	bus_space_tag_t iot = fb->fb_iot;
506	bus_space_handle_t ioh = fb->fb_ioh;
507	int w, h, i;
508
509	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
510	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
511
512	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
513	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
514
515	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
516	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
517
518	h += (i << 7) & 0x100;
519	h += (i << 3) & 0x200;
520
521	*width = (w+1) << 3;
522	*height = h+1;
523}
524
525static void
526s3_putcmap(struct fb_dev *fb, const struct fb_cmap *cmap)
527{
528	bus_space_tag_t iot = fb->fb_iot;
529	bus_space_handle_t ioh = fb->fb_ioh;
530	int i;
531
532	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);
533	for (i = 0; i < CMAP_SIZE; i++) {
534		bus_space_write_1(iot, ioh, S3_DAC_DATA, cmap[i].r >> 2);
535		bus_space_write_1(iot, ioh, S3_DAC_DATA, cmap[i].g >> 2);
536		bus_space_write_1(iot, ioh, S3_DAC_DATA, cmap[i].b >> 2);
537	}
538}
539
540static void
541s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y,
542    int width, int height, int mask)
543{
544	bus_space_tag_t iot = fb->fb_iot;
545	bus_space_handle_t ioh = fb->fb_ioh;
546	u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW;
547
548	if (src_x > dest_x)
549		cmd |= S3_INC_X;
550	else {
551		src_x += width-1;
552		dest_x += width-1;
553	}
554
555	if (src_y > dest_y)
556		cmd |= S3_INC_Y;
557	else {
558		src_y += height-1;
559		dest_y += height-1;
560	}
561
562	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
563		;
564
565	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW);
566	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
567	bus_space_write_2(iot, ioh, S3_CUR_X, src_x);
568	bus_space_write_2(iot, ioh, S3_CUR_Y, src_y);
569	bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x);
570	bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y);
571	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
572	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
573	bus_space_write_2(iot, ioh, S3_CMD, cmd);
574
575	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
576		;
577}
578
579static void
580s3_fill(struct fb_dev *fb, int x, int y, int width, int height,
581    int color, int mask)
582{
583	bus_space_tag_t iot = fb->fb_iot;
584	bus_space_handle_t ioh = fb->fb_ioh;
585	u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW;
586
587	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
588		;
589
590	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW);
591	bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color);
592	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
593	bus_space_write_2(iot, ioh, S3_CUR_X, x);
594	bus_space_write_2(iot, ioh, S3_CUR_Y, y);
595	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
596	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
597	bus_space_write_2(iot, ioh, S3_CMD, cmd);
598
599	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
600		;
601}
602