at803x.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_STATUS 0x13
  22. #define AT803X_SMART_SPEED 0x14
  23. #define AT803X_LED_CONTROL 0x18
  24. #define AT803X_WOL_ENABLE 0x01
  25. #define AT803X_DEVICE_ADDR 0x03
  26. #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
  27. #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
  28. #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
  29. #define AT803X_MMD_ACCESS_CONTROL 0x0D
  30. #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E
  31. #define AT803X_FUNC_DATA 0x4003
  32. #define AT803X_INER 0x0012
  33. #define AT803X_INER_INIT 0xec00
  34. #define AT803X_INSR 0x0013
  35. #define AT803X_DEBUG_ADDR 0x1D
  36. #define AT803X_DEBUG_DATA 0x1E
  37. #define AT803X_DEBUG_SYSTEM_MODE_CTRL 0x05
  38. #define AT803X_DEBUG_RGMII_TX_CLK_DLY BIT(8)
  39. #define ATH8030_PHY_ID 0x004dd076
  40. #define ATH8031_PHY_ID 0x004dd074
  41. #define ATH8035_PHY_ID 0x004dd072
  42. MODULE_DESCRIPTION("Atheros 803x PHY driver");
  43. MODULE_AUTHOR("Matus Ujhelyi");
  44. MODULE_LICENSE("GPL");
  45. struct at803x_priv {
  46. bool phy_reset:1;
  47. struct gpio_desc *gpiod_reset;
  48. };
  49. struct at803x_context {
  50. u16 bmcr;
  51. u16 advertise;
  52. u16 control1000;
  53. u16 int_enable;
  54. u16 smart_speed;
  55. u16 led_control;
  56. };
  57. /* save relevant PHY registers to private copy */
  58. static void at803x_context_save(struct phy_device *phydev,
  59. struct at803x_context *context)
  60. {
  61. context->bmcr = phy_read(phydev, MII_BMCR);
  62. context->advertise = phy_read(phydev, MII_ADVERTISE);
  63. context->control1000 = phy_read(phydev, MII_CTRL1000);
  64. context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
  65. context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
  66. context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
  67. }
  68. /* restore relevant PHY registers from private copy */
  69. static void at803x_context_restore(struct phy_device *phydev,
  70. const struct at803x_context *context)
  71. {
  72. phy_write(phydev, MII_BMCR, context->bmcr);
  73. phy_write(phydev, MII_ADVERTISE, context->advertise);
  74. phy_write(phydev, MII_CTRL1000, context->control1000);
  75. phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
  76. phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
  77. phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
  78. }
  79. static int at803x_set_wol(struct phy_device *phydev,
  80. struct ethtool_wolinfo *wol)
  81. {
  82. struct net_device *ndev = phydev->attached_dev;
  83. const u8 *mac;
  84. int ret;
  85. u32 value;
  86. unsigned int i, offsets[] = {
  87. AT803X_LOC_MAC_ADDR_32_47_OFFSET,
  88. AT803X_LOC_MAC_ADDR_16_31_OFFSET,
  89. AT803X_LOC_MAC_ADDR_0_15_OFFSET,
  90. };
  91. if (!ndev)
  92. return -ENODEV;
  93. if (wol->wolopts & WAKE_MAGIC) {
  94. mac = (const u8 *) ndev->dev_addr;
  95. if (!is_valid_ether_addr(mac))
  96. return -EINVAL;
  97. for (i = 0; i < 3; i++) {
  98. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
  99. AT803X_DEVICE_ADDR);
  100. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
  101. offsets[i]);
  102. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
  103. AT803X_FUNC_DATA);
  104. phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
  105. mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
  106. }
  107. value = phy_read(phydev, AT803X_INTR_ENABLE);
  108. value |= AT803X_WOL_ENABLE;
  109. ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
  110. if (ret)
  111. return ret;
  112. value = phy_read(phydev, AT803X_INTR_STATUS);
  113. } else {
  114. value = phy_read(phydev, AT803X_INTR_ENABLE);
  115. value &= (~AT803X_WOL_ENABLE);
  116. ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
  117. if (ret)
  118. return ret;
  119. value = phy_read(phydev, AT803X_INTR_STATUS);
  120. }
  121. return ret;
  122. }
  123. static void at803x_get_wol(struct phy_device *phydev,
  124. struct ethtool_wolinfo *wol)
  125. {
  126. u32 value;
  127. wol->supported = WAKE_MAGIC;
  128. wol->wolopts = 0;
  129. value = phy_read(phydev, AT803X_INTR_ENABLE);
  130. if (value & AT803X_WOL_ENABLE)
  131. wol->wolopts |= WAKE_MAGIC;
  132. }
  133. static int at803x_suspend(struct phy_device *phydev)
  134. {
  135. int value;
  136. int wol_enabled;
  137. mutex_lock(&phydev->lock);
  138. value = phy_read(phydev, AT803X_INTR_ENABLE);
  139. wol_enabled = value & AT803X_WOL_ENABLE;
  140. value = phy_read(phydev, MII_BMCR);
  141. if (wol_enabled)
  142. value |= BMCR_ISOLATE;
  143. else
  144. value |= BMCR_PDOWN;
  145. phy_write(phydev, MII_BMCR, value);
  146. mutex_unlock(&phydev->lock);
  147. return 0;
  148. }
  149. static int at803x_resume(struct phy_device *phydev)
  150. {
  151. int value;
  152. mutex_lock(&phydev->lock);
  153. value = phy_read(phydev, MII_BMCR);
  154. value &= ~(BMCR_PDOWN | BMCR_ISOLATE);
  155. phy_write(phydev, MII_BMCR, value);
  156. mutex_unlock(&phydev->lock);
  157. return 0;
  158. }
  159. static int at803x_probe(struct phy_device *phydev)
  160. {
  161. struct device *dev = &phydev->dev;
  162. struct at803x_priv *priv;
  163. struct gpio_desc *gpiod_reset;
  164. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  165. if (!priv)
  166. return -ENOMEM;
  167. gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  168. if (IS_ERR(gpiod_reset))
  169. return PTR_ERR(gpiod_reset);
  170. priv->gpiod_reset = gpiod_reset;
  171. phydev->priv = priv;
  172. return 0;
  173. }
  174. static int at803x_config_init(struct phy_device *phydev)
  175. {
  176. int ret;
  177. ret = genphy_config_init(phydev);
  178. if (ret < 0)
  179. return ret;
  180. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
  181. ret = phy_write(phydev, AT803X_DEBUG_ADDR,
  182. AT803X_DEBUG_SYSTEM_MODE_CTRL);
  183. if (ret)
  184. return ret;
  185. ret = phy_write(phydev, AT803X_DEBUG_DATA,
  186. AT803X_DEBUG_RGMII_TX_CLK_DLY);
  187. if (ret)
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int at803x_ack_interrupt(struct phy_device *phydev)
  193. {
  194. int err;
  195. err = phy_read(phydev, AT803X_INSR);
  196. return (err < 0) ? err : 0;
  197. }
  198. static int at803x_config_intr(struct phy_device *phydev)
  199. {
  200. int err;
  201. int value;
  202. value = phy_read(phydev, AT803X_INER);
  203. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  204. err = phy_write(phydev, AT803X_INER,
  205. value | AT803X_INER_INIT);
  206. else
  207. err = phy_write(phydev, AT803X_INER, 0);
  208. return err;
  209. }
  210. static void at803x_link_change_notify(struct phy_device *phydev)
  211. {
  212. struct at803x_priv *priv = phydev->priv;
  213. /*
  214. * Conduct a hardware reset for AT8030 every time a link loss is
  215. * signalled. This is necessary to circumvent a hardware bug that
  216. * occurs when the cable is unplugged while TX packets are pending
  217. * in the FIFO. In such cases, the FIFO enters an error mode it
  218. * cannot recover from by software.
  219. */
  220. if (phydev->drv->phy_id == ATH8030_PHY_ID) {
  221. if (phydev->state == PHY_NOLINK) {
  222. if (priv->gpiod_reset && !priv->phy_reset) {
  223. struct at803x_context context;
  224. at803x_context_save(phydev, &context);
  225. gpiod_set_value(priv->gpiod_reset, 0);
  226. msleep(1);
  227. gpiod_set_value(priv->gpiod_reset, 1);
  228. msleep(1);
  229. at803x_context_restore(phydev, &context);
  230. dev_dbg(&phydev->dev, "%s(): phy was reset\n",
  231. __func__);
  232. priv->phy_reset = true;
  233. }
  234. } else {
  235. priv->phy_reset = false;
  236. }
  237. }
  238. }
  239. static struct phy_driver at803x_driver[] = {
  240. {
  241. /* ATHEROS 8035 */
  242. .phy_id = ATH8035_PHY_ID,
  243. .name = "Atheros 8035 ethernet",
  244. .phy_id_mask = 0xffffffef,
  245. .probe = at803x_probe,
  246. .config_init = at803x_config_init,
  247. .link_change_notify = at803x_link_change_notify,
  248. .set_wol = at803x_set_wol,
  249. .get_wol = at803x_get_wol,
  250. .suspend = at803x_suspend,
  251. .resume = at803x_resume,
  252. .features = PHY_GBIT_FEATURES,
  253. .flags = PHY_HAS_INTERRUPT,
  254. .config_aneg = genphy_config_aneg,
  255. .read_status = genphy_read_status,
  256. .ack_interrupt = at803x_ack_interrupt,
  257. .config_intr = at803x_config_intr,
  258. .driver = {
  259. .owner = THIS_MODULE,
  260. },
  261. }, {
  262. /* ATHEROS 8030 */
  263. .phy_id = ATH8030_PHY_ID,
  264. .name = "Atheros 8030 ethernet",
  265. .phy_id_mask = 0xffffffef,
  266. .probe = at803x_probe,
  267. .config_init = at803x_config_init,
  268. .link_change_notify = at803x_link_change_notify,
  269. .set_wol = at803x_set_wol,
  270. .get_wol = at803x_get_wol,
  271. .suspend = at803x_suspend,
  272. .resume = at803x_resume,
  273. .features = PHY_GBIT_FEATURES,
  274. .flags = PHY_HAS_INTERRUPT,
  275. .config_aneg = genphy_config_aneg,
  276. .read_status = genphy_read_status,
  277. .ack_interrupt = at803x_ack_interrupt,
  278. .config_intr = at803x_config_intr,
  279. .driver = {
  280. .owner = THIS_MODULE,
  281. },
  282. }, {
  283. /* ATHEROS 8031 */
  284. .phy_id = ATH8031_PHY_ID,
  285. .name = "Atheros 8031 ethernet",
  286. .phy_id_mask = 0xffffffef,
  287. .probe = at803x_probe,
  288. .config_init = at803x_config_init,
  289. .link_change_notify = at803x_link_change_notify,
  290. .set_wol = at803x_set_wol,
  291. .get_wol = at803x_get_wol,
  292. .suspend = at803x_suspend,
  293. .resume = at803x_resume,
  294. .features = PHY_GBIT_FEATURES,
  295. .flags = PHY_HAS_INTERRUPT,
  296. .config_aneg = genphy_config_aneg,
  297. .read_status = genphy_read_status,
  298. .ack_interrupt = &at803x_ack_interrupt,
  299. .config_intr = &at803x_config_intr,
  300. .driver = {
  301. .owner = THIS_MODULE,
  302. },
  303. } };
  304. module_phy_driver(at803x_driver);
  305. static struct mdio_device_id __maybe_unused atheros_tbl[] = {
  306. { ATH8030_PHY_ID, 0xffffffef },
  307. { ATH8031_PHY_ID, 0xffffffef },
  308. { ATH8035_PHY_ID, 0xffffffef },
  309. { }
  310. };
  311. MODULE_DEVICE_TABLE(mdio, atheros_tbl);