blob: 5a23f782851a025fd3c1aa7da40b613740fb9d86 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +05302/*
Bjorn Helgaas96291d52017-09-01 16:35:50 -05003 * Synopsys DesignWare PCIe host controller driver
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +05304 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +05309 */
10
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +000011#include <linux/irqchip/chained_irq.h>
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053012#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 "pcie-designware.h"
19
20static struct pci_ops dw_pcie_ops;
21
22static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
23 u32 *val)
24{
25 struct dw_pcie *pci;
26
27 if (pp->ops->rd_own_conf)
28 return pp->ops->rd_own_conf(pp, where, size, val);
29
30 pci = to_dw_pcie_from_pp(pp);
31 return dw_pcie_read(pci->dbi_base + where, size, val);
32}
33
34static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
35 u32 val)
36{
37 struct dw_pcie *pci;
38
39 if (pp->ops->wr_own_conf)
40 return pp->ops->wr_own_conf(pp, where, size, val);
41
42 pci = to_dw_pcie_from_pp(pp);
43 return dw_pcie_write(pci->dbi_base + where, size, val);
44}
45
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +000046static void dw_msi_ack_irq(struct irq_data *d)
47{
48 irq_chip_ack_parent(d);
49}
50
51static void dw_msi_mask_irq(struct irq_data *d)
52{
53 pci_msi_mask_irq(d);
54 irq_chip_mask_parent(d);
55}
56
57static void dw_msi_unmask_irq(struct irq_data *d)
58{
59 pci_msi_unmask_irq(d);
60 irq_chip_unmask_parent(d);
61}
62
63static struct irq_chip dw_pcie_msi_irq_chip = {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053064 .name = "PCI-MSI",
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +000065 .irq_ack = dw_msi_ack_irq,
66 .irq_mask = dw_msi_mask_irq,
67 .irq_unmask = dw_msi_unmask_irq,
68};
69
70static struct msi_domain_info dw_pcie_msi_domain_info = {
71 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
72 MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
73 .chip = &dw_pcie_msi_irq_chip,
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053074};
75
76/* MSI int handler */
77irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
78{
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053079 int i, pos, irq;
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +000080 u32 val, num_ctrls;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053081 irqreturn_t ret = IRQ_NONE;
82
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +000083 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
84
85 for (i = 0; i < num_ctrls; i++) {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053086 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
Dan Carpenter1b497e62017-03-16 14:34:51 -050087 &val);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050088 if (!val)
89 continue;
90
91 ret = IRQ_HANDLED;
92 pos = 0;
Dan Carpenter1b497e62017-03-16 14:34:51 -050093 while ((pos = find_next_bit((unsigned long *) &val, 32,
94 pos)) != 32) {
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050095 irq = irq_find_mapping(pp->irq_domain, i * 32 + pos);
Faiz Abbas8c934092017-08-10 16:54:55 +053096 generic_handle_irq(irq);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050097 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12,
98 4, 1 << pos);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050099 pos++;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530100 }
101 }
102
103 return ret;
104}
105
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000106/* Chained MSI interrupt service routine */
107static void dw_chained_msi_isr(struct irq_desc *desc)
108{
109 struct irq_chip *chip = irq_desc_get_chip(desc);
110 struct pcie_port *pp;
111
112 chained_irq_enter(chip, desc);
113
114 pp = irq_desc_get_handler_data(desc);
115 dw_handle_msi_irq(pp);
116
117 chained_irq_exit(chip, desc);
118}
119
120static void dw_pci_setup_msi_msg(struct irq_data *data, struct msi_msg *msg)
121{
122 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
123 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
124 u64 msi_target;
125
126 if (pp->ops->get_msi_addr)
127 msi_target = pp->ops->get_msi_addr(pp);
128 else
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 if (pp->ops->get_msi_data)
135 msg->data = pp->ops->get_msi_data(pp, data->hwirq);
136 else
137 msg->data = data->hwirq;
138
139 dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
140 (int)data->hwirq, msg->address_hi, msg->address_lo);
141}
142
143static int dw_pci_msi_set_affinity(struct irq_data *irq_data,
144 const struct cpumask *mask, bool force)
145{
146 return -EINVAL;
147}
148
149static void dw_pci_bottom_mask(struct irq_data *data)
150{
151 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
152 unsigned int res, bit, ctrl;
153 unsigned long flags;
154
155 raw_spin_lock_irqsave(&pp->lock, flags);
156
157 if (pp->ops->msi_clear_irq) {
158 pp->ops->msi_clear_irq(pp, data->hwirq);
159 } else {
160 ctrl = data->hwirq / 32;
161 res = ctrl * 12;
162 bit = data->hwirq % 32;
163
164 pp->irq_status[ctrl] &= ~(1 << bit);
165 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4,
166 pp->irq_status[ctrl]);
167 }
168
169 raw_spin_unlock_irqrestore(&pp->lock, flags);
170}
171
172static void dw_pci_bottom_unmask(struct irq_data *data)
173{
174 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
175 unsigned int res, bit, ctrl;
176 unsigned long flags;
177
178 raw_spin_lock_irqsave(&pp->lock, flags);
179
180 if (pp->ops->msi_set_irq) {
181 pp->ops->msi_set_irq(pp, data->hwirq);
182 } else {
183 ctrl = data->hwirq / 32;
184 res = ctrl * 12;
185 bit = data->hwirq % 32;
186
187 pp->irq_status[ctrl] |= 1 << bit;
188 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4,
189 pp->irq_status[ctrl]);
190 }
191
192 raw_spin_unlock_irqrestore(&pp->lock, flags);
193}
194
195static void dw_pci_bottom_ack(struct irq_data *d)
196{
197 struct msi_desc *msi = irq_data_get_msi_desc(d);
198 struct pcie_port *pp;
199
200 pp = msi_desc_to_pci_sysdata(msi);
201
202 if (pp->ops->msi_irq_ack)
203 pp->ops->msi_irq_ack(d->hwirq, pp);
204}
205
206static struct irq_chip dw_pci_msi_bottom_irq_chip = {
207 .name = "DWPCI-MSI",
208 .irq_ack = dw_pci_bottom_ack,
209 .irq_compose_msi_msg = dw_pci_setup_msi_msg,
210 .irq_set_affinity = dw_pci_msi_set_affinity,
211 .irq_mask = dw_pci_bottom_mask,
212 .irq_unmask = dw_pci_bottom_unmask,
213};
214
215static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
216 unsigned int virq, unsigned int nr_irqs,
217 void *args)
218{
219 struct pcie_port *pp = domain->host_data;
220 unsigned long flags;
221 u32 i;
222 int bit;
223
224 raw_spin_lock_irqsave(&pp->lock, flags);
225
226 bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
227 order_base_2(nr_irqs));
228
229 raw_spin_unlock_irqrestore(&pp->lock, flags);
230
231 if (bit < 0)
232 return -ENOSPC;
233
234 for (i = 0; i < nr_irqs; i++)
235 irq_domain_set_info(domain, virq + i, bit + i,
236 &dw_pci_msi_bottom_irq_chip,
237 pp, handle_edge_irq,
238 NULL, NULL);
239
240 return 0;
241}
242
243static void dw_pcie_irq_domain_free(struct irq_domain *domain,
244 unsigned int virq, unsigned int nr_irqs)
245{
246 struct irq_data *data = irq_domain_get_irq_data(domain, virq);
247 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
248 unsigned long flags;
249
250 raw_spin_lock_irqsave(&pp->lock, flags);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100251
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000252 bitmap_release_region(pp->msi_irq_in_use, data->hwirq,
253 order_base_2(nr_irqs));
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100254
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000255 raw_spin_unlock_irqrestore(&pp->lock, flags);
256}
257
258static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
259 .alloc = dw_pcie_irq_domain_alloc,
260 .free = dw_pcie_irq_domain_free,
261};
262
263int dw_pcie_allocate_domains(struct pcie_port *pp)
264{
265 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
266 struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
267
268 pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
269 &dw_pcie_msi_domain_ops, pp);
270 if (!pp->irq_domain) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100271 dev_err(pci->dev, "Failed to create IRQ domain\n");
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000272 return -ENOMEM;
273 }
274
275 pp->msi_domain = pci_msi_create_irq_domain(fwnode,
276 &dw_pcie_msi_domain_info,
277 pp->irq_domain);
278 if (!pp->msi_domain) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100279 dev_err(pci->dev, "Failed to create MSI domain\n");
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000280 irq_domain_remove(pp->irq_domain);
281 return -ENOMEM;
282 }
283
284 return 0;
285}
286
287void dw_pcie_free_msi(struct pcie_port *pp)
288{
289 irq_set_chained_handler(pp->msi_irq, NULL);
290 irq_set_handler_data(pp->msi_irq, NULL);
291
292 irq_domain_remove(pp->msi_domain);
293 irq_domain_remove(pp->irq_domain);
294}
295
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530296void dw_pcie_msi_init(struct pcie_port *pp)
297{
Niklas Cassel111111a2017-12-20 00:29:22 +0100298 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
299 struct device *dev = pci->dev;
300 struct page *page;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530301 u64 msi_target;
302
Niklas Cassel111111a2017-12-20 00:29:22 +0100303 page = alloc_page(GFP_KERNEL);
304 pp->msi_data = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
305 if (dma_mapping_error(dev, pp->msi_data)) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100306 dev_err(dev, "Failed to map MSI data\n");
Niklas Cassel111111a2017-12-20 00:29:22 +0100307 __free_page(page);
308 return;
309 }
310 msi_target = (u64)pp->msi_data;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530311
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100312 /* Program the msi_data */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530313 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000314 lower_32_bits(msi_target));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530315 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4,
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000316 upper_32_bits(msi_target));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530317}
318
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530319int 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);
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000325 struct resource_entry *win, *tmp;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530326 struct pci_bus *bus, *child;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500327 struct pci_host_bridge *bridge;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530328 struct resource *cfg_res;
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000329 int ret;
330
331 raw_spin_lock_init(&pci->pp.lock);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530332
333 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
334 if (cfg_res) {
335 pp->cfg0_size = resource_size(cfg_res) / 2;
336 pp->cfg1_size = resource_size(cfg_res) / 2;
337 pp->cfg0_base = cfg_res->start;
338 pp->cfg1_base = cfg_res->start + pp->cfg0_size;
339 } else if (!pp->va_cfg0_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100340 dev_err(dev, "Missing *config* reg space\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530341 }
342
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500343 bridge = pci_alloc_host_bridge(0);
344 if (!bridge)
345 return -ENOMEM;
346
347 ret = of_pci_get_host_bridge_resources(np, 0, 0xff,
348 &bridge->windows, &pp->io_base);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530349 if (ret)
350 return ret;
351
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500352 ret = devm_request_pci_bus_resources(dev, &bridge->windows);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530353 if (ret)
354 goto error;
355
356 /* Get the I/O and memory ranges from DT */
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500357 resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530358 switch (resource_type(win->res)) {
359 case IORESOURCE_IO:
360 ret = pci_remap_iospace(win->res, pp->io_base);
361 if (ret) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100362 dev_warn(dev, "Error %d: failed to map resource %pR\n",
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530363 ret, win->res);
364 resource_list_destroy_entry(win);
365 } else {
366 pp->io = win->res;
367 pp->io->name = "I/O";
368 pp->io_size = resource_size(pp->io);
369 pp->io_bus_addr = pp->io->start - win->offset;
370 }
371 break;
372 case IORESOURCE_MEM:
373 pp->mem = win->res;
374 pp->mem->name = "MEM";
375 pp->mem_size = resource_size(pp->mem);
376 pp->mem_bus_addr = pp->mem->start - win->offset;
377 break;
378 case 0:
379 pp->cfg = win->res;
380 pp->cfg0_size = resource_size(pp->cfg) / 2;
381 pp->cfg1_size = resource_size(pp->cfg) / 2;
382 pp->cfg0_base = pp->cfg->start;
383 pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
384 break;
385 case IORESOURCE_BUS:
386 pp->busn = win->res;
387 break;
388 }
389 }
390
391 if (!pci->dbi_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100392 pci->dbi_base = devm_pci_remap_cfgspace(dev,
393 pp->cfg->start,
394 resource_size(pp->cfg));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530395 if (!pci->dbi_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100396 dev_err(dev, "Error with ioremap\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530397 ret = -ENOMEM;
398 goto error;
399 }
400 }
401
402 pp->mem_base = pp->mem->start;
403
404 if (!pp->va_cfg0_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100405 pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
406 pp->cfg0_base, pp->cfg0_size);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530407 if (!pp->va_cfg0_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100408 dev_err(dev, "Error with ioremap in function\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530409 ret = -ENOMEM;
410 goto error;
411 }
412 }
413
414 if (!pp->va_cfg1_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100415 pp->va_cfg1_base = devm_pci_remap_cfgspace(dev,
416 pp->cfg1_base,
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530417 pp->cfg1_size);
418 if (!pp->va_cfg1_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100419 dev_err(dev, "Error with ioremap\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530420 ret = -ENOMEM;
421 goto error;
422 }
423 }
424
425 ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
426 if (ret)
427 pci->num_viewport = 2;
428
429 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000430 /*
431 * If a specific SoC driver needs to change the
432 * default number of vectors, it needs to implement
433 * the set_num_vectors callback.
434 */
435 if (!pp->ops->set_num_vectors) {
436 pp->num_vectors = MSI_DEF_NUM_VECTORS;
437 } else {
438 pp->ops->set_num_vectors(pp);
439
440 if (pp->num_vectors > MAX_MSI_IRQS ||
441 pp->num_vectors == 0) {
442 dev_err(dev,
443 "Invalid number of vectors\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530444 goto error;
445 }
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000446 }
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530447
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000448 if (!pp->ops->msi_host_init) {
449 ret = dw_pcie_allocate_domains(pp);
450 if (ret)
451 goto error;
452
453 if (pp->msi_irq)
454 irq_set_chained_handler_and_data(pp->msi_irq,
455 dw_chained_msi_isr,
456 pp);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530457 } else {
Gustavo Pimentel3f43ccc2018-03-06 11:54:54 +0000458 ret = pp->ops->msi_host_init(pp);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530459 if (ret < 0)
460 goto error;
461 }
462 }
463
Bjorn Andersson4a301762017-07-15 23:39:45 -0700464 if (pp->ops->host_init) {
465 ret = pp->ops->host_init(pp);
466 if (ret)
467 goto error;
468 }
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530469
470 pp->root_bus_nr = pp->busn->start;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500471
472 bridge->dev.parent = dev;
473 bridge->sysdata = pp;
474 bridge->busnr = pp->root_bus_nr;
475 bridge->ops = &dw_pcie_ops;
Lorenzo Pieralisi60eca192017-06-28 15:14:07 -0500476 bridge->map_irq = of_irq_parse_and_map_pci;
477 bridge->swizzle_irq = pci_common_swizzle;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530478
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500479 ret = pci_scan_root_bus_bridge(bridge);
480 if (ret)
481 goto error;
482
483 bus = bridge->bus;
484
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530485 if (pp->ops->scan_bus)
486 pp->ops->scan_bus(pp);
487
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530488 pci_bus_size_bridges(bus);
489 pci_bus_assign_resources(bus);
490
491 list_for_each_entry(child, &bus->children, node)
492 pcie_bus_configure_settings(child);
493
494 pci_bus_add_devices(bus);
495 return 0;
496
497error:
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500498 pci_free_host_bridge(bridge);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530499 return ret;
500}
501
502static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
503 u32 devfn, int where, int size, u32 *val)
504{
505 int ret, type;
506 u32 busdev, cfg_size;
507 u64 cpu_addr;
508 void __iomem *va_cfg_base;
509 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
510
511 if (pp->ops->rd_other_conf)
512 return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val);
513
514 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
515 PCIE_ATU_FUNC(PCI_FUNC(devfn));
516
517 if (bus->parent->number == pp->root_bus_nr) {
518 type = PCIE_ATU_TYPE_CFG0;
519 cpu_addr = pp->cfg0_base;
520 cfg_size = pp->cfg0_size;
521 va_cfg_base = pp->va_cfg0_base;
522 } else {
523 type = PCIE_ATU_TYPE_CFG1;
524 cpu_addr = pp->cfg1_base;
525 cfg_size = pp->cfg1_size;
526 va_cfg_base = pp->va_cfg1_base;
527 }
528
529 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
530 type, cpu_addr,
531 busdev, cfg_size);
532 ret = dw_pcie_read(va_cfg_base + where, size, val);
533 if (pci->num_viewport <= 2)
534 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
535 PCIE_ATU_TYPE_IO, pp->io_base,
536 pp->io_bus_addr, pp->io_size);
537
538 return ret;
539}
540
541static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
542 u32 devfn, int where, int size, u32 val)
543{
544 int ret, type;
545 u32 busdev, cfg_size;
546 u64 cpu_addr;
547 void __iomem *va_cfg_base;
548 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
549
550 if (pp->ops->wr_other_conf)
551 return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val);
552
553 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
554 PCIE_ATU_FUNC(PCI_FUNC(devfn));
555
556 if (bus->parent->number == pp->root_bus_nr) {
557 type = PCIE_ATU_TYPE_CFG0;
558 cpu_addr = pp->cfg0_base;
559 cfg_size = pp->cfg0_size;
560 va_cfg_base = pp->va_cfg0_base;
561 } else {
562 type = PCIE_ATU_TYPE_CFG1;
563 cpu_addr = pp->cfg1_base;
564 cfg_size = pp->cfg1_size;
565 va_cfg_base = pp->va_cfg1_base;
566 }
567
568 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
569 type, cpu_addr,
570 busdev, cfg_size);
571 ret = dw_pcie_write(va_cfg_base + where, size, val);
572 if (pci->num_viewport <= 2)
573 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
574 PCIE_ATU_TYPE_IO, pp->io_base,
575 pp->io_bus_addr, pp->io_size);
576
577 return ret;
578}
579
580static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
581 int dev)
582{
583 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
584
585 /* If there is no link, then there is no device */
586 if (bus->number != pp->root_bus_nr) {
587 if (!dw_pcie_link_up(pci))
588 return 0;
589 }
590
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100591 /* Access only one slot on each root port */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530592 if (bus->number == pp->root_bus_nr && dev > 0)
593 return 0;
594
595 return 1;
596}
597
598static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
599 int size, u32 *val)
600{
601 struct pcie_port *pp = bus->sysdata;
602
603 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
604 *val = 0xffffffff;
605 return PCIBIOS_DEVICE_NOT_FOUND;
606 }
607
608 if (bus->number == pp->root_bus_nr)
609 return dw_pcie_rd_own_conf(pp, where, size, val);
610
611 return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
612}
613
614static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
615 int where, int size, u32 val)
616{
617 struct pcie_port *pp = bus->sysdata;
618
619 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
620 return PCIBIOS_DEVICE_NOT_FOUND;
621
622 if (bus->number == pp->root_bus_nr)
623 return dw_pcie_wr_own_conf(pp, where, size, val);
624
625 return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
626}
627
628static struct pci_ops dw_pcie_ops = {
629 .read = dw_pcie_rd_conf,
630 .write = dw_pcie_wr_conf,
631};
632
633static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci)
634{
635 u32 val;
636
637 val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT);
638 if (val == 0xffffffff)
639 return 1;
640
641 return 0;
642}
643
644void dw_pcie_setup_rc(struct pcie_port *pp)
645{
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000646 u32 val, ctrl, num_ctrls;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530647 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
648
649 dw_pcie_setup(pci);
650
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000651 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
652
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000653 /* Initialize IRQ Status array */
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000654 for (ctrl = 0; ctrl < num_ctrls; ctrl++)
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000655 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + (ctrl * 12), 4,
656 &pp->irq_status[ctrl]);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100657
658 /* Setup RC BARs */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530659 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
660 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
661
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100662 /* Setup interrupt pins */
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800663 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530664 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
665 val &= 0xffff00ff;
666 val |= 0x00000100;
667 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800668 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530669
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100670 /* Setup bus numbers */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530671 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
672 val &= 0xff000000;
Koen Vandeputtefc110eb2018-03-07 10:46:39 -0600673 val |= 0x00ff0100;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530674 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
675
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100676 /* Setup command register */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530677 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
678 val &= 0xffff0000;
679 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
680 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
681 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
682
683 /*
684 * If the platform provides ->rd_other_conf, it means the platform
685 * uses its own address translation component rather than ATU, so
686 * we should not program the ATU here.
687 */
688 if (!pp->ops->rd_other_conf) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100689 /* Get iATU unroll support */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530690 pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci);
691 dev_dbg(pci->dev, "iATU unroll: %s\n",
692 pci->iatu_unroll_enabled ? "enabled" : "disabled");
693
694 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
695 PCIE_ATU_TYPE_MEM, pp->mem_base,
696 pp->mem_bus_addr, pp->mem_size);
697 if (pci->num_viewport > 2)
698 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
699 PCIE_ATU_TYPE_IO, pp->io_base,
700 pp->io_bus_addr, pp->io_size);
701 }
702
703 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
704
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800705 /* Enable write permission for the DBI read-only register */
706 dw_pcie_dbi_ro_wr_en(pci);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100707 /* Program correct class for RC */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530708 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800709 /* Better disable write permission right after the update */
710 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530711
712 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
713 val |= PORT_LOGIC_SPEED_CHANGE;
714 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
715}