wl1251_boot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. wl->event_mask = SCAN_COMPLETE_EVENT_ID | BSS_LOSE_EVENT_ID |
  242. SYNCHRONIZATION_TIMEOUT_EVENT_ID |
  243. ROAMING_TRIGGER_LOW_RSSI_EVENT_ID |
  244. ROAMING_TRIGGER_REGAINED_RSSI_EVENT_ID |
  245. REGAINED_BSS_EVENT_ID | BT_PTA_SENSE_EVENT_ID |
  246. BT_PTA_PREDICTION_EVENT_ID;
  247. ret = wl1251_event_unmask(wl);
  248. if (ret < 0) {
  249. wl1251_error("EVENT mask setting failed");
  250. return ret;
  251. }
  252. wl1251_event_mbox_config(wl);
  253. /* firmware startup completed */
  254. return 0;
  255. }
  256. static int wl1251_boot_upload_firmware(struct wl1251 *wl)
  257. {
  258. int addr, chunk_num, partition_limit;
  259. size_t fw_data_len, len;
  260. u8 *p, *buf;
  261. /* whal_FwCtrl_LoadFwImageSm() */
  262. wl1251_debug(DEBUG_BOOT, "chip id before fw upload: 0x%x",
  263. wl1251_reg_read32(wl, CHIP_ID_B));
  264. /* 10.0 check firmware length and set partition */
  265. fw_data_len = (wl->fw[4] << 24) | (wl->fw[5] << 16) |
  266. (wl->fw[6] << 8) | (wl->fw[7]);
  267. wl1251_debug(DEBUG_BOOT, "fw_data_len %zu chunk_size %d", fw_data_len,
  268. CHUNK_SIZE);
  269. if ((fw_data_len % 4) != 0) {
  270. wl1251_error("firmware length not multiple of four");
  271. return -EIO;
  272. }
  273. buf = kmalloc(CHUNK_SIZE, GFP_KERNEL);
  274. if (!buf) {
  275. wl1251_error("allocation for firmware upload chunk failed");
  276. return -ENOMEM;
  277. }
  278. wl1251_set_partition(wl, WL1251_PART_DOWN_MEM_START,
  279. WL1251_PART_DOWN_MEM_SIZE,
  280. WL1251_PART_DOWN_REG_START,
  281. WL1251_PART_DOWN_REG_SIZE);
  282. /* 10.1 set partition limit and chunk num */
  283. chunk_num = 0;
  284. partition_limit = WL1251_PART_DOWN_MEM_SIZE;
  285. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  286. /* 10.2 update partition, if needed */
  287. addr = WL1251_PART_DOWN_MEM_START +
  288. (chunk_num + 2) * CHUNK_SIZE;
  289. if (addr > partition_limit) {
  290. addr = WL1251_PART_DOWN_MEM_START +
  291. chunk_num * CHUNK_SIZE;
  292. partition_limit = chunk_num * CHUNK_SIZE +
  293. WL1251_PART_DOWN_MEM_SIZE;
  294. wl1251_set_partition(wl,
  295. addr,
  296. WL1251_PART_DOWN_MEM_SIZE,
  297. WL1251_PART_DOWN_REG_START,
  298. WL1251_PART_DOWN_REG_SIZE);
  299. }
  300. /* 10.3 upload the chunk */
  301. addr = WL1251_PART_DOWN_MEM_START + chunk_num * CHUNK_SIZE;
  302. p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
  303. wl1251_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  304. p, addr);
  305. /* need to copy the chunk for dma */
  306. len = CHUNK_SIZE;
  307. memcpy(buf, p, len);
  308. wl1251_mem_write(wl, addr, buf, len);
  309. chunk_num++;
  310. }
  311. /* 10.4 upload the last chunk */
  312. addr = WL1251_PART_DOWN_MEM_START + chunk_num * CHUNK_SIZE;
  313. p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
  314. /* need to copy the chunk for dma */
  315. len = fw_data_len % CHUNK_SIZE;
  316. memcpy(buf, p, len);
  317. wl1251_debug(DEBUG_BOOT, "uploading fw last chunk (%zu B) 0x%p to 0x%x",
  318. len, p, addr);
  319. wl1251_mem_write(wl, addr, buf, len);
  320. kfree(buf);
  321. return 0;
  322. }
  323. static int wl1251_boot_upload_nvs(struct wl1251 *wl)
  324. {
  325. size_t nvs_len, nvs_bytes_written, burst_len;
  326. int nvs_start, i;
  327. u32 dest_addr, val;
  328. u8 *nvs_ptr, *nvs;
  329. nvs = wl->nvs;
  330. if (nvs == NULL)
  331. return -ENODEV;
  332. nvs_ptr = nvs;
  333. nvs_len = wl->nvs_len;
  334. nvs_start = wl->fw_len;
  335. /*
  336. * Layout before the actual NVS tables:
  337. * 1 byte : burst length.
  338. * 2 bytes: destination address.
  339. * n bytes: data to burst copy.
  340. *
  341. * This is ended by a 0 length, then the NVS tables.
  342. */
  343. while (nvs_ptr[0]) {
  344. burst_len = nvs_ptr[0];
  345. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  346. /* We move our pointer to the data */
  347. nvs_ptr += 3;
  348. for (i = 0; i < burst_len; i++) {
  349. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  350. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  351. wl1251_debug(DEBUG_BOOT,
  352. "nvs burst write 0x%x: 0x%x",
  353. dest_addr, val);
  354. wl1251_mem_write32(wl, dest_addr, val);
  355. nvs_ptr += 4;
  356. dest_addr += 4;
  357. }
  358. }
  359. /*
  360. * We've reached the first zero length, the first NVS table
  361. * is 7 bytes further.
  362. */
  363. nvs_ptr += 7;
  364. nvs_len -= nvs_ptr - nvs;
  365. nvs_len = ALIGN(nvs_len, 4);
  366. /* Now we must set the partition correctly */
  367. wl1251_set_partition(wl, nvs_start,
  368. WL1251_PART_DOWN_MEM_SIZE,
  369. WL1251_PART_DOWN_REG_START,
  370. WL1251_PART_DOWN_REG_SIZE);
  371. /* And finally we upload the NVS tables */
  372. nvs_bytes_written = 0;
  373. while (nvs_bytes_written < nvs_len) {
  374. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  375. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  376. val = cpu_to_le32(val);
  377. wl1251_debug(DEBUG_BOOT,
  378. "nvs write table 0x%x: 0x%x",
  379. nvs_start, val);
  380. wl1251_mem_write32(wl, nvs_start, val);
  381. nvs_ptr += 4;
  382. nvs_bytes_written += 4;
  383. nvs_start += 4;
  384. }
  385. return 0;
  386. }
  387. int wl1251_boot(struct wl1251 *wl)
  388. {
  389. int ret = 0, minor_minor_e2_ver;
  390. u32 tmp, boot_data;
  391. /* halt embedded ARM CPU while loading firmware */
  392. wl1251_reg_write32(wl, ACX_REG_ECPU_CONTROL, ECPU_CONTROL_HALT);
  393. ret = wl1251_boot_soft_reset(wl);
  394. if (ret < 0)
  395. goto out;
  396. /* 2. start processing NVS file */
  397. if (wl->use_eeprom) {
  398. wl1251_reg_write32(wl, ACX_REG_EE_START, START_EEPROM_MGR);
  399. msleep(4000);
  400. wl1251_reg_write32(wl, ACX_EEPROMLESS_IND_REG, USE_EEPROM);
  401. } else {
  402. ret = wl1251_boot_upload_nvs(wl);
  403. if (ret < 0)
  404. goto out;
  405. /* write firmware's last address (ie. it's length) to
  406. * ACX_EEPROMLESS_IND_REG */
  407. wl1251_reg_write32(wl, ACX_EEPROMLESS_IND_REG, wl->fw_len);
  408. }
  409. /* 6. read the EEPROM parameters */
  410. tmp = wl1251_reg_read32(wl, SCR_PAD2);
  411. /* 7. read bootdata */
  412. wl->boot_attr.radio_type = (tmp & 0x0000FF00) >> 8;
  413. wl->boot_attr.major = (tmp & 0x00FF0000) >> 16;
  414. tmp = wl1251_reg_read32(wl, SCR_PAD3);
  415. /* 8. check bootdata and call restart sequence */
  416. wl->boot_attr.minor = (tmp & 0x00FF0000) >> 16;
  417. minor_minor_e2_ver = (tmp & 0xFF000000) >> 24;
  418. wl1251_debug(DEBUG_BOOT, "radioType 0x%x majorE2Ver 0x%x "
  419. "minorE2Ver 0x%x minor_minor_e2_ver 0x%x",
  420. wl->boot_attr.radio_type, wl->boot_attr.major,
  421. wl->boot_attr.minor, minor_minor_e2_ver);
  422. ret = wl1251_boot_init_seq(wl);
  423. if (ret < 0)
  424. goto out;
  425. /* 9. NVS processing done */
  426. boot_data = wl1251_reg_read32(wl, ACX_REG_ECPU_CONTROL);
  427. wl1251_debug(DEBUG_BOOT, "halt boot_data 0x%x", boot_data);
  428. /* 10. check that ECPU_CONTROL_HALT bits are set in
  429. * pWhalBus->uBootData and start uploading firmware
  430. */
  431. if ((boot_data & ECPU_CONTROL_HALT) == 0) {
  432. wl1251_error("boot failed, ECPU_CONTROL_HALT not set");
  433. ret = -EIO;
  434. goto out;
  435. }
  436. ret = wl1251_boot_upload_firmware(wl);
  437. if (ret < 0)
  438. goto out;
  439. /* 10.5 start firmware */
  440. ret = wl1251_boot_run_firmware(wl);
  441. if (ret < 0)
  442. goto out;
  443. out:
  444. return ret;
  445. }