1 /* $NetBSD: voyager.c,v 1.22 2026/07/30 16:17:08 rkujawa 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.22 2026/07/30 16:17:08 rkujawa 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 #include "vac.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 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_send_start(void *, int); 102 static int voyager_i2c_send_stop(void *, int); 103 static int voyager_i2c_initiate_xfer(void *, i2c_addr_t, int); 104 static int voyager_i2c_read_byte(void *, uint8_t *, int); 105 static int voyager_i2c_write_byte(void *, uint8_t, int); 106 107 /* I2C bitbang glue */ 108 static void voyager_i2cbb_set_bits(void *, uint32_t); 109 static void voyager_i2cbb_set_dir(void *, uint32_t); 110 static uint32_t voyager_i2cbb_read(void *); 111 112 static const struct i2c_bitbang_ops voyager_i2cbb_ops = { 113 voyager_i2cbb_set_bits, 114 voyager_i2cbb_set_dir, 115 voyager_i2cbb_read, 116 { 117 1 << 13, 118 1 << 6, 119 1 << 13, 120 0 121 } 122 }; 123 #define GPIO_I2C_BITS ((1 << 6) | (1 << 13)) 124 125 #ifdef VOYAGER_DEBUG 126 static void voyager_print_pwm(struct voyager_softc *, int); 127 #endif 128 129 static int 130 voyager_match(device_t parent, cfdata_t match, void *aux) 131 { 132 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 133 134 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 135 return 0; 136 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SILMOTION) 137 return 0; 138 139 /* only chip tested on so far - may need a list */ 140 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SILMOTION_SM502) 141 return 100; 142 return (0); 143 } 144 145 static void 146 voyager_attach(device_t parent, device_t self, void *aux) 147 { 148 struct voyager_softc *sc = device_private(self); 149 struct pci_attach_args *pa = aux; 150 pci_intr_handle_t ih; 151 struct voyager_attach_args vaa; 152 uint32_t reg; 153 const char *intrstr; 154 int i; 155 char intrbuf[PCI_INTRSTR_LEN]; 156 157 sc->sc_pc = pa->pa_pc; 158 sc->sc_pcitag = pa->pa_tag; 159 sc->sc_memt = pa->pa_memt; 160 sc->sc_iot = pa->pa_iot; 161 sc->sc_dev = self; 162 163 pci_aprint_devinfo(pa, NULL); 164 165 if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0, 166 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 167 aprint_error_dev(self, "failed to map registers\n"); 168 return; 169 } 170 171 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 172 BUS_SPACE_MAP_LINEAR, 173 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { 174 aprint_error_dev(self, "failed to map the frame buffer\n"); 175 bus_space_unmap(sc->sc_memt, sc->sc_regh, sc->sc_regsize); 176 return; 177 } 178 179 voyager_cookie = sc; 180 181 /* disable all interrupts */ 182 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, 0); 183 /* ack anything firmware left pending */ 184 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_CLEAR_R, 185 0xffffffff); 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 /* 196 * Map and establish the interrupt (or not). 197 */ 198 if (pci_intr_map(pa, &ih)) { 199 aprint_normal_dev(self, 200 "no interrupt mapped, continuing without\n"); 201 sc->sc_ih = NULL; 202 } else { 203 intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, 204 sizeof(intrbuf)); 205 sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, ih, IPL_VM, 206 voyager_intr, NULL, /* so we get the clock frame instead */ 207 device_xname(self)); 208 if (sc->sc_ih == NULL) { 209 aprint_error_dev(self, "couldn't establish interrupt"); 210 if (intrstr != NULL) 211 aprint_error(" at %s", intrstr); 212 aprint_error("\n"); 213 return; 214 } 215 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 216 } 217 218 #ifdef VOYAGER_DEBUG 219 voyager_print_pwm(sc, SM502_PWM0); 220 voyager_print_pwm(sc, SM502_PWM1); 221 voyager_print_pwm(sc, SM502_PWM2); 222 #endif 223 224 /* attach the framebuffer driver */ 225 vaa.vaa_memh = sc->sc_fbh; 226 vaa.vaa_mem_pa = sc->sc_fb; 227 vaa.vaa_regh = sc->sc_regh; 228 vaa.vaa_reg_pa = sc->sc_reg; 229 vaa.vaa_tag = sc->sc_memt; 230 vaa.vaa_pc = sc->sc_pc; 231 vaa.vaa_pcitag = sc->sc_pcitag; 232 #if NVOYAGERFB > 0 233 strcpy(vaa.vaa_name, "voyagerfb"); 234 config_found(sc->sc_dev, &vaa, voyager_print, 235 CFARGS(.iattr = "voyagerbus")); 236 #endif 237 #if NPWMCLOCK > 0 238 strcpy(vaa.vaa_name, "pwmclock"); 239 config_found(sc->sc_dev, &vaa, voyager_print, 240 CFARGS(.iattr = "voyagerbus")); 241 #endif 242 #if NVAC > 0 243 strcpy(vaa.vaa_name, "vac"); 244 config_found(sc->sc_dev, &vaa, voyager_print, 245 CFARGS(.iattr = "voyagerbus")); 246 #endif 247 /* 248 * see if the i2c pins are configured as gpio and if so, use them 249 * should probably be a compile time option 250 */ 251 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL); 252 if ((reg & GPIO_I2C_BITS) == 0) { 253 /* both bits as outputs */ 254 voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS); 255 256 /* Fill in the i2c tag */ 257 iic_tag_init(&sc->sc_i2c); 258 sc->sc_i2c.ic_cookie = sc; 259 sc->sc_i2c.ic_send_start = voyager_i2c_send_start; 260 sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop; 261 sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer; 262 sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte; 263 sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte; 264 iicbus_attach(self, &sc->sc_i2c); 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 static int 319 voyager_i2c_send_start(void *cookie, int flags) 320 { 321 return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops)); 322 } 323 324 static int 325 voyager_i2c_send_stop(void *cookie, int flags) 326 { 327 328 return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops)); 329 } 330 331 static int 332 voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 333 { 334 /* 335 * for some reason i2c_bitbang_initiate_xfer left-shifts 336 * the I2C-address and then sets the direction bit 337 */ 338 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, 339 &voyager_i2cbb_ops)); 340 } 341 342 static int 343 voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags) 344 { 345 int ret; 346 347 ret = i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops); 348 return ret; 349 } 350 351 static int 352 voyager_i2c_write_byte(void *cookie, uint8_t val, int flags) 353 { 354 int ret; 355 356 ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops); 357 delay(500); 358 return ret; 359 } 360 361 void 362 voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits) 363 { 364 struct voyager_softc *sc = cookie; 365 uint32_t reg; 366 367 /* don't interfere with i2c ops */ 368 mutex_enter(&sc->sc_i2c.ic_bus_lock); 369 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum); 370 reg &= mask; 371 reg |= bits; 372 bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg); 373 mutex_exit(&sc->sc_i2c.ic_bus_lock); 374 } 375 376 static int 377 voyager_intr(void *cookie) 378 { 379 struct voyager_softc *sc = voyager_cookie; 380 struct voyager_intr *ih; 381 uint32_t intrs; 382 uint32_t mask, bit; 383 int num; 384 385 intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS); 386 mask = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 387 intrs &= mask; 388 389 while (intrs != 0) { 390 num = ffs32(intrs) - 1; 391 bit = 1 << num; 392 intrs &= ~bit; 393 ih = &sc->sc_intrs[num]; 394 if (ih->vih_func != NULL) { 395 if (ih->vih_arg == NULL) { 396 ih->vih_func(cookie); 397 } else { 398 ih->vih_func(ih->vih_arg); 399 } 400 } 401 ih->vih_count.ev_count++; 402 } 403 return 0; 404 } 405 406 void * 407 voyager_establish_intr(device_t dev, int bit, int (*handler)(void *), void *arg) 408 { 409 struct voyager_softc *sc = device_private(dev); 410 struct voyager_intr *ih; 411 uint32_t reg; 412 413 if ((bit < 0) || (bit > 31)) { 414 aprint_error_dev(dev, "bogus interrupt %d\n", bit); 415 return NULL; 416 } 417 418 ih = &sc->sc_intrs[bit]; 419 if (ih->vih_func != NULL) { 420 aprint_error_dev(dev, "interrupt %d is already in use\n", bit); 421 return NULL; 422 } 423 ih->vih_func = handler; 424 ih->vih_arg = arg; 425 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK); 426 reg |= 1 << bit; 427 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, reg); 428 429 return (void *)(uintptr_t)(0x80000000 | bit); 430 } 431 432 void 433 voyager_disestablish_intr(device_t dev, void *ih) 434 { 435 } 436 437 /* timer */ 438 #ifdef VOYAGER_DEBUG 439 static void 440 voyager_print_pwm(struct voyager_softc *sc, int pwmreg) 441 { 442 uint32_t reg; 443 444 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, pwmreg); 445 aprint_debug_dev(sc->sc_dev, "%08x: %08x = %d Hz, %d high, %d low\n", 446 pwmreg, reg, 447 96000000 / (1 << ((reg & SM502_PWM_CLOCK_DIV_MASK) >> SM502_PWM_CLOCK_DIV_SHIFT)), 448 (reg & SM502_PWM_CLOCK_HIGH_MASK) >> SM502_PWM_CLOCK_HIGH_SHIFT, 449 (reg & SM502_PWM_CLOCK_LOW_MASK) >> SM502_PWM_CLOCK_LOW_SHIFT); 450 } 451 #endif 452 453 uint32_t 454 voyager_set_pwm(int freq, int duty_cycle) 455 { 456 int ifreq, factor, bit, steps; 457 uint32_t reg = 0, hi, lo; 458 459 /* 460 * find the smallest divider that gets us within 4096 steps of the 461 * target frequency 462 */ 463 ifreq = freq * 4096; 464 factor = 96000000 / ifreq; 465 bit = fls32(factor); 466 factor = 1 << bit; 467 steps = 96000000 / (factor * freq); 468 /* can't have it all off */ 469 if (duty_cycle < 1) 470 duty_cycle = 1; 471 /* can't be always on either */ 472 if (duty_cycle > 999) 473 duty_cycle = 999; 474 hi = steps * duty_cycle / 1000; 475 if (hi < 1) 476 hi = 1; 477 lo = steps - hi; 478 if (lo < 1) { 479 hi = steps - 1; 480 lo = 1; 481 } 482 DPRINTF("%d hz -> %d, %d, %d / %d\n", freq, factor, steps, lo, hi); 483 reg = ((hi - 1) & 0xfff) << SM502_PWM_CLOCK_HIGH_SHIFT; 484 reg |= ((lo - 1) & 0xfff) << SM502_PWM_CLOCK_LOW_SHIFT; 485 reg |= (bit & 0xf) << SM502_PWM_CLOCK_DIV_SHIFT; 486 DPRINTF("reg: %08x\n", reg); 487 return reg; 488 } 489