iwl-ucode.c 14 KB

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