at803x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * drivers/net/phy/at803x.c
  3. *
  4. * Driver for Atheros 803x PHY
  5. *
  6. * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/phy.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/gpio/consumer.h>
  20. #define AT803X_INTR_ENABLE 0x12
  21. #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
  22. #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
  23. #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
  24. #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
  25. #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
  26. #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
  27. #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
  28. #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
  29. #define AT803X_INTR_ENABLE_WOL BIT(0)
  30. #define AT803X_INTR_STATUS 0x13
  31. #define AT803X_SMART_SPEED 0x14
  32. #define AT803X_LED_CONTROL 0x18
  33. #define AT803X_DEVICE_ADDR 0x03
  34. #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
  35. #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
  36. #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
  37. #define AT803X_MMD_ACCESS_CONTROL 0x0D
  38. #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E
  39. #define AT803X_FUNC_DATA 0x4003
  40. #define AT803X_REG_CHIP_CONFIG 0x1f
  41. #define AT803X_BT_BX_REG_SEL 0x8000
  42. #define AT803X_DEBUG_ADDR 0x1D
  43. #define AT803X_DEBUG_DATA 0x1E
  44. #define AT803X_MODE_CFG_MASK 0x0F
  45. #define AT803X_MODE_CFG_SGMII 0x01
  46. #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
  47. #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
  48. #define AT803X_DEBUG_REG_0 0x00
  49. #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
  50. #define AT803X_DEBUG_REG_5 0x05
  51. #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
  52. #define ATH8030_PHY_ID 0x004dd076
  53. #define ATH8031_PHY_ID 0x004dd074
  54. #define ATH8035_PHY_ID 0x004dd072
  55. #define AT803X_PHY_ID_MASK 0xffffffef
  56. MODULE_DESCRIPTION("Atheros 803x PHY driver");
  57. MODULE_AUTHOR("Matus Ujhelyi");
  58. MODULE_LICENSE("GPL");
  59. struct at803x_priv {
  60. bool phy_reset:1;
  61. struct gpio_desc *gpiod_reset;
  62. };
  63. struct at803x_context {
  64. u16 bmcr;
  65. u16 advertise;
  66. u16 control1000;
  67. u16 int_enable;
  68. u16 smart_speed;
  69. u16 led_control;
  70. };
  71. static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
  72. {
  73. int ret;
  74. ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
  75. if (ret < 0)
  76. return ret;
  77. return phy_read(phydev, AT803X_DEBUG_DATA);
  78. }
  79. static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
  80. u16 clear, u16 set)
  81. {
  82. u16 val;
  83. int ret;
  84. ret = at803x_debug_reg_read(phydev, reg);
  85. if (ret < 0)
  86. return ret;
  87. val = ret & 0xffff;
  88. val &= ~clear;
  89. val |= set;
  90. return phy_write(phydev, AT803X_DEBUG_DATA, val);
  91. }
  92. static inline int at803x_enable_rx_delay(struct phy_device *phydev)
  93. {
  94. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
  95. AT803X_DEBUG_RX_CLK_DLY_EN);
  96. }
  97. static inline int at803x_enable_tx_delay(struct phy_device *phydev)
  98. {
  99. return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
  100. AT803X_DEBUG_TX_CLK_DLY_EN);
  101. }
  102. /* save relevant PHY registers to private copy */
  103. static void at803x_context_save(struct phy_device *phydev,
  104. struct at803x_context *context)
  105. {
  106. context->bmcr = phy_read(phydev, MII_BMCR);
  107. context->advertise = phy_read(phydev, MII_ADVERTISE);
  108. context->control1000 = phy_read(phydev, MII_CTRL1000);
  109. context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
  110. context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
  111. context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
  112. }
  113. /* restore relevant PHY registers from private copy */
  114. static void at803x_context_restore(struct phy_device *phydev,
  115. const struct at803x_context *context)
  116. {
  117. phy_write(phydev, MII_BMCR, context->bmcr);
  118. phy_write(phydev, MII_ADVERTISE, context->advertise);
  119. phy_write(phydev, MII_CTRL1000, context->control1000);
  120. phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
  121. phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
  122. phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
  123. }
  124. static int at803x_set_wol(struct phy_device *phydev,
  125. struct ethtool_wolinfo *wol)
  126. {
  127. struct net_device *ndev = phydev->attached_dev;
  128. const u8 *mac;
  129. int ret;
  130. u32 value;
  131. unsigned int i, offsets[] = {
  132. AT803X_LOC_MAC_ADDR_32_47_OFFSET,
  133. AT803X_LOC_MAC_ADDR_16_31_OFFSET,
  134. AT803X_LOC_MAC_ADDR_0_15_OFFSET,
  135. };
  136. if (!ndev)
  137. return -ENODEV;
  138. if (wol->wolopts & WAKE_MAGIC) {
  139. mac = (const u8 *) ndev->dev_addr;
  140. if (!is_valid_ether_addr(mac))
  141. return -EFAULT;
  142. for (i = 0; i < 3; i++) {
  143. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
  144. AT803X_DEVICE_ADDR);
  145. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
  146. offsets[i]);
  147. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
  148. AT803X_FUNC_DATA);
  149. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
  150. mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
  151. }
  152. value = phy_read(phydev, AT803X_INTR_ENABLE);
  153. value |= AT803X_INTR_ENABLE_WOL;
  154. ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
  155. if (ret)
  156. return ret;
  157. value = phy_read(phydev, AT803X_INTR_STATUS);
  158. } else {
  159. value = phy_read(phydev, AT803X_INTR_ENABLE);
  160. value &= (~AT803X_INTR_ENABLE_WOL);
  161. ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
  162. if (ret)
  163. return ret;
  164. value = phy_read(phydev, AT803X_INTR_STATUS);
  165. }
  166. return ret;
  167. }
  168. static void at803x_get_wol(struct phy_device *phydev,
  169. struct ethtool_wolinfo *wol)
  170. {
  171. u32 value;
  172. wol->supported = WAKE_MAGIC;
  173. wol->wolopts = 0;
  174. value = phy_read(phydev, AT803X_INTR_ENABLE);
  175. if (value & AT803X_INTR_ENABLE_WOL)
  176. wol->wolopts |= WAKE_MAGIC;
  177. }
  178. static int at803x_suspend(struct phy_device *phydev)
  179. {
  180. int value;
  181. int wol_enabled;
  182. mutex_lock(&phydev->lock);
  183. value = phy_read(phydev, AT803X_INTR_ENABLE);
  184. wol_enabled = value & AT803X_INTR_ENABLE_WOL;
  185. value = phy_read(phydev, MII_BMCR);
  186. if (wol_enabled)
  187. value |= BMCR_ISOLATE;
  188. else
  189. value |= BMCR_PDOWN;
  190. phy_write(phydev, MII_BMCR, value);
  191. mutex_unlock(&phydev->lock);
  192. return 0;
  193. }
  194. static int at803x_resume(struct phy_device *phydev)
  195. {
  196. int value;
  197. mutex_lock(&phydev->lock);
  198. value = phy_read(phydev, MII_BMCR);
  199. value &= ~(BMCR_PDOWN | BMCR_ISOLATE);
  200. phy_write(phydev, MII_BMCR, value);
  201. mutex_unlock(&phydev->lock);
  202. return 0;
  203. }
  204. static int at803x_probe(struct phy_device *phydev)
  205. {
  206. struct device *dev = &phydev->mdio.dev;
  207. struct at803x_priv *priv;
  208. struct gpio_desc *gpiod_reset;
  209. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  210. if (!priv)
  211. return -ENOMEM;
  212. if (phydev->drv->phy_id != ATH8030_PHY_ID)
  213. goto does_not_require_reset_workaround;
  214. gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  215. if (IS_ERR(gpiod_reset))
  216. return PTR_ERR(gpiod_reset);
  217. priv->gpiod_reset = gpiod_reset;
  218. does_not_require_reset_workaround:
  219. phydev->priv = priv;
  220. return 0;
  221. }
  222. static int at803x_config_init(struct phy_device *phydev)
  223. {
  224. int ret;
  225. ret = genphy_config_init(phydev);
  226. if (ret < 0)
  227. return ret;
  228. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
  229. phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
  230. ret = at803x_enable_rx_delay(phydev);
  231. if (ret < 0)
  232. return ret;
  233. }
  234. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  235. phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
  236. ret = at803x_enable_tx_delay(phydev);
  237. if (ret < 0)
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. static int at803x_ack_interrupt(struct phy_device *phydev)
  243. {
  244. int err;
  245. err = phy_read(phydev, AT803X_INTR_STATUS);
  246. return (err < 0) ? err : 0;
  247. }
  248. static int at803x_config_intr(struct phy_device *phydev)
  249. {
  250. int err;
  251. int value;
  252. value = phy_read(phydev, AT803X_INTR_ENABLE);
  253. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  254. value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
  255. value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
  256. value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
  257. value |= AT803X_INTR_ENABLE_LINK_FAIL;
  258. value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
  259. err = phy_write(phydev, AT803X_INTR_ENABLE, value);
  260. }
  261. else
  262. err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
  263. return err;
  264. }
  265. static void at803x_link_change_notify(struct phy_device *phydev)
  266. {
  267. struct at803x_priv *priv = phydev->priv;
  268. /*
  269. * Conduct a hardware reset for AT8030 every time a link loss is
  270. * signalled. This is necessary to circumvent a hardware bug that
  271. * occurs when the cable is unplugged while TX packets are pending
  272. * in the FIFO. In such cases, the FIFO enters an error mode it
  273. * cannot recover from by software.
  274. */
  275. if (phydev->state == PHY_NOLINK) {
  276. if (priv->gpiod_reset && !priv->phy_reset) {
  277. struct at803x_context context;
  278. at803x_context_save(phydev, &context);
  279. gpiod_set_value(priv->gpiod_reset, 1);
  280. msleep(1);
  281. gpiod_set_value(priv->gpiod_reset, 0);
  282. msleep(1);
  283. at803x_context_restore(phydev, &context);
  284. phydev_dbg(phydev, "%s(): phy was reset\n",
  285. __func__);
  286. priv->phy_reset = true;
  287. }
  288. } else {
  289. priv->phy_reset = false;
  290. }
  291. }
  292. static int at803x_aneg_done(struct phy_device *phydev)
  293. {
  294. int ccr;
  295. int aneg_done = genphy_aneg_done(phydev);
  296. if (aneg_done != BMSR_ANEGCOMPLETE)
  297. return aneg_done;
  298. /*
  299. * in SGMII mode, if copper side autoneg is successful,
  300. * also check SGMII side autoneg result
  301. */
  302. ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
  303. if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII)
  304. return aneg_done;
  305. /* switch to SGMII/fiber page */
  306. phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
  307. /* check if the SGMII link is OK. */
  308. if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) {
  309. pr_warn("803x_aneg_done: SGMII link is not ok\n");
  310. aneg_done = 0;
  311. }
  312. /* switch back to copper page */
  313. phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
  314. return aneg_done;
  315. }
  316. static struct phy_driver at803x_driver[] = {
  317. {
  318. /* ATHEROS 8035 */
  319. .phy_id = ATH8035_PHY_ID,
  320. .name = "Atheros 8035 ethernet",
  321. .phy_id_mask = AT803X_PHY_ID_MASK,
  322. .probe = at803x_probe,
  323. .config_init = at803x_config_init,
  324. .set_wol = at803x_set_wol,
  325. .get_wol = at803x_get_wol,
  326. .suspend = at803x_suspend,
  327. .resume = at803x_resume,
  328. .features = PHY_GBIT_FEATURES,
  329. .flags = PHY_HAS_INTERRUPT,
  330. .config_aneg = genphy_config_aneg,
  331. .read_status = genphy_read_status,
  332. .ack_interrupt = at803x_ack_interrupt,
  333. .config_intr = at803x_config_intr,
  334. }, {
  335. /* ATHEROS 8030 */
  336. .phy_id = ATH8030_PHY_ID,
  337. .name = "Atheros 8030 ethernet",
  338. .phy_id_mask = AT803X_PHY_ID_MASK,
  339. .probe = at803x_probe,
  340. .config_init = at803x_config_init,
  341. .link_change_notify = at803x_link_change_notify,
  342. .set_wol = at803x_set_wol,
  343. .get_wol = at803x_get_wol,
  344. .suspend = at803x_suspend,
  345. .resume = at803x_resume,
  346. .features = PHY_BASIC_FEATURES,
  347. .flags = PHY_HAS_INTERRUPT,
  348. .config_aneg = genphy_config_aneg,
  349. .read_status = genphy_read_status,
  350. .ack_interrupt = at803x_ack_interrupt,
  351. .config_intr = at803x_config_intr,
  352. }, {
  353. /* ATHEROS 8031 */
  354. .phy_id = ATH8031_PHY_ID,
  355. .name = "Atheros 8031 ethernet",
  356. .phy_id_mask = AT803X_PHY_ID_MASK,
  357. .probe = at803x_probe,
  358. .config_init = at803x_config_init,
  359. .set_wol = at803x_set_wol,
  360. .get_wol = at803x_get_wol,
  361. .suspend = at803x_suspend,
  362. .resume = at803x_resume,
  363. .features = PHY_GBIT_FEATURES,
  364. .flags = PHY_HAS_INTERRUPT,
  365. .config_aneg = genphy_config_aneg,
  366. .read_status = genphy_read_status,
  367. .aneg_done = at803x_aneg_done,
  368. .ack_interrupt = &at803x_ack_interrupt,
  369. .config_intr = &at803x_config_intr,
  370. } };
  371. module_phy_driver(at803x_driver);
  372. static struct mdio_device_id __maybe_unused atheros_tbl[] = {
  373. { ATH8030_PHY_ID, AT803X_PHY_ID_MASK },
  374. { ATH8031_PHY_ID, AT803X_PHY_ID_MASK },
  375. { ATH8035_PHY_ID, AT803X_PHY_ID_MASK },
  376. { }
  377. };
  378. MODULE_DEVICE_TABLE(mdio, atheros_tbl);