1 /* $NetBSD: voyager.c,v 1.16 2021/04/24 23:36:57 thorpej 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.16 2021/04/24 23:36:57 thorpej 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_xname(sc->sc_pc, ih, IPL_VM, 203 voyager_intr, NULL, /* so we get the clock frame instead */ 204 device_xname(self)); 205 if (sc->sc_ih == NULL) { 206 aprint_error_dev(self, "couldn't establish interrupt"); 207 if (intrstr != NULL) 208 aprint_error(" at %s", intrstr); 209 aprint_error("\n"); 210 return; 211 } 212 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 213 214 #ifdef VOYAGER_DEBUG 215 voyager_print_pwm(sc, SM502_PWM0); 216 voyager_print_pwm(sc, SM502_PWM1); 217 voyager_print_pwm(sc, SM502_PWM2); 218 #endif 219 220 /* attach the framebuffer driver */ 221 vaa.vaa_memh = sc->sc_fbh; 222 vaa.vaa_mem_pa = sc->sc_fb; 223 vaa.vaa_regh = sc->sc_regh; 224 vaa.vaa_reg_pa = sc->sc_reg; 225 vaa.vaa_tag = sc->sc_memt; 226 vaa.vaa_pc = sc->sc_pc; 227 vaa.vaa_pcitag = sc->sc_pcitag; 228 #if NVOYAGERFB > 0 229 strcpy(vaa.vaa_name, "voyagerfb"); 230 config_found(sc->sc_dev, &vaa, voyager_print, 231 CFARG_IATTR, "voyagerbus", 232 CFARG_EOL); 233 #endif 234 #if NPWMCLOCK > 0 235 strcpy(vaa.vaa_name, "pwmclock"); 236 config_found(sc->sc_dev, &vaa, voyager_print, 237 CFARG_IATTR, "voyagerbus", 238 CFARG_EOL); 239 #endif 240 #ifdef notyet 241 strcpy(vaa.vaa_name, "vac"); 242 config_found(sc->sc_dev, &vaa, voyager_print, 243 CFARG_IATTR, "voyagerbus", 244 CFARG_EOL); 245 #endif 246 /* we use this mutex whether there's an i2c bus or not */ 247 mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE); 248 249 /* 250 * see if the i2c pins are configured as gpio and if so, use them 251 * should probably be a compile time option 252 */ 253 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL); 254 if ((reg & GPIO_I2C_BITS) == 0) { 255 /* both bits as outputs */ 256 voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS); 257 258 /* Fill in the i2c tag */ 259 memset(&iba, 0, sizeof(iba)); 260 iic_tag_init(&sc->sc_i2c); 261 sc->sc_i2c.ic_cookie = sc; 262 sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus; 263 sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus; 264 sc->sc_i2c.ic_send_start = voyager_i2c_send_start; 265 sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop; 266 sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer; 267 sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte; 268 sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte; 269 iba.iba_tag = &sc->sc_i2c; 270 config_found(self, &iba, iicbus_print, 271 CFARG_IATTR, "i2cbus", 272 CFARG_EOL); 273 } 274 voyager_control_gpio(sc, ~(1 << 16), 0); 275 voyager_gpio_dir(sc, 0xffffffff, 1 << 16); 276 voyager_write_gpio(sc, 0xffffffff, 1 << 16); 277 } 278 279 static int 280 voyager_print(void *aux, const char *what) 281 { 282 /*struct voyager_attach_args *vaa = aux;*/ 283 284 if (what == NULL) 285 return 0; 286 287 printf("%s:", what); 288 289 return 0; 290 } 291 292 static void 293 voyager_i2cbb_set_bits(void *cookie, uint32_t bits) 294 { 295 struct voyager_softc *sc = cookie; 296 uint32_t reg; 297 298 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); 299 reg &= ~GPIO_I2C_BITS; 300 reg |= bits; 301 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg); 302 } 303 304 static void 305 voyager_i2cbb_set_dir(void *cookie, uint32_t bits) 306 { 307 struct voyager_softc *sc = cookie; 308 uint32_t reg; 309 310 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0); 311 reg &= ~(1 << 13); 312 reg |= bits; 313 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg); 314 } 315 316 static uint32_t 317 voyager_i2cbb_read(void *cookie) 318 { 319 struct voyager_softc *sc = cookie; 320 uint32_t reg; 321 322 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); 323 return reg; 324 } 325 326 /* higher level I2C stuff */ 327 static int 328 voyager_i2c_acquire_bus(void *cookie, int flags) 329 { 330 struct voyager_softc *sc = cookie; 331 332 /* We also have to serialize against voyager_twiddle_bits() */ 333 mutex_enter(&sc->sc_i2c_lock); 334 return 0; 335 } 336 337 static void 338 voyager_i2c_release_bus(void *cookie, int flags) 339 { 340 struct voyager_softc *sc = cookie; 341 342 mutex_exit(&sc->sc_i2c_lock); 343 } 344 345 static int 346 voyager_i2c_send_start(void *cookie, int flags) 347 { 348 return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops)); 349 } 350 351 static int 352 voyager_i2c_send_stop(void *cookie, int flags) 353 { 354 355 return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops)); 356 } 357 358 static int 359 voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 360 { 361 /* 362 * for some reason i2c_bitbang_initiate_xfer left-shifts 363 * the I2C-address and then sets the direction bit 364 */ 365 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, 366 &voyager_i2cbb_ops)); 367 } 368 369 static int 370 voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags) 371 { 372 int ret; 373 374 ret = i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops); 375 return ret; 376 } 377 378 static int 379 voyager_i2c_write_byte(void *cookie, uint8_t val, int flags) 380 { 381 int ret; 382 383 ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops); 384 delay(500); 385 return ret; 386 } 387 388 void 389 voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits) 390 { 391 struct voyager_softc *sc = cookie; 392 uint32_t reg; 393 394 /* don't interfere with i2c ops */ 395 mutex_enter(&sc->sc_i2c_lock); 396 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum); 397 reg &= mask; 398 reg |= bits; 399 bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg); 400 mutex_exit(&sc->sc_i2c_lock); 401 } 402 403 static int 404 voyager_intr(void *cookie) 405 { 406 struct voyager_softc *sc = voyager_cookie; 407 struct voyager_intr *ih; 408 uint32_t intrs; 409 uint32_t mask, bit; 410 int num; 411 412 intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS); 413 mask = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 414 intrs &= mask; 415 416 while (intrs != 0) { 417 num = ffs32(intrs) - 1; 418 bit = 1 << num; 419 intrs &= ~bit; 420 ih = &sc->sc_intrs[num]; 421 if (ih->vih_func != NULL) { 422 if (ih->vih_arg == NULL) { 423 ih->vih_func(cookie); 424 } else { 425 ih->vih_func(ih->vih_arg); 426 } 427 } 428 ih->vih_count.ev_count++; 429 } 430 return 0; 431 } 432 433 void * 434 voyager_establish_intr(device_t dev, int bit, int (*handler)(void *), void *arg) 435 { 436 struct voyager_softc *sc = device_private(dev); 437 struct voyager_intr *ih; 438 uint32_t reg; 439 440 if ((bit < 0) || (bit > 31)) { 441 aprint_error_dev(dev, "bogus interrupt %d\n", bit); 442 return NULL; 443 } 444 445 ih = &sc->sc_intrs[bit]; 446 if (ih->vih_func != NULL) { 447 aprint_error_dev(dev, "interrupt %d is already in use\n", bit); 448 return NULL; 449 } 450 ih->vih_func = handler; 451 ih->vih_arg = arg; 452 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 453 reg |= 1 << bit; 454 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, reg); 455 456 return (void *)(uintptr_t)(0x80000000 | bit); 457 } 458 459 void 460 voyager_disestablish_intr(device_t dev, void *ih) 461 { 462 } 463 464 /* timer */ 465 #ifdef VOYAGER_DEBUG 466 static void 467 voyager_print_pwm(struct voyager_softc *sc, int pwmreg) 468 { 469 uint32_t reg; 470 471 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, pwmreg); 472 aprint_debug_dev(sc->sc_dev, "%08x: %08x = %d Hz, %d high, %d low\n", 473 pwmreg, reg, 474 96000000 / (1 << ((reg & SM502_PWM_CLOCK_DIV_MASK) >> SM502_PWM_CLOCK_DIV_SHIFT)), 475 (reg & SM502_PWM_CLOCK_HIGH_MASK) >> SM502_PWM_CLOCK_HIGH_SHIFT, 476 (reg & SM502_PWM_CLOCK_LOW_MASK) >> SM502_PWM_CLOCK_LOW_SHIFT); 477 } 478 #endif 479 480 uint32_t 481 voyager_set_pwm(int freq, int duty_cycle) 482 { 483 int ifreq, factor, bit, steps; 484 uint32_t reg = 0, hi, lo; 485 486 /* 487 * find the smallest divider that gets us within 4096 steps of the 488 * target frequency 489 */ 490 ifreq = freq * 4096; 491 factor = 96000000 / ifreq; 492 bit = fls32(factor); 493 factor = 1 << bit; 494 steps = 96000000 / (factor * freq); 495 /* can't have it all off */ 496 if (duty_cycle < 1) 497 duty_cycle = 1; 498 /* can't be always on either */ 499 if (duty_cycle > 999) 500 duty_cycle = 999; 501 hi = steps * duty_cycle / 1000; 502 if (hi < 1) 503 hi = 1; 504 lo = steps - hi; 505 if (lo < 1) { 506 hi = steps - 1; 507 lo = 1; 508 } 509 DPRINTF("%d hz -> %d, %d, %d / %d\n", freq, factor, steps, lo, hi); 510 reg = ((hi - 1) & 0xfff) << SM502_PWM_CLOCK_HIGH_SHIFT; 511 reg |= ((lo - 1) & 0xfff) << SM502_PWM_CLOCK_LOW_SHIFT; 512 reg |= (bit & 0xf) << SM502_PWM_CLOCK_DIV_SHIFT; 513 DPRINTF("reg: %08x\n", reg); 514 return reg; 515 } 516