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