fwio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Firmware loading.
  4. *
  5. * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
  6. * Copyright (c) 2010, ST-Ericsson
  7. */
  8. #include <linux/firmware.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/bitfield.h>
  12. #include "fwio.h"
  13. #include "wfx.h"
  14. #include "hwio.h"
  15. /* Addresses below are in SRAM area */
  16. #define WFX_DNLD_FIFO 0x09004000
  17. #define DNLD_BLOCK_SIZE 0x0400
  18. #define DNLD_FIFO_SIZE 0x8000 /* (32 * DNLD_BLOCK_SIZE) */
  19. /* Download Control Area (DCA) */
  20. #define WFX_DCA_IMAGE_SIZE 0x0900C000
  21. #define WFX_DCA_PUT 0x0900C004
  22. #define WFX_DCA_GET 0x0900C008
  23. #define WFX_DCA_HOST_STATUS 0x0900C00C
  24. #define HOST_READY 0x87654321
  25. #define HOST_INFO_READ 0xA753BD99
  26. #define HOST_UPLOAD_PENDING 0xABCDDCBA
  27. #define HOST_UPLOAD_COMPLETE 0xD4C64A99
  28. #define HOST_OK_TO_JUMP 0x174FC882
  29. #define WFX_DCA_NCP_STATUS 0x0900C010
  30. #define NCP_NOT_READY 0x12345678
  31. #define NCP_READY 0x87654321
  32. #define NCP_INFO_READY 0xBD53EF99
  33. #define NCP_DOWNLOAD_PENDING 0xABCDDCBA
  34. #define NCP_DOWNLOAD_COMPLETE 0xCAFEFECA
  35. #define NCP_AUTH_OK 0xD4C64A99
  36. #define NCP_AUTH_FAIL 0x174FC882
  37. #define NCP_PUB_KEY_RDY 0x7AB41D19
  38. #define WFX_DCA_FW_SIGNATURE 0x0900C014
  39. #define FW_SIGNATURE_SIZE 0x40
  40. #define WFX_DCA_FW_HASH 0x0900C054
  41. #define FW_HASH_SIZE 0x08
  42. #define WFX_DCA_FW_VERSION 0x0900C05C
  43. #define FW_VERSION_SIZE 0x04
  44. #define WFX_DCA_RESERVED 0x0900C060
  45. #define DCA_RESERVED_SIZE 0x20
  46. #define WFX_STATUS_INFO 0x0900C080
  47. #define WFX_BOOTLOADER_LABEL 0x0900C084
  48. #define BOOTLOADER_LABEL_SIZE 0x3C
  49. #define WFX_PTE_INFO 0x0900C0C0
  50. #define PTE_INFO_KEYSET_IDX 0x0D
  51. #define PTE_INFO_SIZE 0x10
  52. #define WFX_ERR_INFO 0x0900C0D0
  53. #define ERR_INVALID_SEC_TYPE 0x05
  54. #define ERR_SIG_VERIF_FAILED 0x0F
  55. #define ERR_AES_CTRL_KEY 0x10
  56. #define ERR_ECC_PUB_KEY 0x11
  57. #define ERR_MAC_KEY 0x18
  58. #define DCA_TIMEOUT 50 /* milliseconds */
  59. #define WAKEUP_TIMEOUT 200 /* milliseconds */
  60. static const char * const fwio_errors[] = {
  61. [ERR_INVALID_SEC_TYPE] = "Invalid section type or wrong encryption",
  62. [ERR_SIG_VERIF_FAILED] = "Signature verification failed",
  63. [ERR_AES_CTRL_KEY] = "AES control key not initialized",
  64. [ERR_ECC_PUB_KEY] = "ECC public key not initialized",
  65. [ERR_MAC_KEY] = "MAC key not initialized",
  66. };
  67. /* request_firmware() allocate data using vmalloc(). It is not compatible with underlying hardware
  68. * that use DMA. Function below detect this case and allocate a bounce buffer if necessary.
  69. *
  70. * Notice that, in doubt, you can enable CONFIG_DEBUG_SG to ask kernel to detect this problem at
  71. * runtime (else, kernel silently fail).
  72. *
  73. * NOTE: it may also be possible to use 'pages' from struct firmware and avoid bounce buffer
  74. */
  75. static int wfx_sram_write_dma_safe(struct wfx_dev *wdev, u32 addr, const u8 *buf, size_t len)
  76. {
  77. int ret;
  78. const u8 *tmp;
  79. if (!virt_addr_valid(buf)) {
  80. tmp = kmemdup(buf, len, GFP_KERNEL);
  81. if (!tmp)
  82. return -ENOMEM;
  83. } else {
  84. tmp = buf;
  85. }
  86. ret = wfx_sram_buf_write(wdev, addr, tmp, len);
  87. if (tmp != buf)
  88. kfree(tmp);
  89. return ret;
  90. }
  91. static int get_firmware(struct wfx_dev *wdev, u32 keyset_chip,
  92. const struct firmware **fw, int *file_offset)
  93. {
  94. int keyset_file;
  95. char filename[256];
  96. const char *data;
  97. int ret;
  98. snprintf(filename, sizeof(filename), "%s_%02X.sec",
  99. wdev->pdata.file_fw, keyset_chip);
  100. ret = firmware_request_nowarn(fw, filename, wdev->dev);
  101. if (ret) {
  102. dev_info(wdev->dev, "can't load %s, falling back to %s.sec\n",
  103. filename, wdev->pdata.file_fw);
  104. snprintf(filename, sizeof(filename), "%s.sec", wdev->pdata.file_fw);
  105. ret = request_firmware(fw, filename, wdev->dev);
  106. if (ret) {
  107. dev_err(wdev->dev, "can't load %s\n", filename);
  108. *fw = NULL;
  109. return ret;
  110. }
  111. }
  112. data = (*fw)->data;
  113. if (memcmp(data, "KEYSET", 6) != 0) {
  114. /* Legacy firmware format */
  115. *file_offset = 0;
  116. keyset_file = 0x90;
  117. } else {
  118. *file_offset = 8;
  119. keyset_file = (hex_to_bin(data[6]) * 16) | hex_to_bin(data[7]);
  120. if (keyset_file < 0) {
  121. dev_err(wdev->dev, "%s corrupted\n", filename);
  122. release_firmware(*fw);
  123. *fw = NULL;
  124. return -EINVAL;
  125. }
  126. }
  127. if (keyset_file != keyset_chip) {
  128. dev_err(wdev->dev, "firmware keyset is incompatible with chip (file: 0x%02X, chip: 0x%02X)\n",
  129. keyset_file, keyset_chip);
  130. release_firmware(*fw);
  131. *fw = NULL;
  132. return -ENODEV;
  133. }
  134. wdev->keyset = keyset_file;
  135. return 0;
  136. }
  137. static int wait_ncp_status(struct wfx_dev *wdev, u32 status)
  138. {
  139. ktime_t now, start;
  140. u32 reg;
  141. int ret;
  142. start = ktime_get();
  143. for (;;) {
  144. ret = wfx_sram_reg_read(wdev, WFX_DCA_NCP_STATUS, &reg);
  145. if (ret < 0)
  146. return -EIO;
  147. now = ktime_get();
  148. if (reg == status)
  149. break;
  150. if (ktime_after(now, ktime_add_ms(start, DCA_TIMEOUT)))
  151. return -ETIMEDOUT;
  152. }
  153. if (ktime_compare(now, start))
  154. dev_dbg(wdev->dev, "chip answer after %lldus\n", ktime_us_delta(now, start));
  155. else
  156. dev_dbg(wdev->dev, "chip answer immediately\n");
  157. return 0;
  158. }
  159. static int upload_firmware(struct wfx_dev *wdev, const u8 *data, size_t len)
  160. {
  161. int ret;
  162. u32 offs, bytes_done = 0;
  163. ktime_t now, start;
  164. if (len % DNLD_BLOCK_SIZE) {
  165. dev_err(wdev->dev, "firmware size is not aligned. Buffer overrun will occur\n");
  166. return -EIO;
  167. }
  168. offs = 0;
  169. while (offs < len) {
  170. start = ktime_get();
  171. for (;;) {
  172. now = ktime_get();
  173. if (offs + DNLD_BLOCK_SIZE - bytes_done < DNLD_FIFO_SIZE)
  174. break;
  175. if (ktime_after(now, ktime_add_ms(start, DCA_TIMEOUT)))
  176. return -ETIMEDOUT;
  177. ret = wfx_sram_reg_read(wdev, WFX_DCA_GET, &bytes_done);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. if (ktime_compare(now, start))
  182. dev_dbg(wdev->dev, "answer after %lldus\n", ktime_us_delta(now, start));
  183. ret = wfx_sram_write_dma_safe(wdev, WFX_DNLD_FIFO + (offs % DNLD_FIFO_SIZE),
  184. data + offs, DNLD_BLOCK_SIZE);
  185. if (ret < 0)
  186. return ret;
  187. /* The device seems to not support writing 0 in this register during first loop */
  188. offs += DNLD_BLOCK_SIZE;
  189. ret = wfx_sram_reg_write(wdev, WFX_DCA_PUT, offs);
  190. if (ret < 0)
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static void print_boot_status(struct wfx_dev *wdev)
  196. {
  197. u32 reg;
  198. wfx_sram_reg_read(wdev, WFX_STATUS_INFO, &reg);
  199. if (reg == 0x12345678)
  200. return;
  201. wfx_sram_reg_read(wdev, WFX_ERR_INFO, &reg);
  202. if (reg < ARRAY_SIZE(fwio_errors) && fwio_errors[reg])
  203. dev_info(wdev->dev, "secure boot: %s\n", fwio_errors[reg]);
  204. else
  205. dev_info(wdev->dev, "secure boot: Error %#02x\n", reg);
  206. }
  207. static int load_firmware_secure(struct wfx_dev *wdev)
  208. {
  209. const struct firmware *fw = NULL;
  210. int header_size;
  211. int fw_offset;
  212. ktime_t start;
  213. u8 *buf;
  214. int ret;
  215. BUILD_BUG_ON(PTE_INFO_SIZE > BOOTLOADER_LABEL_SIZE);
  216. buf = kmalloc(BOOTLOADER_LABEL_SIZE + 1, GFP_KERNEL);
  217. if (!buf)
  218. return -ENOMEM;
  219. wfx_sram_reg_write(wdev, WFX_DCA_HOST_STATUS, HOST_READY);
  220. ret = wait_ncp_status(wdev, NCP_INFO_READY);
  221. if (ret)
  222. goto error;
  223. wfx_sram_buf_read(wdev, WFX_BOOTLOADER_LABEL, buf, BOOTLOADER_LABEL_SIZE);
  224. buf[BOOTLOADER_LABEL_SIZE] = 0;
  225. dev_dbg(wdev->dev, "bootloader: \"%s\"\n", buf);
  226. wfx_sram_buf_read(wdev, WFX_PTE_INFO, buf, PTE_INFO_SIZE);
  227. ret = get_firmware(wdev, buf[PTE_INFO_KEYSET_IDX], &fw, &fw_offset);
  228. if (ret)
  229. goto error;
  230. header_size = fw_offset + FW_SIGNATURE_SIZE + FW_HASH_SIZE;
  231. wfx_sram_reg_write(wdev, WFX_DCA_HOST_STATUS, HOST_INFO_READ);
  232. ret = wait_ncp_status(wdev, NCP_READY);
  233. if (ret)
  234. goto error;
  235. wfx_sram_reg_write(wdev, WFX_DNLD_FIFO, 0xFFFFFFFF); /* Fifo init */
  236. wfx_sram_write_dma_safe(wdev, WFX_DCA_FW_VERSION, "\x01\x00\x00\x00", FW_VERSION_SIZE);
  237. wfx_sram_write_dma_safe(wdev, WFX_DCA_FW_SIGNATURE, fw->data + fw_offset,
  238. FW_SIGNATURE_SIZE);
  239. wfx_sram_write_dma_safe(wdev, WFX_DCA_FW_HASH, fw->data + fw_offset + FW_SIGNATURE_SIZE,
  240. FW_HASH_SIZE);
  241. wfx_sram_reg_write(wdev, WFX_DCA_IMAGE_SIZE, fw->size - header_size);
  242. wfx_sram_reg_write(wdev, WFX_DCA_HOST_STATUS, HOST_UPLOAD_PENDING);
  243. ret = wait_ncp_status(wdev, NCP_DOWNLOAD_PENDING);
  244. if (ret)
  245. goto error;
  246. start = ktime_get();
  247. ret = upload_firmware(wdev, fw->data + header_size, fw->size - header_size);
  248. if (ret)
  249. goto error;
  250. dev_dbg(wdev->dev, "firmware load after %lldus\n",
  251. ktime_us_delta(ktime_get(), start));
  252. wfx_sram_reg_write(wdev, WFX_DCA_HOST_STATUS, HOST_UPLOAD_COMPLETE);
  253. ret = wait_ncp_status(wdev, NCP_AUTH_OK);
  254. /* Legacy ROM support */
  255. if (ret < 0)
  256. ret = wait_ncp_status(wdev, NCP_PUB_KEY_RDY);
  257. if (ret < 0)
  258. goto error;
  259. wfx_sram_reg_write(wdev, WFX_DCA_HOST_STATUS, HOST_OK_TO_JUMP);
  260. error:
  261. kfree(buf);
  262. release_firmware(fw);
  263. if (ret)
  264. print_boot_status(wdev);
  265. return ret;
  266. }
  267. static int init_gpr(struct wfx_dev *wdev)
  268. {
  269. int ret, i;
  270. static const struct {
  271. int index;
  272. u32 value;
  273. } gpr_init[] = {
  274. { 0x07, 0x208775 },
  275. { 0x08, 0x2EC020 },
  276. { 0x09, 0x3C3C3C },
  277. { 0x0B, 0x322C44 },
  278. { 0x0C, 0xA06497 },
  279. };
  280. for (i = 0; i < ARRAY_SIZE(gpr_init); i++) {
  281. ret = wfx_igpr_reg_write(wdev, gpr_init[i].index, gpr_init[i].value);
  282. if (ret < 0)
  283. return ret;
  284. dev_dbg(wdev->dev, " index %02x: %08x\n", gpr_init[i].index, gpr_init[i].value);
  285. }
  286. return 0;
  287. }
  288. int wfx_init_device(struct wfx_dev *wdev)
  289. {
  290. int ret;
  291. int hw_revision, hw_type;
  292. int wakeup_timeout = 50; /* ms */
  293. ktime_t now, start;
  294. u32 reg;
  295. reg = CFG_DIRECT_ACCESS_MODE | CFG_CPU_RESET | CFG_BYTE_ORDER_ABCD;
  296. if (wdev->pdata.use_rising_clk)
  297. reg |= CFG_CLK_RISE_EDGE;
  298. ret = wfx_config_reg_write(wdev, reg);
  299. if (ret < 0) {
  300. dev_err(wdev->dev, "bus returned an error during first write access. Host configuration error?\n");
  301. return -EIO;
  302. }
  303. ret = wfx_config_reg_read(wdev, &reg);
  304. if (ret < 0) {
  305. dev_err(wdev->dev, "bus returned an error during first read access. Bus configuration error?\n");
  306. return -EIO;
  307. }
  308. if (reg == 0 || reg == ~0) {
  309. dev_err(wdev->dev, "chip mute. Bus configuration error or chip wasn't reset?\n");
  310. return -EIO;
  311. }
  312. dev_dbg(wdev->dev, "initial config register value: %08x\n", reg);
  313. hw_revision = FIELD_GET(CFG_DEVICE_ID_MAJOR, reg);
  314. if (hw_revision == 0) {
  315. dev_err(wdev->dev, "bad hardware revision number: %d\n", hw_revision);
  316. return -ENODEV;
  317. }
  318. hw_type = FIELD_GET(CFG_DEVICE_ID_TYPE, reg);
  319. if (hw_type == 1) {
  320. dev_notice(wdev->dev, "development hardware detected\n");
  321. wakeup_timeout = 2000;
  322. }
  323. ret = init_gpr(wdev);
  324. if (ret < 0)
  325. return ret;
  326. ret = wfx_control_reg_write(wdev, CTRL_WLAN_WAKEUP);
  327. if (ret < 0)
  328. return -EIO;
  329. start = ktime_get();
  330. for (;;) {
  331. ret = wfx_control_reg_read(wdev, &reg);
  332. now = ktime_get();
  333. if (reg & CTRL_WLAN_READY)
  334. break;
  335. if (ktime_after(now, ktime_add_ms(start, wakeup_timeout))) {
  336. dev_err(wdev->dev, "chip didn't wake up. Chip wasn't reset?\n");
  337. return -ETIMEDOUT;
  338. }
  339. }
  340. dev_dbg(wdev->dev, "chip wake up after %lldus\n", ktime_us_delta(now, start));
  341. ret = wfx_config_reg_write_bits(wdev, CFG_CPU_RESET, 0);
  342. if (ret < 0)
  343. return ret;
  344. ret = load_firmware_secure(wdev);
  345. if (ret < 0)
  346. return ret;
  347. return wfx_config_reg_write_bits(wdev,
  348. CFG_DIRECT_ACCESS_MODE |
  349. CFG_IRQ_ENABLE_DATA |
  350. CFG_IRQ_ENABLE_WRDY,
  351. CFG_IRQ_ENABLE_DATA);
  352. }