phy-zynqmp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-zynqmp.c - PHY driver for Xilinx ZynqMP GT.
  4. *
  5. * Copyright (C) 2018-2021 Xilinx Inc.
  6. *
  7. * Author: Anurag Kumar Vulisha <anuragku@xilinx.com>
  8. * Author: Subbaraya Sundeep <sundeep.lkml@gmail.com>
  9. * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. */
  11. #include <common.h>
  12. #include <clk-uclass.h>
  13. #include <dm.h>
  14. #include <generic-phy.h>
  15. #include <log.h>
  16. #include <power-domain.h>
  17. #include <regmap.h>
  18. #include <syscon.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/sys_proto.h>
  21. #include <asm/arch/hardware.h>
  22. #include <dm/device.h>
  23. #include <dm/device_compat.h>
  24. #include <dm/lists.h>
  25. #include <dt-bindings/phy/phy.h>
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. /*
  30. * Lane Registers
  31. */
  32. /* TX De-emphasis parameters */
  33. #define L0_TX_ANA_TM_18 0x0048
  34. #define L0_TX_ANA_TM_118 0x01d8
  35. #define L0_TX_ANA_TM_118_FORCE_17_0 BIT(0)
  36. /* DN Resistor calibration code parameters */
  37. #define L0_TXPMA_ST_3 0x0b0c
  38. #define L0_DN_CALIB_CODE 0x3f
  39. /* PMA control parameters */
  40. #define L0_TXPMD_TM_45 0x0cb4
  41. #define L0_TXPMD_TM_48 0x0cc0
  42. #define L0_TXPMD_TM_45_OVER_DP_MAIN BIT(0)
  43. #define L0_TXPMD_TM_45_ENABLE_DP_MAIN BIT(1)
  44. #define L0_TXPMD_TM_45_OVER_DP_POST1 BIT(2)
  45. #define L0_TXPMD_TM_45_ENABLE_DP_POST1 BIT(3)
  46. #define L0_TXPMD_TM_45_OVER_DP_POST2 BIT(4)
  47. #define L0_TXPMD_TM_45_ENABLE_DP_POST2 BIT(5)
  48. /* PCS control parameters */
  49. #define L0_TM_DIG_6 0x106c
  50. #define L0_TM_DIS_DESCRAMBLE_DECODER 0x0f
  51. #define L0_TX_DIG_61 0x00f4
  52. #define L0_TM_DISABLE_SCRAMBLE_ENCODER 0x0f
  53. /* PLL Test Mode register parameters */
  54. #define L0_TM_PLL_DIG_37 0x2094
  55. #define L0_TM_COARSE_CODE_LIMIT 0x10
  56. /* PLL SSC step size offsets */
  57. #define L0_PLL_SS_STEPS_0_LSB 0x2368
  58. #define L0_PLL_SS_STEPS_1_MSB 0x236c
  59. #define L0_PLL_SS_STEP_SIZE_0_LSB 0x2370
  60. #define L0_PLL_SS_STEP_SIZE_1 0x2374
  61. #define L0_PLL_SS_STEP_SIZE_2 0x2378
  62. #define L0_PLL_SS_STEP_SIZE_3_MSB 0x237c
  63. #define L0_PLL_STATUS_READ_1 0x23e4
  64. /* SSC step size parameters */
  65. #define STEP_SIZE_0_MASK 0xff
  66. #define STEP_SIZE_1_MASK 0xff
  67. #define STEP_SIZE_2_MASK 0xff
  68. #define STEP_SIZE_3_MASK 0x3
  69. #define STEP_SIZE_SHIFT 8
  70. #define FORCE_STEP_SIZE 0x10
  71. #define FORCE_STEPS 0x20
  72. #define STEPS_0_MASK 0xff
  73. #define STEPS_1_MASK 0x07
  74. /* Reference clock selection parameters */
  75. #define L0_Ln_REF_CLK_SEL(n) (0x2860 + (n) * 4)
  76. #define L0_REF_CLK_SEL_MASK 0x8f
  77. /* Calibration digital logic parameters */
  78. #define L3_TM_CALIB_DIG19 0xec4c
  79. #define L3_CALIB_DONE_STATUS 0xef14
  80. #define L3_TM_CALIB_DIG18 0xec48
  81. #define L3_TM_CALIB_DIG19_NSW 0x07
  82. #define L3_TM_CALIB_DIG18_NSW 0xe0
  83. #define L3_TM_OVERRIDE_NSW_CODE 0x20
  84. #define L3_CALIB_DONE 0x02
  85. #define L3_NSW_SHIFT 5
  86. #define L3_NSW_PIPE_SHIFT 4
  87. #define L3_NSW_CALIB_SHIFT 3
  88. #define PHY_REG_OFFSET 0x4000
  89. /*
  90. * Global Registers
  91. */
  92. /* Refclk selection parameters */
  93. #define PLL_REF_SEL(n) (0x10000 + (n) * 4)
  94. #define PLL_FREQ_MASK 0x1f
  95. #define PLL_STATUS_LOCKED 0x10
  96. /* Inter Connect Matrix parameters */
  97. #define ICM_CFG0 0x10010
  98. #define ICM_CFG1 0x10014
  99. #define ICM_CFG0_L0_MASK 0x07
  100. #define ICM_CFG0_L1_MASK 0x70
  101. #define ICM_CFG1_L2_MASK 0x07
  102. #define ICM_CFG2_L3_MASK 0x70
  103. #define ICM_CFG_SHIFT 4
  104. /* Inter Connect Matrix allowed protocols */
  105. #define ICM_PROTOCOL_PD 0x0
  106. #define ICM_PROTOCOL_PCIE 0x1
  107. #define ICM_PROTOCOL_SATA 0x2
  108. #define ICM_PROTOCOL_USB 0x3
  109. #define ICM_PROTOCOL_DP 0x4
  110. #define ICM_PROTOCOL_SGMII 0x5
  111. /* Test Mode common reset control parameters */
  112. #define TM_CMN_RST 0x10018
  113. #define TM_CMN_RST_EN 0x1
  114. #define TM_CMN_RST_SET 0x2
  115. #define TM_CMN_RST_MASK 0x3
  116. /* Bus width parameters */
  117. #define TX_PROT_BUS_WIDTH 0x10040
  118. #define RX_PROT_BUS_WIDTH 0x10044
  119. #define PROT_BUS_WIDTH_10 0x0
  120. #define PROT_BUS_WIDTH_20 0x1
  121. #define PROT_BUS_WIDTH_40 0x2
  122. #define PROT_BUS_WIDTH_MASK 0x3
  123. #define PROT_BUS_WIDTH_SHIFT 2
  124. /* Number of GT lanes */
  125. #define NUM_LANES 4
  126. /* SIOU SATA control register */
  127. #define SATA_CONTROL_OFFSET 0x0100
  128. /* Total number of controllers */
  129. #define CONTROLLERS_PER_LANE 5
  130. /* Protocol Type parameters */
  131. enum {
  132. XPSGTR_TYPE_USB0 = 0, /* USB controller 0 */
  133. XPSGTR_TYPE_USB1 = 1, /* USB controller 1 */
  134. XPSGTR_TYPE_SATA_0 = 2, /* SATA controller lane 0 */
  135. XPSGTR_TYPE_SATA_1 = 3, /* SATA controller lane 1 */
  136. XPSGTR_TYPE_PCIE_0 = 4, /* PCIe controller lane 0 */
  137. XPSGTR_TYPE_PCIE_1 = 5, /* PCIe controller lane 1 */
  138. XPSGTR_TYPE_PCIE_2 = 6, /* PCIe controller lane 2 */
  139. XPSGTR_TYPE_PCIE_3 = 7, /* PCIe controller lane 3 */
  140. XPSGTR_TYPE_DP_0 = 8, /* Display Port controller lane 0 */
  141. XPSGTR_TYPE_DP_1 = 9, /* Display Port controller lane 1 */
  142. XPSGTR_TYPE_SGMII0 = 10, /* Ethernet SGMII controller 0 */
  143. XPSGTR_TYPE_SGMII1 = 11, /* Ethernet SGMII controller 1 */
  144. XPSGTR_TYPE_SGMII2 = 12, /* Ethernet SGMII controller 2 */
  145. XPSGTR_TYPE_SGMII3 = 13, /* Ethernet SGMII controller 3 */
  146. };
  147. /* Timeout values */
  148. #define TIMEOUT_US 10000
  149. #define IOU_SLCR_GEM_CLK_CTRL 0x308
  150. #define GEM_CTRL_GEM_SGMII_MODE BIT(2)
  151. #define GEM_CTRL_GEM_REF_SRC_SEL BIT(1)
  152. #define IOU_SLCR_GEM_CTRL 0x360
  153. #define GEM_CTRL_GEM_SGMII_SD BIT(0)
  154. /**
  155. * struct xpsgtr_ssc - structure to hold SSC settings for a lane
  156. * @refclk_rate: PLL reference clock frequency
  157. * @pll_ref_clk: value to be written to register for corresponding ref clk rate
  158. * @steps: number of steps of SSC (Spread Spectrum Clock)
  159. * @step_size: step size of each step
  160. */
  161. struct xpsgtr_ssc {
  162. u32 refclk_rate;
  163. u8 pll_ref_clk;
  164. u32 steps;
  165. u32 step_size;
  166. };
  167. /**
  168. * struct xpsgtr_phy - representation of a lane
  169. * @dev: pointer to the xpsgtr_dev instance
  170. * @refclk: reference clock index
  171. * @type: controller which uses this lane
  172. * @lane: lane number
  173. * @protocol: protocol in which the lane operates
  174. */
  175. struct xpsgtr_phy {
  176. struct xpsgtr_dev *dev;
  177. unsigned int refclk;
  178. u8 type;
  179. u8 lane;
  180. u8 protocol;
  181. };
  182. /**
  183. * struct xpsgtr_dev - representation of a ZynMP GT device
  184. * @dev: pointer to device
  185. * @serdes: serdes base address
  186. * @siou: siou base address
  187. * @phys: PHY lanes
  188. * @refclk_sscs: spread spectrum settings for the reference clocks
  189. * @clk: reference clocks
  190. */
  191. struct xpsgtr_dev {
  192. struct udevice *dev;
  193. u8 *serdes;
  194. u8 *siou;
  195. struct xpsgtr_phy phys[NUM_LANES];
  196. const struct xpsgtr_ssc *refclk_sscs[NUM_LANES];
  197. struct clk clk[NUM_LANES];
  198. };
  199. /* Configuration Data */
  200. /* lookup table to hold all settings needed for a ref clock frequency */
  201. static const struct xpsgtr_ssc ssc_lookup[] = {
  202. { 19200000, 0x05, 608, 264020 },
  203. { 20000000, 0x06, 634, 243454 },
  204. { 24000000, 0x07, 760, 168973 },
  205. { 26000000, 0x08, 824, 143860 },
  206. { 27000000, 0x09, 856, 86551 },
  207. { 38400000, 0x0a, 1218, 65896 },
  208. { 40000000, 0x0b, 634, 243454 },
  209. { 52000000, 0x0c, 824, 143860 },
  210. { 100000000, 0x0d, 1058, 87533 },
  211. { 108000000, 0x0e, 856, 86551 },
  212. { 125000000, 0x0f, 992, 119497 },
  213. { 135000000, 0x10, 1070, 55393 },
  214. { 150000000, 0x11, 792, 187091 }
  215. };
  216. /* I/O Accessors */
  217. static u32 xpsgtr_read(struct xpsgtr_dev *gtr_dev, u32 reg)
  218. {
  219. return readl(gtr_dev->serdes + reg);
  220. }
  221. static void xpsgtr_write(struct xpsgtr_dev *gtr_dev, u32 reg, u32 value)
  222. {
  223. writel(value, gtr_dev->serdes + reg);
  224. }
  225. static void xpsgtr_clr_set(struct xpsgtr_dev *gtr_dev, u32 reg,
  226. u32 clr, u32 set)
  227. {
  228. u32 value = xpsgtr_read(gtr_dev, reg);
  229. value &= ~clr;
  230. value |= set;
  231. xpsgtr_write(gtr_dev, reg, value);
  232. }
  233. static u32 xpsgtr_read_phy(struct xpsgtr_phy *gtr_phy, u32 reg)
  234. {
  235. void __iomem *addr = gtr_phy->dev->serdes
  236. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  237. return readl(addr);
  238. }
  239. static void xpsgtr_write_phy(struct xpsgtr_phy *gtr_phy,
  240. u32 reg, u32 value)
  241. {
  242. void __iomem *addr = gtr_phy->dev->serdes
  243. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  244. writel(value, addr);
  245. }
  246. static void xpsgtr_clr_set_phy(struct xpsgtr_phy *gtr_phy,
  247. u32 reg, u32 clr, u32 set)
  248. {
  249. void __iomem *addr = gtr_phy->dev->serdes
  250. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  251. writel((readl(addr) & ~clr) | set, addr);
  252. }
  253. /* Configure PLL and spread-sprectrum clock. */
  254. static void xpsgtr_configure_pll(struct xpsgtr_phy *gtr_phy)
  255. {
  256. const struct xpsgtr_ssc *ssc;
  257. u32 step_size;
  258. ssc = gtr_phy->dev->refclk_sscs[gtr_phy->refclk];
  259. step_size = ssc->step_size;
  260. xpsgtr_clr_set(gtr_phy->dev, PLL_REF_SEL(gtr_phy->lane),
  261. PLL_FREQ_MASK, ssc->pll_ref_clk);
  262. /* Enable lane clock sharing, if required */
  263. if (gtr_phy->refclk != gtr_phy->lane) {
  264. /* Lane3 Ref Clock Selection Register */
  265. xpsgtr_clr_set(gtr_phy->dev, L0_Ln_REF_CLK_SEL(gtr_phy->lane),
  266. L0_REF_CLK_SEL_MASK, 1 << gtr_phy->refclk);
  267. }
  268. /* SSC step size [7:0] */
  269. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_0_LSB,
  270. STEP_SIZE_0_MASK, step_size & STEP_SIZE_0_MASK);
  271. /* SSC step size [15:8] */
  272. step_size >>= STEP_SIZE_SHIFT;
  273. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_1,
  274. STEP_SIZE_1_MASK, step_size & STEP_SIZE_1_MASK);
  275. /* SSC step size [23:16] */
  276. step_size >>= STEP_SIZE_SHIFT;
  277. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_2,
  278. STEP_SIZE_2_MASK, step_size & STEP_SIZE_2_MASK);
  279. /* SSC steps [7:0] */
  280. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_0_LSB,
  281. STEPS_0_MASK, ssc->steps & STEPS_0_MASK);
  282. /* SSC steps [10:8] */
  283. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_1_MSB,
  284. STEPS_1_MASK,
  285. (ssc->steps >> STEP_SIZE_SHIFT) & STEPS_1_MASK);
  286. /* SSC step size [24:25] */
  287. step_size >>= STEP_SIZE_SHIFT;
  288. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_3_MSB,
  289. STEP_SIZE_3_MASK, (step_size & STEP_SIZE_3_MASK) |
  290. FORCE_STEP_SIZE | FORCE_STEPS);
  291. }
  292. /* Configure the lane protocol. */
  293. static void xpsgtr_lane_set_protocol(struct xpsgtr_phy *gtr_phy)
  294. {
  295. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  296. u8 protocol = gtr_phy->protocol;
  297. switch (gtr_phy->lane) {
  298. case 0:
  299. xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L0_MASK, protocol);
  300. break;
  301. case 1:
  302. xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L1_MASK,
  303. protocol << ICM_CFG_SHIFT);
  304. break;
  305. case 2:
  306. xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L0_MASK, protocol);
  307. break;
  308. case 3:
  309. xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L1_MASK,
  310. protocol << ICM_CFG_SHIFT);
  311. break;
  312. default:
  313. /* We already checked 0 <= lane <= 3 */
  314. break;
  315. }
  316. }
  317. /* Bypass (de)scrambler and 8b/10b decoder and encoder. */
  318. static void xpsgtr_bypass_scrambler_8b10b(struct xpsgtr_phy *gtr_phy)
  319. {
  320. xpsgtr_write_phy(gtr_phy, L0_TM_DIG_6, L0_TM_DIS_DESCRAMBLE_DECODER);
  321. xpsgtr_write_phy(gtr_phy, L0_TX_DIG_61, L0_TM_DISABLE_SCRAMBLE_ENCODER);
  322. }
  323. /* DP-specific initialization. */
  324. static void xpsgtr_phy_init_dp(struct xpsgtr_phy *gtr_phy)
  325. {
  326. xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_45,
  327. L0_TXPMD_TM_45_OVER_DP_MAIN |
  328. L0_TXPMD_TM_45_ENABLE_DP_MAIN |
  329. L0_TXPMD_TM_45_OVER_DP_POST1 |
  330. L0_TXPMD_TM_45_OVER_DP_POST2 |
  331. L0_TXPMD_TM_45_ENABLE_DP_POST2);
  332. xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_118,
  333. L0_TX_ANA_TM_118_FORCE_17_0);
  334. }
  335. /* SATA-specific initialization. */
  336. static void xpsgtr_phy_init_sata(struct xpsgtr_phy *gtr_phy)
  337. {
  338. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  339. xpsgtr_bypass_scrambler_8b10b(gtr_phy);
  340. writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET);
  341. }
  342. /* SGMII-specific initialization. */
  343. static void xpsgtr_phy_init_sgmii(struct xpsgtr_phy *gtr_phy)
  344. {
  345. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  346. u32 shift = gtr_phy->lane * PROT_BUS_WIDTH_SHIFT;
  347. /* Set SGMII protocol TX and RX bus width to 10 bits. */
  348. xpsgtr_clr_set(gtr_dev, TX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift,
  349. PROT_BUS_WIDTH_10 << shift);
  350. xpsgtr_clr_set(gtr_dev, RX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift,
  351. PROT_BUS_WIDTH_10 << shift);
  352. xpsgtr_bypass_scrambler_8b10b(gtr_phy);
  353. /*
  354. * Below code is just temporary solution till we have a way how to
  355. * do it via firmware interface in sync with Linux. Till that happen
  356. * this is the most sensible thing to do here.
  357. */
  358. /* GEM I/O Clock Control */
  359. clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CLK_CTRL,
  360. 0xf << shift,
  361. (GEM_CTRL_GEM_SGMII_MODE | GEM_CTRL_GEM_REF_SRC_SEL) <<
  362. shift);
  363. /* Setup signal detect */
  364. clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CTRL,
  365. PROT_BUS_WIDTH_MASK << shift,
  366. GEM_CTRL_GEM_SGMII_SD << shift);
  367. }
  368. static int xpsgtr_init(struct phy *x)
  369. {
  370. struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev);
  371. struct xpsgtr_phy *gtr_phy;
  372. u32 phy_lane = x->id;
  373. gtr_phy = &gtr_dev->phys[phy_lane];
  374. /* Enable coarse code saturation limiting logic. */
  375. xpsgtr_write_phy(gtr_phy, L0_TM_PLL_DIG_37, L0_TM_COARSE_CODE_LIMIT);
  376. /*
  377. * Configure the PLL, the lane protocol, and perform protocol-specific
  378. * initialization.
  379. */
  380. xpsgtr_configure_pll(gtr_phy);
  381. xpsgtr_lane_set_protocol(gtr_phy);
  382. switch (gtr_phy->protocol) {
  383. case ICM_PROTOCOL_SGMII:
  384. xpsgtr_phy_init_sgmii(gtr_phy);
  385. break;
  386. case ICM_PROTOCOL_SATA:
  387. xpsgtr_phy_init_sata(gtr_phy);
  388. break;
  389. case ICM_PROTOCOL_DP:
  390. xpsgtr_phy_init_dp(gtr_phy);
  391. break;
  392. }
  393. dev_dbg(gtr_dev->dev, "lane %u (type %u, protocol %u): init done\n",
  394. gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
  395. return 0;
  396. }
  397. /* Wait for the PLL to lock (with a timeout). */
  398. static int xpsgtr_wait_pll_lock(struct phy *phy)
  399. {
  400. struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev);
  401. struct xpsgtr_phy *gtr_phy;
  402. u32 phy_lane = phy->id;
  403. int ret = 0;
  404. unsigned int timeout = TIMEOUT_US;
  405. gtr_phy = &gtr_dev->phys[phy_lane];
  406. dev_dbg(gtr_dev->dev, "Waiting for PLL lock\n");
  407. while (1) {
  408. u32 reg = xpsgtr_read_phy(gtr_phy, L0_PLL_STATUS_READ_1);
  409. if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED) {
  410. ret = 0;
  411. break;
  412. }
  413. if (--timeout == 0) {
  414. ret = -ETIMEDOUT;
  415. break;
  416. }
  417. udelay(1);
  418. }
  419. if (ret == -ETIMEDOUT)
  420. dev_err(gtr_dev->dev,
  421. "lane %u (type %u, protocol %u): PLL lock timeout\n",
  422. gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
  423. return ret;
  424. }
  425. static int xpsgtr_power_on(struct phy *phy)
  426. {
  427. struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev);
  428. struct xpsgtr_phy *gtr_phy;
  429. u32 phy_lane = phy->id;
  430. int ret = 0;
  431. gtr_phy = &gtr_dev->phys[phy_lane];
  432. /*
  433. * Wait for the PLL to lock. For DP, only wait on DP0 to avoid
  434. * cumulating waits for both lanes. The user is expected to initialize
  435. * lane 0 last.
  436. */
  437. if (gtr_phy->protocol != ICM_PROTOCOL_DP ||
  438. gtr_phy->type == XPSGTR_TYPE_DP_0)
  439. ret = xpsgtr_wait_pll_lock(phy);
  440. return ret;
  441. }
  442. /*
  443. * OF Xlate Support
  444. */
  445. /* Set the lane type and protocol based on the PHY type and instance number. */
  446. static int xpsgtr_set_lane_type(struct xpsgtr_phy *gtr_phy, u8 phy_type,
  447. unsigned int phy_instance)
  448. {
  449. unsigned int num_phy_types;
  450. const int *phy_types;
  451. switch (phy_type) {
  452. case PHY_TYPE_SATA: {
  453. static const int types[] = {
  454. XPSGTR_TYPE_SATA_0,
  455. XPSGTR_TYPE_SATA_1,
  456. };
  457. phy_types = types;
  458. num_phy_types = ARRAY_SIZE(types);
  459. gtr_phy->protocol = ICM_PROTOCOL_SATA;
  460. break;
  461. }
  462. case PHY_TYPE_USB3: {
  463. static const int types[] = {
  464. XPSGTR_TYPE_USB0,
  465. XPSGTR_TYPE_USB1,
  466. };
  467. phy_types = types;
  468. num_phy_types = ARRAY_SIZE(types);
  469. gtr_phy->protocol = ICM_PROTOCOL_USB;
  470. break;
  471. }
  472. case PHY_TYPE_DP: {
  473. static const int types[] = {
  474. XPSGTR_TYPE_DP_0,
  475. XPSGTR_TYPE_DP_1,
  476. };
  477. phy_types = types;
  478. num_phy_types = ARRAY_SIZE(types);
  479. gtr_phy->protocol = ICM_PROTOCOL_DP;
  480. break;
  481. }
  482. case PHY_TYPE_PCIE: {
  483. static const int types[] = {
  484. XPSGTR_TYPE_PCIE_0,
  485. XPSGTR_TYPE_PCIE_1,
  486. XPSGTR_TYPE_PCIE_2,
  487. XPSGTR_TYPE_PCIE_3,
  488. };
  489. phy_types = types;
  490. num_phy_types = ARRAY_SIZE(types);
  491. gtr_phy->protocol = ICM_PROTOCOL_PCIE;
  492. break;
  493. }
  494. case PHY_TYPE_SGMII: {
  495. static const int types[] = {
  496. XPSGTR_TYPE_SGMII0,
  497. XPSGTR_TYPE_SGMII1,
  498. XPSGTR_TYPE_SGMII2,
  499. XPSGTR_TYPE_SGMII3,
  500. };
  501. phy_types = types;
  502. num_phy_types = ARRAY_SIZE(types);
  503. gtr_phy->protocol = ICM_PROTOCOL_SGMII;
  504. break;
  505. }
  506. default:
  507. return -EINVAL;
  508. }
  509. if (phy_instance >= num_phy_types)
  510. return -EINVAL;
  511. gtr_phy->type = phy_types[phy_instance];
  512. return 0;
  513. }
  514. /*
  515. * Valid combinations of controllers and lanes (Interconnect Matrix).
  516. */
  517. static const unsigned int icm_matrix[NUM_LANES][CONTROLLERS_PER_LANE] = {
  518. { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
  519. XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 },
  520. { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0,
  521. XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 },
  522. { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
  523. XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 },
  524. { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1,
  525. XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 }
  526. };
  527. /* Translate OF phandle and args to PHY instance. */
  528. static int xpsgtr_of_xlate(struct phy *x,
  529. struct ofnode_phandle_args *args)
  530. {
  531. struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev);
  532. struct xpsgtr_phy *gtr_phy;
  533. struct udevice *dev = x->dev;
  534. unsigned int phy_instance;
  535. unsigned int phy_lane;
  536. unsigned int phy_type;
  537. unsigned int refclk;
  538. unsigned int i;
  539. int ret;
  540. if (args->args_count != 4) {
  541. dev_err(dev, "Invalid number of cells in 'phy' property\n");
  542. return -EINVAL;
  543. }
  544. /*
  545. * Get the PHY parameters from the OF arguments and derive the lane
  546. * type.
  547. */
  548. phy_lane = args->args[0];
  549. if (phy_lane >= NUM_LANES) {
  550. dev_err(dev, "Invalid lane number %u\n", phy_lane);
  551. return -EINVAL;
  552. }
  553. gtr_phy = &gtr_dev->phys[phy_lane];
  554. phy_type = args->args[1];
  555. phy_instance = args->args[2];
  556. ret = xpsgtr_set_lane_type(gtr_phy, phy_type, phy_instance);
  557. if (ret) {
  558. dev_err(dev, "Invalid PHY type and/or instance\n");
  559. return ret;
  560. }
  561. refclk = args->args[3];
  562. if (refclk >= ARRAY_SIZE(gtr_dev->refclk_sscs) ||
  563. !gtr_dev->refclk_sscs[refclk]) {
  564. dev_err(dev, "Invalid reference clock number %u\n", refclk);
  565. return -EINVAL;
  566. }
  567. gtr_phy->refclk = refclk;
  568. /* This is difference compare to Linux */
  569. gtr_phy->dev = gtr_dev;
  570. gtr_phy->lane = phy_lane;
  571. /*
  572. * Ensure that the Interconnect Matrix is obeyed, i.e a given lane type
  573. * is allowed to operate on the lane.
  574. */
  575. for (i = 0; i < CONTROLLERS_PER_LANE; i++) {
  576. if (icm_matrix[phy_lane][i] == gtr_phy->type) {
  577. x->id = phy_lane;
  578. return 0;
  579. }
  580. }
  581. return -EINVAL;
  582. }
  583. /*
  584. * Probe & Platform Driver
  585. */
  586. static int xpsgtr_get_ref_clocks(struct udevice *dev)
  587. {
  588. unsigned int refclk;
  589. struct xpsgtr_dev *gtr_dev = dev_get_priv(dev);
  590. int ret;
  591. for (refclk = 0; refclk < NUM_LANES; ++refclk) {
  592. int i;
  593. u32 rate;
  594. char name[8];
  595. struct clk *clk = &gtr_dev->clk[refclk];
  596. snprintf(name, sizeof(name), "ref%u", refclk);
  597. dev_dbg(dev, "Checking name: %s\n", name);
  598. ret = clk_get_by_name(dev, name, clk);
  599. if (ret == -ENODATA) {
  600. dev_dbg(dev, "%s clock not specified (err %d)\n",
  601. name, ret);
  602. continue;
  603. } else if (ret) {
  604. dev_dbg(dev, "couldn't get clock %s (err %d)\n",
  605. name, ret);
  606. return ret;
  607. }
  608. rate = clk_get_rate(clk);
  609. dev_dbg(dev, "clk rate %d\n", rate);
  610. ret = clk_enable(clk);
  611. if (ret) {
  612. dev_err(dev, "failed to enable refclk %d clock\n",
  613. refclk);
  614. return ret;
  615. }
  616. for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) {
  617. if (rate == ssc_lookup[i].refclk_rate) {
  618. gtr_dev->refclk_sscs[refclk] = &ssc_lookup[i];
  619. dev_dbg(dev, "Found rate %d\n", i);
  620. break;
  621. }
  622. }
  623. if (i == ARRAY_SIZE(ssc_lookup)) {
  624. dev_err(dev,
  625. "Invalid rate %u for reference clock %u\n",
  626. rate, refclk);
  627. return -EINVAL;
  628. }
  629. }
  630. return 0;
  631. }
  632. static int xpsgtr_probe(struct udevice *dev)
  633. {
  634. struct xpsgtr_dev *gtr_dev = dev_get_priv(dev);
  635. gtr_dev->serdes = dev_remap_addr_name(dev, "serdes");
  636. if (!gtr_dev->serdes)
  637. return -EINVAL;
  638. gtr_dev->siou = dev_remap_addr_name(dev, "siou");
  639. if (!gtr_dev->siou)
  640. return -EINVAL;
  641. gtr_dev->dev = dev;
  642. return xpsgtr_get_ref_clocks(dev);
  643. }
  644. static const struct udevice_id xpsgtr_phy_ids[] = {
  645. { .compatible = "xlnx,zynqmp-psgtr-v1.1", },
  646. { }
  647. };
  648. static const struct phy_ops xpsgtr_phy_ops = {
  649. .init = xpsgtr_init,
  650. .of_xlate = xpsgtr_of_xlate,
  651. .power_on = xpsgtr_power_on,
  652. };
  653. U_BOOT_DRIVER(psgtr_phy) = {
  654. .name = "psgtr_phy",
  655. .id = UCLASS_PHY,
  656. .of_match = xpsgtr_phy_ids,
  657. .ops = &xpsgtr_phy_ops,
  658. .probe = xpsgtr_probe,
  659. .priv_auto = sizeof(struct xpsgtr_dev),
  660. };