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