iwl-ucode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include "iwl-dev.h"
  32. #include "iwl-core.h"
  33. #include "iwl-io.h"
  34. #include "iwl-agn-hw.h"
  35. #include "iwl-agn.h"
  36. #include "iwl-agn-calib.h"
  37. #include "iwl-trans.h"
  38. #include "iwl-fh.h"
  39. #include "iwl-op-mode.h"
  40. /******************************************************************************
  41. *
  42. * uCode download functions
  43. *
  44. ******************************************************************************/
  45. static inline const struct fw_img *
  46. iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
  47. {
  48. if (ucode_type >= IWL_UCODE_TYPE_MAX)
  49. return NULL;
  50. return &priv->fw->img[ucode_type];
  51. }
  52. /*
  53. * Calibration
  54. */
  55. static int iwl_set_Xtal_calib(struct iwl_priv *priv)
  56. {
  57. struct iwl_calib_xtal_freq_cmd cmd;
  58. __le16 *xtal_calib =
  59. (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
  60. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
  61. cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
  62. cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
  63. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  64. }
  65. static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
  66. {
  67. struct iwl_calib_temperature_offset_cmd cmd;
  68. __le16 *offset_calib =
  69. (__le16 *)iwl_eeprom_query_addr(priv->shrd,
  70. EEPROM_RAW_TEMPERATURE);
  71. memset(&cmd, 0, sizeof(cmd));
  72. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  73. memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
  74. if (!(cmd.radio_sensor_offset))
  75. cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
  76. IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
  77. le16_to_cpu(cmd.radio_sensor_offset));
  78. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  79. }
  80. static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
  81. {
  82. struct iwl_calib_temperature_offset_v2_cmd cmd;
  83. __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
  84. EEPROM_KELVIN_TEMPERATURE);
  85. __le16 *offset_calib_low =
  86. (__le16 *)iwl_eeprom_query_addr(priv->shrd,
  87. EEPROM_RAW_TEMPERATURE);
  88. struct iwl_eeprom_calib_hdr *hdr;
  89. memset(&cmd, 0, sizeof(cmd));
  90. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  91. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
  92. EEPROM_CALIB_ALL);
  93. memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
  94. sizeof(*offset_calib_high));
  95. memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
  96. sizeof(*offset_calib_low));
  97. if (!(cmd.radio_sensor_offset_low)) {
  98. IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
  99. cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
  100. cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
  101. }
  102. memcpy(&cmd.burntVoltageRef, &hdr->voltage,
  103. sizeof(hdr->voltage));
  104. IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
  105. le16_to_cpu(cmd.radio_sensor_offset_high));
  106. IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
  107. le16_to_cpu(cmd.radio_sensor_offset_low));
  108. IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
  109. le16_to_cpu(cmd.burntVoltageRef));
  110. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  111. }
  112. static int iwl_send_calib_cfg(struct iwl_priv *priv)
  113. {
  114. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  115. struct iwl_host_cmd cmd = {
  116. .id = CALIBRATION_CFG_CMD,
  117. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  118. .data = { &calib_cfg_cmd, },
  119. };
  120. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  121. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  122. calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
  123. calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
  124. calib_cfg_cmd.ucd_calib_cfg.flags =
  125. IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
  126. return iwl_dvm_send_cmd(priv, &cmd);
  127. }
  128. int iwl_init_alive_start(struct iwl_priv *priv)
  129. {
  130. int ret;
  131. if (cfg(priv)->bt_params &&
  132. cfg(priv)->bt_params->advanced_bt_coexist) {
  133. /*
  134. * Tell uCode we are ready to perform calibration
  135. * need to perform this before any calibration
  136. * no need to close the envlope since we are going
  137. * to load the runtime uCode later.
  138. */
  139. ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
  140. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  141. if (ret)
  142. return ret;
  143. }
  144. ret = iwl_send_calib_cfg(priv);
  145. if (ret)
  146. return ret;
  147. /**
  148. * temperature offset calibration is only needed for runtime ucode,
  149. * so prepare the value now.
  150. */
  151. if (cfg(priv)->need_temp_offset_calib) {
  152. if (cfg(priv)->temp_offset_v2)
  153. return iwl_set_temperature_offset_calib_v2(priv);
  154. else
  155. return iwl_set_temperature_offset_calib(priv);
  156. }
  157. return 0;
  158. }
  159. static int iwl_send_wimax_coex(struct iwl_priv *priv)
  160. {
  161. struct iwl_wimax_coex_cmd coex_cmd;
  162. /* coexistence is disabled */
  163. memset(&coex_cmd, 0, sizeof(coex_cmd));
  164. return iwl_dvm_send_cmd_pdu(priv,
  165. COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
  166. sizeof(coex_cmd), &coex_cmd);
  167. }
  168. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  169. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  170. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  171. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  172. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  173. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  174. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  175. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  176. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  177. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  178. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  179. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  180. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  181. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  182. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  183. ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  184. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  185. ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  186. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  187. 0, 0, 0, 0, 0, 0, 0
  188. };
  189. void iwl_send_prio_tbl(struct iwl_priv *priv)
  190. {
  191. struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
  192. memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
  193. sizeof(iwl_bt_prio_tbl));
  194. if (iwl_dvm_send_cmd_pdu(priv,
  195. REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
  196. sizeof(prio_tbl_cmd), &prio_tbl_cmd))
  197. IWL_ERR(priv, "failed to send BT prio tbl command\n");
  198. }
  199. int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
  200. {
  201. struct iwl_bt_coex_prot_env_cmd env_cmd;
  202. int ret;
  203. env_cmd.action = action;
  204. env_cmd.type = type;
  205. ret = iwl_dvm_send_cmd_pdu(priv,
  206. REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
  207. sizeof(env_cmd), &env_cmd);
  208. if (ret)
  209. IWL_ERR(priv, "failed to send BT env command\n");
  210. return ret;
  211. }
  212. static int iwl_alive_notify(struct iwl_priv *priv)
  213. {
  214. int ret;
  215. iwl_trans_fw_alive(trans(priv));
  216. priv->passive_no_rx = false;
  217. priv->transport_queue_stop = 0;
  218. ret = iwl_send_wimax_coex(priv);
  219. if (ret)
  220. return ret;
  221. if (!cfg(priv)->no_xtal_calib) {
  222. ret = iwl_set_Xtal_calib(priv);
  223. if (ret)
  224. return ret;
  225. }
  226. return iwl_send_calib_results(priv);
  227. }
  228. /**
  229. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  230. * using sample data 100 bytes apart. If these sample points are good,
  231. * it's a pretty good bet that everything between them is good, too.
  232. */
  233. static int iwl_verify_sec_sparse(struct iwl_priv *priv,
  234. const struct fw_desc *fw_desc)
  235. {
  236. __le32 *image = (__le32 *)fw_desc->v_addr;
  237. u32 len = fw_desc->len;
  238. u32 val;
  239. u32 i;
  240. IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
  241. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  242. /* read data comes through single port, auto-incr addr */
  243. /* NOTE: Use the debugless read so we don't flood kernel log
  244. * if IWL_DL_IO is set */
  245. iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
  246. i + fw_desc->offset);
  247. val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  248. if (val != le32_to_cpu(*image))
  249. return -EIO;
  250. }
  251. return 0;
  252. }
  253. static void iwl_print_mismatch_sec(struct iwl_priv *priv,
  254. const struct fw_desc *fw_desc)
  255. {
  256. __le32 *image = (__le32 *)fw_desc->v_addr;
  257. u32 len = fw_desc->len;
  258. u32 val;
  259. u32 offs;
  260. int errors = 0;
  261. IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
  262. iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
  263. fw_desc->offset);
  264. for (offs = 0;
  265. offs < len && errors < 20;
  266. offs += sizeof(u32), image++) {
  267. /* read data comes through single port, auto-incr addr */
  268. val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  269. if (val != le32_to_cpu(*image)) {
  270. IWL_ERR(priv, "uCode INST section at "
  271. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  272. offs, val, le32_to_cpu(*image));
  273. errors++;
  274. }
  275. }
  276. }
  277. /**
  278. * iwl_verify_ucode - determine which instruction image is in SRAM,
  279. * and verify its contents
  280. */
  281. static int iwl_verify_ucode(struct iwl_priv *priv,
  282. enum iwl_ucode_type ucode_type)
  283. {
  284. const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
  285. if (!img) {
  286. IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
  287. return -EINVAL;
  288. }
  289. if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
  290. IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
  291. return 0;
  292. }
  293. IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
  294. iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
  295. return -EIO;
  296. }
  297. struct iwl_alive_data {
  298. bool valid;
  299. u8 subtype;
  300. };
  301. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  302. struct iwl_rx_packet *pkt, void *data)
  303. {
  304. struct iwl_priv *priv =
  305. container_of(notif_wait, struct iwl_priv, notif_wait);
  306. struct iwl_alive_data *alive_data = data;
  307. struct iwl_alive_resp *palive;
  308. palive = (void *)pkt->data;
  309. IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
  310. "0x%01X 0x%01X\n",
  311. palive->is_valid, palive->ver_type,
  312. palive->ver_subtype);
  313. priv->device_pointers.error_event_table =
  314. le32_to_cpu(palive->error_event_table_ptr);
  315. priv->device_pointers.log_event_table =
  316. le32_to_cpu(palive->log_event_table_ptr);
  317. alive_data->subtype = palive->ver_subtype;
  318. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  319. return true;
  320. }
  321. #define UCODE_ALIVE_TIMEOUT HZ
  322. #define UCODE_CALIB_TIMEOUT (2*HZ)
  323. int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
  324. enum iwl_ucode_type ucode_type)
  325. {
  326. struct iwl_notification_wait alive_wait;
  327. struct iwl_alive_data alive_data;
  328. const struct fw_img *fw;
  329. int ret;
  330. enum iwl_ucode_type old_type;
  331. static const u8 alive_cmd[] = { REPLY_ALIVE };
  332. old_type = priv->cur_ucode;
  333. priv->cur_ucode = ucode_type;
  334. fw = iwl_get_ucode_image(priv, ucode_type);
  335. priv->ucode_loaded = false;
  336. if (!fw)
  337. return -EINVAL;
  338. iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
  339. alive_cmd, ARRAY_SIZE(alive_cmd),
  340. iwl_alive_fn, &alive_data);
  341. ret = iwl_trans_start_fw(trans(priv), fw);
  342. if (ret) {
  343. priv->cur_ucode = old_type;
  344. iwl_remove_notification(&priv->notif_wait, &alive_wait);
  345. return ret;
  346. }
  347. /*
  348. * Some things may run in the background now, but we
  349. * just wait for the ALIVE notification here.
  350. */
  351. ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
  352. UCODE_ALIVE_TIMEOUT);
  353. if (ret) {
  354. priv->cur_ucode = old_type;
  355. return ret;
  356. }
  357. if (!alive_data.valid) {
  358. IWL_ERR(priv, "Loaded ucode is not valid!\n");
  359. priv->cur_ucode = old_type;
  360. return -EIO;
  361. }
  362. /*
  363. * This step takes a long time (60-80ms!!) and
  364. * WoWLAN image should be loaded quickly, so
  365. * skip it for WoWLAN.
  366. */
  367. if (ucode_type != IWL_UCODE_WOWLAN) {
  368. ret = iwl_verify_ucode(priv, ucode_type);
  369. if (ret) {
  370. priv->cur_ucode = old_type;
  371. return ret;
  372. }
  373. /* delay a bit to give rfkill time to run */
  374. msleep(5);
  375. }
  376. ret = iwl_alive_notify(priv);
  377. if (ret) {
  378. IWL_WARN(priv,
  379. "Could not complete ALIVE transition: %d\n", ret);
  380. priv->cur_ucode = old_type;
  381. return ret;
  382. }
  383. priv->ucode_loaded = true;
  384. return 0;
  385. }
  386. static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
  387. struct iwl_rx_packet *pkt, void *data)
  388. {
  389. struct iwl_priv *priv = data;
  390. struct iwl_calib_hdr *hdr;
  391. int len;
  392. if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
  393. WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
  394. return true;
  395. }
  396. hdr = (struct iwl_calib_hdr *)pkt->data;
  397. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  398. /* reduce the size by the length field itself */
  399. len -= sizeof(__le32);
  400. if (iwl_calib_set(priv, hdr, len))
  401. IWL_ERR(priv, "Failed to record calibration data %d\n",
  402. hdr->op_code);
  403. return false;
  404. }
  405. int iwl_run_init_ucode(struct iwl_priv *priv)
  406. {
  407. struct iwl_notification_wait calib_wait;
  408. static const u8 calib_complete[] = {
  409. CALIBRATION_RES_NOTIFICATION,
  410. CALIBRATION_COMPLETE_NOTIFICATION
  411. };
  412. int ret;
  413. lockdep_assert_held(&priv->mutex);
  414. /* No init ucode required? Curious, but maybe ok */
  415. if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
  416. return 0;
  417. if (priv->init_ucode_run)
  418. return 0;
  419. iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
  420. calib_complete, ARRAY_SIZE(calib_complete),
  421. iwlagn_wait_calib, priv);
  422. /* Will also start the device */
  423. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  424. if (ret)
  425. goto error;
  426. ret = iwl_init_alive_start(priv);
  427. if (ret)
  428. goto error;
  429. /*
  430. * Some things may run in the background now, but we
  431. * just wait for the calibration complete notification.
  432. */
  433. ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
  434. UCODE_CALIB_TIMEOUT);
  435. if (!ret)
  436. priv->init_ucode_run = true;
  437. goto out;
  438. error:
  439. iwl_remove_notification(&priv->notif_wait, &calib_wait);
  440. out:
  441. /* Whatever happened, stop the device */
  442. iwl_trans_stop_device(trans(priv));
  443. priv->ucode_loaded = false;
  444. return ret;
  445. }