1*37bac77eSYang Li // SPDX-License-Identifier: (GPL-2.0-only OR MIT)
2*37bac77eSYang Li /*
3*37bac77eSYang Li * Copyright (C) 2024 Amlogic, Inc. All rights reserved
4*37bac77eSYang Li */
5*37bac77eSYang Li
6*37bac77eSYang Li #include <linux/kernel.h>
7*37bac77eSYang Li #include <linux/delay.h>
8*37bac77eSYang Li #include <linux/device.h>
9*37bac77eSYang Li #include <linux/property.h>
10*37bac77eSYang Li #include <linux/of.h>
11*37bac77eSYang Li #include <linux/serdev.h>
12*37bac77eSYang Li #include <linux/clk.h>
13*37bac77eSYang Li #include <linux/firmware.h>
14*37bac77eSYang Li #include <linux/gpio/consumer.h>
15*37bac77eSYang Li #include <linux/regulator/consumer.h>
16*37bac77eSYang Li #include <net/bluetooth/bluetooth.h>
17*37bac77eSYang Li #include <net/bluetooth/hci_core.h>
18*37bac77eSYang Li #include <net/bluetooth/hci.h>
19*37bac77eSYang Li
20*37bac77eSYang Li #include "hci_uart.h"
21*37bac77eSYang Li
22*37bac77eSYang Li #define AML_EVT_HEAD_SIZE 4
23*37bac77eSYang Li #define AML_BDADDR_DEFAULT (&(bdaddr_t) {{ 0x00, 0xff, 0x00, 0x22, 0x2d, 0xae }})
24*37bac77eSYang Li
25*37bac77eSYang Li #define AML_FIRMWARE_OPERATION_SIZE (248)
26*37bac77eSYang Li #define AML_FIRMWARE_MAX_SIZE (512 * 1024)
27*37bac77eSYang Li
28*37bac77eSYang Li /* TCI command */
29*37bac77eSYang Li #define AML_TCI_CMD_READ 0xFEF0
30*37bac77eSYang Li #define AML_TCI_CMD_WRITE 0xFEF1
31*37bac77eSYang Li #define AML_TCI_CMD_UPDATE_BAUDRATE 0xFEF2
32*37bac77eSYang Li #define AML_TCI_CMD_HARDWARE_RESET 0xFEF2
33*37bac77eSYang Li #define AML_TCI_CMD_DOWNLOAD_BT_FW 0xFEF3
34*37bac77eSYang Li
35*37bac77eSYang Li /* Vendor command */
36*37bac77eSYang Li #define AML_BT_HCI_VENDOR_CMD 0xFC1A
37*37bac77eSYang Li
38*37bac77eSYang Li /* TCI operation parameter in controller chip */
39*37bac77eSYang Li #define AML_OP_UART_MODE 0x00A30128
40*37bac77eSYang Li #define AML_OP_EVT_ENABLE 0x00A70014
41*37bac77eSYang Li #define AML_OP_MEM_HARD_TRANS_EN 0x00A7000C
42*37bac77eSYang Li #define AML_OP_RF_CFG 0x00F03040
43*37bac77eSYang Li #define AML_OP_RAM_POWER_CTR 0x00F03050
44*37bac77eSYang Li #define AML_OP_HARDWARE_RST 0x00F03058
45*37bac77eSYang Li #define AML_OP_ICCM_RAM_BASE 0x00000000
46*37bac77eSYang Li #define AML_OP_DCCM_RAM_BASE 0x00D00000
47*37bac77eSYang Li
48*37bac77eSYang Li /* UART configuration */
49*37bac77eSYang Li #define AML_UART_XMIT_EN BIT(12)
50*37bac77eSYang Li #define AML_UART_RECV_EN BIT(13)
51*37bac77eSYang Li #define AML_UART_TIMEOUT_INT_EN BIT(14)
52*37bac77eSYang Li #define AML_UART_CLK_SOURCE 40000000
53*37bac77eSYang Li
54*37bac77eSYang Li /* Controller event */
55*37bac77eSYang Li #define AML_EVT_EN BIT(24)
56*37bac77eSYang Li
57*37bac77eSYang Li /* RAM power control */
58*37bac77eSYang Li #define AML_RAM_POWER_ON (0)
59*37bac77eSYang Li #define AML_RAM_POWER_OFF (1)
60*37bac77eSYang Li
61*37bac77eSYang Li /* RF configuration */
62*37bac77eSYang Li #define AML_RF_ANT_SINGLE BIT(28)
63*37bac77eSYang Li #define AML_RF_ANT_DOUBLE BIT(29)
64*37bac77eSYang Li
65*37bac77eSYang Li /* Memory transaction */
66*37bac77eSYang Li #define AML_MM_CTR_HARD_TRAS_EN BIT(27)
67*37bac77eSYang Li
68*37bac77eSYang Li /* Controller reset */
69*37bac77eSYang Li #define AML_CTR_CPU_RESET BIT(8)
70*37bac77eSYang Li #define AML_CTR_MAC_RESET BIT(9)
71*37bac77eSYang Li #define AML_CTR_PHY_RESET BIT(10)
72*37bac77eSYang Li
73*37bac77eSYang Li enum {
74*37bac77eSYang Li FW_ICCM,
75*37bac77eSYang Li FW_DCCM
76*37bac77eSYang Li };
77*37bac77eSYang Li
78*37bac77eSYang Li struct aml_fw_len {
79*37bac77eSYang Li u32 iccm_len;
80*37bac77eSYang Li u32 dccm_len;
81*37bac77eSYang Li };
82*37bac77eSYang Li
83*37bac77eSYang Li struct aml_tci_rsp {
84*37bac77eSYang Li u8 num_cmd_packet;
85*37bac77eSYang Li u16 opcode;
86*37bac77eSYang Li u8 status;
87*37bac77eSYang Li } __packed;
88*37bac77eSYang Li
89*37bac77eSYang Li struct aml_device_data {
90*37bac77eSYang Li int iccm_offset;
91*37bac77eSYang Li int dccm_offset;
92*37bac77eSYang Li bool is_coex;
93*37bac77eSYang Li };
94*37bac77eSYang Li
95*37bac77eSYang Li struct aml_serdev {
96*37bac77eSYang Li struct hci_uart serdev_hu;
97*37bac77eSYang Li struct device *dev;
98*37bac77eSYang Li struct gpio_desc *bt_en_gpio;
99*37bac77eSYang Li struct regulator *bt_supply;
100*37bac77eSYang Li struct clk *lpo_clk;
101*37bac77eSYang Li const struct aml_device_data *aml_dev_data;
102*37bac77eSYang Li const char *firmware_name;
103*37bac77eSYang Li };
104*37bac77eSYang Li
105*37bac77eSYang Li struct aml_data {
106*37bac77eSYang Li struct sk_buff *rx_skb;
107*37bac77eSYang Li struct sk_buff_head txq;
108*37bac77eSYang Li };
109*37bac77eSYang Li
110*37bac77eSYang Li static const struct h4_recv_pkt aml_recv_pkts[] = {
111*37bac77eSYang Li { H4_RECV_ACL, .recv = hci_recv_frame },
112*37bac77eSYang Li { H4_RECV_SCO, .recv = hci_recv_frame },
113*37bac77eSYang Li { H4_RECV_EVENT, .recv = hci_recv_frame },
114*37bac77eSYang Li { H4_RECV_ISO, .recv = hci_recv_frame },
115*37bac77eSYang Li };
116*37bac77eSYang Li
117*37bac77eSYang Li /* The TCI command is a private command, which is for setting baud rate,
118*37bac77eSYang Li * downloading firmware, initiating RAM.
119*37bac77eSYang Li *
120*37bac77eSYang Li * op_code | op_len | op_addr | parameter |
121*37bac77eSYang Li * --------|-----------------------|---------|-------------|
122*37bac77eSYang Li * 2B | 1B len(addr+param) | 4B | len(param) |
123*37bac77eSYang Li */
aml_send_tci_cmd(struct hci_dev * hdev,u16 op_code,u32 op_addr,u32 * param,u32 param_len)124*37bac77eSYang Li static int aml_send_tci_cmd(struct hci_dev *hdev, u16 op_code, u32 op_addr,
125*37bac77eSYang Li u32 *param, u32 param_len)
126*37bac77eSYang Li {
127*37bac77eSYang Li struct aml_tci_rsp *rsp = NULL;
128*37bac77eSYang Li struct sk_buff *skb = NULL;
129*37bac77eSYang Li size_t buf_len = 0;
130*37bac77eSYang Li u8 *buf = NULL;
131*37bac77eSYang Li int err = 0;
132*37bac77eSYang Li
133*37bac77eSYang Li buf_len = sizeof(op_addr) + param_len;
134*37bac77eSYang Li buf = kmalloc(buf_len, GFP_KERNEL);
135*37bac77eSYang Li if (!buf)
136*37bac77eSYang Li return -ENOMEM;
137*37bac77eSYang Li
138*37bac77eSYang Li memcpy(buf, &op_addr, sizeof(op_addr));
139*37bac77eSYang Li if (param && param_len > 0)
140*37bac77eSYang Li memcpy(buf + sizeof(op_addr), param, param_len);
141*37bac77eSYang Li
142*37bac77eSYang Li skb = __hci_cmd_sync_ev(hdev, op_code, buf_len, buf,
143*37bac77eSYang Li HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
144*37bac77eSYang Li if (IS_ERR(skb)) {
145*37bac77eSYang Li err = PTR_ERR(skb);
146*37bac77eSYang Li bt_dev_err(hdev, "Failed to send TCI cmd (error: %d)", err);
147*37bac77eSYang Li goto exit;
148*37bac77eSYang Li }
149*37bac77eSYang Li
150*37bac77eSYang Li rsp = skb_pull_data(skb, sizeof(struct aml_tci_rsp));
151*37bac77eSYang Li if (!rsp)
152*37bac77eSYang Li goto skb_free;
153*37bac77eSYang Li
154*37bac77eSYang Li if (rsp->opcode != op_code || rsp->status != 0x00) {
155*37bac77eSYang Li bt_dev_err(hdev, "send TCI cmd (0x%04X), response (0x%04X):(%d)",
156*37bac77eSYang Li op_code, rsp->opcode, rsp->status);
157*37bac77eSYang Li err = -EINVAL;
158*37bac77eSYang Li goto skb_free;
159*37bac77eSYang Li }
160*37bac77eSYang Li
161*37bac77eSYang Li skb_free:
162*37bac77eSYang Li kfree_skb(skb);
163*37bac77eSYang Li
164*37bac77eSYang Li exit:
165*37bac77eSYang Li kfree(buf);
166*37bac77eSYang Li return err;
167*37bac77eSYang Li }
168*37bac77eSYang Li
aml_update_chip_baudrate(struct hci_dev * hdev,u32 baud)169*37bac77eSYang Li static int aml_update_chip_baudrate(struct hci_dev *hdev, u32 baud)
170*37bac77eSYang Li {
171*37bac77eSYang Li u32 value;
172*37bac77eSYang Li
173*37bac77eSYang Li value = ((AML_UART_CLK_SOURCE / baud) - 1) & 0x0FFF;
174*37bac77eSYang Li value |= AML_UART_XMIT_EN | AML_UART_RECV_EN | AML_UART_TIMEOUT_INT_EN;
175*37bac77eSYang Li
176*37bac77eSYang Li return aml_send_tci_cmd(hdev, AML_TCI_CMD_UPDATE_BAUDRATE,
177*37bac77eSYang Li AML_OP_UART_MODE, &value, sizeof(value));
178*37bac77eSYang Li }
179*37bac77eSYang Li
aml_start_chip(struct hci_dev * hdev)180*37bac77eSYang Li static int aml_start_chip(struct hci_dev *hdev)
181*37bac77eSYang Li {
182*37bac77eSYang Li u32 value = 0;
183*37bac77eSYang Li int ret;
184*37bac77eSYang Li
185*37bac77eSYang Li value = AML_MM_CTR_HARD_TRAS_EN;
186*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_WRITE,
187*37bac77eSYang Li AML_OP_MEM_HARD_TRANS_EN,
188*37bac77eSYang Li &value, sizeof(value));
189*37bac77eSYang Li if (ret)
190*37bac77eSYang Li return ret;
191*37bac77eSYang Li
192*37bac77eSYang Li /* controller hardware reset */
193*37bac77eSYang Li value = AML_CTR_CPU_RESET | AML_CTR_MAC_RESET | AML_CTR_PHY_RESET;
194*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_HARDWARE_RESET,
195*37bac77eSYang Li AML_OP_HARDWARE_RST,
196*37bac77eSYang Li &value, sizeof(value));
197*37bac77eSYang Li return ret;
198*37bac77eSYang Li }
199*37bac77eSYang Li
aml_send_firmware_segment(struct hci_dev * hdev,u8 fw_type,u8 * seg,u32 seg_size,u32 offset)200*37bac77eSYang Li static int aml_send_firmware_segment(struct hci_dev *hdev,
201*37bac77eSYang Li u8 fw_type,
202*37bac77eSYang Li u8 *seg,
203*37bac77eSYang Li u32 seg_size,
204*37bac77eSYang Li u32 offset)
205*37bac77eSYang Li {
206*37bac77eSYang Li u32 op_addr = 0;
207*37bac77eSYang Li
208*37bac77eSYang Li if (fw_type == FW_ICCM)
209*37bac77eSYang Li op_addr = AML_OP_ICCM_RAM_BASE + offset;
210*37bac77eSYang Li else if (fw_type == FW_DCCM)
211*37bac77eSYang Li op_addr = AML_OP_DCCM_RAM_BASE + offset;
212*37bac77eSYang Li
213*37bac77eSYang Li return aml_send_tci_cmd(hdev, AML_TCI_CMD_DOWNLOAD_BT_FW,
214*37bac77eSYang Li op_addr, (u32 *)seg, seg_size);
215*37bac77eSYang Li }
216*37bac77eSYang Li
aml_send_firmware(struct hci_dev * hdev,u8 fw_type,u8 * fw,u32 fw_size,u32 offset)217*37bac77eSYang Li static int aml_send_firmware(struct hci_dev *hdev, u8 fw_type,
218*37bac77eSYang Li u8 *fw, u32 fw_size, u32 offset)
219*37bac77eSYang Li {
220*37bac77eSYang Li u32 seg_size = 0;
221*37bac77eSYang Li u32 seg_off = 0;
222*37bac77eSYang Li
223*37bac77eSYang Li if (fw_size > AML_FIRMWARE_MAX_SIZE) {
224*37bac77eSYang Li bt_dev_err(hdev,
225*37bac77eSYang Li "Firmware size %d kB is larger than the maximum of 512 kB. Aborting.",
226*37bac77eSYang Li fw_size);
227*37bac77eSYang Li return -EINVAL;
228*37bac77eSYang Li }
229*37bac77eSYang Li while (fw_size > 0) {
230*37bac77eSYang Li seg_size = (fw_size > AML_FIRMWARE_OPERATION_SIZE) ?
231*37bac77eSYang Li AML_FIRMWARE_OPERATION_SIZE : fw_size;
232*37bac77eSYang Li if (aml_send_firmware_segment(hdev, fw_type, (fw + seg_off),
233*37bac77eSYang Li seg_size, offset)) {
234*37bac77eSYang Li bt_dev_err(hdev, "Failed send firmware, type: %d, offset: 0x%x",
235*37bac77eSYang Li fw_type, offset);
236*37bac77eSYang Li return -EINVAL;
237*37bac77eSYang Li }
238*37bac77eSYang Li seg_off += seg_size;
239*37bac77eSYang Li fw_size -= seg_size;
240*37bac77eSYang Li offset += seg_size;
241*37bac77eSYang Li }
242*37bac77eSYang Li return 0;
243*37bac77eSYang Li }
244*37bac77eSYang Li
aml_download_firmware(struct hci_dev * hdev,const char * fw_name)245*37bac77eSYang Li static int aml_download_firmware(struct hci_dev *hdev, const char *fw_name)
246*37bac77eSYang Li {
247*37bac77eSYang Li struct hci_uart *hu = hci_get_drvdata(hdev);
248*37bac77eSYang Li struct aml_serdev *amldev = serdev_device_get_drvdata(hu->serdev);
249*37bac77eSYang Li const struct firmware *firmware = NULL;
250*37bac77eSYang Li struct aml_fw_len *fw_len = NULL;
251*37bac77eSYang Li u8 *iccm_start = NULL, *dccm_start = NULL;
252*37bac77eSYang Li u32 iccm_len, dccm_len;
253*37bac77eSYang Li u32 value = 0;
254*37bac77eSYang Li int ret = 0;
255*37bac77eSYang Li
256*37bac77eSYang Li /* Enable firmware download event */
257*37bac77eSYang Li value = AML_EVT_EN;
258*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_WRITE,
259*37bac77eSYang Li AML_OP_EVT_ENABLE,
260*37bac77eSYang Li &value, sizeof(value));
261*37bac77eSYang Li if (ret)
262*37bac77eSYang Li goto exit;
263*37bac77eSYang Li
264*37bac77eSYang Li /* RAM power on */
265*37bac77eSYang Li value = AML_RAM_POWER_ON;
266*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_WRITE,
267*37bac77eSYang Li AML_OP_RAM_POWER_CTR,
268*37bac77eSYang Li &value, sizeof(value));
269*37bac77eSYang Li if (ret)
270*37bac77eSYang Li goto exit;
271*37bac77eSYang Li
272*37bac77eSYang Li /* Check RAM power status */
273*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_READ,
274*37bac77eSYang Li AML_OP_RAM_POWER_CTR, NULL, 0);
275*37bac77eSYang Li if (ret)
276*37bac77eSYang Li goto exit;
277*37bac77eSYang Li
278*37bac77eSYang Li ret = request_firmware(&firmware, fw_name, &hdev->dev);
279*37bac77eSYang Li if (ret < 0) {
280*37bac77eSYang Li bt_dev_err(hdev, "Failed to load <%s>:(%d)", fw_name, ret);
281*37bac77eSYang Li goto exit;
282*37bac77eSYang Li }
283*37bac77eSYang Li
284*37bac77eSYang Li fw_len = (struct aml_fw_len *)firmware->data;
285*37bac77eSYang Li
286*37bac77eSYang Li /* Download ICCM */
287*37bac77eSYang Li iccm_start = (u8 *)(firmware->data) + sizeof(struct aml_fw_len)
288*37bac77eSYang Li + amldev->aml_dev_data->iccm_offset;
289*37bac77eSYang Li iccm_len = fw_len->iccm_len - amldev->aml_dev_data->iccm_offset;
290*37bac77eSYang Li ret = aml_send_firmware(hdev, FW_ICCM, iccm_start, iccm_len,
291*37bac77eSYang Li amldev->aml_dev_data->iccm_offset);
292*37bac77eSYang Li if (ret) {
293*37bac77eSYang Li bt_dev_err(hdev, "Failed to send FW_ICCM (%d)", ret);
294*37bac77eSYang Li goto exit;
295*37bac77eSYang Li }
296*37bac77eSYang Li
297*37bac77eSYang Li /* Download DCCM */
298*37bac77eSYang Li dccm_start = (u8 *)(firmware->data) + sizeof(struct aml_fw_len) + fw_len->iccm_len;
299*37bac77eSYang Li dccm_len = fw_len->dccm_len;
300*37bac77eSYang Li ret = aml_send_firmware(hdev, FW_DCCM, dccm_start, dccm_len,
301*37bac77eSYang Li amldev->aml_dev_data->dccm_offset);
302*37bac77eSYang Li if (ret) {
303*37bac77eSYang Li bt_dev_err(hdev, "Failed to send FW_DCCM (%d)", ret);
304*37bac77eSYang Li goto exit;
305*37bac77eSYang Li }
306*37bac77eSYang Li
307*37bac77eSYang Li /* Disable firmware download event */
308*37bac77eSYang Li value = 0;
309*37bac77eSYang Li ret = aml_send_tci_cmd(hdev, AML_TCI_CMD_WRITE,
310*37bac77eSYang Li AML_OP_EVT_ENABLE,
311*37bac77eSYang Li &value, sizeof(value));
312*37bac77eSYang Li if (ret)
313*37bac77eSYang Li goto exit;
314*37bac77eSYang Li
315*37bac77eSYang Li exit:
316*37bac77eSYang Li if (firmware)
317*37bac77eSYang Li release_firmware(firmware);
318*37bac77eSYang Li return ret;
319*37bac77eSYang Li }
320*37bac77eSYang Li
aml_send_reset(struct hci_dev * hdev)321*37bac77eSYang Li static int aml_send_reset(struct hci_dev *hdev)
322*37bac77eSYang Li {
323*37bac77eSYang Li struct sk_buff *skb;
324*37bac77eSYang Li int err;
325*37bac77eSYang Li
326*37bac77eSYang Li skb = __hci_cmd_sync_ev(hdev, HCI_OP_RESET, 0, NULL,
327*37bac77eSYang Li HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
328*37bac77eSYang Li if (IS_ERR(skb)) {
329*37bac77eSYang Li err = PTR_ERR(skb);
330*37bac77eSYang Li bt_dev_err(hdev, "Failed to send hci reset cmd (%d)", err);
331*37bac77eSYang Li return err;
332*37bac77eSYang Li }
333*37bac77eSYang Li
334*37bac77eSYang Li kfree_skb(skb);
335*37bac77eSYang Li return 0;
336*37bac77eSYang Li }
337*37bac77eSYang Li
aml_dump_fw_version(struct hci_dev * hdev)338*37bac77eSYang Li static int aml_dump_fw_version(struct hci_dev *hdev)
339*37bac77eSYang Li {
340*37bac77eSYang Li struct aml_tci_rsp *rsp = NULL;
341*37bac77eSYang Li struct sk_buff *skb;
342*37bac77eSYang Li u8 value[6] = {0};
343*37bac77eSYang Li u8 *fw_ver = NULL;
344*37bac77eSYang Li int err = 0;
345*37bac77eSYang Li
346*37bac77eSYang Li skb = __hci_cmd_sync_ev(hdev, AML_BT_HCI_VENDOR_CMD, sizeof(value), value,
347*37bac77eSYang Li HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
348*37bac77eSYang Li if (IS_ERR(skb)) {
349*37bac77eSYang Li err = PTR_ERR(skb);
350*37bac77eSYang Li bt_dev_err(hdev, "Failed to get fw version (error: %d)", err);
351*37bac77eSYang Li return err;
352*37bac77eSYang Li }
353*37bac77eSYang Li
354*37bac77eSYang Li rsp = skb_pull_data(skb, sizeof(struct aml_tci_rsp));
355*37bac77eSYang Li if (!rsp)
356*37bac77eSYang Li goto exit;
357*37bac77eSYang Li
358*37bac77eSYang Li if (rsp->opcode != AML_BT_HCI_VENDOR_CMD || rsp->status != 0x00) {
359*37bac77eSYang Li bt_dev_err(hdev, "dump version, error response (0x%04X):(%d)",
360*37bac77eSYang Li rsp->opcode, rsp->status);
361*37bac77eSYang Li err = -EINVAL;
362*37bac77eSYang Li goto exit;
363*37bac77eSYang Li }
364*37bac77eSYang Li
365*37bac77eSYang Li fw_ver = (u8 *)rsp + AML_EVT_HEAD_SIZE;
366*37bac77eSYang Li bt_dev_info(hdev, "fw_version: date = %02x.%02x, number = 0x%02x%02x",
367*37bac77eSYang Li *(fw_ver + 1), *fw_ver, *(fw_ver + 3), *(fw_ver + 2));
368*37bac77eSYang Li
369*37bac77eSYang Li exit:
370*37bac77eSYang Li kfree_skb(skb);
371*37bac77eSYang Li return err;
372*37bac77eSYang Li }
373*37bac77eSYang Li
aml_set_bdaddr(struct hci_dev * hdev,const bdaddr_t * bdaddr)374*37bac77eSYang Li static int aml_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
375*37bac77eSYang Li {
376*37bac77eSYang Li struct aml_tci_rsp *rsp = NULL;
377*37bac77eSYang Li struct sk_buff *skb;
378*37bac77eSYang Li int err = 0;
379*37bac77eSYang Li
380*37bac77eSYang Li bt_dev_info(hdev, "set bdaddr (%pM)", bdaddr);
381*37bac77eSYang Li skb = __hci_cmd_sync_ev(hdev, AML_BT_HCI_VENDOR_CMD,
382*37bac77eSYang Li sizeof(bdaddr_t), bdaddr,
383*37bac77eSYang Li HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
384*37bac77eSYang Li if (IS_ERR(skb)) {
385*37bac77eSYang Li err = PTR_ERR(skb);
386*37bac77eSYang Li bt_dev_err(hdev, "Failed to set bdaddr (error: %d)", err);
387*37bac77eSYang Li return err;
388*37bac77eSYang Li }
389*37bac77eSYang Li
390*37bac77eSYang Li rsp = skb_pull_data(skb, sizeof(struct aml_tci_rsp));
391*37bac77eSYang Li if (!rsp)
392*37bac77eSYang Li goto exit;
393*37bac77eSYang Li
394*37bac77eSYang Li if (rsp->opcode != AML_BT_HCI_VENDOR_CMD || rsp->status != 0x00) {
395*37bac77eSYang Li bt_dev_err(hdev, "error response (0x%x):(%d)", rsp->opcode, rsp->status);
396*37bac77eSYang Li err = -EINVAL;
397*37bac77eSYang Li goto exit;
398*37bac77eSYang Li }
399*37bac77eSYang Li
400*37bac77eSYang Li exit:
401*37bac77eSYang Li kfree_skb(skb);
402*37bac77eSYang Li return err;
403*37bac77eSYang Li }
404*37bac77eSYang Li
aml_check_bdaddr(struct hci_dev * hdev)405*37bac77eSYang Li static int aml_check_bdaddr(struct hci_dev *hdev)
406*37bac77eSYang Li {
407*37bac77eSYang Li struct hci_rp_read_bd_addr *paddr;
408*37bac77eSYang Li struct sk_buff *skb;
409*37bac77eSYang Li int err;
410*37bac77eSYang Li
411*37bac77eSYang Li if (bacmp(&hdev->public_addr, BDADDR_ANY))
412*37bac77eSYang Li return 0;
413*37bac77eSYang Li
414*37bac77eSYang Li skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
415*37bac77eSYang Li HCI_INIT_TIMEOUT);
416*37bac77eSYang Li if (IS_ERR(skb)) {
417*37bac77eSYang Li err = PTR_ERR(skb);
418*37bac77eSYang Li bt_dev_err(hdev, "Failed to read bdaddr (error: %d)", err);
419*37bac77eSYang Li return err;
420*37bac77eSYang Li }
421*37bac77eSYang Li
422*37bac77eSYang Li paddr = skb_pull_data(skb, sizeof(struct hci_rp_read_bd_addr));
423*37bac77eSYang Li if (!paddr)
424*37bac77eSYang Li goto exit;
425*37bac77eSYang Li
426*37bac77eSYang Li if (!bacmp(&paddr->bdaddr, AML_BDADDR_DEFAULT)) {
427*37bac77eSYang Li bt_dev_info(hdev, "amlbt using default bdaddr (%pM)", &paddr->bdaddr);
428*37bac77eSYang Li set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
429*37bac77eSYang Li }
430*37bac77eSYang Li
431*37bac77eSYang Li exit:
432*37bac77eSYang Li kfree_skb(skb);
433*37bac77eSYang Li return 0;
434*37bac77eSYang Li }
435*37bac77eSYang Li
aml_config_rf(struct hci_dev * hdev,bool is_coex)436*37bac77eSYang Li static int aml_config_rf(struct hci_dev *hdev, bool is_coex)
437*37bac77eSYang Li {
438*37bac77eSYang Li u32 value = AML_RF_ANT_DOUBLE;
439*37bac77eSYang Li
440*37bac77eSYang Li /* Use a single antenna when co-existing with wifi */
441*37bac77eSYang Li if (is_coex)
442*37bac77eSYang Li value = AML_RF_ANT_SINGLE;
443*37bac77eSYang Li
444*37bac77eSYang Li return aml_send_tci_cmd(hdev, AML_TCI_CMD_WRITE,
445*37bac77eSYang Li AML_OP_RF_CFG,
446*37bac77eSYang Li &value, sizeof(value));
447*37bac77eSYang Li }
448*37bac77eSYang Li
aml_parse_dt(struct aml_serdev * amldev)449*37bac77eSYang Li static int aml_parse_dt(struct aml_serdev *amldev)
450*37bac77eSYang Li {
451*37bac77eSYang Li struct device *pdev = amldev->dev;
452*37bac77eSYang Li
453*37bac77eSYang Li amldev->bt_en_gpio = devm_gpiod_get(pdev, "enable",
454*37bac77eSYang Li GPIOD_OUT_LOW);
455*37bac77eSYang Li if (IS_ERR(amldev->bt_en_gpio)) {
456*37bac77eSYang Li dev_err(pdev, "Failed to acquire enable gpios");
457*37bac77eSYang Li return PTR_ERR(amldev->bt_en_gpio);
458*37bac77eSYang Li }
459*37bac77eSYang Li
460*37bac77eSYang Li if (device_property_read_string(pdev, "firmware-name",
461*37bac77eSYang Li &amldev->firmware_name)) {
462*37bac77eSYang Li dev_err(pdev, "Failed to acquire firmware path");
463*37bac77eSYang Li return -ENODEV;
464*37bac77eSYang Li }
465*37bac77eSYang Li
466*37bac77eSYang Li amldev->bt_supply = devm_regulator_get(pdev, "vddio");
467*37bac77eSYang Li if (IS_ERR(amldev->bt_supply)) {
468*37bac77eSYang Li dev_err(pdev, "Failed to acquire regulator");
469*37bac77eSYang Li return PTR_ERR(amldev->bt_supply);
470*37bac77eSYang Li }
471*37bac77eSYang Li
472*37bac77eSYang Li amldev->lpo_clk = devm_clk_get(pdev, NULL);
473*37bac77eSYang Li if (IS_ERR(amldev->lpo_clk)) {
474*37bac77eSYang Li dev_err(pdev, "Failed to acquire clock source");
475*37bac77eSYang Li return PTR_ERR(amldev->lpo_clk);
476*37bac77eSYang Li }
477*37bac77eSYang Li
478*37bac77eSYang Li return 0;
479*37bac77eSYang Li }
480*37bac77eSYang Li
aml_power_on(struct aml_serdev * amldev)481*37bac77eSYang Li static int aml_power_on(struct aml_serdev *amldev)
482*37bac77eSYang Li {
483*37bac77eSYang Li int err;
484*37bac77eSYang Li
485*37bac77eSYang Li err = regulator_enable(amldev->bt_supply);
486*37bac77eSYang Li if (err) {
487*37bac77eSYang Li dev_err(amldev->dev, "Failed to enable regulator: (%d)", err);
488*37bac77eSYang Li return err;
489*37bac77eSYang Li }
490*37bac77eSYang Li
491*37bac77eSYang Li err = clk_prepare_enable(amldev->lpo_clk);
492*37bac77eSYang Li if (err) {
493*37bac77eSYang Li dev_err(amldev->dev, "Failed to enable lpo clock: (%d)", err);
494*37bac77eSYang Li return err;
495*37bac77eSYang Li }
496*37bac77eSYang Li
497*37bac77eSYang Li gpiod_set_value_cansleep(amldev->bt_en_gpio, 1);
498*37bac77eSYang Li
499*37bac77eSYang Li /* Wait 20ms for bluetooth controller power on */
500*37bac77eSYang Li msleep(20);
501*37bac77eSYang Li return 0;
502*37bac77eSYang Li }
503*37bac77eSYang Li
aml_power_off(struct aml_serdev * amldev)504*37bac77eSYang Li static int aml_power_off(struct aml_serdev *amldev)
505*37bac77eSYang Li {
506*37bac77eSYang Li gpiod_set_value_cansleep(amldev->bt_en_gpio, 0);
507*37bac77eSYang Li
508*37bac77eSYang Li clk_disable_unprepare(amldev->lpo_clk);
509*37bac77eSYang Li
510*37bac77eSYang Li regulator_disable(amldev->bt_supply);
511*37bac77eSYang Li
512*37bac77eSYang Li return 0;
513*37bac77eSYang Li }
514*37bac77eSYang Li
aml_set_baudrate(struct hci_uart * hu,unsigned int speed)515*37bac77eSYang Li static int aml_set_baudrate(struct hci_uart *hu, unsigned int speed)
516*37bac77eSYang Li {
517*37bac77eSYang Li /* update controller baudrate */
518*37bac77eSYang Li if (aml_update_chip_baudrate(hu->hdev, speed) != 0) {
519*37bac77eSYang Li bt_dev_err(hu->hdev, "Failed to update baud rate");
520*37bac77eSYang Li return -EINVAL;
521*37bac77eSYang Li }
522*37bac77eSYang Li
523*37bac77eSYang Li /* update local baudrate */
524*37bac77eSYang Li serdev_device_set_baudrate(hu->serdev, speed);
525*37bac77eSYang Li
526*37bac77eSYang Li return 0;
527*37bac77eSYang Li }
528*37bac77eSYang Li
529*37bac77eSYang Li /* Initialize protocol */
aml_open(struct hci_uart * hu)530*37bac77eSYang Li static int aml_open(struct hci_uart *hu)
531*37bac77eSYang Li {
532*37bac77eSYang Li struct aml_serdev *amldev = serdev_device_get_drvdata(hu->serdev);
533*37bac77eSYang Li struct aml_data *aml_data;
534*37bac77eSYang Li int err;
535*37bac77eSYang Li
536*37bac77eSYang Li err = aml_parse_dt(amldev);
537*37bac77eSYang Li if (err)
538*37bac77eSYang Li return err;
539*37bac77eSYang Li
540*37bac77eSYang Li if (!hci_uart_has_flow_control(hu)) {
541*37bac77eSYang Li bt_dev_err(hu->hdev, "no flow control");
542*37bac77eSYang Li return -EOPNOTSUPP;
543*37bac77eSYang Li }
544*37bac77eSYang Li
545*37bac77eSYang Li aml_data = kzalloc(sizeof(*aml_data), GFP_KERNEL);
546*37bac77eSYang Li if (!aml_data)
547*37bac77eSYang Li return -ENOMEM;
548*37bac77eSYang Li
549*37bac77eSYang Li skb_queue_head_init(&aml_data->txq);
550*37bac77eSYang Li
551*37bac77eSYang Li hu->priv = aml_data;
552*37bac77eSYang Li
553*37bac77eSYang Li return 0;
554*37bac77eSYang Li }
555*37bac77eSYang Li
aml_close(struct hci_uart * hu)556*37bac77eSYang Li static int aml_close(struct hci_uart *hu)
557*37bac77eSYang Li {
558*37bac77eSYang Li struct aml_serdev *amldev = serdev_device_get_drvdata(hu->serdev);
559*37bac77eSYang Li struct aml_data *aml_data = hu->priv;
560*37bac77eSYang Li
561*37bac77eSYang Li skb_queue_purge(&aml_data->txq);
562*37bac77eSYang Li kfree_skb(aml_data->rx_skb);
563*37bac77eSYang Li kfree(aml_data);
564*37bac77eSYang Li
565*37bac77eSYang Li hu->priv = NULL;
566*37bac77eSYang Li
567*37bac77eSYang Li return aml_power_off(amldev);
568*37bac77eSYang Li }
569*37bac77eSYang Li
aml_flush(struct hci_uart * hu)570*37bac77eSYang Li static int aml_flush(struct hci_uart *hu)
571*37bac77eSYang Li {
572*37bac77eSYang Li struct aml_data *aml_data = hu->priv;
573*37bac77eSYang Li
574*37bac77eSYang Li skb_queue_purge(&aml_data->txq);
575*37bac77eSYang Li
576*37bac77eSYang Li return 0;
577*37bac77eSYang Li }
578*37bac77eSYang Li
aml_setup(struct hci_uart * hu)579*37bac77eSYang Li static int aml_setup(struct hci_uart *hu)
580*37bac77eSYang Li {
581*37bac77eSYang Li struct aml_serdev *amldev = serdev_device_get_drvdata(hu->serdev);
582*37bac77eSYang Li struct hci_dev *hdev = amldev->serdev_hu.hdev;
583*37bac77eSYang Li int err;
584*37bac77eSYang Li
585*37bac77eSYang Li /* Setup bdaddr */
586*37bac77eSYang Li hdev->set_bdaddr = aml_set_bdaddr;
587*37bac77eSYang Li
588*37bac77eSYang Li err = aml_power_on(amldev);
589*37bac77eSYang Li if (err)
590*37bac77eSYang Li return err;
591*37bac77eSYang Li
592*37bac77eSYang Li err = aml_set_baudrate(hu, amldev->serdev_hu.proto->oper_speed);
593*37bac77eSYang Li if (err)
594*37bac77eSYang Li return err;
595*37bac77eSYang Li
596*37bac77eSYang Li err = aml_download_firmware(hdev, amldev->firmware_name);
597*37bac77eSYang Li if (err)
598*37bac77eSYang Li return err;
599*37bac77eSYang Li
600*37bac77eSYang Li err = aml_config_rf(hdev, amldev->aml_dev_data->is_coex);
601*37bac77eSYang Li if (err)
602*37bac77eSYang Li return err;
603*37bac77eSYang Li
604*37bac77eSYang Li err = aml_start_chip(hdev);
605*37bac77eSYang Li if (err)
606*37bac77eSYang Li return err;
607*37bac77eSYang Li
608*37bac77eSYang Li /* Wait 60ms for controller startup */
609*37bac77eSYang Li msleep(60);
610*37bac77eSYang Li
611*37bac77eSYang Li err = aml_dump_fw_version(hdev);
612*37bac77eSYang Li if (err)
613*37bac77eSYang Li return err;
614*37bac77eSYang Li
615*37bac77eSYang Li err = aml_send_reset(hdev);
616*37bac77eSYang Li if (err)
617*37bac77eSYang Li return err;
618*37bac77eSYang Li
619*37bac77eSYang Li err = aml_check_bdaddr(hdev);
620*37bac77eSYang Li if (err)
621*37bac77eSYang Li return err;
622*37bac77eSYang Li
623*37bac77eSYang Li return 0;
624*37bac77eSYang Li }
625*37bac77eSYang Li
aml_enqueue(struct hci_uart * hu,struct sk_buff * skb)626*37bac77eSYang Li static int aml_enqueue(struct hci_uart *hu, struct sk_buff *skb)
627*37bac77eSYang Li {
628*37bac77eSYang Li struct aml_data *aml_data = hu->priv;
629*37bac77eSYang Li
630*37bac77eSYang Li skb_queue_tail(&aml_data->txq, skb);
631*37bac77eSYang Li
632*37bac77eSYang Li return 0;
633*37bac77eSYang Li }
634*37bac77eSYang Li
aml_dequeue(struct hci_uart * hu)635*37bac77eSYang Li static struct sk_buff *aml_dequeue(struct hci_uart *hu)
636*37bac77eSYang Li {
637*37bac77eSYang Li struct aml_data *aml_data = hu->priv;
638*37bac77eSYang Li struct sk_buff *skb;
639*37bac77eSYang Li
640*37bac77eSYang Li skb = skb_dequeue(&aml_data->txq);
641*37bac77eSYang Li
642*37bac77eSYang Li /* Prepend skb with frame type */
643*37bac77eSYang Li if (skb)
644*37bac77eSYang Li memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
645*37bac77eSYang Li
646*37bac77eSYang Li return skb;
647*37bac77eSYang Li }
648*37bac77eSYang Li
aml_recv(struct hci_uart * hu,const void * data,int count)649*37bac77eSYang Li static int aml_recv(struct hci_uart *hu, const void *data, int count)
650*37bac77eSYang Li {
651*37bac77eSYang Li struct aml_data *aml_data = hu->priv;
652*37bac77eSYang Li int err;
653*37bac77eSYang Li
654*37bac77eSYang Li aml_data->rx_skb = h4_recv_buf(hu->hdev, aml_data->rx_skb, data, count,
655*37bac77eSYang Li aml_recv_pkts,
656*37bac77eSYang Li ARRAY_SIZE(aml_recv_pkts));
657*37bac77eSYang Li if (IS_ERR(aml_data->rx_skb)) {
658*37bac77eSYang Li err = PTR_ERR(aml_data->rx_skb);
659*37bac77eSYang Li bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
660*37bac77eSYang Li aml_data->rx_skb = NULL;
661*37bac77eSYang Li return err;
662*37bac77eSYang Li }
663*37bac77eSYang Li
664*37bac77eSYang Li return count;
665*37bac77eSYang Li }
666*37bac77eSYang Li
667*37bac77eSYang Li static const struct hci_uart_proto aml_hci_proto = {
668*37bac77eSYang Li .id = HCI_UART_AML,
669*37bac77eSYang Li .name = "AML",
670*37bac77eSYang Li .init_speed = 115200,
671*37bac77eSYang Li .oper_speed = 4000000,
672*37bac77eSYang Li .open = aml_open,
673*37bac77eSYang Li .close = aml_close,
674*37bac77eSYang Li .setup = aml_setup,
675*37bac77eSYang Li .flush = aml_flush,
676*37bac77eSYang Li .recv = aml_recv,
677*37bac77eSYang Li .enqueue = aml_enqueue,
678*37bac77eSYang Li .dequeue = aml_dequeue,
679*37bac77eSYang Li };
680*37bac77eSYang Li
aml_device_driver_shutdown(struct device * dev)681*37bac77eSYang Li static void aml_device_driver_shutdown(struct device *dev)
682*37bac77eSYang Li {
683*37bac77eSYang Li struct aml_serdev *amldev = dev_get_drvdata(dev);
684*37bac77eSYang Li
685*37bac77eSYang Li aml_power_off(amldev);
686*37bac77eSYang Li }
687*37bac77eSYang Li
aml_serdev_probe(struct serdev_device * serdev)688*37bac77eSYang Li static int aml_serdev_probe(struct serdev_device *serdev)
689*37bac77eSYang Li {
690*37bac77eSYang Li struct aml_serdev *amldev;
691*37bac77eSYang Li int err;
692*37bac77eSYang Li
693*37bac77eSYang Li amldev = devm_kzalloc(&serdev->dev, sizeof(*amldev), GFP_KERNEL);
694*37bac77eSYang Li if (!amldev)
695*37bac77eSYang Li return -ENOMEM;
696*37bac77eSYang Li
697*37bac77eSYang Li amldev->serdev_hu.serdev = serdev;
698*37bac77eSYang Li amldev->dev = &serdev->dev;
699*37bac77eSYang Li serdev_device_set_drvdata(serdev, amldev);
700*37bac77eSYang Li
701*37bac77eSYang Li err = hci_uart_register_device(&amldev->serdev_hu, &aml_hci_proto);
702*37bac77eSYang Li if (err)
703*37bac77eSYang Li return dev_err_probe(amldev->dev, err,
704*37bac77eSYang Li "Failed to register hci uart device");
705*37bac77eSYang Li
706*37bac77eSYang Li amldev->aml_dev_data = device_get_match_data(&serdev->dev);
707*37bac77eSYang Li
708*37bac77eSYang Li return 0;
709*37bac77eSYang Li }
710*37bac77eSYang Li
aml_serdev_remove(struct serdev_device * serdev)711*37bac77eSYang Li static void aml_serdev_remove(struct serdev_device *serdev)
712*37bac77eSYang Li {
713*37bac77eSYang Li struct aml_serdev *amldev = serdev_device_get_drvdata(serdev);
714*37bac77eSYang Li
715*37bac77eSYang Li hci_uart_unregister_device(&amldev->serdev_hu);
716*37bac77eSYang Li }
717*37bac77eSYang Li
718*37bac77eSYang Li static const struct aml_device_data data_w155s2 = {
719*37bac77eSYang Li .iccm_offset = 256 * 1024,
720*37bac77eSYang Li };
721*37bac77eSYang Li
722*37bac77eSYang Li static const struct aml_device_data data_w265s2 = {
723*37bac77eSYang Li .iccm_offset = 384 * 1024,
724*37bac77eSYang Li };
725*37bac77eSYang Li
726*37bac77eSYang Li static const struct of_device_id aml_bluetooth_of_match[] = {
727*37bac77eSYang Li { .compatible = "amlogic,w155s2-bt", .data = &data_w155s2 },
728*37bac77eSYang Li { .compatible = "amlogic,w265s2-bt", .data = &data_w265s2 },
729*37bac77eSYang Li { /* sentinel */ },
730*37bac77eSYang Li };
731*37bac77eSYang Li MODULE_DEVICE_TABLE(of, aml_bluetooth_of_match);
732*37bac77eSYang Li
733*37bac77eSYang Li static struct serdev_device_driver aml_serdev_driver = {
734*37bac77eSYang Li .probe = aml_serdev_probe,
735*37bac77eSYang Li .remove = aml_serdev_remove,
736*37bac77eSYang Li .driver = {
737*37bac77eSYang Li .name = "hci_uart_aml",
738*37bac77eSYang Li .of_match_table = aml_bluetooth_of_match,
739*37bac77eSYang Li .shutdown = aml_device_driver_shutdown,
740*37bac77eSYang Li },
741*37bac77eSYang Li };
742*37bac77eSYang Li
aml_init(void)743*37bac77eSYang Li int __init aml_init(void)
744*37bac77eSYang Li {
745*37bac77eSYang Li serdev_device_driver_register(&aml_serdev_driver);
746*37bac77eSYang Li
747*37bac77eSYang Li return hci_uart_register_proto(&aml_hci_proto);
748*37bac77eSYang Li }
749*37bac77eSYang Li
aml_deinit(void)750*37bac77eSYang Li int __exit aml_deinit(void)
751*37bac77eSYang Li {
752*37bac77eSYang Li serdev_device_driver_unregister(&aml_serdev_driver);
753*37bac77eSYang Li
754*37bac77eSYang Li return hci_uart_unregister_proto(&aml_hci_proto);
755*37bac77eSYang Li }
756