boot.c 14 KB

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