1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2022 MediaTek Inc.
3 *
4 * Author: Lorenzo Bianconi <lorenzo@kernel.org>
5 * Sujuan Chen <sujuan.chen@mediatek.com>
6 */
7
8 #include <linux/firmware.h>
9 #include <linux/of_address.h>
10 #include <linux/of_reserved_mem.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/soc/mediatek/mtk_wed.h>
13 #include <asm/unaligned.h>
14
15 #include "mtk_wed_regs.h"
16 #include "mtk_wed_wo.h"
17 #include "mtk_wed.h"
18
wo_r32(struct mtk_wed_wo * wo,u32 reg)19 static u32 wo_r32(struct mtk_wed_wo *wo, u32 reg)
20 {
21 return readl(wo->boot.addr + reg);
22 }
23
wo_w32(struct mtk_wed_wo * wo,u32 reg,u32 val)24 static void wo_w32(struct mtk_wed_wo *wo, u32 reg, u32 val)
25 {
26 writel(val, wo->boot.addr + reg);
27 }
28
29 static struct sk_buff *
mtk_wed_mcu_msg_alloc(const void * data,int data_len)30 mtk_wed_mcu_msg_alloc(const void *data, int data_len)
31 {
32 int length = sizeof(struct mtk_wed_mcu_hdr) + data_len;
33 struct sk_buff *skb;
34
35 skb = alloc_skb(length, GFP_KERNEL);
36 if (!skb)
37 return NULL;
38
39 memset(skb->head, 0, length);
40 skb_reserve(skb, sizeof(struct mtk_wed_mcu_hdr));
41 if (data && data_len)
42 skb_put_data(skb, data, data_len);
43
44 return skb;
45 }
46
47 static struct sk_buff *
mtk_wed_mcu_get_response(struct mtk_wed_wo * wo,unsigned long expires)48 mtk_wed_mcu_get_response(struct mtk_wed_wo *wo, unsigned long expires)
49 {
50 if (!time_is_after_jiffies(expires))
51 return NULL;
52
53 wait_event_timeout(wo->mcu.wait, !skb_queue_empty(&wo->mcu.res_q),
54 expires - jiffies);
55 return skb_dequeue(&wo->mcu.res_q);
56 }
57
mtk_wed_mcu_rx_event(struct mtk_wed_wo * wo,struct sk_buff * skb)58 void mtk_wed_mcu_rx_event(struct mtk_wed_wo *wo, struct sk_buff *skb)
59 {
60 skb_queue_tail(&wo->mcu.res_q, skb);
61 wake_up(&wo->mcu.wait);
62 }
63
64 static void
mtk_wed_update_rx_stats(struct mtk_wed_device * wed,struct sk_buff * skb)65 mtk_wed_update_rx_stats(struct mtk_wed_device *wed, struct sk_buff *skb)
66 {
67 u32 count = get_unaligned_le32(skb->data);
68 struct mtk_wed_wo_rx_stats *stats;
69 int i;
70
71 if (!wed->wlan.update_wo_rx_stats)
72 return;
73
74 if (count * sizeof(*stats) > skb->len - sizeof(u32))
75 return;
76
77 stats = (struct mtk_wed_wo_rx_stats *)(skb->data + sizeof(u32));
78 for (i = 0 ; i < count ; i++)
79 wed->wlan.update_wo_rx_stats(wed, &stats[i]);
80 }
81
mtk_wed_mcu_rx_unsolicited_event(struct mtk_wed_wo * wo,struct sk_buff * skb)82 void mtk_wed_mcu_rx_unsolicited_event(struct mtk_wed_wo *wo,
83 struct sk_buff *skb)
84 {
85 struct mtk_wed_mcu_hdr *hdr = (struct mtk_wed_mcu_hdr *)skb->data;
86
87 skb_pull(skb, sizeof(*hdr));
88
89 switch (hdr->cmd) {
90 case MTK_WED_WO_EVT_LOG_DUMP:
91 dev_notice(wo->hw->dev, "%s\n", skb->data);
92 break;
93 case MTK_WED_WO_EVT_PROFILING: {
94 struct mtk_wed_wo_log_info *info = (void *)skb->data;
95 u32 count = skb->len / sizeof(*info);
96 int i;
97
98 for (i = 0 ; i < count ; i++)
99 dev_notice(wo->hw->dev,
100 "SN:%u latency: total=%u, rro:%u, mod:%u\n",
101 le32_to_cpu(info[i].sn),
102 le32_to_cpu(info[i].total),
103 le32_to_cpu(info[i].rro),
104 le32_to_cpu(info[i].mod));
105 break;
106 }
107 case MTK_WED_WO_EVT_RXCNT_INFO:
108 mtk_wed_update_rx_stats(wo->hw->wed_dev, skb);
109 break;
110 default:
111 break;
112 }
113
114 dev_kfree_skb(skb);
115 }
116
117 static int
mtk_wed_mcu_skb_send_msg(struct mtk_wed_wo * wo,struct sk_buff * skb,int id,int cmd,u16 * wait_seq,bool wait_resp)118 mtk_wed_mcu_skb_send_msg(struct mtk_wed_wo *wo, struct sk_buff *skb,
119 int id, int cmd, u16 *wait_seq, bool wait_resp)
120 {
121 struct mtk_wed_mcu_hdr *hdr;
122
123 /* TODO: make it dynamic based on cmd */
124 wo->mcu.timeout = 20 * HZ;
125
126 hdr = (struct mtk_wed_mcu_hdr *)skb_push(skb, sizeof(*hdr));
127 hdr->cmd = cmd;
128 hdr->length = cpu_to_le16(skb->len);
129
130 if (wait_resp && wait_seq) {
131 u16 seq = ++wo->mcu.seq;
132
133 if (!seq)
134 seq = ++wo->mcu.seq;
135 *wait_seq = seq;
136
137 hdr->flag |= cpu_to_le16(MTK_WED_WARP_CMD_FLAG_NEED_RSP);
138 hdr->seq = cpu_to_le16(seq);
139 }
140 if (id == MTK_WED_MODULE_ID_WO)
141 hdr->flag |= cpu_to_le16(MTK_WED_WARP_CMD_FLAG_FROM_TO_WO);
142
143 return mtk_wed_wo_queue_tx_skb(wo, &wo->q_tx, skb);
144 }
145
146 static int
mtk_wed_mcu_parse_response(struct mtk_wed_wo * wo,struct sk_buff * skb,int cmd,int seq)147 mtk_wed_mcu_parse_response(struct mtk_wed_wo *wo, struct sk_buff *skb,
148 int cmd, int seq)
149 {
150 struct mtk_wed_mcu_hdr *hdr;
151
152 if (!skb) {
153 dev_err(wo->hw->dev, "Message %08x (seq %d) timeout\n",
154 cmd, seq);
155 return -ETIMEDOUT;
156 }
157
158 hdr = (struct mtk_wed_mcu_hdr *)skb->data;
159 if (le16_to_cpu(hdr->seq) != seq)
160 return -EAGAIN;
161
162 skb_pull(skb, sizeof(*hdr));
163 switch (cmd) {
164 case MTK_WED_WO_CMD_RXCNT_INFO:
165 mtk_wed_update_rx_stats(wo->hw->wed_dev, skb);
166 break;
167 default:
168 break;
169 }
170
171 return 0;
172 }
173
mtk_wed_mcu_send_msg(struct mtk_wed_wo * wo,int id,int cmd,const void * data,int len,bool wait_resp)174 int mtk_wed_mcu_send_msg(struct mtk_wed_wo *wo, int id, int cmd,
175 const void *data, int len, bool wait_resp)
176 {
177 unsigned long expires;
178 struct sk_buff *skb;
179 u16 seq;
180 int ret;
181
182 skb = mtk_wed_mcu_msg_alloc(data, len);
183 if (!skb)
184 return -ENOMEM;
185
186 mutex_lock(&wo->mcu.mutex);
187
188 ret = mtk_wed_mcu_skb_send_msg(wo, skb, id, cmd, &seq, wait_resp);
189 if (ret || !wait_resp)
190 goto unlock;
191
192 expires = jiffies + wo->mcu.timeout;
193 do {
194 skb = mtk_wed_mcu_get_response(wo, expires);
195 ret = mtk_wed_mcu_parse_response(wo, skb, cmd, seq);
196 dev_kfree_skb(skb);
197 } while (ret == -EAGAIN);
198
199 unlock:
200 mutex_unlock(&wo->mcu.mutex);
201
202 return ret;
203 }
204
mtk_wed_mcu_msg_update(struct mtk_wed_device * dev,int id,void * data,int len)205 int mtk_wed_mcu_msg_update(struct mtk_wed_device *dev, int id, void *data,
206 int len)
207 {
208 struct mtk_wed_wo *wo = dev->hw->wed_wo;
209
210 if (dev->hw->version == 1)
211 return 0;
212
213 if (WARN_ON(!wo))
214 return -ENODEV;
215
216 return mtk_wed_mcu_send_msg(wo, MTK_WED_MODULE_ID_WO, id, data, len,
217 true);
218 }
219
220 static int
mtk_wed_get_memory_region(struct mtk_wed_wo * wo,struct mtk_wed_wo_memory_region * region)221 mtk_wed_get_memory_region(struct mtk_wed_wo *wo,
222 struct mtk_wed_wo_memory_region *region)
223 {
224 struct reserved_mem *rmem;
225 struct device_node *np;
226 int index;
227
228 index = of_property_match_string(wo->hw->node, "memory-region-names",
229 region->name);
230 if (index < 0)
231 return index;
232
233 np = of_parse_phandle(wo->hw->node, "memory-region", index);
234 if (!np)
235 return -ENODEV;
236
237 rmem = of_reserved_mem_lookup(np);
238 of_node_put(np);
239
240 if (!rmem)
241 return -ENODEV;
242
243 region->phy_addr = rmem->base;
244 region->size = rmem->size;
245 region->addr = devm_ioremap(wo->hw->dev, region->phy_addr, region->size);
246
247 return !region->addr ? -EINVAL : 0;
248 }
249
250 static int
mtk_wed_mcu_run_firmware(struct mtk_wed_wo * wo,const struct firmware * fw,struct mtk_wed_wo_memory_region * region)251 mtk_wed_mcu_run_firmware(struct mtk_wed_wo *wo, const struct firmware *fw,
252 struct mtk_wed_wo_memory_region *region)
253 {
254 const u8 *first_region_ptr, *region_ptr, *trailer_ptr, *ptr = fw->data;
255 const struct mtk_wed_fw_trailer *trailer;
256 const struct mtk_wed_fw_region *fw_region;
257
258 trailer_ptr = fw->data + fw->size - sizeof(*trailer);
259 trailer = (const struct mtk_wed_fw_trailer *)trailer_ptr;
260 region_ptr = trailer_ptr - trailer->num_region * sizeof(*fw_region);
261 first_region_ptr = region_ptr;
262
263 while (region_ptr < trailer_ptr) {
264 u32 length;
265
266 fw_region = (const struct mtk_wed_fw_region *)region_ptr;
267 length = le32_to_cpu(fw_region->len);
268
269 if (region->phy_addr != le32_to_cpu(fw_region->addr))
270 goto next;
271
272 if (region->size < length)
273 goto next;
274
275 if (first_region_ptr < ptr + length)
276 goto next;
277
278 if (region->shared && region->consumed)
279 return 0;
280
281 if (!region->shared || !region->consumed) {
282 memcpy_toio(region->addr, ptr, length);
283 region->consumed = true;
284 return 0;
285 }
286 next:
287 region_ptr += sizeof(*fw_region);
288 ptr += length;
289 }
290
291 return -EINVAL;
292 }
293
294 static int
mtk_wed_mcu_load_firmware(struct mtk_wed_wo * wo)295 mtk_wed_mcu_load_firmware(struct mtk_wed_wo *wo)
296 {
297 static struct mtk_wed_wo_memory_region mem_region[] = {
298 [MTK_WED_WO_REGION_EMI] = {
299 .name = "wo-emi",
300 },
301 [MTK_WED_WO_REGION_ILM] = {
302 .name = "wo-ilm",
303 },
304 [MTK_WED_WO_REGION_DATA] = {
305 .name = "wo-data",
306 .shared = true,
307 },
308 };
309 const struct mtk_wed_fw_trailer *trailer;
310 const struct firmware *fw;
311 const char *fw_name;
312 u32 val, boot_cr;
313 int ret, i;
314
315 /* load firmware region metadata */
316 for (i = 0; i < ARRAY_SIZE(mem_region); i++) {
317 ret = mtk_wed_get_memory_region(wo, &mem_region[i]);
318 if (ret)
319 return ret;
320 }
321
322 wo->boot.name = "wo-boot";
323 ret = mtk_wed_get_memory_region(wo, &wo->boot);
324 if (ret)
325 return ret;
326
327 /* set dummy cr */
328 wed_w32(wo->hw->wed_dev, MTK_WED_SCR0 + 4 * MTK_WED_DUMMY_CR_FWDL,
329 wo->hw->index + 1);
330
331 /* load firmware */
332 if (of_device_is_compatible(wo->hw->node, "mediatek,mt7981-wed"))
333 fw_name = MT7981_FIRMWARE_WO;
334 else
335 fw_name = wo->hw->index ? MT7986_FIRMWARE_WO1 : MT7986_FIRMWARE_WO0;
336
337 ret = request_firmware(&fw, fw_name, wo->hw->dev);
338 if (ret)
339 return ret;
340
341 trailer = (void *)(fw->data + fw->size -
342 sizeof(struct mtk_wed_fw_trailer));
343 dev_info(wo->hw->dev,
344 "MTK WED WO Firmware Version: %.10s, Build Time: %.15s\n",
345 trailer->fw_ver, trailer->build_date);
346 dev_info(wo->hw->dev, "MTK WED WO Chip ID %02x Region %d\n",
347 trailer->chip_id, trailer->num_region);
348
349 for (i = 0; i < ARRAY_SIZE(mem_region); i++) {
350 ret = mtk_wed_mcu_run_firmware(wo, fw, &mem_region[i]);
351 if (ret)
352 goto out;
353 }
354
355 /* set the start address */
356 boot_cr = wo->hw->index ? MTK_WO_MCU_CFG_LS_WA_BOOT_ADDR_ADDR
357 : MTK_WO_MCU_CFG_LS_WM_BOOT_ADDR_ADDR;
358 wo_w32(wo, boot_cr, mem_region[MTK_WED_WO_REGION_EMI].phy_addr >> 16);
359 /* wo firmware reset */
360 wo_w32(wo, MTK_WO_MCU_CFG_LS_WF_MCCR_CLR_ADDR, 0xc00);
361
362 val = wo_r32(wo, MTK_WO_MCU_CFG_LS_WF_MCU_CFG_WM_WA_ADDR);
363 val |= wo->hw->index ? MTK_WO_MCU_CFG_LS_WF_WM_WA_WA_CPU_RSTB_MASK
364 : MTK_WO_MCU_CFG_LS_WF_WM_WA_WM_CPU_RSTB_MASK;
365 wo_w32(wo, MTK_WO_MCU_CFG_LS_WF_MCU_CFG_WM_WA_ADDR, val);
366 out:
367 release_firmware(fw);
368
369 return ret;
370 }
371
372 static u32
mtk_wed_mcu_read_fw_dl(struct mtk_wed_wo * wo)373 mtk_wed_mcu_read_fw_dl(struct mtk_wed_wo *wo)
374 {
375 return wed_r32(wo->hw->wed_dev,
376 MTK_WED_SCR0 + 4 * MTK_WED_DUMMY_CR_FWDL);
377 }
378
mtk_wed_mcu_init(struct mtk_wed_wo * wo)379 int mtk_wed_mcu_init(struct mtk_wed_wo *wo)
380 {
381 u32 val;
382 int ret;
383
384 skb_queue_head_init(&wo->mcu.res_q);
385 init_waitqueue_head(&wo->mcu.wait);
386 mutex_init(&wo->mcu.mutex);
387
388 ret = mtk_wed_mcu_load_firmware(wo);
389 if (ret)
390 return ret;
391
392 return readx_poll_timeout(mtk_wed_mcu_read_fw_dl, wo, val, !val,
393 100, MTK_FW_DL_TIMEOUT);
394 }
395
396 MODULE_FIRMWARE(MT7981_FIRMWARE_WO);
397 MODULE_FIRMWARE(MT7986_FIRMWARE_WO0);
398 MODULE_FIRMWARE(MT7986_FIRMWARE_WO1);
399