boot.c 21 KB

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