blob: 18f79d249c7f081c5ac167806cd697cc9563c896 [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
Anton Schubert9c28d612015-08-11 11:54:01 +020049#define PCIE_CONF_DATA_OFF 0x18fc
50#define PCIE_MASK_OFF 0x1910
51#define PCIE_MASK_ENABLE_INTS (0xf << 24)
52#define PCIE_CTRL_OFF 0x1a00
53#define PCIE_CTRL_X1_MODE BIT(0)
Pali Rohár2344a762021-10-22 16:22:14 +020054#define PCIE_CTRL_RC_MODE BIT(1)
Anton Schubert9c28d612015-08-11 11:54:01 +020055#define PCIE_STAT_OFF 0x1a04
56#define PCIE_STAT_BUS (0xff << 8)
57#define PCIE_STAT_DEV (0x1f << 16)
58#define PCIE_STAT_LINK_DOWN BIT(0)
59#define PCIE_DEBUG_CTRL 0x1a60
60#define PCIE_DEBUG_SOFT_RESET BIT(20)
61
Anton Schubert9c28d612015-08-11 11:54:01 +020062struct mvebu_pcie {
63 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020064 void __iomem *base;
65 void __iomem *membase;
66 struct resource mem;
67 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010068 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020069 u32 port;
70 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010071 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020072 u32 lane_mask;
Marek Behún10eb2cc2021-02-08 23:01:40 +010073 int first_busno;
Pali Rohára7b61ab2021-10-22 16:22:10 +020074 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010075 char name[16];
76 unsigned int mem_target;
77 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010078 unsigned int io_target;
79 unsigned int io_attr;
Pali Rohára48e4282021-11-11 16:35:45 +010080 u32 cfgcache[(0x3c - 0x10) / 4];
Anton Schubert9c28d612015-08-11 11:54:01 +020081};
82
Anton Schubert9c28d612015-08-11 11:54:01 +020083/*
84 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030085 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020086 * and 64K of I/O space when registered.
87 */
88static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
Phil Sutterba8ae032021-01-03 23:06:46 +010089static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +020090
Anton Schubert9c28d612015-08-11 11:54:01 +020091static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
92{
93 u32 val;
94 val = readl(pcie->base + PCIE_STAT_OFF);
95 return !(val & PCIE_STAT_LINK_DOWN);
96}
97
98static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
99{
100 u32 stat;
101
102 stat = readl(pcie->base + PCIE_STAT_OFF);
103 stat &= ~PCIE_STAT_BUS;
104 stat |= busno << 8;
105 writel(stat, pcie->base + PCIE_STAT_OFF);
106}
107
108static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
109{
110 u32 stat;
111
112 stat = readl(pcie->base + PCIE_STAT_OFF);
113 stat &= ~PCIE_STAT_DEV;
114 stat |= devno << 16;
115 writel(stat, pcie->base + PCIE_STAT_OFF);
116}
117
Anton Schubert9c28d612015-08-11 11:54:01 +0200118static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
119{
120 return container_of(hose, struct mvebu_pcie, hose);
121}
122
Pali Rohára7b61ab2021-10-22 16:22:10 +0200123static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
124 int busno, int dev, int func)
Marek Behún10eb2cc2021-02-08 23:01:40 +0100125{
Pali Rohára7b61ab2021-10-22 16:22:10 +0200126 /* On primary bus is only one PCI Bridge */
127 if (busno == pcie->first_busno && (dev != 0 || func != 0))
128 return false;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100129
Pali Rohár79b4eb22021-10-22 16:22:12 +0200130 /* Access to other buses is possible when link is up */
131 if (busno != pcie->first_busno && !mvebu_pcie_link_up(pcie))
132 return false;
133
Pali Rohára7b61ab2021-10-22 16:22:10 +0200134 /* On secondary bus can be only one PCIe device */
135 if (busno == pcie->sec_busno && dev != 0)
136 return false;
137
138 return true;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100139}
140
Simon Glassc4e72c42020-01-27 08:49:37 -0700141static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100142 uint offset, ulong *valuep,
143 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200144{
Simon Glassc69cda22020-12-03 16:55:20 -0700145 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200146 int busno = PCI_BUS(bdf) - dev_seq(bus);
147 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100148
Marek Behún10eb2cc2021-02-08 23:01:40 +0100149 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
150 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200151
Pali Rohára7b61ab2021-10-22 16:22:10 +0200152 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100153 debug("- out of range\n");
154 *valuep = pci_get_ff(size);
155 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200156 }
157
Pali Rohára7b61ab2021-10-22 16:22:10 +0200158 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100159 * The configuration space of the PCI Bridge on primary (first) bus is
160 * of Type 0 but the BAR registers (including ROM BAR) don't have the
161 * same meaning as in the PCIe specification. Therefore do not access
162 * BAR registers and non-common registers (those which have different
163 * meaning for Type 0 and Type 1 config space) of the PCI Bridge and
164 * instead read their content from driver virtual cfgcache[].
Pali Rohára7b61ab2021-10-22 16:22:10 +0200165 */
Pali Rohára48e4282021-11-11 16:35:45 +0100166 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
167 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200168 data = pcie->cfgcache[(offset - 0x10) / 4];
169 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
170 offset, size, data);
171 *valuep = pci_conv_32_to_size(data, offset, size);
172 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200173 }
174
175 /*
176 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
177 * secondary bus with device number 1.
178 */
179 if (busno == pcie->first_busno)
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100180 addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200181 else
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100182 addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200183
Anton Schubert9c28d612015-08-11 11:54:01 +0200184 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200185 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100186
187 /* read data */
Pali Rohár657177a2021-10-22 16:22:09 +0200188 switch (size) {
189 case PCI_SIZE_8:
190 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
191 break;
192 case PCI_SIZE_16:
193 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
194 break;
195 case PCI_SIZE_32:
196 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
197 break;
198 default:
199 return -EINVAL;
200 }
201
Pali Rohára7b61ab2021-10-22 16:22:10 +0200202 if (busno == pcie->first_busno &&
203 (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
204 /*
205 * Change Header Type of PCI Bridge device to Type 1
206 * (0x01, used by PCI Bridges) because mvebu reports
207 * Type 0 (0x00, used by Upstream and Endpoint devices).
208 */
209 data = pci_conv_size_to_32(data, 0, offset, size);
210 data &= ~0x007f0000;
211 data |= PCI_HEADER_TYPE_BRIDGE << 16;
212 data = pci_conv_32_to_size(data, offset, size);
213 }
214
Marek Behún26f7a762021-02-08 23:01:39 +0100215 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár657177a2021-10-22 16:22:09 +0200216 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200217
218 return 0;
219}
220
Stefan Roese94f453e2019-01-25 11:52:43 +0100221static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
222 uint offset, ulong value,
223 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200224{
Simon Glassc69cda22020-12-03 16:55:20 -0700225 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200226 int busno = PCI_BUS(bdf) - dev_seq(bus);
227 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100228
Marek Behún10eb2cc2021-02-08 23:01:40 +0100229 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
230 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100231 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200232
Pali Rohára7b61ab2021-10-22 16:22:10 +0200233 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100234 debug("- out of range\n");
235 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200236 }
237
Pali Rohára7b61ab2021-10-22 16:22:10 +0200238 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100239 * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific
240 * config registers are not available, so we write their content only
241 * into driver virtual cfgcache[].
242 * And as explained in mvebu_pcie_probe(), mvebu has its own specific
243 * way for configuring primary and secondary bus numbers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200244 */
Pali Rohára48e4282021-11-11 16:35:45 +0100245 if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) ||
246 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200247 debug("Writing to cfgcache only\n");
248 data = pcie->cfgcache[(offset - 0x10) / 4];
249 data = pci_conv_size_to_32(data, value, offset, size);
250 /* mvebu PCI bridge does not have configurable bars */
251 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
Pali Rohára48e4282021-11-11 16:35:45 +0100252 (offset & ~3) == PCI_BASE_ADDRESS_1 ||
253 (offset & ~3) == PCI_ROM_ADDRESS1)
Pali Rohára7b61ab2021-10-22 16:22:10 +0200254 data = 0x0;
255 pcie->cfgcache[(offset - 0x10) / 4] = data;
256 /* mvebu has its own way how to set PCI primary bus number */
257 if (offset == PCI_PRIMARY_BUS) {
258 pcie->first_busno = data & 0xff;
259 debug("Primary bus number was changed to %d\n",
260 pcie->first_busno);
261 }
262 /* mvebu has its own way how to set PCI secondary bus number */
263 if (offset == PCI_SECONDARY_BUS ||
264 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
265 pcie->sec_busno = (data >> 8) & 0xff;
266 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
267 debug("Secondary bus number was changed to %d\n",
268 pcie->sec_busno);
269 }
270 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200271 }
272
273 /*
274 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
275 * secondary bus with device number 1.
276 */
277 if (busno == pcie->first_busno)
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100278 addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200279 else
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100280 addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200281
Marek Behún241d7632021-02-08 23:01:38 +0100282 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200283 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100284
285 /* write data */
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200286 switch (size) {
287 case PCI_SIZE_8:
288 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
289 break;
290 case PCI_SIZE_16:
291 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
292 break;
293 case PCI_SIZE_32:
294 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
295 break;
296 default:
297 return -EINVAL;
298 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200299
300 return 0;
301}
302
303/*
304 * Setup PCIE BARs and Address Decode Wins:
Pali Rohár4a1a5932021-11-11 16:35:42 +0100305 * BAR[0] -> internal registers
306 * BAR[1] -> covers all DRAM banks
307 * BAR[2] -> disabled
Anton Schubert9c28d612015-08-11 11:54:01 +0200308 * WIN[0-3] -> DRAM bank[0-3]
309 */
310static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
311{
312 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
313 u32 size;
314 int i;
315
316 /* First, disable and clear BARs and windows. */
317 for (i = 1; i < 3; i++) {
318 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
319 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
320 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
321 }
322
323 for (i = 0; i < 5; i++) {
324 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
325 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
326 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
327 }
328
329 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
330 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
331 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
332
333 /* Setup windows for DDR banks. Count total DDR size on the fly. */
334 size = 0;
335 for (i = 0; i < dram->num_cs; i++) {
336 const struct mbus_dram_window *cs = dram->cs + i;
337
338 writel(cs->base & 0xffff0000,
339 pcie->base + PCIE_WIN04_BASE_OFF(i));
340 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
341 writel(((cs->size - 1) & 0xffff0000) |
342 (cs->mbus_attr << 8) |
343 (dram->mbus_dram_target_id << 4) | 1,
344 pcie->base + PCIE_WIN04_CTRL_OFF(i));
345
346 size += cs->size;
347 }
348
349 /* Round up 'size' to the nearest power of two. */
350 if ((size & (size - 1)) != 0)
351 size = 1 << fls(size);
352
353 /* Setup BAR[1] to all DRAM banks. */
354 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
355 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
356 writel(((size - 1) & 0xffff0000) | 0x1,
357 pcie->base + PCIE_BAR_CTRL_OFF(1));
Pali Rohár4a1a5932021-11-11 16:35:42 +0100358
359 /* Setup BAR[0] to internal registers. */
360 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
361 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
Anton Schubert9c28d612015-08-11 11:54:01 +0200362}
363
Stefan Roese94f453e2019-01-25 11:52:43 +0100364static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200365{
Simon Glassc69cda22020-12-03 16:55:20 -0700366 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100367 struct udevice *ctlr = pci_get_controller(dev);
368 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert9c28d612015-08-11 11:54:01 +0200369 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200370
Pali Rohár2344a762021-10-22 16:22:14 +0200371 /* Setup PCIe controller to Root Complex mode */
372 reg = readl(pcie->base + PCIE_CTRL_OFF);
373 reg |= PCIE_CTRL_RC_MODE;
374 writel(reg, pcie->base + PCIE_CTRL_OFF);
375
Pali Rohára7b61ab2021-10-22 16:22:10 +0200376 /*
377 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
378 * because default value is Memory controller (0x508000) which
379 * U-Boot cannot recognize as P2P Bridge.
380 *
381 * Note that this mvebu PCI Bridge does not have compliant Type 1
Pali Rohára48e4282021-11-11 16:35:45 +0100382 * Configuration Space. Header Type is reported as Type 0 and it
383 * has format of Type 0 config space.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200384 *
Pali Rohára48e4282021-11-11 16:35:45 +0100385 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
386 * have the same format in Marvell's specification as in PCIe
387 * specification, but their meaning is totally different and they do
388 * different things: they are aliased into internal mvebu registers
389 * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or
390 * reconfigured by pci device drivers.
391 *
392 * So our driver converts Type 0 config space to Type 1 and reports
393 * Header Type as Type 1. Access to BAR registers and to non-existent
394 * Type 1 registers is redirected to the virtual cfgcache[] buffer,
395 * which avoids changing unrelated registers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200396 */
397 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
398 reg &= ~0xffffff00;
399 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
400 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200401
Pali Rohára7b61ab2021-10-22 16:22:10 +0200402 /*
403 * mvebu uses local bus number and local device number to determinate
404 * type of config request. Type 0 is used if target bus number equals
405 * local bus number and target device number differs from local device
406 * number. Type 1 is used if target bus number differs from local bus
407 * number. And when target bus number equals local bus number and
408 * target device equals local device number then request is routed to
409 * PCI Bridge which represent local PCIe Root Port.
410 *
411 * It means that PCI primary and secondary buses shares one bus number
412 * which is configured via local bus number. Determination if config
413 * request should go to primary or secondary bus is done based on local
414 * device number.
415 *
416 * PCIe is point-to-point bus, so at secondary bus is always exactly one
417 * device with number 0. So set local device number to 1, it would not
418 * conflict with any device on secondary bus number and will ensure that
419 * accessing secondary bus and all buses behind secondary would work
420 * automatically and correctly. Therefore this configuration of local
421 * device number implies that setting of local bus number configures
422 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
423 * later configure it via config write requests to the correct value.
424 * mvebu_pcie_write_config() catches config write requests which tries
425 * to change primary/secondary bus number and correctly updates local
426 * bus number based on new secondary bus number.
427 *
428 * With this configuration is PCI Bridge available at secondary bus as
429 * device number 1. But it must be available at primary bus as device
430 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
431 * functions rewrite address to the real one when accessing primary bus.
432 */
433 mvebu_pcie_set_local_bus_nr(pcie, 0);
434 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200435
Stefan Roese94f453e2019-01-25 11:52:43 +0100436 pcie->mem.start = (u32)mvebu_pcie_membase;
Pali Rohárcbf0d3a2021-11-06 12:16:12 +0100437 pcie->mem.end = pcie->mem.start + MBUS_PCI_MEM_SIZE - 1;
438 mvebu_pcie_membase += MBUS_PCI_MEM_SIZE;
Stefan Roese94f453e2019-01-25 11:52:43 +0100439
440 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
441 (phys_addr_t)pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100442 resource_size(&pcie->mem))) {
Stefan Roese94f453e2019-01-25 11:52:43 +0100443 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
Pali Roháre1cee892021-11-11 16:35:43 +0100444 (u32)pcie->mem.start, (unsigned)resource_size(&pcie->mem));
Stefan Roese94f453e2019-01-25 11:52:43 +0100445 }
446
Phil Sutterba8ae032021-01-03 23:06:46 +0100447 pcie->io.start = (u32)mvebu_pcie_iobase;
448 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
449 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
450
451 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
452 (phys_addr_t)pcie->io.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100453 resource_size(&pcie->io))) {
Phil Sutterba8ae032021-01-03 23:06:46 +0100454 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
Pali Roháre1cee892021-11-11 16:35:43 +0100455 (u32)pcie->io.start, (unsigned)resource_size(&pcie->io));
Phil Sutterba8ae032021-01-03 23:06:46 +0100456 }
457
Stefan Roese94f453e2019-01-25 11:52:43 +0100458 /* Setup windows and configure host bridge */
459 mvebu_pcie_setup_wins(pcie);
460
Stefan Roese94f453e2019-01-25 11:52:43 +0100461 /* PCI memory space */
462 pci_set_region(hose->regions + 0, pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100463 pcie->mem.start, resource_size(&pcie->mem), PCI_REGION_MEM);
Stefan Roese94f453e2019-01-25 11:52:43 +0100464 pci_set_region(hose->regions + 1,
465 0, 0,
466 gd->ram_size,
467 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100468 pci_set_region(hose->regions + 2, pcie->io.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100469 pcie->io.start, resource_size(&pcie->io), PCI_REGION_IO);
Phil Sutterba8ae032021-01-03 23:06:46 +0100470 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100471
Pali Rohára7b61ab2021-10-22 16:22:10 +0200472 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
473 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
474 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
475 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
476 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
477
Stefan Roese94f453e2019-01-25 11:52:43 +0100478 return 0;
479}
480
481static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
482{
483 const u32 *addr;
484 int len;
485
486 addr = ofnode_get_property(node, "assigned-addresses", &len);
487 if (!addr) {
488 pr_err("property \"assigned-addresses\" not found");
489 return -FDT_ERR_NOTFOUND;
490 }
491
492 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
493
494 return 0;
495}
496
497#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
498#define DT_TYPE_IO 0x1
499#define DT_TYPE_MEM32 0x2
500#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
501#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
502
503static int mvebu_get_tgt_attr(ofnode node, int devfn,
504 unsigned long type,
505 unsigned int *tgt,
506 unsigned int *attr)
507{
508 const int na = 3, ns = 2;
509 const __be32 *range;
510 int rlen, nranges, rangesz, pna, i;
511
512 *tgt = -1;
513 *attr = -1;
514
515 range = ofnode_get_property(node, "ranges", &rlen);
516 if (!range)
517 return -EINVAL;
518
Stefan Roese0df62e82019-02-11 07:53:34 +0100519 /*
520 * Linux uses of_n_addr_cells() to get the number of address cells
521 * here. Currently this function is only available in U-Boot when
522 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
523 * general, lets't hardcode the "pna" value in the U-Boot code.
524 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100525 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
526 rangesz = pna + na + ns;
527 nranges = rlen / sizeof(__be32) / rangesz;
528
529 for (i = 0; i < nranges; i++, range += rangesz) {
530 u32 flags = of_read_number(range, 1);
531 u32 slot = of_read_number(range + 1, 1);
532 u64 cpuaddr = of_read_number(range + na, pna);
533 unsigned long rtype;
534
535 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
536 rtype = IORESOURCE_IO;
537 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
538 rtype = IORESOURCE_MEM;
539 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200540 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200541
Stefan Roese94f453e2019-01-25 11:52:43 +0100542 /*
543 * The Linux code used PCI_SLOT() here, which expects devfn
544 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
545 * only expects devfn in 15..8, where its saved in this driver.
546 */
547 if (slot == PCI_DEV(devfn) && type == rtype) {
548 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
549 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
550 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100551 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200552 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100553
554 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200555}
Stefan Roese94f453e2019-01-25 11:52:43 +0100556
Simon Glassd1998a92020-12-03 16:55:21 -0700557static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100558{
Simon Glassc69cda22020-12-03 16:55:20 -0700559 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100560 int ret = 0;
561
562 /* Get port number, lane number and memory target / attr */
563 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
564 &pcie->port)) {
565 ret = -ENODEV;
566 goto err;
567 }
568
569 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
570 pcie->lane = 0;
571
572 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
573
574 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
575 pcie->devfn = pci_get_devfn(dev);
576 if (pcie->devfn < 0) {
577 ret = -ENODEV;
578 goto err;
579 }
580
581 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
582 IORESOURCE_MEM,
583 &pcie->mem_target, &pcie->mem_attr);
584 if (ret < 0) {
585 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
586 goto err;
587 }
588
Phil Sutterba8ae032021-01-03 23:06:46 +0100589 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
590 IORESOURCE_IO,
591 &pcie->io_target, &pcie->io_attr);
592 if (ret < 0) {
593 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
594 goto err;
595 }
596
Stefan Roese94f453e2019-01-25 11:52:43 +0100597 /* Parse PCIe controller register base from DT */
598 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
599 if (ret < 0)
600 goto err;
601
Stefan Roese94f453e2019-01-25 11:52:43 +0100602 return 0;
603
604err:
605 return ret;
606}
607
608static const struct dm_pci_ops mvebu_pcie_ops = {
609 .read_config = mvebu_pcie_read_config,
610 .write_config = mvebu_pcie_write_config,
611};
612
613static struct driver pcie_mvebu_drv = {
614 .name = "pcie_mvebu",
615 .id = UCLASS_PCI,
616 .ops = &mvebu_pcie_ops,
617 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700618 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700619 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100620};
621
622/*
623 * Use a MISC device to bind the n instances (child nodes) of the
624 * PCIe base controller in UCLASS_PCI.
625 */
626static int mvebu_pcie_bind(struct udevice *parent)
627{
628 struct mvebu_pcie *pcie;
629 struct uclass_driver *drv;
630 struct udevice *dev;
631 ofnode subnode;
632
Pali Rohár03a8a5e2021-10-22 16:22:15 +0200633 /* Lookup pci driver */
Stefan Roese94f453e2019-01-25 11:52:43 +0100634 drv = lists_uclass_lookup(UCLASS_PCI);
635 if (!drv) {
636 puts("Cannot find PCI driver\n");
637 return -ENOENT;
638 }
639
640 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
641 if (!ofnode_is_available(subnode))
642 continue;
643
644 pcie = calloc(1, sizeof(*pcie));
645 if (!pcie)
646 return -ENOMEM;
647
648 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700649 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
650 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100651 }
652
653 return 0;
654}
655
656static const struct udevice_id mvebu_pcie_ids[] = {
657 { .compatible = "marvell,armada-xp-pcie" },
658 { .compatible = "marvell,armada-370-pcie" },
659 { }
660};
661
662U_BOOT_DRIVER(pcie_mvebu_base) = {
663 .name = "pcie_mvebu_base",
664 .id = UCLASS_MISC,
665 .of_match = mvebu_pcie_ids,
666 .bind = mvebu_pcie_bind,
667};