Home | History | Annotate | Line # | Download | only in apbus
xafb.c revision 1.17.10.1
      1 /*	$NetBSD: xafb.c,v 1.17.10.1 2016/07/26 03:24:17 pgoyette 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.17.10.1 2016/07/26 03:24:17 pgoyette 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/malloc.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
    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
    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 = malloc(sizeof(struct xafb_devconfig), M_DEVBUF,
    161 		    M_WAITOK|M_ZERO);
    162 		dc->dc_fbpaddr = (paddr_t)0x10000000;
    163 		dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    164 		dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
    165 		if (xafb_common_init(dc) != 0) {
    166 			aprint_error(": couldn't initialize device\n");
    167 			return;
    168 		}
    169 
    170 		ri = &dc->dc_ri;
    171 
    172 		/* clear screen */
    173 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
    174 	}
    175 	sc->sc_dc = dc;
    176 
    177 	for (i = 0; i < 256; i++) {
    178 		sc->sc_cmap_red[i] = i;
    179 		sc->sc_cmap_green[i] = i;
    180 		sc->sc_cmap_blue[i] = i;
    181 	}
    182 
    183 	aprint_normal(": %d x %d, %dbpp\n",
    184 	    ri->ri_width, ri->ri_height, ri->ri_depth);
    185 
    186 	wsa.console = console;
    187 	wsa.scrdata = &xafb_screenlist;
    188 	wsa.accessops = &xafb_accessops;
    189 	wsa.accesscookie = sc;
    190 
    191 	config_found(self, &wsa, wsemuldisplaydevprint);
    192 }
    193 
    194 void
    195 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
    196 {
    197 	volatile struct xafb_reg *reg = dc->dc_reg;
    198 
    199 	reg->index = index;
    200 	reg->zero = 0;
    201 	reg->rgb = r;
    202 	reg->rgb = g;
    203 	reg->rgb = b;
    204 }
    205 
    206 int
    207 xafb_common_init(struct xafb_devconfig *dc)
    208 {
    209 	struct rasops_info *ri = &dc->dc_ri;
    210 	int i;
    211 
    212 	for (i = 0; i < 256; i++)
    213 		xafb_setcolor(dc, i, i, i, i);
    214 
    215 	/* initialize rasops */
    216 	ri->ri_width = 1280;
    217 	ri->ri_height = 1024;
    218 	ri->ri_depth = 8;
    219 	ri->ri_stride = 2048;
    220 	ri->ri_bits = (void *)dc->dc_fbbase;
    221 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
    222 	if (dc == &xafb_console_dc)
    223 		ri->ri_flg |= RI_NO_AUTO;
    224 
    225 	rasops_init(ri, 44, 100);
    226 
    227 	/* mono */
    228 	ri->ri_devcmap[0] = 0;				/* bg */
    229 	ri->ri_devcmap[1] = 0xffffffff;			/* fg */
    230 
    231 	xafb_stdscreen.nrows = ri->ri_rows;
    232 	xafb_stdscreen.ncols = ri->ri_cols;
    233 	xafb_stdscreen.textops = &ri->ri_ops;
    234 	xafb_stdscreen.capabilities = ri->ri_caps;
    235 
    236 	return 0;
    237 }
    238 
    239 int
    240 xafb_is_console(void)
    241 {
    242 	volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
    243 
    244 	if (*dipsw & 1)					/* XXX right? */
    245 		return 1;
    246 
    247 	return 0;
    248 }
    249 
    250 int
    251 xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    252 {
    253 	struct xafb_softc *sc = v;
    254 	struct xafb_devconfig *dc = sc->sc_dc;
    255 	struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
    256 	struct wsdisplay_fbinfo *wdf = (void *)data;
    257 
    258 	switch (cmd) {
    259 	case WSDISPLAYIO_GTYPE:
    260 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    261 		return 0;
    262 
    263 	case NEWSMIPS_WSDISPLAYIO_GINFO:
    264 		nwdf->stride = dc->dc_ri.ri_stride;
    265 		/* FALLTHROUGH */
    266 	case WSDISPLAYIO_GINFO:
    267 		wdf->height = dc->dc_ri.ri_height;
    268 		wdf->width = dc->dc_ri.ri_width;
    269 		wdf->depth = dc->dc_ri.ri_depth;
    270 		wdf->cmsize = 256;
    271 		return 0;
    272 
    273 	case WSDISPLAYIO_LINEBYTES:
    274 		*(u_int *)data = dc->dc_ri.ri_stride;
    275 		return 0;
    276 
    277 	case WSDISPLAYIO_GETCMAP:
    278 		return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
    279 
    280 	case WSDISPLAYIO_PUTCMAP:
    281 		return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
    282 
    283 	/* case WSDISPLAYIO_SVIDEO: */
    284 	}
    285 	return EPASSTHROUGH;
    286 }
    287 
    288 paddr_t
    289 xafb_mmap(void *v, void *vs, off_t offset, int prot)
    290 {
    291 	struct xafb_softc *sc = v;
    292 	struct xafb_devconfig *dc = sc->sc_dc;
    293 	struct rasops_info *ri = &dc->dc_ri;
    294 
    295 	if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
    296 		return -1;
    297 
    298 	return mips_btop(dc->dc_fbpaddr + offset);
    299 }
    300 
    301 int
    302 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
    303     void **cookiep, int *ccolp, int *crowp, long *attrp)
    304 {
    305 	struct xafb_softc *sc = v;
    306 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    307 	long defattr;
    308 
    309 	if (sc->sc_nscreens > 0)
    310 		return ENOMEM;
    311 
    312 	*cookiep = ri;
    313 	*ccolp = *crowp = 0;
    314 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    315 	*attrp = defattr;
    316 	sc->sc_nscreens++;
    317 
    318 	return 0;
    319 }
    320 
    321 void
    322 xafb_free_screen(void *v, void *cookie)
    323 {
    324 	struct xafb_softc *sc = v;
    325 
    326 	if (sc->sc_dc == &xafb_console_dc)
    327 		panic("xafb_free_screen: console");
    328 
    329 	sc->sc_nscreens--;
    330 }
    331 
    332 int
    333 xafb_show_screen(void *v, void *cookie, int waitok,
    334     void (*cb)(void *, int, int), void *cbarg)
    335 {
    336 
    337 	return 0;
    338 }
    339 
    340 int
    341 xafb_cnattach(void)
    342 {
    343 	struct xafb_devconfig *dc = &xafb_console_dc;
    344 	struct rasops_info *ri = &dc->dc_ri;
    345 	long defattr;
    346 	int crow = 0;
    347 
    348 	if (!xafb_is_console())
    349 		return -1;
    350 
    351 	dc->dc_fbpaddr = (paddr_t)0x10000000;
    352 	dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    353 	dc->dc_reg = (void *)0xb4903000;			/* XXX */
    354 	xafb_common_init(dc);
    355 
    356 	crow = 0;			/* XXX current cursor pos */
    357 
    358 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    359 	wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
    360 
    361 	return 0;
    362 }
    363 
    364 int
    365 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    366 {
    367 	u_int index = cm->index;
    368 	u_int count = cm->count;
    369 	int error;
    370 
    371 	if (index >= 256 || count > 256 || index + count > 256)
    372 		return EINVAL;
    373 
    374 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    375 	if (error)
    376 		return error;
    377 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    378 	if (error)
    379 		return error;
    380 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    381 	if (error)
    382 		return error;
    383 
    384 	return 0;
    385 }
    386 
    387 int
    388 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    389 {
    390 	u_int index = cm->index;
    391 	u_int count = cm->count;
    392 	int i, error;
    393 	u_char rbuf[256], gbuf[256], bbuf[256];
    394 	u_char *r, *g, *b;
    395 
    396 	if (cm->index >= 256 || cm->count > 256 ||
    397 	    (cm->index + cm->count) > 256)
    398 		return EINVAL;
    399 	error = copyin(cm->red, &rbuf[index], count);
    400 	if (error)
    401 		return error;
    402 	error = copyin(cm->green, &gbuf[index], count);
    403 	if (error)
    404 		return error;
    405 	error = copyin(cm->blue, &bbuf[index], count);
    406 	if (error)
    407 		return error;
    408 
    409 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    410 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    411 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    412 
    413 	r = &sc->sc_cmap_red[index];
    414 	g = &sc->sc_cmap_green[index];
    415 	b = &sc->sc_cmap_blue[index];
    416 	for (i = 0; i < count; i++)
    417 		xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
    418 	return 0;
    419 }
    420