739-02-phy-add-driver-for-MediaTek-XFI-T-PHY.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. From patchwork Thu Feb 1 21:53:06 2024
  2. Content-Type: text/plain; charset="utf-8"
  3. MIME-Version: 1.0
  4. Content-Transfer-Encoding: 7bit
  5. X-Patchwork-Submitter: Daniel Golle <[email protected]>
  6. X-Patchwork-Id: 13541843
  7. Date: Thu, 1 Feb 2024 21:53:06 +0000
  8. From: Daniel Golle <[email protected]>
  9. To: Bc-bocun Chen <[email protected]>,
  10. Chunfeng Yun <[email protected]>,
  11. Vinod Koul <[email protected]>,
  12. Kishon Vijay Abraham I <[email protected]>,
  13. Rob Herring <[email protected]>,
  14. Krzysztof Kozlowski <[email protected]>,
  15. Conor Dooley <[email protected]>,
  16. Daniel Golle <[email protected]>,
  17. Qingfang Deng <[email protected]>,
  18. SkyLake Huang <[email protected]>,
  19. Matthias Brugger <[email protected]>,
  20. AngeloGioacchino Del Regno <[email protected]>,
  21. Philipp Zabel <[email protected]>,
  22. [email protected],
  23. [email protected], [email protected],
  24. [email protected], [email protected],
  25. [email protected]
  26. Subject: [PATCH 2/2] phy: add driver for MediaTek XFI T-PHY
  27. Message-ID:
  28. <dd6b40ea1f7f8459a9a2cfe7fa60c1108332ade6.1706823233.git.daniel@makrotopia.org>
  29. References:
  30. <702afb0c1246d95c90b22e57105304028bdd3083.1706823233.git.daniel@makrotopia.org>
  31. MIME-Version: 1.0
  32. Content-Disposition: inline
  33. In-Reply-To:
  34. <702afb0c1246d95c90b22e57105304028bdd3083.1706823233.git.daniel@makrotopia.org>
  35. List-Id: Linux Phy Mailing list <linux-phy.lists.infradead.org>
  36. Add driver for MediaTek's XFI T-PHY, 10 Gigabit/s Ethernet SerDes PHY
  37. which can be found in the MT7988 SoC.
  38. The PHY can operates only in PHY_MODE_ETHERNET, the submode is one of
  39. PHY_INTERFACE_MODE_* corresponding to the supported modes:
  40. * USXGMII \
  41. * 10GBase-R }- USXGMII PCS - XGDM \
  42. * 5GBase-R / \
  43. }- Ethernet MAC
  44. * 2500Base-X \ /
  45. * 1000Base-X }- LynxI PCS - GDM /
  46. * Cisco SGMII (MAC side) /
  47. In order to work-around a performance issue present on the first of
  48. two XFI T-PHYs present in MT7988, special tuning is applied which can be
  49. selected by adding the 'mediatek,usxgmii-performance-errata' property to
  50. the device tree node.
  51. There is no documentation for most registers used for the
  52. analog/tuning part, however, most of the registers have been partially
  53. reverse-engineered from MediaTek's SDK implementation (an opaque
  54. sequence of 32-bit register writes) and descriptions for all relevant
  55. digital registers and bits such as resets and muxes have been supplied
  56. by MediaTek.
  57. Signed-off-by: Daniel Golle <[email protected]>
  58. ---
  59. MAINTAINERS | 1 +
  60. drivers/phy/mediatek/Kconfig | 12 +
  61. drivers/phy/mediatek/Makefile | 1 +
  62. drivers/phy/mediatek/phy-mtk-xfi-tphy.c | 392 ++++++++++++++++++++++++
  63. 4 files changed, 406 insertions(+)
  64. create mode 100644 drivers/phy/mediatek/phy-mtk-xfi-tphy.c
  65. --- a/drivers/phy/mediatek/Kconfig
  66. +++ b/drivers/phy/mediatek/Kconfig
  67. @@ -13,6 +13,18 @@ config PHY_MTK_PCIE
  68. callback for PCIe GEN3 port, it supports software efuse
  69. initialization.
  70. +config PHY_MTK_XFI_TPHY
  71. + tristate "MediaTek XFI T-PHY Driver"
  72. + depends on ARCH_MEDIATEK || COMPILE_TEST
  73. + depends on OF && OF_ADDRESS
  74. + depends on HAS_IOMEM
  75. + select GENERIC_PHY
  76. + help
  77. + Say 'Y' here to add support for MediaTek XFI T-PHY driver.
  78. + The driver provides access to the Ethernet SerDes T-PHY supporting
  79. + 1GE and 2.5GE modes via the LynxI PCS, and 5GE and 10GE modes
  80. + via the USXGMII PCS found in MediaTek SoCs with 10G Ethernet.
  81. +
  82. config PHY_MTK_TPHY
  83. tristate "MediaTek T-PHY Driver"
  84. depends on ARCH_MEDIATEK || COMPILE_TEST
  85. --- a/drivers/phy/mediatek/Makefile
  86. +++ b/drivers/phy/mediatek/Makefile
  87. @@ -8,6 +8,7 @@ obj-$(CONFIG_PHY_MTK_PCIE) += phy-mtk-p
  88. obj-$(CONFIG_PHY_MTK_TPHY) += phy-mtk-tphy.o
  89. obj-$(CONFIG_PHY_MTK_UFS) += phy-mtk-ufs.o
  90. obj-$(CONFIG_PHY_MTK_XSPHY) += phy-mtk-xsphy.o
  91. +obj-$(CONFIG_PHY_MTK_XFI_TPHY) += phy-mtk-xfi-tphy.o
  92. phy-mtk-hdmi-drv-y := phy-mtk-hdmi.o
  93. phy-mtk-hdmi-drv-y += phy-mtk-hdmi-mt2701.o
  94. --- /dev/null
  95. +++ b/drivers/phy/mediatek/phy-mtk-xfi-tphy.c
  96. @@ -0,0 +1,393 @@
  97. +// SPDX-License-Identifier: GPL-2.0-or-later
  98. +/* MediaTek 10GE SerDes PHY driver
  99. + *
  100. + * Copyright (c) 2024 Daniel Golle <[email protected]>
  101. + * Bc-bocun Chen <[email protected]>
  102. + * based on mtk_usxgmii.c found in MediaTek's SDK released under GPL-2.0
  103. + * Copyright (c) 2022 MediaTek Inc.
  104. + * Author: Henry Yen <[email protected]>
  105. + */
  106. +
  107. +#include <linux/module.h>
  108. +#include <linux/device.h>
  109. +#include <linux/platform_device.h>
  110. +#include <linux/of.h>
  111. +#include <linux/io.h>
  112. +#include <linux/clk.h>
  113. +#include <linux/reset.h>
  114. +#include <linux/phy.h>
  115. +#include <linux/phy/phy.h>
  116. +
  117. +#define MTK_XFI_TPHY_NUM_CLOCKS 2
  118. +
  119. +#define REG_DIG_GLB_70 0x0070
  120. +#define XTP_PCS_RX_EQ_IN_PROGRESS(x) FIELD_PREP(GENMASK(25, 24), (x))
  121. +#define XTP_PCS_MODE_MASK GENMASK(17, 16)
  122. +#define XTP_PCS_MODE(x) FIELD_PREP(GENMASK(17, 16), (x))
  123. +#define XTP_PCS_RST_B BIT(15)
  124. +#define XTP_FRC_PCS_RST_B BIT(14)
  125. +#define XTP_PCS_PWD_SYNC_MASK GENMASK(13, 12)
  126. +#define XTP_PCS_PWD_SYNC(x) FIELD_PREP(XTP_PCS_PWD_SYNC_MASK, (x))
  127. +#define XTP_PCS_PWD_ASYNC_MASK GENMASK(11, 10)
  128. +#define XTP_PCS_PWD_ASYNC(x) FIELD_PREP(XTP_PCS_PWD_ASYNC_MASK, (x))
  129. +#define XTP_FRC_PCS_PWD_ASYNC BIT(8)
  130. +#define XTP_PCS_UPDT BIT(4)
  131. +#define XTP_PCS_IN_FR_RG BIT(0)
  132. +
  133. +#define REG_DIG_GLB_F4 0x00f4
  134. +#define XFI_DPHY_PCS_SEL BIT(0)
  135. +#define XFI_DPHY_PCS_SEL_SGMII FIELD_PREP(XFI_DPHY_PCS_SEL, 1)
  136. +#define XFI_DPHY_PCS_SEL_USXGMII FIELD_PREP(XFI_DPHY_PCS_SEL, 0)
  137. +#define XFI_DPHY_AD_SGDT_FRC_EN BIT(5)
  138. +
  139. +#define REG_DIG_LN_TRX_40 0x3040
  140. +#define XTP_LN_FRC_TX_DATA_EN BIT(29)
  141. +#define XTP_LN_TX_DATA_EN BIT(28)
  142. +
  143. +#define REG_DIG_LN_TRX_B0 0x30b0
  144. +#define XTP_LN_FRC_TX_MACCK_EN BIT(5)
  145. +#define XTP_LN_TX_MACCK_EN BIT(4)
  146. +
  147. +#define REG_ANA_GLB_D0 0x90d0
  148. +#define XTP_GLB_USXGMII_SEL_MASK GENMASK(3, 1)
  149. +#define XTP_GLB_USXGMII_SEL(x) FIELD_PREP(GENMASK(3, 1), (x))
  150. +#define XTP_GLB_USXGMII_EN BIT(0)
  151. +
  152. +struct mtk_xfi_tphy {
  153. + void __iomem *base;
  154. + struct device *dev;
  155. + struct reset_control *reset;
  156. + struct clk_bulk_data clocks[MTK_XFI_TPHY_NUM_CLOCKS];
  157. + bool da_war;
  158. +};
  159. +
  160. +static void mtk_xfi_tphy_write(struct mtk_xfi_tphy *xfi_tphy, u16 reg,
  161. + u32 value)
  162. +{
  163. + iowrite32(value, xfi_tphy->base + reg);
  164. +}
  165. +
  166. +static void mtk_xfi_tphy_rmw(struct mtk_xfi_tphy *xfi_tphy, u16 reg,
  167. + u32 clr, u32 set)
  168. +{
  169. + u32 val;
  170. +
  171. + val = ioread32(xfi_tphy->base + reg);
  172. + val &= ~clr;
  173. + val |= set;
  174. + iowrite32(val, xfi_tphy->base + reg);
  175. +}
  176. +
  177. +static void mtk_xfi_tphy_set(struct mtk_xfi_tphy *xfi_tphy, u16 reg,
  178. + u32 set)
  179. +{
  180. + mtk_xfi_tphy_rmw(xfi_tphy, reg, 0, set);
  181. +}
  182. +
  183. +static void mtk_xfi_tphy_clear(struct mtk_xfi_tphy *xfi_tphy, u16 reg,
  184. + u32 clr)
  185. +{
  186. + mtk_xfi_tphy_rmw(xfi_tphy, reg, clr, 0);
  187. +}
  188. +
  189. +static void mtk_xfi_tphy_setup(struct mtk_xfi_tphy *xfi_tphy,
  190. + phy_interface_t interface)
  191. +{
  192. + bool is_2p5g = (interface == PHY_INTERFACE_MODE_2500BASEX);
  193. + bool is_1g = (interface == PHY_INTERFACE_MODE_1000BASEX ||
  194. + interface == PHY_INTERFACE_MODE_SGMII);
  195. + bool is_10g = (interface == PHY_INTERFACE_MODE_10GBASER ||
  196. + interface == PHY_INTERFACE_MODE_USXGMII);
  197. + bool is_5g = (interface == PHY_INTERFACE_MODE_5GBASER);
  198. + bool is_xgmii = (is_10g || is_5g);
  199. +
  200. + dev_dbg(xfi_tphy->dev, "setting up for mode %s\n", phy_modes(interface));
  201. +
  202. + /* Setup PLL setting */
  203. + mtk_xfi_tphy_rmw(xfi_tphy, 0x9024, 0x100000, is_10g ? 0x0 : 0x100000);
  204. + mtk_xfi_tphy_rmw(xfi_tphy, 0x2020, 0x202000, is_5g ? 0x202000 : 0x0);
  205. + mtk_xfi_tphy_rmw(xfi_tphy, 0x2030, 0x500, is_1g ? 0x0 : 0x500);
  206. + mtk_xfi_tphy_rmw(xfi_tphy, 0x2034, 0xa00, is_1g ? 0x0 : 0xa00);
  207. + mtk_xfi_tphy_rmw(xfi_tphy, 0x2040, 0x340000, is_1g ? 0x200000 :
  208. + 0x140000);
  209. +
  210. + /* Setup RXFE BW setting */
  211. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50f0, 0xc10, is_1g ? 0x410 :
  212. + is_5g ? 0x800 : 0x400);
  213. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50e0, 0x4000, is_5g ? 0x0 : 0x4000);
  214. +
  215. + /* Setup RX CDR setting */
  216. + mtk_xfi_tphy_rmw(xfi_tphy, 0x506c, 0x30000, is_5g ? 0x0 : 0x30000);
  217. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5070, 0x670000, is_5g ? 0x620000 : 0x50000);
  218. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5074, 0x180000, is_5g ? 0x180000 : 0x0);
  219. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5078, 0xf000400, is_5g ? 0x8000000 :
  220. + 0x7000400);
  221. + mtk_xfi_tphy_rmw(xfi_tphy, 0x507c, 0x5000500, is_5g ? 0x4000400 :
  222. + 0x1000100);
  223. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5080, 0x1410, is_1g ? 0x400 :
  224. + is_5g ? 0x1010 : 0x0);
  225. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5084, 0x30300, is_1g ? 0x30300 :
  226. + is_5g ? 0x30100 :
  227. + 0x100);
  228. + mtk_xfi_tphy_rmw(xfi_tphy, 0x5088, 0x60200, is_1g ? 0x20200 :
  229. + is_5g ? 0x40000 :
  230. + 0x20000);
  231. +
  232. + /* Setting RXFE adaptation range setting */
  233. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50e4, 0xc0000, is_5g ? 0x0 : 0xc0000);
  234. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50e8, 0x40000, is_5g ? 0x0 : 0x40000);
  235. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50ec, 0xa00, is_1g ? 0x200 : 0x800);
  236. + mtk_xfi_tphy_rmw(xfi_tphy, 0x50a8, 0xee0000, is_5g ? 0x800000 :
  237. + 0x6e0000);
  238. + mtk_xfi_tphy_rmw(xfi_tphy, 0x6004, 0x190000, is_5g ? 0x0 : 0x190000);
  239. + if (is_10g)
  240. + mtk_xfi_tphy_write(xfi_tphy, 0x00f8, 0x01423342);
  241. + else if (is_5g)
  242. + mtk_xfi_tphy_write(xfi_tphy, 0x00f8, 0x00a132a1);
  243. + else if (is_2p5g)
  244. + mtk_xfi_tphy_write(xfi_tphy, 0x00f8, 0x009c329c);
  245. + else
  246. + mtk_xfi_tphy_write(xfi_tphy, 0x00f8, 0x00fa32fa);
  247. +
  248. + /* Force SGDT_OUT off and select PCS */
  249. + mtk_xfi_tphy_rmw(xfi_tphy, REG_DIG_GLB_F4,
  250. + XFI_DPHY_AD_SGDT_FRC_EN | XFI_DPHY_PCS_SEL,
  251. + XFI_DPHY_AD_SGDT_FRC_EN |
  252. + (is_xgmii ? XFI_DPHY_PCS_SEL_USXGMII :
  253. + XFI_DPHY_PCS_SEL_SGMII));
  254. +
  255. +
  256. + /* Force GLB_CKDET_OUT */
  257. + mtk_xfi_tphy_set(xfi_tphy, 0x0030, 0xc00);
  258. +
  259. + /* Force AEQ on */
  260. + mtk_xfi_tphy_write(xfi_tphy, REG_DIG_GLB_70,
  261. + XTP_PCS_RX_EQ_IN_PROGRESS(2) |
  262. + XTP_PCS_PWD_SYNC(2) |
  263. + XTP_PCS_PWD_ASYNC(2));
  264. +
  265. + usleep_range(1, 5);
  266. + writel(XTP_LN_FRC_TX_DATA_EN, xfi_tphy->base + REG_DIG_LN_TRX_40);
  267. +
  268. + /* Setup TX DA default value */
  269. + mtk_xfi_tphy_rmw(xfi_tphy, 0x30b0, 0x30, 0x20);
  270. + mtk_xfi_tphy_write(xfi_tphy, 0x3028, 0x00008a01);
  271. + mtk_xfi_tphy_write(xfi_tphy, 0x302c, 0x0000a884);
  272. + mtk_xfi_tphy_write(xfi_tphy, 0x3024, 0x00083002);
  273. +
  274. + /* Setup RG default value */
  275. + if (is_xgmii) {
  276. + mtk_xfi_tphy_write(xfi_tphy, 0x3010, 0x00022220);
  277. + mtk_xfi_tphy_write(xfi_tphy, 0x5064, 0x0f020a01);
  278. + mtk_xfi_tphy_write(xfi_tphy, 0x50b4, 0x06100600);
  279. + if (interface == PHY_INTERFACE_MODE_USXGMII)
  280. + mtk_xfi_tphy_write(xfi_tphy, 0x3048, 0x40704000);
  281. + else
  282. + mtk_xfi_tphy_write(xfi_tphy, 0x3048, 0x47684100);
  283. + } else {
  284. + mtk_xfi_tphy_write(xfi_tphy, 0x3010, 0x00011110);
  285. + mtk_xfi_tphy_write(xfi_tphy, 0x3048, 0x40704000);
  286. + }
  287. +
  288. + if (is_1g)
  289. + mtk_xfi_tphy_write(xfi_tphy, 0x3064, 0x0000c000);
  290. +
  291. + /* Setup RX EQ initial value */
  292. + mtk_xfi_tphy_rmw(xfi_tphy, 0x3050, 0xa8000000,
  293. + (interface != PHY_INTERFACE_MODE_10GBASER) ?
  294. + 0xa8000000 : 0x0);
  295. + mtk_xfi_tphy_rmw(xfi_tphy, 0x3054, 0xaa,
  296. + (interface != PHY_INTERFACE_MODE_10GBASER) ?
  297. + 0xaa : 0x0);
  298. +
  299. + if (is_xgmii)
  300. + mtk_xfi_tphy_write(xfi_tphy, 0x306c, 0x00000f00);
  301. + else if (is_2p5g)
  302. + mtk_xfi_tphy_write(xfi_tphy, 0x306c, 0x22000f00);
  303. + else
  304. + mtk_xfi_tphy_write(xfi_tphy, 0x306c, 0x20200f00);
  305. +
  306. + if (interface == PHY_INTERFACE_MODE_10GBASER && xfi_tphy->da_war)
  307. + mtk_xfi_tphy_rmw(xfi_tphy, 0xa008, 0x10000, 0x10000);
  308. +
  309. + mtk_xfi_tphy_rmw(xfi_tphy, 0xa060, 0x50000, is_xgmii ? 0x40000 :
  310. + 0x50000);
  311. +
  312. + /* Setup PHYA speed */
  313. + mtk_xfi_tphy_rmw(xfi_tphy, REG_ANA_GLB_D0,
  314. + XTP_GLB_USXGMII_SEL_MASK | XTP_GLB_USXGMII_EN,
  315. + is_10g ? XTP_GLB_USXGMII_SEL(0) :
  316. + is_5g ? XTP_GLB_USXGMII_SEL(1) :
  317. + is_2p5g ? XTP_GLB_USXGMII_SEL(2) :
  318. + XTP_GLB_USXGMII_SEL(3));
  319. + mtk_xfi_tphy_set(xfi_tphy, REG_ANA_GLB_D0, XTP_GLB_USXGMII_EN);
  320. +
  321. + /* Release reset */
  322. + mtk_xfi_tphy_set(xfi_tphy, REG_DIG_GLB_70,
  323. + XTP_PCS_RST_B | XTP_FRC_PCS_RST_B);
  324. + usleep_range(150, 500);
  325. +
  326. + /* Switch to P0 */
  327. + mtk_xfi_tphy_rmw(xfi_tphy, REG_DIG_GLB_70,
  328. + XTP_PCS_PWD_SYNC_MASK |
  329. + XTP_PCS_PWD_ASYNC_MASK,
  330. + XTP_FRC_PCS_PWD_ASYNC |
  331. + XTP_PCS_UPDT | XTP_PCS_IN_FR_RG);
  332. + usleep_range(1, 5);
  333. +
  334. + mtk_xfi_tphy_clear(xfi_tphy, REG_DIG_GLB_70, XTP_PCS_UPDT);
  335. + usleep_range(15, 50);
  336. +
  337. + if (is_xgmii) {
  338. + /* Switch to Gen3 */
  339. + mtk_xfi_tphy_rmw(xfi_tphy, REG_DIG_GLB_70,
  340. + XTP_PCS_MODE_MASK | XTP_PCS_UPDT,
  341. + XTP_PCS_MODE(2) | XTP_PCS_UPDT);
  342. + } else {
  343. + /* Switch to Gen2 */
  344. + mtk_xfi_tphy_rmw(xfi_tphy, REG_DIG_GLB_70,
  345. + XTP_PCS_MODE_MASK | XTP_PCS_UPDT,
  346. + XTP_PCS_MODE(1) | XTP_PCS_UPDT);
  347. + }
  348. + usleep_range(1, 5);
  349. +
  350. + mtk_xfi_tphy_clear(xfi_tphy, REG_DIG_GLB_70, XTP_PCS_UPDT);
  351. +
  352. + usleep_range(100, 500);
  353. +
  354. + /* Enable MAC CK */
  355. + mtk_xfi_tphy_set(xfi_tphy, REG_DIG_LN_TRX_B0, XTP_LN_TX_MACCK_EN);
  356. + mtk_xfi_tphy_clear(xfi_tphy, REG_DIG_GLB_F4, XFI_DPHY_AD_SGDT_FRC_EN);
  357. +
  358. + /* Enable TX data */
  359. + mtk_xfi_tphy_set(xfi_tphy, REG_DIG_LN_TRX_40,
  360. + XTP_LN_FRC_TX_DATA_EN | XTP_LN_TX_DATA_EN);
  361. + usleep_range(400, 1000);
  362. +}
  363. +
  364. +static int mtk_xfi_tphy_set_mode(struct phy *phy, enum phy_mode mode, int
  365. + submode)
  366. +{
  367. + struct mtk_xfi_tphy *xfi_tphy = phy_get_drvdata(phy);
  368. +
  369. + if (mode != PHY_MODE_ETHERNET)
  370. + return -EINVAL;
  371. +
  372. + switch (submode) {
  373. + case PHY_INTERFACE_MODE_1000BASEX:
  374. + case PHY_INTERFACE_MODE_2500BASEX:
  375. + case PHY_INTERFACE_MODE_SGMII:
  376. + case PHY_INTERFACE_MODE_5GBASER:
  377. + case PHY_INTERFACE_MODE_10GBASER:
  378. + case PHY_INTERFACE_MODE_USXGMII:
  379. + mtk_xfi_tphy_setup(xfi_tphy, submode);
  380. + return 0;
  381. + default:
  382. + return -EINVAL;
  383. + }
  384. +}
  385. +
  386. +static int mtk_xfi_tphy_reset(struct phy *phy)
  387. +{
  388. + struct mtk_xfi_tphy *xfi_tphy = phy_get_drvdata(phy);
  389. +
  390. + reset_control_assert(xfi_tphy->reset);
  391. + usleep_range(100, 500);
  392. + reset_control_deassert(xfi_tphy->reset);
  393. + usleep_range(1, 10);
  394. +
  395. + return 0;
  396. +}
  397. +
  398. +static int mtk_xfi_tphy_power_on(struct phy *phy)
  399. +{
  400. + struct mtk_xfi_tphy *xfi_tphy = phy_get_drvdata(phy);
  401. +
  402. + return clk_bulk_prepare_enable(MTK_XFI_TPHY_NUM_CLOCKS, xfi_tphy->clocks);
  403. +}
  404. +
  405. +static int mtk_xfi_tphy_power_off(struct phy *phy)
  406. +{
  407. + struct mtk_xfi_tphy *xfi_tphy = phy_get_drvdata(phy);
  408. +
  409. + clk_bulk_disable_unprepare(MTK_XFI_TPHY_NUM_CLOCKS, xfi_tphy->clocks);
  410. +
  411. + return 0;
  412. +}
  413. +
  414. +static const struct phy_ops mtk_xfi_tphy_ops = {
  415. + .power_on = mtk_xfi_tphy_power_on,
  416. + .power_off = mtk_xfi_tphy_power_off,
  417. + .set_mode = mtk_xfi_tphy_set_mode,
  418. + .reset = mtk_xfi_tphy_reset,
  419. + .owner = THIS_MODULE,
  420. +};
  421. +
  422. +static int mtk_xfi_tphy_probe(struct platform_device *pdev)
  423. +{
  424. + struct device_node *np = pdev->dev.of_node;
  425. + struct phy_provider *phy_provider;
  426. + struct mtk_xfi_tphy *xfi_tphy;
  427. + struct phy *phy;
  428. +
  429. + if (!np)
  430. + return -ENODEV;
  431. +
  432. + xfi_tphy = devm_kzalloc(&pdev->dev, sizeof(*xfi_tphy), GFP_KERNEL);
  433. + if (!xfi_tphy)
  434. + return -ENOMEM;
  435. +
  436. + xfi_tphy->base = devm_of_iomap(&pdev->dev, np, 0, NULL);
  437. + if (!xfi_tphy->base)
  438. + return -EIO;
  439. +
  440. + xfi_tphy->dev = &pdev->dev;
  441. +
  442. + xfi_tphy->clocks[0].id = "topxtal";
  443. + xfi_tphy->clocks[0].clk = devm_clk_get(&pdev->dev, xfi_tphy->clocks[0].id);
  444. + if (IS_ERR(xfi_tphy->clocks[0].clk))
  445. + return PTR_ERR(xfi_tphy->clocks[0].clk);
  446. +
  447. + xfi_tphy->clocks[1].id = "xfipll";
  448. + xfi_tphy->clocks[1].clk = devm_clk_get(&pdev->dev, xfi_tphy->clocks[1].id);
  449. + if (IS_ERR(xfi_tphy->clocks[1].clk))
  450. + return PTR_ERR(xfi_tphy->clocks[1].clk);
  451. +
  452. + xfi_tphy->reset = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  453. + if (IS_ERR(xfi_tphy->reset))
  454. + return PTR_ERR(xfi_tphy->reset);
  455. +
  456. + xfi_tphy->da_war = of_property_read_bool(np,
  457. + "mediatek,usxgmii-performance-errata");
  458. +
  459. + phy = devm_phy_create(&pdev->dev, NULL, &mtk_xfi_tphy_ops);
  460. + if (IS_ERR(phy))
  461. + return PTR_ERR(phy);
  462. +
  463. + phy_set_drvdata(phy, xfi_tphy);
  464. +
  465. + phy_provider = devm_of_phy_provider_register(&pdev->dev,
  466. + of_phy_simple_xlate);
  467. +
  468. + return PTR_ERR_OR_ZERO(phy_provider);
  469. +}
  470. +
  471. +static const struct of_device_id mtk_xfi_tphy_match[] = {
  472. + { .compatible = "mediatek,mt7988-xfi-tphy", },
  473. + { }
  474. +};
  475. +MODULE_DEVICE_TABLE(of, mtk_xfi_tphy_match);
  476. +
  477. +static struct platform_driver mtk_xfi_tphy_driver = {
  478. + .probe = mtk_xfi_tphy_probe,
  479. + .driver = {
  480. + .name = "mtk-xfi-tphy",
  481. + .of_match_table = mtk_xfi_tphy_match,
  482. + },
  483. +};
  484. +module_platform_driver(mtk_xfi_tphy_driver);
  485. +
  486. +MODULE_DESCRIPTION("MediaTek XFI T-PHY driver");
  487. +MODULE_AUTHOR("Daniel Golle <[email protected]>");
  488. +MODULE_AUTHOR("Bc-bocun Chen <[email protected]>");
  489. +MODULE_LICENSE("GPL");