mt7530-mdio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, NULL, regmap,
  97. MT7531_PHYA_CTRL_SIGNAL3);
  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 const struct regmap_config regmap_config = {
  116. .reg_bits = 16,
  117. .val_bits = 32,
  118. .reg_stride = 4,
  119. .max_register = MT7530_CREV,
  120. .disable_locking = true,
  121. };
  122. static int
  123. mt7530_probe(struct mdio_device *mdiodev)
  124. {
  125. struct mt7530_priv *priv;
  126. struct device_node *dn;
  127. int ret;
  128. dn = mdiodev->dev.of_node;
  129. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  130. if (!priv)
  131. return -ENOMEM;
  132. priv->bus = mdiodev->bus;
  133. priv->dev = &mdiodev->dev;
  134. priv->mdiodev = mdiodev;
  135. ret = mt7530_probe_common(priv);
  136. if (ret)
  137. return ret;
  138. /* Use medatek,mcm property to distinguish hardware type that would
  139. * cause a little bit differences on power-on sequence.
  140. * Not MCM that indicates switch works as the remote standalone
  141. * integrated circuit so the GPIO pin would be used to complete
  142. * the reset, otherwise memory-mapped register accessing used
  143. * through syscon provides in the case of MCM.
  144. */
  145. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  146. if (priv->mcm) {
  147. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  148. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  149. if (IS_ERR(priv->rstc)) {
  150. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  151. return PTR_ERR(priv->rstc);
  152. }
  153. } else {
  154. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  155. GPIOD_OUT_LOW);
  156. if (IS_ERR(priv->reset)) {
  157. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  158. return PTR_ERR(priv->reset);
  159. }
  160. }
  161. if (priv->id == ID_MT7530) {
  162. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  163. if (IS_ERR(priv->core_pwr))
  164. return PTR_ERR(priv->core_pwr);
  165. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  166. if (IS_ERR(priv->io_pwr))
  167. return PTR_ERR(priv->io_pwr);
  168. }
  169. priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus, priv,
  170. &regmap_config);
  171. if (IS_ERR(priv->regmap))
  172. return PTR_ERR(priv->regmap);
  173. if (priv->id == ID_MT7531)
  174. priv->create_sgmii = mt7531_create_sgmii;
  175. return dsa_register_switch(priv->ds);
  176. }
  177. static void
  178. mt7530_remove(struct mdio_device *mdiodev)
  179. {
  180. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  181. int ret = 0, i;
  182. if (!priv)
  183. return;
  184. ret = regulator_disable(priv->core_pwr);
  185. if (ret < 0)
  186. dev_err(priv->dev,
  187. "Failed to disable core power: %d\n", ret);
  188. ret = regulator_disable(priv->io_pwr);
  189. if (ret < 0)
  190. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  191. ret);
  192. mt7530_remove_common(priv);
  193. for (i = 0; i < 2; ++i)
  194. mtk_pcs_lynxi_destroy(priv->ports[5 + i].sgmii_pcs);
  195. }
  196. static void mt7530_shutdown(struct mdio_device *mdiodev)
  197. {
  198. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  199. if (!priv)
  200. return;
  201. dsa_switch_shutdown(priv->ds);
  202. dev_set_drvdata(&mdiodev->dev, NULL);
  203. }
  204. static struct mdio_driver mt7530_mdio_driver = {
  205. .probe = mt7530_probe,
  206. .remove = mt7530_remove,
  207. .shutdown = mt7530_shutdown,
  208. .mdiodrv.driver = {
  209. .name = "mt7530-mdio",
  210. .of_match_table = mt7530_of_match,
  211. },
  212. };
  213. mdio_module_driver(mt7530_mdio_driver);
  214. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  215. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch (MDIO)");
  216. MODULE_LICENSE("GPL");