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