phy-zynqmp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-zynqmp.c - PHY driver for Xilinx ZynqMP GT.
  4. *
  5. * Copyright (C) 2018-2020 Xilinx Inc.
  6. *
  7. * Author: Anurag Kumar Vulisha <[email protected]>
  8. * Author: Subbaraya Sundeep <[email protected]>
  9. * Author: Laurent Pinchart <[email protected]>
  10. *
  11. * This driver is tested for USB, SATA and Display Port currently.
  12. * Other controllers PCIe and SGMII should also work but that is
  13. * experimental as of now.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <dt-bindings/phy/phy.h>
  25. /*
  26. * Lane Registers
  27. */
  28. /* TX De-emphasis parameters */
  29. #define L0_TX_ANA_TM_18 0x0048
  30. #define L0_TX_ANA_TM_118 0x01d8
  31. #define L0_TX_ANA_TM_118_FORCE_17_0 BIT(0)
  32. /* DN Resistor calibration code parameters */
  33. #define L0_TXPMA_ST_3 0x0b0c
  34. #define L0_DN_CALIB_CODE 0x3f
  35. /* PMA control parameters */
  36. #define L0_TXPMD_TM_45 0x0cb4
  37. #define L0_TXPMD_TM_48 0x0cc0
  38. #define L0_TXPMD_TM_45_OVER_DP_MAIN BIT(0)
  39. #define L0_TXPMD_TM_45_ENABLE_DP_MAIN BIT(1)
  40. #define L0_TXPMD_TM_45_OVER_DP_POST1 BIT(2)
  41. #define L0_TXPMD_TM_45_ENABLE_DP_POST1 BIT(3)
  42. #define L0_TXPMD_TM_45_OVER_DP_POST2 BIT(4)
  43. #define L0_TXPMD_TM_45_ENABLE_DP_POST2 BIT(5)
  44. /* PCS control parameters */
  45. #define L0_TM_DIG_6 0x106c
  46. #define L0_TM_DIS_DESCRAMBLE_DECODER 0x0f
  47. #define L0_TX_DIG_61 0x00f4
  48. #define L0_TM_DISABLE_SCRAMBLE_ENCODER 0x0f
  49. /* PLL Test Mode register parameters */
  50. #define L0_TM_PLL_DIG_37 0x2094
  51. #define L0_TM_COARSE_CODE_LIMIT 0x10
  52. /* PLL SSC step size offsets */
  53. #define L0_PLL_SS_STEPS_0_LSB 0x2368
  54. #define L0_PLL_SS_STEPS_1_MSB 0x236c
  55. #define L0_PLL_SS_STEP_SIZE_0_LSB 0x2370
  56. #define L0_PLL_SS_STEP_SIZE_1 0x2374
  57. #define L0_PLL_SS_STEP_SIZE_2 0x2378
  58. #define L0_PLL_SS_STEP_SIZE_3_MSB 0x237c
  59. #define L0_PLL_STATUS_READ_1 0x23e4
  60. /* SSC step size parameters */
  61. #define STEP_SIZE_0_MASK 0xff
  62. #define STEP_SIZE_1_MASK 0xff
  63. #define STEP_SIZE_2_MASK 0xff
  64. #define STEP_SIZE_3_MASK 0x3
  65. #define STEP_SIZE_SHIFT 8
  66. #define FORCE_STEP_SIZE 0x10
  67. #define FORCE_STEPS 0x20
  68. #define STEPS_0_MASK 0xff
  69. #define STEPS_1_MASK 0x07
  70. /* Reference clock selection parameters */
  71. #define L0_Ln_REF_CLK_SEL(n) (0x2860 + (n) * 4)
  72. #define L0_REF_CLK_SEL_MASK 0x8f
  73. /* Calibration digital logic parameters */
  74. #define L3_TM_CALIB_DIG19 0xec4c
  75. #define L3_CALIB_DONE_STATUS 0xef14
  76. #define L3_TM_CALIB_DIG18 0xec48
  77. #define L3_TM_CALIB_DIG19_NSW 0x07
  78. #define L3_TM_CALIB_DIG18_NSW 0xe0
  79. #define L3_TM_OVERRIDE_NSW_CODE 0x20
  80. #define L3_CALIB_DONE 0x02
  81. #define L3_NSW_SHIFT 5
  82. #define L3_NSW_PIPE_SHIFT 4
  83. #define L3_NSW_CALIB_SHIFT 3
  84. #define PHY_REG_OFFSET 0x4000
  85. /*
  86. * Global Registers
  87. */
  88. /* Refclk selection parameters */
  89. #define PLL_REF_SEL(n) (0x10000 + (n) * 4)
  90. #define PLL_FREQ_MASK 0x1f
  91. #define PLL_STATUS_LOCKED 0x10
  92. /* Inter Connect Matrix parameters */
  93. #define ICM_CFG0 0x10010
  94. #define ICM_CFG1 0x10014
  95. #define ICM_CFG0_L0_MASK 0x07
  96. #define ICM_CFG0_L1_MASK 0x70
  97. #define ICM_CFG1_L2_MASK 0x07
  98. #define ICM_CFG2_L3_MASK 0x70
  99. #define ICM_CFG_SHIFT 4
  100. /* Inter Connect Matrix allowed protocols */
  101. #define ICM_PROTOCOL_PD 0x0
  102. #define ICM_PROTOCOL_PCIE 0x1
  103. #define ICM_PROTOCOL_SATA 0x2
  104. #define ICM_PROTOCOL_USB 0x3
  105. #define ICM_PROTOCOL_DP 0x4
  106. #define ICM_PROTOCOL_SGMII 0x5
  107. /* Test Mode common reset control parameters */
  108. #define TM_CMN_RST 0x10018
  109. #define TM_CMN_RST_EN 0x1
  110. #define TM_CMN_RST_SET 0x2
  111. #define TM_CMN_RST_MASK 0x3
  112. /* Bus width parameters */
  113. #define TX_PROT_BUS_WIDTH 0x10040
  114. #define RX_PROT_BUS_WIDTH 0x10044
  115. #define PROT_BUS_WIDTH_10 0x0
  116. #define PROT_BUS_WIDTH_20 0x1
  117. #define PROT_BUS_WIDTH_40 0x2
  118. #define PROT_BUS_WIDTH_SHIFT(n) ((n) * 2)
  119. #define PROT_BUS_WIDTH_MASK(n) GENMASK((n) * 2 + 1, (n) * 2)
  120. /* Number of GT lanes */
  121. #define NUM_LANES 4
  122. /* SIOU SATA control register */
  123. #define SATA_CONTROL_OFFSET 0x0100
  124. /* Total number of controllers */
  125. #define CONTROLLERS_PER_LANE 5
  126. /* Protocol Type parameters */
  127. #define XPSGTR_TYPE_USB0 0 /* USB controller 0 */
  128. #define XPSGTR_TYPE_USB1 1 /* USB controller 1 */
  129. #define XPSGTR_TYPE_SATA_0 2 /* SATA controller lane 0 */
  130. #define XPSGTR_TYPE_SATA_1 3 /* SATA controller lane 1 */
  131. #define XPSGTR_TYPE_PCIE_0 4 /* PCIe controller lane 0 */
  132. #define XPSGTR_TYPE_PCIE_1 5 /* PCIe controller lane 1 */
  133. #define XPSGTR_TYPE_PCIE_2 6 /* PCIe controller lane 2 */
  134. #define XPSGTR_TYPE_PCIE_3 7 /* PCIe controller lane 3 */
  135. #define XPSGTR_TYPE_DP_0 8 /* Display Port controller lane 0 */
  136. #define XPSGTR_TYPE_DP_1 9 /* Display Port controller lane 1 */
  137. #define XPSGTR_TYPE_SGMII0 10 /* Ethernet SGMII controller 0 */
  138. #define XPSGTR_TYPE_SGMII1 11 /* Ethernet SGMII controller 1 */
  139. #define XPSGTR_TYPE_SGMII2 12 /* Ethernet SGMII controller 2 */
  140. #define XPSGTR_TYPE_SGMII3 13 /* Ethernet SGMII controller 3 */
  141. /* Timeout values */
  142. #define TIMEOUT_US 1000
  143. struct xpsgtr_dev;
  144. /**
  145. * struct xpsgtr_ssc - structure to hold SSC settings for a lane
  146. * @refclk_rate: PLL reference clock frequency
  147. * @pll_ref_clk: value to be written to register for corresponding ref clk rate
  148. * @steps: number of steps of SSC (Spread Spectrum Clock)
  149. * @step_size: step size of each step
  150. */
  151. struct xpsgtr_ssc {
  152. u32 refclk_rate;
  153. u8 pll_ref_clk;
  154. u32 steps;
  155. u32 step_size;
  156. };
  157. /**
  158. * struct xpsgtr_phy - representation of a lane
  159. * @phy: pointer to the kernel PHY device
  160. * @type: controller which uses this lane
  161. * @lane: lane number
  162. * @protocol: protocol in which the lane operates
  163. * @skip_phy_init: skip phy_init() if true
  164. * @dev: pointer to the xpsgtr_dev instance
  165. * @refclk: reference clock index
  166. */
  167. struct xpsgtr_phy {
  168. struct phy *phy;
  169. u8 type;
  170. u8 lane;
  171. u8 protocol;
  172. bool skip_phy_init;
  173. struct xpsgtr_dev *dev;
  174. unsigned int refclk;
  175. };
  176. /**
  177. * struct xpsgtr_dev - representation of a ZynMP GT device
  178. * @dev: pointer to device
  179. * @serdes: serdes base address
  180. * @siou: siou base address
  181. * @gtr_mutex: mutex for locking
  182. * @phys: PHY lanes
  183. * @refclk_sscs: spread spectrum settings for the reference clocks
  184. * @clk: reference clocks
  185. * @tx_term_fix: fix for GT issue
  186. * @saved_icm_cfg0: stored value of ICM CFG0 register
  187. * @saved_icm_cfg1: stored value of ICM CFG1 register
  188. */
  189. struct xpsgtr_dev {
  190. struct device *dev;
  191. void __iomem *serdes;
  192. void __iomem *siou;
  193. struct mutex gtr_mutex; /* mutex for locking */
  194. struct xpsgtr_phy phys[NUM_LANES];
  195. const struct xpsgtr_ssc *refclk_sscs[NUM_LANES];
  196. struct clk *clk[NUM_LANES];
  197. bool tx_term_fix;
  198. unsigned int saved_icm_cfg0;
  199. unsigned int saved_icm_cfg1;
  200. };
  201. /*
  202. * Configuration Data
  203. */
  204. /* lookup table to hold all settings needed for a ref clock frequency */
  205. static const struct xpsgtr_ssc ssc_lookup[] = {
  206. { 19200000, 0x05, 608, 264020 },
  207. { 20000000, 0x06, 634, 243454 },
  208. { 24000000, 0x07, 760, 168973 },
  209. { 26000000, 0x08, 824, 143860 },
  210. { 27000000, 0x09, 856, 86551 },
  211. { 38400000, 0x0a, 1218, 65896 },
  212. { 40000000, 0x0b, 634, 243454 },
  213. { 52000000, 0x0c, 824, 143860 },
  214. { 100000000, 0x0d, 1058, 87533 },
  215. { 108000000, 0x0e, 856, 86551 },
  216. { 125000000, 0x0f, 992, 119497 },
  217. { 135000000, 0x10, 1070, 55393 },
  218. { 150000000, 0x11, 792, 187091 }
  219. };
  220. /*
  221. * I/O Accessors
  222. */
  223. static inline u32 xpsgtr_read(struct xpsgtr_dev *gtr_dev, u32 reg)
  224. {
  225. return readl(gtr_dev->serdes + reg);
  226. }
  227. static inline void xpsgtr_write(struct xpsgtr_dev *gtr_dev, u32 reg, u32 value)
  228. {
  229. writel(value, gtr_dev->serdes + reg);
  230. }
  231. static inline void xpsgtr_clr_set(struct xpsgtr_dev *gtr_dev, u32 reg,
  232. u32 clr, u32 set)
  233. {
  234. u32 value = xpsgtr_read(gtr_dev, reg);
  235. value &= ~clr;
  236. value |= set;
  237. xpsgtr_write(gtr_dev, reg, value);
  238. }
  239. static inline u32 xpsgtr_read_phy(struct xpsgtr_phy *gtr_phy, u32 reg)
  240. {
  241. void __iomem *addr = gtr_phy->dev->serdes
  242. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  243. return readl(addr);
  244. }
  245. static inline void xpsgtr_write_phy(struct xpsgtr_phy *gtr_phy,
  246. u32 reg, u32 value)
  247. {
  248. void __iomem *addr = gtr_phy->dev->serdes
  249. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  250. writel(value, addr);
  251. }
  252. static inline void xpsgtr_clr_set_phy(struct xpsgtr_phy *gtr_phy,
  253. u32 reg, u32 clr, u32 set)
  254. {
  255. void __iomem *addr = gtr_phy->dev->serdes
  256. + gtr_phy->lane * PHY_REG_OFFSET + reg;
  257. writel((readl(addr) & ~clr) | set, addr);
  258. }
  259. /*
  260. * Hardware Configuration
  261. */
  262. /* Wait for the PLL to lock (with a timeout). */
  263. static int xpsgtr_wait_pll_lock(struct phy *phy)
  264. {
  265. struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
  266. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  267. unsigned int timeout = TIMEOUT_US;
  268. int ret;
  269. dev_dbg(gtr_dev->dev, "Waiting for PLL lock\n");
  270. while (1) {
  271. u32 reg = xpsgtr_read_phy(gtr_phy, L0_PLL_STATUS_READ_1);
  272. if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED) {
  273. ret = 0;
  274. break;
  275. }
  276. if (--timeout == 0) {
  277. ret = -ETIMEDOUT;
  278. break;
  279. }
  280. udelay(1);
  281. }
  282. if (ret == -ETIMEDOUT)
  283. dev_err(gtr_dev->dev,
  284. "lane %u (type %u, protocol %u): PLL lock timeout\n",
  285. gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
  286. return ret;
  287. }
  288. /* Configure PLL and spread-sprectrum clock. */
  289. static void xpsgtr_configure_pll(struct xpsgtr_phy *gtr_phy)
  290. {
  291. const struct xpsgtr_ssc *ssc;
  292. u32 step_size;
  293. ssc = gtr_phy->dev->refclk_sscs[gtr_phy->refclk];
  294. step_size = ssc->step_size;
  295. xpsgtr_clr_set(gtr_phy->dev, PLL_REF_SEL(gtr_phy->lane),
  296. PLL_FREQ_MASK, ssc->pll_ref_clk);
  297. /* Enable lane clock sharing, if required */
  298. if (gtr_phy->refclk != gtr_phy->lane) {
  299. /* Lane3 Ref Clock Selection Register */
  300. xpsgtr_clr_set(gtr_phy->dev, L0_Ln_REF_CLK_SEL(gtr_phy->lane),
  301. L0_REF_CLK_SEL_MASK, 1 << gtr_phy->refclk);
  302. }
  303. /* SSC step size [7:0] */
  304. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_0_LSB,
  305. STEP_SIZE_0_MASK, step_size & STEP_SIZE_0_MASK);
  306. /* SSC step size [15:8] */
  307. step_size >>= STEP_SIZE_SHIFT;
  308. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_1,
  309. STEP_SIZE_1_MASK, step_size & STEP_SIZE_1_MASK);
  310. /* SSC step size [23:16] */
  311. step_size >>= STEP_SIZE_SHIFT;
  312. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_2,
  313. STEP_SIZE_2_MASK, step_size & STEP_SIZE_2_MASK);
  314. /* SSC steps [7:0] */
  315. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_0_LSB,
  316. STEPS_0_MASK, ssc->steps & STEPS_0_MASK);
  317. /* SSC steps [10:8] */
  318. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_1_MSB,
  319. STEPS_1_MASK,
  320. (ssc->steps >> STEP_SIZE_SHIFT) & STEPS_1_MASK);
  321. /* SSC step size [24:25] */
  322. step_size >>= STEP_SIZE_SHIFT;
  323. xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_3_MSB,
  324. STEP_SIZE_3_MASK, (step_size & STEP_SIZE_3_MASK) |
  325. FORCE_STEP_SIZE | FORCE_STEPS);
  326. }
  327. /* Configure the lane protocol. */
  328. static void xpsgtr_lane_set_protocol(struct xpsgtr_phy *gtr_phy)
  329. {
  330. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  331. u8 protocol = gtr_phy->protocol;
  332. switch (gtr_phy->lane) {
  333. case 0:
  334. xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L0_MASK, protocol);
  335. break;
  336. case 1:
  337. xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L1_MASK,
  338. protocol << ICM_CFG_SHIFT);
  339. break;
  340. case 2:
  341. xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L0_MASK, protocol);
  342. break;
  343. case 3:
  344. xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L1_MASK,
  345. protocol << ICM_CFG_SHIFT);
  346. break;
  347. default:
  348. /* We already checked 0 <= lane <= 3 */
  349. break;
  350. }
  351. }
  352. /* Bypass (de)scrambler and 8b/10b decoder and encoder. */
  353. static void xpsgtr_bypass_scrambler_8b10b(struct xpsgtr_phy *gtr_phy)
  354. {
  355. xpsgtr_write_phy(gtr_phy, L0_TM_DIG_6, L0_TM_DIS_DESCRAMBLE_DECODER);
  356. xpsgtr_write_phy(gtr_phy, L0_TX_DIG_61, L0_TM_DISABLE_SCRAMBLE_ENCODER);
  357. }
  358. /* DP-specific initialization. */
  359. static void xpsgtr_phy_init_dp(struct xpsgtr_phy *gtr_phy)
  360. {
  361. xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_45,
  362. L0_TXPMD_TM_45_OVER_DP_MAIN |
  363. L0_TXPMD_TM_45_ENABLE_DP_MAIN |
  364. L0_TXPMD_TM_45_OVER_DP_POST1 |
  365. L0_TXPMD_TM_45_OVER_DP_POST2 |
  366. L0_TXPMD_TM_45_ENABLE_DP_POST2);
  367. xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_118,
  368. L0_TX_ANA_TM_118_FORCE_17_0);
  369. }
  370. /* SATA-specific initialization. */
  371. static void xpsgtr_phy_init_sata(struct xpsgtr_phy *gtr_phy)
  372. {
  373. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  374. xpsgtr_bypass_scrambler_8b10b(gtr_phy);
  375. writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET);
  376. }
  377. /* SGMII-specific initialization. */
  378. static void xpsgtr_phy_init_sgmii(struct xpsgtr_phy *gtr_phy)
  379. {
  380. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  381. u32 mask = PROT_BUS_WIDTH_MASK(gtr_phy->lane);
  382. u32 val = PROT_BUS_WIDTH_10 << PROT_BUS_WIDTH_SHIFT(gtr_phy->lane);
  383. /* Set SGMII protocol TX and RX bus width to 10 bits. */
  384. xpsgtr_clr_set(gtr_dev, TX_PROT_BUS_WIDTH, mask, val);
  385. xpsgtr_clr_set(gtr_dev, RX_PROT_BUS_WIDTH, mask, val);
  386. xpsgtr_bypass_scrambler_8b10b(gtr_phy);
  387. }
  388. /* Configure TX de-emphasis and margining for DP. */
  389. static void xpsgtr_phy_configure_dp(struct xpsgtr_phy *gtr_phy, unsigned int pre,
  390. unsigned int voltage)
  391. {
  392. static const u8 voltage_swing[4][4] = {
  393. { 0x2a, 0x27, 0x24, 0x20 },
  394. { 0x27, 0x23, 0x20, 0xff },
  395. { 0x24, 0x20, 0xff, 0xff },
  396. { 0xff, 0xff, 0xff, 0xff }
  397. };
  398. static const u8 pre_emphasis[4][4] = {
  399. { 0x02, 0x02, 0x02, 0x02 },
  400. { 0x01, 0x01, 0x01, 0xff },
  401. { 0x00, 0x00, 0xff, 0xff },
  402. { 0xff, 0xff, 0xff, 0xff }
  403. };
  404. xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_48, voltage_swing[pre][voltage]);
  405. xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_18, pre_emphasis[pre][voltage]);
  406. }
  407. /*
  408. * PHY Operations
  409. */
  410. static bool xpsgtr_phy_init_required(struct xpsgtr_phy *gtr_phy)
  411. {
  412. /*
  413. * As USB may save the snapshot of the states during hibernation, doing
  414. * phy_init() will put the USB controller into reset, resulting in the
  415. * losing of the saved snapshot. So try to avoid phy_init() for USB
  416. * except when gtr_phy->skip_phy_init is false (this happens when FPD is
  417. * shutdown during suspend or when gt lane is changed from current one)
  418. */
  419. if (gtr_phy->protocol == ICM_PROTOCOL_USB && gtr_phy->skip_phy_init)
  420. return false;
  421. else
  422. return true;
  423. }
  424. /*
  425. * There is a functional issue in the GT. The TX termination resistance can be
  426. * out of spec due to a issue in the calibration logic. This is the workaround
  427. * to fix it, required for XCZU9EG silicon.
  428. */
  429. static int xpsgtr_phy_tx_term_fix(struct xpsgtr_phy *gtr_phy)
  430. {
  431. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  432. u32 timeout = TIMEOUT_US;
  433. u32 nsw;
  434. /* Enabling Test Mode control for CMN Rest */
  435. xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
  436. /* Set Test Mode reset */
  437. xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_EN);
  438. xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG18, 0x00);
  439. xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG19, L3_TM_OVERRIDE_NSW_CODE);
  440. /*
  441. * As a part of work around sequence for PMOS calibration fix,
  442. * we need to configure any lane ICM_CFG to valid protocol. This
  443. * will deassert the CMN_Resetn signal.
  444. */
  445. xpsgtr_lane_set_protocol(gtr_phy);
  446. /* Clear Test Mode reset */
  447. xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
  448. dev_dbg(gtr_dev->dev, "calibrating...\n");
  449. do {
  450. u32 reg = xpsgtr_read(gtr_dev, L3_CALIB_DONE_STATUS);
  451. if ((reg & L3_CALIB_DONE) == L3_CALIB_DONE)
  452. break;
  453. if (!--timeout) {
  454. dev_err(gtr_dev->dev, "calibration time out\n");
  455. return -ETIMEDOUT;
  456. }
  457. udelay(1);
  458. } while (timeout > 0);
  459. dev_dbg(gtr_dev->dev, "calibration done\n");
  460. /* Reading NMOS Register Code */
  461. nsw = xpsgtr_read(gtr_dev, L0_TXPMA_ST_3) & L0_DN_CALIB_CODE;
  462. /* Set Test Mode reset */
  463. xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_EN);
  464. /* Writing NMOS register values back [5:3] */
  465. xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG19, nsw >> L3_NSW_CALIB_SHIFT);
  466. /* Writing NMOS register value [2:0] */
  467. xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG18,
  468. ((nsw & L3_TM_CALIB_DIG19_NSW) << L3_NSW_SHIFT) |
  469. (1 << L3_NSW_PIPE_SHIFT));
  470. /* Clear Test Mode reset */
  471. xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
  472. return 0;
  473. }
  474. static int xpsgtr_phy_init(struct phy *phy)
  475. {
  476. struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
  477. struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
  478. int ret = 0;
  479. mutex_lock(&gtr_dev->gtr_mutex);
  480. /* Skip initialization if not required. */
  481. if (!xpsgtr_phy_init_required(gtr_phy))
  482. goto out;
  483. if (gtr_dev->tx_term_fix) {
  484. ret = xpsgtr_phy_tx_term_fix(gtr_phy);
  485. if (ret < 0)
  486. goto out;
  487. gtr_dev->tx_term_fix = false;
  488. }
  489. /* Enable coarse code saturation limiting logic. */
  490. xpsgtr_write_phy(gtr_phy, L0_TM_PLL_DIG_37, L0_TM_COARSE_CODE_LIMIT);
  491. /*
  492. * Configure the PLL, the lane protocol, and perform protocol-specific
  493. * initialization.
  494. */
  495. xpsgtr_configure_pll(gtr_phy);
  496. xpsgtr_lane_set_protocol(gtr_phy);
  497. switch (gtr_phy->protocol) {
  498. case ICM_PROTOCOL_DP:
  499. xpsgtr_phy_init_dp(gtr_phy);
  500. break;
  501. case ICM_PROTOCOL_SATA:
  502. xpsgtr_phy_init_sata(gtr_phy);
  503. break;
  504. case ICM_PROTOCOL_SGMII:
  505. xpsgtr_phy_init_sgmii(gtr_phy);
  506. break;
  507. }
  508. out:
  509. mutex_unlock(&gtr_dev->gtr_mutex);
  510. return ret;
  511. }
  512. static int xpsgtr_phy_exit(struct phy *phy)
  513. {
  514. struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
  515. gtr_phy->skip_phy_init = false;
  516. return 0;
  517. }
  518. static int xpsgtr_phy_power_on(struct phy *phy)
  519. {
  520. struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
  521. int ret = 0;
  522. /* Skip initialization if not required. */
  523. if (!xpsgtr_phy_init_required(gtr_phy))
  524. return ret;
  525. /*
  526. * Wait for the PLL to lock. For DP, only wait on DP0 to avoid
  527. * cumulating waits for both lanes. The user is expected to initialize
  528. * lane 0 last.
  529. */
  530. if (gtr_phy->protocol != ICM_PROTOCOL_DP ||
  531. gtr_phy->type == XPSGTR_TYPE_DP_0)
  532. ret = xpsgtr_wait_pll_lock(phy);
  533. return ret;
  534. }
  535. static int xpsgtr_phy_configure(struct phy *phy, union phy_configure_opts *opts)
  536. {
  537. struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
  538. if (gtr_phy->protocol != ICM_PROTOCOL_DP)
  539. return 0;
  540. xpsgtr_phy_configure_dp(gtr_phy, opts->dp.pre[0], opts->dp.voltage[0]);
  541. return 0;
  542. }
  543. static const struct phy_ops xpsgtr_phyops = {
  544. .init = xpsgtr_phy_init,
  545. .exit = xpsgtr_phy_exit,
  546. .power_on = xpsgtr_phy_power_on,
  547. .configure = xpsgtr_phy_configure,
  548. .owner = THIS_MODULE,
  549. };
  550. /*
  551. * OF Xlate Support
  552. */
  553. /* Set the lane type and protocol based on the PHY type and instance number. */
  554. static int xpsgtr_set_lane_type(struct xpsgtr_phy *gtr_phy, u8 phy_type,
  555. unsigned int phy_instance)
  556. {
  557. unsigned int num_phy_types;
  558. const int *phy_types;
  559. switch (phy_type) {
  560. case PHY_TYPE_SATA: {
  561. static const int types[] = {
  562. XPSGTR_TYPE_SATA_0,
  563. XPSGTR_TYPE_SATA_1,
  564. };
  565. phy_types = types;
  566. num_phy_types = ARRAY_SIZE(types);
  567. gtr_phy->protocol = ICM_PROTOCOL_SATA;
  568. break;
  569. }
  570. case PHY_TYPE_USB3: {
  571. static const int types[] = {
  572. XPSGTR_TYPE_USB0,
  573. XPSGTR_TYPE_USB1,
  574. };
  575. phy_types = types;
  576. num_phy_types = ARRAY_SIZE(types);
  577. gtr_phy->protocol = ICM_PROTOCOL_USB;
  578. break;
  579. }
  580. case PHY_TYPE_DP: {
  581. static const int types[] = {
  582. XPSGTR_TYPE_DP_0,
  583. XPSGTR_TYPE_DP_1,
  584. };
  585. phy_types = types;
  586. num_phy_types = ARRAY_SIZE(types);
  587. gtr_phy->protocol = ICM_PROTOCOL_DP;
  588. break;
  589. }
  590. case PHY_TYPE_PCIE: {
  591. static const int types[] = {
  592. XPSGTR_TYPE_PCIE_0,
  593. XPSGTR_TYPE_PCIE_1,
  594. XPSGTR_TYPE_PCIE_2,
  595. XPSGTR_TYPE_PCIE_3,
  596. };
  597. phy_types = types;
  598. num_phy_types = ARRAY_SIZE(types);
  599. gtr_phy->protocol = ICM_PROTOCOL_PCIE;
  600. break;
  601. }
  602. case PHY_TYPE_SGMII: {
  603. static const int types[] = {
  604. XPSGTR_TYPE_SGMII0,
  605. XPSGTR_TYPE_SGMII1,
  606. XPSGTR_TYPE_SGMII2,
  607. XPSGTR_TYPE_SGMII3,
  608. };
  609. phy_types = types;
  610. num_phy_types = ARRAY_SIZE(types);
  611. gtr_phy->protocol = ICM_PROTOCOL_SGMII;
  612. break;
  613. }
  614. default:
  615. return -EINVAL;
  616. }
  617. if (phy_instance >= num_phy_types)
  618. return -EINVAL;
  619. gtr_phy->type = phy_types[phy_instance];
  620. return 0;
  621. }
  622. /*
  623. * Valid combinations of controllers and lanes (Interconnect Matrix).
  624. */
  625. static const unsigned int icm_matrix[NUM_LANES][CONTROLLERS_PER_LANE] = {
  626. { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
  627. XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 },
  628. { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0,
  629. XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 },
  630. { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
  631. XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 },
  632. { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1,
  633. XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 }
  634. };
  635. /* Translate OF phandle and args to PHY instance. */
  636. static struct phy *xpsgtr_xlate(struct device *dev,
  637. struct of_phandle_args *args)
  638. {
  639. struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
  640. struct xpsgtr_phy *gtr_phy;
  641. unsigned int phy_instance;
  642. unsigned int phy_lane;
  643. unsigned int phy_type;
  644. unsigned int refclk;
  645. unsigned int i;
  646. int ret;
  647. if (args->args_count != 4) {
  648. dev_err(dev, "Invalid number of cells in 'phy' property\n");
  649. return ERR_PTR(-EINVAL);
  650. }
  651. /*
  652. * Get the PHY parameters from the OF arguments and derive the lane
  653. * type.
  654. */
  655. phy_lane = args->args[0];
  656. if (phy_lane >= ARRAY_SIZE(gtr_dev->phys)) {
  657. dev_err(dev, "Invalid lane number %u\n", phy_lane);
  658. return ERR_PTR(-ENODEV);
  659. }
  660. gtr_phy = &gtr_dev->phys[phy_lane];
  661. phy_type = args->args[1];
  662. phy_instance = args->args[2];
  663. ret = xpsgtr_set_lane_type(gtr_phy, phy_type, phy_instance);
  664. if (ret < 0) {
  665. dev_err(gtr_dev->dev, "Invalid PHY type and/or instance\n");
  666. return ERR_PTR(ret);
  667. }
  668. refclk = args->args[3];
  669. if (refclk >= ARRAY_SIZE(gtr_dev->refclk_sscs) ||
  670. !gtr_dev->refclk_sscs[refclk]) {
  671. dev_err(dev, "Invalid reference clock number %u\n", refclk);
  672. return ERR_PTR(-EINVAL);
  673. }
  674. gtr_phy->refclk = refclk;
  675. /*
  676. * Ensure that the Interconnect Matrix is obeyed, i.e a given lane type
  677. * is allowed to operate on the lane.
  678. */
  679. for (i = 0; i < CONTROLLERS_PER_LANE; i++) {
  680. if (icm_matrix[phy_lane][i] == gtr_phy->type)
  681. return gtr_phy->phy;
  682. }
  683. return ERR_PTR(-EINVAL);
  684. }
  685. /*
  686. * Power Management
  687. */
  688. static int __maybe_unused xpsgtr_suspend(struct device *dev)
  689. {
  690. struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
  691. unsigned int i;
  692. /* Save the snapshot ICM_CFG registers. */
  693. gtr_dev->saved_icm_cfg0 = xpsgtr_read(gtr_dev, ICM_CFG0);
  694. gtr_dev->saved_icm_cfg1 = xpsgtr_read(gtr_dev, ICM_CFG1);
  695. for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++)
  696. clk_disable_unprepare(gtr_dev->clk[i]);
  697. return 0;
  698. }
  699. static int __maybe_unused xpsgtr_resume(struct device *dev)
  700. {
  701. struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
  702. unsigned int icm_cfg0, icm_cfg1;
  703. unsigned int i;
  704. bool skip_phy_init;
  705. int err;
  706. for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++) {
  707. err = clk_prepare_enable(gtr_dev->clk[i]);
  708. if (err)
  709. goto err_clk_put;
  710. }
  711. icm_cfg0 = xpsgtr_read(gtr_dev, ICM_CFG0);
  712. icm_cfg1 = xpsgtr_read(gtr_dev, ICM_CFG1);
  713. /* Return if no GT lanes got configured before suspend. */
  714. if (!gtr_dev->saved_icm_cfg0 && !gtr_dev->saved_icm_cfg1)
  715. return 0;
  716. /* Check if the ICM configurations changed after suspend. */
  717. if (icm_cfg0 == gtr_dev->saved_icm_cfg0 &&
  718. icm_cfg1 == gtr_dev->saved_icm_cfg1)
  719. skip_phy_init = true;
  720. else
  721. skip_phy_init = false;
  722. /* Update the skip_phy_init for all gtr_phy instances. */
  723. for (i = 0; i < ARRAY_SIZE(gtr_dev->phys); i++)
  724. gtr_dev->phys[i].skip_phy_init = skip_phy_init;
  725. return 0;
  726. err_clk_put:
  727. while (i--)
  728. clk_disable_unprepare(gtr_dev->clk[i]);
  729. return err;
  730. }
  731. static const struct dev_pm_ops xpsgtr_pm_ops = {
  732. SET_SYSTEM_SLEEP_PM_OPS(xpsgtr_suspend, xpsgtr_resume)
  733. };
  734. /*
  735. * Probe & Platform Driver
  736. */
  737. static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)
  738. {
  739. unsigned int refclk;
  740. int ret;
  741. for (refclk = 0; refclk < ARRAY_SIZE(gtr_dev->refclk_sscs); ++refclk) {
  742. unsigned long rate;
  743. unsigned int i;
  744. struct clk *clk;
  745. char name[8];
  746. snprintf(name, sizeof(name), "ref%u", refclk);
  747. clk = devm_clk_get_optional(gtr_dev->dev, name);
  748. if (IS_ERR(clk)) {
  749. ret = dev_err_probe(gtr_dev->dev, PTR_ERR(clk),
  750. "Failed to get reference clock %u\n",
  751. refclk);
  752. goto err_clk_put;
  753. }
  754. if (!clk)
  755. continue;
  756. ret = clk_prepare_enable(clk);
  757. if (ret)
  758. goto err_clk_put;
  759. gtr_dev->clk[refclk] = clk;
  760. /*
  761. * Get the spread spectrum (SSC) settings for the reference
  762. * clock rate.
  763. */
  764. rate = clk_get_rate(clk);
  765. for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) {
  766. if (rate == ssc_lookup[i].refclk_rate) {
  767. gtr_dev->refclk_sscs[refclk] = &ssc_lookup[i];
  768. break;
  769. }
  770. }
  771. if (i == ARRAY_SIZE(ssc_lookup)) {
  772. dev_err(gtr_dev->dev,
  773. "Invalid rate %lu for reference clock %u\n",
  774. rate, refclk);
  775. ret = -EINVAL;
  776. goto err_clk_put;
  777. }
  778. }
  779. return 0;
  780. err_clk_put:
  781. while (refclk--)
  782. clk_disable_unprepare(gtr_dev->clk[refclk]);
  783. return ret;
  784. }
  785. static int xpsgtr_probe(struct platform_device *pdev)
  786. {
  787. struct device_node *np = pdev->dev.of_node;
  788. struct xpsgtr_dev *gtr_dev;
  789. struct phy_provider *provider;
  790. unsigned int port;
  791. unsigned int i;
  792. int ret;
  793. gtr_dev = devm_kzalloc(&pdev->dev, sizeof(*gtr_dev), GFP_KERNEL);
  794. if (!gtr_dev)
  795. return -ENOMEM;
  796. gtr_dev->dev = &pdev->dev;
  797. platform_set_drvdata(pdev, gtr_dev);
  798. mutex_init(&gtr_dev->gtr_mutex);
  799. if (of_device_is_compatible(np, "xlnx,zynqmp-psgtr"))
  800. gtr_dev->tx_term_fix =
  801. of_property_read_bool(np, "xlnx,tx-termination-fix");
  802. /* Acquire resources. */
  803. gtr_dev->serdes = devm_platform_ioremap_resource_byname(pdev, "serdes");
  804. if (IS_ERR(gtr_dev->serdes))
  805. return PTR_ERR(gtr_dev->serdes);
  806. gtr_dev->siou = devm_platform_ioremap_resource_byname(pdev, "siou");
  807. if (IS_ERR(gtr_dev->siou))
  808. return PTR_ERR(gtr_dev->siou);
  809. ret = xpsgtr_get_ref_clocks(gtr_dev);
  810. if (ret)
  811. return ret;
  812. /* Create PHYs. */
  813. for (port = 0; port < ARRAY_SIZE(gtr_dev->phys); ++port) {
  814. struct xpsgtr_phy *gtr_phy = &gtr_dev->phys[port];
  815. struct phy *phy;
  816. gtr_phy->lane = port;
  817. gtr_phy->dev = gtr_dev;
  818. phy = devm_phy_create(&pdev->dev, np, &xpsgtr_phyops);
  819. if (IS_ERR(phy)) {
  820. dev_err(&pdev->dev, "failed to create PHY\n");
  821. ret = PTR_ERR(phy);
  822. goto err_clk_put;
  823. }
  824. gtr_phy->phy = phy;
  825. phy_set_drvdata(phy, gtr_phy);
  826. }
  827. /* Register the PHY provider. */
  828. provider = devm_of_phy_provider_register(&pdev->dev, xpsgtr_xlate);
  829. if (IS_ERR(provider)) {
  830. dev_err(&pdev->dev, "registering provider failed\n");
  831. ret = PTR_ERR(provider);
  832. goto err_clk_put;
  833. }
  834. return 0;
  835. err_clk_put:
  836. for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++)
  837. clk_disable_unprepare(gtr_dev->clk[i]);
  838. return ret;
  839. }
  840. static const struct of_device_id xpsgtr_of_match[] = {
  841. { .compatible = "xlnx,zynqmp-psgtr", },
  842. { .compatible = "xlnx,zynqmp-psgtr-v1.1", },
  843. {},
  844. };
  845. MODULE_DEVICE_TABLE(of, xpsgtr_of_match);
  846. static struct platform_driver xpsgtr_driver = {
  847. .probe = xpsgtr_probe,
  848. .driver = {
  849. .name = "xilinx-psgtr",
  850. .of_match_table = xpsgtr_of_match,
  851. .pm = &xpsgtr_pm_ops,
  852. },
  853. };
  854. module_platform_driver(xpsgtr_driver);
  855. MODULE_AUTHOR("Xilinx Inc.");
  856. MODULE_LICENSE("GPL v2");
  857. MODULE_DESCRIPTION("Xilinx ZynqMP High speed Gigabit Transceiver");