obio.c revision 1.7.10.2       1  1.7.10.2      yamt /*	$NetBSD: obio.c,v 1.7.10.2 2009/06/20 07:20:01 yamt 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.7.10.2      yamt __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.7.10.2 2009/06/20 07:20:01 yamt 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.7.10.2      yamt static int	obio_match(device_t, cfdata_t, void *);
     63  1.7.10.2      yamt static void	obio_attach(device_t, device_t, void *);
     64  1.7.10.2      yamt static int	obio_search(device_t, cfdata_t, const int *, void *);
     65       1.1       bsh static int	obio_print(void *, const char *);
     66       1.1       bsh 
     67       1.1       bsh /* attach structures */
     68  1.7.10.2      yamt CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc), obio_match, obio_attach,
     69       1.1       bsh     NULL, NULL);
     70       1.1       bsh 
     71       1.1       bsh uint32_t obio_intr_mask;
     72       1.1       bsh 
     73       1.1       bsh static int
     74       1.1       bsh obio_spurious(void *arg)
     75       1.1       bsh {
     76       1.1       bsh 	int irqno = (int)arg;
     77       1.1       bsh 
     78  1.7.10.2      yamt 	aprint_normal("Spurious interrupt %d on On-board peripheral", irqno);
     79       1.1       bsh 	return 1;
     80       1.1       bsh }
     81       1.1       bsh 
     82       1.1       bsh 
     83       1.1       bsh /*
     84       1.1       bsh  * interrupt handler for GPIO0 (on-board peripherals)
     85       1.1       bsh  *
     86       1.1       bsh  * On Lubbock, 8 interrupts are ORed through on-board logic,
     87       1.1       bsh  * and routed to GPIO0 of PXA250 processor.
     88       1.1       bsh  */
     89       1.1       bsh static int
     90       1.1       bsh obio_intr(void *arg)
     91       1.1       bsh {
     92       1.1       bsh 	int irqno, pending, mask;
     93       1.1       bsh 	struct obio_softc *sc = (struct obio_softc *)arg;
     94       1.1       bsh 	int psw;
     95       1.1       bsh 
     96       1.1       bsh 	mask = sc->sc_obio_intr_mask; /* real irq mask for obio */
     97       1.1       bsh 
     98       1.1       bsh 	psw = disable_interrupts(I32_bit|F32_bit);
     99       1.1       bsh 	pending = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh,
    100       1.1       bsh 	    LUBBOCK_INTRCTL);
    101       1.1       bsh 	/* Here is a chance to lose some interrupts.
    102       1.1       bsh 	 * You need to modify FPGA program to avoid it
    103       1.1       bsh 	 */
    104       1.1       bsh 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRCTL, 0);
    105       1.1       bsh 	restore_interrupts(psw);
    106       1.1       bsh 
    107       1.1       bsh 
    108       1.1       bsh 	pending &= mask;
    109       1.1       bsh 	while (pending) {
    110       1.1       bsh 		irqno = 0;
    111       1.1       bsh 
    112       1.1       bsh 		for ( ;pending; ++irqno) {
    113       1.1       bsh 			if (0 == (pending & (1U<<irqno)))
    114       1.1       bsh 				continue;
    115       1.1       bsh 			pending &= ~(1U<<irqno);
    116       1.1       bsh 
    117       1.1       bsh #ifdef notyet
    118       1.1       bsh 			/* if ipl of this irq is higher than current spl level,
    119       1.1       bsh 			   call the handler directly instead of dispatching it to
    120       1.1       bsh 			   software interrupt. */
    121  1.7.10.1      yamt 			if (sc->sc_handler[irqno].level > curcpl()) {
    122       1.1       bsh 				(* sc->sc_handler[irqno].func)(
    123       1.1       bsh 					sc->sc_handler[irqno].arg );
    124       1.1       bsh 			}
    125       1.1       bsh 			else
    126       1.1       bsh #endif
    127       1.1       bsh 			{
    128       1.1       bsh 				/* mask this interrupt until software
    129       1.1       bsh 				   interrupt is handled. */
    130       1.1       bsh 				sc->sc_obio_intr_pending |= (1U<<irqno);
    131       1.1       bsh 				mask &= ~(1U<<irqno);
    132       1.1       bsh 				bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh,
    133       1.1       bsh 				    LUBBOCK_INTRMASK, mask);
    134       1.1       bsh 
    135       1.1       bsh 				/* handle it later */
    136       1.7      matt 				softint_schedule(sc->sc_si);
    137       1.1       bsh 			}
    138       1.1       bsh 		}
    139       1.1       bsh 
    140       1.1       bsh 		psw = disable_interrupts(I32_bit|F32_bit);
    141       1.1       bsh 		pending = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh,
    142       1.1       bsh 		    LUBBOCK_INTRCTL);
    143       1.1       bsh 		bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
    144       1.1       bsh 		    LUBBOCK_INTRCTL,0);
    145       1.1       bsh 		restore_interrupts(psw);
    146       1.1       bsh 		pending &= mask;
    147       1.1       bsh 	}
    148       1.1       bsh 
    149       1.1       bsh 	/* GPIO interrupt is edge triggered.  make a pulse
    150       1.1       bsh 	   to let Cotulla notice when other interrupts are
    151       1.1       bsh 	   still pending */
    152       1.1       bsh 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRMASK, 0);
    153       1.1       bsh 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, LUBBOCK_INTRMASK, mask);
    154       1.1       bsh 	return 1;
    155       1.1       bsh }
    156       1.1       bsh 
    157       1.1       bsh static void
    158       1.1       bsh obio_softintr(void *arg)
    159       1.1       bsh {
    160       1.1       bsh 	struct obio_softc *sc = (struct obio_softc *)arg;
    161       1.1       bsh 	int irqno;
    162       1.1       bsh 	int psw;
    163  1.7.10.1      yamt 	int spl_save = curcpl();
    164       1.1       bsh 
    165       1.1       bsh 	psw = disable_interrupts(I32_bit);
    166       1.1       bsh 	while ((irqno = find_first_bit(sc->sc_obio_intr_pending)) >= 0) {
    167       1.1       bsh 		sc->sc_obio_intr_pending &= ~(1U<<irqno);
    168       1.1       bsh 
    169       1.1       bsh 		restore_interrupts(psw);
    170       1.1       bsh 
    171       1.1       bsh 		_splraise(sc->sc_handler[irqno].level);
    172       1.1       bsh 		(* sc->sc_handler[irqno].func)(
    173       1.1       bsh 			sc->sc_handler[irqno].arg);
    174       1.1       bsh 		splx(spl_save);
    175       1.1       bsh 
    176       1.1       bsh 		psw = disable_interrupts(I32_bit);
    177       1.1       bsh 	}
    178       1.1       bsh 
    179       1.1       bsh 	bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh,
    180       1.1       bsh 	    LUBBOCK_INTRMASK, sc->sc_obio_intr_mask);
    181       1.1       bsh 
    182       1.1       bsh 	restore_interrupts(psw);
    183       1.1       bsh }
    184       1.1       bsh 
    185       1.1       bsh /*
    186       1.1       bsh  * int obio_print(void *aux, const char *name)
    187       1.1       bsh  * print configuration info for children
    188       1.1       bsh  */
    189       1.1       bsh 
    190       1.1       bsh static int
    191       1.1       bsh obio_print(void *aux, const char *name)
    192       1.1       bsh {
    193       1.1       bsh 	struct obio_attach_args *oba = (struct obio_attach_args*)aux;
    194       1.1       bsh 
    195       1.1       bsh 	if (oba->oba_addr != OBIOCF_ADDR_DEFAULT)
    196  1.7.10.2      yamt 		aprint_normal(" addr 0x%lx", oba->oba_addr);
    197       1.1       bsh         if (oba->oba_intr > 0)
    198  1.7.10.2      yamt 		aprint_normal(" intr %d", oba->oba_intr);
    199       1.1       bsh         return (UNCONF);
    200       1.1       bsh }
    201       1.1       bsh 
    202       1.1       bsh int
    203  1.7.10.2      yamt obio_match(device_t parent, cfdata_t match, void *aux)
    204       1.1       bsh {
    205       1.1       bsh 	return 1;
    206       1.1       bsh }
    207       1.1       bsh 
    208       1.1       bsh void
    209  1.7.10.2      yamt obio_attach(device_t parent, device_t self, void *aux)
    210       1.1       bsh {
    211  1.7.10.2      yamt 	struct obio_softc *sc = device_private(self);
    212       1.1       bsh 	int system_id, baseboard_id, expansion_id, processor_card_id;
    213       1.1       bsh 	struct pxaip_attach_args *sa = (struct pxaip_attach_args *)aux;
    214       1.4       bsh 	const char *processor_card_name;
    215       1.1       bsh 	int i;
    216       1.1       bsh 
    217       1.1       bsh 
    218       1.1       bsh 	/* Map on-board FPGA registers */
    219  1.7.10.2      yamt 	sc->sc_dev = self;
    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.7.10.2      yamt 		aprint_normal_dev(self, "can't map FPGA registers\n");
    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.7.10.2      yamt obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    291       1.1       bsh {
    292  1.7.10.2      yamt 	struct obio_softc *sc = device_private(parent);
    293       1.1       bsh 	struct obio_attach_args oba;
    294       1.1       bsh 
    295       1.1       bsh 	oba.oba_sc = sc;
    296       1.1       bsh         oba.oba_iot = sc->sc_iot;
    297       1.1       bsh         oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
    298       1.1       bsh         oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
    299       1.1       bsh 
    300       1.1       bsh         if (config_match(parent, cf, &oba))
    301       1.1       bsh                 config_attach(parent, cf, &oba, obio_print);
    302       1.1       bsh 
    303       1.1       bsh         return 0;
    304       1.1       bsh }
    305       1.1       bsh 
    306       1.1       bsh void *
    307       1.1       bsh obio_intr_establish(struct obio_softc *sc,
    308       1.1       bsh 		    int irq, int ipl, int (*func)(void *), void *arg)
    309       1.1       bsh {
    310       1.1       bsh 	int psw;
    311       1.1       bsh 
    312       1.1       bsh 	if (irq < 0 || N_OBIO_IRQ <= irq)
    313       1.1       bsh 		panic("Bad irq no for obio");
    314       1.1       bsh 
    315       1.1       bsh 	psw = disable_interrupts(I32_bit);
    316       1.1       bsh 
    317       1.1       bsh 	sc->sc_handler[irq].func = func;
    318       1.1       bsh 	sc->sc_handler[irq].arg = arg;
    319       1.1       bsh 	sc->sc_handler[irq].level = ipl;
    320       1.1       bsh 
    321       1.1       bsh #ifdef notyet
    322       1.1       bsh 	if (ipl > sc->sc_ipl) {
    323       1.1       bsh 		pxa2x0_update_intr_masks(sc->sc_intr, ipl);
    324       1.1       bsh 		sc->sc_ipl = ipl;
    325       1.1       bsh 	}
    326       1.1       bsh #endif
    327       1.1       bsh 
    328       1.1       bsh 	sc->sc_obio_intr_mask |= (1U << irq);
    329       1.1       bsh 	bus_space_write_4(sc->sc_iot, sc->sc_obioreg_ioh,
    330       1.1       bsh 	    LUBBOCK_INTRMASK, sc->sc_obio_intr_mask);
    331       1.1       bsh 
    332       1.1       bsh 	enable_interrupts(psw);
    333       1.1       bsh 	return &sc->sc_handler[irq];
    334       1.1       bsh }
    335