ucode.c 12 KB

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