1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
6  * Copyright(c) 2015 Intel Deutschland GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * The full GNU General Public License is included in this distribution
18  * in the file called COPYING.
19  *
20  * Contact Information:
21  *  Intel Linux Wireless <linuxwifi@intel.com>
22  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
23  *
24  *****************************************************************************/
25 
26 #include <linux/kernel.h>
27 
28 #include "iwl-io.h"
29 #include "iwl-agn-hw.h"
30 #include "iwl-trans.h"
31 #include "iwl-fh.h"
32 #include "iwl-op-mode.h"
33 
34 #include "dev.h"
35 #include "agn.h"
36 #include "calib.h"
37 
38 /******************************************************************************
39  *
40  * uCode download functions
41  *
42  ******************************************************************************/
43 
44 /*
45  *  Calibration
46  */
47 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
48 {
49 	struct iwl_calib_xtal_freq_cmd cmd;
50 	__le16 *xtal_calib = priv->nvm_data->xtal_calib;
51 
52 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
53 	cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
54 	cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
55 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
56 }
57 
58 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
59 {
60 	struct iwl_calib_temperature_offset_cmd cmd;
61 
62 	memset(&cmd, 0, sizeof(cmd));
63 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
64 	cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
65 	if (!(cmd.radio_sensor_offset))
66 		cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
67 
68 	IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
69 			le16_to_cpu(cmd.radio_sensor_offset));
70 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
71 }
72 
73 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
74 {
75 	struct iwl_calib_temperature_offset_v2_cmd cmd;
76 
77 	memset(&cmd, 0, sizeof(cmd));
78 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
79 	cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
80 	cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
81 	if (!cmd.radio_sensor_offset_low) {
82 		IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
83 		cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
84 		cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
85 	}
86 	cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
87 
88 	IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
89 			le16_to_cpu(cmd.radio_sensor_offset_high));
90 	IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
91 			le16_to_cpu(cmd.radio_sensor_offset_low));
92 	IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
93 			le16_to_cpu(cmd.burntVoltageRef));
94 
95 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
96 }
97 
98 static int iwl_send_calib_cfg(struct iwl_priv *priv)
99 {
100 	struct iwl_calib_cfg_cmd calib_cfg_cmd;
101 	struct iwl_host_cmd cmd = {
102 		.id = CALIBRATION_CFG_CMD,
103 		.len = { sizeof(struct iwl_calib_cfg_cmd), },
104 		.data = { &calib_cfg_cmd, },
105 	};
106 
107 	memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
108 	calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
109 	calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
110 	calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
111 	calib_cfg_cmd.ucd_calib_cfg.flags =
112 		IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
113 
114 	return iwl_dvm_send_cmd(priv, &cmd);
115 }
116 
117 int iwl_init_alive_start(struct iwl_priv *priv)
118 {
119 	int ret;
120 
121 	if (priv->lib->bt_params &&
122 	    priv->lib->bt_params->advanced_bt_coexist) {
123 		/*
124 		 * Tell uCode we are ready to perform calibration
125 		 * need to perform this before any calibration
126 		 * no need to close the envlope since we are going
127 		 * to load the runtime uCode later.
128 		 */
129 		ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
130 			BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
131 		if (ret)
132 			return ret;
133 
134 	}
135 
136 	ret = iwl_send_calib_cfg(priv);
137 	if (ret)
138 		return ret;
139 
140 	/**
141 	 * temperature offset calibration is only needed for runtime ucode,
142 	 * so prepare the value now.
143 	 */
144 	if (priv->lib->need_temp_offset_calib) {
145 		if (priv->lib->temp_offset_v2)
146 			return iwl_set_temperature_offset_calib_v2(priv);
147 		else
148 			return iwl_set_temperature_offset_calib(priv);
149 	}
150 
151 	return 0;
152 }
153 
154 static int iwl_send_wimax_coex(struct iwl_priv *priv)
155 {
156 	struct iwl_wimax_coex_cmd coex_cmd;
157 
158 	/* coexistence is disabled */
159 	memset(&coex_cmd, 0, sizeof(coex_cmd));
160 
161 	return iwl_dvm_send_cmd_pdu(priv,
162 				COEX_PRIORITY_TABLE_CMD, 0,
163 				sizeof(coex_cmd), &coex_cmd);
164 }
165 
166 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
167 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
168 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
169 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
170 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
171 	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
172 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
173 	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
174 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
175 	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
176 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
177 	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
178 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
179 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
180 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
181 	((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
182 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
183 	((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
184 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
185 	0, 0, 0, 0, 0, 0, 0
186 };
187 
188 void iwl_send_prio_tbl(struct iwl_priv *priv)
189 {
190 	struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
191 
192 	memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
193 		sizeof(iwl_bt_prio_tbl));
194 	if (iwl_dvm_send_cmd_pdu(priv,
195 				REPLY_BT_COEX_PRIO_TABLE, 0,
196 				sizeof(prio_tbl_cmd), &prio_tbl_cmd))
197 		IWL_ERR(priv, "failed to send BT prio tbl command\n");
198 }
199 
200 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
201 {
202 	struct iwl_bt_coex_prot_env_cmd env_cmd;
203 	int ret;
204 
205 	env_cmd.action = action;
206 	env_cmd.type = type;
207 	ret = iwl_dvm_send_cmd_pdu(priv,
208 			       REPLY_BT_COEX_PROT_ENV, 0,
209 			       sizeof(env_cmd), &env_cmd);
210 	if (ret)
211 		IWL_ERR(priv, "failed to send BT env command\n");
212 	return ret;
213 }
214 
215 static const u8 iwlagn_default_queue_to_tx_fifo[] = {
216 	IWL_TX_FIFO_VO,
217 	IWL_TX_FIFO_VI,
218 	IWL_TX_FIFO_BE,
219 	IWL_TX_FIFO_BK,
220 };
221 
222 static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
223 	IWL_TX_FIFO_VO,
224 	IWL_TX_FIFO_VI,
225 	IWL_TX_FIFO_BE,
226 	IWL_TX_FIFO_BK,
227 	IWL_TX_FIFO_BK_IPAN,
228 	IWL_TX_FIFO_BE_IPAN,
229 	IWL_TX_FIFO_VI_IPAN,
230 	IWL_TX_FIFO_VO_IPAN,
231 	IWL_TX_FIFO_BE_IPAN,
232 	IWL_TX_FIFO_UNUSED,
233 	IWL_TX_FIFO_AUX,
234 };
235 
236 static int iwl_alive_notify(struct iwl_priv *priv)
237 {
238 	const u8 *queue_to_txf;
239 	u8 n_queues;
240 	int ret;
241 	int i;
242 
243 	iwl_trans_fw_alive(priv->trans, 0);
244 
245 	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
246 	    priv->nvm_data->sku_cap_ipan_enable) {
247 		n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
248 		queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
249 	} else {
250 		n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
251 		queue_to_txf = iwlagn_default_queue_to_tx_fifo;
252 	}
253 
254 	for (i = 0; i < n_queues; i++)
255 		if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
256 			iwl_trans_ac_txq_enable(priv->trans, i,
257 						queue_to_txf[i], 0);
258 
259 	priv->passive_no_rx = false;
260 	priv->transport_queue_stop = 0;
261 
262 	ret = iwl_send_wimax_coex(priv);
263 	if (ret)
264 		return ret;
265 
266 	if (!priv->lib->no_xtal_calib) {
267 		ret = iwl_set_Xtal_calib(priv);
268 		if (ret)
269 			return ret;
270 	}
271 
272 	return iwl_send_calib_results(priv);
273 }
274 
275 struct iwl_alive_data {
276 	bool valid;
277 	u8 subtype;
278 };
279 
280 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
281 			 struct iwl_rx_packet *pkt, void *data)
282 {
283 	struct iwl_priv *priv =
284 		container_of(notif_wait, struct iwl_priv, notif_wait);
285 	struct iwl_alive_data *alive_data = data;
286 	struct iwl_alive_resp *palive;
287 
288 	palive = (void *)pkt->data;
289 
290 	IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
291 		       "0x%01X 0x%01X\n",
292 		       palive->is_valid, palive->ver_type,
293 		       palive->ver_subtype);
294 
295 	priv->device_pointers.error_event_table =
296 		le32_to_cpu(palive->error_event_table_ptr);
297 	priv->device_pointers.log_event_table =
298 		le32_to_cpu(palive->log_event_table_ptr);
299 
300 	alive_data->subtype = palive->ver_subtype;
301 	alive_data->valid = palive->is_valid == UCODE_VALID_OK;
302 
303 	return true;
304 }
305 
306 #define UCODE_ALIVE_TIMEOUT	HZ
307 #define UCODE_CALIB_TIMEOUT	(2*HZ)
308 
309 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
310 				 enum iwl_ucode_type ucode_type)
311 {
312 	struct iwl_notification_wait alive_wait;
313 	struct iwl_alive_data alive_data;
314 	const struct fw_img *fw;
315 	int ret;
316 	enum iwl_ucode_type old_type;
317 	static const u16 alive_cmd[] = { REPLY_ALIVE };
318 
319 	fw = iwl_get_ucode_image(priv->fw, ucode_type);
320 	if (WARN_ON(!fw))
321 		return -EINVAL;
322 
323 	old_type = priv->cur_ucode;
324 	priv->cur_ucode = ucode_type;
325 	priv->ucode_loaded = false;
326 
327 	iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
328 				   alive_cmd, ARRAY_SIZE(alive_cmd),
329 				   iwl_alive_fn, &alive_data);
330 
331 	ret = iwl_trans_start_fw(priv->trans, fw, false);
332 	if (ret) {
333 		priv->cur_ucode = old_type;
334 		iwl_remove_notification(&priv->notif_wait, &alive_wait);
335 		return ret;
336 	}
337 
338 	/*
339 	 * Some things may run in the background now, but we
340 	 * just wait for the ALIVE notification here.
341 	 */
342 	ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
343 					UCODE_ALIVE_TIMEOUT);
344 	if (ret) {
345 		priv->cur_ucode = old_type;
346 		return ret;
347 	}
348 
349 	if (!alive_data.valid) {
350 		IWL_ERR(priv, "Loaded ucode is not valid!\n");
351 		priv->cur_ucode = old_type;
352 		return -EIO;
353 	}
354 
355 	priv->ucode_loaded = true;
356 
357 	if (ucode_type != IWL_UCODE_WOWLAN) {
358 		/* delay a bit to give rfkill time to run */
359 		msleep(5);
360 	}
361 
362 	ret = iwl_alive_notify(priv);
363 	if (ret) {
364 		IWL_WARN(priv,
365 			"Could not complete ALIVE transition: %d\n", ret);
366 		priv->cur_ucode = old_type;
367 		return ret;
368 	}
369 
370 	return 0;
371 }
372 
373 static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
374 			      struct iwl_rx_packet *pkt, void *data)
375 {
376 	struct iwl_priv *priv = data;
377 	struct iwl_calib_hdr *hdr;
378 
379 	if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
380 		WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
381 		return true;
382 	}
383 
384 	hdr = (struct iwl_calib_hdr *)pkt->data;
385 
386 	if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
387 		IWL_ERR(priv, "Failed to record calibration data %d\n",
388 			hdr->op_code);
389 
390 	return false;
391 }
392 
393 int iwl_run_init_ucode(struct iwl_priv *priv)
394 {
395 	struct iwl_notification_wait calib_wait;
396 	static const u16 calib_complete[] = {
397 		CALIBRATION_RES_NOTIFICATION,
398 		CALIBRATION_COMPLETE_NOTIFICATION
399 	};
400 	int ret;
401 
402 	lockdep_assert_held(&priv->mutex);
403 
404 	/* No init ucode required? Curious, but maybe ok */
405 	if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
406 		return 0;
407 
408 	iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
409 				   calib_complete, ARRAY_SIZE(calib_complete),
410 				   iwlagn_wait_calib, priv);
411 
412 	/* Will also start the device */
413 	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
414 	if (ret)
415 		goto error;
416 
417 	ret = iwl_init_alive_start(priv);
418 	if (ret)
419 		goto error;
420 
421 	/*
422 	 * Some things may run in the background now, but we
423 	 * just wait for the calibration complete notification.
424 	 */
425 	ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
426 					UCODE_CALIB_TIMEOUT);
427 
428 	goto out;
429 
430  error:
431 	iwl_remove_notification(&priv->notif_wait, &calib_wait);
432  out:
433 	/* Whatever happened, stop the device */
434 	iwl_trans_stop_device(priv->trans);
435 	priv->ucode_loaded = false;
436 
437 	return ret;
438 }
439