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