boot.c 21 KB

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