mt76x02_usb_mcu.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/firmware.h>
  7. #include "mt76x02.h"
  8. #include "mt76x02_mcu.h"
  9. #include "mt76x02_usb.h"
  10. #define MT_CMD_HDR_LEN 4
  11. #define MT_FCE_DMA_ADDR 0x0230
  12. #define MT_FCE_DMA_LEN 0x0234
  13. #define MT_TX_CPU_FROM_FCE_CPU_DESC_IDX 0x09a8
  14. static void
  15. mt76x02u_multiple_mcu_reads(struct mt76_dev *dev, u8 *data, int len)
  16. {
  17. struct mt76_usb *usb = &dev->usb;
  18. u32 reg, val;
  19. int i;
  20. if (usb->mcu.burst) {
  21. WARN_ON_ONCE(len / 4 != usb->mcu.rp_len);
  22. reg = usb->mcu.rp[0].reg - usb->mcu.base;
  23. for (i = 0; i < usb->mcu.rp_len; i++) {
  24. val = get_unaligned_le32(data + 4 * i);
  25. usb->mcu.rp[i].reg = reg++;
  26. usb->mcu.rp[i].value = val;
  27. }
  28. } else {
  29. WARN_ON_ONCE(len / 8 != usb->mcu.rp_len);
  30. for (i = 0; i < usb->mcu.rp_len; i++) {
  31. reg = get_unaligned_le32(data + 8 * i) -
  32. usb->mcu.base;
  33. val = get_unaligned_le32(data + 8 * i + 4);
  34. WARN_ON_ONCE(usb->mcu.rp[i].reg != reg);
  35. usb->mcu.rp[i].value = val;
  36. }
  37. }
  38. }
  39. static int mt76x02u_mcu_wait_resp(struct mt76_dev *dev, u8 seq)
  40. {
  41. struct mt76_usb *usb = &dev->usb;
  42. u8 *data = usb->mcu.data;
  43. int i, len, ret;
  44. u32 rxfce;
  45. for (i = 0; i < 5; i++) {
  46. ret = mt76u_bulk_msg(dev, data, MCU_RESP_URB_SIZE, &len,
  47. 300, MT_EP_IN_CMD_RESP);
  48. if (ret == -ETIMEDOUT)
  49. continue;
  50. if (ret)
  51. goto out;
  52. if (usb->mcu.rp)
  53. mt76x02u_multiple_mcu_reads(dev, data + 4, len - 8);
  54. rxfce = get_unaligned_le32(data);
  55. if (seq == FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, rxfce) &&
  56. FIELD_GET(MT_RX_FCE_INFO_EVT_TYPE, rxfce) == EVT_CMD_DONE)
  57. return 0;
  58. dev_err(dev->dev, "error: MCU resp evt:%lx seq:%hhx-%lx\n",
  59. FIELD_GET(MT_RX_FCE_INFO_EVT_TYPE, rxfce),
  60. seq, FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, rxfce));
  61. }
  62. out:
  63. dev_err(dev->dev, "error: %s failed with %d\n", __func__, ret);
  64. return ret;
  65. }
  66. static int
  67. __mt76x02u_mcu_send_msg(struct mt76_dev *dev, struct sk_buff *skb,
  68. int cmd, bool wait_resp)
  69. {
  70. u8 seq = 0;
  71. u32 info;
  72. int ret;
  73. if (test_bit(MT76_REMOVED, &dev->phy.state)) {
  74. ret = 0;
  75. goto out;
  76. }
  77. if (wait_resp) {
  78. seq = ++dev->mcu.msg_seq & 0xf;
  79. if (!seq)
  80. seq = ++dev->mcu.msg_seq & 0xf;
  81. }
  82. info = FIELD_PREP(MT_MCU_MSG_CMD_SEQ, seq) |
  83. FIELD_PREP(MT_MCU_MSG_CMD_TYPE, cmd) |
  84. MT_MCU_MSG_TYPE_CMD;
  85. ret = mt76x02u_skb_dma_info(skb, CPU_TX_PORT, info);
  86. if (ret)
  87. return ret;
  88. ret = mt76u_bulk_msg(dev, skb->data, skb->len, NULL, 500,
  89. MT_EP_OUT_INBAND_CMD);
  90. if (ret)
  91. return ret;
  92. if (wait_resp)
  93. ret = mt76x02u_mcu_wait_resp(dev, seq);
  94. out:
  95. consume_skb(skb);
  96. return ret;
  97. }
  98. static int
  99. mt76x02u_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data,
  100. int len, bool wait_resp)
  101. {
  102. struct sk_buff *skb;
  103. int err;
  104. skb = mt76_mcu_msg_alloc(dev, data, len);
  105. if (!skb)
  106. return -ENOMEM;
  107. mutex_lock(&dev->mcu.mutex);
  108. err = __mt76x02u_mcu_send_msg(dev, skb, cmd, wait_resp);
  109. mutex_unlock(&dev->mcu.mutex);
  110. return err;
  111. }
  112. static inline void skb_put_le32(struct sk_buff *skb, u32 val)
  113. {
  114. put_unaligned_le32(val, skb_put(skb, 4));
  115. }
  116. static int
  117. mt76x02u_mcu_wr_rp(struct mt76_dev *dev, u32 base,
  118. const struct mt76_reg_pair *data, int n)
  119. {
  120. const int max_vals_per_cmd = MT_INBAND_PACKET_MAX_LEN / 8;
  121. const int CMD_RANDOM_WRITE = 12;
  122. struct sk_buff *skb;
  123. int cnt, i, ret;
  124. if (!n)
  125. return 0;
  126. cnt = min(max_vals_per_cmd, n);
  127. skb = alloc_skb(cnt * 8 + MT_DMA_HDR_LEN + 4, GFP_KERNEL);
  128. if (!skb)
  129. return -ENOMEM;
  130. skb_reserve(skb, MT_DMA_HDR_LEN);
  131. for (i = 0; i < cnt; i++) {
  132. skb_put_le32(skb, base + data[i].reg);
  133. skb_put_le32(skb, data[i].value);
  134. }
  135. mutex_lock(&dev->mcu.mutex);
  136. ret = __mt76x02u_mcu_send_msg(dev, skb, CMD_RANDOM_WRITE, cnt == n);
  137. mutex_unlock(&dev->mcu.mutex);
  138. if (ret)
  139. return ret;
  140. return mt76x02u_mcu_wr_rp(dev, base, data + cnt, n - cnt);
  141. }
  142. static int
  143. mt76x02u_mcu_rd_rp(struct mt76_dev *dev, u32 base,
  144. struct mt76_reg_pair *data, int n)
  145. {
  146. const int CMD_RANDOM_READ = 10;
  147. const int max_vals_per_cmd = MT_INBAND_PACKET_MAX_LEN / 8;
  148. struct mt76_usb *usb = &dev->usb;
  149. struct sk_buff *skb;
  150. int cnt, i, ret;
  151. if (!n)
  152. return 0;
  153. cnt = min(max_vals_per_cmd, n);
  154. if (cnt != n)
  155. return -EINVAL;
  156. skb = alloc_skb(cnt * 8 + MT_DMA_HDR_LEN + 4, GFP_KERNEL);
  157. if (!skb)
  158. return -ENOMEM;
  159. skb_reserve(skb, MT_DMA_HDR_LEN);
  160. for (i = 0; i < cnt; i++) {
  161. skb_put_le32(skb, base + data[i].reg);
  162. skb_put_le32(skb, data[i].value);
  163. }
  164. mutex_lock(&dev->mcu.mutex);
  165. usb->mcu.rp = data;
  166. usb->mcu.rp_len = n;
  167. usb->mcu.base = base;
  168. usb->mcu.burst = false;
  169. ret = __mt76x02u_mcu_send_msg(dev, skb, CMD_RANDOM_READ, true);
  170. usb->mcu.rp = NULL;
  171. mutex_unlock(&dev->mcu.mutex);
  172. return ret;
  173. }
  174. void mt76x02u_mcu_fw_reset(struct mt76x02_dev *dev)
  175. {
  176. mt76u_vendor_request(&dev->mt76, MT_VEND_DEV_MODE,
  177. USB_DIR_OUT | USB_TYPE_VENDOR,
  178. 0x1, 0, NULL, 0);
  179. }
  180. EXPORT_SYMBOL_GPL(mt76x02u_mcu_fw_reset);
  181. static int
  182. __mt76x02u_mcu_fw_send_data(struct mt76x02_dev *dev, u8 *data,
  183. const void *fw_data, int len, u32 dst_addr)
  184. {
  185. __le32 info;
  186. u32 val;
  187. int err, data_len;
  188. info = cpu_to_le32(FIELD_PREP(MT_MCU_MSG_PORT, CPU_TX_PORT) |
  189. FIELD_PREP(MT_MCU_MSG_LEN, len) |
  190. MT_MCU_MSG_TYPE_CMD);
  191. memcpy(data, &info, sizeof(info));
  192. memcpy(data + sizeof(info), fw_data, len);
  193. memset(data + sizeof(info) + len, 0, 4);
  194. mt76u_single_wr(&dev->mt76, MT_VEND_WRITE_FCE,
  195. MT_FCE_DMA_ADDR, dst_addr);
  196. len = roundup(len, 4);
  197. mt76u_single_wr(&dev->mt76, MT_VEND_WRITE_FCE,
  198. MT_FCE_DMA_LEN, len << 16);
  199. data_len = MT_CMD_HDR_LEN + len + sizeof(info);
  200. err = mt76u_bulk_msg(&dev->mt76, data, data_len, NULL, 1000,
  201. MT_EP_OUT_INBAND_CMD);
  202. if (err) {
  203. dev_err(dev->mt76.dev, "firmware upload failed: %d\n", err);
  204. return err;
  205. }
  206. val = mt76_rr(dev, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX);
  207. val++;
  208. mt76_wr(dev, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX, val);
  209. return 0;
  210. }
  211. int mt76x02u_mcu_fw_send_data(struct mt76x02_dev *dev, const void *data,
  212. int data_len, u32 max_payload, u32 offset)
  213. {
  214. int len, err = 0, pos = 0, max_len = max_payload - 8;
  215. u8 *buf;
  216. buf = kmalloc(max_payload, GFP_KERNEL);
  217. if (!buf)
  218. return -ENOMEM;
  219. while (data_len > 0) {
  220. len = min_t(int, data_len, max_len);
  221. err = __mt76x02u_mcu_fw_send_data(dev, buf, data + pos,
  222. len, offset + pos);
  223. if (err < 0)
  224. break;
  225. data_len -= len;
  226. pos += len;
  227. usleep_range(5000, 10000);
  228. }
  229. kfree(buf);
  230. return err;
  231. }
  232. EXPORT_SYMBOL_GPL(mt76x02u_mcu_fw_send_data);
  233. void mt76x02u_init_mcu(struct mt76_dev *dev)
  234. {
  235. static const struct mt76_mcu_ops mt76x02u_mcu_ops = {
  236. .headroom = MT_CMD_HDR_LEN,
  237. .tailroom = 8,
  238. .mcu_send_msg = mt76x02u_mcu_send_msg,
  239. .mcu_parse_response = mt76x02_mcu_parse_response,
  240. .mcu_wr_rp = mt76x02u_mcu_wr_rp,
  241. .mcu_rd_rp = mt76x02u_mcu_rd_rp,
  242. };
  243. dev->mcu_ops = &mt76x02u_mcu_ops;
  244. }
  245. EXPORT_SYMBOL_GPL(mt76x02u_init_mcu);
  246. MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
  247. MODULE_LICENSE("Dual BSD/GPL");