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