ts73xx-fpga.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Technologic Systems TS-73xx SBC FPGA loader
  3. *
  4. * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
  5. *
  6. * FPGA Manager Driver for the on-board Altera Cyclone II FPGA found on
  7. * TS-7300, heavily based on load_fpga.c in their vendor tree.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/string.h>
  23. #include <linux/iopoll.h>
  24. #include <linux/fpga/fpga-mgr.h>
  25. #define TS73XX_FPGA_DATA_REG 0
  26. #define TS73XX_FPGA_CONFIG_REG 1
  27. #define TS73XX_FPGA_WRITE_DONE 0x1
  28. #define TS73XX_FPGA_WRITE_DONE_TIMEOUT 1000 /* us */
  29. #define TS73XX_FPGA_RESET 0x2
  30. #define TS73XX_FPGA_RESET_LOW_DELAY 30 /* us */
  31. #define TS73XX_FPGA_RESET_HIGH_DELAY 80 /* us */
  32. #define TS73XX_FPGA_LOAD_OK 0x4
  33. #define TS73XX_FPGA_CONFIG_LOAD 0x8
  34. struct ts73xx_fpga_priv {
  35. void __iomem *io_base;
  36. struct device *dev;
  37. };
  38. static enum fpga_mgr_states ts73xx_fpga_state(struct fpga_manager *mgr)
  39. {
  40. return FPGA_MGR_STATE_UNKNOWN;
  41. }
  42. static int ts73xx_fpga_write_init(struct fpga_manager *mgr,
  43. struct fpga_image_info *info,
  44. const char *buf, size_t count)
  45. {
  46. struct ts73xx_fpga_priv *priv = mgr->priv;
  47. /* Reset the FPGA */
  48. writeb(0, priv->io_base + TS73XX_FPGA_CONFIG_REG);
  49. udelay(TS73XX_FPGA_RESET_LOW_DELAY);
  50. writeb(TS73XX_FPGA_RESET, priv->io_base + TS73XX_FPGA_CONFIG_REG);
  51. udelay(TS73XX_FPGA_RESET_HIGH_DELAY);
  52. return 0;
  53. }
  54. static int ts73xx_fpga_write(struct fpga_manager *mgr, const char *buf,
  55. size_t count)
  56. {
  57. struct ts73xx_fpga_priv *priv = mgr->priv;
  58. size_t i = 0;
  59. int ret;
  60. u8 reg;
  61. while (count--) {
  62. ret = readb_poll_timeout(priv->io_base + TS73XX_FPGA_CONFIG_REG,
  63. reg, !(reg & TS73XX_FPGA_WRITE_DONE),
  64. 1, TS73XX_FPGA_WRITE_DONE_TIMEOUT);
  65. if (ret < 0)
  66. return ret;
  67. writeb(buf[i], priv->io_base + TS73XX_FPGA_DATA_REG);
  68. i++;
  69. }
  70. return 0;
  71. }
  72. static int ts73xx_fpga_write_complete(struct fpga_manager *mgr,
  73. struct fpga_image_info *info)
  74. {
  75. struct ts73xx_fpga_priv *priv = mgr->priv;
  76. u8 reg;
  77. usleep_range(1000, 2000);
  78. reg = readb(priv->io_base + TS73XX_FPGA_CONFIG_REG);
  79. reg |= TS73XX_FPGA_CONFIG_LOAD;
  80. writeb(reg, priv->io_base + TS73XX_FPGA_CONFIG_REG);
  81. usleep_range(1000, 2000);
  82. reg = readb(priv->io_base + TS73XX_FPGA_CONFIG_REG);
  83. reg &= ~TS73XX_FPGA_CONFIG_LOAD;
  84. writeb(reg, priv->io_base + TS73XX_FPGA_CONFIG_REG);
  85. reg = readb(priv->io_base + TS73XX_FPGA_CONFIG_REG);
  86. if ((reg & TS73XX_FPGA_LOAD_OK) != TS73XX_FPGA_LOAD_OK)
  87. return -ETIMEDOUT;
  88. return 0;
  89. }
  90. static const struct fpga_manager_ops ts73xx_fpga_ops = {
  91. .state = ts73xx_fpga_state,
  92. .write_init = ts73xx_fpga_write_init,
  93. .write = ts73xx_fpga_write,
  94. .write_complete = ts73xx_fpga_write_complete,
  95. };
  96. static int ts73xx_fpga_probe(struct platform_device *pdev)
  97. {
  98. struct device *kdev = &pdev->dev;
  99. struct ts73xx_fpga_priv *priv;
  100. struct resource *res;
  101. priv = devm_kzalloc(kdev, sizeof(*priv), GFP_KERNEL);
  102. if (!priv)
  103. return -ENOMEM;
  104. priv->dev = kdev;
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. priv->io_base = devm_ioremap_resource(kdev, res);
  107. if (IS_ERR(priv->io_base)) {
  108. dev_err(kdev, "unable to remap registers\n");
  109. return PTR_ERR(priv->io_base);
  110. }
  111. return fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
  112. &ts73xx_fpga_ops, priv);
  113. }
  114. static int ts73xx_fpga_remove(struct platform_device *pdev)
  115. {
  116. fpga_mgr_unregister(&pdev->dev);
  117. return 0;
  118. }
  119. static struct platform_driver ts73xx_fpga_driver = {
  120. .driver = {
  121. .name = "ts73xx-fpga-mgr",
  122. },
  123. .probe = ts73xx_fpga_probe,
  124. .remove = ts73xx_fpga_remove,
  125. };
  126. module_platform_driver(ts73xx_fpga_driver);
  127. MODULE_AUTHOR("Florian Fainelli <f.fainelli@gmail.com>");
  128. MODULE_DESCRIPTION("TS-73xx FPGA Manager driver");
  129. MODULE_LICENSE("GPL v2");