altera-freeze-bridge.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * FPGA Freeze Bridge Controller
  3. *
  4. * Copyright (C) 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. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/of_device.h>
  22. #include <linux/module.h>
  23. #include <linux/fpga/fpga-bridge.h>
  24. #define FREEZE_CSR_STATUS_OFFSET 0
  25. #define FREEZE_CSR_CTRL_OFFSET 4
  26. #define FREEZE_CSR_ILLEGAL_REQ_OFFSET 8
  27. #define FREEZE_CSR_REG_VERSION 12
  28. #define FREEZE_CSR_SUPPORTED_VERSION 2
  29. #define FREEZE_CSR_OFFICIAL_VERSION 0xad000003
  30. #define FREEZE_CSR_STATUS_FREEZE_REQ_DONE BIT(0)
  31. #define FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE BIT(1)
  32. #define FREEZE_CSR_CTRL_FREEZE_REQ BIT(0)
  33. #define FREEZE_CSR_CTRL_RESET_REQ BIT(1)
  34. #define FREEZE_CSR_CTRL_UNFREEZE_REQ BIT(2)
  35. #define FREEZE_BRIDGE_NAME "freeze"
  36. struct altera_freeze_br_data {
  37. struct device *dev;
  38. void __iomem *base_addr;
  39. bool enable;
  40. };
  41. /*
  42. * Poll status until status bit is set or we have a timeout.
  43. */
  44. static int altera_freeze_br_req_ack(struct altera_freeze_br_data *priv,
  45. u32 timeout, u32 req_ack)
  46. {
  47. struct device *dev = priv->dev;
  48. void __iomem *csr_illegal_req_addr = priv->base_addr +
  49. FREEZE_CSR_ILLEGAL_REQ_OFFSET;
  50. u32 status, illegal, ctrl;
  51. int ret = -ETIMEDOUT;
  52. do {
  53. illegal = readl(csr_illegal_req_addr);
  54. if (illegal) {
  55. dev_err(dev, "illegal request detected 0x%x", illegal);
  56. writel(1, csr_illegal_req_addr);
  57. illegal = readl(csr_illegal_req_addr);
  58. if (illegal)
  59. dev_err(dev, "illegal request not cleared 0x%x",
  60. illegal);
  61. ret = -EINVAL;
  62. break;
  63. }
  64. status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
  65. dev_dbg(dev, "%s %x %x\n", __func__, status, req_ack);
  66. status &= req_ack;
  67. if (status) {
  68. ctrl = readl(priv->base_addr + FREEZE_CSR_CTRL_OFFSET);
  69. dev_dbg(dev, "%s request %x acknowledged %x %x\n",
  70. __func__, req_ack, status, ctrl);
  71. ret = 0;
  72. break;
  73. }
  74. udelay(1);
  75. } while (timeout--);
  76. if (ret == -ETIMEDOUT)
  77. dev_err(dev, "%s timeout waiting for 0x%x\n",
  78. __func__, req_ack);
  79. return ret;
  80. }
  81. static int altera_freeze_br_do_freeze(struct altera_freeze_br_data *priv,
  82. u32 timeout)
  83. {
  84. struct device *dev = priv->dev;
  85. void __iomem *csr_ctrl_addr = priv->base_addr +
  86. FREEZE_CSR_CTRL_OFFSET;
  87. u32 status;
  88. int ret;
  89. status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
  90. dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
  91. if (status & FREEZE_CSR_STATUS_FREEZE_REQ_DONE) {
  92. dev_dbg(dev, "%s bridge already disabled %d\n",
  93. __func__, status);
  94. return 0;
  95. } else if (!(status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE)) {
  96. dev_err(dev, "%s bridge not enabled %d\n", __func__, status);
  97. return -EINVAL;
  98. }
  99. writel(FREEZE_CSR_CTRL_FREEZE_REQ, csr_ctrl_addr);
  100. ret = altera_freeze_br_req_ack(priv, timeout,
  101. FREEZE_CSR_STATUS_FREEZE_REQ_DONE);
  102. if (ret)
  103. writel(0, csr_ctrl_addr);
  104. else
  105. writel(FREEZE_CSR_CTRL_RESET_REQ, csr_ctrl_addr);
  106. return ret;
  107. }
  108. static int altera_freeze_br_do_unfreeze(struct altera_freeze_br_data *priv,
  109. u32 timeout)
  110. {
  111. struct device *dev = priv->dev;
  112. void __iomem *csr_ctrl_addr = priv->base_addr +
  113. FREEZE_CSR_CTRL_OFFSET;
  114. u32 status;
  115. int ret;
  116. writel(0, csr_ctrl_addr);
  117. status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
  118. dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
  119. if (status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE) {
  120. dev_dbg(dev, "%s bridge already enabled %d\n",
  121. __func__, status);
  122. return 0;
  123. } else if (!(status & FREEZE_CSR_STATUS_FREEZE_REQ_DONE)) {
  124. dev_err(dev, "%s bridge not frozen %d\n", __func__, status);
  125. return -EINVAL;
  126. }
  127. writel(FREEZE_CSR_CTRL_UNFREEZE_REQ, csr_ctrl_addr);
  128. ret = altera_freeze_br_req_ack(priv, timeout,
  129. FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE);
  130. status = readl(priv->base_addr + FREEZE_CSR_STATUS_OFFSET);
  131. dev_dbg(dev, "%s %d %d\n", __func__, status, readl(csr_ctrl_addr));
  132. writel(0, csr_ctrl_addr);
  133. return ret;
  134. }
  135. /*
  136. * enable = 1 : allow traffic through the bridge
  137. * enable = 0 : disable traffic through the bridge
  138. */
  139. static int altera_freeze_br_enable_set(struct fpga_bridge *bridge,
  140. bool enable)
  141. {
  142. struct altera_freeze_br_data *priv = bridge->priv;
  143. struct fpga_image_info *info = bridge->info;
  144. u32 timeout = 0;
  145. int ret;
  146. if (enable) {
  147. if (info)
  148. timeout = info->enable_timeout_us;
  149. ret = altera_freeze_br_do_unfreeze(bridge->priv, timeout);
  150. } else {
  151. if (info)
  152. timeout = info->disable_timeout_us;
  153. ret = altera_freeze_br_do_freeze(bridge->priv, timeout);
  154. }
  155. if (!ret)
  156. priv->enable = enable;
  157. return ret;
  158. }
  159. static int altera_freeze_br_enable_show(struct fpga_bridge *bridge)
  160. {
  161. struct altera_freeze_br_data *priv = bridge->priv;
  162. return priv->enable;
  163. }
  164. static const struct fpga_bridge_ops altera_freeze_br_br_ops = {
  165. .enable_set = altera_freeze_br_enable_set,
  166. .enable_show = altera_freeze_br_enable_show,
  167. };
  168. static const struct of_device_id altera_freeze_br_of_match[] = {
  169. { .compatible = "altr,freeze-bridge-controller", },
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, altera_freeze_br_of_match);
  173. static int altera_freeze_br_probe(struct platform_device *pdev)
  174. {
  175. struct device *dev = &pdev->dev;
  176. struct device_node *np = pdev->dev.of_node;
  177. void __iomem *base_addr;
  178. struct altera_freeze_br_data *priv;
  179. struct resource *res;
  180. u32 status, revision;
  181. if (!np)
  182. return -ENODEV;
  183. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  184. base_addr = devm_ioremap_resource(dev, res);
  185. if (IS_ERR(base_addr))
  186. return PTR_ERR(base_addr);
  187. revision = readl(base_addr + FREEZE_CSR_REG_VERSION);
  188. if ((revision != FREEZE_CSR_SUPPORTED_VERSION) &&
  189. (revision != FREEZE_CSR_OFFICIAL_VERSION)) {
  190. dev_err(dev,
  191. "%s unexpected revision 0x%x != 0x%x != 0x%x\n",
  192. __func__, revision, FREEZE_CSR_SUPPORTED_VERSION,
  193. FREEZE_CSR_OFFICIAL_VERSION);
  194. return -EINVAL;
  195. }
  196. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  197. if (!priv)
  198. return -ENOMEM;
  199. priv->dev = dev;
  200. status = readl(base_addr + FREEZE_CSR_STATUS_OFFSET);
  201. if (status & FREEZE_CSR_STATUS_UNFREEZE_REQ_DONE)
  202. priv->enable = 1;
  203. priv->base_addr = base_addr;
  204. return fpga_bridge_register(dev, FREEZE_BRIDGE_NAME,
  205. &altera_freeze_br_br_ops, priv);
  206. }
  207. static int altera_freeze_br_remove(struct platform_device *pdev)
  208. {
  209. fpga_bridge_unregister(&pdev->dev);
  210. return 0;
  211. }
  212. static struct platform_driver altera_freeze_br_driver = {
  213. .probe = altera_freeze_br_probe,
  214. .remove = altera_freeze_br_remove,
  215. .driver = {
  216. .name = "altera_freeze_br",
  217. .of_match_table = of_match_ptr(altera_freeze_br_of_match),
  218. },
  219. };
  220. module_platform_driver(altera_freeze_br_driver);
  221. MODULE_DESCRIPTION("Altera Freeze Bridge");
  222. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  223. MODULE_LICENSE("GPL v2");