boot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include "debug.h"
  26. #include "acx.h"
  27. #include "boot.h"
  28. #include "io.h"
  29. #include "event.h"
  30. #include "rx.h"
  31. #include "hw_ops.h"
  32. static int wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
  33. {
  34. u32 cpu_ctrl;
  35. int ret;
  36. /* 10.5.0 run the firmware (I) */
  37. ret = wlcore_read_reg(wl, REG_ECPU_CONTROL, &cpu_ctrl);
  38. if (ret < 0)
  39. goto out;
  40. /* 10.5.1 run the firmware (II) */
  41. cpu_ctrl |= flag;
  42. ret = wlcore_write_reg(wl, REG_ECPU_CONTROL, cpu_ctrl);
  43. out:
  44. return ret;
  45. }
  46. static int wlcore_boot_parse_fw_ver(struct wl1271 *wl,
  47. struct wl1271_static_data *static_data)
  48. {
  49. int ret;
  50. strncpy(wl->chip.fw_ver_str, static_data->fw_version,
  51. sizeof(wl->chip.fw_ver_str));
  52. /* make sure the string is NULL-terminated */
  53. wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
  54. ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
  55. &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
  56. &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
  57. &wl->chip.fw_ver[4]);
  58. if (ret != 5) {
  59. wl1271_warning("fw version incorrect value");
  60. memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
  61. ret = -EINVAL;
  62. goto out;
  63. }
  64. ret = wlcore_identify_fw(wl);
  65. if (ret < 0)
  66. goto out;
  67. out:
  68. return ret;
  69. }
  70. static int wlcore_validate_fw_ver(struct wl1271 *wl)
  71. {
  72. unsigned int *fw_ver = wl->chip.fw_ver;
  73. unsigned int *min_ver = (wl->fw_type == WL12XX_FW_TYPE_MULTI) ?
  74. wl->min_mr_fw_ver : wl->min_sr_fw_ver;
  75. char min_fw_str[32] = "";
  76. int i;
  77. /* the chip must be exactly equal */
  78. if ((min_ver[FW_VER_CHIP] != WLCORE_FW_VER_IGNORE) &&
  79. (min_ver[FW_VER_CHIP] != fw_ver[FW_VER_CHIP]))
  80. goto fail;
  81. /* the firmware type must be equal */
  82. if ((min_ver[FW_VER_IF_TYPE] != WLCORE_FW_VER_IGNORE) &&
  83. (min_ver[FW_VER_IF_TYPE] != fw_ver[FW_VER_IF_TYPE]))
  84. goto fail;
  85. /* the project number must be equal */
  86. if ((min_ver[FW_VER_SUBTYPE] != WLCORE_FW_VER_IGNORE) &&
  87. (min_ver[FW_VER_SUBTYPE] != fw_ver[FW_VER_SUBTYPE]))
  88. goto fail;
  89. /* the API version must be greater or equal */
  90. if ((min_ver[FW_VER_MAJOR] != WLCORE_FW_VER_IGNORE) &&
  91. (min_ver[FW_VER_MAJOR] > fw_ver[FW_VER_MAJOR]))
  92. goto fail;
  93. /* if the API version is equal... */
  94. if (((min_ver[FW_VER_MAJOR] == WLCORE_FW_VER_IGNORE) ||
  95. (min_ver[FW_VER_MAJOR] == fw_ver[FW_VER_MAJOR])) &&
  96. /* ...the minor must be greater or equal */
  97. ((min_ver[FW_VER_MINOR] != WLCORE_FW_VER_IGNORE) &&
  98. (min_ver[FW_VER_MINOR] > fw_ver[FW_VER_MINOR])))
  99. goto fail;
  100. return 0;
  101. fail:
  102. for (i = 0; i < NUM_FW_VER; i++)
  103. if (min_ver[i] == WLCORE_FW_VER_IGNORE)
  104. snprintf(min_fw_str, sizeof(min_fw_str),
  105. "%s*.", min_fw_str);
  106. else
  107. snprintf(min_fw_str, sizeof(min_fw_str),
  108. "%s%u.", min_fw_str, min_ver[i]);
  109. wl1271_error("Your WiFi FW version (%u.%u.%u.%u.%u) is invalid.\n"
  110. "Please use at least FW %s\n"
  111. "You can get the latest firmwares at:\n"
  112. "git://git.ti.com/wilink8-wlan/wl18xx_fw.git",
  113. fw_ver[FW_VER_CHIP], fw_ver[FW_VER_IF_TYPE],
  114. fw_ver[FW_VER_MAJOR], fw_ver[FW_VER_SUBTYPE],
  115. fw_ver[FW_VER_MINOR], min_fw_str);
  116. return -EINVAL;
  117. }
  118. static int wlcore_boot_static_data(struct wl1271 *wl)
  119. {
  120. struct wl1271_static_data *static_data;
  121. size_t len = sizeof(*static_data) + wl->static_data_priv_len;
  122. int ret;
  123. static_data = kmalloc(len, GFP_KERNEL);
  124. if (!static_data) {
  125. ret = -ENOMEM;
  126. goto out;
  127. }
  128. ret = wlcore_read(wl, wl->cmd_box_addr, static_data, len, false);
  129. if (ret < 0)
  130. goto out_free;
  131. ret = wlcore_boot_parse_fw_ver(wl, static_data);
  132. if (ret < 0)
  133. goto out_free;
  134. ret = wlcore_validate_fw_ver(wl);
  135. if (ret < 0)
  136. goto out_free;
  137. ret = wlcore_handle_static_data(wl, static_data);
  138. if (ret < 0)
  139. goto out_free;
  140. out_free:
  141. kfree(static_data);
  142. out:
  143. return ret;
  144. }
  145. static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
  146. size_t fw_data_len, u32 dest)
  147. {
  148. struct wlcore_partition_set partition;
  149. int addr, chunk_num, partition_limit;
  150. u8 *p, *chunk;
  151. int ret;
  152. /* whal_FwCtrl_LoadFwImageSm() */
  153. wl1271_debug(DEBUG_BOOT, "starting firmware upload");
  154. wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
  155. fw_data_len, CHUNK_SIZE);
  156. if ((fw_data_len % 4) != 0) {
  157. wl1271_error("firmware length not multiple of four");
  158. return -EIO;
  159. }
  160. chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
  161. if (!chunk) {
  162. wl1271_error("allocation for firmware upload chunk failed");
  163. return -ENOMEM;
  164. }
  165. memcpy(&partition, &wl->ptable[PART_DOWN], sizeof(partition));
  166. partition.mem.start = dest;
  167. ret = wlcore_set_partition(wl, &partition);
  168. if (ret < 0)
  169. goto out;
  170. /* 10.1 set partition limit and chunk num */
  171. chunk_num = 0;
  172. partition_limit = wl->ptable[PART_DOWN].mem.size;
  173. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  174. /* 10.2 update partition, if needed */
  175. addr = dest + (chunk_num + 2) * CHUNK_SIZE;
  176. if (addr > partition_limit) {
  177. addr = dest + chunk_num * CHUNK_SIZE;
  178. partition_limit = chunk_num * CHUNK_SIZE +
  179. wl->ptable[PART_DOWN].mem.size;
  180. partition.mem.start = addr;
  181. ret = wlcore_set_partition(wl, &partition);
  182. if (ret < 0)
  183. goto out;
  184. }
  185. /* 10.3 upload the chunk */
  186. addr = dest + chunk_num * CHUNK_SIZE;
  187. p = buf + chunk_num * CHUNK_SIZE;
  188. memcpy(chunk, p, CHUNK_SIZE);
  189. wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  190. p, addr);
  191. ret = wlcore_write(wl, addr, chunk, CHUNK_SIZE, false);
  192. if (ret < 0)
  193. goto out;
  194. chunk_num++;
  195. }
  196. /* 10.4 upload the last chunk */
  197. addr = dest + chunk_num * CHUNK_SIZE;
  198. p = buf + chunk_num * CHUNK_SIZE;
  199. memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
  200. wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
  201. fw_data_len % CHUNK_SIZE, p, addr);
  202. ret = wlcore_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
  203. out:
  204. kfree(chunk);
  205. return ret;
  206. }
  207. int wlcore_boot_upload_firmware(struct wl1271 *wl)
  208. {
  209. u32 chunks, addr, len;
  210. int ret = 0;
  211. u8 *fw;
  212. fw = wl->fw;
  213. chunks = be32_to_cpup((__be32 *) fw);
  214. fw += sizeof(u32);
  215. wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
  216. while (chunks--) {
  217. addr = be32_to_cpup((__be32 *) fw);
  218. fw += sizeof(u32);
  219. len = be32_to_cpup((__be32 *) fw);
  220. fw += sizeof(u32);
  221. if (len > 300000) {
  222. wl1271_info("firmware chunk too long: %u", len);
  223. return -EINVAL;
  224. }
  225. wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
  226. chunks, addr, len);
  227. ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
  228. if (ret != 0)
  229. break;
  230. fw += len;
  231. }
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(wlcore_boot_upload_firmware);
  235. int wlcore_boot_upload_nvs(struct wl1271 *wl)
  236. {
  237. struct platform_device *pdev = wl->pdev;
  238. struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
  239. const char *nvs_name = "unknown";
  240. size_t nvs_len, burst_len;
  241. int i;
  242. u32 dest_addr, val;
  243. u8 *nvs_ptr, *nvs_aligned;
  244. int ret;
  245. if (wl->nvs == NULL) {
  246. wl1271_error("NVS file is needed during boot");
  247. return -ENODEV;
  248. }
  249. if (pdev_data && pdev_data->family)
  250. nvs_name = pdev_data->family->nvs_name;
  251. if (wl->quirks & WLCORE_QUIRK_LEGACY_NVS) {
  252. struct wl1271_nvs_file *nvs =
  253. (struct wl1271_nvs_file *)wl->nvs;
  254. /*
  255. * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
  256. * band configurations) can be removed when those NVS files stop
  257. * floating around.
  258. */
  259. if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
  260. wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
  261. if (nvs->general_params.dual_mode_select)
  262. wl->enable_11a = true;
  263. }
  264. if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
  265. (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
  266. wl->enable_11a)) {
  267. wl1271_error("%s size is not as expected: %zu != %zu",
  268. nvs_name, wl->nvs_len,
  269. sizeof(struct wl1271_nvs_file));
  270. kfree(wl->nvs);
  271. wl->nvs = NULL;
  272. wl->nvs_len = 0;
  273. return -EILSEQ;
  274. }
  275. /* only the first part of the NVS needs to be uploaded */
  276. nvs_len = sizeof(nvs->nvs);
  277. nvs_ptr = (u8 *) nvs->nvs;
  278. } else {
  279. struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
  280. if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
  281. if (nvs->general_params.dual_mode_select)
  282. wl->enable_11a = true;
  283. } else {
  284. wl1271_error("%s size is not as expected: %zu != %zu",
  285. nvs_name, wl->nvs_len,
  286. sizeof(struct wl128x_nvs_file));
  287. kfree(wl->nvs);
  288. wl->nvs = NULL;
  289. wl->nvs_len = 0;
  290. return -EILSEQ;
  291. }
  292. /* only the first part of the NVS needs to be uploaded */
  293. nvs_len = sizeof(nvs->nvs);
  294. nvs_ptr = (u8 *)nvs->nvs;
  295. }
  296. /* update current MAC address to NVS */
  297. nvs_ptr[11] = wl->addresses[0].addr[0];
  298. nvs_ptr[10] = wl->addresses[0].addr[1];
  299. nvs_ptr[6] = wl->addresses[0].addr[2];
  300. nvs_ptr[5] = wl->addresses[0].addr[3];
  301. nvs_ptr[4] = wl->addresses[0].addr[4];
  302. nvs_ptr[3] = wl->addresses[0].addr[5];
  303. /*
  304. * Layout before the actual NVS tables:
  305. * 1 byte : burst length.
  306. * 2 bytes: destination address.
  307. * n bytes: data to burst copy.
  308. *
  309. * This is ended by a 0 length, then the NVS tables.
  310. */
  311. /* FIXME: Do we need to check here whether the LSB is 1? */
  312. while (nvs_ptr[0]) {
  313. burst_len = nvs_ptr[0];
  314. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  315. /*
  316. * Due to our new wl1271_translate_reg_addr function,
  317. * we need to add the register partition start address
  318. * to the destination
  319. */
  320. dest_addr += wl->curr_part.reg.start;
  321. /* We move our pointer to the data */
  322. nvs_ptr += 3;
  323. for (i = 0; i < burst_len; i++) {
  324. if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
  325. goto out_badnvs;
  326. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  327. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  328. wl1271_debug(DEBUG_BOOT,
  329. "nvs burst write 0x%x: 0x%x",
  330. dest_addr, val);
  331. ret = wlcore_write32(wl, dest_addr, val);
  332. if (ret < 0)
  333. return ret;
  334. nvs_ptr += 4;
  335. dest_addr += 4;
  336. }
  337. if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
  338. goto out_badnvs;
  339. }
  340. /*
  341. * We've reached the first zero length, the first NVS table
  342. * is located at an aligned offset which is at least 7 bytes further.
  343. * NOTE: The wl->nvs->nvs element must be first, in order to
  344. * simplify the casting, we assume it is at the beginning of
  345. * the wl->nvs structure.
  346. */
  347. nvs_ptr = (u8 *)wl->nvs +
  348. ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
  349. if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
  350. goto out_badnvs;
  351. nvs_len -= nvs_ptr - (u8 *)wl->nvs;
  352. /* Now we must set the partition correctly */
  353. ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
  354. if (ret < 0)
  355. return ret;
  356. /* Copy the NVS tables to a new block to ensure alignment */
  357. nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
  358. if (!nvs_aligned)
  359. return -ENOMEM;
  360. /* And finally we upload the NVS tables */
  361. ret = wlcore_write_data(wl, REG_CMD_MBOX_ADDRESS, nvs_aligned, nvs_len,
  362. false);
  363. kfree(nvs_aligned);
  364. return ret;
  365. out_badnvs:
  366. wl1271_error("nvs data is malformed");
  367. return -EILSEQ;
  368. }
  369. EXPORT_SYMBOL_GPL(wlcore_boot_upload_nvs);
  370. int wlcore_boot_run_firmware(struct wl1271 *wl)
  371. {
  372. int loop, ret;
  373. u32 chip_id, intr;
  374. /* Make sure we have the boot partition */
  375. ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
  376. if (ret < 0)
  377. return ret;
  378. ret = wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  379. if (ret < 0)
  380. return ret;
  381. ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &chip_id);
  382. if (ret < 0)
  383. return ret;
  384. wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  385. if (chip_id != wl->chip.id) {
  386. wl1271_error("chip id doesn't match after firmware boot");
  387. return -EIO;
  388. }
  389. /* wait for init to complete */
  390. loop = 0;
  391. while (loop++ < INIT_LOOP) {
  392. udelay(INIT_LOOP_DELAY);
  393. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  394. if (ret < 0)
  395. return ret;
  396. if (intr == 0xffffffff) {
  397. wl1271_error("error reading hardware complete "
  398. "init indication");
  399. return -EIO;
  400. }
  401. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  402. else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
  403. ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
  404. WL1271_ACX_INTR_INIT_COMPLETE);
  405. if (ret < 0)
  406. return ret;
  407. break;
  408. }
  409. }
  410. if (loop > INIT_LOOP) {
  411. wl1271_error("timeout waiting for the hardware to "
  412. "complete initialization");
  413. return -EIO;
  414. }
  415. /* get hardware config command mail box */
  416. ret = wlcore_read_reg(wl, REG_COMMAND_MAILBOX_PTR, &wl->cmd_box_addr);
  417. if (ret < 0)
  418. return ret;
  419. wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x", wl->cmd_box_addr);
  420. /* get hardware config event mail box */
  421. ret = wlcore_read_reg(wl, REG_EVENT_MAILBOX_PTR, &wl->mbox_ptr[0]);
  422. if (ret < 0)
  423. return ret;
  424. wl->mbox_ptr[1] = wl->mbox_ptr[0] + wl->mbox_size;
  425. wl1271_debug(DEBUG_MAILBOX, "MBOX ptrs: 0x%x 0x%x",
  426. wl->mbox_ptr[0], wl->mbox_ptr[1]);
  427. ret = wlcore_boot_static_data(wl);
  428. if (ret < 0) {
  429. wl1271_error("error getting static data");
  430. return ret;
  431. }
  432. /*
  433. * in case of full asynchronous mode the firmware event must be
  434. * ready to receive event from the command mailbox
  435. */
  436. /* unmask required mbox events */
  437. ret = wl1271_event_unmask(wl);
  438. if (ret < 0) {
  439. wl1271_error("EVENT mask setting failed");
  440. return ret;
  441. }
  442. /* set the working partition to its "running" mode offset */
  443. ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
  444. /* firmware startup completed */
  445. return ret;
  446. }
  447. EXPORT_SYMBOL_GPL(wlcore_boot_run_firmware);