altera-fpga2sdram.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. u32 enable;
  95. struct regmap *sysmgr;
  96. int ret = 0;
  97. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  98. if (!priv)
  99. return -ENOMEM;
  100. priv->dev = dev;
  101. priv->sdrctl = syscon_regmap_lookup_by_compatible("altr,sdr-ctl");
  102. if (IS_ERR(priv->sdrctl)) {
  103. dev_err(dev, "regmap for altr,sdr-ctl lookup failed.\n");
  104. return PTR_ERR(priv->sdrctl);
  105. }
  106. sysmgr = syscon_regmap_lookup_by_compatible("altr,sys-mgr");
  107. if (IS_ERR(sysmgr)) {
  108. dev_err(dev, "regmap for altr,sys-mgr lookup failed.\n");
  109. return PTR_ERR(sysmgr);
  110. }
  111. /* Get f2s bridge configuration saved in handoff register */
  112. regmap_read(sysmgr, SYSMGR_ISWGRP_HANDOFF3, &priv->mask);
  113. ret = fpga_bridge_register(dev, F2S_BRIDGE_NAME,
  114. &altera_fpga2sdram_br_ops, priv);
  115. if (ret)
  116. return ret;
  117. dev_info(dev, "driver initialized with handoff %08x\n", priv->mask);
  118. if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
  119. if (enable > 1) {
  120. dev_warn(dev, "invalid bridge-enable %u > 1\n", enable);
  121. } else {
  122. dev_info(dev, "%s bridge\n",
  123. (enable ? "enabling" : "disabling"));
  124. ret = _alt_fpga2sdram_enable_set(priv, enable);
  125. if (ret) {
  126. fpga_bridge_unregister(&pdev->dev);
  127. return ret;
  128. }
  129. }
  130. }
  131. return ret;
  132. }
  133. static int alt_fpga_bridge_remove(struct platform_device *pdev)
  134. {
  135. fpga_bridge_unregister(&pdev->dev);
  136. return 0;
  137. }
  138. MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
  139. static struct platform_driver altera_fpga_driver = {
  140. .probe = alt_fpga_bridge_probe,
  141. .remove = alt_fpga_bridge_remove,
  142. .driver = {
  143. .name = "altera_fpga2sdram_bridge",
  144. .of_match_table = of_match_ptr(altera_fpga_of_match),
  145. },
  146. };
  147. module_platform_driver(altera_fpga_driver);
  148. MODULE_DESCRIPTION("Altera SoCFPGA FPGA to SDRAM Bridge");
  149. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  150. MODULE_LICENSE("GPL v2");