phy-zynqmp.c 28 KB

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