Home | History | Annotate | Line # | Download | only in lubbock
obio.c revision 1.8
      1  1.8      matt /*	$NetBSD: obio.c,v 1.8 2008/04/27 18:58:46 matt 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.8      matt __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.8 2008/04/27 18:58:46 matt 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.1       bsh 
     46  1.1       bsh #include <machine/cpu.h>
     47  1.1       bsh #include <machine/bus.h>
     48  1.1       bsh #include <machine/intr.h>
     49  1.1       bsh #include <arm/cpufunc.h>
     50  1.1       bsh 
     51  1.1       bsh #include <arm/mainbus/mainbus.h>
     52  1.1       bsh #include <arm/xscale/pxa2x0reg.h>
     53  1.1       bsh #include <arm/xscale/pxa2x0var.h>
     54  1.1       bsh #include <arm/xscale/pxa2x0_gpio.h>
     55  1.1       bsh #include <arm/sa11x0/sa11x0_var.h>
     56  1.1       bsh #include <evbarm/lubbock/lubbock_reg.h>
     57  1.1       bsh #include <evbarm/lubbock/lubbock_var.h>
     58  1.1       bsh 
     59  1.1       bsh #include "locators.h"
     60  1.1       bsh 
     61  1.1       bsh /* prototypes */
     62  1.1       bsh static int	obio_match(struct device *, struct cfdata *, void *);
     63  1.1       bsh static void	obio_attach(struct device *, struct device *, void *);
     64  1.4       bsh static int	obio_search(struct device *, struct cfdata *,
     65  1.5  drochner 		    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.1       bsh CFATTACH_DECL(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.1       bsh 	printf("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.1       bsh 	while ((irqno = find_first_bit(sc->sc_obio_intr_pending)) >= 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.1       bsh                 printf(" addr 0x%lx", oba->oba_addr);
    198  1.1       bsh         if (oba->oba_intr > 0)
    199  1.1       bsh                 printf(" 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.1       bsh obio_match(struct device *parent, struct cfdata *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.1       bsh obio_attach(struct device *parent, struct device *self, void *aux)
    211  1.1       bsh {
    212  1.1       bsh 	struct obio_softc *sc = (struct obio_softc*)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.1       bsh 	sc->sc_iot = &pxa2x0_bs_tag;
    221  1.1       bsh 	if (bus_space_map(sc->sc_iot, LUBBOCK_OBIO_PBASE, LUBBOCK_OBIO_SIZE,
    222  1.1       bsh 	    0, &(sc->sc_obioreg_ioh))) {
    223  1.1       bsh 		printf("%s: can't map FPGA registers\n", self->dv_xname);
    224  1.1       bsh 	}
    225  1.1       bsh 
    226  1.1       bsh 	system_id = bus_space_read_4(sc->sc_iot, sc->sc_obioreg_ioh,
    227  1.1       bsh 	    LUBBOCK_SYSTEMID);
    228  1.1       bsh 
    229  1.1       bsh 	baseboard_id = (system_id>>8) & 0x0f;
    230  1.1       bsh 	expansion_id = (system_id>>4) & 0x0f;
    231  1.1       bsh 	processor_card_id = system_id & 0x0f;
    232  1.1       bsh 
    233  1.1       bsh 	switch (processor_card_id) {
    234  1.1       bsh 	case 0: processor_card_name = "Cotulla"; break;
    235  1.1       bsh 	case 1: processor_card_name = "Sabinal"; break;
    236  1.1       bsh 	default: processor_card_name = "(unknown)";
    237  1.1       bsh 	}
    238  1.1       bsh 
    239  1.1       bsh 	printf(" : baseboard=%d (%s), expansion card=%d, processor card=%d (%s)\n",
    240  1.1       bsh 	       baseboard_id,
    241  1.1       bsh 	       baseboard_id==8 ? "DBPXA250(lubbock)" : "(unknown)",
    242  1.1       bsh 	       expansion_id,
    243  1.1       bsh 	       processor_card_id, processor_card_name );
    244  1.1       bsh 
    245  1.1       bsh 	/*
    246  1.1       bsh 	 *  Mask all interrupts.
    247  1.1       bsh 	 *  They are later unmasked at each device's attach routine.
    248  1.1       bsh 	 */
    249  1.1       bsh 	bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh,
    250  1.1       bsh 	    LUBBOCK_INTRMASK,0);
    251  1.1       bsh 
    252  1.1       bsh 	sc->sc_intr = sa->pxa_intr;	/* irq no. on ICU. */
    253  1.1       bsh 	sc->sc_obio_intr_mask = 0;	/* No interrupt used */
    254  1.1       bsh 	sc->sc_obio_intr_pending = 0;
    255  1.1       bsh 	sc->sc_ipl = IPL_BIO;
    256  1.1       bsh 
    257  1.1       bsh 	for (i=0; i < N_OBIO_IRQ; ++i) {
    258  1.1       bsh 		sc->sc_handler[i].func = obio_spurious;
    259  1.1       bsh 		sc->sc_handler[i].arg = (void *)i;
    260  1.1       bsh 	}
    261  1.1       bsh 
    262  1.1       bsh 
    263  1.1       bsh 	/*
    264  1.1       bsh 	 * establish interrupt handler.
    265  1.1       bsh 	 */
    266  1.1       bsh #if 0
    267  1.1       bsh 	/*
    268  1.1       bsh 	 * level is lowest at first, and changed when
    269  1.1       bsh 	 * sub-interrupt handlers are established
    270  1.1       bsh 	 */
    271  1.1       bsh 	sc->sc_ipl = IPL_BIO;
    272  1.1       bsh #else
    273  1.1       bsh 	/*
    274  1.1       bsh 	 * level is very high to allow high priority sub-interrupts.
    275  1.1       bsh 	 */
    276  1.1       bsh 	sc->sc_ipl = IPL_AUDIO;
    277  1.1       bsh #endif
    278  1.1       bsh 	sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl,
    279  1.1       bsh 	    obio_intr, sc);
    280  1.7      matt 	sc->sc_si = softint_establish(SOFTINT_NET, obio_softintr, sc);
    281  1.1       bsh 
    282  1.1       bsh 
    283  1.1       bsh 	/*
    284  1.1       bsh 	 *  Attach each devices
    285  1.1       bsh 	 */
    286  1.3  drochner 	config_search_ia(obio_search, self, "obio", NULL);
    287  1.1       bsh }
    288  1.1       bsh 
    289  1.1       bsh int
    290  1.5  drochner obio_search(struct device *parent, struct cfdata *cf, const int *ldesc,
    291  1.4       bsh     void *aux)
    292  1.1       bsh {
    293  1.1       bsh 	struct obio_softc *sc = (struct obio_softc *)parent;
    294  1.1       bsh 	struct obio_attach_args oba;
    295  1.1       bsh 
    296  1.1       bsh 	oba.oba_sc = sc;
    297  1.1       bsh         oba.oba_iot = sc->sc_iot;
    298  1.1       bsh         oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
    299  1.1       bsh         oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
    300  1.1       bsh 
    301  1.1       bsh         if (config_match(parent, cf, &oba))
    302  1.1       bsh                 config_attach(parent, cf, &oba, obio_print);
    303  1.1       bsh 
    304  1.1       bsh         return 0;
    305  1.1       bsh }
    306  1.1       bsh 
    307  1.1       bsh void *
    308  1.1       bsh obio_intr_establish(struct obio_softc *sc,
    309  1.1       bsh 		    int irq, int ipl, int (*func)(void *), void *arg)
    310  1.1       bsh {
    311  1.1       bsh 	int psw;
    312  1.1       bsh 
    313  1.1       bsh 	if (irq < 0 || N_OBIO_IRQ <= irq)
    314  1.1       bsh 		panic("Bad irq no for obio");
    315  1.1       bsh 
    316  1.1       bsh 	psw = disable_interrupts(I32_bit);
    317  1.1       bsh 
    318  1.1       bsh 	sc->sc_handler[irq].func = func;
    319  1.1       bsh 	sc->sc_handler[irq].arg = arg;
    320  1.1       bsh 	sc->sc_handler[irq].level = ipl;
    321  1.1       bsh 
    322  1.1       bsh #ifdef notyet
    323  1.1       bsh 	if (ipl > sc->sc_ipl) {
    324  1.1       bsh 		pxa2x0_update_intr_masks(sc->sc_intr, ipl);
    325  1.1       bsh 		sc->sc_ipl = ipl;
    326  1.1       bsh 	}
    327  1.1       bsh #endif
    328  1.1       bsh 
    329  1.1       bsh 	sc->sc_obio_intr_mask |= (1U << irq);
    330  1.1       bsh 	bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh,
    331  1.1       bsh 	    LUBBOCK_INTRMASK, sc->sc_obio_intr_mask);
    332  1.1       bsh 
    333  1.1       bsh 	enable_interrupts(psw);
    334  1.1       bsh 	return &sc->sc_handler[irq];
    335  1.1       bsh }
    336