1 /* $NetBSD: crmfb.c,v 1.33 2011/04/13 16:22:09 plunky Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> 5 * 2008 Michael Lorenz <macallan@netbsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * SGI-CRM (O2) Framebuffer driver 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.33 2011/04/13 16:22:09 plunky Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/malloc.h> 41 42 #define _SGIMIPS_BUS_DMA_PRIVATE 43 #include <machine/autoconf.h> 44 #include <machine/bus.h> 45 #include <machine/machtype.h> 46 #include <machine/vmparam.h> 47 48 #include <dev/arcbios/arcbios.h> 49 #include <dev/arcbios/arcbiosvar.h> 50 51 #include <dev/wscons/wsdisplayvar.h> 52 #include <dev/wscons/wsconsio.h> 53 #include <dev/wsfont/wsfont.h> 54 #include <dev/rasops/rasops.h> 55 #include <dev/wscons/wsdisplay_vconsvar.h> 56 57 #include <dev/i2c/i2cvar.h> 58 #include <dev/i2c/i2c_bitbang.h> 59 #include <dev/i2c/ddcvar.h> 60 #include <dev/videomode/videomode.h> 61 #include <dev/videomode/edidvar.h> 62 63 #include <arch/sgimips/dev/crmfbreg.h> 64 65 #include "opt_crmfb.h" 66 67 #ifdef CRMFB_DEBUG 68 #define DPRINTF printf 69 #else 70 #define DPRINTF while (0) printf 71 #endif 72 73 struct wsscreen_descr crmfb_defaultscreen = { 74 "default", 75 0, 0, 76 NULL, 77 8, 16, 78 WSSCREEN_WSCOLORS, 79 NULL, 80 }; 81 82 const struct wsscreen_descr *_crmfb_scrlist[] = { 83 &crmfb_defaultscreen, 84 }; 85 86 struct wsscreen_list crmfb_screenlist = { 87 sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *), 88 _crmfb_scrlist 89 }; 90 91 static struct vcons_screen crmfb_console_screen; 92 93 static int crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 94 static paddr_t crmfb_mmap(void *, void *, off_t, int); 95 static void crmfb_init_screen(void *, struct vcons_screen *, int, long *); 96 97 struct wsdisplay_accessops crmfb_accessops = { 98 crmfb_ioctl, 99 crmfb_mmap, 100 NULL, /* alloc_screen */ 101 NULL, /* free_screen */ 102 NULL, /* show_screen */ 103 NULL, /* load_font */ 104 NULL, /* pollc */ 105 NULL, /* scroll */ 106 }; 107 108 /* Memory to allocate to SGI-CRM -- remember, this is stolen from 109 * host memory! 110 */ 111 #define CRMFB_TILESIZE (512*128) 112 113 static int crmfb_match(device_t, struct cfdata *, void *); 114 static void crmfb_attach(device_t, device_t, void *); 115 int crmfb_probe(void); 116 117 #define KERNADDR(p) ((void *)((p).addr)) 118 #define DMAADDR(p) ((p).map->dm_segs[0].ds_addr) 119 120 #define CRMFB_REG_MASK(msb, lsb) \ 121 ( (((uint32_t) 1 << ((msb)-(lsb)+1)) - 1) << (lsb) ) 122 123 124 struct crmfb_dma { 125 bus_dmamap_t map; 126 void *addr; 127 bus_dma_segment_t segs[1]; 128 int nsegs; 129 size_t size; 130 }; 131 132 struct crmfb_softc { 133 device_t sc_dev; 134 struct vcons_data sc_vd; 135 struct i2c_controller sc_i2c; 136 int sc_dir; 137 138 bus_space_tag_t sc_iot; 139 bus_space_handle_t sc_ioh; 140 bus_space_handle_t sc_reh; 141 142 bus_dma_tag_t sc_dmat; 143 144 struct crmfb_dma sc_dma; 145 struct crmfb_dma sc_dmai; 146 147 int sc_width; 148 int sc_height; 149 int sc_depth; 150 int sc_tiles_x, sc_tiles_y; 151 uint32_t sc_fbsize; 152 int sc_mte_direction; 153 uint8_t *sc_scratch; 154 paddr_t sc_linear; 155 int sc_wsmode; 156 struct edid_info sc_edid_info; 157 158 /* cursor stuff */ 159 int sc_cur_x; 160 int sc_cur_y; 161 int sc_hot_x; 162 int sc_hot_y; 163 164 u_char sc_cmap_red[256]; 165 u_char sc_cmap_green[256]; 166 u_char sc_cmap_blue[256]; 167 }; 168 169 static int crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *); 170 static int crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *); 171 static void crmfb_set_palette(struct crmfb_softc *, 172 int, uint8_t, uint8_t, uint8_t); 173 static int crmfb_set_curpos(struct crmfb_softc *, int, int); 174 static int crmfb_gcursor(struct crmfb_softc *, struct wsdisplay_cursor *); 175 static int crmfb_scursor(struct crmfb_softc *, struct wsdisplay_cursor *); 176 static inline void crmfb_write_reg(struct crmfb_softc *, int, uint32_t); 177 static inline uint32_t crmfb_read_reg(struct crmfb_softc *, int); 178 static int crmfb_wait_dma_idle(struct crmfb_softc *); 179 180 /* setup video hw in given colour depth */ 181 static int crmfb_setup_video(struct crmfb_softc *, int); 182 static void crmfb_setup_palette(struct crmfb_softc *); 183 184 static void crmfb_fill_rect(struct crmfb_softc *, int, int, int, int, uint32_t); 185 static void crmfb_bitblt(struct crmfb_softc *, int, int, int, int, int, int, 186 uint32_t); 187 static void crmfb_scroll(struct crmfb_softc *, int, int, int, int, int, int); 188 189 static void crmfb_copycols(void *, int, int, int, int); 190 static void crmfb_erasecols(void *, int, int, int, long); 191 static void crmfb_copyrows(void *, int, int, int); 192 static void crmfb_eraserows(void *, int, int, long); 193 static void crmfb_cursor(void *, int, int, int); 194 static void crmfb_putchar(void *, int, int, u_int, long); 195 196 /* I2C glue */ 197 static int crmfb_i2c_acquire_bus(void *, int); 198 static void crmfb_i2c_release_bus(void *, int); 199 static int crmfb_i2c_send_start(void *, int); 200 static int crmfb_i2c_send_stop(void *, int); 201 static int crmfb_i2c_initiate_xfer(void *, i2c_addr_t, int); 202 static int crmfb_i2c_read_byte(void *, uint8_t *, int); 203 static int crmfb_i2c_write_byte(void *, uint8_t, int); 204 205 /* I2C bitbang glue */ 206 static void crmfb_i2cbb_set_bits(void *, uint32_t); 207 static void crmfb_i2cbb_set_dir(void *, uint32_t); 208 static uint32_t crmfb_i2cbb_read(void *); 209 210 static const struct i2c_bitbang_ops crmfb_i2cbb_ops = { 211 crmfb_i2cbb_set_bits, 212 crmfb_i2cbb_set_dir, 213 crmfb_i2cbb_read, 214 { 215 CRMFB_I2C_SDA, 216 CRMFB_I2C_SCL, 217 0, 218 1 219 } 220 }; 221 static void crmfb_setup_ddc(struct crmfb_softc *); 222 223 /* mode setting stuff */ 224 static uint32_t calc_pll(int); /* frequency in kHz */ 225 static int crmfb_set_mode(struct crmfb_softc *, const struct videomode *); 226 227 CFATTACH_DECL_NEW(crmfb, sizeof(struct crmfb_softc), 228 crmfb_match, crmfb_attach, NULL, NULL); 229 230 static int 231 crmfb_match(device_t parent, struct cfdata *cf, void *opaque) 232 { 233 return crmfb_probe(); 234 } 235 236 static void 237 crmfb_attach(device_t parent, device_t self, void *opaque) 238 { 239 struct mainbus_attach_args *ma; 240 struct crmfb_softc *sc; 241 struct rasops_info *ri; 242 struct wsemuldisplaydev_attach_args aa; 243 uint32_t d, h; 244 uint16_t *p; 245 unsigned long v; 246 long defattr; 247 const char *consdev; 248 int rv, i; 249 250 sc = device_private(self); 251 sc->sc_dev = self; 252 253 ma = (struct mainbus_attach_args *)opaque; 254 255 sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME; 256 sc->sc_dmat = &sgimips_default_bus_dma_tag; 257 sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL; 258 259 aprint_normal(": SGI CRIME Graphics Display Engine\n"); 260 rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */, 261 BUS_SPACE_MAP_LINEAR, &sc->sc_ioh); 262 if (rv) 263 panic("crmfb_attach: can't map I/O space"); 264 rv = bus_space_map(sc->sc_iot, 0x15000000, 0x6000, 0, &sc->sc_reh); 265 if (rv) 266 panic("crmfb_attach: can't map rendering engine"); 267 268 /* determine mode configured by firmware */ 269 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP); 270 sc->sc_width = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff; 271 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP); 272 sc->sc_height = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff; 273 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE); 274 h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3; 275 if (h == 0) 276 sc->sc_depth = 8; 277 else if (h == 1) 278 sc->sc_depth = 16; 279 else 280 sc->sc_depth = 32; 281 282 if (sc->sc_width == 0 || sc->sc_height == 0) { 283 aprint_error_dev(sc->sc_dev, 284 "device unusable if not setup by firmware\n"); 285 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0 /* XXX */); 286 return; 287 } 288 289 aprint_normal_dev(sc->sc_dev, "initial resolution %dx%d\n", 290 sc->sc_width, sc->sc_height); 291 292 crmfb_setup_ddc(sc); 293 if ((sc->sc_edid_info.edid_preferred_mode != NULL)) { 294 if (crmfb_set_mode(sc, sc->sc_edid_info.edid_preferred_mode)) 295 aprint_normal_dev(sc->sc_dev, "using %dx%d\n", 296 sc->sc_width, sc->sc_height); 297 } 298 /* 299 * first determine how many tiles we need 300 * in 32bit each tile is 128x128 pixels 301 */ 302 sc->sc_tiles_x = (sc->sc_width + 127) >> 7; 303 sc->sc_tiles_y = (sc->sc_height + 127) >> 7; 304 sc->sc_fbsize = 0x10000 * sc->sc_tiles_x * sc->sc_tiles_y; 305 306 sc->sc_dmai.size = 256 * sizeof(uint16_t); 307 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0, 308 sc->sc_dmai.segs, 309 sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]), 310 &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT); 311 if (rv) 312 panic("crmfb_attach: can't allocate DMA memory"); 313 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs, 314 sc->sc_dmai.size, &sc->sc_dmai.addr, 315 BUS_DMA_NOWAIT); 316 if (rv) 317 panic("crmfb_attach: can't map DMA memory"); 318 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1, 319 sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map); 320 if (rv) 321 panic("crmfb_attach: can't create DMA map"); 322 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr, 323 sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT); 324 if (rv) 325 panic("crmfb_attach: can't load DMA map"); 326 327 /* allocate an extra 64Kb for a linear buffer */ 328 sc->sc_dma.size = 0x10000 * (16 * sc->sc_tiles_x + 1); 329 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0, 330 sc->sc_dma.segs, 331 sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]), 332 &sc->sc_dma.nsegs, BUS_DMA_NOWAIT); 333 if (rv) 334 panic("crmfb_attach: can't allocate DMA memory"); 335 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs, 336 sc->sc_dma.size, &sc->sc_dma.addr, 337 BUS_DMA_NOWAIT | BUS_DMA_COHERENT); 338 if (rv) 339 panic("crmfb_attach: can't map DMA memory"); 340 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1, 341 sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map); 342 if (rv) 343 panic("crmfb_attach: can't create DMA map"); 344 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr, 345 sc->sc_dma.size, NULL, BUS_DMA_NOWAIT); 346 if (rv) 347 panic("crmfb_attach: can't load DMA map"); 348 349 p = KERNADDR(sc->sc_dmai); 350 v = (unsigned long)DMAADDR(sc->sc_dma); 351 for (i = 0; i < (sc->sc_tiles_x * sc->sc_tiles_y); i++) { 352 p[i] = ((uint32_t)v >> 16) + i; 353 } 354 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmai.map, 0, sc->sc_dmai.size, 355 BUS_DMASYNC_PREWRITE); 356 sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * sc->sc_tiles_x); 357 sc->sc_linear = (paddr_t)DMAADDR(sc->sc_dma) + 0x100000 * sc->sc_tiles_x; 358 359 aprint_normal_dev(sc->sc_dev, "allocated %d byte fb @ %p (%p)\n", 360 sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma)); 361 362 crmfb_setup_video(sc, 8); 363 ri = &crmfb_console_screen.scr_ri; 364 memset(ri, 0, sizeof(struct rasops_info)); 365 366 vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops); 367 sc->sc_vd.init_screen = crmfb_init_screen; 368 crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 369 vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr); 370 371 crmfb_defaultscreen.ncols = ri->ri_cols; 372 crmfb_defaultscreen.nrows = ri->ri_rows; 373 crmfb_defaultscreen.textops = &ri->ri_ops; 374 crmfb_defaultscreen.capabilities = ri->ri_caps; 375 crmfb_defaultscreen.modecookie = NULL; 376 377 crmfb_setup_palette(sc); 378 crmfb_fill_rect(sc, 0, 0, sc->sc_width, sc->sc_height, 379 ri->ri_devcmap[(defattr >> 16) & 0xff]); 380 381 consdev = arcbios_GetEnvironmentVariable("ConsoleOut"); 382 if (consdev != NULL && strcmp(consdev, "video()") == 0) { 383 wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr); 384 vcons_replay_msgbuf(&crmfb_console_screen); 385 aa.console = 1; 386 } else 387 aa.console = 0; 388 aa.scrdata = &crmfb_screenlist; 389 aa.accessops = &crmfb_accessops; 390 aa.accesscookie = &sc->sc_vd; 391 392 config_found(self, &aa, wsemuldisplaydevprint); 393 394 sc->sc_cur_x = 0; 395 sc->sc_cur_y = 0; 396 sc->sc_hot_x = 0; 397 sc->sc_hot_y = 0; 398 399 return; 400 } 401 402 int 403 crmfb_probe(void) 404 { 405 406 if (mach_type != MACH_SGI_IP32) 407 return 0; 408 409 return 1; 410 } 411 412 static int 413 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l) 414 { 415 struct vcons_data *vd; 416 struct crmfb_softc *sc; 417 struct vcons_screen *ms; 418 struct wsdisplay_fbinfo *wdf; 419 int nmode; 420 421 vd = (struct vcons_data *)v; 422 sc = (struct crmfb_softc *)vd->cookie; 423 ms = (struct vcons_screen *)vd->active; 424 425 switch (cmd) { 426 case WSDISPLAYIO_GTYPE: 427 /* not really, but who cares? */ 428 /* wsfb does */ 429 *(u_int *)data = WSDISPLAY_TYPE_CRIME; 430 return 0; 431 case WSDISPLAYIO_GINFO: 432 if (vd->active != NULL) { 433 wdf = (void *)data; 434 wdf->height = sc->sc_height; 435 wdf->width = sc->sc_width; 436 wdf->depth = 32; 437 wdf->cmsize = 256; 438 return 0; 439 } else 440 return ENODEV; 441 case WSDISPLAYIO_GETCMAP: 442 if (sc->sc_depth == 8) 443 return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data); 444 else 445 return EINVAL; 446 case WSDISPLAYIO_PUTCMAP: 447 if (sc->sc_depth == 8) 448 return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data); 449 else 450 return EINVAL; 451 case WSDISPLAYIO_LINEBYTES: 452 *(u_int *)data = sc->sc_width * sc->sc_depth / 8; 453 return 0; 454 case WSDISPLAYIO_SMODE: 455 nmode = *(int *)data; 456 if (nmode != sc->sc_wsmode) { 457 sc->sc_wsmode = nmode; 458 if (nmode == WSDISPLAYIO_MODE_EMUL) { 459 crmfb_setup_video(sc, 8); 460 crmfb_setup_palette(sc); 461 vcons_redraw_screen(vd->active); 462 } else { 463 crmfb_setup_video(sc, 32); 464 } 465 } 466 return 0; 467 case WSDISPLAYIO_SVIDEO: 468 case WSDISPLAYIO_GVIDEO: 469 return ENODEV; /* not supported yet */ 470 471 case WSDISPLAYIO_GCURPOS: 472 { 473 struct wsdisplay_curpos *pos; 474 475 pos = (struct wsdisplay_curpos *)data; 476 pos->x = sc->sc_cur_x; 477 pos->y = sc->sc_cur_y; 478 } 479 return 0; 480 case WSDISPLAYIO_SCURPOS: 481 { 482 struct wsdisplay_curpos *pos; 483 484 pos = (struct wsdisplay_curpos *)data; 485 crmfb_set_curpos(sc, pos->x, pos->y); 486 } 487 return 0; 488 case WSDISPLAYIO_GCURMAX: 489 { 490 struct wsdisplay_curpos *pos; 491 492 pos = (struct wsdisplay_curpos *)data; 493 pos->x = 32; 494 pos->y = 32; 495 } 496 return 0; 497 case WSDISPLAYIO_GCURSOR: 498 { 499 struct wsdisplay_cursor *cu; 500 501 cu = (struct wsdisplay_cursor *)data; 502 return crmfb_gcursor(sc, cu); 503 } 504 case WSDISPLAYIO_SCURSOR: 505 { 506 struct wsdisplay_cursor *cu; 507 508 cu = (struct wsdisplay_cursor *)data; 509 return crmfb_scursor(sc, cu); 510 } 511 } 512 return EPASSTHROUGH; 513 } 514 515 static paddr_t 516 crmfb_mmap(void *v, void *vs, off_t offset, int prot) 517 { 518 struct vcons_data *vd; 519 struct crmfb_softc *sc; 520 paddr_t pa; 521 522 vd = (struct vcons_data *)v; 523 sc = (struct crmfb_softc *)vd->cookie; 524 525 /* we probably shouldn't let anyone mmap the framebuffer */ 526 #if 1 527 if (offset >= 0 && offset < (0x100000 * sc->sc_tiles_x)) { 528 pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs, 529 sc->sc_dma.nsegs, offset, prot, 530 BUS_DMA_WAITOK | BUS_DMA_COHERENT); 531 return pa; 532 } 533 #endif 534 /* 535 * here would the TLBs be but we don't want to show them to userland 536 * so we return the page containing the status register 537 */ 538 if ((offset >= 0x15000000) && (offset < 0x15002000)) 539 return bus_space_mmap(sc->sc_iot, 0x15004000, 0, prot, 0); 540 /* now the actual engine registers */ 541 if ((offset >= 0x15002000) && (offset < 0x15005000)) 542 return bus_space_mmap(sc->sc_iot, offset, 0, prot, 0); 543 /* and now the scratch area */ 544 if ((offset >= 0x15010000) && (offset < 0x15020000)) 545 return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs, 546 sc->sc_dma.nsegs, 547 offset + (0x100000 * sc->sc_tiles_x) - 0x15010000, 548 prot, BUS_DMA_WAITOK | BUS_DMA_COHERENT); 549 return -1; 550 } 551 552 static void 553 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing, 554 long *defattr) 555 { 556 struct crmfb_softc *sc; 557 struct rasops_info *ri; 558 559 sc = (struct crmfb_softc *)c; 560 ri = &scr->scr_ri; 561 562 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 563 ri->ri_depth = sc->sc_depth; 564 ri->ri_width = sc->sc_width; 565 ri->ri_height = sc->sc_height; 566 ri->ri_stride = ri->ri_width * (ri->ri_depth / 8); 567 568 switch (ri->ri_depth) { 569 case 16: 570 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5; 571 ri->ri_rpos = 10; 572 ri->ri_gpos = 5; 573 ri->ri_bpos = 0; 574 break; 575 case 32: 576 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8; 577 ri->ri_rpos = 8; 578 ri->ri_gpos = 16; 579 ri->ri_bpos = 24; 580 break; 581 } 582 583 ri->ri_bits = KERNADDR(sc->sc_dma); 584 585 if (existing) 586 ri->ri_flg |= RI_CLEAR; 587 588 rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8); 589 ri->ri_caps = WSSCREEN_WSCOLORS; 590 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight, 591 ri->ri_width / ri->ri_font->fontwidth); 592 ri->ri_hw = scr; 593 594 ri->ri_ops.cursor = crmfb_cursor; 595 ri->ri_ops.copyrows = crmfb_copyrows; 596 ri->ri_ops.eraserows = crmfb_eraserows; 597 ri->ri_ops.copycols = crmfb_copycols; 598 ri->ri_ops.erasecols = crmfb_erasecols; 599 ri->ri_ops.putchar = crmfb_putchar; 600 601 return; 602 } 603 604 static int 605 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm) 606 { 607 u_int idx, cnt; 608 u_char r[256], g[256], b[256]; 609 u_char *rp, *gp, *bp; 610 int rv, i; 611 612 idx = cm->index; 613 cnt = cm->count; 614 615 if (idx >= 255 || cnt > 256 || idx + cnt > 256) 616 return EINVAL; 617 618 rv = copyin(cm->red, &r[idx], cnt); 619 if (rv) 620 return rv; 621 rv = copyin(cm->green, &g[idx], cnt); 622 if (rv) 623 return rv; 624 rv = copyin(cm->blue, &b[idx], cnt); 625 if (rv) 626 return rv; 627 628 memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt); 629 memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt); 630 memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt); 631 632 rp = &sc->sc_cmap_red[idx]; 633 gp = &sc->sc_cmap_green[idx]; 634 bp = &sc->sc_cmap_blue[idx]; 635 636 for (i = 0; i < cnt; i++) { 637 crmfb_set_palette(sc, idx, *rp, *gp, *bp); 638 idx++; 639 rp++, gp++, bp++; 640 } 641 642 return 0; 643 } 644 645 static int 646 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm) 647 { 648 u_int idx, cnt; 649 int rv; 650 651 idx = cm->index; 652 cnt = cm->count; 653 654 if (idx >= 255 || cnt > 256 || idx + cnt > 256) 655 return EINVAL; 656 657 rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt); 658 if (rv) 659 return rv; 660 rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt); 661 if (rv) 662 return rv; 663 rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt); 664 if (rv) 665 return rv; 666 667 return 0; 668 } 669 670 static void 671 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g, 672 uint8_t b) 673 { 674 uint32_t val; 675 676 if (reg > 255 || sc->sc_depth != 8) 677 return; 678 679 while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63) 680 DELAY(10); 681 682 val = (r << 8) | (g << 16) | (b << 24); 683 crmfb_write_reg(sc, CRMFB_CMAP + (reg * 4), val); 684 685 return; 686 } 687 688 static int 689 crmfb_set_curpos(struct crmfb_softc *sc, int x, int y) 690 { 691 uint32_t val; 692 693 sc->sc_cur_x = x; 694 sc->sc_cur_y = y; 695 696 val = ((x - sc->sc_hot_x) & 0xffff) | ((y - sc->sc_hot_y) << 16); 697 crmfb_write_reg(sc, CRMFB_CURSOR_POS, val); 698 699 return 0; 700 } 701 702 static int 703 crmfb_gcursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur) 704 { 705 /* do nothing for now */ 706 return 0; 707 } 708 709 static int 710 crmfb_scursor(struct crmfb_softc *sc, struct wsdisplay_cursor *cur) 711 { 712 if (cur->which & WSDISPLAY_CURSOR_DOCUR) { 713 714 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, cur->enable ? 1 : 0); 715 } 716 if (cur->which & WSDISPLAY_CURSOR_DOHOT) { 717 718 sc->sc_hot_x = cur->hot.x; 719 sc->sc_hot_y = cur->hot.y; 720 } 721 if (cur->which & WSDISPLAY_CURSOR_DOPOS) { 722 723 crmfb_set_curpos(sc, cur->pos.x, cur->pos.y); 724 } 725 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) { 726 int i; 727 uint32_t val; 728 729 for (i = 0; i < cur->cmap.count; i++) { 730 val = (cur->cmap.red[i] << 24) | 731 (cur->cmap.green[i] << 16) | 732 (cur->cmap.blue[i] << 8); 733 crmfb_write_reg(sc, CRMFB_CURSOR_CMAP0 + 734 ((i + cur->cmap.index) << 2), val); 735 } 736 } 737 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) { 738 739 int i, j, cnt = 0; 740 uint32_t latch = 0, omask; 741 uint8_t imask; 742 for (i = 0; i < 64; i++) { 743 omask = 0x80000000; 744 imask = 0x01; 745 cur->image[cnt] &= cur->mask[cnt]; 746 for (j = 0; j < 8; j++) { 747 if (cur->image[cnt] & imask) 748 latch |= omask; 749 omask >>= 1; 750 if (cur->mask[cnt] & imask) 751 latch |= omask; 752 omask >>= 1; 753 imask <<= 1; 754 } 755 cnt++; 756 imask = 0x01; 757 cur->image[cnt] &= cur->mask[cnt]; 758 for (j = 0; j < 8; j++) { 759 if (cur->image[cnt] & imask) 760 latch |= omask; 761 omask >>= 1; 762 if (cur->mask[cnt] & imask) 763 latch |= omask; 764 omask >>= 1; 765 imask <<= 1; 766 } 767 cnt++; 768 crmfb_write_reg(sc, CRMFB_CURSOR_BITMAP + (i << 2), 769 latch); 770 latch = 0; 771 } 772 } 773 return 0; 774 } 775 776 static inline void 777 crmfb_write_reg(struct crmfb_softc *sc, int offset, uint32_t val) 778 { 779 780 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val); 781 wbflush(); 782 } 783 784 static inline uint32_t 785 crmfb_read_reg(struct crmfb_softc *sc, int offset) 786 { 787 788 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset); 789 } 790 791 static int 792 crmfb_wait_dma_idle(struct crmfb_softc *sc) 793 { 794 int bail = 100000, idle; 795 796 do { 797 idle = ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, 798 CRMFB_OVR_CONTROL) & 1) == 0) && 799 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, 800 CRMFB_FRM_CONTROL) & 1) == 0) && 801 ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, 802 CRMFB_DID_CONTROL) & 1) == 0); 803 if (!idle) 804 delay(10); 805 bail--; 806 } while ((!idle) && (bail > 0)); 807 return idle; 808 } 809 810 static int 811 crmfb_setup_video(struct crmfb_softc *sc, int depth) 812 { 813 uint64_t reg; 814 uint32_t d, h, mode, page; 815 int i, bail, tile_width, tlbptr, lptr, j, tx, shift, overhang; 816 const char *wantsync; 817 uint16_t v; 818 819 /* disable DMA */ 820 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL); 821 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT); 822 crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d); 823 DELAY(50000); 824 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL); 825 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT); 826 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d); 827 DELAY(50000); 828 crmfb_write_reg(sc, CRMFB_DID_CONTROL, d); 829 DELAY(50000); 830 831 if (!crmfb_wait_dma_idle(sc)) 832 aprint_error("crmfb: crmfb_wait_dma_idle timed out\n"); 833 834 /* ensure that CRM starts drawing at the top left of the screen 835 * when we re-enable DMA later 836 */ 837 d = (1 << CRMFB_VT_XY_FREEZE_SHIFT); 838 crmfb_write_reg(sc, CRMFB_VT_XY, d); 839 delay(1000); 840 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK); 841 d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT); 842 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d); 843 844 /* wait for dotclock to turn off */ 845 bail = 10000; 846 while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK) & 847 (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT)) && (bail > 0)) { 848 delay(10); 849 bail--; 850 } 851 852 /* reset FIFO */ 853 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE); 854 d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT); 855 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d); 856 d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT); 857 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d); 858 859 /* setup colour mode */ 860 switch (depth) { 861 case 8: 862 h = CRMFB_MODE_TYP_I8; 863 tile_width = 512; 864 break; 865 case 16: 866 h = CRMFB_MODE_TYP_ARGB5; 867 tile_width = 256; 868 break; 869 case 32: 870 h = CRMFB_MODE_TYP_RGB8; 871 tile_width = 128; 872 break; 873 default: 874 panic("Unsupported depth"); 875 } 876 d = h << CRMFB_MODE_TYP_SHIFT; 877 d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT; 878 for (i = 0; i < (32 * 4); i += 4) 879 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d); 880 wbflush(); 881 882 /* setup tile pointer, but don't turn on DMA yet! */ 883 h = DMAADDR(sc->sc_dmai); 884 d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT; 885 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d); 886 887 /* init framebuffer width and pixel size */ 888 /*d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);*/ 889 890 d = ((int)(sc->sc_width / tile_width)) << 891 CRMFB_FRM_TILESIZE_WIDTH_SHIFT; 892 overhang = sc->sc_width % tile_width; 893 if (overhang != 0) { 894 uint32_t val; 895 DPRINTF("tile width: %d\n", tile_width); 896 DPRINTF("overhang: %d\n", overhang); 897 val = (overhang * (depth >> 3)) >> 5; 898 DPRINTF("reg: %08x\n", val); 899 d |= (val & 0x1f); 900 DPRINTF("d: %08x\n", d); 901 } 902 903 switch (depth) { 904 case 8: 905 h = CRMFB_FRM_TILESIZE_DEPTH_8; 906 break; 907 case 16: 908 h = CRMFB_FRM_TILESIZE_DEPTH_16; 909 break; 910 case 32: 911 h = CRMFB_FRM_TILESIZE_DEPTH_32; 912 break; 913 default: 914 panic("Unsupported depth"); 915 } 916 d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT); 917 crmfb_write_reg(sc, CRMFB_FRM_TILESIZE, d); 918 919 /*h = sc->sc_width * sc->sc_height / (512 / (depth >> 3));*/ 920 h = sc->sc_height; 921 d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT; 922 crmfb_write_reg(sc, CRMFB_FRM_PIXSIZE, d); 923 924 /* turn off firmware overlay and hardware cursor */ 925 crmfb_write_reg(sc, CRMFB_OVR_WIDTH_TILE, 0); 926 crmfb_write_reg(sc, CRMFB_CURSOR_CONTROL, 0); 927 928 /* turn on DMA for the framebuffer */ 929 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL); 930 d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT); 931 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d); 932 933 /* enable drawing again */ 934 crmfb_write_reg(sc, CRMFB_VT_XY, 0); 935 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK); 936 d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT); 937 crmfb_write_reg(sc, CRMFB_DOTCLOCK, d); 938 939 /* turn off sync-on-green */ 940 941 wantsync = arcbios_GetEnvironmentVariable("SyncOnGreen"); 942 if ( (wantsync != NULL) && (wantsync[0] == 'n') ) { 943 d = ( 1 << CRMFB_VT_FLAGS_SYNC_LOW_LSB) & 944 CRMFB_REG_MASK(CRMFB_VT_FLAGS_SYNC_LOW_MSB, 945 CRMFB_VT_FLAGS_SYNC_LOW_LSB); 946 crmfb_write_reg(sc, CRMFB_VT_FLAGS, d); 947 } 948 949 sc->sc_depth = depth; 950 951 /* finally set up the drawing engine's TLB A */ 952 v = (DMAADDR(sc->sc_dma) >> 16) & 0xffff; 953 tlbptr = 0; 954 tx = ((sc->sc_width + (tile_width - 1)) & ~(tile_width - 1)) / 955 tile_width; 956 957 DPRINTF("tx: %d\n", tx); 958 959 for (i = 0; i < 16; i++) { 960 reg = 0; 961 shift = 64; 962 lptr = 0; 963 for (j = 0; j < tx; j++) { 964 shift -= 16; 965 reg |= (((uint64_t)(v | 0x8000)) << shift); 966 if (shift == 0) { 967 shift = 64; 968 bus_space_write_8(sc->sc_iot, sc->sc_reh, 969 CRIME_RE_TLB_A + tlbptr + lptr, 970 reg); 971 DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg); 972 reg = 0; 973 lptr += 8; 974 } 975 v++; 976 } 977 if (shift != 64) { 978 bus_space_write_8(sc->sc_iot, sc->sc_reh, 979 CRIME_RE_TLB_A + tlbptr + lptr, reg); 980 DPRINTF("%04x: %016"PRIx64"\n", tlbptr + lptr, reg); 981 } 982 tlbptr += 32; 983 } 984 sc->sc_scratch = (char *)KERNADDR(sc->sc_dma) + (0xf0000 * tx); 985 986 /* now put the last 64kB into the 1st linear TLB */ 987 page = (sc->sc_linear >> 12) | 0x80000000; 988 tlbptr = 0; 989 for (i = 0; i < 8; i++) { 990 reg = ((uint64_t)page << 32) | (page + 1); 991 bus_space_write_8(sc->sc_iot, sc->sc_reh, 992 CRIME_RE_LINEAR_A + tlbptr, reg); 993 page += 2; 994 tlbptr += 8; 995 } 996 wbflush(); 997 998 /* do some very basic engine setup */ 999 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_CLIPMODE, 0); 1000 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_SRC, 0); 1001 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_WINOFFSET_DST, 0); 1002 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PLANEMASK, 1003 0xffffffff); 1004 1005 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x20, 0); 1006 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x28, 0); 1007 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x30, 0); 1008 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x38, 0); 1009 bus_space_write_8(sc->sc_iot, sc->sc_reh, 0x40, 0); 1010 1011 switch (depth) { 1012 case 8: 1013 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_8 | 1014 DE_MODE_TYPE_CI | DE_MODE_PIXDEPTH_8; 1015 break; 1016 case 16: 1017 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_16 | 1018 DE_MODE_TYPE_RGB | DE_MODE_PIXDEPTH_16; 1019 break; 1020 case 32: 1021 mode = DE_MODE_TLB_A | DE_MODE_BUFDEPTH_32 | 1022 DE_MODE_TYPE_RGBA | DE_MODE_PIXDEPTH_32; 1023 break; 1024 default: 1025 panic("%s: unsuported colour depth %d\n", __func__, 1026 depth); 1027 } 1028 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_DST, mode); 1029 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_MODE_SRC, mode); 1030 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_X, 1); 1031 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_STEP_Y, 1); 1032 1033 /* initialize memory transfer engine */ 1034 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE, 1035 MTE_MODE_DST_ECC | 1036 (MTE_TLB_A << MTE_DST_TLB_SHIFT) | 1037 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) | 1038 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) | 1039 MTE_MODE_COPY); 1040 sc->sc_mte_direction = 1; 1041 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, 1); 1042 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, 1); 1043 1044 return 0; 1045 } 1046 1047 static void 1048 crmfb_set_mte_direction(struct crmfb_softc *sc, int dir) 1049 { 1050 if (dir == sc->sc_mte_direction) 1051 return; 1052 1053 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST_Y_STEP, dir); 1054 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC_Y_STEP, dir); 1055 sc->sc_mte_direction = dir; 1056 } 1057 1058 static void 1059 crmfb_setup_palette(struct crmfb_softc *sc) 1060 { 1061 int i; 1062 1063 for (i = 0; i < 256; i++) { 1064 crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2], 1065 rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]); 1066 sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2]; 1067 sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1]; 1068 sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0]; 1069 } 1070 } 1071 1072 static inline void 1073 crmfb_wait_idle(struct crmfb_softc *sc) 1074 { 1075 int i = 0; 1076 1077 do { 1078 i++; 1079 } while (((bus_space_read_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STATUS) & 1080 CRIME_DE_IDLE) == 0) && (i < 100000000)); 1081 if (i >= 100000000) 1082 aprint_error("crmfb_wait_idle() timed out\n"); 1083 } 1084 1085 static void 1086 crmfb_fill_rect(struct crmfb_softc *sc, int x, int y, int width, int height, 1087 uint32_t colour) 1088 { 1089 crmfb_wait_idle(sc); 1090 crmfb_set_mte_direction(sc, 1); 1091 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE, 1092 MTE_MODE_DST_ECC | 1093 (MTE_TLB_A << MTE_DST_TLB_SHIFT) | 1094 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) | 1095 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) | 1096 0); 1097 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_BG, colour); 1098 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST0, 1099 (x << 16) | (y & 0xffff)); 1100 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1101 CRIME_MTE_DST1 | CRIME_DE_START, 1102 ((x + width - 1) << 16) | ((y + height - 1) & 0xffff)); 1103 } 1104 1105 static void 1106 crmfb_bitblt(struct crmfb_softc *sc, int xs, int ys, int xd, int yd, 1107 int wi, int he, uint32_t rop) 1108 { 1109 uint32_t prim = DE_PRIM_RECTANGLE; 1110 int rxa, rya, rxe, rye, rxs, rys; 1111 crmfb_wait_idle(sc); 1112 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE, 1113 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | DE_DRAWMODE_ROP | 1114 DE_DRAWMODE_XFER_EN); 1115 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, rop); 1116 if (xs < xd) { 1117 prim |= DE_PRIM_RL; 1118 rxe = xd; 1119 rxa = xd + wi - 1; 1120 rxs = xs + wi - 1; 1121 } else { 1122 prim |= DE_PRIM_LR; 1123 rxe = xd + wi - 1; 1124 rxa = xd; 1125 rxs = xs; 1126 } 1127 if (ys < yd) { 1128 prim |= DE_PRIM_BT; 1129 rye = yd; 1130 rya = yd + he - 1; 1131 rys = ys + he - 1; 1132 } else { 1133 prim |= DE_PRIM_TB; 1134 rye = yd + he - 1; 1135 rya = yd; 1136 rys = ys; 1137 } 1138 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, prim); 1139 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_XFER_ADDR_SRC, 1140 (rxs << 16) | (rys & 0xffff)); 1141 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_X_VERTEX_0, 1142 (rxa << 16) | (rya & 0xffff)); 1143 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1144 CRIME_DE_X_VERTEX_1 | CRIME_DE_START, 1145 (rxe << 16) | (rye & 0xffff)); 1146 } 1147 1148 static void 1149 crmfb_scroll(struct crmfb_softc *sc, int xs, int ys, int xd, int yd, 1150 int wi, int he) 1151 { 1152 int rxa, rya, rxe, rye, rxd, ryd, rxde, ryde; 1153 1154 rxa = xs; 1155 rxe = xs + wi - 1; 1156 rxd = xd; 1157 rxde = xd + wi - 1; 1158 1159 crmfb_wait_idle(sc); 1160 1161 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_MODE, 1162 MTE_MODE_DST_ECC | 1163 (MTE_TLB_A << MTE_DST_TLB_SHIFT) | 1164 (MTE_TLB_A << MTE_SRC_TLB_SHIFT) | 1165 (MTE_DEPTH_8 << MTE_DEPTH_SHIFT) | 1166 MTE_MODE_COPY); 1167 1168 if (ys < yd) { 1169 /* bottom to top */ 1170 rye = ys; 1171 rya = ys + he - 1; 1172 ryd = yd + he - 1; 1173 ryde = yd; 1174 crmfb_set_mte_direction(sc, -1); 1175 } else { 1176 /* top to bottom */ 1177 rye = ys + he - 1; 1178 rya = ys; 1179 ryd = yd; 1180 ryde = yd + he - 1; 1181 crmfb_set_mte_direction(sc, 1); 1182 } 1183 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC0, 1184 (rxa << 16) | rya); 1185 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_SRC1, 1186 (rxe << 16) | rye); 1187 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1188 CRIME_MTE_DST0, 1189 (rxd << 16) | ryd); 1190 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_MTE_DST1 | 1191 CRIME_DE_START, 1192 (rxde << 16) | ryde); 1193 } 1194 1195 static void 1196 crmfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 1197 { 1198 struct rasops_info *ri = cookie; 1199 struct vcons_screen *scr = ri->ri_hw; 1200 int32_t xs, xd, y, width, height; 1201 1202 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; 1203 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; 1204 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1205 width = ri->ri_font->fontwidth * ncols; 1206 height = ri->ri_font->fontheight; 1207 crmfb_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, 3); 1208 } 1209 1210 static void 1211 crmfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr) 1212 { 1213 struct rasops_info *ri = cookie; 1214 struct vcons_screen *scr = ri->ri_hw; 1215 int32_t x, y, width, height, bg; 1216 1217 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; 1218 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1219 width = ri->ri_font->fontwidth * ncols; 1220 height = ri->ri_font->fontheight; 1221 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff]; 1222 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg); 1223 } 1224 1225 static void 1226 crmfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 1227 { 1228 struct rasops_info *ri = cookie; 1229 struct vcons_screen *scr = ri->ri_hw; 1230 int32_t x, ys, yd, width, height; 1231 1232 x = ri->ri_xorigin; 1233 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; 1234 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; 1235 width = ri->ri_emuwidth; 1236 height = ri->ri_font->fontheight * nrows; 1237 1238 crmfb_scroll(scr->scr_cookie, x, ys, x, yd, width, height); 1239 } 1240 1241 static void 1242 crmfb_eraserows(void *cookie, int row, int nrows, long fillattr) 1243 { 1244 struct rasops_info *ri = cookie; 1245 struct vcons_screen *scr = ri->ri_hw; 1246 int32_t x, y, width, height, bg; 1247 1248 if ((row == 0) && (nrows == ri->ri_rows)) { 1249 x = y = 0; 1250 width = ri->ri_width; 1251 height = ri->ri_height; 1252 } else { 1253 x = ri->ri_xorigin; 1254 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1255 width = ri->ri_emuwidth; 1256 height = ri->ri_font->fontheight * nrows; 1257 } 1258 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff]; 1259 crmfb_fill_rect(scr->scr_cookie, x, y, width, height, bg); 1260 } 1261 1262 static void 1263 crmfb_cursor(void *cookie, int on, int row, int col) 1264 { 1265 struct rasops_info *ri = cookie; 1266 struct vcons_screen *scr = ri->ri_hw; 1267 struct crmfb_softc *sc = scr->scr_cookie; 1268 int x, y, wi,he; 1269 1270 wi = ri->ri_font->fontwidth; 1271 he = ri->ri_font->fontheight; 1272 1273 if (ri->ri_flg & RI_CURSOR) { 1274 x = ri->ri_ccol * wi + ri->ri_xorigin; 1275 y = ri->ri_crow * he + ri->ri_yorigin; 1276 crmfb_bitblt(sc, x, y, x, y, wi, he, 12); 1277 ri->ri_flg &= ~RI_CURSOR; 1278 } 1279 1280 ri->ri_crow = row; 1281 ri->ri_ccol = col; 1282 1283 if (on) 1284 { 1285 x = ri->ri_ccol * wi + ri->ri_xorigin; 1286 y = ri->ri_crow * he + ri->ri_yorigin; 1287 crmfb_bitblt(sc, x, y, x, y, wi, he, 12); 1288 ri->ri_flg |= RI_CURSOR; 1289 } 1290 } 1291 1292 static void 1293 crmfb_putchar(void *cookie, int row, int col, u_int c, long attr) 1294 { 1295 struct rasops_info *ri = cookie; 1296 struct vcons_screen *scr = ri->ri_hw; 1297 struct crmfb_softc *sc = scr->scr_cookie; 1298 struct wsdisplay_font *font = PICK_FONT(ri, c); 1299 uint32_t bg, fg; 1300 int x, y, wi, he, i, uc; 1301 uint8_t *fd8; 1302 uint16_t *fd16; 1303 void *fd; 1304 1305 wi = font->fontwidth; 1306 he = font->fontheight; 1307 1308 x = ri->ri_xorigin + col * wi; 1309 y = ri->ri_yorigin + row * he; 1310 1311 bg = ri->ri_devcmap[(attr >> 16) & 0xff]; 1312 fg = ri->ri_devcmap[(attr >> 24) & 0xff]; 1313 uc = c - font->firstchar; 1314 fd = (uint8_t *)font->data + uc * ri->ri_fontscale; 1315 if (c == 0x20) { 1316 crmfb_fill_rect(sc, x, y, wi, he, bg); 1317 } else { 1318 crmfb_wait_idle(sc); 1319 /* setup */ 1320 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_ROP, 3); 1321 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_FG, fg); 1322 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_BG, bg); 1323 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_DRAWMODE, 1324 DE_DRAWMODE_PLANEMASK | DE_DRAWMODE_BYTEMASK | 1325 DE_DRAWMODE_ROP | 1326 DE_DRAWMODE_OPAQUE_STIP | DE_DRAWMODE_POLY_STIP); 1327 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_PRIMITIVE, 1328 DE_PRIM_RECTANGLE | DE_PRIM_LR | DE_PRIM_TB); 1329 bus_space_write_4(sc->sc_iot, sc->sc_reh, CRIME_DE_STIPPLE_MODE, 1330 0x001f0000); 1331 /* now let's feed the engine */ 1332 if (font->stride == 1) { 1333 /* shovel in 8 bit quantities */ 1334 fd8 = fd; 1335 for (i = 0; i < he; i++) { 1336 /* 1337 * the pipeline should be long enough to 1338 * draw any character without having to wait 1339 */ 1340 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1341 CRIME_DE_STIPPLE_PAT, *fd8 << 24); 1342 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1343 CRIME_DE_X_VERTEX_0, (x << 16) | y); 1344 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1345 CRIME_DE_X_VERTEX_1 | CRIME_DE_START, 1346 ((x + wi) << 16) | y); 1347 y++; 1348 fd8++; 1349 } 1350 } else if (font->stride == 2) { 1351 /* shovel in 16 bit quantities */ 1352 fd16 = fd; 1353 for (i = 0; i < he; i++) { 1354 /* 1355 * the pipeline should be long enough to 1356 * draw any character without having to wait 1357 */ 1358 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1359 CRIME_DE_STIPPLE_PAT, *fd16 << 16); 1360 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1361 CRIME_DE_X_VERTEX_0, (x << 16) | y); 1362 bus_space_write_4(sc->sc_iot, sc->sc_reh, 1363 CRIME_DE_X_VERTEX_1 | CRIME_DE_START, 1364 ((x + wi) << 16) | y); 1365 y++; 1366 fd16++; 1367 } 1368 } 1369 } 1370 } 1371 1372 static void 1373 crmfb_setup_ddc(struct crmfb_softc *sc) 1374 { 1375 int i; 1376 char edid_data[128]; 1377 1378 memset(edid_data, 0, 128); 1379 sc->sc_i2c.ic_cookie = sc; 1380 sc->sc_i2c.ic_acquire_bus = crmfb_i2c_acquire_bus; 1381 sc->sc_i2c.ic_release_bus = crmfb_i2c_release_bus; 1382 sc->sc_i2c.ic_send_start = crmfb_i2c_send_start; 1383 sc->sc_i2c.ic_send_stop = crmfb_i2c_send_stop; 1384 sc->sc_i2c.ic_initiate_xfer = crmfb_i2c_initiate_xfer; 1385 sc->sc_i2c.ic_read_byte = crmfb_i2c_read_byte; 1386 sc->sc_i2c.ic_write_byte = crmfb_i2c_write_byte; 1387 sc->sc_i2c.ic_exec = NULL; 1388 i = 0; 1389 while (edid_data[1] == 0 && i++ < 10) 1390 ddc_read_edid(&sc->sc_i2c, edid_data, 128); 1391 if (i > 1) 1392 aprint_debug_dev(sc->sc_dev, 1393 "had to try %d times to get EDID data\n", i); 1394 if (i < 11) { 1395 edid_parse(edid_data, &sc->sc_edid_info); 1396 edid_print(&sc->sc_edid_info); 1397 } 1398 } 1399 1400 /* I2C bitbanging */ 1401 static void 1402 crmfb_i2cbb_set_bits(void *cookie, uint32_t bits) 1403 { 1404 struct crmfb_softc *sc = cookie; 1405 1406 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA, bits ^ 3); 1407 } 1408 1409 static void 1410 crmfb_i2cbb_set_dir(void *cookie, uint32_t dir) 1411 { 1412 1413 /* Nothing to do */ 1414 } 1415 1416 static uint32_t 1417 crmfb_i2cbb_read(void *cookie) 1418 { 1419 struct crmfb_softc *sc = cookie; 1420 1421 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_I2C_VGA) ^ 3; 1422 } 1423 1424 /* higher level I2C stuff */ 1425 static int 1426 crmfb_i2c_acquire_bus(void *cookie, int flags) 1427 { 1428 1429 /* private bus */ 1430 return 0; 1431 } 1432 1433 static void 1434 crmfb_i2c_release_bus(void *cookie, int flags) 1435 { 1436 1437 /* private bus */ 1438 } 1439 1440 static int 1441 crmfb_i2c_send_start(void *cookie, int flags) 1442 { 1443 1444 return i2c_bitbang_send_start(cookie, flags, &crmfb_i2cbb_ops); 1445 } 1446 1447 static int 1448 crmfb_i2c_send_stop(void *cookie, int flags) 1449 { 1450 1451 return i2c_bitbang_send_stop(cookie, flags, &crmfb_i2cbb_ops); 1452 } 1453 1454 static int 1455 crmfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 1456 { 1457 1458 return i2c_bitbang_initiate_xfer(cookie, addr, flags, 1459 &crmfb_i2cbb_ops); 1460 } 1461 1462 static int 1463 crmfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags) 1464 { 1465 1466 return i2c_bitbang_read_byte(cookie, valp, flags, &crmfb_i2cbb_ops); 1467 } 1468 1469 static int 1470 crmfb_i2c_write_byte(void *cookie, uint8_t val, int flags) 1471 { 1472 1473 return i2c_bitbang_write_byte(cookie, val, flags, &crmfb_i2cbb_ops); 1474 } 1475 1476 /* mode setting stuff */ 1477 static uint32_t 1478 calc_pll(int f_out) 1479 { 1480 uint32_t ret; 1481 int f_in = 20000; /* 20MHz in */ 1482 int M, N, P; 1483 int error, div, best = 9999999; 1484 int ff1, ff2; 1485 int MM = 0, NN = 0, PP = 0, ff = 0; 1486 1487 /* f_out = M * f_in / (N * (1 << P) */ 1488 1489 for (P = 0; P < 4; P++) { 1490 for (N = 64; N > 0; N--) { 1491 div = N * (1 << P); 1492 M = f_out * div / f_in; 1493 if ((M < 257) && (M > 100)) { 1494 ff1 = M * f_in / div; 1495 ff2 = (M + 1) * f_in / div; 1496 error = abs(ff1 - f_out); 1497 if (error < best) { 1498 MM = M; 1499 NN = N; 1500 PP = P; 1501 ff = ff1; 1502 best = error; 1503 } 1504 error = abs(ff2 - f_out); 1505 if ((error < best) && ( M < 256)){ 1506 MM = M + 1; 1507 NN = N; 1508 PP = P; 1509 ff = ff2; 1510 best = error; 1511 } 1512 } 1513 } 1514 } 1515 DPRINTF("%d: M %d N %d P %d -> %d\n", f_out, MM, NN, PP, ff); 1516 /* now shove the parameters into the register's format */ 1517 ret = (MM - 1) | ((NN - 1) << 8) | (P << 14); 1518 return ret; 1519 } 1520 1521 static int 1522 crmfb_set_mode(struct crmfb_softc *sc, const struct videomode *mode) 1523 { 1524 uint32_t d, dc; 1525 int tmp, diff; 1526 1527 if ((mode->hdisplay % 32) != 0) { 1528 aprint_error_dev(sc->sc_dev, "hdisplay (%d) is not a multiple of 32\n", mode->hdisplay); 1529 return FALSE; 1530 } 1531 if (mode->dot_clock > 140000) { 1532 aprint_error_dev(sc->sc_dev, "requested dot clock is too high ( %d MHz )\n", mode->dot_clock / 1000); 1533 return FALSE; 1534 } 1535 1536 /* disable DMA */ 1537 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL); 1538 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT); 1539 crmfb_write_reg(sc, CRMFB_OVR_CONTROL, d); 1540 DELAY(50000); 1541 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL); 1542 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT); 1543 crmfb_write_reg(sc, CRMFB_FRM_CONTROL, d); 1544 DELAY(50000); 1545 crmfb_write_reg(sc, CRMFB_DID_CONTROL, d); 1546 DELAY(50000); 1547 1548 if (!crmfb_wait_dma_idle(sc)) 1549 aprint_error("crmfb: crmfb_wait_dma_idle timed out\n"); 1550 1551 /* ok, now we're good to go */ 1552 dc = calc_pll(mode->dot_clock); 1553 1554 crmfb_write_reg(sc, CRMFB_VT_XY, 1 << CRMFB_VT_XY_FREEZE_SHIFT); 1555 delay(1000); 1556 1557 /* set the dot clock pll but don't start it yet */ 1558 crmfb_write_reg(sc, CRMFB_DOTCLOCK, dc); 1559 delay(10000); 1560 1561 /* pixel counter */ 1562 d = mode->htotal | (mode->vtotal << 12); 1563 crmfb_write_reg(sc, CRMFB_VT_XYMAX, d); 1564 1565 /* video timings */ 1566 d = mode->vsync_end | (mode->vsync_start << 12); 1567 crmfb_write_reg(sc, CRMFB_VT_VSYNC, d); 1568 1569 d = mode->hsync_end | (mode->hsync_start << 12); 1570 crmfb_write_reg(sc, CRMFB_VT_HSYNC, d); 1571 1572 d = mode->vtotal | (mode->vdisplay << 12); 1573 crmfb_write_reg(sc, CRMFB_VT_VBLANK, d); 1574 1575 d = (mode->htotal - 5) | ((mode->hdisplay - 5) << 12); 1576 crmfb_write_reg(sc, CRMFB_VT_HBLANK, d); 1577 1578 d = mode->vtotal | (mode->vdisplay << 12); 1579 crmfb_write_reg(sc, CRMFB_VT_VCMAP, d); 1580 d = mode->htotal | (mode->hdisplay << 12); 1581 crmfb_write_reg(sc, CRMFB_VT_HCMAP, d); 1582 1583 d = 0; 1584 if (mode->flags & VID_NHSYNC) d |= CRMFB_VT_FLAGS_HDRV_INVERT; 1585 if (mode->flags & VID_NVSYNC) d |= CRMFB_VT_FLAGS_VDRV_INVERT; 1586 crmfb_write_reg(sc, CRMFB_VT_FLAGS, d); 1587 1588 diff = -abs(mode->vtotal - mode->vdisplay - 1); 1589 d = ((uint32_t)diff << 12) & 0x00fff000; 1590 d |= (mode->htotal - 20); 1591 crmfb_write_reg(sc, CRMFB_VT_DID_STARTXY, d); 1592 1593 d = ((uint32_t)(diff + 1) << 12) & 0x00fff000; 1594 d |= (mode->htotal - 54); 1595 crmfb_write_reg(sc, CRMFB_VT_CRS_STARTXY, d); 1596 1597 d = ((uint32_t)diff << 12) & 0x00fff000; 1598 d |= (mode->htotal - 4); 1599 crmfb_write_reg(sc, CRMFB_VT_VC_STARTXY, d); 1600 1601 tmp = mode->htotal - 19; 1602 d = tmp << 12; 1603 d |= ((tmp + mode->hdisplay - 2) % mode->htotal); 1604 crmfb_write_reg(sc, CRMFB_VT_HPIX_EN, d); 1605 1606 d = mode->vdisplay | (mode->vtotal << 12); 1607 crmfb_write_reg(sc, CRMFB_VT_VPIX_EN, d); 1608 1609 sc->sc_width = mode->hdisplay; 1610 sc->sc_height = mode->vdisplay; 1611 1612 return TRUE; 1613 } 1614 1615