blob: 3a12ea0cf2859ca5bb8839dac6719626e67677c5 [file] [log] [blame]
Sean Wang9aebfd42019-03-08 09:15:44 +08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019 MediaTek Inc.
3
4/*
5 * Bluetooth support for MediaTek SDIO devices
6 *
7 * This file is written based on btsdio.c and btmtkuart.c.
8 *
9 * Author: Sean Wang <sean.wang@mediatek.com>
10 *
11 */
12
13#include <asm/unaligned.h>
14#include <linux/atomic.h>
Sean Wang9aebfd42019-03-08 09:15:44 +080015#include <linux/init.h>
16#include <linux/iopoll.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Sean Wang7f3c5632019-04-18 17:08:02 +080019#include <linux/pm_runtime.h>
Sean Wang9aebfd42019-03-08 09:15:44 +080020#include <linux/skbuff.h>
21
22#include <linux/mmc/host.h>
23#include <linux/mmc/sdio_ids.h>
24#include <linux/mmc/sdio_func.h>
25
26#include <net/bluetooth/bluetooth.h>
27#include <net/bluetooth/hci_core.h>
28
29#include "h4_recv.h"
Sean Wang3a722042021-10-19 05:30:13 +080030#include "btmtk.h"
Sean Wang9aebfd42019-03-08 09:15:44 +080031
32#define VERSION "0.1"
33
Sean Wang7f3c5632019-04-18 17:08:02 +080034#define MTKBTSDIO_AUTOSUSPEND_DELAY 8000
35
36static bool enable_autosuspend;
37
Sean Wang9aebfd42019-03-08 09:15:44 +080038struct btmtksdio_data {
39 const char *fwname;
Sean Wangc603bf12021-10-19 05:30:21 +080040 u16 chipid;
Sean Wang9aebfd42019-03-08 09:15:44 +080041};
42
43static const struct btmtksdio_data mt7663_data = {
44 .fwname = FIRMWARE_MT7663,
Sean Wangc603bf12021-10-19 05:30:21 +080045 .chipid = 0x7663,
Sean Wang9aebfd42019-03-08 09:15:44 +080046};
47
48static const struct btmtksdio_data mt7668_data = {
49 .fwname = FIRMWARE_MT7668,
Sean Wangc603bf12021-10-19 05:30:21 +080050 .chipid = 0x7668,
51};
52
53static const struct btmtksdio_data mt7921_data = {
54 .fwname = FIRMWARE_MT7961,
55 .chipid = 0x7921,
Sean Wang9aebfd42019-03-08 09:15:44 +080056};
57
58static const struct sdio_device_id btmtksdio_table[] = {
Pali Rohárbaaa1102020-05-22 16:44:06 +020059 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7663),
Sean Wang9aebfd42019-03-08 09:15:44 +080060 .driver_data = (kernel_ulong_t)&mt7663_data },
Pali Rohárbaaa1102020-05-22 16:44:06 +020061 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7668),
Sean Wang9aebfd42019-03-08 09:15:44 +080062 .driver_data = (kernel_ulong_t)&mt7668_data },
Sean Wangc603bf12021-10-19 05:30:21 +080063 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7961),
64 .driver_data = (kernel_ulong_t)&mt7921_data },
Sean Wang9aebfd42019-03-08 09:15:44 +080065 { } /* Terminating entry */
66};
Bartosz Golaszewski53121a72019-11-07 10:46:10 +010067MODULE_DEVICE_TABLE(sdio, btmtksdio_table);
Sean Wang9aebfd42019-03-08 09:15:44 +080068
69#define MTK_REG_CHLPCR 0x4 /* W1S */
70#define C_INT_EN_SET BIT(0)
71#define C_INT_EN_CLR BIT(1)
Sean Wang2e47cc22019-04-18 17:08:00 +080072#define C_FW_OWN_REQ_SET BIT(8) /* For write */
73#define C_COM_DRV_OWN BIT(8) /* For read */
Sean Wang9aebfd42019-03-08 09:15:44 +080074#define C_FW_OWN_REQ_CLR BIT(9)
75
76#define MTK_REG_CSDIOCSR 0x8
77#define SDIO_RE_INIT_EN BIT(0)
78#define SDIO_INT_CTL BIT(2)
79
80#define MTK_REG_CHCR 0xc
81#define C_INT_CLR_CTRL BIT(1)
82
83/* CHISR have the same bits field definition with CHIER */
84#define MTK_REG_CHISR 0x10
85#define MTK_REG_CHIER 0x14
86#define FW_OWN_BACK_INT BIT(0)
87#define RX_DONE_INT BIT(1)
88#define TX_EMPTY BIT(2)
89#define TX_FIFO_OVERFLOW BIT(8)
90#define RX_PKT_LEN GENMASK(31, 16)
91
92#define MTK_REG_CTDR 0x18
93
94#define MTK_REG_CRDR 0x1c
95
Sean Wang184ea402021-10-19 05:30:18 +080096#define MTK_REG_CRPLR 0x24
97
Sean Wang9aebfd42019-03-08 09:15:44 +080098#define MTK_SDIO_BLOCK_SIZE 256
99
100#define BTMTKSDIO_TX_WAIT_VND_EVT 1
Sean Wangd555b1f2021-11-20 06:25:43 +0800101#define BTMTKSDIO_HW_TX_READY 2
Sean Wang4b4b2222021-12-02 02:02:46 +0800102#define BTMTKSDIO_FUNC_ENABLED 3
Sean Wang9aebfd42019-03-08 09:15:44 +0800103
Sean Wang9aebfd42019-03-08 09:15:44 +0800104struct mtkbtsdio_hdr {
105 __le16 len;
106 __le16 reserved;
107 u8 bt_type;
108} __packed;
109
Sean Wang9aebfd42019-03-08 09:15:44 +0800110struct btmtksdio_dev {
111 struct hci_dev *hdev;
112 struct sdio_func *func;
Sean Wang7f3c5632019-04-18 17:08:02 +0800113 struct device *dev;
Sean Wang9aebfd42019-03-08 09:15:44 +0800114
Sean Wang26270bc2021-10-19 05:30:16 +0800115 struct work_struct txrx_work;
Sean Wang9aebfd42019-03-08 09:15:44 +0800116 unsigned long tx_state;
117 struct sk_buff_head txq;
118
119 struct sk_buff *evt_skb;
120
121 const struct btmtksdio_data *data;
122};
123
124static int mtk_hci_wmt_sync(struct hci_dev *hdev,
125 struct btmtk_hci_wmt_params *wmt_params)
126{
127 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
128 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
Sean Wangc603bf12021-10-19 05:30:21 +0800129 struct btmtk_hci_wmt_evt_reg *wmt_evt_reg;
Sean Wang9aebfd42019-03-08 09:15:44 +0800130 u32 hlen, status = BTMTK_WMT_INVALID;
131 struct btmtk_hci_wmt_evt *wmt_evt;
Sean Wang3a722042021-10-19 05:30:13 +0800132 struct btmtk_hci_wmt_cmd *wc;
133 struct btmtk_wmt_hdr *hdr;
Sean Wang9aebfd42019-03-08 09:15:44 +0800134 int err;
135
Sean Wang3a722042021-10-19 05:30:13 +0800136 /* Send the WMT command and wait until the WMT event returns */
Sean Wang9aebfd42019-03-08 09:15:44 +0800137 hlen = sizeof(*hdr) + wmt_params->dlen;
138 if (hlen > 255)
139 return -EINVAL;
140
Sean Wang3a722042021-10-19 05:30:13 +0800141 wc = kzalloc(hlen, GFP_KERNEL);
142 if (!wc)
143 return -ENOMEM;
144
145 hdr = &wc->hdr;
Sean Wang9aebfd42019-03-08 09:15:44 +0800146 hdr->dir = 1;
147 hdr->op = wmt_params->op;
148 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
149 hdr->flag = wmt_params->flag;
Sean Wang3a722042021-10-19 05:30:13 +0800150 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
Sean Wang9aebfd42019-03-08 09:15:44 +0800151
152 set_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
153
Sean Wang3a722042021-10-19 05:30:13 +0800154 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
Sean Wang9aebfd42019-03-08 09:15:44 +0800155 if (err < 0) {
156 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
Sean Wang3a722042021-10-19 05:30:13 +0800157 goto err_free_wc;
Sean Wang9aebfd42019-03-08 09:15:44 +0800158 }
159
160 /* The vendor specific WMT commands are all answered by a vendor
161 * specific event and will not have the Command Status or Command
162 * Complete as with usual HCI command flow control.
163 *
164 * After sending the command, wait for BTMTKSDIO_TX_WAIT_VND_EVT
165 * state to be cleared. The driver specific event receive routine
166 * will clear that state and with that indicate completion of the
167 * WMT command.
168 */
169 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT,
170 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
171 if (err == -EINTR) {
172 bt_dev_err(hdev, "Execution of wmt command interrupted");
173 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
Sean Wang3a722042021-10-19 05:30:13 +0800174 goto err_free_wc;
Sean Wang9aebfd42019-03-08 09:15:44 +0800175 }
176
177 if (err) {
178 bt_dev_err(hdev, "Execution of wmt command timed out");
179 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
Sean Wang3a722042021-10-19 05:30:13 +0800180 err = -ETIMEDOUT;
181 goto err_free_wc;
Sean Wang9aebfd42019-03-08 09:15:44 +0800182 }
183
184 /* Parse and handle the return WMT event */
185 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
186 if (wmt_evt->whdr.op != hdr->op) {
187 bt_dev_err(hdev, "Wrong op received %d expected %d",
188 wmt_evt->whdr.op, hdr->op);
189 err = -EIO;
190 goto err_free_skb;
191 }
192
193 switch (wmt_evt->whdr.op) {
Sean Wang3a722042021-10-19 05:30:13 +0800194 case BTMTK_WMT_SEMAPHORE:
Sean Wang9aebfd42019-03-08 09:15:44 +0800195 if (wmt_evt->whdr.flag == 2)
196 status = BTMTK_WMT_PATCH_UNDONE;
197 else
198 status = BTMTK_WMT_PATCH_DONE;
199 break;
Sean Wang3a722042021-10-19 05:30:13 +0800200 case BTMTK_WMT_FUNC_CTRL:
Sean Wang9aebfd42019-03-08 09:15:44 +0800201 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
202 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
203 status = BTMTK_WMT_ON_DONE;
204 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
205 status = BTMTK_WMT_ON_PROGRESS;
206 else
207 status = BTMTK_WMT_ON_UNDONE;
208 break;
Sean Wangc603bf12021-10-19 05:30:21 +0800209 case BTMTK_WMT_PATCH_DWNLD:
210 if (wmt_evt->whdr.flag == 2)
211 status = BTMTK_WMT_PATCH_DONE;
212 else if (wmt_evt->whdr.flag == 1)
213 status = BTMTK_WMT_PATCH_PROGRESS;
214 else
215 status = BTMTK_WMT_PATCH_UNDONE;
216 break;
217 case BTMTK_WMT_REGISTER:
218 wmt_evt_reg = (struct btmtk_hci_wmt_evt_reg *)wmt_evt;
219 if (le16_to_cpu(wmt_evt->whdr.dlen) == 12)
220 status = le32_to_cpu(wmt_evt_reg->val);
221 break;
Sean Wang9aebfd42019-03-08 09:15:44 +0800222 }
223
224 if (wmt_params->status)
225 *wmt_params->status = status;
226
227err_free_skb:
228 kfree_skb(bdev->evt_skb);
229 bdev->evt_skb = NULL;
Sean Wang3a722042021-10-19 05:30:13 +0800230err_free_wc:
231 kfree(wc);
Sean Wang9aebfd42019-03-08 09:15:44 +0800232
233 return err;
234}
235
236static int btmtksdio_tx_packet(struct btmtksdio_dev *bdev,
237 struct sk_buff *skb)
238{
239 struct mtkbtsdio_hdr *sdio_hdr;
240 int err;
241
242 /* Make sure that there are enough rooms for SDIO header */
243 if (unlikely(skb_headroom(skb) < sizeof(*sdio_hdr))) {
244 err = pskb_expand_head(skb, sizeof(*sdio_hdr), 0,
245 GFP_ATOMIC);
246 if (err < 0)
247 return err;
248 }
249
250 /* Prepend MediaTek SDIO Specific Header */
251 skb_push(skb, sizeof(*sdio_hdr));
252
253 sdio_hdr = (void *)skb->data;
254 sdio_hdr->len = cpu_to_le16(skb->len);
255 sdio_hdr->reserved = cpu_to_le16(0);
256 sdio_hdr->bt_type = hci_skb_pkt_type(skb);
257
Sean Wangd555b1f2021-11-20 06:25:43 +0800258 clear_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
Sean Wang9aebfd42019-03-08 09:15:44 +0800259 err = sdio_writesb(bdev->func, MTK_REG_CTDR, skb->data,
260 round_up(skb->len, MTK_SDIO_BLOCK_SIZE));
261 if (err < 0)
262 goto err_skb_pull;
263
264 bdev->hdev->stat.byte_tx += skb->len;
265
266 kfree_skb(skb);
267
268 return 0;
269
270err_skb_pull:
271 skb_pull(skb, sizeof(*sdio_hdr));
272
273 return err;
274}
275
276static u32 btmtksdio_drv_own_query(struct btmtksdio_dev *bdev)
277{
278 return sdio_readl(bdev->func, MTK_REG_CHLPCR, NULL);
279}
280
Sean Wang9aebfd42019-03-08 09:15:44 +0800281static int btmtksdio_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
282{
283 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
284 struct hci_event_hdr *hdr = (void *)skb->data;
285 int err;
286
287 /* Fix up the vendor event id with 0xff for vendor specific instead
288 * of 0xe4 so that event send via monitoring socket can be parsed
289 * properly.
290 */
291 if (hdr->evt == 0xe4)
292 hdr->evt = HCI_EV_VENDOR;
293
294 /* When someone waits for the WMT event, the skb is being cloned
295 * and being processed the events from there then.
296 */
297 if (test_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state)) {
298 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
299 if (!bdev->evt_skb) {
300 err = -ENOMEM;
301 goto err_out;
302 }
303 }
304
305 err = hci_recv_frame(hdev, skb);
306 if (err < 0)
307 goto err_free_skb;
308
309 if (hdr->evt == HCI_EV_VENDOR) {
310 if (test_and_clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT,
311 &bdev->tx_state)) {
312 /* Barrier to sync with other CPUs */
313 smp_mb__after_atomic();
314 wake_up_bit(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT);
315 }
316 }
317
318 return 0;
319
320err_free_skb:
321 kfree_skb(bdev->evt_skb);
322 bdev->evt_skb = NULL;
323
324err_out:
325 return err;
326}
327
Sean Wangdb57b622021-11-20 06:25:46 +0800328static int btmtksdio_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
329{
330 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
331 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
332
333 switch (handle) {
334 case 0xfc6f:
335 /* Firmware dump from device: when the firmware hangs, the
336 * device can no longer suspend and thus disable auto-suspend.
337 */
338 pm_runtime_forbid(bdev->dev);
339 fallthrough;
340 case 0x05ff:
341 case 0x05fe:
342 /* Firmware debug logging */
343 return hci_recv_diag(hdev, skb);
344 }
345
346 return hci_recv_frame(hdev, skb);
347}
348
Sean Wang9aebfd42019-03-08 09:15:44 +0800349static const struct h4_recv_pkt mtk_recv_pkts[] = {
Sean Wangdb57b622021-11-20 06:25:46 +0800350 { H4_RECV_ACL, .recv = btmtksdio_recv_acl },
Sean Wang9aebfd42019-03-08 09:15:44 +0800351 { H4_RECV_SCO, .recv = hci_recv_frame },
352 { H4_RECV_EVENT, .recv = btmtksdio_recv_event },
353};
354
355static int btmtksdio_rx_packet(struct btmtksdio_dev *bdev, u16 rx_size)
356{
357 const struct h4_recv_pkt *pkts = mtk_recv_pkts;
358 int pkts_count = ARRAY_SIZE(mtk_recv_pkts);
359 struct mtkbtsdio_hdr *sdio_hdr;
Sean Wang9aebfd42019-03-08 09:15:44 +0800360 int err, i, pad_size;
361 struct sk_buff *skb;
362 u16 dlen;
363
364 if (rx_size < sizeof(*sdio_hdr))
365 return -EILSEQ;
366
367 /* A SDIO packet is exactly containing a Bluetooth packet */
368 skb = bt_skb_alloc(rx_size, GFP_KERNEL);
369 if (!skb)
370 return -ENOMEM;
371
372 skb_put(skb, rx_size);
373
374 err = sdio_readsb(bdev->func, skb->data, MTK_REG_CRDR, rx_size);
375 if (err < 0)
376 goto err_kfree_skb;
377
Sean Wang9aebfd42019-03-08 09:15:44 +0800378 sdio_hdr = (void *)skb->data;
379
380 /* We assume the default error as -EILSEQ simply to make the error path
381 * be cleaner.
382 */
383 err = -EILSEQ;
384
385 if (rx_size != le16_to_cpu(sdio_hdr->len)) {
386 bt_dev_err(bdev->hdev, "Rx size in sdio header is mismatched ");
387 goto err_kfree_skb;
388 }
389
390 hci_skb_pkt_type(skb) = sdio_hdr->bt_type;
391
392 /* Remove MediaTek SDIO header */
393 skb_pull(skb, sizeof(*sdio_hdr));
394
395 /* We have to dig into the packet to get payload size and then know how
396 * many padding bytes at the tail, these padding bytes should be removed
397 * before the packet is indicated to the core layer.
398 */
399 for (i = 0; i < pkts_count; i++) {
400 if (sdio_hdr->bt_type == (&pkts[i])->type)
401 break;
402 }
403
404 if (i >= pkts_count) {
405 bt_dev_err(bdev->hdev, "Invalid bt type 0x%02x",
406 sdio_hdr->bt_type);
407 goto err_kfree_skb;
408 }
409
410 /* Remaining bytes cannot hold a header*/
411 if (skb->len < (&pkts[i])->hlen) {
412 bt_dev_err(bdev->hdev, "The size of bt header is mismatched");
413 goto err_kfree_skb;
414 }
415
416 switch ((&pkts[i])->lsize) {
Jagdish Tirumala1ca2a3942020-12-15 15:17:30 +0530417 case 1:
418 dlen = skb->data[(&pkts[i])->loff];
419 break;
420 case 2:
421 dlen = get_unaligned_le16(skb->data +
Sean Wang9aebfd42019-03-08 09:15:44 +0800422 (&pkts[i])->loff);
Jagdish Tirumala1ca2a3942020-12-15 15:17:30 +0530423 break;
424 default:
425 goto err_kfree_skb;
Sean Wang9aebfd42019-03-08 09:15:44 +0800426 }
427
428 pad_size = skb->len - (&pkts[i])->hlen - dlen;
429
430 /* Remaining bytes cannot hold a payload */
431 if (pad_size < 0) {
432 bt_dev_err(bdev->hdev, "The size of bt payload is mismatched");
433 goto err_kfree_skb;
434 }
435
436 /* Remove padding bytes */
437 skb_trim(skb, skb->len - pad_size);
438
439 /* Complete frame */
440 (&pkts[i])->recv(bdev->hdev, skb);
441
Sean Wangbcaa7d72019-04-18 17:08:01 +0800442 bdev->hdev->stat.byte_rx += rx_size;
443
Sean Wang9aebfd42019-03-08 09:15:44 +0800444 return 0;
445
446err_kfree_skb:
Sean Wang9aebfd42019-03-08 09:15:44 +0800447 kfree_skb(skb);
448
449 return err;
450}
451
Sean Wang26270bc2021-10-19 05:30:16 +0800452static void btmtksdio_txrx_work(struct work_struct *work)
Sean Wang9aebfd42019-03-08 09:15:44 +0800453{
Sean Wang26270bc2021-10-19 05:30:16 +0800454 struct btmtksdio_dev *bdev = container_of(work, struct btmtksdio_dev,
455 txrx_work);
456 unsigned long txrx_timeout;
Sean Wang184ea402021-10-19 05:30:18 +0800457 u32 int_status, rx_size;
Sean Wang26270bc2021-10-19 05:30:16 +0800458 struct sk_buff *skb;
Sean Wang26270bc2021-10-19 05:30:16 +0800459 int err;
Sean Wang7f3c5632019-04-18 17:08:02 +0800460
461 pm_runtime_get_sync(bdev->dev);
462
463 sdio_claim_host(bdev->func);
464
Sean Wang9aebfd42019-03-08 09:15:44 +0800465 /* Disable interrupt */
Sean Wang26270bc2021-10-19 05:30:16 +0800466 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
Sean Wang9aebfd42019-03-08 09:15:44 +0800467
Sean Wang26270bc2021-10-19 05:30:16 +0800468 txrx_timeout = jiffies + 5 * HZ;
469
470 do {
471 int_status = sdio_readl(bdev->func, MTK_REG_CHISR, NULL);
472
473 /* Ack an interrupt as soon as possible before any operation on
474 * hardware.
475 *
476 * Note that we don't ack any status during operations to avoid race
477 * condition between the host and the device such as it's possible to
478 * mistakenly ack RX_DONE for the next packet and then cause interrupts
479 * not be raised again but there is still pending data in the hardware
480 * FIFO.
481 */
482 sdio_writel(bdev->func, int_status, MTK_REG_CHISR, NULL);
483
484 if (int_status & FW_OWN_BACK_INT)
485 bt_dev_dbg(bdev->hdev, "Get fw own back");
486
487 if (int_status & TX_EMPTY)
Sean Wangd555b1f2021-11-20 06:25:43 +0800488 set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
489
Sean Wang26270bc2021-10-19 05:30:16 +0800490 else if (unlikely(int_status & TX_FIFO_OVERFLOW))
491 bt_dev_warn(bdev->hdev, "Tx fifo overflow");
492
Sean Wangd555b1f2021-11-20 06:25:43 +0800493 if (test_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state)) {
Mark-yw Chen10fe40e2021-10-19 05:30:19 +0800494 skb = skb_dequeue(&bdev->txq);
495 if (skb) {
496 err = btmtksdio_tx_packet(bdev, skb);
497 if (err < 0) {
498 bdev->hdev->stat.err_tx++;
499 skb_queue_head(&bdev->txq, skb);
500 }
501 }
502 }
503
Sean Wang26270bc2021-10-19 05:30:16 +0800504 if (int_status & RX_DONE_INT) {
Sean Wang184ea402021-10-19 05:30:18 +0800505 rx_size = sdio_readl(bdev->func, MTK_REG_CRPLR, NULL);
506 rx_size = (rx_size & RX_PKT_LEN) >> 16;
Sean Wang26270bc2021-10-19 05:30:16 +0800507 if (btmtksdio_rx_packet(bdev, rx_size) < 0)
508 bdev->hdev->stat.err_rx++;
509 }
Sean Wang26270bc2021-10-19 05:30:16 +0800510 } while (int_status || time_is_before_jiffies(txrx_timeout));
511
Sean Wang9aebfd42019-03-08 09:15:44 +0800512 /* Enable interrupt */
Sean Wang26270bc2021-10-19 05:30:16 +0800513 sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, 0);
514
515 sdio_release_host(bdev->func);
Sean Wang7f3c5632019-04-18 17:08:02 +0800516
517 pm_runtime_mark_last_busy(bdev->dev);
518 pm_runtime_put_autosuspend(bdev->dev);
Sean Wang9aebfd42019-03-08 09:15:44 +0800519}
520
Sean Wang26270bc2021-10-19 05:30:16 +0800521static void btmtksdio_interrupt(struct sdio_func *func)
522{
523 struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
524
525 /* Disable interrupt */
526 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
527
528 schedule_work(&bdev->txrx_work);
529}
530
Sean Wang9aebfd42019-03-08 09:15:44 +0800531static int btmtksdio_open(struct hci_dev *hdev)
532{
533 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
Sean Wang77b210d2021-10-19 05:30:15 +0800534 u32 status, val;
Sean Wang9aebfd42019-03-08 09:15:44 +0800535 int err;
Sean Wang9aebfd42019-03-08 09:15:44 +0800536
537 sdio_claim_host(bdev->func);
538
539 err = sdio_enable_func(bdev->func);
540 if (err < 0)
541 goto err_release_host;
542
Sean Wang4b4b2222021-12-02 02:02:46 +0800543 set_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
544
Sean Wang9aebfd42019-03-08 09:15:44 +0800545 /* Get ownership from the device */
546 sdio_writel(bdev->func, C_FW_OWN_REQ_CLR, MTK_REG_CHLPCR, &err);
547 if (err < 0)
548 goto err_disable_func;
549
550 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
Sean Wang2e47cc22019-04-18 17:08:00 +0800551 status & C_COM_DRV_OWN, 2000, 1000000);
Sean Wang9aebfd42019-03-08 09:15:44 +0800552 if (err < 0) {
553 bt_dev_err(bdev->hdev, "Cannot get ownership from device");
554 goto err_disable_func;
555 }
556
557 /* Disable interrupt & mask out all interrupt sources */
558 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, &err);
559 if (err < 0)
560 goto err_disable_func;
561
562 sdio_writel(bdev->func, 0, MTK_REG_CHIER, &err);
563 if (err < 0)
564 goto err_disable_func;
565
566 err = sdio_claim_irq(bdev->func, btmtksdio_interrupt);
567 if (err < 0)
568 goto err_disable_func;
569
570 err = sdio_set_block_size(bdev->func, MTK_SDIO_BLOCK_SIZE);
571 if (err < 0)
572 goto err_release_irq;
573
574 /* SDIO CMD 5 allows the SDIO device back to idle state an
575 * synchronous interrupt is supported in SDIO 4-bit mode
576 */
Sean Wang5b23ac12021-10-19 05:30:17 +0800577 val = sdio_readl(bdev->func, MTK_REG_CSDIOCSR, &err);
578 if (err < 0)
579 goto err_release_irq;
580
581 val |= SDIO_INT_CTL;
582 sdio_writel(bdev->func, val, MTK_REG_CSDIOCSR, &err);
Sean Wang9aebfd42019-03-08 09:15:44 +0800583 if (err < 0)
584 goto err_release_irq;
585
Sean Wang77b210d2021-10-19 05:30:15 +0800586 /* Explitly set write-1-clear method */
587 val = sdio_readl(bdev->func, MTK_REG_CHCR, &err);
588 if (err < 0)
589 goto err_release_irq;
590
591 val |= C_INT_CLR_CTRL;
592 sdio_writel(bdev->func, val, MTK_REG_CHCR, &err);
Sean Wang9aebfd42019-03-08 09:15:44 +0800593 if (err < 0)
594 goto err_release_irq;
595
596 /* Setup interrupt sources */
597 sdio_writel(bdev->func, RX_DONE_INT | TX_EMPTY | TX_FIFO_OVERFLOW,
598 MTK_REG_CHIER, &err);
599 if (err < 0)
600 goto err_release_irq;
601
602 /* Enable interrupt */
603 sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, &err);
604 if (err < 0)
605 goto err_release_irq;
606
607 sdio_release_host(bdev->func);
608
609 return 0;
610
611err_release_irq:
612 sdio_release_irq(bdev->func);
613
614err_disable_func:
615 sdio_disable_func(bdev->func);
616
617err_release_host:
618 sdio_release_host(bdev->func);
619
620 return err;
621}
622
623static int btmtksdio_close(struct hci_dev *hdev)
624{
625 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
626 u32 status;
627 int err;
628
629 sdio_claim_host(bdev->func);
630
631 /* Disable interrupt */
632 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL);
633
634 sdio_release_irq(bdev->func);
635
Sean Wang26270bc2021-10-19 05:30:16 +0800636 cancel_work_sync(&bdev->txrx_work);
637
Sean Wang9aebfd42019-03-08 09:15:44 +0800638 /* Return ownership to the device */
639 sdio_writel(bdev->func, C_FW_OWN_REQ_SET, MTK_REG_CHLPCR, NULL);
640
641 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
Sean Wang2e47cc22019-04-18 17:08:00 +0800642 !(status & C_COM_DRV_OWN), 2000, 1000000);
Sean Wang9aebfd42019-03-08 09:15:44 +0800643 if (err < 0)
644 bt_dev_err(bdev->hdev, "Cannot return ownership to device");
645
Sean Wang4b4b2222021-12-02 02:02:46 +0800646 clear_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
Sean Wang9aebfd42019-03-08 09:15:44 +0800647 sdio_disable_func(bdev->func);
648
649 sdio_release_host(bdev->func);
650
651 return 0;
652}
653
654static int btmtksdio_flush(struct hci_dev *hdev)
655{
656 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
657
658 skb_queue_purge(&bdev->txq);
659
Sean Wang26270bc2021-10-19 05:30:16 +0800660 cancel_work_sync(&bdev->txrx_work);
Sean Wang9aebfd42019-03-08 09:15:44 +0800661
662 return 0;
663}
664
665static int btmtksdio_func_query(struct hci_dev *hdev)
666{
667 struct btmtk_hci_wmt_params wmt_params;
668 int status, err;
669 u8 param = 0;
670
671 /* Query whether the function is enabled */
Sean Wang3a722042021-10-19 05:30:13 +0800672 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
Sean Wang9aebfd42019-03-08 09:15:44 +0800673 wmt_params.flag = 4;
674 wmt_params.dlen = sizeof(param);
675 wmt_params.data = &param;
676 wmt_params.status = &status;
677
678 err = mtk_hci_wmt_sync(hdev, &wmt_params);
679 if (err < 0) {
680 bt_dev_err(hdev, "Failed to query function status (%d)", err);
681 return err;
682 }
683
684 return status;
685}
686
Sean Wangc603bf12021-10-19 05:30:21 +0800687static int mt76xx_setup(struct hci_dev *hdev, const char *fwname)
Sean Wang9aebfd42019-03-08 09:15:44 +0800688{
Sean Wang9aebfd42019-03-08 09:15:44 +0800689 struct btmtk_hci_wmt_params wmt_params;
Sean Wang9aebfd42019-03-08 09:15:44 +0800690 struct btmtk_tci_sleep tci_sleep;
Sean Wang9aebfd42019-03-08 09:15:44 +0800691 struct sk_buff *skb;
692 int err, status;
693 u8 param = 0x1;
694
Sean Wang9aebfd42019-03-08 09:15:44 +0800695 /* Query whether the firmware is already download */
Sean Wang3a722042021-10-19 05:30:13 +0800696 wmt_params.op = BTMTK_WMT_SEMAPHORE;
Sean Wang9aebfd42019-03-08 09:15:44 +0800697 wmt_params.flag = 1;
698 wmt_params.dlen = 0;
699 wmt_params.data = NULL;
700 wmt_params.status = &status;
701
702 err = mtk_hci_wmt_sync(hdev, &wmt_params);
703 if (err < 0) {
704 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
705 return err;
706 }
707
708 if (status == BTMTK_WMT_PATCH_DONE) {
709 bt_dev_info(hdev, "Firmware already downloaded");
710 goto ignore_setup_fw;
711 }
712
713 /* Setup a firmware which the device definitely requires */
Sean Wangc603bf12021-10-19 05:30:21 +0800714 err = btmtk_setup_firmware(hdev, fwname, mtk_hci_wmt_sync);
Sean Wang9aebfd42019-03-08 09:15:44 +0800715 if (err < 0)
716 return err;
717
718ignore_setup_fw:
719 /* Query whether the device is already enabled */
720 err = readx_poll_timeout(btmtksdio_func_query, hdev, status,
721 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
722 2000, 5000000);
723 /* -ETIMEDOUT happens */
724 if (err < 0)
725 return err;
726
727 /* The other errors happen in btusb_mtk_func_query */
728 if (status < 0)
729 return status;
730
731 if (status == BTMTK_WMT_ON_DONE) {
732 bt_dev_info(hdev, "function already on");
733 goto ignore_func_on;
734 }
735
736 /* Enable Bluetooth protocol */
Sean Wang3a722042021-10-19 05:30:13 +0800737 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
Sean Wang9aebfd42019-03-08 09:15:44 +0800738 wmt_params.flag = 0;
739 wmt_params.dlen = sizeof(param);
740 wmt_params.data = &param;
741 wmt_params.status = NULL;
742
743 err = mtk_hci_wmt_sync(hdev, &wmt_params);
744 if (err < 0) {
745 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
746 return err;
747 }
748
749ignore_func_on:
750 /* Apply the low power environment setup */
751 tci_sleep.mode = 0x5;
752 tci_sleep.duration = cpu_to_le16(0x640);
753 tci_sleep.host_duration = cpu_to_le16(0x640);
754 tci_sleep.host_wakeup_pin = 0;
755 tci_sleep.time_compensation = 0;
756
757 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
758 HCI_INIT_TIMEOUT);
759 if (IS_ERR(skb)) {
760 err = PTR_ERR(skb);
761 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
762 return err;
763 }
764 kfree_skb(skb);
765
Sean Wangc603bf12021-10-19 05:30:21 +0800766 return 0;
767}
768
769static int mt79xx_setup(struct hci_dev *hdev, const char *fwname)
770{
771 struct btmtk_hci_wmt_params wmt_params;
772 u8 param = 0x1;
773 int err;
774
775 err = btmtk_setup_firmware_79xx(hdev, fwname, mtk_hci_wmt_sync);
776 if (err < 0) {
777 bt_dev_err(hdev, "Failed to setup 79xx firmware (%d)", err);
778 return err;
779 }
780
781 /* Enable Bluetooth protocol */
782 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
783 wmt_params.flag = 0;
784 wmt_params.dlen = sizeof(param);
785 wmt_params.data = &param;
786 wmt_params.status = NULL;
787
788 err = mtk_hci_wmt_sync(hdev, &wmt_params);
789 if (err < 0) {
790 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
791 return err;
792 }
793
Łukasz Bartosik630491f2021-12-01 21:13:51 +0100794 hci_set_msft_opcode(hdev, 0xFD30);
795
Sean Wangc603bf12021-10-19 05:30:21 +0800796 return err;
797}
798
799static int btsdio_mtk_reg_read(struct hci_dev *hdev, u32 reg, u32 *val)
800{
801 struct btmtk_hci_wmt_params wmt_params;
802 struct reg_read_cmd {
803 u8 type;
804 u8 rsv;
805 u8 num;
806 __le32 addr;
807 } __packed reg_read = {
808 .type = 1,
809 .num = 1,
810 };
811 u32 status;
812 int err;
813
814 reg_read.addr = cpu_to_le32(reg);
815 wmt_params.op = BTMTK_WMT_REGISTER;
816 wmt_params.flag = BTMTK_WMT_REG_READ;
817 wmt_params.dlen = sizeof(reg_read);
818 wmt_params.data = &reg_read;
819 wmt_params.status = &status;
820
821 err = mtk_hci_wmt_sync(hdev, &wmt_params);
822 if (err < 0) {
823 bt_dev_err(hdev, "Failed to read reg(%d)", err);
824 return err;
825 }
826
827 *val = status;
828
829 return err;
830}
831
832static int btmtksdio_setup(struct hci_dev *hdev)
833{
834 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
835 ktime_t calltime, delta, rettime;
836 unsigned long long duration;
837 char fwname[64];
838 int err, dev_id;
839 u32 fw_version = 0;
840
841 calltime = ktime_get();
Sean Wangd555b1f2021-11-20 06:25:43 +0800842 set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
Sean Wangc603bf12021-10-19 05:30:21 +0800843
844 switch (bdev->data->chipid) {
845 case 0x7921:
846 err = btsdio_mtk_reg_read(hdev, 0x70010200, &dev_id);
847 if (err < 0) {
848 bt_dev_err(hdev, "Failed to get device id (%d)", err);
849 return err;
850 }
851
852 err = btsdio_mtk_reg_read(hdev, 0x80021004, &fw_version);
853 if (err < 0) {
854 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
855 return err;
856 }
857
858 snprintf(fwname, sizeof(fwname),
859 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
860 dev_id & 0xffff, (fw_version & 0xff) + 1);
861 err = mt79xx_setup(hdev, fwname);
862 if (err < 0)
863 return err;
864 break;
865 case 0x7663:
866 case 0x7668:
867 err = mt76xx_setup(hdev, bdev->data->fwname);
868 if (err < 0)
869 return err;
870 break;
871 default:
872 return -ENODEV;
873 }
874
Sean Wang9aebfd42019-03-08 09:15:44 +0800875 rettime = ktime_get();
876 delta = ktime_sub(rettime, calltime);
877 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
878
Sean Wang7f3c5632019-04-18 17:08:02 +0800879 pm_runtime_set_autosuspend_delay(bdev->dev,
880 MTKBTSDIO_AUTOSUSPEND_DELAY);
881 pm_runtime_use_autosuspend(bdev->dev);
882
883 err = pm_runtime_set_active(bdev->dev);
884 if (err < 0)
885 return err;
886
887 /* Default forbid runtime auto suspend, that can be allowed by
888 * enable_autosuspend flag or the PM runtime entry under sysfs.
889 */
890 pm_runtime_forbid(bdev->dev);
891 pm_runtime_enable(bdev->dev);
892
893 if (enable_autosuspend)
894 pm_runtime_allow(bdev->dev);
895
Sean Wang9aebfd42019-03-08 09:15:44 +0800896 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
897
898 return 0;
899}
900
901static int btmtksdio_shutdown(struct hci_dev *hdev)
902{
Sean Wang7f3c5632019-04-18 17:08:02 +0800903 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
Sean Wang9aebfd42019-03-08 09:15:44 +0800904 struct btmtk_hci_wmt_params wmt_params;
905 u8 param = 0x0;
906 int err;
907
Sean Wang7f3c5632019-04-18 17:08:02 +0800908 /* Get back the state to be consistent with the state
909 * in btmtksdio_setup.
910 */
911 pm_runtime_get_sync(bdev->dev);
912
Sean Wang9aebfd42019-03-08 09:15:44 +0800913 /* Disable the device */
Sean Wang3a722042021-10-19 05:30:13 +0800914 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
Sean Wang9aebfd42019-03-08 09:15:44 +0800915 wmt_params.flag = 0;
916 wmt_params.dlen = sizeof(param);
917 wmt_params.data = &param;
918 wmt_params.status = NULL;
919
920 err = mtk_hci_wmt_sync(hdev, &wmt_params);
921 if (err < 0) {
922 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
923 return err;
924 }
925
Sean Wang7f3c5632019-04-18 17:08:02 +0800926 pm_runtime_put_noidle(bdev->dev);
927 pm_runtime_disable(bdev->dev);
928
Sean Wang9aebfd42019-03-08 09:15:44 +0800929 return 0;
930}
931
932static int btmtksdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
933{
934 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
935
936 switch (hci_skb_pkt_type(skb)) {
937 case HCI_COMMAND_PKT:
938 hdev->stat.cmd_tx++;
939 break;
940
941 case HCI_ACLDATA_PKT:
942 hdev->stat.acl_tx++;
943 break;
944
945 case HCI_SCODATA_PKT:
946 hdev->stat.sco_tx++;
947 break;
948
949 default:
950 return -EILSEQ;
951 }
952
953 skb_queue_tail(&bdev->txq, skb);
954
Sean Wang26270bc2021-10-19 05:30:16 +0800955 schedule_work(&bdev->txrx_work);
Sean Wang9aebfd42019-03-08 09:15:44 +0800956
957 return 0;
958}
959
960static int btmtksdio_probe(struct sdio_func *func,
961 const struct sdio_device_id *id)
962{
963 struct btmtksdio_dev *bdev;
964 struct hci_dev *hdev;
965 int err;
966
967 bdev = devm_kzalloc(&func->dev, sizeof(*bdev), GFP_KERNEL);
968 if (!bdev)
969 return -ENOMEM;
970
971 bdev->data = (void *)id->driver_data;
972 if (!bdev->data)
973 return -ENODEV;
974
Sean Wang7f3c5632019-04-18 17:08:02 +0800975 bdev->dev = &func->dev;
Sean Wang9aebfd42019-03-08 09:15:44 +0800976 bdev->func = func;
977
Sean Wang26270bc2021-10-19 05:30:16 +0800978 INIT_WORK(&bdev->txrx_work, btmtksdio_txrx_work);
Sean Wang9aebfd42019-03-08 09:15:44 +0800979 skb_queue_head_init(&bdev->txq);
980
981 /* Initialize and register HCI device */
982 hdev = hci_alloc_dev();
983 if (!hdev) {
984 dev_err(&func->dev, "Can't allocate HCI device\n");
985 return -ENOMEM;
986 }
987
988 bdev->hdev = hdev;
989
990 hdev->bus = HCI_SDIO;
991 hci_set_drvdata(hdev, bdev);
992
993 hdev->open = btmtksdio_open;
994 hdev->close = btmtksdio_close;
995 hdev->flush = btmtksdio_flush;
996 hdev->setup = btmtksdio_setup;
997 hdev->shutdown = btmtksdio_shutdown;
998 hdev->send = btmtksdio_send_frame;
Sean Wang877ec9e2021-10-19 05:30:14 +0800999 hdev->set_bdaddr = btmtk_set_bdaddr;
1000
Sean Wang9aebfd42019-03-08 09:15:44 +08001001 SET_HCIDEV_DEV(hdev, &func->dev);
1002
1003 hdev->manufacturer = 70;
1004 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1005
1006 err = hci_register_dev(hdev);
1007 if (err < 0) {
1008 dev_err(&func->dev, "Can't register HCI device\n");
1009 hci_free_dev(hdev);
1010 return err;
1011 }
1012
1013 sdio_set_drvdata(func, bdev);
1014
Sean Wang7f3c5632019-04-18 17:08:02 +08001015 /* pm_runtime_enable would be done after the firmware is being
1016 * downloaded because the core layer probably already enables
1017 * runtime PM for this func such as the case host->caps &
1018 * MMC_CAP_POWER_OFF_CARD.
1019 */
1020 if (pm_runtime_enabled(bdev->dev))
1021 pm_runtime_disable(bdev->dev);
1022
1023 /* As explaination in drivers/mmc/core/sdio_bus.c tells us:
1024 * Unbound SDIO functions are always suspended.
1025 * During probe, the function is set active and the usage count
1026 * is incremented. If the driver supports runtime PM,
1027 * it should call pm_runtime_put_noidle() in its probe routine and
1028 * pm_runtime_get_noresume() in its remove routine.
1029 *
1030 * So, put a pm_runtime_put_noidle here !
1031 */
1032 pm_runtime_put_noidle(bdev->dev);
1033
Sean Wang9aebfd42019-03-08 09:15:44 +08001034 return 0;
1035}
1036
1037static void btmtksdio_remove(struct sdio_func *func)
1038{
1039 struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
1040 struct hci_dev *hdev;
1041
1042 if (!bdev)
1043 return;
1044
Sean Wang7f3c5632019-04-18 17:08:02 +08001045 /* Be consistent the state in btmtksdio_probe */
1046 pm_runtime_get_noresume(bdev->dev);
1047
Sean Wang9aebfd42019-03-08 09:15:44 +08001048 hdev = bdev->hdev;
1049
1050 sdio_set_drvdata(func, NULL);
1051 hci_unregister_dev(hdev);
1052 hci_free_dev(hdev);
1053}
1054
Sean Wang7f3c5632019-04-18 17:08:02 +08001055#ifdef CONFIG_PM
1056static int btmtksdio_runtime_suspend(struct device *dev)
1057{
1058 struct sdio_func *func = dev_to_sdio_func(dev);
1059 struct btmtksdio_dev *bdev;
1060 u32 status;
1061 int err;
1062
1063 bdev = sdio_get_drvdata(func);
1064 if (!bdev)
1065 return 0;
1066
Sean Wang4b4b2222021-12-02 02:02:46 +08001067 if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1068 return 0;
1069
Sean Wang561ae1d2021-12-02 02:02:47 +08001070 sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1071
Sean Wang7f3c5632019-04-18 17:08:02 +08001072 sdio_claim_host(bdev->func);
1073
1074 sdio_writel(bdev->func, C_FW_OWN_REQ_SET, MTK_REG_CHLPCR, &err);
1075 if (err < 0)
1076 goto out;
1077
1078 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
1079 !(status & C_COM_DRV_OWN), 2000, 1000000);
1080out:
1081 bt_dev_info(bdev->hdev, "status (%d) return ownership to device", err);
1082
1083 sdio_release_host(bdev->func);
1084
1085 return err;
1086}
1087
1088static int btmtksdio_runtime_resume(struct device *dev)
1089{
1090 struct sdio_func *func = dev_to_sdio_func(dev);
1091 struct btmtksdio_dev *bdev;
1092 u32 status;
1093 int err;
1094
1095 bdev = sdio_get_drvdata(func);
1096 if (!bdev)
1097 return 0;
1098
Sean Wang4b4b2222021-12-02 02:02:46 +08001099 if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1100 return 0;
1101
Sean Wang7f3c5632019-04-18 17:08:02 +08001102 sdio_claim_host(bdev->func);
1103
1104 sdio_writel(bdev->func, C_FW_OWN_REQ_CLR, MTK_REG_CHLPCR, &err);
1105 if (err < 0)
1106 goto out;
1107
1108 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
1109 status & C_COM_DRV_OWN, 2000, 1000000);
1110out:
1111 bt_dev_info(bdev->hdev, "status (%d) get ownership from device", err);
1112
1113 sdio_release_host(bdev->func);
1114
1115 return err;
1116}
1117
1118static UNIVERSAL_DEV_PM_OPS(btmtksdio_pm_ops, btmtksdio_runtime_suspend,
1119 btmtksdio_runtime_resume, NULL);
1120#define BTMTKSDIO_PM_OPS (&btmtksdio_pm_ops)
1121#else /* CONFIG_PM */
1122#define BTMTKSDIO_PM_OPS NULL
1123#endif /* CONFIG_PM */
1124
Sean Wang9aebfd42019-03-08 09:15:44 +08001125static struct sdio_driver btmtksdio_driver = {
1126 .name = "btmtksdio",
1127 .probe = btmtksdio_probe,
1128 .remove = btmtksdio_remove,
1129 .id_table = btmtksdio_table,
Sean Wang7f3c5632019-04-18 17:08:02 +08001130 .drv = {
1131 .owner = THIS_MODULE,
1132 .pm = BTMTKSDIO_PM_OPS,
1133 }
Sean Wang9aebfd42019-03-08 09:15:44 +08001134};
1135
Sean Wanga6094a42019-03-14 05:01:59 +08001136module_sdio_driver(btmtksdio_driver);
Sean Wang9aebfd42019-03-08 09:15:44 +08001137
Sean Wang7f3c5632019-04-18 17:08:02 +08001138module_param(enable_autosuspend, bool, 0644);
1139MODULE_PARM_DESC(enable_autosuspend, "Enable autosuspend by default");
1140
Sean Wang9aebfd42019-03-08 09:15:44 +08001141MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1142MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION);
1143MODULE_VERSION(VERSION);
1144MODULE_LICENSE("GPL");