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