Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Synopsys Designware PCIe host controller driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * Author: Jingoo Han <jg1.han@samsung.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/irqdomain.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_pci.h> |
| 17 | #include <linux/pci_regs.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | |
| 20 | #include "pcie-designware.h" |
| 21 | |
| 22 | static struct pci_ops dw_pcie_ops; |
| 23 | |
| 24 | static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, |
| 25 | u32 *val) |
| 26 | { |
| 27 | struct dw_pcie *pci; |
| 28 | |
| 29 | if (pp->ops->rd_own_conf) |
| 30 | return pp->ops->rd_own_conf(pp, where, size, val); |
| 31 | |
| 32 | pci = to_dw_pcie_from_pp(pp); |
| 33 | return dw_pcie_read(pci->dbi_base + where, size, val); |
| 34 | } |
| 35 | |
| 36 | static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, |
| 37 | u32 val) |
| 38 | { |
| 39 | struct dw_pcie *pci; |
| 40 | |
| 41 | if (pp->ops->wr_own_conf) |
| 42 | return pp->ops->wr_own_conf(pp, where, size, val); |
| 43 | |
| 44 | pci = to_dw_pcie_from_pp(pp); |
| 45 | return dw_pcie_write(pci->dbi_base + where, size, val); |
| 46 | } |
| 47 | |
| 48 | static struct irq_chip dw_msi_irq_chip = { |
| 49 | .name = "PCI-MSI", |
| 50 | .irq_enable = pci_msi_unmask_irq, |
| 51 | .irq_disable = pci_msi_mask_irq, |
| 52 | .irq_mask = pci_msi_mask_irq, |
| 53 | .irq_unmask = pci_msi_unmask_irq, |
| 54 | }; |
| 55 | |
| 56 | /* MSI int handler */ |
| 57 | irqreturn_t dw_handle_msi_irq(struct pcie_port *pp) |
| 58 | { |
Dan Carpenter | 1b497e6 | 2017-03-16 14:34:51 -0500 | [diff] [blame] | 59 | u32 val; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 60 | int i, pos, irq; |
| 61 | irqreturn_t ret = IRQ_NONE; |
| 62 | |
| 63 | for (i = 0; i < MAX_MSI_CTRLS; i++) { |
| 64 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4, |
Dan Carpenter | 1b497e6 | 2017-03-16 14:34:51 -0500 | [diff] [blame] | 65 | &val); |
Bjorn Helgaas | dbe4a09 | 2017-03-16 14:34:59 -0500 | [diff] [blame] | 66 | if (!val) |
| 67 | continue; |
| 68 | |
| 69 | ret = IRQ_HANDLED; |
| 70 | pos = 0; |
Dan Carpenter | 1b497e6 | 2017-03-16 14:34:51 -0500 | [diff] [blame] | 71 | while ((pos = find_next_bit((unsigned long *) &val, 32, |
| 72 | pos)) != 32) { |
Bjorn Helgaas | dbe4a09 | 2017-03-16 14:34:59 -0500 | [diff] [blame] | 73 | irq = irq_find_mapping(pp->irq_domain, i * 32 + pos); |
| 74 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, |
| 75 | 4, 1 << pos); |
| 76 | generic_handle_irq(irq); |
| 77 | pos++; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | void dw_pcie_msi_init(struct pcie_port *pp) |
| 85 | { |
| 86 | u64 msi_target; |
| 87 | |
| 88 | pp->msi_data = __get_free_pages(GFP_KERNEL, 0); |
| 89 | msi_target = virt_to_phys((void *)pp->msi_data); |
| 90 | |
| 91 | /* program the msi_data */ |
| 92 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4, |
| 93 | (u32)(msi_target & 0xffffffff)); |
| 94 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, |
| 95 | (u32)(msi_target >> 32 & 0xffffffff)); |
| 96 | } |
| 97 | |
| 98 | static void dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq) |
| 99 | { |
| 100 | unsigned int res, bit, val; |
| 101 | |
| 102 | res = (irq / 32) * 12; |
| 103 | bit = irq % 32; |
| 104 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val); |
| 105 | val &= ~(1 << bit); |
| 106 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val); |
| 107 | } |
| 108 | |
| 109 | static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base, |
| 110 | unsigned int nvec, unsigned int pos) |
| 111 | { |
| 112 | unsigned int i; |
| 113 | |
| 114 | for (i = 0; i < nvec; i++) { |
| 115 | irq_set_msi_desc_off(irq_base, i, NULL); |
| 116 | /* Disable corresponding interrupt on MSI controller */ |
| 117 | if (pp->ops->msi_clear_irq) |
| 118 | pp->ops->msi_clear_irq(pp, pos + i); |
| 119 | else |
| 120 | dw_pcie_msi_clear_irq(pp, pos + i); |
| 121 | } |
| 122 | |
| 123 | bitmap_release_region(pp->msi_irq_in_use, pos, order_base_2(nvec)); |
| 124 | } |
| 125 | |
| 126 | static void dw_pcie_msi_set_irq(struct pcie_port *pp, int irq) |
| 127 | { |
| 128 | unsigned int res, bit, val; |
| 129 | |
| 130 | res = (irq / 32) * 12; |
| 131 | bit = irq % 32; |
| 132 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val); |
| 133 | val |= 1 << bit; |
| 134 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val); |
| 135 | } |
| 136 | |
| 137 | static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos) |
| 138 | { |
| 139 | int irq, pos0, i; |
| 140 | struct pcie_port *pp; |
| 141 | |
| 142 | pp = (struct pcie_port *)msi_desc_to_pci_sysdata(desc); |
| 143 | pos0 = bitmap_find_free_region(pp->msi_irq_in_use, MAX_MSI_IRQS, |
| 144 | order_base_2(no_irqs)); |
| 145 | if (pos0 < 0) |
| 146 | goto no_valid_irq; |
| 147 | |
| 148 | irq = irq_find_mapping(pp->irq_domain, pos0); |
| 149 | if (!irq) |
| 150 | goto no_valid_irq; |
| 151 | |
| 152 | /* |
| 153 | * irq_create_mapping (called from dw_pcie_host_init) pre-allocates |
| 154 | * descs so there is no need to allocate descs here. We can therefore |
| 155 | * assume that if irq_find_mapping above returns non-zero, then the |
| 156 | * descs are also successfully allocated. |
| 157 | */ |
| 158 | |
| 159 | for (i = 0; i < no_irqs; i++) { |
| 160 | if (irq_set_msi_desc_off(irq, i, desc) != 0) { |
| 161 | clear_irq_range(pp, irq, i, pos0); |
| 162 | goto no_valid_irq; |
| 163 | } |
| 164 | /*Enable corresponding interrupt in MSI interrupt controller */ |
| 165 | if (pp->ops->msi_set_irq) |
| 166 | pp->ops->msi_set_irq(pp, pos0 + i); |
| 167 | else |
| 168 | dw_pcie_msi_set_irq(pp, pos0 + i); |
| 169 | } |
| 170 | |
| 171 | *pos = pos0; |
| 172 | desc->nvec_used = no_irqs; |
| 173 | desc->msi_attrib.multiple = order_base_2(no_irqs); |
| 174 | |
| 175 | return irq; |
| 176 | |
| 177 | no_valid_irq: |
| 178 | *pos = pos0; |
| 179 | return -ENOSPC; |
| 180 | } |
| 181 | |
| 182 | static void dw_msi_setup_msg(struct pcie_port *pp, unsigned int irq, u32 pos) |
| 183 | { |
| 184 | struct msi_msg msg; |
| 185 | u64 msi_target; |
| 186 | |
| 187 | if (pp->ops->get_msi_addr) |
| 188 | msi_target = pp->ops->get_msi_addr(pp); |
| 189 | else |
| 190 | msi_target = virt_to_phys((void *)pp->msi_data); |
| 191 | |
| 192 | msg.address_lo = (u32)(msi_target & 0xffffffff); |
| 193 | msg.address_hi = (u32)(msi_target >> 32 & 0xffffffff); |
| 194 | |
| 195 | if (pp->ops->get_msi_data) |
| 196 | msg.data = pp->ops->get_msi_data(pp, pos); |
| 197 | else |
| 198 | msg.data = pos; |
| 199 | |
| 200 | pci_write_msi_msg(irq, &msg); |
| 201 | } |
| 202 | |
| 203 | static int dw_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev, |
| 204 | struct msi_desc *desc) |
| 205 | { |
| 206 | int irq, pos; |
| 207 | struct pcie_port *pp = pdev->bus->sysdata; |
| 208 | |
| 209 | if (desc->msi_attrib.is_msix) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | irq = assign_irq(1, desc, &pos); |
| 213 | if (irq < 0) |
| 214 | return irq; |
| 215 | |
| 216 | dw_msi_setup_msg(pp, irq, pos); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int dw_msi_setup_irqs(struct msi_controller *chip, struct pci_dev *pdev, |
| 222 | int nvec, int type) |
| 223 | { |
| 224 | #ifdef CONFIG_PCI_MSI |
| 225 | int irq, pos; |
| 226 | struct msi_desc *desc; |
| 227 | struct pcie_port *pp = pdev->bus->sysdata; |
| 228 | |
| 229 | /* MSI-X interrupts are not supported */ |
| 230 | if (type == PCI_CAP_ID_MSIX) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | WARN_ON(!list_is_singular(&pdev->dev.msi_list)); |
| 234 | desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list); |
| 235 | |
| 236 | irq = assign_irq(nvec, desc, &pos); |
| 237 | if (irq < 0) |
| 238 | return irq; |
| 239 | |
| 240 | dw_msi_setup_msg(pp, irq, pos); |
| 241 | |
| 242 | return 0; |
| 243 | #else |
| 244 | return -EINVAL; |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | static void dw_msi_teardown_irq(struct msi_controller *chip, unsigned int irq) |
| 249 | { |
| 250 | struct irq_data *data = irq_get_irq_data(irq); |
| 251 | struct msi_desc *msi = irq_data_get_msi_desc(data); |
| 252 | struct pcie_port *pp = (struct pcie_port *)msi_desc_to_pci_sysdata(msi); |
| 253 | |
| 254 | clear_irq_range(pp, irq, 1, data->hwirq); |
| 255 | } |
| 256 | |
| 257 | static struct msi_controller dw_pcie_msi_chip = { |
| 258 | .setup_irq = dw_msi_setup_irq, |
| 259 | .setup_irqs = dw_msi_setup_irqs, |
| 260 | .teardown_irq = dw_msi_teardown_irq, |
| 261 | }; |
| 262 | |
| 263 | static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq, |
| 264 | irq_hw_number_t hwirq) |
| 265 | { |
| 266 | irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq); |
| 267 | irq_set_chip_data(irq, domain->host_data); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static const struct irq_domain_ops msi_domain_ops = { |
| 273 | .map = dw_pcie_msi_map, |
| 274 | }; |
| 275 | |
| 276 | int dw_pcie_host_init(struct pcie_port *pp) |
| 277 | { |
| 278 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 279 | struct device *dev = pci->dev; |
| 280 | struct device_node *np = dev->of_node; |
| 281 | struct platform_device *pdev = to_platform_device(dev); |
| 282 | struct pci_bus *bus, *child; |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 283 | struct pci_host_bridge *bridge; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 284 | struct resource *cfg_res; |
| 285 | int i, ret; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 286 | struct resource_entry *win, *tmp; |
| 287 | |
| 288 | cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config"); |
| 289 | if (cfg_res) { |
| 290 | pp->cfg0_size = resource_size(cfg_res) / 2; |
| 291 | pp->cfg1_size = resource_size(cfg_res) / 2; |
| 292 | pp->cfg0_base = cfg_res->start; |
| 293 | pp->cfg1_base = cfg_res->start + pp->cfg0_size; |
| 294 | } else if (!pp->va_cfg0_base) { |
| 295 | dev_err(dev, "missing *config* reg space\n"); |
| 296 | } |
| 297 | |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 298 | bridge = pci_alloc_host_bridge(0); |
| 299 | if (!bridge) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | ret = of_pci_get_host_bridge_resources(np, 0, 0xff, |
| 303 | &bridge->windows, &pp->io_base); |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 304 | if (ret) |
| 305 | return ret; |
| 306 | |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 307 | ret = devm_request_pci_bus_resources(dev, &bridge->windows); |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 308 | if (ret) |
| 309 | goto error; |
| 310 | |
| 311 | /* Get the I/O and memory ranges from DT */ |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 312 | resource_list_for_each_entry_safe(win, tmp, &bridge->windows) { |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 313 | switch (resource_type(win->res)) { |
| 314 | case IORESOURCE_IO: |
| 315 | ret = pci_remap_iospace(win->res, pp->io_base); |
| 316 | if (ret) { |
| 317 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 318 | ret, win->res); |
| 319 | resource_list_destroy_entry(win); |
| 320 | } else { |
| 321 | pp->io = win->res; |
| 322 | pp->io->name = "I/O"; |
| 323 | pp->io_size = resource_size(pp->io); |
| 324 | pp->io_bus_addr = pp->io->start - win->offset; |
| 325 | } |
| 326 | break; |
| 327 | case IORESOURCE_MEM: |
| 328 | pp->mem = win->res; |
| 329 | pp->mem->name = "MEM"; |
| 330 | pp->mem_size = resource_size(pp->mem); |
| 331 | pp->mem_bus_addr = pp->mem->start - win->offset; |
| 332 | break; |
| 333 | case 0: |
| 334 | pp->cfg = win->res; |
| 335 | pp->cfg0_size = resource_size(pp->cfg) / 2; |
| 336 | pp->cfg1_size = resource_size(pp->cfg) / 2; |
| 337 | pp->cfg0_base = pp->cfg->start; |
| 338 | pp->cfg1_base = pp->cfg->start + pp->cfg0_size; |
| 339 | break; |
| 340 | case IORESOURCE_BUS: |
| 341 | pp->busn = win->res; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (!pci->dbi_base) { |
Lorenzo Pieralisi | cc7b0d4 | 2017-04-19 17:49:03 +0100 | [diff] [blame] | 347 | pci->dbi_base = devm_pci_remap_cfgspace(dev, |
| 348 | pp->cfg->start, |
| 349 | resource_size(pp->cfg)); |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 350 | if (!pci->dbi_base) { |
| 351 | dev_err(dev, "error with ioremap\n"); |
| 352 | ret = -ENOMEM; |
| 353 | goto error; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | pp->mem_base = pp->mem->start; |
| 358 | |
| 359 | if (!pp->va_cfg0_base) { |
Lorenzo Pieralisi | cc7b0d4 | 2017-04-19 17:49:03 +0100 | [diff] [blame] | 360 | pp->va_cfg0_base = devm_pci_remap_cfgspace(dev, |
| 361 | pp->cfg0_base, pp->cfg0_size); |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 362 | if (!pp->va_cfg0_base) { |
| 363 | dev_err(dev, "error with ioremap in function\n"); |
| 364 | ret = -ENOMEM; |
| 365 | goto error; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (!pp->va_cfg1_base) { |
Lorenzo Pieralisi | cc7b0d4 | 2017-04-19 17:49:03 +0100 | [diff] [blame] | 370 | pp->va_cfg1_base = devm_pci_remap_cfgspace(dev, |
| 371 | pp->cfg1_base, |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 372 | pp->cfg1_size); |
| 373 | if (!pp->va_cfg1_base) { |
| 374 | dev_err(dev, "error with ioremap\n"); |
| 375 | ret = -ENOMEM; |
| 376 | goto error; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport); |
| 381 | if (ret) |
| 382 | pci->num_viewport = 2; |
| 383 | |
| 384 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 385 | if (!pp->ops->msi_host_init) { |
| 386 | pp->irq_domain = irq_domain_add_linear(dev->of_node, |
| 387 | MAX_MSI_IRQS, &msi_domain_ops, |
| 388 | &dw_pcie_msi_chip); |
| 389 | if (!pp->irq_domain) { |
| 390 | dev_err(dev, "irq domain init failed\n"); |
| 391 | ret = -ENXIO; |
| 392 | goto error; |
| 393 | } |
| 394 | |
| 395 | for (i = 0; i < MAX_MSI_IRQS; i++) |
| 396 | irq_create_mapping(pp->irq_domain, i); |
| 397 | } else { |
| 398 | ret = pp->ops->msi_host_init(pp, &dw_pcie_msi_chip); |
| 399 | if (ret < 0) |
| 400 | goto error; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (pp->ops->host_init) |
| 405 | pp->ops->host_init(pp); |
| 406 | |
| 407 | pp->root_bus_nr = pp->busn->start; |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 408 | |
| 409 | bridge->dev.parent = dev; |
| 410 | bridge->sysdata = pp; |
| 411 | bridge->busnr = pp->root_bus_nr; |
| 412 | bridge->ops = &dw_pcie_ops; |
Lorenzo Pieralisi | 60eca19 | 2017-06-28 15:14:07 -0500 | [diff] [blame] | 413 | bridge->map_irq = of_irq_parse_and_map_pci; |
| 414 | bridge->swizzle_irq = pci_common_swizzle; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 415 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 416 | bridge->msi = &dw_pcie_msi_chip; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 417 | dw_pcie_msi_chip.dev = dev; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 418 | } |
| 419 | |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 420 | ret = pci_scan_root_bus_bridge(bridge); |
| 421 | if (ret) |
| 422 | goto error; |
| 423 | |
| 424 | bus = bridge->bus; |
| 425 | |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 426 | if (pp->ops->scan_bus) |
| 427 | pp->ops->scan_bus(pp); |
| 428 | |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 429 | pci_bus_size_bridges(bus); |
| 430 | pci_bus_assign_resources(bus); |
| 431 | |
| 432 | list_for_each_entry(child, &bus->children, node) |
| 433 | pcie_bus_configure_settings(child); |
| 434 | |
| 435 | pci_bus_add_devices(bus); |
| 436 | return 0; |
| 437 | |
| 438 | error: |
Lorenzo Pieralisi | 295aeb9 | 2017-06-28 15:13:56 -0500 | [diff] [blame] | 439 | pci_free_host_bridge(bridge); |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 444 | u32 devfn, int where, int size, u32 *val) |
| 445 | { |
| 446 | int ret, type; |
| 447 | u32 busdev, cfg_size; |
| 448 | u64 cpu_addr; |
| 449 | void __iomem *va_cfg_base; |
| 450 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 451 | |
| 452 | if (pp->ops->rd_other_conf) |
| 453 | return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val); |
| 454 | |
| 455 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | |
| 456 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); |
| 457 | |
| 458 | if (bus->parent->number == pp->root_bus_nr) { |
| 459 | type = PCIE_ATU_TYPE_CFG0; |
| 460 | cpu_addr = pp->cfg0_base; |
| 461 | cfg_size = pp->cfg0_size; |
| 462 | va_cfg_base = pp->va_cfg0_base; |
| 463 | } else { |
| 464 | type = PCIE_ATU_TYPE_CFG1; |
| 465 | cpu_addr = pp->cfg1_base; |
| 466 | cfg_size = pp->cfg1_size; |
| 467 | va_cfg_base = pp->va_cfg1_base; |
| 468 | } |
| 469 | |
| 470 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, |
| 471 | type, cpu_addr, |
| 472 | busdev, cfg_size); |
| 473 | ret = dw_pcie_read(va_cfg_base + where, size, val); |
| 474 | if (pci->num_viewport <= 2) |
| 475 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, |
| 476 | PCIE_ATU_TYPE_IO, pp->io_base, |
| 477 | pp->io_bus_addr, pp->io_size); |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 483 | u32 devfn, int where, int size, u32 val) |
| 484 | { |
| 485 | int ret, type; |
| 486 | u32 busdev, cfg_size; |
| 487 | u64 cpu_addr; |
| 488 | void __iomem *va_cfg_base; |
| 489 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 490 | |
| 491 | if (pp->ops->wr_other_conf) |
| 492 | return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val); |
| 493 | |
| 494 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | |
| 495 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); |
| 496 | |
| 497 | if (bus->parent->number == pp->root_bus_nr) { |
| 498 | type = PCIE_ATU_TYPE_CFG0; |
| 499 | cpu_addr = pp->cfg0_base; |
| 500 | cfg_size = pp->cfg0_size; |
| 501 | va_cfg_base = pp->va_cfg0_base; |
| 502 | } else { |
| 503 | type = PCIE_ATU_TYPE_CFG1; |
| 504 | cpu_addr = pp->cfg1_base; |
| 505 | cfg_size = pp->cfg1_size; |
| 506 | va_cfg_base = pp->va_cfg1_base; |
| 507 | } |
| 508 | |
| 509 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, |
| 510 | type, cpu_addr, |
| 511 | busdev, cfg_size); |
| 512 | ret = dw_pcie_write(va_cfg_base + where, size, val); |
| 513 | if (pci->num_viewport <= 2) |
| 514 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, |
| 515 | PCIE_ATU_TYPE_IO, pp->io_base, |
| 516 | pp->io_bus_addr, pp->io_size); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus, |
| 522 | int dev) |
| 523 | { |
| 524 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 525 | |
| 526 | /* If there is no link, then there is no device */ |
| 527 | if (bus->number != pp->root_bus_nr) { |
| 528 | if (!dw_pcie_link_up(pci)) |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | /* access only one slot on each root port */ |
| 533 | if (bus->number == pp->root_bus_nr && dev > 0) |
| 534 | return 0; |
| 535 | |
| 536 | return 1; |
| 537 | } |
| 538 | |
| 539 | static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 540 | int size, u32 *val) |
| 541 | { |
| 542 | struct pcie_port *pp = bus->sysdata; |
| 543 | |
| 544 | if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) { |
| 545 | *val = 0xffffffff; |
| 546 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 547 | } |
| 548 | |
| 549 | if (bus->number == pp->root_bus_nr) |
| 550 | return dw_pcie_rd_own_conf(pp, where, size, val); |
| 551 | |
| 552 | return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val); |
| 553 | } |
| 554 | |
| 555 | static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 556 | int where, int size, u32 val) |
| 557 | { |
| 558 | struct pcie_port *pp = bus->sysdata; |
| 559 | |
| 560 | if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) |
| 561 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 562 | |
| 563 | if (bus->number == pp->root_bus_nr) |
| 564 | return dw_pcie_wr_own_conf(pp, where, size, val); |
| 565 | |
| 566 | return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val); |
| 567 | } |
| 568 | |
| 569 | static struct pci_ops dw_pcie_ops = { |
| 570 | .read = dw_pcie_rd_conf, |
| 571 | .write = dw_pcie_wr_conf, |
| 572 | }; |
| 573 | |
| 574 | static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci) |
| 575 | { |
| 576 | u32 val; |
| 577 | |
| 578 | val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT); |
| 579 | if (val == 0xffffffff) |
| 580 | return 1; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | void dw_pcie_setup_rc(struct pcie_port *pp) |
| 586 | { |
| 587 | u32 val; |
| 588 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 589 | |
| 590 | dw_pcie_setup(pci); |
| 591 | |
| 592 | /* setup RC BARs */ |
| 593 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004); |
| 594 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000); |
| 595 | |
| 596 | /* setup interrupt pins */ |
| 597 | val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE); |
| 598 | val &= 0xffff00ff; |
| 599 | val |= 0x00000100; |
| 600 | dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val); |
| 601 | |
| 602 | /* setup bus numbers */ |
| 603 | val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS); |
| 604 | val &= 0xff000000; |
| 605 | val |= 0x00010100; |
| 606 | dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val); |
| 607 | |
| 608 | /* setup command register */ |
| 609 | val = dw_pcie_readl_dbi(pci, PCI_COMMAND); |
| 610 | val &= 0xffff0000; |
| 611 | val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
| 612 | PCI_COMMAND_MASTER | PCI_COMMAND_SERR; |
| 613 | dw_pcie_writel_dbi(pci, PCI_COMMAND, val); |
| 614 | |
| 615 | /* |
| 616 | * If the platform provides ->rd_other_conf, it means the platform |
| 617 | * uses its own address translation component rather than ATU, so |
| 618 | * we should not program the ATU here. |
| 619 | */ |
| 620 | if (!pp->ops->rd_other_conf) { |
| 621 | /* get iATU unroll support */ |
| 622 | pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci); |
| 623 | dev_dbg(pci->dev, "iATU unroll: %s\n", |
| 624 | pci->iatu_unroll_enabled ? "enabled" : "disabled"); |
| 625 | |
| 626 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0, |
| 627 | PCIE_ATU_TYPE_MEM, pp->mem_base, |
| 628 | pp->mem_bus_addr, pp->mem_size); |
| 629 | if (pci->num_viewport > 2) |
| 630 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2, |
| 631 | PCIE_ATU_TYPE_IO, pp->io_base, |
| 632 | pp->io_bus_addr, pp->io_size); |
| 633 | } |
| 634 | |
| 635 | dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0); |
| 636 | |
| 637 | /* program correct class for RC */ |
| 638 | dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI); |
| 639 | |
| 640 | dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val); |
| 641 | val |= PORT_LOGIC_SPEED_CHANGE; |
| 642 | dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val); |
| 643 | } |