// SPDX-License-Identifier: GPL-2.0 /* * Beagleplay Linux Driver for Greybus * * Copyright (c) 2023 Ayush Singh <[email protected]> * Copyright (c) 2023 BeagleBoard.org Foundation */ #include <linux/unaligned.h> #include <linux/crc32.h> #include <linux/gpio/consumer.h> #include <linux/firmware.h> #include <linux/greybus.h> #include <linux/serdev.h> #include <linux/crc-ccitt.h> #include <linux/circ_buf.h> #define CC1352_FIRMWARE_SIZE … #define CC1352_BOOTLOADER_TIMEOUT … #define CC1352_BOOTLOADER_ACK … #define CC1352_BOOTLOADER_NACK … #define RX_HDLC_PAYLOAD … #define CRC_LEN … #define MAX_RX_HDLC … #define TX_CIRC_BUF_SIZE … #define ADDRESS_GREYBUS … #define ADDRESS_DBG … #define ADDRESS_CONTROL … #define HDLC_FRAME … #define HDLC_ESC … #define HDLC_XOR … #define CONTROL_SVC_START … #define CONTROL_SVC_STOP … /* The maximum number of CPorts supported by Greybus Host Device */ #define GB_MAX_CPORTS … /** * struct gb_beagleplay - BeaglePlay Greybus driver * * @sd: underlying serdev device * * @gb_hd: greybus host device * * @tx_work: hdlc transmit work * @tx_producer_lock: hdlc transmit data producer lock. acquired when appending data to buffer. * @tx_consumer_lock: hdlc transmit data consumer lock. acquired when sending data over uart. * @tx_circ_buf: hdlc transmit circular buffer. * @tx_crc: hdlc transmit crc-ccitt fcs * * @rx_buffer_len: length of receive buffer filled. * @rx_buffer: hdlc frame receive buffer * @rx_in_esc: hdlc rx flag to indicate ESC frame * * @fwl: underlying firmware upload device * @bootloader_backdoor_gpio: cc1352p7 boot gpio * @rst_gpio: cc1352p7 reset gpio * @flashing_mode: flag to indicate that flashing is currently in progress * @fwl_ack_com: completion to signal an Ack/Nack * @fwl_ack: Ack/Nack byte received * @fwl_cmd_response_com: completion to signal a bootloader command response * @fwl_cmd_response: bootloader command response data * @fwl_crc32: crc32 of firmware to flash * @fwl_reset_addr: flag to indicate if we need to send COMMAND_DOWNLOAD again */ struct gb_beagleplay { … }; /** * struct hdlc_payload - Structure to represent part of HDCL frame payload data. * * @len: buffer length in bytes * @buf: payload buffer */ struct hdlc_payload { … }; /** * struct hdlc_greybus_frame - Structure to represent greybus HDLC frame payload * * @cport: cport id * @hdr: greybus operation header * @payload: greybus message payload * * The HDLC payload sent over UART for greybus address has cport preappended to greybus message */ struct hdlc_greybus_frame { … } __packed; /** * enum cc1352_bootloader_cmd: CC1352 Bootloader Commands * * @COMMAND_DOWNLOAD: Prepares flash programming * @COMMAND_GET_STATUS: Returns the status of the last command that was issued * @COMMAND_SEND_DATA: Transfers data and programs flash * @COMMAND_RESET: Performs a system reset * @COMMAND_CRC32: Calculates CRC32 over a specified memory area * @COMMAND_BANK_ERASE: Performs an erase of all of the customer-accessible * flash sectors not protected by FCFG1 and CCFG * writeprotect bits. * * CC1352 Bootloader serial bus commands */ enum cc1352_bootloader_cmd { … }; /** * enum cc1352_bootloader_status: CC1352 Bootloader COMMAND_GET_STATUS response * * @COMMAND_RET_SUCCESS: Status for successful command * @COMMAND_RET_UNKNOWN_CMD: Status for unknown command * @COMMAND_RET_INVALID_CMD: Status for invalid command (in other words, * incorrect packet size) * @COMMAND_RET_INVALID_ADR: Status for invalid input address * @COMMAND_RET_FLASH_FAIL: Status for failing flash erase or program operation */ enum cc1352_bootloader_status { … }; /** * struct cc1352_bootloader_packet: CC1352 Bootloader Request Packet * * @len: length of packet + optional request data * @checksum: 8-bit checksum excluding len * @cmd: bootloader command */ struct cc1352_bootloader_packet { … } __packed; #define CC1352_BOOTLOADER_PKT_MAX_SIZE … /** * struct cc1352_bootloader_download_cmd_data: CC1352 Bootloader COMMAND_DOWNLOAD request data * * @addr: address to start programming data into * @size: size of data that will be sent */ struct cc1352_bootloader_download_cmd_data { … } __packed; /** * struct cc1352_bootloader_crc32_cmd_data: CC1352 Bootloader COMMAND_CRC32 request data * * @addr: address where crc32 calculation starts * @size: number of bytes comprised by crc32 calculation * @read_repeat: number of read repeats for each data location */ struct cc1352_bootloader_crc32_cmd_data { … } __packed; static void hdlc_rx_greybus_frame(struct gb_beagleplay *bg, u8 *buf, u16 len) { … } static void hdlc_rx_dbg_frame(const struct gb_beagleplay *bg, const char *buf, u16 len) { … } /** * hdlc_write() - Consume HDLC Buffer. * @bg: beagleplay greybus driver * * Assumes that consumer lock has been acquired. */ static void hdlc_write(struct gb_beagleplay *bg) { … } /** * hdlc_append() - Queue HDLC data for sending. * @bg: beagleplay greybus driver * @value: hdlc byte to transmit * * Assumes that producer lock as been acquired. */ static void hdlc_append(struct gb_beagleplay *bg, u8 value) { … } static void hdlc_append_escaped(struct gb_beagleplay *bg, u8 value) { … } static void hdlc_append_tx_frame(struct gb_beagleplay *bg) { … } static void hdlc_append_tx_u8(struct gb_beagleplay *bg, u8 value) { … } static void hdlc_append_tx_buf(struct gb_beagleplay *bg, const u8 *buf, u16 len) { … } static void hdlc_append_tx_crc(struct gb_beagleplay *bg) { … } static void hdlc_transmit(struct work_struct *work) { … } static void hdlc_tx_frames(struct gb_beagleplay *bg, u8 address, u8 control, const struct hdlc_payload payloads[], size_t count) { … } static void hdlc_tx_s_frame_ack(struct gb_beagleplay *bg) { … } static void hdlc_rx_frame(struct gb_beagleplay *bg) { … } static size_t hdlc_rx(struct gb_beagleplay *bg, const u8 *data, size_t count) { … } static int hdlc_init(struct gb_beagleplay *bg) { … } static void hdlc_deinit(struct gb_beagleplay *bg) { … } /** * csum8: Calculate 8-bit checksum on data * * @data: bytes to calculate 8-bit checksum of * @size: number of bytes * @base: starting value for checksum */ static u8 csum8(const u8 *data, size_t size, u8 base) { … } static void cc1352_bootloader_send_ack(struct gb_beagleplay *bg) { … } static void cc1352_bootloader_send_nack(struct gb_beagleplay *bg) { … } /** * cc1352_bootloader_pkt_rx: Process a CC1352 Bootloader Packet * * @bg: beagleplay greybus driver * @data: packet buffer * @count: packet buffer size * * @return: number of bytes processed * * Here are the steps to successfully receive a packet from cc1352 bootloader * according to the docs: * 1. Wait for nonzero data to be returned from the device. This is important * as the device may send zero bytes between a sent and a received data * packet. The first nonzero byte received is the size of the packet that is * being received. * 2. Read the next byte, which is the checksum for the packet. * 3. Read the data bytes from the device. During the data phase, packet size * minus 2 bytes is sent. * 4. Calculate the checksum of the data bytes and verify it matches the * checksum received in the packet. * 5. Send an acknowledge byte or a not-acknowledge byte to the device to * indicate the successful or unsuccessful reception of the packet. */ static int cc1352_bootloader_pkt_rx(struct gb_beagleplay *bg, const u8 *data, size_t count) { … } static size_t cc1352_bootloader_rx(struct gb_beagleplay *bg, const u8 *data, size_t count) { … } static size_t gb_tty_receive(struct serdev_device *sd, const u8 *data, size_t count) { … } static void gb_tty_wakeup(struct serdev_device *serdev) { … } static struct serdev_device_ops gb_beagleplay_ops = …; /** * gb_message_send() - Send greybus message using HDLC over UART * * @hd: pointer to greybus host device * @cport: AP cport where message originates * @msg: greybus message to send * @mask: gfp mask * * Greybus HDLC frame has the following payload: * 1. le16 cport * 2. gb_operation_msg_hdr msg_header * 3. u8 *msg_payload */ static int gb_message_send(struct gb_host_device *hd, u16 cport, struct gb_message *msg, gfp_t mask) { … } static void gb_message_cancel(struct gb_message *message) { … } static struct gb_hd_driver gb_hdlc_driver = …; static void gb_beagleplay_start_svc(struct gb_beagleplay *bg) { … } static void gb_beagleplay_stop_svc(struct gb_beagleplay *bg) { … } static int cc1352_bootloader_wait_for_ack(struct gb_beagleplay *bg) { … } static int cc1352_bootloader_sync(struct gb_beagleplay *bg) { … } static int cc1352_bootloader_get_status(struct gb_beagleplay *bg) { … } static int cc1352_bootloader_erase(struct gb_beagleplay *bg) { … } static int cc1352_bootloader_reset(struct gb_beagleplay *bg) { … } /** * cc1352_bootloader_empty_pkt: Calculate the number of empty bytes in the current packet * * @data: packet bytes array to check * @size: number of bytes in array */ static size_t cc1352_bootloader_empty_pkt(const u8 *data, size_t size) { … } static int cc1352_bootloader_crc32(struct gb_beagleplay *bg, u32 *crc32) { … } static int cc1352_bootloader_download(struct gb_beagleplay *bg, u32 size, u32 addr) { … } static int cc1352_bootloader_send_data(struct gb_beagleplay *bg, const u8 *data, size_t size) { … } static void gb_greybus_deinit(struct gb_beagleplay *bg) { … } static int gb_greybus_init(struct gb_beagleplay *bg) { … } static enum fw_upload_err cc1352_prepare(struct fw_upload *fw_upload, const u8 *data, u32 size) { … } static void cc1352_cleanup(struct fw_upload *fw_upload) { … } static enum fw_upload_err cc1352_write(struct fw_upload *fw_upload, const u8 *data, u32 offset, u32 size, u32 *written) { … } static enum fw_upload_err cc1352_poll_complete(struct fw_upload *fw_upload) { … } static void cc1352_cancel(struct fw_upload *fw_upload) { … } static void gb_serdev_deinit(struct gb_beagleplay *bg) { … } static int gb_serdev_init(struct gb_beagleplay *bg) { … } static const struct fw_upload_ops cc1352_bootloader_ops = …; static int gb_fw_init(struct gb_beagleplay *bg) { … } static void gb_fw_deinit(struct gb_beagleplay *bg) { … } static int gb_beagleplay_probe(struct serdev_device *serdev) { … } static void gb_beagleplay_remove(struct serdev_device *serdev) { … } static const struct of_device_id gb_beagleplay_of_match[] = …; MODULE_DEVICE_TABLE(of, gb_beagleplay_of_match); static struct serdev_device_driver gb_beagleplay_driver = …; module_serdev_device_driver(…) …; MODULE_LICENSE(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …;