1 /* $NetBSD: voyager.c,v 1.12 2016/11/16 22:05:19 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2009, 2011 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 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: voyager.c,v 1.12 2016/11/16 22:05:19 macallan Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/device.h> 35 #include <sys/malloc.h> 36 #include <sys/lwp.h> 37 #include <sys/kauth.h> 38 39 #include <dev/pci/pcivar.h> 40 #include <dev/pci/pcireg.h> 41 #include <dev/pci/pcidevs.h> 42 #include <dev/pci/pciio.h> 43 #include <dev/ic/sm502reg.h> 44 #include <dev/i2c/i2cvar.h> 45 #include <dev/i2c/i2c_bitbang.h> 46 47 #include <sys/evcnt.h> 48 #include <sys/bitops.h> 49 50 #include <dev/pci/voyagervar.h> 51 52 #include "opt_voyager.h" 53 #include "voyagerfb.h" 54 #include "pwmclock.h" 55 56 #ifdef VOYAGER_DEBUG 57 #define DPRINTF aprint_normal 58 #else 59 #define DPRINTF while (0) printf 60 #endif 61 62 /* interrupt stuff */ 63 struct voyager_intr { 64 int (*vih_func)(void *); 65 void *vih_arg; 66 struct evcnt vih_count; 67 char vih_name[32]; 68 }; 69 70 struct voyager_softc { 71 device_t sc_dev; 72 73 pci_chipset_tag_t sc_pc; 74 pcitag_t sc_pcitag; 75 76 bus_space_tag_t sc_memt; 77 bus_space_tag_t sc_iot; 78 79 bus_space_handle_t sc_fbh, sc_regh; 80 bus_addr_t sc_fb, sc_reg; 81 bus_size_t sc_fbsize, sc_regsize; 82 83 struct i2c_controller sc_i2c; 84 kmutex_t sc_i2c_lock; 85 86 /* interrupt dispatcher */ 87 void *sc_ih; 88 struct voyager_intr sc_intrs[32]; 89 }; 90 91 void *voyager_cookie = NULL; 92 93 static int voyager_match(device_t, cfdata_t, void *); 94 static void voyager_attach(device_t, device_t, void *); 95 static int voyager_print(void *, const char *); 96 static int voyager_intr(void *); 97 98 CFATTACH_DECL_NEW(voyager, sizeof(struct voyager_softc), 99 voyager_match, voyager_attach, NULL, NULL); 100 101 /* I2C glue */ 102 static int voyager_i2c_acquire_bus(void *, int); 103 static void voyager_i2c_release_bus(void *, int); 104 static int voyager_i2c_send_start(void *, int); 105 static int voyager_i2c_send_stop(void *, int); 106 static int voyager_i2c_initiate_xfer(void *, i2c_addr_t, int); 107 static int voyager_i2c_read_byte(void *, uint8_t *, int); 108 static int voyager_i2c_write_byte(void *, uint8_t, int); 109 110 /* I2C bitbang glue */ 111 static void voyager_i2cbb_set_bits(void *, uint32_t); 112 static void voyager_i2cbb_set_dir(void *, uint32_t); 113 static uint32_t voyager_i2cbb_read(void *); 114 115 static const struct i2c_bitbang_ops voyager_i2cbb_ops = { 116 voyager_i2cbb_set_bits, 117 voyager_i2cbb_set_dir, 118 voyager_i2cbb_read, 119 { 120 1 << 13, 121 1 << 6, 122 1 << 13, 123 0 124 } 125 }; 126 #define GPIO_I2C_BITS ((1 << 6) | (1 << 13)) 127 128 #ifdef VOYAGER_DEBUG 129 static void voyager_print_pwm(struct voyager_softc *, int); 130 #endif 131 132 static int 133 voyager_match(device_t parent, cfdata_t match, void *aux) 134 { 135 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 136 137 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 138 return 0; 139 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SILMOTION) 140 return 0; 141 142 /* only chip tested on so far - may need a list */ 143 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SILMOTION_SM502) 144 return 100; 145 return (0); 146 } 147 148 static void 149 voyager_attach(device_t parent, device_t self, void *aux) 150 { 151 struct voyager_softc *sc = device_private(self); 152 struct pci_attach_args *pa = aux; 153 pci_intr_handle_t ih; 154 struct voyager_attach_args vaa; 155 struct i2cbus_attach_args iba; 156 uint32_t reg; 157 const char *intrstr; 158 int i; 159 char intrbuf[PCI_INTRSTR_LEN]; 160 161 sc->sc_pc = pa->pa_pc; 162 sc->sc_pcitag = pa->pa_tag; 163 sc->sc_memt = pa->pa_memt; 164 sc->sc_iot = pa->pa_iot; 165 sc->sc_dev = self; 166 167 voyager_cookie = sc; 168 169 pci_aprint_devinfo(pa, NULL); 170 171 if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0, 172 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 173 aprint_error("%s: failed to map registers.\n", 174 device_xname(sc->sc_dev)); 175 } 176 177 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 178 BUS_SPACE_MAP_LINEAR, 179 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { 180 aprint_error("%s: failed to map the frame buffer.\n", 181 device_xname(sc->sc_dev)); 182 } 183 184 /* disable all interrupts */ 185 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, 0); 186 187 /* initialize handler list */ 188 for (i = 0; i < 32; i++) { 189 sc->sc_intrs[i].vih_func = NULL; 190 snprintf(sc->sc_intrs[i].vih_name, 32, "int %d", i); 191 evcnt_attach_dynamic(&sc->sc_intrs[i].vih_count, 192 EVCNT_TYPE_INTR, NULL, "voyager", sc->sc_intrs[i].vih_name); 193 } 194 195 /* Map and establish the interrupt. */ 196 if (pci_intr_map(pa, &ih)) { 197 aprint_error_dev(self, "couldn't map interrupt\n"); 198 return; 199 } 200 201 intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, sizeof(intrbuf)); 202 sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_VM, 203 voyager_intr, NULL); /* so we get the clock frame instead */ 204 if (sc->sc_ih == NULL) { 205 aprint_error_dev(self, "couldn't establish interrupt"); 206 if (intrstr != NULL) 207 aprint_error(" at %s", intrstr); 208 aprint_error("\n"); 209 return; 210 } 211 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 212 213 #ifdef VOYAGER_DEBUG 214 voyager_print_pwm(sc, SM502_PWM0); 215 voyager_print_pwm(sc, SM502_PWM1); 216 voyager_print_pwm(sc, SM502_PWM2); 217 #endif 218 219 /* attach the framebuffer driver */ 220 vaa.vaa_memh = sc->sc_fbh; 221 vaa.vaa_mem_pa = sc->sc_fb; 222 vaa.vaa_regh = sc->sc_regh; 223 vaa.vaa_reg_pa = sc->sc_reg; 224 vaa.vaa_tag = sc->sc_memt; 225 vaa.vaa_pc = sc->sc_pc; 226 vaa.vaa_pcitag = sc->sc_pcitag; 227 #if NVOYAGERFB > 0 228 strcpy(vaa.vaa_name, "voyagerfb"); 229 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); 230 #endif 231 #if NPWMCLOCK > 0 232 strcpy(vaa.vaa_name, "pwmclock"); 233 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); 234 #endif 235 #ifdef notyet 236 strcpy(vaa.vaa_name, "vac"); 237 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); 238 #endif 239 /* we use this mutex wether there's an i2c bus or not */ 240 mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE); 241 242 /* 243 * see if the i2c pins are configured as gpio and if so, use them 244 * should probably be a compile time option 245 */ 246 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL); 247 if ((reg & GPIO_I2C_BITS) == 0) { 248 /* both bits as outputs */ 249 voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS); 250 251 /* Fill in the i2c tag */ 252 memset(&sc->sc_i2c, 0, sizeof(sc->sc_i2c)); 253 memset(&iba, 0, sizeof(iba)); 254 sc->sc_i2c.ic_cookie = sc; 255 sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus; 256 sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus; 257 sc->sc_i2c.ic_send_start = voyager_i2c_send_start; 258 sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop; 259 sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer; 260 sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte; 261 sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte; 262 sc->sc_i2c.ic_exec = NULL; 263 iba.iba_tag = &sc->sc_i2c; 264 config_found_ia(self, "i2cbus", &iba, iicbus_print); 265 } 266 voyager_control_gpio(sc, ~(1 << 16), 0); 267 voyager_gpio_dir(sc, 0xffffffff, 1 << 16); 268 voyager_write_gpio(sc, 0xffffffff, 1 << 16); 269 } 270 271 static int 272 voyager_print(void *aux, const char *what) 273 { 274 /*struct voyager_attach_args *vaa = aux;*/ 275 276 if (what == NULL) 277 return 0; 278 279 printf("%s:", what); 280 281 return 0; 282 } 283 284 static void 285 voyager_i2cbb_set_bits(void *cookie, uint32_t bits) 286 { 287 struct voyager_softc *sc = cookie; 288 uint32_t reg; 289 290 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); 291 reg &= ~GPIO_I2C_BITS; 292 reg |= bits; 293 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg); 294 } 295 296 static void 297 voyager_i2cbb_set_dir(void *cookie, uint32_t bits) 298 { 299 struct voyager_softc *sc = cookie; 300 uint32_t reg; 301 302 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0); 303 reg &= ~(1 << 13); 304 reg |= bits; 305 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg); 306 } 307 308 static uint32_t 309 voyager_i2cbb_read(void *cookie) 310 { 311 struct voyager_softc *sc = cookie; 312 uint32_t reg; 313 314 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); 315 return reg; 316 } 317 318 /* higher level I2C stuff */ 319 static int 320 voyager_i2c_acquire_bus(void *cookie, int flags) 321 { 322 struct voyager_softc *sc = cookie; 323 324 mutex_enter(&sc->sc_i2c_lock); 325 return 0; 326 } 327 328 static void 329 voyager_i2c_release_bus(void *cookie, int flags) 330 { 331 struct voyager_softc *sc = cookie; 332 333 mutex_exit(&sc->sc_i2c_lock); 334 } 335 336 static int 337 voyager_i2c_send_start(void *cookie, int flags) 338 { 339 return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops)); 340 } 341 342 static int 343 voyager_i2c_send_stop(void *cookie, int flags) 344 { 345 346 return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops)); 347 } 348 349 static int 350 voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 351 { 352 /* 353 * for some reason i2c_bitbang_initiate_xfer left-shifts 354 * the I2C-address and then sets the direction bit 355 */ 356 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, 357 &voyager_i2cbb_ops)); 358 } 359 360 static int 361 voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags) 362 { 363 int ret; 364 365 ret = i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops); 366 return ret; 367 } 368 369 static int 370 voyager_i2c_write_byte(void *cookie, uint8_t val, int flags) 371 { 372 int ret; 373 374 ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops); 375 delay(500); 376 return ret; 377 } 378 379 void 380 voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits) 381 { 382 struct voyager_softc *sc = cookie; 383 uint32_t reg; 384 385 /* don't interfere with i2c ops */ 386 mutex_enter(&sc->sc_i2c_lock); 387 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum); 388 reg &= mask; 389 reg |= bits; 390 bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg); 391 mutex_exit(&sc->sc_i2c_lock); 392 } 393 394 static int 395 voyager_intr(void *cookie) 396 { 397 struct voyager_softc *sc = voyager_cookie; 398 struct voyager_intr *ih; 399 uint32_t intrs; 400 uint32_t mask, bit; 401 int num; 402 403 intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS); 404 mask = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 405 intrs &= mask; 406 407 while (intrs != 0) { 408 num = ffs32(intrs) - 1; 409 bit = 1 << num; 410 intrs &= ~bit; 411 ih = &sc->sc_intrs[num]; 412 if (ih->vih_func != NULL) { 413 if (ih->vih_arg == NULL) { 414 ih->vih_func(cookie); 415 } else { 416 ih->vih_func(ih->vih_arg); 417 } 418 } 419 ih->vih_count.ev_count++; 420 } 421 return 0; 422 } 423 424 void * 425 voyager_establish_intr(device_t dev, int bit, int (*handler)(void *), void *arg) 426 { 427 struct voyager_softc *sc = device_private(dev); 428 struct voyager_intr *ih; 429 uint32_t reg; 430 431 if ((bit < 0) || (bit > 31)) { 432 aprint_error_dev(dev, "bogus interrupt %d\n", bit); 433 return NULL; 434 } 435 436 ih = &sc->sc_intrs[bit]; 437 if (ih->vih_func != NULL) { 438 aprint_error_dev(dev, "interrupt %d is already in use\n", bit); 439 return NULL; 440 } 441 ih->vih_func = handler; 442 ih->vih_arg = arg; 443 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 444 reg |= 1 << bit; 445 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, reg); 446 447 return (void *)(uintptr_t)(0x80000000 | bit); 448 } 449 450 void 451 voyager_disestablish_intr(device_t dev, void *ih) 452 { 453 } 454 455 /* timer */ 456 #ifdef VOYAGER_DEBUG 457 static void 458 voyager_print_pwm(struct voyager_softc *sc, int pwmreg) 459 { 460 uint32_t reg; 461 462 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, pwmreg); 463 aprint_debug_dev(sc->sc_dev, "%08x: %08x = %d Hz, %d high, %d low\n", 464 pwmreg, reg, 465 96000000 / (1 << ((reg & SM502_PWM_CLOCK_DIV_MASK) >> SM502_PWM_CLOCK_DIV_SHIFT)), 466 (reg & SM502_PWM_CLOCK_HIGH_MASK) >> SM502_PWM_CLOCK_HIGH_SHIFT, 467 (reg & SM502_PWM_CLOCK_LOW_MASK) >> SM502_PWM_CLOCK_LOW_SHIFT); 468 } 469 #endif 470 471 uint32_t 472 voyager_set_pwm(int freq, int duty_cycle) 473 { 474 int ifreq, factor, bit, steps; 475 uint32_t reg = 0, hi, lo; 476 477 /* 478 * find the smallest divider that gets us within 4096 steps of the 479 * target frequency 480 */ 481 ifreq = freq * 4096; 482 factor = 96000000 / ifreq; 483 bit = fls32(factor); 484 factor = 1 << bit; 485 steps = 96000000 / (factor * freq); 486 /* can't have it all off */ 487 if (duty_cycle < 1) 488 duty_cycle = 1; 489 /* can't be always on either */ 490 if (duty_cycle > 999) 491 duty_cycle = 999; 492 hi = steps * duty_cycle / 1000; 493 if (hi < 1) 494 hi = 1; 495 lo = steps - hi; 496 if (lo < 1) { 497 hi = steps - 1; 498 lo = 1; 499 } 500 DPRINTF("%d hz -> %d, %d, %d / %d\n", freq, factor, steps, lo, hi); 501 reg = ((hi - 1) & 0xfff) << SM502_PWM_CLOCK_HIGH_SHIFT; 502 reg |= ((lo - 1) & 0xfff) << SM502_PWM_CLOCK_LOW_SHIFT; 503 reg |= (bit & 0xf) << SM502_PWM_CLOCK_DIV_SHIFT; 504 DPRINTF("reg: %08x\n", reg); 505 return reg; 506 } 507