1 // SPDX-License-Identifier: ISC
3 #include <linux/firmware.h>
8 #define MCU_SKB_RESERVE 8
10 struct mt7603_fw_trailer
{
17 mt7603_mcu_parse_response(struct mt76_dev
*mdev
, int cmd
,
18 struct sk_buff
*skb
, int seq
)
20 struct mt7603_dev
*dev
= container_of(mdev
, struct mt7603_dev
, mt76
);
21 struct mt7603_mcu_rxd
*rxd
;
25 "MCU message %d (seq %d) timed out\n",
27 dev
->mcu_hang
= MT7603_WATCHDOG_TIMEOUT
;
31 rxd
= (struct mt7603_mcu_rxd
*)skb
->data
;
39 mt7603_mcu_skb_send_msg(struct mt76_dev
*mdev
, struct sk_buff
*skb
,
40 int cmd
, int *wait_seq
)
42 struct mt7603_dev
*dev
= container_of(mdev
, struct mt7603_dev
, mt76
);
43 int hdrlen
= dev
->mcu_running
? sizeof(struct mt7603_mcu_txd
) : 12;
44 struct mt7603_mcu_txd
*txd
;
47 mdev
->mcu
.timeout
= 3 * HZ
;
49 seq
= ++mdev
->mcu
.msg_seq
& 0xf;
51 seq
= ++mdev
->mcu
.msg_seq
& 0xf;
53 txd
= (struct mt7603_mcu_txd
*)skb_push(skb
, hdrlen
);
55 txd
->len
= cpu_to_le16(skb
->len
);
56 if (cmd
== -MCU_CMD_FW_SCATTER
)
57 txd
->pq_id
= cpu_to_le16(MCU_PORT_QUEUE_FW
);
59 txd
->pq_id
= cpu_to_le16(MCU_PORT_QUEUE
);
60 txd
->pkt_type
= MCU_PKT_ID
;
65 txd
->set_query
= MCU_Q_NA
;
67 txd
->cid
= MCU_CMD_EXT_CID
;
69 txd
->set_query
= MCU_Q_SET
;
76 return mt76_tx_queue_skb_raw(dev
, mdev
->q_mcu
[MT_MCUQ_WM
], skb
, 0);
80 mt7603_mcu_init_download(struct mt7603_dev
*dev
, u32 addr
, u32 len
)
87 .addr
= cpu_to_le32(addr
),
88 .len
= cpu_to_le32(len
),
89 .mode
= cpu_to_le32(BIT(31)),
92 return mt76_mcu_send_msg(&dev
->mt76
, -MCU_CMD_TARGET_ADDRESS_LEN_REQ
,
93 &req
, sizeof(req
), true);
97 mt7603_mcu_start_firmware(struct mt7603_dev
*dev
, u32 addr
)
103 .override
= cpu_to_le32(addr
? 1 : 0),
104 .addr
= cpu_to_le32(addr
),
107 return mt76_mcu_send_msg(&dev
->mt76
, -MCU_CMD_FW_START_REQ
, &req
,
112 mt7603_mcu_restart(struct mt76_dev
*dev
)
114 return mt76_mcu_send_msg(dev
, -MCU_CMD_RESTART_DL_REQ
, NULL
, 0, true);
117 static int mt7603_load_firmware(struct mt7603_dev
*dev
)
119 const struct firmware
*fw
;
120 const struct mt7603_fw_trailer
*hdr
;
121 const char *firmware
;
126 if (is_mt7628(dev
)) {
127 if (mt76xx_rev(dev
) == MT7628_REV_E1
)
128 firmware
= MT7628_FIRMWARE_E1
;
130 firmware
= MT7628_FIRMWARE_E2
;
132 if (mt76xx_rev(dev
) < MT7603_REV_E2
)
133 firmware
= MT7603_FIRMWARE_E1
;
135 firmware
= MT7603_FIRMWARE_E2
;
138 ret
= request_firmware(&fw
, firmware
, dev
->mt76
.dev
);
142 if (!fw
|| !fw
->data
|| fw
->size
< sizeof(*hdr
)) {
143 dev_err(dev
->mt76
.dev
, "Invalid firmware\n");
148 hdr
= (const struct mt7603_fw_trailer
*)(fw
->data
+ fw
->size
-
151 dev_info(dev
->mt76
.dev
, "Firmware Version: %.10s\n", hdr
->fw_ver
);
152 dev_info(dev
->mt76
.dev
, "Build Time: %.15s\n", hdr
->build_date
);
154 addr
= mt7603_reg_map(dev
, 0x50012498);
155 mt76_wr(dev
, addr
, 0x5);
156 mt76_wr(dev
, addr
, 0x5);
159 /* switch to bypass mode */
160 mt76_rmw(dev
, MT_SCH_4
, MT_SCH_4_FORCE_QID
,
161 MT_SCH_4_BYPASS
| FIELD_PREP(MT_SCH_4_FORCE_QID
, 5));
163 val
= mt76_rr(dev
, MT_TOP_MISC2
);
165 dev_info(dev
->mt76
.dev
, "Firmware already running...\n");
169 if (!mt76_poll_msec(dev
, MT_TOP_MISC2
, BIT(0) | BIT(1), BIT(0), 500)) {
170 dev_err(dev
->mt76
.dev
, "Timeout waiting for ROM code to become ready\n");
175 dl_len
= le32_to_cpu(hdr
->dl_len
) + 4;
176 ret
= mt7603_mcu_init_download(dev
, MCU_FIRMWARE_ADDRESS
, dl_len
);
178 dev_err(dev
->mt76
.dev
, "Download request failed\n");
182 ret
= mt76_mcu_send_firmware(&dev
->mt76
, -MCU_CMD_FW_SCATTER
,
185 dev_err(dev
->mt76
.dev
, "Failed to send firmware to device\n");
189 ret
= mt7603_mcu_start_firmware(dev
, MCU_FIRMWARE_ADDRESS
);
191 dev_err(dev
->mt76
.dev
, "Failed to start firmware\n");
195 if (!mt76_poll_msec(dev
, MT_TOP_MISC2
, BIT(1), BIT(1), 500)) {
196 dev_err(dev
->mt76
.dev
, "Timeout waiting for firmware to initialize\n");
202 mt76_clear(dev
, MT_SCH_4
, MT_SCH_4_FORCE_QID
| MT_SCH_4_BYPASS
);
204 mt76_set(dev
, MT_SCH_4
, BIT(8));
205 mt76_clear(dev
, MT_SCH_4
, BIT(8));
207 dev
->mcu_running
= true;
208 snprintf(dev
->mt76
.hw
->wiphy
->fw_version
,
209 sizeof(dev
->mt76
.hw
->wiphy
->fw_version
),
210 "%.10s-%.15s", hdr
->fw_ver
, hdr
->build_date
);
211 dev_info(dev
->mt76
.dev
, "firmware init done\n");
214 release_firmware(fw
);
219 int mt7603_mcu_init(struct mt7603_dev
*dev
)
221 static const struct mt76_mcu_ops mt7603_mcu_ops
= {
222 .headroom
= sizeof(struct mt7603_mcu_txd
),
223 .mcu_skb_send_msg
= mt7603_mcu_skb_send_msg
,
224 .mcu_parse_response
= mt7603_mcu_parse_response
,
225 .mcu_restart
= mt7603_mcu_restart
,
228 dev
->mt76
.mcu_ops
= &mt7603_mcu_ops
;
229 return mt7603_load_firmware(dev
);
232 void mt7603_mcu_exit(struct mt7603_dev
*dev
)
234 __mt76_mcu_restart(&dev
->mt76
);
235 skb_queue_purge(&dev
->mt76
.mcu
.res_q
);
238 int mt7603_mcu_set_eeprom(struct mt7603_dev
*dev
)
240 static const u16 req_fields
[] = {
241 #define WORD(_start) \
244 #define GROUP_2G(_start) \
249 MT_EE_NIC_CONF_0
+ 1,
250 WORD(MT_EE_NIC_CONF_1
),
251 MT_EE_WIFI_RF_SETTING
,
252 MT_EE_TX_POWER_DELTA_BW40
,
253 MT_EE_TX_POWER_DELTA_BW80
+ 1,
254 MT_EE_TX_POWER_EXT_PA_5G
,
255 MT_EE_TEMP_SENSOR_CAL
,
256 GROUP_2G(MT_EE_TX_POWER_0_START_2G
),
257 GROUP_2G(MT_EE_TX_POWER_1_START_2G
),
258 WORD(MT_EE_TX_POWER_CCK
),
259 WORD(MT_EE_TX_POWER_OFDM_2G_6M
),
260 WORD(MT_EE_TX_POWER_OFDM_2G_24M
),
261 WORD(MT_EE_TX_POWER_OFDM_2G_54M
),
262 WORD(MT_EE_TX_POWER_HT_BPSK_QPSK
),
263 WORD(MT_EE_TX_POWER_HT_16_64_QAM
),
264 WORD(MT_EE_TX_POWER_HT_64_QAM
),
265 MT_EE_ELAN_RX_MODE_GAIN
,
266 MT_EE_ELAN_RX_MODE_NF
,
267 MT_EE_ELAN_RX_MODE_P1DB
,
268 MT_EE_ELAN_BYPASS_MODE_GAIN
,
269 MT_EE_ELAN_BYPASS_MODE_NF
,
270 MT_EE_ELAN_BYPASS_MODE_P1DB
,
271 WORD(MT_EE_STEP_NUM_NEG_6_7
),
272 WORD(MT_EE_STEP_NUM_NEG_4_5
),
273 WORD(MT_EE_STEP_NUM_NEG_2_3
),
274 WORD(MT_EE_STEP_NUM_NEG_0_1
),
275 WORD(MT_EE_REF_STEP_24G
),
276 WORD(MT_EE_STEP_NUM_PLUS_1_2
),
277 WORD(MT_EE_STEP_NUM_PLUS_3_4
),
278 WORD(MT_EE_STEP_NUM_PLUS_5_6
),
279 MT_EE_STEP_NUM_PLUS_7
,
280 MT_EE_XTAL_FREQ_OFFSET
,
281 MT_EE_XTAL_TRIM_2_COMP
,
282 MT_EE_XTAL_TRIM_3_COMP
,
285 /* unknown fields below */
296 WORD(0x130), WORD(0x132), WORD(0x134), WORD(0x136),
297 WORD(0x138), WORD(0x13a), WORD(0x13c), WORD(0x13e),
314 .len
= ARRAY_SIZE(req_fields
) - 1,
316 const int size
= 0xff * sizeof(struct req_data
);
317 u8
*req
, *eep
= (u8
*)dev
->mt76
.eeprom
.data
;
318 int i
, ret
, len
= sizeof(req_hdr
) + size
;
319 struct req_data
*data
;
321 BUILD_BUG_ON(ARRAY_SIZE(req_fields
) * sizeof(*data
) > size
);
323 req
= kmalloc(len
, GFP_KERNEL
);
327 memcpy(req
, &req_hdr
, sizeof(req_hdr
));
328 data
= (struct req_data
*)(req
+ sizeof(req_hdr
));
329 memset(data
, 0, size
);
330 for (i
= 0; i
< ARRAY_SIZE(req_fields
); i
++) {
331 data
[i
].addr
= cpu_to_le16(req_fields
[i
]);
332 data
[i
].val
= eep
[req_fields
[i
]];
335 ret
= mt76_mcu_send_msg(&dev
->mt76
, MCU_EXT_CMD_EFUSE_BUFFER_MODE
,
342 static int mt7603_mcu_set_tx_power(struct mt7603_dev
*dev
)
349 u8 rate_power_delta
[14];
351 u8 ch_power_delta
[6];
352 u8 temp_comp_power
[17];
355 .center_channel
= dev
->mphy
.chandef
.chan
->hw_value
,
356 #define EEP_VAL(n) ((u8 *)dev->mt76.eeprom.data)[n]
357 .tssi
= EEP_VAL(MT_EE_NIC_CONF_1
+ 1),
358 .temp_comp
= EEP_VAL(MT_EE_NIC_CONF_1
),
360 EEP_VAL(MT_EE_TX_POWER_0_START_2G
+ 2),
361 EEP_VAL(MT_EE_TX_POWER_1_START_2G
+ 2)
363 .bw_power_delta
= EEP_VAL(MT_EE_TX_POWER_DELTA_BW40
),
365 EEP_VAL(MT_EE_TX_POWER_0_START_2G
+ 3),
366 EEP_VAL(MT_EE_TX_POWER_0_START_2G
+ 4),
367 EEP_VAL(MT_EE_TX_POWER_0_START_2G
+ 5),
368 EEP_VAL(MT_EE_TX_POWER_1_START_2G
+ 3),
369 EEP_VAL(MT_EE_TX_POWER_1_START_2G
+ 4),
370 EEP_VAL(MT_EE_TX_POWER_1_START_2G
+ 5)
374 u8
*eep
= (u8
*)dev
->mt76
.eeprom
.data
;
376 memcpy(req
.rate_power_delta
, eep
+ MT_EE_TX_POWER_CCK
,
377 sizeof(req
.rate_power_delta
));
379 memcpy(req
.temp_comp_power
, eep
+ MT_EE_STEP_NUM_NEG_6_7
,
380 sizeof(req
.temp_comp_power
));
382 return mt76_mcu_send_msg(&dev
->mt76
, MCU_EXT_CMD_SET_TX_POWER_CTRL
,
383 &req
, sizeof(req
), true);
386 int mt7603_mcu_set_channel(struct mt7603_dev
*dev
)
388 struct cfg80211_chan_def
*chandef
= &dev
->mphy
.chandef
;
389 struct ieee80211_hw
*hw
= mt76_hw(dev
);
390 int n_chains
= hweight8(dev
->mphy
.antenna_mask
);
401 .control_chan
= chandef
->chan
->hw_value
,
402 .center_chan
= chandef
->chan
->hw_value
,
404 .tx_streams
= n_chains
,
405 .rx_streams
= n_chains
,
410 if (dev
->mphy
.chandef
.width
== NL80211_CHAN_WIDTH_40
) {
412 if (chandef
->center_freq1
> chandef
->chan
->center_freq
)
413 req
.center_chan
+= 2;
415 req
.center_chan
-= 2;
418 tx_power
= hw
->conf
.power_level
* 2;
419 if (dev
->mphy
.antenna_mask
== 3)
421 tx_power
= min(tx_power
, dev
->tx_power_limit
);
423 dev
->mphy
.txpower_cur
= tx_power
;
425 for (i
= 0; i
< ARRAY_SIZE(req
.txpower
); i
++)
426 req
.txpower
[i
] = tx_power
;
428 ret
= mt76_mcu_send_msg(&dev
->mt76
, MCU_EXT_CMD_CHANNEL_SWITCH
, &req
,
433 return mt7603_mcu_set_tx_power(dev
);