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