blob: a327a83411e9cbeccf081db9fdbb774eb2b9da29 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Anton Schubert9c28d612015-08-11 11:54:01 +02002/*
3 * PCIe driver for Marvell MVEBU SoCs
4 *
5 * Based on Barebox drivers/pci/pci-mvebu.c
6 *
7 * Ported to U-Boot by:
8 * Anton Schubert <anton.schubert@gmx.de>
9 * Stefan Roese <sr@denx.de>
Anton Schubert9c28d612015-08-11 11:54:01 +020010 */
11
12#include <common.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010013#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010017#include <dm/device-internal.h>
18#include <dm/lists.h>
19#include <dm/of_access.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020020#include <pci.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020021#include <asm/io.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/soc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010025#include <linux/errno.h>
26#include <linux/ioport.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020027#include <linux/mbus.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31/* PCIe unit register offsets */
32#define SELECT(x, n) ((x >> n) & 1UL)
33
34#define PCIE_DEV_ID_OFF 0x0000
35#define PCIE_CMD_OFF 0x0004
36#define PCIE_DEV_REV_OFF 0x0008
37#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
38#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
Pali Rohára7b61ab2021-10-22 16:22:10 +020039#define PCIE_EXP_ROM_BAR_OFF 0x0030
Anton Schubert9c28d612015-08-11 11:54:01 +020040#define PCIE_CAPAB_OFF 0x0060
41#define PCIE_CTRL_STAT_OFF 0x0068
42#define PCIE_HEADER_LOG_4_OFF 0x0128
43#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
44#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
45#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
46#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
47#define PCIE_WIN5_CTRL_OFF 0x1880
48#define PCIE_WIN5_BASE_OFF 0x1884
49#define PCIE_WIN5_REMAP_OFF 0x188c
50#define PCIE_CONF_ADDR_OFF 0x18f8
51#define PCIE_CONF_ADDR_EN BIT(31)
52#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
53#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
54#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
55#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
Pali Rohára7b61ab2021-10-22 16:22:10 +020056#define PCIE_CONF_ADDR(b, d, f, reg) \
57 (PCIE_CONF_BUS(b) | PCIE_CONF_DEV(d) | \
58 PCIE_CONF_FUNC(f) | PCIE_CONF_REG(reg) | \
Anton Schubert9c28d612015-08-11 11:54:01 +020059 PCIE_CONF_ADDR_EN)
60#define PCIE_CONF_DATA_OFF 0x18fc
61#define PCIE_MASK_OFF 0x1910
62#define PCIE_MASK_ENABLE_INTS (0xf << 24)
63#define PCIE_CTRL_OFF 0x1a00
64#define PCIE_CTRL_X1_MODE BIT(0)
Pali Rohár2344a762021-10-22 16:22:14 +020065#define PCIE_CTRL_RC_MODE BIT(1)
Anton Schubert9c28d612015-08-11 11:54:01 +020066#define PCIE_STAT_OFF 0x1a04
67#define PCIE_STAT_BUS (0xff << 8)
68#define PCIE_STAT_DEV (0x1f << 16)
69#define PCIE_STAT_LINK_DOWN BIT(0)
70#define PCIE_DEBUG_CTRL 0x1a60
71#define PCIE_DEBUG_SOFT_RESET BIT(20)
72
Anton Schubert9c28d612015-08-11 11:54:01 +020073struct mvebu_pcie {
74 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020075 void __iomem *base;
76 void __iomem *membase;
77 struct resource mem;
78 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010079 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020080 u32 port;
81 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010082 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020083 u32 lane_mask;
Marek Behún10eb2cc2021-02-08 23:01:40 +010084 int first_busno;
Pali Rohára7b61ab2021-10-22 16:22:10 +020085 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010086 char name[16];
87 unsigned int mem_target;
88 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010089 unsigned int io_target;
90 unsigned int io_attr;
Pali Rohára7b61ab2021-10-22 16:22:10 +020091 u32 cfgcache[0x34 - 0x10];
Anton Schubert9c28d612015-08-11 11:54:01 +020092};
93
Anton Schubert9c28d612015-08-11 11:54:01 +020094/*
95 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030096 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020097 * and 64K of I/O space when registered.
98 */
99static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
VlaoMao49b23e02017-09-22 18:49:02 +0300100#define PCIE_MEM_SIZE (128 << 20)
Phil Sutterba8ae032021-01-03 23:06:46 +0100101static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +0200102
Anton Schubert9c28d612015-08-11 11:54:01 +0200103static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
104{
105 u32 val;
106 val = readl(pcie->base + PCIE_STAT_OFF);
107 return !(val & PCIE_STAT_LINK_DOWN);
108}
109
110static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
111{
112 u32 stat;
113
114 stat = readl(pcie->base + PCIE_STAT_OFF);
115 stat &= ~PCIE_STAT_BUS;
116 stat |= busno << 8;
117 writel(stat, pcie->base + PCIE_STAT_OFF);
118}
119
120static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
121{
122 u32 stat;
123
124 stat = readl(pcie->base + PCIE_STAT_OFF);
125 stat &= ~PCIE_STAT_DEV;
126 stat |= devno << 16;
127 writel(stat, pcie->base + PCIE_STAT_OFF);
128}
129
Anton Schubert9c28d612015-08-11 11:54:01 +0200130static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
131{
132 return container_of(hose, struct mvebu_pcie, hose);
133}
134
Pali Rohára7b61ab2021-10-22 16:22:10 +0200135static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
136 int busno, int dev, int func)
Marek Behún10eb2cc2021-02-08 23:01:40 +0100137{
Pali Rohára7b61ab2021-10-22 16:22:10 +0200138 /* On primary bus is only one PCI Bridge */
139 if (busno == pcie->first_busno && (dev != 0 || func != 0))
140 return false;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100141
Pali Rohár79b4eb22021-10-22 16:22:12 +0200142 /* Access to other buses is possible when link is up */
143 if (busno != pcie->first_busno && !mvebu_pcie_link_up(pcie))
144 return false;
145
Pali Rohára7b61ab2021-10-22 16:22:10 +0200146 /* On secondary bus can be only one PCIe device */
147 if (busno == pcie->sec_busno && dev != 0)
148 return false;
149
150 return true;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100151}
152
Simon Glassc4e72c42020-01-27 08:49:37 -0700153static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100154 uint offset, ulong *valuep,
155 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200156{
Simon Glassc69cda22020-12-03 16:55:20 -0700157 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200158 int busno = PCI_BUS(bdf) - dev_seq(bus);
159 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100160
Marek Behún10eb2cc2021-02-08 23:01:40 +0100161 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
162 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200163
Pali Rohára7b61ab2021-10-22 16:22:10 +0200164 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100165 debug("- out of range\n");
166 *valuep = pci_get_ff(size);
167 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200168 }
169
Pali Rohára7b61ab2021-10-22 16:22:10 +0200170 /*
171 * mvebu has different internal registers mapped into PCI config space
172 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
173 * for this range and instead read content from driver virtual cfgcache
174 */
175 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
176 data = pcie->cfgcache[(offset - 0x10) / 4];
177 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
178 offset, size, data);
179 *valuep = pci_conv_32_to_size(data, offset, size);
180 return 0;
181 } else if (busno == pcie->first_busno &&
182 (offset & ~3) == PCI_ROM_ADDRESS1) {
183 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
184 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
185 }
186
187 /*
188 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
189 * secondary bus with device number 1.
190 */
191 if (busno == pcie->first_busno)
192 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
193 else
194 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
195
Anton Schubert9c28d612015-08-11 11:54:01 +0200196 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200197 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100198
199 /* read data */
Pali Rohár657177a2021-10-22 16:22:09 +0200200 switch (size) {
201 case PCI_SIZE_8:
202 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
203 break;
204 case PCI_SIZE_16:
205 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
206 break;
207 case PCI_SIZE_32:
208 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
209 break;
210 default:
211 return -EINVAL;
212 }
213
Pali Rohára7b61ab2021-10-22 16:22:10 +0200214 if (busno == pcie->first_busno &&
215 (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
216 /*
217 * Change Header Type of PCI Bridge device to Type 1
218 * (0x01, used by PCI Bridges) because mvebu reports
219 * Type 0 (0x00, used by Upstream and Endpoint devices).
220 */
221 data = pci_conv_size_to_32(data, 0, offset, size);
222 data &= ~0x007f0000;
223 data |= PCI_HEADER_TYPE_BRIDGE << 16;
224 data = pci_conv_32_to_size(data, offset, size);
225 }
226
Marek Behún26f7a762021-02-08 23:01:39 +0100227 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár657177a2021-10-22 16:22:09 +0200228 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200229
230 return 0;
231}
232
Stefan Roese94f453e2019-01-25 11:52:43 +0100233static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
234 uint offset, ulong value,
235 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200236{
Simon Glassc69cda22020-12-03 16:55:20 -0700237 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200238 int busno = PCI_BUS(bdf) - dev_seq(bus);
239 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100240
Marek Behún10eb2cc2021-02-08 23:01:40 +0100241 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
242 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100243 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200244
Pali Rohára7b61ab2021-10-22 16:22:10 +0200245 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100246 debug("- out of range\n");
247 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200248 }
249
Pali Rohára7b61ab2021-10-22 16:22:10 +0200250 /*
251 * mvebu has different internal registers mapped into PCI config space
252 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
253 * for this range and instead write content to driver virtual cfgcache
254 */
255 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
256 debug("Writing to cfgcache only\n");
257 data = pcie->cfgcache[(offset - 0x10) / 4];
258 data = pci_conv_size_to_32(data, value, offset, size);
259 /* mvebu PCI bridge does not have configurable bars */
260 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
261 (offset & ~3) == PCI_BASE_ADDRESS_1)
262 data = 0x0;
263 pcie->cfgcache[(offset - 0x10) / 4] = data;
264 /* mvebu has its own way how to set PCI primary bus number */
265 if (offset == PCI_PRIMARY_BUS) {
266 pcie->first_busno = data & 0xff;
267 debug("Primary bus number was changed to %d\n",
268 pcie->first_busno);
269 }
270 /* mvebu has its own way how to set PCI secondary bus number */
271 if (offset == PCI_SECONDARY_BUS ||
272 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
273 pcie->sec_busno = (data >> 8) & 0xff;
274 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
275 debug("Secondary bus number was changed to %d\n",
276 pcie->sec_busno);
277 }
278 return 0;
279 } else if (busno == pcie->first_busno &&
280 (offset & ~3) == PCI_ROM_ADDRESS1) {
281 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
282 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
283 }
284
285 /*
286 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
287 * secondary bus with device number 1.
288 */
289 if (busno == pcie->first_busno)
290 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
291 else
292 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
293
Marek Behún241d7632021-02-08 23:01:38 +0100294 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200295 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100296
297 /* write data */
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200298 switch (size) {
299 case PCI_SIZE_8:
300 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
301 break;
302 case PCI_SIZE_16:
303 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
304 break;
305 case PCI_SIZE_32:
306 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
307 break;
308 default:
309 return -EINVAL;
310 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200311
312 return 0;
313}
314
315/*
316 * Setup PCIE BARs and Address Decode Wins:
317 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
318 * WIN[0-3] -> DRAM bank[0-3]
319 */
320static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
321{
322 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
323 u32 size;
324 int i;
325
326 /* First, disable and clear BARs and windows. */
327 for (i = 1; i < 3; i++) {
328 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
329 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
330 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
331 }
332
333 for (i = 0; i < 5; i++) {
334 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
335 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
336 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
337 }
338
339 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
340 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
341 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
342
343 /* Setup windows for DDR banks. Count total DDR size on the fly. */
344 size = 0;
345 for (i = 0; i < dram->num_cs; i++) {
346 const struct mbus_dram_window *cs = dram->cs + i;
347
348 writel(cs->base & 0xffff0000,
349 pcie->base + PCIE_WIN04_BASE_OFF(i));
350 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
351 writel(((cs->size - 1) & 0xffff0000) |
352 (cs->mbus_attr << 8) |
353 (dram->mbus_dram_target_id << 4) | 1,
354 pcie->base + PCIE_WIN04_CTRL_OFF(i));
355
356 size += cs->size;
357 }
358
359 /* Round up 'size' to the nearest power of two. */
360 if ((size & (size - 1)) != 0)
361 size = 1 << fls(size);
362
363 /* Setup BAR[1] to all DRAM banks. */
364 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
365 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
366 writel(((size - 1) & 0xffff0000) | 0x1,
367 pcie->base + PCIE_BAR_CTRL_OFF(1));
368}
369
Stefan Roese94f453e2019-01-25 11:52:43 +0100370static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200371{
Simon Glassc69cda22020-12-03 16:55:20 -0700372 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100373 struct udevice *ctlr = pci_get_controller(dev);
374 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert9c28d612015-08-11 11:54:01 +0200375 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200376
Pali Rohár2344a762021-10-22 16:22:14 +0200377 /* Setup PCIe controller to Root Complex mode */
378 reg = readl(pcie->base + PCIE_CTRL_OFF);
379 reg |= PCIE_CTRL_RC_MODE;
380 writel(reg, pcie->base + PCIE_CTRL_OFF);
381
Pali Rohára7b61ab2021-10-22 16:22:10 +0200382 /*
383 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
384 * because default value is Memory controller (0x508000) which
385 * U-Boot cannot recognize as P2P Bridge.
386 *
387 * Note that this mvebu PCI Bridge does not have compliant Type 1
388 * Configuration Space. Header Type is reported as Type 0 and in
389 * range 0x10-0x34 it has aliased internal mvebu registers 0x10-0x34
390 * (e.g. PCIE_BAR_LO_OFF) and register 0x38 is reserved.
391 *
392 * Driver for this range redirects access to virtual cfgcache[] buffer
393 * which avoids changing internal mvebu registers. And changes Header
394 * Type response value to Type 1.
395 */
396 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
397 reg &= ~0xffffff00;
398 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
399 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200400
Pali Rohára7b61ab2021-10-22 16:22:10 +0200401 /*
402 * mvebu uses local bus number and local device number to determinate
403 * type of config request. Type 0 is used if target bus number equals
404 * local bus number and target device number differs from local device
405 * number. Type 1 is used if target bus number differs from local bus
406 * number. And when target bus number equals local bus number and
407 * target device equals local device number then request is routed to
408 * PCI Bridge which represent local PCIe Root Port.
409 *
410 * It means that PCI primary and secondary buses shares one bus number
411 * which is configured via local bus number. Determination if config
412 * request should go to primary or secondary bus is done based on local
413 * device number.
414 *
415 * PCIe is point-to-point bus, so at secondary bus is always exactly one
416 * device with number 0. So set local device number to 1, it would not
417 * conflict with any device on secondary bus number and will ensure that
418 * accessing secondary bus and all buses behind secondary would work
419 * automatically and correctly. Therefore this configuration of local
420 * device number implies that setting of local bus number configures
421 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
422 * later configure it via config write requests to the correct value.
423 * mvebu_pcie_write_config() catches config write requests which tries
424 * to change primary/secondary bus number and correctly updates local
425 * bus number based on new secondary bus number.
426 *
427 * With this configuration is PCI Bridge available at secondary bus as
428 * device number 1. But it must be available at primary bus as device
429 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
430 * functions rewrite address to the real one when accessing primary bus.
431 */
432 mvebu_pcie_set_local_bus_nr(pcie, 0);
433 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200434
Stefan Roese94f453e2019-01-25 11:52:43 +0100435 pcie->mem.start = (u32)mvebu_pcie_membase;
436 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
437 mvebu_pcie_membase += PCIE_MEM_SIZE;
438
439 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
440 (phys_addr_t)pcie->mem.start,
441 PCIE_MEM_SIZE)) {
442 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
443 (u32)pcie->mem.start, PCIE_MEM_SIZE);
444 }
445
Phil Sutterba8ae032021-01-03 23:06:46 +0100446 pcie->io.start = (u32)mvebu_pcie_iobase;
447 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
448 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
449
450 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
451 (phys_addr_t)pcie->io.start,
452 MBUS_PCI_IO_SIZE)) {
453 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
454 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
455 }
456
Stefan Roese94f453e2019-01-25 11:52:43 +0100457 /* Setup windows and configure host bridge */
458 mvebu_pcie_setup_wins(pcie);
459
Stefan Roese94f453e2019-01-25 11:52:43 +0100460 /* PCI memory space */
461 pci_set_region(hose->regions + 0, pcie->mem.start,
462 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
463 pci_set_region(hose->regions + 1,
464 0, 0,
465 gd->ram_size,
466 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100467 pci_set_region(hose->regions + 2, pcie->io.start,
468 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
469 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100470
Marek Behún193a1e92019-08-07 15:01:56 +0200471 /* Set BAR0 to internal registers */
472 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
473 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
474
Pali Rohára7b61ab2021-10-22 16:22:10 +0200475 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
476 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
477 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
478 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
479 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
480
Stefan Roese94f453e2019-01-25 11:52:43 +0100481 return 0;
482}
483
484static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
485{
486 const u32 *addr;
487 int len;
488
489 addr = ofnode_get_property(node, "assigned-addresses", &len);
490 if (!addr) {
491 pr_err("property \"assigned-addresses\" not found");
492 return -FDT_ERR_NOTFOUND;
493 }
494
495 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
496
497 return 0;
498}
499
500#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
501#define DT_TYPE_IO 0x1
502#define DT_TYPE_MEM32 0x2
503#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
504#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
505
506static int mvebu_get_tgt_attr(ofnode node, int devfn,
507 unsigned long type,
508 unsigned int *tgt,
509 unsigned int *attr)
510{
511 const int na = 3, ns = 2;
512 const __be32 *range;
513 int rlen, nranges, rangesz, pna, i;
514
515 *tgt = -1;
516 *attr = -1;
517
518 range = ofnode_get_property(node, "ranges", &rlen);
519 if (!range)
520 return -EINVAL;
521
Stefan Roese0df62e82019-02-11 07:53:34 +0100522 /*
523 * Linux uses of_n_addr_cells() to get the number of address cells
524 * here. Currently this function is only available in U-Boot when
525 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
526 * general, lets't hardcode the "pna" value in the U-Boot code.
527 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100528 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
529 rangesz = pna + na + ns;
530 nranges = rlen / sizeof(__be32) / rangesz;
531
532 for (i = 0; i < nranges; i++, range += rangesz) {
533 u32 flags = of_read_number(range, 1);
534 u32 slot = of_read_number(range + 1, 1);
535 u64 cpuaddr = of_read_number(range + na, pna);
536 unsigned long rtype;
537
538 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
539 rtype = IORESOURCE_IO;
540 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
541 rtype = IORESOURCE_MEM;
542 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200543 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200544
Stefan Roese94f453e2019-01-25 11:52:43 +0100545 /*
546 * The Linux code used PCI_SLOT() here, which expects devfn
547 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
548 * only expects devfn in 15..8, where its saved in this driver.
549 */
550 if (slot == PCI_DEV(devfn) && type == rtype) {
551 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
552 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
553 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100554 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200555 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100556
557 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200558}
Stefan Roese94f453e2019-01-25 11:52:43 +0100559
Simon Glassd1998a92020-12-03 16:55:21 -0700560static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100561{
Simon Glassc69cda22020-12-03 16:55:20 -0700562 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100563 int ret = 0;
564
565 /* Get port number, lane number and memory target / attr */
566 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
567 &pcie->port)) {
568 ret = -ENODEV;
569 goto err;
570 }
571
572 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
573 pcie->lane = 0;
574
575 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
576
577 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
578 pcie->devfn = pci_get_devfn(dev);
579 if (pcie->devfn < 0) {
580 ret = -ENODEV;
581 goto err;
582 }
583
584 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
585 IORESOURCE_MEM,
586 &pcie->mem_target, &pcie->mem_attr);
587 if (ret < 0) {
588 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
589 goto err;
590 }
591
Phil Sutterba8ae032021-01-03 23:06:46 +0100592 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
593 IORESOURCE_IO,
594 &pcie->io_target, &pcie->io_attr);
595 if (ret < 0) {
596 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
597 goto err;
598 }
599
Stefan Roese94f453e2019-01-25 11:52:43 +0100600 /* Parse PCIe controller register base from DT */
601 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
602 if (ret < 0)
603 goto err;
604
Stefan Roese94f453e2019-01-25 11:52:43 +0100605 return 0;
606
607err:
608 return ret;
609}
610
611static const struct dm_pci_ops mvebu_pcie_ops = {
612 .read_config = mvebu_pcie_read_config,
613 .write_config = mvebu_pcie_write_config,
614};
615
616static struct driver pcie_mvebu_drv = {
617 .name = "pcie_mvebu",
618 .id = UCLASS_PCI,
619 .ops = &mvebu_pcie_ops,
620 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700621 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700622 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100623};
624
625/*
626 * Use a MISC device to bind the n instances (child nodes) of the
627 * PCIe base controller in UCLASS_PCI.
628 */
629static int mvebu_pcie_bind(struct udevice *parent)
630{
631 struct mvebu_pcie *pcie;
632 struct uclass_driver *drv;
633 struct udevice *dev;
634 ofnode subnode;
635
636 /* Lookup eth driver */
637 drv = lists_uclass_lookup(UCLASS_PCI);
638 if (!drv) {
639 puts("Cannot find PCI driver\n");
640 return -ENOENT;
641 }
642
643 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
644 if (!ofnode_is_available(subnode))
645 continue;
646
647 pcie = calloc(1, sizeof(*pcie));
648 if (!pcie)
649 return -ENOMEM;
650
651 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700652 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
653 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100654 }
655
656 return 0;
657}
658
659static const struct udevice_id mvebu_pcie_ids[] = {
660 { .compatible = "marvell,armada-xp-pcie" },
661 { .compatible = "marvell,armada-370-pcie" },
662 { }
663};
664
665U_BOOT_DRIVER(pcie_mvebu_base) = {
666 .name = "pcie_mvebu_base",
667 .id = UCLASS_MISC,
668 .of_match = mvebu_pcie_ids,
669 .bind = mvebu_pcie_bind,
670};