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