1 /* $NetBSD: mvsocgpp.c,v 1.3 2011/08/13 15:38:47 jakllsch Exp $ */ 2 /* 3 * Copyright (c) 2008, 2010 KIYOHARA Takashi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: mvsocgpp.c,v 1.3 2011/08/13 15:38:47 jakllsch Exp $"); 30 31 #include "gpio.h" 32 33 #define _INTR_PRIVATE 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/device.h> 38 #include <sys/errno.h> 39 #include <sys/evcnt.h> 40 #include <sys/gpio.h> 41 #include <sys/kmem.h> 42 43 #include <machine/intr.h> 44 45 #include <arm/marvell/mvsocreg.h> 46 #include <arm/marvell/mvsocvar.h> 47 #include <arm/marvell/mvsocgppreg.h> 48 #include <arm/marvell/mvsocgppvar.h> 49 #include <arm/pic/picvar.h> 50 51 #include <dev/marvell/marvellvar.h> 52 53 #if NGPIO > 0 54 #include <sys/gpio.h> 55 #include <dev/gpio/gpiovar.h> 56 #endif 57 58 #define MVSOCGPP_DUMPREG 59 60 #define MVSOCGPP_READ(sc, reg) \ 61 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)) 62 #define MVSOCGPP_WRITE(sc, reg, val) \ 63 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 64 65 struct mvsocgpp_softc { 66 device_t sc_dev; 67 68 bus_space_tag_t sc_iot; 69 bus_space_handle_t sc_ioh; 70 71 struct mvsocgpp_pic { 72 struct pic_softc gpio_pic; 73 int group; 74 uint32_t edge; 75 uint32_t level; 76 } *sc_pic; 77 78 #if NGPIO > 0 79 struct gpio_chipset_tag sc_gpio_chipset; 80 gpio_pin_t *sc_pins; 81 #endif 82 }; 83 84 static int mvsocgpp_match(device_t, struct cfdata *, void *); 85 static void mvsocgpp_attach(device_t, device_t, void *); 86 87 #ifdef MVSOCGPP_DUMPREG 88 static void mvsocgpp_dump_reg(struct mvsocgpp_softc *); 89 #endif 90 91 static void gpio_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t); 92 static void gpio_pic_block_irqs(struct pic_softc *, size_t, uint32_t); 93 static int gpio_pic_find_pending_irqs(struct pic_softc *); 94 static void gpio_pic_establish_irq(struct pic_softc *, struct intrsource *); 95 96 static struct pic_ops gpio_pic_ops = { 97 .pic_unblock_irqs = gpio_pic_unblock_irqs, 98 .pic_block_irqs = gpio_pic_block_irqs, 99 .pic_find_pending_irqs = gpio_pic_find_pending_irqs, 100 .pic_establish_irq = gpio_pic_establish_irq, 101 }; 102 103 static struct mvsocgpp_softc *mvsocgpp_softc; /* One unit per One SoC */ 104 int gpp_irqbase = 0; 105 int gpp_npins = 0; 106 107 108 CFATTACH_DECL_NEW(mvsocgpp, sizeof(struct mvsocgpp_softc), 109 mvsocgpp_match, mvsocgpp_attach, NULL, NULL); 110 111 112 /* ARGSUSED */ 113 static int 114 mvsocgpp_match(device_t parent, struct cfdata *match, void *aux) 115 { 116 struct marvell_attach_args *mva = aux; 117 118 if (strcmp(mva->mva_name, match->cf_name) != 0) 119 return 0; 120 if (mva->mva_offset == MVA_OFFSET_DEFAULT || 121 mva->mva_irq == MVA_IRQ_DEFAULT) 122 return 0; 123 124 mva->mva_size = MVSOC_GPP_SIZE; 125 return 1; 126 } 127 128 /* ARGSUSED */ 129 static void 130 mvsocgpp_attach(device_t parent, device_t self, void *aux) 131 { 132 struct mvsocgpp_softc *sc = device_private(self); 133 struct marvell_attach_args *mva = aux; 134 struct pic_softc *gpio_pic; 135 #if NGPIO > 0 136 struct gpiobus_attach_args gba; 137 gpio_pin_t *pins; 138 uint32_t mask, dir, valin, valout, polarity, blink; 139 #endif 140 int i, j; 141 void *ih; 142 143 dir = valin = valout = polarity = blink = 0; 144 145 aprint_normal(": Marvell SoC General Purpose I/O Port Interface\n"); 146 aprint_naive("\n"); 147 148 sc->sc_dev = self; 149 sc->sc_iot = mva->mva_iot; 150 /* Map I/O registers for oriongpp */ 151 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, 152 mva->mva_offset, mva->mva_size, &sc->sc_ioh)) { 153 aprint_error_dev(self, "can't map registers\n"); 154 return; 155 } 156 157 if (gpp_npins > 0) 158 aprint_normal_dev(self, "%d gpio pins\n", gpp_npins); 159 else { 160 aprint_error_dev(self, "gpp_npins not initialized\n"); 161 return; 162 } 163 164 mvsocgpp_softc = sc; 165 166 for (i = 0; i < gpp_npins; i += 32) 167 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOIC(i), 0); 168 169 sc->sc_pic = 170 kmem_zalloc(sizeof(struct mvsocgpp_pic) * gpp_npins / 8, KM_SLEEP); 171 for (i = 0, j = 0; i < gpp_npins; i += 8, j++) { 172 gpio_pic = &(sc->sc_pic + j)->gpio_pic; 173 gpio_pic->pic_ops = &gpio_pic_ops; 174 snprintf(gpio_pic->pic_name, sizeof(gpio_pic->pic_name), 175 "%s[%d:%d]", device_xname(self), i + 7, i); 176 gpio_pic->pic_maxsources = 177 (gpp_npins - i) > 8 ? 8 : gpp_npins - i; 178 pic_add(gpio_pic, gpp_irqbase + i); 179 aprint_normal_dev(self, "interrupts %d..%d", 180 gpp_irqbase + i, gpp_irqbase + i + 7); 181 ih = intr_establish(mva->mva_irq + j, 182 IPL_HIGH, IST_LEVEL_HIGH, pic_handle_intr, gpio_pic); 183 aprint_normal(", intr %d\n", mva->mva_irq + j); 184 185 (sc->sc_pic + j)->group = j; 186 } 187 188 #ifdef MVSOCGPP_DUMPREG 189 mvsocgpp_dump_reg(sc); 190 #endif 191 192 #if NGPIO > 0 193 sc->sc_pins = kmem_zalloc(sizeof(gpio_pin_t) * gpp_npins, KM_SLEEP); 194 195 for (i = 0, mask = 1; i < gpp_npins; i++, mask <<= 1) { 196 if ((i & (32 - 1)) == 0) { 197 mask = 1; 198 dir = MVSOCGPP_READ(sc, MVSOCGPP_GPIODOEC(i)); 199 valin = MVSOCGPP_READ(sc, MVSOCGPP_GPIODI(i)); 200 valout = MVSOCGPP_READ(sc, MVSOCGPP_GPIODO(i)); 201 polarity = MVSOCGPP_READ(sc, MVSOCGPP_GPIODIP(i)); 202 blink = MVSOCGPP_READ(sc, MVSOCGPP_GPIOBE(i)); 203 } 204 pins = &sc->sc_pins[i]; 205 pins->pin_num = i; 206 pins->pin_caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | 207 GPIO_PIN_INVIN | GPIO_PIN_PULSATE); 208 if (dir & mask) { 209 pins->pin_flags = GPIO_PIN_INPUT; 210 pins->pin_state = 211 (valin & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW; 212 } else { 213 pins->pin_flags = GPIO_PIN_OUTPUT; 214 pins->pin_state = 215 (valout & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW; 216 } 217 if (polarity & mask) { 218 pins->pin_flags |= GPIO_PIN_INVIN; 219 } 220 if (blink & mask) { 221 pins->pin_flags |= GPIO_PIN_PULSATE; 222 } 223 } 224 sc->sc_gpio_chipset.gp_cookie = sc; 225 sc->sc_gpio_chipset.gp_pin_read = mvsocgpp_pin_read; 226 sc->sc_gpio_chipset.gp_pin_write = mvsocgpp_pin_write; 227 sc->sc_gpio_chipset.gp_pin_ctl = mvsocgpp_pin_ctl; 228 gba.gba_gc = &sc->sc_gpio_chipset; 229 gba.gba_pins = sc->sc_pins; 230 gba.gba_npins = gpp_npins; 231 config_found_ia(self, "gpiobus", &gba, gpiobus_print); 232 #endif 233 } 234 235 /* 236 * arch/arm/pic functions. 237 */ 238 239 static void 240 gpio_pic_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask) 241 { 242 struct mvsocgpp_softc *sc = mvsocgpp_softc; 243 struct mvsocgpp_pic *mvsocgpp_pic = (struct mvsocgpp_pic *)pic; 244 uint32_t mask; 245 int pin = mvsocgpp_pic->group << 3; 246 247 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOIC(pin), 248 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIC(pin)) & ~irq_mask); 249 if (irq_mask & mvsocgpp_pic->edge) { 250 mask = MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(pin)); 251 mask |= (irq_mask & mvsocgpp_pic->edge); 252 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOIM(pin), mask); 253 } 254 if (irq_mask & mvsocgpp_pic->level) { 255 mask = MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(pin)); 256 mask |= (irq_mask & mvsocgpp_pic->level); 257 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOILM(pin), mask); 258 } 259 } 260 261 /* ARGSUSED */ 262 static void 263 gpio_pic_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t irq_mask) 264 { 265 struct mvsocgpp_softc *sc = mvsocgpp_softc; 266 struct mvsocgpp_pic *mvsocgpp_pic = (struct mvsocgpp_pic *)pic; 267 int pin = mvsocgpp_pic->group << 3; 268 269 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOIM(pin), 270 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(pin)) & ~irq_mask); 271 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOILM(pin), 272 MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(pin)) & ~irq_mask); 273 } 274 275 static int 276 gpio_pic_find_pending_irqs(struct pic_softc *pic) 277 { 278 struct mvsocgpp_softc *sc = mvsocgpp_softc; 279 struct mvsocgpp_pic *mvsocgpp_pic = (struct mvsocgpp_pic *)pic; 280 uint32_t pending; 281 int pin = mvsocgpp_pic->group << 3; 282 283 pending = MVSOCGPP_READ(sc, MVSOCGPP_GPIOIC(pin)); 284 pending &= (0xff << mvsocgpp_pic->group); 285 pending &= (MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(pin)) | 286 MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(pin))); 287 288 if (pending == 0) 289 return 0; 290 291 return pic_mark_pending_sources(pic, 0, pending); 292 } 293 294 static void 295 gpio_pic_establish_irq(struct pic_softc *pic, struct intrsource *is) 296 { 297 struct mvsocgpp_softc *sc = mvsocgpp_softc; 298 struct mvsocgpp_pic *mvsocgpp_pic = (struct mvsocgpp_pic *)pic; 299 uint32_t im, ilm, mask; 300 int type, pin; 301 302 type = is->is_type; 303 pin = pic->pic_irqbase + is->is_irq - gpp_irqbase; 304 mask = MVSOCGPP_GPIOPIN(pin); 305 306 switch (type) { 307 case IST_LEVEL_LOW: 308 case IST_EDGE_FALLING: 309 mvsocgpp_pin_ctl(NULL, pin, GPIO_PIN_INPUT | GPIO_PIN_INVIN); 310 break; 311 312 case IST_LEVEL_HIGH: 313 case IST_EDGE_RISING: 314 mvsocgpp_pin_ctl(NULL, pin, GPIO_PIN_INPUT); 315 break; 316 317 default: 318 panic("unknwon interrupt type %d for pin %d.\n", type, pin); 319 } 320 321 im = MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(pin)); 322 ilm = MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(pin)); 323 switch (type) { 324 case IST_EDGE_FALLING: 325 case IST_EDGE_RISING: 326 im |= mask; 327 ilm &= ~mask; 328 mvsocgpp_pic->edge |= mask; 329 mvsocgpp_pic->level &= ~mask; 330 break; 331 332 case IST_LEVEL_LOW: 333 case IST_LEVEL_HIGH: 334 im &= ~mask; 335 ilm |= mask; 336 mvsocgpp_pic->edge &= ~mask; 337 mvsocgpp_pic->level |= mask; 338 break; 339 } 340 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOIM(pin), im); 341 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOILM(pin), ilm); 342 } 343 344 345 /* 346 * gpio(4) functions, and can call you. 347 */ 348 349 /* ARGSUSED */ 350 int 351 mvsocgpp_pin_read(void *arg, int pin) 352 { 353 struct mvsocgpp_softc *sc = mvsocgpp_softc; 354 uint32_t val; 355 356 KASSERT(sc != NULL); 357 358 val = MVSOCGPP_READ(sc, MVSOCGPP_GPIODI(pin)); 359 return (val & MVSOCGPP_GPIOPIN(pin)) != 0; 360 } 361 362 /* ARGSUSED */ 363 void 364 mvsocgpp_pin_write(void *arg, int pin, int value) 365 { 366 struct mvsocgpp_softc *sc = mvsocgpp_softc; 367 uint32_t old, new, mask = MVSOCGPP_GPIOPIN(pin); 368 369 KASSERT(sc != NULL); 370 371 old = MVSOCGPP_READ(sc, MVSOCGPP_GPIODO(pin)); 372 if (value) 373 new = old | mask; 374 else 375 new = old & ~mask; 376 if (new != old) 377 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIODO(pin), new); 378 } 379 380 /* ARGSUSED */ 381 void 382 mvsocgpp_pin_ctl(void *arg, int pin, int flags) 383 { 384 struct mvsocgpp_softc *sc = mvsocgpp_softc; 385 uint32_t old, new, mask = MVSOCGPP_GPIOPIN(pin); 386 387 KASSERT(sc != NULL); 388 389 old = MVSOCGPP_READ(sc, MVSOCGPP_GPIODOEC(pin)); 390 switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) { 391 case GPIO_PIN_INPUT: 392 new = old | mask; 393 break; 394 395 case GPIO_PIN_OUTPUT: 396 new = old & ~mask; 397 break; 398 399 default: 400 return; 401 } 402 if (new != old) 403 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIODOEC(pin), new); 404 405 /* Blink every 2^24 TCLK */ 406 old = MVSOCGPP_READ(sc, MVSOCGPP_GPIOBE(pin)); 407 if (flags & GPIO_PIN_PULSATE) 408 new = old | mask; 409 else 410 new = old & ~mask; 411 if (new != old) 412 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIOBE(pin), new); 413 414 old = MVSOCGPP_READ(sc, MVSOCGPP_GPIODIP(pin)); 415 if (flags & GPIO_PIN_INVIN) 416 new = old | mask; 417 else 418 new = old & ~mask; 419 if (new != old) 420 MVSOCGPP_WRITE(sc, MVSOCGPP_GPIODIP(pin), new); 421 } 422 423 424 #ifdef MVSOCGPP_DUMPREG 425 static void 426 mvsocgpp_dump_reg(struct mvsocgpp_softc *sc) 427 { 428 429 aprint_normal_dev(sc->sc_dev, " Data Out: \t0x%08x\n", 430 MVSOCGPP_READ(sc, MVSOCGPP_GPIODO(0))); 431 aprint_normal_dev(sc->sc_dev, " Data Out Enable Control: \t0x%08x\n", 432 MVSOCGPP_READ(sc, MVSOCGPP_GPIODOEC(0))); 433 aprint_normal_dev(sc->sc_dev, " Data Blink Enable: \t0x%08x\n", 434 MVSOCGPP_READ(sc, MVSOCGPP_GPIOBE(0))); 435 aprint_normal_dev(sc->sc_dev, " Data In Polarity: \t0x%08x\n", 436 MVSOCGPP_READ(sc, MVSOCGPP_GPIODIP(0))); 437 aprint_normal_dev(sc->sc_dev, " Data In: \t0x%08x\n", 438 MVSOCGPP_READ(sc, MVSOCGPP_GPIODI(0))); 439 aprint_normal_dev(sc->sc_dev, " Interrupt Cause: \t0x%08x\n", 440 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIC(0))); 441 aprint_normal_dev(sc->sc_dev, " Interrupt Mask: \t0x%08x\n", 442 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(0))); 443 aprint_normal_dev(sc->sc_dev, " Interrupt Level Mask: \t0x%08x\n", 444 MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(0))); 445 446 if (gpp_npins <= 32) 447 return; 448 449 aprint_normal_dev(sc->sc_dev, " High Data Out: \t0x%08x\n", 450 MVSOCGPP_READ(sc, MVSOCGPP_GPIODO(32))); 451 aprint_normal_dev(sc->sc_dev, " High Data Out Enable Ctrl:\t0x%08x\n", 452 MVSOCGPP_READ(sc, MVSOCGPP_GPIODOEC(32))); 453 aprint_normal_dev(sc->sc_dev, " High Blink Enable: \t0x%08x\n", 454 MVSOCGPP_READ(sc, MVSOCGPP_GPIOBE(32))); 455 aprint_normal_dev(sc->sc_dev, " High Data In Polarity: \t0x%08x\n", 456 MVSOCGPP_READ(sc, MVSOCGPP_GPIODIP(32))); 457 aprint_normal_dev(sc->sc_dev, " High Data In: \t0x%08x\n", 458 MVSOCGPP_READ(sc, MVSOCGPP_GPIODI(32))); 459 aprint_normal_dev(sc->sc_dev, " High Interrupt Cause: \t0x%08x\n", 460 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIC(32))); 461 aprint_normal_dev(sc->sc_dev, " High Interrupt Mask: \t0x%08x\n", 462 MVSOCGPP_READ(sc, MVSOCGPP_GPIOIM(32))); 463 aprint_normal_dev(sc->sc_dev, " High Interrupt Level Mask:\t0x%08x\n", 464 MVSOCGPP_READ(sc, MVSOCGPP_GPIOILM(32))); 465 } 466 #endif 467