wl1271_boot.c 13 KB

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