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