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 | |
| 133 | if (PCI_BUS(bdf) < bus->seq) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if ((PCI_BUS(bdf) > bus->seq) && (!ls_pcie_link_up(pcie))) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | if (PCI_BUS(bdf) <= (bus->seq + 1) && (PCI_DEV(bdf) > 0)) |
| 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 | |
| 155 | if (PCI_BUS(bdf) == bus->seq) { |
| 156 | *paddress = pcie->dbi + offset; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - bus->seq) | |
| 161 | PCIE_ATU_DEV(PCI_DEV(bdf)) | |
| 162 | PCIE_ATU_FUNC(PCI_FUNC(bdf)); |
| 163 | |
| 164 | if (PCI_BUS(bdf) == bus->seq + 1) { |
| 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) { |
| 276 | printf("PCIe%d: %s disabled\n", pcie->idx, dev->name); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | pcie->dbi = map_physmem(pcie_rc->dbi_res.start, |
| 281 | fdt_resource_size(&pcie_rc->dbi_res), |
| 282 | MAP_NOCACHE); |
| 283 | |
| 284 | pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f; |
| 285 | if (pcie->mode == PCI_HEADER_TYPE_NORMAL) |
| 286 | return 0; |
| 287 | |
| 288 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 289 | "lut", &pcie_rc->lut_res); |
| 290 | if (!ret) |
| 291 | pcie->lut = map_physmem(pcie_rc->lut_res.start, |
| 292 | fdt_resource_size(&pcie_rc->lut_res), |
| 293 | MAP_NOCACHE); |
| 294 | |
| 295 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 296 | "ctrl", &pcie_rc->ctrl_res); |
| 297 | if (!ret) |
| 298 | pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start, |
| 299 | fdt_resource_size(&pcie_rc->ctrl_res), |
| 300 | MAP_NOCACHE); |
| 301 | if (!pcie->ctrl) |
| 302 | pcie->ctrl = pcie->lut; |
| 303 | |
| 304 | if (!pcie->ctrl) { |
| 305 | printf("%s: NOT find CTRL\n", dev->name); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | ret = fdt_get_named_resource(fdt, node, "reg", "reg-names", |
| 310 | "config", &pcie_rc->cfg_res); |
| 311 | if (ret) { |
| 312 | printf("%s: resource \"config\" not found\n", dev->name); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Fix the pcie memory map address and PF control registers address |
| 318 | * for LS2088A series SoCs |
| 319 | */ |
| 320 | svr = get_svr(); |
| 321 | svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 322 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
| 323 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 324 | svr == SVR_LS2081A || svr == SVR_LS2041A) { |
| 325 | cfg_size = fdt_resource_size(&pcie_rc->cfg_res); |
| 326 | pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR + |
| 327 | LS2088A_PCIE_PHYS_SIZE * pcie->idx; |
| 328 | pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size; |
| 329 | pcie->ctrl = pcie->lut + 0x40000; |
| 330 | } |
| 331 | |
| 332 | pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start, |
| 333 | fdt_resource_size(&pcie_rc->cfg_res), |
| 334 | MAP_NOCACHE); |
| 335 | pcie_rc->cfg1 = pcie_rc->cfg0 + |
| 336 | fdt_resource_size(&pcie_rc->cfg_res) / 2; |
| 337 | |
| 338 | pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian"); |
| 339 | |
| 340 | debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n", |
| 341 | dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut, |
| 342 | (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0, |
| 343 | pcie->big_endian); |
| 344 | |
| 345 | printf("PCIe%u: %s %s", pcie->idx, dev->name, "Root Complex"); |
| 346 | ls_pcie_setup_ctrl(pcie_rc); |
| 347 | |
| 348 | if (!ls_pcie_link_up(pcie)) { |
| 349 | /* Let the user know there's no PCIe link */ |
| 350 | printf(": no link\n"); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /* Print the negotiated PCIe link width */ |
| 355 | link_sta = readw(pcie->dbi + PCIE_LINK_STA); |
| 356 | printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4, |
| 357 | link_sta & PCIE_LINK_SPEED_MASK); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static const struct dm_pci_ops ls_pcie_ops = { |
| 363 | .read_config = ls_pcie_read_config, |
| 364 | .write_config = ls_pcie_write_config, |
| 365 | }; |
| 366 | |
| 367 | static const struct udevice_id ls_pcie_ids[] = { |
| 368 | { .compatible = "fsl,ls-pcie" }, |
| 369 | { } |
| 370 | }; |
| 371 | |
| 372 | U_BOOT_DRIVER(pci_layerscape) = { |
| 373 | .name = "pci_layerscape", |
| 374 | .id = UCLASS_PCI, |
| 375 | .of_match = ls_pcie_ids, |
| 376 | .ops = &ls_pcie_ops, |
| 377 | .probe = ls_pcie_probe, |
| 378 | .priv_auto_alloc_size = sizeof(struct ls_pcie_rc), |
| 379 | }; |