boot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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/slab.h>
  24. #include <linux/wl12xx.h>
  25. #include <linux/export.h>
  26. #include "debug.h"
  27. #include "acx.h"
  28. #include "reg.h"
  29. #include "boot.h"
  30. #include "io.h"
  31. #include "event.h"
  32. #include "rx.h"
  33. static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
  34. {
  35. u32 cpu_ctrl;
  36. /* 10.5.0 run the firmware (I) */
  37. cpu_ctrl = wl1271_read32(wl, ACX_REG_ECPU_CONTROL);
  38. /* 10.5.1 run the firmware (II) */
  39. cpu_ctrl |= flag;
  40. wl1271_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
  41. }
  42. static unsigned int wl12xx_get_fw_ver_quirks(struct wl1271 *wl)
  43. {
  44. unsigned int quirks = 0;
  45. unsigned int *fw_ver = wl->chip.fw_ver;
  46. /* Only new station firmwares support routing fw logs to the host */
  47. if ((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_STA) &&
  48. (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_FWLOG_STA_MIN))
  49. quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
  50. /* This feature is not yet supported for AP mode */
  51. if (fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_AP)
  52. quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
  53. return quirks;
  54. }
  55. static void wl1271_parse_fw_ver(struct wl1271 *wl)
  56. {
  57. int ret;
  58. ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
  59. &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
  60. &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
  61. &wl->chip.fw_ver[4]);
  62. if (ret != 5) {
  63. wl1271_warning("fw version incorrect value");
  64. memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
  65. return;
  66. }
  67. /* Check if any quirks are needed with older fw versions */
  68. wl->quirks |= wl12xx_get_fw_ver_quirks(wl);
  69. }
  70. static void wl1271_boot_fw_version(struct wl1271 *wl)
  71. {
  72. struct wl1271_static_data static_data;
  73. wl1271_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
  74. false);
  75. strncpy(wl->chip.fw_ver_str, static_data.fw_version,
  76. sizeof(wl->chip.fw_ver_str));
  77. /* make sure the string is NULL-terminated */
  78. wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
  79. wl1271_parse_fw_ver(wl);
  80. }
  81. static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
  82. size_t fw_data_len, u32 dest)
  83. {
  84. struct wl1271_partition_set partition;
  85. int addr, chunk_num, partition_limit;
  86. u8 *p, *chunk;
  87. /* whal_FwCtrl_LoadFwImageSm() */
  88. wl1271_debug(DEBUG_BOOT, "starting firmware upload");
  89. wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
  90. fw_data_len, CHUNK_SIZE);
  91. if ((fw_data_len % 4) != 0) {
  92. wl1271_error("firmware length not multiple of four");
  93. return -EIO;
  94. }
  95. chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
  96. if (!chunk) {
  97. wl1271_error("allocation for firmware upload chunk failed");
  98. return -ENOMEM;
  99. }
  100. memcpy(&partition, &wl12xx_part_table[PART_DOWN], sizeof(partition));
  101. partition.mem.start = dest;
  102. wl1271_set_partition(wl, &partition);
  103. /* 10.1 set partition limit and chunk num */
  104. chunk_num = 0;
  105. partition_limit = wl12xx_part_table[PART_DOWN].mem.size;
  106. while (chunk_num < fw_data_len / CHUNK_SIZE) {
  107. /* 10.2 update partition, if needed */
  108. addr = dest + (chunk_num + 2) * CHUNK_SIZE;
  109. if (addr > partition_limit) {
  110. addr = dest + chunk_num * CHUNK_SIZE;
  111. partition_limit = chunk_num * CHUNK_SIZE +
  112. wl12xx_part_table[PART_DOWN].mem.size;
  113. partition.mem.start = addr;
  114. wl1271_set_partition(wl, &partition);
  115. }
  116. /* 10.3 upload the chunk */
  117. addr = dest + chunk_num * CHUNK_SIZE;
  118. p = buf + chunk_num * CHUNK_SIZE;
  119. memcpy(chunk, p, CHUNK_SIZE);
  120. wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
  121. p, addr);
  122. wl1271_write(wl, addr, chunk, CHUNK_SIZE, false);
  123. chunk_num++;
  124. }
  125. /* 10.4 upload the last chunk */
  126. addr = dest + chunk_num * CHUNK_SIZE;
  127. p = buf + chunk_num * CHUNK_SIZE;
  128. memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
  129. wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
  130. fw_data_len % CHUNK_SIZE, p, addr);
  131. wl1271_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
  132. kfree(chunk);
  133. return 0;
  134. }
  135. static int wl1271_boot_upload_firmware(struct wl1271 *wl)
  136. {
  137. u32 chunks, addr, len;
  138. int ret = 0;
  139. u8 *fw;
  140. fw = wl->fw;
  141. chunks = be32_to_cpup((__be32 *) fw);
  142. fw += sizeof(u32);
  143. wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
  144. while (chunks--) {
  145. addr = be32_to_cpup((__be32 *) fw);
  146. fw += sizeof(u32);
  147. len = be32_to_cpup((__be32 *) fw);
  148. fw += sizeof(u32);
  149. if (len > 300000) {
  150. wl1271_info("firmware chunk too long: %u", len);
  151. return -EINVAL;
  152. }
  153. wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
  154. chunks, addr, len);
  155. ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
  156. if (ret != 0)
  157. break;
  158. fw += len;
  159. }
  160. return ret;
  161. }
  162. static int wl1271_boot_upload_nvs(struct wl1271 *wl)
  163. {
  164. size_t nvs_len, burst_len;
  165. int i;
  166. u32 dest_addr, val;
  167. u8 *nvs_ptr, *nvs_aligned;
  168. if (wl->nvs == NULL)
  169. return -ENODEV;
  170. if (wl->chip.id == CHIP_ID_1283_PG20) {
  171. struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
  172. if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
  173. if (nvs->general_params.dual_mode_select)
  174. wl->enable_11a = true;
  175. } else {
  176. wl1271_error("nvs size is not as expected: %zu != %zu",
  177. wl->nvs_len,
  178. sizeof(struct wl128x_nvs_file));
  179. kfree(wl->nvs);
  180. wl->nvs = NULL;
  181. wl->nvs_len = 0;
  182. return -EILSEQ;
  183. }
  184. /* only the first part of the NVS needs to be uploaded */
  185. nvs_len = sizeof(nvs->nvs);
  186. nvs_ptr = (u8 *)nvs->nvs;
  187. } else {
  188. struct wl1271_nvs_file *nvs =
  189. (struct wl1271_nvs_file *)wl->nvs;
  190. /*
  191. * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
  192. * band configurations) can be removed when those NVS files stop
  193. * floating around.
  194. */
  195. if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
  196. wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
  197. if (nvs->general_params.dual_mode_select)
  198. wl->enable_11a = true;
  199. }
  200. if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
  201. (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
  202. wl->enable_11a)) {
  203. wl1271_error("nvs size is not as expected: %zu != %zu",
  204. wl->nvs_len, sizeof(struct wl1271_nvs_file));
  205. kfree(wl->nvs);
  206. wl->nvs = NULL;
  207. wl->nvs_len = 0;
  208. return -EILSEQ;
  209. }
  210. /* only the first part of the NVS needs to be uploaded */
  211. nvs_len = sizeof(nvs->nvs);
  212. nvs_ptr = (u8 *) nvs->nvs;
  213. }
  214. /* update current MAC address to NVS */
  215. nvs_ptr[11] = wl->addresses[0].addr[0];
  216. nvs_ptr[10] = wl->addresses[0].addr[1];
  217. nvs_ptr[6] = wl->addresses[0].addr[2];
  218. nvs_ptr[5] = wl->addresses[0].addr[3];
  219. nvs_ptr[4] = wl->addresses[0].addr[4];
  220. nvs_ptr[3] = wl->addresses[0].addr[5];
  221. /*
  222. * Layout before the actual NVS tables:
  223. * 1 byte : burst length.
  224. * 2 bytes: destination address.
  225. * n bytes: data to burst copy.
  226. *
  227. * This is ended by a 0 length, then the NVS tables.
  228. */
  229. /* FIXME: Do we need to check here whether the LSB is 1? */
  230. while (nvs_ptr[0]) {
  231. burst_len = nvs_ptr[0];
  232. dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
  233. /*
  234. * Due to our new wl1271_translate_reg_addr function,
  235. * we need to add the REGISTER_BASE to the destination
  236. */
  237. dest_addr += REGISTERS_BASE;
  238. /* We move our pointer to the data */
  239. nvs_ptr += 3;
  240. for (i = 0; i < burst_len; i++) {
  241. if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
  242. goto out_badnvs;
  243. val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
  244. | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
  245. wl1271_debug(DEBUG_BOOT,
  246. "nvs burst write 0x%x: 0x%x",
  247. dest_addr, val);
  248. wl1271_write32(wl, dest_addr, val);
  249. nvs_ptr += 4;
  250. dest_addr += 4;
  251. }
  252. if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
  253. goto out_badnvs;
  254. }
  255. /*
  256. * We've reached the first zero length, the first NVS table
  257. * is located at an aligned offset which is at least 7 bytes further.
  258. * NOTE: The wl->nvs->nvs element must be first, in order to
  259. * simplify the casting, we assume it is at the beginning of
  260. * the wl->nvs structure.
  261. */
  262. nvs_ptr = (u8 *)wl->nvs +
  263. ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
  264. if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
  265. goto out_badnvs;
  266. nvs_len -= nvs_ptr - (u8 *)wl->nvs;
  267. /* Now we must set the partition correctly */
  268. wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
  269. /* Copy the NVS tables to a new block to ensure alignment */
  270. nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
  271. if (!nvs_aligned)
  272. return -ENOMEM;
  273. /* And finally we upload the NVS tables */
  274. wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
  275. kfree(nvs_aligned);
  276. return 0;
  277. out_badnvs:
  278. wl1271_error("nvs data is malformed");
  279. return -EILSEQ;
  280. }
  281. static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
  282. {
  283. wl1271_enable_interrupts(wl);
  284. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
  285. WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
  286. wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
  287. }
  288. static int wl1271_boot_soft_reset(struct wl1271 *wl)
  289. {
  290. unsigned long timeout;
  291. u32 boot_data;
  292. /* perform soft reset */
  293. wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
  294. /* SOFT_RESET is self clearing */
  295. timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
  296. while (1) {
  297. boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
  298. wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
  299. if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
  300. break;
  301. if (time_after(jiffies, timeout)) {
  302. /* 1.2 check pWhalBus->uSelfClearTime if the
  303. * timeout was reached */
  304. wl1271_error("soft reset timeout");
  305. return -1;
  306. }
  307. udelay(SOFT_RESET_STALL_TIME);
  308. }
  309. /* disable Rx/Tx */
  310. wl1271_write32(wl, ENABLE, 0x0);
  311. /* disable auto calibration on start*/
  312. wl1271_write32(wl, SPARE_A2, 0xffff);
  313. return 0;
  314. }
  315. static int wl1271_boot_run_firmware(struct wl1271 *wl)
  316. {
  317. int loop, ret;
  318. u32 chip_id, intr;
  319. wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
  320. chip_id = wl1271_read32(wl, CHIP_ID_B);
  321. wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
  322. if (chip_id != wl->chip.id) {
  323. wl1271_error("chip id doesn't match after firmware boot");
  324. return -EIO;
  325. }
  326. /* wait for init to complete */
  327. loop = 0;
  328. while (loop++ < INIT_LOOP) {
  329. udelay(INIT_LOOP_DELAY);
  330. intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  331. if (intr == 0xffffffff) {
  332. wl1271_error("error reading hardware complete "
  333. "init indication");
  334. return -EIO;
  335. }
  336. /* check that ACX_INTR_INIT_COMPLETE is enabled */
  337. else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
  338. wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
  339. WL1271_ACX_INTR_INIT_COMPLETE);
  340. break;
  341. }
  342. }
  343. if (loop > INIT_LOOP) {
  344. wl1271_error("timeout waiting for the hardware to "
  345. "complete initialization");
  346. return -EIO;
  347. }
  348. /* get hardware config command mail box */
  349. wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
  350. /* get hardware config event mail box */
  351. wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
  352. /* set the working partition to its "running" mode offset */
  353. wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
  354. wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
  355. wl->cmd_box_addr, wl->event_box_addr);
  356. wl1271_boot_fw_version(wl);
  357. /*
  358. * in case of full asynchronous mode the firmware event must be
  359. * ready to receive event from the command mailbox
  360. */
  361. /* unmask required mbox events */
  362. wl->event_mask = BSS_LOSE_EVENT_ID |
  363. SCAN_COMPLETE_EVENT_ID |
  364. PS_REPORT_EVENT_ID |
  365. ROLE_STOP_COMPLETE_EVENT_ID |
  366. RSSI_SNR_TRIGGER_0_EVENT_ID |
  367. PSPOLL_DELIVERY_FAILURE_EVENT_ID |
  368. SOFT_GEMINI_SENSE_EVENT_ID |
  369. PERIODIC_SCAN_REPORT_EVENT_ID |
  370. PERIODIC_SCAN_COMPLETE_EVENT_ID |
  371. DUMMY_PACKET_EVENT_ID |
  372. PEER_REMOVE_COMPLETE_EVENT_ID |
  373. BA_SESSION_RX_CONSTRAINT_EVENT_ID |
  374. REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID |
  375. INACTIVE_STA_EVENT_ID |
  376. MAX_TX_RETRY_EVENT_ID |
  377. CHANNEL_SWITCH_COMPLETE_EVENT_ID;
  378. ret = wl1271_event_unmask(wl);
  379. if (ret < 0) {
  380. wl1271_error("EVENT mask setting failed");
  381. return ret;
  382. }
  383. wl1271_event_mbox_config(wl);
  384. /* firmware startup completed */
  385. return 0;
  386. }
  387. static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
  388. {
  389. u32 polarity;
  390. polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
  391. /* We use HIGH polarity, so unset the LOW bit */
  392. polarity &= ~POLARITY_LOW;
  393. wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
  394. return 0;
  395. }
  396. static int wl128x_switch_tcxo_to_fref(struct wl1271 *wl)
  397. {
  398. u16 spare_reg;
  399. /* Mask bits [2] & [8:4] in the sys_clk_cfg register */
  400. spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
  401. if (spare_reg == 0xFFFF)
  402. return -EFAULT;
  403. spare_reg |= (BIT(3) | BIT(5) | BIT(6));
  404. wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
  405. /* Enable FREF_CLK_REQ & mux MCS and coex PLLs to FREF */
  406. wl1271_top_reg_write(wl, SYS_CLK_CFG_REG,
  407. WL_CLK_REQ_TYPE_PG2 | MCS_PLL_CLK_SEL_FREF);
  408. /* Delay execution for 15msec, to let the HW settle */
  409. mdelay(15);
  410. return 0;
  411. }
  412. static bool wl128x_is_tcxo_valid(struct wl1271 *wl)
  413. {
  414. u16 tcxo_detection;
  415. tcxo_detection = wl1271_top_reg_read(wl, TCXO_CLK_DETECT_REG);
  416. if (tcxo_detection & TCXO_DET_FAILED)
  417. return false;
  418. return true;
  419. }
  420. static bool wl128x_is_fref_valid(struct wl1271 *wl)
  421. {
  422. u16 fref_detection;
  423. fref_detection = wl1271_top_reg_read(wl, FREF_CLK_DETECT_REG);
  424. if (fref_detection & FREF_CLK_DETECT_FAIL)
  425. return false;
  426. return true;
  427. }
  428. static int wl128x_manually_configure_mcs_pll(struct wl1271 *wl)
  429. {
  430. wl1271_top_reg_write(wl, MCS_PLL_M_REG, MCS_PLL_M_REG_VAL);
  431. wl1271_top_reg_write(wl, MCS_PLL_N_REG, MCS_PLL_N_REG_VAL);
  432. wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, MCS_PLL_CONFIG_REG_VAL);
  433. return 0;
  434. }
  435. static int wl128x_configure_mcs_pll(struct wl1271 *wl, int clk)
  436. {
  437. u16 spare_reg;
  438. u16 pll_config;
  439. u8 input_freq;
  440. /* Mask bits [3:1] in the sys_clk_cfg register */
  441. spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
  442. if (spare_reg == 0xFFFF)
  443. return -EFAULT;
  444. spare_reg |= BIT(2);
  445. wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
  446. /* Handle special cases of the TCXO clock */
  447. if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_8 ||
  448. wl->tcxo_clock == WL12XX_TCXOCLOCK_33_6)
  449. return wl128x_manually_configure_mcs_pll(wl);
  450. /* Set the input frequency according to the selected clock source */
  451. input_freq = (clk & 1) + 1;
  452. pll_config = wl1271_top_reg_read(wl, MCS_PLL_CONFIG_REG);
  453. if (pll_config == 0xFFFF)
  454. return -EFAULT;
  455. pll_config |= (input_freq << MCS_SEL_IN_FREQ_SHIFT);
  456. pll_config |= MCS_PLL_ENABLE_HP;
  457. wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, pll_config);
  458. return 0;
  459. }
  460. /*
  461. * WL128x has two clocks input - TCXO and FREF.
  462. * TCXO is the main clock of the device, while FREF is used to sync
  463. * between the GPS and the cellular modem.
  464. * In cases where TCXO is 32.736MHz or 16.368MHz, the FREF will be used
  465. * as the WLAN/BT main clock.
  466. */
  467. static int wl128x_boot_clk(struct wl1271 *wl, int *selected_clock)
  468. {
  469. u16 sys_clk_cfg;
  470. /* For XTAL-only modes, FREF will be used after switching from TCXO */
  471. if (wl->ref_clock == WL12XX_REFCLOCK_26_XTAL ||
  472. wl->ref_clock == WL12XX_REFCLOCK_38_XTAL) {
  473. if (!wl128x_switch_tcxo_to_fref(wl))
  474. return -EINVAL;
  475. goto fref_clk;
  476. }
  477. /* Query the HW, to determine which clock source we should use */
  478. sys_clk_cfg = wl1271_top_reg_read(wl, SYS_CLK_CFG_REG);
  479. if (sys_clk_cfg == 0xFFFF)
  480. return -EINVAL;
  481. if (sys_clk_cfg & PRCM_CM_EN_MUX_WLAN_FREF)
  482. goto fref_clk;
  483. /* If TCXO is either 32.736MHz or 16.368MHz, switch to FREF */
  484. if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_368 ||
  485. wl->tcxo_clock == WL12XX_TCXOCLOCK_32_736) {
  486. if (!wl128x_switch_tcxo_to_fref(wl))
  487. return -EINVAL;
  488. goto fref_clk;
  489. }
  490. /* TCXO clock is selected */
  491. if (!wl128x_is_tcxo_valid(wl))
  492. return -EINVAL;
  493. *selected_clock = wl->tcxo_clock;
  494. goto config_mcs_pll;
  495. fref_clk:
  496. /* FREF clock is selected */
  497. if (!wl128x_is_fref_valid(wl))
  498. return -EINVAL;
  499. *selected_clock = wl->ref_clock;
  500. config_mcs_pll:
  501. return wl128x_configure_mcs_pll(wl, *selected_clock);
  502. }
  503. static int wl127x_boot_clk(struct wl1271 *wl)
  504. {
  505. u32 pause;
  506. u32 clk;
  507. if (WL127X_PG_GET_MAJOR(wl->hw_pg_ver) < 3)
  508. wl->quirks |= WL12XX_QUIRK_END_OF_TRANSACTION;
  509. if (wl->ref_clock == CONF_REF_CLK_19_2_E ||
  510. wl->ref_clock == CONF_REF_CLK_38_4_E ||
  511. wl->ref_clock == CONF_REF_CLK_38_4_M_XTAL)
  512. /* ref clk: 19.2/38.4/38.4-XTAL */
  513. clk = 0x3;
  514. else if (wl->ref_clock == CONF_REF_CLK_26_E ||
  515. wl->ref_clock == CONF_REF_CLK_52_E)
  516. /* ref clk: 26/52 */
  517. clk = 0x5;
  518. else
  519. return -EINVAL;
  520. if (wl->ref_clock != CONF_REF_CLK_19_2_E) {
  521. u16 val;
  522. /* Set clock type (open drain) */
  523. val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
  524. val &= FREF_CLK_TYPE_BITS;
  525. wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
  526. /* Set clock pull mode (no pull) */
  527. val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
  528. val |= NO_PULL;
  529. wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
  530. } else {
  531. u16 val;
  532. /* Set clock polarity */
  533. val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
  534. val &= FREF_CLK_POLARITY_BITS;
  535. val |= CLK_REQ_OUTN_SEL;
  536. wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
  537. }
  538. wl1271_write32(wl, PLL_PARAMETERS, clk);
  539. pause = wl1271_read32(wl, PLL_PARAMETERS);
  540. wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
  541. pause &= ~(WU_COUNTER_PAUSE_VAL);
  542. pause |= WU_COUNTER_PAUSE_VAL;
  543. wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
  544. return 0;
  545. }
  546. /* uploads NVS and firmware */
  547. int wl1271_load_firmware(struct wl1271 *wl)
  548. {
  549. int ret = 0;
  550. u32 tmp, clk;
  551. int selected_clock = -1;
  552. if (wl->chip.id == CHIP_ID_1283_PG20) {
  553. ret = wl128x_boot_clk(wl, &selected_clock);
  554. if (ret < 0)
  555. goto out;
  556. } else {
  557. ret = wl127x_boot_clk(wl);
  558. if (ret < 0)
  559. goto out;
  560. }
  561. /* Continue the ELP wake up sequence */
  562. wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
  563. udelay(500);
  564. wl1271_set_partition(wl, &wl12xx_part_table[PART_DRPW]);
  565. /* Read-modify-write DRPW_SCRATCH_START register (see next state)
  566. to be used by DRPw FW. The RTRIM value will be added by the FW
  567. before taking DRPw out of reset */
  568. wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
  569. clk = wl1271_read32(wl, DRPW_SCRATCH_START);
  570. wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
  571. if (wl->chip.id == CHIP_ID_1283_PG20) {
  572. clk |= ((selected_clock & 0x3) << 1) << 4;
  573. } else {
  574. clk |= (wl->ref_clock << 1) << 4;
  575. }
  576. wl1271_write32(wl, DRPW_SCRATCH_START, clk);
  577. wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
  578. /* Disable interrupts */
  579. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
  580. ret = wl1271_boot_soft_reset(wl);
  581. if (ret < 0)
  582. goto out;
  583. /* 2. start processing NVS file */
  584. ret = wl1271_boot_upload_nvs(wl);
  585. if (ret < 0)
  586. goto out;
  587. /* write firmware's last address (ie. it's length) to
  588. * ACX_EEPROMLESS_IND_REG */
  589. wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
  590. wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
  591. tmp = wl1271_read32(wl, CHIP_ID_B);
  592. wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
  593. /* 6. read the EEPROM parameters */
  594. tmp = wl1271_read32(wl, SCR_PAD2);
  595. /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
  596. * to upload_fw) */
  597. if (wl->chip.id == CHIP_ID_1283_PG20)
  598. wl1271_top_reg_write(wl, SDIO_IO_DS, wl->conf.hci_io_ds);
  599. ret = wl1271_boot_upload_firmware(wl);
  600. if (ret < 0)
  601. goto out;
  602. out:
  603. return ret;
  604. }
  605. EXPORT_SYMBOL_GPL(wl1271_load_firmware);
  606. int wl1271_boot(struct wl1271 *wl)
  607. {
  608. int ret;
  609. /* upload NVS and firmware */
  610. ret = wl1271_load_firmware(wl);
  611. if (ret)
  612. return ret;
  613. /* 10.5 start firmware */
  614. ret = wl1271_boot_run_firmware(wl);
  615. if (ret < 0)
  616. goto out;
  617. ret = wl1271_boot_write_irq_polarity(wl);
  618. if (ret < 0)
  619. goto out;
  620. wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
  621. WL1271_ACX_ALL_EVENTS_VECTOR);
  622. /* Enable firmware interrupts now */
  623. wl1271_boot_enable_interrupts(wl);
  624. wl1271_event_mbox_config(wl);
  625. out:
  626. return ret;
  627. }