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