wl1271_boot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 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 "wl1271_acx.h"
  25. #include "wl1271_reg.h"
  26. #include "wl1271_boot.h"
  27. #include "wl1271_spi.h"
  28. #include "wl1271_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_spi_read32(wl, ACX_REG_ECPU_CONTROL);
  90. /* 10.5.1 run the firmware (II) */
  91. cpu_ctrl |= flag;
  92. wl1271_spi_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_spi_read(wl, wl->cmd_box_addr,
  98. &static_data, sizeof(static_data), 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_spi_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_spi_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, *nvs_aligned;
  191. nvs = wl->nvs;
  192. if (nvs == NULL)
  193. return -ENODEV;
  194. nvs_ptr = nvs;
  195. nvs_len = wl->nvs_len;
  196. /* Update the device MAC address into the nvs */
  197. nvs[11] = wl->mac_addr[0];
  198. nvs[10] = wl->mac_addr[1];
  199. nvs[6] = wl->mac_addr[2];
  200. nvs[5] = wl->mac_addr[3];
  201. nvs[4] = wl->mac_addr[4];
  202. nvs[3] = wl->mac_addr[5];
  203. /*
  204. * Layout before the actual NVS tables:
  205. * 1 byte : burst length.
  206. * 2 bytes: destination address.
  207. * n bytes: data to burst copy.
  208. *
  209. * This is ended by a 0 length, then the NVS tables.
  210. */
  211. /* FIXME: Do we need to check here whether the LSB is 1? */
  212. while (nvs_ptr[0]) {
  213. burst_len = nvs_ptr[0];
  214. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  215. /* FIXME: Due to our new wl1271_translate_reg_addr function,
  216. we need to add the REGISTER_BASE to the destination */
  217. dest_addr += REGISTERS_BASE;
  218. /* We move our pointer to the data */
  219. nvs_ptr += 3;
  220. for (i = 0; i < burst_len; i++) {
  221. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  222. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  223. wl1271_debug(DEBUG_BOOT,
  224. "nvs burst write 0x%x: 0x%x",
  225. dest_addr, val);
  226. wl1271_spi_write32(wl, dest_addr, val);
  227. nvs_ptr += 4;
  228. dest_addr += 4;
  229. }
  230. }
  231. /*
  232. * We've reached the first zero length, the first NVS table
  233. * is 7 bytes further.
  234. */
  235. nvs_ptr += 7;
  236. nvs_len -= nvs_ptr - nvs;
  237. nvs_len = ALIGN(nvs_len, 4);
  238. /* FIXME: The driver sets the partition here, but this is not needed,
  239. since it sets to the same one as currently in use */
  240. /* Now we must set the partition correctly */
  241. wl1271_set_partition(wl, &part_table[PART_WORK]);
  242. /* Copy the NVS tables to a new block to ensure alignment */
  243. nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
  244. if (!nvs_aligned)
  245. return -ENOMEM;
  246. /* And finally we upload the NVS tables */
  247. /* FIXME: In wl1271, we upload everything at once.
  248. No endianness handling needed here?! The ref driver doesn't do
  249. anything about it at this point */
  250. wl1271_spi_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
  251. kfree(nvs_aligned);
  252. return 0;
  253. }
  254. static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
  255. {
  256. enable_irq(wl->irq);
  257. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
  258. WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
  259. wl1271_spi_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
  260. }
  261. static int wl1271_boot_soft_reset(struct wl1271 *wl)
  262. {
  263. unsigned long timeout;
  264. u32 boot_data;
  265. /* perform soft reset */
  266. wl1271_spi_write32(wl, ACX_REG_SLV_SOFT_RESET,
  267. ACX_SLV_SOFT_RESET_BIT);
  268. /* SOFT_RESET is self clearing */
  269. timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
  270. while (1) {
  271. boot_data = wl1271_spi_read32(wl, ACX_REG_SLV_SOFT_RESET);
  272. wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
  273. if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
  274. break;
  275. if (time_after(jiffies, timeout)) {
  276. /* 1.2 check pWhalBus->uSelfClearTime if the
  277. * timeout was reached */
  278. wl1271_error("soft reset timeout");
  279. return -1;
  280. }
  281. udelay(SOFT_RESET_STALL_TIME);
  282. }
  283. /* disable Rx/Tx */
  284. wl1271_spi_write32(wl, ENABLE, 0x0);
  285. /* disable auto calibration on start*/
  286. wl1271_spi_write32(wl, SPARE_A2, 0xffff);
  287. return 0;
  288. }
  289. static int wl1271_boot_run_firmware(struct wl1271 *wl)
  290. {
  291. int loop, ret;
  292. u32 chip_id, interrupt;
  293. wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  294. chip_id = wl1271_spi_read32(wl, CHIP_ID_B);
  295. wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  296. if (chip_id != wl->chip.id) {
  297. wl1271_error("chip id doesn't match after firmware boot");
  298. return -EIO;
  299. }
  300. /* wait for init to complete */
  301. loop = 0;
  302. while (loop++ < INIT_LOOP) {
  303. udelay(INIT_LOOP_DELAY);
  304. interrupt = wl1271_spi_read32(wl,
  305. ACX_REG_INTERRUPT_NO_CLEAR);
  306. if (interrupt == 0xffffffff) {
  307. wl1271_error("error reading hardware complete "
  308. "init indication");
  309. return -EIO;
  310. }
  311. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  312. else if (interrupt & WL1271_ACX_INTR_INIT_COMPLETE) {
  313. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
  314. WL1271_ACX_INTR_INIT_COMPLETE);
  315. break;
  316. }
  317. }
  318. if (loop > INIT_LOOP) {
  319. wl1271_error("timeout waiting for the hardware to "
  320. "complete initialization");
  321. return -EIO;
  322. }
  323. /* get hardware config command mail box */
  324. wl->cmd_box_addr = wl1271_spi_read32(wl, REG_COMMAND_MAILBOX_PTR);
  325. /* get hardware config event mail box */
  326. wl->event_box_addr = wl1271_spi_read32(wl, REG_EVENT_MAILBOX_PTR);
  327. /* set the working partition to its "running" mode offset */
  328. wl1271_set_partition(wl, &part_table[PART_WORK]);
  329. wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
  330. wl->cmd_box_addr, wl->event_box_addr);
  331. wl1271_boot_fw_version(wl);
  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. SCAN_COMPLETE_EVENT_ID |
  339. PS_REPORT_EVENT_ID;
  340. ret = wl1271_event_unmask(wl);
  341. if (ret < 0) {
  342. wl1271_error("EVENT mask setting failed");
  343. return ret;
  344. }
  345. wl1271_event_mbox_config(wl);
  346. /* firmware startup completed */
  347. return 0;
  348. }
  349. static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
  350. {
  351. u32 polarity;
  352. polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
  353. /* We use HIGH polarity, so unset the LOW bit */
  354. polarity &= ~POLARITY_LOW;
  355. wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
  356. return 0;
  357. }
  358. int wl1271_boot(struct wl1271 *wl)
  359. {
  360. int ret = 0;
  361. u32 tmp, clk, pause;
  362. if (REF_CLOCK == 0 || REF_CLOCK == 2 || REF_CLOCK == 4)
  363. /* ref clk: 19.2/38.4/38.4-XTAL */
  364. clk = 0x3;
  365. else if (REF_CLOCK == 1 || REF_CLOCK == 3)
  366. /* ref clk: 26/52 */
  367. clk = 0x5;
  368. if (REF_CLOCK != 0) {
  369. u16 val;
  370. /* Set clock type */
  371. val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
  372. val &= FREF_CLK_TYPE_BITS;
  373. val |= CLK_REQ_PRCM;
  374. wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
  375. } else {
  376. u16 val;
  377. /* Set clock polarity */
  378. val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
  379. val &= FREF_CLK_POLARITY_BITS;
  380. val |= CLK_REQ_OUTN_SEL;
  381. wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
  382. }
  383. wl1271_spi_write32(wl, PLL_PARAMETERS, clk);
  384. pause = wl1271_spi_read32(wl, PLL_PARAMETERS);
  385. wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
  386. pause &= ~(WU_COUNTER_PAUSE_VAL); /* FIXME: This should probably be
  387. * WU_COUNTER_PAUSE_VAL instead of
  388. * 0x3ff (magic number ). How does
  389. * this work?! */
  390. pause |= WU_COUNTER_PAUSE_VAL;
  391. wl1271_spi_write32(wl, WU_COUNTER_PAUSE, pause);
  392. /* Continue the ELP wake up sequence */
  393. wl1271_spi_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
  394. udelay(500);
  395. wl1271_set_partition(wl, &part_table[PART_DRPW]);
  396. /* Read-modify-write DRPW_SCRATCH_START register (see next state)
  397. to be used by DRPw FW. The RTRIM value will be added by the FW
  398. before taking DRPw out of reset */
  399. wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
  400. clk = wl1271_spi_read32(wl, DRPW_SCRATCH_START);
  401. wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
  402. /* 2 */
  403. clk |= (REF_CLOCK << 1) << 4;
  404. wl1271_spi_write32(wl, DRPW_SCRATCH_START, clk);
  405. wl1271_set_partition(wl, &part_table[PART_WORK]);
  406. /* Disable interrupts */
  407. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
  408. ret = wl1271_boot_soft_reset(wl);
  409. if (ret < 0)
  410. goto out;
  411. /* 2. start processing NVS file */
  412. ret = wl1271_boot_upload_nvs(wl);
  413. if (ret < 0)
  414. goto out;
  415. /* write firmware's last address (ie. it's length) to
  416. * ACX_EEPROMLESS_IND_REG */
  417. wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
  418. wl1271_spi_write32(wl, ACX_EEPROMLESS_IND_REG,
  419. ACX_EEPROMLESS_IND_REG);
  420. tmp = wl1271_spi_read32(wl, CHIP_ID_B);
  421. wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
  422. /* 6. read the EEPROM parameters */
  423. tmp = wl1271_spi_read32(wl, SCR_PAD2);
  424. ret = wl1271_boot_write_irq_polarity(wl);
  425. if (ret < 0)
  426. goto out;
  427. /* FIXME: Need to check whether this is really what we want */
  428. wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
  429. WL1271_ACX_ALL_EVENTS_VECTOR);
  430. /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
  431. * to upload_fw) */
  432. ret = wl1271_boot_upload_firmware(wl);
  433. if (ret < 0)
  434. goto out;
  435. /* 10.5 start firmware */
  436. ret = wl1271_boot_run_firmware(wl);
  437. if (ret < 0)
  438. goto out;
  439. /* Enable firmware interrupts now */
  440. wl1271_boot_enable_interrupts(wl);
  441. /* set the wl1271 default filters */
  442. wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
  443. wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
  444. wl1271_event_mbox_config(wl);
  445. out:
  446. return ret;
  447. }