boot.c 21 KB

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