altera-pr-ip-core.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Driver for Altera Partial Reconfiguration IP Core
  3. *
  4. * Copyright (C) 2016-2017 Intel Corporation
  5. *
  6. * Based on socfpga-a10.c Copyright (C) 2015-2016 Altera Corporation
  7. * by Alan Tull <atull@opensource.altera.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/fpga/altera-pr-ip-core.h>
  23. #include <linux/fpga/fpga-mgr.h>
  24. #include <linux/module.h>
  25. #define ALT_PR_DATA_OFST 0x00
  26. #define ALT_PR_CSR_OFST 0x04
  27. #define ALT_PR_CSR_PR_START BIT(0)
  28. #define ALT_PR_CSR_STATUS_SFT 2
  29. #define ALT_PR_CSR_STATUS_MSK (7 << ALT_PR_CSR_STATUS_SFT)
  30. #define ALT_PR_CSR_STATUS_NRESET (0 << ALT_PR_CSR_STATUS_SFT)
  31. #define ALT_PR_CSR_STATUS_PR_ERR (1 << ALT_PR_CSR_STATUS_SFT)
  32. #define ALT_PR_CSR_STATUS_CRC_ERR (2 << ALT_PR_CSR_STATUS_SFT)
  33. #define ALT_PR_CSR_STATUS_BAD_BITS (3 << ALT_PR_CSR_STATUS_SFT)
  34. #define ALT_PR_CSR_STATUS_PR_IN_PROG (4 << ALT_PR_CSR_STATUS_SFT)
  35. #define ALT_PR_CSR_STATUS_PR_SUCCESS (5 << ALT_PR_CSR_STATUS_SFT)
  36. struct alt_pr_priv {
  37. void __iomem *reg_base;
  38. };
  39. static enum fpga_mgr_states alt_pr_fpga_state(struct fpga_manager *mgr)
  40. {
  41. struct alt_pr_priv *priv = mgr->priv;
  42. const char *err = "unknown";
  43. enum fpga_mgr_states ret = FPGA_MGR_STATE_UNKNOWN;
  44. u32 val;
  45. val = readl(priv->reg_base + ALT_PR_CSR_OFST);
  46. val &= ALT_PR_CSR_STATUS_MSK;
  47. switch (val) {
  48. case ALT_PR_CSR_STATUS_NRESET:
  49. return FPGA_MGR_STATE_RESET;
  50. case ALT_PR_CSR_STATUS_PR_ERR:
  51. err = "pr error";
  52. ret = FPGA_MGR_STATE_WRITE_ERR;
  53. break;
  54. case ALT_PR_CSR_STATUS_CRC_ERR:
  55. err = "crc error";
  56. ret = FPGA_MGR_STATE_WRITE_ERR;
  57. break;
  58. case ALT_PR_CSR_STATUS_BAD_BITS:
  59. err = "bad bits";
  60. ret = FPGA_MGR_STATE_WRITE_ERR;
  61. break;
  62. case ALT_PR_CSR_STATUS_PR_IN_PROG:
  63. return FPGA_MGR_STATE_WRITE;
  64. case ALT_PR_CSR_STATUS_PR_SUCCESS:
  65. return FPGA_MGR_STATE_OPERATING;
  66. default:
  67. break;
  68. }
  69. dev_err(&mgr->dev, "encountered error code %d (%s) in %s()\n",
  70. val, err, __func__);
  71. return ret;
  72. }
  73. static int alt_pr_fpga_write_init(struct fpga_manager *mgr,
  74. struct fpga_image_info *info,
  75. const char *buf, size_t count)
  76. {
  77. struct alt_pr_priv *priv = mgr->priv;
  78. u32 val;
  79. if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  80. dev_err(&mgr->dev, "%s Partial Reconfiguration flag not set\n",
  81. __func__);
  82. return -EINVAL;
  83. }
  84. val = readl(priv->reg_base + ALT_PR_CSR_OFST);
  85. if (val & ALT_PR_CSR_PR_START) {
  86. dev_err(&mgr->dev,
  87. "%s Partial Reconfiguration already started\n",
  88. __func__);
  89. return -EINVAL;
  90. }
  91. writel(val | ALT_PR_CSR_PR_START, priv->reg_base + ALT_PR_CSR_OFST);
  92. return 0;
  93. }
  94. static int alt_pr_fpga_write(struct fpga_manager *mgr, const char *buf,
  95. size_t count)
  96. {
  97. struct alt_pr_priv *priv = mgr->priv;
  98. u32 *buffer_32 = (u32 *)buf;
  99. size_t i = 0;
  100. if (count <= 0)
  101. return -EINVAL;
  102. /* Write out the complete 32-bit chunks */
  103. while (count >= sizeof(u32)) {
  104. writel(buffer_32[i++], priv->reg_base);
  105. count -= sizeof(u32);
  106. }
  107. /* Write out remaining non 32-bit chunks */
  108. switch (count) {
  109. case 3:
  110. writel(buffer_32[i++] & 0x00ffffff, priv->reg_base);
  111. break;
  112. case 2:
  113. writel(buffer_32[i++] & 0x0000ffff, priv->reg_base);
  114. break;
  115. case 1:
  116. writel(buffer_32[i++] & 0x000000ff, priv->reg_base);
  117. break;
  118. case 0:
  119. break;
  120. default:
  121. /* This will never happen */
  122. return -EFAULT;
  123. }
  124. if (alt_pr_fpga_state(mgr) == FPGA_MGR_STATE_WRITE_ERR)
  125. return -EIO;
  126. return 0;
  127. }
  128. static int alt_pr_fpga_write_complete(struct fpga_manager *mgr,
  129. struct fpga_image_info *info)
  130. {
  131. u32 i = 0;
  132. do {
  133. switch (alt_pr_fpga_state(mgr)) {
  134. case FPGA_MGR_STATE_WRITE_ERR:
  135. return -EIO;
  136. case FPGA_MGR_STATE_OPERATING:
  137. dev_info(&mgr->dev,
  138. "successful partial reconfiguration\n");
  139. return 0;
  140. default:
  141. break;
  142. }
  143. udelay(1);
  144. } while (info->config_complete_timeout_us > i++);
  145. dev_err(&mgr->dev, "timed out waiting for write to complete\n");
  146. return -ETIMEDOUT;
  147. }
  148. static const struct fpga_manager_ops alt_pr_ops = {
  149. .state = alt_pr_fpga_state,
  150. .write_init = alt_pr_fpga_write_init,
  151. .write = alt_pr_fpga_write,
  152. .write_complete = alt_pr_fpga_write_complete,
  153. };
  154. int alt_pr_register(struct device *dev, void __iomem *reg_base)
  155. {
  156. struct alt_pr_priv *priv;
  157. u32 val;
  158. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  159. if (!priv)
  160. return -ENOMEM;
  161. priv->reg_base = reg_base;
  162. val = readl(priv->reg_base + ALT_PR_CSR_OFST);
  163. dev_dbg(dev, "%s status=%d start=%d\n", __func__,
  164. (val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
  165. (int)(val & ALT_PR_CSR_PR_START));
  166. return fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
  167. }
  168. EXPORT_SYMBOL_GPL(alt_pr_register);
  169. int alt_pr_unregister(struct device *dev)
  170. {
  171. dev_dbg(dev, "%s\n", __func__);
  172. fpga_mgr_unregister(dev);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(alt_pr_unregister);
  176. MODULE_AUTHOR("Matthew Gerlach <matthew.gerlach@linux.intel.com>");
  177. MODULE_DESCRIPTION("Altera Partial Reconfiguration IP Core");
  178. MODULE_LICENSE("GPL v2");