boot.c 14 KB

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