Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003  *
0004  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
0005  * Copyright(c) 2015 Intel Deutschland GmbH
0006  *****************************************************************************/
0007 
0008 #include <linux/kernel.h>
0009 
0010 #include "iwl-io.h"
0011 #include "iwl-agn-hw.h"
0012 #include "iwl-trans.h"
0013 #include "iwl-fh.h"
0014 #include "iwl-op-mode.h"
0015 
0016 #include "dev.h"
0017 #include "agn.h"
0018 #include "calib.h"
0019 
0020 /******************************************************************************
0021  *
0022  * uCode download functions
0023  *
0024  ******************************************************************************/
0025 
0026 /*
0027  *  Calibration
0028  */
0029 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
0030 {
0031     struct iwl_calib_xtal_freq_cmd cmd;
0032     __le16 *xtal_calib = priv->nvm_data->xtal_calib;
0033 
0034     iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
0035     cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
0036     cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
0037     return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
0038 }
0039 
0040 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
0041 {
0042     struct iwl_calib_temperature_offset_cmd cmd;
0043 
0044     memset(&cmd, 0, sizeof(cmd));
0045     iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
0046     cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
0047     if (!(cmd.radio_sensor_offset))
0048         cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
0049 
0050     IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
0051             le16_to_cpu(cmd.radio_sensor_offset));
0052     return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
0053 }
0054 
0055 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
0056 {
0057     struct iwl_calib_temperature_offset_v2_cmd cmd;
0058 
0059     memset(&cmd, 0, sizeof(cmd));
0060     iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
0061     cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
0062     cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
0063     if (!cmd.radio_sensor_offset_low) {
0064         IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
0065         cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
0066         cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
0067     }
0068     cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
0069 
0070     IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
0071             le16_to_cpu(cmd.radio_sensor_offset_high));
0072     IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
0073             le16_to_cpu(cmd.radio_sensor_offset_low));
0074     IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
0075             le16_to_cpu(cmd.burntVoltageRef));
0076 
0077     return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
0078 }
0079 
0080 static int iwl_send_calib_cfg(struct iwl_priv *priv)
0081 {
0082     struct iwl_calib_cfg_cmd calib_cfg_cmd;
0083     struct iwl_host_cmd cmd = {
0084         .id = CALIBRATION_CFG_CMD,
0085         .len = { sizeof(struct iwl_calib_cfg_cmd), },
0086         .data = { &calib_cfg_cmd, },
0087     };
0088 
0089     memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
0090     calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
0091     calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
0092     calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
0093     calib_cfg_cmd.ucd_calib_cfg.flags =
0094         IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
0095 
0096     return iwl_dvm_send_cmd(priv, &cmd);
0097 }
0098 
0099 int iwl_init_alive_start(struct iwl_priv *priv)
0100 {
0101     int ret;
0102 
0103     if (priv->lib->bt_params &&
0104         priv->lib->bt_params->advanced_bt_coexist) {
0105         /*
0106          * Tell uCode we are ready to perform calibration
0107          * need to perform this before any calibration
0108          * no need to close the envlope since we are going
0109          * to load the runtime uCode later.
0110          */
0111         ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
0112             BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
0113         if (ret)
0114             return ret;
0115 
0116     }
0117 
0118     ret = iwl_send_calib_cfg(priv);
0119     if (ret)
0120         return ret;
0121 
0122     /**
0123      * temperature offset calibration is only needed for runtime ucode,
0124      * so prepare the value now.
0125      */
0126     if (priv->lib->need_temp_offset_calib) {
0127         if (priv->lib->temp_offset_v2)
0128             return iwl_set_temperature_offset_calib_v2(priv);
0129         else
0130             return iwl_set_temperature_offset_calib(priv);
0131     }
0132 
0133     return 0;
0134 }
0135 
0136 static int iwl_send_wimax_coex(struct iwl_priv *priv)
0137 {
0138     struct iwl_wimax_coex_cmd coex_cmd;
0139 
0140     /* coexistence is disabled */
0141     memset(&coex_cmd, 0, sizeof(coex_cmd));
0142 
0143     return iwl_dvm_send_cmd_pdu(priv,
0144                 COEX_PRIORITY_TABLE_CMD, 0,
0145                 sizeof(coex_cmd), &coex_cmd);
0146 }
0147 
0148 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
0149     ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0150         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0151     ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0152         (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0153     ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0154         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0155     ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0156         (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0157     ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0158         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0159     ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0160         (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0161     ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0162         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0163     ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0164         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0165     ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
0166         (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
0167     0, 0, 0, 0, 0, 0, 0
0168 };
0169 
0170 void iwl_send_prio_tbl(struct iwl_priv *priv)
0171 {
0172     struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
0173 
0174     memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
0175         sizeof(iwl_bt_prio_tbl));
0176     if (iwl_dvm_send_cmd_pdu(priv,
0177                 REPLY_BT_COEX_PRIO_TABLE, 0,
0178                 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
0179         IWL_ERR(priv, "failed to send BT prio tbl command\n");
0180 }
0181 
0182 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
0183 {
0184     struct iwl_bt_coex_prot_env_cmd env_cmd;
0185     int ret;
0186 
0187     env_cmd.action = action;
0188     env_cmd.type = type;
0189     ret = iwl_dvm_send_cmd_pdu(priv,
0190                    REPLY_BT_COEX_PROT_ENV, 0,
0191                    sizeof(env_cmd), &env_cmd);
0192     if (ret)
0193         IWL_ERR(priv, "failed to send BT env command\n");
0194     return ret;
0195 }
0196 
0197 static const u8 iwlagn_default_queue_to_tx_fifo[] = {
0198     IWL_TX_FIFO_VO,
0199     IWL_TX_FIFO_VI,
0200     IWL_TX_FIFO_BE,
0201     IWL_TX_FIFO_BK,
0202 };
0203 
0204 static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
0205     IWL_TX_FIFO_VO,
0206     IWL_TX_FIFO_VI,
0207     IWL_TX_FIFO_BE,
0208     IWL_TX_FIFO_BK,
0209     IWL_TX_FIFO_BK_IPAN,
0210     IWL_TX_FIFO_BE_IPAN,
0211     IWL_TX_FIFO_VI_IPAN,
0212     IWL_TX_FIFO_VO_IPAN,
0213     IWL_TX_FIFO_BE_IPAN,
0214     IWL_TX_FIFO_UNUSED,
0215     IWL_TX_FIFO_AUX,
0216 };
0217 
0218 static int iwl_alive_notify(struct iwl_priv *priv)
0219 {
0220     const u8 *queue_to_txf;
0221     u8 n_queues;
0222     int ret;
0223     int i;
0224 
0225     iwl_trans_fw_alive(priv->trans, 0);
0226 
0227     if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
0228         priv->nvm_data->sku_cap_ipan_enable) {
0229         n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
0230         queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
0231     } else {
0232         n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
0233         queue_to_txf = iwlagn_default_queue_to_tx_fifo;
0234     }
0235 
0236     for (i = 0; i < n_queues; i++)
0237         if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
0238             iwl_trans_ac_txq_enable(priv->trans, i,
0239                         queue_to_txf[i], 0);
0240 
0241     priv->passive_no_rx = false;
0242     priv->transport_queue_stop = 0;
0243 
0244     ret = iwl_send_wimax_coex(priv);
0245     if (ret)
0246         return ret;
0247 
0248     if (!priv->lib->no_xtal_calib) {
0249         ret = iwl_set_Xtal_calib(priv);
0250         if (ret)
0251             return ret;
0252     }
0253 
0254     return iwl_send_calib_results(priv);
0255 }
0256 
0257 struct iwl_alive_data {
0258     bool valid;
0259     u8 subtype;
0260 };
0261 
0262 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
0263              struct iwl_rx_packet *pkt, void *data)
0264 {
0265     struct iwl_priv *priv =
0266         container_of(notif_wait, struct iwl_priv, notif_wait);
0267     struct iwl_alive_data *alive_data = data;
0268     struct iwl_alive_resp *palive;
0269 
0270     palive = (void *)pkt->data;
0271 
0272     IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
0273                "0x%01X 0x%01X\n",
0274                palive->is_valid, palive->ver_type,
0275                palive->ver_subtype);
0276 
0277     priv->device_pointers.error_event_table =
0278         le32_to_cpu(palive->error_event_table_ptr);
0279     priv->device_pointers.log_event_table =
0280         le32_to_cpu(palive->log_event_table_ptr);
0281 
0282     alive_data->subtype = palive->ver_subtype;
0283     alive_data->valid = palive->is_valid == UCODE_VALID_OK;
0284 
0285     return true;
0286 }
0287 
0288 #define UCODE_ALIVE_TIMEOUT HZ
0289 #define UCODE_CALIB_TIMEOUT (2*HZ)
0290 
0291 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
0292                  enum iwl_ucode_type ucode_type)
0293 {
0294     struct iwl_notification_wait alive_wait;
0295     struct iwl_alive_data alive_data;
0296     const struct fw_img *fw;
0297     int ret;
0298     enum iwl_ucode_type old_type;
0299     static const u16 alive_cmd[] = { REPLY_ALIVE };
0300 
0301     fw = iwl_get_ucode_image(priv->fw, ucode_type);
0302     if (WARN_ON(!fw))
0303         return -EINVAL;
0304 
0305     old_type = priv->cur_ucode;
0306     priv->cur_ucode = ucode_type;
0307     priv->ucode_loaded = false;
0308 
0309     iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
0310                    alive_cmd, ARRAY_SIZE(alive_cmd),
0311                    iwl_alive_fn, &alive_data);
0312 
0313     ret = iwl_trans_start_fw(priv->trans, fw, false);
0314     if (ret) {
0315         priv->cur_ucode = old_type;
0316         iwl_remove_notification(&priv->notif_wait, &alive_wait);
0317         return ret;
0318     }
0319 
0320     /*
0321      * Some things may run in the background now, but we
0322      * just wait for the ALIVE notification here.
0323      */
0324     ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
0325                     UCODE_ALIVE_TIMEOUT);
0326     if (ret) {
0327         priv->cur_ucode = old_type;
0328         return ret;
0329     }
0330 
0331     if (!alive_data.valid) {
0332         IWL_ERR(priv, "Loaded ucode is not valid!\n");
0333         priv->cur_ucode = old_type;
0334         return -EIO;
0335     }
0336 
0337     priv->ucode_loaded = true;
0338 
0339     if (ucode_type != IWL_UCODE_WOWLAN) {
0340         /* delay a bit to give rfkill time to run */
0341         msleep(5);
0342     }
0343 
0344     ret = iwl_alive_notify(priv);
0345     if (ret) {
0346         IWL_WARN(priv,
0347             "Could not complete ALIVE transition: %d\n", ret);
0348         priv->cur_ucode = old_type;
0349         return ret;
0350     }
0351 
0352     return 0;
0353 }
0354 
0355 static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
0356                   struct iwl_rx_packet *pkt, void *data)
0357 {
0358     struct iwl_priv *priv = data;
0359     struct iwl_calib_hdr *hdr;
0360 
0361     if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
0362         WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
0363         return true;
0364     }
0365 
0366     hdr = (struct iwl_calib_hdr *)pkt->data;
0367 
0368     if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
0369         IWL_ERR(priv, "Failed to record calibration data %d\n",
0370             hdr->op_code);
0371 
0372     return false;
0373 }
0374 
0375 int iwl_run_init_ucode(struct iwl_priv *priv)
0376 {
0377     struct iwl_notification_wait calib_wait;
0378     static const u16 calib_complete[] = {
0379         CALIBRATION_RES_NOTIFICATION,
0380         CALIBRATION_COMPLETE_NOTIFICATION
0381     };
0382     int ret;
0383 
0384     lockdep_assert_held(&priv->mutex);
0385 
0386     /* No init ucode required? Curious, but maybe ok */
0387     if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
0388         return 0;
0389 
0390     iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
0391                    calib_complete, ARRAY_SIZE(calib_complete),
0392                    iwlagn_wait_calib, priv);
0393 
0394     /* Will also start the device */
0395     ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
0396     if (ret)
0397         goto error;
0398 
0399     ret = iwl_init_alive_start(priv);
0400     if (ret)
0401         goto error;
0402 
0403     /*
0404      * Some things may run in the background now, but we
0405      * just wait for the calibration complete notification.
0406      */
0407     ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
0408                     UCODE_CALIB_TIMEOUT);
0409 
0410     goto out;
0411 
0412  error:
0413     iwl_remove_notification(&priv->notif_wait, &calib_wait);
0414  out:
0415     /* Whatever happened, stop the device */
0416     iwl_trans_stop_device(priv->trans);
0417     priv->ucode_loaded = false;
0418 
0419     return ret;
0420 }