mt76x02_mcu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/firmware.h>
  8. #include <linux/delay.h>
  9. #include "mt76x02_mcu.h"
  10. int mt76x02_mcu_parse_response(struct mt76_dev *mdev, int cmd,
  11. struct sk_buff *skb, int seq)
  12. {
  13. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  14. u32 *rxfce;
  15. if (!skb) {
  16. dev_err(mdev->dev,
  17. "MCU message %d (seq %d) timed out\n", cmd,
  18. seq);
  19. dev->mcu_timeout = 1;
  20. return -ETIMEDOUT;
  21. }
  22. rxfce = (u32 *)skb->cb;
  23. if (seq != FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, *rxfce))
  24. return -EAGAIN;
  25. return 0;
  26. }
  27. EXPORT_SYMBOL_GPL(mt76x02_mcu_parse_response);
  28. int mt76x02_mcu_msg_send(struct mt76_dev *mdev, int cmd, const void *data,
  29. int len, bool wait_resp)
  30. {
  31. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  32. unsigned long expires = jiffies + HZ;
  33. struct sk_buff *skb;
  34. u32 tx_info;
  35. int ret;
  36. u8 seq;
  37. if (dev->mcu_timeout)
  38. return -EIO;
  39. skb = mt76_mcu_msg_alloc(mdev, data, len);
  40. if (!skb)
  41. return -ENOMEM;
  42. mutex_lock(&mdev->mcu.mutex);
  43. seq = ++mdev->mcu.msg_seq & 0xf;
  44. if (!seq)
  45. seq = ++mdev->mcu.msg_seq & 0xf;
  46. tx_info = MT_MCU_MSG_TYPE_CMD |
  47. FIELD_PREP(MT_MCU_MSG_CMD_TYPE, cmd) |
  48. FIELD_PREP(MT_MCU_MSG_CMD_SEQ, seq) |
  49. FIELD_PREP(MT_MCU_MSG_PORT, CPU_TX_PORT) |
  50. FIELD_PREP(MT_MCU_MSG_LEN, skb->len);
  51. ret = mt76_tx_queue_skb_raw(dev, mdev->q_mcu[MT_MCUQ_WM], skb, tx_info);
  52. if (ret)
  53. goto out;
  54. while (wait_resp) {
  55. skb = mt76_mcu_get_response(&dev->mt76, expires);
  56. ret = mt76x02_mcu_parse_response(mdev, cmd, skb, seq);
  57. dev_kfree_skb(skb);
  58. if (ret != -EAGAIN)
  59. break;
  60. }
  61. out:
  62. mutex_unlock(&mdev->mcu.mutex);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL_GPL(mt76x02_mcu_msg_send);
  66. int mt76x02_mcu_function_select(struct mt76x02_dev *dev, enum mcu_function func,
  67. u32 val)
  68. {
  69. struct {
  70. __le32 id;
  71. __le32 value;
  72. } __packed __aligned(4) msg = {
  73. .id = cpu_to_le32(func),
  74. .value = cpu_to_le32(val),
  75. };
  76. bool wait = false;
  77. if (func != Q_SELECT)
  78. wait = true;
  79. return mt76_mcu_send_msg(&dev->mt76, CMD_FUN_SET_OP, &msg,
  80. sizeof(msg), wait);
  81. }
  82. EXPORT_SYMBOL_GPL(mt76x02_mcu_function_select);
  83. int mt76x02_mcu_set_radio_state(struct mt76x02_dev *dev, bool on)
  84. {
  85. struct {
  86. __le32 mode;
  87. __le32 level;
  88. } __packed __aligned(4) msg = {
  89. .mode = cpu_to_le32(on ? RADIO_ON : RADIO_OFF),
  90. .level = cpu_to_le32(0),
  91. };
  92. return mt76_mcu_send_msg(&dev->mt76, CMD_POWER_SAVING_OP, &msg,
  93. sizeof(msg), false);
  94. }
  95. EXPORT_SYMBOL_GPL(mt76x02_mcu_set_radio_state);
  96. int mt76x02_mcu_calibrate(struct mt76x02_dev *dev, int type, u32 param)
  97. {
  98. struct {
  99. __le32 id;
  100. __le32 value;
  101. } __packed __aligned(4) msg = {
  102. .id = cpu_to_le32(type),
  103. .value = cpu_to_le32(param),
  104. };
  105. bool is_mt76x2e = mt76_is_mmio(&dev->mt76) && is_mt76x2(dev);
  106. int ret;
  107. if (is_mt76x2e)
  108. mt76_rmw(dev, MT_MCU_COM_REG0, BIT(31), 0);
  109. ret = mt76_mcu_send_msg(&dev->mt76, CMD_CALIBRATION_OP, &msg,
  110. sizeof(msg), true);
  111. if (ret)
  112. return ret;
  113. if (is_mt76x2e &&
  114. WARN_ON(!mt76_poll_msec(dev, MT_MCU_COM_REG0,
  115. BIT(31), BIT(31), 100)))
  116. return -ETIMEDOUT;
  117. return 0;
  118. }
  119. EXPORT_SYMBOL_GPL(mt76x02_mcu_calibrate);
  120. int mt76x02_mcu_cleanup(struct mt76x02_dev *dev)
  121. {
  122. struct sk_buff *skb;
  123. mt76_wr(dev, MT_MCU_INT_LEVEL, 1);
  124. usleep_range(20000, 30000);
  125. while ((skb = skb_dequeue(&dev->mt76.mcu.res_q)) != NULL)
  126. dev_kfree_skb(skb);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(mt76x02_mcu_cleanup);
  130. void mt76x02_set_ethtool_fwver(struct mt76x02_dev *dev,
  131. const struct mt76x02_fw_header *h)
  132. {
  133. u16 bld = le16_to_cpu(h->build_ver);
  134. u16 ver = le16_to_cpu(h->fw_ver);
  135. snprintf(dev->mt76.hw->wiphy->fw_version,
  136. sizeof(dev->mt76.hw->wiphy->fw_version),
  137. "%d.%d.%02d-b%x",
  138. (ver >> 12) & 0xf, (ver >> 8) & 0xf, ver & 0xf, bld);
  139. }
  140. EXPORT_SYMBOL_GPL(mt76x02_set_ethtool_fwver);