Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.16
      1 /*	$NetBSD: ofb.c,v 1.16 2000/10/01 19:08:30 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 <uvm/uvm_extern.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 paddr_t 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, node;
    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 		node = dc->dc_node;
    141 		sc->nscreens = 1;
    142 	} else {
    143 		int i, len, screenbytes;
    144 
    145 		dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
    146 		bzero(dc, sizeof(struct ofb_devconfig));
    147 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    148 		if (node == 0) {
    149 			printf(": ofdev not found\n");
    150 			return;
    151 		}
    152 
    153 		/* XXX There are two child screens on PowerBook. */
    154 		bzero(devinfo, sizeof(devinfo));
    155 		OF_getprop(node, "device_type", devinfo, sizeof(devinfo));
    156 		len = strlen(devinfo);
    157 		if (strcmp(devinfo + len - 7, "-parent") == 0)
    158 			node = OF_child(node);
    159 
    160 		ofb_common_init(node, dc);
    161 
    162 		screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
    163 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    164 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    165 	}
    166 	sc->sc_dc = dc;
    167 
    168 	/* XXX */
    169 	if (OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    170 	    sizeof(sc->sc_addrs)) == -1) {
    171 		node = OF_parent(node);
    172 		OF_getprop(node, "assigned-addresses", sc->sc_addrs,
    173 		    sizeof(sc->sc_addrs));
    174 	}
    175 
    176 	if (dc->dc_paddr == 0) {
    177 		printf(": cannot map framebuffer\n");
    178 		return;
    179 	}
    180 
    181 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    182 	printf(": %s\n", devinfo);
    183 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    184 	       dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
    185 
    186 	sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
    187 	sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
    188 	sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
    189 	sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
    190 
    191 	a.console = console;
    192 	a.scrdata = &ofb_screenlist;
    193 	a.accessops = &ofb_accessops;
    194 	a.accesscookie = sc;
    195 
    196 	config_found(self, &a, wsemuldisplaydevprint);
    197 }
    198 
    199 void
    200 ofb_common_init(node, dc)
    201 	int node;
    202 	struct ofb_devconfig *dc;
    203 {
    204 	struct rasops_info *ri = &dc->dc_ri;
    205 	int32_t addr, width, height, linebytes, depth;
    206 
    207 	dc->dc_node = node;
    208 	if (dc->dc_ih == 0) {
    209 		char name[64];
    210 
    211 		bzero(name, 64);
    212 		OF_package_to_path(node, name, sizeof(name));
    213 		dc->dc_ih = OF_open(name);
    214 	}
    215 
    216 	/* XXX /chaos/control doesn't have "width", "height", ... */
    217 	width = height = -1;
    218 	if (OF_getprop(node, "width", &width, 4) != 4)
    219 		OF_interpret("screen-width", 1, &width);
    220 	if (OF_getprop(node, "height", &height, 4) != 4)
    221 		OF_interpret("screen-height", 1, &height);
    222 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    223 		linebytes = width;			/* XXX */
    224 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    225 		depth = 8;				/* XXX */
    226 	if (OF_getprop(node, "address", &addr, 4) != 4)
    227 		OF_interpret("frame-buffer-adr", 1, &addr);
    228 
    229 	if (width == -1 || height == -1 || addr == 0 || addr == -1)
    230 		return;
    231 
    232 	dc->dc_paddr = addr;		/* PA of the frame buffer */
    233 
    234 	/* Make sure 0/0/0 is black and 255/255/255 is white. */
    235 	OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
    236 	OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
    237 
    238 	/* initialize rasops */
    239 	ri->ri_width = width;
    240 	ri->ri_height = height;
    241 	ri->ri_depth = depth;
    242 	ri->ri_stride = linebytes;
    243 	ri->ri_bits = (char *)addr;
    244 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
    245 
    246 	/* If screen is smaller than 1024x768, use small font. */
    247 	if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
    248 		int cols, rows;
    249 
    250 		OF_interpret("#lines", 1, &rows);
    251 		OF_interpret("#columns", 1, &cols);
    252 
    253 		ri->ri_font = &openfirm6x11;
    254 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    255 		rasops_init(ri, rows, cols);
    256 
    257 		ri->ri_xorigin = 2;
    258 		ri->ri_yorigin = 3;
    259 		ri->ri_bits = (char *)addr + ri->ri_xorigin +
    260 			      ri->ri_stride * ri->ri_yorigin;
    261 	} else
    262 		rasops_init(ri, height/22, (width/12) & ~7);	/* XXX 12x22 */
    263 
    264 	/* black on white */
    265 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    266 	ri->ri_devcmap[1] = 0;				/* fg */
    267 
    268 	ofb_stdscreen.nrows = ri->ri_rows;
    269 	ofb_stdscreen.ncols = ri->ri_cols;
    270 	ofb_stdscreen.textops = &ri->ri_ops;
    271 	ofb_stdscreen.capabilities = ri->ri_caps;
    272 }
    273 
    274 int
    275 ofb_is_console()
    276 {
    277 	int chosen, stdout, node;
    278 	char type[16];
    279 
    280 	chosen = OF_finddevice("/chosen");
    281 	OF_getprop(chosen, "stdout", &stdout, 4);
    282 	node = OF_instance_to_package(stdout);
    283 	OF_getprop(node, "device_type", type, sizeof(type));
    284 	if (strcmp(type, "display") == 0)
    285 		return 1;
    286 	else
    287 		return 0;
    288 }
    289 
    290 int
    291 ofb_ioctl(v, cmd, data, flag, p)
    292 	void *v;
    293 	u_long cmd;
    294 	caddr_t data;
    295 	int flag;
    296 	struct proc *p;
    297 {
    298 	struct ofb_softc *sc = v;
    299 	struct ofb_devconfig *dc = sc->sc_dc;
    300 	struct wsdisplay_fbinfo *wdf;
    301 	struct grfinfo *gm;
    302 
    303 	switch (cmd) {
    304 	case WSDISPLAYIO_GTYPE:
    305 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    306 		return 0;
    307 
    308 	case WSDISPLAYIO_GINFO:
    309 		wdf = (void *)data;
    310 		wdf->height = dc->dc_ri.ri_height;
    311 		wdf->width = dc->dc_ri.ri_width;
    312 		wdf->depth = dc->dc_ri.ri_depth;
    313 		wdf->cmsize = 256;
    314 		return 0;
    315 
    316 	case WSDISPLAYIO_GETCMAP:
    317 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    318 
    319 	case WSDISPLAYIO_PUTCMAP:
    320 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    321 
    322 	/* XXX There are no way to know framebuffer pa from a user program. */
    323 	case GRFIOCGINFO:
    324 		gm = (void *)data;
    325 		bzero(gm, sizeof(struct grfinfo));
    326 		gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
    327 		gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
    328 		return 0;
    329 	}
    330 	return -1;
    331 }
    332 
    333 paddr_t
    334 ofb_mmap(v, offset, prot)
    335 	void *v;
    336 	off_t offset;
    337 	int prot;
    338 {
    339 	struct ofb_softc *sc = v;
    340 	struct ofb_devconfig *dc = sc->sc_dc;
    341 	struct rasops_info *ri = &dc->dc_ri;
    342 	u_int32_t *ap = sc->sc_addrs;
    343 	paddr_t pa;
    344 	int i;
    345 
    346 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    347 		return dc->dc_paddr + offset;
    348 
    349 	pa = offset;
    350 	for (i = 0; i < 6; i++) {
    351 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    352 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    353 			if (pa >= ap[2] && pa < ap[2] + ap[4])
    354 				return pa;
    355 		/* XXX I/O space? */
    356 		}
    357 		ap += 5;
    358 	}
    359 
    360 	return -1;
    361 }
    362 
    363 int
    364 ofb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    365 	void *v;
    366 	const struct wsscreen_descr *type;
    367 	void **cookiep;
    368 	int *curxp, *curyp;
    369 	long *attrp;
    370 {
    371 	struct ofb_softc *sc = v;
    372 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    373 	long defattr;
    374 
    375 	if (sc->nscreens > 0)
    376 		return (ENOMEM);
    377 
    378 	*cookiep = ri;			/* one and only for now */
    379 	*curxp = 0;
    380 	*curyp = 0;
    381 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    382 	*attrp = defattr;
    383 	sc->nscreens++;
    384 	return 0;
    385 }
    386 
    387 void
    388 ofb_free_screen(v, cookie)
    389 	void *v;
    390 	void *cookie;
    391 {
    392 	struct ofb_softc *sc = v;
    393 
    394 	if (sc->sc_dc == &ofb_console_dc)
    395 		panic("ofb_free_screen: console");
    396 
    397 	sc->nscreens--;
    398 }
    399 
    400 int
    401 ofb_show_screen(v, cookie, waitok, cb, cbarg)
    402 	void *v;
    403 	void *cookie;
    404 	int waitok;
    405 	void (*cb) __P((void *, int, int));
    406 	void *cbarg;
    407 {
    408 
    409 	return (0);
    410 }
    411 
    412 int
    413 ofb_cnattach()
    414 {
    415 	struct ofb_devconfig *dc = &ofb_console_dc;
    416 	struct rasops_info *ri = &dc->dc_ri;
    417 	long defattr;
    418 	int crow = 0;
    419 	int chosen, stdout, node;
    420 
    421 	chosen = OF_finddevice("/chosen");
    422 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    423 	node = OF_instance_to_package(stdout);
    424 	dc->dc_ih = stdout;
    425 
    426 	/* get current cursor position */
    427 	OF_interpret("line#", 1, &crow);
    428 
    429 	/* move (rom monitor) cursor to the lowest line - 1 */
    430 	OF_interpret("#lines 2 - to line#", 0);
    431 
    432 	ofb_common_init(node, dc);
    433 
    434 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    435 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    436 
    437 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    438 			*(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
    439 		crow = 0;
    440 	}
    441 
    442 	(*ri->ri_ops.alloc_attr)(ri, 0, 0, 0, &defattr);
    443 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    444 
    445 	return 0;
    446 }
    447 
    448 int
    449 copy_rom_font()
    450 {
    451 	u_char *romfont;
    452 	int char_width, char_height;
    453 	int chosen, mmu, m, e;
    454 
    455 	/* Get ROM FONT address. */
    456 	OF_interpret("font-adr", 1, &romfont);
    457 	if (romfont == NULL)
    458 		return -1;
    459 
    460 	chosen = OF_finddevice("/chosen");
    461 	OF_getprop(chosen, "mmu", &mmu, 4);
    462 
    463 	/*
    464 	 * Convert to physcal address.  We cannot access to Open Firmware's
    465 	 * virtual address space.
    466 	 */
    467 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    468 
    469 	/* Get character size */
    470 	OF_interpret("char-width", 1, &char_width);
    471 	OF_interpret("char-height", 1, &char_height);
    472 
    473 	openfirm6x11.name = "Open Firmware";
    474 	openfirm6x11.firstchar = 32;
    475 	openfirm6x11.numchars = 96;
    476 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    477 	openfirm6x11.fontwidth = char_width;
    478 	openfirm6x11.fontheight = char_height;
    479 	openfirm6x11.stride = 1;
    480 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    481 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    482 	openfirm6x11.data = romfont;
    483 
    484 	return 0;
    485 }
    486 
    487 int
    488 ofb_getcmap(sc, cm)
    489 	struct ofb_softc *sc;
    490 	struct wsdisplay_cmap *cm;
    491 {
    492 	u_int index = cm->index;
    493 	u_int count = cm->count;
    494 	int error;
    495 
    496 	if (index >= 256 || count > 256 || index + count > 256)
    497 		return EINVAL;
    498 
    499 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    500 	if (error)
    501 		return error;
    502 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    503 	if (error)
    504 		return error;
    505 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    506 	if (error)
    507 		return error;
    508 
    509 	return 0;
    510 }
    511 
    512 int
    513 ofb_putcmap(sc, cm)
    514 	struct ofb_softc *sc;
    515 	struct wsdisplay_cmap *cm;
    516 {
    517 	struct ofb_devconfig *dc = sc->sc_dc;
    518 	int index = cm->index;
    519 	int count = cm->count;
    520 	int i;
    521 	u_char *r, *g, *b;
    522 
    523 	if (cm->index >= 256 || cm->count > 256 ||
    524 	    (cm->index + cm->count) > 256)
    525 		return EINVAL;
    526 	if (!uvm_useracc(cm->red, cm->count, B_READ) ||
    527 	    !uvm_useracc(cm->green, cm->count, B_READ) ||
    528 	    !uvm_useracc(cm->blue, cm->count, B_READ))
    529 		return EFAULT;
    530 	copyin(cm->red,   &sc->sc_cmap_red[index],   count);
    531 	copyin(cm->green, &sc->sc_cmap_green[index], count);
    532 	copyin(cm->blue,  &sc->sc_cmap_blue[index],  count);
    533 
    534 	r = &sc->sc_cmap_red[index];
    535 	g = &sc->sc_cmap_green[index];
    536 	b = &sc->sc_cmap_blue[index];
    537 
    538 	for (i = 0; i < count; i++) {
    539 		OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
    540 		r++, g++, b++, index++;
    541 	}
    542 
    543 	return 0;
    544 }
    545