lan9303_mdio.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
  4. *
  5. * Partially based on a patch from
  6. * Copyright (c) 2014 Stefan Roese <sr@denx.de>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/mdio.h>
  11. #include <linux/phy.h>
  12. #include <linux/of.h>
  13. #include "lan9303.h"
  14. /* Generate phy-addr and -reg from the input address */
  15. #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
  16. #define PHY_REG(x) (((x) >> 1) & 0x1f)
  17. struct lan9303_mdio {
  18. struct mdio_device *device;
  19. struct lan9303 chip;
  20. };
  21. static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val)
  22. {
  23. mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val);
  24. }
  25. static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val)
  26. {
  27. struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
  28. reg <<= 2; /* reg num to offset */
  29. mutex_lock(&sw_dev->device->bus->mdio_lock);
  30. lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
  31. lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
  32. mutex_unlock(&sw_dev->device->bus->mdio_lock);
  33. return 0;
  34. }
  35. static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
  36. {
  37. return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
  38. }
  39. static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
  40. {
  41. struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
  42. reg <<= 2; /* reg num to offset */
  43. mutex_lock(&sw_dev->device->bus->mdio_lock);
  44. *val = lan9303_mdio_real_read(sw_dev->device, reg);
  45. *val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
  46. mutex_unlock(&sw_dev->device->bus->mdio_lock);
  47. return 0;
  48. }
  49. static int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg,
  50. u16 val)
  51. {
  52. struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
  53. return mdiobus_write_nested(sw_dev->device->bus, phy, reg, val);
  54. }
  55. static int lan9303_mdio_phy_read(struct lan9303 *chip, int phy, int reg)
  56. {
  57. struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
  58. return mdiobus_read_nested(sw_dev->device->bus, phy, reg);
  59. }
  60. static const struct lan9303_phy_ops lan9303_mdio_phy_ops = {
  61. .phy_read = lan9303_mdio_phy_read,
  62. .phy_write = lan9303_mdio_phy_write,
  63. };
  64. static const struct regmap_config lan9303_mdio_regmap_config = {
  65. .reg_bits = 8,
  66. .val_bits = 32,
  67. .reg_stride = 1,
  68. .can_multi_write = true,
  69. .max_register = 0x0ff, /* address bits 0..1 are not used */
  70. .reg_format_endian = REGMAP_ENDIAN_LITTLE,
  71. .volatile_table = &lan9303_register_set,
  72. .wr_table = &lan9303_register_set,
  73. .rd_table = &lan9303_register_set,
  74. .reg_read = lan9303_mdio_read,
  75. .reg_write = lan9303_mdio_write,
  76. .cache_type = REGCACHE_NONE,
  77. };
  78. static int lan9303_mdio_probe(struct mdio_device *mdiodev)
  79. {
  80. struct lan9303_mdio *sw_dev;
  81. int ret;
  82. sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
  83. GFP_KERNEL);
  84. if (!sw_dev)
  85. return -ENOMEM;
  86. sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
  87. &lan9303_mdio_regmap_config);
  88. if (IS_ERR(sw_dev->chip.regmap)) {
  89. ret = PTR_ERR(sw_dev->chip.regmap);
  90. dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
  91. return ret;
  92. }
  93. /* link forward and backward */
  94. sw_dev->device = mdiodev;
  95. dev_set_drvdata(&mdiodev->dev, sw_dev);
  96. sw_dev->chip.dev = &mdiodev->dev;
  97. sw_dev->chip.ops = &lan9303_mdio_phy_ops;
  98. ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
  99. if (ret != 0)
  100. return ret;
  101. dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
  102. return 0;
  103. }
  104. static void lan9303_mdio_remove(struct mdio_device *mdiodev)
  105. {
  106. struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
  107. if (!sw_dev)
  108. return;
  109. lan9303_remove(&sw_dev->chip);
  110. }
  111. /*-------------------------------------------------------------------------*/
  112. static const struct of_device_id lan9303_mdio_of_match[] = {
  113. { .compatible = "smsc,lan9303-mdio" },
  114. { /* sentinel */ },
  115. };
  116. MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
  117. static struct mdio_driver lan9303_mdio_driver = {
  118. .mdiodrv.driver = {
  119. .name = "LAN9303_MDIO",
  120. .of_match_table = of_match_ptr(lan9303_mdio_of_match),
  121. },
  122. .probe = lan9303_mdio_probe,
  123. .remove = lan9303_mdio_remove,
  124. };
  125. mdio_module_driver(lan9303_mdio_driver);
  126. MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
  127. MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
  128. MODULE_LICENSE("GPL v2");