1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
4 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2015 Intel Deutschland GmbH
8 * Intel Linux Wireless <linuxwifi@intel.com>
9 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
11 *****************************************************************************/
13 #include <linux/kernel.h>
16 #include "iwl-agn-hw.h"
17 #include "iwl-trans.h"
19 #include "iwl-op-mode.h"
25 /******************************************************************************
27 * uCode download functions
29 ******************************************************************************/
34 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
36 struct iwl_calib_xtal_freq_cmd cmd;
37 __le16 *xtal_calib = priv->nvm_data->xtal_calib;
39 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
40 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
41 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
42 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
45 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
47 struct iwl_calib_temperature_offset_cmd cmd;
49 memset(&cmd, 0, sizeof(cmd));
50 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
51 cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
52 if (!(cmd.radio_sensor_offset))
53 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
55 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
56 le16_to_cpu(cmd.radio_sensor_offset));
57 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
60 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
62 struct iwl_calib_temperature_offset_v2_cmd cmd;
64 memset(&cmd, 0, sizeof(cmd));
65 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
66 cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
67 cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
68 if (!cmd.radio_sensor_offset_low) {
69 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
70 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
71 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
73 cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
75 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
76 le16_to_cpu(cmd.radio_sensor_offset_high));
77 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
78 le16_to_cpu(cmd.radio_sensor_offset_low));
79 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
80 le16_to_cpu(cmd.burntVoltageRef));
82 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
85 static int iwl_send_calib_cfg(struct iwl_priv *priv)
87 struct iwl_calib_cfg_cmd calib_cfg_cmd;
88 struct iwl_host_cmd cmd = {
89 .id = CALIBRATION_CFG_CMD,
90 .len = { sizeof(struct iwl_calib_cfg_cmd), },
91 .data = { &calib_cfg_cmd, },
94 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
95 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
96 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
97 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
98 calib_cfg_cmd.ucd_calib_cfg.flags =
99 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
101 return iwl_dvm_send_cmd(priv, &cmd);
104 int iwl_init_alive_start(struct iwl_priv *priv)
108 if (priv->lib->bt_params &&
109 priv->lib->bt_params->advanced_bt_coexist) {
111 * Tell uCode we are ready to perform calibration
112 * need to perform this before any calibration
113 * no need to close the envlope since we are going
114 * to load the runtime uCode later.
116 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
117 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
123 ret = iwl_send_calib_cfg(priv);
128 * temperature offset calibration is only needed for runtime ucode,
129 * so prepare the value now.
131 if (priv->lib->need_temp_offset_calib) {
132 if (priv->lib->temp_offset_v2)
133 return iwl_set_temperature_offset_calib_v2(priv);
135 return iwl_set_temperature_offset_calib(priv);
141 static int iwl_send_wimax_coex(struct iwl_priv *priv)
143 struct iwl_wimax_coex_cmd coex_cmd;
145 /* coexistence is disabled */
146 memset(&coex_cmd, 0, sizeof(coex_cmd));
148 return iwl_dvm_send_cmd_pdu(priv,
149 COEX_PRIORITY_TABLE_CMD, 0,
150 sizeof(coex_cmd), &coex_cmd);
153 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
154 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
155 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
156 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
157 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
158 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
159 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
160 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
161 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
162 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
163 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
164 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
165 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
166 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
167 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
168 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
169 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
170 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
171 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
175 void iwl_send_prio_tbl(struct iwl_priv *priv)
177 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
179 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
180 sizeof(iwl_bt_prio_tbl));
181 if (iwl_dvm_send_cmd_pdu(priv,
182 REPLY_BT_COEX_PRIO_TABLE, 0,
183 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
184 IWL_ERR(priv, "failed to send BT prio tbl command\n");
187 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
189 struct iwl_bt_coex_prot_env_cmd env_cmd;
192 env_cmd.action = action;
194 ret = iwl_dvm_send_cmd_pdu(priv,
195 REPLY_BT_COEX_PROT_ENV, 0,
196 sizeof(env_cmd), &env_cmd);
198 IWL_ERR(priv, "failed to send BT env command\n");
202 static const u8 iwlagn_default_queue_to_tx_fifo[] = {
209 static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
223 static int iwl_alive_notify(struct iwl_priv *priv)
225 const u8 *queue_to_txf;
230 iwl_trans_fw_alive(priv->trans, 0);
232 if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
233 priv->nvm_data->sku_cap_ipan_enable) {
234 n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
235 queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
237 n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
238 queue_to_txf = iwlagn_default_queue_to_tx_fifo;
241 for (i = 0; i < n_queues; i++)
242 if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
243 iwl_trans_ac_txq_enable(priv->trans, i,
246 priv->passive_no_rx = false;
247 priv->transport_queue_stop = 0;
249 ret = iwl_send_wimax_coex(priv);
253 if (!priv->lib->no_xtal_calib) {
254 ret = iwl_set_Xtal_calib(priv);
259 return iwl_send_calib_results(priv);
262 struct iwl_alive_data {
267 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
268 struct iwl_rx_packet *pkt, void *data)
270 struct iwl_priv *priv =
271 container_of(notif_wait, struct iwl_priv, notif_wait);
272 struct iwl_alive_data *alive_data = data;
273 struct iwl_alive_resp *palive;
275 palive = (void *)pkt->data;
277 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
279 palive->is_valid, palive->ver_type,
280 palive->ver_subtype);
282 priv->device_pointers.error_event_table =
283 le32_to_cpu(palive->error_event_table_ptr);
284 priv->device_pointers.log_event_table =
285 le32_to_cpu(palive->log_event_table_ptr);
287 alive_data->subtype = palive->ver_subtype;
288 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
293 #define UCODE_ALIVE_TIMEOUT HZ
294 #define UCODE_CALIB_TIMEOUT (2*HZ)
296 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
297 enum iwl_ucode_type ucode_type)
299 struct iwl_notification_wait alive_wait;
300 struct iwl_alive_data alive_data;
301 const struct fw_img *fw;
303 enum iwl_ucode_type old_type;
304 static const u16 alive_cmd[] = { REPLY_ALIVE };
306 fw = iwl_get_ucode_image(priv->fw, ucode_type);
310 old_type = priv->cur_ucode;
311 priv->cur_ucode = ucode_type;
312 priv->ucode_loaded = false;
314 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
315 alive_cmd, ARRAY_SIZE(alive_cmd),
316 iwl_alive_fn, &alive_data);
318 ret = iwl_trans_start_fw(priv->trans, fw, false);
320 priv->cur_ucode = old_type;
321 iwl_remove_notification(&priv->notif_wait, &alive_wait);
326 * Some things may run in the background now, but we
327 * just wait for the ALIVE notification here.
329 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
330 UCODE_ALIVE_TIMEOUT);
332 priv->cur_ucode = old_type;
336 if (!alive_data.valid) {
337 IWL_ERR(priv, "Loaded ucode is not valid!\n");
338 priv->cur_ucode = old_type;
342 priv->ucode_loaded = true;
344 if (ucode_type != IWL_UCODE_WOWLAN) {
345 /* delay a bit to give rfkill time to run */
349 ret = iwl_alive_notify(priv);
352 "Could not complete ALIVE transition: %d\n", ret);
353 priv->cur_ucode = old_type;
360 static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
361 struct iwl_rx_packet *pkt, void *data)
363 struct iwl_priv *priv = data;
364 struct iwl_calib_hdr *hdr;
366 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
367 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
371 hdr = (struct iwl_calib_hdr *)pkt->data;
373 if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
374 IWL_ERR(priv, "Failed to record calibration data %d\n",
380 int iwl_run_init_ucode(struct iwl_priv *priv)
382 struct iwl_notification_wait calib_wait;
383 static const u16 calib_complete[] = {
384 CALIBRATION_RES_NOTIFICATION,
385 CALIBRATION_COMPLETE_NOTIFICATION
389 lockdep_assert_held(&priv->mutex);
391 /* No init ucode required? Curious, but maybe ok */
392 if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
395 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
396 calib_complete, ARRAY_SIZE(calib_complete),
397 iwlagn_wait_calib, priv);
399 /* Will also start the device */
400 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
404 ret = iwl_init_alive_start(priv);
409 * Some things may run in the background now, but we
410 * just wait for the calibration complete notification.
412 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
413 UCODE_CALIB_TIMEOUT);
418 iwl_remove_notification(&priv->notif_wait, &calib_wait);
420 /* Whatever happened, stop the device */
421 iwl_trans_stop_device(priv->trans);
422 priv->ucode_loaded = false;