altera-hps2fpga.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * FPGA to/from HPS Bridge Driver for Altera SoCFPGA Devices
  3. *
  4. * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
  5. *
  6. * Includes this patch from the mailing list:
  7. * fpga: altera-hps2fpga: fix HPS2FPGA bridge visibility to L3 masters
  8. * Signed-off-by: Anatolij Gustschin <agust@denx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /*
  23. * This driver manages bridges on a Altera SOCFPGA between the ARM host
  24. * processor system (HPS) and the embedded FPGA.
  25. *
  26. * This driver supports enabling and disabling of the configured ports, which
  27. * allows for safe reprogramming of the FPGA, assuming that the new FPGA image
  28. * uses the same port configuration. Bridges must be disabled before
  29. * reprogramming the FPGA and re-enabled after the FPGA has been programmed.
  30. */
  31. #include <linux/clk.h>
  32. #include <linux/fpga/fpga-bridge.h>
  33. #include <linux/kernel.h>
  34. #include <linux/mfd/syscon.h>
  35. #include <linux/module.h>
  36. #include <linux/of_platform.h>
  37. #include <linux/regmap.h>
  38. #include <linux/reset.h>
  39. #include <linux/spinlock.h>
  40. #define ALT_L3_REMAP_OFST 0x0
  41. #define ALT_L3_REMAP_MPUZERO_MSK 0x00000001
  42. #define ALT_L3_REMAP_H2F_MSK 0x00000008
  43. #define ALT_L3_REMAP_LWH2F_MSK 0x00000010
  44. #define HPS2FPGA_BRIDGE_NAME "hps2fpga"
  45. #define LWHPS2FPGA_BRIDGE_NAME "lwhps2fpga"
  46. #define FPGA2HPS_BRIDGE_NAME "fpga2hps"
  47. struct altera_hps2fpga_data {
  48. const char *name;
  49. struct reset_control *bridge_reset;
  50. struct regmap *l3reg;
  51. unsigned int remap_mask;
  52. struct clk *clk;
  53. };
  54. static int alt_hps2fpga_enable_show(struct fpga_bridge *bridge)
  55. {
  56. struct altera_hps2fpga_data *priv = bridge->priv;
  57. return reset_control_status(priv->bridge_reset);
  58. }
  59. /* The L3 REMAP register is write only, so keep a cached value. */
  60. static unsigned int l3_remap_shadow;
  61. static DEFINE_SPINLOCK(l3_remap_lock);
  62. static int _alt_hps2fpga_enable_set(struct altera_hps2fpga_data *priv,
  63. bool enable)
  64. {
  65. unsigned long flags;
  66. int ret;
  67. /* bring bridge out of reset */
  68. if (enable)
  69. ret = reset_control_deassert(priv->bridge_reset);
  70. else
  71. ret = reset_control_assert(priv->bridge_reset);
  72. if (ret)
  73. return ret;
  74. /* Allow bridge to be visible to L3 masters or not */
  75. if (priv->remap_mask) {
  76. spin_lock_irqsave(&l3_remap_lock, flags);
  77. l3_remap_shadow |= ALT_L3_REMAP_MPUZERO_MSK;
  78. if (enable)
  79. l3_remap_shadow |= priv->remap_mask;
  80. else
  81. l3_remap_shadow &= ~priv->remap_mask;
  82. ret = regmap_write(priv->l3reg, ALT_L3_REMAP_OFST,
  83. l3_remap_shadow);
  84. spin_unlock_irqrestore(&l3_remap_lock, flags);
  85. }
  86. return ret;
  87. }
  88. static int alt_hps2fpga_enable_set(struct fpga_bridge *bridge, bool enable)
  89. {
  90. return _alt_hps2fpga_enable_set(bridge->priv, enable);
  91. }
  92. static const struct fpga_bridge_ops altera_hps2fpga_br_ops = {
  93. .enable_set = alt_hps2fpga_enable_set,
  94. .enable_show = alt_hps2fpga_enable_show,
  95. };
  96. static struct altera_hps2fpga_data hps2fpga_data = {
  97. .name = HPS2FPGA_BRIDGE_NAME,
  98. .remap_mask = ALT_L3_REMAP_H2F_MSK,
  99. };
  100. static struct altera_hps2fpga_data lwhps2fpga_data = {
  101. .name = LWHPS2FPGA_BRIDGE_NAME,
  102. .remap_mask = ALT_L3_REMAP_LWH2F_MSK,
  103. };
  104. static struct altera_hps2fpga_data fpga2hps_data = {
  105. .name = FPGA2HPS_BRIDGE_NAME,
  106. };
  107. static const struct of_device_id altera_fpga_of_match[] = {
  108. { .compatible = "altr,socfpga-hps2fpga-bridge",
  109. .data = &hps2fpga_data },
  110. { .compatible = "altr,socfpga-lwhps2fpga-bridge",
  111. .data = &lwhps2fpga_data },
  112. { .compatible = "altr,socfpga-fpga2hps-bridge",
  113. .data = &fpga2hps_data },
  114. {},
  115. };
  116. static int alt_fpga_bridge_probe(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. struct altera_hps2fpga_data *priv;
  120. const struct of_device_id *of_id;
  121. u32 enable;
  122. int ret;
  123. of_id = of_match_device(altera_fpga_of_match, dev);
  124. if (!of_id) {
  125. dev_err(dev, "failed to match device\n");
  126. return -ENODEV;
  127. }
  128. priv = (struct altera_hps2fpga_data *)of_id->data;
  129. priv->bridge_reset = of_reset_control_get_exclusive_by_index(dev->of_node,
  130. 0);
  131. if (IS_ERR(priv->bridge_reset)) {
  132. dev_err(dev, "Could not get %s reset control\n", priv->name);
  133. return PTR_ERR(priv->bridge_reset);
  134. }
  135. if (priv->remap_mask) {
  136. priv->l3reg = syscon_regmap_lookup_by_compatible("altr,l3regs");
  137. if (IS_ERR(priv->l3reg)) {
  138. dev_err(dev, "regmap for altr,l3regs lookup failed\n");
  139. return PTR_ERR(priv->l3reg);
  140. }
  141. }
  142. priv->clk = devm_clk_get(dev, NULL);
  143. if (IS_ERR(priv->clk)) {
  144. dev_err(dev, "no clock specified\n");
  145. return PTR_ERR(priv->clk);
  146. }
  147. ret = clk_prepare_enable(priv->clk);
  148. if (ret) {
  149. dev_err(dev, "could not enable clock\n");
  150. return -EBUSY;
  151. }
  152. if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
  153. if (enable > 1) {
  154. dev_warn(dev, "invalid bridge-enable %u > 1\n", enable);
  155. } else {
  156. dev_info(dev, "%s bridge\n",
  157. (enable ? "enabling" : "disabling"));
  158. ret = _alt_hps2fpga_enable_set(priv, enable);
  159. if (ret)
  160. goto err;
  161. }
  162. }
  163. ret = fpga_bridge_register(dev, priv->name, &altera_hps2fpga_br_ops,
  164. priv);
  165. err:
  166. if (ret)
  167. clk_disable_unprepare(priv->clk);
  168. return ret;
  169. }
  170. static int alt_fpga_bridge_remove(struct platform_device *pdev)
  171. {
  172. struct fpga_bridge *bridge = platform_get_drvdata(pdev);
  173. struct altera_hps2fpga_data *priv = bridge->priv;
  174. fpga_bridge_unregister(&pdev->dev);
  175. clk_disable_unprepare(priv->clk);
  176. return 0;
  177. }
  178. MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
  179. static struct platform_driver alt_fpga_bridge_driver = {
  180. .probe = alt_fpga_bridge_probe,
  181. .remove = alt_fpga_bridge_remove,
  182. .driver = {
  183. .name = "altera_hps2fpga_bridge",
  184. .of_match_table = of_match_ptr(altera_fpga_of_match),
  185. },
  186. };
  187. module_platform_driver(alt_fpga_bridge_driver);
  188. MODULE_DESCRIPTION("Altera SoCFPGA HPS to FPGA Bridge");
  189. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  190. MODULE_LICENSE("GPL v2");