boot.c 12 KB

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