pcie-kirin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Kirin Phone SoCs
  4. *
  5. * Copyright (C) 2017 HiSilicon Electronics Co., Ltd.
  6. * https://www.huawei.com
  7. *
  8. * Author: Xiaowei Song <songxiaowei@huawei.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/compiler.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/of.h>
  18. #include <linux/of_pci.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/pci.h>
  21. #include <linux/pci_regs.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/resource.h>
  25. #include <linux/types.h>
  26. #include "pcie-designware.h"
  27. #define to_kirin_pcie(x) dev_get_drvdata((x)->dev)
  28. /* PCIe ELBI registers */
  29. #define SOC_PCIECTRL_CTRL0_ADDR 0x000
  30. #define SOC_PCIECTRL_CTRL1_ADDR 0x004
  31. #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
  32. /* info located in APB */
  33. #define PCIE_APP_LTSSM_ENABLE 0x01c
  34. #define PCIE_APB_PHY_STATUS0 0x400
  35. #define PCIE_LINKUP_ENABLE (0x8020)
  36. #define PCIE_LTSSM_ENABLE_BIT (0x1 << 11)
  37. /* info located in sysctrl */
  38. #define SCTRL_PCIE_CMOS_OFFSET 0x60
  39. #define SCTRL_PCIE_CMOS_BIT 0x10
  40. #define SCTRL_PCIE_ISO_OFFSET 0x44
  41. #define SCTRL_PCIE_ISO_BIT 0x30
  42. #define SCTRL_PCIE_HPCLK_OFFSET 0x190
  43. #define SCTRL_PCIE_HPCLK_BIT 0x184000
  44. #define SCTRL_PCIE_OE_OFFSET 0x14a
  45. #define PCIE_DEBOUNCE_PARAM 0xF0F400
  46. #define PCIE_OE_BYPASS (0x3 << 28)
  47. /*
  48. * Max number of connected PCI slots at an external PCI bridge
  49. *
  50. * This is used on HiKey 970, which has a PEX 8606 bridge with 4 connected
  51. * lanes (lane 0 upstream, and the other three lanes, one connected to an
  52. * in-board Ethernet adapter and the other two connected to M.2 and mini
  53. * PCI slots.
  54. *
  55. * Each slot has a different clock source and uses a separate PERST# pin.
  56. */
  57. #define MAX_PCI_SLOTS 3
  58. enum pcie_kirin_phy_type {
  59. PCIE_KIRIN_INTERNAL_PHY,
  60. PCIE_KIRIN_EXTERNAL_PHY
  61. };
  62. struct kirin_pcie {
  63. enum pcie_kirin_phy_type type;
  64. struct dw_pcie *pci;
  65. struct regmap *apb;
  66. struct phy *phy;
  67. void *phy_priv; /* only for PCIE_KIRIN_INTERNAL_PHY */
  68. /* DWC PERST# */
  69. struct gpio_desc *id_dwc_perst_gpio;
  70. /* Per-slot PERST# */
  71. int num_slots;
  72. struct gpio_desc *id_reset_gpio[MAX_PCI_SLOTS];
  73. const char *reset_names[MAX_PCI_SLOTS];
  74. /* Per-slot clkreq */
  75. int n_gpio_clkreq;
  76. struct gpio_desc *id_clkreq_gpio[MAX_PCI_SLOTS];
  77. const char *clkreq_names[MAX_PCI_SLOTS];
  78. };
  79. /*
  80. * Kirin 960 PHY. Can't be split into a PHY driver without changing the
  81. * DT schema.
  82. */
  83. #define REF_CLK_FREQ 100000000
  84. /* PHY info located in APB */
  85. #define PCIE_APB_PHY_CTRL0 0x0
  86. #define PCIE_APB_PHY_CTRL1 0x4
  87. #define PCIE_APB_PHY_STATUS0 0x400
  88. #define PIPE_CLK_STABLE BIT(19)
  89. #define PHY_REF_PAD_BIT BIT(8)
  90. #define PHY_PWR_DOWN_BIT BIT(22)
  91. #define PHY_RST_ACK_BIT BIT(16)
  92. /* peri_crg ctrl */
  93. #define CRGCTRL_PCIE_ASSERT_OFFSET 0x88
  94. #define CRGCTRL_PCIE_ASSERT_BIT 0x8c000000
  95. /* Time for delay */
  96. #define REF_2_PERST_MIN 21000
  97. #define REF_2_PERST_MAX 25000
  98. #define PERST_2_ACCESS_MIN 10000
  99. #define PERST_2_ACCESS_MAX 12000
  100. #define PIPE_CLK_WAIT_MIN 550
  101. #define PIPE_CLK_WAIT_MAX 600
  102. #define TIME_CMOS_MIN 100
  103. #define TIME_CMOS_MAX 105
  104. #define TIME_PHY_PD_MIN 10
  105. #define TIME_PHY_PD_MAX 11
  106. struct hi3660_pcie_phy {
  107. struct device *dev;
  108. void __iomem *base;
  109. struct regmap *crgctrl;
  110. struct regmap *sysctrl;
  111. struct clk *apb_sys_clk;
  112. struct clk *apb_phy_clk;
  113. struct clk *phy_ref_clk;
  114. struct clk *aclk;
  115. struct clk *aux_clk;
  116. };
  117. /* Registers in PCIePHY */
  118. static inline void kirin_apb_phy_writel(struct hi3660_pcie_phy *hi3660_pcie_phy,
  119. u32 val, u32 reg)
  120. {
  121. writel(val, hi3660_pcie_phy->base + reg);
  122. }
  123. static inline u32 kirin_apb_phy_readl(struct hi3660_pcie_phy *hi3660_pcie_phy,
  124. u32 reg)
  125. {
  126. return readl(hi3660_pcie_phy->base + reg);
  127. }
  128. static int hi3660_pcie_phy_get_clk(struct hi3660_pcie_phy *phy)
  129. {
  130. struct device *dev = phy->dev;
  131. phy->phy_ref_clk = devm_clk_get(dev, "pcie_phy_ref");
  132. if (IS_ERR(phy->phy_ref_clk))
  133. return PTR_ERR(phy->phy_ref_clk);
  134. phy->aux_clk = devm_clk_get(dev, "pcie_aux");
  135. if (IS_ERR(phy->aux_clk))
  136. return PTR_ERR(phy->aux_clk);
  137. phy->apb_phy_clk = devm_clk_get(dev, "pcie_apb_phy");
  138. if (IS_ERR(phy->apb_phy_clk))
  139. return PTR_ERR(phy->apb_phy_clk);
  140. phy->apb_sys_clk = devm_clk_get(dev, "pcie_apb_sys");
  141. if (IS_ERR(phy->apb_sys_clk))
  142. return PTR_ERR(phy->apb_sys_clk);
  143. phy->aclk = devm_clk_get(dev, "pcie_aclk");
  144. if (IS_ERR(phy->aclk))
  145. return PTR_ERR(phy->aclk);
  146. return 0;
  147. }
  148. static int hi3660_pcie_phy_get_resource(struct hi3660_pcie_phy *phy)
  149. {
  150. struct device *dev = phy->dev;
  151. struct platform_device *pdev;
  152. /* registers */
  153. pdev = container_of(dev, struct platform_device, dev);
  154. phy->base = devm_platform_ioremap_resource_byname(pdev, "phy");
  155. if (IS_ERR(phy->base))
  156. return PTR_ERR(phy->base);
  157. phy->crgctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-crgctrl");
  158. if (IS_ERR(phy->crgctrl))
  159. return PTR_ERR(phy->crgctrl);
  160. phy->sysctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-sctrl");
  161. if (IS_ERR(phy->sysctrl))
  162. return PTR_ERR(phy->sysctrl);
  163. return 0;
  164. }
  165. static int hi3660_pcie_phy_start(struct hi3660_pcie_phy *phy)
  166. {
  167. struct device *dev = phy->dev;
  168. u32 reg_val;
  169. reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1);
  170. reg_val &= ~PHY_REF_PAD_BIT;
  171. kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1);
  172. reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL0);
  173. reg_val &= ~PHY_PWR_DOWN_BIT;
  174. kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL0);
  175. usleep_range(TIME_PHY_PD_MIN, TIME_PHY_PD_MAX);
  176. reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1);
  177. reg_val &= ~PHY_RST_ACK_BIT;
  178. kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1);
  179. usleep_range(PIPE_CLK_WAIT_MIN, PIPE_CLK_WAIT_MAX);
  180. reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_STATUS0);
  181. if (reg_val & PIPE_CLK_STABLE)
  182. return dev_err_probe(dev, -ETIMEDOUT,
  183. "PIPE clk is not stable\n");
  184. return 0;
  185. }
  186. static void hi3660_pcie_phy_oe_enable(struct hi3660_pcie_phy *phy)
  187. {
  188. u32 val;
  189. regmap_read(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, &val);
  190. val |= PCIE_DEBOUNCE_PARAM;
  191. val &= ~PCIE_OE_BYPASS;
  192. regmap_write(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, val);
  193. }
  194. static int hi3660_pcie_phy_clk_ctrl(struct hi3660_pcie_phy *phy, bool enable)
  195. {
  196. int ret = 0;
  197. if (!enable)
  198. goto close_clk;
  199. ret = clk_set_rate(phy->phy_ref_clk, REF_CLK_FREQ);
  200. if (ret)
  201. return ret;
  202. ret = clk_prepare_enable(phy->phy_ref_clk);
  203. if (ret)
  204. return ret;
  205. ret = clk_prepare_enable(phy->apb_sys_clk);
  206. if (ret)
  207. goto apb_sys_fail;
  208. ret = clk_prepare_enable(phy->apb_phy_clk);
  209. if (ret)
  210. goto apb_phy_fail;
  211. ret = clk_prepare_enable(phy->aclk);
  212. if (ret)
  213. goto aclk_fail;
  214. ret = clk_prepare_enable(phy->aux_clk);
  215. if (ret)
  216. goto aux_clk_fail;
  217. return 0;
  218. close_clk:
  219. clk_disable_unprepare(phy->aux_clk);
  220. aux_clk_fail:
  221. clk_disable_unprepare(phy->aclk);
  222. aclk_fail:
  223. clk_disable_unprepare(phy->apb_phy_clk);
  224. apb_phy_fail:
  225. clk_disable_unprepare(phy->apb_sys_clk);
  226. apb_sys_fail:
  227. clk_disable_unprepare(phy->phy_ref_clk);
  228. return ret;
  229. }
  230. static int hi3660_pcie_phy_power_on(struct kirin_pcie *pcie)
  231. {
  232. struct hi3660_pcie_phy *phy = pcie->phy_priv;
  233. int ret;
  234. /* Power supply for Host */
  235. regmap_write(phy->sysctrl,
  236. SCTRL_PCIE_CMOS_OFFSET, SCTRL_PCIE_CMOS_BIT);
  237. usleep_range(TIME_CMOS_MIN, TIME_CMOS_MAX);
  238. hi3660_pcie_phy_oe_enable(phy);
  239. ret = hi3660_pcie_phy_clk_ctrl(phy, true);
  240. if (ret)
  241. return ret;
  242. /* ISO disable, PCIeCtrl, PHY assert and clk gate clear */
  243. regmap_write(phy->sysctrl,
  244. SCTRL_PCIE_ISO_OFFSET, SCTRL_PCIE_ISO_BIT);
  245. regmap_write(phy->crgctrl,
  246. CRGCTRL_PCIE_ASSERT_OFFSET, CRGCTRL_PCIE_ASSERT_BIT);
  247. regmap_write(phy->sysctrl,
  248. SCTRL_PCIE_HPCLK_OFFSET, SCTRL_PCIE_HPCLK_BIT);
  249. ret = hi3660_pcie_phy_start(phy);
  250. if (ret)
  251. goto disable_clks;
  252. return 0;
  253. disable_clks:
  254. hi3660_pcie_phy_clk_ctrl(phy, false);
  255. return ret;
  256. }
  257. static int hi3660_pcie_phy_init(struct platform_device *pdev,
  258. struct kirin_pcie *pcie)
  259. {
  260. struct device *dev = &pdev->dev;
  261. struct hi3660_pcie_phy *phy;
  262. int ret;
  263. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  264. if (!phy)
  265. return -ENOMEM;
  266. pcie->phy_priv = phy;
  267. phy->dev = dev;
  268. ret = hi3660_pcie_phy_get_clk(phy);
  269. if (ret)
  270. return ret;
  271. return hi3660_pcie_phy_get_resource(phy);
  272. }
  273. static int hi3660_pcie_phy_power_off(struct kirin_pcie *pcie)
  274. {
  275. struct hi3660_pcie_phy *phy = pcie->phy_priv;
  276. /* Drop power supply for Host */
  277. regmap_write(phy->sysctrl, SCTRL_PCIE_CMOS_OFFSET, 0x00);
  278. hi3660_pcie_phy_clk_ctrl(phy, false);
  279. return 0;
  280. }
  281. /*
  282. * The non-PHY part starts here
  283. */
  284. static const struct regmap_config pcie_kirin_regmap_conf = {
  285. .name = "kirin_pcie_apb",
  286. .reg_bits = 32,
  287. .val_bits = 32,
  288. .reg_stride = 4,
  289. };
  290. static int kirin_pcie_get_gpio_enable(struct kirin_pcie *pcie,
  291. struct platform_device *pdev)
  292. {
  293. struct device *dev = &pdev->dev;
  294. int ret, i;
  295. /* This is an optional property */
  296. ret = gpiod_count(dev, "hisilicon,clken");
  297. if (ret < 0)
  298. return 0;
  299. if (ret > MAX_PCI_SLOTS)
  300. return dev_err_probe(dev, -EINVAL,
  301. "Too many GPIO clock requests!\n");
  302. pcie->n_gpio_clkreq = ret;
  303. for (i = 0; i < pcie->n_gpio_clkreq; i++) {
  304. pcie->id_clkreq_gpio[i] = devm_gpiod_get_index(dev,
  305. "hisilicon,clken", i,
  306. GPIOD_OUT_LOW);
  307. if (IS_ERR(pcie->id_clkreq_gpio[i]))
  308. return dev_err_probe(dev, PTR_ERR(pcie->id_clkreq_gpio[i]),
  309. "unable to get a valid clken gpio\n");
  310. pcie->clkreq_names[i] = devm_kasprintf(dev, GFP_KERNEL,
  311. "pcie_clkreq_%d", i);
  312. if (!pcie->clkreq_names[i])
  313. return -ENOMEM;
  314. gpiod_set_consumer_name(pcie->id_clkreq_gpio[i],
  315. pcie->clkreq_names[i]);
  316. }
  317. return 0;
  318. }
  319. static int kirin_pcie_parse_port(struct kirin_pcie *pcie,
  320. struct platform_device *pdev,
  321. struct device_node *node)
  322. {
  323. struct device *dev = &pdev->dev;
  324. int ret, slot, i;
  325. for_each_available_child_of_node_scoped(node, parent) {
  326. for_each_available_child_of_node_scoped(parent, child) {
  327. i = pcie->num_slots;
  328. pcie->id_reset_gpio[i] = devm_fwnode_gpiod_get_index(dev,
  329. of_fwnode_handle(child),
  330. "reset", 0, GPIOD_OUT_LOW,
  331. NULL);
  332. if (IS_ERR(pcie->id_reset_gpio[i])) {
  333. if (PTR_ERR(pcie->id_reset_gpio[i]) == -ENOENT)
  334. continue;
  335. return dev_err_probe(dev, PTR_ERR(pcie->id_reset_gpio[i]),
  336. "unable to get a valid reset gpio\n");
  337. }
  338. if (pcie->num_slots + 1 >= MAX_PCI_SLOTS)
  339. return dev_err_probe(dev, -EINVAL,
  340. "Too many PCI slots!\n");
  341. pcie->num_slots++;
  342. ret = of_pci_get_devfn(child);
  343. if (ret < 0)
  344. return dev_err_probe(dev, ret,
  345. "failed to parse devfn\n");
  346. slot = PCI_SLOT(ret);
  347. pcie->reset_names[i] = devm_kasprintf(dev, GFP_KERNEL,
  348. "pcie_perst_%d",
  349. slot);
  350. if (!pcie->reset_names[i])
  351. return -ENOMEM;
  352. gpiod_set_consumer_name(pcie->id_reset_gpio[i],
  353. pcie->reset_names[i]);
  354. }
  355. }
  356. return 0;
  357. }
  358. static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
  359. struct platform_device *pdev)
  360. {
  361. struct device *dev = &pdev->dev;
  362. struct device_node *node = dev->of_node;
  363. void __iomem *apb_base;
  364. int ret;
  365. apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
  366. if (IS_ERR(apb_base))
  367. return PTR_ERR(apb_base);
  368. kirin_pcie->apb = devm_regmap_init_mmio(dev, apb_base,
  369. &pcie_kirin_regmap_conf);
  370. if (IS_ERR(kirin_pcie->apb))
  371. return PTR_ERR(kirin_pcie->apb);
  372. /* pcie internal PERST# gpio */
  373. kirin_pcie->id_dwc_perst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  374. if (IS_ERR(kirin_pcie->id_dwc_perst_gpio))
  375. return dev_err_probe(dev, PTR_ERR(kirin_pcie->id_dwc_perst_gpio),
  376. "unable to get a valid gpio pin\n");
  377. gpiod_set_consumer_name(kirin_pcie->id_dwc_perst_gpio, "pcie_perst_bridge");
  378. ret = kirin_pcie_get_gpio_enable(kirin_pcie, pdev);
  379. if (ret)
  380. return ret;
  381. /* Parse OF children */
  382. for_each_available_child_of_node_scoped(node, child) {
  383. ret = kirin_pcie_parse_port(kirin_pcie, pdev, child);
  384. if (ret)
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static void kirin_pcie_sideband_dbi_w_mode(struct kirin_pcie *kirin_pcie,
  390. bool on)
  391. {
  392. u32 val;
  393. regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, &val);
  394. if (on)
  395. val = val | PCIE_ELBI_SLV_DBI_ENABLE;
  396. else
  397. val = val & ~PCIE_ELBI_SLV_DBI_ENABLE;
  398. regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, val);
  399. }
  400. static void kirin_pcie_sideband_dbi_r_mode(struct kirin_pcie *kirin_pcie,
  401. bool on)
  402. {
  403. u32 val;
  404. regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, &val);
  405. if (on)
  406. val = val | PCIE_ELBI_SLV_DBI_ENABLE;
  407. else
  408. val = val & ~PCIE_ELBI_SLV_DBI_ENABLE;
  409. regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, val);
  410. }
  411. static int kirin_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
  412. int where, int size, u32 *val)
  413. {
  414. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  415. if (PCI_SLOT(devfn))
  416. return PCIBIOS_DEVICE_NOT_FOUND;
  417. *val = dw_pcie_read_dbi(pci, where, size);
  418. return PCIBIOS_SUCCESSFUL;
  419. }
  420. static int kirin_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
  421. int where, int size, u32 val)
  422. {
  423. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  424. if (PCI_SLOT(devfn))
  425. return PCIBIOS_DEVICE_NOT_FOUND;
  426. dw_pcie_write_dbi(pci, where, size, val);
  427. return PCIBIOS_SUCCESSFUL;
  428. }
  429. static int kirin_pcie_add_bus(struct pci_bus *bus)
  430. {
  431. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  432. struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
  433. int i, ret;
  434. if (!kirin_pcie->num_slots)
  435. return 0;
  436. /* Send PERST# to each slot */
  437. for (i = 0; i < kirin_pcie->num_slots; i++) {
  438. ret = gpiod_direction_output_raw(kirin_pcie->id_reset_gpio[i], 1);
  439. if (ret) {
  440. dev_err(pci->dev, "PERST# %s error: %d\n",
  441. kirin_pcie->reset_names[i], ret);
  442. }
  443. }
  444. usleep_range(PERST_2_ACCESS_MIN, PERST_2_ACCESS_MAX);
  445. return 0;
  446. }
  447. static struct pci_ops kirin_pci_ops = {
  448. .read = kirin_pcie_rd_own_conf,
  449. .write = kirin_pcie_wr_own_conf,
  450. .add_bus = kirin_pcie_add_bus,
  451. };
  452. static u32 kirin_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  453. u32 reg, size_t size)
  454. {
  455. struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
  456. u32 ret;
  457. kirin_pcie_sideband_dbi_r_mode(kirin_pcie, true);
  458. dw_pcie_read(base + reg, size, &ret);
  459. kirin_pcie_sideband_dbi_r_mode(kirin_pcie, false);
  460. return ret;
  461. }
  462. static void kirin_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  463. u32 reg, size_t size, u32 val)
  464. {
  465. struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
  466. kirin_pcie_sideband_dbi_w_mode(kirin_pcie, true);
  467. dw_pcie_write(base + reg, size, val);
  468. kirin_pcie_sideband_dbi_w_mode(kirin_pcie, false);
  469. }
  470. static bool kirin_pcie_link_up(struct dw_pcie *pci)
  471. {
  472. struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
  473. u32 val;
  474. regmap_read(kirin_pcie->apb, PCIE_APB_PHY_STATUS0, &val);
  475. return (val & PCIE_LINKUP_ENABLE) == PCIE_LINKUP_ENABLE;
  476. }
  477. static int kirin_pcie_start_link(struct dw_pcie *pci)
  478. {
  479. struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
  480. /* assert LTSSM enable */
  481. regmap_write(kirin_pcie->apb, PCIE_APP_LTSSM_ENABLE,
  482. PCIE_LTSSM_ENABLE_BIT);
  483. return 0;
  484. }
  485. static int kirin_pcie_host_init(struct dw_pcie_rp *pp)
  486. {
  487. pp->bridge->ops = &kirin_pci_ops;
  488. return 0;
  489. }
  490. static const struct dw_pcie_ops kirin_dw_pcie_ops = {
  491. .read_dbi = kirin_pcie_read_dbi,
  492. .write_dbi = kirin_pcie_write_dbi,
  493. .link_up = kirin_pcie_link_up,
  494. .start_link = kirin_pcie_start_link,
  495. };
  496. static const struct dw_pcie_host_ops kirin_pcie_host_ops = {
  497. .init = kirin_pcie_host_init,
  498. };
  499. static int kirin_pcie_power_off(struct kirin_pcie *kirin_pcie)
  500. {
  501. int i;
  502. if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY)
  503. return hi3660_pcie_phy_power_off(kirin_pcie);
  504. for (i = 0; i < kirin_pcie->n_gpio_clkreq; i++)
  505. gpiod_direction_output_raw(kirin_pcie->id_clkreq_gpio[i], 1);
  506. phy_power_off(kirin_pcie->phy);
  507. phy_exit(kirin_pcie->phy);
  508. return 0;
  509. }
  510. static int kirin_pcie_power_on(struct platform_device *pdev,
  511. struct kirin_pcie *kirin_pcie)
  512. {
  513. struct device *dev = &pdev->dev;
  514. int ret;
  515. if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY) {
  516. ret = hi3660_pcie_phy_init(pdev, kirin_pcie);
  517. if (ret)
  518. return ret;
  519. ret = hi3660_pcie_phy_power_on(kirin_pcie);
  520. if (ret)
  521. return ret;
  522. } else {
  523. kirin_pcie->phy = devm_of_phy_get(dev, dev->of_node, NULL);
  524. if (IS_ERR(kirin_pcie->phy))
  525. return PTR_ERR(kirin_pcie->phy);
  526. ret = phy_init(kirin_pcie->phy);
  527. if (ret)
  528. goto err;
  529. ret = phy_power_on(kirin_pcie->phy);
  530. if (ret)
  531. goto err;
  532. }
  533. /* perst assert Endpoint */
  534. usleep_range(REF_2_PERST_MIN, REF_2_PERST_MAX);
  535. ret = gpiod_direction_output_raw(kirin_pcie->id_dwc_perst_gpio, 1);
  536. if (ret)
  537. goto err;
  538. usleep_range(PERST_2_ACCESS_MIN, PERST_2_ACCESS_MAX);
  539. return 0;
  540. err:
  541. kirin_pcie_power_off(kirin_pcie);
  542. return ret;
  543. }
  544. static void kirin_pcie_remove(struct platform_device *pdev)
  545. {
  546. struct kirin_pcie *kirin_pcie = platform_get_drvdata(pdev);
  547. dw_pcie_host_deinit(&kirin_pcie->pci->pp);
  548. kirin_pcie_power_off(kirin_pcie);
  549. }
  550. struct kirin_pcie_data {
  551. enum pcie_kirin_phy_type phy_type;
  552. };
  553. static const struct kirin_pcie_data kirin_960_data = {
  554. .phy_type = PCIE_KIRIN_INTERNAL_PHY,
  555. };
  556. static const struct kirin_pcie_data kirin_970_data = {
  557. .phy_type = PCIE_KIRIN_EXTERNAL_PHY,
  558. };
  559. static const struct of_device_id kirin_pcie_match[] = {
  560. { .compatible = "hisilicon,kirin960-pcie", .data = &kirin_960_data },
  561. { .compatible = "hisilicon,kirin970-pcie", .data = &kirin_970_data },
  562. {},
  563. };
  564. static int kirin_pcie_probe(struct platform_device *pdev)
  565. {
  566. struct device *dev = &pdev->dev;
  567. const struct kirin_pcie_data *data;
  568. struct kirin_pcie *kirin_pcie;
  569. struct dw_pcie *pci;
  570. int ret;
  571. data = of_device_get_match_data(dev);
  572. if (!data)
  573. return dev_err_probe(dev, -EINVAL, "OF data missing\n");
  574. kirin_pcie = devm_kzalloc(dev, sizeof(struct kirin_pcie), GFP_KERNEL);
  575. if (!kirin_pcie)
  576. return -ENOMEM;
  577. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  578. if (!pci)
  579. return -ENOMEM;
  580. pci->dev = dev;
  581. pci->ops = &kirin_dw_pcie_ops;
  582. pci->pp.ops = &kirin_pcie_host_ops;
  583. kirin_pcie->pci = pci;
  584. kirin_pcie->type = data->phy_type;
  585. ret = kirin_pcie_get_resource(kirin_pcie, pdev);
  586. if (ret)
  587. return ret;
  588. platform_set_drvdata(pdev, kirin_pcie);
  589. ret = kirin_pcie_power_on(pdev, kirin_pcie);
  590. if (ret)
  591. return ret;
  592. return dw_pcie_host_init(&pci->pp);
  593. }
  594. static struct platform_driver kirin_pcie_driver = {
  595. .probe = kirin_pcie_probe,
  596. .remove = kirin_pcie_remove,
  597. .driver = {
  598. .name = "kirin-pcie",
  599. .of_match_table = kirin_pcie_match,
  600. .suppress_bind_attrs = true,
  601. },
  602. };
  603. module_platform_driver(kirin_pcie_driver);
  604. MODULE_DEVICE_TABLE(of, kirin_pcie_match);
  605. MODULE_DESCRIPTION("PCIe host controller driver for Kirin Phone SoCs");
  606. MODULE_AUTHOR("Xiaowei Song <songxiaowei@huawei.com>");
  607. MODULE_LICENSE("GPL v2");