aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/fpga/altera-cvp.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/fpga/altera-cvp.c')
-rw-r--r--drivers/fpga/altera-cvp.c339
1 files changed, 268 insertions, 71 deletions
diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
index 53b963071c7b..4e0edb60bfba 100644
--- a/drivers/fpga/altera-cvp.c
+++ b/drivers/fpga/altera-cvp.c
@@ -22,10 +22,10 @@
22#define TIMEOUT_US 2000 /* CVP STATUS timeout for USERMODE polling */ 22#define TIMEOUT_US 2000 /* CVP STATUS timeout for USERMODE polling */
23 23
24/* Vendor Specific Extended Capability Registers */ 24/* Vendor Specific Extended Capability Registers */
25#define VSE_PCIE_EXT_CAP_ID 0x200 25#define VSE_PCIE_EXT_CAP_ID 0x0
26#define VSE_PCIE_EXT_CAP_ID_VAL 0x000b /* 16bit */ 26#define VSE_PCIE_EXT_CAP_ID_VAL 0x000b /* 16bit */
27 27
28#define VSE_CVP_STATUS 0x21c /* 32bit */ 28#define VSE_CVP_STATUS 0x1c /* 32bit */
29#define VSE_CVP_STATUS_CFG_RDY BIT(18) /* CVP_CONFIG_READY */ 29#define VSE_CVP_STATUS_CFG_RDY BIT(18) /* CVP_CONFIG_READY */
30#define VSE_CVP_STATUS_CFG_ERR BIT(19) /* CVP_CONFIG_ERROR */ 30#define VSE_CVP_STATUS_CFG_ERR BIT(19) /* CVP_CONFIG_ERROR */
31#define VSE_CVP_STATUS_CVP_EN BIT(20) /* ctrl block is enabling CVP */ 31#define VSE_CVP_STATUS_CVP_EN BIT(20) /* ctrl block is enabling CVP */
@@ -33,26 +33,44 @@
33#define VSE_CVP_STATUS_CFG_DONE BIT(23) /* CVP_CONFIG_DONE */ 33#define VSE_CVP_STATUS_CFG_DONE BIT(23) /* CVP_CONFIG_DONE */
34#define VSE_CVP_STATUS_PLD_CLK_IN_USE BIT(24) /* PLD_CLK_IN_USE */ 34#define VSE_CVP_STATUS_PLD_CLK_IN_USE BIT(24) /* PLD_CLK_IN_USE */
35 35
36#define VSE_CVP_MODE_CTRL 0x220 /* 32bit */ 36#define VSE_CVP_MODE_CTRL 0x20 /* 32bit */
37#define VSE_CVP_MODE_CTRL_CVP_MODE BIT(0) /* CVP (1) or normal mode (0) */ 37#define VSE_CVP_MODE_CTRL_CVP_MODE BIT(0) /* CVP (1) or normal mode (0) */
38#define VSE_CVP_MODE_CTRL_HIP_CLK_SEL BIT(1) /* PMA (1) or fabric clock (0) */ 38#define VSE_CVP_MODE_CTRL_HIP_CLK_SEL BIT(1) /* PMA (1) or fabric clock (0) */
39#define VSE_CVP_MODE_CTRL_NUMCLKS_OFF 8 /* NUMCLKS bits offset */ 39#define VSE_CVP_MODE_CTRL_NUMCLKS_OFF 8 /* NUMCLKS bits offset */
40#define VSE_CVP_MODE_CTRL_NUMCLKS_MASK GENMASK(15, 8) 40#define VSE_CVP_MODE_CTRL_NUMCLKS_MASK GENMASK(15, 8)
41 41
42#define VSE_CVP_DATA 0x228 /* 32bit */ 42#define VSE_CVP_DATA 0x28 /* 32bit */
43#define VSE_CVP_PROG_CTRL 0x22c /* 32bit */ 43#define VSE_CVP_PROG_CTRL 0x2c /* 32bit */
44#define VSE_CVP_PROG_CTRL_CONFIG BIT(0) 44#define VSE_CVP_PROG_CTRL_CONFIG BIT(0)
45#define VSE_CVP_PROG_CTRL_START_XFER BIT(1) 45#define VSE_CVP_PROG_CTRL_START_XFER BIT(1)
46#define VSE_CVP_PROG_CTRL_MASK GENMASK(1, 0)
46 47
47#define VSE_UNCOR_ERR_STATUS 0x234 /* 32bit */ 48#define VSE_UNCOR_ERR_STATUS 0x34 /* 32bit */
48#define VSE_UNCOR_ERR_CVP_CFG_ERR BIT(5) /* CVP_CONFIG_ERROR_LATCHED */ 49#define VSE_UNCOR_ERR_CVP_CFG_ERR BIT(5) /* CVP_CONFIG_ERROR_LATCHED */
49 50
51#define V1_VSEC_OFFSET 0x200 /* Vendor Specific Offset V1 */
52/* V2 Defines */
53#define VSE_CVP_TX_CREDITS 0x49 /* 8bit */
54
55#define V2_CREDIT_TIMEOUT_US 20000
56#define V2_CHECK_CREDIT_US 10
57#define V2_POLL_TIMEOUT_US 1000000
58#define V2_USER_TIMEOUT_US 500000
59
60#define V1_POLL_TIMEOUT_US 10
61
50#define DRV_NAME "altera-cvp" 62#define DRV_NAME "altera-cvp"
51#define ALTERA_CVP_MGR_NAME "Altera CvP FPGA Manager" 63#define ALTERA_CVP_MGR_NAME "Altera CvP FPGA Manager"
52 64
65/* Write block sizes */
66#define ALTERA_CVP_V1_SIZE 4
67#define ALTERA_CVP_V2_SIZE 4096
68
53/* Optional CvP config error status check for debugging */ 69/* Optional CvP config error status check for debugging */
54static bool altera_cvp_chkcfg; 70static bool altera_cvp_chkcfg;
55 71
72struct cvp_priv;
73
56struct altera_cvp_conf { 74struct altera_cvp_conf {
57 struct fpga_manager *mgr; 75 struct fpga_manager *mgr;
58 struct pci_dev *pci_dev; 76 struct pci_dev *pci_dev;
@@ -61,14 +79,47 @@ struct altera_cvp_conf {
61 u32 data); 79 u32 data);
62 char mgr_name[64]; 80 char mgr_name[64];
63 u8 numclks; 81 u8 numclks;
82 u32 sent_packets;
83 u32 vsec_offset;
84 const struct cvp_priv *priv;
85};
86
87struct cvp_priv {
88 void (*switch_clk)(struct altera_cvp_conf *conf);
89 int (*clear_state)(struct altera_cvp_conf *conf);
90 int (*wait_credit)(struct fpga_manager *mgr, u32 blocks);
91 size_t block_size;
92 int poll_time_us;
93 int user_time_us;
64}; 94};
65 95
96static int altera_read_config_byte(struct altera_cvp_conf *conf,
97 int where, u8 *val)
98{
99 return pci_read_config_byte(conf->pci_dev, conf->vsec_offset + where,
100 val);
101}
102
103static int altera_read_config_dword(struct altera_cvp_conf *conf,
104 int where, u32 *val)
105{
106 return pci_read_config_dword(conf->pci_dev, conf->vsec_offset + where,
107 val);
108}
109
110static int altera_write_config_dword(struct altera_cvp_conf *conf,
111 int where, u32 val)
112{
113 return pci_write_config_dword(conf->pci_dev, conf->vsec_offset + where,
114 val);
115}
116
66static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr) 117static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr)
67{ 118{
68 struct altera_cvp_conf *conf = mgr->priv; 119 struct altera_cvp_conf *conf = mgr->priv;
69 u32 status; 120 u32 status;
70 121
71 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &status); 122 altera_read_config_dword(conf, VSE_CVP_STATUS, &status);
72 123
73 if (status & VSE_CVP_STATUS_CFG_DONE) 124 if (status & VSE_CVP_STATUS_CFG_DONE)
74 return FPGA_MGR_STATE_OPERATING; 125 return FPGA_MGR_STATE_OPERATING;
@@ -86,7 +137,8 @@ static void altera_cvp_write_data_iomem(struct altera_cvp_conf *conf, u32 val)
86 137
87static void altera_cvp_write_data_config(struct altera_cvp_conf *conf, u32 val) 138static void altera_cvp_write_data_config(struct altera_cvp_conf *conf, u32 val)
88{ 139{
89 pci_write_config_dword(conf->pci_dev, VSE_CVP_DATA, val); 140 pci_write_config_dword(conf->pci_dev, conf->vsec_offset + VSE_CVP_DATA,
141 val);
90} 142}
91 143
92/* switches between CvP clock and internal clock */ 144/* switches between CvP clock and internal clock */
@@ -96,10 +148,10 @@ static void altera_cvp_dummy_write(struct altera_cvp_conf *conf)
96 u32 val; 148 u32 val;
97 149
98 /* set 1 CVP clock cycle for every CVP Data Register Write */ 150 /* set 1 CVP clock cycle for every CVP Data Register Write */
99 pci_read_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, &val); 151 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
100 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK; 152 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
101 val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF; 153 val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
102 pci_write_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, val); 154 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
103 155
104 for (i = 0; i < CVP_DUMMY_WR; i++) 156 for (i = 0; i < CVP_DUMMY_WR; i++)
105 conf->write_data(conf, 0); /* dummy data, could be any value */ 157 conf->write_data(conf, 0); /* dummy data, could be any value */
@@ -116,7 +168,7 @@ static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
116 retries++; 168 retries++;
117 169
118 do { 170 do {
119 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val); 171 altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
120 if ((val & status_mask) == status_val) 172 if ((val & status_mask) == status_val)
121 return 0; 173 return 0;
122 174
@@ -127,32 +179,136 @@ static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
127 return -ETIMEDOUT; 179 return -ETIMEDOUT;
128} 180}
129 181
182static int altera_cvp_chk_error(struct fpga_manager *mgr, size_t bytes)
183{
184 struct altera_cvp_conf *conf = mgr->priv;
185 u32 val;
186 int ret;
187
188 /* STEP 10 (optional) - check CVP_CONFIG_ERROR flag */
189 ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
190 if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) {
191 dev_err(&mgr->dev, "CVP_CONFIG_ERROR after %zu bytes!\n",
192 bytes);
193 return -EPROTO;
194 }
195 return 0;
196}
197
198/*
199 * CvP Version2 Functions
200 * Recent Intel FPGAs use a credit mechanism to throttle incoming
201 * bitstreams and a different method of clearing the state.
202 */
203
204static int altera_cvp_v2_clear_state(struct altera_cvp_conf *conf)
205{
206 u32 val;
207 int ret;
208
209 /* Clear the START_XFER and CVP_CONFIG bits */
210 ret = altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
211 if (ret) {
212 dev_err(&conf->pci_dev->dev,
213 "Error reading CVP Program Control Register\n");
214 return ret;
215 }
216
217 val &= ~VSE_CVP_PROG_CTRL_MASK;
218 ret = altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
219 if (ret) {
220 dev_err(&conf->pci_dev->dev,
221 "Error writing CVP Program Control Register\n");
222 return ret;
223 }
224
225 return altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0,
226 conf->priv->poll_time_us);
227}
228
229static int altera_cvp_v2_wait_for_credit(struct fpga_manager *mgr,
230 u32 blocks)
231{
232 u32 timeout = V2_CREDIT_TIMEOUT_US / V2_CHECK_CREDIT_US;
233 struct altera_cvp_conf *conf = mgr->priv;
234 int ret;
235 u8 val;
236
237 do {
238 ret = altera_read_config_byte(conf, VSE_CVP_TX_CREDITS, &val);
239 if (ret) {
240 dev_err(&conf->pci_dev->dev,
241 "Error reading CVP Credit Register\n");
242 return ret;
243 }
244
245 /* Return if there is space in FIFO */
246 if (val - (u8)conf->sent_packets)
247 return 0;
248
249 ret = altera_cvp_chk_error(mgr, blocks * ALTERA_CVP_V2_SIZE);
250 if (ret) {
251 dev_err(&conf->pci_dev->dev,
252 "CE Bit error credit reg[0x%x]:sent[0x%x]\n",
253 val, conf->sent_packets);
254 return -EAGAIN;
255 }
256
257 /* Limit the check credit byte traffic */
258 usleep_range(V2_CHECK_CREDIT_US, V2_CHECK_CREDIT_US + 1);
259 } while (timeout--);
260
261 dev_err(&conf->pci_dev->dev, "Timeout waiting for credit\n");
262 return -ETIMEDOUT;
263}
264
265static int altera_cvp_send_block(struct altera_cvp_conf *conf,
266 const u32 *data, size_t len)
267{
268 u32 mask, words = len / sizeof(u32);
269 int i, remainder;
270
271 for (i = 0; i < words; i++)
272 conf->write_data(conf, *data++);
273
274 /* write up to 3 trailing bytes, if any */
275 remainder = len % sizeof(u32);
276 if (remainder) {
277 mask = BIT(remainder * 8) - 1;
278 if (mask)
279 conf->write_data(conf, *data & mask);
280 }
281
282 return 0;
283}
284
130static int altera_cvp_teardown(struct fpga_manager *mgr, 285static int altera_cvp_teardown(struct fpga_manager *mgr,
131 struct fpga_image_info *info) 286 struct fpga_image_info *info)
132{ 287{
133 struct altera_cvp_conf *conf = mgr->priv; 288 struct altera_cvp_conf *conf = mgr->priv;
134 struct pci_dev *pdev = conf->pci_dev;
135 int ret; 289 int ret;
136 u32 val; 290 u32 val;
137 291
138 /* STEP 12 - reset START_XFER bit */ 292 /* STEP 12 - reset START_XFER bit */
139 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 293 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
140 val &= ~VSE_CVP_PROG_CTRL_START_XFER; 294 val &= ~VSE_CVP_PROG_CTRL_START_XFER;
141 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 295 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
142 296
143 /* STEP 13 - reset CVP_CONFIG bit */ 297 /* STEP 13 - reset CVP_CONFIG bit */
144 val &= ~VSE_CVP_PROG_CTRL_CONFIG; 298 val &= ~VSE_CVP_PROG_CTRL_CONFIG;
145 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 299 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
146 300
147 /* 301 /*
148 * STEP 14 302 * STEP 14
149 * - set CVP_NUMCLKS to 1 and then issue CVP_DUMMY_WR dummy 303 * - set CVP_NUMCLKS to 1 and then issue CVP_DUMMY_WR dummy
150 * writes to the HIP 304 * writes to the HIP
151 */ 305 */
152 altera_cvp_dummy_write(conf); /* from CVP clock to internal clock */ 306 if (conf->priv->switch_clk)
307 conf->priv->switch_clk(conf);
153 308
154 /* STEP 15 - poll CVP_CONFIG_READY bit for 0 with 10us timeout */ 309 /* STEP 15 - poll CVP_CONFIG_READY bit for 0 with 10us timeout */
155 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0, 10); 310 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0,
311 conf->priv->poll_time_us);
156 if (ret) 312 if (ret)
157 dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n"); 313 dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n");
158 314
@@ -164,7 +320,6 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
164 const char *buf, size_t count) 320 const char *buf, size_t count)
165{ 321{
166 struct altera_cvp_conf *conf = mgr->priv; 322 struct altera_cvp_conf *conf = mgr->priv;
167 struct pci_dev *pdev = conf->pci_dev;
168 u32 iflags, val; 323 u32 iflags, val;
169 int ret; 324 int ret;
170 325
@@ -184,7 +339,7 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
184 conf->numclks = 1; /* for uncompressed and unencrypted images */ 339 conf->numclks = 1; /* for uncompressed and unencrypted images */
185 340
186 /* STEP 1 - read CVP status and check CVP_EN flag */ 341 /* STEP 1 - read CVP status and check CVP_EN flag */
187 pci_read_config_dword(pdev, VSE_CVP_STATUS, &val); 342 altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
188 if (!(val & VSE_CVP_STATUS_CVP_EN)) { 343 if (!(val & VSE_CVP_STATUS_CVP_EN)) {
189 dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val); 344 dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val);
190 return -ENODEV; 345 return -ENODEV;
@@ -202,30 +357,42 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
202 * - set HIP_CLK_SEL and CVP_MODE (must be set in the order mentioned) 357 * - set HIP_CLK_SEL and CVP_MODE (must be set in the order mentioned)
203 */ 358 */
204 /* switch from fabric to PMA clock */ 359 /* switch from fabric to PMA clock */
205 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 360 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
206 val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL; 361 val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
207 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 362 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
208 363
209 /* set CVP mode */ 364 /* set CVP mode */
210 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 365 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
211 val |= VSE_CVP_MODE_CTRL_CVP_MODE; 366 val |= VSE_CVP_MODE_CTRL_CVP_MODE;
212 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 367 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
213 368
214 /* 369 /*
215 * STEP 3 370 * STEP 3
216 * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP 371 * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
217 */ 372 */
218 altera_cvp_dummy_write(conf); 373 if (conf->priv->switch_clk)
374 conf->priv->switch_clk(conf);
375
376 if (conf->priv->clear_state) {
377 ret = conf->priv->clear_state(conf);
378 if (ret) {
379 dev_err(&mgr->dev, "Problem clearing out state\n");
380 return ret;
381 }
382 }
383
384 conf->sent_packets = 0;
219 385
220 /* STEP 4 - set CVP_CONFIG bit */ 386 /* STEP 4 - set CVP_CONFIG bit */
221 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 387 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
222 /* request control block to begin transfer using CVP */ 388 /* request control block to begin transfer using CVP */
223 val |= VSE_CVP_PROG_CTRL_CONFIG; 389 val |= VSE_CVP_PROG_CTRL_CONFIG;
224 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 390 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
225 391
226 /* STEP 5 - poll CVP_CONFIG READY for 1 with 10us timeout */ 392 /* STEP 5 - poll CVP_CONFIG READY for 1 with timeout */
227 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 393 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY,
228 VSE_CVP_STATUS_CFG_RDY, 10); 394 VSE_CVP_STATUS_CFG_RDY,
395 conf->priv->poll_time_us);
229 if (ret) { 396 if (ret) {
230 dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n"); 397 dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
231 return ret; 398 return ret;
@@ -235,33 +402,28 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
235 * STEP 6 402 * STEP 6
236 * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP 403 * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
237 */ 404 */
238 altera_cvp_dummy_write(conf); 405 if (conf->priv->switch_clk)
406 conf->priv->switch_clk(conf);
407
408 if (altera_cvp_chkcfg) {
409 ret = altera_cvp_chk_error(mgr, 0);
410 if (ret) {
411 dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
412 return ret;
413 }
414 }
239 415
240 /* STEP 7 - set START_XFER */ 416 /* STEP 7 - set START_XFER */
241 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 417 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
242 val |= VSE_CVP_PROG_CTRL_START_XFER; 418 val |= VSE_CVP_PROG_CTRL_START_XFER;
243 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 419 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
244 420
245 /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */ 421 /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */
246 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 422 if (conf->priv->switch_clk) {
247 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK; 423 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
248 val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF; 424 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
249 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 425 val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
250 426 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
251 return 0;
252}
253
254static inline int altera_cvp_chk_error(struct fpga_manager *mgr, size_t bytes)
255{
256 struct altera_cvp_conf *conf = mgr->priv;
257 u32 val;
258
259 /* STEP 10 (optional) - check CVP_CONFIG_ERROR flag */
260 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val);
261 if (val & VSE_CVP_STATUS_CFG_ERR) {
262 dev_err(&mgr->dev, "CVP_CONFIG_ERROR after %zu bytes!\n",
263 bytes);
264 return -EPROTO;
265 } 427 }
266 return 0; 428 return 0;
267} 429}
@@ -270,20 +432,32 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
270 size_t count) 432 size_t count)
271{ 433{
272 struct altera_cvp_conf *conf = mgr->priv; 434 struct altera_cvp_conf *conf = mgr->priv;
435 size_t done, remaining, len;
273 const u32 *data; 436 const u32 *data;
274 size_t done, remaining;
275 int status = 0; 437 int status = 0;
276 u32 mask;
277 438
278 /* STEP 9 - write 32-bit data from RBF file to CVP data register */ 439 /* STEP 9 - write 32-bit data from RBF file to CVP data register */
279 data = (u32 *)buf; 440 data = (u32 *)buf;
280 remaining = count; 441 remaining = count;
281 done = 0; 442 done = 0;
282 443
283 while (remaining >= 4) { 444 while (remaining) {
284 conf->write_data(conf, *data++); 445 /* Use credit throttling if available */
285 done += 4; 446 if (conf->priv->wait_credit) {
286 remaining -= 4; 447 status = conf->priv->wait_credit(mgr, done);
448 if (status) {
449 dev_err(&conf->pci_dev->dev,
450 "Wait Credit ERR: 0x%x\n", status);
451 return status;
452 }
453 }
454
455 len = min(conf->priv->block_size, remaining);
456 altera_cvp_send_block(conf, data, len);
457 data += len / sizeof(u32);
458 done += len;
459 remaining -= len;
460 conf->sent_packets++;
287 461
288 /* 462 /*
289 * STEP 10 (optional) and STEP 11 463 * STEP 10 (optional) and STEP 11
@@ -301,11 +475,6 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
301 } 475 }
302 } 476 }
303 477
304 /* write up to 3 trailing bytes, if any */
305 mask = BIT(remaining * 8) - 1;
306 if (mask)
307 conf->write_data(conf, *data & mask);
308
309 if (altera_cvp_chkcfg) 478 if (altera_cvp_chkcfg)
310 status = altera_cvp_chk_error(mgr, count); 479 status = altera_cvp_chk_error(mgr, count);
311 480
@@ -316,31 +485,30 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr,
316 struct fpga_image_info *info) 485 struct fpga_image_info *info)
317{ 486{
318 struct altera_cvp_conf *conf = mgr->priv; 487 struct altera_cvp_conf *conf = mgr->priv;
319 struct pci_dev *pdev = conf->pci_dev; 488 u32 mask, val;
320 int ret; 489 int ret;
321 u32 mask;
322 u32 val;
323 490
324 ret = altera_cvp_teardown(mgr, info); 491 ret = altera_cvp_teardown(mgr, info);
325 if (ret) 492 if (ret)
326 return ret; 493 return ret;
327 494
328 /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */ 495 /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */
329 pci_read_config_dword(pdev, VSE_UNCOR_ERR_STATUS, &val); 496 altera_read_config_dword(conf, VSE_UNCOR_ERR_STATUS, &val);
330 if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) { 497 if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) {
331 dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n"); 498 dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n");
332 return -EPROTO; 499 return -EPROTO;
333 } 500 }
334 501
335 /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */ 502 /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */
336 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 503 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
337 val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL; 504 val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
338 val &= ~VSE_CVP_MODE_CTRL_CVP_MODE; 505 val &= ~VSE_CVP_MODE_CTRL_CVP_MODE;
339 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 506 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
340 507
341 /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */ 508 /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */
342 mask = VSE_CVP_STATUS_PLD_CLK_IN_USE | VSE_CVP_STATUS_USERMODE; 509 mask = VSE_CVP_STATUS_PLD_CLK_IN_USE | VSE_CVP_STATUS_USERMODE;
343 ret = altera_cvp_wait_status(conf, mask, mask, TIMEOUT_US); 510 ret = altera_cvp_wait_status(conf, mask, mask,
511 conf->priv->user_time_us);
344 if (ret) 512 if (ret)
345 dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n"); 513 dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n");
346 514
@@ -354,6 +522,21 @@ static const struct fpga_manager_ops altera_cvp_ops = {
354 .write_complete = altera_cvp_write_complete, 522 .write_complete = altera_cvp_write_complete,
355}; 523};
356 524
525static const struct cvp_priv cvp_priv_v1 = {
526 .switch_clk = altera_cvp_dummy_write,
527 .block_size = ALTERA_CVP_V1_SIZE,
528 .poll_time_us = V1_POLL_TIMEOUT_US,
529 .user_time_us = TIMEOUT_US,
530};
531
532static const struct cvp_priv cvp_priv_v2 = {
533 .clear_state = altera_cvp_v2_clear_state,
534 .wait_credit = altera_cvp_v2_wait_for_credit,
535 .block_size = ALTERA_CVP_V2_SIZE,
536 .poll_time_us = V2_POLL_TIMEOUT_US,
537 .user_time_us = V2_USER_TIMEOUT_US,
538};
539
357static ssize_t chkcfg_show(struct device_driver *dev, char *buf) 540static ssize_t chkcfg_show(struct device_driver *dev, char *buf)
358{ 541{
359 return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg); 542 return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg);
@@ -395,22 +578,29 @@ static int altera_cvp_probe(struct pci_dev *pdev,
395{ 578{
396 struct altera_cvp_conf *conf; 579 struct altera_cvp_conf *conf;
397 struct fpga_manager *mgr; 580 struct fpga_manager *mgr;
581 int ret, offset;
398 u16 cmd, val; 582 u16 cmd, val;
399 u32 regval; 583 u32 regval;
400 int ret; 584
585 /* Discover the Vendor Specific Offset for this device */
586 offset = pci_find_next_ext_capability(pdev, 0, PCI_EXT_CAP_ID_VNDR);
587 if (!offset) {
588 dev_err(&pdev->dev, "No Vendor Specific Offset.\n");
589 return -ENODEV;
590 }
401 591
402 /* 592 /*
403 * First check if this is the expected FPGA device. PCI config 593 * First check if this is the expected FPGA device. PCI config
404 * space access works without enabling the PCI device, memory 594 * space access works without enabling the PCI device, memory
405 * space access is enabled further down. 595 * space access is enabled further down.
406 */ 596 */
407 pci_read_config_word(pdev, VSE_PCIE_EXT_CAP_ID, &val); 597 pci_read_config_word(pdev, offset + VSE_PCIE_EXT_CAP_ID, &val);
408 if (val != VSE_PCIE_EXT_CAP_ID_VAL) { 598 if (val != VSE_PCIE_EXT_CAP_ID_VAL) {
409 dev_err(&pdev->dev, "Wrong EXT_CAP_ID value 0x%x\n", val); 599 dev_err(&pdev->dev, "Wrong EXT_CAP_ID value 0x%x\n", val);
410 return -ENODEV; 600 return -ENODEV;
411 } 601 }
412 602
413 pci_read_config_dword(pdev, VSE_CVP_STATUS, &regval); 603 pci_read_config_dword(pdev, offset + VSE_CVP_STATUS, &regval);
414 if (!(regval & VSE_CVP_STATUS_CVP_EN)) { 604 if (!(regval & VSE_CVP_STATUS_CVP_EN)) {
415 dev_err(&pdev->dev, 605 dev_err(&pdev->dev,
416 "CVP is disabled for this device: CVP_STATUS Reg 0x%x\n", 606 "CVP is disabled for this device: CVP_STATUS Reg 0x%x\n",
@@ -422,6 +612,8 @@ static int altera_cvp_probe(struct pci_dev *pdev,
422 if (!conf) 612 if (!conf)
423 return -ENOMEM; 613 return -ENOMEM;
424 614
615 conf->vsec_offset = offset;
616
425 /* 617 /*
426 * Enable memory BAR access. We cannot use pci_enable_device() here 618 * Enable memory BAR access. We cannot use pci_enable_device() here
427 * because it will make the driver unusable with FPGA devices that 619 * because it will make the driver unusable with FPGA devices that
@@ -446,6 +638,11 @@ static int altera_cvp_probe(struct pci_dev *pdev,
446 conf->pci_dev = pdev; 638 conf->pci_dev = pdev;
447 conf->write_data = altera_cvp_write_data_iomem; 639 conf->write_data = altera_cvp_write_data_iomem;
448 640
641 if (conf->vsec_offset == V1_VSEC_OFFSET)
642 conf->priv = &cvp_priv_v1;
643 else
644 conf->priv = &cvp_priv_v2;
645
449 conf->map = pci_iomap(pdev, CVP_BAR, 0); 646 conf->map = pci_iomap(pdev, CVP_BAR, 0);
450 if (!conf->map) { 647 if (!conf->map) {
451 dev_warn(&pdev->dev, "Mapping CVP BAR failed\n"); 648 dev_warn(&pdev->dev, "Mapping CVP BAR failed\n");