altera-cvp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FPGA Manager Driver for Altera Arria/Cyclone/Stratix CvP
  4. *
  5. * Copyright (C) 2017 DENX Software Engineering
  6. *
  7. * Anatolij Gustschin <agust@denx.de>
  8. *
  9. * Manage Altera FPGA firmware using PCIe CvP.
  10. * Firmware must be in binary "rbf" format.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/fpga/fpga-mgr.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/sizes.h>
  18. #define CVP_BAR 0 /* BAR used for data transfer in memory mode */
  19. #define CVP_DUMMY_WR 244 /* dummy writes to clear CvP state machine */
  20. #define TIMEOUT_US 2000 /* CVP STATUS timeout for USERMODE polling */
  21. /* Vendor Specific Extended Capability Registers */
  22. #define VSE_CVP_STATUS 0x1c /* 32bit */
  23. #define VSE_CVP_STATUS_CFG_RDY BIT(18) /* CVP_CONFIG_READY */
  24. #define VSE_CVP_STATUS_CFG_ERR BIT(19) /* CVP_CONFIG_ERROR */
  25. #define VSE_CVP_STATUS_CVP_EN BIT(20) /* ctrl block is enabling CVP */
  26. #define VSE_CVP_STATUS_USERMODE BIT(21) /* USERMODE */
  27. #define VSE_CVP_STATUS_CFG_DONE BIT(23) /* CVP_CONFIG_DONE */
  28. #define VSE_CVP_STATUS_PLD_CLK_IN_USE BIT(24) /* PLD_CLK_IN_USE */
  29. #define VSE_CVP_MODE_CTRL 0x20 /* 32bit */
  30. #define VSE_CVP_MODE_CTRL_CVP_MODE BIT(0) /* CVP (1) or normal mode (0) */
  31. #define VSE_CVP_MODE_CTRL_HIP_CLK_SEL BIT(1) /* PMA (1) or fabric clock (0) */
  32. #define VSE_CVP_MODE_CTRL_NUMCLKS_OFF 8 /* NUMCLKS bits offset */
  33. #define VSE_CVP_MODE_CTRL_NUMCLKS_MASK GENMASK(15, 8)
  34. #define VSE_CVP_DATA 0x28 /* 32bit */
  35. #define VSE_CVP_PROG_CTRL 0x2c /* 32bit */
  36. #define VSE_CVP_PROG_CTRL_CONFIG BIT(0)
  37. #define VSE_CVP_PROG_CTRL_START_XFER BIT(1)
  38. #define VSE_CVP_PROG_CTRL_MASK GENMASK(1, 0)
  39. #define VSE_UNCOR_ERR_STATUS 0x34 /* 32bit */
  40. #define VSE_UNCOR_ERR_CVP_CFG_ERR BIT(5) /* CVP_CONFIG_ERROR_LATCHED */
  41. #define V1_VSEC_OFFSET 0x200 /* Vendor Specific Offset V1 */
  42. /* V2 Defines */
  43. #define VSE_CVP_TX_CREDITS 0x49 /* 8bit */
  44. #define V2_CREDIT_TIMEOUT_US 40000
  45. #define V2_CHECK_CREDIT_US 10
  46. #define V2_POLL_TIMEOUT_US 1000000
  47. #define V2_USER_TIMEOUT_US 500000
  48. #define V1_POLL_TIMEOUT_US 10
  49. #define DRV_NAME "altera-cvp"
  50. #define ALTERA_CVP_MGR_NAME "Altera CvP FPGA Manager"
  51. /* Write block sizes */
  52. #define ALTERA_CVP_V1_SIZE 4
  53. #define ALTERA_CVP_V2_SIZE 4096
  54. /* Optional CvP config error status check for debugging */
  55. static bool altera_cvp_chkcfg;
  56. struct cvp_priv;
  57. struct altera_cvp_conf {
  58. struct pci_dev *pci_dev;
  59. void __iomem *map;
  60. void (*write_data)(struct altera_cvp_conf *conf,
  61. u32 data);
  62. char mgr_name[64];
  63. u8 numclks;
  64. u32 sent_packets;
  65. u32 vsec_offset;
  66. const struct cvp_priv *priv;
  67. };
  68. struct cvp_priv {
  69. void (*switch_clk)(struct altera_cvp_conf *conf);
  70. int (*clear_state)(struct altera_cvp_conf *conf);
  71. int (*wait_credit)(struct fpga_manager *mgr, u32 blocks);
  72. size_t block_size;
  73. int poll_time_us;
  74. int user_time_us;
  75. };
  76. static int altera_read_config_byte(struct altera_cvp_conf *conf,
  77. int where, u8 *val)
  78. {
  79. return pci_read_config_byte(conf->pci_dev, conf->vsec_offset + where,
  80. val);
  81. }
  82. static int altera_read_config_dword(struct altera_cvp_conf *conf,
  83. int where, u32 *val)
  84. {
  85. return pci_read_config_dword(conf->pci_dev, conf->vsec_offset + where,
  86. val);
  87. }
  88. static int altera_write_config_dword(struct altera_cvp_conf *conf,
  89. int where, u32 val)
  90. {
  91. return pci_write_config_dword(conf->pci_dev, conf->vsec_offset + where,
  92. val);
  93. }
  94. static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr)
  95. {
  96. struct altera_cvp_conf *conf = mgr->priv;
  97. u32 status;
  98. altera_read_config_dword(conf, VSE_CVP_STATUS, &status);
  99. if (status & VSE_CVP_STATUS_CFG_DONE)
  100. return FPGA_MGR_STATE_OPERATING;
  101. if (status & VSE_CVP_STATUS_CVP_EN)
  102. return FPGA_MGR_STATE_POWER_UP;
  103. return FPGA_MGR_STATE_UNKNOWN;
  104. }
  105. static void altera_cvp_write_data_iomem(struct altera_cvp_conf *conf, u32 val)
  106. {
  107. writel(val, conf->map);
  108. }
  109. static void altera_cvp_write_data_config(struct altera_cvp_conf *conf, u32 val)
  110. {
  111. pci_write_config_dword(conf->pci_dev, conf->vsec_offset + VSE_CVP_DATA,
  112. val);
  113. }
  114. /* switches between CvP clock and internal clock */
  115. static void altera_cvp_dummy_write(struct altera_cvp_conf *conf)
  116. {
  117. unsigned int i;
  118. u32 val;
  119. /* set 1 CVP clock cycle for every CVP Data Register Write */
  120. altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
  121. val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
  122. val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
  123. altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
  124. for (i = 0; i < CVP_DUMMY_WR; i++)
  125. conf->write_data(conf, 0); /* dummy data, could be any value */
  126. }
  127. static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
  128. u32 status_val, int timeout_us)
  129. {
  130. unsigned int retries;
  131. u32 val;
  132. retries = timeout_us / 10;
  133. if (timeout_us % 10)
  134. retries++;
  135. do {
  136. altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
  137. if ((val & status_mask) == status_val)
  138. return 0;
  139. /* use small usleep value to re-check and break early */
  140. usleep_range(10, 11);
  141. } while (--retries);
  142. return -ETIMEDOUT;
  143. }
  144. static int altera_cvp_chk_error(struct fpga_manager *mgr, size_t bytes)
  145. {
  146. struct altera_cvp_conf *conf = mgr->priv;
  147. u32 val;
  148. int ret;
  149. /* STEP 10 (optional) - check CVP_CONFIG_ERROR flag */
  150. ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
  151. if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) {
  152. dev_err(&mgr->dev, "CVP_CONFIG_ERROR after %zu bytes!\n",
  153. bytes);
  154. return -EPROTO;
  155. }
  156. return 0;
  157. }
  158. /*
  159. * CvP Version2 Functions
  160. * Recent Intel FPGAs use a credit mechanism to throttle incoming
  161. * bitstreams and a different method of clearing the state.
  162. */
  163. static int altera_cvp_v2_clear_state(struct altera_cvp_conf *conf)
  164. {
  165. u32 val;
  166. int ret;
  167. /* Clear the START_XFER and CVP_CONFIG bits */
  168. ret = altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
  169. if (ret) {
  170. dev_err(&conf->pci_dev->dev,
  171. "Error reading CVP Program Control Register\n");
  172. return ret;
  173. }
  174. val &= ~VSE_CVP_PROG_CTRL_MASK;
  175. ret = altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
  176. if (ret) {
  177. dev_err(&conf->pci_dev->dev,
  178. "Error writing CVP Program Control Register\n");
  179. return ret;
  180. }
  181. return altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0,
  182. conf->priv->poll_time_us);
  183. }
  184. static int altera_cvp_v2_wait_for_credit(struct fpga_manager *mgr,
  185. u32 blocks)
  186. {
  187. u32 timeout = V2_CREDIT_TIMEOUT_US / V2_CHECK_CREDIT_US;
  188. struct altera_cvp_conf *conf = mgr->priv;
  189. int ret;
  190. u8 val;
  191. do {
  192. ret = altera_read_config_byte(conf, VSE_CVP_TX_CREDITS, &val);
  193. if (ret) {
  194. dev_err(&conf->pci_dev->dev,
  195. "Error reading CVP Credit Register\n");
  196. return ret;
  197. }
  198. /* Return if there is space in FIFO */
  199. if (val - (u8)conf->sent_packets)
  200. return 0;
  201. ret = altera_cvp_chk_error(mgr, blocks * ALTERA_CVP_V2_SIZE);
  202. if (ret) {
  203. dev_err(&conf->pci_dev->dev,
  204. "CE Bit error credit reg[0x%x]:sent[0x%x]\n",
  205. val, conf->sent_packets);
  206. return -EAGAIN;
  207. }
  208. /* Limit the check credit byte traffic */
  209. usleep_range(V2_CHECK_CREDIT_US, V2_CHECK_CREDIT_US + 1);
  210. } while (timeout--);
  211. dev_err(&conf->pci_dev->dev, "Timeout waiting for credit\n");
  212. return -ETIMEDOUT;
  213. }
  214. static int altera_cvp_send_block(struct altera_cvp_conf *conf,
  215. const u32 *data, size_t len)
  216. {
  217. u32 mask, words = len / sizeof(u32);
  218. int i, remainder;
  219. for (i = 0; i < words; i++)
  220. conf->write_data(conf, *data++);
  221. /* write up to 3 trailing bytes, if any */
  222. remainder = len % sizeof(u32);
  223. if (remainder) {
  224. mask = BIT(remainder * 8) - 1;
  225. if (mask)
  226. conf->write_data(conf, *data & mask);
  227. }
  228. return 0;
  229. }
  230. static int altera_cvp_teardown(struct fpga_manager *mgr,
  231. struct fpga_image_info *info)
  232. {
  233. struct altera_cvp_conf *conf = mgr->priv;
  234. int ret;
  235. u32 val;
  236. /* STEP 12 - reset START_XFER bit */
  237. altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
  238. val &= ~VSE_CVP_PROG_CTRL_START_XFER;
  239. altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
  240. /* STEP 13 - reset CVP_CONFIG bit */
  241. val &= ~VSE_CVP_PROG_CTRL_CONFIG;
  242. altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
  243. /*
  244. * STEP 14
  245. * - set CVP_NUMCLKS to 1 and then issue CVP_DUMMY_WR dummy
  246. * writes to the HIP
  247. */
  248. if (conf->priv->switch_clk)
  249. conf->priv->switch_clk(conf);
  250. /* STEP 15 - poll CVP_CONFIG_READY bit for 0 with 10us timeout */
  251. ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0,
  252. conf->priv->poll_time_us);
  253. if (ret)
  254. dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n");
  255. return ret;
  256. }
  257. static int altera_cvp_write_init(struct fpga_manager *mgr,
  258. struct fpga_image_info *info,
  259. const char *buf, size_t count)
  260. {
  261. struct altera_cvp_conf *conf = mgr->priv;
  262. u32 iflags, val;
  263. int ret;
  264. iflags = info ? info->flags : 0;
  265. if (iflags & FPGA_MGR_PARTIAL_RECONFIG) {
  266. dev_err(&mgr->dev, "Partial reconfiguration not supported.\n");
  267. return -EINVAL;
  268. }
  269. /* Determine allowed clock to data ratio */
  270. if (iflags & FPGA_MGR_COMPRESSED_BITSTREAM)
  271. conf->numclks = 8; /* ratio for all compressed images */
  272. else if (iflags & FPGA_MGR_ENCRYPTED_BITSTREAM)
  273. conf->numclks = 4; /* for uncompressed and encrypted images */
  274. else
  275. conf->numclks = 1; /* for uncompressed and unencrypted images */
  276. /* STEP 1 - read CVP status and check CVP_EN flag */
  277. altera_read_config_dword(conf, VSE_CVP_STATUS, &val);
  278. if (!(val & VSE_CVP_STATUS_CVP_EN)) {
  279. dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val);
  280. return -ENODEV;
  281. }
  282. if (val & VSE_CVP_STATUS_CFG_RDY) {
  283. dev_warn(&mgr->dev, "CvP already started, tear down first\n");
  284. ret = altera_cvp_teardown(mgr, info);
  285. if (ret)
  286. return ret;
  287. }
  288. /*
  289. * STEP 2
  290. * - set HIP_CLK_SEL and CVP_MODE (must be set in the order mentioned)
  291. */
  292. /* switch from fabric to PMA clock */
  293. altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
  294. val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
  295. altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
  296. /* set CVP mode */
  297. altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
  298. val |= VSE_CVP_MODE_CTRL_CVP_MODE;
  299. altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
  300. /*
  301. * STEP 3
  302. * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
  303. */
  304. if (conf->priv->switch_clk)
  305. conf->priv->switch_clk(conf);
  306. if (conf->priv->clear_state) {
  307. ret = conf->priv->clear_state(conf);
  308. if (ret) {
  309. dev_err(&mgr->dev, "Problem clearing out state\n");
  310. return ret;
  311. }
  312. }
  313. conf->sent_packets = 0;
  314. /* STEP 4 - set CVP_CONFIG bit */
  315. altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
  316. /* request control block to begin transfer using CVP */
  317. val |= VSE_CVP_PROG_CTRL_CONFIG;
  318. altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
  319. /* STEP 5 - poll CVP_CONFIG READY for 1 with timeout */
  320. ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY,
  321. VSE_CVP_STATUS_CFG_RDY,
  322. conf->priv->poll_time_us);
  323. if (ret) {
  324. dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
  325. return ret;
  326. }
  327. /*
  328. * STEP 6
  329. * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
  330. */
  331. if (conf->priv->switch_clk)
  332. conf->priv->switch_clk(conf);
  333. if (altera_cvp_chkcfg) {
  334. ret = altera_cvp_chk_error(mgr, 0);
  335. if (ret) {
  336. dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
  337. return ret;
  338. }
  339. }
  340. /* STEP 7 - set START_XFER */
  341. altera_read_config_dword(conf, VSE_CVP_PROG_CTRL, &val);
  342. val |= VSE_CVP_PROG_CTRL_START_XFER;
  343. altera_write_config_dword(conf, VSE_CVP_PROG_CTRL, val);
  344. /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */
  345. if (conf->priv->switch_clk) {
  346. altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
  347. val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
  348. val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
  349. altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
  350. }
  351. return 0;
  352. }
  353. static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
  354. size_t count)
  355. {
  356. struct altera_cvp_conf *conf = mgr->priv;
  357. size_t done, remaining, len;
  358. const u32 *data;
  359. int status = 0;
  360. /* STEP 9 - write 32-bit data from RBF file to CVP data register */
  361. data = (u32 *)buf;
  362. remaining = count;
  363. done = 0;
  364. while (remaining) {
  365. /* Use credit throttling if available */
  366. if (conf->priv->wait_credit) {
  367. status = conf->priv->wait_credit(mgr, done);
  368. if (status) {
  369. dev_err(&conf->pci_dev->dev,
  370. "Wait Credit ERR: 0x%x\n", status);
  371. return status;
  372. }
  373. }
  374. len = min(conf->priv->block_size, remaining);
  375. altera_cvp_send_block(conf, data, len);
  376. data += len / sizeof(u32);
  377. done += len;
  378. remaining -= len;
  379. conf->sent_packets++;
  380. /*
  381. * STEP 10 (optional) and STEP 11
  382. * - check error flag
  383. * - loop until data transfer completed
  384. * Config images can be huge (more than 40 MiB), so
  385. * only check after a new 4k data block has been written.
  386. * This reduces the number of checks and speeds up the
  387. * configuration process.
  388. */
  389. if (altera_cvp_chkcfg && !(done % SZ_4K)) {
  390. status = altera_cvp_chk_error(mgr, done);
  391. if (status < 0)
  392. return status;
  393. }
  394. }
  395. if (altera_cvp_chkcfg)
  396. status = altera_cvp_chk_error(mgr, count);
  397. return status;
  398. }
  399. static int altera_cvp_write_complete(struct fpga_manager *mgr,
  400. struct fpga_image_info *info)
  401. {
  402. struct altera_cvp_conf *conf = mgr->priv;
  403. u32 mask, val;
  404. int ret;
  405. ret = altera_cvp_teardown(mgr, info);
  406. if (ret)
  407. return ret;
  408. /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */
  409. altera_read_config_dword(conf, VSE_UNCOR_ERR_STATUS, &val);
  410. if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) {
  411. dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n");
  412. return -EPROTO;
  413. }
  414. /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */
  415. altera_read_config_dword(conf, VSE_CVP_MODE_CTRL, &val);
  416. val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
  417. val &= ~VSE_CVP_MODE_CTRL_CVP_MODE;
  418. altera_write_config_dword(conf, VSE_CVP_MODE_CTRL, val);
  419. /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */
  420. mask = VSE_CVP_STATUS_PLD_CLK_IN_USE | VSE_CVP_STATUS_USERMODE;
  421. ret = altera_cvp_wait_status(conf, mask, mask,
  422. conf->priv->user_time_us);
  423. if (ret)
  424. dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n");
  425. return ret;
  426. }
  427. static const struct fpga_manager_ops altera_cvp_ops = {
  428. .state = altera_cvp_state,
  429. .write_init = altera_cvp_write_init,
  430. .write = altera_cvp_write,
  431. .write_complete = altera_cvp_write_complete,
  432. };
  433. static const struct cvp_priv cvp_priv_v1 = {
  434. .switch_clk = altera_cvp_dummy_write,
  435. .block_size = ALTERA_CVP_V1_SIZE,
  436. .poll_time_us = V1_POLL_TIMEOUT_US,
  437. .user_time_us = TIMEOUT_US,
  438. };
  439. static const struct cvp_priv cvp_priv_v2 = {
  440. .clear_state = altera_cvp_v2_clear_state,
  441. .wait_credit = altera_cvp_v2_wait_for_credit,
  442. .block_size = ALTERA_CVP_V2_SIZE,
  443. .poll_time_us = V2_POLL_TIMEOUT_US,
  444. .user_time_us = V2_USER_TIMEOUT_US,
  445. };
  446. static ssize_t chkcfg_show(struct device_driver *dev, char *buf)
  447. {
  448. return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg);
  449. }
  450. static ssize_t chkcfg_store(struct device_driver *drv, const char *buf,
  451. size_t count)
  452. {
  453. int ret;
  454. ret = kstrtobool(buf, &altera_cvp_chkcfg);
  455. if (ret)
  456. return ret;
  457. return count;
  458. }
  459. static DRIVER_ATTR_RW(chkcfg);
  460. static int altera_cvp_probe(struct pci_dev *pdev,
  461. const struct pci_device_id *dev_id);
  462. static void altera_cvp_remove(struct pci_dev *pdev);
  463. static struct pci_device_id altera_cvp_id_tbl[] = {
  464. { PCI_VDEVICE(ALTERA, PCI_ANY_ID) },
  465. { }
  466. };
  467. MODULE_DEVICE_TABLE(pci, altera_cvp_id_tbl);
  468. static struct pci_driver altera_cvp_driver = {
  469. .name = DRV_NAME,
  470. .id_table = altera_cvp_id_tbl,
  471. .probe = altera_cvp_probe,
  472. .remove = altera_cvp_remove,
  473. };
  474. static int altera_cvp_probe(struct pci_dev *pdev,
  475. const struct pci_device_id *dev_id)
  476. {
  477. struct altera_cvp_conf *conf;
  478. struct fpga_manager *mgr;
  479. u16 cmd, offset;
  480. u32 regval;
  481. int ret;
  482. /*
  483. * First check if this is the expected FPGA device. PCI config
  484. * space access works without enabling the PCI device, memory
  485. * space access is enabled further down.
  486. */
  487. offset = pci_find_vsec_capability(pdev, PCI_VENDOR_ID_ALTERA, 0x1172);
  488. if (!offset) {
  489. dev_err(&pdev->dev, "Wrong VSEC ID value\n");
  490. return -ENODEV;
  491. }
  492. pci_read_config_dword(pdev, offset + VSE_CVP_STATUS, &regval);
  493. if (!(regval & VSE_CVP_STATUS_CVP_EN)) {
  494. dev_err(&pdev->dev,
  495. "CVP is disabled for this device: CVP_STATUS Reg 0x%x\n",
  496. regval);
  497. return -ENODEV;
  498. }
  499. conf = devm_kzalloc(&pdev->dev, sizeof(*conf), GFP_KERNEL);
  500. if (!conf)
  501. return -ENOMEM;
  502. conf->vsec_offset = offset;
  503. /*
  504. * Enable memory BAR access. We cannot use pci_enable_device() here
  505. * because it will make the driver unusable with FPGA devices that
  506. * have additional big IOMEM resources (e.g. 4GiB BARs) on 32-bit
  507. * platform. Such BARs will not have an assigned address range and
  508. * pci_enable_device() will fail, complaining about not claimed BAR,
  509. * even if the concerned BAR is not needed for FPGA configuration
  510. * at all. Thus, enable the device via PCI config space command.
  511. */
  512. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  513. if (!(cmd & PCI_COMMAND_MEMORY)) {
  514. cmd |= PCI_COMMAND_MEMORY;
  515. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  516. }
  517. ret = pci_request_region(pdev, CVP_BAR, "CVP");
  518. if (ret) {
  519. dev_err(&pdev->dev, "Requesting CVP BAR region failed\n");
  520. goto err_disable;
  521. }
  522. conf->pci_dev = pdev;
  523. conf->write_data = altera_cvp_write_data_iomem;
  524. if (conf->vsec_offset == V1_VSEC_OFFSET)
  525. conf->priv = &cvp_priv_v1;
  526. else
  527. conf->priv = &cvp_priv_v2;
  528. conf->map = pci_iomap(pdev, CVP_BAR, 0);
  529. if (!conf->map) {
  530. dev_warn(&pdev->dev, "Mapping CVP BAR failed\n");
  531. conf->write_data = altera_cvp_write_data_config;
  532. }
  533. snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
  534. ALTERA_CVP_MGR_NAME, pci_name(pdev));
  535. mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
  536. &altera_cvp_ops, conf);
  537. if (IS_ERR(mgr)) {
  538. ret = PTR_ERR(mgr);
  539. goto err_unmap;
  540. }
  541. pci_set_drvdata(pdev, mgr);
  542. return 0;
  543. err_unmap:
  544. if (conf->map)
  545. pci_iounmap(pdev, conf->map);
  546. pci_release_region(pdev, CVP_BAR);
  547. err_disable:
  548. cmd &= ~PCI_COMMAND_MEMORY;
  549. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  550. return ret;
  551. }
  552. static void altera_cvp_remove(struct pci_dev *pdev)
  553. {
  554. struct fpga_manager *mgr = pci_get_drvdata(pdev);
  555. struct altera_cvp_conf *conf = mgr->priv;
  556. u16 cmd;
  557. fpga_mgr_unregister(mgr);
  558. if (conf->map)
  559. pci_iounmap(pdev, conf->map);
  560. pci_release_region(pdev, CVP_BAR);
  561. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  562. cmd &= ~PCI_COMMAND_MEMORY;
  563. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  564. }
  565. static int __init altera_cvp_init(void)
  566. {
  567. int ret;
  568. ret = pci_register_driver(&altera_cvp_driver);
  569. if (ret)
  570. return ret;
  571. ret = driver_create_file(&altera_cvp_driver.driver,
  572. &driver_attr_chkcfg);
  573. if (ret)
  574. pr_warn("Can't create sysfs chkcfg file\n");
  575. return 0;
  576. }
  577. static void __exit altera_cvp_exit(void)
  578. {
  579. driver_remove_file(&altera_cvp_driver.driver, &driver_attr_chkcfg);
  580. pci_unregister_driver(&altera_cvp_driver);
  581. }
  582. module_init(altera_cvp_init);
  583. module_exit(altera_cvp_exit);
  584. MODULE_LICENSE("GPL v2");
  585. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  586. MODULE_DESCRIPTION("Module to load Altera FPGA over CvP");