blob: 34e6258992c16c993b3af1f4b9020669e7190772 [file] [log] [blame]
Ley Foon Taneaa61112015-10-23 18:27:12 +08001/*
2 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19#include <linux/irqchip/chained_irq.h>
20#include <linux/module.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/of_pci.h>
24#include <linux/pci.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27
28#define RP_TX_REG0 0x2000
29#define RP_TX_REG1 0x2004
30#define RP_TX_CNTRL 0x2008
31#define RP_TX_EOP 0x2
32#define RP_TX_SOP 0x1
33#define RP_RXCPL_STATUS 0x2010
34#define RP_RXCPL_EOP 0x2
35#define RP_RXCPL_SOP 0x1
36#define RP_RXCPL_REG0 0x2014
37#define RP_RXCPL_REG1 0x2018
38#define P2A_INT_STATUS 0x3060
39#define P2A_INT_STS_ALL 0xf
40#define P2A_INT_ENABLE 0x3070
41#define P2A_INT_ENA_ALL 0xf
42#define RP_LTSSM 0x3c64
Ley Foon Taneff31f42016-03-02 17:43:07 +080043#define RP_LTSSM_MASK 0x1f
Ley Foon Taneaa61112015-10-23 18:27:12 +080044#define LTSSM_L0 0xf
45
46/* TLP configuration type 0 and 1 */
47#define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
48#define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
49#define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
50#define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
51#define TLP_PAYLOAD_SIZE 0x01
52#define TLP_READ_TAG 0x1d
53#define TLP_WRITE_TAG 0x10
54#define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
55#define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
56#define TLP_CFG_DW2(bus, devfn, offset) \
57 (((bus) << 24) | ((devfn) << 16) | (offset))
58#define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
Ley Foon Tanea1d3792015-12-04 16:21:16 -060059#define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
Ley Foon Taneaa61112015-10-23 18:27:12 +080060#define TLP_HDR_SIZE 3
61#define TLP_LOOP 500
Ley Foon Tan23ec5672015-12-04 16:21:12 -060062#define RP_DEVFN 0
Ley Foon Taneaa61112015-10-23 18:27:12 +080063
Ley Foon Tan411dc322016-08-15 14:06:02 +080064#define LINK_UP_TIMEOUT HZ
65#define LINK_RETRAIN_TIMEOUT HZ
Ley Foon Tan3a928e92016-06-21 16:53:13 +080066
Ley Foon Taneaa61112015-10-23 18:27:12 +080067#define INTX_NUM 4
68
69#define DWORD_MASK 3
70
71struct altera_pcie {
72 struct platform_device *pdev;
73 void __iomem *cra_base;
74 int irq;
75 u8 root_bus_nr;
76 struct irq_domain *irq_domain;
77 struct resource bus_range;
78 struct list_head resources;
79};
80
81struct tlp_rp_regpair_t {
82 u32 ctrl;
83 u32 reg0;
84 u32 reg1;
85};
86
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050087static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
88 const u32 reg)
89{
90 writel_relaxed(value, pcie->cra_base + reg);
91}
92
93static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
94{
95 return readl_relaxed(pcie->cra_base + reg);
96}
97
98static bool altera_pcie_link_is_up(struct altera_pcie *pcie)
99{
100 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
101}
102
Ley Foon Tan411dc322016-08-15 14:06:02 +0800103static void altera_wait_link_retrain(struct pci_dev *dev)
104{
105 u16 reg16;
106 unsigned long start_jiffies;
107 struct altera_pcie *pcie = dev->bus->sysdata;
108
109 /* Wait for link training end. */
110 start_jiffies = jiffies;
111 for (;;) {
112 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &reg16);
113 if (!(reg16 & PCI_EXP_LNKSTA_LT))
114 break;
115
116 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
117 dev_err(&pcie->pdev->dev, "link retrain timeout\n");
118 break;
119 }
120 udelay(100);
121 }
122
123 /* Wait for link is up */
124 start_jiffies = jiffies;
125 for (;;) {
126 if (altera_pcie_link_is_up(pcie))
127 break;
128
129 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
130 dev_err(&pcie->pdev->dev, "link up timeout\n");
131 break;
132 }
133 udelay(100);
134 }
135}
136
Ley Foon Taneaa61112015-10-23 18:27:12 +0800137static void altera_pcie_retrain(struct pci_dev *dev)
138{
139 u16 linkcap, linkstat;
Ley Foon Tanc6220322016-06-21 16:53:12 +0800140 struct altera_pcie *pcie = dev->bus->sysdata;
141
142 if (!altera_pcie_link_is_up(pcie))
143 return;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800144
145 /*
146 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
147 * current speed is 2.5 GB/s.
148 */
149 pcie_capability_read_word(dev, PCI_EXP_LNKCAP, &linkcap);
150
151 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
152 return;
153
154 pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat);
Ley Foon Tan3a928e92016-06-21 16:53:13 +0800155 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800156 pcie_capability_set_word(dev, PCI_EXP_LNKCTL,
157 PCI_EXP_LNKCTL_RL);
Ley Foon Tan411dc322016-08-15 14:06:02 +0800158 altera_wait_link_retrain(dev);
Ley Foon Tan3a928e92016-06-21 16:53:13 +0800159 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800160}
161DECLARE_PCI_FIXUP_EARLY(0x1172, PCI_ANY_ID, altera_pcie_retrain);
162
163/*
164 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
165 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
166 * using these registers, so it can be reached by DMA from EP devices.
167 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
168 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
169 * should be hidden during enumeration to avoid the sizing and resource
170 * allocation by PCIe core.
171 */
172static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
173 int offset)
174{
175 if (pci_is_root_bus(bus) && (devfn == 0) &&
176 (offset == PCI_BASE_ADDRESS_0))
177 return true;
178
179 return false;
180}
181
Ley Foon Taneaa61112015-10-23 18:27:12 +0800182static void tlp_write_tx(struct altera_pcie *pcie,
183 struct tlp_rp_regpair_t *tlp_rp_regdata)
184{
185 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
186 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
187 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
188}
189
Ley Foon Taneaa61112015-10-23 18:27:12 +0800190static bool altera_pcie_valid_config(struct altera_pcie *pcie,
191 struct pci_bus *bus, int dev)
192{
193 /* If there is no link, then there is no device */
194 if (bus->number != pcie->root_bus_nr) {
195 if (!altera_pcie_link_is_up(pcie))
196 return false;
197 }
198
199 /* access only one slot on each root port */
200 if (bus->number == pcie->root_bus_nr && dev > 0)
201 return false;
202
203 /*
204 * Do not read more than one device on the bus directly attached
205 * to root port, root port can only attach to one downstream port.
206 */
207 if (bus->primary == pcie->root_bus_nr && dev > 0)
208 return false;
209
210 return true;
211}
212
213static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
214{
Dan Carpenter7f52f312015-12-04 16:21:08 -0600215 int i;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800216 bool sop = 0;
217 u32 ctrl;
218 u32 reg0, reg1;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600219 u32 comp_status = 1;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800220
221 /*
222 * Minimum 2 loops to read TLP headers and 1 loop to read data
223 * payload.
224 */
Dan Carpenter7f52f312015-12-04 16:21:08 -0600225 for (i = 0; i < TLP_LOOP; i++) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800226 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
227 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
228 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
229 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
230
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600231 if (ctrl & RP_RXCPL_SOP) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800232 sop = true;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600233 comp_status = TLP_COMP_STATUS(reg1);
234 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800235
236 if (ctrl & RP_RXCPL_EOP) {
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600237 if (comp_status)
238 return PCIBIOS_DEVICE_NOT_FOUND;
239
Ley Foon Taneaa61112015-10-23 18:27:12 +0800240 if (value)
241 *value = reg0;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600242
Ley Foon Taneaa61112015-10-23 18:27:12 +0800243 return PCIBIOS_SUCCESSFUL;
244 }
245 }
246 udelay(5);
247 }
248
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600249 return PCIBIOS_DEVICE_NOT_FOUND;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800250}
251
252static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
253 u32 data, bool align)
254{
255 struct tlp_rp_regpair_t tlp_rp_regdata;
256
257 tlp_rp_regdata.reg0 = headers[0];
258 tlp_rp_regdata.reg1 = headers[1];
259 tlp_rp_regdata.ctrl = RP_TX_SOP;
260 tlp_write_tx(pcie, &tlp_rp_regdata);
261
262 if (align) {
263 tlp_rp_regdata.reg0 = headers[2];
264 tlp_rp_regdata.reg1 = 0;
265 tlp_rp_regdata.ctrl = 0;
266 tlp_write_tx(pcie, &tlp_rp_regdata);
267
268 tlp_rp_regdata.reg0 = data;
269 tlp_rp_regdata.reg1 = 0;
270 } else {
271 tlp_rp_regdata.reg0 = headers[2];
272 tlp_rp_regdata.reg1 = data;
273 }
274
275 tlp_rp_regdata.ctrl = RP_TX_EOP;
276 tlp_write_tx(pcie, &tlp_rp_regdata);
277}
278
279static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
280 int where, u8 byte_en, u32 *value)
281{
282 u32 headers[TLP_HDR_SIZE];
283
284 if (bus == pcie->root_bus_nr)
285 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0);
286 else
287 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1);
288
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600289 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800290 TLP_READ_TAG, byte_en);
291 headers[2] = TLP_CFG_DW2(bus, devfn, where);
292
293 tlp_write_packet(pcie, headers, 0, false);
294
295 return tlp_read_packet(pcie, value);
296}
297
298static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
299 int where, u8 byte_en, u32 value)
300{
301 u32 headers[TLP_HDR_SIZE];
302 int ret;
303
304 if (bus == pcie->root_bus_nr)
305 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0);
306 else
307 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1);
308
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600309 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800310 TLP_WRITE_TAG, byte_en);
311 headers[2] = TLP_CFG_DW2(bus, devfn, where);
312
313 /* check alignment to Qword */
314 if ((where & 0x7) == 0)
315 tlp_write_packet(pcie, headers, value, true);
316 else
317 tlp_write_packet(pcie, headers, value, false);
318
319 ret = tlp_read_packet(pcie, NULL);
320 if (ret != PCIBIOS_SUCCESSFUL)
321 return ret;
322
323 /*
324 * Monitor changes to PCI_PRIMARY_BUS register on root port
325 * and update local copy of root bus number accordingly.
326 */
327 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
328 pcie->root_bus_nr = (u8)(value);
329
330 return PCIBIOS_SUCCESSFUL;
331}
332
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800333static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
334 unsigned int devfn, int where, int size,
335 u32 *value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800336{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800337 int ret;
338 u32 data;
339 u8 byte_en;
340
Ley Foon Taneaa61112015-10-23 18:27:12 +0800341 switch (size) {
342 case 1:
343 byte_en = 1 << (where & 3);
344 break;
345 case 2:
346 byte_en = 3 << (where & 3);
347 break;
348 default:
349 byte_en = 0xf;
350 break;
351 }
352
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800353 ret = tlp_cfg_dword_read(pcie, busno, devfn,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800354 (where & ~DWORD_MASK), byte_en, &data);
355 if (ret != PCIBIOS_SUCCESSFUL)
356 return ret;
357
358 switch (size) {
359 case 1:
360 *value = (data >> (8 * (where & 0x3))) & 0xff;
361 break;
362 case 2:
363 *value = (data >> (8 * (where & 0x2))) & 0xffff;
364 break;
365 default:
366 *value = data;
367 break;
368 }
369
370 return PCIBIOS_SUCCESSFUL;
371}
372
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800373static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
374 unsigned int devfn, int where, int size,
375 u32 value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800376{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800377 u32 data32;
378 u32 shift = 8 * (where & 3);
379 u8 byte_en;
380
Ley Foon Taneaa61112015-10-23 18:27:12 +0800381 switch (size) {
382 case 1:
383 data32 = (value & 0xff) << shift;
384 byte_en = 1 << (where & 3);
385 break;
386 case 2:
387 data32 = (value & 0xffff) << shift;
388 byte_en = 3 << (where & 3);
389 break;
390 default:
391 data32 = value;
392 byte_en = 0xf;
393 break;
394 }
395
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800396 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
397 byte_en, data32);
398}
399
400static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
401 int where, int size, u32 *value)
402{
403 struct altera_pcie *pcie = bus->sysdata;
404
405 if (altera_pcie_hide_rc_bar(bus, devfn, where))
406 return PCIBIOS_BAD_REGISTER_NUMBER;
407
408 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn))) {
409 *value = 0xffffffff;
410 return PCIBIOS_DEVICE_NOT_FOUND;
411 }
412
413 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
414 value);
415}
416
417static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
418 int where, int size, u32 value)
419{
420 struct altera_pcie *pcie = bus->sysdata;
421
422 if (altera_pcie_hide_rc_bar(bus, devfn, where))
423 return PCIBIOS_BAD_REGISTER_NUMBER;
424
425 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn)))
426 return PCIBIOS_DEVICE_NOT_FOUND;
427
428 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
429 value);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800430}
431
432static struct pci_ops altera_pcie_ops = {
433 .read = altera_pcie_cfg_read,
434 .write = altera_pcie_cfg_write,
435};
436
437static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
438 irq_hw_number_t hwirq)
439{
440 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
441 irq_set_chip_data(irq, domain->host_data);
442
443 return 0;
444}
445
446static const struct irq_domain_ops intx_domain_ops = {
447 .map = altera_pcie_intx_map,
448};
449
450static void altera_pcie_isr(struct irq_desc *desc)
451{
452 struct irq_chip *chip = irq_desc_get_chip(desc);
453 struct altera_pcie *pcie;
454 unsigned long status;
455 u32 bit;
456 u32 virq;
457
458 chained_irq_enter(chip, desc);
459 pcie = irq_desc_get_handler_data(desc);
460
461 while ((status = cra_readl(pcie, P2A_INT_STATUS)
462 & P2A_INT_STS_ALL) != 0) {
463 for_each_set_bit(bit, &status, INTX_NUM) {
464 /* clear interrupts */
465 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
466
467 virq = irq_find_mapping(pcie->irq_domain, bit + 1);
468 if (virq)
469 generic_handle_irq(virq);
470 else
471 dev_err(&pcie->pdev->dev,
472 "unexpected IRQ, INT%d\n", bit);
473 }
474 }
475
476 chained_irq_exit(chip, desc);
477}
478
Ley Foon Taneaa61112015-10-23 18:27:12 +0800479static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
480{
481 int err, res_valid = 0;
482 struct device *dev = &pcie->pdev->dev;
483 struct device_node *np = dev->of_node;
484 struct resource_entry *win;
485
486 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
487 NULL);
488 if (err)
489 return err;
490
Bjorn Helgaas74462282016-05-31 12:14:17 -0500491 err = devm_request_pci_bus_resources(dev, &pcie->resources);
492 if (err)
493 goto out_release_res;
494
Ley Foon Taneaa61112015-10-23 18:27:12 +0800495 resource_list_for_each_entry(win, &pcie->resources) {
Bjorn Helgaas74462282016-05-31 12:14:17 -0500496 struct resource *res = win->res;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800497
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500498 if (resource_type(res) == IORESOURCE_MEM)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800499 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800500 }
501
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500502 if (res_valid)
503 return 0;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800504
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500505 dev_err(dev, "non-prefetchable memory resource required\n");
506 err = -EINVAL;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800507
508out_release_res:
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500509 pci_free_resource_list(&pcie->resources);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800510 return err;
511}
512
513static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
514{
515 struct device *dev = &pcie->pdev->dev;
516 struct device_node *node = dev->of_node;
517
518 /* Setup INTx */
Ley Foon Tan99496bd2015-12-04 16:21:21 -0600519 pcie->irq_domain = irq_domain_add_linear(node, INTX_NUM + 1,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800520 &intx_domain_ops, pcie);
521 if (!pcie->irq_domain) {
522 dev_err(dev, "Failed to get a INTx IRQ domain\n");
523 return -ENOMEM;
524 }
525
526 return 0;
527}
528
529static int altera_pcie_parse_dt(struct altera_pcie *pcie)
530{
531 struct resource *cra;
532 struct platform_device *pdev = pcie->pdev;
533
534 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
535 if (!cra) {
536 dev_err(&pdev->dev, "no Cra memory resource defined\n");
537 return -ENODEV;
538 }
539
540 pcie->cra_base = devm_ioremap_resource(&pdev->dev, cra);
541 if (IS_ERR(pcie->cra_base)) {
542 dev_err(&pdev->dev, "failed to map cra memory\n");
543 return PTR_ERR(pcie->cra_base);
544 }
545
546 /* setup IRQ */
547 pcie->irq = platform_get_irq(pdev, 0);
548 if (pcie->irq <= 0) {
549 dev_err(&pdev->dev, "failed to get IRQ: %d\n", pcie->irq);
550 return -EINVAL;
551 }
552
553 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
554
555 return 0;
556}
557
558static int altera_pcie_probe(struct platform_device *pdev)
559{
560 struct altera_pcie *pcie;
561 struct pci_bus *bus;
562 struct pci_bus *child;
563 int ret;
564
565 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
566 if (!pcie)
567 return -ENOMEM;
568
569 pcie->pdev = pdev;
570
571 ret = altera_pcie_parse_dt(pcie);
572 if (ret) {
573 dev_err(&pdev->dev, "Parsing DT failed\n");
574 return ret;
575 }
576
577 INIT_LIST_HEAD(&pcie->resources);
578
579 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
580 if (ret) {
581 dev_err(&pdev->dev, "Failed add resources\n");
582 return ret;
583 }
584
585 ret = altera_pcie_init_irq_domain(pcie);
586 if (ret) {
587 dev_err(&pdev->dev, "Failed creating IRQ Domain\n");
588 return ret;
589 }
590
591 /* clear all interrupts */
592 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
593 /* enable all interrupts */
594 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
595
596 bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops,
597 pcie, &pcie->resources);
598 if (!bus)
599 return -ENOMEM;
600
601 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
602 pci_assign_unassigned_bus_resources(bus);
603
604 /* Configure PCI Express setting. */
605 list_for_each_entry(child, &bus->children, node)
606 pcie_bus_configure_settings(child);
607
608 pci_bus_add_devices(bus);
609
610 platform_set_drvdata(pdev, pcie);
611 return ret;
612}
613
614static const struct of_device_id altera_pcie_of_match[] = {
615 { .compatible = "altr,pcie-root-port-1.0", },
616 {},
617};
618MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
619
620static struct platform_driver altera_pcie_driver = {
621 .probe = altera_pcie_probe,
622 .driver = {
623 .name = "altera-pcie",
624 .of_match_table = altera_pcie_of_match,
625 .suppress_bind_attrs = true,
626 },
627};
628
629static int altera_pcie_init(void)
630{
631 return platform_driver_register(&altera_pcie_driver);
632}
633module_init(altera_pcie_init);
634
635MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
636MODULE_DESCRIPTION("Altera PCIe host controller driver");
637MODULE_LICENSE("GPL v2");