blob: 81e2157a7cfbcd20605c228223476fc268bb045e [file] [log] [blame]
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +05301/*
Bjorn Helgaas96291d52017-09-01 16:35:50 -05002 * Synopsys DesignWare PCIe host controller driver
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +05303 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
7 * Author: Jingoo Han <jg1.han@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/irqdomain.h>
15#include <linux/of_address.h>
16#include <linux/of_pci.h>
17#include <linux/pci_regs.h>
18#include <linux/platform_device.h>
19
20#include "pcie-designware.h"
21
22static struct pci_ops dw_pcie_ops;
23
24static 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
36static 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
48static struct irq_chip dw_msi_irq_chip = {
49 .name = "PCI-MSI",
50 .irq_enable = pci_msi_unmask_irq,
51 .irq_disable = pci_msi_mask_irq,
52 .irq_mask = pci_msi_mask_irq,
53 .irq_unmask = pci_msi_unmask_irq,
54};
55
56/* MSI int handler */
57irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
58{
Dan Carpenter1b497e62017-03-16 14:34:51 -050059 u32 val;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053060 int i, pos, irq;
61 irqreturn_t ret = IRQ_NONE;
62
63 for (i = 0; i < MAX_MSI_CTRLS; i++) {
64 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
Dan Carpenter1b497e62017-03-16 14:34:51 -050065 &val);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050066 if (!val)
67 continue;
68
69 ret = IRQ_HANDLED;
70 pos = 0;
Dan Carpenter1b497e62017-03-16 14:34:51 -050071 while ((pos = find_next_bit((unsigned long *) &val, 32,
72 pos)) != 32) {
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050073 irq = irq_find_mapping(pp->irq_domain, i * 32 + pos);
Faiz Abbas8c934092017-08-10 16:54:55 +053074 generic_handle_irq(irq);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050075 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12,
76 4, 1 << pos);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050077 pos++;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053078 }
79 }
80
81 return ret;
82}
83
84void dw_pcie_msi_init(struct pcie_port *pp)
85{
86 u64 msi_target;
87
88 pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
89 msi_target = virt_to_phys((void *)pp->msi_data);
90
91 /* program the msi_data */
92 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
93 (u32)(msi_target & 0xffffffff));
94 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4,
95 (u32)(msi_target >> 32 & 0xffffffff));
96}
97
98static void dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
99{
100 unsigned int res, bit, val;
101
102 res = (irq / 32) * 12;
103 bit = irq % 32;
104 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
105 val &= ~(1 << bit);
106 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
107}
108
109static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
110 unsigned int nvec, unsigned int pos)
111{
112 unsigned int i;
113
114 for (i = 0; i < nvec; i++) {
115 irq_set_msi_desc_off(irq_base, i, NULL);
116 /* Disable corresponding interrupt on MSI controller */
117 if (pp->ops->msi_clear_irq)
118 pp->ops->msi_clear_irq(pp, pos + i);
119 else
120 dw_pcie_msi_clear_irq(pp, pos + i);
121 }
122
123 bitmap_release_region(pp->msi_irq_in_use, pos, order_base_2(nvec));
124}
125
126static void dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
127{
128 unsigned int res, bit, val;
129
130 res = (irq / 32) * 12;
131 bit = irq % 32;
132 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
133 val |= 1 << bit;
134 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
135}
136
137static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
138{
139 int irq, pos0, i;
140 struct pcie_port *pp;
141
142 pp = (struct pcie_port *)msi_desc_to_pci_sysdata(desc);
143 pos0 = bitmap_find_free_region(pp->msi_irq_in_use, MAX_MSI_IRQS,
144 order_base_2(no_irqs));
145 if (pos0 < 0)
146 goto no_valid_irq;
147
148 irq = irq_find_mapping(pp->irq_domain, pos0);
149 if (!irq)
150 goto no_valid_irq;
151
152 /*
153 * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
154 * descs so there is no need to allocate descs here. We can therefore
155 * assume that if irq_find_mapping above returns non-zero, then the
156 * descs are also successfully allocated.
157 */
158
159 for (i = 0; i < no_irqs; i++) {
160 if (irq_set_msi_desc_off(irq, i, desc) != 0) {
161 clear_irq_range(pp, irq, i, pos0);
162 goto no_valid_irq;
163 }
164 /*Enable corresponding interrupt in MSI interrupt controller */
165 if (pp->ops->msi_set_irq)
166 pp->ops->msi_set_irq(pp, pos0 + i);
167 else
168 dw_pcie_msi_set_irq(pp, pos0 + i);
169 }
170
171 *pos = pos0;
172 desc->nvec_used = no_irqs;
173 desc->msi_attrib.multiple = order_base_2(no_irqs);
174
175 return irq;
176
177no_valid_irq:
178 *pos = pos0;
179 return -ENOSPC;
180}
181
182static void dw_msi_setup_msg(struct pcie_port *pp, unsigned int irq, u32 pos)
183{
184 struct msi_msg msg;
185 u64 msi_target;
186
187 if (pp->ops->get_msi_addr)
188 msi_target = pp->ops->get_msi_addr(pp);
189 else
190 msi_target = virt_to_phys((void *)pp->msi_data);
191
192 msg.address_lo = (u32)(msi_target & 0xffffffff);
193 msg.address_hi = (u32)(msi_target >> 32 & 0xffffffff);
194
195 if (pp->ops->get_msi_data)
196 msg.data = pp->ops->get_msi_data(pp, pos);
197 else
198 msg.data = pos;
199
200 pci_write_msi_msg(irq, &msg);
201}
202
203static int dw_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
204 struct msi_desc *desc)
205{
206 int irq, pos;
207 struct pcie_port *pp = pdev->bus->sysdata;
208
209 if (desc->msi_attrib.is_msix)
210 return -EINVAL;
211
212 irq = assign_irq(1, desc, &pos);
213 if (irq < 0)
214 return irq;
215
216 dw_msi_setup_msg(pp, irq, pos);
217
218 return 0;
219}
220
221static int dw_msi_setup_irqs(struct msi_controller *chip, struct pci_dev *pdev,
222 int nvec, int type)
223{
224#ifdef CONFIG_PCI_MSI
225 int irq, pos;
226 struct msi_desc *desc;
227 struct pcie_port *pp = pdev->bus->sysdata;
228
229 /* MSI-X interrupts are not supported */
230 if (type == PCI_CAP_ID_MSIX)
231 return -EINVAL;
232
233 WARN_ON(!list_is_singular(&pdev->dev.msi_list));
234 desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
235
236 irq = assign_irq(nvec, desc, &pos);
237 if (irq < 0)
238 return irq;
239
240 dw_msi_setup_msg(pp, irq, pos);
241
242 return 0;
243#else
244 return -EINVAL;
245#endif
246}
247
248static void dw_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
249{
250 struct irq_data *data = irq_get_irq_data(irq);
251 struct msi_desc *msi = irq_data_get_msi_desc(data);
252 struct pcie_port *pp = (struct pcie_port *)msi_desc_to_pci_sysdata(msi);
253
254 clear_irq_range(pp, irq, 1, data->hwirq);
255}
256
257static struct msi_controller dw_pcie_msi_chip = {
258 .setup_irq = dw_msi_setup_irq,
259 .setup_irqs = dw_msi_setup_irqs,
260 .teardown_irq = dw_msi_teardown_irq,
261};
262
263static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
264 irq_hw_number_t hwirq)
265{
266 irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
267 irq_set_chip_data(irq, domain->host_data);
268
269 return 0;
270}
271
272static const struct irq_domain_ops msi_domain_ops = {
273 .map = dw_pcie_msi_map,
274};
275
276int dw_pcie_host_init(struct pcie_port *pp)
277{
278 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
279 struct device *dev = pci->dev;
280 struct device_node *np = dev->of_node;
281 struct platform_device *pdev = to_platform_device(dev);
282 struct pci_bus *bus, *child;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500283 struct pci_host_bridge *bridge;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530284 struct resource *cfg_res;
285 int i, ret;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530286 struct resource_entry *win, *tmp;
287
288 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
289 if (cfg_res) {
290 pp->cfg0_size = resource_size(cfg_res) / 2;
291 pp->cfg1_size = resource_size(cfg_res) / 2;
292 pp->cfg0_base = cfg_res->start;
293 pp->cfg1_base = cfg_res->start + pp->cfg0_size;
294 } else if (!pp->va_cfg0_base) {
295 dev_err(dev, "missing *config* reg space\n");
296 }
297
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500298 bridge = pci_alloc_host_bridge(0);
299 if (!bridge)
300 return -ENOMEM;
301
302 ret = of_pci_get_host_bridge_resources(np, 0, 0xff,
303 &bridge->windows, &pp->io_base);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530304 if (ret)
305 return ret;
306
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500307 ret = devm_request_pci_bus_resources(dev, &bridge->windows);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530308 if (ret)
309 goto error;
310
311 /* Get the I/O and memory ranges from DT */
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500312 resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530313 switch (resource_type(win->res)) {
314 case IORESOURCE_IO:
315 ret = pci_remap_iospace(win->res, pp->io_base);
316 if (ret) {
317 dev_warn(dev, "error %d: failed to map resource %pR\n",
318 ret, win->res);
319 resource_list_destroy_entry(win);
320 } else {
321 pp->io = win->res;
322 pp->io->name = "I/O";
323 pp->io_size = resource_size(pp->io);
324 pp->io_bus_addr = pp->io->start - win->offset;
325 }
326 break;
327 case IORESOURCE_MEM:
328 pp->mem = win->res;
329 pp->mem->name = "MEM";
330 pp->mem_size = resource_size(pp->mem);
331 pp->mem_bus_addr = pp->mem->start - win->offset;
332 break;
333 case 0:
334 pp->cfg = win->res;
335 pp->cfg0_size = resource_size(pp->cfg) / 2;
336 pp->cfg1_size = resource_size(pp->cfg) / 2;
337 pp->cfg0_base = pp->cfg->start;
338 pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
339 break;
340 case IORESOURCE_BUS:
341 pp->busn = win->res;
342 break;
343 }
344 }
345
346 if (!pci->dbi_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100347 pci->dbi_base = devm_pci_remap_cfgspace(dev,
348 pp->cfg->start,
349 resource_size(pp->cfg));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530350 if (!pci->dbi_base) {
351 dev_err(dev, "error with ioremap\n");
352 ret = -ENOMEM;
353 goto error;
354 }
355 }
356
357 pp->mem_base = pp->mem->start;
358
359 if (!pp->va_cfg0_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100360 pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
361 pp->cfg0_base, pp->cfg0_size);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530362 if (!pp->va_cfg0_base) {
363 dev_err(dev, "error with ioremap in function\n");
364 ret = -ENOMEM;
365 goto error;
366 }
367 }
368
369 if (!pp->va_cfg1_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100370 pp->va_cfg1_base = devm_pci_remap_cfgspace(dev,
371 pp->cfg1_base,
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530372 pp->cfg1_size);
373 if (!pp->va_cfg1_base) {
374 dev_err(dev, "error with ioremap\n");
375 ret = -ENOMEM;
376 goto error;
377 }
378 }
379
380 ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
381 if (ret)
382 pci->num_viewport = 2;
383
384 if (IS_ENABLED(CONFIG_PCI_MSI)) {
385 if (!pp->ops->msi_host_init) {
386 pp->irq_domain = irq_domain_add_linear(dev->of_node,
387 MAX_MSI_IRQS, &msi_domain_ops,
388 &dw_pcie_msi_chip);
389 if (!pp->irq_domain) {
390 dev_err(dev, "irq domain init failed\n");
391 ret = -ENXIO;
392 goto error;
393 }
394
395 for (i = 0; i < MAX_MSI_IRQS; i++)
396 irq_create_mapping(pp->irq_domain, i);
397 } else {
398 ret = pp->ops->msi_host_init(pp, &dw_pcie_msi_chip);
399 if (ret < 0)
400 goto error;
401 }
402 }
403
Bjorn Andersson4a301762017-07-15 23:39:45 -0700404 if (pp->ops->host_init) {
405 ret = pp->ops->host_init(pp);
406 if (ret)
407 goto error;
408 }
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530409
410 pp->root_bus_nr = pp->busn->start;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500411
412 bridge->dev.parent = dev;
413 bridge->sysdata = pp;
414 bridge->busnr = pp->root_bus_nr;
415 bridge->ops = &dw_pcie_ops;
Lorenzo Pieralisi60eca192017-06-28 15:14:07 -0500416 bridge->map_irq = of_irq_parse_and_map_pci;
417 bridge->swizzle_irq = pci_common_swizzle;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530418 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500419 bridge->msi = &dw_pcie_msi_chip;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530420 dw_pcie_msi_chip.dev = dev;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530421 }
422
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500423 ret = pci_scan_root_bus_bridge(bridge);
424 if (ret)
425 goto error;
426
427 bus = bridge->bus;
428
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530429 if (pp->ops->scan_bus)
430 pp->ops->scan_bus(pp);
431
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530432 pci_bus_size_bridges(bus);
433 pci_bus_assign_resources(bus);
434
435 list_for_each_entry(child, &bus->children, node)
436 pcie_bus_configure_settings(child);
437
438 pci_bus_add_devices(bus);
439 return 0;
440
441error:
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500442 pci_free_host_bridge(bridge);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530443 return ret;
444}
445
446static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
447 u32 devfn, int where, int size, u32 *val)
448{
449 int ret, type;
450 u32 busdev, cfg_size;
451 u64 cpu_addr;
452 void __iomem *va_cfg_base;
453 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
454
455 if (pp->ops->rd_other_conf)
456 return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val);
457
458 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
459 PCIE_ATU_FUNC(PCI_FUNC(devfn));
460
461 if (bus->parent->number == pp->root_bus_nr) {
462 type = PCIE_ATU_TYPE_CFG0;
463 cpu_addr = pp->cfg0_base;
464 cfg_size = pp->cfg0_size;
465 va_cfg_base = pp->va_cfg0_base;
466 } else {
467 type = PCIE_ATU_TYPE_CFG1;
468 cpu_addr = pp->cfg1_base;
469 cfg_size = pp->cfg1_size;
470 va_cfg_base = pp->va_cfg1_base;
471 }
472
473 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
474 type, cpu_addr,
475 busdev, cfg_size);
476 ret = dw_pcie_read(va_cfg_base + where, size, val);
477 if (pci->num_viewport <= 2)
478 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
479 PCIE_ATU_TYPE_IO, pp->io_base,
480 pp->io_bus_addr, pp->io_size);
481
482 return ret;
483}
484
485static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
486 u32 devfn, int where, int size, u32 val)
487{
488 int ret, type;
489 u32 busdev, cfg_size;
490 u64 cpu_addr;
491 void __iomem *va_cfg_base;
492 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
493
494 if (pp->ops->wr_other_conf)
495 return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val);
496
497 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
498 PCIE_ATU_FUNC(PCI_FUNC(devfn));
499
500 if (bus->parent->number == pp->root_bus_nr) {
501 type = PCIE_ATU_TYPE_CFG0;
502 cpu_addr = pp->cfg0_base;
503 cfg_size = pp->cfg0_size;
504 va_cfg_base = pp->va_cfg0_base;
505 } else {
506 type = PCIE_ATU_TYPE_CFG1;
507 cpu_addr = pp->cfg1_base;
508 cfg_size = pp->cfg1_size;
509 va_cfg_base = pp->va_cfg1_base;
510 }
511
512 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
513 type, cpu_addr,
514 busdev, cfg_size);
515 ret = dw_pcie_write(va_cfg_base + where, size, val);
516 if (pci->num_viewport <= 2)
517 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
518 PCIE_ATU_TYPE_IO, pp->io_base,
519 pp->io_bus_addr, pp->io_size);
520
521 return ret;
522}
523
524static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
525 int dev)
526{
527 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
528
529 /* If there is no link, then there is no device */
530 if (bus->number != pp->root_bus_nr) {
531 if (!dw_pcie_link_up(pci))
532 return 0;
533 }
534
535 /* access only one slot on each root port */
536 if (bus->number == pp->root_bus_nr && dev > 0)
537 return 0;
538
539 return 1;
540}
541
542static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
543 int size, u32 *val)
544{
545 struct pcie_port *pp = bus->sysdata;
546
547 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
548 *val = 0xffffffff;
549 return PCIBIOS_DEVICE_NOT_FOUND;
550 }
551
552 if (bus->number == pp->root_bus_nr)
553 return dw_pcie_rd_own_conf(pp, where, size, val);
554
555 return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
556}
557
558static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
559 int where, int size, u32 val)
560{
561 struct pcie_port *pp = bus->sysdata;
562
563 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
564 return PCIBIOS_DEVICE_NOT_FOUND;
565
566 if (bus->number == pp->root_bus_nr)
567 return dw_pcie_wr_own_conf(pp, where, size, val);
568
569 return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
570}
571
572static struct pci_ops dw_pcie_ops = {
573 .read = dw_pcie_rd_conf,
574 .write = dw_pcie_wr_conf,
575};
576
577static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci)
578{
579 u32 val;
580
581 val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT);
582 if (val == 0xffffffff)
583 return 1;
584
585 return 0;
586}
587
588void dw_pcie_setup_rc(struct pcie_port *pp)
589{
590 u32 val;
591 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
592
593 dw_pcie_setup(pci);
594
595 /* setup RC BARs */
596 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
597 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
598
599 /* setup interrupt pins */
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800600 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530601 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
602 val &= 0xffff00ff;
603 val |= 0x00000100;
604 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800605 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530606
607 /* setup bus numbers */
608 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
609 val &= 0xff000000;
610 val |= 0x00010100;
611 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
612
613 /* setup command register */
614 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
615 val &= 0xffff0000;
616 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
617 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
618 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
619
620 /*
621 * If the platform provides ->rd_other_conf, it means the platform
622 * uses its own address translation component rather than ATU, so
623 * we should not program the ATU here.
624 */
625 if (!pp->ops->rd_other_conf) {
626 /* get iATU unroll support */
627 pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci);
628 dev_dbg(pci->dev, "iATU unroll: %s\n",
629 pci->iatu_unroll_enabled ? "enabled" : "disabled");
630
631 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
632 PCIE_ATU_TYPE_MEM, pp->mem_base,
633 pp->mem_bus_addr, pp->mem_size);
634 if (pci->num_viewport > 2)
635 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
636 PCIE_ATU_TYPE_IO, pp->io_base,
637 pp->io_bus_addr, pp->io_size);
638 }
639
640 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
641
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800642 /* Enable write permission for the DBI read-only register */
643 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530644 /* program correct class for RC */
645 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800646 /* Better disable write permission right after the update */
647 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530648
649 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
650 val |= PORT_LOGIC_SPEED_CHANGE;
651 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
652}