1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Bluetooth HCI UART driver for marvell devices 5 * 6 * Copyright (C) 2016 Marvell International Ltd. 7 * Copyright (C) 2016 Intel Corporation 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/errno.h> 12 #include <linux/skbuff.h> 13 #include <linux/firmware.h> 14 #include <linux/module.h> 15 #include <linux/tty.h> 16 #include <linux/of.h> 17 #include <linux/serdev.h> 18 19 #include <net/bluetooth/bluetooth.h> 20 #include <net/bluetooth/hci_core.h> 21 22 #include "hci_uart.h" 23 24 #define HCI_FW_REQ_PKT 0xA5 25 #define HCI_CHIP_VER_PKT 0xAA 26 27 #define MRVL_ACK 0x5A 28 #define MRVL_NAK 0xBF 29 #define MRVL_RAW_DATA 0x1F 30 #define MRVL_SET_BAUDRATE 0xFC09 31 32 enum { 33 STATE_CHIP_VER_PENDING, 34 STATE_FW_REQ_PENDING, 35 STATE_FW_LOADED, 36 }; 37 38 struct mrvl_data { 39 struct sk_buff *rx_skb; 40 struct sk_buff_head txq; 41 struct sk_buff_head rawq; 42 unsigned long flags; 43 unsigned int tx_len; 44 u8 id, rev; 45 }; 46 47 struct mrvl_serdev { 48 struct hci_uart hu; 49 }; 50 51 struct hci_mrvl_pkt { 52 __le16 lhs; 53 __le16 rhs; 54 } __packed; 55 #define HCI_MRVL_PKT_SIZE 4 56 57 static int mrvl_open(struct hci_uart *hu) 58 { 59 struct mrvl_data *mrvl; 60 61 BT_DBG("hu %p", hu); 62 63 if (!hci_uart_has_flow_control(hu)) 64 return -EOPNOTSUPP; 65 66 mrvl = kzalloc_obj(*mrvl); 67 if (!mrvl) 68 return -ENOMEM; 69 70 skb_queue_head_init(&mrvl->txq); 71 skb_queue_head_init(&mrvl->rawq); 72 73 set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags); 74 75 hu->priv = mrvl; 76 77 return 0; 78 } 79 80 static int mrvl_close(struct hci_uart *hu) 81 { 82 struct mrvl_data *mrvl = hu->priv; 83 84 BT_DBG("hu %p", hu); 85 86 skb_queue_purge(&mrvl->txq); 87 skb_queue_purge(&mrvl->rawq); 88 kfree_skb(mrvl->rx_skb); 89 kfree(mrvl); 90 91 hu->priv = NULL; 92 return 0; 93 } 94 95 static int mrvl_flush(struct hci_uart *hu) 96 { 97 struct mrvl_data *mrvl = hu->priv; 98 99 BT_DBG("hu %p", hu); 100 101 skb_queue_purge(&mrvl->txq); 102 skb_queue_purge(&mrvl->rawq); 103 104 return 0; 105 } 106 107 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu) 108 { 109 struct mrvl_data *mrvl = hu->priv; 110 struct sk_buff *skb; 111 112 skb = skb_dequeue(&mrvl->txq); 113 if (!skb) { 114 /* Any raw data ? */ 115 skb = skb_dequeue(&mrvl->rawq); 116 } else { 117 /* Prepend skb with frame type */ 118 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); 119 } 120 121 return skb; 122 } 123 124 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb) 125 { 126 struct mrvl_data *mrvl = hu->priv; 127 128 skb_queue_tail(&mrvl->txq, skb); 129 return 0; 130 } 131 132 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type) 133 { 134 struct mrvl_data *mrvl = hu->priv; 135 struct sk_buff *skb; 136 137 /* No H4 payload, only 1 byte header */ 138 skb = bt_skb_alloc(0, GFP_ATOMIC); 139 if (!skb) { 140 bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet"); 141 return; 142 } 143 hci_skb_pkt_type(skb) = type; 144 145 skb_queue_tail(&mrvl->txq, skb); 146 hci_uart_tx_wakeup(hu); 147 } 148 149 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb) 150 { 151 struct hci_mrvl_pkt *pkt = (void *)skb->data; 152 struct hci_uart *hu = hci_get_drvdata(hdev); 153 struct mrvl_data *mrvl = hu->priv; 154 int ret = 0; 155 156 if ((pkt->lhs ^ pkt->rhs) != 0xffff) { 157 bt_dev_err(hdev, "Corrupted mrvl header"); 158 mrvl_send_ack(hu, MRVL_NAK); 159 ret = -EINVAL; 160 goto done; 161 } 162 mrvl_send_ack(hu, MRVL_ACK); 163 164 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) { 165 bt_dev_err(hdev, "Received unexpected firmware request"); 166 ret = -EINVAL; 167 goto done; 168 } 169 170 mrvl->tx_len = le16_to_cpu(pkt->lhs); 171 172 clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 173 smp_mb__after_atomic(); 174 wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING); 175 176 done: 177 kfree_skb(skb); 178 return ret; 179 } 180 181 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb) 182 { 183 struct hci_mrvl_pkt *pkt = (void *)skb->data; 184 struct hci_uart *hu = hci_get_drvdata(hdev); 185 struct mrvl_data *mrvl = hu->priv; 186 u16 version = le16_to_cpu(pkt->lhs); 187 int ret = 0; 188 189 if ((pkt->lhs ^ pkt->rhs) != 0xffff) { 190 bt_dev_err(hdev, "Corrupted mrvl header"); 191 mrvl_send_ack(hu, MRVL_NAK); 192 ret = -EINVAL; 193 goto done; 194 } 195 mrvl_send_ack(hu, MRVL_ACK); 196 197 if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) { 198 bt_dev_err(hdev, "Received unexpected chip version"); 199 goto done; 200 } 201 202 mrvl->id = version; 203 mrvl->rev = version >> 8; 204 205 bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev); 206 207 clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags); 208 smp_mb__after_atomic(); 209 wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING); 210 211 done: 212 kfree_skb(skb); 213 return ret; 214 } 215 216 #define HCI_RECV_CHIP_VER \ 217 .type = HCI_CHIP_VER_PKT, \ 218 .hlen = HCI_MRVL_PKT_SIZE, \ 219 .loff = 0, \ 220 .lsize = 0, \ 221 .maxlen = HCI_MRVL_PKT_SIZE 222 223 #define HCI_RECV_FW_REQ \ 224 .type = HCI_FW_REQ_PKT, \ 225 .hlen = HCI_MRVL_PKT_SIZE, \ 226 .loff = 0, \ 227 .lsize = 0, \ 228 .maxlen = HCI_MRVL_PKT_SIZE 229 230 static const struct h4_recv_pkt mrvl_recv_pkts[] = { 231 { H4_RECV_ACL, .recv = hci_recv_frame }, 232 { H4_RECV_SCO, .recv = hci_recv_frame }, 233 { H4_RECV_EVENT, .recv = hci_recv_frame }, 234 { HCI_RECV_FW_REQ, .recv = mrvl_recv_fw_req }, 235 { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver }, 236 }; 237 238 static int mrvl_recv(struct hci_uart *hu, const void *data, int count) 239 { 240 struct mrvl_data *mrvl = hu->priv; 241 242 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 243 return -EUNATCH; 244 245 /* We might receive some noise when there is no firmware loaded. Therefore, 246 * we drop data if the firmware is not loaded yet and if there is no fw load 247 * request pending. 248 */ 249 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags) && 250 !test_bit(STATE_FW_LOADED, &mrvl->flags)) 251 return count; 252 253 mrvl->rx_skb = h4_recv_buf(hu, mrvl->rx_skb, data, count, 254 mrvl_recv_pkts, 255 ARRAY_SIZE(mrvl_recv_pkts)); 256 if (IS_ERR(mrvl->rx_skb)) { 257 int err = PTR_ERR(mrvl->rx_skb); 258 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); 259 mrvl->rx_skb = NULL; 260 return err; 261 } 262 263 return count; 264 } 265 266 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name) 267 { 268 struct hci_uart *hu = hci_get_drvdata(hdev); 269 struct mrvl_data *mrvl = hu->priv; 270 const struct firmware *fw = NULL; 271 const u8 *fw_ptr, *fw_max; 272 int err; 273 274 err = request_firmware(&fw, name, &hdev->dev); 275 if (err < 0) { 276 bt_dev_err(hdev, "Failed to load firmware file %s", name); 277 return err; 278 } 279 280 fw_ptr = fw->data; 281 fw_max = fw->data + fw->size; 282 283 bt_dev_info(hdev, "Loading %s", name); 284 285 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 286 287 while (fw_ptr <= fw_max) { 288 struct sk_buff *skb; 289 290 /* Controller drives the firmware load by sending firmware 291 * request packets containing the expected fragment size. 292 */ 293 err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING, 294 TASK_INTERRUPTIBLE, 295 msecs_to_jiffies(2000)); 296 if (err == -EINTR) { 297 bt_dev_err(hdev, "Firmware load interrupted"); 298 break; 299 } else if (err) { 300 bt_dev_err(hdev, "Firmware request timeout"); 301 err = -ETIMEDOUT; 302 break; 303 } 304 305 bt_dev_dbg(hdev, "Firmware request, expecting %d bytes", 306 mrvl->tx_len); 307 308 if (fw_ptr == fw_max) { 309 /* Controller requests a null size once firmware is 310 * fully loaded. If controller expects more data, there 311 * is an issue. 312 */ 313 if (!mrvl->tx_len) { 314 bt_dev_info(hdev, "Firmware loading complete"); 315 } else { 316 bt_dev_err(hdev, "Firmware loading failure"); 317 err = -EINVAL; 318 } 319 break; 320 } 321 322 if (fw_ptr + mrvl->tx_len > fw_max) { 323 mrvl->tx_len = fw_max - fw_ptr; 324 bt_dev_dbg(hdev, "Adjusting tx_len to %d", 325 mrvl->tx_len); 326 } 327 328 skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL); 329 if (!skb) { 330 bt_dev_err(hdev, "Failed to alloc mem for FW packet"); 331 err = -ENOMEM; 332 break; 333 } 334 bt_cb(skb)->pkt_type = MRVL_RAW_DATA; 335 336 skb_put_data(skb, fw_ptr, mrvl->tx_len); 337 fw_ptr += mrvl->tx_len; 338 339 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 340 341 skb_queue_tail(&mrvl->rawq, skb); 342 hci_uart_tx_wakeup(hu); 343 } 344 345 release_firmware(fw); 346 return err; 347 } 348 349 static int mrvl_setup(struct hci_uart *hu) 350 { 351 int err; 352 struct mrvl_data *mrvl = hu->priv; 353 354 hci_uart_set_flow_control(hu, true); 355 356 err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin"); 357 if (err) { 358 bt_dev_err(hu->hdev, "Unable to download firmware helper"); 359 return -EINVAL; 360 } 361 362 /* Let the final ack go out before switching the baudrate */ 363 hci_uart_wait_until_sent(hu); 364 365 if (hu->serdev) 366 serdev_device_set_baudrate(hu->serdev, hu->oper_speed); 367 else 368 hci_uart_set_baudrate(hu, hu->oper_speed); 369 370 hci_uart_set_flow_control(hu, false); 371 372 err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin"); 373 if (err) 374 return err; 375 376 set_bit(STATE_FW_LOADED, &mrvl->flags); 377 378 return 0; 379 } 380 381 static int mrvl_set_baudrate(struct hci_uart *hu, unsigned int speed) 382 { 383 int err; 384 struct mrvl_data *mrvl = hu->priv; 385 __le32 speed_le = cpu_to_le32(speed); 386 387 /* The firmware might be loaded by the Wifi driver over SDIO. We wait 388 * up to 10s for the CTS to go up. Afterward, we know that the firmware 389 * is ready. 390 */ 391 err = serdev_device_wait_for_cts(hu->serdev, true, 10000); 392 if (err) { 393 bt_dev_err(hu->hdev, "Wait for CTS failed with %d\n", err); 394 return err; 395 } 396 397 set_bit(STATE_FW_LOADED, &mrvl->flags); 398 399 err = __hci_cmd_sync_status(hu->hdev, MRVL_SET_BAUDRATE, 400 sizeof(speed_le), &speed_le, 401 HCI_INIT_TIMEOUT); 402 if (err) { 403 bt_dev_err(hu->hdev, "send command failed: %d", err); 404 return err; 405 } 406 407 serdev_device_set_baudrate(hu->serdev, speed); 408 409 /* We forcefully have to send a command to the bluetooth module so that 410 * the driver detects it after a baudrate change. This is foreseen by 411 * hci_serdev by setting HCI_UART_VND_DETECT which then causes a dummy 412 * local version read. 413 */ 414 set_bit(HCI_UART_VND_DETECT, &hu->hdev_flags); 415 416 return 0; 417 } 418 419 static const struct hci_uart_proto mrvl_proto_8897 = { 420 .id = HCI_UART_MRVL, 421 .name = "Marvell", 422 .init_speed = 115200, 423 .oper_speed = 3000000, 424 .open = mrvl_open, 425 .close = mrvl_close, 426 .flush = mrvl_flush, 427 .setup = mrvl_setup, 428 .recv = mrvl_recv, 429 .enqueue = mrvl_enqueue, 430 .dequeue = mrvl_dequeue, 431 }; 432 433 static const struct hci_uart_proto mrvl_proto_8997 = { 434 .id = HCI_UART_MRVL, 435 .name = "Marvell 8997", 436 .init_speed = 115200, 437 .oper_speed = 3000000, 438 .open = mrvl_open, 439 .close = mrvl_close, 440 .flush = mrvl_flush, 441 .set_baudrate = mrvl_set_baudrate, 442 .recv = mrvl_recv, 443 .enqueue = mrvl_enqueue, 444 .dequeue = mrvl_dequeue, 445 }; 446 447 static int mrvl_serdev_probe(struct serdev_device *serdev) 448 { 449 struct mrvl_serdev *mrvldev; 450 const struct hci_uart_proto *mrvl_proto = device_get_match_data(&serdev->dev); 451 452 mrvldev = devm_kzalloc_obj(&serdev->dev, *mrvldev); 453 if (!mrvldev) 454 return -ENOMEM; 455 456 mrvldev->hu.oper_speed = mrvl_proto->oper_speed; 457 if (mrvl_proto->set_baudrate) 458 of_property_read_u32(serdev->dev.of_node, "max-speed", &mrvldev->hu.oper_speed); 459 460 mrvldev->hu.serdev = serdev; 461 serdev_device_set_drvdata(serdev, mrvldev); 462 463 return hci_uart_register_device(&mrvldev->hu, mrvl_proto); 464 } 465 466 static void mrvl_serdev_remove(struct serdev_device *serdev) 467 { 468 struct mrvl_serdev *mrvldev = serdev_device_get_drvdata(serdev); 469 470 hci_uart_unregister_device(&mrvldev->hu); 471 } 472 473 static const struct of_device_id __maybe_unused mrvl_bluetooth_of_match[] = { 474 { .compatible = "mrvl,88w8897", .data = &mrvl_proto_8897}, 475 { .compatible = "mrvl,88w8997", .data = &mrvl_proto_8997}, 476 { }, 477 }; 478 MODULE_DEVICE_TABLE(of, mrvl_bluetooth_of_match); 479 480 static struct serdev_device_driver mrvl_serdev_driver = { 481 .probe = mrvl_serdev_probe, 482 .remove = mrvl_serdev_remove, 483 .driver = { 484 .name = "hci_uart_mrvl", 485 .of_match_table = of_match_ptr(mrvl_bluetooth_of_match), 486 }, 487 }; 488 489 int __init mrvl_init(void) 490 { 491 serdev_device_driver_register(&mrvl_serdev_driver); 492 493 return hci_uart_register_proto(&mrvl_proto_8897); 494 } 495 496 int __exit mrvl_deinit(void) 497 { 498 serdev_device_driver_unregister(&mrvl_serdev_driver); 499 500 return hci_uart_unregister_proto(&mrvl_proto_8897); 501 } 502