1 /* $NetBSD: valkyriefb.c,v 1.2 2012/05/23 21:46:17 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2012 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * A console driver for Apple's Valkyrie onboard video controller, found in 30 * for example the Performa 6360 31 * This should be easy enough to adapt to mac68k but I don't have the hardware 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: valkyriefb.c,v 1.2 2012/05/23 21:46:17 macallan Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/device.h> 41 42 #include <dev/ofw/openfirm.h> 43 44 #include <machine/autoconf.h> 45 46 #include <dev/wscons/wsdisplayvar.h> 47 #include <dev/wscons/wsconsio.h> 48 #include <dev/wsfont/wsfont.h> 49 #include <dev/rasops/rasops.h> 50 #include <dev/wscons/wsdisplay_vconsvar.h> 51 52 #include <dev/videomode/videomode.h> 53 54 #include <arch/macppc/dev/valkyriefbreg.h> 55 #include <arch/macppc/dev/videopllvar.h> 56 57 #include "opt_wsemul.h" 58 #include "opt_valkyriefb.h" 59 60 #include "videopll.h" 61 #if NVIDEOPLL < 1 62 #error "valkyriefb requires videopll at iic" 63 #endif 64 65 struct valkyriefb_softc { 66 device_t sc_dev; 67 int sc_node; 68 uint8_t *sc_base; 69 uint8_t *sc_fbaddr; 70 71 int sc_depth; 72 int sc_width, sc_height, sc_linebytes; 73 const struct videomode *sc_videomode; 74 uint8_t sc_modereg; 75 76 int sc_mode; 77 uint32_t sc_bg; 78 79 u_char sc_cmap_red[256]; 80 u_char sc_cmap_green[256]; 81 u_char sc_cmap_blue[256]; 82 83 struct vcons_data vd; 84 }; 85 86 struct valkyriefb_mode { 87 int width; 88 int height; 89 uint8_t mode; 90 }; 91 92 /* 93 * Translate screen resolutions to values for Valkyrie's mode register. 94 * These numbers seem to roughly correspond to MacOS video mode numbers 95 * there are many numbers that result in the same resolution which in MacOS 96 * only differ by the display refresh rate, which isn't set by Valkyrie at 97 * all, instead there's a PLL chip hooked to cuda's i2c bus. It doesn't seem 98 * to matter which number we use as long as the resolution is right and we 99 * program the PLL for the right pixel clock 100 */ 101 static struct valkyriefb_mode modetab[] = { 102 { 640, 480, 6 }, 103 { 800, 600, 12 }, 104 { 832, 624, 9 }, 105 { 1024, 768, 14}, 106 }; 107 108 static struct vcons_screen valkyriefb_console_screen; 109 110 static int valkyriefb_match(device_t, cfdata_t, void *); 111 static void valkyriefb_attach(device_t, device_t, void *); 112 static int valkyriefb_init(device_t); 113 static int valkyriefb_set_mode(struct valkyriefb_softc *, 114 const struct videomode *, int); 115 116 CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc), 117 valkyriefb_match, valkyriefb_attach, NULL, NULL); 118 119 struct wsscreen_descr valkyriefb_defaultscreen = { 120 "default", 121 0, 0, 122 NULL, 123 8, 16, 124 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 125 NULL, 126 }; 127 128 const struct wsscreen_descr *_valkyriefb_scrlist[] = { 129 &valkyriefb_defaultscreen, 130 /* XXX other formats, graphics screen? */ 131 }; 132 133 struct wsscreen_list valkyriefb_screenlist = { 134 sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *), _valkyriefb_scrlist 135 }; 136 137 static int valkyriefb_ioctl(void *, void *, u_long, void *, int, 138 struct lwp *); 139 static paddr_t valkyriefb_mmap(void *, void *, off_t, int); 140 141 static void valkyriefb_init_screen(void *, struct vcons_screen *, int, 142 long *); 143 144 145 struct wsdisplay_accessops valkyriefb_accessops = { 146 valkyriefb_ioctl, 147 valkyriefb_mmap, 148 NULL, 149 NULL, 150 NULL, 151 NULL, /* load_font */ 152 NULL, /* polls */ 153 NULL, /* scroll */ 154 }; 155 156 static inline void 157 valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val) 158 { 159 *(sc->sc_base + VAL_REGS_OFFSET + reg) = val; 160 __asm("eieio; sync;"); 161 } 162 163 static inline uint8_t 164 valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg) 165 { 166 return *(sc->sc_base + VAL_REGS_OFFSET + reg); 167 } 168 169 static void 170 valkyriefb_write_cmap(struct valkyriefb_softc *sc, 171 int reg, uint8_t r, uint8_t g, uint8_t b) 172 { 173 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg; 174 __asm("eieio; sync;"); 175 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r; 176 __asm("eieio; sync;"); 177 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g; 178 __asm("eieio; sync;"); 179 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b; 180 __asm("eieio; sync;"); 181 } 182 183 static int 184 valkyriefb_match(device_t parent, cfdata_t cf, void *aux) 185 { 186 struct confargs *ca = aux; 187 188 if (strcmp(ca->ca_name, "valkyrie") != 0) 189 return 0; 190 return 1; 191 } 192 193 static void 194 valkyriefb_attach(device_t parent, device_t self, void *aux) 195 { 196 struct valkyriefb_softc *sc = device_private(self); 197 struct confargs *ca = aux; 198 199 sc->sc_dev = self; 200 sc->sc_node = ca->ca_node; 201 202 aprint_normal(" address 0x%08x\n", ca->ca_reg[0]); 203 aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n"); 204 sc->sc_base = (uint8_t *)ca->ca_reg[0]; 205 #ifdef VALKYRIEFB_DEBUG 206 for (i = 0; i < 0x40; i += 8) { 207 aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i, valkyriefb_read_reg(sc, i)); 208 } 209 #endif 210 config_finalize_register(sc->sc_dev, valkyriefb_init); 211 sc->sc_fbaddr = (uint8_t *)(sc->sc_base + 0x1000); 212 } 213 214 static int 215 valkyriefb_init(device_t self) 216 { 217 struct valkyriefb_softc *sc = device_private(self); 218 const struct videomode *mode; 219 struct rasops_info *ri; 220 prop_dictionary_t dict; 221 struct wsemuldisplaydev_attach_args aa; 222 bool console = FALSE; 223 long defattr; 224 225 mode = pick_mode_by_ref(800, 600, 60); 226 if (mode == NULL) 227 return 0; 228 229 valkyriefb_set_mode(sc, mode, 8); 230 231 vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen, &valkyriefb_accessops); 232 sc->vd.init_screen = valkyriefb_init_screen; 233 234 dict = device_properties(sc->sc_dev); 235 prop_dictionary_get_bool(dict, "is_console", &console); 236 237 ri = &valkyriefb_console_screen.scr_ri; 238 vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr); 239 memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf], 240 sc->sc_width * sc->sc_linebytes); 241 valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 242 243 valkyriefb_defaultscreen.textops = &ri->ri_ops; 244 valkyriefb_defaultscreen.capabilities = ri->ri_caps; 245 valkyriefb_defaultscreen.nrows = ri->ri_rows; 246 valkyriefb_defaultscreen.ncols = ri->ri_cols; 247 if (console) { 248 wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0, defattr); 249 vcons_replay_msgbuf(&valkyriefb_console_screen); 250 } 251 aa.console = console; 252 aa.scrdata = &valkyriefb_screenlist; 253 aa.accessops = &valkyriefb_accessops; 254 aa.accesscookie = &sc->vd; 255 256 config_found(self, &aa, wsemuldisplaydevprint); 257 258 return 0; 259 } 260 261 static int 262 valkyriefb_set_mode(struct valkyriefb_softc *sc, 263 const struct videomode *mode, int depth) 264 { 265 int i; 266 uint8_t modereg, tmp; 267 268 /* first find the parameter for the mode register */ 269 i = 0; 270 while ((i < __arraycount(modetab)) && 271 (modetab[i].width != mode->hdisplay) && 272 (modetab[i].height != mode->vdisplay)) 273 i++; 274 if ((modetab[i].width == mode->hdisplay) && 275 (modetab[i].height == mode->vdisplay)) { 276 modereg = modetab[i].mode; 277 } else { 278 aprint_error_dev(sc->sc_dev, "Can't find a mode register value for %s\n", mode->name); 279 return EINVAL; 280 } 281 282 /* check if we have enough video memory */ 283 if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) { 284 aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n", mode->name); 285 return EINVAL; 286 } 287 288 /* reject depths other than 8 or 16 */ 289 if ((depth != 8) && (depth != 16)) { 290 aprint_error_dev(sc->sc_dev, "Depth [%d] is unsupported for %s\n", depth, mode->name); 291 return EINVAL; 292 } 293 294 /* now start programming the chip */ 295 valkyriefb_write_reg(sc, VAL_STATUS, 0); 296 delay(100); 297 valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP); 298 299 if (depth == 8) { 300 sc->sc_depth = 8; 301 valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8); 302 for (i = 0; i < 256; i++) { 303 tmp = i & 0xe0; 304 /* 305 * replicate bits so 0xe0 maps to a red value of 0xff 306 * in order to make white look actually white 307 */ 308 tmp |= (tmp >> 3) | (tmp >> 6); 309 sc->sc_cmap_red[i] = tmp; 310 311 tmp = (i & 0x1c) << 3; 312 tmp |= (tmp >> 3) | (tmp >> 6); 313 sc->sc_cmap_green[i] = tmp; 314 315 tmp = (i & 0x03) << 6; 316 tmp |= tmp >> 2; 317 tmp |= tmp >> 4; 318 sc->sc_cmap_blue[i] = tmp; 319 320 valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i], 321 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 322 } 323 } else if (depth == 16) { 324 sc->sc_depth = 16; 325 valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16); 326 /* does the palette have any effect in 16bit? */ 327 } 328 329 videopll_set_freq(mode->dot_clock); 330 delay(100); 331 valkyriefb_write_reg(sc, VAL_MODE, modereg); 332 333 sc->sc_modereg = modereg; 334 sc->sc_videomode = mode; 335 sc->sc_width = mode->hdisplay; 336 sc->sc_height = mode->vdisplay; 337 sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3); 338 aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n", 339 sc->sc_width, sc->sc_height, sc->sc_depth); 340 return 0; 341 } 342 343 static int 344 valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 345 struct lwp *l) 346 { 347 struct vcons_data *vd = v; 348 struct valkyriefb_softc *sc = vd->cookie; 349 struct wsdisplay_fbinfo *wdf; 350 struct vcons_screen *ms = vd->active; 351 352 switch (cmd) { 353 case WSDISPLAYIO_GTYPE: 354 *(u_int *)data = WSDISPLAY_TYPE_VALKYRIE; 355 return 0; 356 357 case WSDISPLAYIO_GINFO: 358 wdf = (void *)data; 359 wdf->height = ms->scr_ri.ri_height; 360 wdf->width = ms->scr_ri.ri_width; 361 wdf->depth = ms->scr_ri.ri_depth; 362 wdf->cmsize = 256; 363 return 0; 364 #if 0 365 case WSDISPLAYIO_GETCMAP: 366 return valkyriefb_getcmap(sc, 367 (struct wsdisplay_cmap *)data); 368 369 case WSDISPLAYIO_PUTCMAP: 370 return valkyriefb_putcmap(sc, 371 (struct wsdisplay_cmap *)data); 372 #endif 373 374 case WSDISPLAYIO_SMODE: { 375 int new_mode = *(int*)data; 376 if (new_mode != sc->sc_mode) { 377 sc->sc_mode = new_mode; 378 if (new_mode == WSDISPLAYIO_MODE_EMUL) { 379 vcons_redraw_screen(ms); 380 } 381 } 382 } 383 return 0; 384 } 385 return EPASSTHROUGH; 386 } 387 388 static paddr_t 389 valkyriefb_mmap(void *v, void *vs, off_t offset, int prot) 390 { 391 struct vcons_data *vd = v; 392 struct valkyriefb_softc *sc = vd->cookie; 393 paddr_t pa; 394 395 /* 'regular' framebuffer mmap()ing */ 396 if (offset < 0x100000) { 397 pa = (paddr_t)(sc->sc_base + 0x1000 + offset); 398 return pa; 399 } 400 return -1; 401 } 402 403 static void 404 valkyriefb_init_screen(void *cookie, struct vcons_screen *scr, 405 int existing, long *defattr) 406 { 407 struct valkyriefb_softc *sc = cookie; 408 struct rasops_info *ri = &scr->scr_ri; 409 410 memset(ri, 0, sizeof(struct rasops_info)); 411 ri->ri_depth = sc->sc_depth; 412 ri->ri_width = sc->sc_width; 413 ri->ri_height = sc->sc_height; 414 ri->ri_stride = sc->sc_linebytes; 415 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA; 416 ri->ri_bits = sc->sc_fbaddr; 417 418 /* 419 * We probably shouldn't set this flag together with RI_ENABLE_ALPHA 420 * since the CPU is likely slow enough to make scrolling using 421 * framebuffer reads faster than redrawing 422 */ 423 scr->scr_flags |= VCONS_DONT_READ; 424 425 rasops_init(ri, 0, 0); 426 ri->ri_caps = WSSCREEN_WSCOLORS; 427 428 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 429 sc->sc_width / ri->ri_font->fontwidth); 430 431 ri->ri_hw = scr; 432 } 433