1 // SPDX-License-Identifier: (GPL-2.0-only OR MIT)
3 * Copyright (C) 2024 Amlogic, Inc. All rights reserved
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/property.h>
11 #include <linux/serdev.h>
12 #include <linux/clk.h>
13 #include <linux/firmware.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <net/bluetooth/bluetooth.h>
17 #include <net/bluetooth/hci_core.h>
18 #include <net/bluetooth/hci.h>
22 #define AML_EVT_HEAD_SIZE 4
23 #define AML_BDADDR_DEFAULT (&(bdaddr_t) {{ 0x00, 0xff, 0x00, 0x22, 0x2d, 0xae }})
25 #define AML_FIRMWARE_OPERATION_SIZE (248)
26 #define AML_FIRMWARE_MAX_SIZE (512 * 1024)
29 #define AML_TCI_CMD_READ 0xFEF0
30 #define AML_TCI_CMD_WRITE 0xFEF1
31 #define AML_TCI_CMD_UPDATE_BAUDRATE 0xFEF2
32 #define AML_TCI_CMD_HARDWARE_RESET 0xFEF2
33 #define AML_TCI_CMD_DOWNLOAD_BT_FW 0xFEF3
36 #define AML_BT_HCI_VENDOR_CMD 0xFC1A
38 /* TCI operation parameter in controller chip */
39 #define AML_OP_UART_MODE 0x00A30128
40 #define AML_OP_EVT_ENABLE 0x00A70014
41 #define AML_OP_MEM_HARD_TRANS_EN 0x00A7000C
42 #define AML_OP_RF_CFG 0x00F03040
43 #define AML_OP_RAM_POWER_CTR 0x00F03050
44 #define AML_OP_HARDWARE_RST 0x00F03058
45 #define AML_OP_ICCM_RAM_BASE 0x00000000
46 #define AML_OP_DCCM_RAM_BASE 0x00D00000
48 /* UART configuration */
49 #define AML_UART_XMIT_EN BIT(12)
50 #define AML_UART_RECV_EN BIT(13)
51 #define AML_UART_TIMEOUT_INT_EN BIT(14)
52 #define AML_UART_CLK_SOURCE 40000000
54 /* Controller event */
55 #define AML_EVT_EN BIT(24)
57 /* RAM power control */
58 #define AML_RAM_POWER_ON (0)
59 #define AML_RAM_POWER_OFF (1)
61 /* RF configuration */
62 #define AML_RF_ANT_SINGLE BIT(28)
63 #define AML_RF_ANT_DOUBLE BIT(29)
65 /* Memory transaction */
66 #define AML_MM_CTR_HARD_TRAS_EN BIT(27)
68 /* Controller reset */
69 #define AML_CTR_CPU_RESET BIT(8)
70 #define AML_CTR_MAC_RESET BIT(9)
71 #define AML_CTR_PHY_RESET BIT(10)
89 struct aml_device_data
{
96 struct hci_uart serdev_hu
;
98 struct gpio_desc
*bt_en_gpio
;
99 struct regulator
*bt_supply
;
101 const struct aml_device_data
*aml_dev_data
;
102 const char *firmware_name
;
106 struct sk_buff
*rx_skb
;
107 struct sk_buff_head txq
;
110 static const struct h4_recv_pkt aml_recv_pkts
[] = {
111 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
112 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
113 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
114 { H4_RECV_ISO
, .recv
= hci_recv_frame
},
117 /* The TCI command is a private command, which is for setting baud rate,
118 * downloading firmware, initiating RAM.
120 * op_code | op_len | op_addr | parameter |
121 * --------|-----------------------|---------|-------------|
122 * 2B | 1B len(addr+param) | 4B | len(param) |
124 static int aml_send_tci_cmd(struct hci_dev
*hdev
, u16 op_code
, u32 op_addr
,
125 u32
*param
, u32 param_len
)
127 struct aml_tci_rsp
*rsp
= NULL
;
128 struct sk_buff
*skb
= NULL
;
133 buf_len
= sizeof(op_addr
) + param_len
;
134 buf
= kmalloc(buf_len
, GFP_KERNEL
);
138 memcpy(buf
, &op_addr
, sizeof(op_addr
));
139 if (param
&& param_len
> 0)
140 memcpy(buf
+ sizeof(op_addr
), param
, param_len
);
142 skb
= __hci_cmd_sync_ev(hdev
, op_code
, buf_len
, buf
,
143 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
146 bt_dev_err(hdev
, "Failed to send TCI cmd (error: %d)", err
);
150 rsp
= skb_pull_data(skb
, sizeof(struct aml_tci_rsp
));
154 if (rsp
->opcode
!= op_code
|| rsp
->status
!= 0x00) {
155 bt_dev_err(hdev
, "send TCI cmd (0x%04X), response (0x%04X):(%d)",
156 op_code
, rsp
->opcode
, rsp
->status
);
169 static int aml_update_chip_baudrate(struct hci_dev
*hdev
, u32 baud
)
173 value
= ((AML_UART_CLK_SOURCE
/ baud
) - 1) & 0x0FFF;
174 value
|= AML_UART_XMIT_EN
| AML_UART_RECV_EN
| AML_UART_TIMEOUT_INT_EN
;
176 return aml_send_tci_cmd(hdev
, AML_TCI_CMD_UPDATE_BAUDRATE
,
177 AML_OP_UART_MODE
, &value
, sizeof(value
));
180 static int aml_start_chip(struct hci_dev
*hdev
)
185 value
= AML_MM_CTR_HARD_TRAS_EN
;
186 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_WRITE
,
187 AML_OP_MEM_HARD_TRANS_EN
,
188 &value
, sizeof(value
));
192 /* controller hardware reset */
193 value
= AML_CTR_CPU_RESET
| AML_CTR_MAC_RESET
| AML_CTR_PHY_RESET
;
194 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_HARDWARE_RESET
,
196 &value
, sizeof(value
));
200 static int aml_send_firmware_segment(struct hci_dev
*hdev
,
208 if (fw_type
== FW_ICCM
)
209 op_addr
= AML_OP_ICCM_RAM_BASE
+ offset
;
210 else if (fw_type
== FW_DCCM
)
211 op_addr
= AML_OP_DCCM_RAM_BASE
+ offset
;
213 return aml_send_tci_cmd(hdev
, AML_TCI_CMD_DOWNLOAD_BT_FW
,
214 op_addr
, (u32
*)seg
, seg_size
);
217 static int aml_send_firmware(struct hci_dev
*hdev
, u8 fw_type
,
218 u8
*fw
, u32 fw_size
, u32 offset
)
223 if (fw_size
> AML_FIRMWARE_MAX_SIZE
) {
225 "Firmware size %d kB is larger than the maximum of 512 kB. Aborting.",
229 while (fw_size
> 0) {
230 seg_size
= (fw_size
> AML_FIRMWARE_OPERATION_SIZE
) ?
231 AML_FIRMWARE_OPERATION_SIZE
: fw_size
;
232 if (aml_send_firmware_segment(hdev
, fw_type
, (fw
+ seg_off
),
234 bt_dev_err(hdev
, "Failed send firmware, type: %d, offset: 0x%x",
245 static int aml_download_firmware(struct hci_dev
*hdev
, const char *fw_name
)
247 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
248 struct aml_serdev
*amldev
= serdev_device_get_drvdata(hu
->serdev
);
249 const struct firmware
*firmware
= NULL
;
250 struct aml_fw_len
*fw_len
= NULL
;
251 u8
*iccm_start
= NULL
, *dccm_start
= NULL
;
252 u32 iccm_len
, dccm_len
;
256 /* Enable firmware download event */
258 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_WRITE
,
260 &value
, sizeof(value
));
265 value
= AML_RAM_POWER_ON
;
266 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_WRITE
,
267 AML_OP_RAM_POWER_CTR
,
268 &value
, sizeof(value
));
272 /* Check RAM power status */
273 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_READ
,
274 AML_OP_RAM_POWER_CTR
, NULL
, 0);
278 ret
= request_firmware(&firmware
, fw_name
, &hdev
->dev
);
280 bt_dev_err(hdev
, "Failed to load <%s>:(%d)", fw_name
, ret
);
284 fw_len
= (struct aml_fw_len
*)firmware
->data
;
287 iccm_start
= (u8
*)(firmware
->data
) + sizeof(struct aml_fw_len
)
288 + amldev
->aml_dev_data
->iccm_offset
;
289 iccm_len
= fw_len
->iccm_len
- amldev
->aml_dev_data
->iccm_offset
;
290 ret
= aml_send_firmware(hdev
, FW_ICCM
, iccm_start
, iccm_len
,
291 amldev
->aml_dev_data
->iccm_offset
);
293 bt_dev_err(hdev
, "Failed to send FW_ICCM (%d)", ret
);
298 dccm_start
= (u8
*)(firmware
->data
) + sizeof(struct aml_fw_len
) + fw_len
->iccm_len
;
299 dccm_len
= fw_len
->dccm_len
;
300 ret
= aml_send_firmware(hdev
, FW_DCCM
, dccm_start
, dccm_len
,
301 amldev
->aml_dev_data
->dccm_offset
);
303 bt_dev_err(hdev
, "Failed to send FW_DCCM (%d)", ret
);
307 /* Disable firmware download event */
309 ret
= aml_send_tci_cmd(hdev
, AML_TCI_CMD_WRITE
,
311 &value
, sizeof(value
));
317 release_firmware(firmware
);
321 static int aml_send_reset(struct hci_dev
*hdev
)
326 skb
= __hci_cmd_sync_ev(hdev
, HCI_OP_RESET
, 0, NULL
,
327 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
330 bt_dev_err(hdev
, "Failed to send hci reset cmd (%d)", err
);
338 static int aml_dump_fw_version(struct hci_dev
*hdev
)
340 struct aml_tci_rsp
*rsp
= NULL
;
346 skb
= __hci_cmd_sync_ev(hdev
, AML_BT_HCI_VENDOR_CMD
, sizeof(value
), value
,
347 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
350 bt_dev_err(hdev
, "Failed to get fw version (error: %d)", err
);
354 rsp
= skb_pull_data(skb
, sizeof(struct aml_tci_rsp
));
358 if (rsp
->opcode
!= AML_BT_HCI_VENDOR_CMD
|| rsp
->status
!= 0x00) {
359 bt_dev_err(hdev
, "dump version, error response (0x%04X):(%d)",
360 rsp
->opcode
, rsp
->status
);
365 fw_ver
= (u8
*)rsp
+ AML_EVT_HEAD_SIZE
;
366 bt_dev_info(hdev
, "fw_version: date = %02x.%02x, number = 0x%02x%02x",
367 *(fw_ver
+ 1), *fw_ver
, *(fw_ver
+ 3), *(fw_ver
+ 2));
374 static int aml_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
376 struct aml_tci_rsp
*rsp
= NULL
;
380 bt_dev_info(hdev
, "set bdaddr (%pM)", bdaddr
);
381 skb
= __hci_cmd_sync_ev(hdev
, AML_BT_HCI_VENDOR_CMD
,
382 sizeof(bdaddr_t
), bdaddr
,
383 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
386 bt_dev_err(hdev
, "Failed to set bdaddr (error: %d)", err
);
390 rsp
= skb_pull_data(skb
, sizeof(struct aml_tci_rsp
));
394 if (rsp
->opcode
!= AML_BT_HCI_VENDOR_CMD
|| rsp
->status
!= 0x00) {
395 bt_dev_err(hdev
, "error response (0x%x):(%d)", rsp
->opcode
, rsp
->status
);
405 static int aml_check_bdaddr(struct hci_dev
*hdev
)
407 struct hci_rp_read_bd_addr
*paddr
;
411 if (bacmp(&hdev
->public_addr
, BDADDR_ANY
))
414 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
418 bt_dev_err(hdev
, "Failed to read bdaddr (error: %d)", err
);
422 paddr
= skb_pull_data(skb
, sizeof(struct hci_rp_read_bd_addr
));
426 if (!bacmp(&paddr
->bdaddr
, AML_BDADDR_DEFAULT
)) {
427 bt_dev_info(hdev
, "amlbt using default bdaddr (%pM)", &paddr
->bdaddr
);
428 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
436 static int aml_config_rf(struct hci_dev
*hdev
, bool is_coex
)
438 u32 value
= AML_RF_ANT_DOUBLE
;
440 /* Use a single antenna when co-existing with wifi */
442 value
= AML_RF_ANT_SINGLE
;
444 return aml_send_tci_cmd(hdev
, AML_TCI_CMD_WRITE
,
446 &value
, sizeof(value
));
449 static int aml_parse_dt(struct aml_serdev
*amldev
)
451 struct device
*pdev
= amldev
->dev
;
453 amldev
->bt_en_gpio
= devm_gpiod_get(pdev
, "enable",
455 if (IS_ERR(amldev
->bt_en_gpio
)) {
456 dev_err(pdev
, "Failed to acquire enable gpios");
457 return PTR_ERR(amldev
->bt_en_gpio
);
460 if (device_property_read_string(pdev
, "firmware-name",
461 &amldev
->firmware_name
)) {
462 dev_err(pdev
, "Failed to acquire firmware path");
466 amldev
->bt_supply
= devm_regulator_get(pdev
, "vddio");
467 if (IS_ERR(amldev
->bt_supply
)) {
468 dev_err(pdev
, "Failed to acquire regulator");
469 return PTR_ERR(amldev
->bt_supply
);
472 amldev
->lpo_clk
= devm_clk_get(pdev
, NULL
);
473 if (IS_ERR(amldev
->lpo_clk
)) {
474 dev_err(pdev
, "Failed to acquire clock source");
475 return PTR_ERR(amldev
->lpo_clk
);
481 static int aml_power_on(struct aml_serdev
*amldev
)
485 err
= regulator_enable(amldev
->bt_supply
);
487 dev_err(amldev
->dev
, "Failed to enable regulator: (%d)", err
);
491 err
= clk_prepare_enable(amldev
->lpo_clk
);
493 dev_err(amldev
->dev
, "Failed to enable lpo clock: (%d)", err
);
497 gpiod_set_value_cansleep(amldev
->bt_en_gpio
, 1);
499 /* Wait 20ms for bluetooth controller power on */
504 static int aml_power_off(struct aml_serdev
*amldev
)
506 gpiod_set_value_cansleep(amldev
->bt_en_gpio
, 0);
508 clk_disable_unprepare(amldev
->lpo_clk
);
510 regulator_disable(amldev
->bt_supply
);
515 static int aml_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
517 /* update controller baudrate */
518 if (aml_update_chip_baudrate(hu
->hdev
, speed
) != 0) {
519 bt_dev_err(hu
->hdev
, "Failed to update baud rate");
523 /* update local baudrate */
524 serdev_device_set_baudrate(hu
->serdev
, speed
);
529 /* Initialize protocol */
530 static int aml_open(struct hci_uart
*hu
)
532 struct aml_serdev
*amldev
= serdev_device_get_drvdata(hu
->serdev
);
533 struct aml_data
*aml_data
;
536 err
= aml_parse_dt(amldev
);
540 if (!hci_uart_has_flow_control(hu
)) {
541 bt_dev_err(hu
->hdev
, "no flow control");
545 aml_data
= kzalloc(sizeof(*aml_data
), GFP_KERNEL
);
549 skb_queue_head_init(&aml_data
->txq
);
556 static int aml_close(struct hci_uart
*hu
)
558 struct aml_serdev
*amldev
= serdev_device_get_drvdata(hu
->serdev
);
559 struct aml_data
*aml_data
= hu
->priv
;
561 skb_queue_purge(&aml_data
->txq
);
562 kfree_skb(aml_data
->rx_skb
);
567 return aml_power_off(amldev
);
570 static int aml_flush(struct hci_uart
*hu
)
572 struct aml_data
*aml_data
= hu
->priv
;
574 skb_queue_purge(&aml_data
->txq
);
579 static int aml_setup(struct hci_uart
*hu
)
581 struct aml_serdev
*amldev
= serdev_device_get_drvdata(hu
->serdev
);
582 struct hci_dev
*hdev
= amldev
->serdev_hu
.hdev
;
586 hdev
->set_bdaddr
= aml_set_bdaddr
;
588 err
= aml_power_on(amldev
);
592 err
= aml_set_baudrate(hu
, amldev
->serdev_hu
.proto
->oper_speed
);
596 err
= aml_download_firmware(hdev
, amldev
->firmware_name
);
600 err
= aml_config_rf(hdev
, amldev
->aml_dev_data
->is_coex
);
604 err
= aml_start_chip(hdev
);
608 /* Wait 60ms for controller startup */
611 err
= aml_dump_fw_version(hdev
);
615 err
= aml_send_reset(hdev
);
619 err
= aml_check_bdaddr(hdev
);
626 static int aml_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
628 struct aml_data
*aml_data
= hu
->priv
;
630 skb_queue_tail(&aml_data
->txq
, skb
);
635 static struct sk_buff
*aml_dequeue(struct hci_uart
*hu
)
637 struct aml_data
*aml_data
= hu
->priv
;
640 skb
= skb_dequeue(&aml_data
->txq
);
642 /* Prepend skb with frame type */
644 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
649 static int aml_recv(struct hci_uart
*hu
, const void *data
, int count
)
651 struct aml_data
*aml_data
= hu
->priv
;
654 aml_data
->rx_skb
= h4_recv_buf(hu
->hdev
, aml_data
->rx_skb
, data
, count
,
656 ARRAY_SIZE(aml_recv_pkts
));
657 if (IS_ERR(aml_data
->rx_skb
)) {
658 err
= PTR_ERR(aml_data
->rx_skb
);
659 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
660 aml_data
->rx_skb
= NULL
;
667 static const struct hci_uart_proto aml_hci_proto
= {
670 .init_speed
= 115200,
671 .oper_speed
= 4000000,
677 .enqueue
= aml_enqueue
,
678 .dequeue
= aml_dequeue
,
681 static void aml_device_driver_shutdown(struct device
*dev
)
683 struct aml_serdev
*amldev
= dev_get_drvdata(dev
);
685 aml_power_off(amldev
);
688 static int aml_serdev_probe(struct serdev_device
*serdev
)
690 struct aml_serdev
*amldev
;
693 amldev
= devm_kzalloc(&serdev
->dev
, sizeof(*amldev
), GFP_KERNEL
);
697 amldev
->serdev_hu
.serdev
= serdev
;
698 amldev
->dev
= &serdev
->dev
;
699 serdev_device_set_drvdata(serdev
, amldev
);
701 err
= hci_uart_register_device(&amldev
->serdev_hu
, &aml_hci_proto
);
703 return dev_err_probe(amldev
->dev
, err
,
704 "Failed to register hci uart device");
706 amldev
->aml_dev_data
= device_get_match_data(&serdev
->dev
);
711 static void aml_serdev_remove(struct serdev_device
*serdev
)
713 struct aml_serdev
*amldev
= serdev_device_get_drvdata(serdev
);
715 hci_uart_unregister_device(&amldev
->serdev_hu
);
718 static const struct aml_device_data data_w155s2
= {
719 .iccm_offset
= 256 * 1024,
722 static const struct aml_device_data data_w265s2
= {
723 .iccm_offset
= 384 * 1024,
726 static const struct of_device_id aml_bluetooth_of_match
[] = {
727 { .compatible
= "amlogic,w155s2-bt", .data
= &data_w155s2
},
728 { .compatible
= "amlogic,w265s2-bt", .data
= &data_w265s2
},
731 MODULE_DEVICE_TABLE(of
, aml_bluetooth_of_match
);
733 static struct serdev_device_driver aml_serdev_driver
= {
734 .probe
= aml_serdev_probe
,
735 .remove
= aml_serdev_remove
,
737 .name
= "hci_uart_aml",
738 .of_match_table
= aml_bluetooth_of_match
,
739 .shutdown
= aml_device_driver_shutdown
,
743 int __init
aml_init(void)
745 serdev_device_driver_register(&aml_serdev_driver
);
747 return hci_uart_register_proto(&aml_hci_proto
);
750 int __exit
aml_deinit(void)
752 serdev_device_driver_unregister(&aml_serdev_driver
);
754 return hci_uart_unregister_proto(&aml_hci_proto
);