blob: 4ca50a28af82c5b4da15d4170f3627dd1b1b7685 [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
Ley Foon Tance4f1c72016-08-26 09:47:25 +080046#define PCIE_CAP_OFFSET 0x80
Ley Foon Taneaa61112015-10-23 18:27:12 +080047/* TLP configuration type 0 and 1 */
48#define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
49#define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
50#define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
51#define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
52#define TLP_PAYLOAD_SIZE 0x01
53#define TLP_READ_TAG 0x1d
54#define TLP_WRITE_TAG 0x10
55#define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
56#define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
57#define TLP_CFG_DW2(bus, devfn, offset) \
58 (((bus) << 24) | ((devfn) << 16) | (offset))
59#define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
Ley Foon Tanea1d3792015-12-04 16:21:16 -060060#define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
Ley Foon Taneaa61112015-10-23 18:27:12 +080061#define TLP_HDR_SIZE 3
62#define TLP_LOOP 500
Ley Foon Tan23ec5672015-12-04 16:21:12 -060063#define RP_DEVFN 0
Ley Foon Taneaa61112015-10-23 18:27:12 +080064
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 INTX_NUM 4
69
70#define DWORD_MASK 3
71
72struct altera_pcie {
73 struct platform_device *pdev;
74 void __iomem *cra_base;
75 int irq;
76 u8 root_bus_nr;
77 struct irq_domain *irq_domain;
78 struct resource bus_range;
79 struct list_head resources;
80};
81
82struct tlp_rp_regpair_t {
83 u32 ctrl;
84 u32 reg0;
85 u32 reg1;
86};
87
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050088static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
89 const u32 reg)
90{
91 writel_relaxed(value, pcie->cra_base + reg);
92}
93
94static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
95{
96 return readl_relaxed(pcie->cra_base + reg);
97}
98
99static bool altera_pcie_link_is_up(struct altera_pcie *pcie)
100{
101 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
102}
103
Ley Foon Taneaa61112015-10-23 18:27:12 +0800104/*
105 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
106 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
107 * using these registers, so it can be reached by DMA from EP devices.
108 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
109 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
110 * should be hidden during enumeration to avoid the sizing and resource
111 * allocation by PCIe core.
112 */
113static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
114 int offset)
115{
116 if (pci_is_root_bus(bus) && (devfn == 0) &&
117 (offset == PCI_BASE_ADDRESS_0))
118 return true;
119
120 return false;
121}
122
Ley Foon Taneaa61112015-10-23 18:27:12 +0800123static void tlp_write_tx(struct altera_pcie *pcie,
124 struct tlp_rp_regpair_t *tlp_rp_regdata)
125{
126 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
127 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
128 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
129}
130
Ley Foon Taneaa61112015-10-23 18:27:12 +0800131static bool altera_pcie_valid_config(struct altera_pcie *pcie,
132 struct pci_bus *bus, int dev)
133{
134 /* If there is no link, then there is no device */
135 if (bus->number != pcie->root_bus_nr) {
136 if (!altera_pcie_link_is_up(pcie))
137 return false;
138 }
139
140 /* access only one slot on each root port */
141 if (bus->number == pcie->root_bus_nr && dev > 0)
142 return false;
143
144 /*
145 * Do not read more than one device on the bus directly attached
146 * to root port, root port can only attach to one downstream port.
147 */
148 if (bus->primary == pcie->root_bus_nr && dev > 0)
149 return false;
150
151 return true;
152}
153
154static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
155{
Dan Carpenter7f52f312015-12-04 16:21:08 -0600156 int i;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800157 bool sop = 0;
158 u32 ctrl;
159 u32 reg0, reg1;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600160 u32 comp_status = 1;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800161
162 /*
163 * Minimum 2 loops to read TLP headers and 1 loop to read data
164 * payload.
165 */
Dan Carpenter7f52f312015-12-04 16:21:08 -0600166 for (i = 0; i < TLP_LOOP; i++) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800167 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
168 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
169 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
170 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
171
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600172 if (ctrl & RP_RXCPL_SOP) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800173 sop = true;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600174 comp_status = TLP_COMP_STATUS(reg1);
175 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800176
177 if (ctrl & RP_RXCPL_EOP) {
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600178 if (comp_status)
179 return PCIBIOS_DEVICE_NOT_FOUND;
180
Ley Foon Taneaa61112015-10-23 18:27:12 +0800181 if (value)
182 *value = reg0;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600183
Ley Foon Taneaa61112015-10-23 18:27:12 +0800184 return PCIBIOS_SUCCESSFUL;
185 }
186 }
187 udelay(5);
188 }
189
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600190 return PCIBIOS_DEVICE_NOT_FOUND;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800191}
192
193static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
194 u32 data, bool align)
195{
196 struct tlp_rp_regpair_t tlp_rp_regdata;
197
198 tlp_rp_regdata.reg0 = headers[0];
199 tlp_rp_regdata.reg1 = headers[1];
200 tlp_rp_regdata.ctrl = RP_TX_SOP;
201 tlp_write_tx(pcie, &tlp_rp_regdata);
202
203 if (align) {
204 tlp_rp_regdata.reg0 = headers[2];
205 tlp_rp_regdata.reg1 = 0;
206 tlp_rp_regdata.ctrl = 0;
207 tlp_write_tx(pcie, &tlp_rp_regdata);
208
209 tlp_rp_regdata.reg0 = data;
210 tlp_rp_regdata.reg1 = 0;
211 } else {
212 tlp_rp_regdata.reg0 = headers[2];
213 tlp_rp_regdata.reg1 = data;
214 }
215
216 tlp_rp_regdata.ctrl = RP_TX_EOP;
217 tlp_write_tx(pcie, &tlp_rp_regdata);
218}
219
220static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
221 int where, u8 byte_en, u32 *value)
222{
223 u32 headers[TLP_HDR_SIZE];
224
225 if (bus == pcie->root_bus_nr)
226 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0);
227 else
228 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1);
229
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600230 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800231 TLP_READ_TAG, byte_en);
232 headers[2] = TLP_CFG_DW2(bus, devfn, where);
233
234 tlp_write_packet(pcie, headers, 0, false);
235
236 return tlp_read_packet(pcie, value);
237}
238
239static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
240 int where, u8 byte_en, u32 value)
241{
242 u32 headers[TLP_HDR_SIZE];
243 int ret;
244
245 if (bus == pcie->root_bus_nr)
246 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0);
247 else
248 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1);
249
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600250 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800251 TLP_WRITE_TAG, byte_en);
252 headers[2] = TLP_CFG_DW2(bus, devfn, where);
253
254 /* check alignment to Qword */
255 if ((where & 0x7) == 0)
256 tlp_write_packet(pcie, headers, value, true);
257 else
258 tlp_write_packet(pcie, headers, value, false);
259
260 ret = tlp_read_packet(pcie, NULL);
261 if (ret != PCIBIOS_SUCCESSFUL)
262 return ret;
263
264 /*
265 * Monitor changes to PCI_PRIMARY_BUS register on root port
266 * and update local copy of root bus number accordingly.
267 */
268 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
269 pcie->root_bus_nr = (u8)(value);
270
271 return PCIBIOS_SUCCESSFUL;
272}
273
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800274static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
275 unsigned int devfn, int where, int size,
276 u32 *value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800277{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800278 int ret;
279 u32 data;
280 u8 byte_en;
281
Ley Foon Taneaa61112015-10-23 18:27:12 +0800282 switch (size) {
283 case 1:
284 byte_en = 1 << (where & 3);
285 break;
286 case 2:
287 byte_en = 3 << (where & 3);
288 break;
289 default:
290 byte_en = 0xf;
291 break;
292 }
293
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800294 ret = tlp_cfg_dword_read(pcie, busno, devfn,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800295 (where & ~DWORD_MASK), byte_en, &data);
296 if (ret != PCIBIOS_SUCCESSFUL)
297 return ret;
298
299 switch (size) {
300 case 1:
301 *value = (data >> (8 * (where & 0x3))) & 0xff;
302 break;
303 case 2:
304 *value = (data >> (8 * (where & 0x2))) & 0xffff;
305 break;
306 default:
307 *value = data;
308 break;
309 }
310
311 return PCIBIOS_SUCCESSFUL;
312}
313
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800314static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
315 unsigned int devfn, int where, int size,
316 u32 value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800317{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800318 u32 data32;
319 u32 shift = 8 * (where & 3);
320 u8 byte_en;
321
Ley Foon Taneaa61112015-10-23 18:27:12 +0800322 switch (size) {
323 case 1:
324 data32 = (value & 0xff) << shift;
325 byte_en = 1 << (where & 3);
326 break;
327 case 2:
328 data32 = (value & 0xffff) << shift;
329 byte_en = 3 << (where & 3);
330 break;
331 default:
332 data32 = value;
333 byte_en = 0xf;
334 break;
335 }
336
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800337 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
338 byte_en, data32);
339}
340
341static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
342 int where, int size, u32 *value)
343{
344 struct altera_pcie *pcie = bus->sysdata;
345
346 if (altera_pcie_hide_rc_bar(bus, devfn, where))
347 return PCIBIOS_BAD_REGISTER_NUMBER;
348
349 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn))) {
350 *value = 0xffffffff;
351 return PCIBIOS_DEVICE_NOT_FOUND;
352 }
353
354 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
355 value);
356}
357
358static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
359 int where, int size, u32 value)
360{
361 struct altera_pcie *pcie = bus->sysdata;
362
363 if (altera_pcie_hide_rc_bar(bus, devfn, where))
364 return PCIBIOS_BAD_REGISTER_NUMBER;
365
366 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn)))
367 return PCIBIOS_DEVICE_NOT_FOUND;
368
369 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
370 value);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800371}
372
373static struct pci_ops altera_pcie_ops = {
374 .read = altera_pcie_cfg_read,
375 .write = altera_pcie_cfg_write,
376};
377
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800378static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
379 unsigned int devfn, int offset, u16 *value)
380{
381 u32 data;
382 int ret;
383
384 ret = _altera_pcie_cfg_read(pcie, busno, devfn,
385 PCIE_CAP_OFFSET + offset, sizeof(*value),
386 &data);
387 *value = data;
388 return ret;
389}
390
391static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
392 unsigned int devfn, int offset, u16 value)
393{
394 return _altera_pcie_cfg_write(pcie, busno, devfn,
395 PCIE_CAP_OFFSET + offset, sizeof(value),
396 value);
397}
398
399static void altera_wait_link_retrain(struct altera_pcie *pcie)
400{
401 u16 reg16;
402 unsigned long start_jiffies;
403
404 /* Wait for link training end. */
405 start_jiffies = jiffies;
406 for (;;) {
407 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
408 PCI_EXP_LNKSTA, &reg16);
409 if (!(reg16 & PCI_EXP_LNKSTA_LT))
410 break;
411
412 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
413 dev_err(&pcie->pdev->dev, "link retrain timeout\n");
414 break;
415 }
416 udelay(100);
417 }
418
419 /* Wait for link is up */
420 start_jiffies = jiffies;
421 for (;;) {
422 if (altera_pcie_link_is_up(pcie))
423 break;
424
425 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
426 dev_err(&pcie->pdev->dev, "link up timeout\n");
427 break;
428 }
429 udelay(100);
430 }
431}
432
433static void altera_pcie_retrain(struct altera_pcie *pcie)
434{
435 u16 linkcap, linkstat, linkctl;
436
437 if (!altera_pcie_link_is_up(pcie))
438 return;
439
440 /*
441 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
442 * current speed is 2.5 GB/s.
443 */
444 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
445 &linkcap);
446 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
447 return;
448
449 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
450 &linkstat);
451 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
452 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
453 PCI_EXP_LNKCTL, &linkctl);
454 linkctl |= PCI_EXP_LNKCTL_RL;
455 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
456 PCI_EXP_LNKCTL, linkctl);
457
458 altera_wait_link_retrain(pcie);
459 }
460}
461
Ley Foon Taneaa61112015-10-23 18:27:12 +0800462static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
463 irq_hw_number_t hwirq)
464{
465 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
466 irq_set_chip_data(irq, domain->host_data);
467
468 return 0;
469}
470
471static const struct irq_domain_ops intx_domain_ops = {
472 .map = altera_pcie_intx_map,
473};
474
475static void altera_pcie_isr(struct irq_desc *desc)
476{
477 struct irq_chip *chip = irq_desc_get_chip(desc);
478 struct altera_pcie *pcie;
479 unsigned long status;
480 u32 bit;
481 u32 virq;
482
483 chained_irq_enter(chip, desc);
484 pcie = irq_desc_get_handler_data(desc);
485
486 while ((status = cra_readl(pcie, P2A_INT_STATUS)
487 & P2A_INT_STS_ALL) != 0) {
488 for_each_set_bit(bit, &status, INTX_NUM) {
489 /* clear interrupts */
490 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
491
492 virq = irq_find_mapping(pcie->irq_domain, bit + 1);
493 if (virq)
494 generic_handle_irq(virq);
495 else
496 dev_err(&pcie->pdev->dev,
497 "unexpected IRQ, INT%d\n", bit);
498 }
499 }
500
501 chained_irq_exit(chip, desc);
502}
503
Ley Foon Taneaa61112015-10-23 18:27:12 +0800504static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
505{
506 int err, res_valid = 0;
507 struct device *dev = &pcie->pdev->dev;
508 struct device_node *np = dev->of_node;
509 struct resource_entry *win;
510
511 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
512 NULL);
513 if (err)
514 return err;
515
Bjorn Helgaas74462282016-05-31 12:14:17 -0500516 err = devm_request_pci_bus_resources(dev, &pcie->resources);
517 if (err)
518 goto out_release_res;
519
Ley Foon Taneaa61112015-10-23 18:27:12 +0800520 resource_list_for_each_entry(win, &pcie->resources) {
Bjorn Helgaas74462282016-05-31 12:14:17 -0500521 struct resource *res = win->res;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800522
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500523 if (resource_type(res) == IORESOURCE_MEM)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800524 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800525 }
526
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500527 if (res_valid)
528 return 0;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800529
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500530 dev_err(dev, "non-prefetchable memory resource required\n");
531 err = -EINVAL;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800532
533out_release_res:
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500534 pci_free_resource_list(&pcie->resources);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800535 return err;
536}
537
538static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
539{
540 struct device *dev = &pcie->pdev->dev;
541 struct device_node *node = dev->of_node;
542
543 /* Setup INTx */
Ley Foon Tan99496bd2015-12-04 16:21:21 -0600544 pcie->irq_domain = irq_domain_add_linear(node, INTX_NUM + 1,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800545 &intx_domain_ops, pcie);
546 if (!pcie->irq_domain) {
547 dev_err(dev, "Failed to get a INTx IRQ domain\n");
548 return -ENOMEM;
549 }
550
551 return 0;
552}
553
554static int altera_pcie_parse_dt(struct altera_pcie *pcie)
555{
556 struct resource *cra;
557 struct platform_device *pdev = pcie->pdev;
558
559 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
560 if (!cra) {
561 dev_err(&pdev->dev, "no Cra memory resource defined\n");
562 return -ENODEV;
563 }
564
565 pcie->cra_base = devm_ioremap_resource(&pdev->dev, cra);
566 if (IS_ERR(pcie->cra_base)) {
567 dev_err(&pdev->dev, "failed to map cra memory\n");
568 return PTR_ERR(pcie->cra_base);
569 }
570
571 /* setup IRQ */
572 pcie->irq = platform_get_irq(pdev, 0);
573 if (pcie->irq <= 0) {
574 dev_err(&pdev->dev, "failed to get IRQ: %d\n", pcie->irq);
575 return -EINVAL;
576 }
577
578 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
579
580 return 0;
581}
582
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800583static void altera_pcie_host_init(struct altera_pcie *pcie)
584{
585 altera_pcie_retrain(pcie);
586}
587
Ley Foon Taneaa61112015-10-23 18:27:12 +0800588static int altera_pcie_probe(struct platform_device *pdev)
589{
590 struct altera_pcie *pcie;
591 struct pci_bus *bus;
592 struct pci_bus *child;
593 int ret;
594
595 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
596 if (!pcie)
597 return -ENOMEM;
598
599 pcie->pdev = pdev;
600
601 ret = altera_pcie_parse_dt(pcie);
602 if (ret) {
603 dev_err(&pdev->dev, "Parsing DT failed\n");
604 return ret;
605 }
606
607 INIT_LIST_HEAD(&pcie->resources);
608
609 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
610 if (ret) {
611 dev_err(&pdev->dev, "Failed add resources\n");
612 return ret;
613 }
614
615 ret = altera_pcie_init_irq_domain(pcie);
616 if (ret) {
617 dev_err(&pdev->dev, "Failed creating IRQ Domain\n");
618 return ret;
619 }
620
621 /* clear all interrupts */
622 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
623 /* enable all interrupts */
624 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800625 altera_pcie_host_init(pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800626
627 bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops,
628 pcie, &pcie->resources);
629 if (!bus)
630 return -ENOMEM;
631
632 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
633 pci_assign_unassigned_bus_resources(bus);
634
635 /* Configure PCI Express setting. */
636 list_for_each_entry(child, &bus->children, node)
637 pcie_bus_configure_settings(child);
638
639 pci_bus_add_devices(bus);
640
641 platform_set_drvdata(pdev, pcie);
642 return ret;
643}
644
645static const struct of_device_id altera_pcie_of_match[] = {
646 { .compatible = "altr,pcie-root-port-1.0", },
647 {},
648};
649MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
650
651static struct platform_driver altera_pcie_driver = {
652 .probe = altera_pcie_probe,
653 .driver = {
654 .name = "altera-pcie",
655 .of_match_table = altera_pcie_of_match,
656 .suppress_bind_attrs = true,
657 },
658};
659
660static int altera_pcie_init(void)
661{
662 return platform_driver_register(&altera_pcie_driver);
663}
664module_init(altera_pcie_init);
665
666MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
667MODULE_DESCRIPTION("Altera PCIe host controller driver");
668MODULE_LICENSE("GPL v2");