altera-fpga2sdram.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * FPGA to SDRAM Bridge Driver for Altera SoCFPGA Devices
  3. *
  4. * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * This driver manages a bridge between an FPGA and the SDRAM used by the ARM
  20. * host processor system (HPS).
  21. *
  22. * The bridge contains 4 read ports, 4 write ports, and 6 command ports.
  23. * Reconfiguring these ports requires that no SDRAM transactions occur during
  24. * reconfiguration. The code reconfiguring the ports cannot run out of SDRAM
  25. * nor can the FPGA access the SDRAM during reconfiguration. This driver does
  26. * not support reconfiguring the ports. The ports are configured by code
  27. * running out of on chip ram before Linux is started and the configuration
  28. * is passed in a handoff register in the system manager.
  29. *
  30. * This driver supports enabling and disabling of the configured ports, which
  31. * allows for safe reprogramming of the FPGA, assuming that the new FPGA image
  32. * uses the same port configuration. Bridges must be disabled before
  33. * reprogramming the FPGA and re-enabled after the FPGA has been programmed.
  34. */
  35. #include <linux/fpga/fpga-bridge.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mfd/syscon.h>
  38. #include <linux/module.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/regmap.h>
  41. #define ALT_SDR_CTL_FPGAPORTRST_OFST 0x80
  42. #define ALT_SDR_CTL_FPGAPORTRST_PORTRSTN_MSK 0x00003fff
  43. #define ALT_SDR_CTL_FPGAPORTRST_RD_SHIFT 0
  44. #define ALT_SDR_CTL_FPGAPORTRST_WR_SHIFT 4
  45. #define ALT_SDR_CTL_FPGAPORTRST_CTRL_SHIFT 8
  46. /*
  47. * From the Cyclone V HPS Memory Map document:
  48. * These registers are used to store handoff information between the
  49. * preloader and the OS. These 8 registers can be used to store any
  50. * information. The contents of these registers have no impact on
  51. * the state of the HPS hardware.
  52. */
  53. #define SYSMGR_ISWGRP_HANDOFF3 (0x8C)
  54. #define F2S_BRIDGE_NAME "fpga2sdram"
  55. struct alt_fpga2sdram_data {
  56. struct device *dev;
  57. struct regmap *sdrctl;
  58. int mask;
  59. };
  60. static int alt_fpga2sdram_enable_show(struct fpga_bridge *bridge)
  61. {
  62. struct alt_fpga2sdram_data *priv = bridge->priv;
  63. int value;
  64. regmap_read(priv->sdrctl, ALT_SDR_CTL_FPGAPORTRST_OFST, &value);
  65. return (value & priv->mask) == priv->mask;
  66. }
  67. static inline int _alt_fpga2sdram_enable_set(struct alt_fpga2sdram_data *priv,
  68. bool enable)
  69. {
  70. return regmap_update_bits(priv->sdrctl, ALT_SDR_CTL_FPGAPORTRST_OFST,
  71. priv->mask, enable ? priv->mask : 0);
  72. }
  73. static int alt_fpga2sdram_enable_set(struct fpga_bridge *bridge, bool enable)
  74. {
  75. return _alt_fpga2sdram_enable_set(bridge->priv, enable);
  76. }
  77. struct prop_map {
  78. char *prop_name;
  79. u32 *prop_value;
  80. u32 prop_max;
  81. };
  82. static const struct fpga_bridge_ops altera_fpga2sdram_br_ops = {
  83. .enable_set = alt_fpga2sdram_enable_set,
  84. .enable_show = alt_fpga2sdram_enable_show,
  85. };
  86. static const struct of_device_id altera_fpga_of_match[] = {
  87. { .compatible = "altr,socfpga-fpga2sdram-bridge" },
  88. {},
  89. };
  90. static int alt_fpga_bridge_probe(struct platform_device *pdev)
  91. {
  92. struct device *dev = &pdev->dev;
  93. struct alt_fpga2sdram_data *priv;
  94. struct fpga_bridge *br;
  95. u32 enable;
  96. struct regmap *sysmgr;
  97. int ret = 0;
  98. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  99. if (!priv)
  100. return -ENOMEM;
  101. priv->dev = dev;
  102. priv->sdrctl = syscon_regmap_lookup_by_compatible("altr,sdr-ctl");
  103. if (IS_ERR(priv->sdrctl)) {
  104. dev_err(dev, "regmap for altr,sdr-ctl lookup failed.\n");
  105. return PTR_ERR(priv->sdrctl);
  106. }
  107. sysmgr = syscon_regmap_lookup_by_compatible("altr,sys-mgr");
  108. if (IS_ERR(sysmgr)) {
  109. dev_err(dev, "regmap for altr,sys-mgr lookup failed.\n");
  110. return PTR_ERR(sysmgr);
  111. }
  112. /* Get f2s bridge configuration saved in handoff register */
  113. regmap_read(sysmgr, SYSMGR_ISWGRP_HANDOFF3, &priv->mask);
  114. br = fpga_bridge_create(dev, F2S_BRIDGE_NAME,
  115. &altera_fpga2sdram_br_ops, priv);
  116. if (!br)
  117. return -ENOMEM;
  118. platform_set_drvdata(pdev, br);
  119. ret = fpga_bridge_register(br);
  120. if (ret) {
  121. fpga_bridge_free(br);
  122. return ret;
  123. }
  124. dev_info(dev, "driver initialized with handoff %08x\n", priv->mask);
  125. if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
  126. if (enable > 1) {
  127. dev_warn(dev, "invalid bridge-enable %u > 1\n", enable);
  128. } else {
  129. dev_info(dev, "%s bridge\n",
  130. (enable ? "enabling" : "disabling"));
  131. ret = _alt_fpga2sdram_enable_set(priv, enable);
  132. if (ret) {
  133. fpga_bridge_unregister(br);
  134. return ret;
  135. }
  136. }
  137. }
  138. return ret;
  139. }
  140. static int alt_fpga_bridge_remove(struct platform_device *pdev)
  141. {
  142. struct fpga_bridge *br = platform_get_drvdata(pdev);
  143. fpga_bridge_unregister(br);
  144. return 0;
  145. }
  146. MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
  147. static struct platform_driver altera_fpga_driver = {
  148. .probe = alt_fpga_bridge_probe,
  149. .remove = alt_fpga_bridge_remove,
  150. .driver = {
  151. .name = "altera_fpga2sdram_bridge",
  152. .of_match_table = of_match_ptr(altera_fpga_of_match),
  153. },
  154. };
  155. module_platform_driver(altera_fpga_driver);
  156. MODULE_DESCRIPTION("Altera SoCFPGA FPGA to SDRAM Bridge");
  157. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  158. MODULE_LICENSE("GPL v2");