Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2020 NXP |
| 4 | * Layerscape PCIe driver |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/arch/fsl_serdes.h> |
| 9 | #include <pci.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <errno.h> |
| 12 | #include <malloc.h> |
| 13 | #include <dm.h> |
| 14 | #include <dm/devres.h> |
| 15 | #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \ |
| 16 | defined(CONFIG_ARM) |
| 17 | #include <asm/arch/clock.h> |
| 18 | #endif |
| 19 | #include "pcie_layerscape.h" |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | static void ls_pcie_cfg0_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev) |
| 24 | { |
| 25 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 26 | |
| 27 | dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0, |
| 28 | PCIE_ATU_VIEWPORT); |
| 29 | dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET); |
| 30 | } |
| 31 | |
| 32 | static void ls_pcie_cfg1_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev) |
| 33 | { |
| 34 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 35 | |
| 36 | dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1, |
| 37 | PCIE_ATU_VIEWPORT); |
| 38 | dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET); |
| 39 | } |
| 40 | |
| 41 | static void ls_pcie_setup_atu(struct ls_pcie_rc *pcie_rc) |
| 42 | { |
| 43 | struct pci_region *io, *mem, *pref; |
| 44 | unsigned long long offset = 0; |
| 45 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 46 | int idx = 0; |
| 47 | uint svr; |
| 48 | |
| 49 | svr = get_svr(); |
| 50 | if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) { |
| 51 | offset = LS1021_PCIE_SPACE_OFFSET + |
| 52 | LS1021_PCIE_SPACE_SIZE * pcie->idx; |
| 53 | } |
| 54 | |
| 55 | /* ATU 0 : OUTBOUND : CFG0 */ |
| 56 | ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0, |
| 57 | PCIE_ATU_TYPE_CFG0, |
| 58 | pcie_rc->cfg_res.start + offset, |
| 59 | 0, |
| 60 | fdt_resource_size(&pcie_rc->cfg_res) / 2); |
| 61 | /* ATU 1 : OUTBOUND : CFG1 */ |
| 62 | ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1, |
| 63 | PCIE_ATU_TYPE_CFG1, |
| 64 | pcie_rc->cfg_res.start + offset + |
| 65 | fdt_resource_size(&pcie_rc->cfg_res) / 2, |
| 66 | 0, |
| 67 | fdt_resource_size(&pcie_rc->cfg_res) / 2); |
| 68 | |
| 69 | pci_get_regions(pcie_rc->bus, &io, &mem, &pref); |
| 70 | idx = PCIE_ATU_REGION_INDEX1 + 1; |
| 71 | |
| 72 | /* Fix the pcie memory map for LS2088A series SoCs */ |
| 73 | svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 74 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
| 75 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 76 | svr == SVR_LS2081A || svr == SVR_LS2041A) { |
| 77 | if (io) |
| 78 | io->phys_start = (io->phys_start & |
| 79 | (PCIE_PHYS_SIZE - 1)) + |
| 80 | LS2088A_PCIE1_PHYS_ADDR + |
| 81 | LS2088A_PCIE_PHYS_SIZE * pcie->idx; |
| 82 | if (mem) |
| 83 | mem->phys_start = (mem->phys_start & |
| 84 | (PCIE_PHYS_SIZE - 1)) + |
| 85 | LS2088A_PCIE1_PHYS_ADDR + |
| 86 | LS2088A_PCIE_PHYS_SIZE * pcie->idx; |
| 87 | if (pref) |
| 88 | pref->phys_start = (pref->phys_start & |
| 89 | (PCIE_PHYS_SIZE - 1)) + |
| 90 | LS2088A_PCIE1_PHYS_ADDR + |
| 91 | LS2088A_PCIE_PHYS_SIZE * pcie->idx; |
| 92 | } |
| 93 | |
| 94 | if (io) |
| 95 | /* ATU : OUTBOUND : IO */ |
| 96 | ls_pcie_atu_outbound_set(pcie, idx++, |
| 97 | PCIE_ATU_TYPE_IO, |
| 98 | io->phys_start + offset, |
| 99 | io->bus_start, |
| 100 | io->size); |
| 101 | |
| 102 | if (mem) |
| 103 | /* ATU : OUTBOUND : MEM */ |
| 104 | ls_pcie_atu_outbound_set(pcie, idx++, |
| 105 | PCIE_ATU_TYPE_MEM, |
| 106 | mem->phys_start + offset, |
| 107 | mem->bus_start, |
| 108 | mem->size); |
| 109 | |
| 110 | if (pref) |
| 111 | /* ATU : OUTBOUND : pref */ |
| 112 | ls_pcie_atu_outbound_set(pcie, idx++, |
| 113 | PCIE_ATU_TYPE_MEM, |
| 114 | pref->phys_start + offset, |
| 115 | pref->bus_start, |
| 116 | pref->size); |
| 117 | |
Xiaowei Bao | bb3f713 | 2020-07-09 23:31:40 +0800 | [diff] [blame] | 118 | ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM, PCIE_ATU_REGION_OUTBOUND); |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* Return 0 if the address is valid, -errno if not valid */ |
| 122 | static int ls_pcie_addr_valid(struct ls_pcie_rc *pcie_rc, pci_dev_t bdf) |
| 123 | { |
| 124 | struct udevice *bus = pcie_rc->bus; |
| 125 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 126 | |
| 127 | if (pcie->mode == PCI_HEADER_TYPE_NORMAL) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | if (!pcie_rc->enabled) |
| 131 | return -ENXIO; |
| 132 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 133 | if (PCI_BUS(bdf) < dev_seq(bus)) |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 136 | if ((PCI_BUS(bdf) > dev_seq(bus)) && (!ls_pcie_link_up(pcie))) |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 139 | if (PCI_BUS(bdf) <= (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0)) |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf, |
| 146 | uint offset, void **paddress) |
| 147 | { |
| 148 | struct ls_pcie_rc *pcie_rc = dev_get_priv(bus); |
| 149 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 150 | u32 busdev; |
| 151 | |
| 152 | if (ls_pcie_addr_valid(pcie_rc, bdf)) |
| 153 | return -EINVAL; |
| 154 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 155 | if (PCI_BUS(bdf) == dev_seq(bus)) { |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 156 | *paddress = pcie->dbi + offset; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 160 | busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - dev_seq(bus)) | |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 161 | PCIE_ATU_DEV(PCI_DEV(bdf)) | |
| 162 | PCIE_ATU_FUNC(PCI_FUNC(bdf)); |
| 163 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 164 | if (PCI_BUS(bdf) == dev_seq(bus) + 1) { |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 165 | ls_pcie_cfg0_set_busdev(pcie_rc, busdev); |
| 166 | *paddress = pcie_rc->cfg0 + offset; |
| 167 | } else { |
| 168 | ls_pcie_cfg1_set_busdev(pcie_rc, busdev); |
| 169 | *paddress = pcie_rc->cfg1 + offset; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf, |
| 175 | uint offset, ulong *valuep, |
| 176 | enum pci_size_t size) |
| 177 | { |
| 178 | return pci_generic_mmap_read_config(bus, ls_pcie_conf_address, |
| 179 | bdf, offset, valuep, size); |
| 180 | } |
| 181 | |
| 182 | static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 183 | uint offset, ulong value, |
| 184 | enum pci_size_t size) |
| 185 | { |
| 186 | return pci_generic_mmap_write_config(bus, ls_pcie_conf_address, |
| 187 | bdf, offset, value, size); |
| 188 | } |
| 189 | |
| 190 | /* Clear multi-function bit */ |
| 191 | static void ls_pcie_clear_multifunction(struct ls_pcie_rc *pcie_rc) |
| 192 | { |
| 193 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 194 | |
| 195 | writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE); |
| 196 | } |
| 197 | |
| 198 | /* Fix class value */ |
| 199 | static void ls_pcie_fix_class(struct ls_pcie_rc *pcie_rc) |
| 200 | { |
| 201 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 202 | |
| 203 | writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE); |
| 204 | } |
| 205 | |
| 206 | /* Drop MSG TLP except for Vendor MSG */ |
| 207 | static void ls_pcie_drop_msg_tlp(struct ls_pcie_rc *pcie_rc) |
| 208 | { |
| 209 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 210 | u32 val; |
| 211 | |
| 212 | val = dbi_readl(pcie, PCIE_STRFMR1); |
| 213 | val &= 0xDFFFFFFF; |
| 214 | dbi_writel(pcie, val, PCIE_STRFMR1); |
| 215 | } |
| 216 | |
| 217 | /* Disable all bars in RC mode */ |
| 218 | static void ls_pcie_disable_bars(struct ls_pcie_rc *pcie_rc) |
| 219 | { |
| 220 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 221 | |
| 222 | dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0); |
| 223 | dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1); |
| 224 | dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1); |
| 225 | } |
| 226 | |
| 227 | static void ls_pcie_setup_ctrl(struct ls_pcie_rc *pcie_rc) |
| 228 | { |
| 229 | struct ls_pcie *pcie = pcie_rc->pcie; |
| 230 | |
| 231 | ls_pcie_setup_atu(pcie_rc); |
| 232 | |
| 233 | ls_pcie_dbi_ro_wr_en(pcie); |
| 234 | ls_pcie_fix_class(pcie_rc); |
| 235 | ls_pcie_clear_multifunction(pcie_rc); |
| 236 | ls_pcie_drop_msg_tlp(pcie_rc); |
| 237 | ls_pcie_dbi_ro_wr_dis(pcie); |
| 238 | |
| 239 | ls_pcie_disable_bars(pcie_rc); |
| 240 | pcie_rc->stream_id_cur = 0; |
| 241 | } |
| 242 | |
| 243 | static int ls_pcie_probe(struct udevice *dev) |
| 244 | { |
| 245 | struct ls_pcie_rc *pcie_rc = dev_get_priv(dev); |
| 246 | const void *fdt = gd->fdt_blob; |
| 247 | int node = dev_of_offset(dev); |
| 248 | struct ls_pcie *pcie; |
| 249 | u16 link_sta; |
| 250 | uint svr; |
| 251 | int ret; |
| 252 | fdt_size_t cfg_size; |
| 253 | |
| 254 | pcie_rc->bus = dev; |
| 255 | |
| 256 | pcie = devm_kmalloc(dev, sizeof(*pcie), GFP_KERNEL); |
| 257 | if (!pcie) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | pcie_rc->pcie = pcie; |
| 261 | |
| 262 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 263 | "dbi", &pcie_rc->dbi_res); |
| 264 | if (ret) { |
| 265 | printf("ls-pcie: resource \"dbi\" not found\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | pcie->idx = (pcie_rc->dbi_res.start - PCIE_SYS_BASE_ADDR) / |
| 270 | PCIE_CCSR_SIZE; |
| 271 | |
| 272 | list_add(&pcie_rc->list, &ls_pcie_list); |
| 273 | |
| 274 | pcie_rc->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx)); |
| 275 | if (!pcie_rc->enabled) { |
Wasim Khan | 8b3d2d2 | 2020-09-28 16:26:04 +0530 | [diff] [blame] | 276 | printf("PCIe%d: %s disabled\n", PCIE_SRDS_PRTCL(pcie->idx), |
| 277 | dev->name); |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | pcie->dbi = map_physmem(pcie_rc->dbi_res.start, |
| 282 | fdt_resource_size(&pcie_rc->dbi_res), |
| 283 | MAP_NOCACHE); |
| 284 | |
| 285 | pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f; |
| 286 | if (pcie->mode == PCI_HEADER_TYPE_NORMAL) |
| 287 | return 0; |
| 288 | |
| 289 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 290 | "lut", &pcie_rc->lut_res); |
| 291 | if (!ret) |
| 292 | pcie->lut = map_physmem(pcie_rc->lut_res.start, |
| 293 | fdt_resource_size(&pcie_rc->lut_res), |
| 294 | MAP_NOCACHE); |
| 295 | |
| 296 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 297 | "ctrl", &pcie_rc->ctrl_res); |
| 298 | if (!ret) |
| 299 | pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start, |
| 300 | fdt_resource_size(&pcie_rc->ctrl_res), |
| 301 | MAP_NOCACHE); |
| 302 | if (!pcie->ctrl) |
| 303 | pcie->ctrl = pcie->lut; |
| 304 | |
| 305 | if (!pcie->ctrl) { |
| 306 | printf("%s: NOT find CTRL\n", dev->name); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 311 | "config", &pcie_rc->cfg_res); |
| 312 | if (ret) { |
| 313 | printf("%s: resource \"config\" not found\n", dev->name); |
| 314 | return ret; |
| 315 | } |
| 316 | |
Wasim Khan | e8cba3f | 2020-09-28 16:26:13 +0530 | [diff] [blame] | 317 | cfg_size = fdt_resource_size(&pcie_rc->cfg_res); |
| 318 | if (cfg_size < SZ_8K) { |
| 319 | printf("PCIe%d: %s Invalid size(0x%llx) for resource \"config\",expected minimum 0x%x\n", |
| 320 | PCIE_SRDS_PRTCL(pcie->idx), dev->name, (u64)cfg_size, SZ_8K); |
| 321 | return 0; |
| 322 | } |
| 323 | |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 324 | /* |
| 325 | * Fix the pcie memory map address and PF control registers address |
| 326 | * for LS2088A series SoCs |
| 327 | */ |
| 328 | svr = get_svr(); |
| 329 | svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 330 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
| 331 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 332 | svr == SVR_LS2081A || svr == SVR_LS2041A) { |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 333 | pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR + |
| 334 | LS2088A_PCIE_PHYS_SIZE * pcie->idx; |
| 335 | pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size; |
| 336 | pcie->ctrl = pcie->lut + 0x40000; |
| 337 | } |
| 338 | |
| 339 | pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start, |
| 340 | fdt_resource_size(&pcie_rc->cfg_res), |
| 341 | MAP_NOCACHE); |
| 342 | pcie_rc->cfg1 = pcie_rc->cfg0 + |
| 343 | fdt_resource_size(&pcie_rc->cfg_res) / 2; |
| 344 | |
| 345 | pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian"); |
| 346 | |
| 347 | debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n", |
| 348 | dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut, |
| 349 | (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0, |
| 350 | pcie->big_endian); |
| 351 | |
Wasim Khan | 8b3d2d2 | 2020-09-28 16:26:04 +0530 | [diff] [blame] | 352 | printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name, |
| 353 | "Root Complex"); |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 354 | ls_pcie_setup_ctrl(pcie_rc); |
| 355 | |
| 356 | if (!ls_pcie_link_up(pcie)) { |
| 357 | /* Let the user know there's no PCIe link */ |
| 358 | printf(": no link\n"); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* Print the negotiated PCIe link width */ |
| 363 | link_sta = readw(pcie->dbi + PCIE_LINK_STA); |
| 364 | printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4, |
| 365 | link_sta & PCIE_LINK_SPEED_MASK); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static const struct dm_pci_ops ls_pcie_ops = { |
| 371 | .read_config = ls_pcie_read_config, |
| 372 | .write_config = ls_pcie_write_config, |
| 373 | }; |
| 374 | |
| 375 | static const struct udevice_id ls_pcie_ids[] = { |
| 376 | { .compatible = "fsl,ls-pcie" }, |
| 377 | { } |
| 378 | }; |
| 379 | |
| 380 | U_BOOT_DRIVER(pci_layerscape) = { |
| 381 | .name = "pci_layerscape", |
| 382 | .id = UCLASS_PCI, |
| 383 | .of_match = ls_pcie_ids, |
| 384 | .ops = &ls_pcie_ops, |
| 385 | .probe = ls_pcie_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 386 | .priv_auto = sizeof(struct ls_pcie_rc), |
Xiaowei Bao | 13b277f | 2020-07-09 23:31:33 +0800 | [diff] [blame] | 387 | }; |