boot.c 14 KB

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