blob: 781aa03aeede34adbad23fa6b37b0d2275d00458 [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
Shawn Lin6e0832f2018-05-31 09:12:37 +080018#include "../../pci.h"
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053019#include "pcie-designware.h"
20
21static struct pci_ops dw_pcie_ops;
22
23static 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
35static 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
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +000047static void dw_msi_ack_irq(struct irq_data *d)
48{
49 irq_chip_ack_parent(d);
50}
51
52static void dw_msi_mask_irq(struct irq_data *d)
53{
54 pci_msi_mask_irq(d);
55 irq_chip_mask_parent(d);
56}
57
58static void dw_msi_unmask_irq(struct irq_data *d)
59{
60 pci_msi_unmask_irq(d);
61 irq_chip_unmask_parent(d);
62}
63
64static struct irq_chip dw_pcie_msi_irq_chip = {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053065 .name = "PCI-MSI",
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +000066 .irq_ack = dw_msi_ack_irq,
67 .irq_mask = dw_msi_mask_irq,
68 .irq_unmask = dw_msi_unmask_irq,
69};
70
71static 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,
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053075};
76
77/* MSI int handler */
78irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
79{
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053080 int i, pos, irq;
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +000081 u32 val, num_ctrls;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053082 irqreturn_t ret = IRQ_NONE;
83
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +000084 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
85
86 for (i = 0; i < num_ctrls; i++) {
Gustavo Pimentel76cbf062018-05-14 16:09:50 +010087 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
88 (i * MSI_REG_CTRL_BLOCK_SIZE),
89 4, &val);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -050090 if (!val)
91 continue;
92
93 ret = IRQ_HANDLED;
94 pos = 0;
Gustavo Pimentel76cbf062018-05-14 16:09:50 +010095 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);
Faiz Abbas8c934092017-08-10 16:54:55 +0530101 generic_handle_irq(irq);
Gustavo Pimentel76cbf062018-05-14 16:09:50 +0100102 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS +
103 (i * MSI_REG_CTRL_BLOCK_SIZE),
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -0500104 4, 1 << pos);
Bjorn Helgaasdbe4a092017-03-16 14:34:59 -0500105 pos++;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530106 }
107 }
108
109 return ret;
110}
111
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000112/* Chained MSI interrupt service routine */
113static void dw_chained_msi_isr(struct irq_desc *desc)
114{
115 struct irq_chip *chip = irq_desc_get_chip(desc);
116 struct pcie_port *pp;
117
118 chained_irq_enter(chip, desc);
119
120 pp = irq_desc_get_handler_data(desc);
121 dw_handle_msi_irq(pp);
122
123 chained_irq_exit(chip, desc);
124}
125
126static void dw_pci_setup_msi_msg(struct irq_data *data, struct msi_msg *msg)
127{
128 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
129 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
130 u64 msi_target;
131
132 if (pp->ops->get_msi_addr)
133 msi_target = pp->ops->get_msi_addr(pp);
134 else
135 msi_target = (u64)pp->msi_data;
136
137 msg->address_lo = lower_32_bits(msi_target);
138 msg->address_hi = upper_32_bits(msi_target);
139
140 if (pp->ops->get_msi_data)
141 msg->data = pp->ops->get_msi_data(pp, data->hwirq);
142 else
143 msg->data = data->hwirq;
144
145 dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
146 (int)data->hwirq, msg->address_hi, msg->address_lo);
147}
148
149static int dw_pci_msi_set_affinity(struct irq_data *irq_data,
150 const struct cpumask *mask, bool force)
151{
152 return -EINVAL;
153}
154
155static void dw_pci_bottom_mask(struct irq_data *data)
156{
157 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
158 unsigned int res, bit, ctrl;
159 unsigned long flags;
160
161 raw_spin_lock_irqsave(&pp->lock, flags);
162
163 if (pp->ops->msi_clear_irq) {
164 pp->ops->msi_clear_irq(pp, data->hwirq);
165 } else {
Gustavo Pimentel76cbf062018-05-14 16:09:50 +0100166 ctrl = data->hwirq / MAX_MSI_IRQS_PER_CTRL;
167 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
168 bit = data->hwirq % MAX_MSI_IRQS_PER_CTRL;
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000169
170 pp->irq_status[ctrl] &= ~(1 << bit);
171 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4,
172 pp->irq_status[ctrl]);
173 }
174
175 raw_spin_unlock_irqrestore(&pp->lock, flags);
176}
177
178static void dw_pci_bottom_unmask(struct irq_data *data)
179{
180 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
181 unsigned int res, bit, ctrl;
182 unsigned long flags;
183
184 raw_spin_lock_irqsave(&pp->lock, flags);
185
186 if (pp->ops->msi_set_irq) {
187 pp->ops->msi_set_irq(pp, data->hwirq);
188 } else {
Gustavo Pimentel76cbf062018-05-14 16:09:50 +0100189 ctrl = data->hwirq / MAX_MSI_IRQS_PER_CTRL;
190 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
191 bit = data->hwirq % MAX_MSI_IRQS_PER_CTRL;
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000192
193 pp->irq_status[ctrl] |= 1 << bit;
194 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4,
195 pp->irq_status[ctrl]);
196 }
197
198 raw_spin_unlock_irqrestore(&pp->lock, flags);
199}
200
201static void dw_pci_bottom_ack(struct irq_data *d)
202{
203 struct msi_desc *msi = irq_data_get_msi_desc(d);
204 struct pcie_port *pp;
205
206 pp = msi_desc_to_pci_sysdata(msi);
207
208 if (pp->ops->msi_irq_ack)
209 pp->ops->msi_irq_ack(d->hwirq, pp);
210}
211
212static struct irq_chip dw_pci_msi_bottom_irq_chip = {
213 .name = "DWPCI-MSI",
214 .irq_ack = dw_pci_bottom_ack,
215 .irq_compose_msi_msg = dw_pci_setup_msi_msg,
216 .irq_set_affinity = dw_pci_msi_set_affinity,
217 .irq_mask = dw_pci_bottom_mask,
218 .irq_unmask = dw_pci_bottom_unmask,
219};
220
221static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
222 unsigned int virq, unsigned int nr_irqs,
223 void *args)
224{
225 struct pcie_port *pp = domain->host_data;
226 unsigned long flags;
227 u32 i;
228 int bit;
229
230 raw_spin_lock_irqsave(&pp->lock, flags);
231
232 bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
233 order_base_2(nr_irqs));
234
235 raw_spin_unlock_irqrestore(&pp->lock, flags);
236
237 if (bit < 0)
238 return -ENOSPC;
239
240 for (i = 0; i < nr_irqs; i++)
241 irq_domain_set_info(domain, virq + i, bit + i,
242 &dw_pci_msi_bottom_irq_chip,
243 pp, handle_edge_irq,
244 NULL, NULL);
245
246 return 0;
247}
248
249static void dw_pcie_irq_domain_free(struct irq_domain *domain,
250 unsigned int virq, unsigned int nr_irqs)
251{
252 struct irq_data *data = irq_domain_get_irq_data(domain, virq);
253 struct pcie_port *pp = irq_data_get_irq_chip_data(data);
254 unsigned long flags;
255
256 raw_spin_lock_irqsave(&pp->lock, flags);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100257
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000258 bitmap_release_region(pp->msi_irq_in_use, data->hwirq,
259 order_base_2(nr_irqs));
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100260
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000261 raw_spin_unlock_irqrestore(&pp->lock, flags);
262}
263
264static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
265 .alloc = dw_pcie_irq_domain_alloc,
266 .free = dw_pcie_irq_domain_free,
267};
268
269int dw_pcie_allocate_domains(struct pcie_port *pp)
270{
271 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
272 struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
273
274 pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
275 &dw_pcie_msi_domain_ops, pp);
276 if (!pp->irq_domain) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100277 dev_err(pci->dev, "Failed to create IRQ domain\n");
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000278 return -ENOMEM;
279 }
280
281 pp->msi_domain = pci_msi_create_irq_domain(fwnode,
282 &dw_pcie_msi_domain_info,
283 pp->irq_domain);
284 if (!pp->msi_domain) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100285 dev_err(pci->dev, "Failed to create MSI domain\n");
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000286 irq_domain_remove(pp->irq_domain);
287 return -ENOMEM;
288 }
289
290 return 0;
291}
292
293void dw_pcie_free_msi(struct pcie_port *pp)
294{
295 irq_set_chained_handler(pp->msi_irq, NULL);
296 irq_set_handler_data(pp->msi_irq, NULL);
297
298 irq_domain_remove(pp->msi_domain);
299 irq_domain_remove(pp->irq_domain);
300}
301
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530302void dw_pcie_msi_init(struct pcie_port *pp)
303{
Niklas Cassel111111a2017-12-20 00:29:22 +0100304 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
305 struct device *dev = pci->dev;
306 struct page *page;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530307 u64 msi_target;
308
Niklas Cassel111111a2017-12-20 00:29:22 +0100309 page = alloc_page(GFP_KERNEL);
310 pp->msi_data = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
311 if (dma_mapping_error(dev, pp->msi_data)) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100312 dev_err(dev, "Failed to map MSI data\n");
Niklas Cassel111111a2017-12-20 00:29:22 +0100313 __free_page(page);
314 return;
315 }
316 msi_target = (u64)pp->msi_data;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530317
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100318 /* Program the msi_data */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530319 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000320 lower_32_bits(msi_target));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530321 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4,
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000322 upper_32_bits(msi_target));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530323}
324
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530325int dw_pcie_host_init(struct pcie_port *pp)
326{
327 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
328 struct device *dev = pci->dev;
329 struct device_node *np = dev->of_node;
330 struct platform_device *pdev = to_platform_device(dev);
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000331 struct resource_entry *win, *tmp;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530332 struct pci_bus *bus, *child;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500333 struct pci_host_bridge *bridge;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530334 struct resource *cfg_res;
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000335 int ret;
336
337 raw_spin_lock_init(&pci->pp.lock);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530338
339 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
340 if (cfg_res) {
Gustavo Pimentel6995de22018-05-14 16:09:49 +0100341 pp->cfg0_size = resource_size(cfg_res) >> 1;
342 pp->cfg1_size = resource_size(cfg_res) >> 1;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530343 pp->cfg0_base = cfg_res->start;
344 pp->cfg1_base = cfg_res->start + pp->cfg0_size;
345 } else if (!pp->va_cfg0_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100346 dev_err(dev, "Missing *config* reg space\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530347 }
348
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500349 bridge = pci_alloc_host_bridge(0);
350 if (!bridge)
351 return -ENOMEM;
352
Jan Kiszka5bd51b32018-05-15 11:07:05 +0200353 ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500354 &bridge->windows, &pp->io_base);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530355 if (ret)
356 return ret;
357
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500358 ret = devm_request_pci_bus_resources(dev, &bridge->windows);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530359 if (ret)
360 goto error;
361
362 /* Get the I/O and memory ranges from DT */
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500363 resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530364 switch (resource_type(win->res)) {
365 case IORESOURCE_IO:
366 ret = pci_remap_iospace(win->res, pp->io_base);
367 if (ret) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100368 dev_warn(dev, "Error %d: failed to map resource %pR\n",
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530369 ret, win->res);
370 resource_list_destroy_entry(win);
371 } else {
372 pp->io = win->res;
373 pp->io->name = "I/O";
374 pp->io_size = resource_size(pp->io);
375 pp->io_bus_addr = pp->io->start - win->offset;
376 }
377 break;
378 case IORESOURCE_MEM:
379 pp->mem = win->res;
380 pp->mem->name = "MEM";
381 pp->mem_size = resource_size(pp->mem);
382 pp->mem_bus_addr = pp->mem->start - win->offset;
383 break;
384 case 0:
385 pp->cfg = win->res;
Gustavo Pimentel6995de22018-05-14 16:09:49 +0100386 pp->cfg0_size = resource_size(pp->cfg) >> 1;
387 pp->cfg1_size = resource_size(pp->cfg) >> 1;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530388 pp->cfg0_base = pp->cfg->start;
389 pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
390 break;
391 case IORESOURCE_BUS:
392 pp->busn = win->res;
393 break;
394 }
395 }
396
397 if (!pci->dbi_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100398 pci->dbi_base = devm_pci_remap_cfgspace(dev,
399 pp->cfg->start,
400 resource_size(pp->cfg));
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530401 if (!pci->dbi_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100402 dev_err(dev, "Error with ioremap\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530403 ret = -ENOMEM;
404 goto error;
405 }
406 }
407
408 pp->mem_base = pp->mem->start;
409
410 if (!pp->va_cfg0_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100411 pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
412 pp->cfg0_base, pp->cfg0_size);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530413 if (!pp->va_cfg0_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100414 dev_err(dev, "Error with ioremap in function\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530415 ret = -ENOMEM;
416 goto error;
417 }
418 }
419
420 if (!pp->va_cfg1_base) {
Lorenzo Pieralisicc7b0d42017-04-19 17:49:03 +0100421 pp->va_cfg1_base = devm_pci_remap_cfgspace(dev,
422 pp->cfg1_base,
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530423 pp->cfg1_size);
424 if (!pp->va_cfg1_base) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100425 dev_err(dev, "Error with ioremap\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530426 ret = -ENOMEM;
427 goto error;
428 }
429 }
430
431 ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
432 if (ret)
433 pci->num_viewport = 2;
434
435 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000436 /*
437 * If a specific SoC driver needs to change the
438 * default number of vectors, it needs to implement
439 * the set_num_vectors callback.
440 */
441 if (!pp->ops->set_num_vectors) {
442 pp->num_vectors = MSI_DEF_NUM_VECTORS;
443 } else {
444 pp->ops->set_num_vectors(pp);
445
446 if (pp->num_vectors > MAX_MSI_IRQS ||
447 pp->num_vectors == 0) {
448 dev_err(dev,
449 "Invalid number of vectors\n");
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530450 goto error;
451 }
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000452 }
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530453
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000454 if (!pp->ops->msi_host_init) {
455 ret = dw_pcie_allocate_domains(pp);
456 if (ret)
457 goto error;
458
459 if (pp->msi_irq)
460 irq_set_chained_handler_and_data(pp->msi_irq,
461 dw_chained_msi_isr,
462 pp);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530463 } else {
Gustavo Pimentel3f43ccc2018-03-06 11:54:54 +0000464 ret = pp->ops->msi_host_init(pp);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530465 if (ret < 0)
466 goto error;
467 }
468 }
469
Bjorn Andersson4a301762017-07-15 23:39:45 -0700470 if (pp->ops->host_init) {
471 ret = pp->ops->host_init(pp);
472 if (ret)
473 goto error;
474 }
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530475
476 pp->root_bus_nr = pp->busn->start;
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500477
478 bridge->dev.parent = dev;
479 bridge->sysdata = pp;
480 bridge->busnr = pp->root_bus_nr;
481 bridge->ops = &dw_pcie_ops;
Lorenzo Pieralisi60eca192017-06-28 15:14:07 -0500482 bridge->map_irq = of_irq_parse_and_map_pci;
483 bridge->swizzle_irq = pci_common_swizzle;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530484
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500485 ret = pci_scan_root_bus_bridge(bridge);
486 if (ret)
487 goto error;
488
489 bus = bridge->bus;
490
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530491 if (pp->ops->scan_bus)
492 pp->ops->scan_bus(pp);
493
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530494 pci_bus_size_bridges(bus);
495 pci_bus_assign_resources(bus);
496
497 list_for_each_entry(child, &bus->children, node)
498 pcie_bus_configure_settings(child);
499
500 pci_bus_add_devices(bus);
501 return 0;
502
503error:
Lorenzo Pieralisi295aeb92017-06-28 15:13:56 -0500504 pci_free_host_bridge(bridge);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530505 return ret;
506}
507
508static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
509 u32 devfn, int where, int size, u32 *val)
510{
511 int ret, type;
512 u32 busdev, cfg_size;
513 u64 cpu_addr;
514 void __iomem *va_cfg_base;
515 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
516
517 if (pp->ops->rd_other_conf)
518 return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val);
519
520 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
521 PCIE_ATU_FUNC(PCI_FUNC(devfn));
522
523 if (bus->parent->number == pp->root_bus_nr) {
524 type = PCIE_ATU_TYPE_CFG0;
525 cpu_addr = pp->cfg0_base;
526 cfg_size = pp->cfg0_size;
527 va_cfg_base = pp->va_cfg0_base;
528 } else {
529 type = PCIE_ATU_TYPE_CFG1;
530 cpu_addr = pp->cfg1_base;
531 cfg_size = pp->cfg1_size;
532 va_cfg_base = pp->va_cfg1_base;
533 }
534
535 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
536 type, cpu_addr,
537 busdev, cfg_size);
538 ret = dw_pcie_read(va_cfg_base + where, size, val);
539 if (pci->num_viewport <= 2)
540 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
541 PCIE_ATU_TYPE_IO, pp->io_base,
542 pp->io_bus_addr, pp->io_size);
543
544 return ret;
545}
546
547static 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 int ret, type;
551 u32 busdev, cfg_size;
552 u64 cpu_addr;
553 void __iomem *va_cfg_base;
554 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
555
556 if (pp->ops->wr_other_conf)
557 return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val);
558
559 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
560 PCIE_ATU_FUNC(PCI_FUNC(devfn));
561
562 if (bus->parent->number == pp->root_bus_nr) {
563 type = PCIE_ATU_TYPE_CFG0;
564 cpu_addr = pp->cfg0_base;
565 cfg_size = pp->cfg0_size;
566 va_cfg_base = pp->va_cfg0_base;
567 } else {
568 type = PCIE_ATU_TYPE_CFG1;
569 cpu_addr = pp->cfg1_base;
570 cfg_size = pp->cfg1_size;
571 va_cfg_base = pp->va_cfg1_base;
572 }
573
574 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
575 type, cpu_addr,
576 busdev, cfg_size);
577 ret = dw_pcie_write(va_cfg_base + where, size, val);
578 if (pci->num_viewport <= 2)
579 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
580 PCIE_ATU_TYPE_IO, pp->io_base,
581 pp->io_bus_addr, pp->io_size);
582
583 return ret;
584}
585
586static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
587 int dev)
588{
589 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
590
591 /* If there is no link, then there is no device */
592 if (bus->number != pp->root_bus_nr) {
593 if (!dw_pcie_link_up(pci))
594 return 0;
595 }
596
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100597 /* Access only one slot on each root port */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530598 if (bus->number == pp->root_bus_nr && dev > 0)
599 return 0;
600
601 return 1;
602}
603
604static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
605 int size, u32 *val)
606{
607 struct pcie_port *pp = bus->sysdata;
608
609 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
610 *val = 0xffffffff;
611 return PCIBIOS_DEVICE_NOT_FOUND;
612 }
613
614 if (bus->number == pp->root_bus_nr)
615 return dw_pcie_rd_own_conf(pp, where, size, val);
616
617 return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
618}
619
620static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
621 int where, int size, u32 val)
622{
623 struct pcie_port *pp = bus->sysdata;
624
625 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
626 return PCIBIOS_DEVICE_NOT_FOUND;
627
628 if (bus->number == pp->root_bus_nr)
629 return dw_pcie_wr_own_conf(pp, where, size, val);
630
631 return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
632}
633
634static struct pci_ops dw_pcie_ops = {
635 .read = dw_pcie_rd_conf,
636 .write = dw_pcie_wr_conf,
637};
638
639static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci)
640{
641 u32 val;
642
643 val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT);
644 if (val == 0xffffffff)
645 return 1;
646
647 return 0;
648}
649
650void dw_pcie_setup_rc(struct pcie_port *pp)
651{
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000652 u32 val, ctrl, num_ctrls;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530653 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
654
655 dw_pcie_setup(pci);
656
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000657 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
658
Gustavo Pimentel7c5925a2018-03-06 11:54:53 +0000659 /* Initialize IRQ Status array */
Gustavo Pimentel1f319cb2018-03-06 11:54:55 +0000660 for (ctrl = 0; ctrl < num_ctrls; ctrl++)
Gustavo Pimentel76cbf062018-05-14 16:09:50 +0100661 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE +
662 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
663 4, &pp->irq_status[ctrl]);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100664
665 /* Setup RC BARs */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530666 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
667 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
668
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100669 /* Setup interrupt pins */
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800670 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530671 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
672 val &= 0xffff00ff;
673 val |= 0x00000100;
674 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800675 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530676
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100677 /* Setup bus numbers */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530678 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
679 val &= 0xff000000;
Koen Vandeputtefc110eb2018-03-07 10:46:39 -0600680 val |= 0x00ff0100;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530681 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
682
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100683 /* Setup command register */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530684 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
685 val &= 0xffff0000;
686 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
687 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
688 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
689
690 /*
691 * If the platform provides ->rd_other_conf, it means the platform
692 * uses its own address translation component rather than ATU, so
693 * we should not program the ATU here.
694 */
695 if (!pp->ops->rd_other_conf) {
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100696 /* Get iATU unroll support */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530697 pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci);
698 dev_dbg(pci->dev, "iATU unroll: %s\n",
699 pci->iatu_unroll_enabled ? "enabled" : "disabled");
700
701 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
702 PCIE_ATU_TYPE_MEM, pp->mem_base,
703 pp->mem_bus_addr, pp->mem_size);
704 if (pci->num_viewport > 2)
705 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
706 PCIE_ATU_TYPE_IO, pp->io_base,
707 pp->io_bus_addr, pp->io_size);
708 }
709
710 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
711
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800712 /* Enable write permission for the DBI read-only register */
713 dw_pcie_dbi_ro_wr_en(pci);
Gustavo Pimentelb4a8a512018-05-14 16:09:48 +0100714 /* Program correct class for RC */
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530715 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
Hou Zhiqiangd91dfe52017-08-28 18:53:00 +0800716 /* Better disable write permission right after the update */
717 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530718
719 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
720 val |= PORT_LOGIC_SPEED_CHANGE;
721 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
722}