wl1271_boot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. },
  40. [PART_WORK] = {
  41. .mem = {
  42. .start = 0x00040000,
  43. .size = 0x00014fc0
  44. },
  45. .reg = {
  46. .start = REGISTERS_BASE,
  47. .size = 0x0000b000
  48. },
  49. },
  50. [PART_DRPW] = {
  51. .mem = {
  52. .start = 0x00040000,
  53. .size = 0x00014fc0
  54. },
  55. .reg = {
  56. .start = DRPW_BASE,
  57. .size = 0x00006000
  58. }
  59. }
  60. };
  61. static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
  62. {
  63. u32 cpu_ctrl;
  64. /* 10.5.0 run the firmware (I) */
  65. cpu_ctrl = wl1271_reg_read32(wl, ACX_REG_ECPU_CONTROL);
  66. /* 10.5.1 run the firmware (II) */
  67. cpu_ctrl |= flag;
  68. wl1271_reg_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
  69. }
  70. static void wl1271_boot_fw_version(struct wl1271 *wl)
  71. {
  72. struct wl1271_static_data static_data;
  73. wl1271_spi_mem_read(wl, wl->cmd_box_addr,
  74. &static_data, sizeof(static_data));
  75. strncpy(wl->chip.fw_ver, static_data.fw_version,
  76. sizeof(wl->chip.fw_ver));
  77. /* make sure the string is NULL-terminated */
  78. wl->chip.fw_ver[sizeof(wl->chip.fw_ver) - 1] = '\0';
  79. }
  80. static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
  81. size_t fw_data_len, u32 dest)
  82. {
  83. int addr, chunk_num, partition_limit;
  84. u8 *p;
  85. /* whal_FwCtrl_LoadFwImageSm() */
  86. wl1271_debug(DEBUG_BOOT, "starting firmware upload");
  87. wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
  88. fw_data_len, CHUNK_SIZE);
  89. if ((fw_data_len % 4) != 0) {
  90. wl1271_error("firmware length not multiple of four");
  91. return -EIO;
  92. }
  93. wl1271_set_partition(wl, dest,
  94. part_table[PART_DOWN].mem.size,
  95. part_table[PART_DOWN].reg.start,
  96. part_table[PART_DOWN].reg.size);
  97. /* 10.1 set partition limit and chunk num */
  98. chunk_num = 0;
  99. partition_limit = part_table[PART_DOWN].mem.size;
  100. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  101. /* 10.2 update partition, if needed */
  102. addr = dest + (chunk_num + 2) * CHUNK_SIZE;
  103. if (addr > partition_limit) {
  104. addr = dest + chunk_num * CHUNK_SIZE;
  105. partition_limit = chunk_num * CHUNK_SIZE +
  106. part_table[PART_DOWN].mem.size;
  107. /* FIXME: Over 80 chars! */
  108. wl1271_set_partition(wl,
  109. addr,
  110. part_table[PART_DOWN].mem.size,
  111. part_table[PART_DOWN].reg.start,
  112. part_table[PART_DOWN].reg.size);
  113. }
  114. /* 10.3 upload the chunk */
  115. addr = dest + chunk_num * CHUNK_SIZE;
  116. p = buf + chunk_num * CHUNK_SIZE;
  117. wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  118. p, addr);
  119. wl1271_spi_mem_write(wl, addr, p, CHUNK_SIZE);
  120. chunk_num++;
  121. }
  122. /* 10.4 upload the last chunk */
  123. addr = dest + chunk_num * CHUNK_SIZE;
  124. p = buf + chunk_num * CHUNK_SIZE;
  125. wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
  126. fw_data_len % CHUNK_SIZE, p, addr);
  127. wl1271_spi_mem_write(wl, addr, p, fw_data_len % CHUNK_SIZE);
  128. return 0;
  129. }
  130. static int wl1271_boot_upload_firmware(struct wl1271 *wl)
  131. {
  132. u32 chunks, addr, len;
  133. u8 *fw;
  134. fw = wl->fw;
  135. chunks = be32_to_cpup((u32 *) fw);
  136. fw += sizeof(u32);
  137. wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
  138. while (chunks--) {
  139. addr = be32_to_cpup((u32 *) fw);
  140. fw += sizeof(u32);
  141. len = be32_to_cpup((u32 *) fw);
  142. fw += sizeof(u32);
  143. if (len > 300000) {
  144. wl1271_info("firmware chunk too long: %u", len);
  145. return -EINVAL;
  146. }
  147. wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
  148. chunks, addr, len);
  149. wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
  150. fw += len;
  151. }
  152. return 0;
  153. }
  154. static int wl1271_boot_upload_nvs(struct wl1271 *wl)
  155. {
  156. size_t nvs_len, burst_len;
  157. int i;
  158. u32 dest_addr, val;
  159. u8 *nvs_ptr, *nvs, *nvs_aligned;
  160. nvs = wl->nvs;
  161. if (nvs == NULL)
  162. return -ENODEV;
  163. nvs_ptr = nvs;
  164. nvs_len = wl->nvs_len;
  165. /* Update the device MAC address into the nvs */
  166. nvs[11] = wl->mac_addr[0];
  167. nvs[10] = wl->mac_addr[1];
  168. nvs[6] = wl->mac_addr[2];
  169. nvs[5] = wl->mac_addr[3];
  170. nvs[4] = wl->mac_addr[4];
  171. nvs[3] = wl->mac_addr[5];
  172. /*
  173. * Layout before the actual NVS tables:
  174. * 1 byte : burst length.
  175. * 2 bytes: destination address.
  176. * n bytes: data to burst copy.
  177. *
  178. * This is ended by a 0 length, then the NVS tables.
  179. */
  180. /* FIXME: Do we need to check here whether the LSB is 1? */
  181. while (nvs_ptr[0]) {
  182. burst_len = nvs_ptr[0];
  183. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  184. /* FIXME: Due to our new wl1271_translate_reg_addr function,
  185. we need to add the REGISTER_BASE to the destination */
  186. dest_addr += REGISTERS_BASE;
  187. /* We move our pointer to the data */
  188. nvs_ptr += 3;
  189. for (i = 0; i < burst_len; i++) {
  190. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  191. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  192. wl1271_debug(DEBUG_BOOT,
  193. "nvs burst write 0x%x: 0x%x",
  194. dest_addr, val);
  195. wl1271_reg_write32(wl, dest_addr, val);
  196. nvs_ptr += 4;
  197. dest_addr += 4;
  198. }
  199. }
  200. /*
  201. * We've reached the first zero length, the first NVS table
  202. * is 7 bytes further.
  203. */
  204. nvs_ptr += 7;
  205. nvs_len -= nvs_ptr - nvs;
  206. nvs_len = ALIGN(nvs_len, 4);
  207. /* FIXME: The driver sets the partition here, but this is not needed,
  208. since it sets to the same one as currently in use */
  209. /* Now we must set the partition correctly */
  210. wl1271_set_partition(wl,
  211. part_table[PART_WORK].mem.start,
  212. part_table[PART_WORK].mem.size,
  213. part_table[PART_WORK].reg.start,
  214. part_table[PART_WORK].reg.size);
  215. /* Copy the NVS tables to a new block to ensure alignment */
  216. nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
  217. /* And finally we upload the NVS tables */
  218. /* FIXME: In wl1271, we upload everything at once.
  219. No endianness handling needed here?! The ref driver doesn't do
  220. anything about it at this point */
  221. wl1271_spi_mem_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len);
  222. kfree(nvs_aligned);
  223. return 0;
  224. }
  225. static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
  226. {
  227. enable_irq(wl->irq);
  228. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
  229. WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
  230. wl1271_reg_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
  231. }
  232. static int wl1271_boot_soft_reset(struct wl1271 *wl)
  233. {
  234. unsigned long timeout;
  235. u32 boot_data;
  236. /* perform soft reset */
  237. wl1271_reg_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
  238. /* SOFT_RESET is self clearing */
  239. timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
  240. while (1) {
  241. boot_data = wl1271_reg_read32(wl, ACX_REG_SLV_SOFT_RESET);
  242. wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
  243. if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
  244. break;
  245. if (time_after(jiffies, timeout)) {
  246. /* 1.2 check pWhalBus->uSelfClearTime if the
  247. * timeout was reached */
  248. wl1271_error("soft reset timeout");
  249. return -1;
  250. }
  251. udelay(SOFT_RESET_STALL_TIME);
  252. }
  253. /* disable Rx/Tx */
  254. wl1271_reg_write32(wl, ENABLE, 0x0);
  255. /* disable auto calibration on start*/
  256. wl1271_reg_write32(wl, SPARE_A2, 0xffff);
  257. return 0;
  258. }
  259. static int wl1271_boot_run_firmware(struct wl1271 *wl)
  260. {
  261. int loop, ret;
  262. u32 chip_id, interrupt;
  263. wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  264. chip_id = wl1271_reg_read32(wl, CHIP_ID_B);
  265. wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  266. if (chip_id != wl->chip.id) {
  267. wl1271_error("chip id doesn't match after firmware boot");
  268. return -EIO;
  269. }
  270. /* wait for init to complete */
  271. loop = 0;
  272. while (loop++ < INIT_LOOP) {
  273. udelay(INIT_LOOP_DELAY);
  274. interrupt = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  275. if (interrupt == 0xffffffff) {
  276. wl1271_error("error reading hardware complete "
  277. "init indication");
  278. return -EIO;
  279. }
  280. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  281. else if (interrupt & WL1271_ACX_INTR_INIT_COMPLETE) {
  282. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  283. WL1271_ACX_INTR_INIT_COMPLETE);
  284. break;
  285. }
  286. }
  287. if (loop >= INIT_LOOP) {
  288. wl1271_error("timeout waiting for the hardware to "
  289. "complete initialization");
  290. return -EIO;
  291. }
  292. /* get hardware config command mail box */
  293. wl->cmd_box_addr = wl1271_reg_read32(wl, REG_COMMAND_MAILBOX_PTR);
  294. /* get hardware config event mail box */
  295. wl->event_box_addr = wl1271_reg_read32(wl, REG_EVENT_MAILBOX_PTR);
  296. /* set the working partition to its "running" mode offset */
  297. wl1271_set_partition(wl,
  298. part_table[PART_WORK].mem.start,
  299. part_table[PART_WORK].mem.size,
  300. part_table[PART_WORK].reg.start,
  301. part_table[PART_WORK].reg.size);
  302. wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
  303. wl->cmd_box_addr, wl->event_box_addr);
  304. wl1271_boot_fw_version(wl);
  305. /*
  306. * in case of full asynchronous mode the firmware event must be
  307. * ready to receive event from the command mailbox
  308. */
  309. /* enable gpio interrupts */
  310. wl1271_boot_enable_interrupts(wl);
  311. /* unmask all mbox events */
  312. wl->event_mask = 0xffffffff;
  313. ret = wl1271_event_unmask(wl);
  314. if (ret < 0) {
  315. wl1271_error("EVENT mask setting failed");
  316. return ret;
  317. }
  318. wl1271_event_mbox_config(wl);
  319. /* firmware startup completed */
  320. return 0;
  321. }
  322. static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
  323. {
  324. u32 polarity, status, i;
  325. wl1271_reg_write32(wl, OCP_POR_CTR, OCP_REG_POLARITY);
  326. wl1271_reg_write32(wl, OCP_CMD, OCP_CMD_READ);
  327. /* Wait until the command is complete (ie. bit 18 is set) */
  328. for (i = 0; i < OCP_CMD_LOOP; i++) {
  329. polarity = wl1271_reg_read32(wl, OCP_DATA_READ);
  330. if (polarity & OCP_READY_MASK)
  331. break;
  332. }
  333. if (i == OCP_CMD_LOOP) {
  334. wl1271_error("OCP command timeout!");
  335. return -EIO;
  336. }
  337. status = polarity & OCP_STATUS_MASK;
  338. if (status != OCP_STATUS_OK) {
  339. wl1271_error("OCP command failed (%d)", status);
  340. return -EIO;
  341. }
  342. /* We use HIGH polarity, so unset the LOW bit */
  343. polarity &= ~POLARITY_LOW;
  344. wl1271_reg_write32(wl, OCP_POR_CTR, OCP_REG_POLARITY);
  345. wl1271_reg_write32(wl, OCP_DATA_WRITE, polarity);
  346. wl1271_reg_write32(wl, OCP_CMD, OCP_CMD_WRITE);
  347. return 0;
  348. }
  349. int wl1271_boot(struct wl1271 *wl)
  350. {
  351. int ret = 0;
  352. u32 tmp, clk, pause;
  353. if (REF_CLOCK == 0 || REF_CLOCK == 2)
  354. /* ref clk: 19.2/38.4 */
  355. clk = 0x3;
  356. else if (REF_CLOCK == 1 || REF_CLOCK == 3)
  357. /* ref clk: 26/52 */
  358. clk = 0x5;
  359. wl1271_reg_write32(wl, PLL_PARAMETERS, clk);
  360. pause = wl1271_reg_read32(wl, PLL_PARAMETERS);
  361. wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
  362. pause &= ~(WU_COUNTER_PAUSE_VAL); /* FIXME: This should probably be
  363. * WU_COUNTER_PAUSE_VAL instead of
  364. * 0x3ff (magic number ). How does
  365. * this work?! */
  366. pause |= WU_COUNTER_PAUSE_VAL;
  367. wl1271_reg_write32(wl, WU_COUNTER_PAUSE, pause);
  368. /* Continue the ELP wake up sequence */
  369. wl1271_reg_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
  370. udelay(500);
  371. wl1271_set_partition(wl,
  372. part_table[PART_DRPW].mem.start,
  373. part_table[PART_DRPW].mem.size,
  374. part_table[PART_DRPW].reg.start,
  375. part_table[PART_DRPW].reg.size);
  376. /* Read-modify-write DRPW_SCRATCH_START register (see next state)
  377. to be used by DRPw FW. The RTRIM value will be added by the FW
  378. before taking DRPw out of reset */
  379. wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
  380. clk = wl1271_reg_read32(wl, DRPW_SCRATCH_START);
  381. wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
  382. /* 2 */
  383. clk |= (REF_CLOCK << 1) << 4;
  384. wl1271_reg_write32(wl, DRPW_SCRATCH_START, clk);
  385. wl1271_set_partition(wl,
  386. part_table[PART_WORK].mem.start,
  387. part_table[PART_WORK].mem.size,
  388. part_table[PART_WORK].reg.start,
  389. part_table[PART_WORK].reg.size);
  390. /* Disable interrupts */
  391. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
  392. ret = wl1271_boot_soft_reset(wl);
  393. if (ret < 0)
  394. goto out;
  395. /* 2. start processing NVS file */
  396. ret = wl1271_boot_upload_nvs(wl);
  397. if (ret < 0)
  398. goto out;
  399. /* write firmware's last address (ie. it's length) to
  400. * ACX_EEPROMLESS_IND_REG */
  401. wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
  402. wl1271_reg_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
  403. tmp = wl1271_reg_read32(wl, CHIP_ID_B);
  404. wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
  405. /* 6. read the EEPROM parameters */
  406. tmp = wl1271_reg_read32(wl, SCR_PAD2);
  407. ret = wl1271_boot_write_irq_polarity(wl);
  408. if (ret < 0)
  409. goto out;
  410. /* FIXME: Need to check whether this is really what we want */
  411. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
  412. WL1271_ACX_ALL_EVENTS_VECTOR);
  413. /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
  414. * to upload_fw) */
  415. ret = wl1271_boot_upload_firmware(wl);
  416. if (ret < 0)
  417. goto out;
  418. /* 10.5 start firmware */
  419. ret = wl1271_boot_run_firmware(wl);
  420. if (ret < 0)
  421. goto out;
  422. /* set the wl1271 default filters */
  423. wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
  424. wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
  425. wl1271_event_mbox_config(wl);
  426. out:
  427. return ret;
  428. }