mt7530-mdio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/gpio/consumer.h>
  3. #include <linux/mdio.h>
  4. #include <linux/module.h>
  5. #include <linux/pcs/pcs-mtk-lynxi.h>
  6. #include <linux/of_irq.h>
  7. #include <linux/of_mdio.h>
  8. #include <linux/of_net.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/regmap.h>
  11. #include <linux/reset.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <net/dsa.h>
  14. #include "mt7530.h"
  15. static int
  16. mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
  17. {
  18. struct mt7530_priv *priv = context;
  19. struct mii_bus *bus = priv->bus;
  20. u16 page, r, lo, hi;
  21. int ret;
  22. page = (reg >> 6) & 0x3ff;
  23. r = (reg >> 2) & 0xf;
  24. lo = val & 0xffff;
  25. hi = val >> 16;
  26. ret = bus->write(bus, priv->mdiodev->addr, 0x1f, page);
  27. if (ret < 0)
  28. return ret;
  29. ret = bus->write(bus, priv->mdiodev->addr, r, lo);
  30. if (ret < 0)
  31. return ret;
  32. ret = bus->write(bus, priv->mdiodev->addr, 0x10, hi);
  33. return ret;
  34. }
  35. static int
  36. mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
  37. {
  38. struct mt7530_priv *priv = context;
  39. struct mii_bus *bus = priv->bus;
  40. u16 page, r, lo, hi;
  41. int ret;
  42. page = (reg >> 6) & 0x3ff;
  43. r = (reg >> 2) & 0xf;
  44. ret = bus->write(bus, priv->mdiodev->addr, 0x1f, page);
  45. if (ret < 0)
  46. return ret;
  47. lo = bus->read(bus, priv->mdiodev->addr, r);
  48. hi = bus->read(bus, priv->mdiodev->addr, 0x10);
  49. *val = (hi << 16) | (lo & 0xffff);
  50. return 0;
  51. }
  52. static void
  53. mt7530_mdio_regmap_lock(void *mdio_lock)
  54. {
  55. mutex_lock_nested(mdio_lock, MDIO_MUTEX_NESTED);
  56. }
  57. static void
  58. mt7530_mdio_regmap_unlock(void *mdio_lock)
  59. {
  60. mutex_unlock(mdio_lock);
  61. }
  62. static const struct regmap_bus mt7530_regmap_bus = {
  63. .reg_write = mt7530_regmap_write,
  64. .reg_read = mt7530_regmap_read,
  65. };
  66. static int
  67. mt7531_create_sgmii(struct mt7530_priv *priv)
  68. {
  69. struct regmap_config *mt7531_pcs_config[2] = {};
  70. struct phylink_pcs *pcs;
  71. struct regmap *regmap;
  72. int i, ret = 0;
  73. for (i = priv->p5_sgmii ? 0 : 1; i < 2; i++) {
  74. mt7531_pcs_config[i] = devm_kzalloc(priv->dev,
  75. sizeof(struct regmap_config),
  76. GFP_KERNEL);
  77. if (!mt7531_pcs_config[i]) {
  78. ret = -ENOMEM;
  79. break;
  80. }
  81. mt7531_pcs_config[i]->name = i ? "port6" : "port5";
  82. mt7531_pcs_config[i]->reg_bits = 16;
  83. mt7531_pcs_config[i]->val_bits = 32;
  84. mt7531_pcs_config[i]->reg_stride = 4;
  85. mt7531_pcs_config[i]->reg_base = MT7531_SGMII_REG_BASE(5 + i);
  86. mt7531_pcs_config[i]->max_register = 0x17c;
  87. mt7531_pcs_config[i]->lock = mt7530_mdio_regmap_lock;
  88. mt7531_pcs_config[i]->unlock = mt7530_mdio_regmap_unlock;
  89. mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
  90. regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus, priv,
  91. mt7531_pcs_config[i]);
  92. if (IS_ERR(regmap)) {
  93. ret = PTR_ERR(regmap);
  94. break;
  95. }
  96. pcs = mtk_pcs_lynxi_create(priv->dev, regmap,
  97. MT7531_PHYA_CTRL_SIGNAL3, 0);
  98. if (!pcs) {
  99. ret = -ENXIO;
  100. break;
  101. }
  102. priv->ports[5 + i].sgmii_pcs = pcs;
  103. }
  104. if (ret && i)
  105. mtk_pcs_lynxi_destroy(priv->ports[5].sgmii_pcs);
  106. return ret;
  107. }
  108. static const struct of_device_id mt7530_of_match[] = {
  109. { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
  110. { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
  111. { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
  112. { /* sentinel */ },
  113. };
  114. MODULE_DEVICE_TABLE(of, mt7530_of_match);
  115. static int
  116. mt7530_probe(struct mdio_device *mdiodev)
  117. {
  118. static struct regmap_config *regmap_config;
  119. struct mt7530_priv *priv;
  120. struct device_node *dn;
  121. int ret;
  122. dn = mdiodev->dev.of_node;
  123. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  124. if (!priv)
  125. return -ENOMEM;
  126. priv->bus = mdiodev->bus;
  127. priv->dev = &mdiodev->dev;
  128. priv->mdiodev = mdiodev;
  129. ret = mt7530_probe_common(priv);
  130. if (ret)
  131. return ret;
  132. /* Use medatek,mcm property to distinguish hardware type that would
  133. * cause a little bit differences on power-on sequence.
  134. * Not MCM that indicates switch works as the remote standalone
  135. * integrated circuit so the GPIO pin would be used to complete
  136. * the reset, otherwise memory-mapped register accessing used
  137. * through syscon provides in the case of MCM.
  138. */
  139. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  140. if (priv->mcm) {
  141. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  142. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  143. if (IS_ERR(priv->rstc)) {
  144. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  145. return PTR_ERR(priv->rstc);
  146. }
  147. } else {
  148. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  149. GPIOD_OUT_LOW);
  150. if (IS_ERR(priv->reset)) {
  151. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  152. return PTR_ERR(priv->reset);
  153. }
  154. }
  155. if (priv->id == ID_MT7530) {
  156. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  157. if (IS_ERR(priv->core_pwr))
  158. return PTR_ERR(priv->core_pwr);
  159. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  160. if (IS_ERR(priv->io_pwr))
  161. return PTR_ERR(priv->io_pwr);
  162. }
  163. regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
  164. GFP_KERNEL);
  165. if (!regmap_config)
  166. return -ENOMEM;
  167. regmap_config->reg_bits = 16;
  168. regmap_config->val_bits = 32;
  169. regmap_config->reg_stride = 4;
  170. regmap_config->max_register = MT7530_CREV;
  171. regmap_config->disable_locking = true;
  172. priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus, priv,
  173. regmap_config);
  174. if (IS_ERR(priv->regmap))
  175. return PTR_ERR(priv->regmap);
  176. if (priv->id == ID_MT7531)
  177. priv->create_sgmii = mt7531_create_sgmii;
  178. return dsa_register_switch(priv->ds);
  179. }
  180. static void
  181. mt7530_remove(struct mdio_device *mdiodev)
  182. {
  183. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  184. int ret = 0, i;
  185. if (!priv)
  186. return;
  187. ret = regulator_disable(priv->core_pwr);
  188. if (ret < 0)
  189. dev_err(priv->dev,
  190. "Failed to disable core power: %d\n", ret);
  191. ret = regulator_disable(priv->io_pwr);
  192. if (ret < 0)
  193. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  194. ret);
  195. mt7530_remove_common(priv);
  196. for (i = 0; i < 2; ++i)
  197. mtk_pcs_lynxi_destroy(priv->ports[5 + i].sgmii_pcs);
  198. }
  199. static void mt7530_shutdown(struct mdio_device *mdiodev)
  200. {
  201. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  202. if (!priv)
  203. return;
  204. dsa_switch_shutdown(priv->ds);
  205. dev_set_drvdata(&mdiodev->dev, NULL);
  206. }
  207. static struct mdio_driver mt7530_mdio_driver = {
  208. .probe = mt7530_probe,
  209. .remove = mt7530_remove,
  210. .shutdown = mt7530_shutdown,
  211. .mdiodrv.driver = {
  212. .name = "mt7530-mdio",
  213. .of_match_table = mt7530_of_match,
  214. },
  215. };
  216. mdio_module_driver(mt7530_mdio_driver);
  217. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  218. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch (MDIO)");
  219. MODULE_LICENSE("GPL");