PabloPL / linux

Linux kernel source tree
Other
17 stars 0 forks source link

Bluetooth support #13

Closed PabloPL closed 4 years ago

PabloPL commented 6 years ago

Hint: 3000000 is uart speed on Nexus S. It's connected to uart0.

There is already driver (maybe it could be used): brcm,bcm43438-bt

xc-racer99 commented 6 years ago

Can confirm that the bcm43438-bt driver detects the chip correctly, however, I've yet to test functionality. dmesg snippet

Bluetooth: hci0: BCM: failed to write update baudrate (-110)
Bluetooth: hci0: Failed to set baudrate
Bluetooth: hci0: BCM: chip id 41
Bluetooth: hci0: BCM: features 0x04
Bluetooth: hci0: BCM4329B1
Bluetooth: hci0: BCM (002.002.023) build 0000
bluetooth hci0: Direct firmware load for brcm/BCM.hcd failed with error -2
Bluetooth: hci0: BCM: Patch brcm/BCM.hcd not found

git diff

diff --git a/arch/arm/boot/dts/s5pv210-aries.dtsi b/arch/arm/boot/dts/s5pv210-aries.dtsi
index d3668f5bf7d5..0861ce1c2847 100644
--- a/arch/arm/boot/dts/s5pv210-aries.dtsi
+++ b/arch/arm/boot/dts/s5pv210-aries.dtsi
@@ -424,6 +424,13 @@
        samsung,pin-pud = <S3C64XX_PIN_PULL_UP>;
        samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
    };
+
+   bt_host_wake: bt-host-wake {
+       samsung,pins = "gph2-5";
+       samsung,pin-function = <EXYNOS_PIN_FUNC_INPUT>;
+       samsung,pin-pud = <S3C64XX_PIN_PULL_DOWN>;
+       samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
+   };
 };

 &uart0 {
@@ -533,3 +540,16 @@
        avdd-supply = <&tsp_avdd_reg>;
    };
 };
+
+&uart0 {
+   pinctrl-names = "default";
+   pinctrl-0 = <&uart0_data &uart0_fctl &bt_host_wake /* &wlan_bt_en */>;
+
+   bluetooth {
+       compatible = "brcm,bcm43438-bt";
+       max-speed = <3000000>;
+       shutdown-gpios = <&gpb 3 GPIO_ACTIVE_HIGH>;
+       device-wakeup-gpios = <&gpg3 4 GPIO_ACTIVE_HIGH>;
+       host-wakeup-gpios = <&gph2 5 GPIO_ACTIVE_HIGH>;
+   };
+};
diff --git a/arch/arm/configs/s5pv210_defconfig b/arch/arm/configs/s5pv210_defconfig
index c057d474025c..cded9b49ad15 100644
--- a/arch/arm/configs/s5pv210_defconfig
+++ b/arch/arm/configs/s5pv210_defconfig
@@ -27,6 +27,11 @@ CONFIG_IP_PNP=y
 CONFIG_IP_PNP_DHCP=y
 CONFIG_IP_PNP_BOOTP=y
 CONFIG_IP_PNP_RARP=y
+CONFIG_BT=y
+CONFIG_BT_RFCOMM=y
+CONFIG_BT_BNEP=y
+CONFIG_BT_HCIUART=y
+CONFIG_BT_HCIUART_BCM=y
 CONFIG_CFG80211=y
 CONFIG_MAC80211=y
 CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
@@ -51,6 +56,7 @@ CONFIG_INPUT_PWM_VIBRA=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_SAMSUNG=y
 CONFIG_SERIAL_SAMSUNG_CONSOLE=y
+CONFIG_SERIAL_DEV_BUS=y
 CONFIG_HW_RANDOM=y
 CONFIG_I2C_GPIO=y
 CONFIG_I2C_S3C2410=y
PabloPL commented 6 years ago

bluetooth hci0: Direct firmware load for brcm/BCM.hcd failed with error -2 Bluetooth: hci0: BCM: Patch brcm/BCM.hcd not found

You need to download stock bluetooth firmware and put it as /lib/firmware/brcm/BCM.hcd (to get rid of this error)

I've also tried exactly the same thing (before Your comment) and got the same result :/.

xc-racer99 commented 6 years ago

Well, adding firmware causes some new errors :)

Bluetooth: hci0: BCM: failed to write update baudrate (-110)
Bluetooth: hci0: Failed to set baudrate
Bluetooth: hci0: BCM: chip id 41
Bluetooth: hci0: BCM: features 0x04
Bluetooth: hci0: BCM4329B1
Bluetooth: hci0: BCM (002.002.023) build 0000
Bluetooth: hci0: Frame reassembly failed (-84)
Bluetooth: hci0: command 0x0c03 tx timeout
Bluetooth: hci0: BCM: Reset failed (-110)

Guess I'll try and figure out what's going wrong...

PabloPL commented 6 years ago

WIth this hack (https://github.com/lategoodbye/rpi-zero/commit/ed5900296dfb7aec7f467477440751e7367a1881) but incrased to 50ms (like in stock kernel), we can "fix" tx timeouts, but it's still not working :/.

I've created branch for it for-upstream/bluetooth.

Stock kernel was using just hci_uart + broadcom_patchram_plus binary (I've looked at old kernel).

xc-racer99 commented 6 years ago

Created new branch https://github.com/PabloPL/linux/tree/for-upstream/bluetooth-2 with semi-working bluetooth.

There were two issues: 1) The device sent out a couple of unknown packets (this was the cause of the EILSEQ errors). Fixed in https://github.com/PabloPL/linux/commit/e83dcc8a62521030f5d899f08f991580977a989a 2) When loading the firmware, it resets the chip's baudrate to 115200 - while the uart controller is still at 3000000. Unfortunately, there's no easy way to workaround this in the driver at this point in time, so https://github.com/PabloPL/linux/commit/dd9d0cf129e6dcb6ef50a0dd2410461172ca1446 caps the baudrate at the initial baudrate (but at least I can scan and detect other devices!). Note that this can be found in the brcm_patchram_plus source code at https://github.com/beidl/brcm-patchram-plus/blob/master/brcm_patchram_plus.c#L658 or the equivalent in Android on line 717 of https://android.googlesource.com/platform/hardware/broadcom/libbt/+/kitkat-mr2.2-release/src/hardware.c

PabloPL commented 6 years ago

Looking at bcm_setup from hci_bcm i'm wondering it's not an error (not tested). There is init_speed and oper_speed (max_speed), but both are set after sending firmware to bluetooth device (bcm4329). Shouldn't be settings init_speed done before it (sending firmware)?

In this way we would:

xc-racer99 commented 6 years ago

Well, looking into this some more, it looks like the samsung serial driver isn't setup to do 3000000 baud... Having a look at https://github.com/PabloPL/linux/blob/master/drivers/tty/serial/samsung.c#L1290 the most that the driver advertises support for is 115200*8 (921600) which is less than 3000000.

Need to do some more testing...

PabloPL commented 6 years ago

Datasheet for s5pv210 is also mentioning that uart supports up to 3Mbps. Maybe we could ask at linux-exynos irc channel about this.

xc-racer99 commented 6 years ago

Yeah, just switching the value doesn't work.

Few other random musings: 1) Different defaults for RTS - https://github.com/xc-racer99/android_kernel_samsung_aries/blob/aosp-7.1/drivers/tty/serial/samsung.c#L353 had this functions, this has also changed in mainline kernel in 2d1e5a48965be57eefce033894d9795672f6ab63 2) Could be a clock issue, vendor kernel explicitly sets the rate and parent of the uart sclk in https://github.com/xc-racer99/android_kernel_samsung_aries/blob/aosp-7.1/arch/arm/mach-s5pv210/clock.c#L1583 3) clk_uart_baud1 in serial driver never actually appears to be used, only clk_uart_baud0

PabloPL commented 5 years ago

Send patch https://github.com/PabloPL/linux/commit/c41618f6bf07927d0a344654058b5de6369e7ce8 to mainline.

Also added new patch https://github.com/PabloPL/linux/commit/b2ea84c29a9ae1775e825bd984fafd779f9df29f which will addd bcm4329 version info to btbcm driver. Previously it didn't know about it and tried to load

/lib/firmware/brcm/BCM.hcd

firmware file (like it's for all unknown devices). Now it'll try to load

/lib/firmware/brcm/BCM4329B1.hcd

Edit 1: Both patches has been accepted in mainline.

PabloPL commented 5 years ago

TODO:

xc-racer99 commented 4 years ago

The max speed changes are now merged upstream. While

Currently wifi needs to be enabled before bt (one gpio enables both wifi and bt). Handle it in dts somehow

still applies, I think this is fine to leave this way.