Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.27
      1 /*	$NetBSD: ofb.c,v 1.27 2002/06/24 21:08:37 nathanw 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 && battable[0xc].batu == 0) {
    254 		battable[0xc].batl = BATL(addr & 0xf0000000, BAT_W, BAT_PP_RW);
    255 		battable[0xc].batu = BATL(0xc0000000, BAT_BL_256M, BAT_Vs);
    256 		addr &= 0x0fffffff;
    257 		addr |= 0xc0000000;
    258 	}
    259 
    260 	/* initialize rasops */
    261 	ri->ri_width = width;
    262 	ri->ri_height = height;
    263 	ri->ri_depth = depth;
    264 	ri->ri_stride = linebytes;
    265 	ri->ri_bits = (char *)addr;
    266 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
    267 
    268 	/* If screen is smaller than 1024x768, use small font. */
    269 	if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
    270 		int cols, rows;
    271 
    272 		OF_interpret("#lines", 1, &rows);
    273 		OF_interpret("#columns", 1, &cols);
    274 
    275 		ri->ri_font = &openfirm6x11;
    276 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    277 		rasops_init(ri, rows, cols);
    278 
    279 		ri->ri_xorigin = 2;
    280 		ri->ri_yorigin = 3;
    281 		ri->ri_bits = (char *)addr + ri->ri_xorigin +
    282 			      ri->ri_stride * ri->ri_yorigin;
    283 	} else {
    284 		rasops_init(ri, 24, 80);
    285 		rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
    286 		    ((width - 2) / ri->ri_font->fontwidth) & ~7);
    287 	}
    288 
    289 	/* black on white */
    290 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    291 	ri->ri_devcmap[1] = 0;				/* fg */
    292 
    293 	ofb_stdscreen.nrows = ri->ri_rows;
    294 	ofb_stdscreen.ncols = ri->ri_cols;
    295 	ofb_stdscreen.textops = &ri->ri_ops;
    296 	ofb_stdscreen.capabilities = ri->ri_caps;
    297 }
    298 
    299 int
    300 ofb_is_console()
    301 {
    302 	int chosen, stdout, node;
    303 	char type[16];
    304 
    305 	chosen = OF_finddevice("/chosen");
    306 	OF_getprop(chosen, "stdout", &stdout, 4);
    307 	node = OF_instance_to_package(stdout);
    308 	OF_getprop(node, "device_type", type, sizeof(type));
    309 	if (strcmp(type, "display") == 0)
    310 		return 1;
    311 	else
    312 		return 0;
    313 }
    314 
    315 int
    316 ofb_ioctl(v, cmd, data, flag, p)
    317 	void *v;
    318 	u_long cmd;
    319 	caddr_t data;
    320 	int flag;
    321 	struct proc *p;
    322 {
    323 	struct ofb_softc *sc = v;
    324 	struct ofb_devconfig *dc = sc->sc_dc;
    325 	struct wsdisplay_fbinfo *wdf;
    326 	struct grfinfo *gm;
    327 
    328 	switch (cmd) {
    329 	case WSDISPLAYIO_GTYPE:
    330 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    331 		return 0;
    332 
    333 	case WSDISPLAYIO_GINFO:
    334 		wdf = (void *)data;
    335 		wdf->height = dc->dc_ri.ri_height;
    336 		wdf->width = dc->dc_ri.ri_width;
    337 		wdf->depth = dc->dc_ri.ri_depth;
    338 		wdf->cmsize = 256;
    339 		return 0;
    340 
    341 	case WSDISPLAYIO_GETCMAP:
    342 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    343 
    344 	case WSDISPLAYIO_PUTCMAP:
    345 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    346 
    347 	/* XXX There are no way to know framebuffer pa from a user program. */
    348 	case GRFIOCGINFO:
    349 		gm = (void *)data;
    350 		memset(gm, 0, sizeof(struct grfinfo));
    351 		gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
    352 		gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
    353 		return 0;
    354 	/* PCI config read/write passthrough. */
    355 	case PCI_IOC_CFGREAD:
    356 	case PCI_IOC_CFGWRITE:
    357 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    358 		    cmd, data, flag, p));
    359 	}
    360 	return EPASSTHROUGH;
    361 }
    362 
    363 paddr_t
    364 ofb_mmap(v, offset, prot)
    365 	void *v;
    366 	off_t offset;
    367 	int prot;
    368 {
    369 	struct ofb_softc *sc = v;
    370 	struct ofb_devconfig *dc = sc->sc_dc;
    371 	struct rasops_info *ri = &dc->dc_ri;
    372 	u_int32_t *ap = sc->sc_addrs;
    373 	paddr_t pa;
    374 	int i;
    375 
    376 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    377 		return dc->dc_paddr + offset;
    378 
    379 	pa = offset;
    380 	for (i = 0; i < 6; i++) {
    381 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    382 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    383 			if (pa >= ap[2] && pa < ap[2] + ap[4])
    384 				return pa;
    385 		/* XXX I/O space? */
    386 		}
    387 		ap += 5;
    388 	}
    389 
    390 	return -1;
    391 }
    392 
    393 int
    394 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    395 	void *v;
    396 	const struct wsscreen_descr *type;
    397 	void **cookiep;
    398 	int *curxp, *curyp;
    399 	long *attrp;
    400 {
    401 	struct ofb_softc *sc = v;
    402 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    403 	long defattr;
    404 
    405 	if (sc->nscreens > 0)
    406 		return (ENOMEM);
    407 
    408 	*cookiep = ri;			/* one and only for now */
    409 	*curxp = 0;
    410 	*curyp = 0;
    411 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    412 	*attrp = defattr;
    413 	sc->nscreens++;
    414 	return 0;
    415 }
    416 
    417 void
    418 ofb_free_screen(v, cookie)
    419 	void *v;
    420 	void *cookie;
    421 {
    422 	struct ofb_softc *sc = v;
    423 
    424 	if (sc->sc_dc == &ofb_console_dc)
    425 		panic("ofb_free_screen: console");
    426 
    427 	sc->nscreens--;
    428 }
    429 
    430 int
    431 ofb_show_screen(v, cookie, waitok, cb, cbarg)
    432 	void *v;
    433 	void *cookie;
    434 	int waitok;
    435 	void (*cb) __P((void *, int, int));
    436 	void *cbarg;
    437 {
    438 
    439 	return (0);
    440 }
    441 
    442 int
    443 ofb_cnattach()
    444 {
    445 	struct ofb_devconfig *dc = &ofb_console_dc;
    446 	struct rasops_info *ri = &dc->dc_ri;
    447 	long defattr;
    448 	int crow = 0;
    449 	int chosen, stdout, node;
    450 
    451 	chosen = OF_finddevice("/chosen");
    452 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    453 	node = OF_instance_to_package(stdout);
    454 	dc->dc_ih = stdout;
    455 
    456 	/* get current cursor position */
    457 	OF_interpret("line#", 1, &crow);
    458 
    459 	/* move (rom monitor) cursor to the lowest line - 1 */
    460 	OF_interpret("#lines 2 - to line#", 0);
    461 
    462 	ofb_common_init(node, dc);
    463 
    464 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    465 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    466 
    467 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    468 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    469 		crow = 0;
    470 	}
    471 
    472 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    473 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    474 
    475 	return 0;
    476 }
    477 
    478 int
    479 copy_rom_font()
    480 {
    481 	u_char *romfont;
    482 	int char_width, char_height;
    483 	int chosen, mmu, m, e;
    484 
    485 	/* Get ROM FONT address. */
    486 	OF_interpret("font-adr", 1, &romfont);
    487 	if (romfont == NULL)
    488 		return -1;
    489 
    490 	chosen = OF_finddevice("/chosen");
    491 	OF_getprop(chosen, "mmu", &mmu, 4);
    492 
    493 	/*
    494 	 * Convert to physcal address.  We cannot access to Open Firmware's
    495 	 * virtual address space.
    496 	 */
    497 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    498 
    499 	/* Get character size */
    500 	OF_interpret("char-width", 1, &char_width);
    501 	OF_interpret("char-height", 1, &char_height);
    502 
    503 	openfirm6x11.name = "Open Firmware";
    504 	openfirm6x11.firstchar = 32;
    505 	openfirm6x11.numchars = 96;
    506 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    507 	openfirm6x11.fontwidth = char_width;
    508 	openfirm6x11.fontheight = char_height;
    509 	openfirm6x11.stride = 1;
    510 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    511 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    512 	openfirm6x11.data = romfont;
    513 
    514 	return 0;
    515 }
    516 
    517 int
    518 ofb_getcmap(sc, cm)
    519 	struct ofb_softc *sc;
    520 	struct wsdisplay_cmap *cm;
    521 {
    522 	u_int index = cm->index;
    523 	u_int count = cm->count;
    524 	int error;
    525 
    526 	if (index >= 256 || count > 256 || index + count > 256)
    527 		return EINVAL;
    528 
    529 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    530 	if (error)
    531 		return error;
    532 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    533 	if (error)
    534 		return error;
    535 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    536 	if (error)
    537 		return error;
    538 
    539 	return 0;
    540 }
    541 
    542 int
    543 ofb_putcmap(sc, cm)
    544 	struct ofb_softc *sc;
    545 	struct wsdisplay_cmap *cm;
    546 {
    547 	struct ofb_devconfig *dc = sc->sc_dc;
    548 	u_int index = cm->index;
    549 	u_int count = cm->count;
    550 	int i;
    551 	u_char *r, *g, *b;
    552 
    553 	if (cm->index >= 256 || cm->count > 256 ||
    554 	    (cm->index + cm->count) > 256)
    555 		return EINVAL;
    556 	if (!uvm_useracc(cm->red, cm->count, B_READ) ||
    557 	    !uvm_useracc(cm->green, cm->count, B_READ) ||
    558 	    !uvm_useracc(cm->blue, cm->count, B_READ))
    559 		return EFAULT;
    560 	copyin(cm->red,   &sc->sc_cmap_red[index],   count);
    561 	copyin(cm->green, &sc->sc_cmap_green[index], count);
    562 	copyin(cm->blue,  &sc->sc_cmap_blue[index],  count);
    563 
    564 	r = &sc->sc_cmap_red[index];
    565 	g = &sc->sc_cmap_green[index];
    566 	b = &sc->sc_cmap_blue[index];
    567 
    568 	for (i = 0; i < count; i++) {
    569 		OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
    570 		r++, g++, b++, index++;
    571 	}
    572 
    573 	return 0;
    574 }
    575