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