altera-cvp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * FPGA Manager Driver for Altera Arria/Cyclone/Stratix CvP
  3. *
  4. * Copyright (C) 2017 DENX Software Engineering
  5. *
  6. * Anatolij Gustschin <agust@denx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * Manage Altera FPGA firmware using PCIe CvP.
  18. * Firmware must be in binary "rbf" format.
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/fpga/fpga-mgr.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/sizes.h>
  26. #define CVP_BAR 0 /* BAR used for data transfer in memory mode */
  27. #define CVP_DUMMY_WR 244 /* dummy writes to clear CvP state machine */
  28. #define TIMEOUT_US 2000 /* CVP STATUS timeout for USERMODE polling */
  29. /* Vendor Specific Extended Capability Registers */
  30. #define VSE_PCIE_EXT_CAP_ID 0x200
  31. #define VSE_PCIE_EXT_CAP_ID_VAL 0x000b /* 16bit */
  32. #define VSE_CVP_STATUS 0x21c /* 32bit */
  33. #define VSE_CVP_STATUS_CFG_RDY BIT(18) /* CVP_CONFIG_READY */
  34. #define VSE_CVP_STATUS_CFG_ERR BIT(19) /* CVP_CONFIG_ERROR */
  35. #define VSE_CVP_STATUS_CVP_EN BIT(20) /* ctrl block is enabling CVP */
  36. #define VSE_CVP_STATUS_USERMODE BIT(21) /* USERMODE */
  37. #define VSE_CVP_STATUS_CFG_DONE BIT(23) /* CVP_CONFIG_DONE */
  38. #define VSE_CVP_STATUS_PLD_CLK_IN_USE BIT(24) /* PLD_CLK_IN_USE */
  39. #define VSE_CVP_MODE_CTRL 0x220 /* 32bit */
  40. #define VSE_CVP_MODE_CTRL_CVP_MODE BIT(0) /* CVP (1) or normal mode (0) */
  41. #define VSE_CVP_MODE_CTRL_HIP_CLK_SEL BIT(1) /* PMA (1) or fabric clock (0) */
  42. #define VSE_CVP_MODE_CTRL_NUMCLKS_OFF 8 /* NUMCLKS bits offset */
  43. #define VSE_CVP_MODE_CTRL_NUMCLKS_MASK GENMASK(15, 8)
  44. #define VSE_CVP_DATA 0x228 /* 32bit */
  45. #define VSE_CVP_PROG_CTRL 0x22c /* 32bit */
  46. #define VSE_CVP_PROG_CTRL_CONFIG BIT(0)
  47. #define VSE_CVP_PROG_CTRL_START_XFER BIT(1)
  48. #define VSE_UNCOR_ERR_STATUS 0x234 /* 32bit */
  49. #define VSE_UNCOR_ERR_CVP_CFG_ERR BIT(5) /* CVP_CONFIG_ERROR_LATCHED */
  50. #define DRV_NAME "altera-cvp"
  51. #define ALTERA_CVP_MGR_NAME "Altera CvP FPGA Manager"
  52. /* Optional CvP config error status check for debugging */
  53. static bool altera_cvp_chkcfg;
  54. struct altera_cvp_conf {
  55. struct fpga_manager *mgr;
  56. struct pci_dev *pci_dev;
  57. void __iomem *map;
  58. void (*write_data)(struct altera_cvp_conf *, u32);
  59. char mgr_name[64];
  60. u8 numclks;
  61. };
  62. static enum fpga_mgr_states altera_cvp_state(struct fpga_manager *mgr)
  63. {
  64. struct altera_cvp_conf *conf = mgr->priv;
  65. u32 status;
  66. pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &status);
  67. if (status & VSE_CVP_STATUS_CFG_DONE)
  68. return FPGA_MGR_STATE_OPERATING;
  69. if (status & VSE_CVP_STATUS_CVP_EN)
  70. return FPGA_MGR_STATE_POWER_UP;
  71. return FPGA_MGR_STATE_UNKNOWN;
  72. }
  73. static void altera_cvp_write_data_iomem(struct altera_cvp_conf *conf, u32 val)
  74. {
  75. writel(val, conf->map);
  76. }
  77. static void altera_cvp_write_data_config(struct altera_cvp_conf *conf, u32 val)
  78. {
  79. pci_write_config_dword(conf->pci_dev, VSE_CVP_DATA, val);
  80. }
  81. /* switches between CvP clock and internal clock */
  82. static void altera_cvp_dummy_write(struct altera_cvp_conf *conf)
  83. {
  84. unsigned int i;
  85. u32 val;
  86. /* set 1 CVP clock cycle for every CVP Data Register Write */
  87. pci_read_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, &val);
  88. val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
  89. val |= 1 << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
  90. pci_write_config_dword(conf->pci_dev, VSE_CVP_MODE_CTRL, val);
  91. for (i = 0; i < CVP_DUMMY_WR; i++)
  92. conf->write_data(conf, 0); /* dummy data, could be any value */
  93. }
  94. static int altera_cvp_wait_status(struct altera_cvp_conf *conf, u32 status_mask,
  95. u32 status_val, int timeout_us)
  96. {
  97. unsigned int retries;
  98. u32 val;
  99. retries = timeout_us / 10;
  100. if (timeout_us % 10)
  101. retries++;
  102. do {
  103. pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val);
  104. if ((val & status_mask) == status_val)
  105. return 0;
  106. /* use small usleep value to re-check and break early */
  107. usleep_range(10, 11);
  108. } while (--retries);
  109. return -ETIMEDOUT;
  110. }
  111. static int altera_cvp_teardown(struct fpga_manager *mgr,
  112. struct fpga_image_info *info)
  113. {
  114. struct altera_cvp_conf *conf = mgr->priv;
  115. struct pci_dev *pdev = conf->pci_dev;
  116. int ret;
  117. u32 val;
  118. /* STEP 12 - reset START_XFER bit */
  119. pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val);
  120. val &= ~VSE_CVP_PROG_CTRL_START_XFER;
  121. pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val);
  122. /* STEP 13 - reset CVP_CONFIG bit */
  123. val &= ~VSE_CVP_PROG_CTRL_CONFIG;
  124. pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val);
  125. /*
  126. * STEP 14
  127. * - set CVP_NUMCLKS to 1 and then issue CVP_DUMMY_WR dummy
  128. * writes to the HIP
  129. */
  130. altera_cvp_dummy_write(conf); /* from CVP clock to internal clock */
  131. /* STEP 15 - poll CVP_CONFIG_READY bit for 0 with 10us timeout */
  132. ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY, 0, 10);
  133. if (ret)
  134. dev_err(&mgr->dev, "CFG_RDY == 0 timeout\n");
  135. return ret;
  136. }
  137. static int altera_cvp_write_init(struct fpga_manager *mgr,
  138. struct fpga_image_info *info,
  139. const char *buf, size_t count)
  140. {
  141. struct altera_cvp_conf *conf = mgr->priv;
  142. struct pci_dev *pdev = conf->pci_dev;
  143. u32 iflags, val;
  144. int ret;
  145. iflags = info ? info->flags : 0;
  146. if (iflags & FPGA_MGR_PARTIAL_RECONFIG) {
  147. dev_err(&mgr->dev, "Partial reconfiguration not supported.\n");
  148. return -EINVAL;
  149. }
  150. /* Determine allowed clock to data ratio */
  151. if (iflags & FPGA_MGR_COMPRESSED_BITSTREAM)
  152. conf->numclks = 8; /* ratio for all compressed images */
  153. else if (iflags & FPGA_MGR_ENCRYPTED_BITSTREAM)
  154. conf->numclks = 4; /* for uncompressed and encrypted images */
  155. else
  156. conf->numclks = 1; /* for uncompressed and unencrypted images */
  157. /* STEP 1 - read CVP status and check CVP_EN flag */
  158. pci_read_config_dword(pdev, VSE_CVP_STATUS, &val);
  159. if (!(val & VSE_CVP_STATUS_CVP_EN)) {
  160. dev_err(&mgr->dev, "CVP mode off: 0x%04x\n", val);
  161. return -ENODEV;
  162. }
  163. if (val & VSE_CVP_STATUS_CFG_RDY) {
  164. dev_warn(&mgr->dev, "CvP already started, teardown first\n");
  165. ret = altera_cvp_teardown(mgr, info);
  166. if (ret)
  167. return ret;
  168. }
  169. /*
  170. * STEP 2
  171. * - set HIP_CLK_SEL and CVP_MODE (must be set in the order mentioned)
  172. */
  173. /* switch from fabric to PMA clock */
  174. pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val);
  175. val |= VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
  176. pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val);
  177. /* set CVP mode */
  178. pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val);
  179. val |= VSE_CVP_MODE_CTRL_CVP_MODE;
  180. pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val);
  181. /*
  182. * STEP 3
  183. * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
  184. */
  185. altera_cvp_dummy_write(conf);
  186. /* STEP 4 - set CVP_CONFIG bit */
  187. pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val);
  188. /* request control block to begin transfer using CVP */
  189. val |= VSE_CVP_PROG_CTRL_CONFIG;
  190. pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val);
  191. /* STEP 5 - poll CVP_CONFIG READY for 1 with 10us timeout */
  192. ret = altera_cvp_wait_status(conf, VSE_CVP_STATUS_CFG_RDY,
  193. VSE_CVP_STATUS_CFG_RDY, 10);
  194. if (ret) {
  195. dev_warn(&mgr->dev, "CFG_RDY == 1 timeout\n");
  196. return ret;
  197. }
  198. /*
  199. * STEP 6
  200. * - set CVP_NUMCLKS to 1 and issue CVP_DUMMY_WR dummy writes to the HIP
  201. */
  202. altera_cvp_dummy_write(conf);
  203. /* STEP 7 - set START_XFER */
  204. pci_read_config_dword(pdev, VSE_CVP_PROG_CTRL, &val);
  205. val |= VSE_CVP_PROG_CTRL_START_XFER;
  206. pci_write_config_dword(pdev, VSE_CVP_PROG_CTRL, val);
  207. /* STEP 8 - start transfer (set CVP_NUMCLKS for bitstream) */
  208. pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val);
  209. val &= ~VSE_CVP_MODE_CTRL_NUMCLKS_MASK;
  210. val |= conf->numclks << VSE_CVP_MODE_CTRL_NUMCLKS_OFF;
  211. pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val);
  212. return 0;
  213. }
  214. static inline int altera_cvp_chk_error(struct fpga_manager *mgr, size_t bytes)
  215. {
  216. struct altera_cvp_conf *conf = mgr->priv;
  217. u32 val;
  218. /* STEP 10 (optional) - check CVP_CONFIG_ERROR flag */
  219. pci_read_config_dword(conf->pci_dev, VSE_CVP_STATUS, &val);
  220. if (val & VSE_CVP_STATUS_CFG_ERR) {
  221. dev_err(&mgr->dev, "CVP_CONFIG_ERROR after %zu bytes!\n",
  222. bytes);
  223. return -EPROTO;
  224. }
  225. return 0;
  226. }
  227. static int altera_cvp_write(struct fpga_manager *mgr, const char *buf,
  228. size_t count)
  229. {
  230. struct altera_cvp_conf *conf = mgr->priv;
  231. const u32 *data;
  232. size_t done, remaining;
  233. int status = 0;
  234. u32 mask;
  235. /* STEP 9 - write 32-bit data from RBF file to CVP data register */
  236. data = (u32 *)buf;
  237. remaining = count;
  238. done = 0;
  239. while (remaining >= 4) {
  240. conf->write_data(conf, *data++);
  241. done += 4;
  242. remaining -= 4;
  243. /*
  244. * STEP 10 (optional) and STEP 11
  245. * - check error flag
  246. * - loop until data transfer completed
  247. * Config images can be huge (more than 40 MiB), so
  248. * only check after a new 4k data block has been written.
  249. * This reduces the number of checks and speeds up the
  250. * configuration process.
  251. */
  252. if (altera_cvp_chkcfg && !(done % SZ_4K)) {
  253. status = altera_cvp_chk_error(mgr, done);
  254. if (status < 0)
  255. return status;
  256. }
  257. }
  258. /* write up to 3 trailing bytes, if any */
  259. mask = BIT(remaining * 8) - 1;
  260. if (mask)
  261. conf->write_data(conf, *data & mask);
  262. if (altera_cvp_chkcfg)
  263. status = altera_cvp_chk_error(mgr, count);
  264. return status;
  265. }
  266. static int altera_cvp_write_complete(struct fpga_manager *mgr,
  267. struct fpga_image_info *info)
  268. {
  269. struct altera_cvp_conf *conf = mgr->priv;
  270. struct pci_dev *pdev = conf->pci_dev;
  271. int ret;
  272. u32 mask;
  273. u32 val;
  274. ret = altera_cvp_teardown(mgr, info);
  275. if (ret)
  276. return ret;
  277. /* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */
  278. pci_read_config_dword(pdev, VSE_UNCOR_ERR_STATUS, &val);
  279. if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) {
  280. dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n");
  281. return -EPROTO;
  282. }
  283. /* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */
  284. pci_read_config_dword(pdev, VSE_CVP_MODE_CTRL, &val);
  285. val &= ~VSE_CVP_MODE_CTRL_HIP_CLK_SEL;
  286. val &= ~VSE_CVP_MODE_CTRL_CVP_MODE;
  287. pci_write_config_dword(pdev, VSE_CVP_MODE_CTRL, val);
  288. /* STEP 18 - poll PLD_CLK_IN_USE and USER_MODE bits */
  289. mask = VSE_CVP_STATUS_PLD_CLK_IN_USE | VSE_CVP_STATUS_USERMODE;
  290. ret = altera_cvp_wait_status(conf, mask, mask, TIMEOUT_US);
  291. if (ret)
  292. dev_err(&mgr->dev, "PLD_CLK_IN_USE|USERMODE timeout\n");
  293. return ret;
  294. }
  295. static const struct fpga_manager_ops altera_cvp_ops = {
  296. .state = altera_cvp_state,
  297. .write_init = altera_cvp_write_init,
  298. .write = altera_cvp_write,
  299. .write_complete = altera_cvp_write_complete,
  300. };
  301. static ssize_t chkcfg_show(struct device_driver *dev, char *buf)
  302. {
  303. return snprintf(buf, 3, "%d\n", altera_cvp_chkcfg);
  304. }
  305. static ssize_t chkcfg_store(struct device_driver *drv, const char *buf,
  306. size_t count)
  307. {
  308. int ret;
  309. ret = kstrtobool(buf, &altera_cvp_chkcfg);
  310. if (ret)
  311. return ret;
  312. return count;
  313. }
  314. static DRIVER_ATTR_RW(chkcfg);
  315. static int altera_cvp_probe(struct pci_dev *pdev,
  316. const struct pci_device_id *dev_id);
  317. static void altera_cvp_remove(struct pci_dev *pdev);
  318. #define PCI_VENDOR_ID_ALTERA 0x1172
  319. static struct pci_device_id altera_cvp_id_tbl[] = {
  320. { PCI_VDEVICE(ALTERA, PCI_ANY_ID) },
  321. { }
  322. };
  323. MODULE_DEVICE_TABLE(pci, altera_cvp_id_tbl);
  324. static struct pci_driver altera_cvp_driver = {
  325. .name = DRV_NAME,
  326. .id_table = altera_cvp_id_tbl,
  327. .probe = altera_cvp_probe,
  328. .remove = altera_cvp_remove,
  329. };
  330. static int altera_cvp_probe(struct pci_dev *pdev,
  331. const struct pci_device_id *dev_id)
  332. {
  333. struct altera_cvp_conf *conf;
  334. u16 cmd, val;
  335. u32 regval;
  336. int ret;
  337. /*
  338. * First check if this is the expected FPGA device. PCI config
  339. * space access works without enabling the PCI device, memory
  340. * space access is enabled further down.
  341. */
  342. pci_read_config_word(pdev, VSE_PCIE_EXT_CAP_ID, &val);
  343. if (val != VSE_PCIE_EXT_CAP_ID_VAL) {
  344. dev_err(&pdev->dev, "Wrong EXT_CAP_ID value 0x%x\n", val);
  345. return -ENODEV;
  346. }
  347. pci_read_config_dword(pdev, VSE_CVP_STATUS, &regval);
  348. if (!(regval & VSE_CVP_STATUS_CVP_EN)) {
  349. dev_err(&pdev->dev,
  350. "CVP is disabled for this device: CVP_STATUS Reg 0x%x\n",
  351. regval);
  352. return -ENODEV;
  353. }
  354. conf = devm_kzalloc(&pdev->dev, sizeof(*conf), GFP_KERNEL);
  355. if (!conf)
  356. return -ENOMEM;
  357. /*
  358. * Enable memory BAR access. We cannot use pci_enable_device() here
  359. * because it will make the driver unusable with FPGA devices that
  360. * have additional big IOMEM resources (e.g. 4GiB BARs) on 32-bit
  361. * platform. Such BARs will not have an assigned address range and
  362. * pci_enable_device() will fail, complaining about not claimed BAR,
  363. * even if the concerned BAR is not needed for FPGA configuration
  364. * at all. Thus, enable the device via PCI config space command.
  365. */
  366. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  367. if (!(cmd & PCI_COMMAND_MEMORY)) {
  368. cmd |= PCI_COMMAND_MEMORY;
  369. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  370. }
  371. ret = pci_request_region(pdev, CVP_BAR, "CVP");
  372. if (ret) {
  373. dev_err(&pdev->dev, "Requesting CVP BAR region failed\n");
  374. goto err_disable;
  375. }
  376. conf->pci_dev = pdev;
  377. conf->write_data = altera_cvp_write_data_iomem;
  378. conf->map = pci_iomap(pdev, CVP_BAR, 0);
  379. if (!conf->map) {
  380. dev_warn(&pdev->dev, "Mapping CVP BAR failed\n");
  381. conf->write_data = altera_cvp_write_data_config;
  382. }
  383. snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
  384. ALTERA_CVP_MGR_NAME, pci_name(pdev));
  385. ret = fpga_mgr_register(&pdev->dev, conf->mgr_name,
  386. &altera_cvp_ops, conf);
  387. if (ret)
  388. goto err_unmap;
  389. ret = driver_create_file(&altera_cvp_driver.driver,
  390. &driver_attr_chkcfg);
  391. if (ret) {
  392. dev_err(&pdev->dev, "Can't create sysfs chkcfg file\n");
  393. fpga_mgr_unregister(&pdev->dev);
  394. goto err_unmap;
  395. }
  396. return 0;
  397. err_unmap:
  398. pci_iounmap(pdev, conf->map);
  399. pci_release_region(pdev, CVP_BAR);
  400. err_disable:
  401. cmd &= ~PCI_COMMAND_MEMORY;
  402. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  403. return ret;
  404. }
  405. static void altera_cvp_remove(struct pci_dev *pdev)
  406. {
  407. struct fpga_manager *mgr = pci_get_drvdata(pdev);
  408. struct altera_cvp_conf *conf = mgr->priv;
  409. u16 cmd;
  410. driver_remove_file(&altera_cvp_driver.driver, &driver_attr_chkcfg);
  411. fpga_mgr_unregister(&pdev->dev);
  412. pci_iounmap(pdev, conf->map);
  413. pci_release_region(pdev, CVP_BAR);
  414. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  415. cmd &= ~PCI_COMMAND_MEMORY;
  416. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  417. }
  418. module_pci_driver(altera_cvp_driver);
  419. MODULE_LICENSE("GPL v2");
  420. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  421. MODULE_DESCRIPTION("Module to load Altera FPGA over CvP");