vitesse-vsc73xx-platform.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* DSA driver for:
  3. * Vitesse VSC7385 SparX-G5 5+1-port Integrated Gigabit Ethernet Switch
  4. * Vitesse VSC7388 SparX-G8 8-port Integrated Gigabit Ethernet Switch
  5. * Vitesse VSC7395 SparX-G5e 5+1-port Integrated Gigabit Ethernet Switch
  6. * Vitesse VSC7398 SparX-G8e 8-port Integrated Gigabit Ethernet Switch
  7. *
  8. * This driver takes control of the switch chip connected over CPU-attached
  9. * address bus and configures it to route packages around when connected to
  10. * a CPU port.
  11. *
  12. * Copyright (C) 2019 Pawel Dembicki <paweldembicki@gmail.com>
  13. * Based on vitesse-vsc-spi.c by:
  14. * Copyright (C) 2018 Linus Wallej <linus.walleij@linaro.org>
  15. * Includes portions of code from the firmware uploader by:
  16. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include "vitesse-vsc73xx.h"
  23. #define VSC73XX_CMD_PLATFORM_BLOCK_SHIFT 14
  24. #define VSC73XX_CMD_PLATFORM_BLOCK_MASK 0x7
  25. #define VSC73XX_CMD_PLATFORM_SUBBLOCK_SHIFT 10
  26. #define VSC73XX_CMD_PLATFORM_SUBBLOCK_MASK 0xf
  27. #define VSC73XX_CMD_PLATFORM_REGISTER_SHIFT 2
  28. /**
  29. * struct vsc73xx_platform - VSC73xx Platform state container
  30. */
  31. struct vsc73xx_platform {
  32. struct platform_device *pdev;
  33. void __iomem *base_addr;
  34. struct vsc73xx vsc;
  35. };
  36. static const struct vsc73xx_ops vsc73xx_platform_ops;
  37. static u32 vsc73xx_make_addr(u8 block, u8 subblock, u8 reg)
  38. {
  39. u32 ret;
  40. ret = (block & VSC73XX_CMD_PLATFORM_BLOCK_MASK)
  41. << VSC73XX_CMD_PLATFORM_BLOCK_SHIFT;
  42. ret |= (subblock & VSC73XX_CMD_PLATFORM_SUBBLOCK_MASK)
  43. << VSC73XX_CMD_PLATFORM_SUBBLOCK_SHIFT;
  44. ret |= reg << VSC73XX_CMD_PLATFORM_REGISTER_SHIFT;
  45. return ret;
  46. }
  47. static int vsc73xx_platform_read(struct vsc73xx *vsc, u8 block, u8 subblock,
  48. u8 reg, u32 *val)
  49. {
  50. struct vsc73xx_platform *vsc_platform = vsc->priv;
  51. u32 offset;
  52. if (!vsc73xx_is_addr_valid(block, subblock))
  53. return -EINVAL;
  54. offset = vsc73xx_make_addr(block, subblock, reg);
  55. /* By default vsc73xx running in big-endian mode.
  56. * (See "Register Addressing" section 5.5.3 in the VSC7385 manual.)
  57. */
  58. *val = ioread32be(vsc_platform->base_addr + offset);
  59. return 0;
  60. }
  61. static int vsc73xx_platform_write(struct vsc73xx *vsc, u8 block, u8 subblock,
  62. u8 reg, u32 val)
  63. {
  64. struct vsc73xx_platform *vsc_platform = vsc->priv;
  65. u32 offset;
  66. if (!vsc73xx_is_addr_valid(block, subblock))
  67. return -EINVAL;
  68. offset = vsc73xx_make_addr(block, subblock, reg);
  69. iowrite32be(val, vsc_platform->base_addr + offset);
  70. return 0;
  71. }
  72. static int vsc73xx_platform_probe(struct platform_device *pdev)
  73. {
  74. struct device *dev = &pdev->dev;
  75. struct vsc73xx_platform *vsc_platform;
  76. struct resource *res = NULL;
  77. int ret;
  78. vsc_platform = devm_kzalloc(dev, sizeof(*vsc_platform), GFP_KERNEL);
  79. if (!vsc_platform)
  80. return -ENOMEM;
  81. platform_set_drvdata(pdev, vsc_platform);
  82. vsc_platform->pdev = pdev;
  83. vsc_platform->vsc.dev = dev;
  84. vsc_platform->vsc.priv = vsc_platform;
  85. vsc_platform->vsc.ops = &vsc73xx_platform_ops;
  86. /* obtain I/O memory space */
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. if (!res) {
  89. dev_err(&pdev->dev, "cannot obtain I/O memory space\n");
  90. ret = -ENXIO;
  91. return ret;
  92. }
  93. vsc_platform->base_addr = devm_ioremap_resource(&pdev->dev, res);
  94. if (IS_ERR(vsc_platform->base_addr)) {
  95. dev_err(&pdev->dev, "cannot request I/O memory space\n");
  96. ret = -ENXIO;
  97. return ret;
  98. }
  99. return vsc73xx_probe(&vsc_platform->vsc);
  100. }
  101. static int vsc73xx_platform_remove(struct platform_device *pdev)
  102. {
  103. struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev);
  104. return vsc73xx_remove(&vsc_platform->vsc);
  105. }
  106. static const struct vsc73xx_ops vsc73xx_platform_ops = {
  107. .read = vsc73xx_platform_read,
  108. .write = vsc73xx_platform_write,
  109. };
  110. static const struct of_device_id vsc73xx_of_match[] = {
  111. {
  112. .compatible = "vitesse,vsc7385",
  113. },
  114. {
  115. .compatible = "vitesse,vsc7388",
  116. },
  117. {
  118. .compatible = "vitesse,vsc7395",
  119. },
  120. {
  121. .compatible = "vitesse,vsc7398",
  122. },
  123. { },
  124. };
  125. MODULE_DEVICE_TABLE(of, vsc73xx_of_match);
  126. static struct platform_driver vsc73xx_platform_driver = {
  127. .probe = vsc73xx_platform_probe,
  128. .remove = vsc73xx_platform_remove,
  129. .driver = {
  130. .name = "vsc73xx-platform",
  131. .of_match_table = vsc73xx_of_match,
  132. },
  133. };
  134. module_platform_driver(vsc73xx_platform_driver);
  135. MODULE_AUTHOR("Pawel Dembicki <paweldembicki@gmail.com>");
  136. MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 Platform driver");
  137. MODULE_LICENSE("GPL v2");