xref: /netbsd-src/sys/arch/newsmips/apbus/xafb.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: xafb.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* "xa" frame buffer driver.  Currently supports 1280x1024x8 only. */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xafb.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/kmem.h>
39 #include <sys/systm.h>
40 
41 #include <uvm/uvm_extern.h>
42 
43 #include <mips/locore.h>
44 
45 #include <machine/adrsmap.h>
46 #include <machine/apcall.h>
47 #include <machine/wsconsio.h>
48 
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/rasops/rasops.h>
51 
52 #include <newsmips/apbus/apbusvar.h>
53 
54 struct xafb_reg {
55 	volatile uint32_t r0;
56 	volatile uint32_t index;
57 	volatile uint32_t r2;
58 	volatile uint32_t zero;
59 	volatile uint32_t r4;
60 	volatile uint32_t r5;
61 	volatile uint32_t r6;
62 	volatile uint32_t rgb;
63 };
64 
65 struct xafb_devconfig {
66 	uint8_t *dc_fbbase;		/* VRAM base address */
67 	paddr_t dc_fbpaddr;		/* VRAM physical address */
68 	struct xafb_reg *dc_reg;	/* register address */
69 	struct rasops_info dc_ri;
70 };
71 
72 struct xafb_softc {
73 	device_t sc_dev;
74 	struct xafb_devconfig *sc_dc;
75 	int sc_nscreens;
76 	uint8_t sc_cmap_red[256];
77 	uint8_t sc_cmap_green[256];
78 	uint8_t sc_cmap_blue[256];
79 };
80 
81 int xafb_match(device_t, cfdata_t, void *);
82 void xafb_attach(device_t, device_t, void *);
83 
84 int xafb_common_init(struct xafb_devconfig *);
85 int xafb_is_console(void);
86 
87 int xafb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
88 paddr_t xafb_mmap(void *, void *, off_t, int);
89 int xafb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
90     int *, long *);
91 void xafb_free_screen(void *, void *);
92 int xafb_show_screen(void *, void *, int, void (*) (void *, int, int), void *);
93 
94 int xafb_cnattach(void);
95 int xafb_getcmap(struct xafb_softc *, struct wsdisplay_cmap *);
96 int xafb_putcmap(struct xafb_softc *, struct wsdisplay_cmap *);
97 
98 static inline void xafb_setcolor(struct xafb_devconfig *, int, int, int, int);
99 
100 CFATTACH_DECL_NEW(xafb, sizeof(struct xafb_softc),
101     xafb_match, xafb_attach, NULL, NULL);
102 
103 struct xafb_devconfig xafb_console_dc;
104 
105 struct wsdisplay_accessops xafb_accessops = {
106 	xafb_ioctl,
107 	xafb_mmap,
108 	xafb_alloc_screen,
109 	xafb_free_screen,
110 	xafb_show_screen,
111 	NULL	/* load_font */
112 };
113 
114 struct wsscreen_descr xafb_stdscreen = {
115 	"std",
116 	0, 0,
117 	0,
118 	0, 0,
119 	WSSCREEN_REVERSE
120 };
121 
122 const struct wsscreen_descr *xafb_scrlist[] = {
123 	&xafb_stdscreen
124 };
125 
126 struct wsscreen_list xafb_screenlist = {
127 	__arraycount(xafb_scrlist), xafb_scrlist
128 };
129 
130 int
xafb_match(device_t parent,cfdata_t cf,void * aux)131 xafb_match(device_t parent, cfdata_t cf, void *aux)
132 {
133 	struct apbus_attach_args *apa = aux;
134 
135 	if (strcmp(apa->apa_name, "xa") != 0)
136 		return 0;
137 
138 	return 1;
139 }
140 
141 void
xafb_attach(device_t parent,device_t self,void * aux)142 xafb_attach(device_t parent, device_t self, void *aux)
143 {
144 	struct xafb_softc *sc = device_private(self);
145 	struct apbus_attach_args *apa = aux;
146 	struct wsemuldisplaydev_attach_args wsa;
147 	struct xafb_devconfig *dc;
148 	struct rasops_info *ri;
149 	int console, i;
150 
151 	sc->sc_dev = self;
152 	console = xafb_is_console();
153 
154 	if (console) {
155 		dc = &xafb_console_dc;
156 		ri = &dc->dc_ri;
157 		ri->ri_flg &= ~RI_NO_AUTO;
158 		sc->sc_nscreens = 1;
159 	} else {
160 		dc = kmem_zalloc(sizeof(struct xafb_devconfig), KM_SLEEP);
161 		dc->dc_fbpaddr = (paddr_t)0x10000000;
162 		dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
163 		dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
164 		if (xafb_common_init(dc) != 0) {
165 			aprint_error(": couldn't initialize device\n");
166 			return;
167 		}
168 
169 		ri = &dc->dc_ri;
170 
171 		/* clear screen */
172 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
173 	}
174 	sc->sc_dc = dc;
175 
176 	for (i = 0; i < 256; i++) {
177 		sc->sc_cmap_red[i] = i;
178 		sc->sc_cmap_green[i] = i;
179 		sc->sc_cmap_blue[i] = i;
180 	}
181 
182 	aprint_normal(": %d x %d, %dbpp\n",
183 	    ri->ri_width, ri->ri_height, ri->ri_depth);
184 
185 	wsa.console = console;
186 	wsa.scrdata = &xafb_screenlist;
187 	wsa.accessops = &xafb_accessops;
188 	wsa.accesscookie = sc;
189 
190 	config_found(self, &wsa, wsemuldisplaydevprint, CFARGS_NONE);
191 }
192 
193 void
xafb_setcolor(struct xafb_devconfig * dc,int index,int r,int g,int b)194 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
195 {
196 	volatile struct xafb_reg *reg = dc->dc_reg;
197 
198 	reg->index = index;
199 	reg->zero = 0;
200 	reg->rgb = r;
201 	reg->rgb = g;
202 	reg->rgb = b;
203 }
204 
205 int
xafb_common_init(struct xafb_devconfig * dc)206 xafb_common_init(struct xafb_devconfig *dc)
207 {
208 	struct rasops_info *ri = &dc->dc_ri;
209 	int i;
210 
211 	for (i = 0; i < 256; i++)
212 		xafb_setcolor(dc, i, i, i, i);
213 
214 	/* initialize rasops */
215 	ri->ri_width = 1280;
216 	ri->ri_height = 1024;
217 	ri->ri_depth = 8;
218 	ri->ri_stride = 2048;
219 	ri->ri_bits = (void *)dc->dc_fbbase;
220 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
221 	if (dc == &xafb_console_dc)
222 		ri->ri_flg |= RI_NO_AUTO;
223 
224 	rasops_init(ri, 44, 100);
225 
226 	/* mono */
227 	ri->ri_devcmap[0] = 0;				/* bg */
228 	ri->ri_devcmap[1] = 0xffffffff;			/* fg */
229 
230 	xafb_stdscreen.nrows = ri->ri_rows;
231 	xafb_stdscreen.ncols = ri->ri_cols;
232 	xafb_stdscreen.textops = &ri->ri_ops;
233 	xafb_stdscreen.capabilities = ri->ri_caps;
234 
235 	return 0;
236 }
237 
238 int
xafb_is_console(void)239 xafb_is_console(void)
240 {
241 	volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
242 
243 	if (*dipsw & 1)					/* XXX right? */
244 		return 1;
245 
246 	return 0;
247 }
248 
249 int
xafb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)250 xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
251 {
252 	struct xafb_softc *sc = v;
253 	struct xafb_devconfig *dc = sc->sc_dc;
254 	struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
255 	struct wsdisplay_fbinfo *wdf = (void *)data;
256 
257 	switch (cmd) {
258 	case WSDISPLAYIO_GTYPE:
259 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
260 		return 0;
261 
262 	case NEWSMIPS_WSDISPLAYIO_GINFO:
263 		nwdf->stride = dc->dc_ri.ri_stride;
264 		/* FALLTHROUGH */
265 	case WSDISPLAYIO_GINFO:
266 		wdf->height = dc->dc_ri.ri_height;
267 		wdf->width = dc->dc_ri.ri_width;
268 		wdf->depth = dc->dc_ri.ri_depth;
269 		wdf->cmsize = 256;
270 		return 0;
271 
272 	case WSDISPLAYIO_LINEBYTES:
273 		*(u_int *)data = dc->dc_ri.ri_stride;
274 		return 0;
275 
276 	case WSDISPLAYIO_GETCMAP:
277 		return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
278 
279 	case WSDISPLAYIO_PUTCMAP:
280 		return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
281 
282 	/* case WSDISPLAYIO_SVIDEO: */
283 	}
284 	return EPASSTHROUGH;
285 }
286 
287 paddr_t
xafb_mmap(void * v,void * vs,off_t offset,int prot)288 xafb_mmap(void *v, void *vs, off_t offset, int prot)
289 {
290 	struct xafb_softc *sc = v;
291 	struct xafb_devconfig *dc = sc->sc_dc;
292 	struct rasops_info *ri = &dc->dc_ri;
293 
294 	if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
295 		return -1;
296 
297 	return mips_btop(dc->dc_fbpaddr + offset);
298 }
299 
300 int
xafb_alloc_screen(void * v,const struct wsscreen_descr * scrdesc,void ** cookiep,int * ccolp,int * crowp,long * attrp)301 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
302     void **cookiep, int *ccolp, int *crowp, long *attrp)
303 {
304 	struct xafb_softc *sc = v;
305 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
306 	long defattr;
307 
308 	if (sc->sc_nscreens > 0)
309 		return ENOMEM;
310 
311 	*cookiep = ri;
312 	*ccolp = *crowp = 0;
313 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
314 	*attrp = defattr;
315 	sc->sc_nscreens++;
316 
317 	return 0;
318 }
319 
320 void
xafb_free_screen(void * v,void * cookie)321 xafb_free_screen(void *v, void *cookie)
322 {
323 	struct xafb_softc *sc = v;
324 
325 	if (sc->sc_dc == &xafb_console_dc)
326 		panic("xafb_free_screen: console");
327 
328 	sc->sc_nscreens--;
329 }
330 
331 int
xafb_show_screen(void * v,void * cookie,int waitok,void (* cb)(void *,int,int),void * cbarg)332 xafb_show_screen(void *v, void *cookie, int waitok,
333     void (*cb)(void *, int, int), void *cbarg)
334 {
335 
336 	return 0;
337 }
338 
339 int
xafb_cnattach(void)340 xafb_cnattach(void)
341 {
342 	struct xafb_devconfig *dc = &xafb_console_dc;
343 	struct rasops_info *ri = &dc->dc_ri;
344 	long defattr;
345 	int crow = 0;
346 
347 	if (!xafb_is_console())
348 		return -1;
349 
350 	dc->dc_fbpaddr = (paddr_t)0x10000000;
351 	dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
352 	dc->dc_reg = (void *)0xb4903000;			/* XXX */
353 	xafb_common_init(dc);
354 
355 	crow = 0;			/* XXX current cursor pos */
356 
357 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
358 	wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
359 
360 	return 0;
361 }
362 
363 int
xafb_getcmap(struct xafb_softc * sc,struct wsdisplay_cmap * cm)364 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
365 {
366 	u_int index = cm->index;
367 	u_int count = cm->count;
368 	int error;
369 
370 	if (index >= 256 || count > 256 || index + count > 256)
371 		return EINVAL;
372 
373 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
374 	if (error)
375 		return error;
376 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
377 	if (error)
378 		return error;
379 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
380 	if (error)
381 		return error;
382 
383 	return 0;
384 }
385 
386 int
xafb_putcmap(struct xafb_softc * sc,struct wsdisplay_cmap * cm)387 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
388 {
389 	u_int index = cm->index;
390 	u_int count = cm->count;
391 	int i, error;
392 	u_char rbuf[256], gbuf[256], bbuf[256];
393 	u_char *r, *g, *b;
394 
395 	if (cm->index >= 256 || cm->count > 256 ||
396 	    (cm->index + cm->count) > 256)
397 		return EINVAL;
398 	error = copyin(cm->red, &rbuf[index], count);
399 	if (error)
400 		return error;
401 	error = copyin(cm->green, &gbuf[index], count);
402 	if (error)
403 		return error;
404 	error = copyin(cm->blue, &bbuf[index], count);
405 	if (error)
406 		return error;
407 
408 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
409 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
410 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
411 
412 	r = &sc->sc_cmap_red[index];
413 	g = &sc->sc_cmap_green[index];
414 	b = &sc->sc_cmap_blue[index];
415 	for (i = 0; i < count; i++)
416 		xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
417 	return 0;
418 }
419