Home | History | Annotate | Line # | Download | only in pci
gten.c revision 1.14
      1 /*	$NetBSD: gten.c,v 1.14 2006/05/09 02:48:36 garbled Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas <matt (at) 3am-software.com>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: gten.c,v 1.14 2006/05/09 02:48:36 garbled Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/buf.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/systm.h>
     50 
     51 #include <uvm/uvm_extern.h>
     52 
     53 #include <dev/pci/pcidevs.h>
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcivar.h>
     56 
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/wscons/wsdisplayvar.h>
     59 #include <dev/rasops/rasops.h>
     60 
     61 #include <machine/bus.h>
     62 #include <machine/gtenvar.h>
     63 
     64 static	int	gten_match(struct device *, struct cfdata *, void *);
     65 static	void	gten_attach(struct device *, struct device *, void *);
     66 static	int	gten_print(void *, const char *);
     67 
     68 CFATTACH_DECL(gten, sizeof(struct gten_softc),
     69     gten_match, gten_attach, NULL, NULL);
     70 
     71 static struct rasops_info gten_console_ri;
     72 static pcitag_t gten_console_pcitag;
     73 
     74 static struct wsscreen_descr gten_stdscreen = {
     75 	"std",
     76 	0, 0,
     77 	0,
     78 	0, 0,
     79 	WSSCREEN_REVERSE
     80 };
     81 
     82 static const struct wsscreen_descr *_gten_scrlist[] = {
     83 	&gten_stdscreen,
     84 	/* XXX other formats, graphics screen? */
     85 };
     86 
     87 static struct wsscreen_list gten_screenlist = {
     88 	sizeof(_gten_scrlist) / sizeof(struct wsscreen_descr *), _gten_scrlist
     89 };
     90 
     91 static int gten_ioctl(void *, void *, u_long, caddr_t, int, struct proc *);
     92 static paddr_t gten_mmap(void *, void *, off_t, int);
     93 static int gten_alloc_screen(void *, const struct wsscreen_descr *,
     94 			     void **, int *, int *, long *);
     95 static void gten_free_screen(void *, void *);
     96 static int gten_show_screen(void *, void *, int,
     97 			    void (*) (void *, int, int), void *);
     98 
     99 static struct wsdisplay_accessops gten_accessops = {
    100 	gten_ioctl,
    101 	gten_mmap,
    102 	gten_alloc_screen,
    103 	gten_free_screen,
    104 	gten_show_screen,
    105 	0 /* load_font */
    106 };
    107 
    108 static void gten_common_init(struct rasops_info *);
    109 static int gten_getcmap(struct gten_softc *, struct wsdisplay_cmap *);
    110 static int gten_putcmap(struct gten_softc *, struct wsdisplay_cmap *);
    111 
    112 #define	GTEN_VRAM_OFFSET	0xf00000
    113 
    114 static int
    115 gten_match(struct device *parent, struct cfdata *match, void *aux)
    116 {
    117 	struct pci_attach_args *pa = aux;
    118 
    119 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_WD &&
    120 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_WD_90C)
    121 		return 2;
    122 
    123 	return 0;
    124 }
    125 
    126 static void
    127 gten_attach(struct device *parent, struct device *self, void *aux)
    128 {
    129 	struct gten_softc *gt = (struct gten_softc *)self;
    130 	struct pci_attach_args *pa = aux;
    131 	struct wsemuldisplaydev_attach_args a;
    132 	int console = (pa->pa_tag == gten_console_pcitag);
    133 	int error;
    134 	char devinfo[256], pbuf[10];
    135 
    136 	error = pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14,
    137 		PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
    138 		&gt->gt_memaddr, &gt->gt_memsize, NULL);
    139 	if (error) {
    140 		printf(": can't determine memory size: error=%d\n",
    141 			error);
    142 		return;
    143 	}
    144 	if (console) {
    145 		gt->gt_ri = &gten_console_ri;
    146 		gt->gt_nscreens = 1;
    147 	} else {
    148 		MALLOC(gt->gt_ri, struct rasops_info *, sizeof(*gt->gt_ri),
    149 			M_DEVBUF, M_NOWAIT);
    150 		if (gt->gt_ri == NULL) {
    151 			printf(": can't alloc memory\n");
    152 			return;
    153 		}
    154 		memset(gt->gt_ri, 0, sizeof(*gt->gt_ri));
    155 #if 0
    156 		error = pci_mapreg_map(pa, 0x14,
    157 			PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
    158 			BUS_SPACE_MAP_LINEAR, NULL,
    159 			(bus_space_handle_t *) &gt->gt_ri->ri_bits,
    160 			NULL, NULL);
    161 #else
    162 		error = bus_space_map(pa->pa_memt, gt->gt_memaddr + GTEN_VRAM_OFFSET,
    163 			960*1024, BUS_SPACE_MAP_LINEAR,
    164 			(bus_space_handle_t *) &gt->gt_ri->ri_bits);
    165 #endif
    166 		if (error) {
    167 			printf(": can't map frame buffer: error=%d\n", error);
    168 			return;
    169 		}
    170 
    171 		gten_common_init(gt->gt_ri);
    172 	}
    173 
    174 	gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits);
    175 	if (gt->gt_paddr == 0) {
    176 		printf(": cannot map framebuffer\n");
    177 		return;
    178 	}
    179 	gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET;
    180 
    181 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    182 	printf(": %s\n", devinfo);
    183 	format_bytes(pbuf, sizeof(pbuf), gt->gt_psize);
    184 	printf("%s: %s, %dx%d, %dbpp\n", self->dv_xname, pbuf,
    185 	       gt->gt_ri->ri_width, gt->gt_ri->ri_height,
    186 	       gt->gt_ri->ri_depth);
    187 #if defined(DEBUG)
    188 	printf("%s: text %dx%d, =+%d+%d\n", self->dv_xname,
    189 	       gt->gt_ri->ri_cols, gt->gt_ri->ri_rows,
    190 	       gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin);
    191 
    192 	{ int i;
    193 	  struct rasops_info *ri = gt->gt_ri;
    194 	for (i = 0; i < 64; i++) {
    195 		int j = i * ri->ri_stride;
    196 		int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride;
    197 		memset(ri->ri_bits + j, 0, 64 - i);
    198 		memset(ri->ri_bits + j + 64 - i, 255, i);
    199 
    200 		memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i);
    201 		memset(ri->ri_bits + j + ri->ri_width - 64, 255, i);
    202 
    203 		memset(ri->ri_bits + k, 0, 64 - i);
    204 		memset(ri->ri_bits + k + 64 - i, 255, i);
    205 
    206 		memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i);
    207 		memset(ri->ri_bits + k + ri->ri_width - 64, 255, i);
    208 	}}
    209 #endif
    210 
    211 	gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0;
    212 	gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff;
    213 	gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff;
    214 	gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff;
    215 
    216 	a.console = console;
    217 	a.scrdata = &gten_screenlist;
    218 	a.accessops = &gten_accessops;
    219 	a.accesscookie = gt;
    220 
    221 	config_found(self, &a, wsemuldisplaydevprint);
    222 }
    223 
    224 static void
    225 gten_common_init(struct rasops_info *ri)
    226 {
    227 	int32_t addr, width, height, linebytes, depth;
    228 	int i, screenbytes;
    229 
    230 	/* initialize rasops */
    231 	ri->ri_width = 640;
    232 	ri->ri_height = 480;
    233 	ri->ri_depth = 8;
    234 	ri->ri_stride = 640;
    235 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
    236 
    237 	rasops_init(ri, 30, 80);
    238 	/* black on white */
    239 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
    240 	ri->ri_devcmap[1] = 0;				/* fg */
    241 
    242 	memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height);
    243 
    244 	gten_stdscreen.nrows = ri->ri_rows;
    245 	gten_stdscreen.ncols = ri->ri_cols;
    246 	gten_stdscreen.textops = &ri->ri_ops;
    247 	gten_stdscreen.capabilities = ri->ri_caps;
    248 }
    249 
    250 static int
    251 gten_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    252     struct proc *p)
    253 {
    254 	struct gten_softc *gt = v;
    255 	struct wsdisplay_fbinfo *wdf;
    256 	struct grfinfo *gm;
    257 
    258 	switch (cmd) {
    259 	case WSDISPLAYIO_GTYPE:
    260 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    261 		return 0;
    262 
    263 	case WSDISPLAYIO_GINFO:
    264 		wdf = (void *)data;
    265 		wdf->height = gt->gt_ri->ri_height;
    266 		wdf->width = gt->gt_ri->ri_width;
    267 		wdf->depth = gt->gt_ri->ri_depth;
    268 		wdf->cmsize = 256;
    269 		return 0;
    270 
    271 	case WSDISPLAYIO_GETCMAP:
    272 		return gten_getcmap(gt, (struct wsdisplay_cmap *)data);
    273 
    274 	case WSDISPLAYIO_PUTCMAP:
    275 		return gten_putcmap(gt, (struct wsdisplay_cmap *)data);
    276 	}
    277 	return EPASSTHROUGH;
    278 }
    279 
    280 static paddr_t
    281 gten_mmap(void *v, void *vs, off_t offset, int prot)
    282 {
    283 	struct gten_softc *gt = v;
    284 
    285 	if (offset >= 0 && offset < gt->gt_psize)
    286 		return gt->gt_paddr + offset;
    287 	if (offset >= 0x1000000 && offset < gt->gt_memsize)
    288 		return gt->gt_memaddr + offset - 0x1000000;
    289 
    290 	return -1;
    291 }
    292 
    293 static int
    294 gten_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    295     int *curxp, int *curyp, long *attrp)
    296 {
    297 	struct gten_softc *gt = v;
    298 	struct rasops_info *ri = gt->gt_ri;
    299 	long defattr;
    300 
    301 	if (gt->gt_nscreens > 0)
    302 		return (ENOMEM);
    303 
    304 	*cookiep = ri;			/* one and only for now */
    305 	*curxp = 0;
    306 	*curyp = 0;
    307 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    308 	*attrp = defattr;
    309 	gt->gt_nscreens++;
    310 	return 0;
    311 }
    312 
    313 static void
    314 gten_free_screen(void *v, void *cookie)
    315 {
    316 	struct gten_softc *gt = v;
    317 
    318 	if (gt->gt_ri == &gten_console_ri)
    319 		panic("gten_free_screen: console");
    320 
    321 	gt->gt_nscreens--;
    322 }
    323 
    324 static int
    325 gten_show_screen(void *v, void *cookie, int waitok,
    326     void (*cb)(void *, int, int), void *cbarg)
    327 {
    328 	return (0);
    329 }
    330 
    331 int
    332 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt)
    333 {
    334 	struct rasops_info *ri = &gten_console_ri;
    335 	u_int32_t mapreg, id, mask, mapsize;
    336 	long defattr;
    337 	pcitag_t tag;
    338 	int s, error;
    339 	bus_size_t bussize;
    340 	bus_addr_t busaddr;
    341 
    342 	tag = pci_make_tag(pc, 0, 14, 0);
    343 
    344 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    345 	if (PCI_VENDOR(id) != PCI_VENDOR_WD ||
    346 	    PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C)
    347 		return ENXIO;
    348 
    349 	mapreg = pci_conf_read(pc, tag, 0x14);
    350 	if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM ||
    351 	    PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT)
    352 		return ENXIO;
    353 
    354         s = splhigh();
    355         pci_conf_write(pc, tag, 0x14, 0xffffffff);
    356         mask = pci_conf_read(pc, tag, 0x14);
    357         pci_conf_write(pc, tag, 0x14, mapreg);
    358 	splx(s);
    359 	bussize = PCI_MAPREG_MEM_SIZE(mask);
    360 	busaddr = PCI_MAPREG_MEM_ADDR(mapreg);
    361 
    362 	error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024,
    363 		BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits);
    364 	if (error)
    365 		return error;
    366 
    367 	gten_common_init(ri);
    368 
    369 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    370 	wsdisplay_cnattach(&gten_stdscreen, ri, 0, 0, defattr);
    371 
    372 	gten_console_pcitag = tag;
    373 
    374 	return 0;
    375 }
    376 
    377 static int
    378 gten_getcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
    379 {
    380 	u_int index = cm->index;
    381 	u_int count = cm->count;
    382 	int error;
    383 
    384 	if (index >= 256 || count > 256 || index + count > 256)
    385 		return EINVAL;
    386 
    387 	error = copyout(&gt->gt_cmap_red[index],   cm->red,   count);
    388 	if (error)
    389 		return error;
    390 	error = copyout(&gt->gt_cmap_green[index], cm->green, count);
    391 	if (error)
    392 		return error;
    393 	error = copyout(&gt->gt_cmap_blue[index],  cm->blue,  count);
    394 	if (error)
    395 		return error;
    396 
    397 	return 0;
    398 }
    399 
    400 static int
    401 gten_putcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
    402 {
    403 	int index = cm->index;
    404 	int count = cm->count;
    405 	int i, error;
    406 	u_char rbuf[256], gbuf[256], bbuf[256];
    407 
    408 	if (cm->index >= 256 || cm->count > 256 ||
    409 	    (cm->index + cm->count) > 256)
    410 		return EINVAL;
    411 	error = copyin(cm->red, &rbuf[index], count);
    412 	if (error)
    413 		return error;
    414 	error = copyin(cm->green, &gbuf[index], count);
    415 	if (error)
    416 		return error;
    417 	error = copyin(cm->blue, &bbuf[index], count);
    418 	if (error)
    419 		return error;
    420 
    421 	memcpy(&gt->gt_cmap_red[index], &rbuf[index], count);
    422 	memcpy(&gt->gt_cmap_green[index], &gbuf[index], count);
    423 	memcpy(&gt->gt_cmap_blue[index], &bbuf[index], count);
    424 	return 0;
    425 }
    426