Home | History | Annotate | Line # | Download | only in ifpga
ifpga_pci.c revision 1.1.4.2
      1 /*	$NetBSD: ifpga_pci.c,v 1.1.4.2 2002/01/10 19:42:08 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 ARM Ltd
      5  * All rights reserved.
      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 the company may not be used to endorse or promote
     16  *    products derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  * Copyright (c) 1997,1998 Mark Brinicombe.
     32  * Copyright (c) 1997,1998 Causality Limited
     33  * All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. All advertising materials mentioning features or use of this software
     44  *    must display the following acknowledgement:
     45  *	This product includes software developed by Mark Brinicombe
     46  *	for the NetBSD Project.
     47  * 4. The name of the company nor the name of the author may be used to
     48  *    endorse or promote products derived from this software without specific
     49  *    prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     52  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     53  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     55  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     57  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/conf.h>
     67 #include <sys/malloc.h>
     68 #include <sys/device.h>
     69 
     70 #define _ARM32_BUS_DMA_PRIVATE
     71 #include <evbarm/integrator/int_bus_dma.h>
     72 
     73 #include <machine/intr.h>
     74 
     75 #include <dev/pci/pcireg.h>
     76 #include <dev/pci/pcivar.h>
     77 
     78 #include <evbarm/ifpga/ifpgareg.h>
     79 #include <evbarm/ifpga/ifpgamem.h>
     80 #include <evbarm/ifpga/ifpga_pcivar.h>
     81 #include <evbarm/dev/v360reg.h>
     82 
     83 
     84 void		ifpga_pci_attach_hook (struct device *, struct device *,
     85 		    struct pcibus_attach_args *);
     86 int		ifpga_pci_bus_maxdevs (void *, int);
     87 pcitag_t	ifpga_pci_make_tag (void *, int, int, int);
     88 void		ifpga_pci_decompose_tag (void *, pcitag_t, int *, int *,
     89 		    int *);
     90 pcireg_t	ifpga_pci_conf_read (void *, pcitag_t, int);
     91 void		ifpga_pci_conf_write (void *, pcitag_t, int, pcireg_t);
     92 int		ifpga_pci_intr_map (struct pci_attach_args *,
     93 		    pci_intr_handle_t *);
     94 const char	*ifpga_pci_intr_string (void *, pci_intr_handle_t);
     95 const struct evcnt *ifpga_pci_intr_evcnt (void *, pci_intr_handle_t);
     96 void		*ifpga_pci_intr_establish (void *, pci_intr_handle_t, int,
     97 		    int (*)(void *), void *);
     98 void		ifpga_pci_intr_disestablish (void *, void *);
     99 
    100 struct arm32_pci_chipset ifpga_pci_chipset = {
    101 	NULL,	/* conf_v */
    102 	ifpga_pci_attach_hook,
    103 	ifpga_pci_bus_maxdevs,
    104 	ifpga_pci_make_tag,
    105 	ifpga_pci_decompose_tag,
    106 	ifpga_pci_conf_read,
    107 	ifpga_pci_conf_write,
    108 	NULL,	/* intr_v */
    109 	ifpga_pci_intr_map,
    110 	ifpga_pci_intr_string,
    111 	ifpga_pci_intr_evcnt,
    112 	ifpga_pci_intr_establish,
    113 	ifpga_pci_intr_disestablish
    114 };
    115 
    116 /*
    117  * Use the integrator-specific bus_dma routines.
    118  */
    119 struct arm32_bus_dma_tag ifpga_pci_bus_dma_tag = {
    120 	0,
    121 	0,
    122 	_bus_dmamap_create,
    123 	_bus_dmamap_destroy,
    124 	integrator_bus_dmamap_load,
    125 	integrator_bus_dmamap_load_mbuf,
    126 	integrator_bus_dmamap_load_uio,
    127 	_bus_dmamap_load_raw,
    128 	_bus_dmamap_unload,
    129 	_bus_dmamap_sync,
    130 	integrator_bus_dmamem_alloc,
    131 	integrator_bus_dmamem_free,
    132 	integrator_bus_dmamem_map,
    133 	_bus_dmamem_unmap,
    134 	integrator_bus_dmamem_mmap,
    135 };
    136 
    137 /*
    138  * Currently we only support 12 devices as we select directly in the
    139  * type 0 config cycle
    140  * (See conf_{read,write} for more detail
    141  */
    142 #define MAX_PCI_DEVICES	21
    143 
    144 /*static int
    145 pci_intr(void *arg)
    146 {
    147 	printf("pci int %x\n", (int)arg);
    148 	return 0;
    149 }*/
    150 
    151 
    152 void
    153 ifpga_pci_attach_hook(struct device *parent, struct device *self,
    154     struct pcibus_attach_args *pba)
    155 {
    156 #ifdef PCI_DEBUG
    157 	printf("ifpga_pci_attach_hook()\n");
    158 #endif
    159 }
    160 
    161 int
    162 ifpga_pci_bus_maxdevs(void *pcv, int busno)
    163 {
    164 #ifdef PCI_DEBUG
    165 	printf("ifpga_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
    166 #endif
    167 	return MAX_PCI_DEVICES;
    168 }
    169 
    170 pcitag_t
    171 ifpga_pci_make_tag(void *pcv, int bus, int device, int function)
    172 {
    173 #ifdef PCI_DEBUG
    174 	printf("ifpga_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
    175 	    pcv, bus, device, function);
    176 #endif
    177 	return (bus << 16) | (device << 11) | (function << 8);
    178 }
    179 
    180 void
    181 ifpga_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep,
    182     int *functionp)
    183 {
    184 #ifdef PCI_DEBUG
    185 	printf("ifpga_pci_decompose_tag(pcv=%p, tag=0x%08lx, bp=%p, dp=%p, "
    186 	    "fp=%p)\n", pcv, tag, busp, devicep, functionp);
    187 #endif
    188 
    189 	if (busp != NULL)
    190 		*busp = (tag >> 16) & 0xff;
    191 	if (devicep != NULL)
    192 		*devicep = (tag >> 11) & 0x1f;
    193 	if (functionp != NULL)
    194 		*functionp = (tag >> 8) & 0x7;
    195 }
    196 
    197 pcireg_t
    198 ifpga_pci_conf_read(void *pcv, pcitag_t tag, int reg)
    199 {
    200 	pcireg_t data;
    201 	struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
    202 	int bus, device, function;
    203 	u_int address;
    204 
    205 	ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
    206 
    207 	/* Reset the appertures so that we can talk to the register space.  */
    208 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
    209 	    IFPGA_PCI_APP0_512MB_BASE);
    210 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
    211 	    IFPGA_PCI_APP1_CONF_BASE);
    212 
    213 	if (bus == 0) {
    214 		address = (1 << (device + 11)) | reg;
    215 		bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    216 		    IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
    217 
    218 		/* Read the value from the bus...  */
    219 		data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
    220 		    address & 0x00ffffff);
    221 
    222 	} else {
    223 		bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    224 		    IFPGA_PCI_APP1_CONF_T1_MAP);
    225 
    226 		/* Read the value from the bus... */
    227 		data = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh,
    228 		    tag | reg);
    229 	}
    230 	/* ... and put the memory spaces back again.  */
    231 
    232 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
    233 	    IFPGA_PCI_APP1_256MB_BASE);
    234 	bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    235 	    IFPGA_PCI_APP1_256MB_MAP);
    236 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
    237 	    IFPGA_PCI_APP0_256MB_BASE);
    238 #ifdef PCI_DEBUG
    239 	printf("ifpga_pci_conf_read(pcv=%p tag=0x%08lx reg=0x%02x)=0x%08x\n",
    240 	    pcv, tag, reg, data);
    241 #endif
    242 	return data;
    243 }
    244 
    245 void
    246 ifpga_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
    247 {
    248 	struct ifpga_pci_softc *sc = (struct ifpga_pci_softc *)pcv;
    249 	int bus, device, function;
    250 	u_int address;
    251 
    252 #ifdef PCI_DEBUG
    253 	printf("ifpga_pci_conf_write(pcv=%p tag=0x%08lx reg=0x%02x, 0x%08x)\n",
    254 	    pcv, tag, reg, data);
    255 #endif
    256 
    257 	ifpga_pci_decompose_tag(pcv, tag, &bus, &device, &function);
    258 
    259 	/* Reset the appertures so that we can talk to the register space.  */
    260 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
    261 	    IFPGA_PCI_APP0_512MB_BASE);
    262 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
    263 	    IFPGA_PCI_APP1_CONF_BASE);
    264 
    265 	if (bus == 0) {
    266 		address = (1 << (device + 11)) | reg;
    267 		bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    268 		    IFPGA_PCI_APP1_CONF_T0_MAP | ((address >> 16) & 0xff00));
    269 
    270 		/* Read the value from the bus...  */
    271 		bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh,
    272 		    address & 0x00ffffff, data);
    273 
    274 	} else {
    275 		bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    276 		    IFPGA_PCI_APP1_CONF_T1_MAP);
    277 
    278 		/* Read the value from the bus... */
    279 		bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh, tag | reg,
    280 		    data);
    281 	}
    282 	/* ... and put the memory spaces back again.  */
    283 
    284 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE1,
    285 	    IFPGA_PCI_APP1_256MB_BASE);
    286 	bus_space_write_2(sc->sc_memt, sc->sc_reg_ioh, V360_LB_MAP1,
    287 	    IFPGA_PCI_APP1_256MB_MAP);
    288 	bus_space_write_4(sc->sc_memt, sc->sc_reg_ioh, V360_LB_BASE0,
    289 	    IFPGA_PCI_APP0_256MB_BASE);
    290 }
    291 
    292 int
    293 ifpga_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    294 {
    295 	int line = pa->pa_intrline;
    296 
    297 #ifdef PCI_DEBUG
    298 	int pin = pa->pa_intrpin;
    299 	void *pcv = pa->pa_pc;
    300 	pcitag_t intrtag = pa->pa_intrtag;
    301 	int bus, device, function;
    302 
    303 	ifpga_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
    304 	printf("ifpga_pci_intr_map: pcv=%p, tag=%08lx pin=%d line=%d "
    305 	    "dev=%d\n", pcv, intrtag, pin, line, device);
    306 #endif
    307 
    308 
    309 #ifdef PCI_DEBUG
    310 	printf("pin %d, line %d mapped to int %d\n", pin, line, line);
    311 #endif
    312 
    313 	*ihp = line;
    314 	return 0;
    315 }
    316 
    317 const char *
    318 ifpga_pci_intr_string(void *pcv, pci_intr_handle_t ih)
    319 {
    320 	static char irqstr[12];		/* 6 + 1 + NULL + sanity */
    321 
    322 #ifdef PCI_DEBUG
    323 	printf("ifpga_pci_intr_string(pcv=0x%p, ih=0x%lx)\n", pcv, ih);
    324 #endif
    325 	if (ih == 0)
    326 		panic("ifpga_pci_intr_string: bogus handle 0x%lx\n", ih);
    327 
    328 	sprintf(irqstr, "pciint%ld", ih - IFPGA_INTRNUM_PCIINT0);
    329 	return irqstr;
    330 }
    331 
    332 const struct evcnt *
    333 ifpga_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
    334 {
    335 
    336 	/* XXX for now, no evcnt parent reported */
    337 	return NULL;
    338 }
    339 
    340 void *
    341 ifpga_pci_intr_establish(void *pcv, pci_intr_handle_t ih, int level,
    342     int (*func) (void *), void *arg)
    343 {
    344 	void *intr;
    345 	int length;
    346 	char *string;
    347 
    348 #ifdef PCI_DEBUG
    349 	printf("ifpga_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, "
    350 	    "func=%p, arg=%p)\n", pcv, ih, level, func, arg);
    351 #endif
    352 
    353 	/* Copy the interrupt string to a private buffer */
    354 	length = strlen(ifpga_pci_intr_string(pcv, ih));
    355 	string = malloc(length + 1, M_DEVBUF, M_WAITOK);
    356 	strcpy(string, ifpga_pci_intr_string(pcv, ih));
    357 	intr = intr_claim(ih, level, string, func, arg);
    358 
    359 	return intr;
    360 }
    361 
    362 void
    363 ifpga_pci_intr_disestablish(void *pcv, void *cookie)
    364 {
    365 #ifdef PCI_DEBUG
    366 	printf("ifpga_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
    367 	    pcv, cookie);
    368 #endif
    369 	/* XXXX Need to free the string */
    370 
    371 	intr_release(cookie);
    372 }
    373