xref: /rk3399_rockchip-uboot/drivers/pci/pci_mvebu.c (revision f77309d34325369dbdf0bf62387c9e974f1b37da)
19c28d61cSAnton Schubert /*
29c28d61cSAnton Schubert  * PCIe driver for Marvell MVEBU SoCs
39c28d61cSAnton Schubert  *
49c28d61cSAnton Schubert  * Based on Barebox drivers/pci/pci-mvebu.c
59c28d61cSAnton Schubert  *
69c28d61cSAnton Schubert  * Ported to U-Boot by:
79c28d61cSAnton Schubert  * Anton Schubert <anton.schubert@gmx.de>
89c28d61cSAnton Schubert  * Stefan Roese <sr@denx.de>
99c28d61cSAnton Schubert  *
109c28d61cSAnton Schubert  * SPDX-License-Identifier:	GPL-2.0
119c28d61cSAnton Schubert  */
129c28d61cSAnton Schubert 
139c28d61cSAnton Schubert #include <common.h>
149c28d61cSAnton Schubert #include <pci.h>
151221ce45SMasahiro Yamada #include <linux/errno.h>
169c28d61cSAnton Schubert #include <asm/io.h>
179c28d61cSAnton Schubert #include <asm/arch/cpu.h>
189c28d61cSAnton Schubert #include <asm/arch/soc.h>
199c28d61cSAnton Schubert #include <linux/mbus.h>
209c28d61cSAnton Schubert 
219c28d61cSAnton Schubert DECLARE_GLOBAL_DATA_PTR;
229c28d61cSAnton Schubert 
239c28d61cSAnton Schubert /* PCIe unit register offsets */
249c28d61cSAnton Schubert #define SELECT(x, n)			((x >> n) & 1UL)
259c28d61cSAnton Schubert 
269c28d61cSAnton Schubert #define PCIE_DEV_ID_OFF			0x0000
279c28d61cSAnton Schubert #define PCIE_CMD_OFF			0x0004
289c28d61cSAnton Schubert #define PCIE_DEV_REV_OFF		0x0008
299c28d61cSAnton Schubert #define  PCIE_BAR_LO_OFF(n)		(0x0010 + ((n) << 3))
309c28d61cSAnton Schubert #define  PCIE_BAR_HI_OFF(n)		(0x0014 + ((n) << 3))
319c28d61cSAnton Schubert #define PCIE_CAPAB_OFF			0x0060
329c28d61cSAnton Schubert #define PCIE_CTRL_STAT_OFF		0x0068
339c28d61cSAnton Schubert #define PCIE_HEADER_LOG_4_OFF		0x0128
349c28d61cSAnton Schubert #define  PCIE_BAR_CTRL_OFF(n)		(0x1804 + (((n) - 1) * 4))
359c28d61cSAnton Schubert #define  PCIE_WIN04_CTRL_OFF(n)		(0x1820 + ((n) << 4))
369c28d61cSAnton Schubert #define  PCIE_WIN04_BASE_OFF(n)		(0x1824 + ((n) << 4))
379c28d61cSAnton Schubert #define  PCIE_WIN04_REMAP_OFF(n)	(0x182c + ((n) << 4))
389c28d61cSAnton Schubert #define PCIE_WIN5_CTRL_OFF		0x1880
399c28d61cSAnton Schubert #define PCIE_WIN5_BASE_OFF		0x1884
409c28d61cSAnton Schubert #define PCIE_WIN5_REMAP_OFF		0x188c
419c28d61cSAnton Schubert #define PCIE_CONF_ADDR_OFF		0x18f8
429c28d61cSAnton Schubert #define  PCIE_CONF_ADDR_EN		BIT(31)
439c28d61cSAnton Schubert #define  PCIE_CONF_REG(r)		((((r) & 0xf00) << 16) | ((r) & 0xfc))
449c28d61cSAnton Schubert #define  PCIE_CONF_BUS(b)		(((b) & 0xff) << 16)
459c28d61cSAnton Schubert #define  PCIE_CONF_DEV(d)		(((d) & 0x1f) << 11)
469c28d61cSAnton Schubert #define  PCIE_CONF_FUNC(f)		(((f) & 0x7) << 8)
479c28d61cSAnton Schubert #define  PCIE_CONF_ADDR(dev, reg) \
489c28d61cSAnton Schubert 	(PCIE_CONF_BUS(PCI_BUS(dev)) | PCIE_CONF_DEV(PCI_DEV(dev))    | \
499c28d61cSAnton Schubert 	 PCIE_CONF_FUNC(PCI_FUNC(dev)) | PCIE_CONF_REG(reg) | \
509c28d61cSAnton Schubert 	 PCIE_CONF_ADDR_EN)
519c28d61cSAnton Schubert #define PCIE_CONF_DATA_OFF		0x18fc
529c28d61cSAnton Schubert #define PCIE_MASK_OFF			0x1910
539c28d61cSAnton Schubert #define  PCIE_MASK_ENABLE_INTS          (0xf << 24)
549c28d61cSAnton Schubert #define PCIE_CTRL_OFF			0x1a00
559c28d61cSAnton Schubert #define  PCIE_CTRL_X1_MODE		BIT(0)
569c28d61cSAnton Schubert #define PCIE_STAT_OFF			0x1a04
579c28d61cSAnton Schubert #define  PCIE_STAT_BUS                  (0xff << 8)
589c28d61cSAnton Schubert #define  PCIE_STAT_DEV                  (0x1f << 16)
599c28d61cSAnton Schubert #define  PCIE_STAT_LINK_DOWN		BIT(0)
609c28d61cSAnton Schubert #define PCIE_DEBUG_CTRL			0x1a60
619c28d61cSAnton Schubert #define  PCIE_DEBUG_SOFT_RESET		BIT(20)
629c28d61cSAnton Schubert 
639c28d61cSAnton Schubert struct resource {
649c28d61cSAnton Schubert 	u32 start;
659c28d61cSAnton Schubert 	u32 end;
669c28d61cSAnton Schubert };
679c28d61cSAnton Schubert 
689c28d61cSAnton Schubert struct mvebu_pcie {
699c28d61cSAnton Schubert 	struct pci_controller hose;
709c28d61cSAnton Schubert 	char *name;
719c28d61cSAnton Schubert 	void __iomem *base;
729c28d61cSAnton Schubert 	void __iomem *membase;
739c28d61cSAnton Schubert 	struct resource mem;
749c28d61cSAnton Schubert 	void __iomem *iobase;
759c28d61cSAnton Schubert 	u32 port;
769c28d61cSAnton Schubert 	u32 lane;
779c28d61cSAnton Schubert 	u32 lane_mask;
789c28d61cSAnton Schubert 	pci_dev_t dev;
799c28d61cSAnton Schubert };
809c28d61cSAnton Schubert 
819c28d61cSAnton Schubert #define to_pcie(_hc)	container_of(_hc, struct mvebu_pcie, pci)
829c28d61cSAnton Schubert 
839c28d61cSAnton Schubert /*
849c28d61cSAnton Schubert  * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
859c28d61cSAnton Schubert  * into SoCs address space. Each controller will map 32M of MEM
869c28d61cSAnton Schubert  * and 64K of I/O space when registered.
879c28d61cSAnton Schubert  */
889c28d61cSAnton Schubert static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
899c28d61cSAnton Schubert #define PCIE_MEM_SIZE	(32 << 20)
909c28d61cSAnton Schubert 
919c28d61cSAnton Schubert #if defined(CONFIG_ARMADA_38X)
929c28d61cSAnton Schubert #define PCIE_BASE(if)					\
939c28d61cSAnton Schubert 	((if) == 0 ?					\
94*882d3fa6SDirk Eibach 	 MVEBU_REG_PCIE0_BASE :				\
95*882d3fa6SDirk Eibach 	 (MVEBU_REG_PCIE_BASE + 0x4000 * (if - 1)))
969c28d61cSAnton Schubert 
979c28d61cSAnton Schubert /*
989c28d61cSAnton Schubert  * On A38x MV6820 these PEX ports are supported:
999c28d61cSAnton Schubert  *  0 - Port 0.0
100*882d3fa6SDirk Eibach  *  1 - Port 1.0
101*882d3fa6SDirk Eibach  *  2 - Port 2.0
102*882d3fa6SDirk Eibach  *  3 - Port 3.0
1039c28d61cSAnton Schubert  */
104*882d3fa6SDirk Eibach #define MAX_PEX 4
1059c28d61cSAnton Schubert static struct mvebu_pcie pcie_bus[MAX_PEX];
1069c28d61cSAnton Schubert 
mvebu_get_port_lane(struct mvebu_pcie * pcie,int pex_idx,int * mem_target,int * mem_attr)1079c28d61cSAnton Schubert static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
1089c28d61cSAnton Schubert 				int *mem_target, int *mem_attr)
1099c28d61cSAnton Schubert {
110*882d3fa6SDirk Eibach 	u8 port[] = { 0, 1, 2, 3 };
111*882d3fa6SDirk Eibach 	u8 lane[] = { 0, 0, 0, 0 };
112*882d3fa6SDirk Eibach 	u8 target[] = { 8, 4, 4, 4 };
113*882d3fa6SDirk Eibach 	u8 attr[] = { 0xe8, 0xe8, 0xd8, 0xb8 };
1149c28d61cSAnton Schubert 
1159c28d61cSAnton Schubert 	pcie->port = port[pex_idx];
1169c28d61cSAnton Schubert 	pcie->lane = lane[pex_idx];
1179c28d61cSAnton Schubert 	*mem_target = target[pex_idx];
1189c28d61cSAnton Schubert 	*mem_attr = attr[pex_idx];
1199c28d61cSAnton Schubert }
1209c28d61cSAnton Schubert #else
1219c28d61cSAnton Schubert #define PCIE_BASE(if)							\
1229c28d61cSAnton Schubert 	((if) < 8 ?							\
1239c28d61cSAnton Schubert 	 (MVEBU_REG_PCIE_BASE + ((if) / 4) * 0x40000 + ((if) % 4) * 0x4000) : \
1249c28d61cSAnton Schubert 	 (MVEBU_REG_PCIE_BASE + 0x2000 + ((if) % 8) * 0x40000))
1259c28d61cSAnton Schubert 
1269c28d61cSAnton Schubert /*
1279c28d61cSAnton Schubert  * On AXP MV78460 these PEX ports are supported:
1289c28d61cSAnton Schubert  *  0 - Port 0.0
1299c28d61cSAnton Schubert  *  1 - Port 0.1
1309c28d61cSAnton Schubert  *  2 - Port 0.2
1319c28d61cSAnton Schubert  *  3 - Port 0.3
1329c28d61cSAnton Schubert  *  4 - Port 1.0
1339c28d61cSAnton Schubert  *  5 - Port 1.1
1349c28d61cSAnton Schubert  *  6 - Port 1.2
1359c28d61cSAnton Schubert  *  7 - Port 1.3
1369c28d61cSAnton Schubert  *  8 - Port 2.0
1379c28d61cSAnton Schubert  *  9 - Port 3.0
1389c28d61cSAnton Schubert  */
1399c28d61cSAnton Schubert #define MAX_PEX 10
1409c28d61cSAnton Schubert static struct mvebu_pcie pcie_bus[MAX_PEX];
1419c28d61cSAnton Schubert 
mvebu_get_port_lane(struct mvebu_pcie * pcie,int pex_idx,int * mem_target,int * mem_attr)1429c28d61cSAnton Schubert static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
1439c28d61cSAnton Schubert 				int *mem_target, int *mem_attr)
1449c28d61cSAnton Schubert {
1459c28d61cSAnton Schubert 	u8 port[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 3 };
1469c28d61cSAnton Schubert 	u8 lane[] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 0 };
1479c28d61cSAnton Schubert 	u8 target[] = { 4, 4, 4, 4, 8, 8, 8, 8, 4, 8 };
1489c28d61cSAnton Schubert 	u8 attr[] = { 0xe8, 0xd8, 0xb8, 0x78,
1499c28d61cSAnton Schubert 		      0xe8, 0xd8, 0xb8, 0x78,
1509c28d61cSAnton Schubert 		      0xf8, 0xf8 };
1519c28d61cSAnton Schubert 
1529c28d61cSAnton Schubert 	pcie->port = port[pex_idx];
1539c28d61cSAnton Schubert 	pcie->lane = lane[pex_idx];
1549c28d61cSAnton Schubert 	*mem_target = target[pex_idx];
1559c28d61cSAnton Schubert 	*mem_attr = attr[pex_idx];
1569c28d61cSAnton Schubert }
1579c28d61cSAnton Schubert #endif
1589c28d61cSAnton Schubert 
mvebu_pex_unit_is_x4(int pex_idx)1599a045278SPhil Sutter static int mvebu_pex_unit_is_x4(int pex_idx)
1609a045278SPhil Sutter {
1619a045278SPhil Sutter 	int pex_unit = pex_idx < 9 ? pex_idx >> 2 : 3;
1629a045278SPhil Sutter 	u32 mask = (0x0f << (pex_unit * 8));
1639a045278SPhil Sutter 
1649a045278SPhil Sutter 	return (readl(COMPHY_REFCLK_ALIGNMENT) & mask) == mask;
1659a045278SPhil Sutter }
1669a045278SPhil Sutter 
mvebu_pcie_link_up(struct mvebu_pcie * pcie)1679c28d61cSAnton Schubert static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
1689c28d61cSAnton Schubert {
1699c28d61cSAnton Schubert 	u32 val;
1709c28d61cSAnton Schubert 	val = readl(pcie->base + PCIE_STAT_OFF);
1719c28d61cSAnton Schubert 	return !(val & PCIE_STAT_LINK_DOWN);
1729c28d61cSAnton Schubert }
1739c28d61cSAnton Schubert 
mvebu_pcie_set_local_bus_nr(struct mvebu_pcie * pcie,int busno)1749c28d61cSAnton Schubert static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
1759c28d61cSAnton Schubert {
1769c28d61cSAnton Schubert 	u32 stat;
1779c28d61cSAnton Schubert 
1789c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
1799c28d61cSAnton Schubert 	stat &= ~PCIE_STAT_BUS;
1809c28d61cSAnton Schubert 	stat |= busno << 8;
1819c28d61cSAnton Schubert 	writel(stat, pcie->base + PCIE_STAT_OFF);
1829c28d61cSAnton Schubert }
1839c28d61cSAnton Schubert 
mvebu_pcie_set_local_dev_nr(struct mvebu_pcie * pcie,int devno)1849c28d61cSAnton Schubert static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
1859c28d61cSAnton Schubert {
1869c28d61cSAnton Schubert 	u32 stat;
1879c28d61cSAnton Schubert 
1889c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
1899c28d61cSAnton Schubert 	stat &= ~PCIE_STAT_DEV;
1909c28d61cSAnton Schubert 	stat |= devno << 16;
1919c28d61cSAnton Schubert 	writel(stat, pcie->base + PCIE_STAT_OFF);
1929c28d61cSAnton Schubert }
1939c28d61cSAnton Schubert 
mvebu_pcie_get_local_bus_nr(struct mvebu_pcie * pcie)1949c28d61cSAnton Schubert static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
1959c28d61cSAnton Schubert {
1969c28d61cSAnton Schubert 	u32 stat;
1979c28d61cSAnton Schubert 
1989c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
1999c28d61cSAnton Schubert 	return (stat & PCIE_STAT_BUS) >> 8;
2009c28d61cSAnton Schubert }
2019c28d61cSAnton Schubert 
mvebu_pcie_get_local_dev_nr(struct mvebu_pcie * pcie)2029c28d61cSAnton Schubert static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
2039c28d61cSAnton Schubert {
2049c28d61cSAnton Schubert 	u32 stat;
2059c28d61cSAnton Schubert 
2069c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
2079c28d61cSAnton Schubert 	return (stat & PCIE_STAT_DEV) >> 16;
2089c28d61cSAnton Schubert }
2099c28d61cSAnton Schubert 
hose_to_pcie(struct pci_controller * hose)2109c28d61cSAnton Schubert static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
2119c28d61cSAnton Schubert {
2129c28d61cSAnton Schubert 	return container_of(hose, struct mvebu_pcie, hose);
2139c28d61cSAnton Schubert }
2149c28d61cSAnton Schubert 
mvebu_pcie_read_config_dword(struct pci_controller * hose,pci_dev_t dev,int offset,u32 * val)2159c28d61cSAnton Schubert static int mvebu_pcie_read_config_dword(struct pci_controller *hose,
2169c28d61cSAnton Schubert 		pci_dev_t dev, int offset, u32 *val)
2179c28d61cSAnton Schubert {
2189c28d61cSAnton Schubert 	struct mvebu_pcie *pcie = hose_to_pcie(hose);
2199c28d61cSAnton Schubert 	int local_bus = PCI_BUS(pcie->dev);
2209c28d61cSAnton Schubert 	int local_dev = PCI_DEV(pcie->dev);
2219c28d61cSAnton Schubert 	u32 reg;
2229c28d61cSAnton Schubert 
2239c28d61cSAnton Schubert 	/* Only allow one other device besides the local one on the local bus */
2249c28d61cSAnton Schubert 	if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
2259c28d61cSAnton Schubert 		if (local_dev == 0 && PCI_DEV(dev) != 1) {
2269c28d61cSAnton Schubert 			/*
2279c28d61cSAnton Schubert 			 * If local dev is 0, the first other dev can
2289c28d61cSAnton Schubert 			 * only be 1
2299c28d61cSAnton Schubert 			 */
2309c28d61cSAnton Schubert 			*val = 0xffffffff;
2319c28d61cSAnton Schubert 			return 1;
2329c28d61cSAnton Schubert 		} else if (local_dev != 0 && PCI_DEV(dev) != 0) {
2339c28d61cSAnton Schubert 			/*
2349c28d61cSAnton Schubert 			 * If local dev is not 0, the first other dev can
2359c28d61cSAnton Schubert 			 * only be 0
2369c28d61cSAnton Schubert 			 */
2379c28d61cSAnton Schubert 			*val = 0xffffffff;
2389c28d61cSAnton Schubert 			return 1;
2399c28d61cSAnton Schubert 		}
2409c28d61cSAnton Schubert 	}
2419c28d61cSAnton Schubert 
2429c28d61cSAnton Schubert 	/* write address */
2439c28d61cSAnton Schubert 	reg = PCIE_CONF_ADDR(dev, offset);
2449c28d61cSAnton Schubert 	writel(reg, pcie->base + PCIE_CONF_ADDR_OFF);
2459c28d61cSAnton Schubert 	*val = readl(pcie->base + PCIE_CONF_DATA_OFF);
2469c28d61cSAnton Schubert 
2479c28d61cSAnton Schubert 	return 0;
2489c28d61cSAnton Schubert }
2499c28d61cSAnton Schubert 
mvebu_pcie_write_config_dword(struct pci_controller * hose,pci_dev_t dev,int offset,u32 val)2509c28d61cSAnton Schubert static int mvebu_pcie_write_config_dword(struct pci_controller *hose,
2519c28d61cSAnton Schubert 		pci_dev_t dev, int offset, u32 val)
2529c28d61cSAnton Schubert {
2539c28d61cSAnton Schubert 	struct mvebu_pcie *pcie = hose_to_pcie(hose);
2549c28d61cSAnton Schubert 	int local_bus = PCI_BUS(pcie->dev);
2559c28d61cSAnton Schubert 	int local_dev = PCI_DEV(pcie->dev);
2569c28d61cSAnton Schubert 
2579c28d61cSAnton Schubert 	/* Only allow one other device besides the local one on the local bus */
2589c28d61cSAnton Schubert 	if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
2599c28d61cSAnton Schubert 		if (local_dev == 0 && PCI_DEV(dev) != 1) {
2609c28d61cSAnton Schubert 			/*
2619c28d61cSAnton Schubert 			 * If local dev is 0, the first other dev can
2629c28d61cSAnton Schubert 			 * only be 1
2639c28d61cSAnton Schubert 			 */
2649c28d61cSAnton Schubert 			return 1;
2659c28d61cSAnton Schubert 		} else if (local_dev != 0 && PCI_DEV(dev) != 0) {
2669c28d61cSAnton Schubert 			/*
2679c28d61cSAnton Schubert 			 * If local dev is not 0, the first other dev can
2689c28d61cSAnton Schubert 			 * only be 0
2699c28d61cSAnton Schubert 			 */
2709c28d61cSAnton Schubert 			return 1;
2719c28d61cSAnton Schubert 		}
2729c28d61cSAnton Schubert 	}
2739c28d61cSAnton Schubert 
2749c28d61cSAnton Schubert 	writel(PCIE_CONF_ADDR(dev, offset), pcie->base + PCIE_CONF_ADDR_OFF);
2759c28d61cSAnton Schubert 	writel(val, pcie->base + PCIE_CONF_DATA_OFF);
2769c28d61cSAnton Schubert 
2779c28d61cSAnton Schubert 	return 0;
2789c28d61cSAnton Schubert }
2799c28d61cSAnton Schubert 
2809c28d61cSAnton Schubert /*
2819c28d61cSAnton Schubert  * Setup PCIE BARs and Address Decode Wins:
2829c28d61cSAnton Schubert  * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
2839c28d61cSAnton Schubert  * WIN[0-3] -> DRAM bank[0-3]
2849c28d61cSAnton Schubert  */
mvebu_pcie_setup_wins(struct mvebu_pcie * pcie)2859c28d61cSAnton Schubert static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
2869c28d61cSAnton Schubert {
2879c28d61cSAnton Schubert 	const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
2889c28d61cSAnton Schubert 	u32 size;
2899c28d61cSAnton Schubert 	int i;
2909c28d61cSAnton Schubert 
2919c28d61cSAnton Schubert 	/* First, disable and clear BARs and windows. */
2929c28d61cSAnton Schubert 	for (i = 1; i < 3; i++) {
2939c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
2949c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
2959c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
2969c28d61cSAnton Schubert 	}
2979c28d61cSAnton Schubert 
2989c28d61cSAnton Schubert 	for (i = 0; i < 5; i++) {
2999c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
3009c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
3019c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
3029c28d61cSAnton Schubert 	}
3039c28d61cSAnton Schubert 
3049c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
3059c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
3069c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
3079c28d61cSAnton Schubert 
3089c28d61cSAnton Schubert 	/* Setup windows for DDR banks. Count total DDR size on the fly. */
3099c28d61cSAnton Schubert 	size = 0;
3109c28d61cSAnton Schubert 	for (i = 0; i < dram->num_cs; i++) {
3119c28d61cSAnton Schubert 		const struct mbus_dram_window *cs = dram->cs + i;
3129c28d61cSAnton Schubert 
3139c28d61cSAnton Schubert 		writel(cs->base & 0xffff0000,
3149c28d61cSAnton Schubert 		       pcie->base + PCIE_WIN04_BASE_OFF(i));
3159c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
3169c28d61cSAnton Schubert 		writel(((cs->size - 1) & 0xffff0000) |
3179c28d61cSAnton Schubert 		       (cs->mbus_attr << 8) |
3189c28d61cSAnton Schubert 		       (dram->mbus_dram_target_id << 4) | 1,
3199c28d61cSAnton Schubert 		       pcie->base + PCIE_WIN04_CTRL_OFF(i));
3209c28d61cSAnton Schubert 
3219c28d61cSAnton Schubert 		size += cs->size;
3229c28d61cSAnton Schubert 	}
3239c28d61cSAnton Schubert 
3249c28d61cSAnton Schubert 	/* Round up 'size' to the nearest power of two. */
3259c28d61cSAnton Schubert 	if ((size & (size - 1)) != 0)
3269c28d61cSAnton Schubert 		size = 1 << fls(size);
3279c28d61cSAnton Schubert 
3289c28d61cSAnton Schubert 	/* Setup BAR[1] to all DRAM banks. */
3299c28d61cSAnton Schubert 	writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
3309c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
3319c28d61cSAnton Schubert 	writel(((size - 1) & 0xffff0000) | 0x1,
3329c28d61cSAnton Schubert 	       pcie->base + PCIE_BAR_CTRL_OFF(1));
3339c28d61cSAnton Schubert }
3349c28d61cSAnton Schubert 
pci_init_board(void)3359c28d61cSAnton Schubert void pci_init_board(void)
3369c28d61cSAnton Schubert {
3379c28d61cSAnton Schubert 	int mem_target, mem_attr, i;
3389c28d61cSAnton Schubert 	int bus = 0;
3399c28d61cSAnton Schubert 	u32 reg;
3409c28d61cSAnton Schubert 	u32 soc_ctrl = readl(MVEBU_SYSTEM_REG_BASE + 0x4);
3419c28d61cSAnton Schubert 
3429c28d61cSAnton Schubert 	/* Check SoC Control Power State */
3439c28d61cSAnton Schubert 	debug("%s: SoC Control %08x, 0en %01lx, 1en %01lx, 2en %01lx\n",
3449c28d61cSAnton Schubert 	      __func__, soc_ctrl, SELECT(soc_ctrl, 0), SELECT(soc_ctrl, 1),
3459c28d61cSAnton Schubert 	      SELECT(soc_ctrl, 2));
3469c28d61cSAnton Schubert 
3479c28d61cSAnton Schubert 	for (i = 0; i < MAX_PEX; i++) {
3489c28d61cSAnton Schubert 		struct mvebu_pcie *pcie = &pcie_bus[i];
3499c28d61cSAnton Schubert 		struct pci_controller *hose = &pcie->hose;
3509c28d61cSAnton Schubert 
3519c28d61cSAnton Schubert 		/* Get port number, lane number and memory target / attr */
3529c28d61cSAnton Schubert 		mvebu_get_port_lane(pcie, i, &mem_target, &mem_attr);
3539c28d61cSAnton Schubert 
3549c28d61cSAnton Schubert 		/* Don't read at all from pci registers if port power is down */
355*882d3fa6SDirk Eibach 		if (SELECT(soc_ctrl, pcie->port) == 0) {
356*882d3fa6SDirk Eibach 			if (pcie->lane == 0)
3579c28d61cSAnton Schubert 				debug("%s: skipping port %d\n", __func__, pcie->port);
3589c28d61cSAnton Schubert 			continue;
3599c28d61cSAnton Schubert 		}
3609c28d61cSAnton Schubert 
3619c28d61cSAnton Schubert 		pcie->base = (void __iomem *)PCIE_BASE(i);
3629c28d61cSAnton Schubert 
3639c28d61cSAnton Schubert 		/* Check link and skip ports that have no link */
3649c28d61cSAnton Schubert 		if (!mvebu_pcie_link_up(pcie)) {
3659c28d61cSAnton Schubert 			debug("%s: PCIe %d.%d - down\n", __func__,
3669c28d61cSAnton Schubert 			      pcie->port, pcie->lane);
3679c28d61cSAnton Schubert 			continue;
3689c28d61cSAnton Schubert 		}
3699c28d61cSAnton Schubert 		debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
3709c28d61cSAnton Schubert 		      pcie->port, pcie->lane, (u32)pcie->base);
3719c28d61cSAnton Schubert 
3729c28d61cSAnton Schubert 		/* Read Id info and local bus/dev */
3739c28d61cSAnton Schubert 		debug("direct conf read %08x, local bus %d, local dev %d\n",
3749c28d61cSAnton Schubert 		      readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
3759c28d61cSAnton Schubert 		      mvebu_pcie_get_local_dev_nr(pcie));
3769c28d61cSAnton Schubert 
3779c28d61cSAnton Schubert 		mvebu_pcie_set_local_bus_nr(pcie, bus);
3789c28d61cSAnton Schubert 		mvebu_pcie_set_local_dev_nr(pcie, 0);
3799c28d61cSAnton Schubert 		pcie->dev = PCI_BDF(bus, 0, 0);
3809c28d61cSAnton Schubert 
3819c28d61cSAnton Schubert 		pcie->mem.start = (u32)mvebu_pcie_membase;
3829c28d61cSAnton Schubert 		pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
3839c28d61cSAnton Schubert 		mvebu_pcie_membase += PCIE_MEM_SIZE;
3849c28d61cSAnton Schubert 
3859c28d61cSAnton Schubert 		if (mvebu_mbus_add_window_by_id(mem_target, mem_attr,
3869c28d61cSAnton Schubert 						(phys_addr_t)pcie->mem.start,
3879c28d61cSAnton Schubert 						PCIE_MEM_SIZE)) {
3889c28d61cSAnton Schubert 			printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
3899c28d61cSAnton Schubert 			       (u32)pcie->mem.start, PCIE_MEM_SIZE);
3909c28d61cSAnton Schubert 		}
3919c28d61cSAnton Schubert 
3929c28d61cSAnton Schubert 		/* Setup windows and configure host bridge */
3939c28d61cSAnton Schubert 		mvebu_pcie_setup_wins(pcie);
3949c28d61cSAnton Schubert 
3959c28d61cSAnton Schubert 		/* Master + slave enable. */
3969c28d61cSAnton Schubert 		reg = readl(pcie->base + PCIE_CMD_OFF);
3979c28d61cSAnton Schubert 		reg |= PCI_COMMAND_MEMORY;
3989c28d61cSAnton Schubert 		reg |= PCI_COMMAND_MASTER;
3999c28d61cSAnton Schubert 		reg |= BIT(10);		/* disable interrupts */
4009c28d61cSAnton Schubert 		writel(reg, pcie->base + PCIE_CMD_OFF);
4019c28d61cSAnton Schubert 
4029c28d61cSAnton Schubert 		/* Setup U-Boot PCI Controller */
4039c28d61cSAnton Schubert 		hose->first_busno = 0;
4049c28d61cSAnton Schubert 		hose->current_busno = bus;
4059c28d61cSAnton Schubert 
4069c28d61cSAnton Schubert 		/* PCI memory space */
4079c28d61cSAnton Schubert 		pci_set_region(hose->regions + 0, pcie->mem.start,
4089c28d61cSAnton Schubert 			       pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
4099c28d61cSAnton Schubert 		pci_set_region(hose->regions + 1,
4109c28d61cSAnton Schubert 			       0, 0,
4119c28d61cSAnton Schubert 			       gd->ram_size,
4129c28d61cSAnton Schubert 			       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
4139c28d61cSAnton Schubert 		hose->region_count = 2;
4149c28d61cSAnton Schubert 
4159c28d61cSAnton Schubert 		pci_set_ops(hose,
4169c28d61cSAnton Schubert 			    pci_hose_read_config_byte_via_dword,
4179c28d61cSAnton Schubert 			    pci_hose_read_config_word_via_dword,
4189c28d61cSAnton Schubert 			    mvebu_pcie_read_config_dword,
4199c28d61cSAnton Schubert 			    pci_hose_write_config_byte_via_dword,
4209c28d61cSAnton Schubert 			    pci_hose_write_config_word_via_dword,
4219c28d61cSAnton Schubert 			    mvebu_pcie_write_config_dword);
4229c28d61cSAnton Schubert 		pci_register_hose(hose);
4239c28d61cSAnton Schubert 
4249c28d61cSAnton Schubert 		hose->last_busno = pci_hose_scan(hose);
4259c28d61cSAnton Schubert 
4269c28d61cSAnton Schubert 		/* Set BAR0 to internal registers */
4279c28d61cSAnton Schubert 		writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
4289c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
4299c28d61cSAnton Schubert 
4309c28d61cSAnton Schubert 		bus = hose->last_busno + 1;
4319a045278SPhil Sutter 
4329a045278SPhil Sutter 		/* need to skip more for X4 links, otherwise scan will hang */
4339a045278SPhil Sutter 		if (mvebu_soc_family() == MVEBU_SOC_AXP) {
4349a045278SPhil Sutter 			if (mvebu_pex_unit_is_x4(i))
4359a045278SPhil Sutter 				i += 3;
4369a045278SPhil Sutter 		}
4379c28d61cSAnton Schubert 	}
4389c28d61cSAnton Schubert }
439