Home | History | Annotate | Line # | Download | only in pci
gten.c revision 1.12.10.1
      1 /*	$NetBSD: gten.c,v 1.12.10.1 2006/04/19 02:33:33 elad 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.12.10.1 2006/04/19 02:33:33 elad 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(v, vs, cmd, data, flag, p)
    252 	void *v;
    253 	void *vs;
    254 	u_long cmd;
    255 	caddr_t data;
    256 	int flag;
    257 	struct proc *p;
    258 {
    259 	struct gten_softc *gt = v;
    260 	struct wsdisplay_fbinfo *wdf;
    261 	struct grfinfo *gm;
    262 
    263 	switch (cmd) {
    264 	case WSDISPLAYIO_GTYPE:
    265 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    266 		return 0;
    267 
    268 	case WSDISPLAYIO_GINFO:
    269 		wdf = (void *)data;
    270 		wdf->height = gt->gt_ri->ri_height;
    271 		wdf->width = gt->gt_ri->ri_width;
    272 		wdf->depth = gt->gt_ri->ri_depth;
    273 		wdf->cmsize = 256;
    274 		return 0;
    275 
    276 	case WSDISPLAYIO_GETCMAP:
    277 		return gten_getcmap(gt, (struct wsdisplay_cmap *)data);
    278 
    279 	case WSDISPLAYIO_PUTCMAP:
    280 		return gten_putcmap(gt, (struct wsdisplay_cmap *)data);
    281 	}
    282 	return EPASSTHROUGH;
    283 }
    284 
    285 static paddr_t
    286 gten_mmap(v, vs, offset, prot)
    287 	void *v;
    288 	void *vs;
    289 	off_t offset;
    290 	int prot;
    291 {
    292 	struct gten_softc *gt = v;
    293 
    294 	if (offset >= 0 && offset < gt->gt_psize)
    295 		return gt->gt_paddr + offset;
    296 	if (offset >= 0x1000000 && offset < gt->gt_memsize)
    297 		return gt->gt_memaddr + offset - 0x1000000;
    298 
    299 	return -1;
    300 }
    301 
    302 static int
    303 gten_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    304 	void *v;
    305 	const struct wsscreen_descr *type;
    306 	void **cookiep;
    307 	int *curxp, *curyp;
    308 	long *attrp;
    309 {
    310 	struct gten_softc *gt = v;
    311 	struct rasops_info *ri = gt->gt_ri;
    312 	long defattr;
    313 
    314 	if (gt->gt_nscreens > 0)
    315 		return (ENOMEM);
    316 
    317 	*cookiep = ri;			/* one and only for now */
    318 	*curxp = 0;
    319 	*curyp = 0;
    320 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    321 	*attrp = defattr;
    322 	gt->gt_nscreens++;
    323 	return 0;
    324 }
    325 
    326 static void
    327 gten_free_screen(v, cookie)
    328 	void *v;
    329 	void *cookie;
    330 {
    331 	struct gten_softc *gt = v;
    332 
    333 	if (gt->gt_ri == &gten_console_ri)
    334 		panic("gten_free_screen: console");
    335 
    336 	gt->gt_nscreens--;
    337 }
    338 
    339 static int
    340 gten_show_screen(v, cookie, waitok, cb, cbarg)
    341 	void *v;
    342 	void *cookie;
    343 	int waitok;
    344 	void (*cb) (void *, int, int);
    345 	void *cbarg;
    346 {
    347 	return (0);
    348 }
    349 
    350 int
    351 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt)
    352 {
    353 	struct rasops_info *ri = &gten_console_ri;
    354 	u_int32_t mapreg, id, mask, mapsize;
    355 	long defattr;
    356 	pcitag_t tag;
    357 	int s, error;
    358 	bus_size_t bussize;
    359 	bus_addr_t busaddr;
    360 
    361 	tag = pci_make_tag(pc, 0, 14, 0);
    362 
    363 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    364 	if (PCI_VENDOR(id) != PCI_VENDOR_WD ||
    365 	    PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C)
    366 		return ENXIO;
    367 
    368 	mapreg = pci_conf_read(pc, tag, 0x14);
    369 	if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM ||
    370 	    PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT)
    371 		return ENXIO;
    372 
    373         s = splhigh();
    374         pci_conf_write(pc, tag, 0x14, 0xffffffff);
    375         mask = pci_conf_read(pc, tag, 0x14);
    376         pci_conf_write(pc, tag, 0x14, mapreg);
    377 	splx(s);
    378 	bussize = PCI_MAPREG_MEM_SIZE(mask);
    379 	busaddr = PCI_MAPREG_MEM_ADDR(mapreg);
    380 
    381 	error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024,
    382 		BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits);
    383 	if (error)
    384 		return error;
    385 
    386 	gten_common_init(ri);
    387 
    388 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    389 	wsdisplay_cnattach(&gten_stdscreen, ri, 0, 0, defattr);
    390 
    391 	gten_console_pcitag = tag;
    392 
    393 	return 0;
    394 }
    395 
    396 static int
    397 gten_getcmap(gt, cm)
    398 	struct gten_softc *gt;
    399 	struct wsdisplay_cmap *cm;
    400 {
    401 	u_int index = cm->index;
    402 	u_int count = cm->count;
    403 	int error;
    404 
    405 	if (index >= 256 || count > 256 || index + count > 256)
    406 		return EINVAL;
    407 
    408 	error = copyout(&gt->gt_cmap_red[index],   cm->red,   count);
    409 	if (error)
    410 		return error;
    411 	error = copyout(&gt->gt_cmap_green[index], cm->green, count);
    412 	if (error)
    413 		return error;
    414 	error = copyout(&gt->gt_cmap_blue[index],  cm->blue,  count);
    415 	if (error)
    416 		return error;
    417 
    418 	return 0;
    419 }
    420 
    421 static int
    422 gten_putcmap(gt, cm)
    423 	struct gten_softc *gt;
    424 	struct wsdisplay_cmap *cm;
    425 {
    426 	int index = cm->index;
    427 	int count = cm->count;
    428 	int i, error;
    429 	u_char rbuf[256], gbuf[256], bbuf[256];
    430 
    431 	if (cm->index >= 256 || cm->count > 256 ||
    432 	    (cm->index + cm->count) > 256)
    433 		return EINVAL;
    434 	error = copyin(cm->red, &rbuf[index], count);
    435 	if (error)
    436 		return error;
    437 	error = copyin(cm->green, &gbuf[index], count);
    438 	if (error)
    439 		return error;
    440 	error = copyin(cm->blue, &bbuf[index], count);
    441 	if (error)
    442 		return error;
    443 
    444 	memcpy(&gt->gt_cmap_red[index], &rbuf[index], count);
    445 	memcpy(&gt->gt_cmap_green[index], &gbuf[index], count);
    446 	memcpy(&gt->gt_cmap_blue[index], &bbuf[index], count);
    447 	return 0;
    448 }
    449