blob: 4cb237d2c45ea0c929a11e9f2651cb02ab5537ff [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Anton Schubert9c28d612015-08-11 11:54:01 +02002/*
3 * PCIe driver for Marvell MVEBU SoCs
4 *
5 * Based on Barebox drivers/pci/pci-mvebu.c
6 *
7 * Ported to U-Boot by:
8 * Anton Schubert <anton.schubert@gmx.de>
9 * Stefan Roese <sr@denx.de>
Anton Schubert9c28d612015-08-11 11:54:01 +020010 */
11
12#include <common.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010013#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010017#include <dm/device-internal.h>
18#include <dm/lists.h>
19#include <dm/of_access.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020020#include <pci.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020021#include <asm/io.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/soc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010025#include <linux/errno.h>
26#include <linux/ioport.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020027#include <linux/mbus.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31/* PCIe unit register offsets */
32#define SELECT(x, n) ((x >> n) & 1UL)
33
34#define PCIE_DEV_ID_OFF 0x0000
35#define PCIE_CMD_OFF 0x0004
36#define PCIE_DEV_REV_OFF 0x0008
37#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
38#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
Pali Rohára7b61ab2021-10-22 16:22:10 +020039#define PCIE_EXP_ROM_BAR_OFF 0x0030
Anton Schubert9c28d612015-08-11 11:54:01 +020040#define PCIE_CAPAB_OFF 0x0060
41#define PCIE_CTRL_STAT_OFF 0x0068
42#define PCIE_HEADER_LOG_4_OFF 0x0128
43#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
44#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
45#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
46#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
47#define PCIE_WIN5_CTRL_OFF 0x1880
48#define PCIE_WIN5_BASE_OFF 0x1884
49#define PCIE_WIN5_REMAP_OFF 0x188c
50#define PCIE_CONF_ADDR_OFF 0x18f8
51#define PCIE_CONF_ADDR_EN BIT(31)
52#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
53#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
54#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
55#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
Pali Rohára7b61ab2021-10-22 16:22:10 +020056#define PCIE_CONF_ADDR(b, d, f, reg) \
57 (PCIE_CONF_BUS(b) | PCIE_CONF_DEV(d) | \
58 PCIE_CONF_FUNC(f) | PCIE_CONF_REG(reg) | \
Anton Schubert9c28d612015-08-11 11:54:01 +020059 PCIE_CONF_ADDR_EN)
60#define PCIE_CONF_DATA_OFF 0x18fc
61#define PCIE_MASK_OFF 0x1910
62#define PCIE_MASK_ENABLE_INTS (0xf << 24)
63#define PCIE_CTRL_OFF 0x1a00
64#define PCIE_CTRL_X1_MODE BIT(0)
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 Schubert9c28d612015-08-11 11:54:01 +020072struct mvebu_pcie {
73 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020074 void __iomem *base;
75 void __iomem *membase;
76 struct resource mem;
77 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010078 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020079 u32 port;
80 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010081 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020082 u32 lane_mask;
Marek Behún10eb2cc2021-02-08 23:01:40 +010083 int first_busno;
Pali Rohára7b61ab2021-10-22 16:22:10 +020084 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010085 char name[16];
86 unsigned int mem_target;
87 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010088 unsigned int io_target;
89 unsigned int io_attr;
Pali Rohára7b61ab2021-10-22 16:22:10 +020090 u32 cfgcache[0x34 - 0x10];
Anton Schubert9c28d612015-08-11 11:54:01 +020091};
92
Anton Schubert9c28d612015-08-11 11:54:01 +020093/*
94 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030095 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-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;
VlaoMao49b23e02017-09-22 18:49:02 +030099#define PCIE_MEM_SIZE (128 << 20)
Phil Sutterba8ae032021-01-03 23:06:46 +0100100static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +0200101
Anton Schubert9c28d612015-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
Anton Schubert9c28d612015-08-11 11:54:01 +0200129static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
130{
131 return container_of(hose, struct mvebu_pcie, hose);
132}
133
Pali Rohára7b61ab2021-10-22 16:22:10 +0200134static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
135 int busno, int dev, int func)
Marek Behún10eb2cc2021-02-08 23:01:40 +0100136{
Pali Rohára7b61ab2021-10-22 16:22:10 +0200137 /* On primary bus is only one PCI Bridge */
138 if (busno == pcie->first_busno && (dev != 0 || func != 0))
139 return false;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100140
Pali Rohár79b4eb22021-10-22 16:22:12 +0200141 /* Access to other buses is possible when link is up */
142 if (busno != pcie->first_busno && !mvebu_pcie_link_up(pcie))
143 return false;
144
Pali Rohára7b61ab2021-10-22 16:22:10 +0200145 /* On secondary bus can be only one PCIe device */
146 if (busno == pcie->sec_busno && dev != 0)
147 return false;
148
149 return true;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100150}
151
Simon Glassc4e72c42020-01-27 08:49:37 -0700152static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100153 uint offset, ulong *valuep,
154 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200155{
Simon Glassc69cda22020-12-03 16:55:20 -0700156 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200157 int busno = PCI_BUS(bdf) - dev_seq(bus);
158 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100159
Marek Behún10eb2cc2021-02-08 23:01:40 +0100160 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
161 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200162
Pali Rohára7b61ab2021-10-22 16:22:10 +0200163 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100164 debug("- out of range\n");
165 *valuep = pci_get_ff(size);
166 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200167 }
168
Pali Rohára7b61ab2021-10-22 16:22:10 +0200169 /*
170 * mvebu has different internal registers mapped into PCI config space
171 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
172 * for this range and instead read content from driver virtual cfgcache
173 */
174 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
175 data = pcie->cfgcache[(offset - 0x10) / 4];
176 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
177 offset, size, data);
178 *valuep = pci_conv_32_to_size(data, offset, size);
179 return 0;
180 } else if (busno == pcie->first_busno &&
181 (offset & ~3) == PCI_ROM_ADDRESS1) {
182 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
183 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
184 }
185
186 /*
187 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
188 * secondary bus with device number 1.
189 */
190 if (busno == pcie->first_busno)
191 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
192 else
193 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
194
Anton Schubert9c28d612015-08-11 11:54:01 +0200195 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200196 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100197
198 /* read data */
Pali Rohár657177a2021-10-22 16:22:09 +0200199 switch (size) {
200 case PCI_SIZE_8:
201 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
202 break;
203 case PCI_SIZE_16:
204 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
205 break;
206 case PCI_SIZE_32:
207 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
208 break;
209 default:
210 return -EINVAL;
211 }
212
Pali Rohára7b61ab2021-10-22 16:22:10 +0200213 if (busno == pcie->first_busno &&
214 (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
215 /*
216 * Change Header Type of PCI Bridge device to Type 1
217 * (0x01, used by PCI Bridges) because mvebu reports
218 * Type 0 (0x00, used by Upstream and Endpoint devices).
219 */
220 data = pci_conv_size_to_32(data, 0, offset, size);
221 data &= ~0x007f0000;
222 data |= PCI_HEADER_TYPE_BRIDGE << 16;
223 data = pci_conv_32_to_size(data, offset, size);
224 }
225
Marek Behún26f7a762021-02-08 23:01:39 +0100226 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár657177a2021-10-22 16:22:09 +0200227 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200228
229 return 0;
230}
231
Stefan Roese94f453e2019-01-25 11:52:43 +0100232static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
233 uint offset, ulong value,
234 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200235{
Simon Glassc69cda22020-12-03 16:55:20 -0700236 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200237 int busno = PCI_BUS(bdf) - dev_seq(bus);
238 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100239
Marek Behún10eb2cc2021-02-08 23:01:40 +0100240 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
241 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100242 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200243
Pali Rohára7b61ab2021-10-22 16:22:10 +0200244 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100245 debug("- out of range\n");
246 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200247 }
248
Pali Rohára7b61ab2021-10-22 16:22:10 +0200249 /*
250 * mvebu has different internal registers mapped into PCI config space
251 * in range 0x10-0x34 for PCI bridge, so do not access PCI config space
252 * for this range and instead write content to driver virtual cfgcache
253 */
254 if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) {
255 debug("Writing to cfgcache only\n");
256 data = pcie->cfgcache[(offset - 0x10) / 4];
257 data = pci_conv_size_to_32(data, value, offset, size);
258 /* mvebu PCI bridge does not have configurable bars */
259 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
260 (offset & ~3) == PCI_BASE_ADDRESS_1)
261 data = 0x0;
262 pcie->cfgcache[(offset - 0x10) / 4] = data;
263 /* mvebu has its own way how to set PCI primary bus number */
264 if (offset == PCI_PRIMARY_BUS) {
265 pcie->first_busno = data & 0xff;
266 debug("Primary bus number was changed to %d\n",
267 pcie->first_busno);
268 }
269 /* mvebu has its own way how to set PCI secondary bus number */
270 if (offset == PCI_SECONDARY_BUS ||
271 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
272 pcie->sec_busno = (data >> 8) & 0xff;
273 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
274 debug("Secondary bus number was changed to %d\n",
275 pcie->sec_busno);
276 }
277 return 0;
278 } else if (busno == pcie->first_busno &&
279 (offset & ~3) == PCI_ROM_ADDRESS1) {
280 /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */
281 offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF;
282 }
283
284 /*
285 * PCI bridge is device 0 at primary bus but mvebu has it mapped on
286 * secondary bus with device number 1.
287 */
288 if (busno == pcie->first_busno)
289 addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset);
290 else
291 addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
292
Marek Behún241d7632021-02-08 23:01:38 +0100293 /* write address */
Pali Rohára7b61ab2021-10-22 16:22:10 +0200294 writel(addr, pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100295
296 /* write data */
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200297 switch (size) {
298 case PCI_SIZE_8:
299 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
300 break;
301 case PCI_SIZE_16:
302 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
303 break;
304 case PCI_SIZE_32:
305 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
306 break;
307 default:
308 return -EINVAL;
309 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200310
311 return 0;
312}
313
314/*
315 * Setup PCIE BARs and Address Decode Wins:
316 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
317 * 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));
367}
368
Stefan Roese94f453e2019-01-25 11:52:43 +0100369static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200370{
Simon Glassc69cda22020-12-03 16:55:20 -0700371 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100372 struct udevice *ctlr = pci_get_controller(dev);
373 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Anton Schubert9c28d612015-08-11 11:54:01 +0200374 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200375
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
382 * Configuration Space. Header Type is reported as Type 0 and in
383 * range 0x10-0x34 it has aliased internal mvebu registers 0x10-0x34
384 * (e.g. PCIE_BAR_LO_OFF) and register 0x38 is reserved.
385 *
386 * Driver for this range redirects access to virtual cfgcache[] buffer
387 * which avoids changing internal mvebu registers. And changes Header
388 * Type response value to Type 1.
389 */
390 reg = readl(pcie->base + PCIE_DEV_REV_OFF);
391 reg &= ~0xffffff00;
392 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
393 writel(reg, pcie->base + PCIE_DEV_REV_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200394
Pali Rohára7b61ab2021-10-22 16:22:10 +0200395 /*
396 * mvebu uses local bus number and local device number to determinate
397 * type of config request. Type 0 is used if target bus number equals
398 * local bus number and target device number differs from local device
399 * number. Type 1 is used if target bus number differs from local bus
400 * number. And when target bus number equals local bus number and
401 * target device equals local device number then request is routed to
402 * PCI Bridge which represent local PCIe Root Port.
403 *
404 * It means that PCI primary and secondary buses shares one bus number
405 * which is configured via local bus number. Determination if config
406 * request should go to primary or secondary bus is done based on local
407 * device number.
408 *
409 * PCIe is point-to-point bus, so at secondary bus is always exactly one
410 * device with number 0. So set local device number to 1, it would not
411 * conflict with any device on secondary bus number and will ensure that
412 * accessing secondary bus and all buses behind secondary would work
413 * automatically and correctly. Therefore this configuration of local
414 * device number implies that setting of local bus number configures
415 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
416 * later configure it via config write requests to the correct value.
417 * mvebu_pcie_write_config() catches config write requests which tries
418 * to change primary/secondary bus number and correctly updates local
419 * bus number based on new secondary bus number.
420 *
421 * With this configuration is PCI Bridge available at secondary bus as
422 * device number 1. But it must be available at primary bus as device
423 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
424 * functions rewrite address to the real one when accessing primary bus.
425 */
426 mvebu_pcie_set_local_bus_nr(pcie, 0);
427 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200428
Stefan Roese94f453e2019-01-25 11:52:43 +0100429 pcie->mem.start = (u32)mvebu_pcie_membase;
430 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
431 mvebu_pcie_membase += PCIE_MEM_SIZE;
432
433 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
434 (phys_addr_t)pcie->mem.start,
435 PCIE_MEM_SIZE)) {
436 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
437 (u32)pcie->mem.start, PCIE_MEM_SIZE);
438 }
439
Phil Sutterba8ae032021-01-03 23:06:46 +0100440 pcie->io.start = (u32)mvebu_pcie_iobase;
441 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
442 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
443
444 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
445 (phys_addr_t)pcie->io.start,
446 MBUS_PCI_IO_SIZE)) {
447 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
448 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
449 }
450
Stefan Roese94f453e2019-01-25 11:52:43 +0100451 /* Setup windows and configure host bridge */
452 mvebu_pcie_setup_wins(pcie);
453
454 /* Master + slave enable. */
455 reg = readl(pcie->base + PCIE_CMD_OFF);
456 reg |= PCI_COMMAND_MEMORY;
Phil Sutterba8ae032021-01-03 23:06:46 +0100457 reg |= PCI_COMMAND_IO;
Stefan Roese94f453e2019-01-25 11:52:43 +0100458 reg |= PCI_COMMAND_MASTER;
459 reg |= BIT(10); /* disable interrupts */
460 writel(reg, pcie->base + PCIE_CMD_OFF);
461
Stefan Roese94f453e2019-01-25 11:52:43 +0100462 /* PCI memory space */
463 pci_set_region(hose->regions + 0, pcie->mem.start,
464 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
465 pci_set_region(hose->regions + 1,
466 0, 0,
467 gd->ram_size,
468 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100469 pci_set_region(hose->regions + 2, pcie->io.start,
470 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
471 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100472
Marek Behún193a1e92019-08-07 15:01:56 +0200473 /* Set BAR0 to internal registers */
474 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
475 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
476
Pali Rohára7b61ab2021-10-22 16:22:10 +0200477 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
478 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
479 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
480 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
481 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
482
Stefan Roese94f453e2019-01-25 11:52:43 +0100483 return 0;
484}
485
486static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
487{
488 const u32 *addr;
489 int len;
490
491 addr = ofnode_get_property(node, "assigned-addresses", &len);
492 if (!addr) {
493 pr_err("property \"assigned-addresses\" not found");
494 return -FDT_ERR_NOTFOUND;
495 }
496
497 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
498
499 return 0;
500}
501
502#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
503#define DT_TYPE_IO 0x1
504#define DT_TYPE_MEM32 0x2
505#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
506#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
507
508static int mvebu_get_tgt_attr(ofnode node, int devfn,
509 unsigned long type,
510 unsigned int *tgt,
511 unsigned int *attr)
512{
513 const int na = 3, ns = 2;
514 const __be32 *range;
515 int rlen, nranges, rangesz, pna, i;
516
517 *tgt = -1;
518 *attr = -1;
519
520 range = ofnode_get_property(node, "ranges", &rlen);
521 if (!range)
522 return -EINVAL;
523
Stefan Roese0df62e82019-02-11 07:53:34 +0100524 /*
525 * Linux uses of_n_addr_cells() to get the number of address cells
526 * here. Currently this function is only available in U-Boot when
527 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
528 * general, lets't hardcode the "pna" value in the U-Boot code.
529 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100530 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
531 rangesz = pna + na + ns;
532 nranges = rlen / sizeof(__be32) / rangesz;
533
534 for (i = 0; i < nranges; i++, range += rangesz) {
535 u32 flags = of_read_number(range, 1);
536 u32 slot = of_read_number(range + 1, 1);
537 u64 cpuaddr = of_read_number(range + na, pna);
538 unsigned long rtype;
539
540 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
541 rtype = IORESOURCE_IO;
542 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
543 rtype = IORESOURCE_MEM;
544 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200545 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200546
Stefan Roese94f453e2019-01-25 11:52:43 +0100547 /*
548 * The Linux code used PCI_SLOT() here, which expects devfn
549 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
550 * only expects devfn in 15..8, where its saved in this driver.
551 */
552 if (slot == PCI_DEV(devfn) && type == rtype) {
553 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
554 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
555 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100556 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200557 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100558
559 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200560}
Stefan Roese94f453e2019-01-25 11:52:43 +0100561
Simon Glassd1998a92020-12-03 16:55:21 -0700562static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100563{
Simon Glassc69cda22020-12-03 16:55:20 -0700564 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100565 int ret = 0;
566
567 /* Get port number, lane number and memory target / attr */
568 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
569 &pcie->port)) {
570 ret = -ENODEV;
571 goto err;
572 }
573
574 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
575 pcie->lane = 0;
576
577 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
578
579 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
580 pcie->devfn = pci_get_devfn(dev);
581 if (pcie->devfn < 0) {
582 ret = -ENODEV;
583 goto err;
584 }
585
586 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
587 IORESOURCE_MEM,
588 &pcie->mem_target, &pcie->mem_attr);
589 if (ret < 0) {
590 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
591 goto err;
592 }
593
Phil Sutterba8ae032021-01-03 23:06:46 +0100594 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
595 IORESOURCE_IO,
596 &pcie->io_target, &pcie->io_attr);
597 if (ret < 0) {
598 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
599 goto err;
600 }
601
Stefan Roese94f453e2019-01-25 11:52:43 +0100602 /* Parse PCIe controller register base from DT */
603 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
604 if (ret < 0)
605 goto err;
606
Stefan Roese94f453e2019-01-25 11:52:43 +0100607 return 0;
608
609err:
610 return ret;
611}
612
613static const struct dm_pci_ops mvebu_pcie_ops = {
614 .read_config = mvebu_pcie_read_config,
615 .write_config = mvebu_pcie_write_config,
616};
617
618static struct driver pcie_mvebu_drv = {
619 .name = "pcie_mvebu",
620 .id = UCLASS_PCI,
621 .ops = &mvebu_pcie_ops,
622 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700623 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700624 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100625};
626
627/*
628 * Use a MISC device to bind the n instances (child nodes) of the
629 * PCIe base controller in UCLASS_PCI.
630 */
631static int mvebu_pcie_bind(struct udevice *parent)
632{
633 struct mvebu_pcie *pcie;
634 struct uclass_driver *drv;
635 struct udevice *dev;
636 ofnode subnode;
637
638 /* Lookup eth driver */
639 drv = lists_uclass_lookup(UCLASS_PCI);
640 if (!drv) {
641 puts("Cannot find PCI driver\n");
642 return -ENOENT;
643 }
644
645 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
646 if (!ofnode_is_available(subnode))
647 continue;
648
649 pcie = calloc(1, sizeof(*pcie));
650 if (!pcie)
651 return -ENOMEM;
652
653 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700654 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
655 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100656 }
657
658 return 0;
659}
660
661static const struct udevice_id mvebu_pcie_ids[] = {
662 { .compatible = "marvell,armada-xp-pcie" },
663 { .compatible = "marvell,armada-370-pcie" },
664 { }
665};
666
667U_BOOT_DRIVER(pcie_mvebu_base) = {
668 .name = "pcie_mvebu_base",
669 .id = UCLASS_MISC,
670 .of_match = mvebu_pcie_ids,
671 .bind = mvebu_pcie_bind,
672};