summaryrefslogtreecommitdiffstats
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.c342
1 files changed, 270 insertions, 72 deletions
diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
index 770915fb97f9..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,41 +33,93 @@
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;
59 void __iomem *map; 77 void __iomem *map;
60 void (*write_data)(struct altera_cvp_conf *, u32); 78 void (*write_data)(struct altera_cvp_conf *conf,
79 u32 data);
61 char mgr_name[64]; 80 char mgr_name[64];
62 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;
63}; 94};
64 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
65static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr) 117static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr)
66{ 118{
67 struct altera_cvp_conf *conf = mgr->priv; 119 struct altera_cvp_conf *conf = mgr->priv;
68 u32 status; 120 u32 status;
69 121
70 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &status); 122 altera_read_config_dword(conf, VSE_CVP_STATUS, &status);
71 123
72 if (status & VSE_CVP_STATUS_CFG_DONE) 124 if (status & VSE_CVP_STATUS_CFG_DONE)
73 return FPGA_MGR_STATE_OPERATING; 125 return FPGA_MGR_STATE_OPERATING;
@@ -85,7 +137,8 @@ static void altera_cvp_write_data_iomem(struct altera_cvp_conf *conf, u32 val)
85 137
86static 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)
87{ 139{
88 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);
89} 142}
90 143
91/* switches between CvP clock and internal clock */ 144/* switches between CvP clock and internal clock */
@@ -95,10 +148,10 @@ static void altera_cvp_dummy_write(struct altera_cvp_conf *conf)
95 u32 val; 148 u32 val;
96 149
97 /* set 1 CVP clock cycle for every CVP Data Register Write */ 150 /* set 1 CVP clock cycle for every CVP Data Register Write */
98 pci_read_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, &val); 151 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
99 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK; 152 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
100 val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF; 153 val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
101 pci_write_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, val); 154 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
102 155
103 for (i = 0; i < CVP_DUMMY_WR; i++) 156 for (i = 0; i < CVP_DUMMY_WR; i++)
104 conf->write_data(conf, 0); /* dummy data, could be any value */ 157 conf->write_data(conf, 0); /* dummy data, could be any value */
@@ -115,7 +168,7 @@ static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
115 retries++; 168 retries++;
116 169
117 do { 170 do {
118 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val); 171 altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
119 if ((val & status_mask) == status_val) 172 if ((val & status_mask) == status_val)
120 return 0; 173 return 0;
121 174
@@ -126,32 +179,136 @@ static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
126 return -ETIMEDOUT; 179 return -ETIMEDOUT;
127} 180}
128 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
129static int altera_cvp_teardown(struct fpga_manager *mgr, 285static int altera_cvp_teardown(struct fpga_manager *mgr,
130 struct fpga_image_info *info) 286 struct fpga_image_info *info)
131{ 287{
132 struct altera_cvp_conf *conf = mgr->priv; 288 struct altera_cvp_conf *conf = mgr->priv;
133 struct pci_dev *pdev = conf->pci_dev;
134 int ret; 289 int ret;
135 u32 val; 290 u32 val;
136 291
137 /* STEP 12 - reset START_XFER bit */ 292 /* STEP 12 - reset START_XFER bit */
138 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 293 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
139 val &= ~VSE_CVP_PROG_CTRL_START_XFER; 294 val &= ~VSE_CVP_PROG_CTRL_START_XFER;
140 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 295 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
141 296
142 /* STEP 13 - reset CVP_CONFIG bit */ 297 /* STEP 13 - reset CVP_CONFIG bit */
143 val &= ~VSE_CVP_PROG_CTRL_CONFIG; 298 val &= ~VSE_CVP_PROG_CTRL_CONFIG;
144 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 299 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
145 300
146 /* 301 /*
147 * STEP 14 302 * STEP 14
148 * - 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
149 * writes to the HIP 304 * writes to the HIP
150 */ 305 */
151 altera_cvp_dummy_write(conf); /* from CVP clock to internal clock */ 306 if (conf->priv->switch_clk)
307 conf->priv->switch_clk(conf);
152 308
153 /* 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 */
154 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);
155 if (ret) 312 if (ret)
156 dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n"); 313 dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n");
157 314
@@ -163,7 +320,6 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
163 const char *buf, size_t count) 320 const char *buf, size_t count)
164{ 321{
165 struct altera_cvp_conf *conf = mgr->priv; 322 struct altera_cvp_conf *conf = mgr->priv;
166 struct pci_dev *pdev = conf->pci_dev;
167 u32 iflags, val; 323 u32 iflags, val;
168 int ret; 324 int ret;
169 325
@@ -183,7 +339,7 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
183 conf->numclks = 1; /* for uncompressed and unencrypted images */ 339 conf->numclks = 1; /* for uncompressed and unencrypted images */
184 340
185 /* STEP 1 - read CVP status and check CVP_EN flag */ 341 /* STEP 1 - read CVP status and check CVP_EN flag */
186 pci_read_config_dword(pdev, VSE_CVP_STATUS, &val); 342 altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
187 if (!(val & VSE_CVP_STATUS_CVP_EN)) { 343 if (!(val & VSE_CVP_STATUS_CVP_EN)) {
188 dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val); 344 dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val);
189 return -ENODEV; 345 return -ENODEV;
@@ -201,30 +357,42 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
201 * - 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)
202 */ 358 */
203 /* switch from fabric to PMA clock */ 359 /* switch from fabric to PMA clock */
204 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 360 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
205 val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL; 361 val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
206 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 362 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
207 363
208 /* set CVP mode */ 364 /* set CVP mode */
209 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 365 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
210 val |= VSE_CVP_MODE_CTRL_CVP_MODE; 366 val |= VSE_CVP_MODE_CTRL_CVP_MODE;
211 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 367 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
212 368
213 /* 369 /*
214 * STEP 3 370 * STEP 3
215 * - 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
216 */ 372 */
217 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;
218 385
219 /* STEP 4 - set CVP_CONFIG bit */ 386 /* STEP 4 - set CVP_CONFIG bit */
220 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 387 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
221 /* request control block to begin transfer using CVP */ 388 /* request control block to begin transfer using CVP */
222 val |= VSE_CVP_PROG_CTRL_CONFIG; 389 val |= VSE_CVP_PROG_CTRL_CONFIG;
223 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 390 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
224 391
225 /* STEP 5 - poll CVP_CONFIG READY for 1 with 10us timeout */ 392 /* STEP 5 - poll CVP_CONFIG READY for 1 with timeout */
226 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 393 ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY,
227 VSE_CVP_STATUS_CFG_RDY, 10); 394 VSE_CVP_STATUS_CFG_RDY,
395 conf->priv->poll_time_us);
228 if (ret) { 396 if (ret) {
229 dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n"); 397 dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
230 return ret; 398 return ret;
@@ -234,33 +402,28 @@ static int altera_cvp_write_init(struct fpga_manager *mgr,
234 * STEP 6 402 * STEP 6
235 * - 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
236 */ 404 */
237 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 }
238 415
239 /* STEP 7 - set START_XFER */ 416 /* STEP 7 - set START_XFER */
240 pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val); 417 altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
241 val |= VSE_CVP_PROG_CTRL_START_XFER; 418 val |= VSE_CVP_PROG_CTRL_START_XFER;
242 pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val); 419 altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
243 420
244 /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */ 421 /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */
245 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 422 if (conf->priv->switch_clk) {
246 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK; 423 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
247 val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF; 424 val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
248 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 425 val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
249 426 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
250 return 0;
251}
252
253static inline int altera_cvp_chk_error(struct fpga_manager *mgr, size_t bytes)
254{
255 struct altera_cvp_conf *conf = mgr->priv;
256 u32 val;
257
258 /* STEP 10 (optional) - check CVP_CONFIG_ERROR flag */
259 pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val);
260 if (val & VSE_CVP_STATUS_CFG_ERR) {
261 dev_err(&mgr->dev, "CVP_CONFIG_ERROR after %zu bytes!\n",
262 bytes);
263 return -EPROTO;
264 } 427 }
265 return 0; 428 return 0;
266} 429}
@@ -269,20 +432,32 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
269 size_t count) 432 size_t count)
270{ 433{
271 struct altera_cvp_conf *conf = mgr->priv; 434 struct altera_cvp_conf *conf = mgr->priv;
435 size_t done, remaining, len;
272 const u32 *data; 436 const u32 *data;
273 size_t done, remaining;
274 int status = 0; 437 int status = 0;
275 u32 mask;
276 438
277 /* 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 */
278 data = (u32 *)buf; 440 data = (u32 *)buf;
279 remaining = count; 441 remaining = count;
280 done = 0; 442 done = 0;
281 443
282 while (remaining >= 4) { 444 while (remaining) {
283 conf->write_data(conf, *data++); 445 /* Use credit throttling if available */
284 done += 4; 446 if (conf->priv->wait_credit) {
285 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++;
286 461
287 /* 462 /*
288 * STEP 10 (optional) and STEP 11 463 * STEP 10 (optional) and STEP 11
@@ -300,11 +475,6 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
300 } 475 }
301 } 476 }
302 477
303 /* write up to 3 trailing bytes, if any */
304 mask = BIT(remaining * 8) - 1;
305 if (mask)
306 conf->write_data(conf, *data & mask);
307
308 if (altera_cvp_chkcfg) 478 if (altera_cvp_chkcfg)
309 status = altera_cvp_chk_error(mgr, count); 479 status = altera_cvp_chk_error(mgr, count);
310 480
@@ -315,31 +485,30 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr,
315 struct fpga_image_info *info) 485 struct fpga_image_info *info)
316{ 486{
317 struct altera_cvp_conf *conf = mgr->priv; 487 struct altera_cvp_conf *conf = mgr->priv;
318 struct pci_dev *pdev = conf->pci_dev; 488 u32 mask, val;
319 int ret; 489 int ret;
320 u32 mask;
321 u32 val;
322 490
323 ret = altera_cvp_teardown(mgr, info); 491 ret = altera_cvp_teardown(mgr, info);
324 if (ret) 492 if (ret)
325 return ret; 493 return ret;
326 494
327 /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */ 495 /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */
328 pci_read_config_dword(pdev, VSE_UNCOR_ERR_STATUS, &val); 496 altera_read_config_dword(conf, VSE_UNCOR_ERR_STATUS, &val);
329 if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) { 497 if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) {
330 dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n"); 498 dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n");
331 return -EPROTO; 499 return -EPROTO;
332 } 500 }
333 501
334 /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */ 502 /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */
335 pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val); 503 altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
336 val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL; 504 val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
337 val &= ~VSE_CVP_MODE_CTRL_CVP_MODE; 505 val &= ~VSE_CVP_MODE_CTRL_CVP_MODE;
338 pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val); 506 altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
339 507
340 /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */ 508 /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */
341 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;
342 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);
343 if (ret) 512 if (ret)
344 dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n"); 513 dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n");
345 514
@@ -353,6 +522,21 @@ static const struct fpga_manager_ops altera_cvp_ops = {
353 .write_complete = altera_cvp_write_complete, 522 .write_complete = altera_cvp_write_complete,
354}; 523};
355 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
356static ssize_t chkcfg_show(struct device_driver *dev, char *buf) 540static ssize_t chkcfg_show(struct device_driver *dev, char *buf)
357{ 541{
358 return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg); 542 return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg);
@@ -394,22 +578,29 @@ static int altera_cvp_probe(struct pci_dev *pdev,
394{ 578{
395 struct altera_cvp_conf *conf; 579 struct altera_cvp_conf *conf;
396 struct fpga_manager *mgr; 580 struct fpga_manager *mgr;
581 int ret, offset;
397 u16 cmd, val; 582 u16 cmd, val;
398 u32 regval; 583 u32 regval;
399 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 }
400 591
401 /* 592 /*
402 * First check if this is the expected FPGA device. PCI config 593 * First check if this is the expected FPGA device. PCI config
403 * space access works without enabling the PCI device, memory 594 * space access works without enabling the PCI device, memory
404 * space access is enabled further down. 595 * space access is enabled further down.
405 */ 596 */
406 pci_read_config_word(pdev, VSE_PCIE_EXT_CAP_ID, &val); 597 pci_read_config_word(pdev, offset + VSE_PCIE_EXT_CAP_ID, &val);
407 if (val != VSE_PCIE_EXT_CAP_ID_VAL) { 598 if (val != VSE_PCIE_EXT_CAP_ID_VAL) {
408 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);
409 return -ENODEV; 600 return -ENODEV;
410 } 601 }
411 602
412 pci_read_config_dword(pdev, VSE_CVP_STATUS, &regval); 603 pci_read_config_dword(pdev, offset + VSE_CVP_STATUS, &regval);
413 if (!(regval & VSE_CVP_STATUS_CVP_EN)) { 604 if (!(regval & VSE_CVP_STATUS_CVP_EN)) {
414 dev_err(&pdev->dev, 605 dev_err(&pdev->dev,
415 "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",
@@ -421,6 +612,8 @@ static int altera_cvp_probe(struct pci_dev *pdev,
421 if (!conf) 612 if (!conf)
422 return -ENOMEM; 613 return -ENOMEM;
423 614
615 conf->vsec_offset = offset;
616
424 /* 617 /*
425 * Enable memory BAR access. We cannot use pci_enable_device() here 618 * Enable memory BAR access. We cannot use pci_enable_device() here
426 * because it will make the driver unusable with FPGA devices that 619 * because it will make the driver unusable with FPGA devices that
@@ -445,6 +638,11 @@ static int altera_cvp_probe(struct pci_dev *pdev,
445 conf->pci_dev = pdev; 638 conf->pci_dev = pdev;
446 conf->write_data = altera_cvp_write_data_iomem; 639 conf->write_data = altera_cvp_write_data_iomem;
447 640
641 if (conf->vsec_offset == V1_VSEC_OFFSET)
642 conf->priv = &cvp_priv_v1;
643 else
644 conf->priv = &cvp_priv_v2;
645
448 conf->map = pci_iomap(pdev, CVP_BAR, 0); 646 conf->map = pci_iomap(pdev, CVP_BAR, 0);
449 if (!conf->map) { 647 if (!conf->map) {
450 dev_warn(&pdev->dev, "Mapping CVP BAR failed\n"); 648 dev_warn(&pdev->dev, "Mapping CVP BAR failed\n");