Home | History | Annotate | Line # | Download | only in marvell
gtpci.c revision 1.21.4.2
      1  1.21.4.2   rmind /*	$NetBSD: gtpci.c,v 1.21.4.2 2010/07/03 01:19:35 rmind Exp $	*/
      2       1.1    matt /*
      3  1.21.4.1   rmind  * Copyright (c) 2008, 2009 KIYOHARA Takashi
      4       1.1    matt  * All rights reserved.
      5       1.1    matt  *
      6       1.1    matt  * Redistribution and use in source and binary forms, with or without
      7       1.1    matt  * modification, are permitted provided that the following conditions
      8       1.1    matt  * are met:
      9       1.1    matt  * 1. Redistributions of source code must retain the above copyright
     10       1.1    matt  *    notice, this list of conditions and the following disclaimer.
     11       1.1    matt  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1    matt  *    notice, this list of conditions and the following disclaimer in the
     13       1.1    matt  *    documentation and/or other materials provided with the distribution.
     14       1.1    matt  *
     15  1.21.4.1   rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.21.4.1   rmind  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.21.4.1   rmind  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.21.4.1   rmind  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.21.4.1   rmind  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.21.4.1   rmind  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.21.4.1   rmind  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.21.4.1   rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.21.4.1   rmind  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.21.4.1   rmind  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25       1.1    matt  * POSSIBILITY OF SUCH DAMAGE.
     26       1.1    matt  */
     27      1.10   lukem 
     28      1.10   lukem #include <sys/cdefs.h>
     29  1.21.4.2   rmind __KERNEL_RCSID(0, "$NetBSD: gtpci.c,v 1.21.4.2 2010/07/03 01:19:35 rmind Exp $");
     30  1.21.4.1   rmind 
     31  1.21.4.1   rmind #include "opt_pci.h"
     32  1.21.4.1   rmind #include "pci.h"
     33       1.1    matt 
     34       1.1    matt #include <sys/param.h>
     35  1.21.4.1   rmind #include <sys/bus.h>
     36       1.1    matt #include <sys/device.h>
     37  1.21.4.1   rmind #include <sys/errno.h>
     38       1.1    matt #include <sys/extent.h>
     39       1.1    matt #include <sys/malloc.h>
     40       1.1    matt 
     41  1.21.4.1   rmind #include <prop/proplib.h>
     42       1.1    matt 
     43       1.1    matt #include <dev/pci/pcireg.h>
     44       1.1    matt #include <dev/pci/pcivar.h>
     45       1.1    matt #include <dev/pci/pciconf.h>
     46  1.21.4.1   rmind 
     47       1.1    matt #include <dev/marvell/gtpcireg.h>
     48       1.1    matt #include <dev/marvell/gtpcivar.h>
     49  1.21.4.1   rmind #include <dev/marvell/marvellreg.h>
     50  1.21.4.1   rmind #include <dev/marvell/marvellvar.h>
     51       1.1    matt 
     52  1.21.4.1   rmind #include <machine/pci_machdep.h>
     53       1.1    matt 
     54  1.21.4.1   rmind #include "locators.h"
     55       1.5    matt 
     56       1.1    matt 
     57  1.21.4.1   rmind #define GTPCI_READ(sc, r) \
     58  1.21.4.1   rmind 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit))
     59  1.21.4.1   rmind #define GTPCI_WRITE(sc, r, v) \
     60  1.21.4.1   rmind 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit), (v))
     61  1.21.4.1   rmind #define GTPCI_WRITE_AC(sc, r, n, v) \
     62  1.21.4.1   rmind     bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit, (n)), (v))
     63       1.1    matt 
     64       1.1    matt 
     65  1.21.4.1   rmind static int gtpci_match(device_t, struct cfdata *, void *);
     66      1.21  cegger static void gtpci_attach(device_t, device_t, void *);
     67       1.1    matt 
     68  1.21.4.2   rmind static void gtpci_init(struct gtpci_softc *, struct gtpci_prot *);
     69  1.21.4.1   rmind static void gtpci_barinit(struct gtpci_softc *);
     70  1.21.4.2   rmind static void gtpci_protinit(struct gtpci_softc *, struct gtpci_prot *);
     71  1.21.4.1   rmind #if NPCI > 0
     72  1.21.4.1   rmind static void gtpci_pci_config(struct gtpci_softc *, bus_space_tag_t,
     73  1.21.4.1   rmind 			     bus_space_tag_t, bus_dma_tag_t, pci_chipset_tag_t,
     74  1.21.4.1   rmind 			     u_long, u_long, u_long, u_long, int);
     75  1.21.4.1   rmind #endif
     76       1.1    matt 
     77       1.1    matt 
     78  1.21.4.1   rmind CFATTACH_DECL_NEW(gtpci_gt, sizeof(struct gtpci_softc),
     79  1.21.4.1   rmind     gtpci_match, gtpci_attach, NULL, NULL);
     80  1.21.4.1   rmind CFATTACH_DECL_NEW(gtpci_mbus, sizeof(struct gtpci_softc),
     81  1.21.4.1   rmind     gtpci_match, gtpci_attach, NULL, NULL);
     82       1.6    matt 
     83  1.21.4.1   rmind 
     84  1.21.4.1   rmind /* ARGSUSED */
     85  1.21.4.1   rmind static int
     86  1.21.4.1   rmind gtpci_match(device_t parent, struct cfdata *match, void *aux)
     87       1.1    matt {
     88  1.21.4.1   rmind 	struct marvell_attach_args *mva = aux;
     89       1.1    matt 
     90  1.21.4.1   rmind 	if (strcmp(mva->mva_name, match->cf_name) != 0)
     91  1.21.4.1   rmind 		return 0;
     92       1.1    matt 
     93  1.21.4.1   rmind 	switch (mva->mva_model) {
     94  1.21.4.1   rmind 	case MARVELL_DISCOVERY:
     95  1.21.4.1   rmind 	case MARVELL_DISCOVERY_II:
     96  1.21.4.1   rmind 	case MARVELL_DISCOVERY_III:
     97  1.21.4.1   rmind #if 0	/* XXXXX */
     98  1.21.4.1   rmind 	case MARVELL_DISCOVERY_LT:
     99  1.21.4.1   rmind 	case MARVELL_DISCOVERY_V:
    100  1.21.4.1   rmind 	case MARVELL_DISCOVERY_VI:
    101  1.21.4.1   rmind #endif
    102  1.21.4.1   rmind 		if (mva->mva_unit == GTCF_UNIT_DEFAULT ||
    103  1.21.4.1   rmind 		    mva->mva_offset != GTCF_OFFSET_DEFAULT)
    104  1.21.4.1   rmind 			return 0;
    105  1.21.4.1   rmind 		break;
    106      1.12   perry 
    107  1.21.4.1   rmind 	case MARVELL_ORION_1_88F5180N:
    108  1.21.4.1   rmind 	case MARVELL_ORION_1_88F5181:
    109  1.21.4.1   rmind 	case MARVELL_ORION_1_88F5182:
    110  1.21.4.1   rmind 	case MARVELL_ORION_2_88F5281:
    111  1.21.4.1   rmind 	case MARVELL_ORION_1_88W8660:
    112  1.21.4.1   rmind 		if (mva->mva_offset == GTCF_OFFSET_DEFAULT)
    113  1.21.4.1   rmind 			return 0;
    114  1.21.4.1   rmind 		mva->mva_unit = 0;	/* unit 0 only */
    115  1.21.4.1   rmind 		break;
    116       1.1    matt 
    117  1.21.4.1   rmind 	default:
    118  1.21.4.1   rmind 		return 0;
    119  1.21.4.1   rmind 	}
    120       1.1    matt 
    121  1.21.4.1   rmind 	mva->mva_size = GTPCI_SIZE;
    122  1.21.4.1   rmind 	return 1;
    123       1.1    matt }
    124       1.1    matt 
    125  1.21.4.1   rmind /* ARGSUSED */
    126  1.21.4.1   rmind static void
    127      1.21  cegger gtpci_attach(device_t parent, device_t self, void *aux)
    128       1.1    matt {
    129  1.21.4.1   rmind 	struct gtpci_softc *sc = device_private(self);
    130  1.21.4.1   rmind 	struct marvell_attach_args *mva = aux;
    131  1.21.4.2   rmind 	struct gtpci_prot *gtpci_prot;
    132  1.21.4.1   rmind 	prop_dictionary_t dict = device_properties(self);
    133  1.21.4.2   rmind 	prop_object_t prot;
    134  1.21.4.2   rmind #if NPCI > 0
    135  1.21.4.1   rmind 	prop_object_t pc, iot, memt;
    136  1.21.4.1   rmind 	prop_array_t int2gpp;
    137  1.21.4.1   rmind 	prop_object_t gpp;
    138  1.21.4.1   rmind 	pci_chipset_tag_t gtpci_chipset;
    139  1.21.4.1   rmind 	bus_space_tag_t gtpci_io_bs_tag, gtpci_mem_bs_tag;
    140  1.21.4.1   rmind 	uint64_t iostart = 0, ioend = 0, memstart = 0, memend = 0;
    141  1.21.4.1   rmind 	int cl_size = 0, intr;
    142  1.21.4.1   rmind #endif
    143       1.1    matt 
    144  1.21.4.1   rmind 	aprint_normal(": Marvell PCI Interface\n");
    145  1.21.4.1   rmind 	aprint_naive("\n");
    146  1.21.4.1   rmind 
    147  1.21.4.2   rmind 	prot = prop_dictionary_get(dict, "prot");
    148  1.21.4.2   rmind 	if (prot != NULL) {
    149  1.21.4.2   rmind 		KASSERT(prop_object_type(prot) == PROP_TYPE_DATA);
    150  1.21.4.2   rmind 		gtpci_prot = __UNCONST(prop_data_data_nocopy(prot));
    151  1.21.4.2   rmind 	} else {
    152  1.21.4.2   rmind 		aprint_verbose_dev(self, "no protection property\n");
    153  1.21.4.2   rmind 		gtpci_prot = NULL;
    154  1.21.4.2   rmind 	}
    155  1.21.4.1   rmind #if NPCI > 0
    156  1.21.4.1   rmind 	iot = prop_dictionary_get(dict, "io-bus-tag");
    157  1.21.4.2   rmind 	if (iot != NULL) {
    158  1.21.4.2   rmind 		KASSERT(prop_object_type(iot) == PROP_TYPE_DATA);
    159  1.21.4.2   rmind 		gtpci_io_bs_tag = __UNCONST(prop_data_data_nocopy(iot));
    160  1.21.4.2   rmind 	} else {
    161  1.21.4.1   rmind 		aprint_error_dev(self, "no io-bus-tag property\n");
    162  1.21.4.2   rmind 		gtpci_io_bs_tag = NULL;
    163  1.21.4.2   rmind 	}
    164  1.21.4.1   rmind 	memt = prop_dictionary_get(dict, "mem-bus-tag");
    165  1.21.4.2   rmind 	if (memt != NULL) {
    166  1.21.4.2   rmind 		KASSERT(prop_object_type(memt) == PROP_TYPE_DATA);
    167  1.21.4.2   rmind 		gtpci_mem_bs_tag = __UNCONST(prop_data_data_nocopy(memt));
    168  1.21.4.2   rmind 	} else {
    169  1.21.4.1   rmind 		aprint_error_dev(self, "no mem-bus-tag property\n");
    170  1.21.4.2   rmind 		gtpci_mem_bs_tag = NULL;
    171  1.21.4.2   rmind 	}
    172  1.21.4.1   rmind 	pc = prop_dictionary_get(dict, "pci-chipset");
    173  1.21.4.1   rmind 	if (pc == NULL) {
    174  1.21.4.1   rmind 		aprint_error_dev(self, "no pci-chipset property\n");
    175  1.21.4.1   rmind 		return;
    176  1.21.4.1   rmind 	}
    177  1.21.4.1   rmind 	KASSERT(prop_object_type(pc) == PROP_TYPE_DATA);
    178  1.21.4.1   rmind 	gtpci_chipset = __UNCONST(prop_data_data_nocopy(pc));
    179  1.21.4.1   rmind #ifdef PCI_NETBSD_CONFIGURE
    180  1.21.4.1   rmind 	if (!prop_dictionary_get_uint64(dict, "iostart", &iostart)) {
    181  1.21.4.1   rmind 		aprint_error_dev(self, "no iostart property\n");
    182  1.21.4.1   rmind 		return;
    183  1.21.4.1   rmind 	}
    184  1.21.4.1   rmind 	if (!prop_dictionary_get_uint64(dict, "ioend", &ioend)) {
    185  1.21.4.1   rmind 		aprint_error_dev(self, "no ioend property\n");
    186  1.21.4.1   rmind 		return;
    187  1.21.4.1   rmind 	}
    188  1.21.4.1   rmind 	if (!prop_dictionary_get_uint64(dict, "memstart", &memstart)) {
    189  1.21.4.1   rmind 		aprint_error_dev(self, "no memstart property\n");
    190       1.2    matt 		return;
    191       1.2    matt 	}
    192  1.21.4.1   rmind 	if (!prop_dictionary_get_uint64(dict, "memend", &memend)) {
    193  1.21.4.1   rmind 		aprint_error_dev(self, "no memend property\n");
    194  1.21.4.1   rmind 		return;
    195  1.21.4.1   rmind 	}
    196  1.21.4.1   rmind 	if (!prop_dictionary_get_uint32(dict, "cache-line-size", &cl_size)) {
    197  1.21.4.1   rmind 		aprint_error_dev(self, "no cache-line-size property\n");
    198  1.21.4.1   rmind 		return;
    199  1.21.4.1   rmind 	}
    200  1.21.4.1   rmind #endif
    201  1.21.4.1   rmind #endif
    202       1.2    matt 
    203  1.21.4.1   rmind 	sc->sc_dev = self;
    204  1.21.4.1   rmind 	sc->sc_model = mva->mva_model;
    205  1.21.4.1   rmind 	sc->sc_rev = mva->mva_revision;
    206  1.21.4.1   rmind 	sc->sc_unit = mva->mva_unit;
    207  1.21.4.1   rmind 	sc->sc_iot = mva->mva_iot;
    208  1.21.4.1   rmind 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    209  1.21.4.1   rmind 	    (mva->mva_offset != GTCF_OFFSET_DEFAULT) ? mva->mva_offset : 0,
    210  1.21.4.1   rmind 	    mva->mva_size, &sc->sc_ioh)) {
    211  1.21.4.1   rmind 		aprint_error_dev(self, "can't map registers\n");
    212  1.21.4.1   rmind 		return;
    213  1.21.4.1   rmind 	}
    214  1.21.4.1   rmind 	sc->sc_pc = gtpci_chipset;
    215  1.21.4.2   rmind 	gtpci_init(sc, gtpci_prot);
    216       1.2    matt 
    217  1.21.4.1   rmind #if NPCI > 0
    218  1.21.4.1   rmind 	int2gpp = prop_dictionary_get(dict, "int2gpp");
    219  1.21.4.1   rmind 	if (int2gpp != NULL) {
    220  1.21.4.1   rmind 		if (prop_object_type(int2gpp) != PROP_TYPE_ARRAY) {
    221  1.21.4.1   rmind 			aprint_error_dev(self, "int2gpp not an array\n");
    222  1.21.4.1   rmind 			return;
    223  1.21.4.1   rmind 		}
    224  1.21.4.1   rmind 		aprint_normal_dev(self, "use intrrupt pin:");
    225  1.21.4.1   rmind 		for (intr = PCI_INTERRUPT_PIN_A;
    226  1.21.4.1   rmind 		    intr <= PCI_INTERRUPT_PIN_D &&
    227  1.21.4.1   rmind 					intr < prop_array_count(int2gpp);
    228  1.21.4.1   rmind 		    intr++) {
    229  1.21.4.1   rmind 			gpp = prop_array_get(int2gpp, intr);
    230  1.21.4.1   rmind 			if (prop_object_type(gpp) != PROP_TYPE_NUMBER) {
    231  1.21.4.1   rmind 				aprint_error_dev(self,
    232  1.21.4.1   rmind 				    "int2gpp[%d] not an number\n", intr);
    233  1.21.4.1   rmind 				return;
    234  1.21.4.1   rmind 			}
    235  1.21.4.1   rmind 			aprint_normal(" %d",
    236  1.21.4.1   rmind 			    (int)prop_number_integer_value(gpp));
    237  1.21.4.1   rmind 		}
    238  1.21.4.1   rmind 		aprint_normal("\n");
    239  1.21.4.1   rmind 	}
    240       1.1    matt 
    241  1.21.4.1   rmind 	gtpci_pci_config(sc, gtpci_io_bs_tag, gtpci_mem_bs_tag, mva->mva_dmat,
    242  1.21.4.1   rmind 	    gtpci_chipset, iostart, ioend, memstart, memend, cl_size);
    243  1.21.4.1   rmind #endif
    244  1.21.4.1   rmind }
    245       1.1    matt 
    246  1.21.4.1   rmind static void
    247  1.21.4.2   rmind gtpci_init(struct gtpci_softc *sc, struct gtpci_prot *prot)
    248  1.21.4.1   rmind {
    249  1.21.4.1   rmind 	uint32_t reg;
    250       1.1    matt 
    251  1.21.4.1   rmind 	/* First, all disable.  Also WA CQ 4382 (bit15 must set 1)*/
    252  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_BARE, GTPCI_BARE_ALLDISABLE | (1 << 15));
    253       1.1    matt 
    254  1.21.4.1   rmind 	/* Enable Internal Arbiter */
    255  1.21.4.1   rmind 	reg = GTPCI_READ(sc, GTPCI_AC);
    256  1.21.4.1   rmind 	reg |= GTPCI_AC_EN;
    257  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_AC, reg);
    258  1.21.4.1   rmind 
    259  1.21.4.1   rmind 	gtpci_barinit(sc);
    260  1.21.4.2   rmind 	if (prot != NULL)
    261  1.21.4.2   rmind 		gtpci_protinit(sc, prot);
    262  1.21.4.1   rmind 
    263  1.21.4.1   rmind 	reg = GTPCI_READ(sc, GTPCI_ADC);
    264  1.21.4.1   rmind 	reg |= GTPCI_ADC_REMAPWRDIS;
    265  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_ADC, reg);
    266  1.21.4.1   rmind 
    267  1.21.4.1   rmind 	/* enable CPU-2-PCI ordering */
    268  1.21.4.1   rmind 	reg = GTPCI_READ(sc, GTPCI_C);
    269  1.21.4.1   rmind 	reg |= GTPCI_C_CPU2PCIORDERING;
    270  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_C, reg);
    271  1.21.4.1   rmind }
    272  1.21.4.1   rmind 
    273  1.21.4.1   rmind static void
    274  1.21.4.1   rmind gtpci_barinit(struct gtpci_softc *sc)
    275  1.21.4.1   rmind {
    276  1.21.4.1   rmind 	static const struct {
    277  1.21.4.1   rmind 		int tag;
    278  1.21.4.1   rmind 		int bars[2];	/* BAR Size registers */
    279  1.21.4.1   rmind 		int bare;	/* Bits of Base Address Registers Enable */
    280  1.21.4.1   rmind 		int func;
    281  1.21.4.1   rmind 		int balow;
    282  1.21.4.1   rmind 		int bahigh;
    283  1.21.4.1   rmind 	} maps[] = {
    284  1.21.4.1   rmind 		{ MARVELL_TAG_SDRAM_CS0,
    285  1.21.4.1   rmind 		  { GTPCI_CS0BARS(0),	GTPCI_CS0BARS(1) },
    286  1.21.4.1   rmind 		  GTPCI_BARE_CS0EN,	0, 0x10, 0x14 },
    287  1.21.4.1   rmind 		{ MARVELL_TAG_SDRAM_CS1,
    288  1.21.4.1   rmind 		  { GTPCI_CS1BARS(0),	GTPCI_CS1BARS(1) },
    289  1.21.4.1   rmind 		  GTPCI_BARE_CS1EN,	0, 0x18, 0x1c },
    290  1.21.4.1   rmind 		{ MARVELL_TAG_SDRAM_CS2,
    291  1.21.4.1   rmind 		  { GTPCI_CS2BARS(0),	GTPCI_CS2BARS(1) },
    292  1.21.4.1   rmind 		  GTPCI_BARE_CS2EN,	1, 0x10, 0x14 },
    293  1.21.4.1   rmind 		{ MARVELL_TAG_SDRAM_CS3,
    294  1.21.4.1   rmind 		  { GTPCI_CS3BARS(0),	GTPCI_CS3BARS(1) },
    295  1.21.4.1   rmind 		  GTPCI_BARE_CS3EN,	1, 0x18, 0x1c },
    296  1.21.4.1   rmind #if 0
    297  1.21.4.1   rmind 		{ ORION_TARGETID_INTERNALREG,
    298  1.21.4.1   rmind 		  { -1,			-1 },
    299  1.21.4.1   rmind 		  GTPCI_BARE_INTMEMEN,	0, 0x20, 0x24 },
    300  1.21.4.1   rmind 
    301  1.21.4.1   rmind 		{ ORION_TARGETID_DEVICE_CS0,
    302  1.21.4.1   rmind 		  { GTPCI_DCS0BARS(0),	GTPCI_DCS0BARS(1) },
    303  1.21.4.1   rmind 		  GTPCI_BARE_DEVCS0EN,	2, 0x10, 0x14 },
    304  1.21.4.1   rmind 		{ ORION_TARGETID_DEVICE_CS1,
    305  1.21.4.1   rmind 		  { GTPCI_DCS1BARS(0),	GTPCI_DCS1BARS(1) },
    306  1.21.4.1   rmind 		  GTPCI_BARE_DEVCS1EN,	2, 0x18, 0x1c },
    307  1.21.4.1   rmind 		{ ORION_TARGETID_DEVICE_CS2,
    308  1.21.4.1   rmind 		  { GTPCI_DCS2BARS(0),	GTPCI_DCS2BARS(1) },
    309  1.21.4.1   rmind 		  GTPCI_BARE_DEVCS2EN,	2, 0x20, 0x24 },
    310  1.21.4.1   rmind 		{ ORION_TARGETID_DEVICE_BOOTCS,
    311  1.21.4.1   rmind 		  { GTPCI_BCSBARS(0),	GTPCI_BCSBARS(1) },
    312  1.21.4.1   rmind 		  GTPCI_BARE_BOOTCSEN,	3, 0x18, 0x1c },
    313  1.21.4.1   rmind 		{ P2P Mem0 BAR,
    314  1.21.4.1   rmind 		  { GTPCI_P2PM0BARS(0),	GTPCI_P2PM0BARS(1) },
    315  1.21.4.1   rmind 		  GTPCI_BARE_P2PMEM0EN,	4, 0x10, 0x14 },
    316  1.21.4.1   rmind 		{ P2P I/O BAR,
    317  1.21.4.1   rmind 		  { GTPCI_P2PIOBARS(0),	GTPCI_P2PIOBARS(1) },
    318  1.21.4.1   rmind 		  GTPCI_BARE_P2PIO0EN,	4, 0x20, 0x24 },
    319  1.21.4.1   rmind 		{ Expansion ROM BAR,
    320  1.21.4.1   rmind 		  { GTPCI_EROMBARS(0),	GTPCI_EROMBARS(1) },
    321  1.21.4.1   rmind 		  0,				},
    322  1.21.4.1   rmind #endif
    323       1.1    matt 
    324  1.21.4.1   rmind 		{ MARVELL_TAG_UNDEFINED,
    325  1.21.4.1   rmind 		  { -1,			-1 },
    326  1.21.4.1   rmind 		  -1,				-1, 0x00, 0x00 },
    327  1.21.4.1   rmind 	};
    328  1.21.4.1   rmind 	device_t pdev = device_parent(sc->sc_dev);
    329  1.21.4.1   rmind 	uint64_t base;
    330  1.21.4.1   rmind 	uint32_t p2pc, size, bare;
    331  1.21.4.1   rmind 	int map, bus, dev, rv;
    332  1.21.4.1   rmind 
    333  1.21.4.1   rmind 	p2pc = GTPCI_READ(sc, GTPCI_P2PC);
    334  1.21.4.1   rmind 	bus = GTPCI_P2PC_BUSNUMBER(p2pc);
    335  1.21.4.1   rmind 	dev = GTPCI_P2PC_DEVNUM(p2pc);
    336  1.21.4.1   rmind 
    337  1.21.4.1   rmind 	bare = GTPCI_BARE_ALLDISABLE;
    338  1.21.4.1   rmind 	for (map = 0; maps[map].tag != MARVELL_TAG_UNDEFINED; map++) {
    339  1.21.4.1   rmind 		rv = marvell_winparams_by_tag(pdev, maps[map].tag, NULL, NULL,
    340  1.21.4.1   rmind 		    &base, &size);
    341  1.21.4.1   rmind 		if (rv != 0 || size == 0)
    342  1.21.4.1   rmind 			continue;
    343  1.21.4.1   rmind 
    344  1.21.4.1   rmind 		if (maps[map].bars[sc->sc_unit] != -1)
    345  1.21.4.1   rmind 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    346  1.21.4.1   rmind 			    maps[map].bars[sc->sc_unit], GTPCI_BARSIZE(size));
    347  1.21.4.1   rmind 		bare &= ~maps[map].bare;
    348  1.21.4.1   rmind 
    349  1.21.4.1   rmind #if 0	/* shall move to pchb(4)? */
    350  1.21.4.1   rmind 		if (maps[map].func != -1) {
    351  1.21.4.1   rmind 			pcitag_t tag;
    352  1.21.4.1   rmind 			pcireg_t reg;
    353  1.21.4.1   rmind 
    354  1.21.4.1   rmind 			tag = gtpci_make_tag(NULL, bus, dev, maps[map].func);
    355  1.21.4.1   rmind 			reg = gtpci_conf_read(sc, tag, maps[map].balow);
    356  1.21.4.1   rmind 			reg &= ~GTPCI_BARLOW_MASK;
    357  1.21.4.1   rmind 			reg |= GTPCI_BARLOW_BASE(base);
    358  1.21.4.1   rmind 			gtpci_conf_write(sc, tag, maps[map].balow, reg);
    359  1.21.4.1   rmind 			reg = gtpci_conf_read(sc, tag, maps[map].bahigh);
    360  1.21.4.1   rmind 			reg = (base >> 16) >> 16;
    361  1.21.4.1   rmind 			gtpci_conf_write(sc, tag, maps[map].bahigh, reg);
    362       1.6    matt 		}
    363  1.21.4.1   rmind #endif
    364       1.9     scw 	}
    365  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_BARE, bare);
    366       1.5    matt }
    367       1.5    matt 
    368  1.21.4.1   rmind static void
    369  1.21.4.2   rmind gtpci_protinit(struct gtpci_softc *sc, struct gtpci_prot *ac_flags)
    370       1.1    matt {
    371  1.21.4.1   rmind 	enum {
    372  1.21.4.2   rmind 		gt642xx = 0,
    373  1.21.4.2   rmind 		mv643xx,
    374  1.21.4.2   rmind 		arm_soc,
    375  1.21.4.1   rmind 	};
    376  1.21.4.2   rmind 	const struct gtpci_ac_rshift {
    377  1.21.4.2   rmind 		uint32_t base_rshift;
    378  1.21.4.2   rmind 		uint32_t size_rshift;
    379  1.21.4.2   rmind 	} ac_rshifts[] = {
    380  1.21.4.2   rmind 		{ 20, 20, },	/* GT642xx */
    381  1.21.4.2   rmind 		{  0,  0, },	/* MV643xx and after */
    382  1.21.4.2   rmind 		{  0,  0, },	/* ARM SoC */
    383  1.21.4.1   rmind 	};
    384  1.21.4.1   rmind 	const uint32_t prot_tags[] = {
    385  1.21.4.1   rmind 		MARVELL_TAG_SDRAM_CS0,
    386  1.21.4.1   rmind 		MARVELL_TAG_SDRAM_CS1,
    387  1.21.4.1   rmind 		MARVELL_TAG_SDRAM_CS2,
    388  1.21.4.1   rmind 		MARVELL_TAG_SDRAM_CS3,
    389  1.21.4.1   rmind 		MARVELL_TAG_UNDEFINED
    390  1.21.4.1   rmind 	};
    391  1.21.4.1   rmind 	device_t pdev = device_parent(sc->sc_dev);
    392  1.21.4.1   rmind 	uint64_t acbase, base;
    393  1.21.4.1   rmind 	uint32_t acsize, size;
    394  1.21.4.2   rmind 	int base_rshift, size_rshift, acbl_flags, acs_flags;
    395  1.21.4.1   rmind 	int prot, rv, p, t;
    396  1.21.4.1   rmind 
    397  1.21.4.1   rmind 	switch (sc->sc_model) {
    398  1.21.4.1   rmind 	case MARVELL_DISCOVERY:
    399  1.21.4.2   rmind 		p = gt642xx;
    400  1.21.4.1   rmind 		break;
    401       1.1    matt 
    402  1.21.4.1   rmind 	case MARVELL_DISCOVERY_II:
    403  1.21.4.1   rmind 	case MARVELL_DISCOVERY_III:
    404       1.3    matt #if 0
    405  1.21.4.1   rmind 	case MARVELL_DISCOVERY_LT:
    406  1.21.4.1   rmind 	case MARVELL_DISCOVERY_V:
    407  1.21.4.1   rmind 	case MARVELL_DISCOVERY_VI:
    408       1.3    matt #endif
    409  1.21.4.2   rmind 		p = mv643xx;
    410  1.21.4.1   rmind 		break;
    411       1.1    matt 
    412  1.21.4.1   rmind 	default:
    413  1.21.4.2   rmind 		p = arm_soc;
    414  1.21.4.1   rmind 		break;
    415  1.21.4.1   rmind 	}
    416  1.21.4.2   rmind 	base_rshift = ac_rshifts[p].base_rshift;
    417  1.21.4.2   rmind 	size_rshift = ac_rshifts[p].size_rshift;
    418  1.21.4.2   rmind 	acbl_flags = ac_flags->acbl_flags;
    419  1.21.4.2   rmind 	acs_flags = ac_flags->acs_flags;
    420  1.21.4.1   rmind 
    421  1.21.4.1   rmind 	t = 0;
    422  1.21.4.1   rmind 	for (prot = 0; prot < GTPCI_NPCIAC; prot++) {
    423  1.21.4.1   rmind 		acbase = acsize = 0;
    424  1.21.4.1   rmind 
    425  1.21.4.1   rmind 		for ( ; prot_tags[t] != MARVELL_TAG_UNDEFINED; t++) {
    426  1.21.4.1   rmind 			rv = marvell_winparams_by_tag(pdev, prot_tags[t],
    427  1.21.4.1   rmind 			    NULL, NULL, &base, &size);
    428  1.21.4.1   rmind 			if (rv != 0 || size == 0)
    429  1.21.4.1   rmind 				continue;
    430  1.21.4.1   rmind 
    431  1.21.4.1   rmind 			if (acsize == 0 || base + size == acbase)
    432  1.21.4.1   rmind 				acbase = base;
    433  1.21.4.1   rmind 			else if (acbase + acsize != base)
    434  1.21.4.1   rmind 				break;
    435  1.21.4.1   rmind 			acsize += size;
    436       1.6    matt 		}
    437       1.6    matt 
    438  1.21.4.1   rmind 		if (acsize != 0) {
    439  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACBL, prot,
    440  1.21.4.2   rmind 			   ((acbase & 0xffffffff) >> base_rshift) | acbl_flags);
    441  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACBH, prot,
    442  1.21.4.1   rmind 			    (acbase >> 32) & 0xffffffff);
    443  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACS, prot,
    444  1.21.4.2   rmind 			    ((acsize - 1) >> size_rshift) | acs_flags);
    445  1.21.4.1   rmind 		} else {
    446  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACBL, prot, 0);
    447  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACBH, prot, 0);
    448  1.21.4.1   rmind 			GTPCI_WRITE_AC(sc, GTPCI_ACS, prot, 0);
    449       1.6    matt 		}
    450       1.6    matt 	}
    451  1.21.4.1   rmind 	return;
    452       1.1    matt }
    453       1.1    matt 
    454  1.21.4.1   rmind #if NPCI > 0
    455  1.21.4.1   rmind static void
    456  1.21.4.1   rmind gtpci_pci_config(struct gtpci_softc *sc, bus_space_tag_t iot,
    457  1.21.4.1   rmind 		 bus_space_tag_t memt, bus_dma_tag_t dmat, pci_chipset_tag_t pc,
    458  1.21.4.1   rmind 		 u_long iostart, u_long ioend, u_long memstart, u_long memend,
    459  1.21.4.1   rmind 		 int cacheline_size)
    460       1.1    matt {
    461  1.21.4.1   rmind 	struct pcibus_attach_args pba;
    462  1.21.4.1   rmind #ifdef PCI_NETBSD_CONFIGURE
    463  1.21.4.1   rmind 	struct extent *ioext = NULL, *memext = NULL;
    464  1.21.4.1   rmind #endif
    465  1.21.4.1   rmind 	uint32_t p2pc, command;
    466       1.1    matt 
    467  1.21.4.1   rmind 	p2pc = GTPCI_READ(sc, GTPCI_P2PC);
    468       1.1    matt 
    469  1.21.4.1   rmind #ifdef PCI_NETBSD_CONFIGURE
    470  1.21.4.1   rmind 	ioext = extent_create("pciio", iostart, ioend, M_DEVBUF, NULL, 0,
    471  1.21.4.1   rmind 	    EX_NOWAIT);
    472  1.21.4.1   rmind 	memext = extent_create("pcimem", memstart, memend, M_DEVBUF, NULL, 0,
    473  1.21.4.1   rmind 	    EX_NOWAIT);
    474  1.21.4.1   rmind 	if (ioext != NULL && memext != NULL)
    475  1.21.4.1   rmind 		pci_configure_bus(pc, ioext, memext, NULL,
    476  1.21.4.1   rmind 		    GTPCI_P2PC_BUSNUMBER(p2pc), cacheline_size);
    477  1.21.4.1   rmind 	else
    478  1.21.4.1   rmind 		aprint_error_dev(sc->sc_dev, "can't create extent %s%s%s\n",
    479  1.21.4.1   rmind 		    ioext == NULL ? "io" : "",
    480  1.21.4.1   rmind 		    ioext == NULL && memext == NULL ? " and " : "",
    481  1.21.4.1   rmind 		    memext == NULL ? "mem" : "");
    482  1.21.4.1   rmind 	if (ioext != NULL)
    483  1.21.4.1   rmind 		extent_destroy(ioext);
    484  1.21.4.1   rmind 	if (memext != NULL)
    485  1.21.4.1   rmind 		extent_destroy(memext);
    486       1.1    matt #endif
    487  1.21.4.1   rmind 
    488  1.21.4.1   rmind 	pba.pba_iot = iot;
    489  1.21.4.1   rmind 	pba.pba_memt = memt;
    490  1.21.4.1   rmind 	pba.pba_dmat = dmat;
    491  1.21.4.1   rmind 	pba.pba_dmat64 = NULL;
    492  1.21.4.1   rmind 	pba.pba_pc = pc;
    493  1.21.4.1   rmind 	if (iot == NULL || memt == NULL) {
    494  1.21.4.1   rmind 		pba.pba_flags = 0;
    495  1.21.4.1   rmind 		aprint_error_dev(sc->sc_dev, "");
    496  1.21.4.1   rmind 		if (iot == NULL)
    497  1.21.4.1   rmind 			aprint_error("io ");
    498  1.21.4.1   rmind 		else
    499  1.21.4.1   rmind 			pba.pba_flags |= PCI_FLAGS_IO_ENABLED;
    500  1.21.4.1   rmind 		if (iot == NULL && memt == NULL)
    501  1.21.4.1   rmind 			aprint_error("and ");
    502  1.21.4.1   rmind 		if (memt == NULL)
    503  1.21.4.1   rmind 			aprint_error("mem");
    504  1.21.4.1   rmind 		else
    505  1.21.4.1   rmind 			pba.pba_flags |= PCI_FLAGS_MEM_ENABLED;
    506  1.21.4.1   rmind 		aprint_error(" access disabled\n");
    507  1.21.4.1   rmind 	} else
    508  1.21.4.1   rmind 		pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    509  1.21.4.1   rmind 	command = GTPCI_READ(sc, GTPCI_C);
    510  1.21.4.1   rmind 	if (command & GTPCI_C_MRDMUL)
    511  1.21.4.1   rmind 		pba.pba_flags |= PCI_FLAGS_MRM_OKAY;
    512  1.21.4.1   rmind 	if (command & GTPCI_C_MRDLINE)
    513  1.21.4.1   rmind 		pba.pba_flags |= PCI_FLAGS_MRL_OKAY;
    514  1.21.4.1   rmind 	pba.pba_flags |= PCI_FLAGS_MWI_OKAY;
    515  1.21.4.1   rmind 	pba.pba_bus = GTPCI_P2PC_BUSNUMBER(p2pc);
    516  1.21.4.1   rmind 	pba.pba_bridgetag = NULL;
    517  1.21.4.1   rmind 	config_found_ia(sc->sc_dev, "pcibus", &pba, NULL);
    518       1.1    matt }
    519       1.1    matt 
    520       1.1    matt 
    521  1.21.4.1   rmind /*
    522  1.21.4.1   rmind  * Dependent code of PCI Interface of Marvell
    523  1.21.4.1   rmind  */
    524  1.21.4.1   rmind 
    525  1.21.4.1   rmind /* ARGSUSED */
    526       1.1    matt void
    527  1.21.4.1   rmind gtpci_attach_hook(device_t parent, device_t self,
    528  1.21.4.1   rmind 		  struct pcibus_attach_args *pba)
    529       1.1    matt {
    530  1.21.4.1   rmind 
    531  1.21.4.1   rmind 	/* Nothing */
    532       1.1    matt }
    533       1.1    matt 
    534       1.1    matt /*
    535  1.21.4.1   rmind  * Bit map for configuration register:
    536  1.21.4.1   rmind  *   [31]    ConfigEn
    537  1.21.4.1   rmind  *   [30:24] Reserved
    538  1.21.4.1   rmind  *   [23:16] BusNum
    539  1.21.4.1   rmind  *   [15:11] DevNum
    540  1.21.4.1   rmind  *   [10: 8] FunctNum
    541  1.21.4.1   rmind  *   [ 7: 2] RegNum
    542  1.21.4.1   rmind  *   [ 1: 0] reserved
    543       1.1    matt  */
    544  1.21.4.1   rmind 
    545  1.21.4.1   rmind /* ARGSUSED */
    546       1.1    matt int
    547  1.21.4.1   rmind gtpci_bus_maxdevs(void *v, int busno)
    548       1.1    matt {
    549  1.21.4.1   rmind 
    550  1.21.4.1   rmind 	return 32;	/* 32 device/bus */
    551       1.1    matt }
    552       1.1    matt 
    553  1.21.4.1   rmind /* ARGSUSED */
    554       1.1    matt pcitag_t
    555  1.21.4.1   rmind gtpci_make_tag(void *v, int bus, int dev, int func)
    556       1.1    matt {
    557  1.21.4.1   rmind 
    558  1.21.4.1   rmind #if DIAGNOSTIC
    559  1.21.4.1   rmind 	if (bus >= 256 || dev >= 32 || func >= 8)
    560  1.21.4.1   rmind 		panic("pci_make_tag: bad request");
    561  1.21.4.1   rmind #endif
    562  1.21.4.1   rmind 
    563  1.21.4.1   rmind 	return (bus << 16) | (dev << 11) | (func << 8);
    564       1.1    matt }
    565       1.1    matt 
    566  1.21.4.1   rmind /* ARGSUSED */
    567       1.1    matt void
    568  1.21.4.1   rmind gtpci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    569       1.1    matt {
    570  1.21.4.1   rmind 
    571       1.1    matt 	if (bp != NULL)
    572  1.21.4.1   rmind 		*bp = (tag >> 16) & 0xff;
    573       1.1    matt 	if (dp != NULL)
    574  1.21.4.1   rmind 		*dp = (tag >> 11) & 0x1f;
    575       1.1    matt 	if (fp != NULL)
    576  1.21.4.1   rmind 		*fp = (tag >> 8) & 0x07;
    577       1.1    matt }
    578       1.1    matt 
    579       1.1    matt pcireg_t
    580  1.21.4.1   rmind gtpci_conf_read(void *v, pcitag_t tag, int reg)
    581       1.1    matt {
    582  1.21.4.1   rmind 	struct gtpci_softc *sc = v;
    583  1.21.4.1   rmind 	const pcireg_t addr = tag | reg;
    584  1.21.4.1   rmind 
    585  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_CA, addr | GTPCI_CA_CONFIGEN);
    586  1.21.4.1   rmind 	if ((addr | GTPCI_CA_CONFIGEN) != GTPCI_READ(sc, GTPCI_CA))
    587  1.21.4.1   rmind 		return -1;
    588  1.21.4.1   rmind 
    589  1.21.4.1   rmind 	return GTPCI_READ(sc, GTPCI_CD);
    590       1.1    matt }
    591       1.1    matt 
    592       1.1    matt void
    593  1.21.4.1   rmind gtpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
    594       1.1    matt {
    595  1.21.4.1   rmind 	struct gtpci_softc *sc = v;
    596  1.21.4.1   rmind 	pcireg_t addr = tag | (reg & 0xfc);
    597       1.1    matt 
    598  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_CA, addr | GTPCI_CA_CONFIGEN);
    599  1.21.4.1   rmind 	if ((addr | GTPCI_CA_CONFIGEN) != GTPCI_READ(sc, GTPCI_CA))
    600  1.21.4.1   rmind 		return;
    601       1.1    matt 
    602  1.21.4.1   rmind 	GTPCI_WRITE(sc, GTPCI_CD, data);
    603       1.1    matt }
    604       1.1    matt 
    605  1.21.4.1   rmind /* ARGSUSED */
    606  1.21.4.1   rmind int
    607  1.21.4.1   rmind gtpci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func, pcireg_t id)
    608       1.1    matt {
    609  1.21.4.1   rmind 	/* Oops, We have two PCI buses. */
    610  1.21.4.1   rmind 	if (dev == 0 &&
    611  1.21.4.1   rmind 	    PCI_VENDOR(id) == PCI_VENDOR_MARVELL) {
    612  1.21.4.1   rmind 		switch (PCI_PRODUCT(id)) {
    613  1.21.4.1   rmind 		case MARVELL_DISCOVERY:
    614  1.21.4.1   rmind 		case MARVELL_DISCOVERY_II:
    615  1.21.4.1   rmind 		case MARVELL_DISCOVERY_III:
    616  1.21.4.1   rmind #if 0
    617  1.21.4.1   rmind 		case MARVELL_DISCOVERY_LT:
    618  1.21.4.1   rmind 		case MARVELL_DISCOVERY_V:
    619  1.21.4.1   rmind 		case MARVELL_DISCOVERY_VI:
    620  1.21.4.1   rmind #endif
    621  1.21.4.1   rmind 		case MARVELL_ORION_1_88F5180N:
    622  1.21.4.1   rmind 		case MARVELL_ORION_1_88F5181:
    623  1.21.4.1   rmind 		case MARVELL_ORION_1_88F5182:
    624  1.21.4.1   rmind 		case MARVELL_ORION_2_88F5281:
    625  1.21.4.1   rmind 		case MARVELL_ORION_1_88W8660:
    626  1.21.4.1   rmind 			/* Don't configure us. */
    627  1.21.4.1   rmind 			return 0;
    628  1.21.4.1   rmind 		}
    629  1.21.4.1   rmind 	}
    630       1.1    matt 
    631  1.21.4.1   rmind 	return PCI_CONF_DEFAULT;
    632       1.1    matt }
    633  1.21.4.1   rmind #endif	/* NPCI > 0 */
    634