altera-hps2fpga.c 5.6 KB

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