ucode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2013 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 <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->nvm_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->nvm_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->nvm_data->kelvin_temperature;
  81. cmd.radio_sensor_offset_low = priv->nvm_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->nvm_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 const u8 iwlagn_default_queue_to_tx_fifo[] = {
  197. IWL_TX_FIFO_VO,
  198. IWL_TX_FIFO_VI,
  199. IWL_TX_FIFO_BE,
  200. IWL_TX_FIFO_BK,
  201. };
  202. static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
  203. IWL_TX_FIFO_VO,
  204. IWL_TX_FIFO_VI,
  205. IWL_TX_FIFO_BE,
  206. IWL_TX_FIFO_BK,
  207. IWL_TX_FIFO_BK_IPAN,
  208. IWL_TX_FIFO_BE_IPAN,
  209. IWL_TX_FIFO_VI_IPAN,
  210. IWL_TX_FIFO_VO_IPAN,
  211. IWL_TX_FIFO_BE_IPAN,
  212. IWL_TX_FIFO_UNUSED,
  213. IWL_TX_FIFO_AUX,
  214. };
  215. static int iwl_alive_notify(struct iwl_priv *priv)
  216. {
  217. const u8 *queue_to_txf;
  218. u8 n_queues;
  219. int ret;
  220. int i;
  221. iwl_trans_fw_alive(priv->trans, 0);
  222. if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
  223. priv->nvm_data->sku_cap_ipan_enable) {
  224. n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
  225. queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
  226. } else {
  227. n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
  228. queue_to_txf = iwlagn_default_queue_to_tx_fifo;
  229. }
  230. for (i = 0; i < n_queues; i++)
  231. if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
  232. iwl_trans_ac_txq_enable(priv->trans, i,
  233. queue_to_txf[i]);
  234. priv->passive_no_rx = false;
  235. priv->transport_queue_stop = 0;
  236. ret = iwl_send_wimax_coex(priv);
  237. if (ret)
  238. return ret;
  239. if (!priv->cfg->no_xtal_calib) {
  240. ret = iwl_set_Xtal_calib(priv);
  241. if (ret)
  242. return ret;
  243. }
  244. return iwl_send_calib_results(priv);
  245. }
  246. struct iwl_alive_data {
  247. bool valid;
  248. u8 subtype;
  249. };
  250. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  251. struct iwl_rx_packet *pkt, void *data)
  252. {
  253. struct iwl_priv *priv =
  254. container_of(notif_wait, struct iwl_priv, notif_wait);
  255. struct iwl_alive_data *alive_data = data;
  256. struct iwl_alive_resp *palive;
  257. palive = (void *)pkt->data;
  258. IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
  259. "0x%01X 0x%01X\n",
  260. palive->is_valid, palive->ver_type,
  261. palive->ver_subtype);
  262. priv->device_pointers.error_event_table =
  263. le32_to_cpu(palive->error_event_table_ptr);
  264. priv->device_pointers.log_event_table =
  265. le32_to_cpu(palive->log_event_table_ptr);
  266. alive_data->subtype = palive->ver_subtype;
  267. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  268. return true;
  269. }
  270. #define UCODE_ALIVE_TIMEOUT HZ
  271. #define UCODE_CALIB_TIMEOUT (2*HZ)
  272. int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
  273. enum iwl_ucode_type ucode_type)
  274. {
  275. struct iwl_notification_wait alive_wait;
  276. struct iwl_alive_data alive_data;
  277. const struct fw_img *fw;
  278. int ret;
  279. enum iwl_ucode_type old_type;
  280. static const u8 alive_cmd[] = { REPLY_ALIVE };
  281. old_type = priv->cur_ucode;
  282. priv->cur_ucode = ucode_type;
  283. fw = iwl_get_ucode_image(priv, ucode_type);
  284. priv->ucode_loaded = false;
  285. if (!fw)
  286. return -EINVAL;
  287. iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
  288. alive_cmd, ARRAY_SIZE(alive_cmd),
  289. iwl_alive_fn, &alive_data);
  290. ret = iwl_trans_start_fw(priv->trans, fw, false);
  291. if (ret) {
  292. priv->cur_ucode = old_type;
  293. iwl_remove_notification(&priv->notif_wait, &alive_wait);
  294. return ret;
  295. }
  296. /*
  297. * Some things may run in the background now, but we
  298. * just wait for the ALIVE notification here.
  299. */
  300. ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
  301. UCODE_ALIVE_TIMEOUT);
  302. if (ret) {
  303. priv->cur_ucode = old_type;
  304. return ret;
  305. }
  306. if (!alive_data.valid) {
  307. IWL_ERR(priv, "Loaded ucode is not valid!\n");
  308. priv->cur_ucode = old_type;
  309. return -EIO;
  310. }
  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. priv->ucode_loaded = true;
  323. return 0;
  324. }
  325. static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
  326. struct iwl_rx_packet *pkt, void *data)
  327. {
  328. struct iwl_priv *priv = data;
  329. struct iwl_calib_hdr *hdr;
  330. int len;
  331. if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
  332. WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
  333. return true;
  334. }
  335. hdr = (struct iwl_calib_hdr *)pkt->data;
  336. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  337. /* reduce the size by the length field itself */
  338. len -= sizeof(__le32);
  339. if (iwl_calib_set(priv, hdr, len))
  340. IWL_ERR(priv, "Failed to record calibration data %d\n",
  341. hdr->op_code);
  342. return false;
  343. }
  344. int iwl_run_init_ucode(struct iwl_priv *priv)
  345. {
  346. struct iwl_notification_wait calib_wait;
  347. static const u8 calib_complete[] = {
  348. CALIBRATION_RES_NOTIFICATION,
  349. CALIBRATION_COMPLETE_NOTIFICATION
  350. };
  351. int ret;
  352. lockdep_assert_held(&priv->mutex);
  353. /* No init ucode required? Curious, but maybe ok */
  354. if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
  355. return 0;
  356. if (priv->init_ucode_run)
  357. return 0;
  358. iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
  359. calib_complete, ARRAY_SIZE(calib_complete),
  360. iwlagn_wait_calib, priv);
  361. /* Will also start the device */
  362. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  363. if (ret)
  364. goto error;
  365. ret = iwl_init_alive_start(priv);
  366. if (ret)
  367. goto error;
  368. /*
  369. * Some things may run in the background now, but we
  370. * just wait for the calibration complete notification.
  371. */
  372. ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
  373. UCODE_CALIB_TIMEOUT);
  374. if (!ret)
  375. priv->init_ucode_run = true;
  376. goto out;
  377. error:
  378. iwl_remove_notification(&priv->notif_wait, &calib_wait);
  379. out:
  380. /* Whatever happened, stop the device */
  381. iwl_trans_stop_device(priv->trans);
  382. priv->ucode_loaded = false;
  383. return ret;
  384. }