pcie-altera.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. /* TLP configuration type 0 and 1 */
  44. #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
  45. #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
  46. #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
  47. #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
  48. #define TLP_PAYLOAD_SIZE 0x01
  49. #define TLP_READ_TAG 0x1d
  50. #define TLP_WRITE_TAG 0x10
  51. #define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
  52. #define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
  53. #define TLP_CFG_DW2(bus, devfn, offset) \
  54. (((bus) << 24) | ((devfn) << 16) | (offset))
  55. #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
  56. #define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
  57. #define TLP_HDR_SIZE 3
  58. #define TLP_LOOP 500
  59. #define RP_DEVFN 0
  60. #define INTX_NUM 4
  61. #define DWORD_MASK 3
  62. struct altera_pcie {
  63. struct platform_device *pdev;
  64. void __iomem *cra_base;
  65. int irq;
  66. u8 root_bus_nr;
  67. struct irq_domain *irq_domain;
  68. struct resource bus_range;
  69. struct list_head resources;
  70. };
  71. struct tlp_rp_regpair_t {
  72. u32 ctrl;
  73. u32 reg0;
  74. u32 reg1;
  75. };
  76. static void altera_pcie_retrain(struct pci_dev *dev)
  77. {
  78. u16 linkcap, linkstat;
  79. /*
  80. * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
  81. * current speed is 2.5 GB/s.
  82. */
  83. pcie_capability_read_word(dev, PCI_EXP_LNKCAP, &linkcap);
  84. if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
  85. return;
  86. pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat);
  87. if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB)
  88. pcie_capability_set_word(dev, PCI_EXP_LNKCTL,
  89. PCI_EXP_LNKCTL_RL);
  90. }
  91. DECLARE_PCI_FIXUP_EARLY(0x1172, PCI_ANY_ID, altera_pcie_retrain);
  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 inline void cra_writel(struct altera_pcie *pcie, const u32 value,
  110. const u32 reg)
  111. {
  112. writel_relaxed(value, pcie->cra_base + reg);
  113. }
  114. static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
  115. {
  116. return readl_relaxed(pcie->cra_base + reg);
  117. }
  118. static void tlp_write_tx(struct altera_pcie *pcie,
  119. struct tlp_rp_regpair_t *tlp_rp_regdata)
  120. {
  121. cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
  122. cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
  123. cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
  124. }
  125. static bool altera_pcie_link_is_up(struct altera_pcie *pcie)
  126. {
  127. return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
  128. }
  129. static bool altera_pcie_valid_config(struct altera_pcie *pcie,
  130. 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) {
  134. if (!altera_pcie_link_is_up(pcie))
  135. return false;
  136. }
  137. /* access only one slot on each root port */
  138. if (bus->number == pcie->root_bus_nr && dev > 0)
  139. return false;
  140. /*
  141. * Do not read more than one device on the bus directly attached
  142. * to root port, root port can only attach to one downstream port.
  143. */
  144. if (bus->primary == pcie->root_bus_nr && dev > 0)
  145. return false;
  146. return true;
  147. }
  148. static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
  149. {
  150. int i;
  151. bool sop = 0;
  152. u32 ctrl;
  153. u32 reg0, reg1;
  154. u32 comp_status = 1;
  155. /*
  156. * Minimum 2 loops to read TLP headers and 1 loop to read data
  157. * payload.
  158. */
  159. for (i = 0; i < TLP_LOOP; i++) {
  160. ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
  161. if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
  162. reg0 = cra_readl(pcie, RP_RXCPL_REG0);
  163. reg1 = cra_readl(pcie, RP_RXCPL_REG1);
  164. if (ctrl & RP_RXCPL_SOP) {
  165. sop = true;
  166. comp_status = TLP_COMP_STATUS(reg1);
  167. }
  168. if (ctrl & RP_RXCPL_EOP) {
  169. if (comp_status)
  170. return PCIBIOS_DEVICE_NOT_FOUND;
  171. if (value)
  172. *value = reg0;
  173. return PCIBIOS_SUCCESSFUL;
  174. }
  175. }
  176. udelay(5);
  177. }
  178. return PCIBIOS_DEVICE_NOT_FOUND;
  179. }
  180. static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
  181. u32 data, bool align)
  182. {
  183. struct tlp_rp_regpair_t tlp_rp_regdata;
  184. tlp_rp_regdata.reg0 = headers[0];
  185. tlp_rp_regdata.reg1 = headers[1];
  186. tlp_rp_regdata.ctrl = RP_TX_SOP;
  187. tlp_write_tx(pcie, &tlp_rp_regdata);
  188. if (align) {
  189. tlp_rp_regdata.reg0 = headers[2];
  190. tlp_rp_regdata.reg1 = 0;
  191. tlp_rp_regdata.ctrl = 0;
  192. tlp_write_tx(pcie, &tlp_rp_regdata);
  193. tlp_rp_regdata.reg0 = data;
  194. tlp_rp_regdata.reg1 = 0;
  195. } else {
  196. tlp_rp_regdata.reg0 = headers[2];
  197. tlp_rp_regdata.reg1 = data;
  198. }
  199. tlp_rp_regdata.ctrl = RP_TX_EOP;
  200. tlp_write_tx(pcie, &tlp_rp_regdata);
  201. }
  202. static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
  203. int where, u8 byte_en, u32 *value)
  204. {
  205. u32 headers[TLP_HDR_SIZE];
  206. if (bus == pcie->root_bus_nr)
  207. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0);
  208. else
  209. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1);
  210. headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
  211. TLP_READ_TAG, byte_en);
  212. headers[2] = TLP_CFG_DW2(bus, devfn, where);
  213. tlp_write_packet(pcie, headers, 0, false);
  214. return tlp_read_packet(pcie, value);
  215. }
  216. static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
  217. int where, u8 byte_en, u32 value)
  218. {
  219. u32 headers[TLP_HDR_SIZE];
  220. int ret;
  221. if (bus == pcie->root_bus_nr)
  222. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0);
  223. else
  224. headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1);
  225. headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
  226. TLP_WRITE_TAG, byte_en);
  227. headers[2] = TLP_CFG_DW2(bus, devfn, where);
  228. /* check alignment to Qword */
  229. if ((where & 0x7) == 0)
  230. tlp_write_packet(pcie, headers, value, true);
  231. else
  232. tlp_write_packet(pcie, headers, value, false);
  233. ret = tlp_read_packet(pcie, NULL);
  234. if (ret != PCIBIOS_SUCCESSFUL)
  235. return ret;
  236. /*
  237. * Monitor changes to PCI_PRIMARY_BUS register on root port
  238. * and update local copy of root bus number accordingly.
  239. */
  240. if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
  241. pcie->root_bus_nr = (u8)(value);
  242. return PCIBIOS_SUCCESSFUL;
  243. }
  244. static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
  245. int where, int size, u32 *value)
  246. {
  247. struct altera_pcie *pcie = bus->sysdata;
  248. int ret;
  249. u32 data;
  250. u8 byte_en;
  251. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  252. return PCIBIOS_BAD_REGISTER_NUMBER;
  253. if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn))) {
  254. *value = 0xffffffff;
  255. return PCIBIOS_DEVICE_NOT_FOUND;
  256. }
  257. switch (size) {
  258. case 1:
  259. byte_en = 1 << (where & 3);
  260. break;
  261. case 2:
  262. byte_en = 3 << (where & 3);
  263. break;
  264. default:
  265. byte_en = 0xf;
  266. break;
  267. }
  268. ret = tlp_cfg_dword_read(pcie, bus->number, devfn,
  269. (where & ~DWORD_MASK), byte_en, &data);
  270. if (ret != PCIBIOS_SUCCESSFUL)
  271. return ret;
  272. switch (size) {
  273. case 1:
  274. *value = (data >> (8 * (where & 0x3))) & 0xff;
  275. break;
  276. case 2:
  277. *value = (data >> (8 * (where & 0x2))) & 0xffff;
  278. break;
  279. default:
  280. *value = data;
  281. break;
  282. }
  283. return PCIBIOS_SUCCESSFUL;
  284. }
  285. static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
  286. int where, int size, u32 value)
  287. {
  288. struct altera_pcie *pcie = bus->sysdata;
  289. u32 data32;
  290. u32 shift = 8 * (where & 3);
  291. u8 byte_en;
  292. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  293. return PCIBIOS_BAD_REGISTER_NUMBER;
  294. if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn)))
  295. return PCIBIOS_DEVICE_NOT_FOUND;
  296. switch (size) {
  297. case 1:
  298. data32 = (value & 0xff) << shift;
  299. byte_en = 1 << (where & 3);
  300. break;
  301. case 2:
  302. data32 = (value & 0xffff) << shift;
  303. byte_en = 3 << (where & 3);
  304. break;
  305. default:
  306. data32 = value;
  307. byte_en = 0xf;
  308. break;
  309. }
  310. return tlp_cfg_dword_write(pcie, bus->number, devfn,
  311. (where & ~DWORD_MASK), byte_en, data32);
  312. }
  313. static struct pci_ops altera_pcie_ops = {
  314. .read = altera_pcie_cfg_read,
  315. .write = altera_pcie_cfg_write,
  316. };
  317. static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  318. irq_hw_number_t hwirq)
  319. {
  320. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  321. irq_set_chip_data(irq, domain->host_data);
  322. return 0;
  323. }
  324. static const struct irq_domain_ops intx_domain_ops = {
  325. .map = altera_pcie_intx_map,
  326. };
  327. static void altera_pcie_isr(struct irq_desc *desc)
  328. {
  329. struct irq_chip *chip = irq_desc_get_chip(desc);
  330. struct altera_pcie *pcie;
  331. unsigned long status;
  332. u32 bit;
  333. u32 virq;
  334. chained_irq_enter(chip, desc);
  335. pcie = irq_desc_get_handler_data(desc);
  336. while ((status = cra_readl(pcie, P2A_INT_STATUS)
  337. & P2A_INT_STS_ALL) != 0) {
  338. for_each_set_bit(bit, &status, INTX_NUM) {
  339. /* clear interrupts */
  340. cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
  341. virq = irq_find_mapping(pcie->irq_domain, bit + 1);
  342. if (virq)
  343. generic_handle_irq(virq);
  344. else
  345. dev_err(&pcie->pdev->dev,
  346. "unexpected IRQ, INT%d\n", bit);
  347. }
  348. }
  349. chained_irq_exit(chip, desc);
  350. }
  351. static void altera_pcie_release_of_pci_ranges(struct altera_pcie *pcie)
  352. {
  353. pci_free_resource_list(&pcie->resources);
  354. }
  355. static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
  356. {
  357. int err, res_valid = 0;
  358. struct device *dev = &pcie->pdev->dev;
  359. struct device_node *np = dev->of_node;
  360. struct resource_entry *win;
  361. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
  362. NULL);
  363. if (err)
  364. return err;
  365. resource_list_for_each_entry(win, &pcie->resources) {
  366. struct resource *parent, *res = win->res;
  367. switch (resource_type(res)) {
  368. case IORESOURCE_MEM:
  369. parent = &iomem_resource;
  370. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  371. break;
  372. default:
  373. continue;
  374. }
  375. err = devm_request_resource(dev, parent, res);
  376. if (err)
  377. goto out_release_res;
  378. }
  379. if (!res_valid) {
  380. dev_err(dev, "non-prefetchable memory resource required\n");
  381. err = -EINVAL;
  382. goto out_release_res;
  383. }
  384. return 0;
  385. out_release_res:
  386. altera_pcie_release_of_pci_ranges(pcie);
  387. return err;
  388. }
  389. static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
  390. {
  391. struct device *dev = &pcie->pdev->dev;
  392. struct device_node *node = dev->of_node;
  393. /* Setup INTx */
  394. pcie->irq_domain = irq_domain_add_linear(node, INTX_NUM + 1,
  395. &intx_domain_ops, pcie);
  396. if (!pcie->irq_domain) {
  397. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  398. return -ENOMEM;
  399. }
  400. return 0;
  401. }
  402. static int altera_pcie_parse_dt(struct altera_pcie *pcie)
  403. {
  404. struct resource *cra;
  405. struct platform_device *pdev = pcie->pdev;
  406. cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
  407. if (!cra) {
  408. dev_err(&pdev->dev, "no Cra memory resource defined\n");
  409. return -ENODEV;
  410. }
  411. pcie->cra_base = devm_ioremap_resource(&pdev->dev, cra);
  412. if (IS_ERR(pcie->cra_base)) {
  413. dev_err(&pdev->dev, "failed to map cra memory\n");
  414. return PTR_ERR(pcie->cra_base);
  415. }
  416. /* setup IRQ */
  417. pcie->irq = platform_get_irq(pdev, 0);
  418. if (pcie->irq <= 0) {
  419. dev_err(&pdev->dev, "failed to get IRQ: %d\n", pcie->irq);
  420. return -EINVAL;
  421. }
  422. irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
  423. return 0;
  424. }
  425. static int altera_pcie_probe(struct platform_device *pdev)
  426. {
  427. struct altera_pcie *pcie;
  428. struct pci_bus *bus;
  429. struct pci_bus *child;
  430. int ret;
  431. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  432. if (!pcie)
  433. return -ENOMEM;
  434. pcie->pdev = pdev;
  435. ret = altera_pcie_parse_dt(pcie);
  436. if (ret) {
  437. dev_err(&pdev->dev, "Parsing DT failed\n");
  438. return ret;
  439. }
  440. INIT_LIST_HEAD(&pcie->resources);
  441. ret = altera_pcie_parse_request_of_pci_ranges(pcie);
  442. if (ret) {
  443. dev_err(&pdev->dev, "Failed add resources\n");
  444. return ret;
  445. }
  446. ret = altera_pcie_init_irq_domain(pcie);
  447. if (ret) {
  448. dev_err(&pdev->dev, "Failed creating IRQ Domain\n");
  449. return ret;
  450. }
  451. /* clear all interrupts */
  452. cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
  453. /* enable all interrupts */
  454. cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
  455. bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops,
  456. pcie, &pcie->resources);
  457. if (!bus)
  458. return -ENOMEM;
  459. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  460. pci_assign_unassigned_bus_resources(bus);
  461. /* Configure PCI Express setting. */
  462. list_for_each_entry(child, &bus->children, node)
  463. pcie_bus_configure_settings(child);
  464. pci_bus_add_devices(bus);
  465. platform_set_drvdata(pdev, pcie);
  466. return ret;
  467. }
  468. static const struct of_device_id altera_pcie_of_match[] = {
  469. { .compatible = "altr,pcie-root-port-1.0", },
  470. {},
  471. };
  472. MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
  473. static struct platform_driver altera_pcie_driver = {
  474. .probe = altera_pcie_probe,
  475. .driver = {
  476. .name = "altera-pcie",
  477. .of_match_table = altera_pcie_of_match,
  478. .suppress_bind_attrs = true,
  479. },
  480. };
  481. static int altera_pcie_init(void)
  482. {
  483. return platform_driver_register(&altera_pcie_driver);
  484. }
  485. module_init(altera_pcie_init);
  486. MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
  487. MODULE_DESCRIPTION("Altera PCIe host controller driver");
  488. MODULE_LICENSE("GPL v2");