lan9303_mdio.c 4.7 KB

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