wl1271_boot.c 14 KB

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