boot.c 11 KB

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