iwl-ucode.c 14 KB

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