fw_inc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (c) 2014-2015 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* Algorithmic part of the firmware download.
  17. * To be included in the container file providing framework
  18. */
  19. #define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
  20. #define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
  21. #define wil_hex_dump_fw(prefix_str, prefix_type, rowsize, \
  22. groupsize, buf, len, ascii) \
  23. print_hex_dump_debug("DBG[ FW ]" prefix_str, \
  24. prefix_type, rowsize, \
  25. groupsize, buf, len, ascii)
  26. #define FW_ADDR_CHECK(ioaddr, val, msg) do { \
  27. ioaddr = wmi_buffer(wil, val); \
  28. if (!ioaddr) { \
  29. wil_err_fw(wil, "bad " msg ": 0x%08x\n", \
  30. le32_to_cpu(val)); \
  31. return -EINVAL; \
  32. } \
  33. } while (0)
  34. /**
  35. * wil_fw_verify - verify firmware file validity
  36. *
  37. * perform various checks for the firmware file header.
  38. * records are not validated.
  39. *
  40. * Return file size or negative error
  41. */
  42. static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
  43. {
  44. const struct wil_fw_record_head *hdr = (const void *)data;
  45. struct wil_fw_record_file_header fh;
  46. const struct wil_fw_record_file_header *fh_;
  47. u32 crc;
  48. u32 dlen;
  49. if (size % 4) {
  50. wil_err_fw(wil, "image size not aligned: %zu\n", size);
  51. return -EINVAL;
  52. }
  53. /* have enough data for the file header? */
  54. if (size < sizeof(*hdr) + sizeof(fh)) {
  55. wil_err_fw(wil, "file too short: %zu bytes\n", size);
  56. return -EINVAL;
  57. }
  58. /* start with the file header? */
  59. if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
  60. wil_err_fw(wil, "no file header\n");
  61. return -EINVAL;
  62. }
  63. /* data_len */
  64. fh_ = (struct wil_fw_record_file_header *)&hdr[1];
  65. dlen = le32_to_cpu(fh_->data_len);
  66. if (dlen % 4) {
  67. wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
  68. return -EINVAL;
  69. }
  70. if (size < dlen) {
  71. wil_err_fw(wil, "file truncated at %zu/%lu\n",
  72. size, (ulong)dlen);
  73. return -EINVAL;
  74. }
  75. if (dlen < sizeof(*hdr) + sizeof(fh)) {
  76. wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
  77. return -EINVAL;
  78. }
  79. /* signature */
  80. if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
  81. wil_err_fw(wil, "bad header signature: 0x%08x\n",
  82. le32_to_cpu(fh_->signature));
  83. return -EINVAL;
  84. }
  85. /* version */
  86. if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
  87. wil_err_fw(wil, "unsupported header version: %d\n",
  88. le32_to_cpu(fh_->version));
  89. return -EINVAL;
  90. }
  91. /* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
  92. fh = *fh_;
  93. fh.crc = 0;
  94. crc = crc32_le(~0, (unsigned char const *)hdr, sizeof(*hdr));
  95. crc = crc32_le(crc, (unsigned char const *)&fh, sizeof(fh));
  96. crc = crc32_le(crc, (unsigned char const *)&fh_[1],
  97. dlen - sizeof(*hdr) - sizeof(fh));
  98. crc = ~crc;
  99. if (crc != le32_to_cpu(fh_->crc)) {
  100. wil_err_fw(wil, "checksum mismatch:"
  101. " calculated for %lu bytes 0x%08x != 0x%08x\n",
  102. (ulong)dlen, crc, le32_to_cpu(fh_->crc));
  103. return -EINVAL;
  104. }
  105. return (int)dlen;
  106. }
  107. static int fw_handle_comment(struct wil6210_priv *wil, const void *data,
  108. size_t size)
  109. {
  110. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
  111. return 0;
  112. }
  113. static int fw_handle_data(struct wil6210_priv *wil, const void *data,
  114. size_t size)
  115. {
  116. const struct wil_fw_record_data *d = data;
  117. void __iomem *dst;
  118. size_t s = size - sizeof(*d);
  119. if (size < sizeof(*d) + sizeof(u32)) {
  120. wil_err_fw(wil, "data record too short: %zu\n", size);
  121. return -EINVAL;
  122. }
  123. FW_ADDR_CHECK(dst, d->addr, "address");
  124. wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(d->addr),
  125. s);
  126. wil_memcpy_toio_32(dst, d->data, s);
  127. wmb(); /* finish before processing next record */
  128. return 0;
  129. }
  130. static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
  131. size_t size)
  132. {
  133. const struct wil_fw_record_fill *d = data;
  134. void __iomem *dst;
  135. u32 v;
  136. size_t s = (size_t)le32_to_cpu(d->size);
  137. if (size != sizeof(*d)) {
  138. wil_err_fw(wil, "bad size for fill record: %zu\n", size);
  139. return -EINVAL;
  140. }
  141. if (s < sizeof(u32)) {
  142. wil_err_fw(wil, "fill size too short: %zu\n", s);
  143. return -EINVAL;
  144. }
  145. if (s % sizeof(u32)) {
  146. wil_err_fw(wil, "fill size not aligned: %zu\n", s);
  147. return -EINVAL;
  148. }
  149. FW_ADDR_CHECK(dst, d->addr, "address");
  150. v = le32_to_cpu(d->value);
  151. wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
  152. le32_to_cpu(d->addr), v, s);
  153. wil_memset_toio_32(dst, v, s);
  154. wmb(); /* finish before processing next record */
  155. return 0;
  156. }
  157. static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
  158. size_t size)
  159. {
  160. const struct wil_fw_record_file_header *d = data;
  161. if (size != sizeof(*d)) {
  162. wil_err_fw(wil, "file header length incorrect: %zu\n", size);
  163. return -EINVAL;
  164. }
  165. wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
  166. d->version, d->data_len);
  167. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
  168. sizeof(d->comment), true);
  169. return 0;
  170. }
  171. static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
  172. size_t size)
  173. {
  174. const struct wil_fw_record_direct_write *d = data;
  175. const struct wil_fw_data_dwrite *block = d->data;
  176. int n, i;
  177. if (size % sizeof(*block)) {
  178. wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
  179. sizeof(*block), size);
  180. return -EINVAL;
  181. }
  182. n = size / sizeof(*block);
  183. for (i = 0; i < n; i++) {
  184. void __iomem *dst;
  185. u32 m = le32_to_cpu(block[i].mask);
  186. u32 v = le32_to_cpu(block[i].value);
  187. u32 x, y;
  188. FW_ADDR_CHECK(dst, block[i].addr, "address");
  189. x = readl(dst);
  190. y = (x & m) | (v & ~m);
  191. wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
  192. "(old 0x%08x val 0x%08x mask 0x%08x)\n",
  193. le32_to_cpu(block[i].addr), y, x, v, m);
  194. writel(y, dst);
  195. wmb(); /* finish before processing next record */
  196. }
  197. return 0;
  198. }
  199. static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
  200. void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
  201. u32 a)
  202. {
  203. unsigned delay = 0;
  204. writel(a, gwa_addr);
  205. writel(gw_cmd, gwa_cmd);
  206. wmb(); /* finish before activate gw */
  207. writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
  208. do {
  209. udelay(1); /* typical time is few usec */
  210. if (delay++ > 100) {
  211. wil_err_fw(wil, "gw timeout\n");
  212. return -EINVAL;
  213. }
  214. } while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
  215. return 0;
  216. }
  217. static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
  218. size_t size)
  219. {
  220. const struct wil_fw_record_gateway_data *d = data;
  221. const struct wil_fw_data_gw *block = d->data;
  222. void __iomem *gwa_addr;
  223. void __iomem *gwa_val;
  224. void __iomem *gwa_cmd;
  225. void __iomem *gwa_ctl;
  226. u32 gw_cmd;
  227. int n, i;
  228. if (size < sizeof(*d) + sizeof(*block)) {
  229. wil_err_fw(wil, "gateway record too short: %zu\n", size);
  230. return -EINVAL;
  231. }
  232. if ((size - sizeof(*d)) % sizeof(*block)) {
  233. wil_err_fw(wil, "gateway record data size"
  234. " not aligned on %zu: %zu\n",
  235. sizeof(*block), size - sizeof(*d));
  236. return -EINVAL;
  237. }
  238. n = (size - sizeof(*d)) / sizeof(*block);
  239. gw_cmd = le32_to_cpu(d->command);
  240. wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
  241. n, gw_cmd);
  242. FW_ADDR_CHECK(gwa_addr, d->gateway_addr_addr, "gateway_addr_addr");
  243. FW_ADDR_CHECK(gwa_val, d->gateway_value_addr, "gateway_value_addr");
  244. FW_ADDR_CHECK(gwa_cmd, d->gateway_cmd_addr, "gateway_cmd_addr");
  245. FW_ADDR_CHECK(gwa_ctl, d->gateway_ctrl_address, "gateway_ctrl_address");
  246. wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
  247. " cmd 0x%08x ctl 0x%08x\n",
  248. le32_to_cpu(d->gateway_addr_addr),
  249. le32_to_cpu(d->gateway_value_addr),
  250. le32_to_cpu(d->gateway_cmd_addr),
  251. le32_to_cpu(d->gateway_ctrl_address));
  252. for (i = 0; i < n; i++) {
  253. int rc;
  254. u32 a = le32_to_cpu(block[i].addr);
  255. u32 v = le32_to_cpu(block[i].value);
  256. wil_dbg_fw(wil, " gw write[%3d] [0x%08x] <== 0x%08x\n",
  257. i, a, v);
  258. writel(v, gwa_val);
  259. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  260. if (rc)
  261. return rc;
  262. }
  263. return 0;
  264. }
  265. static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
  266. size_t size)
  267. {
  268. const struct wil_fw_record_gateway_data4 *d = data;
  269. const struct wil_fw_data_gw4 *block = d->data;
  270. void __iomem *gwa_addr;
  271. void __iomem *gwa_val[ARRAY_SIZE(block->value)];
  272. void __iomem *gwa_cmd;
  273. void __iomem *gwa_ctl;
  274. u32 gw_cmd;
  275. int n, i, k;
  276. if (size < sizeof(*d) + sizeof(*block)) {
  277. wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
  278. return -EINVAL;
  279. }
  280. if ((size - sizeof(*d)) % sizeof(*block)) {
  281. wil_err_fw(wil, "gateway4 record data size"
  282. " not aligned on %zu: %zu\n",
  283. sizeof(*block), size - sizeof(*d));
  284. return -EINVAL;
  285. }
  286. n = (size - sizeof(*d)) / sizeof(*block);
  287. gw_cmd = le32_to_cpu(d->command);
  288. wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
  289. n, gw_cmd);
  290. FW_ADDR_CHECK(gwa_addr, d->gateway_addr_addr, "gateway_addr_addr");
  291. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  292. FW_ADDR_CHECK(gwa_val[k], d->gateway_value_addr[k],
  293. "gateway_value_addr");
  294. FW_ADDR_CHECK(gwa_cmd, d->gateway_cmd_addr, "gateway_cmd_addr");
  295. FW_ADDR_CHECK(gwa_ctl, d->gateway_ctrl_address, "gateway_ctrl_address");
  296. wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
  297. le32_to_cpu(d->gateway_addr_addr),
  298. le32_to_cpu(d->gateway_cmd_addr),
  299. le32_to_cpu(d->gateway_ctrl_address));
  300. wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
  301. d->gateway_value_addr, sizeof(d->gateway_value_addr),
  302. false);
  303. for (i = 0; i < n; i++) {
  304. int rc;
  305. u32 a = le32_to_cpu(block[i].addr);
  306. u32 v[ARRAY_SIZE(block->value)];
  307. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  308. v[k] = le32_to_cpu(block[i].value[k]);
  309. wil_dbg_fw(wil, " gw4 write[%3d] [0x%08x] <==\n", i, a);
  310. wil_hex_dump_fw(" val ", DUMP_PREFIX_NONE, 16, 4, v,
  311. sizeof(v), false);
  312. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  313. writel(v[k], gwa_val[k]);
  314. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  315. if (rc)
  316. return rc;
  317. }
  318. return 0;
  319. }
  320. static const struct {
  321. int type;
  322. int (*handler)(struct wil6210_priv *wil, const void *data, size_t size);
  323. } wil_fw_handlers[] = {
  324. {wil_fw_type_comment, fw_handle_comment},
  325. {wil_fw_type_data, fw_handle_data},
  326. {wil_fw_type_fill, fw_handle_fill},
  327. /* wil_fw_type_action */
  328. /* wil_fw_type_verify */
  329. {wil_fw_type_file_header, fw_handle_file_header},
  330. {wil_fw_type_direct_write, fw_handle_direct_write},
  331. {wil_fw_type_gateway_data, fw_handle_gateway_data},
  332. {wil_fw_type_gateway_data4, fw_handle_gateway_data4},
  333. };
  334. static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
  335. const void *data, size_t size)
  336. {
  337. int i;
  338. for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++) {
  339. if (wil_fw_handlers[i].type == type)
  340. return wil_fw_handlers[i].handler(wil, data, size);
  341. }
  342. wil_err_fw(wil, "unknown record type: %d\n", type);
  343. return -EINVAL;
  344. }
  345. /**
  346. * wil_fw_load - load FW into device
  347. *
  348. * Load the FW and uCode code and data to the corresponding device
  349. * memory regions
  350. *
  351. * Return error code
  352. */
  353. static int wil_fw_load(struct wil6210_priv *wil, const void *data, size_t size)
  354. {
  355. int rc = 0;
  356. const struct wil_fw_record_head *hdr;
  357. size_t s, hdr_sz;
  358. for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
  359. if (size < sizeof(*hdr))
  360. break;
  361. hdr_sz = le32_to_cpu(hdr->size);
  362. s = sizeof(*hdr) + hdr_sz;
  363. if (s > size)
  364. break;
  365. if (hdr_sz % 4) {
  366. wil_err_fw(wil, "unaligned record size: %zu\n",
  367. hdr_sz);
  368. return -EINVAL;
  369. }
  370. rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
  371. &hdr[1], hdr_sz);
  372. if (rc)
  373. return rc;
  374. }
  375. if (size) {
  376. wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
  377. if (size >= sizeof(*hdr)) {
  378. wil_err_fw(wil, "Stop at offset %ld"
  379. " record type %d [%zd bytes]\n",
  380. (long)((const void *)hdr - data),
  381. le16_to_cpu(hdr->type), hdr_sz);
  382. }
  383. return -EINVAL;
  384. }
  385. return rc;
  386. }
  387. /**
  388. * wil_request_firmware - Request firmware and load to device
  389. *
  390. * Request firmware image from the file and load it to device
  391. *
  392. * Return error code
  393. */
  394. int wil_request_firmware(struct wil6210_priv *wil, const char *name)
  395. {
  396. int rc, rc1;
  397. const struct firmware *fw;
  398. size_t sz;
  399. const void *d;
  400. rc = request_firmware(&fw, name, wil_to_dev(wil));
  401. if (rc) {
  402. wil_err_fw(wil, "Failed to load firmware %s\n", name);
  403. return rc;
  404. }
  405. wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
  406. for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
  407. rc1 = wil_fw_verify(wil, d, sz);
  408. if (rc1 < 0) {
  409. rc = rc1;
  410. goto out;
  411. }
  412. rc = wil_fw_load(wil, d, rc1);
  413. if (rc < 0)
  414. goto out;
  415. }
  416. out:
  417. release_firmware(fw);
  418. return rc;
  419. }