ucode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
  6. * Copyright(c) 2015 Intel Deutschland GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of version 2 of the GNU General Public License as
  10. * 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 Street, Fifth Floor, Boston, MA 02110,
  20. * USA
  21. *
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <linuxwifi@intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. *****************************************************************************/
  30. #include <linux/kernel.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. /*
  45. * Calibration
  46. */
  47. static int iwl_set_Xtal_calib(struct iwl_priv *priv)
  48. {
  49. struct iwl_calib_xtal_freq_cmd cmd;
  50. __le16 *xtal_calib = priv->nvm_data->xtal_calib;
  51. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
  52. cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
  53. cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
  54. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  55. }
  56. static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
  57. {
  58. struct iwl_calib_temperature_offset_cmd cmd;
  59. memset(&cmd, 0, sizeof(cmd));
  60. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  61. cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
  62. if (!(cmd.radio_sensor_offset))
  63. cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
  64. IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
  65. le16_to_cpu(cmd.radio_sensor_offset));
  66. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  67. }
  68. static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
  69. {
  70. struct iwl_calib_temperature_offset_v2_cmd cmd;
  71. memset(&cmd, 0, sizeof(cmd));
  72. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  73. cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
  74. cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
  75. if (!cmd.radio_sensor_offset_low) {
  76. IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
  77. cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
  78. cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
  79. }
  80. cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
  81. IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
  82. le16_to_cpu(cmd.radio_sensor_offset_high));
  83. IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
  84. le16_to_cpu(cmd.radio_sensor_offset_low));
  85. IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
  86. le16_to_cpu(cmd.burntVoltageRef));
  87. return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
  88. }
  89. static int iwl_send_calib_cfg(struct iwl_priv *priv)
  90. {
  91. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  92. struct iwl_host_cmd cmd = {
  93. .id = CALIBRATION_CFG_CMD,
  94. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  95. .data = { &calib_cfg_cmd, },
  96. };
  97. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  98. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  99. calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
  100. calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
  101. calib_cfg_cmd.ucd_calib_cfg.flags =
  102. IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
  103. return iwl_dvm_send_cmd(priv, &cmd);
  104. }
  105. int iwl_init_alive_start(struct iwl_priv *priv)
  106. {
  107. int ret;
  108. if (priv->lib->bt_params &&
  109. priv->lib->bt_params->advanced_bt_coexist) {
  110. /*
  111. * Tell uCode we are ready to perform calibration
  112. * need to perform this before any calibration
  113. * no need to close the envlope since we are going
  114. * to load the runtime uCode later.
  115. */
  116. ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
  117. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  118. if (ret)
  119. return ret;
  120. }
  121. ret = iwl_send_calib_cfg(priv);
  122. if (ret)
  123. return ret;
  124. /**
  125. * temperature offset calibration is only needed for runtime ucode,
  126. * so prepare the value now.
  127. */
  128. if (priv->lib->need_temp_offset_calib) {
  129. if (priv->lib->temp_offset_v2)
  130. return iwl_set_temperature_offset_calib_v2(priv);
  131. else
  132. return iwl_set_temperature_offset_calib(priv);
  133. }
  134. return 0;
  135. }
  136. static int iwl_send_wimax_coex(struct iwl_priv *priv)
  137. {
  138. struct iwl_wimax_coex_cmd coex_cmd;
  139. /* coexistence is disabled */
  140. memset(&coex_cmd, 0, sizeof(coex_cmd));
  141. return iwl_dvm_send_cmd_pdu(priv,
  142. COEX_PRIORITY_TABLE_CMD, 0,
  143. sizeof(coex_cmd), &coex_cmd);
  144. }
  145. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  146. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  147. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  148. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  149. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  150. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  151. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  152. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  153. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  154. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  155. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  156. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  157. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  158. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  159. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  160. ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  161. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  162. ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  163. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  164. 0, 0, 0, 0, 0, 0, 0
  165. };
  166. void iwl_send_prio_tbl(struct iwl_priv *priv)
  167. {
  168. struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
  169. memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
  170. sizeof(iwl_bt_prio_tbl));
  171. if (iwl_dvm_send_cmd_pdu(priv,
  172. REPLY_BT_COEX_PRIO_TABLE, 0,
  173. sizeof(prio_tbl_cmd), &prio_tbl_cmd))
  174. IWL_ERR(priv, "failed to send BT prio tbl command\n");
  175. }
  176. int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
  177. {
  178. struct iwl_bt_coex_prot_env_cmd env_cmd;
  179. int ret;
  180. env_cmd.action = action;
  181. env_cmd.type = type;
  182. ret = iwl_dvm_send_cmd_pdu(priv,
  183. REPLY_BT_COEX_PROT_ENV, 0,
  184. sizeof(env_cmd), &env_cmd);
  185. if (ret)
  186. IWL_ERR(priv, "failed to send BT env command\n");
  187. return ret;
  188. }
  189. static const u8 iwlagn_default_queue_to_tx_fifo[] = {
  190. IWL_TX_FIFO_VO,
  191. IWL_TX_FIFO_VI,
  192. IWL_TX_FIFO_BE,
  193. IWL_TX_FIFO_BK,
  194. };
  195. static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
  196. IWL_TX_FIFO_VO,
  197. IWL_TX_FIFO_VI,
  198. IWL_TX_FIFO_BE,
  199. IWL_TX_FIFO_BK,
  200. IWL_TX_FIFO_BK_IPAN,
  201. IWL_TX_FIFO_BE_IPAN,
  202. IWL_TX_FIFO_VI_IPAN,
  203. IWL_TX_FIFO_VO_IPAN,
  204. IWL_TX_FIFO_BE_IPAN,
  205. IWL_TX_FIFO_UNUSED,
  206. IWL_TX_FIFO_AUX,
  207. };
  208. static int iwl_alive_notify(struct iwl_priv *priv)
  209. {
  210. const u8 *queue_to_txf;
  211. u8 n_queues;
  212. int ret;
  213. int i;
  214. iwl_trans_fw_alive(priv->trans, 0);
  215. if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
  216. priv->nvm_data->sku_cap_ipan_enable) {
  217. n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
  218. queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
  219. } else {
  220. n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
  221. queue_to_txf = iwlagn_default_queue_to_tx_fifo;
  222. }
  223. for (i = 0; i < n_queues; i++)
  224. if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
  225. iwl_trans_ac_txq_enable(priv->trans, i,
  226. queue_to_txf[i], 0);
  227. priv->passive_no_rx = false;
  228. priv->transport_queue_stop = 0;
  229. ret = iwl_send_wimax_coex(priv);
  230. if (ret)
  231. return ret;
  232. if (!priv->lib->no_xtal_calib) {
  233. ret = iwl_set_Xtal_calib(priv);
  234. if (ret)
  235. return ret;
  236. }
  237. return iwl_send_calib_results(priv);
  238. }
  239. struct iwl_alive_data {
  240. bool valid;
  241. u8 subtype;
  242. };
  243. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  244. struct iwl_rx_packet *pkt, void *data)
  245. {
  246. struct iwl_priv *priv =
  247. container_of(notif_wait, struct iwl_priv, notif_wait);
  248. struct iwl_alive_data *alive_data = data;
  249. struct iwl_alive_resp *palive;
  250. palive = (void *)pkt->data;
  251. IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
  252. "0x%01X 0x%01X\n",
  253. palive->is_valid, palive->ver_type,
  254. palive->ver_subtype);
  255. priv->device_pointers.error_event_table =
  256. le32_to_cpu(palive->error_event_table_ptr);
  257. priv->device_pointers.log_event_table =
  258. le32_to_cpu(palive->log_event_table_ptr);
  259. alive_data->subtype = palive->ver_subtype;
  260. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  261. return true;
  262. }
  263. #define UCODE_ALIVE_TIMEOUT HZ
  264. #define UCODE_CALIB_TIMEOUT (2*HZ)
  265. int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
  266. enum iwl_ucode_type ucode_type)
  267. {
  268. struct iwl_notification_wait alive_wait;
  269. struct iwl_alive_data alive_data;
  270. const struct fw_img *fw;
  271. int ret;
  272. enum iwl_ucode_type old_type;
  273. static const u16 alive_cmd[] = { REPLY_ALIVE };
  274. fw = iwl_get_ucode_image(priv->fw, ucode_type);
  275. if (WARN_ON(!fw))
  276. return -EINVAL;
  277. old_type = priv->cur_ucode;
  278. priv->cur_ucode = ucode_type;
  279. priv->ucode_loaded = false;
  280. iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
  281. alive_cmd, ARRAY_SIZE(alive_cmd),
  282. iwl_alive_fn, &alive_data);
  283. ret = iwl_trans_start_fw(priv->trans, fw, false);
  284. if (ret) {
  285. priv->cur_ucode = old_type;
  286. iwl_remove_notification(&priv->notif_wait, &alive_wait);
  287. return ret;
  288. }
  289. /*
  290. * Some things may run in the background now, but we
  291. * just wait for the ALIVE notification here.
  292. */
  293. ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
  294. UCODE_ALIVE_TIMEOUT);
  295. if (ret) {
  296. priv->cur_ucode = old_type;
  297. return ret;
  298. }
  299. if (!alive_data.valid) {
  300. IWL_ERR(priv, "Loaded ucode is not valid!\n");
  301. priv->cur_ucode = old_type;
  302. return -EIO;
  303. }
  304. priv->ucode_loaded = true;
  305. if (ucode_type != IWL_UCODE_WOWLAN) {
  306. /* delay a bit to give rfkill time to run */
  307. msleep(5);
  308. }
  309. ret = iwl_alive_notify(priv);
  310. if (ret) {
  311. IWL_WARN(priv,
  312. "Could not complete ALIVE transition: %d\n", ret);
  313. priv->cur_ucode = old_type;
  314. return ret;
  315. }
  316. return 0;
  317. }
  318. static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
  319. struct iwl_rx_packet *pkt, void *data)
  320. {
  321. struct iwl_priv *priv = data;
  322. struct iwl_calib_hdr *hdr;
  323. if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
  324. WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
  325. return true;
  326. }
  327. hdr = (struct iwl_calib_hdr *)pkt->data;
  328. if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
  329. IWL_ERR(priv, "Failed to record calibration data %d\n",
  330. hdr->op_code);
  331. return false;
  332. }
  333. int iwl_run_init_ucode(struct iwl_priv *priv)
  334. {
  335. struct iwl_notification_wait calib_wait;
  336. static const u16 calib_complete[] = {
  337. CALIBRATION_RES_NOTIFICATION,
  338. CALIBRATION_COMPLETE_NOTIFICATION
  339. };
  340. int ret;
  341. lockdep_assert_held(&priv->mutex);
  342. /* No init ucode required? Curious, but maybe ok */
  343. if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
  344. return 0;
  345. iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
  346. calib_complete, ARRAY_SIZE(calib_complete),
  347. iwlagn_wait_calib, priv);
  348. /* Will also start the device */
  349. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  350. if (ret)
  351. goto error;
  352. ret = iwl_init_alive_start(priv);
  353. if (ret)
  354. goto error;
  355. /*
  356. * Some things may run in the background now, but we
  357. * just wait for the calibration complete notification.
  358. */
  359. ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
  360. UCODE_CALIB_TIMEOUT);
  361. goto out;
  362. error:
  363. iwl_remove_notification(&priv->notif_wait, &calib_wait);
  364. out:
  365. /* Whatever happened, stop the device */
  366. iwl_trans_stop_device(priv->trans);
  367. priv->ucode_loaded = false;
  368. return ret;
  369. }