blob: 9248cbc294ce1d434794ee983f294a142e06ef43 [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>
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
Anton Schubert9c28d612015-08-11 11:54:01 +020029/* PCIe unit register offsets */
30#define SELECT(x, n) ((x >> n) & 1UL)
31
32#define PCIE_DEV_ID_OFF 0x0000
33#define PCIE_CMD_OFF 0x0004
34#define PCIE_DEV_REV_OFF 0x0008
35#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
36#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
Pali Rohára7b61ab2021-10-22 16:22:10 +020037#define PCIE_EXP_ROM_BAR_OFF 0x0030
Anton Schubert9c28d612015-08-11 11:54:01 +020038#define PCIE_CAPAB_OFF 0x0060
39#define PCIE_CTRL_STAT_OFF 0x0068
40#define PCIE_HEADER_LOG_4_OFF 0x0128
41#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
42#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
43#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
44#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
45#define PCIE_WIN5_CTRL_OFF 0x1880
46#define PCIE_WIN5_BASE_OFF 0x1884
47#define PCIE_WIN5_REMAP_OFF 0x188c
48#define PCIE_CONF_ADDR_OFF 0x18f8
49#define PCIE_CONF_ADDR_EN BIT(31)
50#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
51#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
52#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
53#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
Pali Rohára7b61ab2021-10-22 16:22:10 +020054#define PCIE_CONF_ADDR(b, d, f, reg) \
55 (PCIE_CONF_BUS(b) | PCIE_CONF_DEV(d) | \
56 PCIE_CONF_FUNC(f) | PCIE_CONF_REG(reg) | \
Anton Schubert9c28d612015-08-11 11:54:01 +020057 PCIE_CONF_ADDR_EN)
58#define PCIE_CONF_DATA_OFF 0x18fc
59#define PCIE_MASK_OFF 0x1910
60#define PCIE_MASK_ENABLE_INTS (0xf << 24)
61#define PCIE_CTRL_OFF 0x1a00
62#define PCIE_CTRL_X1_MODE BIT(0)
Pali Rohár2344a762021-10-22 16:22:14 +020063#define PCIE_CTRL_RC_MODE BIT(1)
Anton Schubert9c28d612015-08-11 11:54:01 +020064#define PCIE_STAT_OFF 0x1a04
65#define PCIE_STAT_BUS (0xff << 8)
66#define PCIE_STAT_DEV (0x1f << 16)
67#define PCIE_STAT_LINK_DOWN BIT(0)
68#define PCIE_DEBUG_CTRL 0x1a60
69#define PCIE_DEBUG_SOFT_RESET BIT(20)
70
Anton Schubert9c28d612015-08-11 11:54:01 +020071struct mvebu_pcie {
72 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020073 void __iomem *base;
74 void __iomem *membase;
75 struct resource mem;
76 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010077 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020078 u32 port;
79 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010080 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020081 u32 lane_mask;
Marek Behún10eb2cc2021-02-08 23:01:40 +010082 int first_busno;
Pali Rohára7b61ab2021-10-22 16:22:10 +020083 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010084 char name[16];
85 unsigned int mem_target;
86 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010087 unsigned int io_target;
88 unsigned int io_attr;
Pali Rohára48e4282021-11-11 16:35:45 +010089 u32 cfgcache[(0x3c - 0x10) / 4];
Anton Schubert9c28d612015-08-11 11:54:01 +020090};
91
Anton Schubert9c28d612015-08-11 11:54:01 +020092/*
93 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030094 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020095 * and 64K of I/O space when registered.
96 */
97static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
Phil Sutterba8ae032021-01-03 23:06:46 +010098static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +020099
Anton Schubert9c28d612015-08-11 11:54:01 +0200100static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
101{
102 u32 val;
103 val = readl(pcie->base + PCIE_STAT_OFF);
104 return !(val & PCIE_STAT_LINK_DOWN);
105}
106
107static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
108{
109 u32 stat;
110
111 stat = readl(pcie->base + PCIE_STAT_OFF);
112 stat &= ~PCIE_STAT_BUS;
113 stat |= busno << 8;
114 writel(stat, pcie->base + PCIE_STAT_OFF);
115}
116
117static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
118{
119 u32 stat;
120
121 stat = readl(pcie->base + PCIE_STAT_OFF);
122 stat &= ~PCIE_STAT_DEV;
123 stat |= devno << 16;
124 writel(stat, pcie->base + PCIE_STAT_OFF);
125}
126
Anton Schubert9c28d612015-08-11 11:54:01 +0200127static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
128{
129 return container_of(hose, struct mvebu_pcie, hose);
130}
131
Pali Rohára7b61ab2021-10-22 16:22:10 +0200132static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
133 int busno, int dev, int func)
Marek Behún10eb2cc2021-02-08 23:01:40 +0100134{
Pali Rohára7b61ab2021-10-22 16:22:10 +0200135 /* On primary bus is only one PCI Bridge */
136 if (busno == pcie->first_busno && (dev != 0 || func != 0))
137 return false;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100138
Pali Rohár79b4eb22021-10-22 16:22:12 +0200139 /* Access to other buses is possible when link is up */
140 if (busno != pcie->first_busno && !mvebu_pcie_link_up(pcie))
141 return false;
142
Pali Rohára7b61ab2021-10-22 16:22:10 +0200143 /* On secondary bus can be only one PCIe device */
144 if (busno == pcie->sec_busno && dev != 0)
145 return false;
146
147 return true;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100148}
149
Simon Glassc4e72c42020-01-27 08:49:37 -0700150static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100151 uint offset, ulong *valuep,
152 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200153{
Simon Glassc69cda22020-12-03 16:55:20 -0700154 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200155 int busno = PCI_BUS(bdf) - dev_seq(bus);
156 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100157
Marek Behún10eb2cc2021-02-08 23:01:40 +0100158 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
159 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200160
Pali Rohára7b61ab2021-10-22 16:22:10 +0200161 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100162 debug("- out of range\n");
163 *valuep = pci_get_ff(size);
164 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200165 }
166
Pali Rohára7b61ab2021-10-22 16:22:10 +0200167 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100168 * The configuration space of the PCI Bridge on primary (first) bus is
169 * of Type 0 but the BAR registers (including ROM BAR) don't have the
170 * same meaning as in the PCIe specification. Therefore do not access
171 * BAR registers and non-common registers (those which have different
172 * meaning for Type 0 and Type 1 config space) of the PCI Bridge and
173 * instead read their content from driver virtual cfgcache[].
Pali Rohára7b61ab2021-10-22 16:22:10 +0200174 */
Pali Rohára48e4282021-11-11 16:35:45 +0100175 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
176 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200177 data = pcie->cfgcache[(offset - 0x10) / 4];
178 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
179 offset, size, data);
180 *valuep = pci_conv_32_to_size(data, offset, size);
181 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200182 }
183
184 /*
185 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
186 * secondary bus with device number 1.
187 */
188 if (busno == pcie->first_busno)
189 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
190 else
191 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
192
Anton Schubert9c28d612015-08-11 11:54:01 +0200193 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200194 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100195
196 /* read data */
Pali Rohár657177a2021-10-22 16:22:09 +0200197 switch (size) {
198 case PCI_SIZE_8:
199 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
200 break;
201 case PCI_SIZE_16:
202 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
203 break;
204 case PCI_SIZE_32:
205 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
206 break;
207 default:
208 return -EINVAL;
209 }
210
Pali Rohára7b61ab2021-10-22 16:22:10 +0200211 if (busno == pcie->first_busno &&
212 (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
213 /*
214 * Change Header Type of PCI Bridge device to Type 1
215 * (0x01, used by PCI Bridges) because mvebu reports
216 * Type 0 (0x00, used by Upstream and Endpoint devices).
217 */
218 data = pci_conv_size_to_32(data, 0, offset, size);
219 data &= ~0x007f0000;
220 data |= PCI_HEADER_TYPE_BRIDGE << 16;
221 data = pci_conv_32_to_size(data, offset, size);
222 }
223
Marek Behún26f7a762021-02-08 23:01:39 +0100224 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár657177a2021-10-22 16:22:09 +0200225 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200226
227 return 0;
228}
229
Stefan Roese94f453e2019-01-25 11:52:43 +0100230static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
231 uint offset, ulong value,
232 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200233{
Simon Glassc69cda22020-12-03 16:55:20 -0700234 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200235 int busno = PCI_BUS(bdf) - dev_seq(bus);
236 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100237
Marek Behún10eb2cc2021-02-08 23:01:40 +0100238 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
239 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100240 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200241
Pali Rohára7b61ab2021-10-22 16:22:10 +0200242 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100243 debug("- out of range\n");
244 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200245 }
246
Pali Rohára7b61ab2021-10-22 16:22:10 +0200247 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100248 * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific
249 * config registers are not available, so we write their content only
250 * into driver virtual cfgcache[].
251 * And as explained in mvebu_pcie_probe(), mvebu has its own specific
252 * way for configuring primary and secondary bus numbers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200253 */
Pali Rohára48e4282021-11-11 16:35:45 +0100254 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
255 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200256 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 ||
Pali Rohára48e4282021-11-11 16:35:45 +0100261 (offset & ~3) == PCI_BASE_ADDRESS_1 ||
262 (offset & ~3) == PCI_ROM_ADDRESS1)
Pali Rohára7b61ab2021-10-22 16:22:10 +0200263 data = 0x0;
264 pcie->cfgcache[(offset - 0x10) / 4] = data;
265 /* mvebu has its own way how to set PCI primary bus number */
266 if (offset == PCI_PRIMARY_BUS) {
267 pcie->first_busno = data & 0xff;
268 debug("Primary bus number was changed to %d\n",
269 pcie->first_busno);
270 }
271 /* mvebu has its own way how to set PCI secondary bus number */
272 if (offset == PCI_SECONDARY_BUS ||
273 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
274 pcie->sec_busno = (data >> 8) & 0xff;
275 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
276 debug("Secondary bus number was changed to %d\n",
277 pcie->sec_busno);
278 }
279 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200280 }
281
282 /*
283 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
284 * secondary bus with device number 1.
285 */
286 if (busno == pcie->first_busno)
287 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
288 else
289 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
290
Marek Behún241d7632021-02-08 23:01:38 +0100291 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200292 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100293
294 /* write data */
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200295 switch (size) {
296 case PCI_SIZE_8:
297 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
298 break;
299 case PCI_SIZE_16:
300 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
301 break;
302 case PCI_SIZE_32:
303 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
304 break;
305 default:
306 return -EINVAL;
307 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200308
309 return 0;
310}
311
312/*
313 * Setup PCIE BARs and Address Decode Wins:
Pali Rohár4a1a5932021-11-11 16:35:42 +0100314 * BAR[0] -> internal registers
315 * BAR[1] -> covers all DRAM banks
316 * BAR[2] -> disabled
Anton Schubert9c28d612015-08-11 11:54:01 +0200317 * WIN[0-3] -> DRAM bank[0-3]
318 */
319static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
320{
321 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
322 u32 size;
323 int i;
324
325 /* First, disable and clear BARs and windows. */
326 for (i = 1; i < 3; i++) {
327 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
328 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
329 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
330 }
331
332 for (i = 0; i < 5; i++) {
333 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
334 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
335 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
336 }
337
338 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
339 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
340 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
341
342 /* Setup windows for DDR banks. Count total DDR size on the fly. */
343 size = 0;
344 for (i = 0; i < dram->num_cs; i++) {
345 const struct mbus_dram_window *cs = dram->cs + i;
346
347 writel(cs->base & 0xffff0000,
348 pcie->base + PCIE_WIN04_BASE_OFF(i));
349 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
350 writel(((cs->size - 1) & 0xffff0000) |
351 (cs->mbus_attr << 8) |
352 (dram->mbus_dram_target_id << 4) | 1,
353 pcie->base + PCIE_WIN04_CTRL_OFF(i));
354
355 size += cs->size;
356 }
357
358 /* Round up 'size' to the nearest power of two. */
359 if ((size & (size - 1)) != 0)
360 size = 1 << fls(size);
361
362 /* Setup BAR[1] to all DRAM banks. */
363 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
364 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
365 writel(((size - 1) & 0xffff0000) | 0x1,
366 pcie->base + PCIE_BAR_CTRL_OFF(1));
Pali Rohár4a1a5932021-11-11 16:35:42 +0100367
368 /* Setup BAR[0] to internal registers. */
369 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
370 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
Anton Schubert9c28d612015-08-11 11:54:01 +0200371}
372
Stefan Roese94f453e2019-01-25 11:52:43 +0100373static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200374{
Simon Glassc69cda22020-12-03 16:55:20 -0700375 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100376 struct udevice *ctlr = pci_get_controller(dev);
377 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert9c28d612015-08-11 11:54:01 +0200378 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200379
Pali Rohár2344a762021-10-22 16:22:14 +0200380 /* Setup PCIe controller to Root Complex mode */
381 reg = readl(pcie->base + PCIE_CTRL_OFF);
382 reg |= PCIE_CTRL_RC_MODE;
383 writel(reg, pcie->base + PCIE_CTRL_OFF);
384
Pali Rohára7b61ab2021-10-22 16:22:10 +0200385 /*
386 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
387 * because default value is Memory controller (0x508000) which
388 * U-Boot cannot recognize as P2P Bridge.
389 *
390 * Note that this mvebu PCI Bridge does not have compliant Type 1
Pali Rohára48e4282021-11-11 16:35:45 +0100391 * Configuration Space. Header Type is reported as Type 0 and it
392 * has format of Type 0 config space.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200393 *
Pali Rohára48e4282021-11-11 16:35:45 +0100394 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
395 * have the same format in Marvell's specification as in PCIe
396 * specification, but their meaning is totally different and they do
397 * different things: they are aliased into internal mvebu registers
398 * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or
399 * reconfigured by pci device drivers.
400 *
401 * So our driver converts Type 0 config space to Type 1 and reports
402 * Header Type as Type 1. Access to BAR registers and to non-existent
403 * Type 1 registers is redirected to the virtual cfgcache[] buffer,
404 * which avoids changing unrelated registers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200405 */
406 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
407 reg &= ~0xffffff00;
408 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
409 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200410
Pali Rohára7b61ab2021-10-22 16:22:10 +0200411 /*
412 * mvebu uses local bus number and local device number to determinate
413 * type of config request. Type 0 is used if target bus number equals
414 * local bus number and target device number differs from local device
415 * number. Type 1 is used if target bus number differs from local bus
416 * number. And when target bus number equals local bus number and
417 * target device equals local device number then request is routed to
418 * PCI Bridge which represent local PCIe Root Port.
419 *
420 * It means that PCI primary and secondary buses shares one bus number
421 * which is configured via local bus number. Determination if config
422 * request should go to primary or secondary bus is done based on local
423 * device number.
424 *
425 * PCIe is point-to-point bus, so at secondary bus is always exactly one
426 * device with number 0. So set local device number to 1, it would not
427 * conflict with any device on secondary bus number and will ensure that
428 * accessing secondary bus and all buses behind secondary would work
429 * automatically and correctly. Therefore this configuration of local
430 * device number implies that setting of local bus number configures
431 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
432 * later configure it via config write requests to the correct value.
433 * mvebu_pcie_write_config() catches config write requests which tries
434 * to change primary/secondary bus number and correctly updates local
435 * bus number based on new secondary bus number.
436 *
437 * With this configuration is PCI Bridge available at secondary bus as
438 * device number 1. But it must be available at primary bus as device
439 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
440 * functions rewrite address to the real one when accessing primary bus.
441 */
442 mvebu_pcie_set_local_bus_nr(pcie, 0);
443 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200444
Stefan Roese94f453e2019-01-25 11:52:43 +0100445 pcie->mem.start = (u32)mvebu_pcie_membase;
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100446 pcie->mem.end = pcie->mem.start + MBUS_PCI_MEM_SIZE - 1;
447 mvebu_pcie_membase += MBUS_PCI_MEM_SIZE;
Stefan Roese94f453e2019-01-25 11:52:43 +0100448
449 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
450 (phys_addr_t)pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100451 resource_size(&pcie->mem))) {
Stefan Roese94f453e2019-01-25 11:52:43 +0100452 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
Pali Roháre1cee892021-11-11 16:35:43 +0100453 (u32)pcie->mem.start, (unsigned)resource_size(&pcie->mem));
Stefan Roese94f453e2019-01-25 11:52:43 +0100454 }
455
Phil Sutterba8ae032021-01-03 23:06:46 +0100456 pcie->io.start = (u32)mvebu_pcie_iobase;
457 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
458 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
459
460 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
461 (phys_addr_t)pcie->io.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100462 resource_size(&pcie->io))) {
Phil Sutterba8ae032021-01-03 23:06:46 +0100463 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
Pali Roháre1cee892021-11-11 16:35:43 +0100464 (u32)pcie->io.start, (unsigned)resource_size(&pcie->io));
Phil Sutterba8ae032021-01-03 23:06:46 +0100465 }
466
Stefan Roese94f453e2019-01-25 11:52:43 +0100467 /* Setup windows and configure host bridge */
468 mvebu_pcie_setup_wins(pcie);
469
Stefan Roese94f453e2019-01-25 11:52:43 +0100470 /* PCI memory space */
471 pci_set_region(hose->regions + 0, pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100472 pcie->mem.start, resource_size(&pcie->mem), PCI_REGION_MEM);
Stefan Roese94f453e2019-01-25 11:52:43 +0100473 pci_set_region(hose->regions + 1,
474 0, 0,
475 gd->ram_size,
476 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100477 pci_set_region(hose->regions + 2, pcie->io.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100478 pcie->io.start, resource_size(&pcie->io), PCI_REGION_IO);
Phil Sutterba8ae032021-01-03 23:06:46 +0100479 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100480
Pali Rohára7b61ab2021-10-22 16:22:10 +0200481 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
482 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
483 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
484 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
485 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
486
Stefan Roese94f453e2019-01-25 11:52:43 +0100487 return 0;
488}
489
490static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
491{
492 const u32 *addr;
493 int len;
494
495 addr = ofnode_get_property(node, "assigned-addresses", &len);
496 if (!addr) {
497 pr_err("property \"assigned-addresses\" not found");
498 return -FDT_ERR_NOTFOUND;
499 }
500
501 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
502
503 return 0;
504}
505
506#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
507#define DT_TYPE_IO 0x1
508#define DT_TYPE_MEM32 0x2
509#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
510#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
511
512static int mvebu_get_tgt_attr(ofnode node, int devfn,
513 unsigned long type,
514 unsigned int *tgt,
515 unsigned int *attr)
516{
517 const int na = 3, ns = 2;
518 const __be32 *range;
519 int rlen, nranges, rangesz, pna, i;
520
521 *tgt = -1;
522 *attr = -1;
523
524 range = ofnode_get_property(node, "ranges", &rlen);
525 if (!range)
526 return -EINVAL;
527
Stefan Roese0df62e82019-02-11 07:53:34 +0100528 /*
529 * Linux uses of_n_addr_cells() to get the number of address cells
530 * here. Currently this function is only available in U-Boot when
531 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
532 * general, lets't hardcode the "pna" value in the U-Boot code.
533 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100534 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
535 rangesz = pna + na + ns;
536 nranges = rlen / sizeof(__be32) / rangesz;
537
538 for (i = 0; i < nranges; i++, range += rangesz) {
539 u32 flags = of_read_number(range, 1);
540 u32 slot = of_read_number(range + 1, 1);
541 u64 cpuaddr = of_read_number(range + na, pna);
542 unsigned long rtype;
543
544 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
545 rtype = IORESOURCE_IO;
546 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
547 rtype = IORESOURCE_MEM;
548 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200549 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200550
Stefan Roese94f453e2019-01-25 11:52:43 +0100551 /*
552 * The Linux code used PCI_SLOT() here, which expects devfn
553 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
554 * only expects devfn in 15..8, where its saved in this driver.
555 */
556 if (slot == PCI_DEV(devfn) && type == rtype) {
557 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
558 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
559 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100560 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200561 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100562
563 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200564}
Stefan Roese94f453e2019-01-25 11:52:43 +0100565
Simon Glassd1998a92020-12-03 16:55:21 -0700566static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100567{
Simon Glassc69cda22020-12-03 16:55:20 -0700568 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100569 int ret = 0;
570
571 /* Get port number, lane number and memory target / attr */
572 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
573 &pcie->port)) {
574 ret = -ENODEV;
575 goto err;
576 }
577
578 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
579 pcie->lane = 0;
580
581 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
582
583 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
584 pcie->devfn = pci_get_devfn(dev);
585 if (pcie->devfn < 0) {
586 ret = -ENODEV;
587 goto err;
588 }
589
590 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
591 IORESOURCE_MEM,
592 &pcie->mem_target, &pcie->mem_attr);
593 if (ret < 0) {
594 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
595 goto err;
596 }
597
Phil Sutterba8ae032021-01-03 23:06:46 +0100598 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
599 IORESOURCE_IO,
600 &pcie->io_target, &pcie->io_attr);
601 if (ret < 0) {
602 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
603 goto err;
604 }
605
Stefan Roese94f453e2019-01-25 11:52:43 +0100606 /* Parse PCIe controller register base from DT */
607 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
608 if (ret < 0)
609 goto err;
610
Stefan Roese94f453e2019-01-25 11:52:43 +0100611 return 0;
612
613err:
614 return ret;
615}
616
617static const struct dm_pci_ops mvebu_pcie_ops = {
618 .read_config = mvebu_pcie_read_config,
619 .write_config = mvebu_pcie_write_config,
620};
621
622static struct driver pcie_mvebu_drv = {
623 .name = "pcie_mvebu",
624 .id = UCLASS_PCI,
625 .ops = &mvebu_pcie_ops,
626 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700627 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700628 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100629};
630
631/*
632 * Use a MISC device to bind the n instances (child nodes) of the
633 * PCIe base controller in UCLASS_PCI.
634 */
635static int mvebu_pcie_bind(struct udevice *parent)
636{
637 struct mvebu_pcie *pcie;
638 struct uclass_driver *drv;
639 struct udevice *dev;
640 ofnode subnode;
641
Pali Rohár03a8a5e2021-10-22 16:22:15 +0200642 /* Lookup pci driver */
Stefan Roese94f453e2019-01-25 11:52:43 +0100643 drv = lists_uclass_lookup(UCLASS_PCI);
644 if (!drv) {
645 puts("Cannot find PCI driver\n");
646 return -ENOENT;
647 }
648
649 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
650 if (!ofnode_is_available(subnode))
651 continue;
652
653 pcie = calloc(1, sizeof(*pcie));
654 if (!pcie)
655 return -ENOMEM;
656
657 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700658 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
659 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100660 }
661
662 return 0;
663}
664
665static const struct udevice_id mvebu_pcie_ids[] = {
666 { .compatible = "marvell,armada-xp-pcie" },
667 { .compatible = "marvell,armada-370-pcie" },
668 { }
669};
670
671U_BOOT_DRIVER(pcie_mvebu_base) = {
672 .name = "pcie_mvebu_base",
673 .id = UCLASS_MISC,
674 .of_match = mvebu_pcie_ids,
675 .bind = mvebu_pcie_bind,
676};