Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.37
      1 /*	$NetBSD: ofb.c,v 1.37 2003/10/20 00:12:10 matt 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/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.37 2003/10/20 00:12:10 matt Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/buf.h>
     35 #include <sys/conf.h>
     36 #include <sys/device.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/kernel.h>
     39 #include <sys/malloc.h>
     40 #include <sys/systm.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <dev/pci/pcidevs.h>
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pciio.h>
     48 
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wscons/wsdisplayvar.h>
     51 #include <dev/rasops/rasops.h>
     52 
     53 #include <dev/ofw/openfirm.h>
     54 #include <dev/ofw/ofw_pci.h>
     55 
     56 #include <machine/bus.h>
     57 #include <machine/autoconf.h>
     58 #include <machine/grfioctl.h>
     59 
     60 #include <powerpc/oea/bat.h>
     61 
     62 #include <macppc/dev/ofbvar.h>
     63 
     64 #if OFB_ENABLE_CACHE
     65 int ofb_enable_cache = 1;
     66 #else
     67 int ofb_enable_cache = 0;
     68 #endif
     69 
     70 int	ofbmatch __P((struct device *, struct cfdata *, void *));
     71 void	ofbattach __P((struct device *, struct device *, void *));
     72 int	ofbprint __P((void *, const char *));
     73 
     74 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     75     ofbmatch, ofbattach, NULL, NULL);
     76 
     77 struct ofb_devconfig ofb_console_dc;
     78 
     79 struct wsscreen_descr ofb_stdscreen = {
     80 	"std",
     81 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
     82 	0,
     83 	0, 0,
     84 	WSSCREEN_REVERSE
     85 };
     86 
     87 const struct wsscreen_descr *_ofb_scrlist[] = {
     88 	&ofb_stdscreen,
     89 	/* XXX other formats, graphics screen? */
     90 };
     91 
     92 struct wsscreen_list ofb_screenlist = {
     93 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     94 };
     95 
     96 static int ofb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     97 static paddr_t ofb_mmap __P((void *, off_t, int));
     98 static int ofb_alloc_screen __P((void *, const struct wsscreen_descr *,
     99 				void **, int *, int *, long *));
    100 static void ofb_free_screen __P((void *, void *));
    101 static int ofb_show_screen __P((void *, void *, int,
    102 				void (*) (void *, int, int), void *));
    103 static int copy_rom_font __P((void));
    104 
    105 struct wsdisplay_accessops ofb_accessops = {
    106 	ofb_ioctl,
    107 	ofb_mmap,
    108 	ofb_alloc_screen,
    109 	ofb_free_screen,
    110 	ofb_show_screen,
    111 	0 /* load_font */
    112 };
    113 
    114 static struct wsdisplay_font openfirm6x11;
    115 
    116 static void ofb_common_init __P((int, struct ofb_devconfig *));
    117 static int ofb_getcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    118 static int ofb_putcmap __P((struct ofb_softc *, struct wsdisplay_cmap *));
    119 
    120 int
    121 ofbmatch(parent, match, aux)
    122 	struct device *parent;
    123 	struct cfdata *match;
    124 	void *aux;
    125 {
    126 	struct pci_attach_args *pa = aux;
    127 
    128 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    129 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    130 		return 1;
    131 
    132 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    133 		return 1;
    134 
    135 	return 0;
    136 }
    137 
    138 void
    139 ofbattach(parent, self, aux)
    140 	struct device *parent, *self;
    141 	void *aux;
    142 {
    143 	struct ofb_softc *sc = (struct ofb_softc *)self;
    144 	struct pci_attach_args *pa = aux;
    145 	struct wsemuldisplaydev_attach_args a;
    146 	int console, node;
    147 	struct ofb_devconfig *dc;
    148 	char devinfo[256];
    149 
    150 	console = ofb_is_console();
    151 
    152 	if (console) {
    153 		dc = &ofb_console_dc;
    154 		node = dc->dc_node;
    155 		sc->nscreens = 1;
    156 	} else {
    157 		int i, len, screenbytes;
    158 
    159 		dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
    160 		memset(dc, 0, sizeof(struct ofb_devconfig));
    161 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    162 		if (node == 0) {
    163 			printf(": ofdev not found\n");
    164 			return;
    165 		}
    166 
    167 		/* XXX There are two child screens on PowerBook. */
    168 		memset(devinfo, 0, sizeof(devinfo));
    169 		OF_getprop(node, "device_type", devinfo, sizeof(devinfo));
    170 		len = strlen(devinfo);
    171 		if (strcmp(devinfo + len - 7, "-parent") == 0)
    172 			node = OF_child(node);
    173 
    174 		ofb_common_init(node, dc);
    175 
    176 		screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
    177 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    178 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    179 	}
    180 	sc->sc_dc = dc;
    181 
    182 	sc->sc_pc = pa->pa_pc;
    183 	sc->sc_pcitag = pa->pa_tag;
    184 
    185 	/* XXX */
    186 	if (OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    187 	    sizeof(sc->sc_addrs)) == -1) {
    188 		node = OF_parent(node);
    189 		OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    190 		    sizeof(sc->sc_addrs));
    191 	}
    192 
    193 	if (dc->dc_paddr == 0) {
    194 		printf(": cannot map framebuffer\n");
    195 		return;
    196 	}
    197 
    198 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    199 	printf(": %s\n", devinfo);
    200 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    201 	       dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
    202 
    203 	sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
    204 	sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
    205 	sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
    206 	sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
    207 
    208 	a.console = console;
    209 	a.scrdata = &ofb_screenlist;
    210 	a.accessops = &ofb_accessops;
    211 	a.accesscookie = sc;
    212 
    213 	config_found(self, &a, wsemuldisplaydevprint);
    214 }
    215 
    216 void
    217 ofb_common_init(node, dc)
    218 	int node;
    219 	struct ofb_devconfig *dc;
    220 {
    221 	struct rasops_info *ri = &dc->dc_ri;
    222 	int32_t addr, width, height, linebytes, depth;
    223 
    224 	dc->dc_node = node;
    225 	if (dc->dc_ih == 0) {
    226 		char name[64];
    227 
    228 		memset(name, 0, 64);
    229 		OF_package_to_path(node, name, sizeof(name));
    230 		dc->dc_ih = OF_open(name);
    231 	}
    232 
    233 	/* XXX /chaos/control doesn't have "width", "height", ... */
    234 	width = height = -1;
    235 	if (OF_getprop(node, "width", &width, 4) != 4)
    236 		OF_interpret("screen-width", 1, &width);
    237 	if (OF_getprop(node, "height", &height, 4) != 4)
    238 		OF_interpret("screen-height", 1, &height);
    239 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    240 		linebytes = width;			/* XXX */
    241 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    242 		depth = 8;				/* XXX */
    243 	if (OF_getprop(node, "address", &addr, 4) != 4)
    244 		OF_interpret("frame-buffer-adr", 1, &addr);
    245 
    246 	if (width == -1 || height == -1 || addr == 0 || addr == -1)
    247 		return;
    248 
    249 	dc->dc_paddr = addr;		/* PA of the frame buffer */
    250 
    251 	/* Make sure 0/0/0 is black and 255/255/255 is white. */
    252 	OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
    253 	OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
    254 
    255 	/* Enable write-through cache. */
    256 	if (ofb_enable_cache) {
    257 		vaddr_t va;
    258 		/*
    259 		 * Let's try to find an empty BAT to use
    260 		 */
    261 		for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
    262 		     va += SEGMENT_LENGTH) {
    263 			if (battable[va >> ADDR_SR_SHFT].batu == 0) {
    264 				battable[va >> ADDR_SR_SHFT].batl =
    265 				    BATL(addr & 0xf0000000,
    266 					 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
    267 				battable[va >> ADDR_SR_SHFT].batu =
    268 				    BATL(va, BAT_BL_256M, BAT_Vs);
    269 				addr &= 0x0fffffff;
    270 				addr |= va;
    271 				break;
    272 			}
    273 		}
    274 	}
    275 
    276 	/* initialize rasops */
    277 	ri->ri_width = width;
    278 	ri->ri_height = height;
    279 	ri->ri_depth = depth;
    280 	ri->ri_stride = linebytes;
    281 	ri->ri_bits = (char *)addr;
    282 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
    283 
    284 	/* If screen is smaller than 1024x768, use small font. */
    285 	if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
    286 		int cols, rows;
    287 
    288 		OF_interpret("#lines", 1, &rows);
    289 		OF_interpret("#columns", 1, &cols);
    290 
    291 		ri->ri_font = &openfirm6x11;
    292 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    293 		rasops_init(ri, rows, cols);
    294 
    295 		ri->ri_xorigin = 2;
    296 		ri->ri_yorigin = 3;
    297 		ri->ri_bits = (char *)addr + ri->ri_xorigin +
    298 			      ri->ri_stride * ri->ri_yorigin;
    299 	} else {
    300 		rasops_init(ri, 24, 80);
    301 		rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
    302 		    ((width - 2) / ri->ri_font->fontwidth) & ~7);
    303 	}
    304 
    305 	/* black on white */
    306 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    307 	ri->ri_devcmap[1] = 0;				/* fg */
    308 
    309 	ofb_stdscreen.nrows = ri->ri_rows;
    310 	ofb_stdscreen.ncols = ri->ri_cols;
    311 	ofb_stdscreen.textops = &ri->ri_ops;
    312 	ofb_stdscreen.capabilities = ri->ri_caps;
    313 }
    314 
    315 int
    316 ofb_is_console()
    317 {
    318 	int chosen, stdout, node;
    319 	char type[16];
    320 
    321 	chosen = OF_finddevice("/chosen");
    322 	OF_getprop(chosen, "stdout", &stdout, 4);
    323 	node = OF_instance_to_package(stdout);
    324 	OF_getprop(node, "device_type", type, sizeof(type));
    325 	if (strcmp(type, "display") == 0)
    326 		return 1;
    327 	else
    328 		return 0;
    329 }
    330 
    331 int
    332 ofb_ioctl(v, cmd, data, flag, p)
    333 	void *v;
    334 	u_long cmd;
    335 	caddr_t data;
    336 	int flag;
    337 	struct proc *p;
    338 {
    339 	struct ofb_softc *sc = v;
    340 	struct ofb_devconfig *dc = sc->sc_dc;
    341 	struct wsdisplay_fbinfo *wdf;
    342 	struct grfinfo *gm;
    343 
    344 	switch (cmd) {
    345 	case WSDISPLAYIO_GTYPE:
    346 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    347 		return 0;
    348 
    349 	case WSDISPLAYIO_GINFO:
    350 		wdf = (void *)data;
    351 		wdf->height = dc->dc_ri.ri_height;
    352 		wdf->width = dc->dc_ri.ri_width;
    353 		wdf->depth = dc->dc_ri.ri_depth;
    354 		wdf->cmsize = 256;
    355 		return 0;
    356 
    357 	case WSDISPLAYIO_GETCMAP:
    358 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    359 
    360 	case WSDISPLAYIO_PUTCMAP:
    361 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    362 
    363 	/* XXX There are no way to know framebuffer pa from a user program. */
    364 	case GRFIOCGINFO:
    365 		gm = (void *)data;
    366 		memset(gm, 0, sizeof(struct grfinfo));
    367 		gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
    368 		gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
    369 		return 0;
    370 	/* PCI config read/write passthrough. */
    371 	case PCI_IOC_CFGREAD:
    372 	case PCI_IOC_CFGWRITE:
    373 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    374 		    cmd, data, flag, p));
    375 	}
    376 	return EPASSTHROUGH;
    377 }
    378 
    379 paddr_t
    380 ofb_mmap(v, offset, prot)
    381 	void *v;
    382 	off_t offset;
    383 	int prot;
    384 {
    385 	struct ofb_softc *sc = v;
    386 	struct ofb_devconfig *dc = sc->sc_dc;
    387 	struct rasops_info *ri = &dc->dc_ri;
    388 	u_int32_t *ap = sc->sc_addrs;
    389 	paddr_t pa;
    390 	int i;
    391 
    392 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    393 		return dc->dc_paddr + offset;
    394 
    395 	pa = offset;
    396 	for (i = 0; i < 6; i++) {
    397 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    398 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    399 			if (pa >= ap[2] && pa < ap[2] + ap[4])
    400 				return pa;
    401 		/* XXX I/O space? */
    402 		}
    403 		ap += 5;
    404 	}
    405 
    406 	return -1;
    407 }
    408 
    409 int
    410 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    411 	void *v;
    412 	const struct wsscreen_descr *type;
    413 	void **cookiep;
    414 	int *curxp, *curyp;
    415 	long *attrp;
    416 {
    417 	struct ofb_softc *sc = v;
    418 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    419 	long defattr;
    420 
    421 	if (sc->nscreens > 0)
    422 		return (ENOMEM);
    423 
    424 	*cookiep = ri;			/* one and only for now */
    425 	*curxp = 0;
    426 	*curyp = 0;
    427 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    428 	*attrp = defattr;
    429 	sc->nscreens++;
    430 	return 0;
    431 }
    432 
    433 void
    434 ofb_free_screen(v, cookie)
    435 	void *v;
    436 	void *cookie;
    437 {
    438 	struct ofb_softc *sc = v;
    439 
    440 	if (sc->sc_dc == &ofb_console_dc)
    441 		panic("ofb_free_screen: console");
    442 
    443 	sc->nscreens--;
    444 }
    445 
    446 int
    447 ofb_show_screen(v, cookie, waitok, cb, cbarg)
    448 	void *v;
    449 	void *cookie;
    450 	int waitok;
    451 	void (*cb) __P((void *, int, int));
    452 	void *cbarg;
    453 {
    454 
    455 	return (0);
    456 }
    457 
    458 int
    459 ofb_cnattach()
    460 {
    461 	struct ofb_devconfig *dc = &ofb_console_dc;
    462 	struct rasops_info *ri = &dc->dc_ri;
    463 	long defattr;
    464 	int crow = 0;
    465 	int chosen, stdout, node;
    466 
    467 	chosen = OF_finddevice("/chosen");
    468 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    469 	node = OF_instance_to_package(stdout);
    470 	dc->dc_ih = stdout;
    471 
    472 	/* get current cursor position */
    473 	OF_interpret("line#", 1, &crow);
    474 
    475 	/* move (rom monitor) cursor to the lowest line - 1 */
    476 	OF_interpret("#lines 2 - to line#", 0);
    477 
    478 	ofb_common_init(node, dc);
    479 
    480 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    481 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    482 
    483 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    484 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    485 		crow = 0;
    486 	}
    487 
    488 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    489 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    490 
    491 	return 0;
    492 }
    493 
    494 int
    495 copy_rom_font()
    496 {
    497 	u_char *romfont;
    498 	int char_width, char_height;
    499 	int chosen, mmu, m, e;
    500 
    501 	/* Get ROM FONT address. */
    502 	OF_interpret("font-adr", 1, &romfont);
    503 	if (romfont == NULL)
    504 		return -1;
    505 
    506 	chosen = OF_finddevice("/chosen");
    507 	OF_getprop(chosen, "mmu", &mmu, 4);
    508 
    509 	/*
    510 	 * Convert to physcal address.  We cannot access to Open Firmware's
    511 	 * virtual address space.
    512 	 */
    513 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    514 
    515 	/* Get character size */
    516 	OF_interpret("char-width", 1, &char_width);
    517 	OF_interpret("char-height", 1, &char_height);
    518 
    519 	openfirm6x11.name = "Open Firmware";
    520 	openfirm6x11.firstchar = 32;
    521 	openfirm6x11.numchars = 96;
    522 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    523 	openfirm6x11.fontwidth = char_width;
    524 	openfirm6x11.fontheight = char_height;
    525 	openfirm6x11.stride = 1;
    526 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    527 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    528 	openfirm6x11.data = romfont;
    529 
    530 	return 0;
    531 }
    532 
    533 int
    534 ofb_getcmap(sc, cm)
    535 	struct ofb_softc *sc;
    536 	struct wsdisplay_cmap *cm;
    537 {
    538 	u_int index = cm->index;
    539 	u_int count = cm->count;
    540 	int error;
    541 
    542 	if (index >= 256 || count > 256 || index + count > 256)
    543 		return EINVAL;
    544 
    545 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    546 	if (error)
    547 		return error;
    548 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    549 	if (error)
    550 		return error;
    551 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    552 	if (error)
    553 		return error;
    554 
    555 	return 0;
    556 }
    557 
    558 int
    559 ofb_putcmap(sc, cm)
    560 	struct ofb_softc *sc;
    561 	struct wsdisplay_cmap *cm;
    562 {
    563 	struct ofb_devconfig *dc = sc->sc_dc;
    564 	u_int index = cm->index;
    565 	u_int count = cm->count;
    566 	int i;
    567 	u_char *r, *g, *b;
    568 
    569 	if (cm->index >= 256 || cm->count > 256 ||
    570 	    (cm->index + cm->count) > 256)
    571 		return EINVAL;
    572 	if (!uvm_useracc(cm->red, cm->count, B_READ) ||
    573 	    !uvm_useracc(cm->green, cm->count, B_READ) ||
    574 	    !uvm_useracc(cm->blue, cm->count, B_READ))
    575 		return EFAULT;
    576 	copyin(cm->red,   &sc->sc_cmap_red[index],   count);
    577 	copyin(cm->green, &sc->sc_cmap_green[index], count);
    578 	copyin(cm->blue,  &sc->sc_cmap_blue[index],  count);
    579 
    580 	r = &sc->sc_cmap_red[index];
    581 	g = &sc->sc_cmap_green[index];
    582 	b = &sc->sc_cmap_blue[index];
    583 
    584 	for (i = 0; i < count; i++) {
    585 		OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
    586 		r++, g++, b++, index++;
    587 	}
    588 
    589 	return 0;
    590 }
    591