mt76x02_mcu.c 3.9 KB

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