1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Altera Partial Reconfiguration IP Core
5 * Copyright (C) 2016-2017 Intel Corporation
7 * Based on socfpga-a10.c Copyright (C) 2015-2016 Altera Corporation
8 * by Alan Tull <atull@opensource.altera.com>
10 #include <linux/delay.h>
11 #include <linux/fpga/altera-pr-ip-core.h>
12 #include <linux/fpga/fpga-mgr.h>
13 #include <linux/module.h>
15 #define ALT_PR_DATA_OFST 0x00
16 #define ALT_PR_CSR_OFST 0x04
18 #define ALT_PR_CSR_PR_START BIT(0)
19 #define ALT_PR_CSR_STATUS_SFT 2
20 #define ALT_PR_CSR_STATUS_MSK (7 << ALT_PR_CSR_STATUS_SFT)
21 #define ALT_PR_CSR_STATUS_NRESET (0 << ALT_PR_CSR_STATUS_SFT)
22 #define ALT_PR_CSR_STATUS_PR_ERR (1 << ALT_PR_CSR_STATUS_SFT)
23 #define ALT_PR_CSR_STATUS_CRC_ERR (2 << ALT_PR_CSR_STATUS_SFT)
24 #define ALT_PR_CSR_STATUS_BAD_BITS (3 << ALT_PR_CSR_STATUS_SFT)
25 #define ALT_PR_CSR_STATUS_PR_IN_PROG (4 << ALT_PR_CSR_STATUS_SFT)
26 #define ALT_PR_CSR_STATUS_PR_SUCCESS (5 << ALT_PR_CSR_STATUS_SFT)
29 void __iomem
*reg_base
;
32 static enum fpga_mgr_states
alt_pr_fpga_state(struct fpga_manager
*mgr
)
34 struct alt_pr_priv
*priv
= mgr
->priv
;
35 const char *err
= "unknown";
36 enum fpga_mgr_states ret
= FPGA_MGR_STATE_UNKNOWN
;
39 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
41 val
&= ALT_PR_CSR_STATUS_MSK
;
44 case ALT_PR_CSR_STATUS_NRESET
:
45 return FPGA_MGR_STATE_RESET
;
47 case ALT_PR_CSR_STATUS_PR_ERR
:
49 ret
= FPGA_MGR_STATE_WRITE_ERR
;
52 case ALT_PR_CSR_STATUS_CRC_ERR
:
54 ret
= FPGA_MGR_STATE_WRITE_ERR
;
57 case ALT_PR_CSR_STATUS_BAD_BITS
:
59 ret
= FPGA_MGR_STATE_WRITE_ERR
;
62 case ALT_PR_CSR_STATUS_PR_IN_PROG
:
63 return FPGA_MGR_STATE_WRITE
;
65 case ALT_PR_CSR_STATUS_PR_SUCCESS
:
66 return FPGA_MGR_STATE_OPERATING
;
72 dev_err(&mgr
->dev
, "encountered error code %d (%s) in %s()\n",
77 static int alt_pr_fpga_write_init(struct fpga_manager
*mgr
,
78 struct fpga_image_info
*info
,
79 const char *buf
, size_t count
)
81 struct alt_pr_priv
*priv
= mgr
->priv
;
84 if (!(info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
)) {
85 dev_err(&mgr
->dev
, "%s Partial Reconfiguration flag not set\n",
90 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
92 if (val
& ALT_PR_CSR_PR_START
) {
94 "%s Partial Reconfiguration already started\n",
99 writel(val
| ALT_PR_CSR_PR_START
, priv
->reg_base
+ ALT_PR_CSR_OFST
);
104 static int alt_pr_fpga_write(struct fpga_manager
*mgr
, const char *buf
,
107 struct alt_pr_priv
*priv
= mgr
->priv
;
108 u32
*buffer_32
= (u32
*)buf
;
114 /* Write out the complete 32-bit chunks */
115 while (count
>= sizeof(u32
)) {
116 writel(buffer_32
[i
++], priv
->reg_base
);
117 count
-= sizeof(u32
);
120 /* Write out remaining non 32-bit chunks */
123 writel(buffer_32
[i
++] & 0x00ffffff, priv
->reg_base
);
126 writel(buffer_32
[i
++] & 0x0000ffff, priv
->reg_base
);
129 writel(buffer_32
[i
++] & 0x000000ff, priv
->reg_base
);
134 /* This will never happen */
138 if (alt_pr_fpga_state(mgr
) == FPGA_MGR_STATE_WRITE_ERR
)
144 static int alt_pr_fpga_write_complete(struct fpga_manager
*mgr
,
145 struct fpga_image_info
*info
)
150 switch (alt_pr_fpga_state(mgr
)) {
151 case FPGA_MGR_STATE_WRITE_ERR
:
154 case FPGA_MGR_STATE_OPERATING
:
156 "successful partial reconfiguration\n");
163 } while (info
->config_complete_timeout_us
> i
++);
165 dev_err(&mgr
->dev
, "timed out waiting for write to complete\n");
169 static const struct fpga_manager_ops alt_pr_ops
= {
170 .state
= alt_pr_fpga_state
,
171 .write_init
= alt_pr_fpga_write_init
,
172 .write
= alt_pr_fpga_write
,
173 .write_complete
= alt_pr_fpga_write_complete
,
176 int alt_pr_register(struct device
*dev
, void __iomem
*reg_base
)
178 struct alt_pr_priv
*priv
;
179 struct fpga_manager
*mgr
;
182 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
186 priv
->reg_base
= reg_base
;
188 val
= readl(priv
->reg_base
+ ALT_PR_CSR_OFST
);
190 dev_dbg(dev
, "%s status=%d start=%d\n", __func__
,
191 (val
& ALT_PR_CSR_STATUS_MSK
) >> ALT_PR_CSR_STATUS_SFT
,
192 (int)(val
& ALT_PR_CSR_PR_START
));
194 mgr
= devm_fpga_mgr_create(dev
, dev_name(dev
), &alt_pr_ops
, priv
);
198 dev_set_drvdata(dev
, mgr
);
200 return fpga_mgr_register(mgr
);
202 EXPORT_SYMBOL_GPL(alt_pr_register
);
204 int alt_pr_unregister(struct device
*dev
)
206 struct fpga_manager
*mgr
= dev_get_drvdata(dev
);
208 dev_dbg(dev
, "%s\n", __func__
);
210 fpga_mgr_unregister(mgr
);
214 EXPORT_SYMBOL_GPL(alt_pr_unregister
);
216 MODULE_AUTHOR("Matthew Gerlach <matthew.gerlach@linux.intel.com>");
217 MODULE_DESCRIPTION("Altera Partial Reconfiguration IP Core");
218 MODULE_LICENSE("GPL v2");