blob: 539171945610cbc508f4c46d08f716e2884cd542 [file] [log] [blame]
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -08005 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07006 *
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
30#include <linux/kernel.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070031#include <linux/init.h>
32
33#include "iwl-dev.h"
34#include "iwl-core.h"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070035#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070036#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070037#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020038#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070039#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070040#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020041#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070042
Don Fryde7f5f92011-11-10 06:55:10 -080043/******************************************************************************
44 *
45 * uCode download functions
46 *
47 ******************************************************************************/
48
Johannes Berg0692fe42012-03-06 13:30:37 -080049static inline const struct fw_img *
50iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -080051{
David Spinadel6dfa8d02012-03-10 13:00:14 -080052 if (ucode_type >= IWL_UCODE_TYPE_MAX)
53 return NULL;
54
55 return &priv->fw->img[ucode_type];
Don Fry8929c242011-11-10 06:55:08 -080056}
57
Wey-Yi Guy741a6262010-03-16 12:37:24 -070058/*
59 * Calibration
60 */
Johannes Berge1991882012-03-06 13:30:36 -080061static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -070062{
63 struct iwl_calib_xtal_freq_cmd cmd;
64 __le16 *xtal_calib =
Johannes Berg11483b52012-04-09 17:46:58 -070065 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070066
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070067 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070068 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
69 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -080070 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -070071}
72
Johannes Berge1991882012-03-06 13:30:36 -080073static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -070074{
75 struct iwl_calib_temperature_offset_cmd cmd;
76 __le16 *offset_calib =
Johannes Berg11483b52012-04-09 17:46:58 -070077 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070078
79 memset(&cmd, 0, sizeof(cmd));
80 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +020081 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070082 if (!(cmd.radio_sensor_offset))
83 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070084
Johannes Berge1991882012-03-06 13:30:36 -080085 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -070086 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -080087 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070088}
89
Johannes Berge1991882012-03-06 13:30:36 -080090static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070091{
92 struct iwl_calib_temperature_offset_v2_cmd cmd;
Johannes Berg11483b52012-04-09 17:46:58 -070093 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070094 EEPROM_KELVIN_TEMPERATURE);
95 __le16 *offset_calib_low =
Johannes Berg11483b52012-04-09 17:46:58 -070096 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -070097 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070098
99 memset(&cmd, 0, sizeof(cmd));
100 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg11483b52012-04-09 17:46:58 -0700101 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700102 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700103 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700104 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700105 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700106 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700107 if (!(cmd.radio_sensor_offset_low)) {
Johannes Berge1991882012-03-06 13:30:36 -0800108 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700109 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
110 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
111 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700112 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
113 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700114
Johannes Berge1991882012-03-06 13:30:36 -0800115 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700116 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800117 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700118 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800119 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700120 le16_to_cpu(cmd.burntVoltageRef));
121
Johannes Berge1991882012-03-06 13:30:36 -0800122 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700123}
124
Johannes Berge1991882012-03-06 13:30:36 -0800125static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700126{
127 struct iwl_calib_cfg_cmd calib_cfg_cmd;
128 struct iwl_host_cmd cmd = {
129 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700130 .len = { sizeof(struct iwl_calib_cfg_cmd), },
131 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700132 };
133
134 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
135 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
136 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
137 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700138 calib_cfg_cmd.ucd_calib_cfg.flags =
139 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700140
Johannes Berge10a0532012-03-06 13:30:39 -0800141 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700142}
143
Johannes Berge1991882012-03-06 13:30:36 -0800144int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700145{
Johannes Bergca7966c2011-04-22 10:15:23 -0700146 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700147
Johannes Berge1991882012-03-06 13:30:36 -0800148 if (cfg(priv)->bt_params &&
149 cfg(priv)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700150 /*
151 * Tell uCode we are ready to perform calibration
152 * need to perform this before any calibration
153 * no need to close the envlope since we are going
154 * to load the runtime uCode later.
155 */
Johannes Berge1991882012-03-06 13:30:36 -0800156 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700157 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700158 if (ret)
159 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700160
161 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700162
Johannes Berge1991882012-03-06 13:30:36 -0800163 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700164 if (ret)
165 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700166
167 /**
168 * temperature offset calibration is only needed for runtime ucode,
169 * so prepare the value now.
170 */
Johannes Berge1991882012-03-06 13:30:36 -0800171 if (cfg(priv)->need_temp_offset_calib) {
172 if (cfg(priv)->temp_offset_v2)
173 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700174 else
Johannes Berge1991882012-03-06 13:30:36 -0800175 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700176 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700177
Johannes Bergca7966c2011-04-22 10:15:23 -0700178 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700179}
180
David Spinadel50c1e9a2012-04-16 14:43:30 -0700181int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700182{
183 struct iwl_wimax_coex_cmd coex_cmd;
184
Johannes Berg85560af2012-03-15 13:26:48 -0700185 /* coexistence is disabled */
186 memset(&coex_cmd, 0, sizeof(coex_cmd));
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700187
Johannes Berge10a0532012-03-06 13:30:39 -0800188 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700189 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700190 sizeof(coex_cmd), &coex_cmd);
191}
192
Don Fry66128b12011-11-28 14:35:14 -0800193static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700194 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
195 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
196 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
197 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
198 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
199 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
200 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
201 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
202 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
203 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
204 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
205 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
206 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
207 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
208 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
209 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
210 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
211 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
212 0, 0, 0, 0, 0, 0, 0
213};
214
Johannes Berge1991882012-03-06 13:30:36 -0800215void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700216{
217 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
218
Don Fry66128b12011-11-28 14:35:14 -0800219 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
220 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800221 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700222 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700223 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800224 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700225}
226
Johannes Berge1991882012-03-06 13:30:36 -0800227int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700228{
229 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700230 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700231
232 env_cmd.action = action;
233 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800234 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700235 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700236 sizeof(env_cmd), &env_cmd);
237 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800238 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700239 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700240}
241
242
Johannes Berge1991882012-03-06 13:30:36 -0800243static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700244{
Wey-Yi Guy74159522011-04-05 09:42:01 -0700245 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700246
Johannes Berge1991882012-03-06 13:30:36 -0800247 iwl_trans_fw_alive(trans(priv));
Johannes Berge755f882012-03-07 09:52:16 -0800248
249 priv->passive_no_rx = false;
250 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800251
Johannes Berge1991882012-03-06 13:30:36 -0800252 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700253 if (ret)
254 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700255
Don Fry38622412011-12-16 07:07:36 -0800256 if (!cfg(priv)->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800257 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800258 if (ret)
259 return ret;
260 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700261
Johannes Berge1991882012-03-06 13:30:36 -0800262 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700263}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700264
265
266/**
267 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
268 * using sample data 100 bytes apart. If these sample points are good,
269 * it's a pretty good bet that everything between them is good, too.
270 */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800271static int iwl_verify_sec_sparse(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800272 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700273{
Johannes Berg35b1d922011-04-05 09:41:56 -0700274 __le32 *image = (__le32 *)fw_desc->v_addr;
275 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700276 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700277 u32 i;
278
Johannes Berge1991882012-03-06 13:30:36 -0800279 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700280
281 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
282 /* read data comes through single port, auto-incr addr */
283 /* NOTE: Use the debugless read so we don't flood kernel log
284 * if IWL_DL_IO is set */
Johannes Berge1991882012-03-06 13:30:36 -0800285 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800286 i + fw_desc->offset);
Johannes Berge1991882012-03-06 13:30:36 -0800287 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700288 if (val != le32_to_cpu(*image))
289 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700290 }
291
Johannes Bergfb662162011-04-05 09:41:55 -0700292 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700293}
294
David Spinadel6dfa8d02012-03-10 13:00:14 -0800295static void iwl_print_mismatch_sec(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800296 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700297{
Johannes Berg35b1d922011-04-05 09:41:56 -0700298 __le32 *image = (__le32 *)fw_desc->v_addr;
299 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700300 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700301 u32 offs;
302 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700303
Johannes Berge1991882012-03-06 13:30:36 -0800304 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700305
Johannes Berge1991882012-03-06 13:30:36 -0800306 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800307 fw_desc->offset);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700308
Johannes Bergfb662162011-04-05 09:41:55 -0700309 for (offs = 0;
310 offs < len && errors < 20;
311 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700312 /* read data comes through single port, auto-incr addr */
Johannes Berge1991882012-03-06 13:30:36 -0800313 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700314 if (val != le32_to_cpu(*image)) {
Johannes Berge1991882012-03-06 13:30:36 -0800315 IWL_ERR(priv, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700316 "offset 0x%x, is 0x%x, s/b 0x%x\n",
317 offs, val, le32_to_cpu(*image));
318 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700319 }
320 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700321}
322
323/**
324 * iwl_verify_ucode - determine which instruction image is in SRAM,
325 * and verify its contents
326 */
Johannes Berge1991882012-03-06 13:30:36 -0800327static int iwl_verify_ucode(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800328 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700329{
Johannes Berg0692fe42012-03-06 13:30:37 -0800330 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800331
332 if (!img) {
Johannes Berge1991882012-03-06 13:30:36 -0800333 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800334 return -EINVAL;
335 }
336
David Spinadel6dfa8d02012-03-10 13:00:14 -0800337 if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
Johannes Berge1991882012-03-06 13:30:36 -0800338 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700339 return 0;
340 }
341
Johannes Berge1991882012-03-06 13:30:36 -0800342 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700343
David Spinadel6dfa8d02012-03-10 13:00:14 -0800344 iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
Johannes Bergfb662162011-04-05 09:41:55 -0700345 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700346}
Johannes Bergca7966c2011-04-22 10:15:23 -0700347
Don Fry69a679b2011-12-07 08:50:46 -0800348struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700349 bool valid;
350 u8 subtype;
351};
352
Johannes Bergdb662d42012-03-15 13:26:44 -0700353static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
354 struct iwl_rx_packet *pkt, void *data)
Johannes Bergca7966c2011-04-22 10:15:23 -0700355{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800356 struct iwl_priv *priv =
357 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800358 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700359 struct iwl_alive_resp *palive;
360
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800361 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700362
Johannes Berge1991882012-03-06 13:30:36 -0800363 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700364 "0x%01X 0x%01X\n",
365 palive->is_valid, palive->ver_type,
366 palive->ver_subtype);
367
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700368 priv->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700369 le32_to_cpu(palive->error_event_table_ptr);
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700370 priv->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700371 le32_to_cpu(palive->log_event_table_ptr);
372
373 alive_data->subtype = palive->ver_subtype;
374 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
Johannes Bergdb662d42012-03-15 13:26:44 -0700375
376 return true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700377}
378
379#define UCODE_ALIVE_TIMEOUT HZ
380#define UCODE_CALIB_TIMEOUT (2*HZ)
381
Johannes Berge1991882012-03-06 13:30:36 -0800382int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800383 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700384{
385 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800386 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800387 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700388 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800389 enum iwl_ucode_type old_type;
Johannes Bergdb662d42012-03-15 13:26:44 -0700390 static const u8 alive_cmd[] = { REPLY_ALIVE };
Johannes Bergca7966c2011-04-22 10:15:23 -0700391
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700392 old_type = priv->cur_ucode;
393 priv->cur_ucode = ucode_type;
Johannes Berge1991882012-03-06 13:30:36 -0800394 fw = iwl_get_ucode_image(priv, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700395
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800396 priv->ucode_loaded = false;
397
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200398 if (!fw)
399 return -EINVAL;
400
Johannes Bergdb662d42012-03-15 13:26:44 -0700401 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
402 alive_cmd, ARRAY_SIZE(alive_cmd),
403 iwl_alive_fn, &alive_data);
Johannes Bergf4720892012-03-06 13:30:57 -0800404
Johannes Berge1991882012-03-06 13:30:36 -0800405 ret = iwl_trans_start_fw(trans(priv), fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700406 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700407 priv->cur_ucode = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800408 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700409 return ret;
410 }
411
Johannes Bergca7966c2011-04-22 10:15:23 -0700412 /*
413 * Some things may run in the background now, but we
414 * just wait for the ALIVE notification here.
415 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800416 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800417 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700418 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700419 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700420 return ret;
421 }
422
423 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800424 IWL_ERR(priv, "Loaded ucode is not valid!\n");
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700425 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700426 return -EIO;
427 }
428
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700429 /*
430 * This step takes a long time (60-80ms!!) and
431 * WoWLAN image should be loaded quickly, so
432 * skip it for WoWLAN.
433 */
434 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Berge1991882012-03-06 13:30:36 -0800435 ret = iwl_verify_ucode(priv, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700436 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700437 priv->cur_ucode = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700438 return ret;
439 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700440
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700441 /* delay a bit to give rfkill time to run */
442 msleep(5);
443 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700444
Johannes Berge1991882012-03-06 13:30:36 -0800445 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700446 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800447 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700448 "Could not complete ALIVE transition: %d\n", ret);
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700449 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700450 return ret;
451 }
452
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800453 priv->ucode_loaded = true;
454
Johannes Bergca7966c2011-04-22 10:15:23 -0700455 return 0;
456}
457
Johannes Berge1f0c502012-03-15 13:26:45 -0700458static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
459 struct iwl_rx_packet *pkt, void *data)
460{
461 struct iwl_priv *priv = data;
462 struct iwl_calib_hdr *hdr;
463 int len;
464
465 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
466 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
467 return true;
468 }
469
470 hdr = (struct iwl_calib_hdr *)pkt->data;
471 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
472
473 /* reduce the size by the length field itself */
474 len -= sizeof(__le32);
475
476 if (iwl_calib_set(priv, hdr, len))
477 IWL_ERR(priv, "Failed to record calibration data %d\n",
478 hdr->op_code);
479
480 return false;
481}
482
Johannes Berge1991882012-03-06 13:30:36 -0800483int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700484{
485 struct iwl_notification_wait calib_wait;
Johannes Bergdb662d42012-03-15 13:26:44 -0700486 static const u8 calib_complete[] = {
Johannes Berge1f0c502012-03-15 13:26:45 -0700487 CALIBRATION_RES_NOTIFICATION,
Johannes Bergdb662d42012-03-15 13:26:44 -0700488 CALIBRATION_COMPLETE_NOTIFICATION
489 };
Johannes Bergca7966c2011-04-22 10:15:23 -0700490 int ret;
491
Johannes Bergb1eea292012-03-06 13:30:42 -0800492 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700493
494 /* No init ucode required? Curious, but maybe ok */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800495 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700496 return 0;
497
David Spinadelb5ea1622012-03-10 13:00:11 -0800498 if (priv->init_ucode_run)
Johannes Bergca7966c2011-04-22 10:15:23 -0700499 return 0;
500
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800501 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergdb662d42012-03-15 13:26:44 -0700502 calib_complete, ARRAY_SIZE(calib_complete),
Johannes Berge1f0c502012-03-15 13:26:45 -0700503 iwlagn_wait_calib, priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700504
505 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800506 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700507 if (ret)
508 goto error;
509
Johannes Berge1991882012-03-06 13:30:36 -0800510 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700511 if (ret)
512 goto error;
513
514 /*
515 * Some things may run in the background now, but we
516 * just wait for the calibration complete notification.
517 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800518 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800519 UCODE_CALIB_TIMEOUT);
David Spinadelb5ea1622012-03-10 13:00:11 -0800520 if (!ret)
521 priv->init_ucode_run = true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700522
523 goto out;
524
525 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800526 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700527 out:
528 /* Whatever happened, stop the device */
Johannes Berge1991882012-03-06 13:30:36 -0800529 iwl_trans_stop_device(trans(priv));
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800530 priv->ucode_loaded = false;
531
Johannes Bergca7966c2011-04-22 10:15:23 -0700532 return ret;
533}