socfpga.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * FPGA Manager Driver for Altera SOCFPGA
  3. *
  4. * Copyright (C) 2013-2015 Altera Corporation
  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/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/fpga/fpga-mgr.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/pm.h>
  27. /* Register offsets */
  28. #define SOCFPGA_FPGMGR_STAT_OFST 0x0
  29. #define SOCFPGA_FPGMGR_CTL_OFST 0x4
  30. #define SOCFPGA_FPGMGR_DCLKCNT_OFST 0x8
  31. #define SOCFPGA_FPGMGR_DCLKSTAT_OFST 0xc
  32. #define SOCFPGA_FPGMGR_GPIO_INTEN_OFST 0x830
  33. #define SOCFPGA_FPGMGR_GPIO_INTMSK_OFST 0x834
  34. #define SOCFPGA_FPGMGR_GPIO_INTTYPE_LEVEL_OFST 0x838
  35. #define SOCFPGA_FPGMGR_GPIO_INT_POL_OFST 0x83c
  36. #define SOCFPGA_FPGMGR_GPIO_INTSTAT_OFST 0x840
  37. #define SOCFPGA_FPGMGR_GPIO_RAW_INTSTAT_OFST 0x844
  38. #define SOCFPGA_FPGMGR_GPIO_PORTA_EOI_OFST 0x84c
  39. #define SOCFPGA_FPGMGR_GPIO_EXT_PORTA_OFST 0x850
  40. /* Register bit defines */
  41. /* SOCFPGA_FPGMGR_STAT register mode field values */
  42. #define SOCFPGA_FPGMGR_STAT_POWER_UP 0x0 /*ramping*/
  43. #define SOCFPGA_FPGMGR_STAT_RESET 0x1
  44. #define SOCFPGA_FPGMGR_STAT_CFG 0x2
  45. #define SOCFPGA_FPGMGR_STAT_INIT 0x3
  46. #define SOCFPGA_FPGMGR_STAT_USER_MODE 0x4
  47. #define SOCFPGA_FPGMGR_STAT_UNKNOWN 0x5
  48. #define SOCFPGA_FPGMGR_STAT_STATE_MASK 0x7
  49. /* This is a flag value that doesn't really happen in this register field */
  50. #define SOCFPGA_FPGMGR_STAT_POWER_OFF 0x0
  51. #define MSEL_PP16_FAST_NOAES_NODC 0x0
  52. #define MSEL_PP16_FAST_AES_NODC 0x1
  53. #define MSEL_PP16_FAST_AESOPT_DC 0x2
  54. #define MSEL_PP16_SLOW_NOAES_NODC 0x4
  55. #define MSEL_PP16_SLOW_AES_NODC 0x5
  56. #define MSEL_PP16_SLOW_AESOPT_DC 0x6
  57. #define MSEL_PP32_FAST_NOAES_NODC 0x8
  58. #define MSEL_PP32_FAST_AES_NODC 0x9
  59. #define MSEL_PP32_FAST_AESOPT_DC 0xa
  60. #define MSEL_PP32_SLOW_NOAES_NODC 0xc
  61. #define MSEL_PP32_SLOW_AES_NODC 0xd
  62. #define MSEL_PP32_SLOW_AESOPT_DC 0xe
  63. #define SOCFPGA_FPGMGR_STAT_MSEL_MASK 0x000000f8
  64. #define SOCFPGA_FPGMGR_STAT_MSEL_SHIFT 3
  65. /* SOCFPGA_FPGMGR_CTL register */
  66. #define SOCFPGA_FPGMGR_CTL_EN 0x00000001
  67. #define SOCFPGA_FPGMGR_CTL_NCE 0x00000002
  68. #define SOCFPGA_FPGMGR_CTL_NCFGPULL 0x00000004
  69. #define CDRATIO_X1 0x00000000
  70. #define CDRATIO_X2 0x00000040
  71. #define CDRATIO_X4 0x00000080
  72. #define CDRATIO_X8 0x000000c0
  73. #define SOCFPGA_FPGMGR_CTL_CDRATIO_MASK 0x000000c0
  74. #define SOCFPGA_FPGMGR_CTL_AXICFGEN 0x00000100
  75. #define CFGWDTH_16 0x00000000
  76. #define CFGWDTH_32 0x00000200
  77. #define SOCFPGA_FPGMGR_CTL_CFGWDTH_MASK 0x00000200
  78. /* SOCFPGA_FPGMGR_DCLKSTAT register */
  79. #define SOCFPGA_FPGMGR_DCLKSTAT_DCNTDONE_E_DONE 0x1
  80. /* SOCFPGA_FPGMGR_GPIO_* registers share the same bit positions */
  81. #define SOCFPGA_FPGMGR_MON_NSTATUS 0x0001
  82. #define SOCFPGA_FPGMGR_MON_CONF_DONE 0x0002
  83. #define SOCFPGA_FPGMGR_MON_INIT_DONE 0x0004
  84. #define SOCFPGA_FPGMGR_MON_CRC_ERROR 0x0008
  85. #define SOCFPGA_FPGMGR_MON_CVP_CONF_DONE 0x0010
  86. #define SOCFPGA_FPGMGR_MON_PR_READY 0x0020
  87. #define SOCFPGA_FPGMGR_MON_PR_ERROR 0x0040
  88. #define SOCFPGA_FPGMGR_MON_PR_DONE 0x0080
  89. #define SOCFPGA_FPGMGR_MON_NCONFIG_PIN 0x0100
  90. #define SOCFPGA_FPGMGR_MON_NSTATUS_PIN 0x0200
  91. #define SOCFPGA_FPGMGR_MON_CONF_DONE_PIN 0x0400
  92. #define SOCFPGA_FPGMGR_MON_FPGA_POWER_ON 0x0800
  93. #define SOCFPGA_FPGMGR_MON_STATUS_MASK 0x0fff
  94. #define SOCFPGA_FPGMGR_NUM_SUPPLIES 3
  95. #define SOCFPGA_RESUME_TIMEOUT 3
  96. /* In power-up order. Reverse for power-down. */
  97. static const char *supply_names[SOCFPGA_FPGMGR_NUM_SUPPLIES] __maybe_unused = {
  98. "FPGA-1.5V",
  99. "FPGA-1.1V",
  100. "FPGA-2.5V",
  101. };
  102. struct socfpga_fpga_priv {
  103. void __iomem *fpga_base_addr;
  104. void __iomem *fpga_data_addr;
  105. struct completion status_complete;
  106. int irq;
  107. };
  108. struct cfgmgr_mode {
  109. /* Values to set in the CTRL register */
  110. u32 ctrl;
  111. /* flag that this table entry is a valid mode */
  112. bool valid;
  113. };
  114. /* For SOCFPGA_FPGMGR_STAT_MSEL field */
  115. static struct cfgmgr_mode cfgmgr_modes[] = {
  116. [MSEL_PP16_FAST_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
  117. [MSEL_PP16_FAST_AES_NODC] = { CFGWDTH_16 | CDRATIO_X2, 1 },
  118. [MSEL_PP16_FAST_AESOPT_DC] = { CFGWDTH_16 | CDRATIO_X4, 1 },
  119. [MSEL_PP16_SLOW_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
  120. [MSEL_PP16_SLOW_AES_NODC] = { CFGWDTH_16 | CDRATIO_X2, 1 },
  121. [MSEL_PP16_SLOW_AESOPT_DC] = { CFGWDTH_16 | CDRATIO_X4, 1 },
  122. [MSEL_PP32_FAST_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
  123. [MSEL_PP32_FAST_AES_NODC] = { CFGWDTH_32 | CDRATIO_X4, 1 },
  124. [MSEL_PP32_FAST_AESOPT_DC] = { CFGWDTH_32 | CDRATIO_X8, 1 },
  125. [MSEL_PP32_SLOW_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
  126. [MSEL_PP32_SLOW_AES_NODC] = { CFGWDTH_32 | CDRATIO_X4, 1 },
  127. [MSEL_PP32_SLOW_AESOPT_DC] = { CFGWDTH_32 | CDRATIO_X8, 1 },
  128. };
  129. static u32 socfpga_fpga_readl(struct socfpga_fpga_priv *priv, u32 reg_offset)
  130. {
  131. return readl(priv->fpga_base_addr + reg_offset);
  132. }
  133. static void socfpga_fpga_writel(struct socfpga_fpga_priv *priv, u32 reg_offset,
  134. u32 value)
  135. {
  136. writel(value, priv->fpga_base_addr + reg_offset);
  137. }
  138. static u32 socfpga_fpga_raw_readl(struct socfpga_fpga_priv *priv,
  139. u32 reg_offset)
  140. {
  141. return __raw_readl(priv->fpga_base_addr + reg_offset);
  142. }
  143. static void socfpga_fpga_raw_writel(struct socfpga_fpga_priv *priv,
  144. u32 reg_offset, u32 value)
  145. {
  146. __raw_writel(value, priv->fpga_base_addr + reg_offset);
  147. }
  148. static void socfpga_fpga_data_writel(struct socfpga_fpga_priv *priv, u32 value)
  149. {
  150. writel(value, priv->fpga_data_addr);
  151. }
  152. static inline void socfpga_fpga_set_bitsl(struct socfpga_fpga_priv *priv,
  153. u32 offset, u32 bits)
  154. {
  155. u32 val;
  156. val = socfpga_fpga_readl(priv, offset);
  157. val |= bits;
  158. socfpga_fpga_writel(priv, offset, val);
  159. }
  160. static inline void socfpga_fpga_clr_bitsl(struct socfpga_fpga_priv *priv,
  161. u32 offset, u32 bits)
  162. {
  163. u32 val;
  164. val = socfpga_fpga_readl(priv, offset);
  165. val &= ~bits;
  166. socfpga_fpga_writel(priv, offset, val);
  167. }
  168. static u32 socfpga_fpga_mon_status_get(struct socfpga_fpga_priv *priv)
  169. {
  170. return socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_GPIO_EXT_PORTA_OFST) &
  171. SOCFPGA_FPGMGR_MON_STATUS_MASK;
  172. }
  173. static u32 socfpga_fpga_state_get(struct socfpga_fpga_priv *priv)
  174. {
  175. u32 status = socfpga_fpga_mon_status_get(priv);
  176. if ((status & SOCFPGA_FPGMGR_MON_FPGA_POWER_ON) == 0)
  177. return SOCFPGA_FPGMGR_STAT_POWER_OFF;
  178. return socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_STAT_OFST) &
  179. SOCFPGA_FPGMGR_STAT_STATE_MASK;
  180. }
  181. static void socfpga_fpga_clear_done_status(struct socfpga_fpga_priv *priv)
  182. {
  183. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_DCLKSTAT_OFST,
  184. SOCFPGA_FPGMGR_DCLKSTAT_DCNTDONE_E_DONE);
  185. }
  186. /*
  187. * Set the DCLKCNT, wait for DCLKSTAT to report the count completed, and clear
  188. * the complete status.
  189. */
  190. static int socfpga_fpga_dclk_set_and_wait_clear(struct socfpga_fpga_priv *priv,
  191. u32 count)
  192. {
  193. int timeout = 2;
  194. u32 done;
  195. /* Clear any existing DONE status. */
  196. if (socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_DCLKSTAT_OFST))
  197. socfpga_fpga_clear_done_status(priv);
  198. /* Issue the DCLK count. */
  199. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_DCLKCNT_OFST, count);
  200. /* Poll DCLKSTAT to see if it completed in the timeout period. */
  201. do {
  202. done = socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_DCLKSTAT_OFST);
  203. if (done == SOCFPGA_FPGMGR_DCLKSTAT_DCNTDONE_E_DONE) {
  204. socfpga_fpga_clear_done_status(priv);
  205. return 0;
  206. }
  207. udelay(1);
  208. } while (timeout--);
  209. return -ETIMEDOUT;
  210. }
  211. static int socfpga_fpga_wait_for_state(struct socfpga_fpga_priv *priv,
  212. u32 state)
  213. {
  214. int timeout = 2;
  215. /*
  216. * HW doesn't support an interrupt for changes in state, so poll to see
  217. * if it matches the requested state within the timeout period.
  218. */
  219. do {
  220. if ((socfpga_fpga_state_get(priv) & state) != 0)
  221. return 0;
  222. msleep(20);
  223. } while (timeout--);
  224. return -ETIMEDOUT;
  225. }
  226. static void socfpga_fpga_enable_irqs(struct socfpga_fpga_priv *priv, u32 irqs)
  227. {
  228. /* set irqs to level sensitive */
  229. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_INTTYPE_LEVEL_OFST, 0);
  230. /* set interrupt polarity */
  231. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_INT_POL_OFST, irqs);
  232. /* clear irqs */
  233. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_PORTA_EOI_OFST, irqs);
  234. /* unmask interrupts */
  235. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_INTMSK_OFST, 0);
  236. /* enable interrupts */
  237. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_INTEN_OFST, irqs);
  238. }
  239. static void socfpga_fpga_disable_irqs(struct socfpga_fpga_priv *priv)
  240. {
  241. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_INTEN_OFST, 0);
  242. }
  243. static irqreturn_t socfpga_fpga_isr(int irq, void *dev_id)
  244. {
  245. struct socfpga_fpga_priv *priv = dev_id;
  246. u32 irqs, st;
  247. bool conf_done, nstatus;
  248. /* clear irqs */
  249. irqs = socfpga_fpga_raw_readl(priv, SOCFPGA_FPGMGR_GPIO_INTSTAT_OFST);
  250. socfpga_fpga_raw_writel(priv, SOCFPGA_FPGMGR_GPIO_PORTA_EOI_OFST, irqs);
  251. st = socfpga_fpga_raw_readl(priv, SOCFPGA_FPGMGR_GPIO_EXT_PORTA_OFST);
  252. conf_done = (st & SOCFPGA_FPGMGR_MON_CONF_DONE) != 0;
  253. nstatus = (st & SOCFPGA_FPGMGR_MON_NSTATUS) != 0;
  254. /* success */
  255. if (conf_done && nstatus) {
  256. /* disable irqs */
  257. socfpga_fpga_raw_writel(priv,
  258. SOCFPGA_FPGMGR_GPIO_INTEN_OFST, 0);
  259. complete(&priv->status_complete);
  260. }
  261. return IRQ_HANDLED;
  262. }
  263. static int socfpga_fpga_wait_for_config_done(struct socfpga_fpga_priv *priv)
  264. {
  265. int timeout, ret = 0;
  266. socfpga_fpga_disable_irqs(priv);
  267. init_completion(&priv->status_complete);
  268. socfpga_fpga_enable_irqs(priv, SOCFPGA_FPGMGR_MON_CONF_DONE);
  269. timeout = wait_for_completion_interruptible_timeout(
  270. &priv->status_complete,
  271. msecs_to_jiffies(10));
  272. if (timeout == 0)
  273. ret = -ETIMEDOUT;
  274. socfpga_fpga_disable_irqs(priv);
  275. return ret;
  276. }
  277. static int socfpga_fpga_cfg_mode_get(struct socfpga_fpga_priv *priv)
  278. {
  279. u32 msel;
  280. msel = socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_STAT_OFST);
  281. msel &= SOCFPGA_FPGMGR_STAT_MSEL_MASK;
  282. msel >>= SOCFPGA_FPGMGR_STAT_MSEL_SHIFT;
  283. /* Check that this MSEL setting is supported */
  284. if ((msel >= ARRAY_SIZE(cfgmgr_modes)) || !cfgmgr_modes[msel].valid)
  285. return -EINVAL;
  286. return msel;
  287. }
  288. static int socfpga_fpga_cfg_mode_set(struct socfpga_fpga_priv *priv)
  289. {
  290. u32 ctrl_reg;
  291. int mode;
  292. /* get value from MSEL pins */
  293. mode = socfpga_fpga_cfg_mode_get(priv);
  294. if (mode < 0)
  295. return mode;
  296. /* Adjust CTRL for the CDRATIO */
  297. ctrl_reg = socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_CTL_OFST);
  298. ctrl_reg &= ~SOCFPGA_FPGMGR_CTL_CDRATIO_MASK;
  299. ctrl_reg &= ~SOCFPGA_FPGMGR_CTL_CFGWDTH_MASK;
  300. ctrl_reg |= cfgmgr_modes[mode].ctrl;
  301. /* Set NCE to 0. */
  302. ctrl_reg &= ~SOCFPGA_FPGMGR_CTL_NCE;
  303. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_CTL_OFST, ctrl_reg);
  304. return 0;
  305. }
  306. static int socfpga_fpga_reset(struct fpga_manager *mgr)
  307. {
  308. struct socfpga_fpga_priv *priv = mgr->priv;
  309. u32 ctrl_reg, status;
  310. int ret;
  311. /*
  312. * Step 1:
  313. * - Set CTRL.CFGWDTH, CTRL.CDRATIO to match cfg mode
  314. * - Set CTRL.NCE to 0
  315. */
  316. ret = socfpga_fpga_cfg_mode_set(priv);
  317. if (ret)
  318. return ret;
  319. /* Step 2: Set CTRL.EN to 1 */
  320. socfpga_fpga_set_bitsl(priv, SOCFPGA_FPGMGR_CTL_OFST,
  321. SOCFPGA_FPGMGR_CTL_EN);
  322. /* Step 3: Set CTRL.NCONFIGPULL to 1 to put FPGA in reset */
  323. ctrl_reg = socfpga_fpga_readl(priv, SOCFPGA_FPGMGR_CTL_OFST);
  324. ctrl_reg |= SOCFPGA_FPGMGR_CTL_NCFGPULL;
  325. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_CTL_OFST, ctrl_reg);
  326. /* Step 4: Wait for STATUS.MODE to report FPGA is in reset phase */
  327. status = socfpga_fpga_wait_for_state(priv, SOCFPGA_FPGMGR_STAT_RESET);
  328. /* Step 5: Set CONTROL.NCONFIGPULL to 0 to release FPGA from reset */
  329. ctrl_reg &= ~SOCFPGA_FPGMGR_CTL_NCFGPULL;
  330. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_CTL_OFST, ctrl_reg);
  331. /* Timeout waiting for reset */
  332. if (status)
  333. return -ETIMEDOUT;
  334. return 0;
  335. }
  336. /*
  337. * Prepare the FPGA to receive the configuration data.
  338. */
  339. static int socfpga_fpga_ops_configure_init(struct fpga_manager *mgr, u32 flags,
  340. const char *buf, size_t count)
  341. {
  342. struct socfpga_fpga_priv *priv = mgr->priv;
  343. int ret;
  344. if (flags & FPGA_MGR_PARTIAL_RECONFIG) {
  345. dev_err(&mgr->dev, "Partial reconfiguration not supported.\n");
  346. return -EINVAL;
  347. }
  348. /* Steps 1 - 5: Reset the FPGA */
  349. ret = socfpga_fpga_reset(mgr);
  350. if (ret)
  351. return ret;
  352. /* Step 6: Wait for FPGA to enter configuration phase */
  353. if (socfpga_fpga_wait_for_state(priv, SOCFPGA_FPGMGR_STAT_CFG))
  354. return -ETIMEDOUT;
  355. /* Step 7: Clear nSTATUS interrupt */
  356. socfpga_fpga_writel(priv, SOCFPGA_FPGMGR_GPIO_PORTA_EOI_OFST,
  357. SOCFPGA_FPGMGR_MON_NSTATUS);
  358. /* Step 8: Set CTRL.AXICFGEN to 1 to enable transfer of config data */
  359. socfpga_fpga_set_bitsl(priv, SOCFPGA_FPGMGR_CTL_OFST,
  360. SOCFPGA_FPGMGR_CTL_AXICFGEN);
  361. return 0;
  362. }
  363. /*
  364. * Step 9: write data to the FPGA data register
  365. */
  366. static int socfpga_fpga_ops_configure_write(struct fpga_manager *mgr,
  367. const char *buf, size_t count)
  368. {
  369. struct socfpga_fpga_priv *priv = mgr->priv;
  370. u32 *buffer_32 = (u32 *)buf;
  371. size_t i = 0;
  372. if (count <= 0)
  373. return -EINVAL;
  374. /* Write out the complete 32-bit chunks. */
  375. while (count >= sizeof(u32)) {
  376. socfpga_fpga_data_writel(priv, buffer_32[i++]);
  377. count -= sizeof(u32);
  378. }
  379. /* Write out remaining non 32-bit chunks. */
  380. switch (count) {
  381. case 3:
  382. socfpga_fpga_data_writel(priv, buffer_32[i++] & 0x00ffffff);
  383. break;
  384. case 2:
  385. socfpga_fpga_data_writel(priv, buffer_32[i++] & 0x0000ffff);
  386. break;
  387. case 1:
  388. socfpga_fpga_data_writel(priv, buffer_32[i++] & 0x000000ff);
  389. break;
  390. case 0:
  391. break;
  392. default:
  393. /* This will never happen. */
  394. return -EFAULT;
  395. }
  396. return 0;
  397. }
  398. static int socfpga_fpga_ops_configure_complete(struct fpga_manager *mgr,
  399. u32 flags)
  400. {
  401. struct socfpga_fpga_priv *priv = mgr->priv;
  402. u32 status;
  403. /*
  404. * Step 10:
  405. * - Observe CONF_DONE and nSTATUS (active low)
  406. * - if CONF_DONE = 1 and nSTATUS = 1, configuration was successful
  407. * - if CONF_DONE = 0 and nSTATUS = 0, configuration failed
  408. */
  409. status = socfpga_fpga_wait_for_config_done(priv);
  410. if (status)
  411. return status;
  412. /* Step 11: Clear CTRL.AXICFGEN to disable transfer of config data */
  413. socfpga_fpga_clr_bitsl(priv, SOCFPGA_FPGMGR_CTL_OFST,
  414. SOCFPGA_FPGMGR_CTL_AXICFGEN);
  415. /*
  416. * Step 12:
  417. * - Write 4 to DCLKCNT
  418. * - Wait for STATUS.DCNTDONE = 1
  419. * - Clear W1C bit in STATUS.DCNTDONE
  420. */
  421. if (socfpga_fpga_dclk_set_and_wait_clear(priv, 4))
  422. return -ETIMEDOUT;
  423. /* Step 13: Wait for STATUS.MODE to report USER MODE */
  424. if (socfpga_fpga_wait_for_state(priv, SOCFPGA_FPGMGR_STAT_USER_MODE))
  425. return -ETIMEDOUT;
  426. /* Step 14: Set CTRL.EN to 0 */
  427. socfpga_fpga_clr_bitsl(priv, SOCFPGA_FPGMGR_CTL_OFST,
  428. SOCFPGA_FPGMGR_CTL_EN);
  429. return 0;
  430. }
  431. /* Translate state register values to FPGA framework state */
  432. static const enum fpga_mgr_states socfpga_state_to_framework_state[] = {
  433. [SOCFPGA_FPGMGR_STAT_POWER_OFF] = FPGA_MGR_STATE_POWER_OFF,
  434. [SOCFPGA_FPGMGR_STAT_RESET] = FPGA_MGR_STATE_RESET,
  435. [SOCFPGA_FPGMGR_STAT_CFG] = FPGA_MGR_STATE_WRITE_INIT,
  436. [SOCFPGA_FPGMGR_STAT_INIT] = FPGA_MGR_STATE_WRITE_INIT,
  437. [SOCFPGA_FPGMGR_STAT_USER_MODE] = FPGA_MGR_STATE_OPERATING,
  438. [SOCFPGA_FPGMGR_STAT_UNKNOWN] = FPGA_MGR_STATE_UNKNOWN,
  439. };
  440. static enum fpga_mgr_states socfpga_fpga_ops_state(struct fpga_manager *mgr)
  441. {
  442. struct socfpga_fpga_priv *priv = mgr->priv;
  443. enum fpga_mgr_states ret;
  444. u32 state;
  445. state = socfpga_fpga_state_get(priv);
  446. if (state < ARRAY_SIZE(socfpga_state_to_framework_state))
  447. ret = socfpga_state_to_framework_state[state];
  448. else
  449. ret = FPGA_MGR_STATE_UNKNOWN;
  450. return ret;
  451. }
  452. static const struct fpga_manager_ops socfpga_fpga_ops = {
  453. .state = socfpga_fpga_ops_state,
  454. .write_init = socfpga_fpga_ops_configure_init,
  455. .write = socfpga_fpga_ops_configure_write,
  456. .write_complete = socfpga_fpga_ops_configure_complete,
  457. };
  458. static int socfpga_fpga_probe(struct platform_device *pdev)
  459. {
  460. struct device *dev = &pdev->dev;
  461. struct socfpga_fpga_priv *priv;
  462. struct resource *res;
  463. int ret;
  464. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  465. if (!priv)
  466. return -ENOMEM;
  467. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  468. priv->fpga_base_addr = devm_ioremap_resource(dev, res);
  469. if (IS_ERR(priv->fpga_base_addr))
  470. return PTR_ERR(priv->fpga_base_addr);
  471. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  472. priv->fpga_data_addr = devm_ioremap_resource(dev, res);
  473. if (IS_ERR(priv->fpga_data_addr))
  474. return PTR_ERR(priv->fpga_data_addr);
  475. priv->irq = platform_get_irq(pdev, 0);
  476. if (priv->irq < 0)
  477. return priv->irq;
  478. ret = devm_request_irq(dev, priv->irq, socfpga_fpga_isr, 0,
  479. dev_name(dev), priv);
  480. if (ret)
  481. return ret;
  482. return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
  483. &socfpga_fpga_ops, priv);
  484. }
  485. static int socfpga_fpga_remove(struct platform_device *pdev)
  486. {
  487. fpga_mgr_unregister(&pdev->dev);
  488. return 0;
  489. }
  490. #ifdef CONFIG_OF
  491. static const struct of_device_id socfpga_fpga_of_match[] = {
  492. { .compatible = "altr,socfpga-fpga-mgr", },
  493. {},
  494. };
  495. MODULE_DEVICE_TABLE(of, socfpga_fpga_of_match);
  496. #endif
  497. static struct platform_driver socfpga_fpga_driver = {
  498. .probe = socfpga_fpga_probe,
  499. .remove = socfpga_fpga_remove,
  500. .driver = {
  501. .name = "socfpga_fpga_manager",
  502. .of_match_table = of_match_ptr(socfpga_fpga_of_match),
  503. },
  504. };
  505. module_platform_driver(socfpga_fpga_driver);
  506. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  507. MODULE_DESCRIPTION("Altera SOCFPGA FPGA Manager");
  508. MODULE_LICENSE("GPL v2");