pcie-qcom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Qualcomm PCIe root complex driver
  3. *
  4. * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
  5. * Copyright 2015 Linaro Limited.
  6. *
  7. * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 and
  11. * only version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/iopoll.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/pci.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/reset.h>
  33. #include <linux/slab.h>
  34. #include <linux/types.h>
  35. #include "pcie-designware.h"
  36. #define PCIE20_PARF_PHY_CTRL 0x40
  37. #define PCIE20_PARF_PHY_REFCLK 0x4C
  38. #define PCIE20_PARF_DBI_BASE_ADDR 0x168
  39. #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16c
  40. #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
  41. #define PCIE20_ELBI_SYS_CTRL 0x04
  42. #define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
  43. #define PCIE20_CAP 0x70
  44. #define PERST_DELAY_US 1000
  45. struct qcom_pcie_resources_v0 {
  46. struct clk *iface_clk;
  47. struct clk *core_clk;
  48. struct clk *phy_clk;
  49. struct reset_control *pci_reset;
  50. struct reset_control *axi_reset;
  51. struct reset_control *ahb_reset;
  52. struct reset_control *por_reset;
  53. struct reset_control *phy_reset;
  54. struct regulator *vdda;
  55. struct regulator *vdda_phy;
  56. struct regulator *vdda_refclk;
  57. };
  58. struct qcom_pcie_resources_v1 {
  59. struct clk *iface;
  60. struct clk *aux;
  61. struct clk *master_bus;
  62. struct clk *slave_bus;
  63. struct reset_control *core;
  64. struct regulator *vdda;
  65. };
  66. union qcom_pcie_resources {
  67. struct qcom_pcie_resources_v0 v0;
  68. struct qcom_pcie_resources_v1 v1;
  69. };
  70. struct qcom_pcie;
  71. struct qcom_pcie_ops {
  72. int (*get_resources)(struct qcom_pcie *pcie);
  73. int (*init)(struct qcom_pcie *pcie);
  74. void (*deinit)(struct qcom_pcie *pcie);
  75. };
  76. struct qcom_pcie {
  77. struct pcie_port pp; /* pp.dbi_base is DT dbi */
  78. void __iomem *parf; /* DT parf */
  79. void __iomem *elbi; /* DT elbi */
  80. union qcom_pcie_resources res;
  81. struct phy *phy;
  82. struct gpio_desc *reset;
  83. struct qcom_pcie_ops *ops;
  84. };
  85. #define to_qcom_pcie(x) container_of(x, struct qcom_pcie, pp)
  86. static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
  87. {
  88. gpiod_set_value(pcie->reset, 1);
  89. usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
  90. }
  91. static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
  92. {
  93. gpiod_set_value(pcie->reset, 0);
  94. usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
  95. }
  96. static irqreturn_t qcom_pcie_msi_irq_handler(int irq, void *arg)
  97. {
  98. struct pcie_port *pp = arg;
  99. return dw_handle_msi_irq(pp);
  100. }
  101. static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
  102. {
  103. u32 val;
  104. if (dw_pcie_link_up(&pcie->pp))
  105. return 0;
  106. /* enable link training */
  107. val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
  108. val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
  109. writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
  110. return dw_pcie_wait_for_link(&pcie->pp);
  111. }
  112. static int qcom_pcie_get_resources_v0(struct qcom_pcie *pcie)
  113. {
  114. struct qcom_pcie_resources_v0 *res = &pcie->res.v0;
  115. struct device *dev = pcie->pp.dev;
  116. res->vdda = devm_regulator_get(dev, "vdda");
  117. if (IS_ERR(res->vdda))
  118. return PTR_ERR(res->vdda);
  119. res->vdda_phy = devm_regulator_get(dev, "vdda_phy");
  120. if (IS_ERR(res->vdda_phy))
  121. return PTR_ERR(res->vdda_phy);
  122. res->vdda_refclk = devm_regulator_get(dev, "vdda_refclk");
  123. if (IS_ERR(res->vdda_refclk))
  124. return PTR_ERR(res->vdda_refclk);
  125. res->iface_clk = devm_clk_get(dev, "iface");
  126. if (IS_ERR(res->iface_clk))
  127. return PTR_ERR(res->iface_clk);
  128. res->core_clk = devm_clk_get(dev, "core");
  129. if (IS_ERR(res->core_clk))
  130. return PTR_ERR(res->core_clk);
  131. res->phy_clk = devm_clk_get(dev, "phy");
  132. if (IS_ERR(res->phy_clk))
  133. return PTR_ERR(res->phy_clk);
  134. res->pci_reset = devm_reset_control_get(dev, "pci");
  135. if (IS_ERR(res->pci_reset))
  136. return PTR_ERR(res->pci_reset);
  137. res->axi_reset = devm_reset_control_get(dev, "axi");
  138. if (IS_ERR(res->axi_reset))
  139. return PTR_ERR(res->axi_reset);
  140. res->ahb_reset = devm_reset_control_get(dev, "ahb");
  141. if (IS_ERR(res->ahb_reset))
  142. return PTR_ERR(res->ahb_reset);
  143. res->por_reset = devm_reset_control_get(dev, "por");
  144. if (IS_ERR(res->por_reset))
  145. return PTR_ERR(res->por_reset);
  146. res->phy_reset = devm_reset_control_get(dev, "phy");
  147. if (IS_ERR(res->phy_reset))
  148. return PTR_ERR(res->phy_reset);
  149. return 0;
  150. }
  151. static int qcom_pcie_get_resources_v1(struct qcom_pcie *pcie)
  152. {
  153. struct qcom_pcie_resources_v1 *res = &pcie->res.v1;
  154. struct device *dev = pcie->pp.dev;
  155. res->vdda = devm_regulator_get(dev, "vdda");
  156. if (IS_ERR(res->vdda))
  157. return PTR_ERR(res->vdda);
  158. res->iface = devm_clk_get(dev, "iface");
  159. if (IS_ERR(res->iface))
  160. return PTR_ERR(res->iface);
  161. res->aux = devm_clk_get(dev, "aux");
  162. if (IS_ERR(res->aux))
  163. return PTR_ERR(res->aux);
  164. res->master_bus = devm_clk_get(dev, "master_bus");
  165. if (IS_ERR(res->master_bus))
  166. return PTR_ERR(res->master_bus);
  167. res->slave_bus = devm_clk_get(dev, "slave_bus");
  168. if (IS_ERR(res->slave_bus))
  169. return PTR_ERR(res->slave_bus);
  170. res->core = devm_reset_control_get(dev, "core");
  171. if (IS_ERR(res->core))
  172. return PTR_ERR(res->core);
  173. return 0;
  174. }
  175. static void qcom_pcie_deinit_v0(struct qcom_pcie *pcie)
  176. {
  177. struct qcom_pcie_resources_v0 *res = &pcie->res.v0;
  178. reset_control_assert(res->pci_reset);
  179. reset_control_assert(res->axi_reset);
  180. reset_control_assert(res->ahb_reset);
  181. reset_control_assert(res->por_reset);
  182. reset_control_assert(res->pci_reset);
  183. clk_disable_unprepare(res->iface_clk);
  184. clk_disable_unprepare(res->core_clk);
  185. clk_disable_unprepare(res->phy_clk);
  186. regulator_disable(res->vdda);
  187. regulator_disable(res->vdda_phy);
  188. regulator_disable(res->vdda_refclk);
  189. }
  190. static int qcom_pcie_init_v0(struct qcom_pcie *pcie)
  191. {
  192. struct qcom_pcie_resources_v0 *res = &pcie->res.v0;
  193. struct device *dev = pcie->pp.dev;
  194. u32 val;
  195. int ret;
  196. ret = regulator_enable(res->vdda);
  197. if (ret) {
  198. dev_err(dev, "cannot enable vdda regulator\n");
  199. return ret;
  200. }
  201. ret = regulator_enable(res->vdda_refclk);
  202. if (ret) {
  203. dev_err(dev, "cannot enable vdda_refclk regulator\n");
  204. goto err_refclk;
  205. }
  206. ret = regulator_enable(res->vdda_phy);
  207. if (ret) {
  208. dev_err(dev, "cannot enable vdda_phy regulator\n");
  209. goto err_vdda_phy;
  210. }
  211. ret = reset_control_assert(res->ahb_reset);
  212. if (ret) {
  213. dev_err(dev, "cannot assert ahb reset\n");
  214. goto err_assert_ahb;
  215. }
  216. ret = clk_prepare_enable(res->iface_clk);
  217. if (ret) {
  218. dev_err(dev, "cannot prepare/enable iface clock\n");
  219. goto err_assert_ahb;
  220. }
  221. ret = clk_prepare_enable(res->phy_clk);
  222. if (ret) {
  223. dev_err(dev, "cannot prepare/enable phy clock\n");
  224. goto err_clk_phy;
  225. }
  226. ret = clk_prepare_enable(res->core_clk);
  227. if (ret) {
  228. dev_err(dev, "cannot prepare/enable core clock\n");
  229. goto err_clk_core;
  230. }
  231. ret = reset_control_deassert(res->ahb_reset);
  232. if (ret) {
  233. dev_err(dev, "cannot deassert ahb reset\n");
  234. goto err_deassert_ahb;
  235. }
  236. /* enable PCIe clocks and resets */
  237. val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
  238. val &= ~BIT(0);
  239. writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
  240. /* enable external reference clock */
  241. val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
  242. val |= BIT(16);
  243. writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
  244. ret = reset_control_deassert(res->phy_reset);
  245. if (ret) {
  246. dev_err(dev, "cannot deassert phy reset\n");
  247. return ret;
  248. }
  249. ret = reset_control_deassert(res->pci_reset);
  250. if (ret) {
  251. dev_err(dev, "cannot deassert pci reset\n");
  252. return ret;
  253. }
  254. ret = reset_control_deassert(res->por_reset);
  255. if (ret) {
  256. dev_err(dev, "cannot deassert por reset\n");
  257. return ret;
  258. }
  259. ret = reset_control_deassert(res->axi_reset);
  260. if (ret) {
  261. dev_err(dev, "cannot deassert axi reset\n");
  262. return ret;
  263. }
  264. /* wait for clock acquisition */
  265. usleep_range(1000, 1500);
  266. return 0;
  267. err_deassert_ahb:
  268. clk_disable_unprepare(res->core_clk);
  269. err_clk_core:
  270. clk_disable_unprepare(res->phy_clk);
  271. err_clk_phy:
  272. clk_disable_unprepare(res->iface_clk);
  273. err_assert_ahb:
  274. regulator_disable(res->vdda_phy);
  275. err_vdda_phy:
  276. regulator_disable(res->vdda_refclk);
  277. err_refclk:
  278. regulator_disable(res->vdda);
  279. return ret;
  280. }
  281. static void qcom_pcie_deinit_v1(struct qcom_pcie *pcie)
  282. {
  283. struct qcom_pcie_resources_v1 *res = &pcie->res.v1;
  284. reset_control_assert(res->core);
  285. clk_disable_unprepare(res->slave_bus);
  286. clk_disable_unprepare(res->master_bus);
  287. clk_disable_unprepare(res->iface);
  288. clk_disable_unprepare(res->aux);
  289. regulator_disable(res->vdda);
  290. }
  291. static int qcom_pcie_init_v1(struct qcom_pcie *pcie)
  292. {
  293. struct qcom_pcie_resources_v1 *res = &pcie->res.v1;
  294. struct device *dev = pcie->pp.dev;
  295. int ret;
  296. ret = reset_control_deassert(res->core);
  297. if (ret) {
  298. dev_err(dev, "cannot deassert core reset\n");
  299. return ret;
  300. }
  301. ret = clk_prepare_enable(res->aux);
  302. if (ret) {
  303. dev_err(dev, "cannot prepare/enable aux clock\n");
  304. goto err_res;
  305. }
  306. ret = clk_prepare_enable(res->iface);
  307. if (ret) {
  308. dev_err(dev, "cannot prepare/enable iface clock\n");
  309. goto err_aux;
  310. }
  311. ret = clk_prepare_enable(res->master_bus);
  312. if (ret) {
  313. dev_err(dev, "cannot prepare/enable master_bus clock\n");
  314. goto err_iface;
  315. }
  316. ret = clk_prepare_enable(res->slave_bus);
  317. if (ret) {
  318. dev_err(dev, "cannot prepare/enable slave_bus clock\n");
  319. goto err_master;
  320. }
  321. ret = regulator_enable(res->vdda);
  322. if (ret) {
  323. dev_err(dev, "cannot enable vdda regulator\n");
  324. goto err_slave;
  325. }
  326. /* change DBI base address */
  327. writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
  328. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  329. u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
  330. val |= BIT(31);
  331. writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
  332. }
  333. return 0;
  334. err_slave:
  335. clk_disable_unprepare(res->slave_bus);
  336. err_master:
  337. clk_disable_unprepare(res->master_bus);
  338. err_iface:
  339. clk_disable_unprepare(res->iface);
  340. err_aux:
  341. clk_disable_unprepare(res->aux);
  342. err_res:
  343. reset_control_assert(res->core);
  344. return ret;
  345. }
  346. static int qcom_pcie_link_up(struct pcie_port *pp)
  347. {
  348. struct qcom_pcie *pcie = to_qcom_pcie(pp);
  349. u16 val = readw(pcie->pp.dbi_base + PCIE20_CAP + PCI_EXP_LNKSTA);
  350. return !!(val & PCI_EXP_LNKSTA_DLLLA);
  351. }
  352. static void qcom_pcie_host_init(struct pcie_port *pp)
  353. {
  354. struct qcom_pcie *pcie = to_qcom_pcie(pp);
  355. int ret;
  356. qcom_ep_reset_assert(pcie);
  357. ret = pcie->ops->init(pcie);
  358. if (ret)
  359. goto err_deinit;
  360. ret = phy_power_on(pcie->phy);
  361. if (ret)
  362. goto err_deinit;
  363. dw_pcie_setup_rc(pp);
  364. if (IS_ENABLED(CONFIG_PCI_MSI))
  365. dw_pcie_msi_init(pp);
  366. qcom_ep_reset_deassert(pcie);
  367. ret = qcom_pcie_establish_link(pcie);
  368. if (ret)
  369. goto err;
  370. return;
  371. err:
  372. qcom_ep_reset_assert(pcie);
  373. phy_power_off(pcie->phy);
  374. err_deinit:
  375. pcie->ops->deinit(pcie);
  376. }
  377. static int qcom_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  378. u32 *val)
  379. {
  380. /* the device class is not reported correctly from the register */
  381. if (where == PCI_CLASS_REVISION && size == 4) {
  382. *val = readl(pp->dbi_base + PCI_CLASS_REVISION);
  383. *val &= 0xff; /* keep revision id */
  384. *val |= PCI_CLASS_BRIDGE_PCI << 16;
  385. return PCIBIOS_SUCCESSFUL;
  386. }
  387. return dw_pcie_cfg_read(pp->dbi_base + where, size, val);
  388. }
  389. static struct pcie_host_ops qcom_pcie_dw_ops = {
  390. .link_up = qcom_pcie_link_up,
  391. .host_init = qcom_pcie_host_init,
  392. .rd_own_conf = qcom_pcie_rd_own_conf,
  393. };
  394. static const struct qcom_pcie_ops ops_v0 = {
  395. .get_resources = qcom_pcie_get_resources_v0,
  396. .init = qcom_pcie_init_v0,
  397. .deinit = qcom_pcie_deinit_v0,
  398. };
  399. static const struct qcom_pcie_ops ops_v1 = {
  400. .get_resources = qcom_pcie_get_resources_v1,
  401. .init = qcom_pcie_init_v1,
  402. .deinit = qcom_pcie_deinit_v1,
  403. };
  404. static int qcom_pcie_probe(struct platform_device *pdev)
  405. {
  406. struct device *dev = &pdev->dev;
  407. struct resource *res;
  408. struct qcom_pcie *pcie;
  409. struct pcie_port *pp;
  410. int ret;
  411. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  412. if (!pcie)
  413. return -ENOMEM;
  414. pp = &pcie->pp;
  415. pcie->ops = (struct qcom_pcie_ops *)of_device_get_match_data(dev);
  416. pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_LOW);
  417. if (IS_ERR(pcie->reset))
  418. return PTR_ERR(pcie->reset);
  419. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "parf");
  420. pcie->parf = devm_ioremap_resource(dev, res);
  421. if (IS_ERR(pcie->parf))
  422. return PTR_ERR(pcie->parf);
  423. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  424. pp->dbi_base = devm_ioremap_resource(dev, res);
  425. if (IS_ERR(pp->dbi_base))
  426. return PTR_ERR(pp->dbi_base);
  427. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "elbi");
  428. pcie->elbi = devm_ioremap_resource(dev, res);
  429. if (IS_ERR(pcie->elbi))
  430. return PTR_ERR(pcie->elbi);
  431. pcie->phy = devm_phy_optional_get(dev, "pciephy");
  432. if (IS_ERR(pcie->phy))
  433. return PTR_ERR(pcie->phy);
  434. pp->dev = dev;
  435. ret = pcie->ops->get_resources(pcie);
  436. if (ret)
  437. return ret;
  438. pp->root_bus_nr = -1;
  439. pp->ops = &qcom_pcie_dw_ops;
  440. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  441. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  442. if (pp->msi_irq < 0)
  443. return pp->msi_irq;
  444. ret = devm_request_irq(dev, pp->msi_irq,
  445. qcom_pcie_msi_irq_handler,
  446. IRQF_SHARED, "qcom-pcie-msi", pp);
  447. if (ret) {
  448. dev_err(dev, "cannot request msi irq\n");
  449. return ret;
  450. }
  451. }
  452. ret = phy_init(pcie->phy);
  453. if (ret)
  454. return ret;
  455. ret = dw_pcie_host_init(pp);
  456. if (ret) {
  457. dev_err(dev, "cannot initialize host\n");
  458. return ret;
  459. }
  460. return 0;
  461. }
  462. static const struct of_device_id qcom_pcie_match[] = {
  463. { .compatible = "qcom,pcie-ipq8064", .data = &ops_v0 },
  464. { .compatible = "qcom,pcie-apq8064", .data = &ops_v0 },
  465. { .compatible = "qcom,pcie-apq8084", .data = &ops_v1 },
  466. { }
  467. };
  468. static struct platform_driver qcom_pcie_driver = {
  469. .probe = qcom_pcie_probe,
  470. .driver = {
  471. .name = "qcom-pcie",
  472. .suppress_bind_attrs = true,
  473. .of_match_table = qcom_pcie_match,
  474. },
  475. };
  476. builtin_platform_driver(qcom_pcie_driver);