1 /******************************************************************************
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
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.
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.
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,
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/init.h>
36 #include "iwl-agn-hw.h"
38 #include "iwl-agn-calib.h"
39 #include "iwl-trans.h"
41 #include "iwl-op-mode.h"
43 static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
44 {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
45 0, COEX_UNASSOC_IDLE_FLAGS},
46 {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
47 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
48 {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
49 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
50 {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
51 0, COEX_CALIBRATION_FLAGS},
52 {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
53 0, COEX_PERIODIC_CALIBRATION_FLAGS},
54 {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
55 0, COEX_CONNECTION_ESTAB_FLAGS},
56 {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
57 0, COEX_ASSOCIATED_IDLE_FLAGS},
58 {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
59 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
60 {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
61 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
62 {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
63 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
64 {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
65 {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
66 {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
67 0, COEX_STAND_ALONE_DEBUG_FLAGS},
68 {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
69 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
70 {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
71 {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
74 /******************************************************************************
76 * uCode download functions
78 ******************************************************************************/
80 static inline const struct fw_img *
81 iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
85 return &priv->fw->ucode_init;
86 case IWL_UCODE_WOWLAN:
87 return &priv->fw->ucode_wowlan;
88 case IWL_UCODE_REGULAR:
89 return &priv->fw->ucode_rt;
99 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
101 struct iwl_calib_xtal_freq_cmd cmd;
103 (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
105 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
106 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
107 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
108 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
111 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
113 struct iwl_calib_temperature_offset_cmd cmd;
114 __le16 *offset_calib =
115 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
116 EEPROM_RAW_TEMPERATURE);
118 memset(&cmd, 0, sizeof(cmd));
119 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
120 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
121 if (!(cmd.radio_sensor_offset))
122 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
124 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
125 le16_to_cpu(cmd.radio_sensor_offset));
126 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
129 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
131 struct iwl_calib_temperature_offset_v2_cmd cmd;
132 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
133 EEPROM_KELVIN_TEMPERATURE);
134 __le16 *offset_calib_low =
135 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
136 EEPROM_RAW_TEMPERATURE);
137 struct iwl_eeprom_calib_hdr *hdr;
139 memset(&cmd, 0, sizeof(cmd));
140 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
141 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
143 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
144 sizeof(*offset_calib_high));
145 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
146 sizeof(*offset_calib_low));
147 if (!(cmd.radio_sensor_offset_low)) {
148 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
149 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
150 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
152 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
153 sizeof(hdr->voltage));
155 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
156 le16_to_cpu(cmd.radio_sensor_offset_high));
157 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
158 le16_to_cpu(cmd.radio_sensor_offset_low));
159 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
160 le16_to_cpu(cmd.burntVoltageRef));
162 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
165 static int iwl_send_calib_cfg(struct iwl_priv *priv)
167 struct iwl_calib_cfg_cmd calib_cfg_cmd;
168 struct iwl_host_cmd cmd = {
169 .id = CALIBRATION_CFG_CMD,
170 .len = { sizeof(struct iwl_calib_cfg_cmd), },
171 .data = { &calib_cfg_cmd, },
174 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
175 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
176 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
177 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
178 calib_cfg_cmd.ucd_calib_cfg.flags =
179 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
181 return iwl_dvm_send_cmd(priv, &cmd);
184 int iwlagn_rx_calib_result(struct iwl_priv *priv,
185 struct iwl_rx_cmd_buffer *rxb,
186 struct iwl_device_cmd *cmd)
188 struct iwl_rx_packet *pkt = rxb_addr(rxb);
189 struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->data;
190 int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
192 /* reduce the size of the length field itself */
195 if (iwl_calib_set(priv, hdr, len))
196 IWL_ERR(priv, "Failed to record calibration data %d\n",
202 int iwl_init_alive_start(struct iwl_priv *priv)
206 if (cfg(priv)->bt_params &&
207 cfg(priv)->bt_params->advanced_bt_coexist) {
209 * Tell uCode we are ready to perform calibration
210 * need to perform this before any calibration
211 * no need to close the envlope since we are going
212 * to load the runtime uCode later.
214 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
215 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
221 ret = iwl_send_calib_cfg(priv);
226 * temperature offset calibration is only needed for runtime ucode,
227 * so prepare the value now.
229 if (cfg(priv)->need_temp_offset_calib) {
230 if (cfg(priv)->temp_offset_v2)
231 return iwl_set_temperature_offset_calib_v2(priv);
233 return iwl_set_temperature_offset_calib(priv);
239 static int iwl_send_wimax_coex(struct iwl_priv *priv)
241 struct iwl_wimax_coex_cmd coex_cmd;
243 if (cfg(priv)->base_params->support_wimax_coexist) {
244 /* UnMask wake up src at associated sleep */
245 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
247 /* UnMask wake up src at unassociated sleep */
248 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
249 memcpy(coex_cmd.sta_prio, cu_priorities,
250 sizeof(struct iwl_wimax_coex_event_entry) *
253 /* enabling the coexistence feature */
254 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
256 /* enabling the priorities tables */
257 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
259 /* coexistence is disabled */
260 memset(&coex_cmd, 0, sizeof(coex_cmd));
262 return iwl_dvm_send_cmd_pdu(priv,
263 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
264 sizeof(coex_cmd), &coex_cmd);
267 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
268 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
269 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
270 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
271 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
272 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
273 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
274 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
275 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
276 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
277 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
278 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
279 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
280 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
281 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
282 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
283 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
284 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
285 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
289 void iwl_send_prio_tbl(struct iwl_priv *priv)
291 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
293 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
294 sizeof(iwl_bt_prio_tbl));
295 if (iwl_dvm_send_cmd_pdu(priv,
296 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
297 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
298 IWL_ERR(priv, "failed to send BT prio tbl command\n");
301 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
303 struct iwl_bt_coex_prot_env_cmd env_cmd;
306 env_cmd.action = action;
308 ret = iwl_dvm_send_cmd_pdu(priv,
309 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
310 sizeof(env_cmd), &env_cmd);
312 IWL_ERR(priv, "failed to send BT env command\n");
317 static int iwl_alive_notify(struct iwl_priv *priv)
321 iwl_trans_fw_alive(trans(priv));
323 priv->passive_no_rx = false;
324 priv->transport_queue_stop = 0;
326 ret = iwl_send_wimax_coex(priv);
330 if (!cfg(priv)->no_xtal_calib) {
331 ret = iwl_set_Xtal_calib(priv);
336 return iwl_send_calib_results(priv);
341 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
342 * using sample data 100 bytes apart. If these sample points are good,
343 * it's a pretty good bet that everything between them is good, too.
345 static int iwl_verify_inst_sparse(struct iwl_priv *priv,
346 const struct fw_desc *fw_desc)
348 __le32 *image = (__le32 *)fw_desc->v_addr;
349 u32 len = fw_desc->len;
353 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
355 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
356 /* read data comes through single port, auto-incr addr */
357 /* NOTE: Use the debugless read so we don't flood kernel log
358 * if IWL_DL_IO is set */
359 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
360 i + IWLAGN_RTC_INST_LOWER_BOUND);
361 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
362 if (val != le32_to_cpu(*image))
369 static void iwl_print_mismatch_inst(struct iwl_priv *priv,
370 const struct fw_desc *fw_desc)
372 __le32 *image = (__le32 *)fw_desc->v_addr;
373 u32 len = fw_desc->len;
378 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
380 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
381 IWLAGN_RTC_INST_LOWER_BOUND);
384 offs < len && errors < 20;
385 offs += sizeof(u32), image++) {
386 /* read data comes through single port, auto-incr addr */
387 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
388 if (val != le32_to_cpu(*image)) {
389 IWL_ERR(priv, "uCode INST section at "
390 "offset 0x%x, is 0x%x, s/b 0x%x\n",
391 offs, val, le32_to_cpu(*image));
398 * iwl_verify_ucode - determine which instruction image is in SRAM,
399 * and verify its contents
401 static int iwl_verify_ucode(struct iwl_priv *priv,
402 enum iwl_ucode_type ucode_type)
404 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
407 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
411 if (!iwl_verify_inst_sparse(priv, &img->code)) {
412 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
416 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
418 iwl_print_mismatch_inst(priv, &img->code);
422 struct iwl_alive_data {
427 static void iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
428 struct iwl_rx_packet *pkt,
431 struct iwl_priv *priv =
432 container_of(notif_wait, struct iwl_priv, notif_wait);
433 struct iwl_alive_data *alive_data = data;
434 struct iwl_alive_resp *palive;
436 palive = (void *)pkt->data;
438 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
440 palive->is_valid, palive->ver_type,
441 palive->ver_subtype);
443 priv->shrd->device_pointers.error_event_table =
444 le32_to_cpu(palive->error_event_table_ptr);
445 priv->shrd->device_pointers.log_event_table =
446 le32_to_cpu(palive->log_event_table_ptr);
448 alive_data->subtype = palive->ver_subtype;
449 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
452 #define UCODE_ALIVE_TIMEOUT HZ
453 #define UCODE_CALIB_TIMEOUT (2*HZ)
455 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
456 enum iwl_ucode_type ucode_type)
458 struct iwl_notification_wait alive_wait;
459 struct iwl_alive_data alive_data;
460 const struct fw_img *fw;
462 enum iwl_ucode_type old_type;
464 old_type = priv->shrd->ucode_type;
465 priv->shrd->ucode_type = ucode_type;
466 fw = iwl_get_ucode_image(priv, ucode_type);
468 priv->ucode_loaded = false;
473 iwl_init_notification_wait(&priv->notif_wait, &alive_wait, REPLY_ALIVE,
474 iwl_alive_fn, &alive_data);
476 ret = iwl_trans_start_fw(trans(priv), fw);
478 priv->shrd->ucode_type = old_type;
479 iwl_remove_notification(&priv->notif_wait, &alive_wait);
484 * Some things may run in the background now, but we
485 * just wait for the ALIVE notification here.
487 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
488 UCODE_ALIVE_TIMEOUT);
490 priv->shrd->ucode_type = old_type;
494 if (!alive_data.valid) {
495 IWL_ERR(priv, "Loaded ucode is not valid!\n");
496 priv->shrd->ucode_type = old_type;
501 * This step takes a long time (60-80ms!!) and
502 * WoWLAN image should be loaded quickly, so
503 * skip it for WoWLAN.
505 if (ucode_type != IWL_UCODE_WOWLAN) {
506 ret = iwl_verify_ucode(priv, ucode_type);
508 priv->shrd->ucode_type = old_type;
512 /* delay a bit to give rfkill time to run */
516 ret = iwl_alive_notify(priv);
519 "Could not complete ALIVE transition: %d\n", ret);
520 priv->shrd->ucode_type = old_type;
524 priv->ucode_loaded = true;
529 int iwl_run_init_ucode(struct iwl_priv *priv)
531 struct iwl_notification_wait calib_wait;
534 lockdep_assert_held(&priv->mutex);
536 /* No init ucode required? Curious, but maybe ok */
537 if (!priv->fw->ucode_init.code.len)
540 if (priv->init_ucode_run)
543 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
544 CALIBRATION_COMPLETE_NOTIFICATION,
547 /* Will also start the device */
548 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
552 ret = iwl_init_alive_start(priv);
557 * Some things may run in the background now, but we
558 * just wait for the calibration complete notification.
560 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
561 UCODE_CALIB_TIMEOUT);
563 priv->init_ucode_run = true;
568 iwl_remove_notification(&priv->notif_wait, &calib_wait);
570 /* Whatever happened, stop the device */
571 iwl_trans_stop_device(trans(priv));
572 priv->ucode_loaded = false;