2 * Driver for Altera Partial Reconfiguration IP Core
4 * Copyright (C) 2016-2017 Intel Corporation
6 * Based on socfpga-a10.c Copyright (C) 2015-2016 Altera Corporation
7 * by Alan Tull <atull@opensource.altera.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/delay.h>
22 #include <linux/fpga/altera-pr-ip-core.h>
23 #include <linux/fpga/fpga-mgr.h>
24 #include <linux/module.h>
26 #define ALT_PR_DATA_OFST 0x00
27 #define ALT_PR_CSR_OFST 0x04
29 #define ALT_PR_CSR_PR_START BIT(0)
30 #define ALT_PR_CSR_STATUS_SFT 2
31 #define ALT_PR_CSR_STATUS_MSK (7 << ALT_PR_CSR_STATUS_SFT)
32 #define ALT_PR_CSR_STATUS_NRESET (0 << ALT_PR_CSR_STATUS_SFT)
33 #define ALT_PR_CSR_STATUS_PR_ERR (1 << ALT_PR_CSR_STATUS_SFT)
34 #define ALT_PR_CSR_STATUS_CRC_ERR (2 << ALT_PR_CSR_STATUS_SFT)
35 #define ALT_PR_CSR_STATUS_BAD_BITS (3 << ALT_PR_CSR_STATUS_SFT)
36 #define ALT_PR_CSR_STATUS_PR_IN_PROG (4 << ALT_PR_CSR_STATUS_SFT)
37 #define ALT_PR_CSR_STATUS_PR_SUCCESS (5 << ALT_PR_CSR_STATUS_SFT)
40 void __iomem
*reg_base
;
43 static enum fpga_mgr_states
alt_pr_fpga_state(struct fpga_manager
*mgr
)
45 struct alt_pr_priv
*priv
= mgr
->priv
;
46 const char *err
= "unknown";
47 enum fpga_mgr_states ret
= FPGA_MGR_STATE_UNKNOWN
;
50 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
52 val
&= ALT_PR_CSR_STATUS_MSK
;
55 case ALT_PR_CSR_STATUS_NRESET
:
56 return FPGA_MGR_STATE_RESET
;
58 case ALT_PR_CSR_STATUS_PR_ERR
:
60 ret
= FPGA_MGR_STATE_WRITE_ERR
;
63 case ALT_PR_CSR_STATUS_CRC_ERR
:
65 ret
= FPGA_MGR_STATE_WRITE_ERR
;
68 case ALT_PR_CSR_STATUS_BAD_BITS
:
70 ret
= FPGA_MGR_STATE_WRITE_ERR
;
73 case ALT_PR_CSR_STATUS_PR_IN_PROG
:
74 return FPGA_MGR_STATE_WRITE
;
76 case ALT_PR_CSR_STATUS_PR_SUCCESS
:
77 return FPGA_MGR_STATE_OPERATING
;
83 dev_err(&mgr
->dev
, "encountered error code %d (%s) in %s()\n",
88 static int alt_pr_fpga_write_init(struct fpga_manager
*mgr
,
89 struct fpga_image_info
*info
,
90 const char *buf
, size_t count
)
92 struct alt_pr_priv
*priv
= mgr
->priv
;
95 if (!(info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
)) {
96 dev_err(&mgr
->dev
, "%s Partial Reconfiguration flag not set\n",
101 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
103 if (val
& ALT_PR_CSR_PR_START
) {
105 "%s Partial Reconfiguration already started\n",
110 writel(val
| ALT_PR_CSR_PR_START
, priv
->reg_base
+ ALT_PR_CSR_OFST
);
115 static int alt_pr_fpga_write(struct fpga_manager
*mgr
, const char *buf
,
118 struct alt_pr_priv
*priv
= mgr
->priv
;
119 u32
*buffer_32
= (u32
*)buf
;
125 /* Write out the complete 32-bit chunks */
126 while (count
>= sizeof(u32
)) {
127 writel(buffer_32
[i
++], priv
->reg_base
);
128 count
-= sizeof(u32
);
131 /* Write out remaining non 32-bit chunks */
134 writel(buffer_32
[i
++] & 0x00ffffff, priv
->reg_base
);
137 writel(buffer_32
[i
++] & 0x0000ffff, priv
->reg_base
);
140 writel(buffer_32
[i
++] & 0x000000ff, priv
->reg_base
);
145 /* This will never happen */
149 if (alt_pr_fpga_state(mgr
) == FPGA_MGR_STATE_WRITE_ERR
)
155 static int alt_pr_fpga_write_complete(struct fpga_manager
*mgr
,
156 struct fpga_image_info
*info
)
161 switch (alt_pr_fpga_state(mgr
)) {
162 case FPGA_MGR_STATE_WRITE_ERR
:
165 case FPGA_MGR_STATE_OPERATING
:
167 "successful partial reconfiguration\n");
174 } while (info
->config_complete_timeout_us
> i
++);
176 dev_err(&mgr
->dev
, "timed out waiting for write to complete\n");
180 static const struct fpga_manager_ops alt_pr_ops
= {
181 .state
= alt_pr_fpga_state
,
182 .write_init
= alt_pr_fpga_write_init
,
183 .write
= alt_pr_fpga_write
,
184 .write_complete
= alt_pr_fpga_write_complete
,
187 int alt_pr_register(struct device
*dev
, void __iomem
*reg_base
)
189 struct alt_pr_priv
*priv
;
192 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
196 priv
->reg_base
= reg_base
;
198 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
200 dev_dbg(dev
, "%s status=%d start=%d\n", __func__
,
201 (val
& ALT_PR_CSR_STATUS_MSK
) >> ALT_PR_CSR_STATUS_SFT
,
202 (int)(val
& ALT_PR_CSR_PR_START
));
204 return fpga_mgr_register(dev
, dev_name(dev
), &alt_pr_ops
, priv
);
206 EXPORT_SYMBOL_GPL(alt_pr_register
);
208 int alt_pr_unregister(struct device
*dev
)
210 dev_dbg(dev
, "%s\n", __func__
);
212 fpga_mgr_unregister(dev
);
216 EXPORT_SYMBOL_GPL(alt_pr_unregister
);
218 MODULE_AUTHOR("Matthew Gerlach <matthew.gerlach@linux.intel.com>");
219 MODULE_DESCRIPTION("Altera Partial Reconfiguration IP Core");
220 MODULE_LICENSE("GPL v2");