pcie-keembay.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 int 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 struct clk *keembay_pcie_probe_clock(struct device *dev,
  127. const char *id, u64 rate)
  128. {
  129. struct clk *clk;
  130. int ret;
  131. clk = devm_clk_get(dev, id);
  132. if (IS_ERR(clk))
  133. return clk;
  134. if (rate) {
  135. ret = clk_set_rate(clk, rate);
  136. if (ret)
  137. return ERR_PTR(ret);
  138. }
  139. ret = clk_prepare_enable(clk);
  140. if (ret)
  141. return ERR_PTR(ret);
  142. ret = devm_add_action_or_reset(dev,
  143. (void(*)(void *))clk_disable_unprepare,
  144. clk);
  145. if (ret)
  146. return ERR_PTR(ret);
  147. return clk;
  148. }
  149. static int keembay_pcie_probe_clocks(struct keembay_pcie *pcie)
  150. {
  151. struct dw_pcie *pci = &pcie->pci;
  152. struct device *dev = pci->dev;
  153. pcie->clk_master = keembay_pcie_probe_clock(dev, "master", 0);
  154. if (IS_ERR(pcie->clk_master))
  155. return dev_err_probe(dev, PTR_ERR(pcie->clk_master),
  156. "Failed to enable master clock");
  157. pcie->clk_aux = keembay_pcie_probe_clock(dev, "aux", AUX_CLK_RATE_HZ);
  158. if (IS_ERR(pcie->clk_aux))
  159. return dev_err_probe(dev, PTR_ERR(pcie->clk_aux),
  160. "Failed to enable auxiliary clock");
  161. return 0;
  162. }
  163. /*
  164. * Initialize the internal PCIe PLL in Host mode.
  165. * See the following sections in Keem Bay data book,
  166. * (1) 6.4.6.1 PCIe Subsystem Example Initialization,
  167. * (2) 6.8 PCIe Low Jitter PLL for Ref Clk Generation.
  168. */
  169. static int keembay_pcie_pll_init(struct keembay_pcie *pcie)
  170. {
  171. struct dw_pcie *pci = &pcie->pci;
  172. u32 val;
  173. int ret;
  174. val = FIELD_PREP(LJPLL_REF_DIV, 0) | FIELD_PREP(LJPLL_FB_DIV, 0x32);
  175. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_2);
  176. val = FIELD_PREP(LJPLL_POST_DIV3A, 0x2) |
  177. FIELD_PREP(LJPLL_POST_DIV2A, 0x2);
  178. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_3);
  179. val = FIELD_PREP(LJPLL_EN, 0x1) | FIELD_PREP(LJPLL_FOUT_EN, 0xc);
  180. writel(val, pcie->apb_base + PCIE_REGS_LJPLL_CNTRL_0);
  181. ret = readl_poll_timeout(pcie->apb_base + PCIE_REGS_LJPLL_STA,
  182. val, val & LJPLL_LOCK, 20,
  183. 500 * USEC_PER_MSEC);
  184. if (ret)
  185. dev_err(pci->dev, "Low jitter PLL is not locked\n");
  186. return ret;
  187. }
  188. static void keembay_pcie_msi_irq_handler(struct irq_desc *desc)
  189. {
  190. struct keembay_pcie *pcie = irq_desc_get_handler_data(desc);
  191. struct irq_chip *chip = irq_desc_get_chip(desc);
  192. u32 val, mask, status;
  193. struct dw_pcie_rp *pp;
  194. /*
  195. * Keem Bay PCIe Controller provides an additional IP logic on top of
  196. * standard DWC IP to clear MSI IRQ by writing '1' to the respective
  197. * bit of the status register.
  198. *
  199. * So, a chained irq handler is defined to handle this additional
  200. * IP logic.
  201. */
  202. chained_irq_enter(chip, desc);
  203. pp = &pcie->pci.pp;
  204. val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
  205. mask = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  206. status = val & mask;
  207. if (status & MSI_CTRL_INT) {
  208. dw_handle_msi_irq(pp);
  209. writel(status, pcie->apb_base + PCIE_REGS_INTERRUPT_STATUS);
  210. }
  211. chained_irq_exit(chip, desc);
  212. }
  213. static int keembay_pcie_setup_msi_irq(struct keembay_pcie *pcie)
  214. {
  215. struct dw_pcie *pci = &pcie->pci;
  216. struct device *dev = pci->dev;
  217. struct platform_device *pdev = to_platform_device(dev);
  218. int irq;
  219. irq = platform_get_irq_byname(pdev, "pcie");
  220. if (irq < 0)
  221. return irq;
  222. irq_set_chained_handler_and_data(irq, keembay_pcie_msi_irq_handler,
  223. pcie);
  224. return 0;
  225. }
  226. static void keembay_pcie_ep_init(struct dw_pcie_ep *ep)
  227. {
  228. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  229. struct keembay_pcie *pcie = dev_get_drvdata(pci->dev);
  230. writel(EDMA_INT_EN, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  231. }
  232. static int keembay_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  233. enum pci_epc_irq_type type,
  234. u16 interrupt_num)
  235. {
  236. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  237. switch (type) {
  238. case PCI_EPC_IRQ_LEGACY:
  239. /* Legacy interrupts are not supported in Keem Bay */
  240. dev_err(pci->dev, "Legacy IRQ is not supported\n");
  241. return -EINVAL;
  242. case PCI_EPC_IRQ_MSI:
  243. return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
  244. case PCI_EPC_IRQ_MSIX:
  245. return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
  246. default:
  247. dev_err(pci->dev, "Unknown IRQ type %d\n", type);
  248. return -EINVAL;
  249. }
  250. }
  251. static const struct pci_epc_features keembay_pcie_epc_features = {
  252. .linkup_notifier = false,
  253. .msi_capable = true,
  254. .msix_capable = true,
  255. .reserved_bar = BIT(BAR_1) | BIT(BAR_3) | BIT(BAR_5),
  256. .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
  257. .align = SZ_16K,
  258. };
  259. static const struct pci_epc_features *
  260. keembay_pcie_get_features(struct dw_pcie_ep *ep)
  261. {
  262. return &keembay_pcie_epc_features;
  263. }
  264. static const struct dw_pcie_ep_ops keembay_pcie_ep_ops = {
  265. .ep_init = keembay_pcie_ep_init,
  266. .raise_irq = keembay_pcie_ep_raise_irq,
  267. .get_features = keembay_pcie_get_features,
  268. };
  269. static const struct dw_pcie_host_ops keembay_pcie_host_ops = {
  270. };
  271. static int keembay_pcie_add_pcie_port(struct keembay_pcie *pcie,
  272. struct platform_device *pdev)
  273. {
  274. struct dw_pcie *pci = &pcie->pci;
  275. struct dw_pcie_rp *pp = &pci->pp;
  276. struct device *dev = &pdev->dev;
  277. u32 val;
  278. int ret;
  279. pp->ops = &keembay_pcie_host_ops;
  280. pp->msi_irq[0] = -ENODEV;
  281. ret = keembay_pcie_setup_msi_irq(pcie);
  282. if (ret)
  283. return ret;
  284. pcie->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  285. if (IS_ERR(pcie->reset))
  286. return PTR_ERR(pcie->reset);
  287. ret = keembay_pcie_probe_clocks(pcie);
  288. if (ret)
  289. return ret;
  290. val = readl(pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
  291. val |= PHY0_SRAM_BYPASS;
  292. writel(val, pcie->apb_base + PCIE_REGS_PCIE_PHY_CNTL);
  293. writel(PCIE_DEVICE_TYPE, pcie->apb_base + PCIE_REGS_PCIE_CFG);
  294. ret = keembay_pcie_pll_init(pcie);
  295. if (ret)
  296. return ret;
  297. val = readl(pcie->apb_base + PCIE_REGS_PCIE_CFG);
  298. writel(val | PCIE_RSTN, pcie->apb_base + PCIE_REGS_PCIE_CFG);
  299. keembay_ep_reset_deassert(pcie);
  300. ret = dw_pcie_host_init(pp);
  301. if (ret) {
  302. keembay_ep_reset_assert(pcie);
  303. dev_err(dev, "Failed to initialize host: %d\n", ret);
  304. return ret;
  305. }
  306. val = readl(pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  307. if (IS_ENABLED(CONFIG_PCI_MSI))
  308. val |= MSI_CTRL_INT_EN;
  309. writel(val, pcie->apb_base + PCIE_REGS_INTERRUPT_ENABLE);
  310. return 0;
  311. }
  312. static int keembay_pcie_probe(struct platform_device *pdev)
  313. {
  314. const struct keembay_pcie_of_data *data;
  315. struct device *dev = &pdev->dev;
  316. struct keembay_pcie *pcie;
  317. struct dw_pcie *pci;
  318. enum dw_pcie_device_mode mode;
  319. data = device_get_match_data(dev);
  320. if (!data)
  321. return -ENODEV;
  322. mode = (enum dw_pcie_device_mode)data->mode;
  323. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  324. if (!pcie)
  325. return -ENOMEM;
  326. pci = &pcie->pci;
  327. pci->dev = dev;
  328. pci->ops = &keembay_pcie_ops;
  329. pcie->mode = mode;
  330. pcie->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
  331. if (IS_ERR(pcie->apb_base))
  332. return PTR_ERR(pcie->apb_base);
  333. platform_set_drvdata(pdev, pcie);
  334. switch (pcie->mode) {
  335. case DW_PCIE_RC_TYPE:
  336. if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_HOST))
  337. return -ENODEV;
  338. return keembay_pcie_add_pcie_port(pcie, pdev);
  339. case DW_PCIE_EP_TYPE:
  340. if (!IS_ENABLED(CONFIG_PCIE_KEEMBAY_EP))
  341. return -ENODEV;
  342. pci->ep.ops = &keembay_pcie_ep_ops;
  343. return dw_pcie_ep_init(&pci->ep);
  344. default:
  345. dev_err(dev, "Invalid device type %d\n", pcie->mode);
  346. return -ENODEV;
  347. }
  348. }
  349. static const struct keembay_pcie_of_data keembay_pcie_rc_of_data = {
  350. .mode = DW_PCIE_RC_TYPE,
  351. };
  352. static const struct keembay_pcie_of_data keembay_pcie_ep_of_data = {
  353. .mode = DW_PCIE_EP_TYPE,
  354. };
  355. static const struct of_device_id keembay_pcie_of_match[] = {
  356. {
  357. .compatible = "intel,keembay-pcie",
  358. .data = &keembay_pcie_rc_of_data,
  359. },
  360. {
  361. .compatible = "intel,keembay-pcie-ep",
  362. .data = &keembay_pcie_ep_of_data,
  363. },
  364. {}
  365. };
  366. static struct platform_driver keembay_pcie_driver = {
  367. .driver = {
  368. .name = "keembay-pcie",
  369. .of_match_table = keembay_pcie_of_match,
  370. .suppress_bind_attrs = true,
  371. },
  372. .probe = keembay_pcie_probe,
  373. };
  374. builtin_platform_driver(keembay_pcie_driver);