blob: be394402836242665d54aa3c36ae2e2d6ba26ea0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Anton Schubert98530e92015-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 Schubert98530e92015-08-11 11:54:01 +020010 */
11
12#include <common.h>
Stefan Roese3179ec62019-01-25 11:52:43 +010013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Stefan Roese3179ec62019-01-25 11:52:43 +010017#include <dm/device-internal.h>
18#include <dm/lists.h>
19#include <dm/of_access.h>
Anton Schubert98530e92015-08-11 11:54:01 +020020#include <pci.h>
Anton Schubert98530e92015-08-11 11:54:01 +020021#include <asm/io.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/soc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Stefan Roese3179ec62019-01-25 11:52:43 +010025#include <linux/errno.h>
26#include <linux/ioport.h>
Anton Schubert98530e92015-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ár9e2274d2021-10-22 16:22:10 +020039#define PCIE_EXP_ROM_BAR_OFF 0x0030
Anton Schubert98530e92015-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ár9e2274d2021-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 Schubert98530e92015-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)
65#define PCIE_STAT_OFF 0x1a04
66#define PCIE_STAT_BUS (0xff << 8)
67#define PCIE_STAT_DEV (0x1f << 16)
68#define PCIE_STAT_LINK_DOWN BIT(0)
69#define PCIE_DEBUG_CTRL 0x1a60
70#define PCIE_DEBUG_SOFT_RESET BIT(20)
71
Anton Schubert98530e92015-08-11 11:54:01 +020072struct mvebu_pcie {
73 struct pci_controller hose;
Anton Schubert98530e92015-08-11 11:54:01 +020074 void __iomem *base;
75 void __iomem *membase;
76 struct resource mem;
77 void __iomem *iobase;
Phil Sutter09577aa2021-01-03 23:06:46 +010078 struct resource io;
Anton Schubert98530e92015-08-11 11:54:01 +020079 u32 port;
80 u32 lane;
Stefan Roese3179ec62019-01-25 11:52:43 +010081 int devfn;
Anton Schubert98530e92015-08-11 11:54:01 +020082 u32 lane_mask;
Marek Behún89036732021-02-08 23:01:40 +010083 int first_busno;
Pali Rohár9e2274d2021-10-22 16:22:10 +020084 int sec_busno;
Stefan Roese3179ec62019-01-25 11:52:43 +010085 char name[16];
86 unsigned int mem_target;
87 unsigned int mem_attr;
Phil Sutter09577aa2021-01-03 23:06:46 +010088 unsigned int io_target;
89 unsigned int io_attr;
Pali Rohár9e2274d2021-10-22 16:22:10 +020090 u32 cfgcache[0x34 - 0x10];
Anton Schubert98530e92015-08-11 11:54:01 +020091};
92
Anton Schubert98530e92015-08-11 11:54:01 +020093/*
94 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao9b9606d2017-09-22 18:49:02 +030095 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert98530e92015-08-11 11:54:01 +020096 * and 64K of I/O space when registered.
97 */
98static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
VlaoMao9b9606d2017-09-22 18:49:02 +030099#define PCIE_MEM_SIZE (128 << 20)
Phil Sutter09577aa2021-01-03 23:06:46 +0100100static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert98530e92015-08-11 11:54:01 +0200101
Anton Schubert98530e92015-08-11 11:54:01 +0200102static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
103{
104 u32 val;
105 val = readl(pcie->base + PCIE_STAT_OFF);
106 return !(val & PCIE_STAT_LINK_DOWN);
107}
108
109static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
110{
111 u32 stat;
112
113 stat = readl(pcie->base + PCIE_STAT_OFF);
114 stat &= ~PCIE_STAT_BUS;
115 stat |= busno << 8;
116 writel(stat, pcie->base + PCIE_STAT_OFF);
117}
118
119static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
120{
121 u32 stat;
122
123 stat = readl(pcie->base + PCIE_STAT_OFF);
124 stat &= ~PCIE_STAT_DEV;
125 stat |= devno << 16;
126 writel(stat, pcie->base + PCIE_STAT_OFF);
127}
128
129static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
130{
131 u32 stat;
132
133 stat = readl(pcie->base + PCIE_STAT_OFF);
134 return (stat & PCIE_STAT_BUS) >> 8;
135}
136
137static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
138{
139 u32 stat;
140
141 stat = readl(pcie->base + PCIE_STAT_OFF);
142 return (stat & PCIE_STAT_DEV) >> 16;
143}
144
145static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
146{
147 return container_of(hose, struct mvebu_pcie, hose);
148}
149
Pali Rohár9e2274d2021-10-22 16:22:10 +0200150static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
151 int busno, int dev, int func)
Marek Behún89036732021-02-08 23:01:40 +0100152{
Pali Rohár9e2274d2021-10-22 16:22:10 +0200153 /* On primary bus is only one PCI Bridge */
154 if (busno == pcie->first_busno && (dev != 0 || func != 0))
155 return false;
Marek Behún89036732021-02-08 23:01:40 +0100156
Pali Rohár9e2274d2021-10-22 16:22:10 +0200157 /* On secondary bus can be only one PCIe device */
158 if (busno == pcie->sec_busno && dev != 0)
159 return false;
160
161 return true;
Marek Behún89036732021-02-08 23:01:40 +0100162}
163
Simon Glass2a311e82020-01-27 08:49:37 -0700164static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese3179ec62019-01-25 11:52:43 +0100165 uint offset, ulong *valuep,
166 enum pci_size_t size)
Anton Schubert98530e92015-08-11 11:54:01 +0200167{
Simon Glassfa20e932020-12-03 16:55:20 -0700168 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohár9e2274d2021-10-22 16:22:10 +0200169 int busno = PCI_BUS(bdf) - dev_seq(bus);
170 u32 addr, data;
Stefan Roese3179ec62019-01-25 11:52:43 +0100171
Marek Behún89036732021-02-08 23:01:40 +0100172 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
173 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert98530e92015-08-11 11:54:01 +0200174
Pali Rohár9e2274d2021-10-22 16:22:10 +0200175 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese8d77b0a2021-01-25 15:25:31 +0100176 debug("- out of range\n");
177 *valuep = pci_get_ff(size);
178 return 0;
179 }
180
Pali Rohár9e2274d2021-10-22 16:22:10 +0200181 /*
182 * mvebu has different internal registers mapped into PCI config space
183 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
184 * for this range and instead read content from driver virtual cfgcache
185 */
186 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
187 data = pcie->cfgcache[(offset - 0x10) / 4];
188 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
189 offset, size, data);
190 *valuep = pci_conv_32_to_size(data, offset, size);
191 return 0;
192 } else if (busno == pcie->first_busno &&
193 (offset & ~3) == PCI_ROM_ADDRESS1) {
194 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
195 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
196 }
197
198 /*
199 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
200 * secondary bus with device number 1.
201 */
202 if (busno == pcie->first_busno)
203 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
204 else
205 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
206
Anton Schubert98530e92015-08-11 11:54:01 +0200207 /* write address */
Pali Rohár9e2274d2021-10-22 16:22:10 +0200208 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún9df558d2021-02-08 23:01:38 +0100209
210 /* read data */
Pali Rohár89220132021-10-22 16:22:09 +0200211 switch (size) {
212 case PCI_SIZE_8:
213 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
214 break;
215 case PCI_SIZE_16:
216 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
217 break;
218 case PCI_SIZE_32:
219 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
220 break;
221 default:
222 return -EINVAL;
223 }
224
Pali Rohár9e2274d2021-10-22 16:22:10 +0200225 if (busno == pcie->first_busno &&
226 (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
227 /*
228 * Change Header Type of PCI Bridge device to Type 1
229 * (0x01, used by PCI Bridges) because mvebu reports
230 * Type 0 (0x00, used by Upstream and Endpoint devices).
231 */
232 data = pci_conv_size_to_32(data, 0, offset, size);
233 data &= ~0x007f0000;
234 data |= PCI_HEADER_TYPE_BRIDGE << 16;
235 data = pci_conv_32_to_size(data, offset, size);
236 }
237
Marek Behúnd2ed1e52021-02-08 23:01:39 +0100238 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár89220132021-10-22 16:22:09 +0200239 *valuep = data;
Anton Schubert98530e92015-08-11 11:54:01 +0200240
241 return 0;
242}
243
Stefan Roese3179ec62019-01-25 11:52:43 +0100244static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
245 uint offset, ulong value,
246 enum pci_size_t size)
Anton Schubert98530e92015-08-11 11:54:01 +0200247{
Simon Glassfa20e932020-12-03 16:55:20 -0700248 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohár9e2274d2021-10-22 16:22:10 +0200249 int busno = PCI_BUS(bdf) - dev_seq(bus);
250 u32 addr, data;
Stefan Roese3179ec62019-01-25 11:52:43 +0100251
Marek Behún89036732021-02-08 23:01:40 +0100252 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
253 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behúnd2ed1e52021-02-08 23:01:39 +0100254 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert98530e92015-08-11 11:54:01 +0200255
Pali Rohár9e2274d2021-10-22 16:22:10 +0200256 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese8d77b0a2021-01-25 15:25:31 +0100257 debug("- out of range\n");
258 return 0;
Pali Rohár9e2274d2021-10-22 16:22:10 +0200259 }
260
261 /*
262 * mvebu has different internal registers mapped into PCI config space
263 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
264 * for this range and instead write content to driver virtual cfgcache
265 */
266 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
267 debug("Writing to cfgcache only\n");
268 data = pcie->cfgcache[(offset - 0x10) / 4];
269 data = pci_conv_size_to_32(data, value, offset, size);
270 /* mvebu PCI bridge does not have configurable bars */
271 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
272 (offset & ~3) == PCI_BASE_ADDRESS_1)
273 data = 0x0;
274 pcie->cfgcache[(offset - 0x10) / 4] = data;
275 /* mvebu has its own way how to set PCI primary bus number */
276 if (offset == PCI_PRIMARY_BUS) {
277 pcie->first_busno = data & 0xff;
278 debug("Primary bus number was changed to %d\n",
279 pcie->first_busno);
280 }
281 /* mvebu has its own way how to set PCI secondary bus number */
282 if (offset == PCI_SECONDARY_BUS ||
283 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
284 pcie->sec_busno = (data >> 8) & 0xff;
285 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
286 debug("Secondary bus number was changed to %d\n",
287 pcie->sec_busno);
288 }
289 return 0;
290 } else if (busno == pcie->first_busno &&
291 (offset & ~3) == PCI_ROM_ADDRESS1) {
292 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
293 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
Anton Schubert98530e92015-08-11 11:54:01 +0200294 }
295
Pali Rohár9e2274d2021-10-22 16:22:10 +0200296 /*
297 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
298 * secondary bus with device number 1.
299 */
300 if (busno == pcie->first_busno)
301 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
302 else
303 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
304
Marek Behún9df558d2021-02-08 23:01:38 +0100305 /* write address */
Pali Rohár9e2274d2021-10-22 16:22:10 +0200306 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún9df558d2021-02-08 23:01:38 +0100307
308 /* write data */
Pali Rohár35bce492021-10-22 16:22:08 +0200309 switch (size) {
310 case PCI_SIZE_8:
311 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
312 break;
313 case PCI_SIZE_16:
314 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
315 break;
316 case PCI_SIZE_32:
317 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
318 break;
319 default:
320 return -EINVAL;
321 }
Anton Schubert98530e92015-08-11 11:54:01 +0200322
323 return 0;
324}
325
326/*
327 * Setup PCIE BARs and Address Decode Wins:
328 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
329 * WIN[0-3] -> DRAM bank[0-3]
330 */
331static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
332{
333 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
334 u32 size;
335 int i;
336
337 /* First, disable and clear BARs and windows. */
338 for (i = 1; i < 3; i++) {
339 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
340 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
341 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
342 }
343
344 for (i = 0; i < 5; i++) {
345 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
346 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
347 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
348 }
349
350 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
351 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
352 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
353
354 /* Setup windows for DDR banks. Count total DDR size on the fly. */
355 size = 0;
356 for (i = 0; i < dram->num_cs; i++) {
357 const struct mbus_dram_window *cs = dram->cs + i;
358
359 writel(cs->base & 0xffff0000,
360 pcie->base + PCIE_WIN04_BASE_OFF(i));
361 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
362 writel(((cs->size - 1) & 0xffff0000) |
363 (cs->mbus_attr << 8) |
364 (dram->mbus_dram_target_id << 4) | 1,
365 pcie->base + PCIE_WIN04_CTRL_OFF(i));
366
367 size += cs->size;
368 }
369
370 /* Round up 'size' to the nearest power of two. */
371 if ((size & (size - 1)) != 0)
372 size = 1 << fls(size);
373
374 /* Setup BAR[1] to all DRAM banks. */
375 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
376 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
377 writel(((size - 1) & 0xffff0000) | 0x1,
378 pcie->base + PCIE_BAR_CTRL_OFF(1));
379}
380
Stefan Roese3179ec62019-01-25 11:52:43 +0100381static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert98530e92015-08-11 11:54:01 +0200382{
Simon Glassfa20e932020-12-03 16:55:20 -0700383 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese3179ec62019-01-25 11:52:43 +0100384 struct udevice *ctlr = pci_get_controller(dev);
385 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert98530e92015-08-11 11:54:01 +0200386 u32 reg;
Anton Schubert98530e92015-08-11 11:54:01 +0200387
Stefan Roese3179ec62019-01-25 11:52:43 +0100388 debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
389 pcie->port, pcie->lane, (u32)pcie->base);
Anton Schubert98530e92015-08-11 11:54:01 +0200390
Pali Rohár9e2274d2021-10-22 16:22:10 +0200391 /*
392 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
393 * because default value is Memory controller (0x508000) which
394 * U-Boot cannot recognize as P2P Bridge.
395 *
396 * Note that this mvebu PCI Bridge does not have compliant Type 1
397 * Configuration Space. Header Type is reported as Type 0 and in
398 * range 0x10-0x34 it has aliased internal mvebu registers 0x10-0x34
399 * (e.g. PCIE_BAR_LO_OFF) and register 0x38 is reserved.
400 *
401 * Driver for this range redirects access to virtual cfgcache[] buffer
402 * which avoids changing internal mvebu registers. And changes Header
403 * Type response value to Type 1.
404 */
405 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
406 reg &= ~0xffffff00;
407 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
408 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Marek Behún89036732021-02-08 23:01:40 +0100409
Pali Rohár9e2274d2021-10-22 16:22:10 +0200410 /*
411 * mvebu uses local bus number and local device number to determinate
412 * type of config request. Type 0 is used if target bus number equals
413 * local bus number and target device number differs from local device
414 * number. Type 1 is used if target bus number differs from local bus
415 * number. And when target bus number equals local bus number and
416 * target device equals local device number then request is routed to
417 * PCI Bridge which represent local PCIe Root Port.
418 *
419 * It means that PCI primary and secondary buses shares one bus number
420 * which is configured via local bus number. Determination if config
421 * request should go to primary or secondary bus is done based on local
422 * device number.
423 *
424 * PCIe is point-to-point bus, so at secondary bus is always exactly one
425 * device with number 0. So set local device number to 1, it would not
426 * conflict with any device on secondary bus number and will ensure that
427 * accessing secondary bus and all buses behind secondary would work
428 * automatically and correctly. Therefore this configuration of local
429 * device number implies that setting of local bus number configures
430 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
431 * later configure it via config write requests to the correct value.
432 * mvebu_pcie_write_config() catches config write requests which tries
433 * to change primary/secondary bus number and correctly updates local
434 * bus number based on new secondary bus number.
435 *
436 * With this configuration is PCI Bridge available at secondary bus as
437 * device number 1. But it must be available at primary bus as device
438 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
439 * functions rewrite address to the real one when accessing primary bus.
440 */
441 mvebu_pcie_set_local_bus_nr(pcie, 0);
442 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert98530e92015-08-11 11:54:01 +0200443
Stefan Roese3179ec62019-01-25 11:52:43 +0100444 pcie->mem.start = (u32)mvebu_pcie_membase;
445 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
446 mvebu_pcie_membase += PCIE_MEM_SIZE;
447
448 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
449 (phys_addr_t)pcie->mem.start,
450 PCIE_MEM_SIZE)) {
451 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
452 (u32)pcie->mem.start, PCIE_MEM_SIZE);
453 }
454
Phil Sutter09577aa2021-01-03 23:06:46 +0100455 pcie->io.start = (u32)mvebu_pcie_iobase;
456 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
457 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
458
459 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
460 (phys_addr_t)pcie->io.start,
461 MBUS_PCI_IO_SIZE)) {
462 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
463 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
464 }
465
Stefan Roese3179ec62019-01-25 11:52:43 +0100466 /* Setup windows and configure host bridge */
467 mvebu_pcie_setup_wins(pcie);
468
469 /* Master + slave enable. */
470 reg = readl(pcie->base + PCIE_CMD_OFF);
471 reg |= PCI_COMMAND_MEMORY;
Phil Sutter09577aa2021-01-03 23:06:46 +0100472 reg |= PCI_COMMAND_IO;
Stefan Roese3179ec62019-01-25 11:52:43 +0100473 reg |= PCI_COMMAND_MASTER;
474 reg |= BIT(10); /* disable interrupts */
475 writel(reg, pcie->base + PCIE_CMD_OFF);
476
Stefan Roese3179ec62019-01-25 11:52:43 +0100477 /* PCI memory space */
478 pci_set_region(hose->regions + 0, pcie->mem.start,
479 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
480 pci_set_region(hose->regions + 1,
481 0, 0,
482 gd->ram_size,
483 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutter09577aa2021-01-03 23:06:46 +0100484 pci_set_region(hose->regions + 2, pcie->io.start,
485 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
486 hose->region_count = 3;
Stefan Roese3179ec62019-01-25 11:52:43 +0100487
Marek Behún587816d2019-08-07 15:01:56 +0200488 /* Set BAR0 to internal registers */
489 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
490 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
491
Pali Rohár9e2274d2021-10-22 16:22:10 +0200492 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
493 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
494 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
495 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
496 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
497
Stefan Roese3179ec62019-01-25 11:52:43 +0100498 return 0;
499}
500
501static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
502{
503 const u32 *addr;
504 int len;
505
506 addr = ofnode_get_property(node, "assigned-addresses", &len);
507 if (!addr) {
508 pr_err("property \"assigned-addresses\" not found");
509 return -FDT_ERR_NOTFOUND;
510 }
511
512 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
513
514 return 0;
515}
516
517#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
518#define DT_TYPE_IO 0x1
519#define DT_TYPE_MEM32 0x2
520#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
521#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
522
523static int mvebu_get_tgt_attr(ofnode node, int devfn,
524 unsigned long type,
525 unsigned int *tgt,
526 unsigned int *attr)
527{
528 const int na = 3, ns = 2;
529 const __be32 *range;
530 int rlen, nranges, rangesz, pna, i;
531
532 *tgt = -1;
533 *attr = -1;
534
535 range = ofnode_get_property(node, "ranges", &rlen);
536 if (!range)
537 return -EINVAL;
538
Stefan Roese24e23bd2019-02-11 07:53:34 +0100539 /*
540 * Linux uses of_n_addr_cells() to get the number of address cells
541 * here. Currently this function is only available in U-Boot when
542 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
543 * general, lets't hardcode the "pna" value in the U-Boot code.
544 */
Stefan Roese3179ec62019-01-25 11:52:43 +0100545 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
546 rangesz = pna + na + ns;
547 nranges = rlen / sizeof(__be32) / rangesz;
Anton Schubert98530e92015-08-11 11:54:01 +0200548
Stefan Roese3179ec62019-01-25 11:52:43 +0100549 for (i = 0; i < nranges; i++, range += rangesz) {
550 u32 flags = of_read_number(range, 1);
551 u32 slot = of_read_number(range + 1, 1);
552 u64 cpuaddr = of_read_number(range + na, pna);
553 unsigned long rtype;
Anton Schubert98530e92015-08-11 11:54:01 +0200554
Stefan Roese3179ec62019-01-25 11:52:43 +0100555 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
556 rtype = IORESOURCE_IO;
557 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
558 rtype = IORESOURCE_MEM;
559 else
Anton Schubert98530e92015-08-11 11:54:01 +0200560 continue;
Stefan Roese3179ec62019-01-25 11:52:43 +0100561
562 /*
563 * The Linux code used PCI_SLOT() here, which expects devfn
564 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
565 * only expects devfn in 15..8, where its saved in this driver.
566 */
567 if (slot == PCI_DEV(devfn) && type == rtype) {
568 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
569 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
570 return 0;
Anton Schubert98530e92015-08-11 11:54:01 +0200571 }
Stefan Roese3179ec62019-01-25 11:52:43 +0100572 }
Anton Schubert98530e92015-08-11 11:54:01 +0200573
Stefan Roese3179ec62019-01-25 11:52:43 +0100574 return -ENOENT;
575}
Anton Schubert98530e92015-08-11 11:54:01 +0200576
Simon Glassaad29ae2020-12-03 16:55:21 -0700577static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese3179ec62019-01-25 11:52:43 +0100578{
Simon Glassfa20e932020-12-03 16:55:20 -0700579 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese3179ec62019-01-25 11:52:43 +0100580 int ret = 0;
Anton Schubert98530e92015-08-11 11:54:01 +0200581
Stefan Roese3179ec62019-01-25 11:52:43 +0100582 /* Get port number, lane number and memory target / attr */
583 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
584 &pcie->port)) {
585 ret = -ENODEV;
586 goto err;
587 }
Anton Schubert98530e92015-08-11 11:54:01 +0200588
Stefan Roese3179ec62019-01-25 11:52:43 +0100589 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
590 pcie->lane = 0;
Anton Schubert98530e92015-08-11 11:54:01 +0200591
Stefan Roese3179ec62019-01-25 11:52:43 +0100592 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
Anton Schubert98530e92015-08-11 11:54:01 +0200593
Stefan Roese3179ec62019-01-25 11:52:43 +0100594 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
595 pcie->devfn = pci_get_devfn(dev);
596 if (pcie->devfn < 0) {
597 ret = -ENODEV;
598 goto err;
599 }
Anton Schubert98530e92015-08-11 11:54:01 +0200600
Stefan Roese3179ec62019-01-25 11:52:43 +0100601 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
602 IORESOURCE_MEM,
603 &pcie->mem_target, &pcie->mem_attr);
604 if (ret < 0) {
605 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
606 goto err;
607 }
Anton Schubert98530e92015-08-11 11:54:01 +0200608
Phil Sutter09577aa2021-01-03 23:06:46 +0100609 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
610 IORESOURCE_IO,
611 &pcie->io_target, &pcie->io_attr);
612 if (ret < 0) {
613 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
614 goto err;
615 }
616
Stefan Roese3179ec62019-01-25 11:52:43 +0100617 /* Parse PCIe controller register base from DT */
618 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
619 if (ret < 0)
620 goto err;
Anton Schubert98530e92015-08-11 11:54:01 +0200621
Stefan Roese3179ec62019-01-25 11:52:43 +0100622 /* Check link and skip ports that have no link */
623 if (!mvebu_pcie_link_up(pcie)) {
624 debug("%s: %s - down\n", __func__, pcie->name);
625 ret = -ENODEV;
626 goto err;
627 }
Anton Schubert98530e92015-08-11 11:54:01 +0200628
Stefan Roese3179ec62019-01-25 11:52:43 +0100629 return 0;
Anton Schubert98530e92015-08-11 11:54:01 +0200630
Stefan Roese3179ec62019-01-25 11:52:43 +0100631err:
632 return ret;
633}
Anton Schubert98530e92015-08-11 11:54:01 +0200634
Stefan Roese3179ec62019-01-25 11:52:43 +0100635static const struct dm_pci_ops mvebu_pcie_ops = {
636 .read_config = mvebu_pcie_read_config,
637 .write_config = mvebu_pcie_write_config,
638};
Phil Sutter68010aa2015-12-25 14:41:20 +0100639
Stefan Roese3179ec62019-01-25 11:52:43 +0100640static struct driver pcie_mvebu_drv = {
641 .name = "pcie_mvebu",
642 .id = UCLASS_PCI,
643 .ops = &mvebu_pcie_ops,
644 .probe = mvebu_pcie_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700645 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700646 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese3179ec62019-01-25 11:52:43 +0100647};
648
649/*
650 * Use a MISC device to bind the n instances (child nodes) of the
651 * PCIe base controller in UCLASS_PCI.
652 */
653static int mvebu_pcie_bind(struct udevice *parent)
654{
655 struct mvebu_pcie *pcie;
656 struct uclass_driver *drv;
657 struct udevice *dev;
658 ofnode subnode;
659
660 /* Lookup eth driver */
661 drv = lists_uclass_lookup(UCLASS_PCI);
662 if (!drv) {
663 puts("Cannot find PCI driver\n");
664 return -ENOENT;
665 }
666
667 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
668 if (!ofnode_is_available(subnode))
669 continue;
670
671 pcie = calloc(1, sizeof(*pcie));
672 if (!pcie)
673 return -ENOMEM;
674
675 /* Create child device UCLASS_PCI and bind it */
Simon Glass884870f2020-11-28 17:50:01 -0700676 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
677 &dev);
Anton Schubert98530e92015-08-11 11:54:01 +0200678 }
Stefan Roese3179ec62019-01-25 11:52:43 +0100679
680 return 0;
Anton Schubert98530e92015-08-11 11:54:01 +0200681}
Stefan Roese3179ec62019-01-25 11:52:43 +0100682
683static const struct udevice_id mvebu_pcie_ids[] = {
684 { .compatible = "marvell,armada-xp-pcie" },
685 { .compatible = "marvell,armada-370-pcie" },
686 { }
687};
688
689U_BOOT_DRIVER(pcie_mvebu_base) = {
690 .name = "pcie_mvebu_base",
691 .id = UCLASS_MISC,
692 .of_match = mvebu_pcie_ids,
693 .bind = mvebu_pcie_bind,
694};