blob: 2235f4760951a8ed80ba2df451549982fcab3a8c [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Ley Foon Taneaa61112015-10-23 18:27:12 +08002/*
3 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
4 *
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -04005 * Author: Ley Foon Tan <lftan@altera.com>
6 * Description: Altera PCIe host controller driver
Ley Foon Taneaa61112015-10-23 18:27:12 +08007 */
8
9#include <linux/delay.h>
10#include <linux/interrupt.h>
11#include <linux/irqchip/chained_irq.h>
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -040012#include <linux/init.h>
Ley Foon Taneaa61112015-10-23 18:27:12 +080013#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/of_pci.h>
16#include <linux/pci.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
20#define RP_TX_REG0 0x2000
21#define RP_TX_REG1 0x2004
22#define RP_TX_CNTRL 0x2008
23#define RP_TX_EOP 0x2
24#define RP_TX_SOP 0x1
25#define RP_RXCPL_STATUS 0x2010
26#define RP_RXCPL_EOP 0x2
27#define RP_RXCPL_SOP 0x1
28#define RP_RXCPL_REG0 0x2014
29#define RP_RXCPL_REG1 0x2018
30#define P2A_INT_STATUS 0x3060
31#define P2A_INT_STS_ALL 0xf
32#define P2A_INT_ENABLE 0x3070
33#define P2A_INT_ENA_ALL 0xf
34#define RP_LTSSM 0x3c64
Ley Foon Taneff31f42016-03-02 17:43:07 +080035#define RP_LTSSM_MASK 0x1f
Ley Foon Taneaa61112015-10-23 18:27:12 +080036#define LTSSM_L0 0xf
37
Ley Foon Tance4f1c72016-08-26 09:47:25 +080038#define PCIE_CAP_OFFSET 0x80
Ley Foon Taneaa61112015-10-23 18:27:12 +080039/* TLP configuration type 0 and 1 */
40#define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
41#define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
42#define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
43#define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
44#define TLP_PAYLOAD_SIZE 0x01
45#define TLP_READ_TAG 0x1d
46#define TLP_WRITE_TAG 0x10
Bjorn Helgaas4f276282016-10-06 13:29:02 -050047#define RP_DEVFN 0
48#define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
Ley Foon Tan2a7275a2017-02-28 18:37:16 +080049#define TLP_CFGRD_DW0(pcie, bus) \
Bjorn Helgaaseb576712016-10-06 13:29:01 -050050 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGRD0 \
51 : TLP_FMTTYPE_CFGRD1) << 24) | \
52 TLP_PAYLOAD_SIZE)
Ley Foon Tan2a7275a2017-02-28 18:37:16 +080053#define TLP_CFGWR_DW0(pcie, bus) \
54 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGWR0 \
55 : TLP_FMTTYPE_CFGWR1) << 24) | \
56 TLP_PAYLOAD_SIZE)
Bjorn Helgaas4f276282016-10-06 13:29:02 -050057#define TLP_CFG_DW1(pcie, tag, be) \
58 (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
Ley Foon Taneaa61112015-10-23 18:27:12 +080059#define TLP_CFG_DW2(bus, devfn, offset) \
60 (((bus) << 24) | ((devfn) << 16) | (offset))
Yadi Hu8ca6e0a2017-02-17 14:20:26 -060061#define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
Ley Foon Taneaa61112015-10-23 18:27:12 +080062#define TLP_HDR_SIZE 3
63#define TLP_LOOP 500
64
Ley Foon Tan411dc322016-08-15 14:06:02 +080065#define LINK_UP_TIMEOUT HZ
66#define LINK_RETRAIN_TIMEOUT HZ
Ley Foon Tan3a928e92016-06-21 16:53:13 +080067
Ley Foon Taneaa61112015-10-23 18:27:12 +080068#define DWORD_MASK 3
69
70struct altera_pcie {
71 struct platform_device *pdev;
Bjorn Helgaasdbeb4bd2016-10-06 13:29:02 -050072 void __iomem *cra_base; /* DT Cra */
Ley Foon Taneaa61112015-10-23 18:27:12 +080073 int irq;
74 u8 root_bus_nr;
75 struct irq_domain *irq_domain;
76 struct resource bus_range;
77 struct list_head resources;
78};
79
80struct tlp_rp_regpair_t {
81 u32 ctrl;
82 u32 reg0;
83 u32 reg1;
84};
85
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050086static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
87 const u32 reg)
88{
89 writel_relaxed(value, pcie->cra_base + reg);
90}
91
92static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
93{
94 return readl_relaxed(pcie->cra_base + reg);
95}
96
Bjorn Helgaas499c0102017-11-09 16:17:39 -060097static bool altera_pcie_link_up(struct altera_pcie *pcie)
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050098{
99 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
100}
101
Ley Foon Taneaa61112015-10-23 18:27:12 +0800102/*
103 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
104 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
105 * using these registers, so it can be reached by DMA from EP devices.
106 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
107 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
108 * should be hidden during enumeration to avoid the sizing and resource
109 * allocation by PCIe core.
110 */
111static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
112 int offset)
113{
114 if (pci_is_root_bus(bus) && (devfn == 0) &&
115 (offset == PCI_BASE_ADDRESS_0))
116 return true;
117
118 return false;
119}
120
Ley Foon Taneaa61112015-10-23 18:27:12 +0800121static void tlp_write_tx(struct altera_pcie *pcie,
122 struct tlp_rp_regpair_t *tlp_rp_regdata)
123{
124 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
125 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
126 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
127}
128
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500129static bool altera_pcie_valid_device(struct altera_pcie *pcie,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800130 struct pci_bus *bus, int dev)
131{
132 /* If there is no link, then there is no device */
133 if (bus->number != pcie->root_bus_nr) {
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600134 if (!altera_pcie_link_up(pcie))
Ley Foon Taneaa61112015-10-23 18:27:12 +0800135 return false;
136 }
137
138 /* access only one slot on each root port */
139 if (bus->number == pcie->root_bus_nr && dev > 0)
140 return false;
141
Ley Foon Taneaa61112015-10-23 18:27:12 +0800142 return true;
143}
144
145static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
146{
Dan Carpenter7f52f312015-12-04 16:21:08 -0600147 int i;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800148 bool sop = 0;
149 u32 ctrl;
150 u32 reg0, reg1;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600151 u32 comp_status = 1;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800152
153 /*
154 * Minimum 2 loops to read TLP headers and 1 loop to read data
155 * payload.
156 */
Dan Carpenter7f52f312015-12-04 16:21:08 -0600157 for (i = 0; i < TLP_LOOP; i++) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800158 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
159 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
160 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
161 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
162
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600163 if (ctrl & RP_RXCPL_SOP) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800164 sop = true;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600165 comp_status = TLP_COMP_STATUS(reg1);
166 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800167
168 if (ctrl & RP_RXCPL_EOP) {
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600169 if (comp_status)
170 return PCIBIOS_DEVICE_NOT_FOUND;
171
Ley Foon Taneaa61112015-10-23 18:27:12 +0800172 if (value)
173 *value = reg0;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600174
Ley Foon Taneaa61112015-10-23 18:27:12 +0800175 return PCIBIOS_SUCCESSFUL;
176 }
177 }
178 udelay(5);
179 }
180
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600181 return PCIBIOS_DEVICE_NOT_FOUND;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800182}
183
184static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
185 u32 data, bool align)
186{
187 struct tlp_rp_regpair_t tlp_rp_regdata;
188
189 tlp_rp_regdata.reg0 = headers[0];
190 tlp_rp_regdata.reg1 = headers[1];
191 tlp_rp_regdata.ctrl = RP_TX_SOP;
192 tlp_write_tx(pcie, &tlp_rp_regdata);
193
194 if (align) {
195 tlp_rp_regdata.reg0 = headers[2];
196 tlp_rp_regdata.reg1 = 0;
197 tlp_rp_regdata.ctrl = 0;
198 tlp_write_tx(pcie, &tlp_rp_regdata);
199
200 tlp_rp_regdata.reg0 = data;
201 tlp_rp_regdata.reg1 = 0;
202 } else {
203 tlp_rp_regdata.reg0 = headers[2];
204 tlp_rp_regdata.reg1 = data;
205 }
206
207 tlp_rp_regdata.ctrl = RP_TX_EOP;
208 tlp_write_tx(pcie, &tlp_rp_regdata);
209}
210
211static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
212 int where, u8 byte_en, u32 *value)
213{
214 u32 headers[TLP_HDR_SIZE];
215
Ley Foon Tan2a7275a2017-02-28 18:37:16 +0800216 headers[0] = TLP_CFGRD_DW0(pcie, bus);
Bjorn Helgaas4f276282016-10-06 13:29:02 -0500217 headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800218 headers[2] = TLP_CFG_DW2(bus, devfn, where);
219
220 tlp_write_packet(pcie, headers, 0, false);
221
222 return tlp_read_packet(pcie, value);
223}
224
225static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
226 int where, u8 byte_en, u32 value)
227{
228 u32 headers[TLP_HDR_SIZE];
229 int ret;
230
Ley Foon Tan2a7275a2017-02-28 18:37:16 +0800231 headers[0] = TLP_CFGWR_DW0(pcie, bus);
Bjorn Helgaas4f276282016-10-06 13:29:02 -0500232 headers[1] = TLP_CFG_DW1(pcie, TLP_WRITE_TAG, byte_en);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800233 headers[2] = TLP_CFG_DW2(bus, devfn, where);
234
235 /* check alignment to Qword */
236 if ((where & 0x7) == 0)
237 tlp_write_packet(pcie, headers, value, true);
238 else
239 tlp_write_packet(pcie, headers, value, false);
240
241 ret = tlp_read_packet(pcie, NULL);
242 if (ret != PCIBIOS_SUCCESSFUL)
243 return ret;
244
245 /*
246 * Monitor changes to PCI_PRIMARY_BUS register on root port
247 * and update local copy of root bus number accordingly.
248 */
249 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
250 pcie->root_bus_nr = (u8)(value);
251
252 return PCIBIOS_SUCCESSFUL;
253}
254
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800255static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
256 unsigned int devfn, int where, int size,
257 u32 *value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800258{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800259 int ret;
260 u32 data;
261 u8 byte_en;
262
Ley Foon Taneaa61112015-10-23 18:27:12 +0800263 switch (size) {
264 case 1:
265 byte_en = 1 << (where & 3);
266 break;
267 case 2:
268 byte_en = 3 << (where & 3);
269 break;
270 default:
271 byte_en = 0xf;
272 break;
273 }
274
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800275 ret = tlp_cfg_dword_read(pcie, busno, devfn,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800276 (where & ~DWORD_MASK), byte_en, &data);
277 if (ret != PCIBIOS_SUCCESSFUL)
278 return ret;
279
280 switch (size) {
281 case 1:
282 *value = (data >> (8 * (where & 0x3))) & 0xff;
283 break;
284 case 2:
285 *value = (data >> (8 * (where & 0x2))) & 0xffff;
286 break;
287 default:
288 *value = data;
289 break;
290 }
291
292 return PCIBIOS_SUCCESSFUL;
293}
294
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800295static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
296 unsigned int devfn, int where, int size,
297 u32 value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800298{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800299 u32 data32;
300 u32 shift = 8 * (where & 3);
301 u8 byte_en;
302
Ley Foon Taneaa61112015-10-23 18:27:12 +0800303 switch (size) {
304 case 1:
305 data32 = (value & 0xff) << shift;
306 byte_en = 1 << (where & 3);
307 break;
308 case 2:
309 data32 = (value & 0xffff) << shift;
310 byte_en = 3 << (where & 3);
311 break;
312 default:
313 data32 = value;
314 byte_en = 0xf;
315 break;
316 }
317
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800318 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
319 byte_en, data32);
320}
321
322static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
323 int where, int size, u32 *value)
324{
325 struct altera_pcie *pcie = bus->sysdata;
326
327 if (altera_pcie_hide_rc_bar(bus, devfn, where))
328 return PCIBIOS_BAD_REGISTER_NUMBER;
329
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500330 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) {
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800331 *value = 0xffffffff;
332 return PCIBIOS_DEVICE_NOT_FOUND;
333 }
334
335 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
336 value);
337}
338
339static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
340 int where, int size, u32 value)
341{
342 struct altera_pcie *pcie = bus->sysdata;
343
344 if (altera_pcie_hide_rc_bar(bus, devfn, where))
345 return PCIBIOS_BAD_REGISTER_NUMBER;
346
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500347 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800348 return PCIBIOS_DEVICE_NOT_FOUND;
349
350 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
351 value);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800352}
353
354static struct pci_ops altera_pcie_ops = {
355 .read = altera_pcie_cfg_read,
356 .write = altera_pcie_cfg_write,
357};
358
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800359static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
360 unsigned int devfn, int offset, u16 *value)
361{
362 u32 data;
363 int ret;
364
365 ret = _altera_pcie_cfg_read(pcie, busno, devfn,
366 PCIE_CAP_OFFSET + offset, sizeof(*value),
367 &data);
368 *value = data;
369 return ret;
370}
371
372static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
373 unsigned int devfn, int offset, u16 value)
374{
375 return _altera_pcie_cfg_write(pcie, busno, devfn,
376 PCIE_CAP_OFFSET + offset, sizeof(value),
377 value);
378}
379
380static void altera_wait_link_retrain(struct altera_pcie *pcie)
381{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500382 struct device *dev = &pcie->pdev->dev;
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800383 u16 reg16;
384 unsigned long start_jiffies;
385
386 /* Wait for link training end. */
387 start_jiffies = jiffies;
388 for (;;) {
389 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
390 PCI_EXP_LNKSTA, &reg16);
391 if (!(reg16 & PCI_EXP_LNKSTA_LT))
392 break;
393
394 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500395 dev_err(dev, "link retrain timeout\n");
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800396 break;
397 }
398 udelay(100);
399 }
400
401 /* Wait for link is up */
402 start_jiffies = jiffies;
403 for (;;) {
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600404 if (altera_pcie_link_up(pcie))
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800405 break;
406
407 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500408 dev_err(dev, "link up timeout\n");
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800409 break;
410 }
411 udelay(100);
412 }
413}
414
415static void altera_pcie_retrain(struct altera_pcie *pcie)
416{
417 u16 linkcap, linkstat, linkctl;
418
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600419 if (!altera_pcie_link_up(pcie))
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800420 return;
421
422 /*
423 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
424 * current speed is 2.5 GB/s.
425 */
426 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
427 &linkcap);
428 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
429 return;
430
431 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
432 &linkstat);
433 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
434 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
435 PCI_EXP_LNKCTL, &linkctl);
436 linkctl |= PCI_EXP_LNKCTL_RL;
437 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
438 PCI_EXP_LNKCTL, linkctl);
439
440 altera_wait_link_retrain(pcie);
441 }
442}
443
Ley Foon Taneaa61112015-10-23 18:27:12 +0800444static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
445 irq_hw_number_t hwirq)
446{
447 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
448 irq_set_chip_data(irq, domain->host_data);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800449 return 0;
450}
451
452static const struct irq_domain_ops intx_domain_ops = {
453 .map = altera_pcie_intx_map,
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500454 .xlate = pci_irqd_intx_xlate,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800455};
456
457static void altera_pcie_isr(struct irq_desc *desc)
458{
459 struct irq_chip *chip = irq_desc_get_chip(desc);
460 struct altera_pcie *pcie;
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500461 struct device *dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800462 unsigned long status;
463 u32 bit;
464 u32 virq;
465
466 chained_irq_enter(chip, desc);
467 pcie = irq_desc_get_handler_data(desc);
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500468 dev = &pcie->pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800469
470 while ((status = cra_readl(pcie, P2A_INT_STATUS)
471 & P2A_INT_STS_ALL) != 0) {
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500472 for_each_set_bit(bit, &status, PCI_NUM_INTX) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800473 /* clear interrupts */
474 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
475
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500476 virq = irq_find_mapping(pcie->irq_domain, bit);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800477 if (virq)
478 generic_handle_irq(virq);
479 else
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500480 dev_err(dev, "unexpected IRQ, INT%d\n", bit);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800481 }
482 }
483
484 chained_irq_exit(chip, desc);
485}
486
Ley Foon Taneaa61112015-10-23 18:27:12 +0800487static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
488{
489 int err, res_valid = 0;
490 struct device *dev = &pcie->pdev->dev;
491 struct device_node *np = dev->of_node;
492 struct resource_entry *win;
493
494 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
495 NULL);
496 if (err)
497 return err;
498
Bjorn Helgaas74462282016-05-31 12:14:17 -0500499 err = devm_request_pci_bus_resources(dev, &pcie->resources);
500 if (err)
501 goto out_release_res;
502
Ley Foon Taneaa61112015-10-23 18:27:12 +0800503 resource_list_for_each_entry(win, &pcie->resources) {
Bjorn Helgaas74462282016-05-31 12:14:17 -0500504 struct resource *res = win->res;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800505
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500506 if (resource_type(res) == IORESOURCE_MEM)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800507 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800508 }
509
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500510 if (res_valid)
511 return 0;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800512
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500513 dev_err(dev, "non-prefetchable memory resource required\n");
514 err = -EINVAL;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800515
516out_release_res:
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500517 pci_free_resource_list(&pcie->resources);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800518 return err;
519}
520
521static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
522{
523 struct device *dev = &pcie->pdev->dev;
524 struct device_node *node = dev->of_node;
525
526 /* Setup INTx */
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500527 pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800528 &intx_domain_ops, pcie);
529 if (!pcie->irq_domain) {
530 dev_err(dev, "Failed to get a INTx IRQ domain\n");
531 return -ENOMEM;
532 }
533
534 return 0;
535}
536
537static int altera_pcie_parse_dt(struct altera_pcie *pcie)
538{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500539 struct device *dev = &pcie->pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800540 struct platform_device *pdev = pcie->pdev;
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500541 struct resource *cra;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800542
543 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500544 pcie->cra_base = devm_ioremap_resource(dev, cra);
Wei Yongjunc19699a2016-10-17 14:56:13 +0000545 if (IS_ERR(pcie->cra_base))
Ley Foon Taneaa61112015-10-23 18:27:12 +0800546 return PTR_ERR(pcie->cra_base);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800547
548 /* setup IRQ */
549 pcie->irq = platform_get_irq(pdev, 0);
Fabio Estevamef753692017-08-31 14:52:07 -0300550 if (pcie->irq < 0) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500551 dev_err(dev, "failed to get IRQ: %d\n", pcie->irq);
Fabio Estevamef753692017-08-31 14:52:07 -0300552 return pcie->irq;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800553 }
554
555 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800556 return 0;
557}
558
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800559static void altera_pcie_host_init(struct altera_pcie *pcie)
560{
561 altera_pcie_retrain(pcie);
562}
563
Ley Foon Taneaa61112015-10-23 18:27:12 +0800564static int altera_pcie_probe(struct platform_device *pdev)
565{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500566 struct device *dev = &pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800567 struct altera_pcie *pcie;
568 struct pci_bus *bus;
569 struct pci_bus *child;
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500570 struct pci_host_bridge *bridge;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800571 int ret;
572
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500573 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
574 if (!bridge)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800575 return -ENOMEM;
576
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500577 pcie = pci_host_bridge_priv(bridge);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800578 pcie->pdev = pdev;
579
580 ret = altera_pcie_parse_dt(pcie);
581 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500582 dev_err(dev, "Parsing DT failed\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800583 return ret;
584 }
585
586 INIT_LIST_HEAD(&pcie->resources);
587
588 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
589 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500590 dev_err(dev, "Failed add resources\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800591 return ret;
592 }
593
594 ret = altera_pcie_init_irq_domain(pcie);
595 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500596 dev_err(dev, "Failed creating IRQ Domain\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800597 return ret;
598 }
599
600 /* clear all interrupts */
601 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
602 /* enable all interrupts */
603 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800604 altera_pcie_host_init(pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800605
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500606 list_splice_init(&pcie->resources, &bridge->windows);
607 bridge->dev.parent = dev;
608 bridge->sysdata = pcie;
609 bridge->busnr = pcie->root_bus_nr;
610 bridge->ops = &altera_pcie_ops;
Lorenzo Pieralisi6ab38092017-06-28 15:14:10 -0500611 bridge->map_irq = of_irq_parse_and_map_pci;
612 bridge->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500613
614 ret = pci_scan_root_bus_bridge(bridge);
615 if (ret < 0)
616 return ret;
617
618 bus = bridge->bus;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800619
Ley Foon Taneaa61112015-10-23 18:27:12 +0800620 pci_assign_unassigned_bus_resources(bus);
621
622 /* Configure PCI Express setting. */
623 list_for_each_entry(child, &bus->children, node)
624 pcie_bus_configure_settings(child);
625
626 pci_bus_add_devices(bus);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800627 return ret;
628}
629
630static const struct of_device_id altera_pcie_of_match[] = {
631 { .compatible = "altr,pcie-root-port-1.0", },
632 {},
633};
Ley Foon Taneaa61112015-10-23 18:27:12 +0800634
635static struct platform_driver altera_pcie_driver = {
636 .probe = altera_pcie_probe,
637 .driver = {
638 .name = "altera-pcie",
639 .of_match_table = altera_pcie_of_match,
640 .suppress_bind_attrs = true,
641 },
642};
643
Wei Yongjunc5d933b2016-09-10 12:31:01 +0000644builtin_platform_driver(altera_pcie_driver);