realtek-mdio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Realtek MDIO interface driver
  3. *
  4. * ASICs we intend to support with this driver:
  5. *
  6. * RTL8366 - The original version, apparently
  7. * RTL8369 - Similar enough to have the same datsheet as RTL8366
  8. * RTL8366RB - Probably reads out "RTL8366 revision B", has a quite
  9. * different register layout from the other two
  10. * RTL8366S - Is this "RTL8366 super"?
  11. * RTL8367 - Has an OpenWRT driver as well
  12. * RTL8368S - Seems to be an alternative name for RTL8366RB
  13. * RTL8370 - Also uses SMI
  14. *
  15. * Copyright (C) 2017 Linus Walleij <[email protected]>
  16. * Copyright (C) 2010 Antti Seppälä <[email protected]>
  17. * Copyright (C) 2010 Roman Yeryomin <[email protected]>
  18. * Copyright (C) 2011 Colin Leitner <[email protected]>
  19. * Copyright (C) 2009-2010 Gabor Juhos <[email protected]>
  20. */
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/overflow.h>
  24. #include <linux/regmap.h>
  25. #include "realtek.h"
  26. /* Read/write via mdiobus */
  27. #define REALTEK_MDIO_CTRL0_REG 31
  28. #define REALTEK_MDIO_START_REG 29
  29. #define REALTEK_MDIO_CTRL1_REG 21
  30. #define REALTEK_MDIO_ADDRESS_REG 23
  31. #define REALTEK_MDIO_DATA_WRITE_REG 24
  32. #define REALTEK_MDIO_DATA_READ_REG 25
  33. #define REALTEK_MDIO_START_OP 0xFFFF
  34. #define REALTEK_MDIO_ADDR_OP 0x000E
  35. #define REALTEK_MDIO_READ_OP 0x0001
  36. #define REALTEK_MDIO_WRITE_OP 0x0003
  37. static int realtek_mdio_write(void *ctx, u32 reg, u32 val)
  38. {
  39. struct realtek_priv *priv = ctx;
  40. struct mii_bus *bus = priv->bus;
  41. int ret;
  42. mutex_lock(&bus->mdio_lock);
  43. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
  44. if (ret)
  45. goto out_unlock;
  46. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_ADDRESS_REG, reg);
  47. if (ret)
  48. goto out_unlock;
  49. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_DATA_WRITE_REG, val);
  50. if (ret)
  51. goto out_unlock;
  52. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_WRITE_OP);
  53. out_unlock:
  54. mutex_unlock(&bus->mdio_lock);
  55. return ret;
  56. }
  57. static int realtek_mdio_read(void *ctx, u32 reg, u32 *val)
  58. {
  59. struct realtek_priv *priv = ctx;
  60. struct mii_bus *bus = priv->bus;
  61. int ret;
  62. mutex_lock(&bus->mdio_lock);
  63. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
  64. if (ret)
  65. goto out_unlock;
  66. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_ADDRESS_REG, reg);
  67. if (ret)
  68. goto out_unlock;
  69. ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_READ_OP);
  70. if (ret)
  71. goto out_unlock;
  72. ret = bus->read(bus, priv->mdio_addr, REALTEK_MDIO_DATA_READ_REG);
  73. if (ret >= 0) {
  74. *val = ret;
  75. ret = 0;
  76. }
  77. out_unlock:
  78. mutex_unlock(&bus->mdio_lock);
  79. return ret;
  80. }
  81. static void realtek_mdio_lock(void *ctx)
  82. {
  83. struct realtek_priv *priv = ctx;
  84. mutex_lock(&priv->map_lock);
  85. }
  86. static void realtek_mdio_unlock(void *ctx)
  87. {
  88. struct realtek_priv *priv = ctx;
  89. mutex_unlock(&priv->map_lock);
  90. }
  91. static const struct regmap_config realtek_mdio_regmap_config = {
  92. .reg_bits = 10, /* A4..A0 R4..R0 */
  93. .val_bits = 16,
  94. .reg_stride = 1,
  95. /* PHY regs are at 0x8000 */
  96. .max_register = 0xffff,
  97. .reg_format_endian = REGMAP_ENDIAN_BIG,
  98. .reg_read = realtek_mdio_read,
  99. .reg_write = realtek_mdio_write,
  100. .cache_type = REGCACHE_NONE,
  101. .lock = realtek_mdio_lock,
  102. .unlock = realtek_mdio_unlock,
  103. };
  104. static const struct regmap_config realtek_mdio_nolock_regmap_config = {
  105. .reg_bits = 10, /* A4..A0 R4..R0 */
  106. .val_bits = 16,
  107. .reg_stride = 1,
  108. /* PHY regs are at 0x8000 */
  109. .max_register = 0xffff,
  110. .reg_format_endian = REGMAP_ENDIAN_BIG,
  111. .reg_read = realtek_mdio_read,
  112. .reg_write = realtek_mdio_write,
  113. .cache_type = REGCACHE_NONE,
  114. .disable_locking = true,
  115. };
  116. static int realtek_mdio_probe(struct mdio_device *mdiodev)
  117. {
  118. struct realtek_priv *priv;
  119. struct device *dev = &mdiodev->dev;
  120. const struct realtek_variant *var;
  121. struct regmap_config rc;
  122. struct device_node *np;
  123. int ret;
  124. var = of_device_get_match_data(dev);
  125. if (!var)
  126. return -EINVAL;
  127. priv = devm_kzalloc(&mdiodev->dev,
  128. size_add(sizeof(*priv), var->chip_data_sz),
  129. GFP_KERNEL);
  130. if (!priv)
  131. return -ENOMEM;
  132. mutex_init(&priv->map_lock);
  133. rc = realtek_mdio_regmap_config;
  134. rc.lock_arg = priv;
  135. priv->map = devm_regmap_init(dev, NULL, priv, &rc);
  136. if (IS_ERR(priv->map)) {
  137. ret = PTR_ERR(priv->map);
  138. dev_err(dev, "regmap init failed: %d\n", ret);
  139. return ret;
  140. }
  141. rc = realtek_mdio_nolock_regmap_config;
  142. priv->map_nolock = devm_regmap_init(dev, NULL, priv, &rc);
  143. if (IS_ERR(priv->map_nolock)) {
  144. ret = PTR_ERR(priv->map_nolock);
  145. dev_err(dev, "regmap init failed: %d\n", ret);
  146. return ret;
  147. }
  148. priv->mdio_addr = mdiodev->addr;
  149. priv->bus = mdiodev->bus;
  150. priv->dev = &mdiodev->dev;
  151. priv->chip_data = (void *)priv + sizeof(*priv);
  152. priv->clk_delay = var->clk_delay;
  153. priv->cmd_read = var->cmd_read;
  154. priv->cmd_write = var->cmd_write;
  155. priv->ops = var->ops;
  156. priv->write_reg_noack = realtek_mdio_write;
  157. np = dev->of_node;
  158. dev_set_drvdata(dev, priv);
  159. /* TODO: if power is software controlled, set up any regulators here */
  160. priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds");
  161. priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  162. if (IS_ERR(priv->reset)) {
  163. dev_err(dev, "failed to get RESET GPIO\n");
  164. return PTR_ERR(priv->reset);
  165. }
  166. if (priv->reset) {
  167. gpiod_set_value(priv->reset, 1);
  168. dev_dbg(dev, "asserted RESET\n");
  169. msleep(REALTEK_HW_STOP_DELAY);
  170. gpiod_set_value(priv->reset, 0);
  171. msleep(REALTEK_HW_START_DELAY);
  172. dev_dbg(dev, "deasserted RESET\n");
  173. }
  174. ret = priv->ops->detect(priv);
  175. if (ret) {
  176. dev_err(dev, "unable to detect switch\n");
  177. return ret;
  178. }
  179. priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
  180. if (!priv->ds)
  181. return -ENOMEM;
  182. priv->ds->dev = dev;
  183. priv->ds->num_ports = priv->num_ports;
  184. priv->ds->priv = priv;
  185. priv->ds->ops = var->ds_ops_mdio;
  186. ret = dsa_register_switch(priv->ds);
  187. if (ret) {
  188. dev_err(priv->dev, "unable to register switch ret = %d\n", ret);
  189. return ret;
  190. }
  191. return 0;
  192. }
  193. static void realtek_mdio_remove(struct mdio_device *mdiodev)
  194. {
  195. struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
  196. if (!priv)
  197. return;
  198. dsa_unregister_switch(priv->ds);
  199. /* leave the device reset asserted */
  200. if (priv->reset)
  201. gpiod_set_value(priv->reset, 1);
  202. }
  203. static void realtek_mdio_shutdown(struct mdio_device *mdiodev)
  204. {
  205. struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
  206. if (!priv)
  207. return;
  208. dsa_switch_shutdown(priv->ds);
  209. dev_set_drvdata(&mdiodev->dev, NULL);
  210. }
  211. static const struct of_device_id realtek_mdio_of_match[] = {
  212. #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB)
  213. { .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, },
  214. #endif
  215. #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB)
  216. { .compatible = "realtek,rtl8365mb", .data = &rtl8365mb_variant, },
  217. #endif
  218. { /* sentinel */ },
  219. };
  220. MODULE_DEVICE_TABLE(of, realtek_mdio_of_match);
  221. static struct mdio_driver realtek_mdio_driver = {
  222. .mdiodrv.driver = {
  223. .name = "realtek-mdio",
  224. .of_match_table = of_match_ptr(realtek_mdio_of_match),
  225. },
  226. .probe = realtek_mdio_probe,
  227. .remove = realtek_mdio_remove,
  228. .shutdown = realtek_mdio_shutdown,
  229. };
  230. mdio_module_driver(realtek_mdio_driver);
  231. MODULE_AUTHOR("Luiz Angelo Daros de Luca <[email protected]>");
  232. MODULE_DESCRIPTION("Driver for Realtek ethernet switch connected via MDIO interface");
  233. MODULE_LICENSE("GPL");