// SPDX-License-Identifier: GPL-2.0-only OR MIT /* * Bluetooth HCI driver for Broadcom 4377/4378/4387/4388 devices attached via PCIe * * Copyright (C) The Asahi Linux Contributors */ #include <linux/async.h> #include <linux/bitfield.h> #include <linux/completion.h> #include <linux/dma-mapping.h> #include <linux/dmi.h> #include <linux/firmware.h> #include <linux/module.h> #include <linux/msi.h> #include <linux/of.h> #include <linux/pci.h> #include <linux/printk.h> #include <asm/unaligned.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> enum bcm4377_chip { … }; #define BCM4377_DEVICE_ID … #define BCM4378_DEVICE_ID … #define BCM4387_DEVICE_ID … #define BCM4388_DEVICE_ID … #define BCM4377_TIMEOUT … #define BCM4377_BOOT_TIMEOUT … /* * These devices only support DMA transactions inside a 32bit window * (possibly to avoid 64 bit arithmetic). The window size cannot exceed * 0xffffffff but is always aligned down to the previous 0x200 byte boundary * which effectively limits the window to [start, start+0xfffffe00]. * We just limit the DMA window to [0, 0xfffffe00] to make sure we don't * run into this limitation. */ #define BCM4377_DMA_MASK … #define BCM4377_PCIECFG_BAR0_WINDOW1 … #define BCM4377_PCIECFG_BAR0_WINDOW2 … #define BCM4377_PCIECFG_BAR0_CORE2_WINDOW1 … #define BCM4377_PCIECFG_BAR0_CORE2_WINDOW2 … #define BCM4377_PCIECFG_BAR2_WINDOW … #define BCM4377_PCIECFG_BAR0_CORE2_WINDOW1_DEFAULT … #define BCM4377_PCIECFG_BAR2_WINDOW_DEFAULT … #define BCM4377_PCIECFG_SUBSYSTEM_CTRL … #define BCM4377_BAR0_FW_DOORBELL … #define BCM4377_BAR0_RTI_CONTROL … #define BCM4377_BAR0_SLEEP_CONTROL … #define BCM4377_BAR0_SLEEP_CONTROL_UNQUIESCE … #define BCM4377_BAR0_SLEEP_CONTROL_AWAKE … #define BCM4377_BAR0_SLEEP_CONTROL_QUIESCE … #define BCM4377_BAR0_DOORBELL … #define BCM4377_BAR0_DOORBELL_VALUE … #define BCM4377_BAR0_DOORBELL_IDX … #define BCM4377_BAR0_DOORBELL_RING … #define BCM4377_BAR0_HOST_WINDOW_LO … #define BCM4377_BAR0_HOST_WINDOW_HI … #define BCM4377_BAR0_HOST_WINDOW_SIZE … #define BCM4377_BAR2_BOOTSTAGE … #define BCM4377_BAR2_FW_LO … #define BCM4377_BAR2_FW_HI … #define BCM4377_BAR2_FW_SIZE … #define BCM4377_BAR2_CONTEXT_ADDR_LO … #define BCM4377_BAR2_CONTEXT_ADDR_HI … #define BCM4377_BAR2_RTI_STATUS … #define BCM4377_BAR2_RTI_WINDOW_LO … #define BCM4377_BAR2_RTI_WINDOW_HI … #define BCM4377_BAR2_RTI_WINDOW_SIZE … #define BCM4377_OTP_SIZE … #define BCM4377_OTP_SYS_VENDOR … #define BCM4377_OTP_CIS … #define BCM4377_OTP_VENDOR_HDR … #define BCM4377_OTP_MAX_PARAM_LEN … #define BCM4377_N_TRANSFER_RINGS … #define BCM4377_N_COMPLETION_RINGS … #define BCM4377_MAX_RING_SIZE … #define BCM4377_MSGID_GENERATION … #define BCM4377_MSGID_ID … #define BCM4377_RING_N_ENTRIES … #define BCM4377_CONTROL_MSG_SIZE … #define BCM4377_XFER_RING_MAX_INPLACE_PAYLOAD_SIZE … #define MAX_ACL_PAYLOAD_SIZE … #define MAX_SCO_PAYLOAD_SIZE … #define MAX_EVENT_PAYLOAD_SIZE … enum bcm4377_otp_params_type { … }; enum bcm4377_transfer_ring_id { … }; enum bcm4377_completion_ring_id { … }; enum bcm4377_doorbell { … }; /* * Transfer ring entry * * flags: Flags to indicate if the payload is appended or mapped * len: Payload length * payload: Optional payload DMA address * id: Message id to recognize the answer in the completion ring entry */ struct bcm4377_xfer_ring_entry { … } __packed; static_assert(…); /* * Completion ring entry * * flags: Flags to indicate if the payload is appended or mapped. If the payload * is mapped it can be found in the buffer of the corresponding transfer * ring message. * ring_id: Transfer ring ID which required this message * msg_id: Message ID specified in transfer ring entry * len: Payload length */ struct bcm4377_completion_ring_entry { … } __packed; static_assert(…); enum bcm4377_control_message_type { … }; /* * Control message used to create a completion ring * * msg_type: Must be BCM4377_CONTROL_MSG_CREATE_COMPLETION_RING * header_size: Unknown, but probably reserved space in front of the entry * footer_size: Number of 32 bit words reserved for payloads after the entry * id/id_again: Completion ring index * ring_iova: DMA address of the ring buffer * n_elements: Number of elements inside the ring buffer * msi: MSI index, doesn't work for all rings though and should be zero * intmod_delay: Unknown delay * intmod_bytes: Unknown */ struct bcm4377_create_completion_ring_msg { … } __packed; static_assert(…); /* * Control ring message used to destroy a completion ring * * msg_type: Must be BCM4377_CONTROL_MSG_DESTROY_COMPLETION_RING * ring_id: Completion ring to be destroyed */ struct bcm4377_destroy_completion_ring_msg { … } __packed; static_assert(…); /* * Control message used to create a transfer ring * * msg_type: Must be BCM4377_CONTROL_MSG_CREATE_XFER_RING * header_size: Number of 32 bit words reserved for unknown content before the * entry * footer_size: Number of 32 bit words reserved for payloads after the entry * ring_id/ring_id_again: Transfer ring index * ring_iova: DMA address of the ring buffer * n_elements: Number of elements inside the ring buffer * completion_ring_id: Completion ring index for acknowledgements and events * doorbell: Doorbell index used to notify device of new entries * flags: Transfer ring flags * - virtual: set if there is no associated shared memory and only the * corresponding completion ring is used * - sync: only set for the SCO rings */ struct bcm4377_create_transfer_ring_msg { … } __packed; static_assert(…); /* * Control ring message used to destroy a transfer ring * * msg_type: Must be BCM4377_CONTROL_MSG_DESTROY_XFER_RING * ring_id: Transfer ring to be destroyed */ struct bcm4377_destroy_transfer_ring_msg { … } __packed; static_assert(…); /* * "Converged IPC" context struct used to make the device aware of all other * shared memory structures. A pointer to this structure is configured inside a * MMIO register. * * version: Protocol version, must be 2. * size: Size of this structure, must be 0x68. * enabled_caps: Enabled capabilities. Unknown bitfield but should be 2. * peripheral_info_addr: DMA address for a 0x20 buffer to which the device will * write unknown contents * {completion,xfer}_ring_{tails,heads}_addr: DMA pointers to ring heads/tails * n_completion_rings: Number of completion rings, the firmware only works if * this is set to BCM4377_N_COMPLETION_RINGS. * n_xfer_rings: Number of transfer rings, the firmware only works if * this is set to BCM4377_N_TRANSFER_RINGS. * control_completion_ring_addr: Control completion ring buffer DMA address * control_xfer_ring_addr: Control transfer ring buffer DMA address * control_xfer_ring_n_entries: Number of control transfer ring entries * control_completion_ring_n_entries: Number of control completion ring entries * control_xfer_ring_doorbell: Control transfer ring doorbell * control_completion_ring_doorbell: Control completion ring doorbell, * must be set to 0xffff * control_xfer_ring_msi: Control completion ring MSI index, must be 0 * control_completion_ring_msi: Control completion ring MSI index, must be 0. * control_xfer_ring_header_size: Number of 32 bit words reserved in front of * every control transfer ring entry * control_xfer_ring_footer_size: Number of 32 bit words reserved after every * control transfer ring entry * control_completion_ring_header_size: Number of 32 bit words reserved in front * of every control completion ring entry * control_completion_ring_footer_size: Number of 32 bit words reserved after * every control completion ring entry * scratch_pad: Optional scratch pad DMA address * scratch_pad_size: Scratch pad size */ struct bcm4377_context { … } __packed; static_assert(…); #define BCM4378_CALIBRATION_CHUNK_SIZE … struct bcm4378_hci_send_calibration_cmd { … } __packed; #define BCM4378_PTB_CHUNK_SIZE … struct bcm4378_hci_send_ptb_cmd { … } __packed; /* * Shared memory structure used to store the ring head and tail pointers. */ struct bcm4377_ring_state { … }; /* * A transfer ring can be used in two configurations: * 1) Send control or HCI messages to the device which are then acknowledged * in the corresponding completion ring * 2) Receiving HCI frames from the devices. In this case the transfer ring * itself contains empty messages that are acknowledged once data is * available from the device. If the payloads fit inside the footers * of the completion ring the transfer ring can be configured to be * virtual such that it has no ring buffer. * * ring_id: ring index hardcoded in the firmware * doorbell: doorbell index to notify device of new entries * payload_size: optional in-place payload size * mapped_payload_size: optional out-of-place payload size * completion_ring: index of corresponding completion ring * n_entries: number of entries inside this ring * generation: ring generation; incremented on hci_open to detect stale messages * sync: set to true for SCO rings * virtual: set to true if this ring has no entries and is just required to * setup a corresponding completion ring for device->host messages * d2h_buffers_only: set to true if this ring is only used to provide large * buffers used by device->host messages in the completion * ring * allow_wait: allow to wait for messages to be acknowledged * enabled: true once the ring has been created and can be used * ring: ring buffer for entries (struct bcm4377_xfer_ring_entry) * ring_dma: DMA address for ring entry buffer * payloads: payload buffer for mapped_payload_size payloads * payloads_dma:DMA address for payload buffer * events: pointer to array of completions if waiting is allowed * msgids: bitmap to keep track of used message ids * lock: Spinlock to protect access to ring structurs used in the irq handler */ struct bcm4377_transfer_ring { … }; /* * A completion ring can be either used to either acknowledge messages sent in * the corresponding transfer ring or to receive messages associated with the * transfer ring. When used to receive messages the transfer ring either * has no ring buffer and is only advanced ("virtual transfer ring") or it * only contains empty DMA buffers to be used for the payloads. * * ring_id: completion ring id, hardcoded in firmware * payload_size: optional payload size after each entry * delay: unknown delay * n_entries: number of entries in this ring * enabled: true once the ring has been created and can be used * ring: ring buffer for entries (struct bcm4377_completion_ring_entry) * ring_dma: DMA address of ring buffer * transfer_rings: bitmap of corresponding transfer ring ids */ struct bcm4377_completion_ring { … }; struct bcm4377_data; /* * Chip-specific configuration struct * * id: Chip id (e.g. 0x4377 for BCM4377) * otp_offset: Offset to the start of the OTP inside BAR0 * bar0_window1: Backplane address mapped to the first window in BAR0 * bar0_window2: Backplane address mapped to the second window in BAR0 * bar0_core2_window2: Optional backplane address mapped to the second core's * second window in BAR0 * has_bar0_core2_window2: Set to true if this chip requires the second core's * second window to be configured * bar2_offset: Offset to the start of the variables in BAR2 * clear_pciecfg_subsystem_ctrl_bit19: Set to true if bit 19 in the * vendor-specific subsystem control * register has to be cleared * disable_aspm: Set to true if ASPM must be disabled due to hardware errata * broken_ext_scan: Set to true if the chip erroneously claims to support * extended scanning * broken_mws_transport_config: Set to true if the chip erroneously claims to * support MWS Transport Configuration * broken_le_ext_adv_report_phy: Set to true if this chip stuffs flags inside * reserved bits of Primary/Secondary_PHY inside * LE Extended Advertising Report events which * have to be ignored * send_calibration: Optional callback to send calibration data * send_ptb: Callback to send "PTB" regulatory/calibration data */ struct bcm4377_hw { … }; static const struct bcm4377_hw bcm4377_hw_variants[]; static const struct dmi_system_id bcm4377_dmi_board_table[]; /* * Private struct associated with each device containing global state * * pdev: Pointer to associated struct pci_dev * hdev: Pointer to associated strucy hci_dev * bar0: iomem pointing to BAR0 * bar1: iomem pointing to BAR2 * bootstage: Current value of the bootstage * rti_status: Current "RTI" status value * hw: Pointer to chip-specific struct bcm4377_hw * taurus_cal_blob: "Taurus" calibration blob used for some chips * taurus_cal_size: "Taurus" calibration blob size * taurus_beamforming_cal_blob: "Taurus" beamforming calibration blob used for * some chips * taurus_beamforming_cal_size: "Taurus" beamforming calibration blob size * stepping: Chip stepping read from OTP; used for firmware selection * vendor: Antenna vendor read from OTP; used for firmware selection * board_type: Board type from FDT or DMI match; used for firmware selection * event: Event for changed bootstage or rti_status; used for booting firmware * ctx: "Converged IPC" context * ctx_dma: "Converged IPC" context DMA address * ring_state: Shared memory buffer containing ring head and tail indexes * ring_state_dma: DMA address for ring_state * {control,hci_acl,sco}_ack_ring: Completion rings used to acknowledge messages * {hci_acl,sco}_event_ring: Completion rings used for device->host messages * control_h2d_ring: Transfer ring used for control messages * {hci,sco,acl}_h2d_ring: Transfer ring used to transfer HCI frames * {hci,sco,acl}_d2h_ring: Transfer ring used to receive HCI frames in the * corresponding completion ring */ struct bcm4377_data { … }; static void bcm4377_ring_doorbell(struct bcm4377_data *bcm4377, u8 doorbell, u16 val) { … } static int bcm4377_extract_msgid(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring, u16 raw_msgid, u8 *msgid) { … } static void bcm4377_handle_event(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring, u16 raw_msgid, u8 entry_flags, u8 type, void *payload, size_t len) { … } static void bcm4377_handle_ack(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring, u16 raw_msgid) { … } static void bcm4377_handle_completion(struct bcm4377_data *bcm4377, struct bcm4377_completion_ring *ring, u16 pos) { … } static void bcm4377_poll_completion_ring(struct bcm4377_data *bcm4377, struct bcm4377_completion_ring *ring) { … } static irqreturn_t bcm4377_irq(int irq, void *data) { … } static int bcm4377_enqueue(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring, void *data, size_t len, bool wait) { … } static int bcm4377_create_completion_ring(struct bcm4377_data *bcm4377, struct bcm4377_completion_ring *ring) { … } static int bcm4377_destroy_completion_ring(struct bcm4377_data *bcm4377, struct bcm4377_completion_ring *ring) { … } static int bcm4377_create_transfer_ring(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring) { … } static int bcm4377_destroy_transfer_ring(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring) { … } static int __bcm4378_send_calibration_chunk(struct bcm4377_data *bcm4377, const void *data, size_t data_len, u16 blocks_left) { … } static int __bcm4378_send_calibration(struct bcm4377_data *bcm4377, const void *data, size_t data_size) { … } static int bcm4378_send_calibration(struct bcm4377_data *bcm4377) { … } static int bcm4387_send_calibration(struct bcm4377_data *bcm4377) { … } static int bcm4388_send_calibration(struct bcm4377_data *bcm4377) { … } static const struct firmware *bcm4377_request_blob(struct bcm4377_data *bcm4377, const char *suffix) { … } static int bcm4377_send_ptb(struct bcm4377_data *bcm4377, const struct firmware *fw) { … } static int bcm4378_send_ptb_chunk(struct bcm4377_data *bcm4377, const void *data, size_t data_len, u16 blocks_left) { … } static int bcm4378_send_ptb(struct bcm4377_data *bcm4377, const struct firmware *fw) { … } static int bcm4377_hci_open(struct hci_dev *hdev) { … } static int bcm4377_hci_close(struct hci_dev *hdev) { … } static bool bcm4377_is_valid_bdaddr(struct bcm4377_data *bcm4377, bdaddr_t *addr) { … } static int bcm4377_check_bdaddr(struct bcm4377_data *bcm4377) { … } static int bcm4377_hci_setup(struct hci_dev *hdev) { … } static int bcm4377_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) { … } static int bcm4377_hci_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) { … } static int bcm4377_alloc_transfer_ring(struct bcm4377_data *bcm4377, struct bcm4377_transfer_ring *ring) { … } static int bcm4377_alloc_completion_ring(struct bcm4377_data *bcm4377, struct bcm4377_completion_ring *ring) { … } static int bcm4377_init_context(struct bcm4377_data *bcm4377) { … } static int bcm4377_prepare_rings(struct bcm4377_data *bcm4377) { … } static int bcm4377_boot(struct bcm4377_data *bcm4377) { … } static int bcm4377_setup_rti(struct bcm4377_data *bcm4377) { … } static int bcm4377_parse_otp_board_params(struct bcm4377_data *bcm4377, char tag, const char *val, size_t len) { … } static int bcm4377_parse_otp_chip_params(struct bcm4377_data *bcm4377, char tag, const char *val, size_t len) { … } static int bcm4377_parse_otp_str(struct bcm4377_data *bcm4377, const u8 *str, enum bcm4377_otp_params_type type) { … } static int bcm4377_parse_otp_sys_vendor(struct bcm4377_data *bcm4377, u8 *otp, size_t size) { … } static int bcm4377_parse_otp(struct bcm4377_data *bcm4377) { … } static int bcm4377_init_cfg(struct bcm4377_data *bcm4377) { … } static int bcm4377_probe_dmi(struct bcm4377_data *bcm4377) { … } static int bcm4377_probe_of(struct bcm4377_data *bcm4377) { … } static void bcm4377_disable_aspm(struct bcm4377_data *bcm4377) { … } static void bcm4377_pci_free_irq_vectors(void *data) { … } static void bcm4377_hci_free_dev(void *data) { … } static void bcm4377_hci_unregister_dev(void *data) { … } static int bcm4377_probe(struct pci_dev *pdev, const struct pci_device_id *id) { … } static int bcm4377_suspend(struct pci_dev *pdev, pm_message_t state) { … } static int bcm4377_resume(struct pci_dev *pdev) { … } static const struct dmi_system_id bcm4377_dmi_board_table[] = …; static const struct bcm4377_hw bcm4377_hw_variants[] = …; #define BCM4377_DEVID_ENTRY(id) … static const struct pci_device_id bcm4377_devid_table[] = …; MODULE_DEVICE_TABLE(pci, bcm4377_devid_table); static struct pci_driver bcm4377_pci_driver = …; module_pci_driver(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …; MODULE_FIRMWARE(…) …;