fw_dnld.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Marvell NFC driver: Firmware downloader
  3. *
  4. * Copyright (C) 2015, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available on the worldwide web at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. *
  13. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  16. * this warranty disclaimer.
  17. */
  18. #include <linux/module.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/firmware.h>
  21. #include <linux/nfc.h>
  22. #include <net/nfc/nci.h>
  23. #include <net/nfc/nci_core.h>
  24. #include "nfcmrvl.h"
  25. #define FW_DNLD_TIMEOUT 15000
  26. #define NCI_OP_PROPRIETARY_BOOT_CMD nci_opcode_pack(NCI_GID_PROPRIETARY, \
  27. NCI_OP_PROP_BOOT_CMD)
  28. /* FW download states */
  29. enum {
  30. STATE_RESET = 0,
  31. STATE_INIT,
  32. STATE_SET_REF_CLOCK,
  33. STATE_SET_HI_CONFIG,
  34. STATE_OPEN_LC,
  35. STATE_FW_DNLD,
  36. STATE_CLOSE_LC,
  37. STATE_BOOT
  38. };
  39. enum {
  40. SUBSTATE_WAIT_COMMAND = 0,
  41. SUBSTATE_WAIT_ACK_CREDIT,
  42. SUBSTATE_WAIT_NACK_CREDIT,
  43. SUBSTATE_WAIT_DATA_CREDIT,
  44. };
  45. /*
  46. ** Patterns for responses
  47. */
  48. static const uint8_t nci_pattern_core_reset_ntf[] = {
  49. 0x60, 0x00, 0x02, 0xA0, 0x01
  50. };
  51. static const uint8_t nci_pattern_core_init_rsp[] = {
  52. 0x40, 0x01, 0x11
  53. };
  54. static const uint8_t nci_pattern_core_set_config_rsp[] = {
  55. 0x40, 0x02, 0x02, 0x00, 0x00
  56. };
  57. static const uint8_t nci_pattern_core_conn_create_rsp[] = {
  58. 0x40, 0x04, 0x04, 0x00
  59. };
  60. static const uint8_t nci_pattern_core_conn_close_rsp[] = {
  61. 0x40, 0x05, 0x01, 0x00
  62. };
  63. static const uint8_t nci_pattern_core_conn_credits_ntf[] = {
  64. 0x60, 0x06, 0x03, 0x01, NCI_CORE_LC_CONNID_PROP_FW_DL, 0x01
  65. };
  66. static const uint8_t nci_pattern_proprietary_boot_rsp[] = {
  67. 0x4F, 0x3A, 0x01, 0x00
  68. };
  69. static struct sk_buff *alloc_lc_skb(struct nfcmrvl_private *priv, uint8_t plen)
  70. {
  71. struct sk_buff *skb;
  72. struct nci_data_hdr *hdr;
  73. skb = nci_skb_alloc(priv->ndev, (NCI_DATA_HDR_SIZE + plen), GFP_KERNEL);
  74. if (!skb) {
  75. pr_err("no memory for data\n");
  76. return NULL;
  77. }
  78. hdr = (struct nci_data_hdr *) skb_put(skb, NCI_DATA_HDR_SIZE);
  79. hdr->conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL;
  80. hdr->rfu = 0;
  81. hdr->plen = plen;
  82. nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
  83. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  84. return skb;
  85. }
  86. static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error)
  87. {
  88. if (priv->fw_dnld.fw) {
  89. release_firmware(priv->fw_dnld.fw);
  90. priv->fw_dnld.fw = NULL;
  91. priv->fw_dnld.header = NULL;
  92. priv->fw_dnld.binary_config = NULL;
  93. }
  94. atomic_set(&priv->ndev->cmd_cnt, 0);
  95. if (timer_pending(&priv->ndev->cmd_timer))
  96. del_timer_sync(&priv->ndev->cmd_timer);
  97. if (timer_pending(&priv->fw_dnld.timer))
  98. del_timer_sync(&priv->fw_dnld.timer);
  99. nfc_info(priv->dev, "FW loading over (%d)]\n", error);
  100. if (error != 0) {
  101. /* failed, halt the chip to avoid power consumption */
  102. nfcmrvl_chip_halt(priv);
  103. }
  104. nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error);
  105. }
  106. static void fw_dnld_timeout(unsigned long arg)
  107. {
  108. struct nfcmrvl_private *priv = (struct nfcmrvl_private *) arg;
  109. nfc_err(priv->dev, "FW loading timeout");
  110. priv->fw_dnld.state = STATE_RESET;
  111. fw_dnld_over(priv, -ETIMEDOUT);
  112. }
  113. static int process_state_reset(struct nfcmrvl_private *priv,
  114. struct sk_buff *skb)
  115. {
  116. if (sizeof(nci_pattern_core_reset_ntf) != skb->len ||
  117. memcmp(skb->data, nci_pattern_core_reset_ntf,
  118. sizeof(nci_pattern_core_reset_ntf)))
  119. return -EINVAL;
  120. nfc_info(priv->dev, "BootROM reset, start fw download\n");
  121. /* Start FW download state machine */
  122. priv->fw_dnld.state = STATE_INIT;
  123. nci_send_cmd(priv->ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  124. return 0;
  125. }
  126. static int process_state_init(struct nfcmrvl_private *priv, struct sk_buff *skb)
  127. {
  128. struct nci_core_set_config_cmd cmd;
  129. if (sizeof(nci_pattern_core_init_rsp) >= skb->len ||
  130. memcmp(skb->data, nci_pattern_core_init_rsp,
  131. sizeof(nci_pattern_core_init_rsp)))
  132. return -EINVAL;
  133. cmd.num_params = 1;
  134. cmd.param.id = NFCMRVL_PROP_REF_CLOCK;
  135. cmd.param.len = 4;
  136. memcpy(cmd.param.val, &priv->fw_dnld.header->ref_clock, 4);
  137. nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len,
  138. &cmd);
  139. priv->fw_dnld.state = STATE_SET_REF_CLOCK;
  140. return 0;
  141. }
  142. static void create_lc(struct nfcmrvl_private *priv)
  143. {
  144. uint8_t param[2] = { NCI_CORE_LC_PROP_FW_DL, 0x0 };
  145. priv->fw_dnld.state = STATE_OPEN_LC;
  146. nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, param);
  147. }
  148. static int process_state_set_ref_clock(struct nfcmrvl_private *priv,
  149. struct sk_buff *skb)
  150. {
  151. struct nci_core_set_config_cmd cmd;
  152. if (sizeof(nci_pattern_core_set_config_rsp) != skb->len ||
  153. memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len))
  154. return -EINVAL;
  155. cmd.num_params = 1;
  156. cmd.param.id = NFCMRVL_PROP_SET_HI_CONFIG;
  157. switch (priv->phy) {
  158. case NFCMRVL_PHY_UART:
  159. cmd.param.len = 5;
  160. memcpy(cmd.param.val,
  161. &priv->fw_dnld.binary_config->uart.baudrate,
  162. 4);
  163. cmd.param.val[4] =
  164. priv->fw_dnld.binary_config->uart.flow_control;
  165. break;
  166. case NFCMRVL_PHY_I2C:
  167. cmd.param.len = 5;
  168. memcpy(cmd.param.val,
  169. &priv->fw_dnld.binary_config->i2c.clk,
  170. 4);
  171. cmd.param.val[4] = 0;
  172. break;
  173. case NFCMRVL_PHY_SPI:
  174. cmd.param.len = 5;
  175. memcpy(cmd.param.val,
  176. &priv->fw_dnld.binary_config->spi.clk,
  177. 4);
  178. cmd.param.val[4] = 0;
  179. break;
  180. default:
  181. create_lc(priv);
  182. return 0;
  183. }
  184. priv->fw_dnld.state = STATE_SET_HI_CONFIG;
  185. nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len,
  186. &cmd);
  187. return 0;
  188. }
  189. static int process_state_set_hi_config(struct nfcmrvl_private *priv,
  190. struct sk_buff *skb)
  191. {
  192. if (sizeof(nci_pattern_core_set_config_rsp) != skb->len ||
  193. memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len))
  194. return -EINVAL;
  195. create_lc(priv);
  196. return 0;
  197. }
  198. static int process_state_open_lc(struct nfcmrvl_private *priv,
  199. struct sk_buff *skb)
  200. {
  201. if (sizeof(nci_pattern_core_conn_create_rsp) >= skb->len ||
  202. memcmp(skb->data, nci_pattern_core_conn_create_rsp,
  203. sizeof(nci_pattern_core_conn_create_rsp)))
  204. return -EINVAL;
  205. priv->fw_dnld.state = STATE_FW_DNLD;
  206. priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
  207. priv->fw_dnld.offset = priv->fw_dnld.binary_config->offset;
  208. return 0;
  209. }
  210. static int process_state_fw_dnld(struct nfcmrvl_private *priv,
  211. struct sk_buff *skb)
  212. {
  213. uint16_t len;
  214. uint16_t comp_len;
  215. struct sk_buff *out_skb;
  216. switch (priv->fw_dnld.substate) {
  217. case SUBSTATE_WAIT_COMMAND:
  218. /*
  219. * Command format:
  220. * B0..2: NCI header
  221. * B3 : Helper command (0xA5)
  222. * B4..5: le16 data size
  223. * B6..7: le16 data size complement (~)
  224. * B8..N: payload
  225. */
  226. /* Remove NCI HDR */
  227. skb_pull(skb, 3);
  228. if (skb->data[0] != HELPER_CMD_PACKET_FORMAT || skb->len != 5) {
  229. nfc_err(priv->dev, "bad command");
  230. return -EINVAL;
  231. }
  232. skb_pull(skb, 1);
  233. memcpy(&len, skb->data, 2);
  234. skb_pull(skb, 2);
  235. memcpy(&comp_len, skb->data, 2);
  236. skb_pull(skb, 2);
  237. len = get_unaligned_le16(&len);
  238. comp_len = get_unaligned_le16(&comp_len);
  239. if (((~len) & 0xFFFF) != comp_len) {
  240. nfc_err(priv->dev, "bad len complement: %x %x %x",
  241. len, comp_len, (~len & 0xFFFF));
  242. out_skb = alloc_lc_skb(priv, 1);
  243. if (!out_skb)
  244. return -ENOMEM;
  245. *skb_put(out_skb, 1) = 0xBF;
  246. nci_send_frame(priv->ndev, out_skb);
  247. priv->fw_dnld.substate = SUBSTATE_WAIT_NACK_CREDIT;
  248. return 0;
  249. }
  250. priv->fw_dnld.chunk_len = len;
  251. out_skb = alloc_lc_skb(priv, 1);
  252. if (!out_skb)
  253. return -ENOMEM;
  254. *skb_put(out_skb, 1) = HELPER_ACK_PACKET_FORMAT;
  255. nci_send_frame(priv->ndev, out_skb);
  256. priv->fw_dnld.substate = SUBSTATE_WAIT_ACK_CREDIT;
  257. break;
  258. case SUBSTATE_WAIT_ACK_CREDIT:
  259. if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
  260. memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
  261. skb->len)) {
  262. nfc_err(priv->dev, "bad packet: waiting for credit");
  263. return -EINVAL;
  264. }
  265. if (priv->fw_dnld.chunk_len == 0) {
  266. /* FW Loading is done */
  267. uint8_t conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL;
  268. priv->fw_dnld.state = STATE_CLOSE_LC;
  269. nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CLOSE_CMD,
  270. 1, &conn_id);
  271. } else {
  272. out_skb = alloc_lc_skb(priv, priv->fw_dnld.chunk_len);
  273. if (!out_skb)
  274. return -ENOMEM;
  275. memcpy(skb_put(out_skb, priv->fw_dnld.chunk_len),
  276. ((uint8_t *)priv->fw_dnld.fw->data) +
  277. priv->fw_dnld.offset,
  278. priv->fw_dnld.chunk_len);
  279. nci_send_frame(priv->ndev, out_skb);
  280. priv->fw_dnld.substate = SUBSTATE_WAIT_DATA_CREDIT;
  281. }
  282. break;
  283. case SUBSTATE_WAIT_DATA_CREDIT:
  284. if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
  285. memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
  286. skb->len)) {
  287. nfc_err(priv->dev, "bad packet: waiting for credit");
  288. return -EINVAL;
  289. }
  290. priv->fw_dnld.offset += priv->fw_dnld.chunk_len;
  291. priv->fw_dnld.chunk_len = 0;
  292. priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
  293. break;
  294. case SUBSTATE_WAIT_NACK_CREDIT:
  295. if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
  296. memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
  297. skb->len)) {
  298. nfc_err(priv->dev, "bad packet: waiting for credit");
  299. return -EINVAL;
  300. }
  301. priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
  302. break;
  303. }
  304. return 0;
  305. }
  306. static int process_state_close_lc(struct nfcmrvl_private *priv,
  307. struct sk_buff *skb)
  308. {
  309. if (sizeof(nci_pattern_core_conn_close_rsp) != skb->len ||
  310. memcmp(skb->data, nci_pattern_core_conn_close_rsp, skb->len))
  311. return -EINVAL;
  312. priv->fw_dnld.state = STATE_BOOT;
  313. nci_send_cmd(priv->ndev, NCI_OP_PROPRIETARY_BOOT_CMD, 0, NULL);
  314. return 0;
  315. }
  316. static int process_state_boot(struct nfcmrvl_private *priv, struct sk_buff *skb)
  317. {
  318. if (sizeof(nci_pattern_proprietary_boot_rsp) != skb->len ||
  319. memcmp(skb->data, nci_pattern_proprietary_boot_rsp, skb->len))
  320. return -EINVAL;
  321. /*
  322. * Update HI config to use the right configuration for the next
  323. * data exchanges.
  324. */
  325. priv->if_ops->nci_update_config(priv,
  326. &priv->fw_dnld.binary_config->config);
  327. if (priv->fw_dnld.binary_config == &priv->fw_dnld.header->helper) {
  328. /*
  329. * This is the case where an helper was needed and we have
  330. * uploaded it. Now we have to wait the next RESET NTF to start
  331. * FW download.
  332. */
  333. priv->fw_dnld.state = STATE_RESET;
  334. priv->fw_dnld.binary_config = &priv->fw_dnld.header->firmware;
  335. nfc_info(priv->dev, "FW loading: helper loaded");
  336. } else {
  337. nfc_info(priv->dev, "FW loading: firmware loaded");
  338. fw_dnld_over(priv, 0);
  339. }
  340. return 0;
  341. }
  342. static void fw_dnld_rx_work(struct work_struct *work)
  343. {
  344. int ret;
  345. struct sk_buff *skb;
  346. struct nfcmrvl_fw_dnld *fw_dnld = container_of(work,
  347. struct nfcmrvl_fw_dnld,
  348. rx_work);
  349. struct nfcmrvl_private *priv = container_of(fw_dnld,
  350. struct nfcmrvl_private,
  351. fw_dnld);
  352. while ((skb = skb_dequeue(&fw_dnld->rx_q))) {
  353. nfc_send_to_raw_sock(priv->ndev->nfc_dev, skb,
  354. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  355. switch (fw_dnld->state) {
  356. case STATE_RESET:
  357. ret = process_state_reset(priv, skb);
  358. break;
  359. case STATE_INIT:
  360. ret = process_state_init(priv, skb);
  361. break;
  362. case STATE_SET_REF_CLOCK:
  363. ret = process_state_set_ref_clock(priv, skb);
  364. break;
  365. case STATE_SET_HI_CONFIG:
  366. ret = process_state_set_hi_config(priv, skb);
  367. break;
  368. case STATE_OPEN_LC:
  369. ret = process_state_open_lc(priv, skb);
  370. break;
  371. case STATE_FW_DNLD:
  372. ret = process_state_fw_dnld(priv, skb);
  373. break;
  374. case STATE_CLOSE_LC:
  375. ret = process_state_close_lc(priv, skb);
  376. break;
  377. case STATE_BOOT:
  378. ret = process_state_boot(priv, skb);
  379. break;
  380. default:
  381. ret = -EFAULT;
  382. }
  383. kfree_skb(skb);
  384. if (ret != 0) {
  385. nfc_err(priv->dev, "FW loading error");
  386. fw_dnld_over(priv, ret);
  387. break;
  388. }
  389. }
  390. }
  391. int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv)
  392. {
  393. char name[32];
  394. INIT_WORK(&priv->fw_dnld.rx_work, fw_dnld_rx_work);
  395. snprintf(name, sizeof(name), "%s_nfcmrvl_fw_dnld_rx_wq",
  396. dev_name(&priv->ndev->nfc_dev->dev));
  397. priv->fw_dnld.rx_wq = create_singlethread_workqueue(name);
  398. if (!priv->fw_dnld.rx_wq)
  399. return -ENOMEM;
  400. skb_queue_head_init(&priv->fw_dnld.rx_q);
  401. return 0;
  402. }
  403. void nfcmrvl_fw_dnld_deinit(struct nfcmrvl_private *priv)
  404. {
  405. destroy_workqueue(priv->fw_dnld.rx_wq);
  406. }
  407. void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv,
  408. struct sk_buff *skb)
  409. {
  410. /* Discard command timer */
  411. if (timer_pending(&priv->ndev->cmd_timer))
  412. del_timer_sync(&priv->ndev->cmd_timer);
  413. /* Allow next command */
  414. atomic_set(&priv->ndev->cmd_cnt, 1);
  415. /* Queue and trigger rx work */
  416. skb_queue_tail(&priv->fw_dnld.rx_q, skb);
  417. queue_work(priv->fw_dnld.rx_wq, &priv->fw_dnld.rx_work);
  418. }
  419. void nfcmrvl_fw_dnld_abort(struct nfcmrvl_private *priv)
  420. {
  421. fw_dnld_over(priv, -EHOSTDOWN);
  422. }
  423. int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
  424. {
  425. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  426. struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld;
  427. int res;
  428. if (!priv->support_fw_dnld)
  429. return -ENOTSUPP;
  430. if (!firmware_name || !firmware_name[0])
  431. return -EINVAL;
  432. strcpy(fw_dnld->name, firmware_name);
  433. /*
  434. * Retrieve FW binary file and parse it to initialize FW download
  435. * state machine.
  436. */
  437. /* Retrieve FW binary */
  438. res = request_firmware(&fw_dnld->fw, firmware_name,
  439. &ndev->nfc_dev->dev);
  440. if (res < 0) {
  441. nfc_err(priv->dev, "failed to retrieve FW %s", firmware_name);
  442. return -ENOENT;
  443. }
  444. fw_dnld->header = (const struct nfcmrvl_fw *) priv->fw_dnld.fw->data;
  445. if (fw_dnld->header->magic != NFCMRVL_FW_MAGIC ||
  446. fw_dnld->header->phy != priv->phy) {
  447. nfc_err(priv->dev, "bad firmware binary %s magic=0x%x phy=%d",
  448. firmware_name, fw_dnld->header->magic,
  449. fw_dnld->header->phy);
  450. release_firmware(fw_dnld->fw);
  451. fw_dnld->header = NULL;
  452. return -EINVAL;
  453. }
  454. if (fw_dnld->header->helper.offset != 0) {
  455. nfc_info(priv->dev, "loading helper");
  456. fw_dnld->binary_config = &fw_dnld->header->helper;
  457. } else {
  458. nfc_info(priv->dev, "loading firmware");
  459. fw_dnld->binary_config = &fw_dnld->header->firmware;
  460. }
  461. /* Configure a timer for timeout */
  462. setup_timer(&priv->fw_dnld.timer, fw_dnld_timeout,
  463. (unsigned long) priv);
  464. mod_timer(&priv->fw_dnld.timer,
  465. jiffies + msecs_to_jiffies(FW_DNLD_TIMEOUT));
  466. /* Ronfigure HI to be sure that it is the bootrom values */
  467. priv->if_ops->nci_update_config(priv,
  468. &fw_dnld->header->bootrom.config);
  469. /* Allow first command */
  470. atomic_set(&priv->ndev->cmd_cnt, 1);
  471. /* First, reset the chip */
  472. priv->fw_dnld.state = STATE_RESET;
  473. nfcmrvl_chip_reset(priv);
  474. /* Now wait for CORE_RESET_NTF or timeout */
  475. return 0;
  476. }