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