blob: ba7c9f883cb6804f68bdc032a546f85ded1e40d6 [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 Berge1991882012-03-06 13:30:36 -080065 (__le16 *)iwl_eeprom_query_addr(priv->shrd, 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 Berge1991882012-03-06 13:30:36 -080077 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -080078 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070079
80 memset(&cmd, 0, sizeof(cmd));
81 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +020082 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070083 if (!(cmd.radio_sensor_offset))
84 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070085
Johannes Berge1991882012-03-06 13:30:36 -080086 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -070087 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -080088 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070089}
90
Johannes Berge1991882012-03-06 13:30:36 -080091static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070092{
93 struct iwl_calib_temperature_offset_v2_cmd cmd;
Johannes Berge1991882012-03-06 13:30:36 -080094 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070095 EEPROM_KELVIN_TEMPERATURE);
96 __le16 *offset_calib_low =
Johannes Berge1991882012-03-06 13:30:36 -080097 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -080098 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -070099 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700100
101 memset(&cmd, 0, sizeof(cmd));
102 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berge1991882012-03-06 13:30:36 -0800103 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700104 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700105 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700106 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700107 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700108 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700109 if (!(cmd.radio_sensor_offset_low)) {
Johannes Berge1991882012-03-06 13:30:36 -0800110 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700111 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
112 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
113 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700114 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
115 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700116
Johannes Berge1991882012-03-06 13:30:36 -0800117 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700118 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800119 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700120 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800121 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700122 le16_to_cpu(cmd.burntVoltageRef));
123
Johannes Berge1991882012-03-06 13:30:36 -0800124 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700125}
126
Johannes Berge1991882012-03-06 13:30:36 -0800127static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700128{
129 struct iwl_calib_cfg_cmd calib_cfg_cmd;
130 struct iwl_host_cmd cmd = {
131 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700132 .len = { sizeof(struct iwl_calib_cfg_cmd), },
133 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700134 };
135
136 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
137 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
138 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
139 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700140 calib_cfg_cmd.ucd_calib_cfg.flags =
141 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700142
Johannes Berge10a0532012-03-06 13:30:39 -0800143 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700144}
145
Johannes Berge1991882012-03-06 13:30:36 -0800146int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700147{
Johannes Bergca7966c2011-04-22 10:15:23 -0700148 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700149
Johannes Berge1991882012-03-06 13:30:36 -0800150 if (cfg(priv)->bt_params &&
151 cfg(priv)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700152 /*
153 * Tell uCode we are ready to perform calibration
154 * need to perform this before any calibration
155 * no need to close the envlope since we are going
156 * to load the runtime uCode later.
157 */
Johannes Berge1991882012-03-06 13:30:36 -0800158 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700159 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700160 if (ret)
161 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700162
163 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700164
Johannes Berge1991882012-03-06 13:30:36 -0800165 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700166 if (ret)
167 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700168
169 /**
170 * temperature offset calibration is only needed for runtime ucode,
171 * so prepare the value now.
172 */
Johannes Berge1991882012-03-06 13:30:36 -0800173 if (cfg(priv)->need_temp_offset_calib) {
174 if (cfg(priv)->temp_offset_v2)
175 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700176 else
Johannes Berge1991882012-03-06 13:30:36 -0800177 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700178 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700179
Johannes Bergca7966c2011-04-22 10:15:23 -0700180 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700181}
182
Johannes Berge1991882012-03-06 13:30:36 -0800183static int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700184{
185 struct iwl_wimax_coex_cmd coex_cmd;
186
Johannes Berg85560af2012-03-15 13:26:48 -0700187 /* coexistence is disabled */
188 memset(&coex_cmd, 0, sizeof(coex_cmd));
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700189
Johannes Berge10a0532012-03-06 13:30:39 -0800190 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700191 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700192 sizeof(coex_cmd), &coex_cmd);
193}
194
Don Fry66128b12011-11-28 14:35:14 -0800195static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700196 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
197 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
198 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
199 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
200 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
201 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
202 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
203 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
204 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
205 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
206 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
207 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
208 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
209 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
210 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
211 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
212 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
213 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
214 0, 0, 0, 0, 0, 0, 0
215};
216
Johannes Berge1991882012-03-06 13:30:36 -0800217void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700218{
219 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
220
Don Fry66128b12011-11-28 14:35:14 -0800221 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
222 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800223 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700224 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700225 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800226 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700227}
228
Johannes Berge1991882012-03-06 13:30:36 -0800229int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700230{
231 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700232 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700233
234 env_cmd.action = action;
235 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800236 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700237 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700238 sizeof(env_cmd), &env_cmd);
239 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800240 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700241 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700242}
243
244
Johannes Berge1991882012-03-06 13:30:36 -0800245static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700246{
Wey-Yi Guy74159522011-04-05 09:42:01 -0700247 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700248
Johannes Berge1991882012-03-06 13:30:36 -0800249 iwl_trans_fw_alive(trans(priv));
Johannes Berge755f882012-03-07 09:52:16 -0800250
251 priv->passive_no_rx = false;
252 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800253
Johannes Berge1991882012-03-06 13:30:36 -0800254 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700255 if (ret)
256 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700257
Don Fry38622412011-12-16 07:07:36 -0800258 if (!cfg(priv)->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800259 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800260 if (ret)
261 return ret;
262 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700263
Johannes Berge1991882012-03-06 13:30:36 -0800264 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700265}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700266
267
268/**
269 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
270 * using sample data 100 bytes apart. If these sample points are good,
271 * it's a pretty good bet that everything between them is good, too.
272 */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800273static int iwl_verify_sec_sparse(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800274 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700275{
Johannes Berg35b1d922011-04-05 09:41:56 -0700276 __le32 *image = (__le32 *)fw_desc->v_addr;
277 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700278 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700279 u32 i;
280
Johannes Berge1991882012-03-06 13:30:36 -0800281 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700282
283 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
284 /* read data comes through single port, auto-incr addr */
285 /* NOTE: Use the debugless read so we don't flood kernel log
286 * if IWL_DL_IO is set */
Johannes Berge1991882012-03-06 13:30:36 -0800287 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800288 i + fw_desc->offset);
Johannes Berge1991882012-03-06 13:30:36 -0800289 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700290 if (val != le32_to_cpu(*image))
291 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700292 }
293
Johannes Bergfb662162011-04-05 09:41:55 -0700294 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700295}
296
David Spinadel6dfa8d02012-03-10 13:00:14 -0800297static void iwl_print_mismatch_sec(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800298 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700299{
Johannes Berg35b1d922011-04-05 09:41:56 -0700300 __le32 *image = (__le32 *)fw_desc->v_addr;
301 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700302 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700303 u32 offs;
304 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700305
Johannes Berge1991882012-03-06 13:30:36 -0800306 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700307
Johannes Berge1991882012-03-06 13:30:36 -0800308 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800309 fw_desc->offset);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700310
Johannes Bergfb662162011-04-05 09:41:55 -0700311 for (offs = 0;
312 offs < len && errors < 20;
313 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700314 /* read data comes through single port, auto-incr addr */
Johannes Berge1991882012-03-06 13:30:36 -0800315 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700316 if (val != le32_to_cpu(*image)) {
Johannes Berge1991882012-03-06 13:30:36 -0800317 IWL_ERR(priv, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700318 "offset 0x%x, is 0x%x, s/b 0x%x\n",
319 offs, val, le32_to_cpu(*image));
320 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700321 }
322 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700323}
324
325/**
326 * iwl_verify_ucode - determine which instruction image is in SRAM,
327 * and verify its contents
328 */
Johannes Berge1991882012-03-06 13:30:36 -0800329static int iwl_verify_ucode(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800330 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700331{
Johannes Berg0692fe42012-03-06 13:30:37 -0800332 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800333
334 if (!img) {
Johannes Berge1991882012-03-06 13:30:36 -0800335 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800336 return -EINVAL;
337 }
338
David Spinadel6dfa8d02012-03-10 13:00:14 -0800339 if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
Johannes Berge1991882012-03-06 13:30:36 -0800340 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700341 return 0;
342 }
343
Johannes Berge1991882012-03-06 13:30:36 -0800344 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700345
David Spinadel6dfa8d02012-03-10 13:00:14 -0800346 iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
Johannes Bergfb662162011-04-05 09:41:55 -0700347 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700348}
Johannes Bergca7966c2011-04-22 10:15:23 -0700349
Don Fry69a679b2011-12-07 08:50:46 -0800350struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700351 bool valid;
352 u8 subtype;
353};
354
Johannes Bergdb662d42012-03-15 13:26:44 -0700355static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
356 struct iwl_rx_packet *pkt, void *data)
Johannes Bergca7966c2011-04-22 10:15:23 -0700357{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800358 struct iwl_priv *priv =
359 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800360 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700361 struct iwl_alive_resp *palive;
362
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800363 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700364
Johannes Berge1991882012-03-06 13:30:36 -0800365 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700366 "0x%01X 0x%01X\n",
367 palive->is_valid, palive->ver_type,
368 palive->ver_subtype);
369
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700370 priv->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700371 le32_to_cpu(palive->error_event_table_ptr);
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700372 priv->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700373 le32_to_cpu(palive->log_event_table_ptr);
374
375 alive_data->subtype = palive->ver_subtype;
376 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
Johannes Bergdb662d42012-03-15 13:26:44 -0700377
378 return true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700379}
380
381#define UCODE_ALIVE_TIMEOUT HZ
382#define UCODE_CALIB_TIMEOUT (2*HZ)
383
Johannes Berge1991882012-03-06 13:30:36 -0800384int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800385 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700386{
387 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800388 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800389 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700390 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800391 enum iwl_ucode_type old_type;
Johannes Bergdb662d42012-03-15 13:26:44 -0700392 static const u8 alive_cmd[] = { REPLY_ALIVE };
Johannes Bergca7966c2011-04-22 10:15:23 -0700393
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700394 old_type = priv->cur_ucode;
395 priv->cur_ucode = ucode_type;
Johannes Berge1991882012-03-06 13:30:36 -0800396 fw = iwl_get_ucode_image(priv, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700397
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800398 priv->ucode_loaded = false;
399
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200400 if (!fw)
401 return -EINVAL;
402
Johannes Bergdb662d42012-03-15 13:26:44 -0700403 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
404 alive_cmd, ARRAY_SIZE(alive_cmd),
405 iwl_alive_fn, &alive_data);
Johannes Bergf4720892012-03-06 13:30:57 -0800406
Johannes Berge1991882012-03-06 13:30:36 -0800407 ret = iwl_trans_start_fw(trans(priv), fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700408 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700409 priv->cur_ucode = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800410 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700411 return ret;
412 }
413
Johannes Bergca7966c2011-04-22 10:15:23 -0700414 /*
415 * Some things may run in the background now, but we
416 * just wait for the ALIVE notification here.
417 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800418 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800419 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700420 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700421 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700422 return ret;
423 }
424
425 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800426 IWL_ERR(priv, "Loaded ucode is not valid!\n");
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700427 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700428 return -EIO;
429 }
430
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700431 /*
432 * This step takes a long time (60-80ms!!) and
433 * WoWLAN image should be loaded quickly, so
434 * skip it for WoWLAN.
435 */
436 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Berge1991882012-03-06 13:30:36 -0800437 ret = iwl_verify_ucode(priv, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700438 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700439 priv->cur_ucode = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700440 return ret;
441 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700442
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700443 /* delay a bit to give rfkill time to run */
444 msleep(5);
445 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700446
Johannes Berge1991882012-03-06 13:30:36 -0800447 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700448 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800449 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700450 "Could not complete ALIVE transition: %d\n", ret);
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700451 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700452 return ret;
453 }
454
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800455 priv->ucode_loaded = true;
456
Johannes Bergca7966c2011-04-22 10:15:23 -0700457 return 0;
458}
459
Johannes Berge1f0c502012-03-15 13:26:45 -0700460static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
461 struct iwl_rx_packet *pkt, void *data)
462{
463 struct iwl_priv *priv = data;
464 struct iwl_calib_hdr *hdr;
465 int len;
466
467 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
468 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
469 return true;
470 }
471
472 hdr = (struct iwl_calib_hdr *)pkt->data;
473 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
474
475 /* reduce the size by the length field itself */
476 len -= sizeof(__le32);
477
478 if (iwl_calib_set(priv, hdr, len))
479 IWL_ERR(priv, "Failed to record calibration data %d\n",
480 hdr->op_code);
481
482 return false;
483}
484
Johannes Berge1991882012-03-06 13:30:36 -0800485int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700486{
487 struct iwl_notification_wait calib_wait;
Johannes Bergdb662d42012-03-15 13:26:44 -0700488 static const u8 calib_complete[] = {
Johannes Berge1f0c502012-03-15 13:26:45 -0700489 CALIBRATION_RES_NOTIFICATION,
Johannes Bergdb662d42012-03-15 13:26:44 -0700490 CALIBRATION_COMPLETE_NOTIFICATION
491 };
Johannes Bergca7966c2011-04-22 10:15:23 -0700492 int ret;
493
Johannes Bergb1eea292012-03-06 13:30:42 -0800494 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700495
496 /* No init ucode required? Curious, but maybe ok */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800497 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700498 return 0;
499
David Spinadelb5ea1622012-03-10 13:00:11 -0800500 if (priv->init_ucode_run)
Johannes Bergca7966c2011-04-22 10:15:23 -0700501 return 0;
502
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800503 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergdb662d42012-03-15 13:26:44 -0700504 calib_complete, ARRAY_SIZE(calib_complete),
Johannes Berge1f0c502012-03-15 13:26:45 -0700505 iwlagn_wait_calib, priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700506
507 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800508 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700509 if (ret)
510 goto error;
511
Johannes Berge1991882012-03-06 13:30:36 -0800512 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700513 if (ret)
514 goto error;
515
516 /*
517 * Some things may run in the background now, but we
518 * just wait for the calibration complete notification.
519 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800520 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800521 UCODE_CALIB_TIMEOUT);
David Spinadelb5ea1622012-03-10 13:00:11 -0800522 if (!ret)
523 priv->init_ucode_run = true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700524
525 goto out;
526
527 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800528 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700529 out:
530 /* Whatever happened, stop the device */
Johannes Berge1991882012-03-06 13:30:36 -0800531 iwl_trans_stop_device(trans(priv));
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800532 priv->ucode_loaded = false;
533
Johannes Bergca7966c2011-04-22 10:15:23 -0700534 return ret;
535}