wl1251_boot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@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 "wl1251_reg.h"
  25. #include "wl1251_boot.h"
  26. #include "wl1251_io.h"
  27. #include "wl1251_spi.h"
  28. #include "wl1251_event.h"
  29. #include "wl1251_acx.h"
  30. void wl1251_boot_target_enable_interrupts(struct wl1251 *wl)
  31. {
  32. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
  33. wl1251_reg_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
  34. }
  35. int wl1251_boot_soft_reset(struct wl1251 *wl)
  36. {
  37. unsigned long timeout;
  38. u32 boot_data;
  39. /* perform soft reset */
  40. wl1251_reg_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
  41. /* SOFT_RESET is self clearing */
  42. timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
  43. while (1) {
  44. boot_data = wl1251_reg_read32(wl, ACX_REG_SLV_SOFT_RESET);
  45. wl1251_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
  46. if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
  47. break;
  48. if (time_after(jiffies, timeout)) {
  49. /* 1.2 check pWhalBus->uSelfClearTime if the
  50. * timeout was reached */
  51. wl1251_error("soft reset timeout");
  52. return -1;
  53. }
  54. udelay(SOFT_RESET_STALL_TIME);
  55. }
  56. /* disable Rx/Tx */
  57. wl1251_reg_write32(wl, ENABLE, 0x0);
  58. /* disable auto calibration on start*/
  59. wl1251_reg_write32(wl, SPARE_A2, 0xffff);
  60. return 0;
  61. }
  62. int wl1251_boot_init_seq(struct wl1251 *wl)
  63. {
  64. u32 scr_pad6, init_data, tmp, elp_cmd, ref_freq;
  65. /*
  66. * col #1: INTEGER_DIVIDER
  67. * col #2: FRACTIONAL_DIVIDER
  68. * col #3: ATTN_BB
  69. * col #4: ALPHA_BB
  70. * col #5: STOP_TIME_BB
  71. * col #6: BB_PLL_LOOP_FILTER
  72. */
  73. static const u32 LUT[REF_FREQ_NUM][LUT_PARAM_NUM] = {
  74. { 83, 87381, 0xB, 5, 0xF00, 3}, /* REF_FREQ_19_2*/
  75. { 61, 141154, 0xB, 5, 0x1450, 2}, /* REF_FREQ_26_0*/
  76. { 41, 174763, 0xC, 6, 0x2D00, 1}, /* REF_FREQ_38_4*/
  77. { 40, 0, 0xC, 6, 0x2EE0, 1}, /* REF_FREQ_40_0*/
  78. { 47, 162280, 0xC, 6, 0x2760, 1} /* REF_FREQ_33_6 */
  79. };
  80. /* read NVS params */
  81. scr_pad6 = wl1251_reg_read32(wl, SCR_PAD6);
  82. wl1251_debug(DEBUG_BOOT, "scr_pad6 0x%x", scr_pad6);
  83. /* read ELP_CMD */
  84. elp_cmd = wl1251_reg_read32(wl, ELP_CMD);
  85. wl1251_debug(DEBUG_BOOT, "elp_cmd 0x%x", elp_cmd);
  86. /* set the BB calibration time to be 300 usec (PLL_CAL_TIME) */
  87. ref_freq = scr_pad6 & 0x000000FF;
  88. wl1251_debug(DEBUG_BOOT, "ref_freq 0x%x", ref_freq);
  89. wl1251_reg_write32(wl, PLL_CAL_TIME, 0x9);
  90. /*
  91. * PG 1.2: set the clock buffer time to be 210 usec (CLK_BUF_TIME)
  92. */
  93. wl1251_reg_write32(wl, CLK_BUF_TIME, 0x6);
  94. /*
  95. * set the clock detect feature to work in the restart wu procedure
  96. * (ELP_CFG_MODE[14]) and Select the clock source type
  97. * (ELP_CFG_MODE[13:12])
  98. */
  99. tmp = ((scr_pad6 & 0x0000FF00) << 4) | 0x00004000;
  100. wl1251_reg_write32(wl, ELP_CFG_MODE, tmp);
  101. /* PG 1.2: enable the BB PLL fix. Enable the PLL_LIMP_CLK_EN_CMD */
  102. elp_cmd |= 0x00000040;
  103. wl1251_reg_write32(wl, ELP_CMD, elp_cmd);
  104. /* PG 1.2: Set the BB PLL stable time to be 1000usec
  105. * (PLL_STABLE_TIME) */
  106. wl1251_reg_write32(wl, CFG_PLL_SYNC_CNT, 0x20);
  107. /* PG 1.2: read clock request time */
  108. init_data = wl1251_reg_read32(wl, CLK_REQ_TIME);
  109. /*
  110. * PG 1.2: set the clock request time to be ref_clk_settling_time -
  111. * 1ms = 4ms
  112. */
  113. if (init_data > 0x21)
  114. tmp = init_data - 0x21;
  115. else
  116. tmp = 0;
  117. wl1251_reg_write32(wl, CLK_REQ_TIME, tmp);
  118. /* set BB PLL configurations in RF AFE */
  119. wl1251_reg_write32(wl, 0x003058cc, 0x4B5);
  120. /* set RF_AFE_REG_5 */
  121. wl1251_reg_write32(wl, 0x003058d4, 0x50);
  122. /* set RF_AFE_CTRL_REG_2 */
  123. wl1251_reg_write32(wl, 0x00305948, 0x11c001);
  124. /*
  125. * change RF PLL and BB PLL divider for VCO clock and adjust VCO
  126. * bais current(RF_AFE_REG_13)
  127. */
  128. wl1251_reg_write32(wl, 0x003058f4, 0x1e);
  129. /* set BB PLL configurations */
  130. tmp = LUT[ref_freq][LUT_PARAM_INTEGER_DIVIDER] | 0x00017000;
  131. wl1251_reg_write32(wl, 0x00305840, tmp);
  132. /* set fractional divider according to Appendix C-BB PLL
  133. * Calculations
  134. */
  135. tmp = LUT[ref_freq][LUT_PARAM_FRACTIONAL_DIVIDER];
  136. wl1251_reg_write32(wl, 0x00305844, tmp);
  137. /* set the initial data for the sigma delta */
  138. wl1251_reg_write32(wl, 0x00305848, 0x3039);
  139. /*
  140. * set the accumulator attenuation value, calibration loop1
  141. * (alpha), calibration loop2 (beta), calibration loop3 (gamma) and
  142. * the VCO gain
  143. */
  144. tmp = (LUT[ref_freq][LUT_PARAM_ATTN_BB] << 16) |
  145. (LUT[ref_freq][LUT_PARAM_ALPHA_BB] << 12) | 0x1;
  146. wl1251_reg_write32(wl, 0x00305854, tmp);
  147. /*
  148. * set the calibration stop time after holdoff time expires and set
  149. * settling time HOLD_OFF_TIME_BB
  150. */
  151. tmp = LUT[ref_freq][LUT_PARAM_STOP_TIME_BB] | 0x000A0000;
  152. wl1251_reg_write32(wl, 0x00305858, tmp);
  153. /*
  154. * set BB PLL Loop filter capacitor3- BB_C3[2:0] and set BB PLL
  155. * constant leakage current to linearize PFD to 0uA -
  156. * BB_ILOOPF[7:3]
  157. */
  158. tmp = LUT[ref_freq][LUT_PARAM_BB_PLL_LOOP_FILTER] | 0x00000030;
  159. wl1251_reg_write32(wl, 0x003058f8, tmp);
  160. /*
  161. * set regulator output voltage for n divider to
  162. * 1.35-BB_REFDIV[1:0], set charge pump current- BB_CPGAIN[4:2],
  163. * set BB PLL Loop filter capacitor2- BB_C2[7:5], set gain of BB
  164. * PLL auto-call to normal mode- BB_CALGAIN_3DB[8]
  165. */
  166. wl1251_reg_write32(wl, 0x003058f0, 0x29);
  167. /* enable restart wakeup sequence (ELP_CMD[0]) */
  168. wl1251_reg_write32(wl, ELP_CMD, elp_cmd | 0x1);
  169. /* restart sequence completed */
  170. udelay(2000);
  171. return 0;
  172. }
  173. static void wl1251_boot_set_ecpu_ctrl(struct wl1251 *wl, u32 flag)
  174. {
  175. u32 cpu_ctrl;
  176. /* 10.5.0 run the firmware (I) */
  177. cpu_ctrl = wl1251_reg_read32(wl, ACX_REG_ECPU_CONTROL);
  178. /* 10.5.1 run the firmware (II) */
  179. cpu_ctrl &= ~flag;
  180. wl1251_reg_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
  181. }
  182. int wl1251_boot_run_firmware(struct wl1251 *wl)
  183. {
  184. int loop, ret;
  185. u32 chip_id, interrupt;
  186. wl1251_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  187. chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  188. wl1251_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  189. if (chip_id != wl->chip_id) {
  190. wl1251_error("chip id doesn't match after firmware boot");
  191. return -EIO;
  192. }
  193. /* wait for init to complete */
  194. loop = 0;
  195. while (loop++ < INIT_LOOP) {
  196. udelay(INIT_LOOP_DELAY);
  197. interrupt = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  198. if (interrupt == 0xffffffff) {
  199. wl1251_error("error reading hardware complete "
  200. "init indication");
  201. return -EIO;
  202. }
  203. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  204. else if (interrupt & WL1251_ACX_INTR_INIT_COMPLETE) {
  205. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  206. WL1251_ACX_INTR_INIT_COMPLETE);
  207. break;
  208. }
  209. }
  210. if (loop >= INIT_LOOP) {
  211. wl1251_error("timeout waiting for the hardware to "
  212. "complete initialization");
  213. return -EIO;
  214. }
  215. /* get hardware config command mail box */
  216. wl->cmd_box_addr = wl1251_reg_read32(wl, REG_COMMAND_MAILBOX_PTR);
  217. /* get hardware config event mail box */
  218. wl->event_box_addr = wl1251_reg_read32(wl, REG_EVENT_MAILBOX_PTR);
  219. /* set the working partition to its "running" mode offset */
  220. wl1251_set_partition(wl, WL1251_PART_WORK_MEM_START,
  221. WL1251_PART_WORK_MEM_SIZE,
  222. WL1251_PART_WORK_REG_START,
  223. WL1251_PART_WORK_REG_SIZE);
  224. wl1251_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
  225. wl->cmd_box_addr, wl->event_box_addr);
  226. wl1251_acx_fw_version(wl, wl->fw_ver, sizeof(wl->fw_ver));
  227. /*
  228. * in case of full asynchronous mode the firmware event must be
  229. * ready to receive event from the command mailbox
  230. */
  231. /* enable gpio interrupts */
  232. wl1251_enable_interrupts(wl);
  233. /* Enable target's interrupts */
  234. wl->intr_mask = WL1251_ACX_INTR_RX0_DATA |
  235. WL1251_ACX_INTR_RX1_DATA |
  236. WL1251_ACX_INTR_TX_RESULT |
  237. WL1251_ACX_INTR_EVENT_A |
  238. WL1251_ACX_INTR_EVENT_B |
  239. WL1251_ACX_INTR_INIT_COMPLETE;
  240. wl1251_boot_target_enable_interrupts(wl);
  241. /* unmask all mbox events */
  242. wl->event_mask = 0xffffffff;
  243. ret = wl1251_event_unmask(wl);
  244. if (ret < 0) {
  245. wl1251_error("EVENT mask setting failed");
  246. return ret;
  247. }
  248. wl1251_event_mbox_config(wl);
  249. /* firmware startup completed */
  250. return 0;
  251. }
  252. static int wl1251_boot_upload_firmware(struct wl1251 *wl)
  253. {
  254. int addr, chunk_num, partition_limit;
  255. size_t fw_data_len;
  256. u8 *p;
  257. /* whal_FwCtrl_LoadFwImageSm() */
  258. wl1251_debug(DEBUG_BOOT, "chip id before fw upload: 0x%x",
  259. wl1251_reg_read32(wl, CHIP_ID_B));
  260. /* 10.0 check firmware length and set partition */
  261. fw_data_len = (wl->fw[4] << 24) | (wl->fw[5] << 16) |
  262. (wl->fw[6] << 8) | (wl->fw[7]);
  263. wl1251_debug(DEBUG_BOOT, "fw_data_len %zu chunk_size %d", fw_data_len,
  264. CHUNK_SIZE);
  265. if ((fw_data_len % 4) != 0) {
  266. wl1251_error("firmware length not multiple of four");
  267. return -EIO;
  268. }
  269. wl1251_set_partition(wl, WL1251_PART_DOWN_MEM_START,
  270. WL1251_PART_DOWN_MEM_SIZE,
  271. WL1251_PART_DOWN_REG_START,
  272. WL1251_PART_DOWN_REG_SIZE);
  273. /* 10.1 set partition limit and chunk num */
  274. chunk_num = 0;
  275. partition_limit = WL1251_PART_DOWN_MEM_SIZE;
  276. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  277. /* 10.2 update partition, if needed */
  278. addr = WL1251_PART_DOWN_MEM_START +
  279. (chunk_num + 2) * CHUNK_SIZE;
  280. if (addr > partition_limit) {
  281. addr = WL1251_PART_DOWN_MEM_START +
  282. chunk_num * CHUNK_SIZE;
  283. partition_limit = chunk_num * CHUNK_SIZE +
  284. WL1251_PART_DOWN_MEM_SIZE;
  285. wl1251_set_partition(wl,
  286. addr,
  287. WL1251_PART_DOWN_MEM_SIZE,
  288. WL1251_PART_DOWN_REG_START,
  289. WL1251_PART_DOWN_REG_SIZE);
  290. }
  291. /* 10.3 upload the chunk */
  292. addr = WL1251_PART_DOWN_MEM_START + chunk_num * CHUNK_SIZE;
  293. p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
  294. wl1251_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  295. p, addr);
  296. wl1251_mem_write(wl, addr, p, CHUNK_SIZE);
  297. chunk_num++;
  298. }
  299. /* 10.4 upload the last chunk */
  300. addr = WL1251_PART_DOWN_MEM_START + chunk_num * CHUNK_SIZE;
  301. p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
  302. wl1251_debug(DEBUG_BOOT, "uploading fw last chunk (%zu B) 0x%p to 0x%x",
  303. fw_data_len % CHUNK_SIZE, p, addr);
  304. wl1251_mem_write(wl, addr, p, fw_data_len % CHUNK_SIZE);
  305. return 0;
  306. }
  307. static int wl1251_boot_upload_nvs(struct wl1251 *wl)
  308. {
  309. size_t nvs_len, nvs_bytes_written, burst_len;
  310. int nvs_start, i;
  311. u32 dest_addr, val;
  312. u8 *nvs_ptr, *nvs;
  313. nvs = wl->nvs;
  314. if (nvs == NULL)
  315. return -ENODEV;
  316. nvs_ptr = nvs;
  317. nvs_len = wl->nvs_len;
  318. nvs_start = wl->fw_len;
  319. /*
  320. * Layout before the actual NVS tables:
  321. * 1 byte : burst length.
  322. * 2 bytes: destination address.
  323. * n bytes: data to burst copy.
  324. *
  325. * This is ended by a 0 length, then the NVS tables.
  326. */
  327. while (nvs_ptr[0]) {
  328. burst_len = nvs_ptr[0];
  329. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  330. /* We move our pointer to the data */
  331. nvs_ptr += 3;
  332. for (i = 0; i < burst_len; i++) {
  333. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  334. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  335. wl1251_debug(DEBUG_BOOT,
  336. "nvs burst write 0x%x: 0x%x",
  337. dest_addr, val);
  338. wl1251_mem_write32(wl, dest_addr, val);
  339. nvs_ptr += 4;
  340. dest_addr += 4;
  341. }
  342. }
  343. /*
  344. * We've reached the first zero length, the first NVS table
  345. * is 7 bytes further.
  346. */
  347. nvs_ptr += 7;
  348. nvs_len -= nvs_ptr - nvs;
  349. nvs_len = ALIGN(nvs_len, 4);
  350. /* Now we must set the partition correctly */
  351. wl1251_set_partition(wl, nvs_start,
  352. WL1251_PART_DOWN_MEM_SIZE,
  353. WL1251_PART_DOWN_REG_START,
  354. WL1251_PART_DOWN_REG_SIZE);
  355. /* And finally we upload the NVS tables */
  356. nvs_bytes_written = 0;
  357. while (nvs_bytes_written < nvs_len) {
  358. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  359. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  360. val = cpu_to_le32(val);
  361. wl1251_debug(DEBUG_BOOT,
  362. "nvs write table 0x%x: 0x%x",
  363. nvs_start, val);
  364. wl1251_mem_write32(wl, nvs_start, val);
  365. nvs_ptr += 4;
  366. nvs_bytes_written += 4;
  367. nvs_start += 4;
  368. }
  369. return 0;
  370. }
  371. int wl1251_boot(struct wl1251 *wl)
  372. {
  373. int ret = 0, minor_minor_e2_ver;
  374. u32 tmp, boot_data;
  375. /* halt embedded ARM CPU while loading firmware */
  376. wl1251_reg_write32(wl, ACX_REG_ECPU_CONTROL, ECPU_CONTROL_HALT);
  377. ret = wl1251_boot_soft_reset(wl);
  378. if (ret < 0)
  379. goto out;
  380. /* 2. start processing NVS file */
  381. ret = wl1251_boot_upload_nvs(wl);
  382. if (ret < 0)
  383. goto out;
  384. /* write firmware's last address (ie. it's length) to
  385. * ACX_EEPROMLESS_IND_REG */
  386. wl1251_reg_write32(wl, ACX_EEPROMLESS_IND_REG, wl->fw_len);
  387. /* 6. read the EEPROM parameters */
  388. tmp = wl1251_reg_read32(wl, SCR_PAD2);
  389. /* 7. read bootdata */
  390. wl->boot_attr.radio_type = (tmp & 0x0000FF00) >> 8;
  391. wl->boot_attr.major = (tmp & 0x00FF0000) >> 16;
  392. tmp = wl1251_reg_read32(wl, SCR_PAD3);
  393. /* 8. check bootdata and call restart sequence */
  394. wl->boot_attr.minor = (tmp & 0x00FF0000) >> 16;
  395. minor_minor_e2_ver = (tmp & 0xFF000000) >> 24;
  396. wl1251_debug(DEBUG_BOOT, "radioType 0x%x majorE2Ver 0x%x "
  397. "minorE2Ver 0x%x minor_minor_e2_ver 0x%x",
  398. wl->boot_attr.radio_type, wl->boot_attr.major,
  399. wl->boot_attr.minor, minor_minor_e2_ver);
  400. ret = wl1251_boot_init_seq(wl);
  401. if (ret < 0)
  402. goto out;
  403. /* 9. NVS processing done */
  404. boot_data = wl1251_reg_read32(wl, ACX_REG_ECPU_CONTROL);
  405. wl1251_debug(DEBUG_BOOT, "halt boot_data 0x%x", boot_data);
  406. /* 10. check that ECPU_CONTROL_HALT bits are set in
  407. * pWhalBus->uBootData and start uploading firmware
  408. */
  409. if ((boot_data & ECPU_CONTROL_HALT) == 0) {
  410. wl1251_error("boot failed, ECPU_CONTROL_HALT not set");
  411. ret = -EIO;
  412. goto out;
  413. }
  414. ret = wl1251_boot_upload_firmware(wl);
  415. if (ret < 0)
  416. goto out;
  417. /* 10.5 start firmware */
  418. ret = wl1251_boot_run_firmware(wl);
  419. if (ret < 0)
  420. goto out;
  421. out:
  422. return ret;
  423. }