vitesse-vsc73xx-platform.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <[email protected]>
  13. * Based on vitesse-vsc-spi.c by:
  14. * Copyright (C) 2018 Linus Wallej <[email protected]>
  15. * Includes portions of code from the firmware uploader by:
  16. * Copyright (C) 2009 Gabor Juhos <[email protected]>
  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. int ret;
  77. vsc_platform = devm_kzalloc(dev, sizeof(*vsc_platform), GFP_KERNEL);
  78. if (!vsc_platform)
  79. return -ENOMEM;
  80. platform_set_drvdata(pdev, vsc_platform);
  81. vsc_platform->pdev = pdev;
  82. vsc_platform->vsc.dev = dev;
  83. vsc_platform->vsc.priv = vsc_platform;
  84. vsc_platform->vsc.ops = &vsc73xx_platform_ops;
  85. /* obtain I/O memory space */
  86. vsc_platform->base_addr = devm_platform_ioremap_resource(pdev, 0);
  87. if (IS_ERR(vsc_platform->base_addr)) {
  88. dev_err(&pdev->dev, "cannot request I/O memory space\n");
  89. ret = -ENXIO;
  90. return ret;
  91. }
  92. return vsc73xx_probe(&vsc_platform->vsc);
  93. }
  94. static int vsc73xx_platform_remove(struct platform_device *pdev)
  95. {
  96. struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev);
  97. if (!vsc_platform)
  98. return 0;
  99. vsc73xx_remove(&vsc_platform->vsc);
  100. return 0;
  101. }
  102. static void vsc73xx_platform_shutdown(struct platform_device *pdev)
  103. {
  104. struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev);
  105. if (!vsc_platform)
  106. return;
  107. vsc73xx_shutdown(&vsc_platform->vsc);
  108. platform_set_drvdata(pdev, NULL);
  109. }
  110. static const struct vsc73xx_ops vsc73xx_platform_ops = {
  111. .read = vsc73xx_platform_read,
  112. .write = vsc73xx_platform_write,
  113. };
  114. static const struct of_device_id vsc73xx_of_match[] = {
  115. {
  116. .compatible = "vitesse,vsc7385",
  117. },
  118. {
  119. .compatible = "vitesse,vsc7388",
  120. },
  121. {
  122. .compatible = "vitesse,vsc7395",
  123. },
  124. {
  125. .compatible = "vitesse,vsc7398",
  126. },
  127. { },
  128. };
  129. MODULE_DEVICE_TABLE(of, vsc73xx_of_match);
  130. static struct platform_driver vsc73xx_platform_driver = {
  131. .probe = vsc73xx_platform_probe,
  132. .remove = vsc73xx_platform_remove,
  133. .shutdown = vsc73xx_platform_shutdown,
  134. .driver = {
  135. .name = "vsc73xx-platform",
  136. .of_match_table = vsc73xx_of_match,
  137. },
  138. };
  139. module_platform_driver(vsc73xx_platform_driver);
  140. MODULE_AUTHOR("Pawel Dembicki <[email protected]>");
  141. MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 Platform driver");
  142. MODULE_LICENSE("GPL v2");