phy-mvebu-cp110-comphy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright (C) 2017 Marvell
  3. *
  4. * Antoine Tenart <antoine.tenart@free-electrons.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. /* Relative to priv->base */
  18. #define MVEBU_COMPHY_SERDES_CFG0(n) (0x0 + (n) * 0x1000)
  19. #define MVEBU_COMPHY_SERDES_CFG0_PU_PLL BIT(1)
  20. #define MVEBU_COMPHY_SERDES_CFG0_GEN_RX(n) ((n) << 3)
  21. #define MVEBU_COMPHY_SERDES_CFG0_GEN_TX(n) ((n) << 7)
  22. #define MVEBU_COMPHY_SERDES_CFG0_PU_RX BIT(11)
  23. #define MVEBU_COMPHY_SERDES_CFG0_PU_TX BIT(12)
  24. #define MVEBU_COMPHY_SERDES_CFG0_HALF_BUS BIT(14)
  25. #define MVEBU_COMPHY_SERDES_CFG1(n) (0x4 + (n) * 0x1000)
  26. #define MVEBU_COMPHY_SERDES_CFG1_RESET BIT(3)
  27. #define MVEBU_COMPHY_SERDES_CFG1_RX_INIT BIT(4)
  28. #define MVEBU_COMPHY_SERDES_CFG1_CORE_RESET BIT(5)
  29. #define MVEBU_COMPHY_SERDES_CFG1_RF_RESET BIT(6)
  30. #define MVEBU_COMPHY_SERDES_CFG2(n) (0x8 + (n) * 0x1000)
  31. #define MVEBU_COMPHY_SERDES_CFG2_DFE_EN BIT(4)
  32. #define MVEBU_COMPHY_SERDES_STATUS0(n) (0x18 + (n) * 0x1000)
  33. #define MVEBU_COMPHY_SERDES_STATUS0_TX_PLL_RDY BIT(2)
  34. #define MVEBU_COMPHY_SERDES_STATUS0_RX_PLL_RDY BIT(3)
  35. #define MVEBU_COMPHY_SERDES_STATUS0_RX_INIT BIT(4)
  36. #define MVEBU_COMPHY_PWRPLL_CTRL(n) (0x804 + (n) * 0x1000)
  37. #define MVEBU_COMPHY_PWRPLL_CTRL_RFREQ(n) ((n) << 0)
  38. #define MVEBU_COMPHY_PWRPLL_PHY_MODE(n) ((n) << 5)
  39. #define MVEBU_COMPHY_IMP_CAL(n) (0x80c + (n) * 0x1000)
  40. #define MVEBU_COMPHY_IMP_CAL_TX_EXT(n) ((n) << 10)
  41. #define MVEBU_COMPHY_IMP_CAL_TX_EXT_EN BIT(15)
  42. #define MVEBU_COMPHY_DFE_RES(n) (0x81c + (n) * 0x1000)
  43. #define MVEBU_COMPHY_DFE_RES_FORCE_GEN_TBL BIT(15)
  44. #define MVEBU_COMPHY_COEF(n) (0x828 + (n) * 0x1000)
  45. #define MVEBU_COMPHY_COEF_DFE_EN BIT(14)
  46. #define MVEBU_COMPHY_COEF_DFE_CTRL BIT(15)
  47. #define MVEBU_COMPHY_GEN1_S0(n) (0x834 + (n) * 0x1000)
  48. #define MVEBU_COMPHY_GEN1_S0_TX_AMP(n) ((n) << 1)
  49. #define MVEBU_COMPHY_GEN1_S0_TX_EMPH(n) ((n) << 7)
  50. #define MVEBU_COMPHY_GEN1_S1(n) (0x838 + (n) * 0x1000)
  51. #define MVEBU_COMPHY_GEN1_S1_RX_MUL_PI(n) ((n) << 0)
  52. #define MVEBU_COMPHY_GEN1_S1_RX_MUL_PF(n) ((n) << 3)
  53. #define MVEBU_COMPHY_GEN1_S1_RX_MUL_FI(n) ((n) << 6)
  54. #define MVEBU_COMPHY_GEN1_S1_RX_MUL_FF(n) ((n) << 8)
  55. #define MVEBU_COMPHY_GEN1_S1_RX_DFE_EN BIT(10)
  56. #define MVEBU_COMPHY_GEN1_S1_RX_DIV(n) ((n) << 11)
  57. #define MVEBU_COMPHY_GEN1_S2(n) (0x8f4 + (n) * 0x1000)
  58. #define MVEBU_COMPHY_GEN1_S2_TX_EMPH(n) ((n) << 0)
  59. #define MVEBU_COMPHY_GEN1_S2_TX_EMPH_EN BIT(4)
  60. #define MVEBU_COMPHY_LOOPBACK(n) (0x88c + (n) * 0x1000)
  61. #define MVEBU_COMPHY_LOOPBACK_DBUS_WIDTH(n) ((n) << 1)
  62. #define MVEBU_COMPHY_VDD_CAL0(n) (0x908 + (n) * 0x1000)
  63. #define MVEBU_COMPHY_VDD_CAL0_CONT_MODE BIT(15)
  64. #define MVEBU_COMPHY_EXT_SELV(n) (0x914 + (n) * 0x1000)
  65. #define MVEBU_COMPHY_EXT_SELV_RX_SAMPL(n) ((n) << 5)
  66. #define MVEBU_COMPHY_MISC_CTRL0(n) (0x93c + (n) * 0x1000)
  67. #define MVEBU_COMPHY_MISC_CTRL0_ICP_FORCE BIT(5)
  68. #define MVEBU_COMPHY_MISC_CTRL0_REFCLK_SEL BIT(10)
  69. #define MVEBU_COMPHY_RX_CTRL1(n) (0x940 + (n) * 0x1000)
  70. #define MVEBU_COMPHY_RX_CTRL1_RXCLK2X_SEL BIT(11)
  71. #define MVEBU_COMPHY_RX_CTRL1_CLK8T_EN BIT(12)
  72. #define MVEBU_COMPHY_SPEED_DIV(n) (0x954 + (n) * 0x1000)
  73. #define MVEBU_COMPHY_SPEED_DIV_TX_FORCE BIT(7)
  74. #define MVEBU_SP_CALIB(n) (0x96c + (n) * 0x1000)
  75. #define MVEBU_SP_CALIB_SAMPLER(n) ((n) << 8)
  76. #define MVEBU_SP_CALIB_SAMPLER_EN BIT(12)
  77. #define MVEBU_COMPHY_TX_SLEW_RATE(n) (0x974 + (n) * 0x1000)
  78. #define MVEBU_COMPHY_TX_SLEW_RATE_EMPH(n) ((n) << 5)
  79. #define MVEBU_COMPHY_TX_SLEW_RATE_SLC(n) ((n) << 10)
  80. #define MVEBU_COMPHY_DLT_CTRL(n) (0x984 + (n) * 0x1000)
  81. #define MVEBU_COMPHY_DLT_CTRL_DTL_FLOOP_EN BIT(2)
  82. #define MVEBU_COMPHY_FRAME_DETECT0(n) (0xa14 + (n) * 0x1000)
  83. #define MVEBU_COMPHY_FRAME_DETECT0_PATN(n) ((n) << 7)
  84. #define MVEBU_COMPHY_FRAME_DETECT3(n) (0xa20 + (n) * 0x1000)
  85. #define MVEBU_COMPHY_FRAME_DETECT3_LOST_TIMEOUT_EN BIT(12)
  86. #define MVEBU_COMPHY_DME(n) (0xa28 + (n) * 0x1000)
  87. #define MVEBU_COMPHY_DME_ETH_MODE BIT(7)
  88. #define MVEBU_COMPHY_TRAINING0(n) (0xa68 + (n) * 0x1000)
  89. #define MVEBU_COMPHY_TRAINING0_P2P_HOLD BIT(15)
  90. #define MVEBU_COMPHY_TRAINING5(n) (0xaa4 + (n) * 0x1000)
  91. #define MVEBU_COMPHY_TRAINING5_RX_TIMER(n) ((n) << 0)
  92. #define MVEBU_COMPHY_TX_TRAIN_PRESET(n) (0xb1c + (n) * 0x1000)
  93. #define MVEBU_COMPHY_TX_TRAIN_PRESET_16B_AUTO_EN BIT(8)
  94. #define MVEBU_COMPHY_TX_TRAIN_PRESET_PRBS11 BIT(9)
  95. #define MVEBU_COMPHY_GEN1_S3(n) (0xc40 + (n) * 0x1000)
  96. #define MVEBU_COMPHY_GEN1_S3_FBCK_SEL BIT(9)
  97. #define MVEBU_COMPHY_GEN1_S4(n) (0xc44 + (n) * 0x1000)
  98. #define MVEBU_COMPHY_GEN1_S4_DFE_RES(n) ((n) << 8)
  99. #define MVEBU_COMPHY_TX_PRESET(n) (0xc68 + (n) * 0x1000)
  100. #define MVEBU_COMPHY_TX_PRESET_INDEX(n) ((n) << 0)
  101. #define MVEBU_COMPHY_GEN1_S5(n) (0xd38 + (n) * 0x1000)
  102. #define MVEBU_COMPHY_GEN1_S5_ICP(n) ((n) << 0)
  103. /* Relative to priv->regmap */
  104. #define MVEBU_COMPHY_CONF1(n) (0x1000 + (n) * 0x28)
  105. #define MVEBU_COMPHY_CONF1_PWRUP BIT(1)
  106. #define MVEBU_COMPHY_CONF1_USB_PCIE BIT(2) /* 0: Ethernet/SATA */
  107. #define MVEBU_COMPHY_CONF6(n) (0x1014 + (n) * 0x28)
  108. #define MVEBU_COMPHY_CONF6_40B BIT(18)
  109. #define MVEBU_COMPHY_SELECTOR 0x1140
  110. #define MVEBU_COMPHY_SELECTOR_PHY(n) ((n) * 0x4)
  111. #define MVEBU_COMPHY_LANES 6
  112. #define MVEBU_COMPHY_PORTS 3
  113. struct mvebu_comhy_conf {
  114. enum phy_mode mode;
  115. unsigned lane;
  116. unsigned port;
  117. u32 mux;
  118. };
  119. #define MVEBU_COMPHY_CONF(_lane, _port, _mode, _mux) \
  120. { \
  121. .lane = _lane, \
  122. .port = _port, \
  123. .mode = _mode, \
  124. .mux = _mux, \
  125. }
  126. static const struct mvebu_comhy_conf mvebu_comphy_cp110_modes[] = {
  127. /* lane 0 */
  128. MVEBU_COMPHY_CONF(0, 1, PHY_MODE_SGMII, 0x1),
  129. /* lane 1 */
  130. MVEBU_COMPHY_CONF(1, 2, PHY_MODE_SGMII, 0x1),
  131. /* lane 2 */
  132. MVEBU_COMPHY_CONF(2, 0, PHY_MODE_SGMII, 0x1),
  133. MVEBU_COMPHY_CONF(2, 0, PHY_MODE_10GKR, 0x1),
  134. /* lane 3 */
  135. MVEBU_COMPHY_CONF(3, 1, PHY_MODE_SGMII, 0x2),
  136. /* lane 4 */
  137. MVEBU_COMPHY_CONF(4, 0, PHY_MODE_SGMII, 0x2),
  138. MVEBU_COMPHY_CONF(4, 0, PHY_MODE_10GKR, 0x2),
  139. MVEBU_COMPHY_CONF(4, 1, PHY_MODE_SGMII, 0x1),
  140. /* lane 5 */
  141. MVEBU_COMPHY_CONF(5, 2, PHY_MODE_SGMII, 0x1),
  142. };
  143. struct mvebu_comphy_priv {
  144. void __iomem *base;
  145. struct regmap *regmap;
  146. struct device *dev;
  147. int modes[MVEBU_COMPHY_LANES];
  148. };
  149. struct mvebu_comphy_lane {
  150. struct mvebu_comphy_priv *priv;
  151. unsigned id;
  152. enum phy_mode mode;
  153. int port;
  154. };
  155. static int mvebu_comphy_get_mux(int lane, int port, enum phy_mode mode)
  156. {
  157. int i, n = ARRAY_SIZE(mvebu_comphy_cp110_modes);
  158. /* Unused PHY mux value is 0x0 */
  159. if (mode == PHY_MODE_INVALID)
  160. return 0;
  161. for (i = 0; i < n; i++) {
  162. if (mvebu_comphy_cp110_modes[i].lane == lane &&
  163. mvebu_comphy_cp110_modes[i].port == port &&
  164. mvebu_comphy_cp110_modes[i].mode == mode)
  165. break;
  166. }
  167. if (i == n)
  168. return -EINVAL;
  169. return mvebu_comphy_cp110_modes[i].mux;
  170. }
  171. static void mvebu_comphy_ethernet_init_reset(struct mvebu_comphy_lane *lane,
  172. enum phy_mode mode)
  173. {
  174. struct mvebu_comphy_priv *priv = lane->priv;
  175. u32 val;
  176. regmap_read(priv->regmap, MVEBU_COMPHY_CONF1(lane->id), &val);
  177. val &= ~MVEBU_COMPHY_CONF1_USB_PCIE;
  178. val |= MVEBU_COMPHY_CONF1_PWRUP;
  179. regmap_write(priv->regmap, MVEBU_COMPHY_CONF1(lane->id), val);
  180. /* Select baud rates and PLLs */
  181. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG0(lane->id));
  182. val &= ~(MVEBU_COMPHY_SERDES_CFG0_PU_PLL |
  183. MVEBU_COMPHY_SERDES_CFG0_PU_RX |
  184. MVEBU_COMPHY_SERDES_CFG0_PU_TX |
  185. MVEBU_COMPHY_SERDES_CFG0_HALF_BUS |
  186. MVEBU_COMPHY_SERDES_CFG0_GEN_RX(0xf) |
  187. MVEBU_COMPHY_SERDES_CFG0_GEN_TX(0xf));
  188. if (mode == PHY_MODE_10GKR)
  189. val |= MVEBU_COMPHY_SERDES_CFG0_GEN_RX(0xe) |
  190. MVEBU_COMPHY_SERDES_CFG0_GEN_TX(0xe);
  191. else if (mode == PHY_MODE_SGMII)
  192. val |= MVEBU_COMPHY_SERDES_CFG0_GEN_RX(0x6) |
  193. MVEBU_COMPHY_SERDES_CFG0_GEN_TX(0x6) |
  194. MVEBU_COMPHY_SERDES_CFG0_HALF_BUS;
  195. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG0(lane->id));
  196. /* reset */
  197. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  198. val &= ~(MVEBU_COMPHY_SERDES_CFG1_RESET |
  199. MVEBU_COMPHY_SERDES_CFG1_CORE_RESET |
  200. MVEBU_COMPHY_SERDES_CFG1_RF_RESET);
  201. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  202. /* de-assert reset */
  203. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  204. val |= MVEBU_COMPHY_SERDES_CFG1_RESET |
  205. MVEBU_COMPHY_SERDES_CFG1_CORE_RESET;
  206. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  207. /* wait until clocks are ready */
  208. mdelay(1);
  209. /* exlicitly disable 40B, the bits isn't clear on reset */
  210. regmap_read(priv->regmap, MVEBU_COMPHY_CONF6(lane->id), &val);
  211. val &= ~MVEBU_COMPHY_CONF6_40B;
  212. regmap_write(priv->regmap, MVEBU_COMPHY_CONF6(lane->id), val);
  213. /* refclk selection */
  214. val = readl(priv->base + MVEBU_COMPHY_MISC_CTRL0(lane->id));
  215. val &= ~MVEBU_COMPHY_MISC_CTRL0_REFCLK_SEL;
  216. if (mode == PHY_MODE_10GKR)
  217. val |= MVEBU_COMPHY_MISC_CTRL0_ICP_FORCE;
  218. writel(val, priv->base + MVEBU_COMPHY_MISC_CTRL0(lane->id));
  219. /* power and pll selection */
  220. val = readl(priv->base + MVEBU_COMPHY_PWRPLL_CTRL(lane->id));
  221. val &= ~(MVEBU_COMPHY_PWRPLL_CTRL_RFREQ(0x1f) |
  222. MVEBU_COMPHY_PWRPLL_PHY_MODE(0x7));
  223. val |= MVEBU_COMPHY_PWRPLL_CTRL_RFREQ(0x1) |
  224. MVEBU_COMPHY_PWRPLL_PHY_MODE(0x4);
  225. writel(val, priv->base + MVEBU_COMPHY_PWRPLL_CTRL(lane->id));
  226. val = readl(priv->base + MVEBU_COMPHY_LOOPBACK(lane->id));
  227. val &= ~MVEBU_COMPHY_LOOPBACK_DBUS_WIDTH(0x7);
  228. val |= MVEBU_COMPHY_LOOPBACK_DBUS_WIDTH(0x1);
  229. writel(val, priv->base + MVEBU_COMPHY_LOOPBACK(lane->id));
  230. }
  231. static int mvebu_comphy_init_plls(struct mvebu_comphy_lane *lane,
  232. enum phy_mode mode)
  233. {
  234. struct mvebu_comphy_priv *priv = lane->priv;
  235. u32 val;
  236. /* SERDES external config */
  237. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG0(lane->id));
  238. val |= MVEBU_COMPHY_SERDES_CFG0_PU_PLL |
  239. MVEBU_COMPHY_SERDES_CFG0_PU_RX |
  240. MVEBU_COMPHY_SERDES_CFG0_PU_TX;
  241. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG0(lane->id));
  242. /* check rx/tx pll */
  243. readl_poll_timeout(priv->base + MVEBU_COMPHY_SERDES_STATUS0(lane->id),
  244. val,
  245. val & (MVEBU_COMPHY_SERDES_STATUS0_RX_PLL_RDY |
  246. MVEBU_COMPHY_SERDES_STATUS0_TX_PLL_RDY),
  247. 1000, 150000);
  248. if (!(val & (MVEBU_COMPHY_SERDES_STATUS0_RX_PLL_RDY |
  249. MVEBU_COMPHY_SERDES_STATUS0_TX_PLL_RDY)))
  250. return -ETIMEDOUT;
  251. /* rx init */
  252. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  253. val |= MVEBU_COMPHY_SERDES_CFG1_RX_INIT;
  254. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  255. /* check rx */
  256. readl_poll_timeout(priv->base + MVEBU_COMPHY_SERDES_STATUS0(lane->id),
  257. val, val & MVEBU_COMPHY_SERDES_STATUS0_RX_INIT,
  258. 1000, 10000);
  259. if (!(val & MVEBU_COMPHY_SERDES_STATUS0_RX_INIT))
  260. return -ETIMEDOUT;
  261. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  262. val &= ~MVEBU_COMPHY_SERDES_CFG1_RX_INIT;
  263. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  264. return 0;
  265. }
  266. static int mvebu_comphy_set_mode_sgmii(struct phy *phy)
  267. {
  268. struct mvebu_comphy_lane *lane = phy_get_drvdata(phy);
  269. struct mvebu_comphy_priv *priv = lane->priv;
  270. u32 val;
  271. mvebu_comphy_ethernet_init_reset(lane, PHY_MODE_SGMII);
  272. val = readl(priv->base + MVEBU_COMPHY_RX_CTRL1(lane->id));
  273. val &= ~MVEBU_COMPHY_RX_CTRL1_CLK8T_EN;
  274. val |= MVEBU_COMPHY_RX_CTRL1_RXCLK2X_SEL;
  275. writel(val, priv->base + MVEBU_COMPHY_RX_CTRL1(lane->id));
  276. val = readl(priv->base + MVEBU_COMPHY_DLT_CTRL(lane->id));
  277. val &= ~MVEBU_COMPHY_DLT_CTRL_DTL_FLOOP_EN;
  278. writel(val, priv->base + MVEBU_COMPHY_DLT_CTRL(lane->id));
  279. regmap_read(priv->regmap, MVEBU_COMPHY_CONF1(lane->id), &val);
  280. val &= ~MVEBU_COMPHY_CONF1_USB_PCIE;
  281. val |= MVEBU_COMPHY_CONF1_PWRUP;
  282. regmap_write(priv->regmap, MVEBU_COMPHY_CONF1(lane->id), val);
  283. val = readl(priv->base + MVEBU_COMPHY_GEN1_S0(lane->id));
  284. val &= ~MVEBU_COMPHY_GEN1_S0_TX_EMPH(0xf);
  285. val |= MVEBU_COMPHY_GEN1_S0_TX_EMPH(0x1);
  286. writel(val, priv->base + MVEBU_COMPHY_GEN1_S0(lane->id));
  287. return mvebu_comphy_init_plls(lane, PHY_MODE_SGMII);
  288. }
  289. static int mvebu_comphy_set_mode_10gkr(struct phy *phy)
  290. {
  291. struct mvebu_comphy_lane *lane = phy_get_drvdata(phy);
  292. struct mvebu_comphy_priv *priv = lane->priv;
  293. u32 val;
  294. mvebu_comphy_ethernet_init_reset(lane, PHY_MODE_10GKR);
  295. val = readl(priv->base + MVEBU_COMPHY_RX_CTRL1(lane->id));
  296. val |= MVEBU_COMPHY_RX_CTRL1_RXCLK2X_SEL |
  297. MVEBU_COMPHY_RX_CTRL1_CLK8T_EN;
  298. writel(val, priv->base + MVEBU_COMPHY_RX_CTRL1(lane->id));
  299. val = readl(priv->base + MVEBU_COMPHY_DLT_CTRL(lane->id));
  300. val |= MVEBU_COMPHY_DLT_CTRL_DTL_FLOOP_EN;
  301. writel(val, priv->base + MVEBU_COMPHY_DLT_CTRL(lane->id));
  302. /* Speed divider */
  303. val = readl(priv->base + MVEBU_COMPHY_SPEED_DIV(lane->id));
  304. val |= MVEBU_COMPHY_SPEED_DIV_TX_FORCE;
  305. writel(val, priv->base + MVEBU_COMPHY_SPEED_DIV(lane->id));
  306. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG2(lane->id));
  307. val |= MVEBU_COMPHY_SERDES_CFG2_DFE_EN;
  308. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG2(lane->id));
  309. /* DFE resolution */
  310. val = readl(priv->base + MVEBU_COMPHY_DFE_RES(lane->id));
  311. val |= MVEBU_COMPHY_DFE_RES_FORCE_GEN_TBL;
  312. writel(val, priv->base + MVEBU_COMPHY_DFE_RES(lane->id));
  313. val = readl(priv->base + MVEBU_COMPHY_GEN1_S0(lane->id));
  314. val &= ~(MVEBU_COMPHY_GEN1_S0_TX_AMP(0x1f) |
  315. MVEBU_COMPHY_GEN1_S0_TX_EMPH(0xf));
  316. val |= MVEBU_COMPHY_GEN1_S0_TX_AMP(0x1c) |
  317. MVEBU_COMPHY_GEN1_S0_TX_EMPH(0xe);
  318. writel(val, priv->base + MVEBU_COMPHY_GEN1_S0(lane->id));
  319. val = readl(priv->base + MVEBU_COMPHY_GEN1_S2(lane->id));
  320. val &= ~MVEBU_COMPHY_GEN1_S2_TX_EMPH(0xf);
  321. val |= MVEBU_COMPHY_GEN1_S2_TX_EMPH_EN;
  322. writel(val, priv->base + MVEBU_COMPHY_GEN1_S2(lane->id));
  323. val = readl(priv->base + MVEBU_COMPHY_TX_SLEW_RATE(lane->id));
  324. val |= MVEBU_COMPHY_TX_SLEW_RATE_EMPH(0x3) |
  325. MVEBU_COMPHY_TX_SLEW_RATE_SLC(0x3f);
  326. writel(val, priv->base + MVEBU_COMPHY_TX_SLEW_RATE(lane->id));
  327. /* Impedance calibration */
  328. val = readl(priv->base + MVEBU_COMPHY_IMP_CAL(lane->id));
  329. val &= ~MVEBU_COMPHY_IMP_CAL_TX_EXT(0x1f);
  330. val |= MVEBU_COMPHY_IMP_CAL_TX_EXT(0xe) |
  331. MVEBU_COMPHY_IMP_CAL_TX_EXT_EN;
  332. writel(val, priv->base + MVEBU_COMPHY_IMP_CAL(lane->id));
  333. val = readl(priv->base + MVEBU_COMPHY_GEN1_S5(lane->id));
  334. val &= ~MVEBU_COMPHY_GEN1_S5_ICP(0xf);
  335. writel(val, priv->base + MVEBU_COMPHY_GEN1_S5(lane->id));
  336. val = readl(priv->base + MVEBU_COMPHY_GEN1_S1(lane->id));
  337. val &= ~(MVEBU_COMPHY_GEN1_S1_RX_MUL_PI(0x7) |
  338. MVEBU_COMPHY_GEN1_S1_RX_MUL_PF(0x7) |
  339. MVEBU_COMPHY_GEN1_S1_RX_MUL_FI(0x3) |
  340. MVEBU_COMPHY_GEN1_S1_RX_MUL_FF(0x3));
  341. val |= MVEBU_COMPHY_GEN1_S1_RX_DFE_EN |
  342. MVEBU_COMPHY_GEN1_S1_RX_MUL_PI(0x2) |
  343. MVEBU_COMPHY_GEN1_S1_RX_MUL_PF(0x2) |
  344. MVEBU_COMPHY_GEN1_S1_RX_MUL_FF(0x1) |
  345. MVEBU_COMPHY_GEN1_S1_RX_DIV(0x3);
  346. writel(val, priv->base + MVEBU_COMPHY_GEN1_S1(lane->id));
  347. val = readl(priv->base + MVEBU_COMPHY_COEF(lane->id));
  348. val &= ~(MVEBU_COMPHY_COEF_DFE_EN | MVEBU_COMPHY_COEF_DFE_CTRL);
  349. writel(val, priv->base + MVEBU_COMPHY_COEF(lane->id));
  350. val = readl(priv->base + MVEBU_COMPHY_GEN1_S4(lane->id));
  351. val &= ~MVEBU_COMPHY_GEN1_S4_DFE_RES(0x3);
  352. val |= MVEBU_COMPHY_GEN1_S4_DFE_RES(0x1);
  353. writel(val, priv->base + MVEBU_COMPHY_GEN1_S4(lane->id));
  354. val = readl(priv->base + MVEBU_COMPHY_GEN1_S3(lane->id));
  355. val |= MVEBU_COMPHY_GEN1_S3_FBCK_SEL;
  356. writel(val, priv->base + MVEBU_COMPHY_GEN1_S3(lane->id));
  357. /* rx training timer */
  358. val = readl(priv->base + MVEBU_COMPHY_TRAINING5(lane->id));
  359. val &= ~MVEBU_COMPHY_TRAINING5_RX_TIMER(0x3ff);
  360. val |= MVEBU_COMPHY_TRAINING5_RX_TIMER(0x13);
  361. writel(val, priv->base + MVEBU_COMPHY_TRAINING5(lane->id));
  362. /* tx train peak to peak hold */
  363. val = readl(priv->base + MVEBU_COMPHY_TRAINING0(lane->id));
  364. val |= MVEBU_COMPHY_TRAINING0_P2P_HOLD;
  365. writel(val, priv->base + MVEBU_COMPHY_TRAINING0(lane->id));
  366. val = readl(priv->base + MVEBU_COMPHY_TX_PRESET(lane->id));
  367. val &= ~MVEBU_COMPHY_TX_PRESET_INDEX(0xf);
  368. val |= MVEBU_COMPHY_TX_PRESET_INDEX(0x2); /* preset coeff */
  369. writel(val, priv->base + MVEBU_COMPHY_TX_PRESET(lane->id));
  370. val = readl(priv->base + MVEBU_COMPHY_FRAME_DETECT3(lane->id));
  371. val &= ~MVEBU_COMPHY_FRAME_DETECT3_LOST_TIMEOUT_EN;
  372. writel(val, priv->base + MVEBU_COMPHY_FRAME_DETECT3(lane->id));
  373. val = readl(priv->base + MVEBU_COMPHY_TX_TRAIN_PRESET(lane->id));
  374. val |= MVEBU_COMPHY_TX_TRAIN_PRESET_16B_AUTO_EN |
  375. MVEBU_COMPHY_TX_TRAIN_PRESET_PRBS11;
  376. writel(val, priv->base + MVEBU_COMPHY_TX_TRAIN_PRESET(lane->id));
  377. val = readl(priv->base + MVEBU_COMPHY_FRAME_DETECT0(lane->id));
  378. val &= ~MVEBU_COMPHY_FRAME_DETECT0_PATN(0x1ff);
  379. val |= MVEBU_COMPHY_FRAME_DETECT0_PATN(0x88);
  380. writel(val, priv->base + MVEBU_COMPHY_FRAME_DETECT0(lane->id));
  381. val = readl(priv->base + MVEBU_COMPHY_DME(lane->id));
  382. val |= MVEBU_COMPHY_DME_ETH_MODE;
  383. writel(val, priv->base + MVEBU_COMPHY_DME(lane->id));
  384. val = readl(priv->base + MVEBU_COMPHY_VDD_CAL0(lane->id));
  385. val |= MVEBU_COMPHY_VDD_CAL0_CONT_MODE;
  386. writel(val, priv->base + MVEBU_COMPHY_VDD_CAL0(lane->id));
  387. val = readl(priv->base + MVEBU_SP_CALIB(lane->id));
  388. val &= ~MVEBU_SP_CALIB_SAMPLER(0x3);
  389. val |= MVEBU_SP_CALIB_SAMPLER(0x3) |
  390. MVEBU_SP_CALIB_SAMPLER_EN;
  391. writel(val, priv->base + MVEBU_SP_CALIB(lane->id));
  392. val &= ~MVEBU_SP_CALIB_SAMPLER_EN;
  393. writel(val, priv->base + MVEBU_SP_CALIB(lane->id));
  394. /* External rx regulator */
  395. val = readl(priv->base + MVEBU_COMPHY_EXT_SELV(lane->id));
  396. val &= ~MVEBU_COMPHY_EXT_SELV_RX_SAMPL(0x1f);
  397. val |= MVEBU_COMPHY_EXT_SELV_RX_SAMPL(0x1a);
  398. writel(val, priv->base + MVEBU_COMPHY_EXT_SELV(lane->id));
  399. return mvebu_comphy_init_plls(lane, PHY_MODE_10GKR);
  400. }
  401. static int mvebu_comphy_power_on(struct phy *phy)
  402. {
  403. struct mvebu_comphy_lane *lane = phy_get_drvdata(phy);
  404. struct mvebu_comphy_priv *priv = lane->priv;
  405. int ret;
  406. u32 mux, val;
  407. mux = mvebu_comphy_get_mux(lane->id, lane->port, lane->mode);
  408. if (mux < 0)
  409. return -ENOTSUPP;
  410. regmap_read(priv->regmap, MVEBU_COMPHY_SELECTOR, &val);
  411. val &= ~(0xf << MVEBU_COMPHY_SELECTOR_PHY(lane->id));
  412. val |= mux << MVEBU_COMPHY_SELECTOR_PHY(lane->id);
  413. regmap_write(priv->regmap, MVEBU_COMPHY_SELECTOR, val);
  414. switch (lane->mode) {
  415. case PHY_MODE_SGMII:
  416. ret = mvebu_comphy_set_mode_sgmii(phy);
  417. break;
  418. case PHY_MODE_10GKR:
  419. ret = mvebu_comphy_set_mode_10gkr(phy);
  420. break;
  421. default:
  422. return -ENOTSUPP;
  423. }
  424. /* digital reset */
  425. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  426. val |= MVEBU_COMPHY_SERDES_CFG1_RF_RESET;
  427. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  428. return ret;
  429. }
  430. static int mvebu_comphy_set_mode(struct phy *phy, enum phy_mode mode)
  431. {
  432. struct mvebu_comphy_lane *lane = phy_get_drvdata(phy);
  433. if (mvebu_comphy_get_mux(lane->id, lane->port, mode) < 0)
  434. return -EINVAL;
  435. lane->mode = mode;
  436. return 0;
  437. }
  438. static int mvebu_comphy_power_off(struct phy *phy)
  439. {
  440. struct mvebu_comphy_lane *lane = phy_get_drvdata(phy);
  441. struct mvebu_comphy_priv *priv = lane->priv;
  442. u32 val;
  443. val = readl(priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  444. val &= ~(MVEBU_COMPHY_SERDES_CFG1_RESET |
  445. MVEBU_COMPHY_SERDES_CFG1_CORE_RESET |
  446. MVEBU_COMPHY_SERDES_CFG1_RF_RESET);
  447. writel(val, priv->base + MVEBU_COMPHY_SERDES_CFG1(lane->id));
  448. regmap_read(priv->regmap, MVEBU_COMPHY_SELECTOR, &val);
  449. val &= ~(0xf << MVEBU_COMPHY_SELECTOR_PHY(lane->id));
  450. regmap_write(priv->regmap, MVEBU_COMPHY_SELECTOR, val);
  451. return 0;
  452. }
  453. static const struct phy_ops mvebu_comphy_ops = {
  454. .power_on = mvebu_comphy_power_on,
  455. .power_off = mvebu_comphy_power_off,
  456. .set_mode = mvebu_comphy_set_mode,
  457. .owner = THIS_MODULE,
  458. };
  459. static struct phy *mvebu_comphy_xlate(struct device *dev,
  460. struct of_phandle_args *args)
  461. {
  462. struct mvebu_comphy_lane *lane;
  463. struct phy *phy;
  464. if (WARN_ON(args->args[0] >= MVEBU_COMPHY_PORTS))
  465. return ERR_PTR(-EINVAL);
  466. phy = of_phy_simple_xlate(dev, args);
  467. if (IS_ERR(phy))
  468. return phy;
  469. lane = phy_get_drvdata(phy);
  470. if (lane->port >= 0)
  471. return ERR_PTR(-EBUSY);
  472. lane->port = args->args[0];
  473. return phy;
  474. }
  475. static int mvebu_comphy_probe(struct platform_device *pdev)
  476. {
  477. struct mvebu_comphy_priv *priv;
  478. struct phy_provider *provider;
  479. struct device_node *child;
  480. struct resource *res;
  481. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  482. if (!priv)
  483. return -ENOMEM;
  484. priv->dev = &pdev->dev;
  485. priv->regmap =
  486. syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  487. "marvell,system-controller");
  488. if (IS_ERR(priv->regmap))
  489. return PTR_ERR(priv->regmap);
  490. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  491. priv->base = devm_ioremap_resource(&pdev->dev, res);
  492. if (!priv->base)
  493. return -ENOMEM;
  494. for_each_available_child_of_node(pdev->dev.of_node, child) {
  495. struct mvebu_comphy_lane *lane;
  496. struct phy *phy;
  497. int ret;
  498. u32 val;
  499. ret = of_property_read_u32(child, "reg", &val);
  500. if (ret < 0) {
  501. dev_err(&pdev->dev, "missing 'reg' property (%d)\n",
  502. ret);
  503. continue;
  504. }
  505. if (val >= MVEBU_COMPHY_LANES) {
  506. dev_err(&pdev->dev, "invalid 'reg' property\n");
  507. continue;
  508. }
  509. lane = devm_kzalloc(&pdev->dev, sizeof(*lane), GFP_KERNEL);
  510. if (!lane)
  511. return -ENOMEM;
  512. phy = devm_phy_create(&pdev->dev, child, &mvebu_comphy_ops);
  513. if (IS_ERR(phy))
  514. return PTR_ERR(phy);
  515. lane->priv = priv;
  516. lane->mode = PHY_MODE_INVALID;
  517. lane->id = val;
  518. lane->port = -1;
  519. phy_set_drvdata(phy, lane);
  520. /*
  521. * Once all modes are supported in this driver we should call
  522. * mvebu_comphy_power_off(phy) here to avoid relying on the
  523. * bootloader/firmware configuration.
  524. */
  525. }
  526. dev_set_drvdata(&pdev->dev, priv);
  527. provider = devm_of_phy_provider_register(&pdev->dev,
  528. mvebu_comphy_xlate);
  529. return PTR_ERR_OR_ZERO(provider);
  530. }
  531. static const struct of_device_id mvebu_comphy_of_match_table[] = {
  532. { .compatible = "marvell,comphy-cp110" },
  533. { },
  534. };
  535. MODULE_DEVICE_TABLE(of, mvebu_comphy_of_match_table);
  536. static struct platform_driver mvebu_comphy_driver = {
  537. .probe = mvebu_comphy_probe,
  538. .driver = {
  539. .name = "mvebu-comphy",
  540. .of_match_table = mvebu_comphy_of_match_table,
  541. },
  542. };
  543. module_platform_driver(mvebu_comphy_driver);
  544. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  545. MODULE_DESCRIPTION("Common PHY driver for mvebu SoCs");
  546. MODULE_LICENSE("GPL v2");