phy-spacemit-k1-pcie.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SpacemiT K1 PCIe and PCIe/USB 3 combo PHY driver
  4. *
  5. * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reserved.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <dt-bindings/phy/phy.h>
  19. /*
  20. * Three PCIe ports are supported in the SpacemiT K1 SoC, and this driver
  21. * supports their PHYs.
  22. *
  23. * The PHY for PCIe port A is different from the PHYs for ports B and C:
  24. * - It has one lane, while ports B and C have two
  25. * - It is a combo PHY can be used for PCIe or USB 3
  26. * - It can automatically calibrate PCIe TX and RX termination settings
  27. *
  28. * The PHY functionality for PCIe ports B and C is identical:
  29. * - They have two PCIe lanes (but can be restricted to 1 via device tree)
  30. * - They are used for PCIe only
  31. * - They are configured using TX and RX values computed for port A
  32. *
  33. * A given board is designed to use the combo PHY for either PCIe or USB 3.
  34. * Whether the combo PHY is configured for PCIe or USB 3 is specified in
  35. * device tree using a phandle plus an argument. The argument indicates
  36. * the type (either PHY_TYPE_PCIE or PHY_TYPE_USB3).
  37. *
  38. * Each PHY has a reset that it gets and deasserts during initialization.
  39. * Each depends also on other clocks and resets provided by the controller
  40. * hardware (PCIe or USB) it is associated with. The controller drivers
  41. * are required to enable any clocks and de-assert any resets that affect
  42. * PHY operation. In addition each PHY implements an internal PLL, driven
  43. * by an external (24 MHz) oscillator.
  44. *
  45. * PCIe PHYs must be programmed with RX and TX calibration values. The
  46. * combo PHY is the only one that can determine these values. They are
  47. * determined by temporarily enabling the combo PHY in PCIe mode at probe
  48. * time (if necessary). This calibration only needs to be done once, and
  49. * when it has completed the TX and RX values are saved.
  50. *
  51. * To allow the combo PHY to be enabled for calibration, the resets and
  52. * clocks it uses in PCIe mode must be supplied.
  53. */
  54. struct k1_pcie_phy {
  55. struct device *dev; /* PHY provider device */
  56. struct phy *phy;
  57. void __iomem *regs;
  58. u32 pcie_lanes; /* Max (1 or 2) unless limited by DT */
  59. struct clk *pll;
  60. struct clk_hw pll_hw; /* Private PLL clock */
  61. /* The remaining fields are only used for the combo PHY */
  62. u32 type; /* PHY_TYPE_PCIE or PHY_TYPE_USB3 */
  63. struct regmap *pmu; /* MMIO regmap (no errors) */
  64. };
  65. #define CALIBRATION_TIMEOUT 500000 /* For combo PHY (usec) */
  66. #define PLL_TIMEOUT 500000 /* For PHY PLL lock (usec) */
  67. #define POLL_DELAY 500 /* Time between polls (usec) */
  68. /* Selecting the combo PHY operating mode requires APMU regmap access */
  69. #define SYSCON_APMU "spacemit,apmu"
  70. /* PMU space, for selecting between PCIe and USB 3 mode (combo PHY only) */
  71. #define PMUA_USB_PHY_CTRL0 0x0110
  72. #define COMBO_PHY_SEL BIT(3) /* 0: PCIe; 1: USB 3 */
  73. #define PCIE_CLK_RES_CTRL 0x03cc
  74. #define PCIE_APP_HOLD_PHY_RST BIT(30)
  75. /* PHY register space */
  76. /* Offset between lane 0 and lane 1 registers when there are two */
  77. #define PHY_LANE_OFFSET 0x0400
  78. /* PHY PLL configuration */
  79. #define PCIE_PU_ADDR_CLK_CFG 0x0008
  80. #define PLL_READY BIT(0) /* read-only */
  81. #define CFG_INTERNAL_TIMER_ADJ GENMASK(10, 7)
  82. #define TIMER_ADJ_USB 0x2
  83. #define TIMER_ADJ_PCIE 0x6
  84. #define CFG_SW_PHY_INIT_DONE BIT(11) /* We set after PLL config */
  85. #define PCIE_RC_DONE_STATUS 0x0018
  86. #define CFG_FORCE_RCV_RETRY BIT(10) /* Used for PCIe */
  87. /* PCIe PHY lane calibration; assumes 24MHz input clock */
  88. #define PCIE_RC_CAL_REG2 0x0020
  89. #define RC_CAL_TOGGLE BIT(22)
  90. #define CLKSEL GENMASK(31, 29)
  91. #define CLKSEL_24M 0x3
  92. /* Additional PHY PLL configuration (USB 3 and PCIe) */
  93. #define PCIE_PU_PLL_1 0x0048
  94. #define REF_100_WSSC BIT(12) /* 1: input is 100MHz, SSC */
  95. #define FREF_SEL GENMASK(15, 13)
  96. #define FREF_24M 0x1
  97. #define SSC_DEP_SEL GENMASK(19, 16)
  98. #define SSC_DEP_NONE 0x0
  99. #define SSC_DEP_5000PPM 0xa
  100. /* PCIe PHY configuration */
  101. #define PCIE_PU_PLL_2 0x004c
  102. #define GEN_REF100 BIT(4) /* 1: generate 100MHz clk */
  103. #define PCIE_RX_REG1 0x0050
  104. #define EN_RTERM BIT(3)
  105. #define AFE_RTERM_REG GENMASK(11, 8)
  106. #define PCIE_RX_REG2 0x0054
  107. #define RX_RTERM_SEL BIT(5) /* 0: use AFE_RTERM_REG value */
  108. #define PCIE_LTSSM_DIS_ENTRY 0x005c
  109. #define CFG_REFCLK_MODE GENMASK(9, 8)
  110. #define RFCLK_MODE_DRIVER 0x1
  111. #define OVRD_REFCLK_MODE BIT(10) /* 1: use CFG_RFCLK_MODE */
  112. #define PCIE_TX_REG1 0x0064
  113. #define TX_RTERM_REG GENMASK(15, 12)
  114. #define TX_RTERM_SEL BIT(25) /* 1: use TX_RTERM_REG */
  115. /* Zeroed for the combo PHY operating in USB mode */
  116. #define USB3_TEST_CTRL 0x0068
  117. /* PHY calibration values, determined by the combo PHY at probe time */
  118. #define PCIE_RCAL_RESULT 0x0084 /* Port A PHY only */
  119. #define RTERM_VALUE_RX GENMASK(3, 0)
  120. #define RTERM_VALUE_TX GENMASK(7, 4)
  121. #define R_TUNE_DONE BIT(10)
  122. static u32 k1_phy_rterm = ~0; /* Invalid initial value */
  123. /* Save the RX and TX receiver termination values */
  124. static void k1_phy_rterm_set(u32 val)
  125. {
  126. k1_phy_rterm = val & (RTERM_VALUE_RX | RTERM_VALUE_TX);
  127. }
  128. static bool k1_phy_rterm_valid(void)
  129. {
  130. /* Valid if no bits outside those we care about are set */
  131. return !(k1_phy_rterm & ~(RTERM_VALUE_RX | RTERM_VALUE_TX));
  132. }
  133. static u32 k1_phy_rterm_rx(void)
  134. {
  135. return FIELD_GET(RTERM_VALUE_RX, k1_phy_rterm);
  136. }
  137. static u32 k1_phy_rterm_tx(void)
  138. {
  139. return FIELD_GET(RTERM_VALUE_TX, k1_phy_rterm);
  140. }
  141. /* Only the combo PHY has a PMU pointer defined */
  142. static bool k1_phy_port_a(struct k1_pcie_phy *k1_phy)
  143. {
  144. return !!k1_phy->pmu;
  145. }
  146. /* The PLL clocks are driven by the external oscillator */
  147. static const struct clk_parent_data k1_pcie_phy_data[] = {
  148. { .fw_name = "refclk", },
  149. };
  150. static struct k1_pcie_phy *clk_hw_to_k1_phy(struct clk_hw *clk_hw)
  151. {
  152. return container_of(clk_hw, struct k1_pcie_phy, pll_hw);
  153. }
  154. /* USB mode only works on the combo PHY, which has only one lane */
  155. static void k1_pcie_phy_pll_prepare_usb(struct k1_pcie_phy *k1_phy)
  156. {
  157. void __iomem *regs = k1_phy->regs;
  158. u32 val;
  159. val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
  160. val &= ~CFG_INTERNAL_TIMER_ADJ;
  161. val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_USB);
  162. writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
  163. val = readl(regs + PCIE_PU_PLL_1);
  164. val &= ~SSC_DEP_SEL;
  165. val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_5000PPM);
  166. writel(val, regs + PCIE_PU_PLL_1);
  167. }
  168. /* Perform PCIe-specific register updates before starting the PLL clock */
  169. static void k1_pcie_phy_pll_prepare_pcie(struct k1_pcie_phy *k1_phy)
  170. {
  171. void __iomem *regs = k1_phy->regs;
  172. u32 val;
  173. u32 i;
  174. for (i = 0; i < k1_phy->pcie_lanes; i++) {
  175. val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
  176. val &= ~CFG_INTERNAL_TIMER_ADJ;
  177. val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_PCIE);
  178. writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
  179. regs += PHY_LANE_OFFSET; /* Next lane */
  180. }
  181. regs = k1_phy->regs;
  182. val = readl(regs + PCIE_RC_DONE_STATUS);
  183. val |= CFG_FORCE_RCV_RETRY;
  184. writel(val, regs + PCIE_RC_DONE_STATUS);
  185. val = readl(regs + PCIE_PU_PLL_1);
  186. val &= ~SSC_DEP_SEL;
  187. val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_NONE);
  188. writel(val, regs + PCIE_PU_PLL_1);
  189. val = readl(regs + PCIE_PU_PLL_2);
  190. val |= GEN_REF100; /* Enable 100 MHz PLL output clock */
  191. writel(val, regs + PCIE_PU_PLL_2);
  192. }
  193. static int k1_pcie_phy_pll_prepare(struct clk_hw *clk_hw)
  194. {
  195. struct k1_pcie_phy *k1_phy = clk_hw_to_k1_phy(clk_hw);
  196. void __iomem *regs = k1_phy->regs;
  197. u32 val;
  198. u32 i;
  199. if (k1_phy_port_a(k1_phy) && k1_phy->type == PHY_TYPE_USB3)
  200. k1_pcie_phy_pll_prepare_usb(k1_phy);
  201. else
  202. k1_pcie_phy_pll_prepare_pcie(k1_phy);
  203. /*
  204. * Disable 100 MHz input reference with spread-spectrum
  205. * clocking and select the 24 MHz clock input frequency
  206. */
  207. val = readl(regs + PCIE_PU_PLL_1);
  208. val &= ~REF_100_WSSC;
  209. val &= ~FREF_SEL;
  210. val |= FIELD_PREP(FREF_SEL, FREF_24M);
  211. writel(val, regs + PCIE_PU_PLL_1);
  212. /* Mark PLL configuration done on all lanes */
  213. for (i = 0; i < k1_phy->pcie_lanes; i++) {
  214. val = readl(regs + PCIE_PU_ADDR_CLK_CFG);
  215. val |= CFG_SW_PHY_INIT_DONE;
  216. writel(val, regs + PCIE_PU_ADDR_CLK_CFG);
  217. regs += PHY_LANE_OFFSET; /* Next lane */
  218. }
  219. /*
  220. * Wait for indication the PHY PLL is locked. Lanes for ports
  221. * B and C share a PLL, so it's enough to sample just lane 0.
  222. */
  223. return readl_poll_timeout(k1_phy->regs + PCIE_PU_ADDR_CLK_CFG,
  224. val, val & PLL_READY,
  225. POLL_DELAY, PLL_TIMEOUT);
  226. }
  227. /* Prepare implies enable, and once enabled, it's always on */
  228. static const struct clk_ops k1_pcie_phy_pll_ops = {
  229. .prepare = k1_pcie_phy_pll_prepare,
  230. };
  231. /* We represent the PHY PLL as a private clock */
  232. static int k1_pcie_phy_pll_setup(struct k1_pcie_phy *k1_phy)
  233. {
  234. struct clk_hw *hw = &k1_phy->pll_hw;
  235. struct device *dev = k1_phy->dev;
  236. struct clk_init_data init = { };
  237. char *name;
  238. int ret;
  239. name = kasprintf(GFP_KERNEL, "pcie%u_phy_pll", k1_phy->phy->id);
  240. if (!name)
  241. return -ENOMEM;
  242. init.name = name;
  243. init.ops = &k1_pcie_phy_pll_ops;
  244. init.parent_data = k1_pcie_phy_data;
  245. init.num_parents = ARRAY_SIZE(k1_pcie_phy_data);
  246. hw->init = &init;
  247. ret = devm_clk_hw_register(dev, hw);
  248. kfree(name); /* __clk_register() duplicates the name we provide */
  249. if (ret)
  250. return ret;
  251. k1_phy->pll = devm_clk_hw_get_clk(dev, hw, "pll");
  252. if (IS_ERR(k1_phy->pll))
  253. return PTR_ERR(k1_phy->pll);
  254. return 0;
  255. }
  256. /* Select PCIe or USB 3 mode for the combo PHY. */
  257. static void k1_combo_phy_sel(struct k1_pcie_phy *k1_phy, bool usb)
  258. {
  259. struct regmap *pmu = k1_phy->pmu;
  260. /* Only change it if it's not already in the desired state */
  261. if (!regmap_test_bits(pmu, PMUA_USB_PHY_CTRL0, COMBO_PHY_SEL) == usb)
  262. regmap_assign_bits(pmu, PMUA_USB_PHY_CTRL0, COMBO_PHY_SEL, usb);
  263. }
  264. static void k1_pcie_phy_init_pcie(struct k1_pcie_phy *k1_phy)
  265. {
  266. u32 rx_rterm = k1_phy_rterm_rx();
  267. u32 tx_rterm = k1_phy_rterm_tx();
  268. void __iomem *regs;
  269. u32 val;
  270. int i;
  271. /* For the combo PHY, set PHY to PCIe mode */
  272. if (k1_phy_port_a(k1_phy))
  273. k1_combo_phy_sel(k1_phy, false);
  274. regs = k1_phy->regs;
  275. for (i = 0; i < k1_phy->pcie_lanes; i++) {
  276. val = readl(regs + PCIE_RX_REG1);
  277. /* Set RX analog front-end receiver termination value */
  278. val &= ~AFE_RTERM_REG;
  279. val |= FIELD_PREP(AFE_RTERM_REG, rx_rterm);
  280. /* And enable refclock receiver termination */
  281. val |= EN_RTERM;
  282. writel(val, regs + PCIE_RX_REG1);
  283. val = readl(regs + PCIE_RX_REG2);
  284. /* Use PCIE_RX_REG1 AFE_RTERM_REG value */
  285. val &= ~RX_RTERM_SEL;
  286. writel(val, regs + PCIE_RX_REG2);
  287. val = readl(regs + PCIE_TX_REG1);
  288. /* Set TX driver termination value */
  289. val &= ~TX_RTERM_REG;
  290. val |= FIELD_PREP(TX_RTERM_REG, tx_rterm);
  291. /* Use PCIE_TX_REG1 TX_RTERM_REG value */
  292. val |= TX_RTERM_SEL;
  293. writel(val, regs + PCIE_TX_REG1);
  294. /* Set the input clock to 24 MHz, and clear RC_CAL_TOGGLE */
  295. val = readl(regs + PCIE_RC_CAL_REG2);
  296. val &= CLKSEL;
  297. val |= FIELD_PREP(CLKSEL, CLKSEL_24M);
  298. val &= ~RC_CAL_TOGGLE;
  299. writel(val, regs + PCIE_RC_CAL_REG2);
  300. /* Now trigger recalibration by setting RC_CAL_TOGGLE again */
  301. val |= RC_CAL_TOGGLE;
  302. writel(val, regs + PCIE_RC_CAL_REG2);
  303. val = readl(regs + PCIE_LTSSM_DIS_ENTRY);
  304. /* Override the reference clock; set to refclk driver mode */
  305. val |= OVRD_REFCLK_MODE;
  306. val &= ~CFG_REFCLK_MODE;
  307. val |= FIELD_PREP(CFG_REFCLK_MODE, RFCLK_MODE_DRIVER);
  308. writel(val, regs + PCIE_LTSSM_DIS_ENTRY);
  309. regs += PHY_LANE_OFFSET; /* Next lane */
  310. }
  311. }
  312. /* Only called for combo PHY */
  313. static void k1_pcie_phy_init_usb(struct k1_pcie_phy *k1_phy)
  314. {
  315. k1_combo_phy_sel(k1_phy, true);
  316. /* We're not doing any testing */
  317. writel(0, k1_phy->regs + USB3_TEST_CTRL);
  318. }
  319. static int k1_pcie_phy_init(struct phy *phy)
  320. {
  321. struct k1_pcie_phy *k1_phy = phy_get_drvdata(phy);
  322. /* Note: port type is only valid for port A (both checks needed) */
  323. if (k1_phy_port_a(k1_phy) && k1_phy->type == PHY_TYPE_USB3)
  324. k1_pcie_phy_init_usb(k1_phy);
  325. else
  326. k1_pcie_phy_init_pcie(k1_phy);
  327. return clk_prepare_enable(k1_phy->pll);
  328. }
  329. static int k1_pcie_phy_exit(struct phy *phy)
  330. {
  331. struct k1_pcie_phy *k1_phy = phy_get_drvdata(phy);
  332. clk_disable_unprepare(k1_phy->pll);
  333. return 0;
  334. }
  335. static const struct phy_ops k1_pcie_phy_ops = {
  336. .init = k1_pcie_phy_init,
  337. .exit = k1_pcie_phy_exit,
  338. .owner = THIS_MODULE,
  339. };
  340. /*
  341. * Get values needed for calibrating PHYs operating in PCIe mode. Only
  342. * the combo PHY is able to do this, and its calibration values are used
  343. * for configuring all PCIe PHYs.
  344. *
  345. * We always need to de-assert the "global" reset on the combo PHY,
  346. * because the USB driver depends on it. If used for PCIe, that driver
  347. * will (also) de-assert this, but by leaving it de-asserted for the
  348. * combo PHY, the USB driver doesn't have to do this. Note: although
  349. * SpacemiT refers to this as the global reset, we name the "phy" reset.
  350. *
  351. * In addition, we guarantee the APP_HOLD_PHY_RESET bit is clear for the
  352. * combo PHY, so the USB driver doesn't have to manage that either. The
  353. * PCIe driver is free to change this bit for normal operation.
  354. *
  355. * Calibration only needs to be done once. It's possible calibration has
  356. * already completed (e.g., it might have happened in the boot loader, or
  357. * -EPROBE_DEFER might result in this function being called again). So we
  358. * check that early too, to avoid doing it more than once.
  359. *
  360. * Otherwise we temporarily power up the PHY using the PCIe app clocks
  361. * and resets, wait for the hardware to indicate calibration is done,
  362. * grab the value, then shut the PHY down again.
  363. */
  364. static int k1_pcie_combo_phy_calibrate(struct k1_pcie_phy *k1_phy)
  365. {
  366. struct reset_control_bulk_data resets[] = {
  367. { .id = "dbi", },
  368. { .id = "mstr", },
  369. { .id = "slv", },
  370. };
  371. struct clk_bulk_data clocks[] = {
  372. { .id = "dbi", },
  373. { .id = "mstr", },
  374. { .id = "slv", },
  375. };
  376. struct device *dev = k1_phy->dev;
  377. int ret = 0;
  378. int val;
  379. /* Nothing to do if we already set the receiver termination value */
  380. if (k1_phy_rterm_valid())
  381. return 0;
  382. /*
  383. * We also guarantee the APP_HOLD_PHY_RESET bit is clear. We can
  384. * leave this bit clear even if an error happens below.
  385. */
  386. regmap_assign_bits(k1_phy->pmu, PCIE_CLK_RES_CTRL,
  387. PCIE_APP_HOLD_PHY_RST, false);
  388. /* If the calibration already completed (e.g. by U-Boot), we're done */
  389. val = readl(k1_phy->regs + PCIE_RCAL_RESULT);
  390. if (val & R_TUNE_DONE)
  391. goto out_tune_done;
  392. /* Put the PHY into PCIe mode */
  393. k1_combo_phy_sel(k1_phy, false);
  394. /* Get and enable the PCIe app clocks */
  395. ret = clk_bulk_get(dev, ARRAY_SIZE(clocks), clocks);
  396. if (ret < 0)
  397. goto out_tune_done;
  398. ret = clk_bulk_prepare_enable(ARRAY_SIZE(clocks), clocks);
  399. if (ret)
  400. goto out_put_clocks;
  401. /* Get the PCIe application resets (not the PHY reset) */
  402. ret = reset_control_bulk_get_shared(dev, ARRAY_SIZE(resets), resets);
  403. if (ret)
  404. goto out_disable_clocks;
  405. /* De-assert the PCIe application resets */
  406. ret = reset_control_bulk_deassert(ARRAY_SIZE(resets), resets);
  407. if (ret)
  408. goto out_put_resets;
  409. /*
  410. * This is the core activity here. Wait for the hardware to
  411. * signal that it has completed calibration/tuning. Once it
  412. * has, the register value will contain the values we'll
  413. * use to configure PCIe PHYs.
  414. */
  415. ret = readl_poll_timeout(k1_phy->regs + PCIE_RCAL_RESULT,
  416. val, val & R_TUNE_DONE,
  417. POLL_DELAY, CALIBRATION_TIMEOUT);
  418. /* Clean up. We're done with the resets and clocks */
  419. reset_control_bulk_assert(ARRAY_SIZE(resets), resets);
  420. out_put_resets:
  421. reset_control_bulk_put(ARRAY_SIZE(resets), resets);
  422. out_disable_clocks:
  423. clk_bulk_disable_unprepare(ARRAY_SIZE(clocks), clocks);
  424. out_put_clocks:
  425. clk_bulk_put(ARRAY_SIZE(clocks), clocks);
  426. out_tune_done:
  427. /* If we got the value without timing out, set k1_phy_rterm */
  428. if (!ret)
  429. k1_phy_rterm_set(val);
  430. return ret;
  431. }
  432. static struct phy *
  433. k1_pcie_combo_phy_xlate(struct device *dev, const struct of_phandle_args *args)
  434. {
  435. struct k1_pcie_phy *k1_phy = dev_get_drvdata(dev);
  436. u32 type;
  437. /* The argument specifying the PHY mode is required */
  438. if (args->args_count != 1)
  439. return ERR_PTR(-EINVAL);
  440. /* We only support PCIe and USB 3 mode */
  441. type = args->args[0];
  442. if (type != PHY_TYPE_PCIE && type != PHY_TYPE_USB3)
  443. return ERR_PTR(-EINVAL);
  444. /* This PHY can only be used once */
  445. if (k1_phy->type != PHY_NONE)
  446. return ERR_PTR(-EBUSY);
  447. k1_phy->type = type;
  448. return k1_phy->phy;
  449. }
  450. /* Use the maximum number of PCIe lanes unless limited by device tree */
  451. static u32 k1_pcie_num_lanes(struct k1_pcie_phy *k1_phy, bool port_a)
  452. {
  453. struct device *dev = k1_phy->dev;
  454. u32 count = 0;
  455. u32 max;
  456. int ret;
  457. ret = of_property_read_u32(dev_of_node(dev), "num-lanes", &count);
  458. if (count == 1)
  459. return 1;
  460. if (count == 2 && !port_a)
  461. return 2;
  462. max = port_a ? 1 : 2;
  463. if (ret != -EINVAL)
  464. dev_warn(dev, "bad lane count %u for port; using %u\n",
  465. count, max);
  466. return max;
  467. }
  468. static int k1_pcie_combo_phy_probe(struct k1_pcie_phy *k1_phy)
  469. {
  470. struct device *dev = k1_phy->dev;
  471. struct regmap *regmap;
  472. int ret;
  473. /* Setting the PHY mode requires access to the PMU regmap */
  474. regmap = syscon_regmap_lookup_by_phandle(dev_of_node(dev), SYSCON_APMU);
  475. if (IS_ERR(regmap))
  476. return dev_err_probe(dev, PTR_ERR(regmap), "failed to get PMU\n");
  477. k1_phy->pmu = regmap;
  478. ret = k1_pcie_combo_phy_calibrate(k1_phy);
  479. if (ret)
  480. return dev_err_probe(dev, ret, "calibration failed\n");
  481. /* Needed by k1_pcie_combo_phy_xlate(), which also sets k1_phy->type */
  482. dev_set_drvdata(dev, k1_phy);
  483. return 0;
  484. }
  485. static int k1_pcie_phy_probe(struct platform_device *pdev)
  486. {
  487. struct phy *(*xlate)(struct device *dev,
  488. const struct of_phandle_args *args);
  489. struct device *dev = &pdev->dev;
  490. struct reset_control *phy_reset;
  491. struct phy_provider *provider;
  492. struct k1_pcie_phy *k1_phy;
  493. bool probing_port_a;
  494. int ret;
  495. xlate = of_device_get_match_data(dev);
  496. probing_port_a = xlate == k1_pcie_combo_phy_xlate;
  497. /* Only the combo PHY can calibrate, so it must probe first */
  498. if (!k1_phy_rterm_valid() && !probing_port_a)
  499. return -EPROBE_DEFER;
  500. k1_phy = devm_kzalloc(dev, sizeof(*k1_phy), GFP_KERNEL);
  501. if (!k1_phy)
  502. return -ENOMEM;
  503. k1_phy->dev = dev;
  504. k1_phy->regs = devm_platform_ioremap_resource(pdev, 0);
  505. if (IS_ERR(k1_phy->regs))
  506. return dev_err_probe(dev, PTR_ERR(k1_phy->regs),
  507. "error mapping registers\n");
  508. /* De-assert the PHY (global) reset and leave it that way */
  509. phy_reset = devm_reset_control_get_exclusive_deasserted(dev, "phy");
  510. if (IS_ERR(phy_reset))
  511. return PTR_ERR(phy_reset);
  512. if (probing_port_a) {
  513. ret = k1_pcie_combo_phy_probe(k1_phy);
  514. if (ret)
  515. return dev_err_probe(dev, ret,
  516. "error probing combo phy\n");
  517. }
  518. k1_phy->pcie_lanes = k1_pcie_num_lanes(k1_phy, probing_port_a);
  519. k1_phy->phy = devm_phy_create(dev, NULL, &k1_pcie_phy_ops);
  520. if (IS_ERR(k1_phy->phy))
  521. return dev_err_probe(dev, PTR_ERR(k1_phy->phy),
  522. "error creating phy\n");
  523. phy_set_drvdata(k1_phy->phy, k1_phy);
  524. ret = k1_pcie_phy_pll_setup(k1_phy);
  525. if (ret)
  526. return dev_err_probe(dev, ret, "error initializing clock\n");
  527. provider = devm_of_phy_provider_register(dev, xlate);
  528. if (IS_ERR(provider))
  529. return dev_err_probe(dev, PTR_ERR(provider),
  530. "error registering provider\n");
  531. return 0;
  532. }
  533. static const struct of_device_id k1_pcie_phy_of_match[] = {
  534. { .compatible = "spacemit,k1-combo-phy", k1_pcie_combo_phy_xlate, },
  535. { .compatible = "spacemit,k1-pcie-phy", of_phy_simple_xlate, },
  536. { },
  537. };
  538. MODULE_DEVICE_TABLE(of, k1_pcie_phy_of_match);
  539. static struct platform_driver k1_pcie_phy_driver = {
  540. .probe = k1_pcie_phy_probe,
  541. .driver = {
  542. .of_match_table = k1_pcie_phy_of_match,
  543. .name = "spacemit-k1-pcie-phy",
  544. }
  545. };
  546. module_platform_driver(k1_pcie_phy_driver);
  547. MODULE_DESCRIPTION("SpacemiT K1 PCIe and USB 3 PHY driver");
  548. MODULE_LICENSE("GPL");