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