pcie-kirin.c 19 KB

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