ucode.c 14 KB

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