Home | History | Annotate | Line # | Download | only in xscale
ixp425_pci.c revision 1.11.14.1
      1  1.11.14.1    skrll /*	$NetBSD: ixp425_pci.c,v 1.11.14.1 2015/12/27 12:09:32 skrll Exp $ */
      2        1.1   ichiro 
      3        1.1   ichiro /*
      4        1.1   ichiro  * Copyright (c) 2003
      5        1.1   ichiro  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6        1.1   ichiro  * All rights reserved.
      7        1.1   ichiro  *
      8        1.1   ichiro  * Redistribution and use in source and binary forms, with or without
      9        1.1   ichiro  * modification, are permitted provided that the following conditions
     10        1.1   ichiro  * are met:
     11        1.1   ichiro  * 1. Redistributions of source code must retain the above copyright
     12        1.1   ichiro  *    notice, this list of conditions and the following disclaimer.
     13        1.1   ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1   ichiro  *    notice, this list of conditions and the following disclaimer in the
     15        1.1   ichiro  *    documentation and/or other materials provided with the distribution.
     16        1.1   ichiro  *
     17        1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     18        1.1   ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19        1.1   ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20        1.1   ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     21        1.1   ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22        1.1   ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23        1.1   ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24        1.1   ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25        1.1   ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26        1.1   ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27        1.1   ichiro  * SUCH DAMAGE.
     28        1.1   ichiro  */
     29        1.1   ichiro 
     30        1.1   ichiro #include <sys/cdefs.h>
     31  1.11.14.1    skrll __KERNEL_RCSID(0, "$NetBSD: ixp425_pci.c,v 1.11.14.1 2015/12/27 12:09:32 skrll Exp $");
     32        1.1   ichiro 
     33        1.1   ichiro #include <sys/param.h>
     34        1.1   ichiro #include <sys/systm.h>
     35        1.1   ichiro #include <sys/device.h>
     36        1.1   ichiro #include <sys/extent.h>
     37        1.1   ichiro #include <sys/malloc.h>
     38        1.1   ichiro 
     39        1.1   ichiro #include <uvm/uvm_extern.h>
     40        1.1   ichiro 
     41        1.7   dyoung #include <sys/bus.h>
     42        1.1   ichiro 
     43        1.1   ichiro #include <arm/xscale/ixp425reg.h>
     44        1.1   ichiro #include <arm/xscale/ixp425var.h>
     45        1.1   ichiro 
     46        1.1   ichiro #include <evbarm/ixdp425/ixdp425reg.h>
     47        1.1   ichiro 
     48        1.1   ichiro #include <dev/pci/pcireg.h>
     49        1.1   ichiro #include <dev/pci/pcivar.h>
     50        1.1   ichiro #include <dev/pci/pciconf.h>
     51        1.1   ichiro 
     52        1.1   ichiro #include "opt_pci.h"
     53        1.1   ichiro #include "pci.h"
     54        1.1   ichiro 
     55       1.10  msaitoh void	ixp425_pci_attach_hook(device_t, device_t,
     56        1.1   ichiro 	    struct pcibus_attach_args *);
     57        1.1   ichiro int	ixp425_pci_bus_maxdevs(void *, int);
     58        1.1   ichiro void	ixp425_pci_decompose_tag(void *, pcitag_t, int *, int *, int *);
     59        1.1   ichiro void	ixp425_pci_conf_setup(void *, struct ixp425_softc *, pcitag_t, int);
     60        1.1   ichiro void	ixp425_pci_conf_write(void *, pcitag_t, int, pcireg_t);
     61        1.9     matt void	ixp425_pci_conf_interrupt(void *, int, int, int, int, int *);
     62        1.1   ichiro pcitag_t ixp425_pci_make_tag(void *, int, int, int);
     63        1.1   ichiro pcireg_t ixp425_pci_conf_read(void *, pcitag_t, int);
     64        1.1   ichiro 
     65        1.1   ichiro #define	MAX_PCI_DEVICES	32
     66        1.1   ichiro 
     67        1.1   ichiro void
     68        1.2      scw ixp425_pci_init(struct ixp425_softc *sc)
     69        1.1   ichiro {
     70        1.2      scw 	pci_chipset_tag_t pc = &sc->ia_pci_chipset;
     71        1.1   ichiro #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     72        1.1   ichiro 	struct extent *ioext, *memext;
     73        1.1   ichiro #endif
     74        1.2      scw 	/*
     75        1.2      scw 	 * Initialise the PCI chipset tag
     76        1.2      scw 	 */
     77        1.2      scw 	pc->pc_conf_v = sc;
     78        1.1   ichiro 	pc->pc_attach_hook = ixp425_pci_attach_hook;
     79        1.1   ichiro 	pc->pc_bus_maxdevs = ixp425_pci_bus_maxdevs;
     80        1.1   ichiro 	pc->pc_make_tag = ixp425_pci_make_tag;
     81        1.1   ichiro 	pc->pc_decompose_tag = ixp425_pci_decompose_tag;
     82        1.1   ichiro 	pc->pc_conf_read = ixp425_pci_conf_read;
     83        1.1   ichiro 	pc->pc_conf_write = ixp425_pci_conf_write;
     84        1.9     matt 	pc->pc_conf_interrupt = ixp425_pci_conf_interrupt;
     85        1.2      scw 
     86        1.2      scw 	/*
     87        1.2      scw 	 * Initialize the bus space tags.
     88        1.2      scw 	 */
     89        1.2      scw 	ixp425_io_bs_init(&sc->sc_pci_iot, sc);
     90        1.2      scw 	ixp425_mem_bs_init(&sc->sc_pci_memt, sc);
     91        1.1   ichiro 
     92        1.1   ichiro #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     93        1.1   ichiro 	ioext  = extent_create("pciio", 0, IXP425_PCI_IO_SIZE - 1,
     94        1.8     para 				NULL, 0, EX_NOWAIT);
     95        1.1   ichiro 	/* PCI MEM space is mapped same address as real memory */
     96        1.3      scw 	memext = extent_create("pcimem", IXP425_PCI_MEM_HWBASE,
     97        1.3      scw 				IXP425_PCI_MEM_HWBASE +
     98        1.1   ichiro 				IXP425_PCI_MEM_SIZE - 1,
     99        1.8     para 				NULL, 0, EX_NOWAIT);
    100       1.10  msaitoh 	aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
    101        1.1   ichiro 	pci_configure_bus(pc, ioext, memext, NULL, 0 /* XXX bus = 0 */,
    102        1.1   ichiro 			  arm_dcache_align);
    103        1.1   ichiro 
    104        1.1   ichiro 	extent_destroy(ioext);
    105        1.1   ichiro 	extent_destroy(memext);
    106        1.1   ichiro #endif
    107        1.1   ichiro }
    108        1.1   ichiro 
    109        1.1   ichiro void
    110        1.9     matt ixp425_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
    111        1.1   ichiro {
    112        1.1   ichiro }
    113        1.1   ichiro 
    114        1.1   ichiro void
    115       1.10  msaitoh ixp425_pci_attach_hook(device_t parent, device_t self,
    116        1.1   ichiro 	struct pcibus_attach_args *pba)
    117        1.1   ichiro {
    118        1.1   ichiro 	/* Nothing to do. */
    119        1.1   ichiro }
    120        1.1   ichiro 
    121        1.1   ichiro int
    122        1.1   ichiro ixp425_pci_bus_maxdevs(void *v, int busno)
    123        1.1   ichiro {
    124        1.1   ichiro 	return(MAX_PCI_DEVICES);
    125        1.1   ichiro }
    126        1.1   ichiro 
    127        1.1   ichiro pcitag_t
    128        1.1   ichiro ixp425_pci_make_tag(void *v, int bus, int device, int function)
    129        1.1   ichiro {
    130        1.1   ichiro #ifdef PCI_DEBUG
    131        1.1   ichiro 	printf("ixp425_pci_make_tag(v=%p, bus=%d, device=%d, function=%d)\n",
    132        1.1   ichiro 		v, bus, device, function);
    133        1.1   ichiro #endif
    134        1.1   ichiro 	return ((bus << 16) | (device << 11) | (function << 8));
    135        1.1   ichiro }
    136        1.1   ichiro 
    137        1.1   ichiro void
    138        1.1   ichiro ixp425_pci_decompose_tag(void *v, pcitag_t tag, int *busp, int *devicep,
    139        1.1   ichiro 	int *functionp)
    140        1.1   ichiro {
    141        1.1   ichiro #ifdef PCI_DEBUG
    142        1.1   ichiro 	printf("ixp425_pci_decompose_tag(v=%p, tag=0x%08lx, bp=%x, dp=%x, fp=%x)\n",
    143        1.1   ichiro 		v, tag, (int)busp, (int)devicep, (int)functionp);
    144        1.1   ichiro #endif
    145        1.1   ichiro 	if (busp != NULL)
    146        1.1   ichiro 		*busp = (tag >> 16) & 0xff;
    147        1.1   ichiro 	if (devicep != NULL)
    148        1.1   ichiro 		*devicep = (tag >> 11) & 0x1f;
    149        1.1   ichiro 	if (functionp != NULL)
    150        1.1   ichiro 		*functionp = (tag >> 8) & 0x7;
    151        1.1   ichiro }
    152        1.1   ichiro 
    153        1.1   ichiro void
    154        1.1   ichiro ixp425_pci_conf_setup(void *v, struct ixp425_softc *sc, pcitag_t tag, int offset)
    155        1.1   ichiro {
    156        1.1   ichiro 	int bus, device, function;
    157        1.1   ichiro 
    158        1.1   ichiro 	ixp425_pci_decompose_tag(v, tag, &bus, &device, &function);
    159        1.1   ichiro 
    160        1.5   simonb 	if (bus == 0) {
    161        1.1   ichiro 		if (device == 0 && function == 0) {
    162        1.1   ichiro 			PCI_CSR_WRITE_4(sc, PCI_NP_AD, (offset & ~3));
    163        1.1   ichiro 		} else {
    164        1.1   ichiro 			/* configuration type 0 */
    165        1.1   ichiro 			PCI_CSR_WRITE_4(sc, PCI_NP_AD, (1U << (32 - device)) |
    166        1.1   ichiro 				(function << 8) | (offset & ~3));
    167        1.1   ichiro 		}
    168        1.1   ichiro 	} else {
    169        1.1   ichiro 			/* configuration type 1 */
    170        1.1   ichiro 		PCI_CSR_WRITE_4(sc, PCI_NP_AD,
    171        1.1   ichiro 			(bus << 16) | (device << 11) |
    172        1.1   ichiro 			(function << 8) | (offset & ~3) | 1);
    173        1.1   ichiro 	}
    174        1.1   ichiro }
    175        1.1   ichiro 
    176        1.1   ichiro /* read/write PCI Non-Pre-fetch Data */
    177        1.1   ichiro 
    178        1.1   ichiro pcireg_t
    179        1.1   ichiro ixp425_pci_conf_read(void *v, pcitag_t tag, int offset)
    180        1.1   ichiro {
    181        1.1   ichiro 	struct ixp425_softc *sc = v;
    182       1.11    skrll 	uint32_t data;
    183        1.1   ichiro 	pcireg_t rv;
    184        1.1   ichiro 	int s;
    185        1.1   ichiro #define PCI_NP_HAVE_BUG
    186        1.1   ichiro #ifdef PCI_NP_HAVE_BUG
    187        1.1   ichiro 	int i;
    188        1.1   ichiro #endif
    189        1.1   ichiro 
    190  1.11.14.1    skrll 	if ((unsigned int)offset >= PCI_CONF_SIZE)
    191  1.11.14.1    skrll 		return (pcireg_t) -1;
    192  1.11.14.1    skrll 
    193        1.1   ichiro 	PCI_CONF_LOCK(s);
    194        1.1   ichiro 	ixp425_pci_conf_setup(v, sc, tag, offset);
    195        1.1   ichiro 
    196        1.1   ichiro #ifdef PCI_DEBUG
    197        1.1   ichiro 	printf("ixp425_pci_conf_read: tag=%lx,offset=%x\n",
    198        1.5   simonb 		tag, offset);
    199        1.1   ichiro #endif
    200        1.1   ichiro 
    201        1.1   ichiro #ifdef PCI_NP_HAVE_BUG
    202        1.1   ichiro 	/* PCI NP Bug workaround */
    203        1.1   ichiro 	for (i = 0; i < 8; i++) {
    204        1.1   ichiro 		PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_READ);
    205        1.1   ichiro 		rv = PCI_CSR_READ_4(sc, PCI_NP_RDATA);
    206        1.1   ichiro 		rv = PCI_CSR_READ_4(sc, PCI_NP_RDATA);
    207        1.1   ichiro 	}
    208        1.1   ichiro #else
    209        1.1   ichiro 	PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_READ);
    210        1.1   ichiro 	rv = PCI_CSR_READ_4(sc, PCI_NP_RDATA);
    211        1.1   ichiro #endif
    212        1.1   ichiro 
    213        1.1   ichiro 	/* check&clear PCI abort */
    214        1.1   ichiro 	data = PCI_CSR_READ_4(sc, PCI_ISR);
    215        1.1   ichiro 	if (data & ISR_PFE) {
    216        1.1   ichiro 		PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
    217        1.1   ichiro 		PCI_CONF_UNLOCK(s);
    218        1.1   ichiro 		return -1;
    219        1.1   ichiro 	} else {
    220        1.1   ichiro 		PCI_CONF_UNLOCK(s);
    221        1.1   ichiro 		return rv;
    222        1.1   ichiro 	}
    223        1.1   ichiro }
    224        1.1   ichiro 
    225        1.1   ichiro void
    226        1.1   ichiro ixp425_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    227        1.1   ichiro {
    228        1.1   ichiro 	struct ixp425_softc *sc = v;
    229       1.11    skrll 	uint32_t data;
    230        1.1   ichiro 	int s;
    231        1.1   ichiro 
    232  1.11.14.1    skrll 	if ((unsigned int)offset >= PCI_CONF_SIZE)
    233  1.11.14.1    skrll 		return;
    234  1.11.14.1    skrll 
    235        1.1   ichiro 	PCI_CONF_LOCK(s);
    236        1.1   ichiro 
    237        1.1   ichiro 	ixp425_pci_conf_setup(v, sc, tag, offset);
    238        1.1   ichiro #ifdef PCI_DEBUG
    239        1.1   ichiro 	printf("ixp425_pci_conf_write: tag=%lx offset=%x <- val=%x\n",
    240        1.5   simonb 		tag, offset, val);
    241        1.1   ichiro #endif
    242        1.1   ichiro 	PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_WRITE);
    243        1.1   ichiro 	PCI_CSR_WRITE_4(sc, PCI_NP_WDATA, val);
    244        1.1   ichiro 
    245        1.1   ichiro 	/* check&clear PCI abort */
    246        1.1   ichiro 	data = PCI_CSR_READ_4(sc, PCI_ISR);
    247        1.1   ichiro 	if (data & ISR_PFE)
    248        1.1   ichiro 		PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
    249        1.1   ichiro 
    250        1.5   simonb 	PCI_CONF_UNLOCK(s);
    251        1.1   ichiro }
    252        1.1   ichiro 
    253        1.1   ichiro /* read/write pci configuration data */
    254        1.1   ichiro 
    255        1.1   ichiro uint32_t
    256        1.1   ichiro ixp425_pci_conf_reg_read(struct ixp425_softc *sc, uint32_t reg)
    257        1.1   ichiro {
    258        1.1   ichiro 	uint32_t data;
    259        1.1   ichiro 
    260        1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_pci_ioh,
    261        1.1   ichiro 		PCI_CRP_AD_CBE, ((reg & ~3) | COMMAND_CRP_READ));
    262        1.1   ichiro 	data = bus_space_read_4(sc->sc_iot, sc->sc_pci_ioh,
    263        1.1   ichiro 		PCI_CRP_AD_RDATA);
    264        1.1   ichiro 
    265        1.1   ichiro 	return data;
    266        1.1   ichiro }
    267        1.1   ichiro 
    268        1.1   ichiro void
    269        1.1   ichiro ixp425_pci_conf_reg_write(struct ixp425_softc *sc, uint32_t reg,
    270        1.1   ichiro 	uint32_t data)
    271        1.1   ichiro {
    272        1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_pci_ioh,
    273        1.1   ichiro 		PCI_CRP_AD_CBE, ((reg & ~3) | COMMAND_CRP_WRITE));
    274        1.1   ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_pci_ioh,
    275        1.1   ichiro 		PCI_CRP_AD_WDATA, data);
    276        1.1   ichiro }
    277