pcie-altera.c 20 KB

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