usb_mcu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/firmware.h>
  17. #include "mt76.h"
  18. #include "dma.h"
  19. #define MT_CMD_HDR_LEN 4
  20. #define MT_FCE_DMA_ADDR 0x0230
  21. #define MT_FCE_DMA_LEN 0x0234
  22. #define MT_TX_CPU_FROM_FCE_CPU_DESC_IDX 0x09a8
  23. struct sk_buff *mt76u_mcu_msg_alloc(const void *data, int len)
  24. {
  25. struct sk_buff *skb;
  26. skb = alloc_skb(MT_CMD_HDR_LEN + len + 8, GFP_KERNEL);
  27. if (!skb)
  28. return NULL;
  29. skb_reserve(skb, MT_CMD_HDR_LEN);
  30. skb_put_data(skb, data, len);
  31. return skb;
  32. }
  33. EXPORT_SYMBOL_GPL(mt76u_mcu_msg_alloc);
  34. void mt76u_mcu_complete_urb(struct urb *urb)
  35. {
  36. struct completion *cmpl = urb->context;
  37. complete(cmpl);
  38. }
  39. EXPORT_SYMBOL_GPL(mt76u_mcu_complete_urb);
  40. static int mt76u_mcu_wait_resp(struct mt76_dev *dev, u8 seq)
  41. {
  42. struct mt76_usb *usb = &dev->usb;
  43. struct mt76u_buf *buf = &usb->mcu.res;
  44. int i, ret;
  45. u32 rxfce;
  46. for (i = 0; i < 5; i++) {
  47. if (!wait_for_completion_timeout(&usb->mcu.cmpl,
  48. msecs_to_jiffies(300)))
  49. continue;
  50. if (buf->urb->status)
  51. return -EIO;
  52. rxfce = get_unaligned_le32(sg_virt(&buf->urb->sg[0]));
  53. ret = mt76u_submit_buf(dev, USB_DIR_IN,
  54. MT_EP_IN_CMD_RESP,
  55. buf, GFP_KERNEL,
  56. mt76u_mcu_complete_urb,
  57. &usb->mcu.cmpl);
  58. if (ret)
  59. return ret;
  60. if (seq == FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, rxfce))
  61. return 0;
  62. dev_err(dev->dev, "error: MCU resp evt:%lx seq:%hhx-%lx\n",
  63. FIELD_GET(MT_RX_FCE_INFO_EVT_TYPE, rxfce),
  64. seq, FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ, rxfce));
  65. }
  66. dev_err(dev->dev, "error: %s timed out\n", __func__);
  67. return -ETIMEDOUT;
  68. }
  69. int mt76u_mcu_send_msg(struct mt76_dev *dev, struct sk_buff *skb,
  70. int cmd, bool wait_resp)
  71. {
  72. struct usb_interface *intf = to_usb_interface(dev->dev);
  73. struct usb_device *udev = interface_to_usbdev(intf);
  74. struct mt76_usb *usb = &dev->usb;
  75. unsigned int pipe;
  76. int ret, sent;
  77. u8 seq = 0;
  78. u32 info;
  79. if (test_bit(MT76_REMOVED, &dev->state))
  80. return 0;
  81. mutex_lock(&usb->mcu.mutex);
  82. pipe = usb_sndbulkpipe(udev, usb->out_ep[MT_EP_OUT_INBAND_CMD]);
  83. if (wait_resp) {
  84. seq = ++usb->mcu.msg_seq & 0xf;
  85. if (!seq)
  86. seq = ++usb->mcu.msg_seq & 0xf;
  87. }
  88. info = FIELD_PREP(MT_MCU_MSG_CMD_SEQ, seq) |
  89. FIELD_PREP(MT_MCU_MSG_CMD_TYPE, cmd) |
  90. MT_MCU_MSG_TYPE_CMD;
  91. ret = mt76u_skb_dma_info(skb, CPU_TX_PORT, info);
  92. if (ret)
  93. goto out;
  94. ret = usb_bulk_msg(udev, pipe, skb->data, skb->len, &sent, 500);
  95. if (ret)
  96. goto out;
  97. if (wait_resp)
  98. ret = mt76u_mcu_wait_resp(dev, seq);
  99. out:
  100. mutex_unlock(&usb->mcu.mutex);
  101. consume_skb(skb);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL_GPL(mt76u_mcu_send_msg);
  105. void mt76u_mcu_fw_reset(struct mt76_dev *dev)
  106. {
  107. mt76u_vendor_request(dev, MT_VEND_DEV_MODE,
  108. USB_DIR_OUT | USB_TYPE_VENDOR,
  109. 0x1, 0, NULL, 0);
  110. }
  111. EXPORT_SYMBOL_GPL(mt76u_mcu_fw_reset);
  112. static int
  113. __mt76u_mcu_fw_send_data(struct mt76_dev *dev, struct mt76u_buf *buf,
  114. const void *fw_data, int len, u32 dst_addr)
  115. {
  116. u8 *data = sg_virt(&buf->urb->sg[0]);
  117. DECLARE_COMPLETION_ONSTACK(cmpl);
  118. __le32 info;
  119. u32 val;
  120. int err;
  121. info = cpu_to_le32(FIELD_PREP(MT_MCU_MSG_PORT, CPU_TX_PORT) |
  122. FIELD_PREP(MT_MCU_MSG_LEN, len) |
  123. MT_MCU_MSG_TYPE_CMD);
  124. memcpy(data, &info, sizeof(info));
  125. memcpy(data + sizeof(info), fw_data, len);
  126. memset(data + sizeof(info) + len, 0, 4);
  127. mt76u_single_wr(dev, MT_VEND_WRITE_FCE,
  128. MT_FCE_DMA_ADDR, dst_addr);
  129. len = roundup(len, 4);
  130. mt76u_single_wr(dev, MT_VEND_WRITE_FCE,
  131. MT_FCE_DMA_LEN, len << 16);
  132. buf->len = MT_CMD_HDR_LEN + len + sizeof(info);
  133. err = mt76u_submit_buf(dev, USB_DIR_OUT,
  134. MT_EP_OUT_INBAND_CMD,
  135. buf, GFP_KERNEL,
  136. mt76u_mcu_complete_urb, &cmpl);
  137. if (err < 0)
  138. return err;
  139. if (!wait_for_completion_timeout(&cmpl,
  140. msecs_to_jiffies(1000))) {
  141. dev_err(dev->dev, "firmware upload timed out\n");
  142. usb_kill_urb(buf->urb);
  143. return -ETIMEDOUT;
  144. }
  145. if (mt76u_urb_error(buf->urb)) {
  146. dev_err(dev->dev, "firmware upload failed: %d\n",
  147. buf->urb->status);
  148. return buf->urb->status;
  149. }
  150. val = mt76u_rr(dev, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX);
  151. val++;
  152. mt76u_wr(dev, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX, val);
  153. return 0;
  154. }
  155. int mt76u_mcu_fw_send_data(struct mt76_dev *dev, const void *data,
  156. int data_len, u32 max_payload, u32 offset)
  157. {
  158. int err, len, pos = 0, max_len = max_payload - 8;
  159. struct mt76u_buf buf;
  160. err = mt76u_buf_alloc(dev, &buf, 1, max_payload, max_payload,
  161. GFP_KERNEL);
  162. if (err < 0)
  163. return err;
  164. while (data_len > 0) {
  165. len = min_t(int, data_len, max_len);
  166. err = __mt76u_mcu_fw_send_data(dev, &buf, data + pos,
  167. len, offset + pos);
  168. if (err < 0)
  169. break;
  170. data_len -= len;
  171. pos += len;
  172. usleep_range(5000, 10000);
  173. }
  174. mt76u_buf_free(&buf);
  175. return err;
  176. }
  177. EXPORT_SYMBOL_GPL(mt76u_mcu_fw_send_data);
  178. int mt76u_mcu_init_rx(struct mt76_dev *dev)
  179. {
  180. struct mt76_usb *usb = &dev->usb;
  181. int err;
  182. err = mt76u_buf_alloc(dev, &usb->mcu.res, 1,
  183. MCU_RESP_URB_SIZE, MCU_RESP_URB_SIZE,
  184. GFP_KERNEL);
  185. if (err < 0)
  186. return err;
  187. err = mt76u_submit_buf(dev, USB_DIR_IN, MT_EP_IN_CMD_RESP,
  188. &usb->mcu.res, GFP_KERNEL,
  189. mt76u_mcu_complete_urb,
  190. &usb->mcu.cmpl);
  191. if (err < 0)
  192. mt76u_buf_free(&usb->mcu.res);
  193. return err;
  194. }
  195. EXPORT_SYMBOL_GPL(mt76u_mcu_init_rx);