2019-07-24 16:58:20 +08:00
|
|
|
// SPDX-License-Identifier: ISC
|
2018-09-10 05:58:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
|
|
|
|
* Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/firmware.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
|
|
|
|
#include "mt76x02_mcu.h"
|
|
|
|
|
2018-10-26 20:00:26 +08:00
|
|
|
int mt76x02_mcu_msg_send(struct mt76_dev *mdev, int cmd, const void *data,
|
|
|
|
int len, bool wait_resp)
|
2018-09-10 05:58:02 +08:00
|
|
|
{
|
2018-10-07 17:45:18 +08:00
|
|
|
struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
|
2018-09-10 05:58:02 +08:00
|
|
|
unsigned long expires = jiffies + HZ;
|
2018-10-26 20:00:26 +08:00
|
|
|
struct sk_buff *skb;
|
2019-02-07 18:10:57 +08:00
|
|
|
u32 tx_info;
|
2018-09-10 05:58:02 +08:00
|
|
|
int ret;
|
|
|
|
u8 seq;
|
|
|
|
|
2018-10-26 20:00:26 +08:00
|
|
|
skb = mt76x02_mcu_msg_alloc(data, len);
|
2018-09-10 05:58:02 +08:00
|
|
|
if (!skb)
|
2018-10-26 20:00:26 +08:00
|
|
|
return -ENOMEM;
|
2018-09-10 05:58:02 +08:00
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
mutex_lock(&mdev->mmio.mcu.mutex);
|
2018-09-10 05:58:02 +08:00
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
seq = ++mdev->mmio.mcu.msg_seq & 0xf;
|
2018-09-10 05:58:02 +08:00
|
|
|
if (!seq)
|
2018-10-07 17:45:18 +08:00
|
|
|
seq = ++mdev->mmio.mcu.msg_seq & 0xf;
|
2018-09-10 05:58:02 +08:00
|
|
|
|
2019-02-07 18:10:57 +08:00
|
|
|
tx_info = MT_MCU_MSG_TYPE_CMD |
|
|
|
|
FIELD_PREP(MT_MCU_MSG_CMD_TYPE, cmd) |
|
|
|
|
FIELD_PREP(MT_MCU_MSG_CMD_SEQ, seq) |
|
|
|
|
FIELD_PREP(MT_MCU_MSG_PORT, CPU_TX_PORT) |
|
|
|
|
FIELD_PREP(MT_MCU_MSG_LEN, skb->len);
|
|
|
|
|
|
|
|
ret = mt76_tx_queue_skb_raw(dev, MT_TXQ_MCU, skb, tx_info);
|
2018-09-10 05:58:02 +08:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
while (wait_resp) {
|
|
|
|
u32 *rxfce;
|
|
|
|
bool check_seq = false;
|
|
|
|
|
2019-02-05 00:51:28 +08:00
|
|
|
skb = mt76_mcu_get_response(&dev->mt76, expires);
|
2018-09-10 05:58:02 +08:00
|
|
|
if (!skb) {
|
2018-10-07 17:45:18 +08:00
|
|
|
dev_err(mdev->dev,
|
2018-09-10 05:58:02 +08:00
|
|
|
"MCU message %d (seq %d) timed out\n", cmd,
|
|
|
|
seq);
|
|
|
|
ret = -ETIMEDOUT;
|
2019-02-25 06:16:45 +08:00
|
|
|
dev->mcu_timeout = 1;
|
2018-09-10 05:58:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-24 16:58:18 +08:00
|
|
|
rxfce = (u32 *)skb->cb;
|
2018-09-10 05:58:02 +08:00
|
|
|
|
|
|
|
if (seq == FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, *rxfce))
|
|
|
|
check_seq = true;
|
|
|
|
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
if (check_seq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2018-10-07 17:45:18 +08:00
|
|
|
mutex_unlock(&mdev->mmio.mcu.mutex);
|
2018-09-10 05:58:02 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_mcu_msg_send);
|
|
|
|
|
2018-10-26 20:00:28 +08:00
|
|
|
int mt76x02_mcu_function_select(struct mt76x02_dev *dev, enum mcu_function func,
|
|
|
|
u32 val)
|
2018-09-10 05:58:02 +08:00
|
|
|
{
|
|
|
|
struct {
|
2019-07-24 16:58:18 +08:00
|
|
|
__le32 id;
|
|
|
|
__le32 value;
|
2018-09-10 05:58:02 +08:00
|
|
|
} __packed __aligned(4) msg = {
|
2019-07-24 16:58:18 +08:00
|
|
|
.id = cpu_to_le32(func),
|
|
|
|
.value = cpu_to_le32(val),
|
2018-09-10 05:58:02 +08:00
|
|
|
};
|
2018-10-26 20:00:28 +08:00
|
|
|
bool wait = false;
|
2018-09-10 05:58:02 +08:00
|
|
|
|
2018-10-26 20:00:28 +08:00
|
|
|
if (func != Q_SELECT)
|
|
|
|
wait = true;
|
|
|
|
|
|
|
|
return mt76_mcu_send_msg(dev, CMD_FUN_SET_OP, &msg, sizeof(msg), wait);
|
2018-09-10 05:58:02 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_mcu_function_select);
|
|
|
|
|
2018-10-26 20:00:29 +08:00
|
|
|
int mt76x02_mcu_set_radio_state(struct mt76x02_dev *dev, bool on)
|
2018-09-10 05:58:02 +08:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
__le32 mode;
|
|
|
|
__le32 level;
|
|
|
|
} __packed __aligned(4) msg = {
|
|
|
|
.mode = cpu_to_le32(on ? RADIO_ON : RADIO_OFF),
|
|
|
|
.level = cpu_to_le32(0),
|
|
|
|
};
|
|
|
|
|
2019-07-24 16:58:18 +08:00
|
|
|
return mt76_mcu_send_msg(dev, CMD_POWER_SAVING_OP, &msg, sizeof(msg),
|
|
|
|
false);
|
2018-09-10 05:58:02 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_mcu_set_radio_state);
|
|
|
|
|
2018-11-09 18:56:10 +08:00
|
|
|
int mt76x02_mcu_calibrate(struct mt76x02_dev *dev, int type, u32 param)
|
2018-09-10 05:58:04 +08:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
__le32 id;
|
|
|
|
__le32 value;
|
|
|
|
} __packed __aligned(4) msg = {
|
|
|
|
.id = cpu_to_le32(type),
|
|
|
|
.value = cpu_to_le32(param),
|
|
|
|
};
|
2019-10-29 21:34:44 +08:00
|
|
|
bool is_mt76x2e = mt76_is_mmio(&dev->mt76) && is_mt76x2(dev);
|
2018-09-10 05:58:04 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-11-09 18:56:10 +08:00
|
|
|
if (is_mt76x2e)
|
2018-10-07 17:45:18 +08:00
|
|
|
mt76_rmw(dev, MT_MCU_COM_REG0, BIT(31), 0);
|
2018-09-10 05:58:04 +08:00
|
|
|
|
2018-10-26 20:00:26 +08:00
|
|
|
ret = mt76_mcu_send_msg(dev, CMD_CALIBRATION_OP, &msg, sizeof(msg),
|
|
|
|
true);
|
2018-09-10 05:58:04 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-09 18:56:10 +08:00
|
|
|
if (is_mt76x2e &&
|
2018-10-07 17:45:18 +08:00
|
|
|
WARN_ON(!mt76_poll_msec(dev, MT_MCU_COM_REG0,
|
|
|
|
BIT(31), BIT(31), 100)))
|
2018-09-10 05:58:04 +08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_mcu_calibrate);
|
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
int mt76x02_mcu_cleanup(struct mt76x02_dev *dev)
|
2018-09-10 05:58:02 +08:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
mt76_wr(dev, MT_MCU_INT_LEVEL, 1);
|
2018-09-10 05:58:02 +08:00
|
|
|
usleep_range(20000, 30000);
|
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
while ((skb = skb_dequeue(&dev->mt76.mmio.mcu.res_q)) != NULL)
|
2018-09-10 05:58:02 +08:00
|
|
|
dev_kfree_skb(skb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_mcu_cleanup);
|
2018-09-20 20:30:56 +08:00
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
void mt76x02_set_ethtool_fwver(struct mt76x02_dev *dev,
|
2018-09-20 20:30:56 +08:00
|
|
|
const struct mt76x02_fw_header *h)
|
|
|
|
{
|
|
|
|
u16 bld = le16_to_cpu(h->build_ver);
|
|
|
|
u16 ver = le16_to_cpu(h->fw_ver);
|
|
|
|
|
2018-10-07 17:45:18 +08:00
|
|
|
snprintf(dev->mt76.hw->wiphy->fw_version,
|
|
|
|
sizeof(dev->mt76.hw->wiphy->fw_version),
|
2018-09-20 20:30:56 +08:00
|
|
|
"%d.%d.%02d-b%x",
|
|
|
|
(ver >> 12) & 0xf, (ver >> 8) & 0xf, ver & 0xf, bld);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mt76x02_set_ethtool_fwver);
|