lan9303_mdio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. mutex_lock(&sw_dev->device->bus->mdio_lock);
  38. lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
  39. lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
  40. mutex_unlock(&sw_dev->device->bus->mdio_lock);
  41. return 0;
  42. }
  43. static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
  44. {
  45. return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
  46. }
  47. static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
  48. {
  49. struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
  50. mutex_lock(&sw_dev->device->bus->mdio_lock);
  51. *val = lan9303_mdio_real_read(sw_dev->device, reg);
  52. *val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
  53. mutex_unlock(&sw_dev->device->bus->mdio_lock);
  54. return 0;
  55. }
  56. static const struct regmap_config lan9303_mdio_regmap_config = {
  57. .reg_bits = 8,
  58. .val_bits = 32,
  59. .reg_stride = 1,
  60. .can_multi_write = true,
  61. .max_register = 0x0ff, /* address bits 0..1 are not used */
  62. .reg_format_endian = REGMAP_ENDIAN_LITTLE,
  63. .volatile_table = &lan9303_register_set,
  64. .wr_table = &lan9303_register_set,
  65. .rd_table = &lan9303_register_set,
  66. .reg_read = lan9303_mdio_read,
  67. .reg_write = lan9303_mdio_write,
  68. .cache_type = REGCACHE_NONE,
  69. };
  70. static int lan9303_mdio_probe(struct mdio_device *mdiodev)
  71. {
  72. struct lan9303_mdio *sw_dev;
  73. int ret;
  74. sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
  75. GFP_KERNEL);
  76. if (!sw_dev)
  77. return -ENOMEM;
  78. sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
  79. &lan9303_mdio_regmap_config);
  80. if (IS_ERR(sw_dev->chip.regmap)) {
  81. ret = PTR_ERR(sw_dev->chip.regmap);
  82. dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
  83. return ret;
  84. }
  85. /* link forward and backward */
  86. sw_dev->device = mdiodev;
  87. dev_set_drvdata(&mdiodev->dev, sw_dev);
  88. sw_dev->chip.dev = &mdiodev->dev;
  89. ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
  90. if (ret != 0)
  91. return ret;
  92. dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
  93. return 0;
  94. }
  95. static void lan9303_mdio_remove(struct mdio_device *mdiodev)
  96. {
  97. struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
  98. if (!sw_dev)
  99. return;
  100. lan9303_remove(&sw_dev->chip);
  101. }
  102. /*-------------------------------------------------------------------------*/
  103. static const struct of_device_id lan9303_mdio_of_match[] = {
  104. { .compatible = "smsc,lan9303-mdio" },
  105. { /* sentinel */ },
  106. };
  107. MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
  108. static struct mdio_driver lan9303_mdio_driver = {
  109. .mdiodrv.driver = {
  110. .name = "LAN9303_MDIO",
  111. .of_match_table = of_match_ptr(lan9303_mdio_of_match),
  112. },
  113. .probe = lan9303_mdio_probe,
  114. .remove = lan9303_mdio_remove,
  115. };
  116. mdio_module_driver(lan9303_mdio_driver);
  117. MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
  118. MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
  119. MODULE_LICENSE("GPL v2");