iwl-agn-ucode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 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/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include <linux/dma-mapping.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-agn-hw.h"
  38. #include "iwl-agn.h"
  39. #include "iwl-agn-calib.h"
  40. #include "iwl-trans.h"
  41. #include "iwl-fh.h"
  42. static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
  43. {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
  44. 0, COEX_UNASSOC_IDLE_FLAGS},
  45. {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
  46. 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
  47. {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
  48. 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
  49. {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
  50. 0, COEX_CALIBRATION_FLAGS},
  51. {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
  52. 0, COEX_PERIODIC_CALIBRATION_FLAGS},
  53. {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
  54. 0, COEX_CONNECTION_ESTAB_FLAGS},
  55. {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
  56. 0, COEX_ASSOCIATED_IDLE_FLAGS},
  57. {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
  58. 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
  59. {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
  60. 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
  61. {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
  62. 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
  63. {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
  64. {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
  65. {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
  66. 0, COEX_STAND_ALONE_DEBUG_FLAGS},
  67. {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
  68. 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
  69. {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
  70. {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
  71. };
  72. /******************************************************************************
  73. *
  74. * uCode download functions
  75. *
  76. ******************************************************************************/
  77. static void iwl_free_fw_desc(struct iwl_bus *bus, struct fw_desc *desc)
  78. {
  79. if (desc->v_addr)
  80. dma_free_coherent(bus->dev, desc->len,
  81. desc->v_addr, desc->p_addr);
  82. desc->v_addr = NULL;
  83. desc->len = 0;
  84. }
  85. static void iwl_free_fw_img(struct iwl_bus *bus, struct fw_img *img)
  86. {
  87. iwl_free_fw_desc(bus, &img->code);
  88. iwl_free_fw_desc(bus, &img->data);
  89. }
  90. void iwl_dealloc_ucode(struct iwl_trans *trans)
  91. {
  92. iwl_free_fw_img(bus(trans), &trans->ucode_rt);
  93. iwl_free_fw_img(bus(trans), &trans->ucode_init);
  94. iwl_free_fw_img(bus(trans), &trans->ucode_wowlan);
  95. }
  96. int iwl_alloc_fw_desc(struct iwl_bus *bus, struct fw_desc *desc,
  97. const void *data, size_t len)
  98. {
  99. if (!len) {
  100. desc->v_addr = NULL;
  101. return -EINVAL;
  102. }
  103. desc->v_addr = dma_alloc_coherent(bus->dev, len,
  104. &desc->p_addr, GFP_KERNEL);
  105. if (!desc->v_addr)
  106. return -ENOMEM;
  107. desc->len = len;
  108. memcpy(desc->v_addr, data, len);
  109. return 0;
  110. }
  111. /*
  112. * ucode
  113. */
  114. static int iwlagn_load_section(struct iwl_trans *trans, const char *name,
  115. struct fw_desc *image, u32 dst_addr)
  116. {
  117. struct iwl_bus *bus = bus(trans);
  118. dma_addr_t phy_addr = image->p_addr;
  119. u32 byte_cnt = image->len;
  120. int ret;
  121. trans->ucode_write_complete = 0;
  122. iwl_write_direct32(bus,
  123. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  124. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  125. iwl_write_direct32(bus,
  126. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL), dst_addr);
  127. iwl_write_direct32(bus,
  128. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  129. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  130. iwl_write_direct32(bus,
  131. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  132. (iwl_get_dma_hi_addr(phy_addr)
  133. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  134. iwl_write_direct32(bus,
  135. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  136. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  137. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  138. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  139. iwl_write_direct32(bus,
  140. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  141. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  142. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  143. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  144. IWL_DEBUG_FW(bus, "%s uCode section being loaded...\n", name);
  145. ret = wait_event_timeout(trans->shrd->wait_command_queue,
  146. trans->ucode_write_complete, 5 * HZ);
  147. if (!ret) {
  148. IWL_ERR(trans, "Could not load the %s uCode section\n",
  149. name);
  150. return -ETIMEDOUT;
  151. }
  152. return 0;
  153. }
  154. static inline struct fw_img *iwl_get_ucode_image(struct iwl_trans *trans,
  155. enum iwl_ucode_type ucode_type)
  156. {
  157. switch (ucode_type) {
  158. case IWL_UCODE_INIT:
  159. return &trans->ucode_init;
  160. case IWL_UCODE_WOWLAN:
  161. return &trans->ucode_wowlan;
  162. case IWL_UCODE_REGULAR:
  163. return &trans->ucode_rt;
  164. case IWL_UCODE_NONE:
  165. break;
  166. }
  167. return NULL;
  168. }
  169. static int iwlagn_load_given_ucode(struct iwl_trans *trans,
  170. enum iwl_ucode_type ucode_type)
  171. {
  172. int ret = 0;
  173. struct fw_img *image = iwl_get_ucode_image(trans, ucode_type);
  174. if (!image) {
  175. IWL_ERR(trans, "Invalid ucode requested (%d)\n",
  176. ucode_type);
  177. return -EINVAL;
  178. }
  179. ret = iwlagn_load_section(trans, "INST", &image->code,
  180. IWLAGN_RTC_INST_LOWER_BOUND);
  181. if (ret)
  182. return ret;
  183. return iwlagn_load_section(trans, "DATA", &image->data,
  184. IWLAGN_RTC_DATA_LOWER_BOUND);
  185. }
  186. /*
  187. * Calibration
  188. */
  189. static int iwlagn_set_Xtal_calib(struct iwl_priv *priv)
  190. {
  191. struct iwl_calib_xtal_freq_cmd cmd;
  192. __le16 *xtal_calib =
  193. (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_XTAL);
  194. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
  195. cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
  196. cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
  197. return iwl_calib_set(&priv->calib_results[IWL_CALIB_XTAL],
  198. (u8 *)&cmd, sizeof(cmd));
  199. }
  200. static int iwlagn_set_temperature_offset_calib(struct iwl_priv *priv)
  201. {
  202. struct iwl_calib_temperature_offset_cmd cmd;
  203. __le16 *offset_calib =
  204. (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
  205. memset(&cmd, 0, sizeof(cmd));
  206. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  207. memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
  208. if (!(cmd.radio_sensor_offset))
  209. cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
  210. IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
  211. le16_to_cpu(cmd.radio_sensor_offset));
  212. return iwl_calib_set(&priv->calib_results[IWL_CALIB_TEMP_OFFSET],
  213. (u8 *)&cmd, sizeof(cmd));
  214. }
  215. static int iwlagn_set_temperature_offset_calib_v2(struct iwl_priv *priv)
  216. {
  217. struct iwl_calib_temperature_offset_v2_cmd cmd;
  218. __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv,
  219. EEPROM_KELVIN_TEMPERATURE);
  220. __le16 *offset_calib_low =
  221. (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
  222. struct iwl_eeprom_calib_hdr *hdr;
  223. memset(&cmd, 0, sizeof(cmd));
  224. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  225. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  226. EEPROM_CALIB_ALL);
  227. memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
  228. sizeof(*offset_calib_high));
  229. memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
  230. sizeof(*offset_calib_low));
  231. if (!(cmd.radio_sensor_offset_low)) {
  232. IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
  233. cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
  234. cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
  235. }
  236. memcpy(&cmd.burntVoltageRef, &hdr->voltage,
  237. sizeof(hdr->voltage));
  238. IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
  239. le16_to_cpu(cmd.radio_sensor_offset_high));
  240. IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
  241. le16_to_cpu(cmd.radio_sensor_offset_low));
  242. IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
  243. le16_to_cpu(cmd.burntVoltageRef));
  244. return iwl_calib_set(&priv->calib_results[IWL_CALIB_TEMP_OFFSET],
  245. (u8 *)&cmd, sizeof(cmd));
  246. }
  247. static int iwlagn_send_calib_cfg(struct iwl_priv *priv)
  248. {
  249. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  250. struct iwl_host_cmd cmd = {
  251. .id = CALIBRATION_CFG_CMD,
  252. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  253. .data = { &calib_cfg_cmd, },
  254. };
  255. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  256. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  257. calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
  258. calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
  259. calib_cfg_cmd.ucd_calib_cfg.flags =
  260. IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
  261. return iwl_trans_send_cmd(trans(priv), &cmd);
  262. }
  263. int iwlagn_rx_calib_result(struct iwl_priv *priv,
  264. struct iwl_rx_mem_buffer *rxb,
  265. struct iwl_device_cmd *cmd)
  266. {
  267. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  268. struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
  269. int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  270. int index;
  271. /* reduce the size of the length field itself */
  272. len -= 4;
  273. /* Define the order in which the results will be sent to the runtime
  274. * uCode. iwl_send_calib_results sends them in a row according to
  275. * their index. We sort them here
  276. */
  277. switch (hdr->op_code) {
  278. case IWL_PHY_CALIBRATE_DC_CMD:
  279. index = IWL_CALIB_DC;
  280. break;
  281. case IWL_PHY_CALIBRATE_LO_CMD:
  282. index = IWL_CALIB_LO;
  283. break;
  284. case IWL_PHY_CALIBRATE_TX_IQ_CMD:
  285. index = IWL_CALIB_TX_IQ;
  286. break;
  287. case IWL_PHY_CALIBRATE_TX_IQ_PERD_CMD:
  288. index = IWL_CALIB_TX_IQ_PERD;
  289. break;
  290. case IWL_PHY_CALIBRATE_BASE_BAND_CMD:
  291. index = IWL_CALIB_BASE_BAND;
  292. break;
  293. default:
  294. IWL_ERR(priv, "Unknown calibration notification %d\n",
  295. hdr->op_code);
  296. return -1;
  297. }
  298. iwl_calib_set(&priv->calib_results[index], pkt->u.raw, len);
  299. return 0;
  300. }
  301. int iwlagn_init_alive_start(struct iwl_priv *priv)
  302. {
  303. int ret;
  304. if (priv->cfg->bt_params &&
  305. priv->cfg->bt_params->advanced_bt_coexist) {
  306. /*
  307. * Tell uCode we are ready to perform calibration
  308. * need to perform this before any calibration
  309. * no need to close the envlope since we are going
  310. * to load the runtime uCode later.
  311. */
  312. ret = iwlagn_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
  313. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  314. if (ret)
  315. return ret;
  316. }
  317. ret = iwlagn_send_calib_cfg(priv);
  318. if (ret)
  319. return ret;
  320. /**
  321. * temperature offset calibration is only needed for runtime ucode,
  322. * so prepare the value now.
  323. */
  324. if (priv->cfg->need_temp_offset_calib) {
  325. if (priv->cfg->temp_offset_v2)
  326. return iwlagn_set_temperature_offset_calib_v2(priv);
  327. else
  328. return iwlagn_set_temperature_offset_calib(priv);
  329. }
  330. return 0;
  331. }
  332. static int iwlagn_send_wimax_coex(struct iwl_priv *priv)
  333. {
  334. struct iwl_wimax_coex_cmd coex_cmd;
  335. if (priv->cfg->base_params->support_wimax_coexist) {
  336. /* UnMask wake up src at associated sleep */
  337. coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
  338. /* UnMask wake up src at unassociated sleep */
  339. coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
  340. memcpy(coex_cmd.sta_prio, cu_priorities,
  341. sizeof(struct iwl_wimax_coex_event_entry) *
  342. COEX_NUM_OF_EVENTS);
  343. /* enabling the coexistence feature */
  344. coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
  345. /* enabling the priorities tables */
  346. coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
  347. } else {
  348. /* coexistence is disabled */
  349. memset(&coex_cmd, 0, sizeof(coex_cmd));
  350. }
  351. return iwl_trans_send_cmd_pdu(trans(priv),
  352. COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
  353. sizeof(coex_cmd), &coex_cmd);
  354. }
  355. static const u8 iwlagn_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  356. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  357. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  358. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  359. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  360. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  361. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  362. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  363. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  364. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  365. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  366. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  367. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  368. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  369. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  370. ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  371. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  372. ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  373. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  374. 0, 0, 0, 0, 0, 0, 0
  375. };
  376. void iwlagn_send_prio_tbl(struct iwl_priv *priv)
  377. {
  378. struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
  379. memcpy(prio_tbl_cmd.prio_tbl, iwlagn_bt_prio_tbl,
  380. sizeof(iwlagn_bt_prio_tbl));
  381. if (iwl_trans_send_cmd_pdu(trans(priv),
  382. REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
  383. sizeof(prio_tbl_cmd), &prio_tbl_cmd))
  384. IWL_ERR(priv, "failed to send BT prio tbl command\n");
  385. }
  386. int iwlagn_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
  387. {
  388. struct iwl_bt_coex_prot_env_cmd env_cmd;
  389. int ret;
  390. env_cmd.action = action;
  391. env_cmd.type = type;
  392. ret = iwl_trans_send_cmd_pdu(trans(priv),
  393. REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
  394. sizeof(env_cmd), &env_cmd);
  395. if (ret)
  396. IWL_ERR(priv, "failed to send BT env command\n");
  397. return ret;
  398. }
  399. static int iwlagn_alive_notify(struct iwl_priv *priv)
  400. {
  401. struct iwl_rxon_context *ctx;
  402. int ret;
  403. if (!priv->tx_cmd_pool)
  404. priv->tx_cmd_pool =
  405. kmem_cache_create("iwlagn_dev_cmd",
  406. sizeof(struct iwl_device_cmd),
  407. sizeof(void *), 0, NULL);
  408. if (!priv->tx_cmd_pool)
  409. return -ENOMEM;
  410. iwl_trans_tx_start(trans(priv));
  411. for_each_context(priv, ctx)
  412. ctx->last_tx_rejected = false;
  413. ret = iwlagn_send_wimax_coex(priv);
  414. if (ret)
  415. return ret;
  416. ret = iwlagn_set_Xtal_calib(priv);
  417. if (ret)
  418. return ret;
  419. return iwl_send_calib_results(priv);
  420. }
  421. /**
  422. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  423. * using sample data 100 bytes apart. If these sample points are good,
  424. * it's a pretty good bet that everything between them is good, too.
  425. */
  426. static int iwl_verify_inst_sparse(struct iwl_bus *bus,
  427. struct fw_desc *fw_desc)
  428. {
  429. __le32 *image = (__le32 *)fw_desc->v_addr;
  430. u32 len = fw_desc->len;
  431. u32 val;
  432. u32 i;
  433. IWL_DEBUG_FW(bus, "ucode inst image size is %u\n", len);
  434. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  435. /* read data comes through single port, auto-incr addr */
  436. /* NOTE: Use the debugless read so we don't flood kernel log
  437. * if IWL_DL_IO is set */
  438. iwl_write_direct32(bus, HBUS_TARG_MEM_RADDR,
  439. i + IWLAGN_RTC_INST_LOWER_BOUND);
  440. val = iwl_read32(bus, HBUS_TARG_MEM_RDAT);
  441. if (val != le32_to_cpu(*image))
  442. return -EIO;
  443. }
  444. return 0;
  445. }
  446. static void iwl_print_mismatch_inst(struct iwl_bus *bus,
  447. struct fw_desc *fw_desc)
  448. {
  449. __le32 *image = (__le32 *)fw_desc->v_addr;
  450. u32 len = fw_desc->len;
  451. u32 val;
  452. u32 offs;
  453. int errors = 0;
  454. IWL_DEBUG_FW(bus, "ucode inst image size is %u\n", len);
  455. iwl_write_direct32(bus, HBUS_TARG_MEM_RADDR,
  456. IWLAGN_RTC_INST_LOWER_BOUND);
  457. for (offs = 0;
  458. offs < len && errors < 20;
  459. offs += sizeof(u32), image++) {
  460. /* read data comes through single port, auto-incr addr */
  461. val = iwl_read32(bus, HBUS_TARG_MEM_RDAT);
  462. if (val != le32_to_cpu(*image)) {
  463. IWL_ERR(bus, "uCode INST section at "
  464. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  465. offs, val, le32_to_cpu(*image));
  466. errors++;
  467. }
  468. }
  469. }
  470. /**
  471. * iwl_verify_ucode - determine which instruction image is in SRAM,
  472. * and verify its contents
  473. */
  474. static int iwl_verify_ucode(struct iwl_trans *trans,
  475. enum iwl_ucode_type ucode_type)
  476. {
  477. struct fw_img *img = iwl_get_ucode_image(trans, ucode_type);
  478. if (!img) {
  479. IWL_ERR(trans, "Invalid ucode requested (%d)\n", ucode_type);
  480. return -EINVAL;
  481. }
  482. if (!iwl_verify_inst_sparse(bus(trans), &img->code)) {
  483. IWL_DEBUG_FW(trans, "uCode is good in inst SRAM\n");
  484. return 0;
  485. }
  486. IWL_ERR(trans, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
  487. iwl_print_mismatch_inst(bus(trans), &img->code);
  488. return -EIO;
  489. }
  490. struct iwlagn_alive_data {
  491. bool valid;
  492. u8 subtype;
  493. };
  494. static void iwlagn_alive_fn(struct iwl_priv *priv,
  495. struct iwl_rx_packet *pkt,
  496. void *data)
  497. {
  498. struct iwlagn_alive_data *alive_data = data;
  499. struct iwl_alive_resp *palive;
  500. palive = &pkt->u.alive_frame;
  501. IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
  502. "0x%01X 0x%01X\n",
  503. palive->is_valid, palive->ver_type,
  504. palive->ver_subtype);
  505. priv->device_pointers.error_event_table =
  506. le32_to_cpu(palive->error_event_table_ptr);
  507. priv->device_pointers.log_event_table =
  508. le32_to_cpu(palive->log_event_table_ptr);
  509. alive_data->subtype = palive->ver_subtype;
  510. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  511. }
  512. #define UCODE_ALIVE_TIMEOUT HZ
  513. #define UCODE_CALIB_TIMEOUT (2*HZ)
  514. int iwlagn_load_ucode_wait_alive(struct iwl_priv *priv,
  515. enum iwl_ucode_type ucode_type)
  516. {
  517. struct iwl_notification_wait alive_wait;
  518. struct iwlagn_alive_data alive_data;
  519. int ret;
  520. enum iwl_ucode_type old_type;
  521. ret = iwl_trans_start_device(trans(priv));
  522. if (ret)
  523. return ret;
  524. iwlagn_init_notification_wait(priv, &alive_wait, REPLY_ALIVE,
  525. iwlagn_alive_fn, &alive_data);
  526. old_type = priv->ucode_type;
  527. priv->ucode_type = ucode_type;
  528. ret = iwlagn_load_given_ucode(trans(priv), ucode_type);
  529. if (ret) {
  530. priv->ucode_type = old_type;
  531. iwlagn_remove_notification(priv, &alive_wait);
  532. return ret;
  533. }
  534. iwl_trans_kick_nic(trans(priv));
  535. /*
  536. * Some things may run in the background now, but we
  537. * just wait for the ALIVE notification here.
  538. */
  539. ret = iwlagn_wait_notification(priv, &alive_wait, UCODE_ALIVE_TIMEOUT);
  540. if (ret) {
  541. priv->ucode_type = old_type;
  542. return ret;
  543. }
  544. if (!alive_data.valid) {
  545. IWL_ERR(priv, "Loaded ucode is not valid!\n");
  546. priv->ucode_type = old_type;
  547. return -EIO;
  548. }
  549. /*
  550. * This step takes a long time (60-80ms!!) and
  551. * WoWLAN image should be loaded quickly, so
  552. * skip it for WoWLAN.
  553. */
  554. if (ucode_type != IWL_UCODE_WOWLAN) {
  555. ret = iwl_verify_ucode(trans(priv), ucode_type);
  556. if (ret) {
  557. priv->ucode_type = old_type;
  558. return ret;
  559. }
  560. /* delay a bit to give rfkill time to run */
  561. msleep(5);
  562. }
  563. ret = iwlagn_alive_notify(priv);
  564. if (ret) {
  565. IWL_WARN(priv,
  566. "Could not complete ALIVE transition: %d\n", ret);
  567. priv->ucode_type = old_type;
  568. return ret;
  569. }
  570. return 0;
  571. }
  572. int iwlagn_run_init_ucode(struct iwl_priv *priv)
  573. {
  574. struct iwl_notification_wait calib_wait;
  575. int ret;
  576. lockdep_assert_held(&priv->shrd->mutex);
  577. /* No init ucode required? Curious, but maybe ok */
  578. if (!trans(priv)->ucode_init.code.len)
  579. return 0;
  580. if (priv->ucode_type != IWL_UCODE_NONE)
  581. return 0;
  582. iwlagn_init_notification_wait(priv, &calib_wait,
  583. CALIBRATION_COMPLETE_NOTIFICATION,
  584. NULL, NULL);
  585. /* Will also start the device */
  586. ret = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  587. if (ret)
  588. goto error;
  589. ret = iwlagn_init_alive_start(priv);
  590. if (ret)
  591. goto error;
  592. /*
  593. * Some things may run in the background now, but we
  594. * just wait for the calibration complete notification.
  595. */
  596. ret = iwlagn_wait_notification(priv, &calib_wait, UCODE_CALIB_TIMEOUT);
  597. goto out;
  598. error:
  599. iwlagn_remove_notification(priv, &calib_wait);
  600. out:
  601. /* Whatever happened, stop the device */
  602. iwl_trans_stop_device(trans(priv));
  603. return ret;
  604. }