wl1271_boot.c 14 KB

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