at803x.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * drivers/net/phy/at803x.c
  4. *
  5. * Driver for Qualcomm Atheros AR803x PHY
  6. *
  7. * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
  8. */
  9. #include <linux/phy.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/ethtool_netlink.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/of.h>
  20. #include <linux/phylink.h>
  21. #include <linux/sfp.h>
  22. #include <dt-bindings/net/qca-ar803x.h>
  23. #include "qcom.h"
  24. #define AT803X_LED_CONTROL 0x18
  25. #define AT803X_REG_CHIP_CONFIG 0x1f
  26. #define AT803X_BT_BX_REG_SEL 0x8000
  27. #define AT803X_MODE_CFG_MASK 0x0F
  28. #define AT803X_MODE_CFG_BASET_RGMII 0x00
  29. #define AT803X_MODE_CFG_BASET_SGMII 0x01
  30. #define AT803X_MODE_CFG_BX1000_RGMII_50OHM 0x02
  31. #define AT803X_MODE_CFG_BX1000_RGMII_75OHM 0x03
  32. #define AT803X_MODE_CFG_BX1000_CONV_50OHM 0x04
  33. #define AT803X_MODE_CFG_BX1000_CONV_75OHM 0x05
  34. #define AT803X_MODE_CFG_FX100_RGMII_50OHM 0x06
  35. #define AT803X_MODE_CFG_FX100_CONV_50OHM 0x07
  36. #define AT803X_MODE_CFG_RGMII_AUTO_MDET 0x0B
  37. #define AT803X_MODE_CFG_FX100_RGMII_75OHM 0x0E
  38. #define AT803X_MODE_CFG_FX100_CONV_75OHM 0x0F
  39. #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
  40. #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
  41. #define AT803X_DEBUG_REG_1F 0x1F
  42. #define AT803X_DEBUG_PLL_ON BIT(2)
  43. #define AT803X_DEBUG_RGMII_1V8 BIT(3)
  44. /* AT803x supports either the XTAL input pad, an internal PLL or the
  45. * DSP as clock reference for the clock output pad. The XTAL reference
  46. * is only used for 25 MHz output, all other frequencies need the PLL.
  47. * The DSP as a clock reference is used in synchronous ethernet
  48. * applications.
  49. *
  50. * By default the PLL is only enabled if there is a link. Otherwise
  51. * the PHY will go into low power state and disabled the PLL. You can
  52. * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
  53. * enabled.
  54. */
  55. #define AT803X_MMD7_CLK25M 0x8016
  56. #define AT803X_CLK_OUT_MASK GENMASK(4, 2)
  57. #define AT803X_CLK_OUT_25MHZ_XTAL 0
  58. #define AT803X_CLK_OUT_25MHZ_DSP 1
  59. #define AT803X_CLK_OUT_50MHZ_PLL 2
  60. #define AT803X_CLK_OUT_50MHZ_DSP 3
  61. #define AT803X_CLK_OUT_62_5MHZ_PLL 4
  62. #define AT803X_CLK_OUT_62_5MHZ_DSP 5
  63. #define AT803X_CLK_OUT_125MHZ_PLL 6
  64. #define AT803X_CLK_OUT_125MHZ_DSP 7
  65. /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
  66. * but doesn't support choosing between XTAL/PLL and DSP.
  67. */
  68. #define AT8035_CLK_OUT_MASK GENMASK(4, 3)
  69. #define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7)
  70. #define AT803X_CLK_OUT_STRENGTH_FULL 0
  71. #define AT803X_CLK_OUT_STRENGTH_HALF 1
  72. #define AT803X_CLK_OUT_STRENGTH_QUARTER 2
  73. #define AT803X_MMD3_SMARTEEE_CTL1 0x805b
  74. #define AT803X_MMD3_SMARTEEE_CTL2 0x805c
  75. #define AT803X_MMD3_SMARTEEE_CTL3 0x805d
  76. #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN BIT(8)
  77. #define ATH9331_PHY_ID 0x004dd041
  78. #define ATH8030_PHY_ID 0x004dd076
  79. #define ATH8031_PHY_ID 0x004dd074
  80. #define ATH8032_PHY_ID 0x004dd023
  81. #define ATH8035_PHY_ID 0x004dd072
  82. #define AT8030_PHY_ID_MASK 0xffffffef
  83. #define QCA9561_PHY_ID 0x004dd042
  84. #define AT803X_PAGE_FIBER 0
  85. #define AT803X_PAGE_COPPER 1
  86. /* don't turn off internal PLL */
  87. #define AT803X_KEEP_PLL_ENABLED BIT(0)
  88. #define AT803X_DISABLE_SMARTEEE BIT(1)
  89. /* disable hibernation mode */
  90. #define AT803X_DISABLE_HIBERNATION_MODE BIT(2)
  91. MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
  92. MODULE_AUTHOR("Matus Ujhelyi");
  93. MODULE_LICENSE("GPL");
  94. struct at803x_priv {
  95. int flags;
  96. u16 clk_25m_reg;
  97. u16 clk_25m_mask;
  98. u8 smarteee_lpi_tw_1g;
  99. u8 smarteee_lpi_tw_100m;
  100. bool is_fiber;
  101. bool is_1000basex;
  102. struct regulator_dev *vddio_rdev;
  103. struct regulator_dev *vddh_rdev;
  104. };
  105. struct at803x_context {
  106. u16 bmcr;
  107. u16 advertise;
  108. u16 control1000;
  109. u16 int_enable;
  110. u16 smart_speed;
  111. u16 led_control;
  112. };
  113. static int at803x_write_page(struct phy_device *phydev, int page)
  114. {
  115. int mask;
  116. int set;
  117. if (page == AT803X_PAGE_COPPER) {
  118. set = AT803X_BT_BX_REG_SEL;
  119. mask = 0;
  120. } else {
  121. set = 0;
  122. mask = AT803X_BT_BX_REG_SEL;
  123. }
  124. return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
  125. }
  126. static int at803x_read_page(struct phy_device *phydev)
  127. {
  128. int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
  129. if (ccr < 0)
  130. return ccr;
  131. if (ccr & AT803X_BT_BX_REG_SEL)
  132. return AT803X_PAGE_COPPER;
  133. return AT803X_PAGE_FIBER;
  134. }
  135. static int at803x_enable_rx_delay(struct phy_device *phydev)
  136. {
  137. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0,
  138. AT803X_DEBUG_RX_CLK_DLY_EN);
  139. }
  140. static int at803x_enable_tx_delay(struct phy_device *phydev)
  141. {
  142. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0,
  143. AT803X_DEBUG_TX_CLK_DLY_EN);
  144. }
  145. static int at803x_disable_rx_delay(struct phy_device *phydev)
  146. {
  147. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
  148. AT803X_DEBUG_RX_CLK_DLY_EN, 0);
  149. }
  150. static int at803x_disable_tx_delay(struct phy_device *phydev)
  151. {
  152. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE,
  153. AT803X_DEBUG_TX_CLK_DLY_EN, 0);
  154. }
  155. /* save relevant PHY registers to private copy */
  156. static void at803x_context_save(struct phy_device *phydev,
  157. struct at803x_context *context)
  158. {
  159. context->bmcr = phy_read(phydev, MII_BMCR);
  160. context->advertise = phy_read(phydev, MII_ADVERTISE);
  161. context->control1000 = phy_read(phydev, MII_CTRL1000);
  162. context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
  163. context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
  164. context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
  165. }
  166. /* restore relevant PHY registers from private copy */
  167. static void at803x_context_restore(struct phy_device *phydev,
  168. const struct at803x_context *context)
  169. {
  170. phy_write(phydev, MII_BMCR, context->bmcr);
  171. phy_write(phydev, MII_ADVERTISE, context->advertise);
  172. phy_write(phydev, MII_CTRL1000, context->control1000);
  173. phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
  174. phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
  175. phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
  176. }
  177. static int at803x_suspend(struct phy_device *phydev)
  178. {
  179. int value;
  180. int wol_enabled;
  181. value = phy_read(phydev, AT803X_INTR_ENABLE);
  182. wol_enabled = value & AT803X_INTR_ENABLE_WOL;
  183. if (wol_enabled)
  184. value = BMCR_ISOLATE;
  185. else
  186. value = BMCR_PDOWN;
  187. phy_modify(phydev, MII_BMCR, 0, value);
  188. return 0;
  189. }
  190. static int at803x_resume(struct phy_device *phydev)
  191. {
  192. return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
  193. }
  194. static int at803x_parse_dt(struct phy_device *phydev)
  195. {
  196. struct device_node *node = phydev->mdio.dev.of_node;
  197. struct at803x_priv *priv = phydev->priv;
  198. u32 freq, strength, tw;
  199. unsigned int sel;
  200. int ret;
  201. if (!IS_ENABLED(CONFIG_OF_MDIO))
  202. return 0;
  203. if (of_property_read_bool(node, "qca,disable-smarteee"))
  204. priv->flags |= AT803X_DISABLE_SMARTEEE;
  205. if (of_property_read_bool(node, "qca,disable-hibernation-mode"))
  206. priv->flags |= AT803X_DISABLE_HIBERNATION_MODE;
  207. if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
  208. if (!tw || tw > 255) {
  209. phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
  210. return -EINVAL;
  211. }
  212. priv->smarteee_lpi_tw_1g = tw;
  213. }
  214. if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
  215. if (!tw || tw > 255) {
  216. phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
  217. return -EINVAL;
  218. }
  219. priv->smarteee_lpi_tw_100m = tw;
  220. }
  221. ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
  222. if (!ret) {
  223. switch (freq) {
  224. case 25000000:
  225. sel = AT803X_CLK_OUT_25MHZ_XTAL;
  226. break;
  227. case 50000000:
  228. sel = AT803X_CLK_OUT_50MHZ_PLL;
  229. break;
  230. case 62500000:
  231. sel = AT803X_CLK_OUT_62_5MHZ_PLL;
  232. break;
  233. case 125000000:
  234. sel = AT803X_CLK_OUT_125MHZ_PLL;
  235. break;
  236. default:
  237. phydev_err(phydev, "invalid qca,clk-out-frequency\n");
  238. return -EINVAL;
  239. }
  240. priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
  241. priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
  242. }
  243. ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
  244. if (!ret) {
  245. priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
  246. switch (strength) {
  247. case AR803X_STRENGTH_FULL:
  248. priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
  249. break;
  250. case AR803X_STRENGTH_HALF:
  251. priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
  252. break;
  253. case AR803X_STRENGTH_QUARTER:
  254. priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
  255. break;
  256. default:
  257. phydev_err(phydev, "invalid qca,clk-out-strength\n");
  258. return -EINVAL;
  259. }
  260. }
  261. return 0;
  262. }
  263. static int at803x_probe(struct phy_device *phydev)
  264. {
  265. struct device *dev = &phydev->mdio.dev;
  266. struct at803x_priv *priv;
  267. int ret;
  268. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  269. if (!priv)
  270. return -ENOMEM;
  271. phydev->priv = priv;
  272. ret = at803x_parse_dt(phydev);
  273. if (ret)
  274. return ret;
  275. return 0;
  276. }
  277. static int at803x_get_features(struct phy_device *phydev)
  278. {
  279. struct at803x_priv *priv = phydev->priv;
  280. int err;
  281. err = genphy_read_abilities(phydev);
  282. if (err)
  283. return err;
  284. if (phydev->drv->phy_id != ATH8031_PHY_ID)
  285. return 0;
  286. /* AR8031/AR8033 have different status registers
  287. * for copper and fiber operation. However, the
  288. * extended status register is the same for both
  289. * operation modes.
  290. *
  291. * As a result of that, ESTATUS_1000_XFULL is set
  292. * to 1 even when operating in copper TP mode.
  293. *
  294. * Remove this mode from the supported link modes
  295. * when not operating in 1000BaseX mode.
  296. */
  297. if (!priv->is_1000basex)
  298. linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  299. phydev->supported);
  300. return 0;
  301. }
  302. static int at803x_smarteee_config(struct phy_device *phydev)
  303. {
  304. struct at803x_priv *priv = phydev->priv;
  305. u16 mask = 0, val = 0;
  306. int ret;
  307. if (priv->flags & AT803X_DISABLE_SMARTEEE)
  308. return phy_modify_mmd(phydev, MDIO_MMD_PCS,
  309. AT803X_MMD3_SMARTEEE_CTL3,
  310. AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
  311. if (priv->smarteee_lpi_tw_1g) {
  312. mask |= 0xff00;
  313. val |= priv->smarteee_lpi_tw_1g << 8;
  314. }
  315. if (priv->smarteee_lpi_tw_100m) {
  316. mask |= 0x00ff;
  317. val |= priv->smarteee_lpi_tw_100m;
  318. }
  319. if (!mask)
  320. return 0;
  321. ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
  322. mask, val);
  323. if (ret)
  324. return ret;
  325. return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
  326. AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
  327. AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
  328. }
  329. static int at803x_clk_out_config(struct phy_device *phydev)
  330. {
  331. struct at803x_priv *priv = phydev->priv;
  332. if (!priv->clk_25m_mask)
  333. return 0;
  334. return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
  335. priv->clk_25m_mask, priv->clk_25m_reg);
  336. }
  337. static int at8031_pll_config(struct phy_device *phydev)
  338. {
  339. struct at803x_priv *priv = phydev->priv;
  340. /* The default after hardware reset is PLL OFF. After a soft reset, the
  341. * values are retained.
  342. */
  343. if (priv->flags & AT803X_KEEP_PLL_ENABLED)
  344. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
  345. 0, AT803X_DEBUG_PLL_ON);
  346. else
  347. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
  348. AT803X_DEBUG_PLL_ON, 0);
  349. }
  350. static int at803x_hibernation_mode_config(struct phy_device *phydev)
  351. {
  352. struct at803x_priv *priv = phydev->priv;
  353. /* The default after hardware reset is hibernation mode enabled. After
  354. * software reset, the value is retained.
  355. */
  356. if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE) &&
  357. !(phydev->dev_flags & PHY_F_RXC_ALWAYS_ON))
  358. return 0;
  359. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
  360. AT803X_DEBUG_HIB_CTRL_PS_HIB_EN, 0);
  361. }
  362. static int at803x_config_init(struct phy_device *phydev)
  363. {
  364. int ret;
  365. /* The RX and TX delay default is:
  366. * after HW reset: RX delay enabled and TX delay disabled
  367. * after SW reset: RX delay enabled, while TX delay retains the
  368. * value before reset.
  369. */
  370. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  371. phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  372. ret = at803x_enable_rx_delay(phydev);
  373. else
  374. ret = at803x_disable_rx_delay(phydev);
  375. if (ret < 0)
  376. return ret;
  377. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  378. phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  379. ret = at803x_enable_tx_delay(phydev);
  380. else
  381. ret = at803x_disable_tx_delay(phydev);
  382. if (ret < 0)
  383. return ret;
  384. ret = at803x_smarteee_config(phydev);
  385. if (ret < 0)
  386. return ret;
  387. ret = at803x_clk_out_config(phydev);
  388. if (ret < 0)
  389. return ret;
  390. ret = at803x_hibernation_mode_config(phydev);
  391. if (ret < 0)
  392. return ret;
  393. /* Ar803x extended next page bit is enabled by default. Cisco
  394. * multigig switches read this bit and attempt to negotiate 10Gbps
  395. * rates even if the next page bit is disabled. This is incorrect
  396. * behaviour but we still need to accommodate it. XNP is only needed
  397. * for 10Gbps support, so disable XNP.
  398. */
  399. return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
  400. }
  401. static void at803x_link_change_notify(struct phy_device *phydev)
  402. {
  403. /*
  404. * Conduct a hardware reset for AT8030 every time a link loss is
  405. * signalled. This is necessary to circumvent a hardware bug that
  406. * occurs when the cable is unplugged while TX packets are pending
  407. * in the FIFO. In such cases, the FIFO enters an error mode it
  408. * cannot recover from by software.
  409. */
  410. if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
  411. struct at803x_context context;
  412. at803x_context_save(phydev, &context);
  413. phy_device_reset(phydev, 1);
  414. usleep_range(1000, 2000);
  415. phy_device_reset(phydev, 0);
  416. usleep_range(1000, 2000);
  417. at803x_context_restore(phydev, &context);
  418. phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
  419. }
  420. }
  421. static int at803x_config_aneg(struct phy_device *phydev)
  422. {
  423. struct at803x_priv *priv = phydev->priv;
  424. int ret;
  425. ret = at803x_prepare_config_aneg(phydev);
  426. if (ret)
  427. return ret;
  428. if (priv->is_1000basex)
  429. return genphy_c37_config_aneg(phydev);
  430. return genphy_config_aneg(phydev);
  431. }
  432. static int at803x_cable_test_result_trans(u16 status)
  433. {
  434. switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
  435. case AT803X_CDT_STATUS_STAT_NORMAL:
  436. return ETHTOOL_A_CABLE_RESULT_CODE_OK;
  437. case AT803X_CDT_STATUS_STAT_SHORT:
  438. return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
  439. case AT803X_CDT_STATUS_STAT_OPEN:
  440. return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
  441. case AT803X_CDT_STATUS_STAT_FAIL:
  442. default:
  443. return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  444. }
  445. }
  446. static bool at803x_cdt_test_failed(u16 status)
  447. {
  448. return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
  449. AT803X_CDT_STATUS_STAT_FAIL;
  450. }
  451. static bool at803x_cdt_fault_length_valid(u16 status)
  452. {
  453. switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
  454. case AT803X_CDT_STATUS_STAT_OPEN:
  455. case AT803X_CDT_STATUS_STAT_SHORT:
  456. return true;
  457. }
  458. return false;
  459. }
  460. static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
  461. {
  462. static const int ethtool_pair[] = {
  463. ETHTOOL_A_CABLE_PAIR_A,
  464. ETHTOOL_A_CABLE_PAIR_B,
  465. ETHTOOL_A_CABLE_PAIR_C,
  466. ETHTOOL_A_CABLE_PAIR_D,
  467. };
  468. int ret, val;
  469. val = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
  470. AT803X_CDT_ENABLE_TEST;
  471. ret = at803x_cdt_start(phydev, val);
  472. if (ret)
  473. return ret;
  474. ret = at803x_cdt_wait_for_completion(phydev, AT803X_CDT_ENABLE_TEST);
  475. if (ret)
  476. return ret;
  477. val = phy_read(phydev, AT803X_CDT_STATUS);
  478. if (val < 0)
  479. return val;
  480. if (at803x_cdt_test_failed(val))
  481. return 0;
  482. ethnl_cable_test_result(phydev, ethtool_pair[pair],
  483. at803x_cable_test_result_trans(val));
  484. if (at803x_cdt_fault_length_valid(val)) {
  485. val = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, val);
  486. ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
  487. at803x_cdt_fault_length(val));
  488. }
  489. return 1;
  490. }
  491. static int at803x_cable_test_get_status(struct phy_device *phydev,
  492. bool *finished, unsigned long pair_mask)
  493. {
  494. int retries = 20;
  495. int pair, ret;
  496. *finished = false;
  497. /* According to the datasheet the CDT can be performed when
  498. * there is no link partner or when the link partner is
  499. * auto-negotiating. Starting the test will restart the AN
  500. * automatically. It seems that doing this repeatedly we will
  501. * get a slot where our link partner won't disturb our
  502. * measurement.
  503. */
  504. while (pair_mask && retries--) {
  505. for_each_set_bit(pair, &pair_mask, 4) {
  506. ret = at803x_cable_test_one_pair(phydev, pair);
  507. if (ret < 0)
  508. return ret;
  509. if (ret)
  510. clear_bit(pair, &pair_mask);
  511. }
  512. if (pair_mask)
  513. msleep(250);
  514. }
  515. *finished = true;
  516. return 0;
  517. }
  518. static void at803x_cable_test_autoneg(struct phy_device *phydev)
  519. {
  520. /* Enable auto-negotiation, but advertise no capabilities, no link
  521. * will be established. A restart of the auto-negotiation is not
  522. * required, because the cable test will automatically break the link.
  523. */
  524. phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
  525. phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
  526. }
  527. static int at803x_cable_test_start(struct phy_device *phydev)
  528. {
  529. at803x_cable_test_autoneg(phydev);
  530. /* we do all the (time consuming) work later */
  531. return 0;
  532. }
  533. static int at8031_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
  534. unsigned int selector)
  535. {
  536. struct phy_device *phydev = rdev_get_drvdata(rdev);
  537. if (selector)
  538. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
  539. 0, AT803X_DEBUG_RGMII_1V8);
  540. else
  541. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
  542. AT803X_DEBUG_RGMII_1V8, 0);
  543. }
  544. static int at8031_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
  545. {
  546. struct phy_device *phydev = rdev_get_drvdata(rdev);
  547. int val;
  548. val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
  549. if (val < 0)
  550. return val;
  551. return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
  552. }
  553. static const struct regulator_ops vddio_regulator_ops = {
  554. .list_voltage = regulator_list_voltage_table,
  555. .set_voltage_sel = at8031_rgmii_reg_set_voltage_sel,
  556. .get_voltage_sel = at8031_rgmii_reg_get_voltage_sel,
  557. };
  558. static const unsigned int vddio_voltage_table[] = {
  559. 1500000,
  560. 1800000,
  561. };
  562. static const struct regulator_desc vddio_desc = {
  563. .name = "vddio",
  564. .of_match = of_match_ptr("vddio-regulator"),
  565. .n_voltages = ARRAY_SIZE(vddio_voltage_table),
  566. .volt_table = vddio_voltage_table,
  567. .ops = &vddio_regulator_ops,
  568. .type = REGULATOR_VOLTAGE,
  569. .owner = THIS_MODULE,
  570. };
  571. static const struct regulator_ops vddh_regulator_ops = {
  572. };
  573. static const struct regulator_desc vddh_desc = {
  574. .name = "vddh",
  575. .of_match = of_match_ptr("vddh-regulator"),
  576. .n_voltages = 1,
  577. .fixed_uV = 2500000,
  578. .ops = &vddh_regulator_ops,
  579. .type = REGULATOR_VOLTAGE,
  580. .owner = THIS_MODULE,
  581. };
  582. static int at8031_register_regulators(struct phy_device *phydev)
  583. {
  584. struct at803x_priv *priv = phydev->priv;
  585. struct device *dev = &phydev->mdio.dev;
  586. struct regulator_config config = { };
  587. config.dev = dev;
  588. config.driver_data = phydev;
  589. priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
  590. if (IS_ERR(priv->vddio_rdev)) {
  591. phydev_err(phydev, "failed to register VDDIO regulator\n");
  592. return PTR_ERR(priv->vddio_rdev);
  593. }
  594. priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
  595. if (IS_ERR(priv->vddh_rdev)) {
  596. phydev_err(phydev, "failed to register VDDH regulator\n");
  597. return PTR_ERR(priv->vddh_rdev);
  598. }
  599. return 0;
  600. }
  601. static int at8031_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
  602. {
  603. struct phy_device *phydev = upstream;
  604. __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_support);
  605. __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
  606. DECLARE_PHY_INTERFACE_MASK(interfaces);
  607. phy_interface_t iface;
  608. linkmode_zero(phy_support);
  609. phylink_set(phy_support, 1000baseX_Full);
  610. phylink_set(phy_support, 1000baseT_Full);
  611. phylink_set(phy_support, Autoneg);
  612. phylink_set(phy_support, Pause);
  613. phylink_set(phy_support, Asym_Pause);
  614. linkmode_zero(sfp_support);
  615. sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
  616. /* Some modules support 10G modes as well as others we support.
  617. * Mask out non-supported modes so the correct interface is picked.
  618. */
  619. linkmode_and(sfp_support, phy_support, sfp_support);
  620. if (linkmode_empty(sfp_support)) {
  621. dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
  622. return -EINVAL;
  623. }
  624. iface = sfp_select_interface(phydev->sfp_bus, sfp_support);
  625. /* Only 1000Base-X is supported by AR8031/8033 as the downstream SerDes
  626. * interface for use with SFP modules.
  627. * However, some copper modules detected as having a preferred SGMII
  628. * interface do default to and function in 1000Base-X mode, so just
  629. * print a warning and allow such modules, as they may have some chance
  630. * of working.
  631. */
  632. if (iface == PHY_INTERFACE_MODE_SGMII)
  633. dev_warn(&phydev->mdio.dev, "module may not function if 1000Base-X not supported\n");
  634. else if (iface != PHY_INTERFACE_MODE_1000BASEX)
  635. return -EINVAL;
  636. return 0;
  637. }
  638. static const struct sfp_upstream_ops at8031_sfp_ops = {
  639. .attach = phy_sfp_attach,
  640. .detach = phy_sfp_detach,
  641. .module_insert = at8031_sfp_insert,
  642. .connect_phy = phy_sfp_connect_phy,
  643. .disconnect_phy = phy_sfp_disconnect_phy,
  644. };
  645. static int at8031_parse_dt(struct phy_device *phydev)
  646. {
  647. struct device_node *node = phydev->mdio.dev.of_node;
  648. struct at803x_priv *priv = phydev->priv;
  649. int ret;
  650. if (of_property_read_bool(node, "qca,keep-pll-enabled"))
  651. priv->flags |= AT803X_KEEP_PLL_ENABLED;
  652. ret = at8031_register_regulators(phydev);
  653. if (ret < 0)
  654. return ret;
  655. ret = devm_regulator_get_enable_optional(&phydev->mdio.dev,
  656. "vddio");
  657. if (ret) {
  658. phydev_err(phydev, "failed to get VDDIO regulator\n");
  659. return ret;
  660. }
  661. /* Only AR8031/8033 support 1000Base-X for SFP modules */
  662. return phy_sfp_probe(phydev, &at8031_sfp_ops);
  663. }
  664. static int at8031_probe(struct phy_device *phydev)
  665. {
  666. struct at803x_priv *priv;
  667. int mode_cfg;
  668. int ccr;
  669. int ret;
  670. ret = at803x_probe(phydev);
  671. if (ret)
  672. return ret;
  673. priv = phydev->priv;
  674. /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
  675. * options.
  676. */
  677. ret = at8031_parse_dt(phydev);
  678. if (ret)
  679. return ret;
  680. ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
  681. if (ccr < 0)
  682. return ccr;
  683. mode_cfg = ccr & AT803X_MODE_CFG_MASK;
  684. switch (mode_cfg) {
  685. case AT803X_MODE_CFG_BX1000_RGMII_50OHM:
  686. case AT803X_MODE_CFG_BX1000_RGMII_75OHM:
  687. priv->is_1000basex = true;
  688. fallthrough;
  689. case AT803X_MODE_CFG_FX100_RGMII_50OHM:
  690. case AT803X_MODE_CFG_FX100_RGMII_75OHM:
  691. priv->is_fiber = true;
  692. break;
  693. }
  694. /* Disable WoL in 1588 register which is enabled
  695. * by default
  696. */
  697. return phy_modify_mmd(phydev, MDIO_MMD_PCS,
  698. AT803X_PHY_MMD3_WOL_CTRL,
  699. AT803X_WOL_EN, 0);
  700. }
  701. static int at8031_config_init(struct phy_device *phydev)
  702. {
  703. struct at803x_priv *priv = phydev->priv;
  704. int ret;
  705. /* Some bootloaders leave the fiber page selected.
  706. * Switch to the appropriate page (fiber or copper), as otherwise we
  707. * read the PHY capabilities from the wrong page.
  708. */
  709. phy_lock_mdio_bus(phydev);
  710. ret = at803x_write_page(phydev,
  711. priv->is_fiber ? AT803X_PAGE_FIBER :
  712. AT803X_PAGE_COPPER);
  713. phy_unlock_mdio_bus(phydev);
  714. if (ret)
  715. return ret;
  716. ret = at8031_pll_config(phydev);
  717. if (ret < 0)
  718. return ret;
  719. return at803x_config_init(phydev);
  720. }
  721. static int at8031_config_intr(struct phy_device *phydev)
  722. {
  723. struct at803x_priv *priv = phydev->priv;
  724. int err, value = 0;
  725. if (phydev->interrupts == PHY_INTERRUPT_ENABLED &&
  726. priv->is_fiber) {
  727. /* Clear any pending interrupts */
  728. err = at803x_ack_interrupt(phydev);
  729. if (err)
  730. return err;
  731. value |= AT803X_INTR_ENABLE_LINK_FAIL_BX;
  732. value |= AT803X_INTR_ENABLE_LINK_SUCCESS_BX;
  733. err = phy_set_bits(phydev, AT803X_INTR_ENABLE, value);
  734. if (err)
  735. return err;
  736. }
  737. return at803x_config_intr(phydev);
  738. }
  739. /* AR8031 and AR8033 share the same read status logic */
  740. static int at8031_read_status(struct phy_device *phydev)
  741. {
  742. struct at803x_priv *priv = phydev->priv;
  743. bool changed;
  744. if (priv->is_1000basex)
  745. return genphy_c37_read_status(phydev, &changed);
  746. return at803x_read_status(phydev);
  747. }
  748. /* AR8031 and AR8035 share the same cable test get status reg */
  749. static int at8031_cable_test_get_status(struct phy_device *phydev,
  750. bool *finished)
  751. {
  752. return at803x_cable_test_get_status(phydev, finished, 0xf);
  753. }
  754. /* AR8031 and AR8035 share the same cable test start logic */
  755. static int at8031_cable_test_start(struct phy_device *phydev)
  756. {
  757. at803x_cable_test_autoneg(phydev);
  758. phy_write(phydev, MII_CTRL1000, 0);
  759. /* we do all the (time consuming) work later */
  760. return 0;
  761. }
  762. /* AR8032, AR9331 and QCA9561 share the same cable test get status reg */
  763. static int at8032_cable_test_get_status(struct phy_device *phydev,
  764. bool *finished)
  765. {
  766. return at803x_cable_test_get_status(phydev, finished, 0x3);
  767. }
  768. static int at8035_parse_dt(struct phy_device *phydev)
  769. {
  770. struct at803x_priv *priv = phydev->priv;
  771. /* Mask is set by the generic at803x_parse_dt
  772. * if property is set. Assume property is set
  773. * with the mask not zero.
  774. */
  775. if (priv->clk_25m_mask) {
  776. /* Fixup for the AR8030/AR8035. This chip has another mask and
  777. * doesn't support the DSP reference. Eg. the lowest bit of the
  778. * mask. The upper two bits select the same frequencies. Mask
  779. * the lowest bit here.
  780. *
  781. * Warning:
  782. * There was no datasheet for the AR8030 available so this is
  783. * just a guess. But the AR8035 is listed as pin compatible
  784. * to the AR8030 so there might be a good chance it works on
  785. * the AR8030 too.
  786. */
  787. priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
  788. priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
  789. }
  790. return 0;
  791. }
  792. /* AR8030 and AR8035 shared the same special mask for clk_25m */
  793. static int at8035_probe(struct phy_device *phydev)
  794. {
  795. int ret;
  796. ret = at803x_probe(phydev);
  797. if (ret)
  798. return ret;
  799. return at8035_parse_dt(phydev);
  800. }
  801. static struct phy_driver at803x_driver[] = {
  802. {
  803. /* Qualcomm Atheros AR8035 */
  804. PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
  805. .name = "Qualcomm Atheros AR8035",
  806. .flags = PHY_POLL_CABLE_TEST,
  807. .probe = at8035_probe,
  808. .config_aneg = at803x_config_aneg,
  809. .config_init = at803x_config_init,
  810. .soft_reset = genphy_soft_reset,
  811. .set_wol = at803x_set_wol,
  812. .get_wol = at803x_get_wol,
  813. .suspend = at803x_suspend,
  814. .resume = at803x_resume,
  815. /* PHY_GBIT_FEATURES */
  816. .read_status = at803x_read_status,
  817. .config_intr = at803x_config_intr,
  818. .handle_interrupt = at803x_handle_interrupt,
  819. .get_tunable = at803x_get_tunable,
  820. .set_tunable = at803x_set_tunable,
  821. .cable_test_start = at8031_cable_test_start,
  822. .cable_test_get_status = at8031_cable_test_get_status,
  823. }, {
  824. /* Qualcomm Atheros AR8030 */
  825. .phy_id = ATH8030_PHY_ID,
  826. .name = "Qualcomm Atheros AR8030",
  827. .phy_id_mask = AT8030_PHY_ID_MASK,
  828. .probe = at8035_probe,
  829. .config_init = at803x_config_init,
  830. .link_change_notify = at803x_link_change_notify,
  831. .set_wol = at803x_set_wol,
  832. .get_wol = at803x_get_wol,
  833. .suspend = at803x_suspend,
  834. .resume = at803x_resume,
  835. /* PHY_BASIC_FEATURES */
  836. .config_intr = at803x_config_intr,
  837. .handle_interrupt = at803x_handle_interrupt,
  838. }, {
  839. /* Qualcomm Atheros AR8031/AR8033 */
  840. PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
  841. .name = "Qualcomm Atheros AR8031/AR8033",
  842. .flags = PHY_POLL_CABLE_TEST,
  843. .probe = at8031_probe,
  844. .config_init = at8031_config_init,
  845. .config_aneg = at803x_config_aneg,
  846. .soft_reset = genphy_soft_reset,
  847. .set_wol = at8031_set_wol,
  848. .get_wol = at803x_get_wol,
  849. .suspend = at803x_suspend,
  850. .resume = at803x_resume,
  851. .read_page = at803x_read_page,
  852. .write_page = at803x_write_page,
  853. .get_features = at803x_get_features,
  854. .read_status = at8031_read_status,
  855. .config_intr = at8031_config_intr,
  856. .handle_interrupt = at803x_handle_interrupt,
  857. .get_tunable = at803x_get_tunable,
  858. .set_tunable = at803x_set_tunable,
  859. .cable_test_start = at8031_cable_test_start,
  860. .cable_test_get_status = at8031_cable_test_get_status,
  861. }, {
  862. /* Qualcomm Atheros AR8032 */
  863. PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
  864. .name = "Qualcomm Atheros AR8032",
  865. .probe = at803x_probe,
  866. .flags = PHY_POLL_CABLE_TEST,
  867. .config_init = at803x_config_init,
  868. .link_change_notify = at803x_link_change_notify,
  869. .suspend = at803x_suspend,
  870. .resume = at803x_resume,
  871. /* PHY_BASIC_FEATURES */
  872. .config_intr = at803x_config_intr,
  873. .handle_interrupt = at803x_handle_interrupt,
  874. .cable_test_start = at803x_cable_test_start,
  875. .cable_test_get_status = at8032_cable_test_get_status,
  876. }, {
  877. /* ATHEROS AR9331 */
  878. PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
  879. .name = "Qualcomm Atheros AR9331 built-in PHY",
  880. .probe = at803x_probe,
  881. .suspend = at803x_suspend,
  882. .resume = at803x_resume,
  883. .flags = PHY_POLL_CABLE_TEST,
  884. /* PHY_BASIC_FEATURES */
  885. .config_intr = at803x_config_intr,
  886. .handle_interrupt = at803x_handle_interrupt,
  887. .cable_test_start = at803x_cable_test_start,
  888. .cable_test_get_status = at8032_cable_test_get_status,
  889. .read_status = at803x_read_status,
  890. .soft_reset = genphy_soft_reset,
  891. .config_aneg = at803x_config_aneg,
  892. }, {
  893. /* Qualcomm Atheros QCA9561 */
  894. PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
  895. .name = "Qualcomm Atheros QCA9561 built-in PHY",
  896. .probe = at803x_probe,
  897. .suspend = at803x_suspend,
  898. .resume = at803x_resume,
  899. .flags = PHY_POLL_CABLE_TEST,
  900. /* PHY_BASIC_FEATURES */
  901. .config_intr = at803x_config_intr,
  902. .handle_interrupt = at803x_handle_interrupt,
  903. .cable_test_start = at803x_cable_test_start,
  904. .cable_test_get_status = at8032_cable_test_get_status,
  905. .read_status = at803x_read_status,
  906. .soft_reset = genphy_soft_reset,
  907. .config_aneg = at803x_config_aneg,
  908. }, };
  909. module_phy_driver(at803x_driver);
  910. static struct mdio_device_id __maybe_unused atheros_tbl[] = {
  911. { ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
  912. { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
  913. { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
  914. { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
  915. { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
  916. { PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
  917. { }
  918. };
  919. MODULE_DEVICE_TABLE(mdio, atheros_tbl);