1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2021 MediaTek Inc.
5 #include <linux/module.h>
6 #include <linux/firmware.h>
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
15 /* It is for mt79xx download rom patch*/
16 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
17 #define MTK_FW_ROM_PATCH_GD_SIZE 64
18 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
19 #define MTK_SEC_MAP_COMMON_SIZE 12
20 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
22 struct btmtk_patch_header {
30 struct btmtk_global_desc {
37 struct btmtk_section_map {
56 static void btmtk_coredump(struct hci_dev *hdev)
60 err = __hci_cmd_send(hdev, 0xfd5b, 0, NULL);
62 bt_dev_err(hdev, "Coredump failed (%d)", err);
65 static void btmtk_coredump_hdr(struct hci_dev *hdev, struct sk_buff *skb)
67 struct btmediatek_data *data = hci_get_priv(hdev);
70 snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
72 skb_put_data(skb, buf, strlen(buf));
74 snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
75 data->cd_info.fw_version);
76 skb_put_data(skb, buf, strlen(buf));
78 snprintf(buf, sizeof(buf), "Driver: %s\n",
79 data->cd_info.driver_name);
80 skb_put_data(skb, buf, strlen(buf));
82 snprintf(buf, sizeof(buf), "Vendor: MediaTek\n");
83 skb_put_data(skb, buf, strlen(buf));
86 static void btmtk_coredump_notify(struct hci_dev *hdev, int state)
88 struct btmediatek_data *data = hci_get_priv(hdev);
91 case HCI_DEVCOREDUMP_IDLE:
92 data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
94 case HCI_DEVCOREDUMP_ACTIVE:
95 data->cd_info.state = HCI_DEVCOREDUMP_ACTIVE;
97 case HCI_DEVCOREDUMP_TIMEOUT:
98 case HCI_DEVCOREDUMP_ABORT:
99 case HCI_DEVCOREDUMP_DONE:
100 data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
101 btmtk_reset_sync(hdev);
106 int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname,
107 wmt_cmd_sync_func_t wmt_cmd_sync)
109 struct btmtk_hci_wmt_params wmt_params;
110 struct btmtk_patch_header *hdr;
111 struct btmtk_global_desc *globaldesc = NULL;
112 struct btmtk_section_map *sectionmap;
113 const struct firmware *fw;
115 const u8 *fw_bin_ptr;
116 int err, dlen, i, status;
117 u8 flag, first_block, retry;
118 u32 section_num, dl_size, section_offset;
121 err = request_firmware(&fw, fwname, &hdev->dev);
123 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
129 hdr = (struct btmtk_patch_header *)fw_ptr;
130 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
131 section_num = le32_to_cpu(globaldesc->section_num);
133 bt_dev_info(hdev, "HW/SW Version: 0x%04x%04x, Build Time: %s",
134 le16_to_cpu(hdr->hwver), le16_to_cpu(hdr->swver), hdr->datetime);
136 for (i = 0; i < section_num; i++) {
139 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
140 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
142 section_offset = le32_to_cpu(sectionmap->secoffset);
143 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
148 cmd[0] = 0; /* 0 means legacy dl mode. */
150 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
151 MTK_FW_ROM_PATCH_GD_SIZE +
152 MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
153 MTK_SEC_MAP_COMMON_SIZE,
154 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
156 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
157 wmt_params.status = &status;
159 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
160 wmt_params.data = &cmd;
162 err = wmt_cmd_sync(hdev, &wmt_params);
164 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
169 if (status == BTMTK_WMT_PATCH_UNDONE) {
171 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
174 } else if (status == BTMTK_WMT_PATCH_DONE) {
177 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
184 fw_ptr += section_offset;
185 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
186 wmt_params.status = NULL;
188 while (dl_size > 0) {
189 dlen = min_t(int, 250, dl_size);
190 if (first_block == 1) {
193 } else if (dl_size - dlen <= 0) {
199 wmt_params.flag = flag;
200 wmt_params.dlen = dlen;
201 wmt_params.data = fw_ptr;
203 err = wmt_cmd_sync(hdev, &wmt_params);
205 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
217 /* Wait a few moments for firmware activation done */
218 usleep_range(100000, 120000);
221 release_firmware(fw);
225 EXPORT_SYMBOL_GPL(btmtk_setup_firmware_79xx);
227 int btmtk_setup_firmware(struct hci_dev *hdev, const char *fwname,
228 wmt_cmd_sync_func_t wmt_cmd_sync)
230 struct btmtk_hci_wmt_params wmt_params;
231 const struct firmware *fw;
237 err = request_firmware(&fw, fwname, &hdev->dev);
239 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
243 /* Power on data RAM the firmware relies on. */
245 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
247 wmt_params.dlen = sizeof(param);
248 wmt_params.data = ¶m;
249 wmt_params.status = NULL;
251 err = wmt_cmd_sync(hdev, &wmt_params);
253 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
260 /* The size of patch header is 30 bytes, should be skip */
270 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
271 wmt_params.status = NULL;
273 while (fw_size > 0) {
274 dlen = min_t(int, 250, fw_size);
276 /* Tell device the position in sequence */
277 if (fw_size - dlen <= 0)
279 else if (fw_size < fw->size - 30)
282 wmt_params.flag = flag;
283 wmt_params.dlen = dlen;
284 wmt_params.data = fw_ptr;
286 err = wmt_cmd_sync(hdev, &wmt_params);
288 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
297 wmt_params.op = BTMTK_WMT_RST;
300 wmt_params.data = NULL;
301 wmt_params.status = NULL;
303 /* Activate funciton the firmware providing to */
304 err = wmt_cmd_sync(hdev, &wmt_params);
306 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
310 /* Wait a few moments for firmware activation done */
311 usleep_range(10000, 12000);
314 release_firmware(fw);
318 EXPORT_SYMBOL_GPL(btmtk_setup_firmware);
320 int btmtk_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
325 skb = __hci_cmd_sync(hdev, 0xfc1a, 6, bdaddr, HCI_INIT_TIMEOUT);
328 bt_dev_err(hdev, "changing Mediatek device address failed (%ld)",
336 EXPORT_SYMBOL_GPL(btmtk_set_bdaddr);
338 void btmtk_reset_sync(struct hci_dev *hdev)
340 struct btmediatek_data *reset_work = hci_get_priv(hdev);
345 err = hci_cmd_sync_queue(hdev, reset_work->reset_sync, NULL, NULL);
347 bt_dev_err(hdev, "failed to reset (%d)", err);
349 hci_dev_unlock(hdev);
351 EXPORT_SYMBOL_GPL(btmtk_reset_sync);
353 int btmtk_register_coredump(struct hci_dev *hdev, const char *name,
356 struct btmediatek_data *data = hci_get_priv(hdev);
358 if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
361 data->cd_info.fw_version = fw_version;
362 data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
363 data->cd_info.driver_name = name;
365 return hci_devcd_register(hdev, btmtk_coredump, btmtk_coredump_hdr,
366 btmtk_coredump_notify);
368 EXPORT_SYMBOL_GPL(btmtk_register_coredump);
370 int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb)
372 struct btmediatek_data *data = hci_get_priv(hdev);
375 if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
378 switch (data->cd_info.state) {
379 case HCI_DEVCOREDUMP_IDLE:
380 err = hci_devcd_init(hdev, MTK_COREDUMP_SIZE);
383 data->cd_info.cnt = 0;
385 /* It is supposed coredump can be done within 5 seconds */
386 schedule_delayed_work(&hdev->dump.dump_timeout,
387 msecs_to_jiffies(5000));
389 case HCI_DEVCOREDUMP_ACTIVE:
391 err = hci_devcd_append(hdev, skb);
396 /* Mediatek coredump data would be more than MTK_COREDUMP_NUM */
397 if (data->cd_info.cnt > MTK_COREDUMP_NUM &&
398 skb->len > MTK_COREDUMP_END_LEN)
399 if (!memcmp((char *)&skb->data[skb->len - MTK_COREDUMP_END_LEN],
400 MTK_COREDUMP_END, MTK_COREDUMP_END_LEN - 1)) {
401 bt_dev_info(hdev, "Mediatek coredump end");
402 hci_devcd_complete(hdev);
413 EXPORT_SYMBOL_GPL(btmtk_process_coredump);
415 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
416 MODULE_AUTHOR("Mark Chen <mark-yw.chen@mediatek.com>");
417 MODULE_DESCRIPTION("Bluetooth support for MediaTek devices ver " VERSION);
418 MODULE_VERSION(VERSION);
419 MODULE_LICENSE("GPL");
420 MODULE_FIRMWARE(FIRMWARE_MT7622);
421 MODULE_FIRMWARE(FIRMWARE_MT7663);
422 MODULE_FIRMWARE(FIRMWARE_MT7668);
423 MODULE_FIRMWARE(FIRMWARE_MT7961);
424 MODULE_FIRMWARE(FIRMWARE_MT7925);