USB: serial: option: add support for Telit ME910 PID 0x1101
[linux/fpc-iii.git] / drivers / fpga / altera-pr-ip-core.c
bloba7b31f9797ce180daa0e0cf91b69424300556626
1 /*
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
16 * more details.
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)
39 struct alt_pr_priv {
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;
48 u32 val;
50 val = readl(priv->reg_base + ALT_PR_CSR_OFST);
52 val &= ALT_PR_CSR_STATUS_MSK;
54 switch (val) {
55 case ALT_PR_CSR_STATUS_NRESET:
56 return FPGA_MGR_STATE_RESET;
58 case ALT_PR_CSR_STATUS_PR_ERR:
59 err = "pr error";
60 ret = FPGA_MGR_STATE_WRITE_ERR;
61 break;
63 case ALT_PR_CSR_STATUS_CRC_ERR:
64 err = "crc error";
65 ret = FPGA_MGR_STATE_WRITE_ERR;
66 break;
68 case ALT_PR_CSR_STATUS_BAD_BITS:
69 err = "bad bits";
70 ret = FPGA_MGR_STATE_WRITE_ERR;
71 break;
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;
79 default:
80 break;
83 dev_err(&mgr->dev, "encountered error code %d (%s) in %s()\n",
84 val, err, __func__);
85 return ret;
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;
93 u32 val;
95 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
96 dev_err(&mgr->dev, "%s Partial Reconfiguration flag not set\n",
97 __func__);
98 return -EINVAL;
101 val = readl(priv->reg_base + ALT_PR_CSR_OFST);
103 if (val & ALT_PR_CSR_PR_START) {
104 dev_err(&mgr->dev,
105 "%s Partial Reconfiguration already started\n",
106 __func__);
107 return -EINVAL;
110 writel(val | ALT_PR_CSR_PR_START, priv->reg_base + ALT_PR_CSR_OFST);
112 return 0;
115 static int alt_pr_fpga_write(struct fpga_manager *mgr, const char *buf,
116 size_t count)
118 struct alt_pr_priv *priv = mgr->priv;
119 u32 *buffer_32 = (u32 *)buf;
120 size_t i = 0;
122 if (count <= 0)
123 return -EINVAL;
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 */
132 switch (count) {
133 case 3:
134 writel(buffer_32[i++] & 0x00ffffff, priv->reg_base);
135 break;
136 case 2:
137 writel(buffer_32[i++] & 0x0000ffff, priv->reg_base);
138 break;
139 case 1:
140 writel(buffer_32[i++] & 0x000000ff, priv->reg_base);
141 break;
142 case 0:
143 break;
144 default:
145 /* This will never happen */
146 return -EFAULT;
149 if (alt_pr_fpga_state(mgr) == FPGA_MGR_STATE_WRITE_ERR)
150 return -EIO;
152 return 0;
155 static int alt_pr_fpga_write_complete(struct fpga_manager *mgr,
156 struct fpga_image_info *info)
158 u32 i = 0;
160 do {
161 switch (alt_pr_fpga_state(mgr)) {
162 case FPGA_MGR_STATE_WRITE_ERR:
163 return -EIO;
165 case FPGA_MGR_STATE_OPERATING:
166 dev_info(&mgr->dev,
167 "successful partial reconfiguration\n");
168 return 0;
170 default:
171 break;
173 udelay(1);
174 } while (info->config_complete_timeout_us > i++);
176 dev_err(&mgr->dev, "timed out waiting for write to complete\n");
177 return -ETIMEDOUT;
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;
190 u32 val;
192 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
193 if (!priv)
194 return -ENOMEM;
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);
214 return 0;
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");