1 /* $NetBSD: gten.c,v 1.16 2007/10/17 19:56:51 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@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.16 2007/10/17 19:56:51 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 >en_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, void *, 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_memaddr, >->gt_memsize, NULL); 139 if (error) { 140 aprint_error(": can't determine memory size: error=%d\n", 141 error); 142 return; 143 } 144 if (console) { 145 gt->gt_ri = >en_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 aprint_error(": 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_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_ri->ri_bits); 165 #endif 166 if (error) { 167 aprint_error(": can't map frame buffer: error=%d\n", 168 error); 169 return; 170 } 171 172 gten_common_init(gt->gt_ri); 173 } 174 175 gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits); 176 if (gt->gt_paddr == 0) { 177 aprint_error(": cannot map framebuffer\n"); 178 return; 179 } 180 gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET; 181 182 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 183 aprint_normal(": %s\n", devinfo); 184 format_bytes(pbuf, sizeof(pbuf), gt->gt_psize); 185 aprint_normal("%s: %s, %dx%d, %dbpp\n", self->dv_xname, pbuf, 186 gt->gt_ri->ri_width, gt->gt_ri->ri_height, 187 gt->gt_ri->ri_depth); 188 #if defined(DEBUG) 189 aprint_debug("%s: text %dx%d, =+%d+%d\n", self->dv_xname, 190 gt->gt_ri->ri_cols, gt->gt_ri->ri_rows, 191 gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin); 192 193 { int i; 194 struct rasops_info *ri = gt->gt_ri; 195 for (i = 0; i < 64; i++) { 196 int j = i * ri->ri_stride; 197 int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride; 198 memset(ri->ri_bits + j, 0, 64 - i); 199 memset(ri->ri_bits + j + 64 - i, 255, i); 200 201 memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i); 202 memset(ri->ri_bits + j + ri->ri_width - 64, 255, i); 203 204 memset(ri->ri_bits + k, 0, 64 - i); 205 memset(ri->ri_bits + k + 64 - i, 255, i); 206 207 memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i); 208 memset(ri->ri_bits + k + ri->ri_width - 64, 255, i); 209 }} 210 #endif 211 212 gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0; 213 gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff; 214 gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff; 215 gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff; 216 217 a.console = console; 218 a.scrdata = >en_screenlist; 219 a.accessops = >en_accessops; 220 a.accesscookie = gt; 221 222 config_found(self, &a, wsemuldisplaydevprint); 223 } 224 225 static void 226 gten_common_init(struct rasops_info *ri) 227 { 228 int32_t addr, width, height, linebytes, depth; 229 int i, screenbytes; 230 231 /* initialize rasops */ 232 ri->ri_width = 640; 233 ri->ri_height = 480; 234 ri->ri_depth = 8; 235 ri->ri_stride = 640; 236 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR; 237 238 rasops_init(ri, 30, 80); 239 /* black on white */ 240 ri->ri_devcmap[0] = 0xffffffff; /* bg */ 241 ri->ri_devcmap[1] = 0; /* fg */ 242 243 memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height); 244 245 gten_stdscreen.nrows = ri->ri_rows; 246 gten_stdscreen.ncols = ri->ri_cols; 247 gten_stdscreen.textops = &ri->ri_ops; 248 gten_stdscreen.capabilities = ri->ri_caps; 249 } 250 251 static int 252 gten_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 253 struct proc *p) 254 { 255 struct gten_softc *gt = v; 256 struct wsdisplay_fbinfo *wdf; 257 struct grfinfo *gm; 258 259 switch (cmd) { 260 case WSDISPLAYIO_GTYPE: 261 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */ 262 return 0; 263 264 case WSDISPLAYIO_GINFO: 265 wdf = (void *)data; 266 wdf->height = gt->gt_ri->ri_height; 267 wdf->width = gt->gt_ri->ri_width; 268 wdf->depth = gt->gt_ri->ri_depth; 269 wdf->cmsize = 256; 270 return 0; 271 272 case WSDISPLAYIO_GETCMAP: 273 return gten_getcmap(gt, (struct wsdisplay_cmap *)data); 274 275 case WSDISPLAYIO_PUTCMAP: 276 return gten_putcmap(gt, (struct wsdisplay_cmap *)data); 277 } 278 return EPASSTHROUGH; 279 } 280 281 static paddr_t 282 gten_mmap(void *v, void *vs, off_t offset, int prot) 283 { 284 struct gten_softc *gt = v; 285 286 if (offset >= 0 && offset < gt->gt_psize) 287 return gt->gt_paddr + offset; 288 if (offset >= 0x1000000 && offset < gt->gt_memsize) 289 return gt->gt_memaddr + offset - 0x1000000; 290 291 return -1; 292 } 293 294 static int 295 gten_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep, 296 int *curxp, int *curyp, long *attrp) 297 { 298 struct gten_softc *gt = v; 299 struct rasops_info *ri = gt->gt_ri; 300 long defattr; 301 302 if (gt->gt_nscreens > 0) 303 return (ENOMEM); 304 305 *cookiep = ri; /* one and only for now */ 306 *curxp = 0; 307 *curyp = 0; 308 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 309 *attrp = defattr; 310 gt->gt_nscreens++; 311 return 0; 312 } 313 314 static void 315 gten_free_screen(void *v, void *cookie) 316 { 317 struct gten_softc *gt = v; 318 319 if (gt->gt_ri == >en_console_ri) 320 panic("gten_free_screen: console"); 321 322 gt->gt_nscreens--; 323 } 324 325 static int 326 gten_show_screen(void *v, void *cookie, int waitok, 327 void (*cb)(void *, int, int), void *cbarg) 328 { 329 return (0); 330 } 331 332 int 333 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt) 334 { 335 struct rasops_info *ri = >en_console_ri; 336 u_int32_t mapreg, id, mask, mapsize; 337 long defattr; 338 pcitag_t tag; 339 int s, error; 340 bus_size_t bussize; 341 bus_addr_t busaddr; 342 343 tag = pci_make_tag(pc, 0, 14, 0); 344 345 id = pci_conf_read(pc, tag, PCI_ID_REG); 346 if (PCI_VENDOR(id) != PCI_VENDOR_WD || 347 PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C) 348 return ENXIO; 349 350 mapreg = pci_conf_read(pc, tag, 0x14); 351 if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM || 352 PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT) 353 return ENXIO; 354 355 s = splhigh(); 356 pci_conf_write(pc, tag, 0x14, 0xffffffff); 357 mask = pci_conf_read(pc, tag, 0x14); 358 pci_conf_write(pc, tag, 0x14, mapreg); 359 splx(s); 360 bussize = PCI_MAPREG_MEM_SIZE(mask); 361 busaddr = PCI_MAPREG_MEM_ADDR(mapreg); 362 363 error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024, 364 BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits); 365 if (error) 366 return error; 367 368 gten_common_init(ri); 369 370 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 371 wsdisplay_cnattach(>en_stdscreen, ri, 0, 0, defattr); 372 373 gten_console_pcitag = tag; 374 375 return 0; 376 } 377 378 static int 379 gten_getcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm) 380 { 381 u_int index = cm->index; 382 u_int count = cm->count; 383 int error; 384 385 if (index >= 256 || count > 256 || index + count > 256) 386 return EINVAL; 387 388 error = copyout(>->gt_cmap_red[index], cm->red, count); 389 if (error) 390 return error; 391 error = copyout(>->gt_cmap_green[index], cm->green, count); 392 if (error) 393 return error; 394 error = copyout(>->gt_cmap_blue[index], cm->blue, count); 395 if (error) 396 return error; 397 398 return 0; 399 } 400 401 static int 402 gten_putcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm) 403 { 404 int index = cm->index; 405 int count = cm->count; 406 int i, error; 407 u_char rbuf[256], gbuf[256], bbuf[256]; 408 409 if (cm->index >= 256 || cm->count > 256 || 410 (cm->index + cm->count) > 256) 411 return EINVAL; 412 error = copyin(cm->red, &rbuf[index], count); 413 if (error) 414 return error; 415 error = copyin(cm->green, &gbuf[index], count); 416 if (error) 417 return error; 418 error = copyin(cm->blue, &bbuf[index], count); 419 if (error) 420 return error; 421 422 memcpy(>->gt_cmap_red[index], &rbuf[index], count); 423 memcpy(>->gt_cmap_green[index], &gbuf[index], count); 424 memcpy(>->gt_cmap_blue[index], &bbuf[index], count); 425 return 0; 426 } 427