Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.46
      1  1.46  macallan /*	$NetBSD: ofb.c,v 1.46 2006/02/16 02:15:29 macallan Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*
      4   1.1    tsubai  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5   1.1    tsubai  * All rights reserved.
      6   1.1    tsubai  *
      7   1.1    tsubai  * Author: Chris G. Demetriou
      8   1.1    tsubai  *
      9   1.1    tsubai  * Permission to use, copy, modify and distribute this software and
     10   1.1    tsubai  * its documentation is hereby granted, provided that both the copyright
     11   1.1    tsubai  * notice and this permission notice appear in all copies of the
     12   1.1    tsubai  * software, derivative works or modified versions, and any portions
     13   1.1    tsubai  * thereof, and that both notices appear in supporting documentation.
     14   1.1    tsubai  *
     15   1.1    tsubai  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1    tsubai  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1    tsubai  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1    tsubai  *
     19   1.1    tsubai  * Carnegie Mellon requests users of this software to return to
     20   1.1    tsubai  *
     21   1.1    tsubai  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1    tsubai  *  School of Computer Science
     23   1.1    tsubai  *  Carnegie Mellon University
     24   1.1    tsubai  *  Pittsburgh PA 15213-3890
     25   1.1    tsubai  *
     26   1.1    tsubai  * any improvements or extensions that they make and grant Carnegie the
     27   1.1    tsubai  * rights to redistribute these changes.
     28   1.1    tsubai  */
     29  1.36     lukem 
     30  1.36     lukem #include <sys/cdefs.h>
     31  1.46  macallan __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.46 2006/02/16 02:15:29 macallan Exp $");
     32   1.1    tsubai 
     33   1.1    tsubai #include <sys/param.h>
     34   1.1    tsubai #include <sys/buf.h>
     35   1.1    tsubai #include <sys/conf.h>
     36   1.1    tsubai #include <sys/device.h>
     37   1.1    tsubai #include <sys/ioctl.h>
     38   1.1    tsubai #include <sys/kernel.h>
     39   1.1    tsubai #include <sys/malloc.h>
     40   1.1    tsubai #include <sys/systm.h>
     41   1.1    tsubai 
     42  1.15       mrg #include <uvm/uvm_extern.h>
     43  1.11    tsubai 
     44   1.3    tsubai #include <dev/pci/pcidevs.h>
     45   1.1    tsubai #include <dev/pci/pcireg.h>
     46   1.1    tsubai #include <dev/pci/pcivar.h>
     47  1.27   nathanw #include <dev/pci/pciio.h>
     48   1.1    tsubai 
     49   1.1    tsubai #include <dev/wscons/wsconsio.h>
     50   1.1    tsubai #include <dev/wscons/wsdisplayvar.h>
     51  1.11    tsubai #include <dev/rasops/rasops.h>
     52  1.45  macallan #include <dev/wsfont/wsfont.h>
     53   1.1    tsubai 
     54  1.20      matt #include <dev/ofw/openfirm.h>
     55  1.13    tsubai #include <dev/ofw/ofw_pci.h>
     56  1.13    tsubai 
     57   1.1    tsubai #include <machine/bus.h>
     58  1.20      matt #include <machine/autoconf.h>
     59   1.1    tsubai #include <machine/grfioctl.h>
     60  1.37      matt 
     61  1.37      matt #include <powerpc/oea/bat.h>
     62   1.1    tsubai 
     63  1.45  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     64   1.1    tsubai #include <macppc/dev/ofbvar.h>
     65   1.1    tsubai 
     66  1.18    tsubai #if OFB_ENABLE_CACHE
     67  1.18    tsubai int ofb_enable_cache = 1;
     68  1.18    tsubai #else
     69  1.18    tsubai int ofb_enable_cache = 0;
     70  1.18    tsubai #endif
     71  1.18    tsubai 
     72  1.44  macallan static int	ofbmatch(struct device *, struct cfdata *, void *);
     73  1.44  macallan static void	ofbattach(struct device *, struct device *, void *);
     74   1.1    tsubai 
     75  1.32   thorpej CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     76  1.32   thorpej     ofbmatch, ofbattach, NULL, NULL);
     77   1.1    tsubai 
     78   1.1    tsubai struct wsscreen_descr ofb_stdscreen = {
     79   1.1    tsubai 	"std",
     80   1.1    tsubai 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
     81  1.11    tsubai 	0,
     82   1.1    tsubai 	0, 0,
     83   1.1    tsubai 	WSSCREEN_REVERSE
     84   1.1    tsubai };
     85   1.1    tsubai 
     86   1.1    tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
     87   1.1    tsubai 	&ofb_stdscreen,
     88   1.1    tsubai 	/* XXX other formats, graphics screen? */
     89   1.1    tsubai };
     90   1.1    tsubai 
     91   1.1    tsubai struct wsscreen_list ofb_screenlist = {
     92   1.1    tsubai 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
     93   1.1    tsubai };
     94   1.1    tsubai 
     95  1.45  macallan static int	ofb_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     96  1.45  macallan static paddr_t	ofb_mmap(void *, off_t, int);
     97  1.45  macallan static int	copy_rom_font(void);
     98  1.45  macallan 
     99  1.45  macallan static int	ofb_init_rasops(int, struct rasops_info *);
    100  1.45  macallan static void	ofb_init_screen(void *, struct vcons_screen *, int, long *);
    101   1.1    tsubai 
    102   1.1    tsubai struct wsdisplay_accessops ofb_accessops = {
    103   1.1    tsubai 	ofb_ioctl,
    104   1.1    tsubai 	ofb_mmap,
    105  1.45  macallan 	NULL,		/* vcons_alloc_screen */
    106  1.45  macallan 	NULL,		/* vcons_free_screen */
    107  1.45  macallan 	NULL,		/* vcons_show_screen */
    108  1.45  macallan 	NULL		/* load_font */
    109   1.1    tsubai };
    110   1.1    tsubai 
    111  1.45  macallan static struct vcons_screen ofb_console_screen;
    112  1.11    tsubai static struct wsdisplay_font openfirm6x11;
    113  1.45  macallan static int    console_node, console_instance;
    114  1.45  macallan static vaddr_t fbaddr;
    115  1.45  macallan static int romfont_loaded = 0;
    116  1.45  macallan 
    117  1.45  macallan static void	ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
    118  1.45  macallan     uint8_t);
    119  1.45  macallan 
    120  1.45  macallan static int	ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    121  1.45  macallan static int	ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    122  1.45  macallan static void	ofb_init_cmap(struct ofb_softc *);
    123  1.11    tsubai 
    124  1.45  macallan extern const u_char rasops_cmap[768];
    125   1.1    tsubai 
    126  1.45  macallan static int
    127  1.44  macallan ofbmatch(struct device *parent, struct cfdata *match, void *aux)
    128   1.1    tsubai {
    129   1.1    tsubai 	struct pci_attach_args *pa = aux;
    130   1.1    tsubai 
    131   1.3    tsubai 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    132  1.11    tsubai 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    133   1.3    tsubai 		return 1;
    134   1.1    tsubai 
    135   1.3    tsubai 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    136   1.3    tsubai 		return 1;
    137   1.3    tsubai 
    138   1.3    tsubai 	return 0;
    139   1.1    tsubai }
    140   1.1    tsubai 
    141  1.44  macallan static void
    142  1.44  macallan ofbattach(struct device *parent, struct device *self, void *aux)
    143   1.1    tsubai {
    144  1.22    tsubai 	struct ofb_softc *sc = (struct ofb_softc *)self;
    145   1.1    tsubai 	struct pci_attach_args *pa = aux;
    146   1.1    tsubai 	struct wsemuldisplaydev_attach_args a;
    147  1.45  macallan 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    148  1.45  macallan 	long defattr;
    149  1.46  macallan 	int console, len, node;
    150   1.5    tsubai 	char devinfo[256];
    151   1.1    tsubai 
    152  1.46  macallan 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    153  1.46  macallan 	console = (node == console_node);
    154  1.46  macallan 
    155  1.42  macallan 	sc->sc_memt = pa->pa_memt;
    156  1.42  macallan 	sc->sc_iot = pa->pa_iot;
    157  1.45  macallan 	sc->sc_pc = pa->pa_pc;
    158  1.45  macallan 	sc->sc_pcitag = pa->pa_tag;
    159  1.45  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    160  1.42  macallan 
    161  1.45  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    162  1.45  macallan 	printf(": %s\n", devinfo);
    163  1.46  macallan 
    164  1.46  macallan 	if (!console)
    165  1.46  macallan 		return;
    166  1.45  macallan 
    167  1.45  macallan 	vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
    168  1.45  macallan 	sc->vd.init_screen = ofb_init_screen;
    169  1.45  macallan 
    170   1.1    tsubai 	console = ofb_is_console();
    171   1.1    tsubai 
    172  1.46  macallan 	sc->sc_node = node;
    173  1.46  macallan 
    174   1.1    tsubai 	if (console) {
    175  1.45  macallan 		sc->sc_ih = console_instance;
    176  1.45  macallan 		vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
    177  1.45  macallan 		ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    178  1.45  macallan 		printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    179  1.45  macallan 		       ri->ri_width, ri->ri_height, ri->ri_depth);
    180   1.1    tsubai 	} else {
    181  1.45  macallan 		char name[64];
    182  1.45  macallan 		if (sc->sc_node == 0) {
    183   1.3    tsubai 			printf(": ofdev not found\n");
    184   1.3    tsubai 			return;
    185   1.3    tsubai 		}
    186  1.16    tsubai 
    187  1.16    tsubai 		/* XXX There are two child screens on PowerBook. */
    188  1.24       wiz 		memset(devinfo, 0, sizeof(devinfo));
    189  1.45  macallan 		OF_getprop(sc->sc_node, "device_type", devinfo, sizeof(devinfo));
    190  1.16    tsubai 		len = strlen(devinfo);
    191  1.16    tsubai 		if (strcmp(devinfo + len - 7, "-parent") == 0)
    192  1.45  macallan 			sc->sc_node = OF_child(sc->sc_node);
    193  1.16    tsubai 
    194  1.45  macallan 		memset(name, 0, 64);
    195  1.45  macallan 		OF_package_to_path(sc->sc_node, name, sizeof(name));
    196  1.45  macallan 		sc->sc_ih = OF_open(name);
    197  1.45  macallan 
    198  1.45  macallan 	}
    199  1.45  macallan 
    200  1.45  macallan 	sc->sc_fbaddr = 0;
    201  1.45  macallan 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
    202  1.45  macallan 		OF_interpret("frame-buffer-adr", 1, &sc->sc_fbaddr);
    203  1.45  macallan 	if (sc->sc_fbaddr == 0) {
    204  1.45  macallan 		printf("%s: Unable to find the framebuffer address.\n",
    205  1.45  macallan 		    sc->sc_dev.dv_xname);
    206  1.45  macallan 		return;
    207   1.1    tsubai 	}
    208  1.45  macallan 
    209  1.16    tsubai 	/* XXX */
    210  1.45  macallan 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    211  1.16    tsubai 	    sizeof(sc->sc_addrs)) == -1) {
    212  1.45  macallan 		sc->sc_node = OF_parent(sc->sc_node);
    213  1.45  macallan 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    214  1.16    tsubai 		    sizeof(sc->sc_addrs));
    215  1.16    tsubai 	}
    216  1.13    tsubai 
    217  1.45  macallan 	ofb_init_cmap(sc);
    218   1.1    tsubai 
    219   1.1    tsubai 	a.console = console;
    220   1.1    tsubai 	a.scrdata = &ofb_screenlist;
    221   1.1    tsubai 	a.accessops = &ofb_accessops;
    222  1.45  macallan 	a.accesscookie = &sc->vd;
    223   1.1    tsubai 
    224   1.1    tsubai 	config_found(self, &a, wsemuldisplaydevprint);
    225   1.1    tsubai }
    226   1.1    tsubai 
    227  1.44  macallan static void
    228  1.45  macallan ofb_init_screen(void *cookie, struct vcons_screen *scr,
    229  1.45  macallan     int existing, long *defattr)
    230   1.1    tsubai {
    231  1.45  macallan 	struct ofb_softc *sc = cookie;
    232  1.45  macallan 	struct rasops_info *ri = &scr->scr_ri;
    233  1.45  macallan 
    234  1.45  macallan 	if (scr != &ofb_console_screen) {
    235  1.45  macallan 		ofb_init_rasops(sc->sc_node, ri);
    236  1.45  macallan 	}
    237  1.45  macallan }
    238   1.1    tsubai 
    239  1.45  macallan static int
    240  1.45  macallan ofb_init_rasops(int node, struct rasops_info *ri)
    241  1.45  macallan {
    242  1.45  macallan 	int32_t width, height, linebytes, depth;
    243   1.3    tsubai 
    244   1.8    tsubai 	/* XXX /chaos/control doesn't have "width", "height", ... */
    245   1.3    tsubai 	width = height = -1;
    246  1.11    tsubai 	if (OF_getprop(node, "width", &width, 4) != 4)
    247   1.3    tsubai 		OF_interpret("screen-width", 1, &width);
    248  1.11    tsubai 	if (OF_getprop(node, "height", &height, 4) != 4)
    249   1.3    tsubai 		OF_interpret("screen-height", 1, &height);
    250  1.11    tsubai 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    251   1.3    tsubai 		linebytes = width;			/* XXX */
    252  1.11    tsubai 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    253   1.3    tsubai 		depth = 8;				/* XXX */
    254  1.45  macallan 	if (OF_getprop(node, "address", &fbaddr, 4) != 4)
    255  1.45  macallan 		OF_interpret("frame-buffer-adr", 1, &fbaddr);
    256  1.11    tsubai 
    257  1.45  macallan 	if (width == -1 || height == -1 || fbaddr == 0 || fbaddr == -1)
    258  1.45  macallan 		return FALSE;
    259  1.18    tsubai 
    260  1.18    tsubai 	/* Enable write-through cache. */
    261  1.29      matt 	if (ofb_enable_cache) {
    262  1.29      matt 		vaddr_t va;
    263  1.29      matt 		/*
    264  1.29      matt 		 * Let's try to find an empty BAT to use
    265  1.29      matt 		 */
    266  1.29      matt 		for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
    267  1.29      matt 		     va += SEGMENT_LENGTH) {
    268  1.29      matt 			if (battable[va >> ADDR_SR_SHFT].batu == 0) {
    269  1.29      matt 				battable[va >> ADDR_SR_SHFT].batl =
    270  1.45  macallan 				    BATL(fbaddr & 0xf0000000,
    271  1.33    briggs 					 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
    272  1.29      matt 				battable[va >> ADDR_SR_SHFT].batu =
    273  1.29      matt 				    BATL(va, BAT_BL_256M, BAT_Vs);
    274  1.45  macallan 				fbaddr &= 0x0fffffff;
    275  1.45  macallan 				fbaddr |= va;
    276  1.29      matt 				break;
    277  1.29      matt 			}
    278  1.29      matt 		}
    279  1.18    tsubai 	}
    280   1.1    tsubai 
    281  1.11    tsubai 	/* initialize rasops */
    282  1.11    tsubai 	ri->ri_width = width;
    283  1.11    tsubai 	ri->ri_height = height;
    284  1.11    tsubai 	ri->ri_depth = depth;
    285  1.11    tsubai 	ri->ri_stride = linebytes;
    286  1.45  macallan 	ri->ri_bits = (char *)fbaddr;
    287  1.45  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    288   1.1    tsubai 
    289   1.8    tsubai 	/* If screen is smaller than 1024x768, use small font. */
    290  1.45  macallan 	if ((width < 1024 || height < 768) && (romfont_loaded)) {
    291  1.11    tsubai 		int cols, rows;
    292   1.8    tsubai 
    293  1.45  macallan 		/*
    294  1.45  macallan 		 * XXX this assumes we're the console which may or may not
    295  1.45  macallan 		 * be the case
    296  1.45  macallan 		 */
    297  1.11    tsubai 		OF_interpret("#lines", 1, &rows);
    298  1.11    tsubai 		OF_interpret("#columns", 1, &cols);
    299  1.11    tsubai 		ri->ri_font = &openfirm6x11;
    300  1.11    tsubai 		ri->ri_wsfcookie = -1;		/* not using wsfont */
    301  1.11    tsubai 		rasops_init(ri, rows, cols);
    302  1.11    tsubai 
    303  1.42  macallan 		ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
    304  1.42  macallan 		ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
    305  1.42  macallan 		    >> 1;
    306  1.45  macallan 		ri->ri_bits = (char *)fbaddr + ri->ri_xorigin +
    307  1.11    tsubai 			      ri->ri_stride * ri->ri_yorigin;
    308  1.17    tsubai 	} else {
    309  1.45  macallan 		/* use as much of the screen as the font permits */
    310  1.45  macallan 		rasops_init(ri, height/8, width/8);
    311  1.45  macallan 		ri->ri_caps = WSSCREEN_WSCOLORS;
    312  1.45  macallan 		rasops_reconfig(ri, height / ri->ri_font->fontheight,
    313  1.45  macallan 		    width / ri->ri_font->fontwidth);
    314  1.17    tsubai 	}
    315  1.11    tsubai 
    316  1.45  macallan 	return TRUE;
    317   1.1    tsubai }
    318   1.1    tsubai 
    319   1.1    tsubai int
    320   1.1    tsubai ofb_is_console()
    321   1.1    tsubai {
    322   1.1    tsubai 	int chosen, stdout, node;
    323   1.1    tsubai 	char type[16];
    324   1.1    tsubai 
    325   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    326   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, 4);
    327   1.1    tsubai 	node = OF_instance_to_package(stdout);
    328   1.1    tsubai 	OF_getprop(node, "device_type", type, sizeof(type));
    329   1.1    tsubai 	if (strcmp(type, "display") == 0)
    330   1.1    tsubai 		return 1;
    331   1.1    tsubai 	else
    332   1.1    tsubai 		return 0;
    333   1.1    tsubai }
    334   1.1    tsubai 
    335  1.44  macallan static int
    336  1.44  macallan ofb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    337   1.1    tsubai {
    338  1.45  macallan 	struct vcons_data *vd = v;
    339  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    340   1.1    tsubai 	struct wsdisplay_fbinfo *wdf;
    341  1.45  macallan 	struct vcons_screen *ms = vd->active;
    342   1.1    tsubai 	struct grfinfo *gm;
    343   1.1    tsubai 
    344   1.1    tsubai 	switch (cmd) {
    345   1.1    tsubai 	case WSDISPLAYIO_GTYPE:
    346   1.1    tsubai 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    347   1.1    tsubai 		return 0;
    348   1.1    tsubai 
    349   1.1    tsubai 	case WSDISPLAYIO_GINFO:
    350  1.45  macallan 		/* we won't get here without any screen anyway */
    351  1.45  macallan 		if (ms != NULL) {
    352  1.45  macallan 			wdf = (void *)data;
    353  1.45  macallan 			wdf->height = ms->scr_ri.ri_width;
    354  1.45  macallan 			wdf->width = ms->scr_ri.ri_height;
    355  1.45  macallan 			wdf->depth = ms->scr_ri.ri_depth;
    356  1.45  macallan 			wdf->cmsize = 256;
    357  1.45  macallan 			return 0;
    358  1.45  macallan 		} else
    359  1.45  macallan 			return ENODEV;
    360   1.1    tsubai 
    361  1.11    tsubai 	case WSDISPLAYIO_GETCMAP:
    362  1.11    tsubai 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    363  1.11    tsubai 
    364   1.1    tsubai 	case WSDISPLAYIO_PUTCMAP:
    365  1.11    tsubai 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    366   1.1    tsubai 
    367   1.1    tsubai 	/* XXX There are no way to know framebuffer pa from a user program. */
    368   1.1    tsubai 	case GRFIOCGINFO:
    369  1.45  macallan 		if (ms != NULL) {
    370  1.45  macallan 			gm = (void *)data;
    371  1.45  macallan 			memset(gm, 0, sizeof(struct grfinfo));
    372  1.45  macallan 			gm->gd_fbaddr = (caddr_t)sc->sc_fbaddr;
    373  1.45  macallan 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
    374  1.45  macallan 			return 0;
    375  1.45  macallan 		} else
    376  1.45  macallan 			return ENODEV;
    377  1.45  macallan 	case WSDISPLAYIO_SMODE:
    378  1.45  macallan 		{
    379  1.45  macallan 			int new_mode = *(int*)data;
    380  1.45  macallan 			if (new_mode != sc->sc_mode)
    381  1.45  macallan 			{
    382  1.45  macallan 				sc->sc_mode = new_mode;
    383  1.45  macallan 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
    384  1.45  macallan 				{
    385  1.45  macallan 					ofb_init_cmap(sc);
    386  1.45  macallan 					vcons_redraw_screen(ms);
    387  1.45  macallan 				}
    388  1.45  macallan 			}
    389  1.45  macallan 		}
    390   1.1    tsubai 		return 0;
    391  1.27   nathanw 	/* PCI config read/write passthrough. */
    392  1.27   nathanw 	case PCI_IOC_CFGREAD:
    393  1.27   nathanw 	case PCI_IOC_CFGWRITE:
    394  1.27   nathanw 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    395  1.43  christos 		    cmd, data, flag, l));
    396   1.1    tsubai 	}
    397  1.26    atatat 	return EPASSTHROUGH;
    398   1.1    tsubai }
    399   1.1    tsubai 
    400  1.44  macallan static paddr_t
    401  1.44  macallan ofb_mmap(void *v, off_t offset, int prot)
    402   1.1    tsubai {
    403  1.45  macallan 	struct vcons_data *vd = v;
    404  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    405  1.45  macallan 	struct rasops_info *ri;
    406  1.13    tsubai 	u_int32_t *ap = sc->sc_addrs;
    407  1.42  macallan 	struct proc *me;
    408  1.13    tsubai 	int i;
    409  1.13    tsubai 
    410  1.45  macallan 	if (vd->active == NULL) {
    411  1.45  macallan 		printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
    412  1.45  macallan 		return -1;
    413  1.45  macallan 	}
    414  1.45  macallan 
    415  1.45  macallan 	ri = &vd->active->scr_ri;
    416  1.45  macallan 
    417  1.42  macallan 	/* framebuffer at offset 0 */
    418  1.13    tsubai 	if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
    419  1.45  macallan 		return sc->sc_fbaddr + offset;
    420  1.13    tsubai 
    421  1.42  macallan 	/*
    422  1.42  macallan 	 * restrict all other mappings to processes with superuser privileges
    423  1.42  macallan 	 * or the kernel itself
    424  1.42  macallan 	 */
    425  1.42  macallan 	me = __curproc();
    426  1.42  macallan 	if (me != NULL) {
    427  1.42  macallan 		if (suser(me->p_ucred, NULL) != 0) {
    428  1.45  macallan 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    429  1.42  macallan 			return -1;
    430  1.42  macallan 		}
    431  1.42  macallan 	}
    432  1.42  macallan 
    433  1.42  macallan 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    434  1.42  macallan #ifdef OFB_FAKE_VGA_FB
    435  1.42  macallan 	if (offset >=0xa0000 && offset < 0xbffff)
    436  1.45  macallan 		return sc->sc_fbaddr + offset - 0xa0000;
    437  1.42  macallan #endif
    438  1.42  macallan 
    439  1.42  macallan 	/* allow to map our IO space */
    440  1.42  macallan 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    441  1.42  macallan 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    442  1.42  macallan 		    BUS_SPACE_MAP_LINEAR);
    443  1.42  macallan 	}
    444  1.42  macallan 
    445  1.13    tsubai 	for (i = 0; i < 6; i++) {
    446  1.13    tsubai 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    447  1.13    tsubai 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    448  1.42  macallan 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    449  1.42  macallan 				return bus_space_mmap(sc->sc_memt, offset, 0,
    450  1.42  macallan 				    prot, BUS_SPACE_MAP_LINEAR);
    451  1.13    tsubai 		}
    452  1.13    tsubai 		ap += 5;
    453  1.13    tsubai 	}
    454   1.1    tsubai 
    455  1.13    tsubai 	return -1;
    456   1.1    tsubai }
    457   1.1    tsubai 
    458   1.1    tsubai int
    459   1.1    tsubai ofb_cnattach()
    460   1.1    tsubai {
    461  1.45  macallan 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    462   1.1    tsubai 	long defattr;
    463  1.11    tsubai 	int crow = 0;
    464   1.1    tsubai 	int chosen, stdout, node;
    465   1.1    tsubai 
    466   1.1    tsubai 	chosen = OF_finddevice("/chosen");
    467   1.1    tsubai 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
    468   1.1    tsubai 	node = OF_instance_to_package(stdout);
    469  1.45  macallan 	console_node = node;
    470  1.45  macallan 	console_instance = stdout;
    471   1.1    tsubai 
    472  1.11    tsubai 	/* get current cursor position */
    473  1.11    tsubai 	OF_interpret("line#", 1, &crow);
    474   1.8    tsubai 
    475  1.11    tsubai 	/* move (rom monitor) cursor to the lowest line - 1 */
    476  1.11    tsubai 	OF_interpret("#lines 2 - to line#", 0);
    477  1.45  macallan 
    478  1.45  macallan 	wsfont_init();
    479  1.45  macallan 	if (copy_rom_font() == 0) {
    480  1.45  macallan 		romfont_loaded = 1;
    481  1.45  macallan 	}
    482  1.45  macallan 
    483  1.45  macallan 	/* set up rasops */
    484  1.45  macallan 	ofb_init_rasops(console_node, ri);
    485   1.8    tsubai 
    486  1.45  macallan 	/*
    487  1.45  macallan 	 * no need to clear the screen here when we're mimicing firmware
    488  1.45  macallan 	 * output anyway
    489  1.45  macallan 	 */
    490  1.45  macallan #if 0
    491  1.11    tsubai 	if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
    492  1.11    tsubai 		int i, screenbytes = ri->ri_stride * ri->ri_height;
    493   1.8    tsubai 
    494   1.8    tsubai 		for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
    495  1.45  macallan 			*(u_int32_t *)(fbaddr + i) = 0xffffffff;
    496   1.8    tsubai 		crow = 0;
    497   1.8    tsubai 	}
    498  1.45  macallan #endif
    499  1.45  macallan 
    500  1.45  macallan 	ofb_stdscreen.nrows = ri->ri_rows;
    501  1.45  macallan 	ofb_stdscreen.ncols = ri->ri_cols;
    502  1.45  macallan 	ofb_stdscreen.textops = &ri->ri_ops;
    503  1.45  macallan 	ofb_stdscreen.capabilities = ri->ri_caps;
    504   1.8    tsubai 
    505  1.45  macallan 	ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
    506  1.11    tsubai 	wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
    507   1.8    tsubai 
    508   1.8    tsubai 	return 0;
    509   1.8    tsubai }
    510   1.8    tsubai 
    511  1.44  macallan static int
    512   1.8    tsubai copy_rom_font()
    513   1.8    tsubai {
    514   1.8    tsubai 	u_char *romfont;
    515   1.8    tsubai 	int char_width, char_height;
    516   1.8    tsubai 	int chosen, mmu, m, e;
    517   1.8    tsubai 
    518   1.8    tsubai 	/* Get ROM FONT address. */
    519   1.8    tsubai 	OF_interpret("font-adr", 1, &romfont);
    520   1.8    tsubai 	if (romfont == NULL)
    521   1.8    tsubai 		return -1;
    522   1.8    tsubai 
    523   1.8    tsubai 	chosen = OF_finddevice("/chosen");
    524   1.8    tsubai 	OF_getprop(chosen, "mmu", &mmu, 4);
    525   1.1    tsubai 
    526   1.8    tsubai 	/*
    527   1.8    tsubai 	 * Convert to physcal address.  We cannot access to Open Firmware's
    528   1.8    tsubai 	 * virtual address space.
    529   1.8    tsubai 	 */
    530   1.8    tsubai 	OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
    531   1.8    tsubai 
    532   1.8    tsubai 	/* Get character size */
    533   1.8    tsubai 	OF_interpret("char-width", 1, &char_width);
    534   1.8    tsubai 	OF_interpret("char-height", 1, &char_height);
    535   1.8    tsubai 
    536  1.11    tsubai 	openfirm6x11.name = "Open Firmware";
    537  1.11    tsubai 	openfirm6x11.firstchar = 32;
    538  1.11    tsubai 	openfirm6x11.numchars = 96;
    539  1.11    tsubai 	openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
    540  1.11    tsubai 	openfirm6x11.fontwidth = char_width;
    541  1.11    tsubai 	openfirm6x11.fontheight = char_height;
    542  1.11    tsubai 	openfirm6x11.stride = 1;
    543  1.11    tsubai 	openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
    544  1.11    tsubai 	openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
    545  1.11    tsubai 	openfirm6x11.data = romfont;
    546  1.11    tsubai 
    547  1.11    tsubai 	return 0;
    548  1.11    tsubai }
    549  1.11    tsubai 
    550  1.44  macallan static int
    551  1.44  macallan ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    552  1.11    tsubai {
    553  1.11    tsubai 	u_int index = cm->index;
    554  1.11    tsubai 	u_int count = cm->count;
    555  1.11    tsubai 	int error;
    556  1.11    tsubai 
    557  1.11    tsubai 	if (index >= 256 || count > 256 || index + count > 256)
    558  1.11    tsubai 		return EINVAL;
    559  1.11    tsubai 
    560  1.11    tsubai 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    561  1.11    tsubai 	if (error)
    562  1.11    tsubai 		return error;
    563  1.11    tsubai 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    564  1.11    tsubai 	if (error)
    565  1.11    tsubai 		return error;
    566  1.11    tsubai 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    567  1.11    tsubai 	if (error)
    568  1.11    tsubai 		return error;
    569   1.8    tsubai 
    570   1.1    tsubai 	return 0;
    571   1.1    tsubai }
    572   1.1    tsubai 
    573  1.45  macallan int
    574  1.44  macallan ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    575   1.1    tsubai {
    576  1.25  jdolecek 	u_int index = cm->index;
    577  1.25  jdolecek 	u_int count = cm->count;
    578  1.38       chs 	int i, error;
    579  1.38       chs 	u_char rbuf[256], gbuf[256], bbuf[256];
    580   1.1    tsubai 	u_char *r, *g, *b;
    581   1.1    tsubai 
    582   1.1    tsubai 	if (cm->index >= 256 || cm->count > 256 ||
    583   1.1    tsubai 	    (cm->index + cm->count) > 256)
    584   1.1    tsubai 		return EINVAL;
    585  1.38       chs 	error = copyin(cm->red, &rbuf[index], count);
    586  1.38       chs 	if (error)
    587  1.38       chs 		return error;
    588  1.38       chs 	error = copyin(cm->green, &gbuf[index], count);
    589  1.38       chs 	if (error)
    590  1.38       chs 		return error;
    591  1.38       chs 	error = copyin(cm->blue, &bbuf[index], count);
    592  1.38       chs 	if (error)
    593  1.38       chs 		return error;
    594  1.38       chs 
    595  1.38       chs 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    596  1.38       chs 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    597  1.38       chs 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    598   1.1    tsubai 
    599   1.1    tsubai 	r = &sc->sc_cmap_red[index];
    600   1.1    tsubai 	g = &sc->sc_cmap_green[index];
    601   1.1    tsubai 	b = &sc->sc_cmap_blue[index];
    602   1.1    tsubai 	for (i = 0; i < count; i++) {
    603  1.45  macallan 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
    604   1.1    tsubai 		r++, g++, b++, index++;
    605   1.1    tsubai 	}
    606   1.1    tsubai 	return 0;
    607   1.1    tsubai }
    608  1.45  macallan 
    609  1.45  macallan static void
    610  1.45  macallan ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
    611  1.45  macallan {
    612  1.45  macallan 	if ((idx<0) || (idx > 255))
    613  1.45  macallan 		return;
    614  1.45  macallan 	OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
    615  1.45  macallan 	sc->sc_cmap_red[idx] = r;
    616  1.45  macallan 	sc->sc_cmap_green[idx] = g;
    617  1.45  macallan 	sc->sc_cmap_blue[idx] = b;
    618  1.45  macallan }
    619  1.45  macallan 
    620  1.45  macallan static void
    621  1.45  macallan ofb_init_cmap(struct ofb_softc *sc)
    622  1.45  macallan {
    623  1.45  macallan 	int idx, i;
    624  1.45  macallan 	/* mess with the palette only when we're running in 8 bit */
    625  1.45  macallan 	if (ofb_console_screen.scr_ri.ri_depth == 8) {
    626  1.45  macallan 		idx = 0;
    627  1.45  macallan 		for (i = 0; i < 256; i++) {
    628  1.45  macallan 			ofb_putpalreg(sc, i, rasops_cmap[idx],
    629  1.45  macallan 			    rasops_cmap[idx + 1], rasops_cmap[idx + 2]);
    630  1.45  macallan 			idx += 3;
    631  1.45  macallan 		}
    632  1.45  macallan 	}
    633  1.45  macallan }
    634