Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /*
0003  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
0004  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/firmware.h>
0009 #include <linux/delay.h>
0010 
0011 #include "mt76x02_mcu.h"
0012 
0013 int mt76x02_mcu_parse_response(struct mt76_dev *mdev, int cmd,
0014                    struct sk_buff *skb, int seq)
0015 {
0016     struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
0017     u32 *rxfce;
0018 
0019     if (!skb) {
0020         dev_err(mdev->dev, "MCU message %02x (seq %d) timed out\n",
0021             abs(cmd), seq);
0022         dev->mcu_timeout = 1;
0023         return -ETIMEDOUT;
0024     }
0025 
0026     rxfce = (u32 *)skb->cb;
0027     if (seq != FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, *rxfce))
0028         return -EAGAIN;
0029 
0030     return 0;
0031 }
0032 EXPORT_SYMBOL_GPL(mt76x02_mcu_parse_response);
0033 
0034 int mt76x02_mcu_msg_send(struct mt76_dev *mdev, int cmd, const void *data,
0035              int len, bool wait_resp)
0036 {
0037     struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
0038     unsigned long expires = jiffies + HZ;
0039     struct sk_buff *skb;
0040     u32 tx_info;
0041     int ret;
0042     u8 seq;
0043 
0044     if (dev->mcu_timeout)
0045         return -EIO;
0046 
0047     skb = mt76_mcu_msg_alloc(mdev, data, len);
0048     if (!skb)
0049         return -ENOMEM;
0050 
0051     mutex_lock(&mdev->mcu.mutex);
0052 
0053     seq = ++mdev->mcu.msg_seq & 0xf;
0054     if (!seq)
0055         seq = ++mdev->mcu.msg_seq & 0xf;
0056 
0057     tx_info = MT_MCU_MSG_TYPE_CMD |
0058           FIELD_PREP(MT_MCU_MSG_CMD_TYPE, cmd) |
0059           FIELD_PREP(MT_MCU_MSG_CMD_SEQ, seq) |
0060           FIELD_PREP(MT_MCU_MSG_PORT, CPU_TX_PORT) |
0061           FIELD_PREP(MT_MCU_MSG_LEN, skb->len);
0062 
0063     ret = mt76_tx_queue_skb_raw(dev, mdev->q_mcu[MT_MCUQ_WM], skb, tx_info);
0064     if (ret)
0065         goto out;
0066 
0067     while (wait_resp) {
0068         skb = mt76_mcu_get_response(&dev->mt76, expires);
0069         ret = mt76x02_mcu_parse_response(mdev, cmd, skb, seq);
0070         dev_kfree_skb(skb);
0071         if (ret != -EAGAIN)
0072             break;
0073     }
0074 
0075 out:
0076     mutex_unlock(&mdev->mcu.mutex);
0077 
0078     return ret;
0079 }
0080 EXPORT_SYMBOL_GPL(mt76x02_mcu_msg_send);
0081 
0082 int mt76x02_mcu_function_select(struct mt76x02_dev *dev, enum mcu_function func,
0083                 u32 val)
0084 {
0085     struct {
0086         __le32 id;
0087         __le32 value;
0088     } __packed __aligned(4) msg = {
0089         .id = cpu_to_le32(func),
0090         .value = cpu_to_le32(val),
0091     };
0092     bool wait = false;
0093 
0094     if (func != Q_SELECT)
0095         wait = true;
0096 
0097     return mt76_mcu_send_msg(&dev->mt76, CMD_FUN_SET_OP, &msg,
0098                  sizeof(msg), wait);
0099 }
0100 EXPORT_SYMBOL_GPL(mt76x02_mcu_function_select);
0101 
0102 int mt76x02_mcu_set_radio_state(struct mt76x02_dev *dev, bool on)
0103 {
0104     struct {
0105         __le32 mode;
0106         __le32 level;
0107     } __packed __aligned(4) msg = {
0108         .mode = cpu_to_le32(on ? RADIO_ON : RADIO_OFF),
0109         .level = cpu_to_le32(0),
0110     };
0111 
0112     return mt76_mcu_send_msg(&dev->mt76, CMD_POWER_SAVING_OP, &msg,
0113                  sizeof(msg), false);
0114 }
0115 EXPORT_SYMBOL_GPL(mt76x02_mcu_set_radio_state);
0116 
0117 int mt76x02_mcu_calibrate(struct mt76x02_dev *dev, int type, u32 param)
0118 {
0119     struct {
0120         __le32 id;
0121         __le32 value;
0122     } __packed __aligned(4) msg = {
0123         .id = cpu_to_le32(type),
0124         .value = cpu_to_le32(param),
0125     };
0126     bool is_mt76x2e = mt76_is_mmio(&dev->mt76) && is_mt76x2(dev);
0127     int ret;
0128 
0129     if (is_mt76x2e)
0130         mt76_rmw(dev, MT_MCU_COM_REG0, BIT(31), 0);
0131 
0132     ret = mt76_mcu_send_msg(&dev->mt76, CMD_CALIBRATION_OP, &msg,
0133                 sizeof(msg), true);
0134     if (ret)
0135         return ret;
0136 
0137     if (is_mt76x2e &&
0138         WARN_ON(!mt76_poll_msec(dev, MT_MCU_COM_REG0,
0139                     BIT(31), BIT(31), 100)))
0140         return -ETIMEDOUT;
0141 
0142     return 0;
0143 }
0144 EXPORT_SYMBOL_GPL(mt76x02_mcu_calibrate);
0145 
0146 int mt76x02_mcu_cleanup(struct mt76x02_dev *dev)
0147 {
0148     struct sk_buff *skb;
0149 
0150     mt76_wr(dev, MT_MCU_INT_LEVEL, 1);
0151     usleep_range(20000, 30000);
0152 
0153     while ((skb = skb_dequeue(&dev->mt76.mcu.res_q)) != NULL)
0154         dev_kfree_skb(skb);
0155 
0156     return 0;
0157 }
0158 EXPORT_SYMBOL_GPL(mt76x02_mcu_cleanup);
0159 
0160 void mt76x02_set_ethtool_fwver(struct mt76x02_dev *dev,
0161                    const struct mt76x02_fw_header *h)
0162 {
0163     u16 bld = le16_to_cpu(h->build_ver);
0164     u16 ver = le16_to_cpu(h->fw_ver);
0165 
0166     snprintf(dev->mt76.hw->wiphy->fw_version,
0167          sizeof(dev->mt76.hw->wiphy->fw_version),
0168          "%d.%d.%02d-b%x",
0169          (ver >> 12) & 0xf, (ver >> 8) & 0xf, ver & 0xf, bld);
0170 }
0171 EXPORT_SYMBOL_GPL(mt76x02_set_ethtool_fwver);