blob: bc40dc68b0f4f686198b302b6cdc3477986a3950 [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"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070034#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070035#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070036#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020037#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070038#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070039#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020040#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070041
Don Fryde7f5f92011-11-10 06:55:10 -080042/******************************************************************************
43 *
44 * uCode download functions
45 *
46 ******************************************************************************/
47
Johannes Berg0692fe42012-03-06 13:30:37 -080048static inline const struct fw_img *
49iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -080050{
David Spinadel6dfa8d02012-03-10 13:00:14 -080051 if (ucode_type >= IWL_UCODE_TYPE_MAX)
52 return NULL;
53
54 return &priv->fw->img[ucode_type];
Don Fry8929c242011-11-10 06:55:08 -080055}
56
Wey-Yi Guy741a6262010-03-16 12:37:24 -070057/*
58 * Calibration
59 */
Johannes Berge1991882012-03-06 13:30:36 -080060static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -070061{
62 struct iwl_calib_xtal_freq_cmd cmd;
63 __le16 *xtal_calib =
Johannes Berg11483b52012-04-09 17:46:58 -070064 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070065
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070066 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070067 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
68 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -080069 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -070070}
71
Johannes Berge1991882012-03-06 13:30:36 -080072static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -070073{
74 struct iwl_calib_temperature_offset_cmd cmd;
75 __le16 *offset_calib =
Johannes Berg11483b52012-04-09 17:46:58 -070076 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070077
78 memset(&cmd, 0, sizeof(cmd));
79 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +020080 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070081 if (!(cmd.radio_sensor_offset))
82 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070083
Johannes Berge1991882012-03-06 13:30:36 -080084 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -070085 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -080086 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070087}
88
Johannes Berge1991882012-03-06 13:30:36 -080089static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070090{
91 struct iwl_calib_temperature_offset_v2_cmd cmd;
Johannes Berg11483b52012-04-09 17:46:58 -070092 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070093 EEPROM_KELVIN_TEMPERATURE);
94 __le16 *offset_calib_low =
Johannes Berg11483b52012-04-09 17:46:58 -070095 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -070096 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070097
98 memset(&cmd, 0, sizeof(cmd));
99 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg11483b52012-04-09 17:46:58 -0700100 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700101 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700102 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700103 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700104 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700105 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700106 if (!(cmd.radio_sensor_offset_low)) {
Johannes Berge1991882012-03-06 13:30:36 -0800107 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700108 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
109 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
110 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700111 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
112 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700113
Johannes Berge1991882012-03-06 13:30:36 -0800114 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700115 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800116 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700117 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800118 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700119 le16_to_cpu(cmd.burntVoltageRef));
120
Johannes Berge1991882012-03-06 13:30:36 -0800121 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700122}
123
Johannes Berge1991882012-03-06 13:30:36 -0800124static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700125{
126 struct iwl_calib_cfg_cmd calib_cfg_cmd;
127 struct iwl_host_cmd cmd = {
128 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700129 .len = { sizeof(struct iwl_calib_cfg_cmd), },
130 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700131 };
132
133 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
134 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
135 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
136 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700137 calib_cfg_cmd.ucd_calib_cfg.flags =
138 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700139
Johannes Berge10a0532012-03-06 13:30:39 -0800140 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700141}
142
Johannes Berge1991882012-03-06 13:30:36 -0800143int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700144{
Johannes Bergca7966c2011-04-22 10:15:23 -0700145 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700146
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200147 if (priv->cfg->bt_params &&
148 priv->cfg->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700149 /*
150 * Tell uCode we are ready to perform calibration
151 * need to perform this before any calibration
152 * no need to close the envlope since we are going
153 * to load the runtime uCode later.
154 */
Johannes Berge1991882012-03-06 13:30:36 -0800155 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700156 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700157 if (ret)
158 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700159
160 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700161
Johannes Berge1991882012-03-06 13:30:36 -0800162 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700163 if (ret)
164 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700165
166 /**
167 * temperature offset calibration is only needed for runtime ucode,
168 * so prepare the value now.
169 */
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200170 if (priv->cfg->need_temp_offset_calib) {
171 if (priv->cfg->temp_offset_v2)
Johannes Berge1991882012-03-06 13:30:36 -0800172 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700173 else
Johannes Berge1991882012-03-06 13:30:36 -0800174 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700175 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700176
Johannes Bergca7966c2011-04-22 10:15:23 -0700177 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700178}
179
David Spinadel50c1e9a2012-04-16 14:43:30 -0700180int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700181{
182 struct iwl_wimax_coex_cmd coex_cmd;
183
Johannes Berg85560af2012-03-15 13:26:48 -0700184 /* coexistence is disabled */
185 memset(&coex_cmd, 0, sizeof(coex_cmd));
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700186
Johannes Berge10a0532012-03-06 13:30:39 -0800187 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700188 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700189 sizeof(coex_cmd), &coex_cmd);
190}
191
Don Fry66128b12011-11-28 14:35:14 -0800192static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700193 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
194 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
195 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
196 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
197 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
198 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
199 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
200 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
201 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
202 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
203 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
204 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
205 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
206 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
207 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
208 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
209 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
210 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
211 0, 0, 0, 0, 0, 0, 0
212};
213
Johannes Berge1991882012-03-06 13:30:36 -0800214void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700215{
216 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
217
Don Fry66128b12011-11-28 14:35:14 -0800218 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
219 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800220 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700221 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700222 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800223 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700224}
225
Johannes Berge1991882012-03-06 13:30:36 -0800226int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700227{
228 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700229 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700230
231 env_cmd.action = action;
232 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800233 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700234 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700235 sizeof(env_cmd), &env_cmd);
236 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800237 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700238 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700239}
240
241
Johannes Berge1991882012-03-06 13:30:36 -0800242static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700243{
Wey-Yi Guy74159522011-04-05 09:42:01 -0700244 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700245
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700246 iwl_trans_fw_alive(priv->trans);
Johannes Berge755f882012-03-07 09:52:16 -0800247
248 priv->passive_no_rx = false;
249 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800250
Johannes Berge1991882012-03-06 13:30:36 -0800251 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700252 if (ret)
253 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700254
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200255 if (!priv->cfg->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800256 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800257 if (ret)
258 return ret;
259 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700260
Johannes Berge1991882012-03-06 13:30:36 -0800261 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700262}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700263
264
265/**
266 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
267 * using sample data 100 bytes apart. If these sample points are good,
268 * it's a pretty good bet that everything between them is good, too.
269 */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800270static int iwl_verify_sec_sparse(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800271 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700272{
Johannes Berg35b1d922011-04-05 09:41:56 -0700273 __le32 *image = (__le32 *)fw_desc->v_addr;
274 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700275 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700276 u32 i;
277
Johannes Berge1991882012-03-06 13:30:36 -0800278 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700279
280 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
281 /* read data comes through single port, auto-incr addr */
282 /* NOTE: Use the debugless read so we don't flood kernel log
283 * if IWL_DL_IO is set */
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700284 iwl_write_direct32(priv->trans, HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800285 i + fw_desc->offset);
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700286 val = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700287 if (val != le32_to_cpu(*image))
288 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700289 }
290
Johannes Bergfb662162011-04-05 09:41:55 -0700291 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700292}
293
David Spinadel6dfa8d02012-03-10 13:00:14 -0800294static void iwl_print_mismatch_sec(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800295 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700296{
Johannes Berg35b1d922011-04-05 09:41:56 -0700297 __le32 *image = (__le32 *)fw_desc->v_addr;
298 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700299 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700300 u32 offs;
301 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700302
Johannes Berge1991882012-03-06 13:30:36 -0800303 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700304
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700305 iwl_write_direct32(priv->trans, HBUS_TARG_MEM_RADDR,
David Spinadel6dfa8d02012-03-10 13:00:14 -0800306 fw_desc->offset);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700307
Johannes Bergfb662162011-04-05 09:41:55 -0700308 for (offs = 0;
309 offs < len && errors < 20;
310 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700311 /* read data comes through single port, auto-incr addr */
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700312 val = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700313 if (val != le32_to_cpu(*image)) {
Johannes Berge1991882012-03-06 13:30:36 -0800314 IWL_ERR(priv, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700315 "offset 0x%x, is 0x%x, s/b 0x%x\n",
316 offs, val, le32_to_cpu(*image));
317 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700318 }
319 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700320}
321
322/**
323 * iwl_verify_ucode - determine which instruction image is in SRAM,
324 * and verify its contents
325 */
Johannes Berge1991882012-03-06 13:30:36 -0800326static int iwl_verify_ucode(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800327 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700328{
Johannes Berg0692fe42012-03-06 13:30:37 -0800329 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800330
331 if (!img) {
Johannes Berge1991882012-03-06 13:30:36 -0800332 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800333 return -EINVAL;
334 }
335
David Spinadel6dfa8d02012-03-10 13:00:14 -0800336 if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
Johannes Berge1991882012-03-06 13:30:36 -0800337 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700338 return 0;
339 }
340
Johannes Berge1991882012-03-06 13:30:36 -0800341 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700342
David Spinadel6dfa8d02012-03-10 13:00:14 -0800343 iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
Johannes Bergfb662162011-04-05 09:41:55 -0700344 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700345}
Johannes Bergca7966c2011-04-22 10:15:23 -0700346
Don Fry69a679b2011-12-07 08:50:46 -0800347struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700348 bool valid;
349 u8 subtype;
350};
351
Johannes Bergdb662d42012-03-15 13:26:44 -0700352static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
353 struct iwl_rx_packet *pkt, void *data)
Johannes Bergca7966c2011-04-22 10:15:23 -0700354{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800355 struct iwl_priv *priv =
356 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800357 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700358 struct iwl_alive_resp *palive;
359
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800360 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700361
Johannes Berge1991882012-03-06 13:30:36 -0800362 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700363 "0x%01X 0x%01X\n",
364 palive->is_valid, palive->ver_type,
365 palive->ver_subtype);
366
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700367 priv->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700368 le32_to_cpu(palive->error_event_table_ptr);
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700369 priv->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700370 le32_to_cpu(palive->log_event_table_ptr);
371
372 alive_data->subtype = palive->ver_subtype;
373 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
Johannes Bergdb662d42012-03-15 13:26:44 -0700374
375 return true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700376}
377
378#define UCODE_ALIVE_TIMEOUT HZ
379#define UCODE_CALIB_TIMEOUT (2*HZ)
380
Johannes Berge1991882012-03-06 13:30:36 -0800381int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800382 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700383{
384 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800385 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800386 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700387 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800388 enum iwl_ucode_type old_type;
Johannes Bergdb662d42012-03-15 13:26:44 -0700389 static const u8 alive_cmd[] = { REPLY_ALIVE };
Johannes Bergca7966c2011-04-22 10:15:23 -0700390
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700391 old_type = priv->cur_ucode;
392 priv->cur_ucode = ucode_type;
Johannes Berge1991882012-03-06 13:30:36 -0800393 fw = iwl_get_ucode_image(priv, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700394
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800395 priv->ucode_loaded = false;
396
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200397 if (!fw)
398 return -EINVAL;
399
Johannes Bergdb662d42012-03-15 13:26:44 -0700400 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
401 alive_cmd, ARRAY_SIZE(alive_cmd),
402 iwl_alive_fn, &alive_data);
Johannes Bergf4720892012-03-06 13:30:57 -0800403
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700404 ret = iwl_trans_start_fw(priv->trans, fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700405 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700406 priv->cur_ucode = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800407 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700408 return ret;
409 }
410
Johannes Bergca7966c2011-04-22 10:15:23 -0700411 /*
412 * Some things may run in the background now, but we
413 * just wait for the ALIVE notification here.
414 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800415 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800416 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700417 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700418 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700419 return ret;
420 }
421
422 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800423 IWL_ERR(priv, "Loaded ucode is not valid!\n");
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700424 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700425 return -EIO;
426 }
427
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700428 /*
429 * This step takes a long time (60-80ms!!) and
430 * WoWLAN image should be loaded quickly, so
431 * skip it for WoWLAN.
432 */
433 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Berge1991882012-03-06 13:30:36 -0800434 ret = iwl_verify_ucode(priv, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700435 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700436 priv->cur_ucode = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700437 return ret;
438 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700439
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700440 /* delay a bit to give rfkill time to run */
441 msleep(5);
442 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700443
Johannes Berge1991882012-03-06 13:30:36 -0800444 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700445 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800446 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700447 "Could not complete ALIVE transition: %d\n", ret);
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700448 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700449 return ret;
450 }
451
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800452 priv->ucode_loaded = true;
453
Johannes Bergca7966c2011-04-22 10:15:23 -0700454 return 0;
455}
456
Johannes Berge1f0c502012-03-15 13:26:45 -0700457static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
458 struct iwl_rx_packet *pkt, void *data)
459{
460 struct iwl_priv *priv = data;
461 struct iwl_calib_hdr *hdr;
462 int len;
463
464 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
465 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
466 return true;
467 }
468
469 hdr = (struct iwl_calib_hdr *)pkt->data;
470 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
471
472 /* reduce the size by the length field itself */
473 len -= sizeof(__le32);
474
475 if (iwl_calib_set(priv, hdr, len))
476 IWL_ERR(priv, "Failed to record calibration data %d\n",
477 hdr->op_code);
478
479 return false;
480}
481
Johannes Berge1991882012-03-06 13:30:36 -0800482int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700483{
484 struct iwl_notification_wait calib_wait;
Johannes Bergdb662d42012-03-15 13:26:44 -0700485 static const u8 calib_complete[] = {
Johannes Berge1f0c502012-03-15 13:26:45 -0700486 CALIBRATION_RES_NOTIFICATION,
Johannes Bergdb662d42012-03-15 13:26:44 -0700487 CALIBRATION_COMPLETE_NOTIFICATION
488 };
Johannes Bergca7966c2011-04-22 10:15:23 -0700489 int ret;
490
Johannes Bergb1eea292012-03-06 13:30:42 -0800491 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700492
493 /* No init ucode required? Curious, but maybe ok */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800494 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700495 return 0;
496
David Spinadelb5ea1622012-03-10 13:00:11 -0800497 if (priv->init_ucode_run)
Johannes Bergca7966c2011-04-22 10:15:23 -0700498 return 0;
499
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800500 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergdb662d42012-03-15 13:26:44 -0700501 calib_complete, ARRAY_SIZE(calib_complete),
Johannes Berge1f0c502012-03-15 13:26:45 -0700502 iwlagn_wait_calib, priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700503
504 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800505 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700506 if (ret)
507 goto error;
508
Johannes Berge1991882012-03-06 13:30:36 -0800509 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700510 if (ret)
511 goto error;
512
513 /*
514 * Some things may run in the background now, but we
515 * just wait for the calibration complete notification.
516 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800517 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800518 UCODE_CALIB_TIMEOUT);
David Spinadelb5ea1622012-03-10 13:00:11 -0800519 if (!ret)
520 priv->init_ucode_run = true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700521
522 goto out;
523
524 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800525 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700526 out:
527 /* Whatever happened, stop the device */
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700528 iwl_trans_stop_device(priv->trans);
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800529 priv->ucode_loaded = false;
530
Johannes Bergca7966c2011-04-22 10:15:23 -0700531 return ret;
532}