1 /* $NetBSD: gten.c,v 1.8 2002/10/02 15:52:29 thorpej 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/param.h> 40 #include <sys/buf.h> 41 #include <sys/conf.h> 42 #include <sys/device.h> 43 #include <sys/ioctl.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/systm.h> 47 48 #include <uvm/uvm_extern.h> 49 50 #include <dev/pci/pcidevs.h> 51 #include <dev/pci/pcireg.h> 52 #include <dev/pci/pcivar.h> 53 54 #include <dev/wscons/wsconsio.h> 55 #include <dev/wscons/wsdisplayvar.h> 56 #include <dev/rasops/rasops.h> 57 58 #include <machine/bus.h> 59 #include <machine/gtenvar.h> 60 61 static int gten_match (struct device *, struct cfdata *, void *); 62 static void gten_attach (struct device *, struct device *, void *); 63 static int gten_print (void *, const char *); 64 65 CFATTACH_DECL(gten, sizeof(struct gten_softc), 66 gten_match, gten_attach, NULL, NULL); 67 68 static struct rasops_info gten_console_ri; 69 static pcitag_t gten_console_pcitag; 70 71 static struct wsscreen_descr gten_stdscreen = { 72 "std", 73 0, 0, 74 0, 75 0, 0, 76 WSSCREEN_REVERSE 77 }; 78 79 static const struct wsscreen_descr *_gten_scrlist[] = { 80 >en_stdscreen, 81 /* XXX other formats, graphics screen? */ 82 }; 83 84 static struct wsscreen_list gten_screenlist = { 85 sizeof(_gten_scrlist) / sizeof(struct wsscreen_descr *), _gten_scrlist 86 }; 87 88 static int gten_ioctl (void *, u_long, caddr_t, int, struct proc *); 89 static paddr_t gten_mmap (void *, off_t, int); 90 static int gten_alloc_screen (void *, const struct wsscreen_descr *, 91 void **, int *, int *, long *); 92 static void gten_free_screen (void *, void *); 93 static int gten_show_screen (void *, void *, int, 94 void (*) (void *, int, int), void *); 95 96 static struct wsdisplay_accessops gten_accessops = { 97 gten_ioctl, 98 gten_mmap, 99 gten_alloc_screen, 100 gten_free_screen, 101 gten_show_screen, 102 0 /* load_font */ 103 }; 104 105 static void gten_common_init (struct rasops_info *); 106 static int gten_getcmap (struct gten_softc *, struct wsdisplay_cmap *); 107 static int gten_putcmap (struct gten_softc *, struct wsdisplay_cmap *); 108 109 #define GTEN_VRAM_OFFSET 0xf00000 110 111 static int 112 gten_match(struct device *parent, struct cfdata *match, void *aux) 113 { 114 struct pci_attach_args *pa = aux; 115 116 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_WD && 117 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_WD_90C) 118 return 2; 119 120 return 0; 121 } 122 123 static void 124 gten_attach(struct device *parent, struct device *self, void *aux) 125 { 126 struct gten_softc *gt = (struct gten_softc *)self; 127 struct pci_attach_args *pa = aux; 128 struct wsemuldisplaydev_attach_args a; 129 int console = (pa->pa_tag == gten_console_pcitag); 130 int error; 131 char devinfo[256], pbuf[10]; 132 133 error = pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, 134 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 135 >->gt_memaddr, >->gt_memsize, NULL); 136 if (error) { 137 printf(": can't determine memory size: error=%d\n", 138 error); 139 return; 140 } 141 if (console) { 142 gt->gt_ri = >en_console_ri; 143 gt->gt_nscreens = 1; 144 } else { 145 MALLOC(gt->gt_ri, struct rasops_info *, sizeof(*gt->gt_ri), 146 M_DEVBUF, M_NOWAIT); 147 if (gt->gt_ri == NULL) { 148 printf(": can't alloc memory\n"); 149 return; 150 } 151 memset(gt->gt_ri, 0, sizeof(*gt->gt_ri)); 152 #if 0 153 error = pci_mapreg_map(pa, 0x14, 154 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 155 BUS_SPACE_MAP_LINEAR, NULL, 156 (bus_space_handle_t *) >->gt_ri->ri_bits, 157 NULL, NULL); 158 #else 159 error = bus_space_map(pa->pa_memt, gt->gt_memaddr + GTEN_VRAM_OFFSET, 160 960*1024, BUS_SPACE_MAP_LINEAR, 161 (bus_space_handle_t *) >->gt_ri->ri_bits); 162 #endif 163 if (error) { 164 printf(": can't map frame buffer: error=%d\n", error); 165 return; 166 } 167 168 gten_common_init(gt->gt_ri); 169 } 170 171 gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits); 172 if (gt->gt_paddr == 0) { 173 printf(": cannot map framebuffer\n"); 174 return; 175 } 176 gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET; 177 178 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo); 179 printf(": %s\n", devinfo); 180 format_bytes(pbuf, sizeof(pbuf), gt->gt_psize); 181 printf("%s: %s, %dx%d, %dbpp\n", self->dv_xname, pbuf, 182 gt->gt_ri->ri_width, gt->gt_ri->ri_height, 183 gt->gt_ri->ri_depth); 184 #if defined(DEBUG) 185 printf("%s: text %dx%d, =+%d+%d\n", self->dv_xname, 186 gt->gt_ri->ri_cols, gt->gt_ri->ri_rows, 187 gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin); 188 189 { int i; 190 struct rasops_info *ri = gt->gt_ri; 191 for (i = 0; i < 64; i++) { 192 int j = i * ri->ri_stride; 193 int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride; 194 memset(ri->ri_bits + j, 0, 64 - i); 195 memset(ri->ri_bits + j + 64 - i, 255, i); 196 197 memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i); 198 memset(ri->ri_bits + j + ri->ri_width - 64, 255, i); 199 200 memset(ri->ri_bits + k, 0, 64 - i); 201 memset(ri->ri_bits + k + 64 - i, 255, i); 202 203 memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i); 204 memset(ri->ri_bits + k + ri->ri_width - 64, 255, i); 205 }} 206 #endif 207 208 gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0; 209 gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff; 210 gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff; 211 gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff; 212 213 a.console = console; 214 a.scrdata = >en_screenlist; 215 a.accessops = >en_accessops; 216 a.accesscookie = gt; 217 218 config_found(self, &a, wsemuldisplaydevprint); 219 } 220 221 static void 222 gten_common_init(struct rasops_info *ri) 223 { 224 int32_t addr, width, height, linebytes, depth; 225 int i, screenbytes; 226 227 /* initialize rasops */ 228 ri->ri_width = 640; 229 ri->ri_height = 480; 230 ri->ri_depth = 8; 231 ri->ri_stride = 640; 232 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR; 233 234 rasops_init(ri, 30, 80); 235 /* black on white */ 236 ri->ri_devcmap[0] = 0xffffffff; /* bg */ 237 ri->ri_devcmap[1] = 0; /* fg */ 238 239 memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height); 240 241 gten_stdscreen.nrows = ri->ri_rows; 242 gten_stdscreen.ncols = ri->ri_cols; 243 gten_stdscreen.textops = &ri->ri_ops; 244 gten_stdscreen.capabilities = ri->ri_caps; 245 } 246 247 static int 248 gten_ioctl(v, cmd, data, flag, p) 249 void *v; 250 u_long cmd; 251 caddr_t data; 252 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(v, offset, prot) 283 void *v; 284 off_t offset; 285 int prot; 286 { 287 struct gten_softc *gt = v; 288 289 if (offset >= 0 && offset < gt->gt_psize) 290 return gt->gt_paddr + offset; 291 if (offset >= 0x1000000 && offset < gt->gt_memsize) 292 return gt->gt_memaddr + offset - 0x1000000; 293 294 return -1; 295 } 296 297 static int 298 gten_alloc_screen(v, type, cookiep, curxp, curyp, attrp) 299 void *v; 300 const struct wsscreen_descr *type; 301 void **cookiep; 302 int *curxp, *curyp; 303 long *attrp; 304 { 305 struct gten_softc *gt = v; 306 struct rasops_info *ri = gt->gt_ri; 307 long defattr; 308 309 if (gt->gt_nscreens > 0) 310 return (ENOMEM); 311 312 *cookiep = ri; /* one and only for now */ 313 *curxp = 0; 314 *curyp = 0; 315 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 316 *attrp = defattr; 317 gt->gt_nscreens++; 318 return 0; 319 } 320 321 static void 322 gten_free_screen(v, cookie) 323 void *v; 324 void *cookie; 325 { 326 struct gten_softc *gt = v; 327 328 if (gt->gt_ri == >en_console_ri) 329 panic("gten_free_screen: console"); 330 331 gt->gt_nscreens--; 332 } 333 334 static int 335 gten_show_screen(v, cookie, waitok, cb, cbarg) 336 void *v; 337 void *cookie; 338 int waitok; 339 void (*cb) (void *, int, int); 340 void *cbarg; 341 { 342 return (0); 343 } 344 345 int 346 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt) 347 { 348 struct rasops_info *ri = >en_console_ri; 349 u_int32_t mapreg, id, mask, mapsize; 350 long defattr; 351 pcitag_t tag; 352 int s, error; 353 bus_size_t bussize; 354 bus_addr_t busaddr; 355 356 tag = pci_make_tag(pc, 0, 14, 0); 357 358 id = pci_conf_read(pc, tag, PCI_ID_REG); 359 if (PCI_VENDOR(id) != PCI_VENDOR_WD || 360 PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C) 361 return ENXIO; 362 363 mapreg = pci_conf_read(pc, tag, 0x14); 364 if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM || 365 PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT) 366 return ENXIO; 367 368 s = splhigh(); 369 pci_conf_write(pc, tag, 0x14, 0xffffffff); 370 mask = pci_conf_read(pc, tag, 0x14); 371 pci_conf_write(pc, tag, 0x14, mapreg); 372 splx(s); 373 bussize = PCI_MAPREG_MEM_SIZE(mask); 374 busaddr = PCI_MAPREG_MEM_ADDR(mapreg); 375 376 error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024, 377 BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits); 378 if (error) 379 return error; 380 381 gten_common_init(ri); 382 383 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 384 wsdisplay_cnattach(>en_stdscreen, ri, 0, 0, defattr); 385 386 gten_console_pcitag = tag; 387 388 return 0; 389 } 390 391 static int 392 gten_getcmap(gt, cm) 393 struct gten_softc *gt; 394 struct wsdisplay_cmap *cm; 395 { 396 u_int index = cm->index; 397 u_int count = cm->count; 398 int error; 399 400 if (index >= 256 || count > 256 || index + count > 256) 401 return EINVAL; 402 403 error = copyout(>->gt_cmap_red[index], cm->red, count); 404 if (error) 405 return error; 406 error = copyout(>->gt_cmap_green[index], cm->green, count); 407 if (error) 408 return error; 409 error = copyout(>->gt_cmap_blue[index], cm->blue, count); 410 if (error) 411 return error; 412 413 return 0; 414 } 415 416 static int 417 gten_putcmap(gt, cm) 418 struct gten_softc *gt; 419 struct wsdisplay_cmap *cm; 420 { 421 int index = cm->index; 422 int count = cm->count; 423 int i; 424 u_char *r, *g, *b; 425 426 if (cm->index >= 256 || cm->count > 256 || 427 (cm->index + cm->count) > 256) 428 return EINVAL; 429 if (!uvm_useracc(cm->red, cm->count, B_READ) || 430 !uvm_useracc(cm->green, cm->count, B_READ) || 431 !uvm_useracc(cm->blue, cm->count, B_READ)) 432 return EFAULT; 433 copyin(cm->red, >->gt_cmap_red[index], count); 434 copyin(cm->green, >->gt_cmap_green[index], count); 435 copyin(cm->blue, >->gt_cmap_blue[index], count); 436 437 r = >->gt_cmap_red[index]; 438 g = >->gt_cmap_green[index]; 439 b = >->gt_cmap_blue[index]; 440 441 #if 0 442 for (i = 0; i < count; i++) { 443 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index); 444 r++, g++, b++, index++; 445 } 446 #endif 447 448 return 0; 449 } 450