Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 1 | // 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> |
| 15 | #include <linux/firmware.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/iopoll.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #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" |
| 30 | |
| 31 | #define VERSION "0.1" |
| 32 | |
| 33 | #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin" |
| 34 | #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin" |
| 35 | |
| 36 | struct btmtksdio_data { |
| 37 | const char *fwname; |
| 38 | }; |
| 39 | |
| 40 | static const struct btmtksdio_data mt7663_data = { |
| 41 | .fwname = FIRMWARE_MT7663, |
| 42 | }; |
| 43 | |
| 44 | static const struct btmtksdio_data mt7668_data = { |
| 45 | .fwname = FIRMWARE_MT7668, |
| 46 | }; |
| 47 | |
| 48 | static const struct sdio_device_id btmtksdio_table[] = { |
| 49 | {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, 0x7663), |
| 50 | .driver_data = (kernel_ulong_t)&mt7663_data }, |
| 51 | {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, 0x7668), |
| 52 | .driver_data = (kernel_ulong_t)&mt7668_data }, |
| 53 | { } /* Terminating entry */ |
| 54 | }; |
| 55 | |
| 56 | #define MTK_REG_CHLPCR 0x4 /* W1S */ |
| 57 | #define C_INT_EN_SET BIT(0) |
| 58 | #define C_INT_EN_CLR BIT(1) |
| 59 | #define C_FW_OWN_REQ_SET BIT(8) |
| 60 | #define C_FW_OWN_REQ_CLR BIT(9) |
| 61 | |
| 62 | #define MTK_REG_CSDIOCSR 0x8 |
| 63 | #define SDIO_RE_INIT_EN BIT(0) |
| 64 | #define SDIO_INT_CTL BIT(2) |
| 65 | |
| 66 | #define MTK_REG_CHCR 0xc |
| 67 | #define C_INT_CLR_CTRL BIT(1) |
| 68 | |
| 69 | /* CHISR have the same bits field definition with CHIER */ |
| 70 | #define MTK_REG_CHISR 0x10 |
| 71 | #define MTK_REG_CHIER 0x14 |
| 72 | #define FW_OWN_BACK_INT BIT(0) |
| 73 | #define RX_DONE_INT BIT(1) |
| 74 | #define TX_EMPTY BIT(2) |
| 75 | #define TX_FIFO_OVERFLOW BIT(8) |
| 76 | #define RX_PKT_LEN GENMASK(31, 16) |
| 77 | |
| 78 | #define MTK_REG_CTDR 0x18 |
| 79 | |
| 80 | #define MTK_REG_CRDR 0x1c |
| 81 | |
| 82 | #define MTK_SDIO_BLOCK_SIZE 256 |
| 83 | |
| 84 | #define BTMTKSDIO_TX_WAIT_VND_EVT 1 |
| 85 | |
| 86 | enum { |
| 87 | MTK_WMT_PATCH_DWNLD = 0x1, |
| 88 | MTK_WMT_TEST = 0x2, |
| 89 | MTK_WMT_WAKEUP = 0x3, |
| 90 | MTK_WMT_HIF = 0x4, |
| 91 | MTK_WMT_FUNC_CTRL = 0x6, |
| 92 | MTK_WMT_RST = 0x7, |
| 93 | MTK_WMT_SEMAPHORE = 0x17, |
| 94 | }; |
| 95 | |
| 96 | enum { |
| 97 | BTMTK_WMT_INVALID, |
| 98 | BTMTK_WMT_PATCH_UNDONE, |
| 99 | BTMTK_WMT_PATCH_DONE, |
| 100 | BTMTK_WMT_ON_UNDONE, |
| 101 | BTMTK_WMT_ON_DONE, |
| 102 | BTMTK_WMT_ON_PROGRESS, |
| 103 | }; |
| 104 | |
| 105 | struct mtkbtsdio_hdr { |
| 106 | __le16 len; |
| 107 | __le16 reserved; |
| 108 | u8 bt_type; |
| 109 | } __packed; |
| 110 | |
| 111 | struct mtk_wmt_hdr { |
| 112 | u8 dir; |
| 113 | u8 op; |
| 114 | __le16 dlen; |
| 115 | u8 flag; |
| 116 | } __packed; |
| 117 | |
| 118 | struct mtk_hci_wmt_cmd { |
| 119 | struct mtk_wmt_hdr hdr; |
| 120 | u8 data[256]; |
| 121 | } __packed; |
| 122 | |
| 123 | struct btmtk_hci_wmt_evt { |
| 124 | struct hci_event_hdr hhdr; |
| 125 | struct mtk_wmt_hdr whdr; |
| 126 | } __packed; |
| 127 | |
| 128 | struct btmtk_hci_wmt_evt_funcc { |
| 129 | struct btmtk_hci_wmt_evt hwhdr; |
| 130 | __be16 status; |
| 131 | } __packed; |
| 132 | |
| 133 | struct btmtk_tci_sleep { |
| 134 | u8 mode; |
| 135 | __le16 duration; |
| 136 | __le16 host_duration; |
| 137 | u8 host_wakeup_pin; |
| 138 | u8 time_compensation; |
| 139 | } __packed; |
| 140 | |
| 141 | struct btmtk_hci_wmt_params { |
| 142 | u8 op; |
| 143 | u8 flag; |
| 144 | u16 dlen; |
| 145 | const void *data; |
| 146 | u32 *status; |
| 147 | }; |
| 148 | |
| 149 | struct btmtksdio_dev { |
| 150 | struct hci_dev *hdev; |
| 151 | struct sdio_func *func; |
| 152 | |
| 153 | struct work_struct tx_work; |
| 154 | unsigned long tx_state; |
| 155 | struct sk_buff_head txq; |
| 156 | |
| 157 | struct sk_buff *evt_skb; |
| 158 | |
| 159 | const struct btmtksdio_data *data; |
| 160 | }; |
| 161 | |
| 162 | static int mtk_hci_wmt_sync(struct hci_dev *hdev, |
| 163 | struct btmtk_hci_wmt_params *wmt_params) |
| 164 | { |
| 165 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 166 | struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc; |
| 167 | u32 hlen, status = BTMTK_WMT_INVALID; |
| 168 | struct btmtk_hci_wmt_evt *wmt_evt; |
| 169 | struct mtk_hci_wmt_cmd wc; |
| 170 | struct mtk_wmt_hdr *hdr; |
| 171 | int err; |
| 172 | |
| 173 | hlen = sizeof(*hdr) + wmt_params->dlen; |
| 174 | if (hlen > 255) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | hdr = (struct mtk_wmt_hdr *)&wc; |
| 178 | hdr->dir = 1; |
| 179 | hdr->op = wmt_params->op; |
| 180 | hdr->dlen = cpu_to_le16(wmt_params->dlen + 1); |
| 181 | hdr->flag = wmt_params->flag; |
| 182 | memcpy(wc.data, wmt_params->data, wmt_params->dlen); |
| 183 | |
| 184 | set_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state); |
| 185 | |
| 186 | err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc); |
| 187 | if (err < 0) { |
| 188 | clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state); |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | /* The vendor specific WMT commands are all answered by a vendor |
| 193 | * specific event and will not have the Command Status or Command |
| 194 | * Complete as with usual HCI command flow control. |
| 195 | * |
| 196 | * After sending the command, wait for BTMTKSDIO_TX_WAIT_VND_EVT |
| 197 | * state to be cleared. The driver specific event receive routine |
| 198 | * will clear that state and with that indicate completion of the |
| 199 | * WMT command. |
| 200 | */ |
| 201 | err = wait_on_bit_timeout(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT, |
| 202 | TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT); |
| 203 | if (err == -EINTR) { |
| 204 | bt_dev_err(hdev, "Execution of wmt command interrupted"); |
| 205 | clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state); |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | if (err) { |
| 210 | bt_dev_err(hdev, "Execution of wmt command timed out"); |
| 211 | clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state); |
| 212 | return -ETIMEDOUT; |
| 213 | } |
| 214 | |
| 215 | /* Parse and handle the return WMT event */ |
| 216 | wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data; |
| 217 | if (wmt_evt->whdr.op != hdr->op) { |
| 218 | bt_dev_err(hdev, "Wrong op received %d expected %d", |
| 219 | wmt_evt->whdr.op, hdr->op); |
| 220 | err = -EIO; |
| 221 | goto err_free_skb; |
| 222 | } |
| 223 | |
| 224 | switch (wmt_evt->whdr.op) { |
| 225 | case MTK_WMT_SEMAPHORE: |
| 226 | if (wmt_evt->whdr.flag == 2) |
| 227 | status = BTMTK_WMT_PATCH_UNDONE; |
| 228 | else |
| 229 | status = BTMTK_WMT_PATCH_DONE; |
| 230 | break; |
| 231 | case MTK_WMT_FUNC_CTRL: |
| 232 | wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt; |
| 233 | if (be16_to_cpu(wmt_evt_funcc->status) == 0x404) |
| 234 | status = BTMTK_WMT_ON_DONE; |
| 235 | else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420) |
| 236 | status = BTMTK_WMT_ON_PROGRESS; |
| 237 | else |
| 238 | status = BTMTK_WMT_ON_UNDONE; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | if (wmt_params->status) |
| 243 | *wmt_params->status = status; |
| 244 | |
| 245 | err_free_skb: |
| 246 | kfree_skb(bdev->evt_skb); |
| 247 | bdev->evt_skb = NULL; |
| 248 | |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | static int btmtksdio_tx_packet(struct btmtksdio_dev *bdev, |
| 253 | struct sk_buff *skb) |
| 254 | { |
| 255 | struct mtkbtsdio_hdr *sdio_hdr; |
| 256 | int err; |
| 257 | |
| 258 | /* Make sure that there are enough rooms for SDIO header */ |
| 259 | if (unlikely(skb_headroom(skb) < sizeof(*sdio_hdr))) { |
| 260 | err = pskb_expand_head(skb, sizeof(*sdio_hdr), 0, |
| 261 | GFP_ATOMIC); |
| 262 | if (err < 0) |
| 263 | return err; |
| 264 | } |
| 265 | |
| 266 | /* Prepend MediaTek SDIO Specific Header */ |
| 267 | skb_push(skb, sizeof(*sdio_hdr)); |
| 268 | |
| 269 | sdio_hdr = (void *)skb->data; |
| 270 | sdio_hdr->len = cpu_to_le16(skb->len); |
| 271 | sdio_hdr->reserved = cpu_to_le16(0); |
| 272 | sdio_hdr->bt_type = hci_skb_pkt_type(skb); |
| 273 | |
| 274 | err = sdio_writesb(bdev->func, MTK_REG_CTDR, skb->data, |
| 275 | round_up(skb->len, MTK_SDIO_BLOCK_SIZE)); |
| 276 | if (err < 0) |
| 277 | goto err_skb_pull; |
| 278 | |
| 279 | bdev->hdev->stat.byte_tx += skb->len; |
| 280 | |
| 281 | kfree_skb(skb); |
| 282 | |
| 283 | return 0; |
| 284 | |
| 285 | err_skb_pull: |
| 286 | skb_pull(skb, sizeof(*sdio_hdr)); |
| 287 | |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | static u32 btmtksdio_drv_own_query(struct btmtksdio_dev *bdev) |
| 292 | { |
| 293 | return sdio_readl(bdev->func, MTK_REG_CHLPCR, NULL); |
| 294 | } |
| 295 | |
| 296 | static void btmtksdio_tx_work(struct work_struct *work) |
| 297 | { |
| 298 | struct btmtksdio_dev *bdev = container_of(work, struct btmtksdio_dev, |
| 299 | tx_work); |
| 300 | struct sk_buff *skb; |
| 301 | int err; |
| 302 | |
| 303 | sdio_claim_host(bdev->func); |
| 304 | |
| 305 | while ((skb = skb_dequeue(&bdev->txq))) { |
| 306 | err = btmtksdio_tx_packet(bdev, skb); |
| 307 | if (err < 0) { |
| 308 | bdev->hdev->stat.err_tx++; |
| 309 | skb_queue_head(&bdev->txq, skb); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | sdio_release_host(bdev->func); |
| 315 | } |
| 316 | |
| 317 | static int btmtksdio_recv_event(struct hci_dev *hdev, struct sk_buff *skb) |
| 318 | { |
| 319 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 320 | struct hci_event_hdr *hdr = (void *)skb->data; |
| 321 | int err; |
| 322 | |
| 323 | /* Fix up the vendor event id with 0xff for vendor specific instead |
| 324 | * of 0xe4 so that event send via monitoring socket can be parsed |
| 325 | * properly. |
| 326 | */ |
| 327 | if (hdr->evt == 0xe4) |
| 328 | hdr->evt = HCI_EV_VENDOR; |
| 329 | |
| 330 | /* When someone waits for the WMT event, the skb is being cloned |
| 331 | * and being processed the events from there then. |
| 332 | */ |
| 333 | if (test_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state)) { |
| 334 | bdev->evt_skb = skb_clone(skb, GFP_KERNEL); |
| 335 | if (!bdev->evt_skb) { |
| 336 | err = -ENOMEM; |
| 337 | goto err_out; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | err = hci_recv_frame(hdev, skb); |
| 342 | if (err < 0) |
| 343 | goto err_free_skb; |
| 344 | |
| 345 | if (hdr->evt == HCI_EV_VENDOR) { |
| 346 | if (test_and_clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, |
| 347 | &bdev->tx_state)) { |
| 348 | /* Barrier to sync with other CPUs */ |
| 349 | smp_mb__after_atomic(); |
| 350 | wake_up_bit(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | |
| 356 | err_free_skb: |
| 357 | kfree_skb(bdev->evt_skb); |
| 358 | bdev->evt_skb = NULL; |
| 359 | |
| 360 | err_out: |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | static const struct h4_recv_pkt mtk_recv_pkts[] = { |
| 365 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
| 366 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
| 367 | { H4_RECV_EVENT, .recv = btmtksdio_recv_event }, |
| 368 | }; |
| 369 | |
| 370 | static int btmtksdio_rx_packet(struct btmtksdio_dev *bdev, u16 rx_size) |
| 371 | { |
| 372 | const struct h4_recv_pkt *pkts = mtk_recv_pkts; |
| 373 | int pkts_count = ARRAY_SIZE(mtk_recv_pkts); |
| 374 | struct mtkbtsdio_hdr *sdio_hdr; |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 375 | int err, i, pad_size; |
| 376 | struct sk_buff *skb; |
| 377 | u16 dlen; |
| 378 | |
| 379 | if (rx_size < sizeof(*sdio_hdr)) |
| 380 | return -EILSEQ; |
| 381 | |
| 382 | /* A SDIO packet is exactly containing a Bluetooth packet */ |
| 383 | skb = bt_skb_alloc(rx_size, GFP_KERNEL); |
| 384 | if (!skb) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | skb_put(skb, rx_size); |
| 388 | |
| 389 | err = sdio_readsb(bdev->func, skb->data, MTK_REG_CRDR, rx_size); |
| 390 | if (err < 0) |
| 391 | goto err_kfree_skb; |
| 392 | |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 393 | bdev->hdev->stat.byte_rx += rx_size; |
| 394 | |
| 395 | sdio_hdr = (void *)skb->data; |
| 396 | |
| 397 | /* We assume the default error as -EILSEQ simply to make the error path |
| 398 | * be cleaner. |
| 399 | */ |
| 400 | err = -EILSEQ; |
| 401 | |
| 402 | if (rx_size != le16_to_cpu(sdio_hdr->len)) { |
| 403 | bt_dev_err(bdev->hdev, "Rx size in sdio header is mismatched "); |
| 404 | goto err_kfree_skb; |
| 405 | } |
| 406 | |
| 407 | hci_skb_pkt_type(skb) = sdio_hdr->bt_type; |
| 408 | |
| 409 | /* Remove MediaTek SDIO header */ |
| 410 | skb_pull(skb, sizeof(*sdio_hdr)); |
| 411 | |
| 412 | /* We have to dig into the packet to get payload size and then know how |
| 413 | * many padding bytes at the tail, these padding bytes should be removed |
| 414 | * before the packet is indicated to the core layer. |
| 415 | */ |
| 416 | for (i = 0; i < pkts_count; i++) { |
| 417 | if (sdio_hdr->bt_type == (&pkts[i])->type) |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | if (i >= pkts_count) { |
| 422 | bt_dev_err(bdev->hdev, "Invalid bt type 0x%02x", |
| 423 | sdio_hdr->bt_type); |
| 424 | goto err_kfree_skb; |
| 425 | } |
| 426 | |
| 427 | /* Remaining bytes cannot hold a header*/ |
| 428 | if (skb->len < (&pkts[i])->hlen) { |
| 429 | bt_dev_err(bdev->hdev, "The size of bt header is mismatched"); |
| 430 | goto err_kfree_skb; |
| 431 | } |
| 432 | |
| 433 | switch ((&pkts[i])->lsize) { |
| 434 | case 1: |
| 435 | dlen = skb->data[(&pkts[i])->loff]; |
| 436 | break; |
| 437 | case 2: |
| 438 | dlen = get_unaligned_le16(skb->data + |
| 439 | (&pkts[i])->loff); |
| 440 | break; |
| 441 | default: |
| 442 | goto err_kfree_skb; |
| 443 | } |
| 444 | |
| 445 | pad_size = skb->len - (&pkts[i])->hlen - dlen; |
| 446 | |
| 447 | /* Remaining bytes cannot hold a payload */ |
| 448 | if (pad_size < 0) { |
| 449 | bt_dev_err(bdev->hdev, "The size of bt payload is mismatched"); |
| 450 | goto err_kfree_skb; |
| 451 | } |
| 452 | |
| 453 | /* Remove padding bytes */ |
| 454 | skb_trim(skb, skb->len - pad_size); |
| 455 | |
| 456 | /* Complete frame */ |
| 457 | (&pkts[i])->recv(bdev->hdev, skb); |
| 458 | |
| 459 | return 0; |
| 460 | |
| 461 | err_kfree_skb: |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 462 | kfree_skb(skb); |
| 463 | |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | static void btmtksdio_interrupt(struct sdio_func *func) |
| 468 | { |
| 469 | struct btmtksdio_dev *bdev = sdio_get_drvdata(func); |
| 470 | u32 int_status; |
| 471 | u16 rx_size; |
| 472 | |
| 473 | /* Disable interrupt */ |
| 474 | sdio_writel(func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0); |
| 475 | |
| 476 | int_status = sdio_readl(func, MTK_REG_CHISR, NULL); |
| 477 | |
| 478 | /* Ack an interrupt as soon as possible before any operation on |
| 479 | * hardware. |
| 480 | * |
| 481 | * Note that we don't ack any status during operations to avoid race |
| 482 | * condition between the host and the device such as it's possible to |
| 483 | * mistakenly ack RX_DONE for the next packet and then cause interrupts |
| 484 | * not be raised again but there is still pending data in the hardware |
| 485 | * FIFO. |
| 486 | */ |
| 487 | sdio_writel(func, int_status, MTK_REG_CHISR, NULL); |
| 488 | |
| 489 | if (unlikely(!int_status)) |
Sean Wang | e1052fb | 2019-04-18 17:07:59 +0800 | [diff] [blame^] | 490 | bt_dev_err(bdev->hdev, "CHISR is 0"); |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 491 | |
| 492 | if (int_status & FW_OWN_BACK_INT) |
Sean Wang | e1052fb | 2019-04-18 17:07:59 +0800 | [diff] [blame^] | 493 | bt_dev_dbg(bdev->hdev, "Get fw own back"); |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 494 | |
| 495 | if (int_status & TX_EMPTY) |
| 496 | schedule_work(&bdev->tx_work); |
| 497 | else if (unlikely(int_status & TX_FIFO_OVERFLOW)) |
Sean Wang | e1052fb | 2019-04-18 17:07:59 +0800 | [diff] [blame^] | 498 | bt_dev_warn(bdev->hdev, "Tx fifo overflow"); |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 499 | |
| 500 | if (int_status & RX_DONE_INT) { |
| 501 | rx_size = (int_status & RX_PKT_LEN) >> 16; |
| 502 | |
| 503 | if (btmtksdio_rx_packet(bdev, rx_size) < 0) |
| 504 | bdev->hdev->stat.err_rx++; |
| 505 | } |
| 506 | |
| 507 | /* Enable interrupt */ |
| 508 | sdio_writel(func, C_INT_EN_SET, MTK_REG_CHLPCR, 0); |
| 509 | } |
| 510 | |
| 511 | static int btmtksdio_open(struct hci_dev *hdev) |
| 512 | { |
| 513 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 514 | int err; |
| 515 | u32 status; |
| 516 | |
| 517 | sdio_claim_host(bdev->func); |
| 518 | |
| 519 | err = sdio_enable_func(bdev->func); |
| 520 | if (err < 0) |
| 521 | goto err_release_host; |
| 522 | |
| 523 | /* Get ownership from the device */ |
| 524 | sdio_writel(bdev->func, C_FW_OWN_REQ_CLR, MTK_REG_CHLPCR, &err); |
| 525 | if (err < 0) |
| 526 | goto err_disable_func; |
| 527 | |
| 528 | err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status, |
| 529 | status & C_FW_OWN_REQ_SET, 2000, 1000000); |
| 530 | if (err < 0) { |
| 531 | bt_dev_err(bdev->hdev, "Cannot get ownership from device"); |
| 532 | goto err_disable_func; |
| 533 | } |
| 534 | |
| 535 | /* Disable interrupt & mask out all interrupt sources */ |
| 536 | sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, &err); |
| 537 | if (err < 0) |
| 538 | goto err_disable_func; |
| 539 | |
| 540 | sdio_writel(bdev->func, 0, MTK_REG_CHIER, &err); |
| 541 | if (err < 0) |
| 542 | goto err_disable_func; |
| 543 | |
| 544 | err = sdio_claim_irq(bdev->func, btmtksdio_interrupt); |
| 545 | if (err < 0) |
| 546 | goto err_disable_func; |
| 547 | |
| 548 | err = sdio_set_block_size(bdev->func, MTK_SDIO_BLOCK_SIZE); |
| 549 | if (err < 0) |
| 550 | goto err_release_irq; |
| 551 | |
| 552 | /* SDIO CMD 5 allows the SDIO device back to idle state an |
| 553 | * synchronous interrupt is supported in SDIO 4-bit mode |
| 554 | */ |
| 555 | sdio_writel(bdev->func, SDIO_INT_CTL | SDIO_RE_INIT_EN, |
| 556 | MTK_REG_CSDIOCSR, &err); |
| 557 | if (err < 0) |
| 558 | goto err_release_irq; |
| 559 | |
| 560 | /* Setup write-1-clear for CHISR register */ |
| 561 | sdio_writel(bdev->func, C_INT_CLR_CTRL, MTK_REG_CHCR, &err); |
| 562 | if (err < 0) |
| 563 | goto err_release_irq; |
| 564 | |
| 565 | /* Setup interrupt sources */ |
| 566 | sdio_writel(bdev->func, RX_DONE_INT | TX_EMPTY | TX_FIFO_OVERFLOW, |
| 567 | MTK_REG_CHIER, &err); |
| 568 | if (err < 0) |
| 569 | goto err_release_irq; |
| 570 | |
| 571 | /* Enable interrupt */ |
| 572 | sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, &err); |
| 573 | if (err < 0) |
| 574 | goto err_release_irq; |
| 575 | |
| 576 | sdio_release_host(bdev->func); |
| 577 | |
| 578 | return 0; |
| 579 | |
| 580 | err_release_irq: |
| 581 | sdio_release_irq(bdev->func); |
| 582 | |
| 583 | err_disable_func: |
| 584 | sdio_disable_func(bdev->func); |
| 585 | |
| 586 | err_release_host: |
| 587 | sdio_release_host(bdev->func); |
| 588 | |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | static int btmtksdio_close(struct hci_dev *hdev) |
| 593 | { |
| 594 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 595 | u32 status; |
| 596 | int err; |
| 597 | |
| 598 | sdio_claim_host(bdev->func); |
| 599 | |
| 600 | /* Disable interrupt */ |
| 601 | sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL); |
| 602 | |
| 603 | sdio_release_irq(bdev->func); |
| 604 | |
| 605 | /* Return ownership to the device */ |
| 606 | sdio_writel(bdev->func, C_FW_OWN_REQ_SET, MTK_REG_CHLPCR, NULL); |
| 607 | |
| 608 | err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status, |
| 609 | !(status & C_FW_OWN_REQ_SET), 2000, 1000000); |
| 610 | if (err < 0) |
| 611 | bt_dev_err(bdev->hdev, "Cannot return ownership to device"); |
| 612 | |
| 613 | sdio_disable_func(bdev->func); |
| 614 | |
| 615 | sdio_release_host(bdev->func); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int btmtksdio_flush(struct hci_dev *hdev) |
| 621 | { |
| 622 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 623 | |
| 624 | skb_queue_purge(&bdev->txq); |
| 625 | |
| 626 | cancel_work_sync(&bdev->tx_work); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int btmtksdio_func_query(struct hci_dev *hdev) |
| 632 | { |
| 633 | struct btmtk_hci_wmt_params wmt_params; |
| 634 | int status, err; |
| 635 | u8 param = 0; |
| 636 | |
| 637 | /* Query whether the function is enabled */ |
| 638 | wmt_params.op = MTK_WMT_FUNC_CTRL; |
| 639 | wmt_params.flag = 4; |
| 640 | wmt_params.dlen = sizeof(param); |
| 641 | wmt_params.data = ¶m; |
| 642 | wmt_params.status = &status; |
| 643 | |
| 644 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 645 | if (err < 0) { |
| 646 | bt_dev_err(hdev, "Failed to query function status (%d)", err); |
| 647 | return err; |
| 648 | } |
| 649 | |
| 650 | return status; |
| 651 | } |
| 652 | |
| 653 | static int mtk_setup_firmware(struct hci_dev *hdev, const char *fwname) |
| 654 | { |
| 655 | struct btmtk_hci_wmt_params wmt_params; |
| 656 | const struct firmware *fw; |
| 657 | const u8 *fw_ptr; |
| 658 | size_t fw_size; |
| 659 | int err, dlen; |
| 660 | u8 flag; |
| 661 | |
| 662 | err = request_firmware(&fw, fwname, &hdev->dev); |
| 663 | if (err < 0) { |
| 664 | bt_dev_err(hdev, "Failed to load firmware file (%d)", err); |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | fw_ptr = fw->data; |
| 669 | fw_size = fw->size; |
| 670 | |
| 671 | /* The size of patch header is 30 bytes, should be skip */ |
| 672 | if (fw_size < 30) { |
| 673 | err = -EINVAL; |
| 674 | goto free_fw; |
| 675 | } |
| 676 | |
| 677 | fw_size -= 30; |
| 678 | fw_ptr += 30; |
| 679 | flag = 1; |
| 680 | |
| 681 | wmt_params.op = MTK_WMT_PATCH_DWNLD; |
| 682 | wmt_params.status = NULL; |
| 683 | |
| 684 | while (fw_size > 0) { |
| 685 | dlen = min_t(int, 250, fw_size); |
| 686 | |
| 687 | /* Tell device the position in sequence */ |
| 688 | if (fw_size - dlen <= 0) |
| 689 | flag = 3; |
| 690 | else if (fw_size < fw->size - 30) |
| 691 | flag = 2; |
| 692 | |
| 693 | wmt_params.flag = flag; |
| 694 | wmt_params.dlen = dlen; |
| 695 | wmt_params.data = fw_ptr; |
| 696 | |
| 697 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 698 | if (err < 0) { |
| 699 | bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)", |
| 700 | err); |
| 701 | goto free_fw; |
| 702 | } |
| 703 | |
| 704 | fw_size -= dlen; |
| 705 | fw_ptr += dlen; |
| 706 | } |
| 707 | |
| 708 | wmt_params.op = MTK_WMT_RST; |
| 709 | wmt_params.flag = 4; |
| 710 | wmt_params.dlen = 0; |
| 711 | wmt_params.data = NULL; |
| 712 | wmt_params.status = NULL; |
| 713 | |
| 714 | /* Activate funciton the firmware providing to */ |
| 715 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 716 | if (err < 0) { |
| 717 | bt_dev_err(hdev, "Failed to send wmt rst (%d)", err); |
| 718 | goto free_fw; |
| 719 | } |
| 720 | |
| 721 | /* Wait a few moments for firmware activation done */ |
| 722 | usleep_range(10000, 12000); |
| 723 | |
| 724 | free_fw: |
| 725 | release_firmware(fw); |
| 726 | return err; |
| 727 | } |
| 728 | |
| 729 | static int btmtksdio_setup(struct hci_dev *hdev) |
| 730 | { |
| 731 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 732 | struct btmtk_hci_wmt_params wmt_params; |
| 733 | ktime_t calltime, delta, rettime; |
| 734 | struct btmtk_tci_sleep tci_sleep; |
| 735 | unsigned long long duration; |
| 736 | struct sk_buff *skb; |
| 737 | int err, status; |
| 738 | u8 param = 0x1; |
| 739 | |
| 740 | calltime = ktime_get(); |
| 741 | |
| 742 | /* Query whether the firmware is already download */ |
| 743 | wmt_params.op = MTK_WMT_SEMAPHORE; |
| 744 | wmt_params.flag = 1; |
| 745 | wmt_params.dlen = 0; |
| 746 | wmt_params.data = NULL; |
| 747 | wmt_params.status = &status; |
| 748 | |
| 749 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 750 | if (err < 0) { |
| 751 | bt_dev_err(hdev, "Failed to query firmware status (%d)", err); |
| 752 | return err; |
| 753 | } |
| 754 | |
| 755 | if (status == BTMTK_WMT_PATCH_DONE) { |
| 756 | bt_dev_info(hdev, "Firmware already downloaded"); |
| 757 | goto ignore_setup_fw; |
| 758 | } |
| 759 | |
| 760 | /* Setup a firmware which the device definitely requires */ |
| 761 | err = mtk_setup_firmware(hdev, bdev->data->fwname); |
| 762 | if (err < 0) |
| 763 | return err; |
| 764 | |
| 765 | ignore_setup_fw: |
| 766 | /* Query whether the device is already enabled */ |
| 767 | err = readx_poll_timeout(btmtksdio_func_query, hdev, status, |
| 768 | status < 0 || status != BTMTK_WMT_ON_PROGRESS, |
| 769 | 2000, 5000000); |
| 770 | /* -ETIMEDOUT happens */ |
| 771 | if (err < 0) |
| 772 | return err; |
| 773 | |
| 774 | /* The other errors happen in btusb_mtk_func_query */ |
| 775 | if (status < 0) |
| 776 | return status; |
| 777 | |
| 778 | if (status == BTMTK_WMT_ON_DONE) { |
| 779 | bt_dev_info(hdev, "function already on"); |
| 780 | goto ignore_func_on; |
| 781 | } |
| 782 | |
| 783 | /* Enable Bluetooth protocol */ |
| 784 | wmt_params.op = MTK_WMT_FUNC_CTRL; |
| 785 | wmt_params.flag = 0; |
| 786 | wmt_params.dlen = sizeof(param); |
| 787 | wmt_params.data = ¶m; |
| 788 | wmt_params.status = NULL; |
| 789 | |
| 790 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 791 | if (err < 0) { |
| 792 | bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err); |
| 793 | return err; |
| 794 | } |
| 795 | |
| 796 | ignore_func_on: |
| 797 | /* Apply the low power environment setup */ |
| 798 | tci_sleep.mode = 0x5; |
| 799 | tci_sleep.duration = cpu_to_le16(0x640); |
| 800 | tci_sleep.host_duration = cpu_to_le16(0x640); |
| 801 | tci_sleep.host_wakeup_pin = 0; |
| 802 | tci_sleep.time_compensation = 0; |
| 803 | |
| 804 | skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep, |
| 805 | HCI_INIT_TIMEOUT); |
| 806 | if (IS_ERR(skb)) { |
| 807 | err = PTR_ERR(skb); |
| 808 | bt_dev_err(hdev, "Failed to apply low power setting (%d)", err); |
| 809 | return err; |
| 810 | } |
| 811 | kfree_skb(skb); |
| 812 | |
| 813 | rettime = ktime_get(); |
| 814 | delta = ktime_sub(rettime, calltime); |
| 815 | duration = (unsigned long long)ktime_to_ns(delta) >> 10; |
| 816 | |
| 817 | bt_dev_info(hdev, "Device setup in %llu usecs", duration); |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int btmtksdio_shutdown(struct hci_dev *hdev) |
| 823 | { |
| 824 | struct btmtk_hci_wmt_params wmt_params; |
| 825 | u8 param = 0x0; |
| 826 | int err; |
| 827 | |
| 828 | /* Disable the device */ |
| 829 | wmt_params.op = MTK_WMT_FUNC_CTRL; |
| 830 | wmt_params.flag = 0; |
| 831 | wmt_params.dlen = sizeof(param); |
| 832 | wmt_params.data = ¶m; |
| 833 | wmt_params.status = NULL; |
| 834 | |
| 835 | err = mtk_hci_wmt_sync(hdev, &wmt_params); |
| 836 | if (err < 0) { |
| 837 | bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err); |
| 838 | return err; |
| 839 | } |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static int btmtksdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb) |
| 845 | { |
| 846 | struct btmtksdio_dev *bdev = hci_get_drvdata(hdev); |
| 847 | |
| 848 | switch (hci_skb_pkt_type(skb)) { |
| 849 | case HCI_COMMAND_PKT: |
| 850 | hdev->stat.cmd_tx++; |
| 851 | break; |
| 852 | |
| 853 | case HCI_ACLDATA_PKT: |
| 854 | hdev->stat.acl_tx++; |
| 855 | break; |
| 856 | |
| 857 | case HCI_SCODATA_PKT: |
| 858 | hdev->stat.sco_tx++; |
| 859 | break; |
| 860 | |
| 861 | default: |
| 862 | return -EILSEQ; |
| 863 | } |
| 864 | |
| 865 | skb_queue_tail(&bdev->txq, skb); |
| 866 | |
| 867 | schedule_work(&bdev->tx_work); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static int btmtksdio_probe(struct sdio_func *func, |
| 873 | const struct sdio_device_id *id) |
| 874 | { |
| 875 | struct btmtksdio_dev *bdev; |
| 876 | struct hci_dev *hdev; |
| 877 | int err; |
| 878 | |
| 879 | bdev = devm_kzalloc(&func->dev, sizeof(*bdev), GFP_KERNEL); |
| 880 | if (!bdev) |
| 881 | return -ENOMEM; |
| 882 | |
| 883 | bdev->data = (void *)id->driver_data; |
| 884 | if (!bdev->data) |
| 885 | return -ENODEV; |
| 886 | |
| 887 | bdev->func = func; |
| 888 | |
| 889 | INIT_WORK(&bdev->tx_work, btmtksdio_tx_work); |
| 890 | skb_queue_head_init(&bdev->txq); |
| 891 | |
| 892 | /* Initialize and register HCI device */ |
| 893 | hdev = hci_alloc_dev(); |
| 894 | if (!hdev) { |
| 895 | dev_err(&func->dev, "Can't allocate HCI device\n"); |
| 896 | return -ENOMEM; |
| 897 | } |
| 898 | |
| 899 | bdev->hdev = hdev; |
| 900 | |
| 901 | hdev->bus = HCI_SDIO; |
| 902 | hci_set_drvdata(hdev, bdev); |
| 903 | |
| 904 | hdev->open = btmtksdio_open; |
| 905 | hdev->close = btmtksdio_close; |
| 906 | hdev->flush = btmtksdio_flush; |
| 907 | hdev->setup = btmtksdio_setup; |
| 908 | hdev->shutdown = btmtksdio_shutdown; |
| 909 | hdev->send = btmtksdio_send_frame; |
| 910 | SET_HCIDEV_DEV(hdev, &func->dev); |
| 911 | |
| 912 | hdev->manufacturer = 70; |
| 913 | set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); |
| 914 | |
| 915 | err = hci_register_dev(hdev); |
| 916 | if (err < 0) { |
| 917 | dev_err(&func->dev, "Can't register HCI device\n"); |
| 918 | hci_free_dev(hdev); |
| 919 | return err; |
| 920 | } |
| 921 | |
| 922 | sdio_set_drvdata(func, bdev); |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static void btmtksdio_remove(struct sdio_func *func) |
| 928 | { |
| 929 | struct btmtksdio_dev *bdev = sdio_get_drvdata(func); |
| 930 | struct hci_dev *hdev; |
| 931 | |
| 932 | if (!bdev) |
| 933 | return; |
| 934 | |
| 935 | hdev = bdev->hdev; |
| 936 | |
| 937 | sdio_set_drvdata(func, NULL); |
| 938 | hci_unregister_dev(hdev); |
| 939 | hci_free_dev(hdev); |
| 940 | } |
| 941 | |
| 942 | static struct sdio_driver btmtksdio_driver = { |
| 943 | .name = "btmtksdio", |
| 944 | .probe = btmtksdio_probe, |
| 945 | .remove = btmtksdio_remove, |
| 946 | .id_table = btmtksdio_table, |
| 947 | }; |
| 948 | |
Sean Wang | a6094a4 | 2019-03-14 05:01:59 +0800 | [diff] [blame] | 949 | module_sdio_driver(btmtksdio_driver); |
Sean Wang | 9aebfd4 | 2019-03-08 09:15:44 +0800 | [diff] [blame] | 950 | |
| 951 | MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); |
| 952 | MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION); |
| 953 | MODULE_VERSION(VERSION); |
| 954 | MODULE_LICENSE("GPL"); |
| 955 | MODULE_FIRMWARE(FIRMWARE_MT7663); |
| 956 | MODULE_FIRMWARE(FIRMWARE_MT7668); |