altera-cvp.c 19 KB

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