diff options
Diffstat (limited to 'drivers/pci/controller/dwc/pcie-designware-host.c')
-rw-r--r-- | drivers/pci/controller/dwc/pcie-designware-host.c | 722 |
1 files changed, 722 insertions, 0 deletions
diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c new file mode 100644 index 000000000000..781aa03aeede --- /dev/null +++ b/drivers/pci/controller/dwc/pcie-designware-host.c | |||
@@ -0,0 +1,722 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Synopsys DesignWare PCIe host controller driver | ||
4 | * | ||
5 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. | ||
6 | * http://www.samsung.com | ||
7 | * | ||
8 | * Author: Jingoo Han <jg1.han@samsung.com> | ||
9 | */ | ||
10 | |||
11 | #include <linux/irqchip/chained_irq.h> | ||
12 | #include <linux/irqdomain.h> | ||
13 | #include <linux/of_address.h> | ||
14 | #include <linux/of_pci.h> | ||
15 | #include <linux/pci_regs.h> | ||
16 | #include <linux/platform_device.h> | ||
17 | |||
18 | #include "../../pci.h" | ||
19 | #include "pcie-designware.h" | ||
20 | |||
21 | static struct pci_ops dw_pcie_ops; | ||
22 | |||
23 | static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, | ||
24 | u32 *val) | ||
25 | { | ||
26 | struct dw_pcie *pci; | ||
27 | |||
28 | if (pp->ops->rd_own_conf) | ||
29 | return pp->ops->rd_own_conf(pp, where, size, val); | ||
30 | |||
31 | pci = to_dw_pcie_from_pp(pp); | ||
32 | return dw_pcie_read(pci->dbi_base + where, size, val); | ||
33 | } | ||
34 | |||
35 | static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, | ||
36 | u32 val) | ||
37 | { | ||
38 | struct dw_pcie *pci; | ||
39 | |||
40 | if (pp->ops->wr_own_conf) | ||
41 | return pp->ops->wr_own_conf(pp, where, size, val); | ||
42 | |||
43 | pci = to_dw_pcie_from_pp(pp); | ||
44 | return dw_pcie_write(pci->dbi_base + where, size, val); | ||
45 | } | ||
46 | |||
47 | static void dw_msi_ack_irq(struct irq_data *d) | ||
48 | { | ||
49 | irq_chip_ack_parent(d); | ||
50 | } | ||
51 | |||
52 | static void dw_msi_mask_irq(struct irq_data *d) | ||
53 | { | ||
54 | pci_msi_mask_irq(d); | ||
55 | irq_chip_mask_parent(d); | ||
56 | } | ||
57 | |||
58 | static void dw_msi_unmask_irq(struct irq_data *d) | ||
59 | { | ||
60 | pci_msi_unmask_irq(d); | ||
61 | irq_chip_unmask_parent(d); | ||
62 | } | ||
63 | |||
64 | static struct irq_chip dw_pcie_msi_irq_chip = { | ||
65 | .name = "PCI-MSI", | ||
66 | .irq_ack = dw_msi_ack_irq, | ||
67 | .irq_mask = dw_msi_mask_irq, | ||
68 | .irq_unmask = dw_msi_unmask_irq, | ||
69 | }; | ||
70 | |||
71 | static struct msi_domain_info dw_pcie_msi_domain_info = { | ||
72 | .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | | ||
73 | MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI), | ||
74 | .chip = &dw_pcie_msi_irq_chip, | ||
75 | }; | ||
76 | |||
77 | /* MSI int handler */ | ||
78 | irqreturn_t dw_handle_msi_irq(struct pcie_port *pp) | ||
79 | { | ||
80 | int i, pos, irq; | ||
81 | u32 val, num_ctrls; | ||
82 | irqreturn_t ret = IRQ_NONE; | ||
83 | |||
84 | num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL; | ||
85 | |||
86 | for (i = 0; i < num_ctrls; i++) { | ||
87 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + | ||
88 | (i * MSI_REG_CTRL_BLOCK_SIZE), | ||
89 | 4, &val); | ||
90 | if (!val) | ||
91 | continue; | ||
92 | |||
93 | ret = IRQ_HANDLED; | ||
94 | pos = 0; | ||
95 | while ((pos = find_next_bit((unsigned long *) &val, | ||
96 | MAX_MSI_IRQS_PER_CTRL, | ||
97 | pos)) != MAX_MSI_IRQS_PER_CTRL) { | ||
98 | irq = irq_find_mapping(pp->irq_domain, | ||
99 | (i * MAX_MSI_IRQS_PER_CTRL) + | ||
100 | pos); | ||
101 | generic_handle_irq(irq); | ||
102 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + | ||
103 | (i * MSI_REG_CTRL_BLOCK_SIZE), | ||
104 | 4, 1 << pos); | ||
105 | pos++; | ||
106 | } | ||
107 | } | ||
108 | |||
109 | return ret; | ||
110 | } | ||
111 | |||
112 | /* Chained MSI interrupt service routine */ | ||
113 | static void dw_chained_msi_isr(struct irq_desc *desc) | ||
114 | { | ||
115 | struct irq_chip *chip = irq_desc_get_chip(desc); | ||
116 | struct pcie_port *pp; | ||
117 | |||
118 | chained_irq_enter(chip, desc); | ||
119 | |||
120 | pp = irq_desc_get_handler_data(desc); | ||
121 | dw_handle_msi_irq(pp); | ||
122 | |||
123 | chained_irq_exit(chip, desc); | ||
124 | } | ||
125 | |||
126 | static void dw_pci_setup_msi_msg(struct irq_data *data, struct msi_msg *msg) | ||
127 | { | ||
128 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); | ||
129 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
130 | u64 msi_target; | ||
131 | |||
132 | if (pp->ops->get_msi_addr) | ||
133 | msi_target = pp->ops->get_msi_addr(pp); | ||
134 | else | ||
135 | msi_target = (u64)pp->msi_data; | ||
136 | |||
137 | msg->address_lo = lower_32_bits(msi_target); | ||
138 | msg->address_hi = upper_32_bits(msi_target); | ||
139 | |||
140 | if (pp->ops->get_msi_data) | ||
141 | msg->data = pp->ops->get_msi_data(pp, data->hwirq); | ||
142 | else | ||
143 | msg->data = data->hwirq; | ||
144 | |||
145 | dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n", | ||
146 | (int)data->hwirq, msg->address_hi, msg->address_lo); | ||
147 | } | ||
148 | |||
149 | static int dw_pci_msi_set_affinity(struct irq_data *irq_data, | ||
150 | const struct cpumask *mask, bool force) | ||
151 | { | ||
152 | return -EINVAL; | ||
153 | } | ||
154 | |||
155 | static void dw_pci_bottom_mask(struct irq_data *data) | ||
156 | { | ||
157 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); | ||
158 | unsigned int res, bit, ctrl; | ||
159 | unsigned long flags; | ||
160 | |||
161 | raw_spin_lock_irqsave(&pp->lock, flags); | ||
162 | |||
163 | if (pp->ops->msi_clear_irq) { | ||
164 | pp->ops->msi_clear_irq(pp, data->hwirq); | ||
165 | } else { | ||
166 | ctrl = data->hwirq / MAX_MSI_IRQS_PER_CTRL; | ||
167 | res = ctrl * MSI_REG_CTRL_BLOCK_SIZE; | ||
168 | bit = data->hwirq % MAX_MSI_IRQS_PER_CTRL; | ||
169 | |||
170 | pp->irq_status[ctrl] &= ~(1 << bit); | ||
171 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, | ||
172 | pp->irq_status[ctrl]); | ||
173 | } | ||
174 | |||
175 | raw_spin_unlock_irqrestore(&pp->lock, flags); | ||
176 | } | ||
177 | |||
178 | static void dw_pci_bottom_unmask(struct irq_data *data) | ||
179 | { | ||
180 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); | ||
181 | unsigned int res, bit, ctrl; | ||
182 | unsigned long flags; | ||
183 | |||
184 | raw_spin_lock_irqsave(&pp->lock, flags); | ||
185 | |||
186 | if (pp->ops->msi_set_irq) { | ||
187 | pp->ops->msi_set_irq(pp, data->hwirq); | ||
188 | } else { | ||
189 | ctrl = data->hwirq / MAX_MSI_IRQS_PER_CTRL; | ||
190 | res = ctrl * MSI_REG_CTRL_BLOCK_SIZE; | ||
191 | bit = data->hwirq % MAX_MSI_IRQS_PER_CTRL; | ||
192 | |||
193 | pp->irq_status[ctrl] |= 1 << bit; | ||
194 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, | ||
195 | pp->irq_status[ctrl]); | ||
196 | } | ||
197 | |||
198 | raw_spin_unlock_irqrestore(&pp->lock, flags); | ||
199 | } | ||
200 | |||
201 | static void dw_pci_bottom_ack(struct irq_data *d) | ||
202 | { | ||
203 | struct msi_desc *msi = irq_data_get_msi_desc(d); | ||
204 | struct pcie_port *pp; | ||
205 | |||
206 | pp = msi_desc_to_pci_sysdata(msi); | ||
207 | |||
208 | if (pp->ops->msi_irq_ack) | ||
209 | pp->ops->msi_irq_ack(d->hwirq, pp); | ||
210 | } | ||
211 | |||
212 | static struct irq_chip dw_pci_msi_bottom_irq_chip = { | ||
213 | .name = "DWPCI-MSI", | ||
214 | .irq_ack = dw_pci_bottom_ack, | ||
215 | .irq_compose_msi_msg = dw_pci_setup_msi_msg, | ||
216 | .irq_set_affinity = dw_pci_msi_set_affinity, | ||
217 | .irq_mask = dw_pci_bottom_mask, | ||
218 | .irq_unmask = dw_pci_bottom_unmask, | ||
219 | }; | ||
220 | |||
221 | static int dw_pcie_irq_domain_alloc(struct irq_domain *domain, | ||
222 | unsigned int virq, unsigned int nr_irqs, | ||
223 | void *args) | ||
224 | { | ||
225 | struct pcie_port *pp = domain->host_data; | ||
226 | unsigned long flags; | ||
227 | u32 i; | ||
228 | int bit; | ||
229 | |||
230 | raw_spin_lock_irqsave(&pp->lock, flags); | ||
231 | |||
232 | bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors, | ||
233 | order_base_2(nr_irqs)); | ||
234 | |||
235 | raw_spin_unlock_irqrestore(&pp->lock, flags); | ||
236 | |||
237 | if (bit < 0) | ||
238 | return -ENOSPC; | ||
239 | |||
240 | for (i = 0; i < nr_irqs; i++) | ||
241 | irq_domain_set_info(domain, virq + i, bit + i, | ||
242 | &dw_pci_msi_bottom_irq_chip, | ||
243 | pp, handle_edge_irq, | ||
244 | NULL, NULL); | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | static void dw_pcie_irq_domain_free(struct irq_domain *domain, | ||
250 | unsigned int virq, unsigned int nr_irqs) | ||
251 | { | ||
252 | struct irq_data *data = irq_domain_get_irq_data(domain, virq); | ||
253 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); | ||
254 | unsigned long flags; | ||
255 | |||
256 | raw_spin_lock_irqsave(&pp->lock, flags); | ||
257 | |||
258 | bitmap_release_region(pp->msi_irq_in_use, data->hwirq, | ||
259 | order_base_2(nr_irqs)); | ||
260 | |||
261 | raw_spin_unlock_irqrestore(&pp->lock, flags); | ||
262 | } | ||
263 | |||
264 | static const struct irq_domain_ops dw_pcie_msi_domain_ops = { | ||
265 | .alloc = dw_pcie_irq_domain_alloc, | ||
266 | .free = dw_pcie_irq_domain_free, | ||
267 | }; | ||
268 | |||
269 | int dw_pcie_allocate_domains(struct pcie_port *pp) | ||
270 | { | ||
271 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
272 | struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node); | ||
273 | |||
274 | pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors, | ||
275 | &dw_pcie_msi_domain_ops, pp); | ||
276 | if (!pp->irq_domain) { | ||
277 | dev_err(pci->dev, "Failed to create IRQ domain\n"); | ||
278 | return -ENOMEM; | ||
279 | } | ||
280 | |||
281 | pp->msi_domain = pci_msi_create_irq_domain(fwnode, | ||
282 | &dw_pcie_msi_domain_info, | ||
283 | pp->irq_domain); | ||
284 | if (!pp->msi_domain) { | ||
285 | dev_err(pci->dev, "Failed to create MSI domain\n"); | ||
286 | irq_domain_remove(pp->irq_domain); | ||
287 | return -ENOMEM; | ||
288 | } | ||
289 | |||
290 | return 0; | ||
291 | } | ||
292 | |||
293 | void dw_pcie_free_msi(struct pcie_port *pp) | ||
294 | { | ||
295 | irq_set_chained_handler(pp->msi_irq, NULL); | ||
296 | irq_set_handler_data(pp->msi_irq, NULL); | ||
297 | |||
298 | irq_domain_remove(pp->msi_domain); | ||
299 | irq_domain_remove(pp->irq_domain); | ||
300 | } | ||
301 | |||
302 | void dw_pcie_msi_init(struct pcie_port *pp) | ||
303 | { | ||
304 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
305 | struct device *dev = pci->dev; | ||
306 | struct page *page; | ||
307 | u64 msi_target; | ||
308 | |||
309 | page = alloc_page(GFP_KERNEL); | ||
310 | pp->msi_data = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE); | ||
311 | if (dma_mapping_error(dev, pp->msi_data)) { | ||
312 | dev_err(dev, "Failed to map MSI data\n"); | ||
313 | __free_page(page); | ||
314 | return; | ||
315 | } | ||
316 | msi_target = (u64)pp->msi_data; | ||
317 | |||
318 | /* Program the msi_data */ | ||
319 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4, | ||
320 | lower_32_bits(msi_target)); | ||
321 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, | ||
322 | upper_32_bits(msi_target)); | ||
323 | } | ||
324 | |||
325 | int dw_pcie_host_init(struct pcie_port *pp) | ||
326 | { | ||
327 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
328 | struct device *dev = pci->dev; | ||
329 | struct device_node *np = dev->of_node; | ||
330 | struct platform_device *pdev = to_platform_device(dev); | ||
331 | struct resource_entry *win, *tmp; | ||
332 | struct pci_bus *bus, *child; | ||
333 | struct pci_host_bridge *bridge; | ||
334 | struct resource *cfg_res; | ||
335 | int ret; | ||
336 | |||
337 | raw_spin_lock_init(&pci->pp.lock); | ||
338 | |||
339 | cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config"); | ||
340 | if (cfg_res) { | ||
341 | pp->cfg0_size = resource_size(cfg_res) >> 1; | ||
342 | pp->cfg1_size = resource_size(cfg_res) >> 1; | ||
343 | pp->cfg0_base = cfg_res->start; | ||
344 | pp->cfg1_base = cfg_res->start + pp->cfg0_size; | ||
345 | } else if (!pp->va_cfg0_base) { | ||
346 | dev_err(dev, "Missing *config* reg space\n"); | ||
347 | } | ||
348 | |||
349 | bridge = pci_alloc_host_bridge(0); | ||
350 | if (!bridge) | ||
351 | return -ENOMEM; | ||
352 | |||
353 | ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, | ||
354 | &bridge->windows, &pp->io_base); | ||
355 | if (ret) | ||
356 | return ret; | ||
357 | |||
358 | ret = devm_request_pci_bus_resources(dev, &bridge->windows); | ||
359 | if (ret) | ||
360 | goto error; | ||
361 | |||
362 | /* Get the I/O and memory ranges from DT */ | ||
363 | resource_list_for_each_entry_safe(win, tmp, &bridge->windows) { | ||
364 | switch (resource_type(win->res)) { | ||
365 | case IORESOURCE_IO: | ||
366 | ret = pci_remap_iospace(win->res, pp->io_base); | ||
367 | if (ret) { | ||
368 | dev_warn(dev, "Error %d: failed to map resource %pR\n", | ||
369 | ret, win->res); | ||
370 | resource_list_destroy_entry(win); | ||
371 | } else { | ||
372 | pp->io = win->res; | ||
373 | pp->io->name = "I/O"; | ||
374 | pp->io_size = resource_size(pp->io); | ||
375 | pp->io_bus_addr = pp->io->start - win->offset; | ||
376 | } | ||
377 | break; | ||
378 | case IORESOURCE_MEM: | ||
379 | pp->mem = win->res; | ||
380 | pp->mem->name = "MEM"; | ||
381 | pp->mem_size = resource_size(pp->mem); | ||
382 | pp->mem_bus_addr = pp->mem->start - win->offset; | ||
383 | break; | ||
384 | case 0: | ||
385 | pp->cfg = win->res; | ||
386 | pp->cfg0_size = resource_size(pp->cfg) >> 1; | ||
387 | pp->cfg1_size = resource_size(pp->cfg) >> 1; | ||
388 | pp->cfg0_base = pp->cfg->start; | ||
389 | pp->cfg1_base = pp->cfg->start + pp->cfg0_size; | ||
390 | break; | ||
391 | case IORESOURCE_BUS: | ||
392 | pp->busn = win->res; | ||
393 | break; | ||
394 | } | ||
395 | } | ||
396 | |||
397 | if (!pci->dbi_base) { | ||
398 | pci->dbi_base = devm_pci_remap_cfgspace(dev, | ||
399 | pp->cfg->start, | ||
400 | resource_size(pp->cfg)); | ||
401 | if (!pci->dbi_base) { | ||
402 | dev_err(dev, "Error with ioremap\n"); | ||
403 | ret = -ENOMEM; | ||
404 | goto error; | ||
405 | } | ||
406 | } | ||
407 | |||
408 | pp->mem_base = pp->mem->start; | ||
409 | |||
410 | if (!pp->va_cfg0_base) { | ||
411 | pp->va_cfg0_base = devm_pci_remap_cfgspace(dev, | ||
412 | pp->cfg0_base, pp->cfg0_size); | ||
413 | if (!pp->va_cfg0_base) { | ||
414 | dev_err(dev, "Error with ioremap in function\n"); | ||
415 | ret = -ENOMEM; | ||
416 | goto error; | ||
417 | } | ||
418 | } | ||
419 | |||
420 | if (!pp->va_cfg1_base) { | ||
421 | pp->va_cfg1_base = devm_pci_remap_cfgspace(dev, | ||
422 | pp->cfg1_base, | ||
423 | pp->cfg1_size); | ||
424 | if (!pp->va_cfg1_base) { | ||
425 | dev_err(dev, "Error with ioremap\n"); | ||
426 | ret = -ENOMEM; | ||
427 | goto error; | ||
428 | } | ||
429 | } | ||
430 | |||
431 | ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport); | ||
432 | if (ret) | ||
433 | pci->num_viewport = 2; | ||
434 | |||
435 | if (IS_ENABLED(CONFIG_PCI_MSI)) { | ||
436 | /* | ||
437 | * If a specific SoC driver needs to change the | ||
438 | * default number of vectors, it needs to implement | ||
439 | * the set_num_vectors callback. | ||
440 | */ | ||
441 | if (!pp->ops->set_num_vectors) { | ||
442 | pp->num_vectors = MSI_DEF_NUM_VECTORS; | ||
443 | } else { | ||
444 | pp->ops->set_num_vectors(pp); | ||
445 | |||
446 | if (pp->num_vectors > MAX_MSI_IRQS || | ||
447 | pp->num_vectors == 0) { | ||
448 | dev_err(dev, | ||
449 | "Invalid number of vectors\n"); | ||
450 | goto error; | ||
451 | } | ||
452 | } | ||
453 | |||
454 | if (!pp->ops->msi_host_init) { | ||
455 | ret = dw_pcie_allocate_domains(pp); | ||
456 | if (ret) | ||
457 | goto error; | ||
458 | |||
459 | if (pp->msi_irq) | ||
460 | irq_set_chained_handler_and_data(pp->msi_irq, | ||
461 | dw_chained_msi_isr, | ||
462 | pp); | ||
463 | } else { | ||
464 | ret = pp->ops->msi_host_init(pp); | ||
465 | if (ret < 0) | ||
466 | goto error; | ||
467 | } | ||
468 | } | ||
469 | |||
470 | if (pp->ops->host_init) { | ||
471 | ret = pp->ops->host_init(pp); | ||
472 | if (ret) | ||
473 | goto error; | ||
474 | } | ||
475 | |||
476 | pp->root_bus_nr = pp->busn->start; | ||
477 | |||
478 | bridge->dev.parent = dev; | ||
479 | bridge->sysdata = pp; | ||
480 | bridge->busnr = pp->root_bus_nr; | ||
481 | bridge->ops = &dw_pcie_ops; | ||
482 | bridge->map_irq = of_irq_parse_and_map_pci; | ||
483 | bridge->swizzle_irq = pci_common_swizzle; | ||
484 | |||
485 | ret = pci_scan_root_bus_bridge(bridge); | ||
486 | if (ret) | ||
487 | goto error; | ||
488 | |||
489 | bus = bridge->bus; | ||
490 | |||
491 | if (pp->ops->scan_bus) | ||
492 | pp->ops->scan_bus(pp); | ||
493 | |||
494 | pci_bus_size_bridges(bus); | ||
495 | pci_bus_assign_resources(bus); | ||
496 | |||
497 | list_for_each_entry(child, &bus->children, node) | ||
498 | pcie_bus_configure_settings(child); | ||
499 | |||
500 | pci_bus_add_devices(bus); | ||
501 | return 0; | ||
502 | |||
503 | error: | ||
504 | pci_free_host_bridge(bridge); | ||
505 | return ret; | ||
506 | } | ||
507 | |||
508 | static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus, | ||
509 | u32 devfn, int where, int size, u32 *val) | ||
510 | { | ||
511 | int ret, type; | ||
512 | u32 busdev, cfg_size; | ||
513 | u64 cpu_addr; | ||
514 | void __iomem *va_cfg_base; | ||
515 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
516 | |||
517 | if (pp->ops->rd_other_conf) | ||
518 | return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val); | ||
519 | |||
520 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | | ||
521 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); | ||
522 | |||
523 | if (bus->parent->number == pp->root_bus_nr) { | ||
524 | type = PCIE_ATU_TYPE_CFG0; | ||
525 | cpu_addr = pp->cfg0_base; | ||
526 | cfg_size = pp->cfg0_size; | ||
527 | va_cfg_base = pp->va_cfg0_base; | ||
528 | } else { | ||
529 | type = PCIE_ATU_TYPE_CFG1; | ||
530 | cpu_addr = pp->cfg1_base; | ||
531 | cfg_size = pp->cfg1_size; | ||
532 | va_cfg_base = pp->va_cfg1_base; | ||
533 | } | ||
534 | |||
535 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, | ||
536 | type, cpu_addr, | ||
537 | busdev, cfg_size); | ||
538 | ret = dw_pcie_read(va_cfg_base + where, size, val); | ||
539 | if (pci->num_viewport <= 2) | ||
540 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, | ||
541 | PCIE_ATU_TYPE_IO, pp->io_base, | ||
542 | pp->io_bus_addr, pp->io_size); | ||
543 | |||
544 | return ret; | ||
545 | } | ||
546 | |||
547 | static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus, | ||
548 | u32 devfn, int where, int size, u32 val) | ||
549 | { | ||
550 | int ret, type; | ||
551 | u32 busdev, cfg_size; | ||
552 | u64 cpu_addr; | ||
553 | void __iomem *va_cfg_base; | ||
554 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
555 | |||
556 | if (pp->ops->wr_other_conf) | ||
557 | return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val); | ||
558 | |||
559 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | | ||
560 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); | ||
561 | |||
562 | if (bus->parent->number == pp->root_bus_nr) { | ||
563 | type = PCIE_ATU_TYPE_CFG0; | ||
564 | cpu_addr = pp->cfg0_base; | ||
565 | cfg_size = pp->cfg0_size; | ||
566 | va_cfg_base = pp->va_cfg0_base; | ||
567 | } else { | ||
568 | type = PCIE_ATU_TYPE_CFG1; | ||
569 | cpu_addr = pp->cfg1_base; | ||
570 | cfg_size = pp->cfg1_size; | ||
571 | va_cfg_base = pp->va_cfg1_base; | ||
572 | } | ||
573 | |||
574 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, | ||
575 | type, cpu_addr, | ||
576 | busdev, cfg_size); | ||
577 | ret = dw_pcie_write(va_cfg_base + where, size, val); | ||
578 | if (pci->num_viewport <= 2) | ||
579 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1, | ||
580 | PCIE_ATU_TYPE_IO, pp->io_base, | ||
581 | pp->io_bus_addr, pp->io_size); | ||
582 | |||
583 | return ret; | ||
584 | } | ||
585 | |||
586 | static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus, | ||
587 | int dev) | ||
588 | { | ||
589 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
590 | |||
591 | /* If there is no link, then there is no device */ | ||
592 | if (bus->number != pp->root_bus_nr) { | ||
593 | if (!dw_pcie_link_up(pci)) | ||
594 | return 0; | ||
595 | } | ||
596 | |||
597 | /* Access only one slot on each root port */ | ||
598 | if (bus->number == pp->root_bus_nr && dev > 0) | ||
599 | return 0; | ||
600 | |||
601 | return 1; | ||
602 | } | ||
603 | |||
604 | static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, | ||
605 | int size, u32 *val) | ||
606 | { | ||
607 | struct pcie_port *pp = bus->sysdata; | ||
608 | |||
609 | if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) { | ||
610 | *val = 0xffffffff; | ||
611 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
612 | } | ||
613 | |||
614 | if (bus->number == pp->root_bus_nr) | ||
615 | return dw_pcie_rd_own_conf(pp, where, size, val); | ||
616 | |||
617 | return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val); | ||
618 | } | ||
619 | |||
620 | static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn, | ||
621 | int where, int size, u32 val) | ||
622 | { | ||
623 | struct pcie_port *pp = bus->sysdata; | ||
624 | |||
625 | if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) | ||
626 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
627 | |||
628 | if (bus->number == pp->root_bus_nr) | ||
629 | return dw_pcie_wr_own_conf(pp, where, size, val); | ||
630 | |||
631 | return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val); | ||
632 | } | ||
633 | |||
634 | static struct pci_ops dw_pcie_ops = { | ||
635 | .read = dw_pcie_rd_conf, | ||
636 | .write = dw_pcie_wr_conf, | ||
637 | }; | ||
638 | |||
639 | static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci) | ||
640 | { | ||
641 | u32 val; | ||
642 | |||
643 | val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT); | ||
644 | if (val == 0xffffffff) | ||
645 | return 1; | ||
646 | |||
647 | return 0; | ||
648 | } | ||
649 | |||
650 | void dw_pcie_setup_rc(struct pcie_port *pp) | ||
651 | { | ||
652 | u32 val, ctrl, num_ctrls; | ||
653 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); | ||
654 | |||
655 | dw_pcie_setup(pci); | ||
656 | |||
657 | num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL; | ||
658 | |||
659 | /* Initialize IRQ Status array */ | ||
660 | for (ctrl = 0; ctrl < num_ctrls; ctrl++) | ||
661 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + | ||
662 | (ctrl * MSI_REG_CTRL_BLOCK_SIZE), | ||
663 | 4, &pp->irq_status[ctrl]); | ||
664 | |||
665 | /* Setup RC BARs */ | ||
666 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004); | ||
667 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000); | ||
668 | |||
669 | /* Setup interrupt pins */ | ||
670 | dw_pcie_dbi_ro_wr_en(pci); | ||
671 | val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE); | ||
672 | val &= 0xffff00ff; | ||
673 | val |= 0x00000100; | ||
674 | dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val); | ||
675 | dw_pcie_dbi_ro_wr_dis(pci); | ||
676 | |||
677 | /* Setup bus numbers */ | ||
678 | val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS); | ||
679 | val &= 0xff000000; | ||
680 | val |= 0x00ff0100; | ||
681 | dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val); | ||
682 | |||
683 | /* Setup command register */ | ||
684 | val = dw_pcie_readl_dbi(pci, PCI_COMMAND); | ||
685 | val &= 0xffff0000; | ||
686 | val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | | ||
687 | PCI_COMMAND_MASTER | PCI_COMMAND_SERR; | ||
688 | dw_pcie_writel_dbi(pci, PCI_COMMAND, val); | ||
689 | |||
690 | /* | ||
691 | * If the platform provides ->rd_other_conf, it means the platform | ||
692 | * uses its own address translation component rather than ATU, so | ||
693 | * we should not program the ATU here. | ||
694 | */ | ||
695 | if (!pp->ops->rd_other_conf) { | ||
696 | /* Get iATU unroll support */ | ||
697 | pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci); | ||
698 | dev_dbg(pci->dev, "iATU unroll: %s\n", | ||
699 | pci->iatu_unroll_enabled ? "enabled" : "disabled"); | ||
700 | |||
701 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0, | ||
702 | PCIE_ATU_TYPE_MEM, pp->mem_base, | ||
703 | pp->mem_bus_addr, pp->mem_size); | ||
704 | if (pci->num_viewport > 2) | ||
705 | dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2, | ||
706 | PCIE_ATU_TYPE_IO, pp->io_base, | ||
707 | pp->io_bus_addr, pp->io_size); | ||
708 | } | ||
709 | |||
710 | dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0); | ||
711 | |||
712 | /* Enable write permission for the DBI read-only register */ | ||
713 | dw_pcie_dbi_ro_wr_en(pci); | ||
714 | /* Program correct class for RC */ | ||
715 | dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI); | ||
716 | /* Better disable write permission right after the update */ | ||
717 | dw_pcie_dbi_ro_wr_dis(pci); | ||
718 | |||
719 | dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val); | ||
720 | val |= PORT_LOGIC_SPEED_CHANGE; | ||
721 | dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val); | ||
722 | } | ||