xref: /rk3399_rockchip-uboot/drivers/pci/pci_mvebu.c (revision 9c28d61c8e65f2b1cf2db1ba262fe37e973beaa7)
1*9c28d61cSAnton Schubert /*
2*9c28d61cSAnton Schubert  * PCIe driver for Marvell MVEBU SoCs
3*9c28d61cSAnton Schubert  *
4*9c28d61cSAnton Schubert  * Based on Barebox drivers/pci/pci-mvebu.c
5*9c28d61cSAnton Schubert  *
6*9c28d61cSAnton Schubert  * Ported to U-Boot by:
7*9c28d61cSAnton Schubert  * Anton Schubert <anton.schubert@gmx.de>
8*9c28d61cSAnton Schubert  * Stefan Roese <sr@denx.de>
9*9c28d61cSAnton Schubert  *
10*9c28d61cSAnton Schubert  * SPDX-License-Identifier:	GPL-2.0
11*9c28d61cSAnton Schubert  */
12*9c28d61cSAnton Schubert 
13*9c28d61cSAnton Schubert #include <common.h>
14*9c28d61cSAnton Schubert #include <pci.h>
15*9c28d61cSAnton Schubert #include <asm/errno.h>
16*9c28d61cSAnton Schubert #include <asm/io.h>
17*9c28d61cSAnton Schubert #include <asm/arch/cpu.h>
18*9c28d61cSAnton Schubert #include <asm/arch/soc.h>
19*9c28d61cSAnton Schubert #include <linux/mbus.h>
20*9c28d61cSAnton Schubert 
21*9c28d61cSAnton Schubert DECLARE_GLOBAL_DATA_PTR;
22*9c28d61cSAnton Schubert 
23*9c28d61cSAnton Schubert /* PCIe unit register offsets */
24*9c28d61cSAnton Schubert #define SELECT(x, n)			((x >> n) & 1UL)
25*9c28d61cSAnton Schubert 
26*9c28d61cSAnton Schubert #define PCIE_DEV_ID_OFF			0x0000
27*9c28d61cSAnton Schubert #define PCIE_CMD_OFF			0x0004
28*9c28d61cSAnton Schubert #define PCIE_DEV_REV_OFF		0x0008
29*9c28d61cSAnton Schubert #define  PCIE_BAR_LO_OFF(n)		(0x0010 + ((n) << 3))
30*9c28d61cSAnton Schubert #define  PCIE_BAR_HI_OFF(n)		(0x0014 + ((n) << 3))
31*9c28d61cSAnton Schubert #define PCIE_CAPAB_OFF			0x0060
32*9c28d61cSAnton Schubert #define PCIE_CTRL_STAT_OFF		0x0068
33*9c28d61cSAnton Schubert #define PCIE_HEADER_LOG_4_OFF		0x0128
34*9c28d61cSAnton Schubert #define  PCIE_BAR_CTRL_OFF(n)		(0x1804 + (((n) - 1) * 4))
35*9c28d61cSAnton Schubert #define  PCIE_WIN04_CTRL_OFF(n)		(0x1820 + ((n) << 4))
36*9c28d61cSAnton Schubert #define  PCIE_WIN04_BASE_OFF(n)		(0x1824 + ((n) << 4))
37*9c28d61cSAnton Schubert #define  PCIE_WIN04_REMAP_OFF(n)	(0x182c + ((n) << 4))
38*9c28d61cSAnton Schubert #define PCIE_WIN5_CTRL_OFF		0x1880
39*9c28d61cSAnton Schubert #define PCIE_WIN5_BASE_OFF		0x1884
40*9c28d61cSAnton Schubert #define PCIE_WIN5_REMAP_OFF		0x188c
41*9c28d61cSAnton Schubert #define PCIE_CONF_ADDR_OFF		0x18f8
42*9c28d61cSAnton Schubert #define  PCIE_CONF_ADDR_EN		BIT(31)
43*9c28d61cSAnton Schubert #define  PCIE_CONF_REG(r)		((((r) & 0xf00) << 16) | ((r) & 0xfc))
44*9c28d61cSAnton Schubert #define  PCIE_CONF_BUS(b)		(((b) & 0xff) << 16)
45*9c28d61cSAnton Schubert #define  PCIE_CONF_DEV(d)		(((d) & 0x1f) << 11)
46*9c28d61cSAnton Schubert #define  PCIE_CONF_FUNC(f)		(((f) & 0x7) << 8)
47*9c28d61cSAnton Schubert #define  PCIE_CONF_ADDR(dev, reg) \
48*9c28d61cSAnton Schubert 	(PCIE_CONF_BUS(PCI_BUS(dev)) | PCIE_CONF_DEV(PCI_DEV(dev))    | \
49*9c28d61cSAnton Schubert 	 PCIE_CONF_FUNC(PCI_FUNC(dev)) | PCIE_CONF_REG(reg) | \
50*9c28d61cSAnton Schubert 	 PCIE_CONF_ADDR_EN)
51*9c28d61cSAnton Schubert #define PCIE_CONF_DATA_OFF		0x18fc
52*9c28d61cSAnton Schubert #define PCIE_MASK_OFF			0x1910
53*9c28d61cSAnton Schubert #define  PCIE_MASK_ENABLE_INTS          (0xf << 24)
54*9c28d61cSAnton Schubert #define PCIE_CTRL_OFF			0x1a00
55*9c28d61cSAnton Schubert #define  PCIE_CTRL_X1_MODE		BIT(0)
56*9c28d61cSAnton Schubert #define PCIE_STAT_OFF			0x1a04
57*9c28d61cSAnton Schubert #define  PCIE_STAT_BUS                  (0xff << 8)
58*9c28d61cSAnton Schubert #define  PCIE_STAT_DEV                  (0x1f << 16)
59*9c28d61cSAnton Schubert #define  PCIE_STAT_LINK_DOWN		BIT(0)
60*9c28d61cSAnton Schubert #define PCIE_DEBUG_CTRL			0x1a60
61*9c28d61cSAnton Schubert #define  PCIE_DEBUG_SOFT_RESET		BIT(20)
62*9c28d61cSAnton Schubert 
63*9c28d61cSAnton Schubert struct resource {
64*9c28d61cSAnton Schubert 	u32 start;
65*9c28d61cSAnton Schubert 	u32 end;
66*9c28d61cSAnton Schubert };
67*9c28d61cSAnton Schubert 
68*9c28d61cSAnton Schubert struct mvebu_pcie {
69*9c28d61cSAnton Schubert 	struct pci_controller hose;
70*9c28d61cSAnton Schubert 	char *name;
71*9c28d61cSAnton Schubert 	void __iomem *base;
72*9c28d61cSAnton Schubert 	void __iomem *membase;
73*9c28d61cSAnton Schubert 	struct resource mem;
74*9c28d61cSAnton Schubert 	void __iomem *iobase;
75*9c28d61cSAnton Schubert 	u32 port;
76*9c28d61cSAnton Schubert 	u32 lane;
77*9c28d61cSAnton Schubert 	u32 lane_mask;
78*9c28d61cSAnton Schubert 	pci_dev_t dev;
79*9c28d61cSAnton Schubert };
80*9c28d61cSAnton Schubert 
81*9c28d61cSAnton Schubert #define to_pcie(_hc)	container_of(_hc, struct mvebu_pcie, pci)
82*9c28d61cSAnton Schubert 
83*9c28d61cSAnton Schubert /*
84*9c28d61cSAnton Schubert  * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
85*9c28d61cSAnton Schubert  * into SoCs address space. Each controller will map 32M of MEM
86*9c28d61cSAnton Schubert  * and 64K of I/O space when registered.
87*9c28d61cSAnton Schubert  */
88*9c28d61cSAnton Schubert static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
89*9c28d61cSAnton Schubert #define PCIE_MEM_SIZE	(32 << 20)
90*9c28d61cSAnton Schubert 
91*9c28d61cSAnton Schubert #if defined(CONFIG_ARMADA_38X)
92*9c28d61cSAnton Schubert #define PCIE_BASE(if)					\
93*9c28d61cSAnton Schubert 	((if) == 0 ?					\
94*9c28d61cSAnton Schubert 	 MVEBU_REG_PCIE_BASE + 0x40000 :		\
95*9c28d61cSAnton Schubert 	 MVEBU_REG_PCIE_BASE + 0x4000 * (if))
96*9c28d61cSAnton Schubert 
97*9c28d61cSAnton Schubert /*
98*9c28d61cSAnton Schubert  * On A38x MV6820 these PEX ports are supported:
99*9c28d61cSAnton Schubert  *  0 - Port 0.0
100*9c28d61cSAnton Schubert  *  1 - Port 0.1
101*9c28d61cSAnton Schubert  *  2 - Port 0.2
102*9c28d61cSAnton Schubert  */
103*9c28d61cSAnton Schubert #define MAX_PEX 3
104*9c28d61cSAnton Schubert static struct mvebu_pcie pcie_bus[MAX_PEX];
105*9c28d61cSAnton Schubert 
106*9c28d61cSAnton Schubert static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
107*9c28d61cSAnton Schubert 				int *mem_target, int *mem_attr)
108*9c28d61cSAnton Schubert {
109*9c28d61cSAnton Schubert 	u8 port[] = { 0, 1, 2 };
110*9c28d61cSAnton Schubert 	u8 lane[] = { 0, 0, 0 };
111*9c28d61cSAnton Schubert 	u8 target[] = { 8, 4, 4 };
112*9c28d61cSAnton Schubert 	u8 attr[] = { 0xe8, 0xe8, 0xd8 };
113*9c28d61cSAnton Schubert 
114*9c28d61cSAnton Schubert 	pcie->port = port[pex_idx];
115*9c28d61cSAnton Schubert 	pcie->lane = lane[pex_idx];
116*9c28d61cSAnton Schubert 	*mem_target = target[pex_idx];
117*9c28d61cSAnton Schubert 	*mem_attr = attr[pex_idx];
118*9c28d61cSAnton Schubert }
119*9c28d61cSAnton Schubert #else
120*9c28d61cSAnton Schubert #define PCIE_BASE(if)							\
121*9c28d61cSAnton Schubert 	((if) < 8 ?							\
122*9c28d61cSAnton Schubert 	 (MVEBU_REG_PCIE_BASE + ((if) / 4) * 0x40000 + ((if) % 4) * 0x4000) : \
123*9c28d61cSAnton Schubert 	 (MVEBU_REG_PCIE_BASE + 0x2000 + ((if) % 8) * 0x40000))
124*9c28d61cSAnton Schubert 
125*9c28d61cSAnton Schubert /*
126*9c28d61cSAnton Schubert  * On AXP MV78460 these PEX ports are supported:
127*9c28d61cSAnton Schubert  *  0 - Port 0.0
128*9c28d61cSAnton Schubert  *  1 - Port 0.1
129*9c28d61cSAnton Schubert  *  2 - Port 0.2
130*9c28d61cSAnton Schubert  *  3 - Port 0.3
131*9c28d61cSAnton Schubert  *  4 - Port 1.0
132*9c28d61cSAnton Schubert  *  5 - Port 1.1
133*9c28d61cSAnton Schubert  *  6 - Port 1.2
134*9c28d61cSAnton Schubert  *  7 - Port 1.3
135*9c28d61cSAnton Schubert  *  8 - Port 2.0
136*9c28d61cSAnton Schubert  *  9 - Port 3.0
137*9c28d61cSAnton Schubert  */
138*9c28d61cSAnton Schubert #define MAX_PEX 10
139*9c28d61cSAnton Schubert static struct mvebu_pcie pcie_bus[MAX_PEX];
140*9c28d61cSAnton Schubert 
141*9c28d61cSAnton Schubert static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
142*9c28d61cSAnton Schubert 				int *mem_target, int *mem_attr)
143*9c28d61cSAnton Schubert {
144*9c28d61cSAnton Schubert 	u8 port[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 3 };
145*9c28d61cSAnton Schubert 	u8 lane[] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 0 };
146*9c28d61cSAnton Schubert 	u8 target[] = { 4, 4, 4, 4, 8, 8, 8, 8, 4, 8 };
147*9c28d61cSAnton Schubert 	u8 attr[] = { 0xe8, 0xd8, 0xb8, 0x78,
148*9c28d61cSAnton Schubert 		      0xe8, 0xd8, 0xb8, 0x78,
149*9c28d61cSAnton Schubert 		      0xf8, 0xf8 };
150*9c28d61cSAnton Schubert 
151*9c28d61cSAnton Schubert 	pcie->port = port[pex_idx];
152*9c28d61cSAnton Schubert 	pcie->lane = lane[pex_idx];
153*9c28d61cSAnton Schubert 	*mem_target = target[pex_idx];
154*9c28d61cSAnton Schubert 	*mem_attr = attr[pex_idx];
155*9c28d61cSAnton Schubert }
156*9c28d61cSAnton Schubert #endif
157*9c28d61cSAnton Schubert 
158*9c28d61cSAnton Schubert static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
159*9c28d61cSAnton Schubert {
160*9c28d61cSAnton Schubert 	u32 val;
161*9c28d61cSAnton Schubert 	val = readl(pcie->base + PCIE_STAT_OFF);
162*9c28d61cSAnton Schubert 	return !(val & PCIE_STAT_LINK_DOWN);
163*9c28d61cSAnton Schubert }
164*9c28d61cSAnton Schubert 
165*9c28d61cSAnton Schubert static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
166*9c28d61cSAnton Schubert {
167*9c28d61cSAnton Schubert 	u32 stat;
168*9c28d61cSAnton Schubert 
169*9c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
170*9c28d61cSAnton Schubert 	stat &= ~PCIE_STAT_BUS;
171*9c28d61cSAnton Schubert 	stat |= busno << 8;
172*9c28d61cSAnton Schubert 	writel(stat, pcie->base + PCIE_STAT_OFF);
173*9c28d61cSAnton Schubert }
174*9c28d61cSAnton Schubert 
175*9c28d61cSAnton Schubert static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
176*9c28d61cSAnton Schubert {
177*9c28d61cSAnton Schubert 	u32 stat;
178*9c28d61cSAnton Schubert 
179*9c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
180*9c28d61cSAnton Schubert 	stat &= ~PCIE_STAT_DEV;
181*9c28d61cSAnton Schubert 	stat |= devno << 16;
182*9c28d61cSAnton Schubert 	writel(stat, pcie->base + PCIE_STAT_OFF);
183*9c28d61cSAnton Schubert }
184*9c28d61cSAnton Schubert 
185*9c28d61cSAnton Schubert static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
186*9c28d61cSAnton Schubert {
187*9c28d61cSAnton Schubert 	u32 stat;
188*9c28d61cSAnton Schubert 
189*9c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
190*9c28d61cSAnton Schubert 	return (stat & PCIE_STAT_BUS) >> 8;
191*9c28d61cSAnton Schubert }
192*9c28d61cSAnton Schubert 
193*9c28d61cSAnton Schubert static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
194*9c28d61cSAnton Schubert {
195*9c28d61cSAnton Schubert 	u32 stat;
196*9c28d61cSAnton Schubert 
197*9c28d61cSAnton Schubert 	stat = readl(pcie->base + PCIE_STAT_OFF);
198*9c28d61cSAnton Schubert 	return (stat & PCIE_STAT_DEV) >> 16;
199*9c28d61cSAnton Schubert }
200*9c28d61cSAnton Schubert 
201*9c28d61cSAnton Schubert static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
202*9c28d61cSAnton Schubert {
203*9c28d61cSAnton Schubert 	return container_of(hose, struct mvebu_pcie, hose);
204*9c28d61cSAnton Schubert }
205*9c28d61cSAnton Schubert 
206*9c28d61cSAnton Schubert static int mvebu_pcie_read_config_dword(struct pci_controller *hose,
207*9c28d61cSAnton Schubert 		pci_dev_t dev, int offset, u32 *val)
208*9c28d61cSAnton Schubert {
209*9c28d61cSAnton Schubert 	struct mvebu_pcie *pcie = hose_to_pcie(hose);
210*9c28d61cSAnton Schubert 	int local_bus = PCI_BUS(pcie->dev);
211*9c28d61cSAnton Schubert 	int local_dev = PCI_DEV(pcie->dev);
212*9c28d61cSAnton Schubert 	u32 reg;
213*9c28d61cSAnton Schubert 
214*9c28d61cSAnton Schubert 	/* Only allow one other device besides the local one on the local bus */
215*9c28d61cSAnton Schubert 	if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
216*9c28d61cSAnton Schubert 		if (local_dev == 0 && PCI_DEV(dev) != 1) {
217*9c28d61cSAnton Schubert 			/*
218*9c28d61cSAnton Schubert 			 * If local dev is 0, the first other dev can
219*9c28d61cSAnton Schubert 			 * only be 1
220*9c28d61cSAnton Schubert 			 */
221*9c28d61cSAnton Schubert 			*val = 0xffffffff;
222*9c28d61cSAnton Schubert 			return 1;
223*9c28d61cSAnton Schubert 		} else if (local_dev != 0 && PCI_DEV(dev) != 0) {
224*9c28d61cSAnton Schubert 			/*
225*9c28d61cSAnton Schubert 			 * If local dev is not 0, the first other dev can
226*9c28d61cSAnton Schubert 			 * only be 0
227*9c28d61cSAnton Schubert 			 */
228*9c28d61cSAnton Schubert 			*val = 0xffffffff;
229*9c28d61cSAnton Schubert 			return 1;
230*9c28d61cSAnton Schubert 		}
231*9c28d61cSAnton Schubert 	}
232*9c28d61cSAnton Schubert 
233*9c28d61cSAnton Schubert 	/* write address */
234*9c28d61cSAnton Schubert 	reg = PCIE_CONF_ADDR(dev, offset);
235*9c28d61cSAnton Schubert 	writel(reg, pcie->base + PCIE_CONF_ADDR_OFF);
236*9c28d61cSAnton Schubert 	*val = readl(pcie->base + PCIE_CONF_DATA_OFF);
237*9c28d61cSAnton Schubert 
238*9c28d61cSAnton Schubert 	return 0;
239*9c28d61cSAnton Schubert }
240*9c28d61cSAnton Schubert 
241*9c28d61cSAnton Schubert static int mvebu_pcie_write_config_dword(struct pci_controller *hose,
242*9c28d61cSAnton Schubert 		pci_dev_t dev, int offset, u32 val)
243*9c28d61cSAnton Schubert {
244*9c28d61cSAnton Schubert 	struct mvebu_pcie *pcie = hose_to_pcie(hose);
245*9c28d61cSAnton Schubert 	int local_bus = PCI_BUS(pcie->dev);
246*9c28d61cSAnton Schubert 	int local_dev = PCI_DEV(pcie->dev);
247*9c28d61cSAnton Schubert 
248*9c28d61cSAnton Schubert 	/* Only allow one other device besides the local one on the local bus */
249*9c28d61cSAnton Schubert 	if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
250*9c28d61cSAnton Schubert 		if (local_dev == 0 && PCI_DEV(dev) != 1) {
251*9c28d61cSAnton Schubert 			/*
252*9c28d61cSAnton Schubert 			 * If local dev is 0, the first other dev can
253*9c28d61cSAnton Schubert 			 * only be 1
254*9c28d61cSAnton Schubert 			 */
255*9c28d61cSAnton Schubert 			return 1;
256*9c28d61cSAnton Schubert 		} else if (local_dev != 0 && PCI_DEV(dev) != 0) {
257*9c28d61cSAnton Schubert 			/*
258*9c28d61cSAnton Schubert 			 * If local dev is not 0, the first other dev can
259*9c28d61cSAnton Schubert 			 * only be 0
260*9c28d61cSAnton Schubert 			 */
261*9c28d61cSAnton Schubert 			return 1;
262*9c28d61cSAnton Schubert 		}
263*9c28d61cSAnton Schubert 	}
264*9c28d61cSAnton Schubert 
265*9c28d61cSAnton Schubert 	writel(PCIE_CONF_ADDR(dev, offset), pcie->base + PCIE_CONF_ADDR_OFF);
266*9c28d61cSAnton Schubert 	writel(val, pcie->base + PCIE_CONF_DATA_OFF);
267*9c28d61cSAnton Schubert 
268*9c28d61cSAnton Schubert 	return 0;
269*9c28d61cSAnton Schubert }
270*9c28d61cSAnton Schubert 
271*9c28d61cSAnton Schubert /*
272*9c28d61cSAnton Schubert  * Setup PCIE BARs and Address Decode Wins:
273*9c28d61cSAnton Schubert  * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
274*9c28d61cSAnton Schubert  * WIN[0-3] -> DRAM bank[0-3]
275*9c28d61cSAnton Schubert  */
276*9c28d61cSAnton Schubert static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
277*9c28d61cSAnton Schubert {
278*9c28d61cSAnton Schubert 	const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
279*9c28d61cSAnton Schubert 	u32 size;
280*9c28d61cSAnton Schubert 	int i;
281*9c28d61cSAnton Schubert 
282*9c28d61cSAnton Schubert 	/* First, disable and clear BARs and windows. */
283*9c28d61cSAnton Schubert 	for (i = 1; i < 3; i++) {
284*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
285*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
286*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
287*9c28d61cSAnton Schubert 	}
288*9c28d61cSAnton Schubert 
289*9c28d61cSAnton Schubert 	for (i = 0; i < 5; i++) {
290*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
291*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
292*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
293*9c28d61cSAnton Schubert 	}
294*9c28d61cSAnton Schubert 
295*9c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
296*9c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
297*9c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
298*9c28d61cSAnton Schubert 
299*9c28d61cSAnton Schubert 	/* Setup windows for DDR banks. Count total DDR size on the fly. */
300*9c28d61cSAnton Schubert 	size = 0;
301*9c28d61cSAnton Schubert 	for (i = 0; i < dram->num_cs; i++) {
302*9c28d61cSAnton Schubert 		const struct mbus_dram_window *cs = dram->cs + i;
303*9c28d61cSAnton Schubert 
304*9c28d61cSAnton Schubert 		writel(cs->base & 0xffff0000,
305*9c28d61cSAnton Schubert 		       pcie->base + PCIE_WIN04_BASE_OFF(i));
306*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
307*9c28d61cSAnton Schubert 		writel(((cs->size - 1) & 0xffff0000) |
308*9c28d61cSAnton Schubert 		       (cs->mbus_attr << 8) |
309*9c28d61cSAnton Schubert 		       (dram->mbus_dram_target_id << 4) | 1,
310*9c28d61cSAnton Schubert 		       pcie->base + PCIE_WIN04_CTRL_OFF(i));
311*9c28d61cSAnton Schubert 
312*9c28d61cSAnton Schubert 		size += cs->size;
313*9c28d61cSAnton Schubert 	}
314*9c28d61cSAnton Schubert 
315*9c28d61cSAnton Schubert 	/* Round up 'size' to the nearest power of two. */
316*9c28d61cSAnton Schubert 	if ((size & (size - 1)) != 0)
317*9c28d61cSAnton Schubert 		size = 1 << fls(size);
318*9c28d61cSAnton Schubert 
319*9c28d61cSAnton Schubert 	/* Setup BAR[1] to all DRAM banks. */
320*9c28d61cSAnton Schubert 	writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
321*9c28d61cSAnton Schubert 	writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
322*9c28d61cSAnton Schubert 	writel(((size - 1) & 0xffff0000) | 0x1,
323*9c28d61cSAnton Schubert 	       pcie->base + PCIE_BAR_CTRL_OFF(1));
324*9c28d61cSAnton Schubert }
325*9c28d61cSAnton Schubert 
326*9c28d61cSAnton Schubert void pci_init_board(void)
327*9c28d61cSAnton Schubert {
328*9c28d61cSAnton Schubert 	int mem_target, mem_attr, i;
329*9c28d61cSAnton Schubert 	int bus = 0;
330*9c28d61cSAnton Schubert 	u32 reg;
331*9c28d61cSAnton Schubert 	u32 soc_ctrl = readl(MVEBU_SYSTEM_REG_BASE + 0x4);
332*9c28d61cSAnton Schubert 
333*9c28d61cSAnton Schubert 	/* Check SoC Control Power State */
334*9c28d61cSAnton Schubert 	debug("%s: SoC Control %08x, 0en %01lx, 1en %01lx, 2en %01lx\n",
335*9c28d61cSAnton Schubert 	      __func__, soc_ctrl, SELECT(soc_ctrl, 0), SELECT(soc_ctrl, 1),
336*9c28d61cSAnton Schubert 	      SELECT(soc_ctrl, 2));
337*9c28d61cSAnton Schubert 
338*9c28d61cSAnton Schubert 	for (i = 0; i < MAX_PEX; i++) {
339*9c28d61cSAnton Schubert 		struct mvebu_pcie *pcie = &pcie_bus[i];
340*9c28d61cSAnton Schubert 		struct pci_controller *hose = &pcie->hose;
341*9c28d61cSAnton Schubert 
342*9c28d61cSAnton Schubert 		/* Get port number, lane number and memory target / attr */
343*9c28d61cSAnton Schubert 		mvebu_get_port_lane(pcie, i, &mem_target, &mem_attr);
344*9c28d61cSAnton Schubert 
345*9c28d61cSAnton Schubert 		/* Don't read at all from pci registers if port power is down */
346*9c28d61cSAnton Schubert 		if (pcie->lane == 0 && SELECT(soc_ctrl, pcie->port) == 0) {
347*9c28d61cSAnton Schubert 			i += 3;
348*9c28d61cSAnton Schubert 			debug("%s: skipping port %d\n", __func__, pcie->port);
349*9c28d61cSAnton Schubert 			continue;
350*9c28d61cSAnton Schubert 		}
351*9c28d61cSAnton Schubert 
352*9c28d61cSAnton Schubert 		pcie->base = (void __iomem *)PCIE_BASE(i);
353*9c28d61cSAnton Schubert 
354*9c28d61cSAnton Schubert 		/* Check link and skip ports that have no link */
355*9c28d61cSAnton Schubert 		if (!mvebu_pcie_link_up(pcie)) {
356*9c28d61cSAnton Schubert 			debug("%s: PCIe %d.%d - down\n", __func__,
357*9c28d61cSAnton Schubert 			      pcie->port, pcie->lane);
358*9c28d61cSAnton Schubert 			continue;
359*9c28d61cSAnton Schubert 		}
360*9c28d61cSAnton Schubert 		debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
361*9c28d61cSAnton Schubert 		      pcie->port, pcie->lane, (u32)pcie->base);
362*9c28d61cSAnton Schubert 
363*9c28d61cSAnton Schubert 		/* Read Id info and local bus/dev */
364*9c28d61cSAnton Schubert 		debug("direct conf read %08x, local bus %d, local dev %d\n",
365*9c28d61cSAnton Schubert 		      readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
366*9c28d61cSAnton Schubert 		      mvebu_pcie_get_local_dev_nr(pcie));
367*9c28d61cSAnton Schubert 
368*9c28d61cSAnton Schubert 		mvebu_pcie_set_local_bus_nr(pcie, bus);
369*9c28d61cSAnton Schubert 		mvebu_pcie_set_local_dev_nr(pcie, 0);
370*9c28d61cSAnton Schubert 		pcie->dev = PCI_BDF(bus, 0, 0);
371*9c28d61cSAnton Schubert 
372*9c28d61cSAnton Schubert 		pcie->mem.start = (u32)mvebu_pcie_membase;
373*9c28d61cSAnton Schubert 		pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
374*9c28d61cSAnton Schubert 		mvebu_pcie_membase += PCIE_MEM_SIZE;
375*9c28d61cSAnton Schubert 
376*9c28d61cSAnton Schubert 		if (mvebu_mbus_add_window_by_id(mem_target, mem_attr,
377*9c28d61cSAnton Schubert 						(phys_addr_t)pcie->mem.start,
378*9c28d61cSAnton Schubert 						PCIE_MEM_SIZE)) {
379*9c28d61cSAnton Schubert 			printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
380*9c28d61cSAnton Schubert 			       (u32)pcie->mem.start, PCIE_MEM_SIZE);
381*9c28d61cSAnton Schubert 		}
382*9c28d61cSAnton Schubert 
383*9c28d61cSAnton Schubert 		/* Setup windows and configure host bridge */
384*9c28d61cSAnton Schubert 		mvebu_pcie_setup_wins(pcie);
385*9c28d61cSAnton Schubert 
386*9c28d61cSAnton Schubert 		/* Master + slave enable. */
387*9c28d61cSAnton Schubert 		reg = readl(pcie->base + PCIE_CMD_OFF);
388*9c28d61cSAnton Schubert 		reg |= PCI_COMMAND_MEMORY;
389*9c28d61cSAnton Schubert 		reg |= PCI_COMMAND_MASTER;
390*9c28d61cSAnton Schubert 		reg |= BIT(10);		/* disable interrupts */
391*9c28d61cSAnton Schubert 		writel(reg, pcie->base + PCIE_CMD_OFF);
392*9c28d61cSAnton Schubert 
393*9c28d61cSAnton Schubert 		/* Setup U-Boot PCI Controller */
394*9c28d61cSAnton Schubert 		hose->first_busno = 0;
395*9c28d61cSAnton Schubert 		hose->current_busno = bus;
396*9c28d61cSAnton Schubert 
397*9c28d61cSAnton Schubert 		/* PCI memory space */
398*9c28d61cSAnton Schubert 		pci_set_region(hose->regions + 0, pcie->mem.start,
399*9c28d61cSAnton Schubert 			       pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
400*9c28d61cSAnton Schubert 		pci_set_region(hose->regions + 1,
401*9c28d61cSAnton Schubert 			       0, 0,
402*9c28d61cSAnton Schubert 			       gd->ram_size,
403*9c28d61cSAnton Schubert 			       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
404*9c28d61cSAnton Schubert 		hose->region_count = 2;
405*9c28d61cSAnton Schubert 
406*9c28d61cSAnton Schubert 		pci_set_ops(hose,
407*9c28d61cSAnton Schubert 			    pci_hose_read_config_byte_via_dword,
408*9c28d61cSAnton Schubert 			    pci_hose_read_config_word_via_dword,
409*9c28d61cSAnton Schubert 			    mvebu_pcie_read_config_dword,
410*9c28d61cSAnton Schubert 			    pci_hose_write_config_byte_via_dword,
411*9c28d61cSAnton Schubert 			    pci_hose_write_config_word_via_dword,
412*9c28d61cSAnton Schubert 			    mvebu_pcie_write_config_dword);
413*9c28d61cSAnton Schubert 		pci_register_hose(hose);
414*9c28d61cSAnton Schubert 
415*9c28d61cSAnton Schubert 		hose->last_busno = pci_hose_scan(hose);
416*9c28d61cSAnton Schubert 
417*9c28d61cSAnton Schubert 		/* Set BAR0 to internal registers */
418*9c28d61cSAnton Schubert 		writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
419*9c28d61cSAnton Schubert 		writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
420*9c28d61cSAnton Schubert 
421*9c28d61cSAnton Schubert 		bus = hose->last_busno + 1;
422*9c28d61cSAnton Schubert 	}
423*9c28d61cSAnton Schubert }
424