blob: 7c8807ade8b9605bf937c9358a9366a6026245d3 [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>
Pali Rohár22f69fc2021-12-16 12:04:06 +010010 * Pali Rohár <pali@kernel.org>
Anton Schubert9c28d612015-08-11 11:54:01 +020011 */
12
13#include <common.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010018#include <dm/device-internal.h>
19#include <dm/lists.h>
20#include <dm/of_access.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020021#include <pci.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020022#include <asm/io.h>
23#include <asm/arch/cpu.h>
24#include <asm/arch/soc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060025#include <linux/bitops.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010026#include <linux/errno.h>
27#include <linux/ioport.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020028#include <linux/mbus.h>
29
30DECLARE_GLOBAL_DATA_PTR;
31
32/* PCIe unit register offsets */
33#define SELECT(x, n) ((x >> n) & 1UL)
34
35#define PCIE_DEV_ID_OFF 0x0000
36#define PCIE_CMD_OFF 0x0004
37#define PCIE_DEV_REV_OFF 0x0008
38#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
39#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
Pali Rohára7b61ab2021-10-22 16:22:10 +020040#define PCIE_EXP_ROM_BAR_OFF 0x0030
Anton Schubert9c28d612015-08-11 11:54:01 +020041#define PCIE_CAPAB_OFF 0x0060
42#define PCIE_CTRL_STAT_OFF 0x0068
43#define PCIE_HEADER_LOG_4_OFF 0x0128
44#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
45#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
46#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
47#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
48#define PCIE_WIN5_CTRL_OFF 0x1880
49#define PCIE_WIN5_BASE_OFF 0x1884
50#define PCIE_WIN5_REMAP_OFF 0x188c
51#define PCIE_CONF_ADDR_OFF 0x18f8
52#define PCIE_CONF_ADDR_EN BIT(31)
53#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
54#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
55#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
56#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
Pali Rohára7b61ab2021-10-22 16:22:10 +020057#define PCIE_CONF_ADDR(b, d, f, reg) \
58 (PCIE_CONF_BUS(b) | PCIE_CONF_DEV(d) | \
59 PCIE_CONF_FUNC(f) | PCIE_CONF_REG(reg) | \
Anton Schubert9c28d612015-08-11 11:54:01 +020060 PCIE_CONF_ADDR_EN)
61#define PCIE_CONF_DATA_OFF 0x18fc
62#define PCIE_MASK_OFF 0x1910
63#define PCIE_MASK_ENABLE_INTS (0xf << 24)
64#define PCIE_CTRL_OFF 0x1a00
65#define PCIE_CTRL_X1_MODE BIT(0)
Pali Rohár2344a762021-10-22 16:22:14 +020066#define PCIE_CTRL_RC_MODE BIT(1)
Anton Schubert9c28d612015-08-11 11:54:01 +020067#define PCIE_STAT_OFF 0x1a04
68#define PCIE_STAT_BUS (0xff << 8)
69#define PCIE_STAT_DEV (0x1f << 16)
70#define PCIE_STAT_LINK_DOWN BIT(0)
71#define PCIE_DEBUG_CTRL 0x1a60
72#define PCIE_DEBUG_SOFT_RESET BIT(20)
73
Anton Schubert9c28d612015-08-11 11:54:01 +020074struct mvebu_pcie {
75 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020076 void __iomem *base;
77 void __iomem *membase;
78 struct resource mem;
79 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010080 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020081 u32 port;
82 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010083 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020084 u32 lane_mask;
Marek Behún10eb2cc2021-02-08 23:01:40 +010085 int first_busno;
Pali Rohára7b61ab2021-10-22 16:22:10 +020086 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010087 char name[16];
88 unsigned int mem_target;
89 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010090 unsigned int io_target;
91 unsigned int io_attr;
Pali Rohára48e4282021-11-11 16:35:45 +010092 u32 cfgcache[(0x3c - 0x10) / 4];
Anton Schubert9c28d612015-08-11 11:54:01 +020093};
94
Anton Schubert9c28d612015-08-11 11:54:01 +020095/*
96 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030097 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020098 * and 64K of I/O space when registered.
99 */
100static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
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 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100171 * The configuration space of the PCI Bridge on primary (first) bus is
172 * of Type 0 but the BAR registers (including ROM BAR) don't have the
173 * same meaning as in the PCIe specification. Therefore do not access
174 * BAR registers and non-common registers (those which have different
175 * meaning for Type 0 and Type 1 config space) of the PCI Bridge and
176 * instead read their content from driver virtual cfgcache[].
Pali Rohára7b61ab2021-10-22 16:22:10 +0200177 */
Pali Rohára48e4282021-11-11 16:35:45 +0100178 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
179 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200180 data = pcie->cfgcache[(offset - 0x10) / 4];
181 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
182 offset, size, data);
183 *valuep = pci_conv_32_to_size(data, offset, size);
184 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200185 }
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 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100251 * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific
252 * config registers are not available, so we write their content only
253 * into driver virtual cfgcache[].
254 * And as explained in mvebu_pcie_probe(), mvebu has its own specific
255 * way for configuring primary and secondary bus numbers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200256 */
Pali Rohára48e4282021-11-11 16:35:45 +0100257 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
258 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200259 debug("Writing to cfgcache only\n");
260 data = pcie->cfgcache[(offset - 0x10) / 4];
261 data = pci_conv_size_to_32(data, value, offset, size);
262 /* mvebu PCI bridge does not have configurable bars */
263 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
Pali Rohára48e4282021-11-11 16:35:45 +0100264 (offset & ~3) == PCI_BASE_ADDRESS_1 ||
265 (offset & ~3) == PCI_ROM_ADDRESS1)
Pali Rohára7b61ab2021-10-22 16:22:10 +0200266 data = 0x0;
267 pcie->cfgcache[(offset - 0x10) / 4] = data;
268 /* mvebu has its own way how to set PCI primary bus number */
269 if (offset == PCI_PRIMARY_BUS) {
270 pcie->first_busno = data & 0xff;
271 debug("Primary bus number was changed to %d\n",
272 pcie->first_busno);
273 }
274 /* mvebu has its own way how to set PCI secondary bus number */
275 if (offset == PCI_SECONDARY_BUS ||
276 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
277 pcie->sec_busno = (data >> 8) & 0xff;
278 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
279 debug("Secondary bus number was changed to %d\n",
280 pcie->sec_busno);
281 }
282 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200283 }
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:
Pali Rohár4a1a5932021-11-11 16:35:42 +0100317 * BAR[0] -> internal registers
318 * BAR[1] -> covers all DRAM banks
319 * BAR[2] -> disabled
Anton Schubert9c28d612015-08-11 11:54:01 +0200320 * WIN[0-3] -> DRAM bank[0-3]
321 */
322static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
323{
324 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
325 u32 size;
326 int i;
327
328 /* First, disable and clear BARs and windows. */
329 for (i = 1; i < 3; i++) {
330 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
331 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
332 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
333 }
334
335 for (i = 0; i < 5; i++) {
336 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
337 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
338 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
339 }
340
341 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
342 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
343 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
344
345 /* Setup windows for DDR banks. Count total DDR size on the fly. */
346 size = 0;
347 for (i = 0; i < dram->num_cs; i++) {
348 const struct mbus_dram_window *cs = dram->cs + i;
349
350 writel(cs->base & 0xffff0000,
351 pcie->base + PCIE_WIN04_BASE_OFF(i));
352 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
353 writel(((cs->size - 1) & 0xffff0000) |
354 (cs->mbus_attr << 8) |
355 (dram->mbus_dram_target_id << 4) | 1,
356 pcie->base + PCIE_WIN04_CTRL_OFF(i));
357
358 size += cs->size;
359 }
360
361 /* Round up 'size' to the nearest power of two. */
362 if ((size & (size - 1)) != 0)
363 size = 1 << fls(size);
364
365 /* Setup BAR[1] to all DRAM banks. */
366 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
367 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
368 writel(((size - 1) & 0xffff0000) | 0x1,
369 pcie->base + PCIE_BAR_CTRL_OFF(1));
Pali Rohár4a1a5932021-11-11 16:35:42 +0100370
371 /* Setup BAR[0] to internal registers. */
372 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
373 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
Anton Schubert9c28d612015-08-11 11:54:01 +0200374}
375
Stefan Roese94f453e2019-01-25 11:52:43 +0100376static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200377{
Simon Glassc69cda22020-12-03 16:55:20 -0700378 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100379 struct udevice *ctlr = pci_get_controller(dev);
380 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert9c28d612015-08-11 11:54:01 +0200381 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200382
Pali Rohár2344a762021-10-22 16:22:14 +0200383 /* Setup PCIe controller to Root Complex mode */
384 reg = readl(pcie->base + PCIE_CTRL_OFF);
385 reg |= PCIE_CTRL_RC_MODE;
386 writel(reg, pcie->base + PCIE_CTRL_OFF);
387
Pali Rohára7b61ab2021-10-22 16:22:10 +0200388 /*
389 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
390 * because default value is Memory controller (0x508000) which
391 * U-Boot cannot recognize as P2P Bridge.
392 *
393 * Note that this mvebu PCI Bridge does not have compliant Type 1
Pali Rohára48e4282021-11-11 16:35:45 +0100394 * Configuration Space. Header Type is reported as Type 0 and it
395 * has format of Type 0 config space.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200396 *
Pali Rohára48e4282021-11-11 16:35:45 +0100397 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
398 * have the same format in Marvell's specification as in PCIe
399 * specification, but their meaning is totally different and they do
400 * different things: they are aliased into internal mvebu registers
401 * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or
402 * reconfigured by pci device drivers.
403 *
404 * So our driver converts Type 0 config space to Type 1 and reports
405 * Header Type as Type 1. Access to BAR registers and to non-existent
406 * Type 1 registers is redirected to the virtual cfgcache[] buffer,
407 * which avoids changing unrelated registers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200408 */
409 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
410 reg &= ~0xffffff00;
411 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
412 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200413
Pali Rohára7b61ab2021-10-22 16:22:10 +0200414 /*
415 * mvebu uses local bus number and local device number to determinate
416 * type of config request. Type 0 is used if target bus number equals
417 * local bus number and target device number differs from local device
418 * number. Type 1 is used if target bus number differs from local bus
419 * number. And when target bus number equals local bus number and
420 * target device equals local device number then request is routed to
421 * PCI Bridge which represent local PCIe Root Port.
422 *
423 * It means that PCI primary and secondary buses shares one bus number
424 * which is configured via local bus number. Determination if config
425 * request should go to primary or secondary bus is done based on local
426 * device number.
427 *
428 * PCIe is point-to-point bus, so at secondary bus is always exactly one
429 * device with number 0. So set local device number to 1, it would not
430 * conflict with any device on secondary bus number and will ensure that
431 * accessing secondary bus and all buses behind secondary would work
432 * automatically and correctly. Therefore this configuration of local
433 * device number implies that setting of local bus number configures
434 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
435 * later configure it via config write requests to the correct value.
436 * mvebu_pcie_write_config() catches config write requests which tries
437 * to change primary/secondary bus number and correctly updates local
438 * bus number based on new secondary bus number.
439 *
440 * With this configuration is PCI Bridge available at secondary bus as
441 * device number 1. But it must be available at primary bus as device
442 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
443 * functions rewrite address to the real one when accessing primary bus.
444 */
445 mvebu_pcie_set_local_bus_nr(pcie, 0);
446 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200447
Stefan Roese94f453e2019-01-25 11:52:43 +0100448 pcie->mem.start = (u32)mvebu_pcie_membase;
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100449 pcie->mem.end = pcie->mem.start + MBUS_PCI_MEM_SIZE - 1;
450 mvebu_pcie_membase += MBUS_PCI_MEM_SIZE;
Stefan Roese94f453e2019-01-25 11:52:43 +0100451
452 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
453 (phys_addr_t)pcie->mem.start,
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100454 MBUS_PCI_MEM_SIZE)) {
Stefan Roese94f453e2019-01-25 11:52:43 +0100455 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100456 (u32)pcie->mem.start, MBUS_PCI_MEM_SIZE);
Stefan Roese94f453e2019-01-25 11:52:43 +0100457 }
458
Phil Sutterba8ae032021-01-03 23:06:46 +0100459 pcie->io.start = (u32)mvebu_pcie_iobase;
460 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
461 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
462
463 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
464 (phys_addr_t)pcie->io.start,
465 MBUS_PCI_IO_SIZE)) {
466 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
467 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
468 }
469
Stefan Roese94f453e2019-01-25 11:52:43 +0100470 /* Setup windows and configure host bridge */
471 mvebu_pcie_setup_wins(pcie);
472
Stefan Roese94f453e2019-01-25 11:52:43 +0100473 /* PCI memory space */
474 pci_set_region(hose->regions + 0, pcie->mem.start,
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100475 pcie->mem.start, MBUS_PCI_MEM_SIZE, PCI_REGION_MEM);
Stefan Roese94f453e2019-01-25 11:52:43 +0100476 pci_set_region(hose->regions + 1,
477 0, 0,
478 gd->ram_size,
479 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100480 pci_set_region(hose->regions + 2, pcie->io.start,
481 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
482 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100483
Pali Rohára7b61ab2021-10-22 16:22:10 +0200484 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
485 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
486 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
487 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
488 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
489
Stefan Roese94f453e2019-01-25 11:52:43 +0100490 return 0;
491}
492
493static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
494{
495 const u32 *addr;
496 int len;
497
498 addr = ofnode_get_property(node, "assigned-addresses", &len);
499 if (!addr) {
500 pr_err("property \"assigned-addresses\" not found");
501 return -FDT_ERR_NOTFOUND;
502 }
503
504 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
505
506 return 0;
507}
508
509#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
510#define DT_TYPE_IO 0x1
511#define DT_TYPE_MEM32 0x2
512#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
513#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
514
515static int mvebu_get_tgt_attr(ofnode node, int devfn,
516 unsigned long type,
517 unsigned int *tgt,
518 unsigned int *attr)
519{
520 const int na = 3, ns = 2;
521 const __be32 *range;
522 int rlen, nranges, rangesz, pna, i;
523
524 *tgt = -1;
525 *attr = -1;
526
527 range = ofnode_get_property(node, "ranges", &rlen);
528 if (!range)
529 return -EINVAL;
530
Stefan Roese0df62e82019-02-11 07:53:34 +0100531 /*
532 * Linux uses of_n_addr_cells() to get the number of address cells
533 * here. Currently this function is only available in U-Boot when
534 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
535 * general, lets't hardcode the "pna" value in the U-Boot code.
536 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100537 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
538 rangesz = pna + na + ns;
539 nranges = rlen / sizeof(__be32) / rangesz;
540
541 for (i = 0; i < nranges; i++, range += rangesz) {
542 u32 flags = of_read_number(range, 1);
543 u32 slot = of_read_number(range + 1, 1);
544 u64 cpuaddr = of_read_number(range + na, pna);
545 unsigned long rtype;
546
547 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
548 rtype = IORESOURCE_IO;
549 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
550 rtype = IORESOURCE_MEM;
551 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200552 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200553
Stefan Roese94f453e2019-01-25 11:52:43 +0100554 /*
555 * The Linux code used PCI_SLOT() here, which expects devfn
556 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
557 * only expects devfn in 15..8, where its saved in this driver.
558 */
559 if (slot == PCI_DEV(devfn) && type == rtype) {
560 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
561 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
562 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100563 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200564 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100565
566 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200567}
Stefan Roese94f453e2019-01-25 11:52:43 +0100568
Simon Glassd1998a92020-12-03 16:55:21 -0700569static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100570{
Simon Glassc69cda22020-12-03 16:55:20 -0700571 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100572 int ret = 0;
573
574 /* Get port number, lane number and memory target / attr */
575 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
576 &pcie->port)) {
577 ret = -ENODEV;
578 goto err;
579 }
580
581 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
582 pcie->lane = 0;
583
584 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
585
586 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
587 pcie->devfn = pci_get_devfn(dev);
588 if (pcie->devfn < 0) {
589 ret = -ENODEV;
590 goto err;
591 }
592
593 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
594 IORESOURCE_MEM,
595 &pcie->mem_target, &pcie->mem_attr);
596 if (ret < 0) {
597 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
598 goto err;
599 }
600
Phil Sutterba8ae032021-01-03 23:06:46 +0100601 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
602 IORESOURCE_IO,
603 &pcie->io_target, &pcie->io_attr);
604 if (ret < 0) {
605 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
606 goto err;
607 }
608
Stefan Roese94f453e2019-01-25 11:52:43 +0100609 /* Parse PCIe controller register base from DT */
610 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
611 if (ret < 0)
612 goto err;
613
Stefan Roese94f453e2019-01-25 11:52:43 +0100614 return 0;
615
616err:
617 return ret;
618}
619
620static const struct dm_pci_ops mvebu_pcie_ops = {
621 .read_config = mvebu_pcie_read_config,
622 .write_config = mvebu_pcie_write_config,
623};
624
625static struct driver pcie_mvebu_drv = {
626 .name = "pcie_mvebu",
627 .id = UCLASS_PCI,
628 .ops = &mvebu_pcie_ops,
629 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700630 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700631 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100632};
633
634/*
635 * Use a MISC device to bind the n instances (child nodes) of the
636 * PCIe base controller in UCLASS_PCI.
637 */
638static int mvebu_pcie_bind(struct udevice *parent)
639{
640 struct mvebu_pcie *pcie;
641 struct uclass_driver *drv;
642 struct udevice *dev;
643 ofnode subnode;
644
Pali Rohár03a8a5e2021-10-22 16:22:15 +0200645 /* Lookup pci driver */
Stefan Roese94f453e2019-01-25 11:52:43 +0100646 drv = lists_uclass_lookup(UCLASS_PCI);
647 if (!drv) {
648 puts("Cannot find PCI driver\n");
649 return -ENOENT;
650 }
651
652 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
653 if (!ofnode_is_available(subnode))
654 continue;
655
656 pcie = calloc(1, sizeof(*pcie));
657 if (!pcie)
658 return -ENOMEM;
659
660 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700661 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
662 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100663 }
664
665 return 0;
666}
667
668static const struct udevice_id mvebu_pcie_ids[] = {
669 { .compatible = "marvell,armada-xp-pcie" },
670 { .compatible = "marvell,armada-370-pcie" },
671 { }
672};
673
674U_BOOT_DRIVER(pcie_mvebu_base) = {
675 .name = "pcie_mvebu_base",
676 .id = UCLASS_MISC,
677 .of_match = mvebu_pcie_ids,
678 .bind = mvebu_pcie_bind,
679};