at803x.c 11 KB

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