blob: 78961271a8eef8e4b6991675ee9de2bb1968b8da [file] [log] [blame]
Neil Armstrongb46caff2021-03-25 15:49:18 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2021 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 *
6 * Copyright (c) 2021 Rockchip, Inc.
7 *
8 * Copyright (C) 2018 Texas Instruments, Inc
9 */
10
Neil Armstrongb46caff2021-03-25 15:49:18 +010011#include <dm.h>
12#include <log.h>
13#include <pci.h>
14#include <dm/device_compat.h>
15#include <asm/io.h>
16#include <linux/delay.h>
17#include "pcie_dw_common.h"
18
19int pcie_dw_get_link_speed(struct pcie_dw *pci)
20{
21 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
22 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
23}
24
25int pcie_dw_get_link_width(struct pcie_dw *pci)
26{
27 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
28 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
29}
30
31static void dw_pcie_writel_ob_unroll(struct pcie_dw *pci, u32 index, u32 reg,
32 u32 val)
33{
34 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
35 void __iomem *base = pci->atu_base;
36
37 writel(val, base + offset + reg);
38}
39
40static u32 dw_pcie_readl_ob_unroll(struct pcie_dw *pci, u32 index, u32 reg)
41{
42 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
43 void __iomem *base = pci->atu_base;
44
45 return readl(base + offset + reg);
46}
47
48/**
49 * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
50 *
51 * @pcie: Pointer to the PCI controller state
52 * @index: ATU region index
53 * @type: ATU accsess type
54 * @cpu_addr: the physical address for the translation entry
55 * @pci_addr: the pcie bus address for the translation entry
56 * @size: the size of the translation entry
57 *
58 * Return: 0 is successful and -1 is failure
59 */
60int pcie_dw_prog_outbound_atu_unroll(struct pcie_dw *pci, int index,
61 int type, u64 cpu_addr,
62 u64 pci_addr, u32 size)
63{
64 u32 retries, val;
65
66 dev_dbg(pci->dev, "ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
67 index, type, cpu_addr, pci_addr, size);
68
69 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
70 lower_32_bits(cpu_addr));
71 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
72 upper_32_bits(cpu_addr));
73 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
74 lower_32_bits(cpu_addr + size - 1));
Ben Dooks3403b9a2022-10-20 16:51:09 +010075 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_LIMIT,
76 upper_32_bits(cpu_addr + size - 1));
Neil Armstrongb46caff2021-03-25 15:49:18 +010077 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
78 lower_32_bits(pci_addr));
79 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
80 upper_32_bits(pci_addr));
81 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
82 type);
83 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
84 PCIE_ATU_ENABLE);
85
86 /*
87 * Make sure ATU enable takes effect before any subsequent config
88 * and I/O accesses.
89 */
90 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
91 val = dw_pcie_readl_ob_unroll(pci, index,
92 PCIE_ATU_UNR_REGION_CTRL2);
93 if (val & PCIE_ATU_ENABLE)
94 return 0;
95
96 udelay(LINK_WAIT_IATU);
97 }
98 dev_err(pci->dev, "outbound iATU is not being enabled\n");
99
100 return -1;
101}
102
103/**
104 * set_cfg_address() - Configure the PCIe controller config space access
105 *
106 * @pcie: Pointer to the PCI controller state
107 * @d: PCI device to access
108 * @where: Offset in the configuration space
109 *
110 * Configures the PCIe controller to access the configuration space of
111 * a specific PCIe device and returns the address to use for this
112 * access.
113 *
114 * Return: Address that can be used to access the configation space
115 * of the requested device / offset
116 */
117static uintptr_t set_cfg_address(struct pcie_dw *pcie,
118 pci_dev_t d, uint where)
119{
120 int bus = PCI_BUS(d) - pcie->first_busno;
121 uintptr_t va_address;
122 u32 atu_type;
123 int ret;
124
125 /* Use dbi_base for own configuration read and write */
126 if (!bus) {
127 va_address = (uintptr_t)pcie->dbi_base;
128 goto out;
129 }
130
131 if (bus == 1)
132 /*
133 * For local bus whose primary bus number is root bridge,
134 * change TLP Type field to 4.
135 */
136 atu_type = PCIE_ATU_TYPE_CFG0;
137 else
138 /* Otherwise, change TLP Type field to 5. */
139 atu_type = PCIE_ATU_TYPE_CFG1;
140
141 /*
142 * Not accessing root port configuration space?
Jonas Karlman8746d9c2023-07-22 13:30:16 +0000143 * Region #1 is used for Outbound CFG space access.
Neil Armstrongb46caff2021-03-25 15:49:18 +0100144 * Direction = Outbound
Jonas Karlman8746d9c2023-07-22 13:30:16 +0000145 * Region Index = 1
Neil Armstrongb46caff2021-03-25 15:49:18 +0100146 */
147 d = PCI_MASK_BUS(d);
148 d = PCI_ADD_BUS(bus, d);
149 ret = pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
150 atu_type, (u64)pcie->cfg_base,
151 d << 8, pcie->cfg_size);
152 if (ret)
153 return (uintptr_t)ret;
154
155 va_address = (uintptr_t)pcie->cfg_base;
156
157out:
158 va_address += where & ~0x3;
159
160 return va_address;
161}
162
163/**
164 * pcie_dw_addr_valid() - Check for valid bus address
165 *
166 * @d: The PCI device to access
167 * @first_busno: Bus number of the PCIe controller root complex
168 *
169 * Return 1 (true) if the PCI device can be accessed by this controller.
170 *
171 * Return: 1 on valid, 0 on invalid
172 */
173static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
174{
175 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
176 return 0;
177 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
178 return 0;
179
180 return 1;
181}
182
183/**
184 * pcie_dw_read_config() - Read from configuration space
185 *
186 * @bus: Pointer to the PCI bus
187 * @bdf: Identifies the PCIe device to access
188 * @offset: The offset into the device's configuration space
189 * @valuep: A pointer at which to store the read value
190 * @size: Indicates the size of access to perform
191 *
192 * Read a value of size @size from offset @offset within the configuration
193 * space of the device identified by the bus, device & function numbers in @bdf
194 * on the PCI bus @bus.
195 *
196 * Return: 0 on success
197 */
198int pcie_dw_read_config(const struct udevice *bus, pci_dev_t bdf,
199 uint offset, ulong *valuep,
200 enum pci_size_t size)
201{
202 struct pcie_dw *pcie = dev_get_priv(bus);
203 uintptr_t va_address;
204 ulong value;
205
206 dev_dbg(pcie->dev, "PCIE CFG read: bdf=%2x:%2x:%2x ",
207 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
208
209 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
210 debug("- out of range\n");
211 *valuep = pci_get_ff(size);
212 return 0;
213 }
214
215 va_address = set_cfg_address(pcie, bdf, offset);
216
Green Wancb6506a2021-05-19 04:16:15 -0700217 value = readl((void __iomem *)va_address);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100218
219 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
220 *valuep = pci_conv_32_to_size(value, offset, size);
221
222 return pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
223 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
224 pcie->io.bus_start, pcie->io.size);
225}
226
227/**
228 * pcie_dw_write_config() - Write to configuration space
229 *
230 * @bus: Pointer to the PCI bus
231 * @bdf: Identifies the PCIe device to access
232 * @offset: The offset into the device's configuration space
233 * @value: The value to write
234 * @size: Indicates the size of access to perform
235 *
236 * Write the value @value of size @size from offset @offset within the
237 * configuration space of the device identified by the bus, device & function
238 * numbers in @bdf on the PCI bus @bus.
239 *
240 * Return: 0 on success
241 */
242int pcie_dw_write_config(struct udevice *bus, pci_dev_t bdf,
243 uint offset, ulong value,
244 enum pci_size_t size)
245{
246 struct pcie_dw *pcie = dev_get_priv(bus);
247 uintptr_t va_address;
248 ulong old;
249
250 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
251 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
252 dev_dbg(pcie->dev, "(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
253
254 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
255 debug("- out of range\n");
256 return 0;
257 }
258
259 va_address = set_cfg_address(pcie, bdf, offset);
260
Green Wancb6506a2021-05-19 04:16:15 -0700261 old = readl((void __iomem *)va_address);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100262 value = pci_conv_size_to_32(old, value, offset, size);
Green Wancb6506a2021-05-19 04:16:15 -0700263 writel(value, (void __iomem *)va_address);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100264
265 return pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
266 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
267 pcie->io.bus_start, pcie->io.size);
268}
269
Neil Armstrongf946ce92024-11-25 10:46:16 +0100270/*
271 * These interfaces resemble the pci_find_*capability() interfaces, but these
272 * are for configuring host controllers, which are bridges *to* PCI devices but
273 * are not PCI devices themselves.
274 */
275static u8 pcie_dw_find_next_cap(struct pcie_dw *pci, u8 cap_ptr, u8 cap)
276{
277 u8 cap_id, next_cap_ptr;
278 u32 val;
279 u16 reg;
280
281 if (!cap_ptr)
282 return 0;
283
284 val = readl(pci->dbi_base + (cap_ptr & ~0x3));
285 reg = pci_conv_32_to_size(val, cap_ptr, 2);
286 cap_id = (reg & 0x00ff);
287
288 if (cap_id > PCI_CAP_ID_MAX)
289 return 0;
290
291 if (cap_id == cap)
292 return cap_ptr;
293
294 next_cap_ptr = (reg & 0xff00) >> 8;
295 return pcie_dw_find_next_cap(pci, next_cap_ptr, cap);
296}
297
298u8 pcie_dw_find_capability(struct pcie_dw *pci, u8 cap)
299{
300 u8 next_cap_ptr;
301 u32 val;
302 u16 reg;
303
304 val = readl(pci->dbi_base + (PCI_CAPABILITY_LIST & ~0x3));
305 reg = pci_conv_32_to_size(val, PCI_CAPABILITY_LIST, 2);
306
307 next_cap_ptr = (reg & 0x00ff);
308
309 return pcie_dw_find_next_cap(pci, next_cap_ptr, cap);
310}
311
Neil Armstrongb46caff2021-03-25 15:49:18 +0100312/**
313 * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion
314 *
315 * @pcie: Pointer to the PCI controller state
316 *
317 * Configure the host BARs of the PCIe controller root port so that
318 * PCI(e) devices may access the system memory.
319 */
320void pcie_dw_setup_host(struct pcie_dw *pci)
321{
322 struct udevice *ctlr = pci_get_controller(pci->dev);
323 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
324 u32 ret;
325
326 if (!pci->atu_base)
327 pci->atu_base = pci->dbi_base + DEFAULT_DBI_ATU_OFFSET;
328
329 /* setup RC BARs */
330 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
331 pci->dbi_base + PCI_BASE_ADDRESS_0);
332 writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1);
333
334 /* setup interrupt pins */
335 clrsetbits_le32(pci->dbi_base + PCI_INTERRUPT_LINE,
336 0xff00, 0x100);
337
338 /* setup bus numbers */
339 clrsetbits_le32(pci->dbi_base + PCI_PRIMARY_BUS,
340 0xffffff, 0x00ff0100);
341
342 /* setup command register */
343 clrsetbits_le32(pci->dbi_base + PCI_PRIMARY_BUS,
344 0xffff,
345 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
346 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
347
348 /* Enable write permission for the DBI read-only register */
349 dw_pcie_dbi_write_enable(pci, true);
350 /* program correct class for RC */
351 writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
352 /* Better disable write permission right after the update */
353 dw_pcie_dbi_write_enable(pci, false);
354
355 setbits_le32(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL,
356 PORT_LOGIC_SPEED_CHANGE);
357
358 for (ret = 0; ret < hose->region_count; ret++) {
359 if (hose->regions[ret].flags == PCI_REGION_IO) {
360 pci->io.phys_start = hose->regions[ret].phys_start; /* IO base */
361 pci->io.bus_start = hose->regions[ret].bus_start; /* IO_bus_addr */
362 pci->io.size = hose->regions[ret].size; /* IO size */
363 } else if (hose->regions[ret].flags == PCI_REGION_MEM) {
364 pci->mem.phys_start = hose->regions[ret].phys_start; /* MEM base */
365 pci->mem.bus_start = hose->regions[ret].bus_start; /* MEM_bus_addr */
366 pci->mem.size = hose->regions[ret].size; /* MEM size */
367 } else if (hose->regions[ret].flags == PCI_REGION_PREFETCH) {
368 pci->prefetch.phys_start = hose->regions[ret].phys_start; /* PREFETCH base */
369 pci->prefetch.bus_start = hose->regions[ret].bus_start; /* PREFETCH_bus_addr */
370 pci->prefetch.size = hose->regions[ret].size; /* PREFETCH size */
371 } else if (hose->regions[ret].flags == PCI_REGION_SYS_MEMORY) {
Jonas Karlman8746d9c2023-07-22 13:30:16 +0000372 if (!pci->cfg_base) {
373 pci->cfg_base = (void *)(pci->io.phys_start - pci->io.size);
374 pci->cfg_size = pci->io.size;
375 }
Neil Armstrongb46caff2021-03-25 15:49:18 +0100376 } else {
377 dev_err(pci->dev, "invalid flags type!\n");
378 }
379 }
380
Green Wancb6506a2021-05-19 04:16:15 -0700381 dev_dbg(pci->dev, "Config space: [0x%llx - 0x%llx, size 0x%llx]\n",
382 (u64)pci->cfg_base, (u64)pci->cfg_base + pci->cfg_size,
383 (u64)pci->cfg_size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100384
Green Wancb6506a2021-05-19 04:16:15 -0700385 dev_dbg(pci->dev, "IO space: [0x%llx - 0x%llx, size 0x%llx]\n",
386 (u64)pci->io.phys_start, (u64)pci->io.phys_start + pci->io.size,
387 (u64)pci->io.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100388
Green Wancb6506a2021-05-19 04:16:15 -0700389 dev_dbg(pci->dev, "IO bus: [0x%llx - 0x%llx, size 0x%llx]\n",
390 (u64)pci->io.bus_start, (u64)pci->io.bus_start + pci->io.size,
391 (u64)pci->io.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100392
Green Wancb6506a2021-05-19 04:16:15 -0700393 dev_dbg(pci->dev, "MEM space: [0x%llx - 0x%llx, size 0x%llx]\n",
394 (u64)pci->mem.phys_start,
395 (u64)pci->mem.phys_start + pci->mem.size,
396 (u64)pci->mem.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100397
Green Wancb6506a2021-05-19 04:16:15 -0700398 dev_dbg(pci->dev, "MEM bus: [0x%llx - 0x%llx, size 0x%llx]\n",
399 (u64)pci->mem.bus_start,
400 (u64)pci->mem.bus_start + pci->mem.size,
401 (u64)pci->mem.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100402
403 if (pci->prefetch.size) {
Green Wancb6506a2021-05-19 04:16:15 -0700404 dev_dbg(pci->dev, "PREFETCH space: [0x%llx - 0x%llx, size 0x%llx]\n",
405 (u64)pci->prefetch.phys_start,
406 (u64)pci->prefetch.phys_start + pci->prefetch.size,
407 (u64)pci->prefetch.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100408
Green Wancb6506a2021-05-19 04:16:15 -0700409 dev_dbg(pci->dev, "PREFETCH bus: [0x%llx - 0x%llx, size 0x%llx]\n",
410 (u64)pci->prefetch.bus_start,
411 (u64)pci->prefetch.bus_start + pci->prefetch.size,
412 (u64)pci->prefetch.size);
Neil Armstrongb46caff2021-03-25 15:49:18 +0100413 }
414}