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