1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #include <linux/module.h>
8 #include <linux/kernel.h>
10 #include <linux/serdev.h>
12 #include <linux/skbuff.h>
13 #include <linux/unaligned.h>
14 #include <linux/firmware.h>
15 #include <linux/string.h>
16 #include <linux/crc8.h>
17 #include <linux/crc32.h>
18 #include <linux/string_helpers.h>
19 #include <linux/gpio/consumer.h>
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
26 #define MANUFACTURER_NXP 37
28 #define BTNXPUART_TX_STATE_ACTIVE 1
29 #define BTNXPUART_FW_DOWNLOADING 2
30 #define BTNXPUART_CHECK_BOOT_SIGNATURE 3
31 #define BTNXPUART_SERDEV_OPEN 4
32 #define BTNXPUART_IR_IN_PROGRESS 5
33 #define BTNXPUART_FW_DOWNLOAD_ABORT 6
35 /* NXP HW err codes */
36 #define BTNXPUART_IR_HW_ERR 0xb0
38 #define FIRMWARE_W8987 "uart8987_bt.bin"
39 #define FIRMWARE_W8987_OLD "uartuart8987_bt.bin"
40 #define FIRMWARE_W8997 "uart8997_bt_v4.bin"
41 #define FIRMWARE_W8997_OLD "uartuart8997_bt_v4.bin"
42 #define FIRMWARE_W9098 "uart9098_bt_v1.bin"
43 #define FIRMWARE_W9098_OLD "uartuart9098_bt_v1.bin"
44 #define FIRMWARE_IW416 "uartiw416_bt.bin"
45 #define FIRMWARE_IW416_OLD "uartiw416_bt_v0.bin"
46 #define FIRMWARE_IW612 "uartspi_n61x_v1.bin.se"
47 #define FIRMWARE_IW610 "uartspi_iw610.bin"
48 #define FIRMWARE_SECURE_IW610 "uartspi_iw610.bin.se"
49 #define FIRMWARE_IW624 "uartiw624_bt.bin"
50 #define FIRMWARE_SECURE_IW624 "uartiw624_bt.bin.se"
51 #define FIRMWARE_AW693 "uartaw693_bt.bin"
52 #define FIRMWARE_SECURE_AW693 "uartaw693_bt.bin.se"
53 #define FIRMWARE_AW693_A1 "uartaw693_bt_v1.bin"
54 #define FIRMWARE_SECURE_AW693_A1 "uartaw693_bt_v1.bin.se"
55 #define FIRMWARE_HELPER "helper_uart_3000000.bin"
57 #define CHIP_ID_W9098 0x5c03
58 #define CHIP_ID_IW416 0x7201
59 #define CHIP_ID_IW612 0x7601
60 #define CHIP_ID_IW624a 0x8000
61 #define CHIP_ID_IW624c 0x8001
62 #define CHIP_ID_AW693a0 0x8200
63 #define CHIP_ID_AW693a1 0x8201
64 #define CHIP_ID_IW610a0 0x8800
65 #define CHIP_ID_IW610a1 0x8801
67 #define FW_SECURE_MASK 0xc0
69 #define FW_AUTH_ILLEGAL 0x40
70 #define FW_AUTH_PLAIN 0x80
71 #define FW_AUTH_ENC 0xc0
73 #define HCI_NXP_PRI_BAUDRATE 115200
74 #define HCI_NXP_SEC_BAUDRATE 3000000
76 #define MAX_FW_FILE_NAME_LEN 50
78 /* Default ps timeout period in milliseconds */
79 #define PS_DEFAULT_TIMEOUT_PERIOD_MS 2000
82 #define WAKEUP_METHOD_DTR 0
83 #define WAKEUP_METHOD_BREAK 1
84 #define WAKEUP_METHOD_EXT_BREAK 2
85 #define WAKEUP_METHOD_RTS 3
86 #define WAKEUP_METHOD_GPIO 4
87 #define WAKEUP_METHOD_INVALID 0xff
89 /* power save mode status */
90 #define PS_MODE_DISABLE 0
91 #define PS_MODE_ENABLE 1
93 /* Power Save Commands to ps_work_func */
94 #define PS_CMD_EXIT_PS 1
95 #define PS_CMD_ENTER_PS 2
97 /* power save state */
98 #define PS_STATE_AWAKE 0
99 #define PS_STATE_SLEEP 1
101 /* Bluetooth vendor command : Sleep mode */
102 #define HCI_NXP_AUTO_SLEEP_MODE 0xfc23
103 /* Bluetooth vendor command : Wakeup method */
104 #define HCI_NXP_WAKEUP_METHOD 0xfc53
105 /* Bluetooth vendor command : Set operational baudrate */
106 #define HCI_NXP_SET_OPER_SPEED 0xfc09
107 /* Bluetooth vendor command: Independent Reset */
108 #define HCI_NXP_IND_RESET 0xfcfc
110 /* Bluetooth Power State : Vendor cmd params */
111 #define BT_PS_ENABLE 0x02
112 #define BT_PS_DISABLE 0x03
114 /* Bluetooth Host Wakeup Methods */
115 #define BT_HOST_WAKEUP_METHOD_NONE 0x00
116 #define BT_HOST_WAKEUP_METHOD_DTR 0x01
117 #define BT_HOST_WAKEUP_METHOD_BREAK 0x02
118 #define BT_HOST_WAKEUP_METHOD_GPIO 0x03
120 /* Bluetooth Chip Wakeup Methods */
121 #define BT_CTRL_WAKEUP_METHOD_DSR 0x00
122 #define BT_CTRL_WAKEUP_METHOD_BREAK 0x01
123 #define BT_CTRL_WAKEUP_METHOD_GPIO 0x02
124 #define BT_CTRL_WAKEUP_METHOD_EXT_BREAK 0x04
125 #define BT_CTRL_WAKEUP_METHOD_RTS 0x05
128 u8 target_ps_mode
; /* ps mode to be set */
129 u8 cur_psmode
; /* current ps_mode */
130 u8 ps_state
; /* controller's power save state */
133 u8 cur_h2c_wakeupmode
;
137 bool driver_sent_cmd
;
140 struct gpio_desc
*h2c_ps_gpio
;
141 struct hci_dev
*hdev
;
142 struct work_struct work
;
143 struct timer_list ps_timer
;
144 struct mutex ps_lock
;
147 struct wakeup_cmd_payload
{
154 struct psmode_cmd_payload
{
156 __le16 c2h_ps_interval
;
159 struct btnxpuart_data
{
160 const char *helper_fw_name
;
162 const char *fw_name_old
;
165 struct btnxpuart_dev
{
166 struct hci_dev
*hdev
;
167 struct serdev_device
*serdev
;
169 struct work_struct tx_work
;
170 unsigned long tx_state
;
171 struct sk_buff_head txq
;
172 struct sk_buff
*rx_skb
;
174 const struct firmware
*fw
;
175 u8 fw_name
[MAX_FW_FILE_NAME_LEN
];
176 u32 fw_dnld_v1_offset
;
177 u32 fw_v1_sent_bytes
;
178 u32 fw_dnld_v3_offset
;
179 u32 fw_v3_offset_correction
;
180 u32 fw_v1_expected_len
;
182 wait_queue_head_t fw_dnld_done_wait_q
;
183 wait_queue_head_t check_boot_sign_wait_q
;
186 u32 current_baudrate
;
187 u32 fw_init_baudrate
;
188 bool timeout_changed
;
189 bool baudrate_changed
;
190 bool helper_downloaded
;
192 struct ps_data psdata
;
193 struct btnxpuart_data
*nxp_data
;
196 #define NXP_V1_FW_REQ_PKT 0xa5
197 #define NXP_V1_CHIP_VER_PKT 0xaa
198 #define NXP_V3_FW_REQ_PKT 0xa7
199 #define NXP_V3_CHIP_VER_PKT 0xab
201 #define NXP_ACK_V1 0x5a
202 #define NXP_NAK_V1 0xbf
203 #define NXP_ACK_V3 0x7a
204 #define NXP_NAK_V3 0x7b
205 #define NXP_CRC_ERROR_V3 0x7c
207 /* Bootloader signature error codes */
208 #define NXP_ACK_RX_TIMEOUT 0x0002 /* ACK not received from host */
209 #define NXP_HDR_RX_TIMEOUT 0x0003 /* FW Header chunk not received */
210 #define NXP_DATA_RX_TIMEOUT 0x0004 /* FW Data chunk not received */
214 #define NXP_RECV_CHIP_VER_V1 \
215 .type = NXP_V1_CHIP_VER_PKT, \
221 #define NXP_RECV_FW_REQ_V1 \
222 .type = NXP_V1_FW_REQ_PKT, \
228 #define NXP_RECV_CHIP_VER_V3 \
229 .type = NXP_V3_CHIP_VER_PKT, \
235 #define NXP_RECV_FW_REQ_V3 \
236 .type = NXP_V3_FW_REQ_PKT, \
247 struct v1_start_ind
{
259 struct v3_start_ind
{
265 /* UART register addresses of BT chip */
266 #define CLKDIVADDR 0x7f00008f
267 #define UARTDIVADDR 0x7f000090
268 #define UARTMCRADDR 0x7f000091
269 #define UARTREINITADDR 0x7f000092
270 #define UARTICRADDR 0x7f000093
271 #define UARTFCRADDR 0x7f000094
273 #define MCR 0x00000022
274 #define INIT 0x00000001
275 #define ICR 0x000000c7
276 #define FCR 0x000000c7
278 #define POLYNOMIAL8 0x07
286 struct uart_reg clkdiv
;
287 struct uart_reg uartdiv
;
289 struct uart_reg re_init
;
295 struct nxp_bootloader_cmd
{
302 struct nxp_v3_rx_timeout_nak
{
308 union nxp_v3_rx_timeout_nak_u
{
309 struct nxp_v3_rx_timeout_nak pkt
;
313 static u8 crc8_table
[CRC8_TABLE_SIZE
];
315 /* Default configurations */
316 #define DEFAULT_H2C_WAKEUP_MODE WAKEUP_METHOD_BREAK
317 #define DEFAULT_PS_MODE PS_MODE_ENABLE
318 #define FW_INIT_BAUDRATE HCI_NXP_PRI_BAUDRATE
320 static struct sk_buff
*nxp_drv_send_cmd(struct hci_dev
*hdev
, u16 opcode
,
324 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
325 struct ps_data
*psdata
= &nxpdev
->psdata
;
328 /* set flag to prevent nxp_enqueue from parsing values from this command and
329 * calling hci_cmd_sync_queue() again.
331 psdata
->driver_sent_cmd
= true;
332 skb
= __hci_cmd_sync(hdev
, opcode
, plen
, param
, HCI_CMD_TIMEOUT
);
333 psdata
->driver_sent_cmd
= false;
338 static void btnxpuart_tx_wakeup(struct btnxpuart_dev
*nxpdev
)
340 if (schedule_work(&nxpdev
->tx_work
))
341 set_bit(BTNXPUART_TX_STATE_ACTIVE
, &nxpdev
->tx_state
);
344 /* NXP Power Save Feature */
345 static void ps_start_timer(struct btnxpuart_dev
*nxpdev
)
347 struct ps_data
*psdata
= &nxpdev
->psdata
;
352 if (psdata
->cur_psmode
== PS_MODE_ENABLE
)
353 mod_timer(&psdata
->ps_timer
, jiffies
+ msecs_to_jiffies(psdata
->h2c_ps_interval
));
355 if (psdata
->ps_state
== PS_STATE_AWAKE
&& psdata
->ps_cmd
== PS_CMD_ENTER_PS
)
356 cancel_work_sync(&psdata
->work
);
359 static void ps_cancel_timer(struct btnxpuart_dev
*nxpdev
)
361 struct ps_data
*psdata
= &nxpdev
->psdata
;
363 flush_work(&psdata
->work
);
364 timer_shutdown_sync(&psdata
->ps_timer
);
367 static void ps_control(struct hci_dev
*hdev
, u8 ps_state
)
369 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
370 struct ps_data
*psdata
= &nxpdev
->psdata
;
373 if (psdata
->ps_state
== ps_state
||
374 !test_bit(BTNXPUART_SERDEV_OPEN
, &nxpdev
->tx_state
))
377 mutex_lock(&psdata
->ps_lock
);
378 switch (psdata
->cur_h2c_wakeupmode
) {
379 case WAKEUP_METHOD_GPIO
:
380 if (ps_state
== PS_STATE_AWAKE
)
381 gpiod_set_value_cansleep(psdata
->h2c_ps_gpio
, 0);
383 gpiod_set_value_cansleep(psdata
->h2c_ps_gpio
, 1);
384 bt_dev_dbg(hdev
, "Set h2c_ps_gpio: %s",
385 str_high_low(ps_state
== PS_STATE_SLEEP
));
387 case WAKEUP_METHOD_DTR
:
388 if (ps_state
== PS_STATE_AWAKE
)
389 status
= serdev_device_set_tiocm(nxpdev
->serdev
, TIOCM_DTR
, 0);
391 status
= serdev_device_set_tiocm(nxpdev
->serdev
, 0, TIOCM_DTR
);
393 case WAKEUP_METHOD_BREAK
:
395 if (ps_state
== PS_STATE_AWAKE
)
396 status
= serdev_device_break_ctl(nxpdev
->serdev
, 0);
398 status
= serdev_device_break_ctl(nxpdev
->serdev
, -1);
399 msleep(20); /* Allow chip to detect UART-break and enter sleep */
400 bt_dev_dbg(hdev
, "Set UART break: %s, status=%d",
401 str_on_off(ps_state
== PS_STATE_SLEEP
), status
);
405 psdata
->ps_state
= ps_state
;
406 mutex_unlock(&psdata
->ps_lock
);
408 if (ps_state
== PS_STATE_AWAKE
)
409 btnxpuart_tx_wakeup(nxpdev
);
412 static void ps_work_func(struct work_struct
*work
)
414 struct ps_data
*data
= container_of(work
, struct ps_data
, work
);
416 if (data
->ps_cmd
== PS_CMD_ENTER_PS
&& data
->cur_psmode
== PS_MODE_ENABLE
)
417 ps_control(data
->hdev
, PS_STATE_SLEEP
);
418 else if (data
->ps_cmd
== PS_CMD_EXIT_PS
)
419 ps_control(data
->hdev
, PS_STATE_AWAKE
);
422 static void ps_timeout_func(struct timer_list
*t
)
424 struct ps_data
*data
= from_timer(data
, t
, ps_timer
);
425 struct hci_dev
*hdev
= data
->hdev
;
426 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
428 if (test_bit(BTNXPUART_TX_STATE_ACTIVE
, &nxpdev
->tx_state
)) {
429 ps_start_timer(nxpdev
);
431 data
->ps_cmd
= PS_CMD_ENTER_PS
;
432 schedule_work(&data
->work
);
436 static int ps_setup(struct hci_dev
*hdev
)
438 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
439 struct serdev_device
*serdev
= nxpdev
->serdev
;
440 struct ps_data
*psdata
= &nxpdev
->psdata
;
442 psdata
->h2c_ps_gpio
= devm_gpiod_get_optional(&serdev
->dev
, "device-wakeup",
444 if (IS_ERR(psdata
->h2c_ps_gpio
)) {
445 bt_dev_err(hdev
, "Error fetching device-wakeup-gpios: %ld",
446 PTR_ERR(psdata
->h2c_ps_gpio
));
447 return PTR_ERR(psdata
->h2c_ps_gpio
);
450 if (!psdata
->h2c_ps_gpio
)
451 psdata
->h2c_wakeup_gpio
= 0xff;
454 INIT_WORK(&psdata
->work
, ps_work_func
);
455 mutex_init(&psdata
->ps_lock
);
456 timer_setup(&psdata
->ps_timer
, ps_timeout_func
, 0);
461 static bool ps_wakeup(struct btnxpuart_dev
*nxpdev
)
463 struct ps_data
*psdata
= &nxpdev
->psdata
;
466 mutex_lock(&psdata
->ps_lock
);
467 ps_state
= psdata
->ps_state
;
468 mutex_unlock(&psdata
->ps_lock
);
470 if (ps_state
!= PS_STATE_AWAKE
) {
471 psdata
->ps_cmd
= PS_CMD_EXIT_PS
;
472 schedule_work(&psdata
->work
);
478 static void ps_cleanup(struct btnxpuart_dev
*nxpdev
)
480 struct ps_data
*psdata
= &nxpdev
->psdata
;
483 mutex_lock(&psdata
->ps_lock
);
484 ps_state
= psdata
->ps_state
;
485 mutex_unlock(&psdata
->ps_lock
);
487 if (ps_state
!= PS_STATE_AWAKE
)
488 ps_control(psdata
->hdev
, PS_STATE_AWAKE
);
490 ps_cancel_timer(nxpdev
);
491 cancel_work_sync(&psdata
->work
);
492 mutex_destroy(&psdata
->ps_lock
);
495 static int send_ps_cmd(struct hci_dev
*hdev
, void *data
)
497 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
498 struct ps_data
*psdata
= &nxpdev
->psdata
;
499 struct psmode_cmd_payload pcmd
;
503 if (psdata
->target_ps_mode
== PS_MODE_ENABLE
)
504 pcmd
.ps_cmd
= BT_PS_ENABLE
;
506 pcmd
.ps_cmd
= BT_PS_DISABLE
;
507 pcmd
.c2h_ps_interval
= __cpu_to_le16(psdata
->c2h_ps_interval
);
509 skb
= nxp_drv_send_cmd(hdev
, HCI_NXP_AUTO_SLEEP_MODE
, sizeof(pcmd
), &pcmd
);
511 bt_dev_err(hdev
, "Setting Power Save mode failed (%ld)", PTR_ERR(skb
));
515 status
= skb_pull_data(skb
, 1);
518 psdata
->cur_psmode
= psdata
->target_ps_mode
;
520 psdata
->target_ps_mode
= psdata
->cur_psmode
;
521 if (psdata
->cur_psmode
== PS_MODE_ENABLE
)
522 ps_start_timer(nxpdev
);
525 bt_dev_dbg(hdev
, "Power Save mode response: status=%d, ps_mode=%d",
526 *status
, psdata
->cur_psmode
);
533 static int send_wakeup_method_cmd(struct hci_dev
*hdev
, void *data
)
535 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
536 struct ps_data
*psdata
= &nxpdev
->psdata
;
537 struct wakeup_cmd_payload pcmd
;
541 pcmd
.c2h_wakeupmode
= psdata
->c2h_wakeupmode
;
542 pcmd
.c2h_wakeup_gpio
= psdata
->c2h_wakeup_gpio
;
543 switch (psdata
->h2c_wakeupmode
) {
544 case WAKEUP_METHOD_GPIO
:
545 pcmd
.h2c_wakeupmode
= BT_CTRL_WAKEUP_METHOD_GPIO
;
547 case WAKEUP_METHOD_DTR
:
548 pcmd
.h2c_wakeupmode
= BT_CTRL_WAKEUP_METHOD_DSR
;
550 case WAKEUP_METHOD_BREAK
:
552 pcmd
.h2c_wakeupmode
= BT_CTRL_WAKEUP_METHOD_BREAK
;
555 pcmd
.h2c_wakeup_gpio
= 0xff;
557 skb
= nxp_drv_send_cmd(hdev
, HCI_NXP_WAKEUP_METHOD
, sizeof(pcmd
), &pcmd
);
559 bt_dev_err(hdev
, "Setting wake-up method failed (%ld)", PTR_ERR(skb
));
563 status
= skb_pull_data(skb
, 1);
566 psdata
->cur_h2c_wakeupmode
= psdata
->h2c_wakeupmode
;
568 psdata
->h2c_wakeupmode
= psdata
->cur_h2c_wakeupmode
;
569 bt_dev_dbg(hdev
, "Set Wakeup Method response: status=%d, h2c_wakeupmode=%d",
570 *status
, psdata
->cur_h2c_wakeupmode
);
577 static void ps_init(struct hci_dev
*hdev
)
579 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
580 struct ps_data
*psdata
= &nxpdev
->psdata
;
581 u8 default_h2c_wakeup_mode
= DEFAULT_H2C_WAKEUP_MODE
;
583 serdev_device_set_tiocm(nxpdev
->serdev
, 0, TIOCM_RTS
);
584 usleep_range(5000, 10000);
585 serdev_device_set_tiocm(nxpdev
->serdev
, TIOCM_RTS
, 0);
586 usleep_range(5000, 10000);
588 psdata
->ps_state
= PS_STATE_AWAKE
;
589 psdata
->c2h_wakeupmode
= BT_HOST_WAKEUP_METHOD_NONE
;
590 psdata
->c2h_wakeup_gpio
= 0xff;
592 psdata
->cur_h2c_wakeupmode
= WAKEUP_METHOD_INVALID
;
593 if (psdata
->h2c_ps_gpio
)
594 default_h2c_wakeup_mode
= WAKEUP_METHOD_GPIO
;
596 psdata
->h2c_ps_interval
= PS_DEFAULT_TIMEOUT_PERIOD_MS
;
598 switch (default_h2c_wakeup_mode
) {
599 case WAKEUP_METHOD_GPIO
:
600 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_GPIO
;
601 gpiod_set_value_cansleep(psdata
->h2c_ps_gpio
, 0);
602 usleep_range(5000, 10000);
604 case WAKEUP_METHOD_DTR
:
605 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_DTR
;
606 serdev_device_set_tiocm(nxpdev
->serdev
, 0, TIOCM_DTR
);
607 serdev_device_set_tiocm(nxpdev
->serdev
, TIOCM_DTR
, 0);
609 case WAKEUP_METHOD_BREAK
:
611 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_BREAK
;
612 serdev_device_break_ctl(nxpdev
->serdev
, -1);
613 usleep_range(5000, 10000);
614 serdev_device_break_ctl(nxpdev
->serdev
, 0);
615 usleep_range(5000, 10000);
619 psdata
->cur_psmode
= PS_MODE_DISABLE
;
620 psdata
->target_ps_mode
= DEFAULT_PS_MODE
;
622 if (psdata
->cur_h2c_wakeupmode
!= psdata
->h2c_wakeupmode
)
623 hci_cmd_sync_queue(hdev
, send_wakeup_method_cmd
, NULL
, NULL
);
624 if (psdata
->cur_psmode
!= psdata
->target_ps_mode
)
625 hci_cmd_sync_queue(hdev
, send_ps_cmd
, NULL
, NULL
);
628 /* NXP Firmware Download Feature */
629 static int nxp_download_firmware(struct hci_dev
*hdev
)
631 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
634 nxpdev
->fw_dnld_v1_offset
= 0;
635 nxpdev
->fw_v1_sent_bytes
= 0;
636 nxpdev
->fw_v1_expected_len
= HDR_LEN
;
637 nxpdev
->boot_reg_offset
= 0;
638 nxpdev
->fw_dnld_v3_offset
= 0;
639 nxpdev
->fw_v3_offset_correction
= 0;
640 nxpdev
->baudrate_changed
= false;
641 nxpdev
->timeout_changed
= false;
642 nxpdev
->helper_downloaded
= false;
644 serdev_device_set_baudrate(nxpdev
->serdev
, HCI_NXP_PRI_BAUDRATE
);
645 serdev_device_set_flow_control(nxpdev
->serdev
, false);
646 nxpdev
->current_baudrate
= HCI_NXP_PRI_BAUDRATE
;
648 /* Wait till FW is downloaded */
649 err
= wait_event_interruptible_timeout(nxpdev
->fw_dnld_done_wait_q
,
650 !test_bit(BTNXPUART_FW_DOWNLOADING
,
652 msecs_to_jiffies(60000));
654 release_firmware(nxpdev
->fw
);
655 memset(nxpdev
->fw_name
, 0, sizeof(nxpdev
->fw_name
));
658 bt_dev_err(hdev
, "FW Download Timeout. offset: %d",
659 nxpdev
->fw_dnld_v1_offset
?
660 nxpdev
->fw_dnld_v1_offset
:
661 nxpdev
->fw_dnld_v3_offset
);
664 if (test_bit(BTNXPUART_FW_DOWNLOAD_ABORT
, &nxpdev
->tx_state
)) {
665 bt_dev_err(hdev
, "FW Download Aborted");
669 serdev_device_set_flow_control(nxpdev
->serdev
, true);
671 /* Allow the downloaded FW to initialize */
677 static void nxp_send_ack(u8 ack
, struct hci_dev
*hdev
)
679 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
684 if (ack
== NXP_ACK_V3
) {
685 ack_nak
[1] = crc8(crc8_table
, ack_nak
, 1, 0xff);
688 serdev_device_write_buf(nxpdev
->serdev
, ack_nak
, len
);
691 static bool nxp_fw_change_baudrate(struct hci_dev
*hdev
, u16 req_len
)
693 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
694 struct nxp_bootloader_cmd nxp_cmd5
;
695 struct uart_config uart_config
;
696 u32 clkdivaddr
= CLKDIVADDR
- nxpdev
->boot_reg_offset
;
697 u32 uartdivaddr
= UARTDIVADDR
- nxpdev
->boot_reg_offset
;
698 u32 uartmcraddr
= UARTMCRADDR
- nxpdev
->boot_reg_offset
;
699 u32 uartreinitaddr
= UARTREINITADDR
- nxpdev
->boot_reg_offset
;
700 u32 uarticraddr
= UARTICRADDR
- nxpdev
->boot_reg_offset
;
701 u32 uartfcraddr
= UARTFCRADDR
- nxpdev
->boot_reg_offset
;
703 if (req_len
== sizeof(nxp_cmd5
)) {
704 nxp_cmd5
.header
= __cpu_to_le32(5);
706 nxp_cmd5
.payload_len
= __cpu_to_le32(sizeof(uart_config
));
707 /* FW expects swapped CRC bytes */
708 nxp_cmd5
.crc
= __cpu_to_be32(crc32_be(0UL, (char *)&nxp_cmd5
,
709 sizeof(nxp_cmd5
) - 4));
711 serdev_device_write_buf(nxpdev
->serdev
, (u8
*)&nxp_cmd5
, sizeof(nxp_cmd5
));
712 nxpdev
->fw_v3_offset_correction
+= req_len
;
713 } else if (req_len
== sizeof(uart_config
)) {
714 uart_config
.clkdiv
.address
= __cpu_to_le32(clkdivaddr
);
715 uart_config
.clkdiv
.value
= __cpu_to_le32(0x00c00000);
716 uart_config
.uartdiv
.address
= __cpu_to_le32(uartdivaddr
);
717 uart_config
.uartdiv
.value
= __cpu_to_le32(1);
718 uart_config
.mcr
.address
= __cpu_to_le32(uartmcraddr
);
719 uart_config
.mcr
.value
= __cpu_to_le32(MCR
);
720 uart_config
.re_init
.address
= __cpu_to_le32(uartreinitaddr
);
721 uart_config
.re_init
.value
= __cpu_to_le32(INIT
);
722 uart_config
.icr
.address
= __cpu_to_le32(uarticraddr
);
723 uart_config
.icr
.value
= __cpu_to_le32(ICR
);
724 uart_config
.fcr
.address
= __cpu_to_le32(uartfcraddr
);
725 uart_config
.fcr
.value
= __cpu_to_le32(FCR
);
726 /* FW expects swapped CRC bytes */
727 uart_config
.crc
= __cpu_to_be32(crc32_be(0UL, (char *)&uart_config
,
728 sizeof(uart_config
) - 4));
730 serdev_device_write_buf(nxpdev
->serdev
, (u8
*)&uart_config
, sizeof(uart_config
));
731 serdev_device_wait_until_sent(nxpdev
->serdev
, 0);
732 nxpdev
->fw_v3_offset_correction
+= req_len
;
738 static bool nxp_fw_change_timeout(struct hci_dev
*hdev
, u16 req_len
)
740 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
741 struct nxp_bootloader_cmd nxp_cmd7
;
743 if (req_len
!= sizeof(nxp_cmd7
))
746 nxp_cmd7
.header
= __cpu_to_le32(7);
747 nxp_cmd7
.arg
= __cpu_to_le32(0x70);
748 nxp_cmd7
.payload_len
= 0;
749 /* FW expects swapped CRC bytes */
750 nxp_cmd7
.crc
= __cpu_to_be32(crc32_be(0UL, (char *)&nxp_cmd7
,
751 sizeof(nxp_cmd7
) - 4));
752 serdev_device_write_buf(nxpdev
->serdev
, (u8
*)&nxp_cmd7
, sizeof(nxp_cmd7
));
753 serdev_device_wait_until_sent(nxpdev
->serdev
, 0);
754 nxpdev
->fw_v3_offset_correction
+= req_len
;
758 static u32
nxp_get_data_len(const u8
*buf
)
760 struct nxp_bootloader_cmd
*hdr
= (struct nxp_bootloader_cmd
*)buf
;
762 return __le32_to_cpu(hdr
->payload_len
);
765 static bool is_fw_downloading(struct btnxpuart_dev
*nxpdev
)
767 return test_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
770 static bool process_boot_signature(struct btnxpuart_dev
*nxpdev
)
772 if (test_bit(BTNXPUART_CHECK_BOOT_SIGNATURE
, &nxpdev
->tx_state
)) {
773 clear_bit(BTNXPUART_CHECK_BOOT_SIGNATURE
, &nxpdev
->tx_state
);
774 wake_up_interruptible(&nxpdev
->check_boot_sign_wait_q
);
777 return is_fw_downloading(nxpdev
);
780 static int nxp_request_firmware(struct hci_dev
*hdev
, const char *fw_name
,
781 const char *fw_name_old
)
783 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
784 const char *fw_name_dt
;
790 if (!strlen(nxpdev
->fw_name
)) {
791 if (strcmp(fw_name
, FIRMWARE_HELPER
) &&
792 !device_property_read_string(&nxpdev
->serdev
->dev
,
795 fw_name
= fw_name_dt
;
796 snprintf(nxpdev
->fw_name
, MAX_FW_FILE_NAME_LEN
, "nxp/%s", fw_name
);
797 err
= request_firmware_direct(&nxpdev
->fw
, nxpdev
->fw_name
, &hdev
->dev
);
798 if (err
< 0 && fw_name_old
) {
799 snprintf(nxpdev
->fw_name
, MAX_FW_FILE_NAME_LEN
, "nxp/%s", fw_name_old
);
800 err
= request_firmware_direct(&nxpdev
->fw
, nxpdev
->fw_name
, &hdev
->dev
);
803 bt_dev_info(hdev
, "Request Firmware: %s", nxpdev
->fw_name
);
805 bt_dev_err(hdev
, "Firmware file %s not found", nxpdev
->fw_name
);
806 clear_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
812 /* for legacy chipsets with V1 bootloader */
813 static int nxp_recv_chip_ver_v1(struct hci_dev
*hdev
, struct sk_buff
*skb
)
815 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
816 struct v1_start_ind
*req
;
819 req
= skb_pull_data(skb
, sizeof(*req
));
823 chip_id
= le16_to_cpu(req
->chip_id
^ req
->chip_id_comp
);
824 if (chip_id
== 0xffff && nxpdev
->fw_dnld_v1_offset
) {
825 nxpdev
->fw_dnld_v1_offset
= 0;
826 nxpdev
->fw_v1_sent_bytes
= 0;
827 nxpdev
->fw_v1_expected_len
= HDR_LEN
;
828 release_firmware(nxpdev
->fw
);
829 memset(nxpdev
->fw_name
, 0, sizeof(nxpdev
->fw_name
));
830 nxp_send_ack(NXP_ACK_V1
, hdev
);
838 static int nxp_recv_fw_req_v1(struct hci_dev
*hdev
, struct sk_buff
*skb
)
840 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
841 struct btnxpuart_data
*nxp_data
= nxpdev
->nxp_data
;
842 struct v1_data_req
*req
;
845 if (!process_boot_signature(nxpdev
))
848 req
= skb_pull_data(skb
, sizeof(*req
));
852 len
= __le16_to_cpu(req
->len
^ req
->len_comp
);
854 bt_dev_dbg(hdev
, "ERR: Send NAK");
855 nxp_send_ack(NXP_NAK_V1
, hdev
);
858 nxp_send_ack(NXP_ACK_V1
, hdev
);
860 len
= __le16_to_cpu(req
->len
);
862 if (!nxp_data
->helper_fw_name
) {
863 if (!nxpdev
->timeout_changed
) {
864 nxpdev
->timeout_changed
= nxp_fw_change_timeout(hdev
,
868 if (!nxpdev
->baudrate_changed
) {
869 nxpdev
->baudrate_changed
= nxp_fw_change_baudrate(hdev
,
871 if (nxpdev
->baudrate_changed
) {
872 serdev_device_set_baudrate(nxpdev
->serdev
,
873 HCI_NXP_SEC_BAUDRATE
);
874 serdev_device_set_flow_control(nxpdev
->serdev
, true);
875 nxpdev
->current_baudrate
= HCI_NXP_SEC_BAUDRATE
;
881 if (!nxp_data
->helper_fw_name
|| nxpdev
->helper_downloaded
) {
882 if (nxp_request_firmware(hdev
, nxp_data
->fw_name
, nxp_data
->fw_name_old
))
884 } else if (nxp_data
->helper_fw_name
&& !nxpdev
->helper_downloaded
) {
885 if (nxp_request_firmware(hdev
, nxp_data
->helper_fw_name
, NULL
))
890 bt_dev_info(hdev
, "FW Download Complete: %zu bytes",
892 if (nxp_data
->helper_fw_name
&& !nxpdev
->helper_downloaded
) {
893 nxpdev
->helper_downloaded
= true;
894 serdev_device_wait_until_sent(nxpdev
->serdev
, 0);
895 serdev_device_set_baudrate(nxpdev
->serdev
,
896 HCI_NXP_SEC_BAUDRATE
);
897 serdev_device_set_flow_control(nxpdev
->serdev
, true);
899 clear_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
900 wake_up_interruptible(&nxpdev
->fw_dnld_done_wait_q
);
905 /* The CRC did not match at the other end.
906 * Simply send the same bytes again.
908 len
= nxpdev
->fw_v1_sent_bytes
;
909 bt_dev_dbg(hdev
, "CRC error. Resend %d bytes of FW.", len
);
911 nxpdev
->fw_dnld_v1_offset
+= nxpdev
->fw_v1_sent_bytes
;
913 /* The FW bin file is made up of many blocks of
914 * 16 byte header and payload data chunks. If the
915 * FW has requested a header, read the payload length
916 * info from the header, before sending the header.
917 * In the next iteration, the FW should request the
918 * payload data chunk, which should be equal to the
919 * payload length read from header. If there is a
920 * mismatch, clearly the driver and FW are out of sync,
921 * and we need to re-send the previous header again.
923 if (len
== nxpdev
->fw_v1_expected_len
) {
925 nxpdev
->fw_v1_expected_len
= nxp_get_data_len(nxpdev
->fw
->data
+
926 nxpdev
->fw_dnld_v1_offset
);
928 nxpdev
->fw_v1_expected_len
= HDR_LEN
;
929 } else if (len
== HDR_LEN
) {
930 /* FW download out of sync. Send previous chunk again */
931 nxpdev
->fw_dnld_v1_offset
-= nxpdev
->fw_v1_sent_bytes
;
932 nxpdev
->fw_v1_expected_len
= HDR_LEN
;
936 if (nxpdev
->fw_dnld_v1_offset
+ len
<= nxpdev
->fw
->size
)
937 serdev_device_write_buf(nxpdev
->serdev
, nxpdev
->fw
->data
+
938 nxpdev
->fw_dnld_v1_offset
, len
);
939 nxpdev
->fw_v1_sent_bytes
= len
;
946 static char *nxp_get_fw_name_from_chipid(struct hci_dev
*hdev
, u16 chipid
,
949 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
950 char *fw_name
= NULL
;
954 fw_name
= FIRMWARE_W9098
;
957 fw_name
= FIRMWARE_IW416
;
960 fw_name
= FIRMWARE_IW612
;
964 nxpdev
->boot_reg_offset
= 1;
965 if ((loader_ver
& FW_SECURE_MASK
) == FW_OPEN
)
966 fw_name
= FIRMWARE_IW624
;
967 else if ((loader_ver
& FW_SECURE_MASK
) != FW_AUTH_ILLEGAL
)
968 fw_name
= FIRMWARE_SECURE_IW624
;
970 bt_dev_err(hdev
, "Illegal loader version %02x", loader_ver
);
972 case CHIP_ID_AW693a0
:
973 if ((loader_ver
& FW_SECURE_MASK
) == FW_OPEN
)
974 fw_name
= FIRMWARE_AW693
;
975 else if ((loader_ver
& FW_SECURE_MASK
) != FW_AUTH_ILLEGAL
)
976 fw_name
= FIRMWARE_SECURE_AW693
;
978 bt_dev_err(hdev
, "Illegal loader version %02x", loader_ver
);
980 case CHIP_ID_AW693a1
:
981 if ((loader_ver
& FW_SECURE_MASK
) == FW_OPEN
)
982 fw_name
= FIRMWARE_AW693_A1
;
983 else if ((loader_ver
& FW_SECURE_MASK
) != FW_AUTH_ILLEGAL
)
984 fw_name
= FIRMWARE_SECURE_AW693_A1
;
986 bt_dev_err(hdev
, "Illegal loader version %02x", loader_ver
);
988 case CHIP_ID_IW610a0
:
989 case CHIP_ID_IW610a1
:
990 if ((loader_ver
& FW_SECURE_MASK
) == FW_OPEN
)
991 fw_name
= FIRMWARE_IW610
;
992 else if ((loader_ver
& FW_SECURE_MASK
) != FW_AUTH_ILLEGAL
)
993 fw_name
= FIRMWARE_SECURE_IW610
;
995 bt_dev_err(hdev
, "Illegal loader version %02x", loader_ver
);
998 bt_dev_err(hdev
, "Unknown chip signature %04x", chipid
);
1004 static char *nxp_get_old_fw_name_from_chipid(struct hci_dev
*hdev
, u16 chipid
,
1007 char *fw_name_old
= NULL
;
1011 fw_name_old
= FIRMWARE_W9098_OLD
;
1014 fw_name_old
= FIRMWARE_IW416_OLD
;
1020 static int nxp_recv_chip_ver_v3(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1022 struct v3_start_ind
*req
= skb_pull_data(skb
, sizeof(*req
));
1023 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1024 const char *fw_name
;
1025 const char *fw_name_old
;
1029 if (!process_boot_signature(nxpdev
))
1032 chip_id
= le16_to_cpu(req
->chip_id
);
1033 loader_ver
= req
->loader_ver
;
1034 bt_dev_info(hdev
, "ChipID: %04x, Version: %d", chip_id
, loader_ver
);
1035 fw_name
= nxp_get_fw_name_from_chipid(hdev
, chip_id
, loader_ver
);
1036 fw_name_old
= nxp_get_old_fw_name_from_chipid(hdev
, chip_id
, loader_ver
);
1037 if (!nxp_request_firmware(hdev
, fw_name
, fw_name_old
))
1038 nxp_send_ack(NXP_ACK_V3
, hdev
);
1045 static void nxp_handle_fw_download_error(struct hci_dev
*hdev
, struct v3_data_req
*req
)
1047 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1048 __u32 offset
= __le32_to_cpu(req
->offset
);
1049 __u16 err
= __le16_to_cpu(req
->error
);
1050 union nxp_v3_rx_timeout_nak_u nak_tx_buf
;
1053 case NXP_ACK_RX_TIMEOUT
:
1054 case NXP_HDR_RX_TIMEOUT
:
1055 case NXP_DATA_RX_TIMEOUT
:
1056 nak_tx_buf
.pkt
.nak
= NXP_NAK_V3
;
1057 nak_tx_buf
.pkt
.offset
= __cpu_to_le32(offset
);
1058 nak_tx_buf
.pkt
.crc
= crc8(crc8_table
, nak_tx_buf
.buf
,
1059 sizeof(nak_tx_buf
) - 1, 0xff);
1060 serdev_device_write_buf(nxpdev
->serdev
, nak_tx_buf
.buf
,
1061 sizeof(nak_tx_buf
));
1064 bt_dev_dbg(hdev
, "Unknown bootloader error code: %d", err
);
1071 static int nxp_recv_fw_req_v3(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1073 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1074 struct v3_data_req
*req
;
1078 if (!process_boot_signature(nxpdev
))
1081 req
= skb_pull_data(skb
, sizeof(*req
));
1082 if (!req
|| !nxpdev
->fw
)
1086 nxp_send_ack(NXP_ACK_V3
, hdev
);
1088 nxp_handle_fw_download_error(hdev
, req
);
1092 len
= __le16_to_cpu(req
->len
);
1094 if (!nxpdev
->timeout_changed
) {
1095 nxpdev
->timeout_changed
= nxp_fw_change_timeout(hdev
, len
);
1099 if (!nxpdev
->baudrate_changed
) {
1100 nxpdev
->baudrate_changed
= nxp_fw_change_baudrate(hdev
, len
);
1101 if (nxpdev
->baudrate_changed
) {
1102 serdev_device_set_baudrate(nxpdev
->serdev
,
1103 HCI_NXP_SEC_BAUDRATE
);
1104 serdev_device_set_flow_control(nxpdev
->serdev
, true);
1105 nxpdev
->current_baudrate
= HCI_NXP_SEC_BAUDRATE
;
1110 if (req
->len
== 0) {
1111 bt_dev_info(hdev
, "FW Download Complete: %zu bytes",
1113 clear_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
1114 wake_up_interruptible(&nxpdev
->fw_dnld_done_wait_q
);
1118 offset
= __le32_to_cpu(req
->offset
);
1119 if (offset
< nxpdev
->fw_v3_offset_correction
) {
1120 /* This scenario should ideally never occur. But if it ever does,
1121 * FW is out of sync and needs a power cycle.
1123 bt_dev_err(hdev
, "Something went wrong during FW download");
1124 bt_dev_err(hdev
, "Please power cycle and try again");
1128 nxpdev
->fw_dnld_v3_offset
= offset
- nxpdev
->fw_v3_offset_correction
;
1129 serdev_device_write_buf(nxpdev
->serdev
, nxpdev
->fw
->data
+
1130 nxpdev
->fw_dnld_v3_offset
, len
);
1137 static int nxp_set_baudrate_cmd(struct hci_dev
*hdev
, void *data
)
1139 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1140 __le32 new_baudrate
= __cpu_to_le32(nxpdev
->new_baudrate
);
1141 struct ps_data
*psdata
= &nxpdev
->psdata
;
1142 struct sk_buff
*skb
;
1148 skb
= nxp_drv_send_cmd(hdev
, HCI_NXP_SET_OPER_SPEED
, 4, (u8
*)&new_baudrate
);
1150 bt_dev_err(hdev
, "Setting baudrate failed (%ld)", PTR_ERR(skb
));
1151 return PTR_ERR(skb
);
1154 status
= (u8
*)skb_pull_data(skb
, 1);
1157 serdev_device_set_baudrate(nxpdev
->serdev
, nxpdev
->new_baudrate
);
1158 nxpdev
->current_baudrate
= nxpdev
->new_baudrate
;
1160 bt_dev_dbg(hdev
, "Set baudrate response: status=%d, baudrate=%d",
1161 *status
, nxpdev
->new_baudrate
);
1168 static int nxp_check_boot_sign(struct btnxpuart_dev
*nxpdev
)
1170 serdev_device_set_baudrate(nxpdev
->serdev
, HCI_NXP_PRI_BAUDRATE
);
1171 if (test_bit(BTNXPUART_IR_IN_PROGRESS
, &nxpdev
->tx_state
))
1172 serdev_device_set_flow_control(nxpdev
->serdev
, false);
1174 serdev_device_set_flow_control(nxpdev
->serdev
, true);
1175 set_bit(BTNXPUART_CHECK_BOOT_SIGNATURE
, &nxpdev
->tx_state
);
1177 return wait_event_interruptible_timeout(nxpdev
->check_boot_sign_wait_q
,
1178 !test_bit(BTNXPUART_CHECK_BOOT_SIGNATURE
,
1180 msecs_to_jiffies(1000));
1183 static int nxp_set_ind_reset(struct hci_dev
*hdev
, void *data
)
1185 static const u8 ir_hw_err
[] = { HCI_EV_HARDWARE_ERROR
,
1186 0x01, BTNXPUART_IR_HW_ERR
};
1187 struct sk_buff
*skb
;
1189 skb
= bt_skb_alloc(3, GFP_ATOMIC
);
1193 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
1194 skb_put_data(skb
, ir_hw_err
, 3);
1196 /* Inject Hardware Error to upper stack */
1197 return hci_recv_frame(hdev
, skb
);
1201 static int nxp_setup(struct hci_dev
*hdev
)
1203 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1206 if (nxp_check_boot_sign(nxpdev
)) {
1207 bt_dev_dbg(hdev
, "Need FW Download.");
1208 err
= nxp_download_firmware(hdev
);
1212 bt_dev_info(hdev
, "FW already running.");
1213 clear_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
1216 serdev_device_set_baudrate(nxpdev
->serdev
, nxpdev
->fw_init_baudrate
);
1217 nxpdev
->current_baudrate
= nxpdev
->fw_init_baudrate
;
1219 if (nxpdev
->current_baudrate
!= HCI_NXP_SEC_BAUDRATE
) {
1220 nxpdev
->new_baudrate
= HCI_NXP_SEC_BAUDRATE
;
1221 hci_cmd_sync_queue(hdev
, nxp_set_baudrate_cmd
, NULL
, NULL
);
1226 if (test_and_clear_bit(BTNXPUART_IR_IN_PROGRESS
, &nxpdev
->tx_state
))
1227 hci_dev_clear_flag(hdev
, HCI_SETUP
);
1232 static void nxp_hw_err(struct hci_dev
*hdev
, u8 code
)
1234 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1237 case BTNXPUART_IR_HW_ERR
:
1238 set_bit(BTNXPUART_IR_IN_PROGRESS
, &nxpdev
->tx_state
);
1239 hci_dev_set_flag(hdev
, HCI_SETUP
);
1246 static int nxp_shutdown(struct hci_dev
*hdev
)
1248 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1249 struct sk_buff
*skb
;
1253 if (test_bit(BTNXPUART_IR_IN_PROGRESS
, &nxpdev
->tx_state
)) {
1254 skb
= nxp_drv_send_cmd(hdev
, HCI_NXP_IND_RESET
, 1, &pcmd
);
1256 return PTR_ERR(skb
);
1258 status
= skb_pull_data(skb
, 1);
1260 serdev_device_set_flow_control(nxpdev
->serdev
, false);
1261 set_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
1269 static int btnxpuart_queue_skb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1271 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1273 /* Prepend skb with frame type */
1274 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
1275 skb_queue_tail(&nxpdev
->txq
, skb
);
1276 btnxpuart_tx_wakeup(nxpdev
);
1280 static int nxp_enqueue(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1282 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1283 struct ps_data
*psdata
= &nxpdev
->psdata
;
1284 struct hci_command_hdr
*hdr
;
1285 struct psmode_cmd_payload ps_parm
;
1286 struct wakeup_cmd_payload wakeup_parm
;
1287 __le32 baudrate_parm
;
1289 /* if vendor commands are received from user space (e.g. hcitool), update
1290 * driver flags accordingly and ask driver to re-send the command to FW.
1291 * In case the payload for any command does not match expected payload
1292 * length, let the firmware and user space program handle it, or throw
1295 if (bt_cb(skb
)->pkt_type
== HCI_COMMAND_PKT
&& !psdata
->driver_sent_cmd
) {
1296 hdr
= (struct hci_command_hdr
*)skb
->data
;
1297 if (hdr
->plen
!= (skb
->len
- HCI_COMMAND_HDR_SIZE
))
1298 return btnxpuart_queue_skb(hdev
, skb
);
1300 switch (__le16_to_cpu(hdr
->opcode
)) {
1301 case HCI_NXP_AUTO_SLEEP_MODE
:
1302 if (hdr
->plen
== sizeof(ps_parm
)) {
1303 memcpy(&ps_parm
, skb
->data
+ HCI_COMMAND_HDR_SIZE
, hdr
->plen
);
1304 if (ps_parm
.ps_cmd
== BT_PS_ENABLE
)
1305 psdata
->target_ps_mode
= PS_MODE_ENABLE
;
1306 else if (ps_parm
.ps_cmd
== BT_PS_DISABLE
)
1307 psdata
->target_ps_mode
= PS_MODE_DISABLE
;
1308 psdata
->c2h_ps_interval
= __le16_to_cpu(ps_parm
.c2h_ps_interval
);
1309 hci_cmd_sync_queue(hdev
, send_ps_cmd
, NULL
, NULL
);
1313 case HCI_NXP_WAKEUP_METHOD
:
1314 if (hdr
->plen
== sizeof(wakeup_parm
)) {
1315 memcpy(&wakeup_parm
, skb
->data
+ HCI_COMMAND_HDR_SIZE
, hdr
->plen
);
1316 psdata
->c2h_wakeupmode
= wakeup_parm
.c2h_wakeupmode
;
1317 psdata
->c2h_wakeup_gpio
= wakeup_parm
.c2h_wakeup_gpio
;
1318 psdata
->h2c_wakeup_gpio
= wakeup_parm
.h2c_wakeup_gpio
;
1319 switch (wakeup_parm
.h2c_wakeupmode
) {
1320 case BT_CTRL_WAKEUP_METHOD_GPIO
:
1321 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_GPIO
;
1323 case BT_CTRL_WAKEUP_METHOD_DSR
:
1324 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_DTR
;
1326 case BT_CTRL_WAKEUP_METHOD_BREAK
:
1328 psdata
->h2c_wakeupmode
= WAKEUP_METHOD_BREAK
;
1331 hci_cmd_sync_queue(hdev
, send_wakeup_method_cmd
, NULL
, NULL
);
1335 case HCI_NXP_SET_OPER_SPEED
:
1336 if (hdr
->plen
== sizeof(baudrate_parm
)) {
1337 memcpy(&baudrate_parm
, skb
->data
+ HCI_COMMAND_HDR_SIZE
, hdr
->plen
);
1338 nxpdev
->new_baudrate
= __le32_to_cpu(baudrate_parm
);
1339 hci_cmd_sync_queue(hdev
, nxp_set_baudrate_cmd
, NULL
, NULL
);
1343 case HCI_NXP_IND_RESET
:
1344 if (hdr
->plen
== 1) {
1345 hci_cmd_sync_queue(hdev
, nxp_set_ind_reset
, NULL
, NULL
);
1354 return btnxpuart_queue_skb(hdev
, skb
);
1361 static struct sk_buff
*nxp_dequeue(void *data
)
1363 struct btnxpuart_dev
*nxpdev
= (struct btnxpuart_dev
*)data
;
1365 ps_start_timer(nxpdev
);
1366 return skb_dequeue(&nxpdev
->txq
);
1369 /* btnxpuart based on serdev */
1370 static void btnxpuart_tx_work(struct work_struct
*work
)
1372 struct btnxpuart_dev
*nxpdev
= container_of(work
, struct btnxpuart_dev
,
1374 struct serdev_device
*serdev
= nxpdev
->serdev
;
1375 struct hci_dev
*hdev
= nxpdev
->hdev
;
1376 struct sk_buff
*skb
;
1379 if (ps_wakeup(nxpdev
))
1382 while ((skb
= nxp_dequeue(nxpdev
))) {
1383 len
= serdev_device_write_buf(serdev
, skb
->data
, skb
->len
);
1384 hdev
->stat
.byte_tx
+= len
;
1388 skb_queue_head(&nxpdev
->txq
, skb
);
1392 switch (hci_skb_pkt_type(skb
)) {
1393 case HCI_COMMAND_PKT
:
1394 hdev
->stat
.cmd_tx
++;
1396 case HCI_ACLDATA_PKT
:
1397 hdev
->stat
.acl_tx
++;
1399 case HCI_SCODATA_PKT
:
1400 hdev
->stat
.sco_tx
++;
1406 clear_bit(BTNXPUART_TX_STATE_ACTIVE
, &nxpdev
->tx_state
);
1409 static int btnxpuart_open(struct hci_dev
*hdev
)
1411 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1414 err
= serdev_device_open(nxpdev
->serdev
);
1416 bt_dev_err(hdev
, "Unable to open UART device %s",
1417 dev_name(&nxpdev
->serdev
->dev
));
1419 set_bit(BTNXPUART_SERDEV_OPEN
, &nxpdev
->tx_state
);
1424 static int btnxpuart_close(struct hci_dev
*hdev
)
1426 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1428 serdev_device_close(nxpdev
->serdev
);
1429 skb_queue_purge(&nxpdev
->txq
);
1430 if (!IS_ERR_OR_NULL(nxpdev
->rx_skb
)) {
1431 kfree_skb(nxpdev
->rx_skb
);
1432 nxpdev
->rx_skb
= NULL
;
1434 clear_bit(BTNXPUART_SERDEV_OPEN
, &nxpdev
->tx_state
);
1438 static int btnxpuart_flush(struct hci_dev
*hdev
)
1440 struct btnxpuart_dev
*nxpdev
= hci_get_drvdata(hdev
);
1442 /* Flush any pending characters */
1443 serdev_device_write_flush(nxpdev
->serdev
);
1444 skb_queue_purge(&nxpdev
->txq
);
1446 cancel_work_sync(&nxpdev
->tx_work
);
1448 if (!IS_ERR_OR_NULL(nxpdev
->rx_skb
)) {
1449 kfree_skb(nxpdev
->rx_skb
);
1450 nxpdev
->rx_skb
= NULL
;
1456 static const struct h4_recv_pkt nxp_recv_pkts
[] = {
1457 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
1458 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
1459 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
1460 { H4_RECV_ISO
, .recv
= hci_recv_frame
},
1461 { NXP_RECV_CHIP_VER_V1
, .recv
= nxp_recv_chip_ver_v1
},
1462 { NXP_RECV_FW_REQ_V1
, .recv
= nxp_recv_fw_req_v1
},
1463 { NXP_RECV_CHIP_VER_V3
, .recv
= nxp_recv_chip_ver_v3
},
1464 { NXP_RECV_FW_REQ_V3
, .recv
= nxp_recv_fw_req_v3
},
1467 static size_t btnxpuart_receive_buf(struct serdev_device
*serdev
,
1468 const u8
*data
, size_t count
)
1470 struct btnxpuart_dev
*nxpdev
= serdev_device_get_drvdata(serdev
);
1472 ps_start_timer(nxpdev
);
1474 nxpdev
->rx_skb
= h4_recv_buf(nxpdev
->hdev
, nxpdev
->rx_skb
, data
, count
,
1475 nxp_recv_pkts
, ARRAY_SIZE(nxp_recv_pkts
));
1476 if (IS_ERR(nxpdev
->rx_skb
)) {
1477 int err
= PTR_ERR(nxpdev
->rx_skb
);
1478 /* Safe to ignore out-of-sync bootloader signatures */
1479 if (!is_fw_downloading(nxpdev
))
1480 bt_dev_err(nxpdev
->hdev
, "Frame reassembly failed (%d)", err
);
1483 if (!is_fw_downloading(nxpdev
))
1484 nxpdev
->hdev
->stat
.byte_rx
+= count
;
1488 static void btnxpuart_write_wakeup(struct serdev_device
*serdev
)
1490 serdev_device_write_wakeup(serdev
);
1493 static const struct serdev_device_ops btnxpuart_client_ops
= {
1494 .receive_buf
= btnxpuart_receive_buf
,
1495 .write_wakeup
= btnxpuart_write_wakeup
,
1498 static int nxp_serdev_probe(struct serdev_device
*serdev
)
1500 struct hci_dev
*hdev
;
1501 struct btnxpuart_dev
*nxpdev
;
1503 nxpdev
= devm_kzalloc(&serdev
->dev
, sizeof(*nxpdev
), GFP_KERNEL
);
1507 nxpdev
->nxp_data
= (struct btnxpuart_data
*)device_get_match_data(&serdev
->dev
);
1509 nxpdev
->serdev
= serdev
;
1510 serdev_device_set_drvdata(serdev
, nxpdev
);
1512 serdev_device_set_client_ops(serdev
, &btnxpuart_client_ops
);
1514 INIT_WORK(&nxpdev
->tx_work
, btnxpuart_tx_work
);
1515 skb_queue_head_init(&nxpdev
->txq
);
1517 init_waitqueue_head(&nxpdev
->fw_dnld_done_wait_q
);
1518 init_waitqueue_head(&nxpdev
->check_boot_sign_wait_q
);
1520 device_property_read_u32(&nxpdev
->serdev
->dev
, "fw-init-baudrate",
1521 &nxpdev
->fw_init_baudrate
);
1522 if (!nxpdev
->fw_init_baudrate
)
1523 nxpdev
->fw_init_baudrate
= FW_INIT_BAUDRATE
;
1525 set_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
1527 crc8_populate_msb(crc8_table
, POLYNOMIAL8
);
1529 /* Initialize and register HCI device */
1530 hdev
= hci_alloc_dev();
1532 dev_err(&serdev
->dev
, "Can't allocate HCI device\n");
1536 nxpdev
->hdev
= hdev
;
1538 hdev
->bus
= HCI_UART
;
1539 hci_set_drvdata(hdev
, nxpdev
);
1541 hdev
->manufacturer
= MANUFACTURER_NXP
;
1542 hdev
->open
= btnxpuart_open
;
1543 hdev
->close
= btnxpuart_close
;
1544 hdev
->flush
= btnxpuart_flush
;
1545 hdev
->setup
= nxp_setup
;
1546 hdev
->send
= nxp_enqueue
;
1547 hdev
->hw_error
= nxp_hw_err
;
1548 hdev
->shutdown
= nxp_shutdown
;
1549 SET_HCIDEV_DEV(hdev
, &serdev
->dev
);
1551 if (hci_register_dev(hdev
) < 0) {
1552 dev_err(&serdev
->dev
, "Can't register HCI device\n");
1566 static void nxp_serdev_remove(struct serdev_device
*serdev
)
1568 struct btnxpuart_dev
*nxpdev
= serdev_device_get_drvdata(serdev
);
1569 struct hci_dev
*hdev
= nxpdev
->hdev
;
1571 if (is_fw_downloading(nxpdev
)) {
1572 set_bit(BTNXPUART_FW_DOWNLOAD_ABORT
, &nxpdev
->tx_state
);
1573 clear_bit(BTNXPUART_FW_DOWNLOADING
, &nxpdev
->tx_state
);
1574 wake_up_interruptible(&nxpdev
->check_boot_sign_wait_q
);
1575 wake_up_interruptible(&nxpdev
->fw_dnld_done_wait_q
);
1577 /* Restore FW baudrate to fw_init_baudrate if changed.
1578 * This will ensure FW baudrate is in sync with
1579 * driver baudrate in case this driver is re-inserted.
1581 if (nxpdev
->current_baudrate
!= nxpdev
->fw_init_baudrate
) {
1582 nxpdev
->new_baudrate
= nxpdev
->fw_init_baudrate
;
1583 nxp_set_baudrate_cmd(hdev
, NULL
);
1587 hci_unregister_dev(hdev
);
1591 #ifdef CONFIG_PM_SLEEP
1592 static int nxp_serdev_suspend(struct device
*dev
)
1594 struct btnxpuart_dev
*nxpdev
= dev_get_drvdata(dev
);
1595 struct ps_data
*psdata
= &nxpdev
->psdata
;
1597 ps_control(psdata
->hdev
, PS_STATE_SLEEP
);
1601 static int nxp_serdev_resume(struct device
*dev
)
1603 struct btnxpuart_dev
*nxpdev
= dev_get_drvdata(dev
);
1604 struct ps_data
*psdata
= &nxpdev
->psdata
;
1606 ps_control(psdata
->hdev
, PS_STATE_AWAKE
);
1611 static struct btnxpuart_data w8987_data __maybe_unused
= {
1612 .helper_fw_name
= NULL
,
1613 .fw_name
= FIRMWARE_W8987
,
1614 .fw_name_old
= FIRMWARE_W8987_OLD
,
1617 static struct btnxpuart_data w8997_data __maybe_unused
= {
1618 .helper_fw_name
= FIRMWARE_HELPER
,
1619 .fw_name
= FIRMWARE_W8997
,
1620 .fw_name_old
= FIRMWARE_W8997_OLD
,
1623 static const struct of_device_id nxpuart_of_match_table
[] __maybe_unused
= {
1624 { .compatible
= "nxp,88w8987-bt", .data
= &w8987_data
},
1625 { .compatible
= "nxp,88w8997-bt", .data
= &w8997_data
},
1628 MODULE_DEVICE_TABLE(of
, nxpuart_of_match_table
);
1630 static const struct dev_pm_ops nxp_pm_ops
= {
1631 SET_SYSTEM_SLEEP_PM_OPS(nxp_serdev_suspend
, nxp_serdev_resume
)
1634 static struct serdev_device_driver nxp_serdev_driver
= {
1635 .probe
= nxp_serdev_probe
,
1636 .remove
= nxp_serdev_remove
,
1638 .name
= "btnxpuart",
1639 .of_match_table
= of_match_ptr(nxpuart_of_match_table
),
1644 module_serdev_device_driver(nxp_serdev_driver
);
1646 MODULE_AUTHOR("Neeraj Sanjay Kale <neeraj.sanjaykale@nxp.com>");
1647 MODULE_DESCRIPTION("NXP Bluetooth Serial driver");
1648 MODULE_LICENSE("GPL");