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