1 // SPDX-License-Identifier: GPL-2.0+
4 * Layerscape PCIe driver
8 #include <asm/arch/fsl_serdes.h>
10 #include <asm/global_data.h>
15 #include <dm/devres.h>
16 #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
18 #include <asm/arch/clock.h>
20 #include "pcie_layerscape.h"
22 DECLARE_GLOBAL_DATA_PTR;
24 static void ls_pcie_cfg0_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
26 struct ls_pcie *pcie = pcie_rc->pcie;
28 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
30 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
33 static void ls_pcie_cfg1_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
35 struct ls_pcie *pcie = pcie_rc->pcie;
37 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
39 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
42 static void ls_pcie_setup_atu(struct ls_pcie_rc *pcie_rc)
44 struct pci_region *io, *mem, *pref;
45 unsigned long long offset = 0;
46 struct ls_pcie *pcie = pcie_rc->pcie;
51 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
52 offset = LS1021_PCIE_SPACE_OFFSET +
53 LS1021_PCIE_SPACE_SIZE * pcie->idx;
56 /* ATU 0 : OUTBOUND : CFG0 */
57 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
59 pcie_rc->cfg_res.start + offset,
61 fdt_resource_size(&pcie_rc->cfg_res) / 2);
62 /* ATU 1 : OUTBOUND : CFG1 */
63 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
65 pcie_rc->cfg_res.start + offset +
66 fdt_resource_size(&pcie_rc->cfg_res) / 2,
68 fdt_resource_size(&pcie_rc->cfg_res) / 2);
70 pci_get_regions(pcie_rc->bus, &io, &mem, &pref);
71 idx = PCIE_ATU_REGION_INDEX1 + 1;
73 /* Fix the pcie memory map for LS2088A series SoCs */
74 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
75 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
76 svr == SVR_LS2048A || svr == SVR_LS2044A ||
77 svr == SVR_LS2081A || svr == SVR_LS2041A) {
79 io->phys_start = (io->phys_start &
80 (PCIE_PHYS_SIZE - 1)) +
81 LS2088A_PCIE1_PHYS_ADDR +
82 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
84 mem->phys_start = (mem->phys_start &
85 (PCIE_PHYS_SIZE - 1)) +
86 LS2088A_PCIE1_PHYS_ADDR +
87 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
89 pref->phys_start = (pref->phys_start &
90 (PCIE_PHYS_SIZE - 1)) +
91 LS2088A_PCIE1_PHYS_ADDR +
92 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
96 /* ATU : OUTBOUND : IO */
97 ls_pcie_atu_outbound_set(pcie, idx++,
99 io->phys_start + offset,
104 /* ATU : OUTBOUND : MEM */
105 ls_pcie_atu_outbound_set(pcie, idx++,
107 mem->phys_start + offset,
112 /* ATU : OUTBOUND : pref */
113 ls_pcie_atu_outbound_set(pcie, idx++,
115 pref->phys_start + offset,
119 ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM, PCIE_ATU_REGION_OUTBOUND);
122 /* Return 0 if the address is valid, -errno if not valid */
123 static int ls_pcie_addr_valid(struct ls_pcie_rc *pcie_rc, pci_dev_t bdf)
125 struct udevice *bus = pcie_rc->bus;
126 struct ls_pcie *pcie = pcie_rc->pcie;
128 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
131 if (!pcie_rc->enabled)
134 if (PCI_BUS(bdf) < dev_seq(bus))
137 if ((PCI_BUS(bdf) > dev_seq(bus)) && (!ls_pcie_link_up(pcie)))
140 if (PCI_BUS(bdf) <= (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0))
146 int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf,
147 uint offset, void **paddress)
149 struct ls_pcie_rc *pcie_rc = dev_get_priv(bus);
150 struct ls_pcie *pcie = pcie_rc->pcie;
153 if (ls_pcie_addr_valid(pcie_rc, bdf))
156 if (PCI_BUS(bdf) == dev_seq(bus)) {
157 *paddress = pcie->dbi + offset;
161 busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - dev_seq(bus)) |
162 PCIE_ATU_DEV(PCI_DEV(bdf)) |
163 PCIE_ATU_FUNC(PCI_FUNC(bdf));
165 if (PCI_BUS(bdf) == dev_seq(bus) + 1) {
166 ls_pcie_cfg0_set_busdev(pcie_rc, busdev);
167 *paddress = pcie_rc->cfg0 + offset;
169 ls_pcie_cfg1_set_busdev(pcie_rc, busdev);
170 *paddress = pcie_rc->cfg1 + offset;
175 static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
176 uint offset, ulong *valuep,
177 enum pci_size_t size)
179 return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
180 bdf, offset, valuep, size);
183 static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
184 uint offset, ulong value,
185 enum pci_size_t size)
187 return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
188 bdf, offset, value, size);
191 /* Clear multi-function bit */
192 static void ls_pcie_clear_multifunction(struct ls_pcie_rc *pcie_rc)
194 struct ls_pcie *pcie = pcie_rc->pcie;
196 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
199 /* Fix class value */
200 static void ls_pcie_fix_class(struct ls_pcie_rc *pcie_rc)
202 struct ls_pcie *pcie = pcie_rc->pcie;
204 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
207 /* Drop MSG TLP except for Vendor MSG */
208 static void ls_pcie_drop_msg_tlp(struct ls_pcie_rc *pcie_rc)
210 struct ls_pcie *pcie = pcie_rc->pcie;
213 val = dbi_readl(pcie, PCIE_STRFMR1);
215 dbi_writel(pcie, val, PCIE_STRFMR1);
218 /* Disable all bars in RC mode */
219 static void ls_pcie_disable_bars(struct ls_pcie_rc *pcie_rc)
221 struct ls_pcie *pcie = pcie_rc->pcie;
223 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
224 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
225 dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
228 static void ls_pcie_setup_ctrl(struct ls_pcie_rc *pcie_rc)
230 struct ls_pcie *pcie = pcie_rc->pcie;
232 ls_pcie_setup_atu(pcie_rc);
234 ls_pcie_dbi_ro_wr_en(pcie);
235 ls_pcie_fix_class(pcie_rc);
236 ls_pcie_clear_multifunction(pcie_rc);
237 ls_pcie_drop_msg_tlp(pcie_rc);
238 ls_pcie_dbi_ro_wr_dis(pcie);
240 ls_pcie_disable_bars(pcie_rc);
241 pcie_rc->stream_id_cur = 0;
244 static int ls_pcie_probe(struct udevice *dev)
246 struct ls_pcie_rc *pcie_rc = dev_get_priv(dev);
247 const void *fdt = gd->fdt_blob;
248 int node = dev_of_offset(dev);
249 struct ls_pcie *pcie;
257 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
261 pcie_rc->pcie = pcie;
263 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
264 "dbi", &pcie_rc->dbi_res);
266 printf("ls-pcie: resource \"dbi\" not found\n");
270 pcie->idx = (pcie_rc->dbi_res.start - PCIE_SYS_BASE_ADDR) /
273 list_add(&pcie_rc->list, &ls_pcie_list);
275 pcie_rc->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
276 if (!pcie_rc->enabled) {
277 printf("PCIe%d: %s disabled\n", PCIE_SRDS_PRTCL(pcie->idx),
282 pcie->dbi = map_physmem(pcie_rc->dbi_res.start,
283 fdt_resource_size(&pcie_rc->dbi_res),
286 pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
287 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
290 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
291 "lut", &pcie_rc->lut_res);
293 pcie->lut = map_physmem(pcie_rc->lut_res.start,
294 fdt_resource_size(&pcie_rc->lut_res),
297 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
298 "ctrl", &pcie_rc->ctrl_res);
300 pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start,
301 fdt_resource_size(&pcie_rc->ctrl_res),
304 pcie->ctrl = pcie->lut;
307 printf("%s: NOT find CTRL\n", dev->name);
311 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
312 "config", &pcie_rc->cfg_res);
314 printf("%s: resource \"config\" not found\n", dev->name);
318 cfg_size = fdt_resource_size(&pcie_rc->cfg_res);
319 if (cfg_size < SZ_8K) {
320 printf("PCIe%d: %s Invalid size(0x%llx) for resource \"config\",expected minimum 0x%x\n",
321 PCIE_SRDS_PRTCL(pcie->idx), dev->name, (u64)cfg_size, SZ_8K);
326 * Fix the pcie memory map address and PF control registers address
327 * for LS2088A series SoCs
330 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
331 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
332 svr == SVR_LS2048A || svr == SVR_LS2044A ||
333 svr == SVR_LS2081A || svr == SVR_LS2041A) {
334 pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
335 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
336 pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size;
337 pcie->ctrl = pcie->lut + 0x40000;
340 pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start,
341 fdt_resource_size(&pcie_rc->cfg_res),
343 pcie_rc->cfg1 = pcie_rc->cfg0 +
344 fdt_resource_size(&pcie_rc->cfg_res) / 2;
346 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
348 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
349 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
350 (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0,
353 printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name,
355 ls_pcie_setup_ctrl(pcie_rc);
357 if (!ls_pcie_link_up(pcie)) {
358 /* Let the user know there's no PCIe link */
359 printf(": no link\n");
363 /* Print the negotiated PCIe link width */
364 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
365 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
366 link_sta & PCIE_LINK_SPEED_MASK);
371 static const struct dm_pci_ops ls_pcie_ops = {
372 .read_config = ls_pcie_read_config,
373 .write_config = ls_pcie_write_config,
376 static const struct udevice_id ls_pcie_ids[] = {
377 { .compatible = "fsl,ls-pcie" },
381 U_BOOT_DRIVER(pci_layerscape) = {
382 .name = "pci_layerscape",
384 .of_match = ls_pcie_ids,
386 .probe = ls_pcie_probe,
387 .priv_auto = sizeof(struct ls_pcie_rc),