pcie-altera.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  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. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irqchip/chained_irq.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_pci.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #define RP_TX_REG0 0x2000
  27. #define RP_TX_REG1 0x2004
  28. #define RP_TX_CNTRL 0x2008
  29. #define RP_TX_EOP 0x2
  30. #define RP_TX_SOP 0x1
  31. #define RP_RXCPL_STATUS 0x2010
  32. #define RP_RXCPL_EOP 0x2
  33. #define RP_RXCPL_SOP 0x1
  34. #define RP_RXCPL_REG0 0x2014
  35. #define RP_RXCPL_REG1 0x2018
  36. #define P2A_INT_STATUS 0x3060
  37. #define P2A_INT_STS_ALL 0xf
  38. #define P2A_INT_ENABLE 0x3070
  39. #define P2A_INT_ENA_ALL 0xf
  40. #define RP_LTSSM 0x3c64
  41. #define RP_LTSSM_MASK 0x1f
  42. #define LTSSM_L0 0xf
  43. #define PCIE_CAP_OFFSET 0x80
  44. /* TLP configuration type 0 and 1 */
  45. #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
  46. #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
  47. #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
  48. #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
  49. #define TLP_PAYLOAD_SIZE 0x01
  50. #define TLP_READ_TAG 0x1d
  51. #define TLP_WRITE_TAG 0x10
  52. #define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
  53. #define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
  54. #define TLP_CFG_DW2(bus, devfn, offset) \
  55. (((bus) << 24) | ((devfn) << 16) | (offset))
  56. #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
  57. #define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
  58. #define TLP_HDR_SIZE 3
  59. #define TLP_LOOP 500
  60. #define RP_DEVFN 0
  61. #define LINK_UP_TIMEOUT HZ
  62. #define LINK_RETRAIN_TIMEOUT HZ
  63. #define INTX_NUM 4
  64. #define DWORD_MASK 3
  65. struct altera_pcie {
  66. struct platform_device *pdev;
  67. void __iomem *cra_base;
  68. int irq;
  69. u8 root_bus_nr;
  70. struct irq_domain *irq_domain;
  71. struct resource bus_range;
  72. struct list_head resources;
  73. };
  74. struct tlp_rp_regpair_t {
  75. u32 ctrl;
  76. u32 reg0;
  77. u32 reg1;
  78. };
  79. static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
  80. const u32 reg)
  81. {
  82. writel_relaxed(value, pcie->cra_base + reg);
  83. }
  84. static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
  85. {
  86. return readl_relaxed(pcie->cra_base + reg);
  87. }
  88. static bool altera_pcie_link_is_up(struct altera_pcie *pcie)
  89. {
  90. return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
  91. }
  92. /*
  93. * Altera PCIe port uses BAR0 of RC's configuration space as the translation
  94. * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
  95. * using these registers, so it can be reached by DMA from EP devices.
  96. * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
  97. * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
  98. * should be hidden during enumeration to avoid the sizing and resource
  99. * allocation by PCIe core.
  100. */
  101. static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
  102. int offset)
  103. {
  104. if (pci_is_root_bus(bus) && (devfn == 0) &&
  105. (offset == PCI_BASE_ADDRESS_0))
  106. return true;
  107. return false;
  108. }
  109. static void tlp_write_tx(struct altera_pcie *pcie,
  110. struct tlp_rp_regpair_t *tlp_rp_regdata)
  111. {
  112. cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
  113. cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
  114. cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
  115. }
  116. static bool altera_pcie_valid_config(struct altera_pcie *pcie,
  117. struct pci_bus *bus, int dev)
  118. {
  119. /* If there is no link, then there is no device */
  120. if (bus->number != pcie->root_bus_nr) {
  121. if (!altera_pcie_link_is_up(pcie))
  122. return false;
  123. }
  124. /* access only one slot on each root port */
  125. if (bus->number == pcie->root_bus_nr && dev > 0)
  126. return false;
  127. /*
  128. * Do not read more than one device on the bus directly attached
  129. * to root port, root port can only attach to one downstream port.
  130. */
  131. if (bus->primary == pcie->root_bus_nr && dev > 0)
  132. return false;
  133. return true;
  134. }
  135. static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
  136. {
  137. int i;
  138. bool sop = 0;
  139. u32 ctrl;
  140. u32 reg0, reg1;
  141. u32 comp_status = 1;
  142. /*
  143. * Minimum 2 loops to read TLP headers and 1 loop to read data
  144. * payload.
  145. */
  146. for (i = 0; i < TLP_LOOP; i++) {
  147. ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
  148. if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
  149. reg0 = cra_readl(pcie, RP_RXCPL_REG0);
  150. reg1 = cra_readl(pcie, RP_RXCPL_REG1);
  151. if (ctrl & RP_RXCPL_SOP) {
  152. sop = true;
  153. comp_status = TLP_COMP_STATUS(reg1);
  154. }
  155. if (ctrl & RP_RXCPL_EOP) {
  156. if (comp_status)
  157. return PCIBIOS_DEVICE_NOT_FOUND;
  158. if (value)
  159. *value = reg0;
  160. return PCIBIOS_SUCCESSFUL;
  161. }
  162. }
  163. udelay(5);
  164. }
  165. return PCIBIOS_DEVICE_NOT_FOUND;
  166. }
  167. static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
  168. u32 data, bool align)
  169. {
  170. struct tlp_rp_regpair_t tlp_rp_regdata;
  171. tlp_rp_regdata.reg0 = headers[0];
  172. tlp_rp_regdata.reg1 = headers[1];
  173. tlp_rp_regdata.ctrl = RP_TX_SOP;
  174. tlp_write_tx(pcie, &tlp_rp_regdata);
  175. if (align) {
  176. tlp_rp_regdata.reg0 = headers[2];
  177. tlp_rp_regdata.reg1 = 0;
  178. tlp_rp_regdata.ctrl = 0;
  179. tlp_write_tx(pcie, &tlp_rp_regdata);
  180. tlp_rp_regdata.reg0 = data;
  181. tlp_rp_regdata.reg1 = 0;
  182. } else {
  183. tlp_rp_regdata.reg0 = headers[2];
  184. tlp_rp_regdata.reg1 = data;
  185. }
  186. tlp_rp_regdata.ctrl = RP_TX_EOP;
  187. tlp_write_tx(pcie, &tlp_rp_regdata);
  188. }
  189. static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
  190. int where, u8 byte_en, u32 *value)
  191. {
  192. u32 headers[TLP_HDR_SIZE];
  193. if (bus == pcie->root_bus_nr)
  194. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0);
  195. else
  196. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1);
  197. headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
  198. TLP_READ_TAG, byte_en);
  199. headers[2] = TLP_CFG_DW2(bus, devfn, where);
  200. tlp_write_packet(pcie, headers, 0, false);
  201. return tlp_read_packet(pcie, value);
  202. }
  203. static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
  204. int where, u8 byte_en, u32 value)
  205. {
  206. u32 headers[TLP_HDR_SIZE];
  207. int ret;
  208. if (bus == pcie->root_bus_nr)
  209. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0);
  210. else
  211. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1);
  212. headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
  213. TLP_WRITE_TAG, byte_en);
  214. headers[2] = TLP_CFG_DW2(bus, devfn, where);
  215. /* check alignment to Qword */
  216. if ((where & 0x7) == 0)
  217. tlp_write_packet(pcie, headers, value, true);
  218. else
  219. tlp_write_packet(pcie, headers, value, false);
  220. ret = tlp_read_packet(pcie, NULL);
  221. if (ret != PCIBIOS_SUCCESSFUL)
  222. return ret;
  223. /*
  224. * Monitor changes to PCI_PRIMARY_BUS register on root port
  225. * and update local copy of root bus number accordingly.
  226. */
  227. if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
  228. pcie->root_bus_nr = (u8)(value);
  229. return PCIBIOS_SUCCESSFUL;
  230. }
  231. static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
  232. unsigned int devfn, int where, int size,
  233. u32 *value)
  234. {
  235. int ret;
  236. u32 data;
  237. u8 byte_en;
  238. switch (size) {
  239. case 1:
  240. byte_en = 1 << (where & 3);
  241. break;
  242. case 2:
  243. byte_en = 3 << (where & 3);
  244. break;
  245. default:
  246. byte_en = 0xf;
  247. break;
  248. }
  249. ret = tlp_cfg_dword_read(pcie, busno, devfn,
  250. (where & ~DWORD_MASK), byte_en, &data);
  251. if (ret != PCIBIOS_SUCCESSFUL)
  252. return ret;
  253. switch (size) {
  254. case 1:
  255. *value = (data >> (8 * (where & 0x3))) & 0xff;
  256. break;
  257. case 2:
  258. *value = (data >> (8 * (where & 0x2))) & 0xffff;
  259. break;
  260. default:
  261. *value = data;
  262. break;
  263. }
  264. return PCIBIOS_SUCCESSFUL;
  265. }
  266. static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
  267. unsigned int devfn, int where, int size,
  268. u32 value)
  269. {
  270. u32 data32;
  271. u32 shift = 8 * (where & 3);
  272. u8 byte_en;
  273. switch (size) {
  274. case 1:
  275. data32 = (value & 0xff) << shift;
  276. byte_en = 1 << (where & 3);
  277. break;
  278. case 2:
  279. data32 = (value & 0xffff) << shift;
  280. byte_en = 3 << (where & 3);
  281. break;
  282. default:
  283. data32 = value;
  284. byte_en = 0xf;
  285. break;
  286. }
  287. return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
  288. byte_en, data32);
  289. }
  290. static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
  291. int where, int size, u32 *value)
  292. {
  293. struct altera_pcie *pcie = bus->sysdata;
  294. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  295. return PCIBIOS_BAD_REGISTER_NUMBER;
  296. if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn))) {
  297. *value = 0xffffffff;
  298. return PCIBIOS_DEVICE_NOT_FOUND;
  299. }
  300. return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
  301. value);
  302. }
  303. static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
  304. int where, int size, u32 value)
  305. {
  306. struct altera_pcie *pcie = bus->sysdata;
  307. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  308. return PCIBIOS_BAD_REGISTER_NUMBER;
  309. if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn)))
  310. return PCIBIOS_DEVICE_NOT_FOUND;
  311. return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
  312. value);
  313. }
  314. static struct pci_ops altera_pcie_ops = {
  315. .read = altera_pcie_cfg_read,
  316. .write = altera_pcie_cfg_write,
  317. };
  318. static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
  319. unsigned int devfn, int offset, u16 *value)
  320. {
  321. u32 data;
  322. int ret;
  323. ret = _altera_pcie_cfg_read(pcie, busno, devfn,
  324. PCIE_CAP_OFFSET + offset, sizeof(*value),
  325. &data);
  326. *value = data;
  327. return ret;
  328. }
  329. static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
  330. unsigned int devfn, int offset, u16 value)
  331. {
  332. return _altera_pcie_cfg_write(pcie, busno, devfn,
  333. PCIE_CAP_OFFSET + offset, sizeof(value),
  334. value);
  335. }
  336. static void altera_wait_link_retrain(struct altera_pcie *pcie)
  337. {
  338. u16 reg16;
  339. unsigned long start_jiffies;
  340. /* Wait for link training end. */
  341. start_jiffies = jiffies;
  342. for (;;) {
  343. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  344. PCI_EXP_LNKSTA, &reg16);
  345. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  346. break;
  347. if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
  348. dev_err(&pcie->pdev->dev, "link retrain timeout\n");
  349. break;
  350. }
  351. udelay(100);
  352. }
  353. /* Wait for link is up */
  354. start_jiffies = jiffies;
  355. for (;;) {
  356. if (altera_pcie_link_is_up(pcie))
  357. break;
  358. if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
  359. dev_err(&pcie->pdev->dev, "link up timeout\n");
  360. break;
  361. }
  362. udelay(100);
  363. }
  364. }
  365. static void altera_pcie_retrain(struct altera_pcie *pcie)
  366. {
  367. u16 linkcap, linkstat, linkctl;
  368. if (!altera_pcie_link_is_up(pcie))
  369. return;
  370. /*
  371. * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
  372. * current speed is 2.5 GB/s.
  373. */
  374. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
  375. &linkcap);
  376. if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
  377. return;
  378. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
  379. &linkstat);
  380. if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
  381. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  382. PCI_EXP_LNKCTL, &linkctl);
  383. linkctl |= PCI_EXP_LNKCTL_RL;
  384. altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  385. PCI_EXP_LNKCTL, linkctl);
  386. altera_wait_link_retrain(pcie);
  387. }
  388. }
  389. static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  390. irq_hw_number_t hwirq)
  391. {
  392. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  393. irq_set_chip_data(irq, domain->host_data);
  394. return 0;
  395. }
  396. static const struct irq_domain_ops intx_domain_ops = {
  397. .map = altera_pcie_intx_map,
  398. };
  399. static void altera_pcie_isr(struct irq_desc *desc)
  400. {
  401. struct irq_chip *chip = irq_desc_get_chip(desc);
  402. struct altera_pcie *pcie;
  403. unsigned long status;
  404. u32 bit;
  405. u32 virq;
  406. chained_irq_enter(chip, desc);
  407. pcie = irq_desc_get_handler_data(desc);
  408. while ((status = cra_readl(pcie, P2A_INT_STATUS)
  409. & P2A_INT_STS_ALL) != 0) {
  410. for_each_set_bit(bit, &status, INTX_NUM) {
  411. /* clear interrupts */
  412. cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
  413. virq = irq_find_mapping(pcie->irq_domain, bit + 1);
  414. if (virq)
  415. generic_handle_irq(virq);
  416. else
  417. dev_err(&pcie->pdev->dev,
  418. "unexpected IRQ, INT%d\n", bit);
  419. }
  420. }
  421. chained_irq_exit(chip, desc);
  422. }
  423. static void altera_pcie_release_of_pci_ranges(struct altera_pcie *pcie)
  424. {
  425. pci_free_resource_list(&pcie->resources);
  426. }
  427. static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
  428. {
  429. int err, res_valid = 0;
  430. struct device *dev = &pcie->pdev->dev;
  431. struct device_node *np = dev->of_node;
  432. struct resource_entry *win;
  433. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
  434. NULL);
  435. if (err)
  436. return err;
  437. resource_list_for_each_entry(win, &pcie->resources) {
  438. struct resource *parent, *res = win->res;
  439. switch (resource_type(res)) {
  440. case IORESOURCE_MEM:
  441. parent = &iomem_resource;
  442. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  443. break;
  444. default:
  445. continue;
  446. }
  447. err = devm_request_resource(dev, parent, res);
  448. if (err)
  449. goto out_release_res;
  450. }
  451. if (!res_valid) {
  452. dev_err(dev, "non-prefetchable memory resource required\n");
  453. err = -EINVAL;
  454. goto out_release_res;
  455. }
  456. return 0;
  457. out_release_res:
  458. altera_pcie_release_of_pci_ranges(pcie);
  459. return err;
  460. }
  461. static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
  462. {
  463. struct device *dev = &pcie->pdev->dev;
  464. struct device_node *node = dev->of_node;
  465. /* Setup INTx */
  466. pcie->irq_domain = irq_domain_add_linear(node, INTX_NUM + 1,
  467. &intx_domain_ops, pcie);
  468. if (!pcie->irq_domain) {
  469. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  470. return -ENOMEM;
  471. }
  472. return 0;
  473. }
  474. static int altera_pcie_parse_dt(struct altera_pcie *pcie)
  475. {
  476. struct resource *cra;
  477. struct platform_device *pdev = pcie->pdev;
  478. cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
  479. if (!cra) {
  480. dev_err(&pdev->dev, "no Cra memory resource defined\n");
  481. return -ENODEV;
  482. }
  483. pcie->cra_base = devm_ioremap_resource(&pdev->dev, cra);
  484. if (IS_ERR(pcie->cra_base)) {
  485. dev_err(&pdev->dev, "failed to map cra memory\n");
  486. return PTR_ERR(pcie->cra_base);
  487. }
  488. /* setup IRQ */
  489. pcie->irq = platform_get_irq(pdev, 0);
  490. if (pcie->irq <= 0) {
  491. dev_err(&pdev->dev, "failed to get IRQ: %d\n", pcie->irq);
  492. return -EINVAL;
  493. }
  494. irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
  495. return 0;
  496. }
  497. static void altera_pcie_host_init(struct altera_pcie *pcie)
  498. {
  499. altera_pcie_retrain(pcie);
  500. }
  501. static int altera_pcie_probe(struct platform_device *pdev)
  502. {
  503. struct altera_pcie *pcie;
  504. struct pci_bus *bus;
  505. struct pci_bus *child;
  506. int ret;
  507. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  508. if (!pcie)
  509. return -ENOMEM;
  510. pcie->pdev = pdev;
  511. ret = altera_pcie_parse_dt(pcie);
  512. if (ret) {
  513. dev_err(&pdev->dev, "Parsing DT failed\n");
  514. return ret;
  515. }
  516. INIT_LIST_HEAD(&pcie->resources);
  517. ret = altera_pcie_parse_request_of_pci_ranges(pcie);
  518. if (ret) {
  519. dev_err(&pdev->dev, "Failed add resources\n");
  520. return ret;
  521. }
  522. ret = altera_pcie_init_irq_domain(pcie);
  523. if (ret) {
  524. dev_err(&pdev->dev, "Failed creating IRQ Domain\n");
  525. return ret;
  526. }
  527. /* clear all interrupts */
  528. cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
  529. /* enable all interrupts */
  530. cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
  531. altera_pcie_host_init(pcie);
  532. bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops,
  533. pcie, &pcie->resources);
  534. if (!bus)
  535. return -ENOMEM;
  536. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  537. pci_assign_unassigned_bus_resources(bus);
  538. /* Configure PCI Express setting. */
  539. list_for_each_entry(child, &bus->children, node)
  540. pcie_bus_configure_settings(child);
  541. pci_bus_add_devices(bus);
  542. platform_set_drvdata(pdev, pcie);
  543. return ret;
  544. }
  545. static const struct of_device_id altera_pcie_of_match[] = {
  546. { .compatible = "altr,pcie-root-port-1.0", },
  547. {},
  548. };
  549. MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
  550. static struct platform_driver altera_pcie_driver = {
  551. .probe = altera_pcie_probe,
  552. .driver = {
  553. .name = "altera-pcie",
  554. .of_match_table = altera_pcie_of_match,
  555. .suppress_bind_attrs = true,
  556. },
  557. };
  558. static int altera_pcie_init(void)
  559. {
  560. return platform_driver_register(&altera_pcie_driver);
  561. }
  562. module_init(altera_pcie_init);
  563. MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
  564. MODULE_DESCRIPTION("Altera PCIe host controller driver");
  565. MODULE_LICENSE("GPL v2");