1 1.16 riastrad /* $NetBSD: obio.c,v 1.16 2023/07/14 12:57:10 riastradh Exp $ */ 2 1.1 bsh 3 1.1 bsh /* 4 1.1 bsh * Copyright (c) 2002, 2003, 2005 Genetec corp. All rights reserved. 5 1.1 bsh * Written by Hiroyuki Bessho for Genetec corp. 6 1.1 bsh * 7 1.1 bsh * Redistribution and use in source and binary forms, with or without 8 1.1 bsh * modification, are permitted provided that the following conditions 9 1.1 bsh * are met: 10 1.1 bsh * 1. Redistributions of source code must retain the above copyright 11 1.1 bsh * notice, this list of conditions and the following disclaimer. 12 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 bsh * notice, this list of conditions and the following disclaimer in the 14 1.1 bsh * documentation and/or other materials provided with the distribution. 15 1.1 bsh * 3. The name of Genetec corp. may not be used to endorse 16 1.1 bsh * or promote products derived from this software without specific prior 17 1.1 bsh * written permission. 18 1.1 bsh * 19 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND 20 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP. 23 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 bsh * POSSIBILITY OF SUCH DAMAGE. 30 1.1 bsh */ 31 1.1 bsh 32 1.1 bsh 33 1.1 bsh #include <sys/param.h> 34 1.1 bsh #include <sys/systm.h> 35 1.1 bsh #include <sys/device.h> 36 1.1 bsh #include <sys/kernel.h> 37 1.1 bsh #include <sys/reboot.h> 38 1.14 riastrad #include <sys/bitops.h> 39 1.1 bsh 40 1.1 bsh #include <machine/cpu.h> 41 1.10 dyoung #include <sys/bus.h> 42 1.1 bsh #include <machine/intr.h> 43 1.1 bsh #include <arm/cpufunc.h> 44 1.1 bsh 45 1.1 bsh #include <arm/mainbus/mainbus.h> 46 1.5 nonaka #include <arm/xscale/pxa2x0cpu.h> 47 1.1 bsh #include <arm/xscale/pxa2x0reg.h> 48 1.1 bsh #include <arm/xscale/pxa2x0var.h> 49 1.1 bsh #include <arm/xscale/pxa2x0_gpio.h> 50 1.1 bsh #include <arm/sa11x0/sa11x0_var.h> 51 1.1 bsh #include <evbarm/g42xxeb/g42xxeb_reg.h> 52 1.1 bsh #include <evbarm/g42xxeb/g42xxeb_var.h> 53 1.1 bsh 54 1.1 bsh #include "locators.h" 55 1.1 bsh 56 1.1 bsh /* prototypes */ 57 1.8 bsh static int obio_match(device_t, cfdata_t, void *); 58 1.8 bsh static void obio_attach(device_t, device_t, void *); 59 1.8 bsh static int obio_search(device_t, cfdata_t, const int *, void *); 60 1.1 bsh static int obio_print(void *, const char *); 61 1.1 bsh 62 1.1 bsh /* attach structures */ 63 1.8 bsh CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc), obio_match, obio_attach, 64 1.1 bsh NULL, NULL); 65 1.1 bsh 66 1.1 bsh static int 67 1.1 bsh obio_spurious(void *arg) 68 1.1 bsh { 69 1.1 bsh int irqno = (int)arg; 70 1.1 bsh 71 1.1 bsh printf("Spurious interrupt %d on On-board peripheral", irqno); 72 1.1 bsh return 1; 73 1.1 bsh } 74 1.1 bsh 75 1.1 bsh 76 1.1 bsh /* 77 1.1 bsh * interrupt handler for GPIO0 (on-board peripherals) 78 1.1 bsh * 79 1.1 bsh * On G4250ebx, 10 interrupts are ORed through on-board logic, 80 1.1 bsh * and routed to GPIO0 of PXA250 processor. 81 1.1 bsh */ 82 1.1 bsh static int 83 1.1 bsh obio_intr(void *arg) 84 1.1 bsh { 85 1.1 bsh int irqno, pending; 86 1.1 bsh struct obio_softc *sc = (struct obio_softc *)arg; 87 1.1 bsh int n=0; 88 1.1 bsh 89 1.1 bsh #define get_pending(sc) \ 90 1.1 bsh (bus_space_read_2( sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1) \ 91 1.1 bsh & ~(sc->sc_intr_pending|sc->sc_intr_mask)) 92 1.1 bsh 93 1.1 bsh #ifdef DEBUG 94 1.1 bsh printf("obio_intr: pend=%x, mask=%x, pend=%x, mask=%x\n", 95 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1), 96 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK), 97 1.1 bsh sc->sc_intr_pending, 98 1.1 bsh sc->sc_intr_mask); 99 1.1 bsh #endif 100 1.1 bsh 101 1.1 bsh for (pending = get_pending(sc); 102 1.14 riastrad (irqno = fls32(pending) - 1) >= 0; 103 1.1 bsh pending = get_pending(sc)) { 104 1.1 bsh 105 1.1 bsh /* reset pending bit */ 106 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, 107 1.1 bsh G42XXEB_INTSTS1, ~(1<<irqno)); 108 1.1 bsh 109 1.1 bsh #if 0 110 1.1 bsh if (sc->sc_handler[irqno].level > saved_spl_level) { 111 1.1 bsh int spl_save = _splraise(sc->sc_handler[irqno].level); 112 1.1 bsh (* sc->sc_handler[irqno].func)( 113 1.1 bsh sc->sc_handler[irqno].arg); 114 1.1 bsh splx(spl_save); 115 1.1 bsh } 116 1.1 bsh else 117 1.1 bsh #endif 118 1.1 bsh { 119 1.1 bsh int psw = disable_interrupts(I32_bit); /* XXX */ 120 1.1 bsh 121 1.1 bsh /* mask this interrupt until software 122 1.1 bsh interrupt is handled. */ 123 1.1 bsh sc->sc_intr_pending |= (1U<<irqno); 124 1.1 bsh obio_update_intrmask(sc); 125 1.1 bsh 126 1.1 bsh restore_interrupts(psw); 127 1.1 bsh ++n; 128 1.1 bsh } 129 1.1 bsh #ifdef DIAGNOSTIC 130 1.1 bsh if (n > 1000) 131 1.1 bsh panic("obio_intr: stayed too long"); 132 1.1 bsh #endif 133 1.1 bsh } 134 1.1 bsh 135 1.1 bsh if (n > 0) { 136 1.1 bsh /* handle it later */ 137 1.6 matt softint_schedule(sc->sc_si); 138 1.1 bsh } 139 1.1 bsh 140 1.1 bsh /* GPIO interrupt is edge triggered. make a pulse 141 1.1 bsh to let Cotulla notice when other interrupts are 142 1.1 bsh still pending */ 143 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, 144 1.1 bsh G42XXEB_INTMASK, 0xffff); 145 1.1 bsh obio_update_intrmask(sc); 146 1.1 bsh 147 1.1 bsh return 1; 148 1.1 bsh } 149 1.1 bsh 150 1.1 bsh static void 151 1.6 matt obio_softint(void *arg) 152 1.1 bsh { 153 1.1 bsh struct obio_softc *sc = (struct obio_softc *)arg; 154 1.1 bsh int irqno; 155 1.7 matt int spl_save = curcpl(); 156 1.1 bsh int psw; 157 1.1 bsh 158 1.1 bsh psw = disable_interrupts(I32_bit); 159 1.16 riastrad while ((irqno = fls32(sc->sc_intr_pending) - 1) >= 0) { 160 1.1 bsh sc->sc_intr_pending &= ~(1U<<irqno); 161 1.1 bsh 162 1.1 bsh restore_interrupts(psw); 163 1.1 bsh 164 1.1 bsh _splraise(sc->sc_handler[irqno].level); 165 1.1 bsh (* sc->sc_handler[irqno].func)( 166 1.1 bsh sc->sc_handler[irqno].arg); 167 1.1 bsh splx(spl_save); 168 1.1 bsh 169 1.1 bsh psw = disable_interrupts(I32_bit); 170 1.1 bsh } 171 1.1 bsh 172 1.1 bsh /* assert(sc->sc_intr_pending==0) */ 173 1.1 bsh 174 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, 175 1.1 bsh G42XXEB_INTMASK, 0xffff); 176 1.1 bsh obio_update_intrmask(sc); 177 1.1 bsh 178 1.1 bsh restore_interrupts(psw); 179 1.1 bsh } 180 1.1 bsh 181 1.1 bsh /* 182 1.1 bsh * int obio_print(void *aux, const char *name) 183 1.1 bsh * print configuration info for children 184 1.1 bsh */ 185 1.1 bsh 186 1.1 bsh static int 187 1.1 bsh obio_print(void *aux, const char *name) 188 1.1 bsh { 189 1.1 bsh struct obio_attach_args *oba = (struct obio_attach_args*)aux; 190 1.1 bsh 191 1.1 bsh if (oba->oba_addr != OBIOCF_ADDR_DEFAULT) 192 1.8 bsh aprint_normal(" addr 0x%lx", oba->oba_addr); 193 1.1 bsh if (oba->oba_intr > 0) 194 1.8 bsh aprint_normal(" intr %d", oba->oba_intr); 195 1.1 bsh return (UNCONF); 196 1.1 bsh } 197 1.1 bsh 198 1.1 bsh int 199 1.11 chs obio_match(device_t parent, cfdata_t match, void *aux) 200 1.1 bsh { 201 1.1 bsh return 1; 202 1.1 bsh } 203 1.1 bsh 204 1.1 bsh void 205 1.8 bsh obio_attach(device_t parent, device_t self, void *aux) 206 1.1 bsh { 207 1.8 bsh struct obio_softc *sc = device_private(self); 208 1.11 chs struct sa11x0_attach_args *sa = aux; 209 1.1 bsh bus_space_tag_t iot = sa->sa_iot; 210 1.1 bsh int i; 211 1.1 bsh uint16_t reg; 212 1.1 bsh 213 1.8 bsh sc->sc_dev = self; 214 1.8 bsh 215 1.1 bsh /* tweak memory access timing for CS3. 216 1.1 bsh the value set by redboot is too slow */ 217 1.1 bsh if (bus_space_map(iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0, 218 1.1 bsh &sc->sc_memctl_ioh)) 219 1.1 bsh goto fail; 220 1.1 bsh bus_space_write_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1, 221 1.1 bsh (0xffff & bus_space_read_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1)) 222 1.1 bsh | (0x6888 << 16)); 223 1.1 bsh 224 1.1 bsh /* Map on-board FPGA registers */ 225 1.1 bsh sc->sc_iot = iot; 226 1.1 bsh if (bus_space_map(iot, G42XXEB_PLDREG_BASE, G42XXEB_PLDREG_SIZE, 227 1.1 bsh 0, &(sc->sc_obioreg_ioh))) 228 1.1 bsh goto fail; 229 1.1 bsh 230 1.1 bsh /* 231 1.1 bsh * Mask all interrupts. 232 1.1 bsh * They are later unmasked at each device's attach routine. 233 1.1 bsh */ 234 1.1 bsh sc->sc_intr_mask = 0xffff; 235 1.1 bsh bus_space_write_2(iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK, 236 1.1 bsh sc->sc_intr_mask ); 237 1.1 bsh 238 1.1 bsh #if 0 239 1.1 bsh sc->sc_intr = 8; /* GPIO0 */ 240 1.1 bsh #endif 241 1.1 bsh sc->sc_intr_pending = 0; 242 1.1 bsh 243 1.1 bsh for (i=0; i < G42XXEB_N_INTS; ++i) { 244 1.1 bsh sc->sc_handler[i].func = obio_spurious; 245 1.1 bsh sc->sc_handler[i].arg = (void *)i; 246 1.1 bsh } 247 1.1 bsh 248 1.1 bsh obio_peripheral_reset(sc, 1, 0); 249 1.1 bsh 250 1.1 bsh /* 251 1.1 bsh * establish interrupt handler. 252 1.1 bsh * level is very high to allow high priority sub-interrupts. 253 1.1 bsh */ 254 1.1 bsh sc->sc_ipl = IPL_AUDIO; 255 1.1 bsh sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl, 256 1.1 bsh obio_intr, sc); 257 1.6 matt sc->sc_si = softint_establish(SOFTINT_NET, obio_softint, sc); 258 1.1 bsh 259 1.1 bsh reg = bus_space_read_2(iot, sc->sc_obioreg_ioh, G42XXEB_PLDVER); 260 1.1 bsh aprint_normal(": board %d version %x\n", reg>>8, reg & 0xff); 261 1.1 bsh 262 1.1 bsh /* 263 1.1 bsh * Attach each devices 264 1.1 bsh */ 265 1.12 thorpej config_search(self, NULL, 266 1.13 thorpej CFARGS(.search = obio_search)); 267 1.1 bsh return; 268 1.1 bsh 269 1.1 bsh fail: 270 1.9 bsh aprint_error_dev(self, "can't map FPGA registers\n"); 271 1.1 bsh } 272 1.1 bsh 273 1.1 bsh int 274 1.8 bsh obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 275 1.1 bsh { 276 1.8 bsh struct obio_softc *sc = device_private(parent); 277 1.1 bsh struct obio_attach_args oba; 278 1.1 bsh 279 1.1 bsh oba.oba_sc = sc; 280 1.1 bsh oba.oba_iot = sc->sc_iot; 281 1.1 bsh oba.oba_addr = cf->cf_loc[OBIOCF_ADDR]; 282 1.1 bsh oba.oba_intr = cf->cf_loc[OBIOCF_INTR]; 283 1.1 bsh 284 1.12 thorpej if (config_probe(parent, cf, &oba)) 285 1.13 thorpej config_attach(parent, cf, &oba, obio_print, CFARGS_NONE); 286 1.1 bsh 287 1.1 bsh return 0; 288 1.1 bsh } 289 1.1 bsh 290 1.1 bsh void * 291 1.1 bsh obio_intr_establish(struct obio_softc *sc, int irq, int ipl, 292 1.1 bsh int type, int (*func)(void *), void *arg) 293 1.1 bsh { 294 1.1 bsh int save; 295 1.1 bsh int regidx, sft; 296 1.1 bsh uint16_t reg; 297 1.1 bsh static const uint8_t ist_code[] = { 298 1.1 bsh 0, 299 1.1 bsh G42XXEB_INT_EDGE_FALLING, /* pulse */ 300 1.1 bsh G42XXEB_INT_EDGE_FALLING, /* IST_EDGE */ 301 1.1 bsh G42XXEB_INT_LEVEL_LOW, /* IST_LEVEL */ 302 1.1 bsh G42XXEB_INT_LEVEL_HIGH, /* IST_LEVEL_HIGH */ 303 1.1 bsh G42XXEB_INT_EDGE_RISING, /* IST_EDGE_RISING */ 304 1.1 bsh G42XXEB_INT_EDGE_BOTH, /* IST_EDGE_BOTH */ 305 1.1 bsh }; 306 1.1 bsh 307 1.1 bsh if (irq < 0 || G42XXEB_N_INTS <= irq) 308 1.1 bsh panic("Bad irq no. for obio (%d)", irq); 309 1.1 bsh 310 1.1 bsh if (type < 0 || IST_EDGE_BOTH < type) 311 1.1 bsh panic("Bad interrupt type for obio (%d)", type); 312 1.1 bsh 313 1.1 bsh regidx = G42XXEB_INTCNTL; 314 1.1 bsh sft = 3 * irq; 315 1.1 bsh if (irq >= 5) { 316 1.1 bsh regidx = G42XXEB_INTCNTH; 317 1.1 bsh sft -= 3*5; 318 1.1 bsh } 319 1.1 bsh 320 1.1 bsh save = disable_interrupts(I32_bit); 321 1.1 bsh 322 1.1 bsh sc->sc_handler[irq].func = func; 323 1.1 bsh sc->sc_handler[irq].arg = arg; 324 1.1 bsh sc->sc_handler[irq].level = ipl; 325 1.1 bsh 326 1.1 bsh /* set interrupt type */ 327 1.1 bsh reg = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx); 328 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx, 329 1.1 bsh (reg & ~(7<<sft)) | (ist_code[type] << sft)); 330 1.1 bsh 331 1.1 bsh #ifdef DEBUG 332 1.1 bsh printf("INTCTL=%x,%x\n", 333 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTL), 334 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTH)); 335 1.1 bsh #endif 336 1.1 bsh 337 1.1 bsh sc->sc_intr_mask &= ~(1U << irq); 338 1.1 bsh obio_update_intrmask(sc); 339 1.1 bsh 340 1.1 bsh restore_interrupts(save); 341 1.1 bsh 342 1.1 bsh #if 0 343 1.1 bsh if (ipl > sc->sc_ipl) { 344 1.1 bsh pxa2x0_update_intr_masks(sc->sc_intr, ipl); 345 1.1 bsh sc->sc_ipl = ipl; 346 1.1 bsh } 347 1.1 bsh #endif 348 1.1 bsh 349 1.1 bsh return &sc->sc_handler[irq]; 350 1.1 bsh } 351 1.1 bsh 352 1.1 bsh void 353 1.1 bsh obio_intr_disestablish(struct obio_softc *sc, int irq, int (* func)(void *)) 354 1.1 bsh { 355 1.1 bsh int error = 0; 356 1.1 bsh int save; 357 1.1 bsh 358 1.1 bsh save = disable_interrupts(I32_bit); 359 1.1 bsh 360 1.1 bsh if (sc->sc_handler[irq].func != func) 361 1.1 bsh error = 1; 362 1.1 bsh else { 363 1.1 bsh sc->sc_handler[irq].func = obio_spurious; 364 1.1 bsh sc->sc_handler[irq].level = IPL_NONE; 365 1.1 bsh 366 1.1 bsh sc->sc_intr_pending &= ~(1U << irq); 367 1.1 bsh sc->sc_intr_mask |= (1U << irq); 368 1.1 bsh obio_update_intrmask(sc); 369 1.1 bsh } 370 1.1 bsh 371 1.1 bsh restore_interrupts(save); 372 1.1 bsh 373 1.1 bsh if (error) 374 1.9 bsh aprint_error_dev(sc->sc_dev, "bad intr_disestablish\n"); 375 1.1 bsh } 376 1.1 bsh 377 1.1 bsh void 378 1.1 bsh obio_intr_mask(struct obio_softc *sc, struct obio_handler *ih) 379 1.1 bsh { 380 1.1 bsh int irqno; 381 1.1 bsh int save; 382 1.1 bsh 383 1.1 bsh irqno = ih - sc->sc_handler; 384 1.1 bsh #ifdef DIAGNOSTIC 385 1.1 bsh if (ih == NULL || ih->func==NULL || irqno < 0 || 386 1.1 bsh irqno >= G42XXEB_N_INTS) 387 1.1 bsh panic("Bad arg for obio_intr_mask"); 388 1.1 bsh #endif 389 1.1 bsh 390 1.1 bsh save = disable_interrupts(I32_bit); 391 1.1 bsh sc->sc_intr_mask |= 1U<<irqno; 392 1.1 bsh obio_update_intrmask(sc); 393 1.1 bsh restore_interrupts(save); 394 1.1 bsh } 395 1.1 bsh 396 1.1 bsh void 397 1.1 bsh obio_intr_unmask(struct obio_softc *sc, struct obio_handler *ih) 398 1.1 bsh { 399 1.1 bsh int irqno; 400 1.1 bsh int save; 401 1.1 bsh 402 1.1 bsh irqno = ih - sc->sc_handler; 403 1.1 bsh #ifdef DIAGNOSTIC 404 1.1 bsh if (ih == NULL || ih->func==NULL || irqno < 0 || 405 1.1 bsh irqno >= G42XXEB_N_INTS) 406 1.1 bsh panic("Bad arg for obio_intr_unmask"); 407 1.1 bsh #endif 408 1.1 bsh 409 1.1 bsh save = disable_interrupts(I32_bit); 410 1.1 bsh sc->sc_intr_mask &= ~(1U<<irqno); 411 1.1 bsh obio_update_intrmask(sc); 412 1.1 bsh restore_interrupts(save); 413 1.1 bsh } 414 1.1 bsh 415 1.1 bsh void 416 1.1 bsh obio_peripheral_reset(struct obio_softc *bsc, int no, int onoff) 417 1.1 bsh { 418 1.1 bsh uint16_t reg; 419 1.1 bsh 420 1.1 bsh reg = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh, 421 1.1 bsh G42XXEB_RST); 422 1.1 bsh bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh, G42XXEB_RST, 423 1.1 bsh onoff ? (reg & ~RST_EXT(no)) : (reg | RST_EXT(no))); 424 1.1 bsh } 425 1.1 bsh 426