firmware.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic driver for NXP NCI NFC chips
  4. *
  5. * Copyright (C) 2014 NXP Semiconductors All rights reserved.
  6. *
  7. * Author: Clément Perrochaud <[email protected]>
  8. *
  9. * Derived from PN544 device driver:
  10. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  11. */
  12. #include <linux/completion.h>
  13. #include <linux/firmware.h>
  14. #include <linux/nfc.h>
  15. #include <asm/unaligned.h>
  16. #include "nxp-nci.h"
  17. /* Crypto operations can take up to 30 seconds */
  18. #define NXP_NCI_FW_ANSWER_TIMEOUT msecs_to_jiffies(30000)
  19. #define NXP_NCI_FW_CMD_RESET 0xF0
  20. #define NXP_NCI_FW_CMD_GETVERSION 0xF1
  21. #define NXP_NCI_FW_CMD_CHECKINTEGRITY 0xE0
  22. #define NXP_NCI_FW_CMD_WRITE 0xC0
  23. #define NXP_NCI_FW_CMD_READ 0xA2
  24. #define NXP_NCI_FW_CMD_GETSESSIONSTATE 0xF2
  25. #define NXP_NCI_FW_CMD_LOG 0xA7
  26. #define NXP_NCI_FW_CMD_FORCE 0xD0
  27. #define NXP_NCI_FW_CMD_GET_DIE_ID 0xF4
  28. #define NXP_NCI_FW_CHUNK_FLAG 0x0400
  29. #define NXP_NCI_FW_RESULT_OK 0x00
  30. #define NXP_NCI_FW_RESULT_INVALID_ADDR 0x01
  31. #define NXP_NCI_FW_RESULT_GENERIC_ERROR 0x02
  32. #define NXP_NCI_FW_RESULT_UNKNOWN_CMD 0x0B
  33. #define NXP_NCI_FW_RESULT_ABORTED_CMD 0x0C
  34. #define NXP_NCI_FW_RESULT_PLL_ERROR 0x0D
  35. #define NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR 0x1E
  36. #define NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR 0x1F
  37. #define NXP_NCI_FW_RESULT_MEM_BSY 0x20
  38. #define NXP_NCI_FW_RESULT_SIGNATURE_ERROR 0x21
  39. #define NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR 0x24
  40. #define NXP_NCI_FW_RESULT_PROTOCOL_ERROR 0x28
  41. #define NXP_NCI_FW_RESULT_SFWU_DEGRADED 0x2A
  42. #define NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK 0x2D
  43. #define NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK 0x2E
  44. #define NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5 0xC5
  45. void nxp_nci_fw_work_complete(struct nxp_nci_info *info, int result)
  46. {
  47. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  48. int r;
  49. if (info->phy_ops->set_mode) {
  50. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
  51. if (r < 0 && result == 0)
  52. result = -r;
  53. }
  54. info->mode = NXP_NCI_MODE_COLD;
  55. if (fw_info->fw) {
  56. release_firmware(fw_info->fw);
  57. fw_info->fw = NULL;
  58. }
  59. nfc_fw_download_done(info->ndev->nfc_dev, fw_info->name, (u32) -result);
  60. }
  61. /* crc_ccitt cannot be used since it is computed MSB first and not LSB first */
  62. static u16 nxp_nci_fw_crc(u8 const *buffer, size_t len)
  63. {
  64. u16 crc = 0xffff;
  65. while (len--) {
  66. crc = ((crc >> 8) | (crc << 8)) ^ *buffer++;
  67. crc ^= (crc & 0xff) >> 4;
  68. crc ^= (crc & 0xff) << 12;
  69. crc ^= (crc & 0xff) << 5;
  70. }
  71. return crc;
  72. }
  73. static int nxp_nci_fw_send_chunk(struct nxp_nci_info *info)
  74. {
  75. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  76. u16 header, crc;
  77. struct sk_buff *skb;
  78. size_t chunk_len;
  79. size_t remaining_len;
  80. int r;
  81. skb = nci_skb_alloc(info->ndev, info->max_payload, GFP_KERNEL);
  82. if (!skb)
  83. return -ENOMEM;
  84. chunk_len = info->max_payload - NXP_NCI_FW_HDR_LEN - NXP_NCI_FW_CRC_LEN;
  85. remaining_len = fw_info->frame_size - fw_info->written;
  86. if (remaining_len > chunk_len) {
  87. header = NXP_NCI_FW_CHUNK_FLAG;
  88. } else {
  89. chunk_len = remaining_len;
  90. header = 0x0000;
  91. }
  92. header |= chunk_len & NXP_NCI_FW_FRAME_LEN_MASK;
  93. put_unaligned_be16(header, skb_put(skb, NXP_NCI_FW_HDR_LEN));
  94. skb_put_data(skb, fw_info->data + fw_info->written, chunk_len);
  95. crc = nxp_nci_fw_crc(skb->data, chunk_len + NXP_NCI_FW_HDR_LEN);
  96. put_unaligned_be16(crc, skb_put(skb, NXP_NCI_FW_CRC_LEN));
  97. r = info->phy_ops->write(info->phy_id, skb);
  98. if (r >= 0)
  99. r = chunk_len;
  100. kfree_skb(skb);
  101. return r;
  102. }
  103. static int nxp_nci_fw_send(struct nxp_nci_info *info)
  104. {
  105. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  106. long completion_rc;
  107. int r;
  108. reinit_completion(&fw_info->cmd_completion);
  109. if (fw_info->written == 0) {
  110. fw_info->frame_size = get_unaligned_be16(fw_info->data) &
  111. NXP_NCI_FW_FRAME_LEN_MASK;
  112. fw_info->data += NXP_NCI_FW_HDR_LEN;
  113. fw_info->size -= NXP_NCI_FW_HDR_LEN;
  114. }
  115. if (fw_info->frame_size > fw_info->size)
  116. return -EMSGSIZE;
  117. r = nxp_nci_fw_send_chunk(info);
  118. if (r < 0)
  119. return r;
  120. fw_info->written += r;
  121. if (*fw_info->data == NXP_NCI_FW_CMD_RESET) {
  122. fw_info->cmd_result = 0;
  123. if (fw_info->fw)
  124. schedule_work(&fw_info->work);
  125. } else {
  126. completion_rc = wait_for_completion_interruptible_timeout(
  127. &fw_info->cmd_completion, NXP_NCI_FW_ANSWER_TIMEOUT);
  128. if (completion_rc == 0)
  129. return -ETIMEDOUT;
  130. }
  131. return 0;
  132. }
  133. void nxp_nci_fw_work(struct work_struct *work)
  134. {
  135. struct nxp_nci_info *info;
  136. struct nxp_nci_fw_info *fw_info;
  137. int r;
  138. fw_info = container_of(work, struct nxp_nci_fw_info, work);
  139. info = container_of(fw_info, struct nxp_nci_info, fw_info);
  140. mutex_lock(&info->info_lock);
  141. r = fw_info->cmd_result;
  142. if (r < 0)
  143. goto exit_work;
  144. if (fw_info->written == fw_info->frame_size) {
  145. fw_info->data += fw_info->frame_size;
  146. fw_info->size -= fw_info->frame_size;
  147. fw_info->written = 0;
  148. }
  149. if (fw_info->size > 0)
  150. r = nxp_nci_fw_send(info);
  151. exit_work:
  152. if (r < 0 || fw_info->size == 0)
  153. nxp_nci_fw_work_complete(info, r);
  154. mutex_unlock(&info->info_lock);
  155. }
  156. int nxp_nci_fw_download(struct nci_dev *ndev, const char *firmware_name)
  157. {
  158. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  159. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  160. int r;
  161. mutex_lock(&info->info_lock);
  162. if (!info->phy_ops->set_mode || !info->phy_ops->write) {
  163. r = -ENOTSUPP;
  164. goto fw_download_exit;
  165. }
  166. if (!firmware_name || firmware_name[0] == '\0') {
  167. r = -EINVAL;
  168. goto fw_download_exit;
  169. }
  170. strcpy(fw_info->name, firmware_name);
  171. r = request_firmware(&fw_info->fw, firmware_name,
  172. ndev->nfc_dev->dev.parent);
  173. if (r < 0)
  174. goto fw_download_exit;
  175. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_FW);
  176. if (r < 0) {
  177. release_firmware(fw_info->fw);
  178. goto fw_download_exit;
  179. }
  180. info->mode = NXP_NCI_MODE_FW;
  181. fw_info->data = fw_info->fw->data;
  182. fw_info->size = fw_info->fw->size;
  183. fw_info->written = 0;
  184. fw_info->frame_size = 0;
  185. fw_info->cmd_result = 0;
  186. schedule_work(&fw_info->work);
  187. fw_download_exit:
  188. mutex_unlock(&info->info_lock);
  189. return r;
  190. }
  191. static int nxp_nci_fw_read_status(u8 stat)
  192. {
  193. switch (stat) {
  194. case NXP_NCI_FW_RESULT_OK:
  195. return 0;
  196. case NXP_NCI_FW_RESULT_INVALID_ADDR:
  197. return -EINVAL;
  198. case NXP_NCI_FW_RESULT_UNKNOWN_CMD:
  199. return -EINVAL;
  200. case NXP_NCI_FW_RESULT_ABORTED_CMD:
  201. return -EMSGSIZE;
  202. case NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR:
  203. return -EADDRNOTAVAIL;
  204. case NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR:
  205. return -ENOBUFS;
  206. case NXP_NCI_FW_RESULT_MEM_BSY:
  207. return -ENOKEY;
  208. case NXP_NCI_FW_RESULT_SIGNATURE_ERROR:
  209. return -EKEYREJECTED;
  210. case NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR:
  211. return -EALREADY;
  212. case NXP_NCI_FW_RESULT_PROTOCOL_ERROR:
  213. return -EPROTO;
  214. case NXP_NCI_FW_RESULT_SFWU_DEGRADED:
  215. return -EHWPOISON;
  216. case NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK:
  217. return 0;
  218. case NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK:
  219. return 0;
  220. case NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5:
  221. return -EINVAL;
  222. default:
  223. return -EIO;
  224. }
  225. }
  226. static u16 nxp_nci_fw_check_crc(struct sk_buff *skb)
  227. {
  228. u16 crc, frame_crc;
  229. size_t len = skb->len - NXP_NCI_FW_CRC_LEN;
  230. crc = nxp_nci_fw_crc(skb->data, len);
  231. frame_crc = get_unaligned_be16(skb->data + len);
  232. return (crc ^ frame_crc);
  233. }
  234. void nxp_nci_fw_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  235. {
  236. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  237. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  238. complete(&fw_info->cmd_completion);
  239. if (skb) {
  240. if (nxp_nci_fw_check_crc(skb) != 0x00)
  241. fw_info->cmd_result = -EBADMSG;
  242. else
  243. fw_info->cmd_result = nxp_nci_fw_read_status(*(u8 *)skb_pull(skb, NXP_NCI_FW_HDR_LEN));
  244. kfree_skb(skb);
  245. } else {
  246. fw_info->cmd_result = -EIO;
  247. }
  248. if (fw_info->fw)
  249. schedule_work(&fw_info->work);
  250. }
  251. EXPORT_SYMBOL(nxp_nci_fw_recv_frame);