1 1.13 riastrad /* $NetBSD: obio.c,v 1.13 2023/07/13 19:42:24 riastradh Exp $ */ 2 1.1 bsh 3 1.1 bsh /* 4 1.1 bsh * Copyright (c) 2002, 2003 Genetec Corporation. All rights reserved. 5 1.1 bsh * Written by Hiroyuki Bessho for Genetec Corporation. 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 Corporation may not be used to endorse or 16 1.1 bsh * 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 CORPORATION ``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 CORPORATION 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 * TODO: dispatch interrupt to SOFTSERIAL or SOFTNET according to requested 34 1.1 bsh * interrupt level. 35 1.1 bsh */ 36 1.2 lukem 37 1.2 lukem #include <sys/cdefs.h> 38 1.13 riastrad __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.13 2023/07/13 19:42:24 riastradh Exp $"); 39 1.1 bsh 40 1.1 bsh #include <sys/param.h> 41 1.1 bsh #include <sys/systm.h> 42 1.1 bsh #include <sys/device.h> 43 1.1 bsh #include <sys/kernel.h> 44 1.1 bsh #include <sys/reboot.h> 45 1.13 riastrad #include <sys/bitops.h> 46 1.1 bsh 47 1.1 bsh #include <machine/cpu.h> 48 1.10 dyoung #include <sys/bus.h> 49 1.1 bsh #include <machine/intr.h> 50 1.1 bsh #include <arm/cpufunc.h> 51 1.1 bsh 52 1.1 bsh #include <arm/mainbus/mainbus.h> 53 1.1 bsh #include <arm/xscale/pxa2x0reg.h> 54 1.1 bsh #include <arm/xscale/pxa2x0var.h> 55 1.1 bsh #include <arm/xscale/pxa2x0_gpio.h> 56 1.1 bsh #include <arm/sa11x0/sa11x0_var.h> 57 1.1 bsh #include <evbarm/lubbock/lubbock_reg.h> 58 1.1 bsh #include <evbarm/lubbock/lubbock_var.h> 59 1.1 bsh 60 1.1 bsh #include "locators.h" 61 1.1 bsh 62 1.1 bsh /* prototypes */ 63 1.9 rjs static int obio_match(device_t, cfdata_t, void *); 64 1.9 rjs static void obio_attach(device_t, device_t, void *); 65 1.9 rjs static int obio_search(device_t, cfdata_t, const int *, void *); 66 1.1 bsh static int obio_print(void *, const char *); 67 1.1 bsh 68 1.1 bsh /* attach structures */ 69 1.9 rjs CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc), obio_match, obio_attach, 70 1.1 bsh NULL, NULL); 71 1.1 bsh 72 1.1 bsh uint32_t obio_intr_mask; 73 1.1 bsh 74 1.1 bsh static int 75 1.1 bsh obio_spurious(void *arg) 76 1.1 bsh { 77 1.1 bsh int irqno = (int)arg; 78 1.1 bsh 79 1.9 rjs aprint_normal("Spurious interrupt %d on On-board peripheral", irqno); 80 1.1 bsh return 1; 81 1.1 bsh } 82 1.1 bsh 83 1.1 bsh 84 1.1 bsh /* 85 1.1 bsh * interrupt handler for GPIO0 (on-board peripherals) 86 1.1 bsh * 87 1.1 bsh * On Lubbock, 8 interrupts are ORed through on-board logic, 88 1.1 bsh * and routed to GPIO0 of PXA250 processor. 89 1.1 bsh */ 90 1.1 bsh static int 91 1.1 bsh obio_intr(void *arg) 92 1.1 bsh { 93 1.1 bsh int irqno, pending, mask; 94 1.1 bsh struct obio_softc *sc = (struct obio_softc *)arg; 95 1.1 bsh int psw; 96 1.1 bsh 97 1.1 bsh mask = sc->sc_obio_intr_mask; /* real irq mask for obio */ 98 1.1 bsh 99 1.1 bsh psw = disable_interrupts(I32_bit|F32_bit); 100 1.1 bsh pending = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, 101 1.1 bsh LUBBOCK_INTRCTL); 102 1.1 bsh /* Here is a chance to lose some interrupts. 103 1.1 bsh * You need to modify FPGA program to avoid it 104 1.1 bsh */ 105 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRCTL, 0); 106 1.1 bsh restore_interrupts(psw); 107 1.1 bsh 108 1.1 bsh 109 1.1 bsh pending &= mask; 110 1.1 bsh while (pending) { 111 1.1 bsh irqno = 0; 112 1.1 bsh 113 1.1 bsh for ( ;pending; ++irqno) { 114 1.1 bsh if (0 == (pending & (1U<<irqno))) 115 1.1 bsh continue; 116 1.1 bsh pending &= ~(1U<<irqno); 117 1.1 bsh 118 1.1 bsh #ifdef notyet 119 1.1 bsh /* if ipl of this irq is higher than current spl level, 120 1.1 bsh call the handler directly instead of dispatching it to 121 1.1 bsh software interrupt. */ 122 1.8 matt if (sc->sc_handler[irqno].level > curcpl()) { 123 1.1 bsh (* sc->sc_handler[irqno].func)( 124 1.1 bsh sc->sc_handler[irqno].arg ); 125 1.1 bsh } 126 1.1 bsh else 127 1.1 bsh #endif 128 1.1 bsh { 129 1.1 bsh /* mask this interrupt until software 130 1.1 bsh interrupt is handled. */ 131 1.1 bsh sc->sc_obio_intr_pending |= (1U<<irqno); 132 1.1 bsh mask &= ~(1U<<irqno); 133 1.1 bsh bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh, 134 1.1 bsh LUBBOCK_INTRMASK, mask); 135 1.1 bsh 136 1.1 bsh /* handle it later */ 137 1.7 matt softint_schedule(sc->sc_si); 138 1.1 bsh } 139 1.1 bsh } 140 1.1 bsh 141 1.1 bsh psw = disable_interrupts(I32_bit|F32_bit); 142 1.1 bsh pending = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, 143 1.1 bsh LUBBOCK_INTRCTL); 144 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, 145 1.1 bsh LUBBOCK_INTRCTL,0); 146 1.1 bsh restore_interrupts(psw); 147 1.1 bsh pending &= mask; 148 1.1 bsh } 149 1.1 bsh 150 1.1 bsh /* GPIO interrupt is edge triggered. make a pulse 151 1.1 bsh to let Cotulla notice when other interrupts are 152 1.1 bsh still pending */ 153 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRMASK, 0); 154 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRMASK, mask); 155 1.1 bsh return 1; 156 1.1 bsh } 157 1.1 bsh 158 1.1 bsh static void 159 1.1 bsh obio_softintr(void *arg) 160 1.1 bsh { 161 1.1 bsh struct obio_softc *sc = (struct obio_softc *)arg; 162 1.1 bsh int irqno; 163 1.1 bsh int psw; 164 1.8 matt int spl_save = curcpl(); 165 1.1 bsh 166 1.1 bsh psw = disable_interrupts(I32_bit); 167 1.13 riastrad while ((irqno = fls32(sc->sc_obio_intr_pending) - 1) >= 0) { 168 1.1 bsh sc->sc_obio_intr_pending &= ~(1U<<irqno); 169 1.1 bsh 170 1.1 bsh restore_interrupts(psw); 171 1.1 bsh 172 1.1 bsh _splraise(sc->sc_handler[irqno].level); 173 1.1 bsh (* sc->sc_handler[irqno].func)( 174 1.1 bsh sc->sc_handler[irqno].arg); 175 1.1 bsh splx(spl_save); 176 1.1 bsh 177 1.1 bsh psw = disable_interrupts(I32_bit); 178 1.1 bsh } 179 1.1 bsh 180 1.1 bsh bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh, 181 1.1 bsh LUBBOCK_INTRMASK, sc->sc_obio_intr_mask); 182 1.1 bsh 183 1.1 bsh restore_interrupts(psw); 184 1.1 bsh } 185 1.1 bsh 186 1.1 bsh /* 187 1.1 bsh * int obio_print(void *aux, const char *name) 188 1.1 bsh * print configuration info for children 189 1.1 bsh */ 190 1.1 bsh 191 1.1 bsh static int 192 1.1 bsh obio_print(void *aux, const char *name) 193 1.1 bsh { 194 1.1 bsh struct obio_attach_args *oba = (struct obio_attach_args*)aux; 195 1.1 bsh 196 1.1 bsh if (oba->oba_addr != OBIOCF_ADDR_DEFAULT) 197 1.9 rjs aprint_normal(" addr 0x%lx", oba->oba_addr); 198 1.1 bsh if (oba->oba_intr > 0) 199 1.9 rjs aprint_normal(" intr %d", oba->oba_intr); 200 1.1 bsh return (UNCONF); 201 1.1 bsh } 202 1.1 bsh 203 1.1 bsh int 204 1.9 rjs obio_match(device_t parent, cfdata_t match, void *aux) 205 1.1 bsh { 206 1.1 bsh return 1; 207 1.1 bsh } 208 1.1 bsh 209 1.1 bsh void 210 1.9 rjs obio_attach(device_t parent, device_t self, void *aux) 211 1.1 bsh { 212 1.9 rjs struct obio_softc *sc = device_private(self); 213 1.1 bsh int system_id, baseboard_id, expansion_id, processor_card_id; 214 1.1 bsh struct pxaip_attach_args *sa = (struct pxaip_attach_args *)aux; 215 1.4 bsh const char *processor_card_name; 216 1.1 bsh int i; 217 1.1 bsh 218 1.1 bsh 219 1.1 bsh /* Map on-board FPGA registers */ 220 1.9 rjs sc->sc_dev = self; 221 1.1 bsh sc->sc_iot = &pxa2x0_bs_tag; 222 1.1 bsh if (bus_space_map(sc->sc_iot, LUBBOCK_OBIO_PBASE, LUBBOCK_OBIO_SIZE, 223 1.1 bsh 0, &(sc->sc_obioreg_ioh))) { 224 1.9 rjs aprint_normal_dev(self, "can't map FPGA registers\n"); 225 1.1 bsh } 226 1.1 bsh 227 1.1 bsh system_id = bus_space_read_4(sc->sc_iot, sc->sc_obioreg_ioh, 228 1.1 bsh LUBBOCK_SYSTEMID); 229 1.1 bsh 230 1.1 bsh baseboard_id = (system_id>>8) & 0x0f; 231 1.1 bsh expansion_id = (system_id>>4) & 0x0f; 232 1.1 bsh processor_card_id = system_id & 0x0f; 233 1.1 bsh 234 1.1 bsh switch (processor_card_id) { 235 1.1 bsh case 0: processor_card_name = "Cotulla"; break; 236 1.1 bsh case 1: processor_card_name = "Sabinal"; break; 237 1.1 bsh default: processor_card_name = "(unknown)"; 238 1.1 bsh } 239 1.1 bsh 240 1.1 bsh printf(" : baseboard=%d (%s), expansion card=%d, processor card=%d (%s)\n", 241 1.1 bsh baseboard_id, 242 1.1 bsh baseboard_id==8 ? "DBPXA250(lubbock)" : "(unknown)", 243 1.1 bsh expansion_id, 244 1.1 bsh processor_card_id, processor_card_name ); 245 1.1 bsh 246 1.1 bsh /* 247 1.1 bsh * Mask all interrupts. 248 1.1 bsh * They are later unmasked at each device's attach routine. 249 1.1 bsh */ 250 1.1 bsh bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh, 251 1.1 bsh LUBBOCK_INTRMASK,0); 252 1.1 bsh 253 1.1 bsh sc->sc_intr = sa->pxa_intr; /* irq no. on ICU. */ 254 1.1 bsh sc->sc_obio_intr_mask = 0; /* No interrupt used */ 255 1.1 bsh sc->sc_obio_intr_pending = 0; 256 1.1 bsh sc->sc_ipl = IPL_BIO; 257 1.1 bsh 258 1.1 bsh for (i=0; i < N_OBIO_IRQ; ++i) { 259 1.1 bsh sc->sc_handler[i].func = obio_spurious; 260 1.1 bsh sc->sc_handler[i].arg = (void *)i; 261 1.1 bsh } 262 1.1 bsh 263 1.1 bsh 264 1.1 bsh /* 265 1.1 bsh * establish interrupt handler. 266 1.1 bsh */ 267 1.1 bsh #if 0 268 1.1 bsh /* 269 1.1 bsh * level is lowest at first, and changed when 270 1.1 bsh * sub-interrupt handlers are established 271 1.1 bsh */ 272 1.1 bsh sc->sc_ipl = IPL_BIO; 273 1.1 bsh #else 274 1.1 bsh /* 275 1.1 bsh * level is very high to allow high priority sub-interrupts. 276 1.1 bsh */ 277 1.1 bsh sc->sc_ipl = IPL_AUDIO; 278 1.1 bsh #endif 279 1.1 bsh sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl, 280 1.1 bsh obio_intr, sc); 281 1.7 matt sc->sc_si = softint_establish(SOFTINT_NET, obio_softintr, sc); 282 1.1 bsh 283 1.1 bsh 284 1.1 bsh /* 285 1.1 bsh * Attach each devices 286 1.1 bsh */ 287 1.11 thorpej config_search(self, NULL, 288 1.12 thorpej CFARGS(.search = obio_search)); 289 1.1 bsh } 290 1.1 bsh 291 1.1 bsh int 292 1.9 rjs obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 293 1.1 bsh { 294 1.9 rjs struct obio_softc *sc = device_private(parent); 295 1.1 bsh struct obio_attach_args oba; 296 1.1 bsh 297 1.1 bsh oba.oba_sc = sc; 298 1.1 bsh oba.oba_iot = sc->sc_iot; 299 1.1 bsh oba.oba_addr = cf->cf_loc[OBIOCF_ADDR]; 300 1.1 bsh oba.oba_intr = cf->cf_loc[OBIOCF_INTR]; 301 1.1 bsh 302 1.11 thorpej if (config_probe(parent, cf, &oba)) 303 1.12 thorpej config_attach(parent, cf, &oba, obio_print, CFARGS_NONE); 304 1.1 bsh 305 1.1 bsh return 0; 306 1.1 bsh } 307 1.1 bsh 308 1.1 bsh void * 309 1.1 bsh obio_intr_establish(struct obio_softc *sc, 310 1.1 bsh int irq, int ipl, int (*func)(void *), void *arg) 311 1.1 bsh { 312 1.1 bsh int psw; 313 1.1 bsh 314 1.1 bsh if (irq < 0 || N_OBIO_IRQ <= irq) 315 1.1 bsh panic("Bad irq no for obio"); 316 1.1 bsh 317 1.1 bsh psw = disable_interrupts(I32_bit); 318 1.1 bsh 319 1.1 bsh sc->sc_handler[irq].func = func; 320 1.1 bsh sc->sc_handler[irq].arg = arg; 321 1.1 bsh sc->sc_handler[irq].level = ipl; 322 1.1 bsh 323 1.1 bsh #ifdef notyet 324 1.1 bsh if (ipl > sc->sc_ipl) { 325 1.1 bsh pxa2x0_update_intr_masks(sc->sc_intr, ipl); 326 1.1 bsh sc->sc_ipl = ipl; 327 1.1 bsh } 328 1.1 bsh #endif 329 1.1 bsh 330 1.1 bsh sc->sc_obio_intr_mask |= (1U << irq); 331 1.1 bsh bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh, 332 1.1 bsh LUBBOCK_INTRMASK, sc->sc_obio_intr_mask); 333 1.1 bsh 334 1.1 bsh enable_interrupts(psw); 335 1.1 bsh return &sc->sc_handler[irq]; 336 1.1 bsh } 337