boot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 "acx.h"
  25. #include "reg.h"
  26. #include "boot.h"
  27. #include "io.h"
  28. #include "event.h"
  29. static struct wl1271_partition_set part_table[PART_TABLE_LEN] = {
  30. [PART_DOWN] = {
  31. .mem = {
  32. .start = 0x00000000,
  33. .size = 0x000177c0
  34. },
  35. .reg = {
  36. .start = REGISTERS_BASE,
  37. .size = 0x00008800
  38. },
  39. .mem2 = {
  40. .start = 0x00000000,
  41. .size = 0x00000000
  42. },
  43. .mem3 = {
  44. .start = 0x00000000,
  45. .size = 0x00000000
  46. },
  47. },
  48. [PART_WORK] = {
  49. .mem = {
  50. .start = 0x00040000,
  51. .size = 0x00014fc0
  52. },
  53. .reg = {
  54. .start = REGISTERS_BASE,
  55. .size = 0x0000a000
  56. },
  57. .mem2 = {
  58. .start = 0x003004f8,
  59. .size = 0x00000004
  60. },
  61. .mem3 = {
  62. .start = 0x00040404,
  63. .size = 0x00000000
  64. },
  65. },
  66. [PART_DRPW] = {
  67. .mem = {
  68. .start = 0x00040000,
  69. .size = 0x00014fc0
  70. },
  71. .reg = {
  72. .start = DRPW_BASE,
  73. .size = 0x00006000
  74. },
  75. .mem2 = {
  76. .start = 0x00000000,
  77. .size = 0x00000000
  78. },
  79. .mem3 = {
  80. .start = 0x00000000,
  81. .size = 0x00000000
  82. }
  83. }
  84. };
  85. static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
  86. {
  87. u32 cpu_ctrl;
  88. /* 10.5.0 run the firmware (I) */
  89. cpu_ctrl = wl1271_read32(wl, ACX_REG_ECPU_CONTROL);
  90. /* 10.5.1 run the firmware (II) */
  91. cpu_ctrl |= flag;
  92. wl1271_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
  93. }
  94. static void wl1271_boot_fw_version(struct wl1271 *wl)
  95. {
  96. struct wl1271_static_data static_data;
  97. wl1271_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
  98. false);
  99. strncpy(wl->chip.fw_ver, static_data.fw_version,
  100. sizeof(wl->chip.fw_ver));
  101. /* make sure the string is NULL-terminated */
  102. wl->chip.fw_ver[sizeof(wl->chip.fw_ver) - 1] = '\0';
  103. }
  104. static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
  105. size_t fw_data_len, u32 dest)
  106. {
  107. struct wl1271_partition_set partition;
  108. int addr, chunk_num, partition_limit;
  109. u8 *p, *chunk;
  110. /* whal_FwCtrl_LoadFwImageSm() */
  111. wl1271_debug(DEBUG_BOOT, "starting firmware upload");
  112. wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
  113. fw_data_len, CHUNK_SIZE);
  114. if ((fw_data_len % 4) != 0) {
  115. wl1271_error("firmware length not multiple of four");
  116. return -EIO;
  117. }
  118. chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
  119. if (!chunk) {
  120. wl1271_error("allocation for firmware upload chunk failed");
  121. return -ENOMEM;
  122. }
  123. memcpy(&partition, &part_table[PART_DOWN], sizeof(partition));
  124. partition.mem.start = dest;
  125. wl1271_set_partition(wl, &partition);
  126. /* 10.1 set partition limit and chunk num */
  127. chunk_num = 0;
  128. partition_limit = part_table[PART_DOWN].mem.size;
  129. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  130. /* 10.2 update partition, if needed */
  131. addr = dest + (chunk_num + 2) * CHUNK_SIZE;
  132. if (addr > partition_limit) {
  133. addr = dest + chunk_num * CHUNK_SIZE;
  134. partition_limit = chunk_num * CHUNK_SIZE +
  135. part_table[PART_DOWN].mem.size;
  136. partition.mem.start = addr;
  137. wl1271_set_partition(wl, &partition);
  138. }
  139. /* 10.3 upload the chunk */
  140. addr = dest + chunk_num * CHUNK_SIZE;
  141. p = buf + chunk_num * CHUNK_SIZE;
  142. memcpy(chunk, p, CHUNK_SIZE);
  143. wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  144. p, addr);
  145. wl1271_write(wl, addr, chunk, CHUNK_SIZE, false);
  146. chunk_num++;
  147. }
  148. /* 10.4 upload the last chunk */
  149. addr = dest + chunk_num * CHUNK_SIZE;
  150. p = buf + chunk_num * CHUNK_SIZE;
  151. memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
  152. wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
  153. fw_data_len % CHUNK_SIZE, p, addr);
  154. wl1271_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
  155. kfree(chunk);
  156. return 0;
  157. }
  158. static int wl1271_boot_upload_firmware(struct wl1271 *wl)
  159. {
  160. u32 chunks, addr, len;
  161. int ret = 0;
  162. u8 *fw;
  163. fw = wl->fw;
  164. chunks = be32_to_cpup((__be32 *) fw);
  165. fw += sizeof(u32);
  166. wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
  167. while (chunks--) {
  168. addr = be32_to_cpup((__be32 *) fw);
  169. fw += sizeof(u32);
  170. len = be32_to_cpup((__be32 *) fw);
  171. fw += sizeof(u32);
  172. if (len > 300000) {
  173. wl1271_info("firmware chunk too long: %u", len);
  174. return -EINVAL;
  175. }
  176. wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
  177. chunks, addr, len);
  178. ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
  179. if (ret != 0)
  180. break;
  181. fw += len;
  182. }
  183. return ret;
  184. }
  185. static int wl1271_boot_upload_nvs(struct wl1271 *wl)
  186. {
  187. size_t nvs_len, burst_len;
  188. int i;
  189. u32 dest_addr, val;
  190. u8 *nvs_ptr, *nvs_aligned;
  191. if (wl->nvs == NULL)
  192. return -ENODEV;
  193. /*
  194. * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz band
  195. * configurations) can be removed when those NVS files stop floating
  196. * around.
  197. */
  198. if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
  199. wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
  200. if (wl->nvs->general_params.dual_mode_select)
  201. wl->enable_11a = true;
  202. }
  203. if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
  204. (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
  205. wl->enable_11a)) {
  206. wl1271_error("nvs size is not as expected: %zu != %zu",
  207. wl->nvs_len, sizeof(struct wl1271_nvs_file));
  208. kfree(wl->nvs);
  209. wl->nvs = NULL;
  210. wl->nvs_len = 0;
  211. return -EILSEQ;
  212. }
  213. /* only the first part of the NVS needs to be uploaded */
  214. nvs_len = sizeof(wl->nvs->nvs);
  215. nvs_ptr = (u8 *)wl->nvs->nvs;
  216. /* update current MAC address to NVS */
  217. nvs_ptr[11] = wl->mac_addr[0];
  218. nvs_ptr[10] = wl->mac_addr[1];
  219. nvs_ptr[6] = wl->mac_addr[2];
  220. nvs_ptr[5] = wl->mac_addr[3];
  221. nvs_ptr[4] = wl->mac_addr[4];
  222. nvs_ptr[3] = wl->mac_addr[5];
  223. /*
  224. * Layout before the actual NVS tables:
  225. * 1 byte : burst length.
  226. * 2 bytes: destination address.
  227. * n bytes: data to burst copy.
  228. *
  229. * This is ended by a 0 length, then the NVS tables.
  230. */
  231. /* FIXME: Do we need to check here whether the LSB is 1? */
  232. while (nvs_ptr[0]) {
  233. burst_len = nvs_ptr[0];
  234. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  235. /*
  236. * Due to our new wl1271_translate_reg_addr function,
  237. * we need to add the REGISTER_BASE to the destination
  238. */
  239. dest_addr += REGISTERS_BASE;
  240. /* We move our pointer to the data */
  241. nvs_ptr += 3;
  242. for (i = 0; i < burst_len; i++) {
  243. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  244. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  245. wl1271_debug(DEBUG_BOOT,
  246. "nvs burst write 0x%x: 0x%x",
  247. dest_addr, val);
  248. wl1271_write32(wl, dest_addr, val);
  249. nvs_ptr += 4;
  250. dest_addr += 4;
  251. }
  252. }
  253. /*
  254. * We've reached the first zero length, the first NVS table
  255. * is located at an aligned offset which is at least 7 bytes further.
  256. */
  257. nvs_ptr = (u8 *)wl->nvs->nvs +
  258. ALIGN(nvs_ptr - (u8 *)wl->nvs->nvs + 7, 4);
  259. nvs_len -= nvs_ptr - (u8 *)wl->nvs->nvs;
  260. /* Now we must set the partition correctly */
  261. wl1271_set_partition(wl, &part_table[PART_WORK]);
  262. /* Copy the NVS tables to a new block to ensure alignment */
  263. nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
  264. if (!nvs_aligned)
  265. return -ENOMEM;
  266. /* And finally we upload the NVS tables */
  267. wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
  268. kfree(nvs_aligned);
  269. return 0;
  270. }
  271. static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
  272. {
  273. wl1271_enable_interrupts(wl);
  274. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
  275. WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
  276. wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
  277. }
  278. static int wl1271_boot_soft_reset(struct wl1271 *wl)
  279. {
  280. unsigned long timeout;
  281. u32 boot_data;
  282. /* perform soft reset */
  283. wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
  284. /* SOFT_RESET is self clearing */
  285. timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
  286. while (1) {
  287. boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
  288. wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
  289. if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
  290. break;
  291. if (time_after(jiffies, timeout)) {
  292. /* 1.2 check pWhalBus->uSelfClearTime if the
  293. * timeout was reached */
  294. wl1271_error("soft reset timeout");
  295. return -1;
  296. }
  297. udelay(SOFT_RESET_STALL_TIME);
  298. }
  299. /* disable Rx/Tx */
  300. wl1271_write32(wl, ENABLE, 0x0);
  301. /* disable auto calibration on start*/
  302. wl1271_write32(wl, SPARE_A2, 0xffff);
  303. return 0;
  304. }
  305. static int wl1271_boot_run_firmware(struct wl1271 *wl)
  306. {
  307. int loop, ret;
  308. u32 chip_id, intr;
  309. wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  310. chip_id = wl1271_read32(wl, CHIP_ID_B);
  311. wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  312. if (chip_id != wl->chip.id) {
  313. wl1271_error("chip id doesn't match after firmware boot");
  314. return -EIO;
  315. }
  316. /* wait for init to complete */
  317. loop = 0;
  318. while (loop++ < INIT_LOOP) {
  319. udelay(INIT_LOOP_DELAY);
  320. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  321. if (intr == 0xffffffff) {
  322. wl1271_error("error reading hardware complete "
  323. "init indication");
  324. return -EIO;
  325. }
  326. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  327. else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
  328. wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
  329. WL1271_ACX_INTR_INIT_COMPLETE);
  330. break;
  331. }
  332. }
  333. if (loop > INIT_LOOP) {
  334. wl1271_error("timeout waiting for the hardware to "
  335. "complete initialization");
  336. return -EIO;
  337. }
  338. /* get hardware config command mail box */
  339. wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
  340. /* get hardware config event mail box */
  341. wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
  342. /* set the working partition to its "running" mode offset */
  343. wl1271_set_partition(wl, &part_table[PART_WORK]);
  344. wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
  345. wl->cmd_box_addr, wl->event_box_addr);
  346. wl1271_boot_fw_version(wl);
  347. /*
  348. * in case of full asynchronous mode the firmware event must be
  349. * ready to receive event from the command mailbox
  350. */
  351. /* unmask required mbox events */
  352. wl->event_mask = BSS_LOSE_EVENT_ID |
  353. SCAN_COMPLETE_EVENT_ID |
  354. PS_REPORT_EVENT_ID |
  355. JOIN_EVENT_COMPLETE_ID |
  356. DISCONNECT_EVENT_COMPLETE_ID |
  357. RSSI_SNR_TRIGGER_0_EVENT_ID |
  358. PSPOLL_DELIVERY_FAILURE_EVENT_ID |
  359. SOFT_GEMINI_SENSE_EVENT_ID;
  360. ret = wl1271_event_unmask(wl);
  361. if (ret < 0) {
  362. wl1271_error("EVENT mask setting failed");
  363. return ret;
  364. }
  365. wl1271_event_mbox_config(wl);
  366. /* firmware startup completed */
  367. return 0;
  368. }
  369. static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
  370. {
  371. u32 polarity;
  372. polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
  373. /* We use HIGH polarity, so unset the LOW bit */
  374. polarity &= ~POLARITY_LOW;
  375. wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
  376. return 0;
  377. }
  378. static void wl1271_boot_hw_version(struct wl1271 *wl)
  379. {
  380. u32 fuse;
  381. fuse = wl1271_top_reg_read(wl, REG_FUSE_DATA_2_1);
  382. fuse = (fuse & PG_VER_MASK) >> PG_VER_OFFSET;
  383. wl->hw_pg_ver = (s8)fuse;
  384. }
  385. /* uploads NVS and firmware */
  386. int wl1271_load_firmware(struct wl1271 *wl)
  387. {
  388. int ret = 0;
  389. u32 tmp, clk, pause;
  390. wl1271_boot_hw_version(wl);
  391. if (wl->ref_clock == 0 || wl->ref_clock == 2 || wl->ref_clock == 4)
  392. /* ref clk: 19.2/38.4/38.4-XTAL */
  393. clk = 0x3;
  394. else if (wl->ref_clock == 1 || wl->ref_clock == 3)
  395. /* ref clk: 26/52 */
  396. clk = 0x5;
  397. else
  398. return -EINVAL;
  399. if (wl->ref_clock != 0) {
  400. u16 val;
  401. /* Set clock type (open drain) */
  402. val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
  403. val &= FREF_CLK_TYPE_BITS;
  404. wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
  405. /* Set clock pull mode (no pull) */
  406. val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
  407. val |= NO_PULL;
  408. wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
  409. } else {
  410. u16 val;
  411. /* Set clock polarity */
  412. val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
  413. val &= FREF_CLK_POLARITY_BITS;
  414. val |= CLK_REQ_OUTN_SEL;
  415. wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
  416. }
  417. wl1271_write32(wl, PLL_PARAMETERS, clk);
  418. pause = wl1271_read32(wl, PLL_PARAMETERS);
  419. wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
  420. pause &= ~(WU_COUNTER_PAUSE_VAL);
  421. pause |= WU_COUNTER_PAUSE_VAL;
  422. wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
  423. /* Continue the ELP wake up sequence */
  424. wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
  425. udelay(500);
  426. wl1271_set_partition(wl, &part_table[PART_DRPW]);
  427. /* Read-modify-write DRPW_SCRATCH_START register (see next state)
  428. to be used by DRPw FW. The RTRIM value will be added by the FW
  429. before taking DRPw out of reset */
  430. wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
  431. clk = wl1271_read32(wl, DRPW_SCRATCH_START);
  432. wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
  433. clk |= (wl->ref_clock << 1) << 4;
  434. wl1271_write32(wl, DRPW_SCRATCH_START, clk);
  435. wl1271_set_partition(wl, &part_table[PART_WORK]);
  436. /* Disable interrupts */
  437. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
  438. ret = wl1271_boot_soft_reset(wl);
  439. if (ret < 0)
  440. goto out;
  441. /* 2. start processing NVS file */
  442. ret = wl1271_boot_upload_nvs(wl);
  443. if (ret < 0)
  444. goto out;
  445. /* write firmware's last address (ie. it's length) to
  446. * ACX_EEPROMLESS_IND_REG */
  447. wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
  448. wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
  449. tmp = wl1271_read32(wl, CHIP_ID_B);
  450. wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
  451. /* 6. read the EEPROM parameters */
  452. tmp = wl1271_read32(wl, SCR_PAD2);
  453. ret = wl1271_boot_write_irq_polarity(wl);
  454. if (ret < 0)
  455. goto out;
  456. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
  457. WL1271_ACX_ALL_EVENTS_VECTOR);
  458. /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
  459. * to upload_fw) */
  460. ret = wl1271_boot_upload_firmware(wl);
  461. if (ret < 0)
  462. goto out;
  463. out:
  464. return ret;
  465. }
  466. EXPORT_SYMBOL_GPL(wl1271_load_firmware);
  467. int wl1271_boot(struct wl1271 *wl)
  468. {
  469. int ret;
  470. /* upload NVS and firmware */
  471. ret = wl1271_load_firmware(wl);
  472. if (ret)
  473. return ret;
  474. /* 10.5 start firmware */
  475. ret = wl1271_boot_run_firmware(wl);
  476. if (ret < 0)
  477. goto out;
  478. /* Enable firmware interrupts now */
  479. wl1271_boot_enable_interrupts(wl);
  480. /* set the wl1271 default filters */
  481. wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
  482. wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
  483. wl1271_event_mbox_config(wl);
  484. out:
  485. return ret;
  486. }