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