2 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
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.
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.
17 #include <linux/module.h>
18 #include <linux/firmware.h>
21 #include "mt76x02_mcu.h"
22 #include "mt76x02_usb.h"
24 #define MT_CMD_HDR_LEN 4
26 #define MT_FCE_DMA_ADDR 0x0230
27 #define MT_FCE_DMA_LEN 0x0234
29 #define MT_TX_CPU_FROM_FCE_CPU_DESC_IDX 0x09a8
32 mt76x02u_multiple_mcu_reads(struct mt76_dev
*dev
, u8
*data
, int len
)
34 struct mt76_usb
*usb
= &dev
->usb
;
39 WARN_ON_ONCE(len
/ 4 != usb
->mcu
.rp_len
);
41 reg
= usb
->mcu
.rp
[0].reg
- usb
->mcu
.base
;
42 for (i
= 0; i
< usb
->mcu
.rp_len
; i
++) {
43 val
= get_unaligned_le32(data
+ 4 * i
);
44 usb
->mcu
.rp
[i
].reg
= reg
++;
45 usb
->mcu
.rp
[i
].value
= val
;
48 WARN_ON_ONCE(len
/ 8 != usb
->mcu
.rp_len
);
50 for (i
= 0; i
< usb
->mcu
.rp_len
; i
++) {
51 reg
= get_unaligned_le32(data
+ 8 * i
) -
53 val
= get_unaligned_le32(data
+ 8 * i
+ 4);
55 WARN_ON_ONCE(usb
->mcu
.rp
[i
].reg
!= reg
);
56 usb
->mcu
.rp
[i
].value
= val
;
61 static int mt76x02u_mcu_wait_resp(struct mt76_dev
*dev
, u8 seq
)
63 struct mt76_usb
*usb
= &dev
->usb
;
64 u8
*data
= usb
->mcu
.data
;
68 for (i
= 0; i
< 5; i
++) {
69 ret
= mt76u_bulk_msg(dev
, data
, MCU_RESP_URB_SIZE
, &len
, 300);
70 if (ret
== -ETIMEDOUT
)
76 mt76x02u_multiple_mcu_reads(dev
, data
+ 4, len
- 8);
78 rxfce
= get_unaligned_le32(data
);
79 if (seq
== FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ
, rxfce
) &&
80 FIELD_GET(MT_RX_FCE_INFO_EVT_TYPE
, rxfce
) == EVT_CMD_DONE
)
83 dev_err(dev
->dev
, "error: MCU resp evt:%lx seq:%hhx-%lx\n",
84 FIELD_GET(MT_RX_FCE_INFO_EVT_TYPE
, rxfce
),
85 seq
, FIELD_GET(MT_RX_FCE_INFO_CMD_SEQ
, rxfce
));
88 dev_err(dev
->dev
, "error: %s failed with %d\n", __func__
, ret
);
93 __mt76x02u_mcu_send_msg(struct mt76_dev
*dev
, struct sk_buff
*skb
,
94 int cmd
, bool wait_resp
)
96 struct mt76_usb
*usb
= &dev
->usb
;
101 if (test_bit(MT76_REMOVED
, &dev
->state
))
105 seq
= ++usb
->mcu
.msg_seq
& 0xf;
107 seq
= ++usb
->mcu
.msg_seq
& 0xf;
110 info
= FIELD_PREP(MT_MCU_MSG_CMD_SEQ
, seq
) |
111 FIELD_PREP(MT_MCU_MSG_CMD_TYPE
, cmd
) |
113 ret
= mt76x02u_skb_dma_info(skb
, CPU_TX_PORT
, info
);
117 ret
= mt76u_bulk_msg(dev
, skb
->data
, skb
->len
, NULL
, 500);
122 ret
= mt76x02u_mcu_wait_resp(dev
, seq
);
130 mt76x02u_mcu_send_msg(struct mt76_dev
*dev
, int cmd
, const void *data
,
131 int len
, bool wait_resp
)
133 struct mt76_usb
*usb
= &dev
->usb
;
137 skb
= mt76_mcu_msg_alloc(data
, MT_CMD_HDR_LEN
, len
, 8);
141 mutex_lock(&usb
->mcu
.mutex
);
142 err
= __mt76x02u_mcu_send_msg(dev
, skb
, cmd
, wait_resp
);
143 mutex_unlock(&usb
->mcu
.mutex
);
148 static inline void skb_put_le32(struct sk_buff
*skb
, u32 val
)
150 put_unaligned_le32(val
, skb_put(skb
, 4));
154 mt76x02u_mcu_wr_rp(struct mt76_dev
*dev
, u32 base
,
155 const struct mt76_reg_pair
*data
, int n
)
157 const int CMD_RANDOM_WRITE
= 12;
158 const int max_vals_per_cmd
= MT_INBAND_PACKET_MAX_LEN
/ 8;
159 struct mt76_usb
*usb
= &dev
->usb
;
166 cnt
= min(max_vals_per_cmd
, n
);
168 skb
= alloc_skb(cnt
* 8 + MT_DMA_HDR_LEN
+ 4, GFP_KERNEL
);
171 skb_reserve(skb
, MT_DMA_HDR_LEN
);
173 for (i
= 0; i
< cnt
; i
++) {
174 skb_put_le32(skb
, base
+ data
[i
].reg
);
175 skb_put_le32(skb
, data
[i
].value
);
178 mutex_lock(&usb
->mcu
.mutex
);
179 ret
= __mt76x02u_mcu_send_msg(dev
, skb
, CMD_RANDOM_WRITE
, cnt
== n
);
180 mutex_unlock(&usb
->mcu
.mutex
);
184 return mt76x02u_mcu_wr_rp(dev
, base
, data
+ cnt
, n
- cnt
);
188 mt76x02u_mcu_rd_rp(struct mt76_dev
*dev
, u32 base
,
189 struct mt76_reg_pair
*data
, int n
)
191 const int CMD_RANDOM_READ
= 10;
192 const int max_vals_per_cmd
= MT_INBAND_PACKET_MAX_LEN
/ 8;
193 struct mt76_usb
*usb
= &dev
->usb
;
200 cnt
= min(max_vals_per_cmd
, n
);
204 skb
= alloc_skb(cnt
* 8 + MT_DMA_HDR_LEN
+ 4, GFP_KERNEL
);
207 skb_reserve(skb
, MT_DMA_HDR_LEN
);
209 for (i
= 0; i
< cnt
; i
++) {
210 skb_put_le32(skb
, base
+ data
[i
].reg
);
211 skb_put_le32(skb
, data
[i
].value
);
214 mutex_lock(&usb
->mcu
.mutex
);
218 usb
->mcu
.base
= base
;
219 usb
->mcu
.burst
= false;
221 ret
= __mt76x02u_mcu_send_msg(dev
, skb
, CMD_RANDOM_READ
, true);
225 mutex_unlock(&usb
->mcu
.mutex
);
230 void mt76x02u_mcu_fw_reset(struct mt76x02_dev
*dev
)
232 mt76u_vendor_request(&dev
->mt76
, MT_VEND_DEV_MODE
,
233 USB_DIR_OUT
| USB_TYPE_VENDOR
,
236 EXPORT_SYMBOL_GPL(mt76x02u_mcu_fw_reset
);
239 __mt76x02u_mcu_fw_send_data(struct mt76x02_dev
*dev
, u8
*data
,
240 const void *fw_data
, int len
, u32 dst_addr
)
246 info
= cpu_to_le32(FIELD_PREP(MT_MCU_MSG_PORT
, CPU_TX_PORT
) |
247 FIELD_PREP(MT_MCU_MSG_LEN
, len
) |
248 MT_MCU_MSG_TYPE_CMD
);
250 memcpy(data
, &info
, sizeof(info
));
251 memcpy(data
+ sizeof(info
), fw_data
, len
);
252 memset(data
+ sizeof(info
) + len
, 0, 4);
254 mt76u_single_wr(&dev
->mt76
, MT_VEND_WRITE_FCE
,
255 MT_FCE_DMA_ADDR
, dst_addr
);
256 len
= roundup(len
, 4);
257 mt76u_single_wr(&dev
->mt76
, MT_VEND_WRITE_FCE
,
258 MT_FCE_DMA_LEN
, len
<< 16);
260 data_len
= MT_CMD_HDR_LEN
+ len
+ sizeof(info
);
262 err
= mt76u_bulk_msg(&dev
->mt76
, data
, data_len
, NULL
, 1000);
264 dev_err(dev
->mt76
.dev
, "firmware upload failed: %d\n", err
);
268 val
= mt76_rr(dev
, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX
);
270 mt76_wr(dev
, MT_TX_CPU_FROM_FCE_CPU_DESC_IDX
, val
);
275 int mt76x02u_mcu_fw_send_data(struct mt76x02_dev
*dev
, const void *data
,
276 int data_len
, u32 max_payload
, u32 offset
)
278 int len
, err
= 0, pos
= 0, max_len
= max_payload
- 8;
281 buf
= kmalloc(max_payload
, GFP_KERNEL
);
285 while (data_len
> 0) {
286 len
= min_t(int, data_len
, max_len
);
287 err
= __mt76x02u_mcu_fw_send_data(dev
, buf
, data
+ pos
,
294 usleep_range(5000, 10000);
300 EXPORT_SYMBOL_GPL(mt76x02u_mcu_fw_send_data
);
302 void mt76x02u_init_mcu(struct mt76_dev
*dev
)
304 static const struct mt76_mcu_ops mt76x02u_mcu_ops
= {
305 .mcu_send_msg
= mt76x02u_mcu_send_msg
,
306 .mcu_wr_rp
= mt76x02u_mcu_wr_rp
,
307 .mcu_rd_rp
= mt76x02u_mcu_rd_rp
,
310 dev
->mcu_ops
= &mt76x02u_mcu_ops
;
312 EXPORT_SYMBOL_GPL(mt76x02u_init_mcu
);
314 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
315 MODULE_LICENSE("Dual BSD/GPL");