Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.13
      1 /*	$NetBSD: ofb.c,v 1.13 2000/06/19 19:35:20 tsubai Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/buf.h>
     32 #include <sys/conf.h>
     33 #include <sys/device.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/kernel.h>
     36 #include <sys/malloc.h>
     37 #include <sys/systm.h>
     38 
     39 #include <vm/vm.h>
     40 
     41 #include <dev/pci/pcidevs.h>
     42 #include <dev/pci/pcireg.h>
     43 #include <dev/pci/pcivar.h>
     44 
     45 #include <dev/wscons/wsconsio.h>
     46 #include <dev/wscons/wsdisplayvar.h>
     47 #include <dev/rasops/rasops.h>
     48 
     49 #include <dev/ofw/ofw_pci.h>
     50 
     51 #include <machine/bus.h>
     52 #include <machine/grfioctl.h>
     53 
     54 #include <macppc/dev/ofbvar.h>
     55 
     56 int	ofbmatch __P((struct device *, struct cfdata *, void *));
     57 void	ofbattach __P((struct device *, struct device *, void *));
     58 int	ofbprint __P((void *, const char *));
     59 
     60 struct cfattach ofb_ca = {
     61 	sizeof(struct ofb_softc), ofbmatch, ofbattach,
     62 };
     63 
     64 struct ofb_devconfig ofb_console_dc;
     65 
     66 struct wsscreen_descr ofb_stdscreen = {
     67 	"std",
     68 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
     69 	0,
     70 	0, 0,
     71 	WSSCREEN_REVERSE
     72 };
     73 
     74 const struct wsscreen_descr *_ofb_scrlist[] = {
     75 	&ofb_stdscreen,
     76 	/* XXX other formats, graphics screen? */
     77 };
     78 
     79 struct wsscreen_list ofb_screenlist = {
     80 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     81 };
     82 
     83 static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     84 static int ofb_mmap __P((void *, off_t, int));
     85 static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
     86 				void **, int *, int *, long *));
     87 static void ofb_free_screen __P((void *, void *));
     88 static int ofb_show_screen __P((void *, void *, int,
     89 				void (*) (void *, int, int), void *));
     90 
     91 struct wsdisplay_accessops ofb_accessops = {
     92 	ofb_ioctl,
     93 	ofb_mmap,
     94 	ofb_alloc_screen,
     95 	ofb_free_screen,
     96 	ofb_show_screen,
     97 	0 /* load_font */
     98 };
     99 
    100 static struct wsdisplay_font openfirm6x11;
    101 
    102 static void ofb_common_init __P((int, struct ofb_devconfig *));
    103 static int ofb_getcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    104 static int ofb_putcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    105 
    106 int
    107 ofbmatch(parent, match, aux)
    108 	struct device *parent;
    109 	struct cfdata *match;
    110 	void *aux;
    111 {
    112 	struct pci_attach_args *pa = aux;
    113 
    114 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    115 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    116 		return 1;
    117 
    118 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    119 		return 1;
    120 
    121 	return 0;
    122 }
    123 
    124 void
    125 ofbattach(parent, self, aux)
    126 	struct device *parent, *self;
    127 	void *aux;
    128 {
    129 	struct ofb_softc *sc = (struct ofb_softc *)self;
    130 	struct pci_attach_args *pa = aux;
    131 	struct wsemuldisplaydev_attach_args a;
    132 	int console;
    133 	struct ofb_devconfig *dc;
    134 	char devinfo[256];
    135 
    136 	console = ofb_is_console();
    137 
    138 	if (console) {
    139 		dc = &ofb_console_dc;
    140 		sc->nscreens = 1;
    141 	} else {
    142 		int node, i, screenbytes;
    143 
    144 		dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
    145 		bzero(dc, sizeof(struct ofb_devconfig));
    146 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    147 		if (node == 0) {
    148 			printf(": ofdev not found\n");
    149 			return;
    150 		}
    151 		ofb_common_init(node, dc);
    152 
    153 		screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
    154 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    155 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    156 	}
    157 	sc->sc_dc = dc;
    158 
    159 	OF_getprop(dc->dc_node, "assigned-addresses", sc->sc_addrs,
    160 	    sizeof(sc->sc_addrs));
    161 
    162 	if (dc->dc_paddr == 0) {
    163 		printf(": cannot map framebuffer\n");
    164 		return;
    165 	}
    166 
    167 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    168 	printf(": %s\n", devinfo);
    169 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    170 	       dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
    171 
    172 	sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
    173 	sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
    174 	sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
    175 	sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
    176 
    177 	a.console = console;
    178 	a.scrdata = &ofb_screenlist;
    179 	a.accessops = &ofb_accessops;
    180 	a.accesscookie = sc;
    181 
    182 	config_found(self, &a, wsemuldisplaydevprint);
    183 }
    184 
    185 void
    186 ofb_common_init(node, dc)
    187 	int node;
    188 	struct ofb_devconfig *dc;
    189 {
    190 	struct rasops_info *ri = &dc->dc_ri;
    191 	int32_t addr, width, height, linebytes, depth;
    192 
    193 	dc->dc_node = node;
    194 	if (dc->dc_ih == 0) {
    195 		char name[64];
    196 
    197 		bzero(name, 64);
    198 		OF_package_to_path(node, name, sizeof(name));
    199 		dc->dc_ih = OF_open(name);
    200 	}
    201 
    202 	/* XXX /chaos/control doesn't have "width", "height", ... */
    203 	width = height = -1;
    204 	if (OF_getprop(node, "width", &width, 4) != 4)
    205 		OF_interpret("screen-width", 1, &width);
    206 	if (OF_getprop(node, "height", &height, 4) != 4)
    207 		OF_interpret("screen-height", 1, &height);
    208 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    209 		linebytes = width;			/* XXX */
    210 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    211 		depth = 8;				/* XXX */
    212 	if (OF_getprop(node, "address", &addr, 4) != 4)
    213 		OF_interpret("frame-buffer-adr", 1, &addr);
    214 
    215 	if (width == -1 || height == -1 || addr == 0 || addr == -1)
    216 		return;
    217 
    218 	dc->dc_paddr = addr;		/* PA of the frame buffer */
    219 
    220 	/* Make sure 0/0/0 is black and 255/255/255 is white. */
    221 	OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
    222 	OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
    223 
    224 	/* initialize rasops */
    225 	ri->ri_width = width;
    226 	ri->ri_height = height;
    227 	ri->ri_depth = depth;
    228 	ri->ri_stride = linebytes;
    229 	ri->ri_bits = (char *)addr;
    230 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
    231 
    232 	/* If screen is smaller than 1024x768, use small font. */
    233 	if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
    234 		int cols, rows;
    235 
    236 		OF_interpret("#lines", 1, &rows);
    237 		OF_interpret("#columns", 1, &cols);
    238 
    239 		ri->ri_font = &openfirm6x11;
    240 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    241 		rasops_init(ri, rows, cols);
    242 
    243 		ri->ri_xorigin = 2;
    244 		ri->ri_yorigin = 3;
    245 		ri->ri_bits = (char *)addr + ri->ri_xorigin +
    246 			      ri->ri_stride * ri->ri_yorigin;
    247 	} else
    248 		rasops_init(ri, height/22, (width/12) & ~7);	/* XXX 12x22 */
    249 
    250 	/* black on white */
    251 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    252 	ri->ri_devcmap[1] = 0;				/* fg */
    253 
    254 	ofb_stdscreen.nrows = ri->ri_rows;
    255 	ofb_stdscreen.ncols = ri->ri_cols;
    256 	ofb_stdscreen.textops = &ri->ri_ops;
    257 	ofb_stdscreen.capabilities = ri->ri_caps;
    258 }
    259 
    260 int
    261 ofb_is_console()
    262 {
    263 	int chosen, stdout, node;
    264 	char type[16];
    265 
    266 	chosen = OF_finddevice("/chosen");
    267 	OF_getprop(chosen, "stdout", &stdout, 4);
    268 	node = OF_instance_to_package(stdout);
    269 	OF_getprop(node, "device_type", type, sizeof(type));
    270 	if (strcmp(type, "display") == 0)
    271 		return 1;
    272 	else
    273 		return 0;
    274 }
    275 
    276 int
    277 ofb_ioctl(v, cmd, data, flag, p)
    278 	void *v;
    279 	u_long cmd;
    280 	caddr_t data;
    281 	int flag;
    282 	struct proc *p;
    283 {
    284 	struct ofb_softc *sc = v;
    285 	struct ofb_devconfig *dc = sc->sc_dc;
    286 	struct wsdisplay_fbinfo *wdf;
    287 	struct grfinfo *gm;
    288 
    289 	switch (cmd) {
    290 	case WSDISPLAYIO_GTYPE:
    291 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    292 		return 0;
    293 
    294 	case WSDISPLAYIO_GINFO:
    295 		wdf = (void *)data;
    296 		wdf->height = dc->dc_ri.ri_height;
    297 		wdf->width = dc->dc_ri.ri_width;
    298 		wdf->depth = dc->dc_ri.ri_depth;
    299 		wdf->cmsize = 256;
    300 		return 0;
    301 
    302 	case WSDISPLAYIO_GETCMAP:
    303 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    304 
    305 	case WSDISPLAYIO_PUTCMAP:
    306 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    307 
    308 	/* XXX There are no way to know framebuffer pa from a user program. */
    309 	case GRFIOCGINFO:
    310 		gm = (void *)data;
    311 		bzero(gm, sizeof(struct grfinfo));
    312 		gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
    313 		gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
    314 		return 0;
    315 	}
    316 	return -1;
    317 }
    318 
    319 int
    320 ofb_mmap(v, offset, prot)
    321 	void *v;
    322 	off_t offset;
    323 	int prot;
    324 {
    325 	struct ofb_softc *sc = v;
    326 	struct ofb_devconfig *dc = sc->sc_dc;
    327 	struct rasops_info *ri = &dc->dc_ri;
    328 	u_int32_t *ap = sc->sc_addrs;
    329 	paddr_t pa;
    330 	int i;
    331 
    332 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    333 		return dc->dc_paddr + offset;
    334 
    335 	pa = offset;
    336 	for (i = 0; i < 6; i++) {
    337 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    338 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    339 			if (pa >= ap[2] && pa < ap[2] + ap[4])
    340 				return pa;
    341 		/* XXX I/O space? */
    342 		}
    343 		ap += 5;
    344 	}
    345 
    346 	return -1;
    347 }
    348 
    349 int
    350 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    351 	void *v;
    352 	const struct wsscreen_descr *type;
    353 	void **cookiep;
    354 	int *curxp, *curyp;
    355 	long *attrp;
    356 {
    357 	struct ofb_softc *sc = v;
    358 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    359 	long defattr;
    360 
    361 	if (sc->nscreens > 0)
    362 		return (ENOMEM);
    363 
    364 	*cookiep = ri;			/* one and only for now */
    365 	*curxp = 0;
    366 	*curyp = 0;
    367 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    368 	*attrp = defattr;
    369 	sc->nscreens++;
    370 	return 0;
    371 }
    372 
    373 void
    374 ofb_free_screen(v, cookie)
    375 	void *v;
    376 	void *cookie;
    377 {
    378 	struct ofb_softc *sc = v;
    379 
    380 	if (sc->sc_dc == &ofb_console_dc)
    381 		panic("ofb_free_screen: console");
    382 
    383 	sc->nscreens--;
    384 }
    385 
    386 int
    387 ofb_show_screen(v, cookie, waitok, cb, cbarg)
    388 	void *v;
    389 	void *cookie;
    390 	int waitok;
    391 	void (*cb) __P((void *, int, int));
    392 	void *cbarg;
    393 {
    394 
    395 	return (0);
    396 }
    397 
    398 int
    399 ofb_cnattach()
    400 {
    401 	struct ofb_devconfig *dc = &ofb_console_dc;
    402 	struct rasops_info *ri = &dc->dc_ri;
    403 	long defattr;
    404 	int crow = 0;
    405 	int chosen, stdout, node;
    406 
    407 	chosen = OF_finddevice("/chosen");
    408 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    409 	node = OF_instance_to_package(stdout);
    410 	dc->dc_ih = stdout;
    411 
    412 	/* get current cursor position */
    413 	OF_interpret("line#", 1, &crow);
    414 
    415 	/* move (rom monitor) cursor to the lowest line - 1 */
    416 	OF_interpret("#lines 2 - to line#", 0);
    417 
    418 	ofb_common_init(node, dc);
    419 
    420 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    421 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    422 
    423 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    424 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    425 		crow = 0;
    426 	}
    427 
    428 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    429 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    430 
    431 	return 0;
    432 }
    433 
    434 int
    435 copy_rom_font()
    436 {
    437 	u_char *romfont;
    438 	int char_width, char_height;
    439 	int chosen, mmu, m, e;
    440 
    441 	/* Get ROM FONT address. */
    442 	OF_interpret("font-adr", 1, &romfont);
    443 	if (romfont == NULL)
    444 		return -1;
    445 
    446 	chosen = OF_finddevice("/chosen");
    447 	OF_getprop(chosen, "mmu", &mmu, 4);
    448 
    449 	/*
    450 	 * Convert to physcal address.  We cannot access to Open Firmware's
    451 	 * virtual address space.
    452 	 */
    453 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    454 
    455 	/* Get character size */
    456 	OF_interpret("char-width", 1, &char_width);
    457 	OF_interpret("char-height", 1, &char_height);
    458 
    459 	openfirm6x11.name = "Open Firmware";
    460 	openfirm6x11.firstchar = 32;
    461 	openfirm6x11.numchars = 96;
    462 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    463 	openfirm6x11.fontwidth = char_width;
    464 	openfirm6x11.fontheight = char_height;
    465 	openfirm6x11.stride = 1;
    466 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    467 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    468 	openfirm6x11.data = romfont;
    469 
    470 	return 0;
    471 }
    472 
    473 int
    474 ofb_getcmap(sc, cm)
    475 	struct ofb_softc *sc;
    476 	struct wsdisplay_cmap *cm;
    477 {
    478 	u_int index = cm->index;
    479 	u_int count = cm->count;
    480 	int error;
    481 
    482 	if (index >= 256 || count > 256 || index + count > 256)
    483 		return EINVAL;
    484 
    485 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    486 	if (error)
    487 		return error;
    488 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    489 	if (error)
    490 		return error;
    491 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    492 	if (error)
    493 		return error;
    494 
    495 	return 0;
    496 }
    497 
    498 int
    499 ofb_putcmap(sc, cm)
    500 	struct ofb_softc *sc;
    501 	struct wsdisplay_cmap *cm;
    502 {
    503 	struct ofb_devconfig *dc = sc->sc_dc;
    504 	int index = cm->index;
    505 	int count = cm->count;
    506 	int i;
    507 	u_char *r, *g, *b;
    508 
    509 	if (cm->index >= 256 || cm->count > 256 ||
    510 	    (cm->index + cm->count) > 256)
    511 		return EINVAL;
    512 	if (!uvm_useracc(cm->red, cm->count, B_READ) ||
    513 	    !uvm_useracc(cm->green, cm->count, B_READ) ||
    514 	    !uvm_useracc(cm->blue, cm->count, B_READ))
    515 		return EFAULT;
    516 	copyin(cm->red,   &sc->sc_cmap_red[index],   count);
    517 	copyin(cm->green, &sc->sc_cmap_green[index], count);
    518 	copyin(cm->blue,  &sc->sc_cmap_blue[index],  count);
    519 
    520 	r = &sc->sc_cmap_red[index];
    521 	g = &sc->sc_cmap_green[index];
    522 	b = &sc->sc_cmap_blue[index];
    523 
    524 	for (i = 0; i < count; i++) {
    525 		OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
    526 		r++, g++, b++, index++;
    527 	}
    528 
    529 	return 0;
    530 }
    531