pcie-altera.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright Altera Corporation (C) 2013-2015. All rights reserved
  4. *
  5. * Author: Ley Foon Tan <[email protected]>
  6. * Description: Altera PCIe host controller driver
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irqchip/chained_irq.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include "../pci.h"
  21. #define RP_TX_REG0 0x2000
  22. #define RP_TX_REG1 0x2004
  23. #define RP_TX_CNTRL 0x2008
  24. #define RP_TX_EOP 0x2
  25. #define RP_TX_SOP 0x1
  26. #define RP_RXCPL_STATUS 0x2010
  27. #define RP_RXCPL_EOP 0x2
  28. #define RP_RXCPL_SOP 0x1
  29. #define RP_RXCPL_REG0 0x2014
  30. #define RP_RXCPL_REG1 0x2018
  31. #define P2A_INT_STATUS 0x3060
  32. #define P2A_INT_STS_ALL 0xf
  33. #define P2A_INT_ENABLE 0x3070
  34. #define P2A_INT_ENA_ALL 0xf
  35. #define RP_LTSSM 0x3c64
  36. #define RP_LTSSM_MASK 0x1f
  37. #define LTSSM_L0 0xf
  38. #define S10_RP_TX_CNTRL 0x2004
  39. #define S10_RP_RXCPL_REG 0x2008
  40. #define S10_RP_RXCPL_STATUS 0x200C
  41. #define S10_RP_CFG_ADDR(pcie, reg) \
  42. (((pcie)->hip_base) + (reg) + (1 << 20))
  43. #define S10_RP_SECONDARY(pcie) \
  44. readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
  45. /* TLP configuration type 0 and 1 */
  46. #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
  47. #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
  48. #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
  49. #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
  50. #define TLP_PAYLOAD_SIZE 0x01
  51. #define TLP_READ_TAG 0x1d
  52. #define TLP_WRITE_TAG 0x10
  53. #define RP_DEVFN 0
  54. #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
  55. #define TLP_CFG_DW0(pcie, cfg) \
  56. (((cfg) << 24) | \
  57. TLP_PAYLOAD_SIZE)
  58. #define TLP_CFG_DW1(pcie, tag, be) \
  59. (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
  60. #define TLP_CFG_DW2(bus, devfn, offset) \
  61. (((bus) << 24) | ((devfn) << 16) | (offset))
  62. #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
  63. #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
  64. #define TLP_HDR_SIZE 3
  65. #define TLP_LOOP 500
  66. #define LINK_UP_TIMEOUT HZ
  67. #define LINK_RETRAIN_TIMEOUT HZ
  68. #define DWORD_MASK 3
  69. #define S10_TLP_FMTTYPE_CFGRD0 0x05
  70. #define S10_TLP_FMTTYPE_CFGRD1 0x04
  71. #define S10_TLP_FMTTYPE_CFGWR0 0x45
  72. #define S10_TLP_FMTTYPE_CFGWR1 0x44
  73. enum altera_pcie_version {
  74. ALTERA_PCIE_V1 = 0,
  75. ALTERA_PCIE_V2,
  76. };
  77. struct altera_pcie {
  78. struct platform_device *pdev;
  79. void __iomem *cra_base;
  80. void __iomem *hip_base;
  81. int irq;
  82. u8 root_bus_nr;
  83. struct irq_domain *irq_domain;
  84. struct resource bus_range;
  85. const struct altera_pcie_data *pcie_data;
  86. };
  87. struct altera_pcie_ops {
  88. int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value);
  89. void (*tlp_write_pkt)(struct altera_pcie *pcie, u32 *headers,
  90. u32 data, bool align);
  91. bool (*get_link_status)(struct altera_pcie *pcie);
  92. int (*rp_read_cfg)(struct altera_pcie *pcie, int where,
  93. int size, u32 *value);
  94. int (*rp_write_cfg)(struct altera_pcie *pcie, u8 busno,
  95. int where, int size, u32 value);
  96. };
  97. struct altera_pcie_data {
  98. const struct altera_pcie_ops *ops;
  99. enum altera_pcie_version version;
  100. u32 cap_offset; /* PCIe capability structure register offset */
  101. u32 cfgrd0;
  102. u32 cfgrd1;
  103. u32 cfgwr0;
  104. u32 cfgwr1;
  105. };
  106. struct tlp_rp_regpair_t {
  107. u32 ctrl;
  108. u32 reg0;
  109. u32 reg1;
  110. };
  111. static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
  112. const u32 reg)
  113. {
  114. writel_relaxed(value, pcie->cra_base + reg);
  115. }
  116. static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
  117. {
  118. return readl_relaxed(pcie->cra_base + reg);
  119. }
  120. static bool altera_pcie_link_up(struct altera_pcie *pcie)
  121. {
  122. return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
  123. }
  124. static bool s10_altera_pcie_link_up(struct altera_pcie *pcie)
  125. {
  126. void __iomem *addr = S10_RP_CFG_ADDR(pcie,
  127. pcie->pcie_data->cap_offset +
  128. PCI_EXP_LNKSTA);
  129. return !!(readw(addr) & PCI_EXP_LNKSTA_DLLLA);
  130. }
  131. /*
  132. * Altera PCIe port uses BAR0 of RC's configuration space as the translation
  133. * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
  134. * using these registers, so it can be reached by DMA from EP devices.
  135. * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
  136. * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
  137. * should be hidden during enumeration to avoid the sizing and resource
  138. * allocation by PCIe core.
  139. */
  140. static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
  141. int offset)
  142. {
  143. if (pci_is_root_bus(bus) && (devfn == 0) &&
  144. (offset == PCI_BASE_ADDRESS_0))
  145. return true;
  146. return false;
  147. }
  148. static void tlp_write_tx(struct altera_pcie *pcie,
  149. struct tlp_rp_regpair_t *tlp_rp_regdata)
  150. {
  151. cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
  152. cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
  153. cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
  154. }
  155. static void s10_tlp_write_tx(struct altera_pcie *pcie, u32 reg0, u32 ctrl)
  156. {
  157. cra_writel(pcie, reg0, RP_TX_REG0);
  158. cra_writel(pcie, ctrl, S10_RP_TX_CNTRL);
  159. }
  160. static bool altera_pcie_valid_device(struct altera_pcie *pcie,
  161. struct pci_bus *bus, int dev)
  162. {
  163. /* If there is no link, then there is no device */
  164. if (bus->number != pcie->root_bus_nr) {
  165. if (!pcie->pcie_data->ops->get_link_status(pcie))
  166. return false;
  167. }
  168. /* access only one slot on each root port */
  169. if (bus->number == pcie->root_bus_nr && dev > 0)
  170. return false;
  171. return true;
  172. }
  173. static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
  174. {
  175. int i;
  176. bool sop = false;
  177. u32 ctrl;
  178. u32 reg0, reg1;
  179. u32 comp_status = 1;
  180. /*
  181. * Minimum 2 loops to read TLP headers and 1 loop to read data
  182. * payload.
  183. */
  184. for (i = 0; i < TLP_LOOP; i++) {
  185. ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
  186. if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
  187. reg0 = cra_readl(pcie, RP_RXCPL_REG0);
  188. reg1 = cra_readl(pcie, RP_RXCPL_REG1);
  189. if (ctrl & RP_RXCPL_SOP) {
  190. sop = true;
  191. comp_status = TLP_COMP_STATUS(reg1);
  192. }
  193. if (ctrl & RP_RXCPL_EOP) {
  194. if (comp_status)
  195. return PCIBIOS_DEVICE_NOT_FOUND;
  196. if (value)
  197. *value = reg0;
  198. return PCIBIOS_SUCCESSFUL;
  199. }
  200. }
  201. udelay(5);
  202. }
  203. return PCIBIOS_DEVICE_NOT_FOUND;
  204. }
  205. static int s10_tlp_read_packet(struct altera_pcie *pcie, u32 *value)
  206. {
  207. u32 ctrl;
  208. u32 comp_status;
  209. u32 dw[4];
  210. u32 count;
  211. struct device *dev = &pcie->pdev->dev;
  212. for (count = 0; count < TLP_LOOP; count++) {
  213. ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
  214. if (ctrl & RP_RXCPL_SOP) {
  215. /* Read first DW */
  216. dw[0] = cra_readl(pcie, S10_RP_RXCPL_REG);
  217. break;
  218. }
  219. udelay(5);
  220. }
  221. /* SOP detection failed, return error */
  222. if (count == TLP_LOOP)
  223. return PCIBIOS_DEVICE_NOT_FOUND;
  224. count = 1;
  225. /* Poll for EOP */
  226. while (count < ARRAY_SIZE(dw)) {
  227. ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
  228. dw[count++] = cra_readl(pcie, S10_RP_RXCPL_REG);
  229. if (ctrl & RP_RXCPL_EOP) {
  230. comp_status = TLP_COMP_STATUS(dw[1]);
  231. if (comp_status)
  232. return PCIBIOS_DEVICE_NOT_FOUND;
  233. if (value && TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
  234. count == 4)
  235. *value = dw[3];
  236. return PCIBIOS_SUCCESSFUL;
  237. }
  238. }
  239. dev_warn(dev, "Malformed TLP packet\n");
  240. return PCIBIOS_DEVICE_NOT_FOUND;
  241. }
  242. static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
  243. u32 data, bool align)
  244. {
  245. struct tlp_rp_regpair_t tlp_rp_regdata;
  246. tlp_rp_regdata.reg0 = headers[0];
  247. tlp_rp_regdata.reg1 = headers[1];
  248. tlp_rp_regdata.ctrl = RP_TX_SOP;
  249. tlp_write_tx(pcie, &tlp_rp_regdata);
  250. if (align) {
  251. tlp_rp_regdata.reg0 = headers[2];
  252. tlp_rp_regdata.reg1 = 0;
  253. tlp_rp_regdata.ctrl = 0;
  254. tlp_write_tx(pcie, &tlp_rp_regdata);
  255. tlp_rp_regdata.reg0 = data;
  256. tlp_rp_regdata.reg1 = 0;
  257. } else {
  258. tlp_rp_regdata.reg0 = headers[2];
  259. tlp_rp_regdata.reg1 = data;
  260. }
  261. tlp_rp_regdata.ctrl = RP_TX_EOP;
  262. tlp_write_tx(pcie, &tlp_rp_regdata);
  263. }
  264. static void s10_tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
  265. u32 data, bool dummy)
  266. {
  267. s10_tlp_write_tx(pcie, headers[0], RP_TX_SOP);
  268. s10_tlp_write_tx(pcie, headers[1], 0);
  269. s10_tlp_write_tx(pcie, headers[2], 0);
  270. s10_tlp_write_tx(pcie, data, RP_TX_EOP);
  271. }
  272. static void get_tlp_header(struct altera_pcie *pcie, u8 bus, u32 devfn,
  273. int where, u8 byte_en, bool read, u32 *headers)
  274. {
  275. u8 cfg;
  276. u8 cfg0 = read ? pcie->pcie_data->cfgrd0 : pcie->pcie_data->cfgwr0;
  277. u8 cfg1 = read ? pcie->pcie_data->cfgrd1 : pcie->pcie_data->cfgwr1;
  278. u8 tag = read ? TLP_READ_TAG : TLP_WRITE_TAG;
  279. if (pcie->pcie_data->version == ALTERA_PCIE_V1)
  280. cfg = (bus == pcie->root_bus_nr) ? cfg0 : cfg1;
  281. else
  282. cfg = (bus > S10_RP_SECONDARY(pcie)) ? cfg0 : cfg1;
  283. headers[0] = TLP_CFG_DW0(pcie, cfg);
  284. headers[1] = TLP_CFG_DW1(pcie, tag, byte_en);
  285. headers[2] = TLP_CFG_DW2(bus, devfn, where);
  286. }
  287. static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
  288. int where, u8 byte_en, u32 *value)
  289. {
  290. u32 headers[TLP_HDR_SIZE];
  291. get_tlp_header(pcie, bus, devfn, where, byte_en, true,
  292. headers);
  293. pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, 0, false);
  294. return pcie->pcie_data->ops->tlp_read_pkt(pcie, value);
  295. }
  296. static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
  297. int where, u8 byte_en, u32 value)
  298. {
  299. u32 headers[TLP_HDR_SIZE];
  300. int ret;
  301. get_tlp_header(pcie, bus, devfn, where, byte_en, false,
  302. headers);
  303. /* check alignment to Qword */
  304. if ((where & 0x7) == 0)
  305. pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
  306. value, true);
  307. else
  308. pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
  309. value, false);
  310. ret = pcie->pcie_data->ops->tlp_read_pkt(pcie, NULL);
  311. if (ret != PCIBIOS_SUCCESSFUL)
  312. return ret;
  313. /*
  314. * Monitor changes to PCI_PRIMARY_BUS register on root port
  315. * and update local copy of root bus number accordingly.
  316. */
  317. if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
  318. pcie->root_bus_nr = (u8)(value);
  319. return PCIBIOS_SUCCESSFUL;
  320. }
  321. static int s10_rp_read_cfg(struct altera_pcie *pcie, int where,
  322. int size, u32 *value)
  323. {
  324. void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
  325. switch (size) {
  326. case 1:
  327. *value = readb(addr);
  328. break;
  329. case 2:
  330. *value = readw(addr);
  331. break;
  332. default:
  333. *value = readl(addr);
  334. break;
  335. }
  336. return PCIBIOS_SUCCESSFUL;
  337. }
  338. static int s10_rp_write_cfg(struct altera_pcie *pcie, u8 busno,
  339. int where, int size, u32 value)
  340. {
  341. void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
  342. switch (size) {
  343. case 1:
  344. writeb(value, addr);
  345. break;
  346. case 2:
  347. writew(value, addr);
  348. break;
  349. default:
  350. writel(value, addr);
  351. break;
  352. }
  353. /*
  354. * Monitor changes to PCI_PRIMARY_BUS register on root port
  355. * and update local copy of root bus number accordingly.
  356. */
  357. if (busno == pcie->root_bus_nr && where == PCI_PRIMARY_BUS)
  358. pcie->root_bus_nr = value & 0xff;
  359. return PCIBIOS_SUCCESSFUL;
  360. }
  361. static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
  362. unsigned int devfn, int where, int size,
  363. u32 *value)
  364. {
  365. int ret;
  366. u32 data;
  367. u8 byte_en;
  368. if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_read_cfg)
  369. return pcie->pcie_data->ops->rp_read_cfg(pcie, where,
  370. size, value);
  371. switch (size) {
  372. case 1:
  373. byte_en = 1 << (where & 3);
  374. break;
  375. case 2:
  376. byte_en = 3 << (where & 3);
  377. break;
  378. default:
  379. byte_en = 0xf;
  380. break;
  381. }
  382. ret = tlp_cfg_dword_read(pcie, busno, devfn,
  383. (where & ~DWORD_MASK), byte_en, &data);
  384. if (ret != PCIBIOS_SUCCESSFUL)
  385. return ret;
  386. switch (size) {
  387. case 1:
  388. *value = (data >> (8 * (where & 0x3))) & 0xff;
  389. break;
  390. case 2:
  391. *value = (data >> (8 * (where & 0x2))) & 0xffff;
  392. break;
  393. default:
  394. *value = data;
  395. break;
  396. }
  397. return PCIBIOS_SUCCESSFUL;
  398. }
  399. static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
  400. unsigned int devfn, int where, int size,
  401. u32 value)
  402. {
  403. u32 data32;
  404. u32 shift = 8 * (where & 3);
  405. u8 byte_en;
  406. if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_write_cfg)
  407. return pcie->pcie_data->ops->rp_write_cfg(pcie, busno,
  408. where, size, value);
  409. switch (size) {
  410. case 1:
  411. data32 = (value & 0xff) << shift;
  412. byte_en = 1 << (where & 3);
  413. break;
  414. case 2:
  415. data32 = (value & 0xffff) << shift;
  416. byte_en = 3 << (where & 3);
  417. break;
  418. default:
  419. data32 = value;
  420. byte_en = 0xf;
  421. break;
  422. }
  423. return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
  424. byte_en, data32);
  425. }
  426. static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
  427. int where, int size, u32 *value)
  428. {
  429. struct altera_pcie *pcie = bus->sysdata;
  430. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  431. return PCIBIOS_BAD_REGISTER_NUMBER;
  432. if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
  433. return PCIBIOS_DEVICE_NOT_FOUND;
  434. return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
  435. value);
  436. }
  437. static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
  438. int where, int size, u32 value)
  439. {
  440. struct altera_pcie *pcie = bus->sysdata;
  441. if (altera_pcie_hide_rc_bar(bus, devfn, where))
  442. return PCIBIOS_BAD_REGISTER_NUMBER;
  443. if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
  444. return PCIBIOS_DEVICE_NOT_FOUND;
  445. return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
  446. value);
  447. }
  448. static struct pci_ops altera_pcie_ops = {
  449. .read = altera_pcie_cfg_read,
  450. .write = altera_pcie_cfg_write,
  451. };
  452. static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
  453. unsigned int devfn, int offset, u16 *value)
  454. {
  455. u32 data;
  456. int ret;
  457. ret = _altera_pcie_cfg_read(pcie, busno, devfn,
  458. pcie->pcie_data->cap_offset + offset,
  459. sizeof(*value),
  460. &data);
  461. *value = data;
  462. return ret;
  463. }
  464. static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
  465. unsigned int devfn, int offset, u16 value)
  466. {
  467. return _altera_pcie_cfg_write(pcie, busno, devfn,
  468. pcie->pcie_data->cap_offset + offset,
  469. sizeof(value),
  470. value);
  471. }
  472. static void altera_wait_link_retrain(struct altera_pcie *pcie)
  473. {
  474. struct device *dev = &pcie->pdev->dev;
  475. u16 reg16;
  476. unsigned long start_jiffies;
  477. /* Wait for link training end. */
  478. start_jiffies = jiffies;
  479. for (;;) {
  480. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  481. PCI_EXP_LNKSTA, &reg16);
  482. if (!(reg16 & PCI_EXP_LNKSTA_LT))
  483. break;
  484. if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
  485. dev_err(dev, "link retrain timeout\n");
  486. break;
  487. }
  488. udelay(100);
  489. }
  490. /* Wait for link is up */
  491. start_jiffies = jiffies;
  492. for (;;) {
  493. if (pcie->pcie_data->ops->get_link_status(pcie))
  494. break;
  495. if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
  496. dev_err(dev, "link up timeout\n");
  497. break;
  498. }
  499. udelay(100);
  500. }
  501. }
  502. static void altera_pcie_retrain(struct altera_pcie *pcie)
  503. {
  504. u16 linkcap, linkstat, linkctl;
  505. if (!pcie->pcie_data->ops->get_link_status(pcie))
  506. return;
  507. /*
  508. * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
  509. * current speed is 2.5 GB/s.
  510. */
  511. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
  512. &linkcap);
  513. if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
  514. return;
  515. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
  516. &linkstat);
  517. if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
  518. altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  519. PCI_EXP_LNKCTL, &linkctl);
  520. linkctl |= PCI_EXP_LNKCTL_RL;
  521. altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
  522. PCI_EXP_LNKCTL, linkctl);
  523. altera_wait_link_retrain(pcie);
  524. }
  525. }
  526. static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  527. irq_hw_number_t hwirq)
  528. {
  529. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  530. irq_set_chip_data(irq, domain->host_data);
  531. return 0;
  532. }
  533. static const struct irq_domain_ops intx_domain_ops = {
  534. .map = altera_pcie_intx_map,
  535. .xlate = pci_irqd_intx_xlate,
  536. };
  537. static void altera_pcie_isr(struct irq_desc *desc)
  538. {
  539. struct irq_chip *chip = irq_desc_get_chip(desc);
  540. struct altera_pcie *pcie;
  541. struct device *dev;
  542. unsigned long status;
  543. u32 bit;
  544. int ret;
  545. chained_irq_enter(chip, desc);
  546. pcie = irq_desc_get_handler_data(desc);
  547. dev = &pcie->pdev->dev;
  548. while ((status = cra_readl(pcie, P2A_INT_STATUS)
  549. & P2A_INT_STS_ALL) != 0) {
  550. for_each_set_bit(bit, &status, PCI_NUM_INTX) {
  551. /* clear interrupts */
  552. cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
  553. ret = generic_handle_domain_irq(pcie->irq_domain, bit);
  554. if (ret)
  555. dev_err_ratelimited(dev, "unexpected IRQ, INT%d\n", bit);
  556. }
  557. }
  558. chained_irq_exit(chip, desc);
  559. }
  560. static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
  561. {
  562. struct device *dev = &pcie->pdev->dev;
  563. struct device_node *node = dev->of_node;
  564. /* Setup INTx */
  565. pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX,
  566. &intx_domain_ops, pcie);
  567. if (!pcie->irq_domain) {
  568. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  569. return -ENOMEM;
  570. }
  571. return 0;
  572. }
  573. static void altera_pcie_irq_teardown(struct altera_pcie *pcie)
  574. {
  575. irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
  576. irq_domain_remove(pcie->irq_domain);
  577. irq_dispose_mapping(pcie->irq);
  578. }
  579. static int altera_pcie_parse_dt(struct altera_pcie *pcie)
  580. {
  581. struct platform_device *pdev = pcie->pdev;
  582. pcie->cra_base = devm_platform_ioremap_resource_byname(pdev, "Cra");
  583. if (IS_ERR(pcie->cra_base))
  584. return PTR_ERR(pcie->cra_base);
  585. if (pcie->pcie_data->version == ALTERA_PCIE_V2) {
  586. pcie->hip_base =
  587. devm_platform_ioremap_resource_byname(pdev, "Hip");
  588. if (IS_ERR(pcie->hip_base))
  589. return PTR_ERR(pcie->hip_base);
  590. }
  591. /* setup IRQ */
  592. pcie->irq = platform_get_irq(pdev, 0);
  593. if (pcie->irq < 0)
  594. return pcie->irq;
  595. irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
  596. return 0;
  597. }
  598. static void altera_pcie_host_init(struct altera_pcie *pcie)
  599. {
  600. altera_pcie_retrain(pcie);
  601. }
  602. static const struct altera_pcie_ops altera_pcie_ops_1_0 = {
  603. .tlp_read_pkt = tlp_read_packet,
  604. .tlp_write_pkt = tlp_write_packet,
  605. .get_link_status = altera_pcie_link_up,
  606. };
  607. static const struct altera_pcie_ops altera_pcie_ops_2_0 = {
  608. .tlp_read_pkt = s10_tlp_read_packet,
  609. .tlp_write_pkt = s10_tlp_write_packet,
  610. .get_link_status = s10_altera_pcie_link_up,
  611. .rp_read_cfg = s10_rp_read_cfg,
  612. .rp_write_cfg = s10_rp_write_cfg,
  613. };
  614. static const struct altera_pcie_data altera_pcie_1_0_data = {
  615. .ops = &altera_pcie_ops_1_0,
  616. .cap_offset = 0x80,
  617. .version = ALTERA_PCIE_V1,
  618. .cfgrd0 = TLP_FMTTYPE_CFGRD0,
  619. .cfgrd1 = TLP_FMTTYPE_CFGRD1,
  620. .cfgwr0 = TLP_FMTTYPE_CFGWR0,
  621. .cfgwr1 = TLP_FMTTYPE_CFGWR1,
  622. };
  623. static const struct altera_pcie_data altera_pcie_2_0_data = {
  624. .ops = &altera_pcie_ops_2_0,
  625. .version = ALTERA_PCIE_V2,
  626. .cap_offset = 0x70,
  627. .cfgrd0 = S10_TLP_FMTTYPE_CFGRD0,
  628. .cfgrd1 = S10_TLP_FMTTYPE_CFGRD1,
  629. .cfgwr0 = S10_TLP_FMTTYPE_CFGWR0,
  630. .cfgwr1 = S10_TLP_FMTTYPE_CFGWR1,
  631. };
  632. static const struct of_device_id altera_pcie_of_match[] = {
  633. {.compatible = "altr,pcie-root-port-1.0",
  634. .data = &altera_pcie_1_0_data },
  635. {.compatible = "altr,pcie-root-port-2.0",
  636. .data = &altera_pcie_2_0_data },
  637. {},
  638. };
  639. static int altera_pcie_probe(struct platform_device *pdev)
  640. {
  641. struct device *dev = &pdev->dev;
  642. struct altera_pcie *pcie;
  643. struct pci_host_bridge *bridge;
  644. int ret;
  645. const struct altera_pcie_data *data;
  646. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  647. if (!bridge)
  648. return -ENOMEM;
  649. pcie = pci_host_bridge_priv(bridge);
  650. pcie->pdev = pdev;
  651. platform_set_drvdata(pdev, pcie);
  652. data = of_device_get_match_data(&pdev->dev);
  653. if (!data)
  654. return -ENODEV;
  655. pcie->pcie_data = data;
  656. ret = altera_pcie_parse_dt(pcie);
  657. if (ret) {
  658. dev_err(dev, "Parsing DT failed\n");
  659. return ret;
  660. }
  661. ret = altera_pcie_init_irq_domain(pcie);
  662. if (ret) {
  663. dev_err(dev, "Failed creating IRQ Domain\n");
  664. return ret;
  665. }
  666. /* clear all interrupts */
  667. cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
  668. /* enable all interrupts */
  669. cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
  670. altera_pcie_host_init(pcie);
  671. bridge->sysdata = pcie;
  672. bridge->busnr = pcie->root_bus_nr;
  673. bridge->ops = &altera_pcie_ops;
  674. return pci_host_probe(bridge);
  675. }
  676. static int altera_pcie_remove(struct platform_device *pdev)
  677. {
  678. struct altera_pcie *pcie = platform_get_drvdata(pdev);
  679. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  680. pci_stop_root_bus(bridge->bus);
  681. pci_remove_root_bus(bridge->bus);
  682. altera_pcie_irq_teardown(pcie);
  683. return 0;
  684. }
  685. static struct platform_driver altera_pcie_driver = {
  686. .probe = altera_pcie_probe,
  687. .remove = altera_pcie_remove,
  688. .driver = {
  689. .name = "altera-pcie",
  690. .of_match_table = altera_pcie_of_match,
  691. },
  692. };
  693. MODULE_DEVICE_TABLE(of, altera_pcie_of_match);
  694. module_platform_driver(altera_pcie_driver);
  695. MODULE_LICENSE("GPL v2");