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