pcie-keembay.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCIe controller driver for Intel Keem Bay
  4. * Copyright (C) 2020 Intel Corporation
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bits.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/init.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include "pcie-designware.h"
  21. /* PCIE_REGS_APB_SLV Registers */
  22. #define PCIE_REGS_PCIE_CFG 0x0004
  23. #define PCIE_DEVICE_TYPE BIT(8)
  24. #define PCIE_RSTN BIT(0)
  25. #define PCIE_REGS_PCIE_APP_CNTRL 0x0008
  26. #define APP_LTSSM_ENABLE BIT(0)
  27. #define PCIE_REGS_INTERRUPT_ENABLE 0x0028
  28. #define MSI_CTRL_INT_EN BIT(8)
  29. #define EDMA_INT_EN GENMASK(7, 0)
  30. #define PCIE_REGS_INTERRUPT_STATUS 0x002c
  31. #define MSI_CTRL_INT BIT(8)
  32. #define PCIE_REGS_PCIE_SII_PM_STATE 0x00b0
  33. #define SMLH_LINK_UP BIT(19)
  34. #define RDLH_LINK_UP BIT(8)
  35. #define PCIE_REGS_PCIE_SII_LINK_UP (SMLH_LINK_UP | RDLH_LINK_UP)
  36. #define PCIE_REGS_PCIE_PHY_CNTL 0x0164
  37. #define PHY0_SRAM_BYPASS BIT(8)
  38. #define PCIE_REGS_PCIE_PHY_STAT 0x0168
  39. #define PHY0_MPLLA_STATE BIT(1)
  40. #define PCIE_REGS_LJPLL_STA 0x016c
  41. #define LJPLL_LOCK BIT(0)
  42. #define PCIE_REGS_LJPLL_CNTRL_0 0x0170
  43. #define LJPLL_EN BIT(29)
  44. #define LJPLL_FOUT_EN GENMASK(24, 21)
  45. #define PCIE_REGS_LJPLL_CNTRL_2 0x0178
  46. #define LJPLL_REF_DIV GENMASK(17, 12)
  47. #define LJPLL_FB_DIV GENMASK(11, 0)
  48. #define PCIE_REGS_LJPLL_CNTRL_3 0x017c
  49. #define LJPLL_POST_DIV3A GENMASK(24, 22)
  50. #define LJPLL_POST_DIV2A GENMASK(18, 16)
  51. #define PERST_DELAY_US 1000
  52. #define AUX_CLK_RATE_HZ 24000000
  53. struct keembay_pcie {
  54. struct dw_pcie pci;
  55. void __iomem *apb_base;
  56. enum dw_pcie_device_mode mode;
  57. struct clk *clk_master;
  58. struct clk *clk_aux;
  59. struct gpio_desc *reset;
  60. };
  61. struct keembay_pcie_of_data {
  62. enum dw_pcie_device_mode mode;
  63. };
  64. static void keembay_ep_reset_assert(struct keembay_pcie *pcie)
  65. {
  66. gpiod_set_value_cansleep(pcie->reset, 1);
  67. usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
  68. }
  69. static void keembay_ep_reset_deassert(struct keembay_pcie *pcie)
  70. {
  71. /*
  72. * Ensure that PERST# is asserted for a minimum of 100ms.
  73. *
  74. * For more details, refer to PCI Express Card Electromechanical
  75. * Specification Revision 1.1, Table-2.4.
  76. */
  77. msleep(100);
  78. gpiod_set_value_cansleep(pcie->reset, 0);
  79. usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
  80. }
  81. static void keembay_pcie_ltssm_set(struct keembay_pcie *pcie, bool enable)
  82. {
  83. u32 val;
  84. val = readl(pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
  85. if (enable)
  86. val |= APP_LTSSM_ENABLE;
  87. else
  88. val &= ~APP_LTSSM_ENABLE;
  89. writel(val, pcie->apb_base + PCIE_REGS_PCIE_APP_CNTRL);
  90. }
  91. static bool keembay_pcie_link_up(struct dw_pcie *pci)
  92. {
  93. struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
  94. u32 val;
  95. val = readl(pcie->apb_base + PCIE_REGS_PCIE_SII_PM_STATE);
  96. return (val & PCIE_REGS_PCIE_SII_LINK_UP) == PCIE_REGS_PCIE_SII_LINK_UP;
  97. }
  98. static int keembay_pcie_start_link(struct dw_pcie *pci)
  99. {
  100. struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
  101. u32 val;
  102. int ret;
  103. if (pcie->mode == DW_PCIE_EP_TYPE)
  104. return 0;
  105. keembay_pcie_ltssm_set(pcie, false);
  106. ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_PCIE_PHY_STAT,
  107. val, val & PHY0_MPLLA_STATE, 20,
  108. 500 * USEC_PER_MSEC);
  109. if (ret) {
  110. dev_err(pci->dev, "MPLLA is not locked\n");
  111. return ret;
  112. }
  113. keembay_pcie_ltssm_set(pcie, true);
  114. return 0;
  115. }
  116. static void keembay_pcie_stop_link(struct dw_pcie *pci)
  117. {
  118. struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
  119. keembay_pcie_ltssm_set(pcie, false);
  120. }
  121. static const struct dw_pcie_ops keembay_pcie_ops = {
  122. .link_up = keembay_pcie_link_up,
  123. .start_link = keembay_pcie_start_link,
  124. .stop_link = keembay_pcie_stop_link,
  125. };
  126. static inline void keembay_pcie_disable_clock(void *data)
  127. {
  128. struct clk *clk = data;
  129. clk_disable_unprepare(clk);
  130. }
  131. static inline struct clk *keembay_pcie_probe_clock(struct device *dev,
  132. const char *id, u64 rate)
  133. {
  134. struct clk *clk;
  135. int ret;
  136. clk = devm_clk_get(dev, id);
  137. if (IS_ERR(clk))
  138. return clk;
  139. if (rate) {
  140. ret = clk_set_rate(clk, rate);
  141. if (ret)
  142. return ERR_PTR(ret);
  143. }
  144. ret = clk_prepare_enable(clk);
  145. if (ret)
  146. return ERR_PTR(ret);
  147. ret = devm_add_action_or_reset(dev, keembay_pcie_disable_clock, clk);
  148. if (ret)
  149. return ERR_PTR(ret);
  150. return clk;
  151. }
  152. static int keembay_pcie_probe_clocks(struct keembay_pcie *pcie)
  153. {
  154. struct dw_pcie *pci = &pcie->pci;
  155. struct device *dev = pci->dev;
  156. pcie->clk_master = keembay_pcie_probe_clock(dev, "master", 0);
  157. if (IS_ERR(pcie->clk_master))
  158. return dev_err_probe(dev, PTR_ERR(pcie->clk_master),
  159. "Failed to enable master clock");
  160. pcie->clk_aux = keembay_pcie_probe_clock(dev, "aux", AUX_CLK_RATE_HZ);
  161. if (IS_ERR(pcie->clk_aux))
  162. return dev_err_probe(dev, PTR_ERR(pcie->clk_aux),
  163. "Failed to enable auxiliary clock");
  164. return 0;
  165. }
  166. /*
  167. * Initialize the internal PCIe PLL in Host mode.
  168. * See the following sections in Keem Bay data book,
  169. * (1) 6.4.6.1 PCIe Subsystem Example Initialization,
  170. * (2) 6.8 PCIe Low Jitter PLL for Ref Clk Generation.
  171. */
  172. static int keembay_pcie_pll_init(struct keembay_pcie *pcie)
  173. {
  174. struct dw_pcie *pci = &pcie->pci;
  175. u32 val;
  176. int ret;
  177. val = FIELD_PREP(LJPLL_REF_DIV, 0) | FIELD_PREP(LJPLL_FB_DIV, 0x32);
  178. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_2);
  179. val = FIELD_PREP(LJPLL_POST_DIV3A, 0x2) |
  180. FIELD_PREP(LJPLL_POST_DIV2A, 0x2);
  181. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_3);
  182. val = FIELD_PREP(LJPLL_EN, 0x1) | FIELD_PREP(LJPLL_FOUT_EN, 0xc);
  183. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_0);
  184. ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_LJPLL_STA,
  185. val, val & LJPLL_LOCK, 20,
  186. 500 * USEC_PER_MSEC);
  187. if (ret)
  188. dev_err(pci->dev, "Low jitter PLL is not locked\n");
  189. return ret;
  190. }
  191. static void keembay_pcie_msi_irq_handler(struct irq_desc *desc)
  192. {
  193. struct keembay_pcie *pcie = irq_desc_get_handler_data(desc);
  194. struct irq_chip *chip = irq_desc_get_chip(desc);
  195. u32 val, mask, status;
  196. struct dw_pcie_rp *pp;
  197. /*
  198. * Keem Bay PCIe Controller provides an additional IP logic on top of
  199. * standard DWC IP to clear MSI IRQ by writing '1' to the respective
  200. * bit of the status register.
  201. *
  202. * So, a chained irq handler is defined to handle this additional
  203. * IP logic.
  204. */
  205. chained_irq_enter(chip, desc);
  206. pp = &pcie->pci.pp;
  207. val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
  208. mask = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  209. status = val & mask;
  210. if (status & MSI_CTRL_INT) {
  211. dw_handle_msi_irq(pp);
  212. writel(status, pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
  213. }
  214. chained_irq_exit(chip, desc);
  215. }
  216. static int keembay_pcie_setup_msi_irq(struct keembay_pcie *pcie)
  217. {
  218. struct dw_pcie *pci = &pcie->pci;
  219. struct device *dev = pci->dev;
  220. struct platform_device *pdev = to_platform_device(dev);
  221. int irq;
  222. irq = platform_get_irq_byname(pdev, "pcie");
  223. if (irq < 0)
  224. return irq;
  225. irq_set_chained_handler_and_data(irq, keembay_pcie_msi_irq_handler,
  226. pcie);
  227. return 0;
  228. }
  229. static void keembay_pcie_ep_init(struct dw_pcie_ep *ep)
  230. {
  231. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  232. struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
  233. writel(EDMA_INT_EN, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  234. }
  235. static int keembay_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  236. unsigned int type, u16 interrupt_num)
  237. {
  238. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  239. switch (type) {
  240. case PCI_IRQ_INTX:
  241. /* INTx interrupts are not supported in Keem Bay */
  242. dev_err(pci->dev, "INTx IRQ is not supported\n");
  243. return -EINVAL;
  244. case PCI_IRQ_MSI:
  245. return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
  246. case PCI_IRQ_MSIX:
  247. return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
  248. default:
  249. dev_err(pci->dev, "Unknown IRQ type %d\n", type);
  250. return -EINVAL;
  251. }
  252. }
  253. static const struct pci_epc_features keembay_pcie_epc_features = {
  254. DWC_EPC_COMMON_FEATURES,
  255. .msi_capable = true,
  256. .msix_capable = true,
  257. .bar[BAR_0] = { .only_64bit = true, },
  258. .bar[BAR_1] = { .type = BAR_RESERVED, },
  259. .bar[BAR_2] = { .only_64bit = true, },
  260. .bar[BAR_3] = { .type = BAR_RESERVED, },
  261. .bar[BAR_4] = { .only_64bit = true, },
  262. .bar[BAR_5] = { .type = BAR_RESERVED, },
  263. .align = SZ_16K,
  264. };
  265. static const struct pci_epc_features *
  266. keembay_pcie_get_features(struct dw_pcie_ep *ep)
  267. {
  268. return &keembay_pcie_epc_features;
  269. }
  270. static const struct dw_pcie_ep_ops keembay_pcie_ep_ops = {
  271. .init = keembay_pcie_ep_init,
  272. .raise_irq = keembay_pcie_ep_raise_irq,
  273. .get_features = keembay_pcie_get_features,
  274. };
  275. static const struct dw_pcie_host_ops keembay_pcie_host_ops = {
  276. };
  277. static int keembay_pcie_add_pcie_port(struct keembay_pcie *pcie,
  278. struct platform_device *pdev)
  279. {
  280. struct dw_pcie *pci = &pcie->pci;
  281. struct dw_pcie_rp *pp = &pci->pp;
  282. struct device *dev = &pdev->dev;
  283. u32 val;
  284. int ret;
  285. pp->ops = &keembay_pcie_host_ops;
  286. pp->msi_irq[0] = -ENODEV;
  287. ret = keembay_pcie_setup_msi_irq(pcie);
  288. if (ret)
  289. return ret;
  290. pcie->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  291. if (IS_ERR(pcie->reset))
  292. return PTR_ERR(pcie->reset);
  293. ret = keembay_pcie_probe_clocks(pcie);
  294. if (ret)
  295. return ret;
  296. val = readl(pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
  297. val |= PHY0_SRAM_BYPASS;
  298. writel(val, pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
  299. writel(PCIE_DEVICE_TYPE, pcie->apb_base + PCIE_REGS_PCIE_CFG);
  300. ret = keembay_pcie_pll_init(pcie);
  301. if (ret)
  302. return ret;
  303. val = readl(pcie->apb_base + PCIE_REGS_PCIE_CFG);
  304. writel(val | PCIE_RSTN, pcie->apb_base + PCIE_REGS_PCIE_CFG);
  305. keembay_ep_reset_deassert(pcie);
  306. ret = dw_pcie_host_init(pp);
  307. if (ret) {
  308. keembay_ep_reset_assert(pcie);
  309. dev_err(dev, "Failed to initialize host: %d\n", ret);
  310. return ret;
  311. }
  312. val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  313. if (IS_ENABLED(CONFIG_PCI_MSI))
  314. val |= MSI_CTRL_INT_EN;
  315. writel(val, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  316. return 0;
  317. }
  318. static int keembay_pcie_probe(struct platform_device *pdev)
  319. {
  320. const struct keembay_pcie_of_data *data;
  321. struct device *dev = &pdev->dev;
  322. struct keembay_pcie *pcie;
  323. struct dw_pcie *pci;
  324. enum dw_pcie_device_mode mode;
  325. int ret;
  326. data = device_get_match_data(dev);
  327. if (!data)
  328. return -ENODEV;
  329. mode = (enum dw_pcie_device_mode)data->mode;
  330. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  331. if (!pcie)
  332. return -ENOMEM;
  333. pci = &pcie->pci;
  334. pci->dev = dev;
  335. pci->ops = &keembay_pcie_ops;
  336. pcie->mode = mode;
  337. pcie->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
  338. if (IS_ERR(pcie->apb_base))
  339. return PTR_ERR(pcie->apb_base);
  340. platform_set_drvdata(pdev, pcie);
  341. switch (pcie->mode) {
  342. case DW_PCIE_RC_TYPE:
  343. if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_HOST))
  344. return -ENODEV;
  345. return keembay_pcie_add_pcie_port(pcie, pdev);
  346. case DW_PCIE_EP_TYPE:
  347. if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_EP))
  348. return -ENODEV;
  349. pci->ep.ops = &keembay_pcie_ep_ops;
  350. ret = dw_pcie_ep_init(&pci->ep);
  351. if (ret)
  352. return ret;
  353. ret = dw_pcie_ep_init_registers(&pci->ep);
  354. if (ret) {
  355. dev_err(dev, "Failed to initialize DWC endpoint registers\n");
  356. dw_pcie_ep_deinit(&pci->ep);
  357. return ret;
  358. }
  359. pci_epc_init_notify(pci->ep.epc);
  360. break;
  361. default:
  362. dev_err(dev, "Invalid device type %d\n", pcie->mode);
  363. return -ENODEV;
  364. }
  365. return 0;
  366. }
  367. static const struct keembay_pcie_of_data keembay_pcie_rc_of_data = {
  368. .mode = DW_PCIE_RC_TYPE,
  369. };
  370. static const struct keembay_pcie_of_data keembay_pcie_ep_of_data = {
  371. .mode = DW_PCIE_EP_TYPE,
  372. };
  373. static const struct of_device_id keembay_pcie_of_match[] = {
  374. {
  375. .compatible = "intel,keembay-pcie",
  376. .data = &keembay_pcie_rc_of_data,
  377. },
  378. {
  379. .compatible = "intel,keembay-pcie-ep",
  380. .data = &keembay_pcie_ep_of_data,
  381. },
  382. {}
  383. };
  384. static struct platform_driver keembay_pcie_driver = {
  385. .driver = {
  386. .name = "keembay-pcie",
  387. .of_match_table = keembay_pcie_of_match,
  388. .suppress_bind_attrs = true,
  389. },
  390. .probe = keembay_pcie_probe,
  391. };
  392. builtin_platform_driver(keembay_pcie_driver);