1ec558bbfSAyush Singh // SPDX-License-Identifier: GPL-2.0 2ec558bbfSAyush Singh /* 3ec558bbfSAyush Singh * Beagleplay Linux Driver for Greybus 4ec558bbfSAyush Singh * 5ec558bbfSAyush Singh * Copyright (c) 2023 Ayush Singh <ayushdevel1325@gmail.com> 6ec558bbfSAyush Singh * Copyright (c) 2023 BeagleBoard.org Foundation 7ec558bbfSAyush Singh */ 8ec558bbfSAyush Singh 9*0cf7befaSAyush Singh #include <asm-generic/unaligned.h> 10*0cf7befaSAyush Singh #include <linux/crc32.h> 11*0cf7befaSAyush Singh #include <linux/gpio/consumer.h> 12*0cf7befaSAyush Singh #include <linux/firmware.h> 13ec558bbfSAyush Singh #include <linux/greybus.h> 14ec558bbfSAyush Singh #include <linux/serdev.h> 15ec558bbfSAyush Singh #include <linux/crc-ccitt.h> 16ec558bbfSAyush Singh #include <linux/circ_buf.h> 17*0cf7befaSAyush Singh 18*0cf7befaSAyush Singh #define CC1352_FIRMWARE_SIZE (704 * 1024) 19*0cf7befaSAyush Singh #define CC1352_BOOTLOADER_TIMEOUT 2000 20*0cf7befaSAyush Singh #define CC1352_BOOTLOADER_ACK 0xcc 21*0cf7befaSAyush Singh #define CC1352_BOOTLOADER_NACK 0x33 22ec558bbfSAyush Singh 23ec558bbfSAyush Singh #define RX_HDLC_PAYLOAD 256 24ec558bbfSAyush Singh #define CRC_LEN 2 25ec558bbfSAyush Singh #define MAX_RX_HDLC (1 + RX_HDLC_PAYLOAD + CRC_LEN) 26ec558bbfSAyush Singh #define TX_CIRC_BUF_SIZE 1024 27ec558bbfSAyush Singh 28ec558bbfSAyush Singh #define ADDRESS_GREYBUS 0x01 29ec558bbfSAyush Singh #define ADDRESS_DBG 0x02 30ec558bbfSAyush Singh #define ADDRESS_CONTROL 0x03 31ec558bbfSAyush Singh 32ec558bbfSAyush Singh #define HDLC_FRAME 0x7E 33ec558bbfSAyush Singh #define HDLC_ESC 0x7D 34ec558bbfSAyush Singh #define HDLC_XOR 0x20 35ec558bbfSAyush Singh 36ec558bbfSAyush Singh #define CONTROL_SVC_START 0x01 37ec558bbfSAyush Singh #define CONTROL_SVC_STOP 0x02 38ec558bbfSAyush Singh 39ec558bbfSAyush Singh /* The maximum number of CPorts supported by Greybus Host Device */ 40ec558bbfSAyush Singh #define GB_MAX_CPORTS 32 41ec558bbfSAyush Singh 42ec558bbfSAyush Singh /** 43ec558bbfSAyush Singh * struct gb_beagleplay - BeaglePlay Greybus driver 44ec558bbfSAyush Singh * 45ec558bbfSAyush Singh * @sd: underlying serdev device 46ec558bbfSAyush Singh * 47ec558bbfSAyush Singh * @gb_hd: greybus host device 48ec558bbfSAyush Singh * 49ec558bbfSAyush Singh * @tx_work: hdlc transmit work 50ec558bbfSAyush Singh * @tx_producer_lock: hdlc transmit data producer lock. acquired when appending data to buffer. 51ec558bbfSAyush Singh * @tx_consumer_lock: hdlc transmit data consumer lock. acquired when sending data over uart. 52ec558bbfSAyush Singh * @tx_circ_buf: hdlc transmit circular buffer. 53ec558bbfSAyush Singh * @tx_crc: hdlc transmit crc-ccitt fcs 54ec558bbfSAyush Singh * 55ec558bbfSAyush Singh * @rx_buffer_len: length of receive buffer filled. 56ec558bbfSAyush Singh * @rx_buffer: hdlc frame receive buffer 57ec558bbfSAyush Singh * @rx_in_esc: hdlc rx flag to indicate ESC frame 58*0cf7befaSAyush Singh * 59*0cf7befaSAyush Singh * @fwl: underlying firmware upload device 60*0cf7befaSAyush Singh * @bootloader_backdoor_gpio: cc1352p7 boot gpio 61*0cf7befaSAyush Singh * @rst_gpio: cc1352p7 reset gpio 62*0cf7befaSAyush Singh * @flashing_mode: flag to indicate that flashing is currently in progress 63*0cf7befaSAyush Singh * @fwl_ack_com: completion to signal an Ack/Nack 64*0cf7befaSAyush Singh * @fwl_ack: Ack/Nack byte received 65*0cf7befaSAyush Singh * @fwl_cmd_response_com: completion to signal a bootloader command response 66*0cf7befaSAyush Singh * @fwl_cmd_response: bootloader command response data 67*0cf7befaSAyush Singh * @fwl_crc32: crc32 of firmware to flash 68*0cf7befaSAyush Singh * @fwl_reset_addr: flag to indicate if we need to send COMMAND_DOWNLOAD again 69ec558bbfSAyush Singh */ 70ec558bbfSAyush Singh struct gb_beagleplay { 71ec558bbfSAyush Singh struct serdev_device *sd; 72ec558bbfSAyush Singh 73ec558bbfSAyush Singh struct gb_host_device *gb_hd; 74ec558bbfSAyush Singh 75ec558bbfSAyush Singh struct work_struct tx_work; 76ec558bbfSAyush Singh spinlock_t tx_producer_lock; 77ec558bbfSAyush Singh spinlock_t tx_consumer_lock; 78ec558bbfSAyush Singh struct circ_buf tx_circ_buf; 79ec558bbfSAyush Singh u16 tx_crc; 80ec558bbfSAyush Singh 81ec558bbfSAyush Singh u16 rx_buffer_len; 82ec558bbfSAyush Singh bool rx_in_esc; 83ec558bbfSAyush Singh u8 rx_buffer[MAX_RX_HDLC]; 84*0cf7befaSAyush Singh 85*0cf7befaSAyush Singh struct fw_upload *fwl; 86*0cf7befaSAyush Singh struct gpio_desc *bootloader_backdoor_gpio; 87*0cf7befaSAyush Singh struct gpio_desc *rst_gpio; 88*0cf7befaSAyush Singh bool flashing_mode; 89*0cf7befaSAyush Singh struct completion fwl_ack_com; 90*0cf7befaSAyush Singh u8 fwl_ack; 91*0cf7befaSAyush Singh struct completion fwl_cmd_response_com; 92*0cf7befaSAyush Singh u32 fwl_cmd_response; 93*0cf7befaSAyush Singh u32 fwl_crc32; 94*0cf7befaSAyush Singh bool fwl_reset_addr; 95ec558bbfSAyush Singh }; 96ec558bbfSAyush Singh 97ec558bbfSAyush Singh /** 98ec558bbfSAyush Singh * struct hdlc_payload - Structure to represent part of HDCL frame payload data. 99ec558bbfSAyush Singh * 100ec558bbfSAyush Singh * @len: buffer length in bytes 101ec558bbfSAyush Singh * @buf: payload buffer 102ec558bbfSAyush Singh */ 103ec558bbfSAyush Singh struct hdlc_payload { 104ec558bbfSAyush Singh u16 len; 105ec558bbfSAyush Singh void *buf; 106ec558bbfSAyush Singh }; 107ec558bbfSAyush Singh 10808b34855SAyush Singh /** 10908b34855SAyush Singh * struct hdlc_greybus_frame - Structure to represent greybus HDLC frame payload 11008b34855SAyush Singh * 11108b34855SAyush Singh * @cport: cport id 11208b34855SAyush Singh * @hdr: greybus operation header 11308b34855SAyush Singh * @payload: greybus message payload 11408b34855SAyush Singh * 11508b34855SAyush Singh * The HDLC payload sent over UART for greybus address has cport preappended to greybus message 11608b34855SAyush Singh */ 11708b34855SAyush Singh struct hdlc_greybus_frame { 11808b34855SAyush Singh __le16 cport; 11908b34855SAyush Singh struct gb_operation_msg_hdr hdr; 12008b34855SAyush Singh u8 payload[]; 12108b34855SAyush Singh } __packed; 12208b34855SAyush Singh 123*0cf7befaSAyush Singh /** 124*0cf7befaSAyush Singh * enum cc1352_bootloader_cmd: CC1352 Bootloader Commands 125*0cf7befaSAyush Singh * 126*0cf7befaSAyush Singh * @COMMAND_DOWNLOAD: Prepares flash programming 127*0cf7befaSAyush Singh * @COMMAND_GET_STATUS: Returns the status of the last command that was issued 128*0cf7befaSAyush Singh * @COMMAND_SEND_DATA: Transfers data and programs flash 129*0cf7befaSAyush Singh * @COMMAND_RESET: Performs a system reset 130*0cf7befaSAyush Singh * @COMMAND_CRC32: Calculates CRC32 over a specified memory area 131*0cf7befaSAyush Singh * @COMMAND_BANK_ERASE: Performs an erase of all of the customer-accessible 132*0cf7befaSAyush Singh * flash sectors not protected by FCFG1 and CCFG 133*0cf7befaSAyush Singh * writeprotect bits. 134*0cf7befaSAyush Singh * 135*0cf7befaSAyush Singh * CC1352 Bootloader serial bus commands 136*0cf7befaSAyush Singh */ 137*0cf7befaSAyush Singh enum cc1352_bootloader_cmd { 138*0cf7befaSAyush Singh COMMAND_DOWNLOAD = 0x21, 139*0cf7befaSAyush Singh COMMAND_GET_STATUS = 0x23, 140*0cf7befaSAyush Singh COMMAND_SEND_DATA = 0x24, 141*0cf7befaSAyush Singh COMMAND_RESET = 0x25, 142*0cf7befaSAyush Singh COMMAND_CRC32 = 0x27, 143*0cf7befaSAyush Singh COMMAND_BANK_ERASE = 0x2c, 144*0cf7befaSAyush Singh }; 145*0cf7befaSAyush Singh 146*0cf7befaSAyush Singh /** 147*0cf7befaSAyush Singh * enum cc1352_bootloader_status: CC1352 Bootloader COMMAND_GET_STATUS response 148*0cf7befaSAyush Singh * 149*0cf7befaSAyush Singh * @COMMAND_RET_SUCCESS: Status for successful command 150*0cf7befaSAyush Singh * @COMMAND_RET_UNKNOWN_CMD: Status for unknown command 151*0cf7befaSAyush Singh * @COMMAND_RET_INVALID_CMD: Status for invalid command (in other words, 152*0cf7befaSAyush Singh * incorrect packet size) 153*0cf7befaSAyush Singh * @COMMAND_RET_INVALID_ADR: Status for invalid input address 154*0cf7befaSAyush Singh * @COMMAND_RET_FLASH_FAIL: Status for failing flash erase or program operation 155*0cf7befaSAyush Singh */ 156*0cf7befaSAyush Singh enum cc1352_bootloader_status { 157*0cf7befaSAyush Singh COMMAND_RET_SUCCESS = 0x40, 158*0cf7befaSAyush Singh COMMAND_RET_UNKNOWN_CMD = 0x41, 159*0cf7befaSAyush Singh COMMAND_RET_INVALID_CMD = 0x42, 160*0cf7befaSAyush Singh COMMAND_RET_INVALID_ADR = 0x43, 161*0cf7befaSAyush Singh COMMAND_RET_FLASH_FAIL = 0x44, 162*0cf7befaSAyush Singh }; 163*0cf7befaSAyush Singh 164*0cf7befaSAyush Singh /** 165*0cf7befaSAyush Singh * struct cc1352_bootloader_packet: CC1352 Bootloader Request Packet 166*0cf7befaSAyush Singh * 167*0cf7befaSAyush Singh * @len: length of packet + optional request data 168*0cf7befaSAyush Singh * @checksum: 8-bit checksum excluding len 169*0cf7befaSAyush Singh * @cmd: bootloader command 170*0cf7befaSAyush Singh */ 171*0cf7befaSAyush Singh struct cc1352_bootloader_packet { 172*0cf7befaSAyush Singh u8 len; 173*0cf7befaSAyush Singh u8 checksum; 174*0cf7befaSAyush Singh u8 cmd; 175*0cf7befaSAyush Singh } __packed; 176*0cf7befaSAyush Singh 177*0cf7befaSAyush Singh #define CC1352_BOOTLOADER_PKT_MAX_SIZE \ 178*0cf7befaSAyush Singh (U8_MAX - sizeof(struct cc1352_bootloader_packet)) 179*0cf7befaSAyush Singh 180*0cf7befaSAyush Singh /** 181*0cf7befaSAyush Singh * struct cc1352_bootloader_download_cmd_data: CC1352 Bootloader COMMAND_DOWNLOAD request data 182*0cf7befaSAyush Singh * 183*0cf7befaSAyush Singh * @addr: address to start programming data into 184*0cf7befaSAyush Singh * @size: size of data that will be sent 185*0cf7befaSAyush Singh */ 186*0cf7befaSAyush Singh struct cc1352_bootloader_download_cmd_data { 187*0cf7befaSAyush Singh __be32 addr; 188*0cf7befaSAyush Singh __be32 size; 189*0cf7befaSAyush Singh } __packed; 190*0cf7befaSAyush Singh 191*0cf7befaSAyush Singh /** 192*0cf7befaSAyush Singh * struct cc1352_bootloader_crc32_cmd_data: CC1352 Bootloader COMMAND_CRC32 request data 193*0cf7befaSAyush Singh * 194*0cf7befaSAyush Singh * @addr: address where crc32 calculation starts 195*0cf7befaSAyush Singh * @size: number of bytes comprised by crc32 calculation 196*0cf7befaSAyush Singh * @read_repeat: number of read repeats for each data location 197*0cf7befaSAyush Singh */ 198*0cf7befaSAyush Singh struct cc1352_bootloader_crc32_cmd_data { 199*0cf7befaSAyush Singh __be32 addr; 200*0cf7befaSAyush Singh __be32 size; 201*0cf7befaSAyush Singh __be32 read_repeat; 202*0cf7befaSAyush Singh } __packed; 203*0cf7befaSAyush Singh 204ec558bbfSAyush Singh static void hdlc_rx_greybus_frame(struct gb_beagleplay *bg, u8 *buf, u16 len) 205ec558bbfSAyush Singh { 20608b34855SAyush Singh struct hdlc_greybus_frame *gb_frame = (struct hdlc_greybus_frame *)buf; 20708b34855SAyush Singh u16 cport_id = le16_to_cpu(gb_frame->cport); 20808b34855SAyush Singh u16 gb_msg_len = le16_to_cpu(gb_frame->hdr.size); 209ec558bbfSAyush Singh 210ec558bbfSAyush Singh dev_dbg(&bg->sd->dev, "Greybus Operation %u type %X cport %u status %u received", 21108b34855SAyush Singh gb_frame->hdr.operation_id, gb_frame->hdr.type, cport_id, gb_frame->hdr.result); 212ec558bbfSAyush Singh 21308b34855SAyush Singh greybus_data_rcvd(bg->gb_hd, cport_id, (u8 *)&gb_frame->hdr, gb_msg_len); 214ec558bbfSAyush Singh } 215ec558bbfSAyush Singh 216ec558bbfSAyush Singh static void hdlc_rx_dbg_frame(const struct gb_beagleplay *bg, const char *buf, u16 len) 217ec558bbfSAyush Singh { 218ec558bbfSAyush Singh dev_dbg(&bg->sd->dev, "CC1352 Log: %.*s", (int)len, buf); 219ec558bbfSAyush Singh } 220ec558bbfSAyush Singh 221ec558bbfSAyush Singh /** 222ec558bbfSAyush Singh * hdlc_write() - Consume HDLC Buffer. 223ec558bbfSAyush Singh * @bg: beagleplay greybus driver 224ec558bbfSAyush Singh * 225ec558bbfSAyush Singh * Assumes that consumer lock has been acquired. 226ec558bbfSAyush Singh */ 227ec558bbfSAyush Singh static void hdlc_write(struct gb_beagleplay *bg) 228ec558bbfSAyush Singh { 229ec558bbfSAyush Singh int written; 230ec558bbfSAyush Singh /* Start consuming HDLC data */ 231ec558bbfSAyush Singh int head = smp_load_acquire(&bg->tx_circ_buf.head); 232ec558bbfSAyush Singh int tail = bg->tx_circ_buf.tail; 233ec558bbfSAyush Singh int count = CIRC_CNT_TO_END(head, tail, TX_CIRC_BUF_SIZE); 234ec558bbfSAyush Singh const unsigned char *buf = &bg->tx_circ_buf.buf[tail]; 235ec558bbfSAyush Singh 236ec558bbfSAyush Singh if (count > 0) { 237ec558bbfSAyush Singh written = serdev_device_write_buf(bg->sd, buf, count); 238ec558bbfSAyush Singh 239ec558bbfSAyush Singh /* Finish consuming HDLC data */ 240ec558bbfSAyush Singh smp_store_release(&bg->tx_circ_buf.tail, (tail + written) & (TX_CIRC_BUF_SIZE - 1)); 241ec558bbfSAyush Singh } 242ec558bbfSAyush Singh } 243ec558bbfSAyush Singh 244ec558bbfSAyush Singh /** 245ec558bbfSAyush Singh * hdlc_append() - Queue HDLC data for sending. 246ec558bbfSAyush Singh * @bg: beagleplay greybus driver 247ec558bbfSAyush Singh * @value: hdlc byte to transmit 248ec558bbfSAyush Singh * 249ec558bbfSAyush Singh * Assumes that producer lock as been acquired. 250ec558bbfSAyush Singh */ 251ec558bbfSAyush Singh static void hdlc_append(struct gb_beagleplay *bg, u8 value) 252ec558bbfSAyush Singh { 253ec558bbfSAyush Singh int tail, head = bg->tx_circ_buf.head; 254ec558bbfSAyush Singh 255ec558bbfSAyush Singh while (true) { 256ec558bbfSAyush Singh tail = READ_ONCE(bg->tx_circ_buf.tail); 257ec558bbfSAyush Singh 258ec558bbfSAyush Singh if (CIRC_SPACE(head, tail, TX_CIRC_BUF_SIZE) >= 1) { 259ec558bbfSAyush Singh bg->tx_circ_buf.buf[head] = value; 260ec558bbfSAyush Singh 261ec558bbfSAyush Singh /* Finish producing HDLC byte */ 262ec558bbfSAyush Singh smp_store_release(&bg->tx_circ_buf.head, 263ec558bbfSAyush Singh (head + 1) & (TX_CIRC_BUF_SIZE - 1)); 264ec558bbfSAyush Singh return; 265ec558bbfSAyush Singh } 266ec558bbfSAyush Singh dev_warn(&bg->sd->dev, "Tx circ buf full"); 267ec558bbfSAyush Singh usleep_range(3000, 5000); 268ec558bbfSAyush Singh } 269ec558bbfSAyush Singh } 270ec558bbfSAyush Singh 271ec558bbfSAyush Singh static void hdlc_append_escaped(struct gb_beagleplay *bg, u8 value) 272ec558bbfSAyush Singh { 273ec558bbfSAyush Singh if (value == HDLC_FRAME || value == HDLC_ESC) { 274ec558bbfSAyush Singh hdlc_append(bg, HDLC_ESC); 275ec558bbfSAyush Singh value ^= HDLC_XOR; 276ec558bbfSAyush Singh } 277ec558bbfSAyush Singh hdlc_append(bg, value); 278ec558bbfSAyush Singh } 279ec558bbfSAyush Singh 280ec558bbfSAyush Singh static void hdlc_append_tx_frame(struct gb_beagleplay *bg) 281ec558bbfSAyush Singh { 282ec558bbfSAyush Singh bg->tx_crc = 0xFFFF; 283ec558bbfSAyush Singh hdlc_append(bg, HDLC_FRAME); 284ec558bbfSAyush Singh } 285ec558bbfSAyush Singh 286ec558bbfSAyush Singh static void hdlc_append_tx_u8(struct gb_beagleplay *bg, u8 value) 287ec558bbfSAyush Singh { 288ec558bbfSAyush Singh bg->tx_crc = crc_ccitt(bg->tx_crc, &value, 1); 289ec558bbfSAyush Singh hdlc_append_escaped(bg, value); 290ec558bbfSAyush Singh } 291ec558bbfSAyush Singh 292ec558bbfSAyush Singh static void hdlc_append_tx_buf(struct gb_beagleplay *bg, const u8 *buf, u16 len) 293ec558bbfSAyush Singh { 294ec558bbfSAyush Singh size_t i; 295ec558bbfSAyush Singh 296ec558bbfSAyush Singh for (i = 0; i < len; i++) 297ec558bbfSAyush Singh hdlc_append_tx_u8(bg, buf[i]); 298ec558bbfSAyush Singh } 299ec558bbfSAyush Singh 300ec558bbfSAyush Singh static void hdlc_append_tx_crc(struct gb_beagleplay *bg) 301ec558bbfSAyush Singh { 302ec558bbfSAyush Singh bg->tx_crc ^= 0xffff; 303ec558bbfSAyush Singh hdlc_append_escaped(bg, bg->tx_crc & 0xff); 304ec558bbfSAyush Singh hdlc_append_escaped(bg, (bg->tx_crc >> 8) & 0xff); 305ec558bbfSAyush Singh } 306ec558bbfSAyush Singh 307ec558bbfSAyush Singh static void hdlc_transmit(struct work_struct *work) 308ec558bbfSAyush Singh { 309ec558bbfSAyush Singh struct gb_beagleplay *bg = container_of(work, struct gb_beagleplay, tx_work); 310ec558bbfSAyush Singh 311ec558bbfSAyush Singh spin_lock_bh(&bg->tx_consumer_lock); 312ec558bbfSAyush Singh hdlc_write(bg); 313ec558bbfSAyush Singh spin_unlock_bh(&bg->tx_consumer_lock); 314ec558bbfSAyush Singh } 315ec558bbfSAyush Singh 316ec558bbfSAyush Singh static void hdlc_tx_frames(struct gb_beagleplay *bg, u8 address, u8 control, 317ec558bbfSAyush Singh const struct hdlc_payload payloads[], size_t count) 318ec558bbfSAyush Singh { 319ec558bbfSAyush Singh size_t i; 320ec558bbfSAyush Singh 321ec558bbfSAyush Singh spin_lock(&bg->tx_producer_lock); 322ec558bbfSAyush Singh 323ec558bbfSAyush Singh hdlc_append_tx_frame(bg); 324ec558bbfSAyush Singh hdlc_append_tx_u8(bg, address); 325ec558bbfSAyush Singh hdlc_append_tx_u8(bg, control); 326ec558bbfSAyush Singh 327ec558bbfSAyush Singh for (i = 0; i < count; ++i) 328ec558bbfSAyush Singh hdlc_append_tx_buf(bg, payloads[i].buf, payloads[i].len); 329ec558bbfSAyush Singh 330ec558bbfSAyush Singh hdlc_append_tx_crc(bg); 331ec558bbfSAyush Singh hdlc_append_tx_frame(bg); 332ec558bbfSAyush Singh 333ec558bbfSAyush Singh spin_unlock(&bg->tx_producer_lock); 334ec558bbfSAyush Singh 335ec558bbfSAyush Singh schedule_work(&bg->tx_work); 336ec558bbfSAyush Singh } 337ec558bbfSAyush Singh 338ec558bbfSAyush Singh static void hdlc_tx_s_frame_ack(struct gb_beagleplay *bg) 339ec558bbfSAyush Singh { 340ec558bbfSAyush Singh hdlc_tx_frames(bg, bg->rx_buffer[0], (bg->rx_buffer[1] >> 1) & 0x7, NULL, 0); 341ec558bbfSAyush Singh } 342ec558bbfSAyush Singh 343ec558bbfSAyush Singh static void hdlc_rx_frame(struct gb_beagleplay *bg) 344ec558bbfSAyush Singh { 345ec558bbfSAyush Singh u16 crc, len; 346ec558bbfSAyush Singh u8 ctrl, *buf; 347ec558bbfSAyush Singh u8 address = bg->rx_buffer[0]; 348ec558bbfSAyush Singh 349ec558bbfSAyush Singh crc = crc_ccitt(0xffff, bg->rx_buffer, bg->rx_buffer_len); 350ec558bbfSAyush Singh if (crc != 0xf0b8) { 351ec558bbfSAyush Singh dev_warn_ratelimited(&bg->sd->dev, "CRC failed from %02x: 0x%04x", address, crc); 352ec558bbfSAyush Singh return; 353ec558bbfSAyush Singh } 354ec558bbfSAyush Singh 355ec558bbfSAyush Singh ctrl = bg->rx_buffer[1]; 356ec558bbfSAyush Singh buf = &bg->rx_buffer[2]; 357ec558bbfSAyush Singh len = bg->rx_buffer_len - 4; 358ec558bbfSAyush Singh 359ec558bbfSAyush Singh /* I-Frame, send S-Frame ACK */ 360ec558bbfSAyush Singh if ((ctrl & 1) == 0) 361ec558bbfSAyush Singh hdlc_tx_s_frame_ack(bg); 362ec558bbfSAyush Singh 363ec558bbfSAyush Singh switch (address) { 364ec558bbfSAyush Singh case ADDRESS_DBG: 365ec558bbfSAyush Singh hdlc_rx_dbg_frame(bg, buf, len); 366ec558bbfSAyush Singh break; 367ec558bbfSAyush Singh case ADDRESS_GREYBUS: 368ec558bbfSAyush Singh hdlc_rx_greybus_frame(bg, buf, len); 369ec558bbfSAyush Singh break; 370ec558bbfSAyush Singh default: 371ec558bbfSAyush Singh dev_warn_ratelimited(&bg->sd->dev, "unknown frame %u", address); 372ec558bbfSAyush Singh } 373ec558bbfSAyush Singh } 374ec558bbfSAyush Singh 375fed99212SFrancesco Dolcini static size_t hdlc_rx(struct gb_beagleplay *bg, const u8 *data, size_t count) 376ec558bbfSAyush Singh { 377ec558bbfSAyush Singh size_t i; 378ec558bbfSAyush Singh u8 c; 379ec558bbfSAyush Singh 380ec558bbfSAyush Singh for (i = 0; i < count; ++i) { 381ec558bbfSAyush Singh c = data[i]; 382ec558bbfSAyush Singh 383ec558bbfSAyush Singh switch (c) { 384ec558bbfSAyush Singh case HDLC_FRAME: 385ec558bbfSAyush Singh if (bg->rx_buffer_len) 386ec558bbfSAyush Singh hdlc_rx_frame(bg); 387ec558bbfSAyush Singh 388ec558bbfSAyush Singh bg->rx_buffer_len = 0; 389ec558bbfSAyush Singh break; 390ec558bbfSAyush Singh case HDLC_ESC: 391ec558bbfSAyush Singh bg->rx_in_esc = true; 392ec558bbfSAyush Singh break; 393ec558bbfSAyush Singh default: 394ec558bbfSAyush Singh if (bg->rx_in_esc) { 395ec558bbfSAyush Singh c ^= 0x20; 396ec558bbfSAyush Singh bg->rx_in_esc = false; 397ec558bbfSAyush Singh } 398ec558bbfSAyush Singh 399ec558bbfSAyush Singh if (bg->rx_buffer_len < MAX_RX_HDLC) { 400ec558bbfSAyush Singh bg->rx_buffer[bg->rx_buffer_len] = c; 401ec558bbfSAyush Singh bg->rx_buffer_len++; 402ec558bbfSAyush Singh } else { 403ec558bbfSAyush Singh dev_err_ratelimited(&bg->sd->dev, "RX Buffer Overflow"); 404ec558bbfSAyush Singh bg->rx_buffer_len = 0; 405ec558bbfSAyush Singh } 406ec558bbfSAyush Singh } 407ec558bbfSAyush Singh } 408ec558bbfSAyush Singh 409ec558bbfSAyush Singh return count; 410ec558bbfSAyush Singh } 411ec558bbfSAyush Singh 412ec558bbfSAyush Singh static int hdlc_init(struct gb_beagleplay *bg) 413ec558bbfSAyush Singh { 414ec558bbfSAyush Singh INIT_WORK(&bg->tx_work, hdlc_transmit); 415ec558bbfSAyush Singh spin_lock_init(&bg->tx_producer_lock); 416ec558bbfSAyush Singh spin_lock_init(&bg->tx_consumer_lock); 417ec558bbfSAyush Singh bg->tx_circ_buf.head = 0; 418ec558bbfSAyush Singh bg->tx_circ_buf.tail = 0; 419ec558bbfSAyush Singh 420ec558bbfSAyush Singh bg->tx_circ_buf.buf = devm_kmalloc(&bg->sd->dev, TX_CIRC_BUF_SIZE, GFP_KERNEL); 421ec558bbfSAyush Singh if (!bg->tx_circ_buf.buf) 422ec558bbfSAyush Singh return -ENOMEM; 423ec558bbfSAyush Singh 424ec558bbfSAyush Singh bg->rx_buffer_len = 0; 425ec558bbfSAyush Singh bg->rx_in_esc = false; 426ec558bbfSAyush Singh 427ec558bbfSAyush Singh return 0; 428ec558bbfSAyush Singh } 429ec558bbfSAyush Singh 430ec558bbfSAyush Singh static void hdlc_deinit(struct gb_beagleplay *bg) 431ec558bbfSAyush Singh { 432ec558bbfSAyush Singh flush_work(&bg->tx_work); 433ec558bbfSAyush Singh } 434ec558bbfSAyush Singh 435*0cf7befaSAyush Singh /** 436*0cf7befaSAyush Singh * csum8: Calculate 8-bit checksum on data 437*0cf7befaSAyush Singh * 438*0cf7befaSAyush Singh * @data: bytes to calculate 8-bit checksum of 439*0cf7befaSAyush Singh * @size: number of bytes 440*0cf7befaSAyush Singh * @base: starting value for checksum 441*0cf7befaSAyush Singh */ 442*0cf7befaSAyush Singh static u8 csum8(const u8 *data, size_t size, u8 base) 443*0cf7befaSAyush Singh { 444*0cf7befaSAyush Singh size_t i; 445*0cf7befaSAyush Singh u8 sum = base; 446*0cf7befaSAyush Singh 447*0cf7befaSAyush Singh for (i = 0; i < size; ++i) 448*0cf7befaSAyush Singh sum += data[i]; 449*0cf7befaSAyush Singh 450*0cf7befaSAyush Singh return sum; 451*0cf7befaSAyush Singh } 452*0cf7befaSAyush Singh 453*0cf7befaSAyush Singh static void cc1352_bootloader_send_ack(struct gb_beagleplay *bg) 454*0cf7befaSAyush Singh { 455*0cf7befaSAyush Singh static const u8 ack[] = { 0x00, CC1352_BOOTLOADER_ACK }; 456*0cf7befaSAyush Singh 457*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, ack, sizeof(ack)); 458*0cf7befaSAyush Singh } 459*0cf7befaSAyush Singh 460*0cf7befaSAyush Singh static void cc1352_bootloader_send_nack(struct gb_beagleplay *bg) 461*0cf7befaSAyush Singh { 462*0cf7befaSAyush Singh static const u8 nack[] = { 0x00, CC1352_BOOTLOADER_NACK }; 463*0cf7befaSAyush Singh 464*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, nack, sizeof(nack)); 465*0cf7befaSAyush Singh } 466*0cf7befaSAyush Singh 467*0cf7befaSAyush Singh /** 468*0cf7befaSAyush Singh * cc1352_bootloader_pkt_rx: Process a CC1352 Bootloader Packet 469*0cf7befaSAyush Singh * 470*0cf7befaSAyush Singh * @bg: beagleplay greybus driver 471*0cf7befaSAyush Singh * @data: packet buffer 472*0cf7befaSAyush Singh * @count: packet buffer size 473*0cf7befaSAyush Singh * 474*0cf7befaSAyush Singh * @return: number of bytes processed 475*0cf7befaSAyush Singh * 476*0cf7befaSAyush Singh * Here are the steps to successfully receive a packet from cc1352 bootloader 477*0cf7befaSAyush Singh * according to the docs: 478*0cf7befaSAyush Singh * 1. Wait for nonzero data to be returned from the device. This is important 479*0cf7befaSAyush Singh * as the device may send zero bytes between a sent and a received data 480*0cf7befaSAyush Singh * packet. The first nonzero byte received is the size of the packet that is 481*0cf7befaSAyush Singh * being received. 482*0cf7befaSAyush Singh * 2. Read the next byte, which is the checksum for the packet. 483*0cf7befaSAyush Singh * 3. Read the data bytes from the device. During the data phase, packet size 484*0cf7befaSAyush Singh * minus 2 bytes is sent. 485*0cf7befaSAyush Singh * 4. Calculate the checksum of the data bytes and verify it matches the 486*0cf7befaSAyush Singh * checksum received in the packet. 487*0cf7befaSAyush Singh * 5. Send an acknowledge byte or a not-acknowledge byte to the device to 488*0cf7befaSAyush Singh * indicate the successful or unsuccessful reception of the packet. 489*0cf7befaSAyush Singh */ 490*0cf7befaSAyush Singh static int cc1352_bootloader_pkt_rx(struct gb_beagleplay *bg, const u8 *data, 491*0cf7befaSAyush Singh size_t count) 492*0cf7befaSAyush Singh { 493*0cf7befaSAyush Singh bool is_valid = false; 494*0cf7befaSAyush Singh 495*0cf7befaSAyush Singh switch (data[0]) { 496*0cf7befaSAyush Singh /* Skip 0x00 bytes. */ 497*0cf7befaSAyush Singh case 0x00: 498*0cf7befaSAyush Singh return 1; 499*0cf7befaSAyush Singh case CC1352_BOOTLOADER_ACK: 500*0cf7befaSAyush Singh case CC1352_BOOTLOADER_NACK: 501*0cf7befaSAyush Singh WRITE_ONCE(bg->fwl_ack, data[0]); 502*0cf7befaSAyush Singh complete(&bg->fwl_ack_com); 503*0cf7befaSAyush Singh return 1; 504*0cf7befaSAyush Singh case 3: 505*0cf7befaSAyush Singh if (count < 3) 506*0cf7befaSAyush Singh return 0; 507*0cf7befaSAyush Singh is_valid = data[1] == data[2]; 508*0cf7befaSAyush Singh WRITE_ONCE(bg->fwl_cmd_response, (u32)data[2]); 509*0cf7befaSAyush Singh break; 510*0cf7befaSAyush Singh case 6: 511*0cf7befaSAyush Singh if (count < 6) 512*0cf7befaSAyush Singh return 0; 513*0cf7befaSAyush Singh is_valid = csum8(&data[2], sizeof(__be32), 0) == data[1]; 514*0cf7befaSAyush Singh WRITE_ONCE(bg->fwl_cmd_response, get_unaligned_be32(&data[2])); 515*0cf7befaSAyush Singh break; 516*0cf7befaSAyush Singh default: 517*0cf7befaSAyush Singh return -EINVAL; 518*0cf7befaSAyush Singh } 519*0cf7befaSAyush Singh 520*0cf7befaSAyush Singh if (is_valid) { 521*0cf7befaSAyush Singh cc1352_bootloader_send_ack(bg); 522*0cf7befaSAyush Singh complete(&bg->fwl_cmd_response_com); 523*0cf7befaSAyush Singh } else { 524*0cf7befaSAyush Singh dev_warn(&bg->sd->dev, 525*0cf7befaSAyush Singh "Dropping bootloader packet with invalid checksum"); 526*0cf7befaSAyush Singh cc1352_bootloader_send_nack(bg); 527*0cf7befaSAyush Singh } 528*0cf7befaSAyush Singh 529*0cf7befaSAyush Singh return data[0]; 530*0cf7befaSAyush Singh } 531*0cf7befaSAyush Singh 532*0cf7befaSAyush Singh static size_t cc1352_bootloader_rx(struct gb_beagleplay *bg, const u8 *data, 533*0cf7befaSAyush Singh size_t count) 534*0cf7befaSAyush Singh { 535*0cf7befaSAyush Singh int ret; 536*0cf7befaSAyush Singh size_t off = 0; 537*0cf7befaSAyush Singh 538*0cf7befaSAyush Singh memcpy(bg->rx_buffer + bg->rx_buffer_len, data, count); 539*0cf7befaSAyush Singh bg->rx_buffer_len += count; 540*0cf7befaSAyush Singh 541*0cf7befaSAyush Singh do { 542*0cf7befaSAyush Singh ret = cc1352_bootloader_pkt_rx(bg, bg->rx_buffer + off, 543*0cf7befaSAyush Singh bg->rx_buffer_len - off); 544*0cf7befaSAyush Singh if (ret < 0) 545*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, ret, 546*0cf7befaSAyush Singh "Invalid Packet"); 547*0cf7befaSAyush Singh off += ret; 548*0cf7befaSAyush Singh } while (ret > 0 && off < count); 549*0cf7befaSAyush Singh 550*0cf7befaSAyush Singh bg->rx_buffer_len -= off; 551*0cf7befaSAyush Singh memmove(bg->rx_buffer, bg->rx_buffer + off, bg->rx_buffer_len); 552*0cf7befaSAyush Singh 553*0cf7befaSAyush Singh return count; 554*0cf7befaSAyush Singh } 555*0cf7befaSAyush Singh 556fed99212SFrancesco Dolcini static size_t gb_tty_receive(struct serdev_device *sd, const u8 *data, 557475fc6e2SJiri Slaby (SUSE) size_t count) 558ec558bbfSAyush Singh { 559ec558bbfSAyush Singh struct gb_beagleplay *bg = serdev_device_get_drvdata(sd); 560ec558bbfSAyush Singh 561*0cf7befaSAyush Singh if (READ_ONCE(bg->flashing_mode)) 562*0cf7befaSAyush Singh return cc1352_bootloader_rx(bg, data, count); 563*0cf7befaSAyush Singh 564ec558bbfSAyush Singh return hdlc_rx(bg, data, count); 565ec558bbfSAyush Singh } 566ec558bbfSAyush Singh 567ec558bbfSAyush Singh static void gb_tty_wakeup(struct serdev_device *serdev) 568ec558bbfSAyush Singh { 569ec558bbfSAyush Singh struct gb_beagleplay *bg = serdev_device_get_drvdata(serdev); 570ec558bbfSAyush Singh 571*0cf7befaSAyush Singh if (!READ_ONCE(bg->flashing_mode)) 572ec558bbfSAyush Singh schedule_work(&bg->tx_work); 573ec558bbfSAyush Singh } 574ec558bbfSAyush Singh 575ec558bbfSAyush Singh static struct serdev_device_ops gb_beagleplay_ops = { 576ec558bbfSAyush Singh .receive_buf = gb_tty_receive, 577ec558bbfSAyush Singh .write_wakeup = gb_tty_wakeup, 578ec558bbfSAyush Singh }; 579ec558bbfSAyush Singh 58008b34855SAyush Singh /** 58108b34855SAyush Singh * gb_message_send() - Send greybus message using HDLC over UART 58208b34855SAyush Singh * 58308b34855SAyush Singh * @hd: pointer to greybus host device 58408b34855SAyush Singh * @cport: AP cport where message originates 58508b34855SAyush Singh * @msg: greybus message to send 58608b34855SAyush Singh * @mask: gfp mask 58708b34855SAyush Singh * 58808b34855SAyush Singh * Greybus HDLC frame has the following payload: 58908b34855SAyush Singh * 1. le16 cport 59008b34855SAyush Singh * 2. gb_operation_msg_hdr msg_header 59108b34855SAyush Singh * 3. u8 *msg_payload 59208b34855SAyush Singh */ 593ec558bbfSAyush Singh static int gb_message_send(struct gb_host_device *hd, u16 cport, struct gb_message *msg, gfp_t mask) 594ec558bbfSAyush Singh { 595ec558bbfSAyush Singh struct gb_beagleplay *bg = dev_get_drvdata(&hd->dev); 59608b34855SAyush Singh struct hdlc_payload payloads[3]; 59708b34855SAyush Singh __le16 cport_id = cpu_to_le16(cport); 598ec558bbfSAyush Singh 599ec558bbfSAyush Singh dev_dbg(&hd->dev, "Sending greybus message with Operation %u, Type: %X on Cport %u", 600ec558bbfSAyush Singh msg->header->operation_id, msg->header->type, cport); 601ec558bbfSAyush Singh 60208b34855SAyush Singh if (le16_to_cpu(msg->header->size) > RX_HDLC_PAYLOAD) 603ec558bbfSAyush Singh return dev_err_probe(&hd->dev, -E2BIG, "Greybus message too big"); 604ec558bbfSAyush Singh 60508b34855SAyush Singh payloads[0].buf = &cport_id; 60608b34855SAyush Singh payloads[0].len = sizeof(cport_id); 60708b34855SAyush Singh payloads[1].buf = msg->header; 60808b34855SAyush Singh payloads[1].len = sizeof(*msg->header); 60908b34855SAyush Singh payloads[2].buf = msg->payload; 61008b34855SAyush Singh payloads[2].len = msg->payload_size; 611ec558bbfSAyush Singh 61208b34855SAyush Singh hdlc_tx_frames(bg, ADDRESS_GREYBUS, 0x03, payloads, 3); 613ec558bbfSAyush Singh greybus_message_sent(bg->gb_hd, msg, 0); 614ec558bbfSAyush Singh 615ec558bbfSAyush Singh return 0; 616ec558bbfSAyush Singh } 617ec558bbfSAyush Singh 618ec558bbfSAyush Singh static void gb_message_cancel(struct gb_message *message) 619ec558bbfSAyush Singh { 620ec558bbfSAyush Singh } 621ec558bbfSAyush Singh 622ec558bbfSAyush Singh static struct gb_hd_driver gb_hdlc_driver = { .message_send = gb_message_send, 623ec558bbfSAyush Singh .message_cancel = gb_message_cancel }; 624ec558bbfSAyush Singh 625ec558bbfSAyush Singh static void gb_beagleplay_start_svc(struct gb_beagleplay *bg) 626ec558bbfSAyush Singh { 627ec558bbfSAyush Singh const u8 command = CONTROL_SVC_START; 628ec558bbfSAyush Singh const struct hdlc_payload payload = { .len = 1, .buf = (void *)&command }; 629ec558bbfSAyush Singh 630ec558bbfSAyush Singh hdlc_tx_frames(bg, ADDRESS_CONTROL, 0x03, &payload, 1); 631ec558bbfSAyush Singh } 632ec558bbfSAyush Singh 633ec558bbfSAyush Singh static void gb_beagleplay_stop_svc(struct gb_beagleplay *bg) 634ec558bbfSAyush Singh { 635ec558bbfSAyush Singh const u8 command = CONTROL_SVC_STOP; 636ec558bbfSAyush Singh const struct hdlc_payload payload = { .len = 1, .buf = (void *)&command }; 637ec558bbfSAyush Singh 638ec558bbfSAyush Singh hdlc_tx_frames(bg, ADDRESS_CONTROL, 0x03, &payload, 1); 639ec558bbfSAyush Singh } 640ec558bbfSAyush Singh 641*0cf7befaSAyush Singh static int cc1352_bootloader_wait_for_ack(struct gb_beagleplay *bg) 642*0cf7befaSAyush Singh { 643*0cf7befaSAyush Singh int ret; 644*0cf7befaSAyush Singh 645*0cf7befaSAyush Singh ret = wait_for_completion_timeout( 646*0cf7befaSAyush Singh &bg->fwl_ack_com, msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT)); 647*0cf7befaSAyush Singh if (ret < 0) 648*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, ret, 649*0cf7befaSAyush Singh "Failed to acquire ack semaphore"); 650*0cf7befaSAyush Singh 651*0cf7befaSAyush Singh switch (READ_ONCE(bg->fwl_ack)) { 652*0cf7befaSAyush Singh case CC1352_BOOTLOADER_ACK: 653*0cf7befaSAyush Singh return 0; 654*0cf7befaSAyush Singh case CC1352_BOOTLOADER_NACK: 655*0cf7befaSAyush Singh return -EAGAIN; 656*0cf7befaSAyush Singh default: 657*0cf7befaSAyush Singh return -EINVAL; 658*0cf7befaSAyush Singh } 659*0cf7befaSAyush Singh } 660*0cf7befaSAyush Singh 661*0cf7befaSAyush Singh static int cc1352_bootloader_sync(struct gb_beagleplay *bg) 662*0cf7befaSAyush Singh { 663*0cf7befaSAyush Singh static const u8 sync_bytes[] = { 0x55, 0x55 }; 664*0cf7befaSAyush Singh 665*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, sync_bytes, sizeof(sync_bytes)); 666*0cf7befaSAyush Singh return cc1352_bootloader_wait_for_ack(bg); 667*0cf7befaSAyush Singh } 668*0cf7befaSAyush Singh 669*0cf7befaSAyush Singh static int cc1352_bootloader_get_status(struct gb_beagleplay *bg) 670*0cf7befaSAyush Singh { 671*0cf7befaSAyush Singh int ret; 672*0cf7befaSAyush Singh static const struct cc1352_bootloader_packet pkt = { 673*0cf7befaSAyush Singh .len = sizeof(pkt), 674*0cf7befaSAyush Singh .checksum = COMMAND_GET_STATUS, 675*0cf7befaSAyush Singh .cmd = COMMAND_GET_STATUS 676*0cf7befaSAyush Singh }; 677*0cf7befaSAyush Singh 678*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 679*0cf7befaSAyush Singh ret = cc1352_bootloader_wait_for_ack(bg); 680*0cf7befaSAyush Singh if (ret < 0) 681*0cf7befaSAyush Singh return ret; 682*0cf7befaSAyush Singh 683*0cf7befaSAyush Singh ret = wait_for_completion_timeout( 684*0cf7befaSAyush Singh &bg->fwl_cmd_response_com, 685*0cf7befaSAyush Singh msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT)); 686*0cf7befaSAyush Singh if (ret < 0) 687*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, ret, 688*0cf7befaSAyush Singh "Failed to acquire last status semaphore"); 689*0cf7befaSAyush Singh 690*0cf7befaSAyush Singh switch (READ_ONCE(bg->fwl_cmd_response)) { 691*0cf7befaSAyush Singh case COMMAND_RET_SUCCESS: 692*0cf7befaSAyush Singh return 0; 693*0cf7befaSAyush Singh default: 694*0cf7befaSAyush Singh return -EINVAL; 695*0cf7befaSAyush Singh } 696*0cf7befaSAyush Singh 697*0cf7befaSAyush Singh return 0; 698*0cf7befaSAyush Singh } 699*0cf7befaSAyush Singh 700*0cf7befaSAyush Singh static int cc1352_bootloader_erase(struct gb_beagleplay *bg) 701*0cf7befaSAyush Singh { 702*0cf7befaSAyush Singh int ret; 703*0cf7befaSAyush Singh static const struct cc1352_bootloader_packet pkt = { 704*0cf7befaSAyush Singh .len = sizeof(pkt), 705*0cf7befaSAyush Singh .checksum = COMMAND_BANK_ERASE, 706*0cf7befaSAyush Singh .cmd = COMMAND_BANK_ERASE 707*0cf7befaSAyush Singh }; 708*0cf7befaSAyush Singh 709*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 710*0cf7befaSAyush Singh 711*0cf7befaSAyush Singh ret = cc1352_bootloader_wait_for_ack(bg); 712*0cf7befaSAyush Singh if (ret < 0) 713*0cf7befaSAyush Singh return ret; 714*0cf7befaSAyush Singh 715*0cf7befaSAyush Singh return cc1352_bootloader_get_status(bg); 716*0cf7befaSAyush Singh } 717*0cf7befaSAyush Singh 718*0cf7befaSAyush Singh static int cc1352_bootloader_reset(struct gb_beagleplay *bg) 719*0cf7befaSAyush Singh { 720*0cf7befaSAyush Singh static const struct cc1352_bootloader_packet pkt = { 721*0cf7befaSAyush Singh .len = sizeof(pkt), 722*0cf7befaSAyush Singh .checksum = COMMAND_RESET, 723*0cf7befaSAyush Singh .cmd = COMMAND_RESET 724*0cf7befaSAyush Singh }; 725*0cf7befaSAyush Singh 726*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 727*0cf7befaSAyush Singh 728*0cf7befaSAyush Singh return cc1352_bootloader_wait_for_ack(bg); 729*0cf7befaSAyush Singh } 730*0cf7befaSAyush Singh 731*0cf7befaSAyush Singh /** 732*0cf7befaSAyush Singh * cc1352_bootloader_empty_pkt: Calculate the number of empty bytes in the current packet 733*0cf7befaSAyush Singh * 734*0cf7befaSAyush Singh * @data: packet bytes array to check 735*0cf7befaSAyush Singh * @size: number of bytes in array 736*0cf7befaSAyush Singh */ 737*0cf7befaSAyush Singh static size_t cc1352_bootloader_empty_pkt(const u8 *data, size_t size) 738*0cf7befaSAyush Singh { 739*0cf7befaSAyush Singh size_t i; 740*0cf7befaSAyush Singh 741*0cf7befaSAyush Singh for (i = 0; i < size && data[i] == 0xff; ++i) 742*0cf7befaSAyush Singh continue; 743*0cf7befaSAyush Singh 744*0cf7befaSAyush Singh return i; 745*0cf7befaSAyush Singh } 746*0cf7befaSAyush Singh 747*0cf7befaSAyush Singh static int cc1352_bootloader_crc32(struct gb_beagleplay *bg, u32 *crc32) 748*0cf7befaSAyush Singh { 749*0cf7befaSAyush Singh int ret; 750*0cf7befaSAyush Singh static const struct cc1352_bootloader_crc32_cmd_data cmd_data = { 751*0cf7befaSAyush Singh .addr = 0, .size = cpu_to_be32(704 * 1024), .read_repeat = 0 752*0cf7befaSAyush Singh }; 753*0cf7befaSAyush Singh const struct cc1352_bootloader_packet pkt = { 754*0cf7befaSAyush Singh .len = sizeof(pkt) + sizeof(cmd_data), 755*0cf7befaSAyush Singh .checksum = csum8((const void *)&cmd_data, sizeof(cmd_data), 756*0cf7befaSAyush Singh COMMAND_CRC32), 757*0cf7befaSAyush Singh .cmd = COMMAND_CRC32 758*0cf7befaSAyush Singh }; 759*0cf7befaSAyush Singh 760*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 761*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&cmd_data, 762*0cf7befaSAyush Singh sizeof(cmd_data)); 763*0cf7befaSAyush Singh 764*0cf7befaSAyush Singh ret = cc1352_bootloader_wait_for_ack(bg); 765*0cf7befaSAyush Singh if (ret < 0) 766*0cf7befaSAyush Singh return ret; 767*0cf7befaSAyush Singh 768*0cf7befaSAyush Singh ret = wait_for_completion_timeout( 769*0cf7befaSAyush Singh &bg->fwl_cmd_response_com, 770*0cf7befaSAyush Singh msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT)); 771*0cf7befaSAyush Singh if (ret < 0) 772*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, ret, 773*0cf7befaSAyush Singh "Failed to acquire last status semaphore"); 774*0cf7befaSAyush Singh 775*0cf7befaSAyush Singh *crc32 = READ_ONCE(bg->fwl_cmd_response); 776*0cf7befaSAyush Singh 777*0cf7befaSAyush Singh return 0; 778*0cf7befaSAyush Singh } 779*0cf7befaSAyush Singh 780*0cf7befaSAyush Singh static int cc1352_bootloader_download(struct gb_beagleplay *bg, u32 size, 781*0cf7befaSAyush Singh u32 addr) 782*0cf7befaSAyush Singh { 783*0cf7befaSAyush Singh int ret; 784*0cf7befaSAyush Singh const struct cc1352_bootloader_download_cmd_data cmd_data = { 785*0cf7befaSAyush Singh .addr = cpu_to_be32(addr), 786*0cf7befaSAyush Singh .size = cpu_to_be32(size), 787*0cf7befaSAyush Singh }; 788*0cf7befaSAyush Singh const struct cc1352_bootloader_packet pkt = { 789*0cf7befaSAyush Singh .len = sizeof(pkt) + sizeof(cmd_data), 790*0cf7befaSAyush Singh .checksum = csum8((const void *)&cmd_data, sizeof(cmd_data), 791*0cf7befaSAyush Singh COMMAND_DOWNLOAD), 792*0cf7befaSAyush Singh .cmd = COMMAND_DOWNLOAD 793*0cf7befaSAyush Singh }; 794*0cf7befaSAyush Singh 795*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 796*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&cmd_data, 797*0cf7befaSAyush Singh sizeof(cmd_data)); 798*0cf7befaSAyush Singh 799*0cf7befaSAyush Singh ret = cc1352_bootloader_wait_for_ack(bg); 800*0cf7befaSAyush Singh if (ret < 0) 801*0cf7befaSAyush Singh return ret; 802*0cf7befaSAyush Singh 803*0cf7befaSAyush Singh return cc1352_bootloader_get_status(bg); 804*0cf7befaSAyush Singh } 805*0cf7befaSAyush Singh 806*0cf7befaSAyush Singh static int cc1352_bootloader_send_data(struct gb_beagleplay *bg, const u8 *data, 807*0cf7befaSAyush Singh size_t size) 808*0cf7befaSAyush Singh { 809*0cf7befaSAyush Singh int ret, rem = min(size, CC1352_BOOTLOADER_PKT_MAX_SIZE); 810*0cf7befaSAyush Singh const struct cc1352_bootloader_packet pkt = { 811*0cf7befaSAyush Singh .len = sizeof(pkt) + rem, 812*0cf7befaSAyush Singh .checksum = csum8(data, rem, COMMAND_SEND_DATA), 813*0cf7befaSAyush Singh .cmd = COMMAND_SEND_DATA 814*0cf7befaSAyush Singh }; 815*0cf7befaSAyush Singh 816*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt)); 817*0cf7befaSAyush Singh serdev_device_write_buf(bg->sd, data, rem); 818*0cf7befaSAyush Singh 819*0cf7befaSAyush Singh ret = cc1352_bootloader_wait_for_ack(bg); 820*0cf7befaSAyush Singh if (ret < 0) 821*0cf7befaSAyush Singh return ret; 822*0cf7befaSAyush Singh 823*0cf7befaSAyush Singh ret = cc1352_bootloader_get_status(bg); 824*0cf7befaSAyush Singh if (ret < 0) 825*0cf7befaSAyush Singh return ret; 826*0cf7befaSAyush Singh 827*0cf7befaSAyush Singh return rem; 828*0cf7befaSAyush Singh } 829*0cf7befaSAyush Singh 830ec558bbfSAyush Singh static void gb_greybus_deinit(struct gb_beagleplay *bg) 831ec558bbfSAyush Singh { 832ec558bbfSAyush Singh gb_hd_del(bg->gb_hd); 833ec558bbfSAyush Singh gb_hd_put(bg->gb_hd); 834ec558bbfSAyush Singh } 835ec558bbfSAyush Singh 836ec558bbfSAyush Singh static int gb_greybus_init(struct gb_beagleplay *bg) 837ec558bbfSAyush Singh { 838ec558bbfSAyush Singh int ret; 839ec558bbfSAyush Singh 840ec558bbfSAyush Singh bg->gb_hd = gb_hd_create(&gb_hdlc_driver, &bg->sd->dev, TX_CIRC_BUF_SIZE, GB_MAX_CPORTS); 841ec558bbfSAyush Singh if (IS_ERR(bg->gb_hd)) { 842ec558bbfSAyush Singh dev_err(&bg->sd->dev, "Failed to create greybus host device"); 843ec558bbfSAyush Singh return PTR_ERR(bg->gb_hd); 844ec558bbfSAyush Singh } 845ec558bbfSAyush Singh 846ec558bbfSAyush Singh ret = gb_hd_add(bg->gb_hd); 847ec558bbfSAyush Singh if (ret) { 848ec558bbfSAyush Singh dev_err(&bg->sd->dev, "Failed to add greybus host device"); 849ec558bbfSAyush Singh goto free_gb_hd; 850ec558bbfSAyush Singh } 851ec558bbfSAyush Singh dev_set_drvdata(&bg->gb_hd->dev, bg); 852ec558bbfSAyush Singh 853ec558bbfSAyush Singh return 0; 854ec558bbfSAyush Singh 855ec558bbfSAyush Singh free_gb_hd: 856ec558bbfSAyush Singh gb_greybus_deinit(bg); 857ec558bbfSAyush Singh return ret; 858ec558bbfSAyush Singh } 859ec558bbfSAyush Singh 860*0cf7befaSAyush Singh static enum fw_upload_err cc1352_prepare(struct fw_upload *fw_upload, 861*0cf7befaSAyush Singh const u8 *data, u32 size) 862*0cf7befaSAyush Singh { 863*0cf7befaSAyush Singh int ret; 864*0cf7befaSAyush Singh u32 curr_crc32; 865*0cf7befaSAyush Singh struct gb_beagleplay *bg = fw_upload->dd_handle; 866*0cf7befaSAyush Singh 867*0cf7befaSAyush Singh dev_info(&bg->sd->dev, "CC1352 Start Flashing..."); 868*0cf7befaSAyush Singh 869*0cf7befaSAyush Singh if (size != CC1352_FIRMWARE_SIZE) 870*0cf7befaSAyush Singh return FW_UPLOAD_ERR_INVALID_SIZE; 871*0cf7befaSAyush Singh 872*0cf7befaSAyush Singh /* Might involve network calls */ 873*0cf7befaSAyush Singh gb_greybus_deinit(bg); 874*0cf7befaSAyush Singh msleep(5 * MSEC_PER_SEC); 875*0cf7befaSAyush Singh 876*0cf7befaSAyush Singh gb_beagleplay_stop_svc(bg); 877*0cf7befaSAyush Singh msleep(200); 878*0cf7befaSAyush Singh flush_work(&bg->tx_work); 879*0cf7befaSAyush Singh 880*0cf7befaSAyush Singh serdev_device_wait_until_sent(bg->sd, CC1352_BOOTLOADER_TIMEOUT); 881*0cf7befaSAyush Singh 882*0cf7befaSAyush Singh WRITE_ONCE(bg->flashing_mode, true); 883*0cf7befaSAyush Singh 884*0cf7befaSAyush Singh gpiod_direction_output(bg->bootloader_backdoor_gpio, 0); 885*0cf7befaSAyush Singh gpiod_direction_output(bg->rst_gpio, 0); 886*0cf7befaSAyush Singh msleep(200); 887*0cf7befaSAyush Singh 888*0cf7befaSAyush Singh gpiod_set_value(bg->rst_gpio, 1); 889*0cf7befaSAyush Singh msleep(200); 890*0cf7befaSAyush Singh 891*0cf7befaSAyush Singh gpiod_set_value(bg->bootloader_backdoor_gpio, 1); 892*0cf7befaSAyush Singh msleep(200); 893*0cf7befaSAyush Singh 894*0cf7befaSAyush Singh gpiod_direction_input(bg->bootloader_backdoor_gpio); 895*0cf7befaSAyush Singh gpiod_direction_input(bg->rst_gpio); 896*0cf7befaSAyush Singh 897*0cf7befaSAyush Singh ret = cc1352_bootloader_sync(bg); 898*0cf7befaSAyush Singh if (ret < 0) 899*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 900*0cf7befaSAyush Singh "Failed to sync"); 901*0cf7befaSAyush Singh 902*0cf7befaSAyush Singh ret = cc1352_bootloader_crc32(bg, &curr_crc32); 903*0cf7befaSAyush Singh if (ret < 0) 904*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 905*0cf7befaSAyush Singh "Failed to fetch crc32"); 906*0cf7befaSAyush Singh 907*0cf7befaSAyush Singh bg->fwl_crc32 = crc32(0xffffffff, data, size) ^ 0xffffffff; 908*0cf7befaSAyush Singh 909*0cf7befaSAyush Singh /* Check if attempting to reflash same firmware */ 910*0cf7befaSAyush Singh if (bg->fwl_crc32 == curr_crc32) { 911*0cf7befaSAyush Singh dev_warn(&bg->sd->dev, "Skipping reflashing same image"); 912*0cf7befaSAyush Singh cc1352_bootloader_reset(bg); 913*0cf7befaSAyush Singh WRITE_ONCE(bg->flashing_mode, false); 914*0cf7befaSAyush Singh msleep(200); 915*0cf7befaSAyush Singh gb_greybus_init(bg); 916*0cf7befaSAyush Singh gb_beagleplay_start_svc(bg); 917*0cf7befaSAyush Singh return FW_UPLOAD_ERR_FW_INVALID; 918*0cf7befaSAyush Singh } 919*0cf7befaSAyush Singh 920*0cf7befaSAyush Singh ret = cc1352_bootloader_erase(bg); 921*0cf7befaSAyush Singh if (ret < 0) 922*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 923*0cf7befaSAyush Singh "Failed to erase"); 924*0cf7befaSAyush Singh 925*0cf7befaSAyush Singh bg->fwl_reset_addr = true; 926*0cf7befaSAyush Singh 927*0cf7befaSAyush Singh return FW_UPLOAD_ERR_NONE; 928*0cf7befaSAyush Singh } 929*0cf7befaSAyush Singh 930*0cf7befaSAyush Singh static void cc1352_cleanup(struct fw_upload *fw_upload) 931*0cf7befaSAyush Singh { 932*0cf7befaSAyush Singh struct gb_beagleplay *bg = fw_upload->dd_handle; 933*0cf7befaSAyush Singh 934*0cf7befaSAyush Singh WRITE_ONCE(bg->flashing_mode, false); 935*0cf7befaSAyush Singh } 936*0cf7befaSAyush Singh 937*0cf7befaSAyush Singh static enum fw_upload_err cc1352_write(struct fw_upload *fw_upload, 938*0cf7befaSAyush Singh const u8 *data, u32 offset, u32 size, 939*0cf7befaSAyush Singh u32 *written) 940*0cf7befaSAyush Singh { 941*0cf7befaSAyush Singh int ret; 942*0cf7befaSAyush Singh size_t empty_bytes; 943*0cf7befaSAyush Singh struct gb_beagleplay *bg = fw_upload->dd_handle; 944*0cf7befaSAyush Singh 945*0cf7befaSAyush Singh /* Skip 0xff packets. Significant performance improvement */ 946*0cf7befaSAyush Singh empty_bytes = cc1352_bootloader_empty_pkt(data + offset, size); 947*0cf7befaSAyush Singh if (empty_bytes >= CC1352_BOOTLOADER_PKT_MAX_SIZE) { 948*0cf7befaSAyush Singh bg->fwl_reset_addr = true; 949*0cf7befaSAyush Singh *written = empty_bytes; 950*0cf7befaSAyush Singh return FW_UPLOAD_ERR_NONE; 951*0cf7befaSAyush Singh } 952*0cf7befaSAyush Singh 953*0cf7befaSAyush Singh if (bg->fwl_reset_addr) { 954*0cf7befaSAyush Singh ret = cc1352_bootloader_download(bg, size, offset); 955*0cf7befaSAyush Singh if (ret < 0) 956*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, 957*0cf7befaSAyush Singh FW_UPLOAD_ERR_HW_ERROR, 958*0cf7befaSAyush Singh "Failed to send download cmd"); 959*0cf7befaSAyush Singh 960*0cf7befaSAyush Singh bg->fwl_reset_addr = false; 961*0cf7befaSAyush Singh } 962*0cf7befaSAyush Singh 963*0cf7befaSAyush Singh ret = cc1352_bootloader_send_data(bg, data + offset, size); 964*0cf7befaSAyush Singh if (ret < 0) 965*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 966*0cf7befaSAyush Singh "Failed to flash firmware"); 967*0cf7befaSAyush Singh *written = ret; 968*0cf7befaSAyush Singh 969*0cf7befaSAyush Singh return FW_UPLOAD_ERR_NONE; 970*0cf7befaSAyush Singh } 971*0cf7befaSAyush Singh 972*0cf7befaSAyush Singh static enum fw_upload_err cc1352_poll_complete(struct fw_upload *fw_upload) 973*0cf7befaSAyush Singh { 974*0cf7befaSAyush Singh u32 curr_crc32; 975*0cf7befaSAyush Singh struct gb_beagleplay *bg = fw_upload->dd_handle; 976*0cf7befaSAyush Singh 977*0cf7befaSAyush Singh if (cc1352_bootloader_crc32(bg, &curr_crc32) < 0) 978*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 979*0cf7befaSAyush Singh "Failed to fetch crc32"); 980*0cf7befaSAyush Singh 981*0cf7befaSAyush Singh if (bg->fwl_crc32 != curr_crc32) 982*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_FW_INVALID, 983*0cf7befaSAyush Singh "Invalid CRC32"); 984*0cf7befaSAyush Singh 985*0cf7befaSAyush Singh if (cc1352_bootloader_reset(bg) < 0) 986*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR, 987*0cf7befaSAyush Singh "Failed to reset"); 988*0cf7befaSAyush Singh 989*0cf7befaSAyush Singh dev_info(&bg->sd->dev, "CC1352 Flashing Successful"); 990*0cf7befaSAyush Singh WRITE_ONCE(bg->flashing_mode, false); 991*0cf7befaSAyush Singh msleep(200); 992*0cf7befaSAyush Singh 993*0cf7befaSAyush Singh if (gb_greybus_init(bg) < 0) 994*0cf7befaSAyush Singh return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_RW_ERROR, 995*0cf7befaSAyush Singh "Failed to initialize greybus"); 996*0cf7befaSAyush Singh 997*0cf7befaSAyush Singh gb_beagleplay_start_svc(bg); 998*0cf7befaSAyush Singh 999*0cf7befaSAyush Singh return FW_UPLOAD_ERR_NONE; 1000*0cf7befaSAyush Singh } 1001*0cf7befaSAyush Singh 1002*0cf7befaSAyush Singh static void cc1352_cancel(struct fw_upload *fw_upload) 1003*0cf7befaSAyush Singh { 1004*0cf7befaSAyush Singh struct gb_beagleplay *bg = fw_upload->dd_handle; 1005*0cf7befaSAyush Singh 1006*0cf7befaSAyush Singh dev_info(&bg->sd->dev, "CC1352 Bootloader Cancel"); 1007*0cf7befaSAyush Singh 1008*0cf7befaSAyush Singh cc1352_bootloader_reset(bg); 1009*0cf7befaSAyush Singh } 1010*0cf7befaSAyush Singh 1011ec558bbfSAyush Singh static void gb_serdev_deinit(struct gb_beagleplay *bg) 1012ec558bbfSAyush Singh { 1013ec558bbfSAyush Singh serdev_device_close(bg->sd); 1014ec558bbfSAyush Singh } 1015ec558bbfSAyush Singh 1016ec558bbfSAyush Singh static int gb_serdev_init(struct gb_beagleplay *bg) 1017ec558bbfSAyush Singh { 1018ec558bbfSAyush Singh int ret; 1019ec558bbfSAyush Singh 1020ec558bbfSAyush Singh serdev_device_set_drvdata(bg->sd, bg); 1021ec558bbfSAyush Singh serdev_device_set_client_ops(bg->sd, &gb_beagleplay_ops); 1022ec558bbfSAyush Singh ret = serdev_device_open(bg->sd); 1023ec558bbfSAyush Singh if (ret) 1024ec558bbfSAyush Singh return dev_err_probe(&bg->sd->dev, ret, "Unable to open serial device"); 1025ec558bbfSAyush Singh 1026ec558bbfSAyush Singh serdev_device_set_baudrate(bg->sd, 115200); 1027ec558bbfSAyush Singh serdev_device_set_flow_control(bg->sd, false); 1028ec558bbfSAyush Singh 1029ec558bbfSAyush Singh return 0; 1030ec558bbfSAyush Singh } 1031ec558bbfSAyush Singh 1032*0cf7befaSAyush Singh static const struct fw_upload_ops cc1352_bootloader_ops = { 1033*0cf7befaSAyush Singh .prepare = cc1352_prepare, 1034*0cf7befaSAyush Singh .write = cc1352_write, 1035*0cf7befaSAyush Singh .poll_complete = cc1352_poll_complete, 1036*0cf7befaSAyush Singh .cancel = cc1352_cancel, 1037*0cf7befaSAyush Singh .cleanup = cc1352_cleanup 1038*0cf7befaSAyush Singh }; 1039*0cf7befaSAyush Singh 1040*0cf7befaSAyush Singh static int gb_fw_init(struct gb_beagleplay *bg) 1041*0cf7befaSAyush Singh { 1042*0cf7befaSAyush Singh int ret; 1043*0cf7befaSAyush Singh struct fw_upload *fwl; 1044*0cf7befaSAyush Singh struct gpio_desc *desc; 1045*0cf7befaSAyush Singh 1046*0cf7befaSAyush Singh bg->fwl = NULL; 1047*0cf7befaSAyush Singh bg->bootloader_backdoor_gpio = NULL; 1048*0cf7befaSAyush Singh bg->rst_gpio = NULL; 1049*0cf7befaSAyush Singh bg->flashing_mode = false; 1050*0cf7befaSAyush Singh bg->fwl_cmd_response = 0; 1051*0cf7befaSAyush Singh bg->fwl_ack = 0; 1052*0cf7befaSAyush Singh init_completion(&bg->fwl_ack_com); 1053*0cf7befaSAyush Singh init_completion(&bg->fwl_cmd_response_com); 1054*0cf7befaSAyush Singh 1055*0cf7befaSAyush Singh desc = devm_gpiod_get(&bg->sd->dev, "bootloader-backdoor", GPIOD_IN); 1056*0cf7befaSAyush Singh if (IS_ERR(desc)) 1057*0cf7befaSAyush Singh return PTR_ERR(desc); 1058*0cf7befaSAyush Singh bg->bootloader_backdoor_gpio = desc; 1059*0cf7befaSAyush Singh 1060*0cf7befaSAyush Singh desc = devm_gpiod_get(&bg->sd->dev, "reset", GPIOD_IN); 1061*0cf7befaSAyush Singh if (IS_ERR(desc)) { 1062*0cf7befaSAyush Singh ret = PTR_ERR(desc); 1063*0cf7befaSAyush Singh goto free_boot; 1064*0cf7befaSAyush Singh } 1065*0cf7befaSAyush Singh bg->rst_gpio = desc; 1066*0cf7befaSAyush Singh 1067*0cf7befaSAyush Singh fwl = firmware_upload_register(THIS_MODULE, &bg->sd->dev, "cc1352p7", 1068*0cf7befaSAyush Singh &cc1352_bootloader_ops, bg); 1069*0cf7befaSAyush Singh if (IS_ERR(fwl)) { 1070*0cf7befaSAyush Singh ret = PTR_ERR(fwl); 1071*0cf7befaSAyush Singh goto free_reset; 1072*0cf7befaSAyush Singh } 1073*0cf7befaSAyush Singh bg->fwl = fwl; 1074*0cf7befaSAyush Singh 1075*0cf7befaSAyush Singh return 0; 1076*0cf7befaSAyush Singh 1077*0cf7befaSAyush Singh free_reset: 1078*0cf7befaSAyush Singh devm_gpiod_put(&bg->sd->dev, bg->rst_gpio); 1079*0cf7befaSAyush Singh bg->rst_gpio = NULL; 1080*0cf7befaSAyush Singh free_boot: 1081*0cf7befaSAyush Singh devm_gpiod_put(&bg->sd->dev, bg->bootloader_backdoor_gpio); 1082*0cf7befaSAyush Singh bg->bootloader_backdoor_gpio = NULL; 1083*0cf7befaSAyush Singh return ret; 1084*0cf7befaSAyush Singh } 1085*0cf7befaSAyush Singh 1086*0cf7befaSAyush Singh static void gb_fw_deinit(struct gb_beagleplay *bg) 1087*0cf7befaSAyush Singh { 1088*0cf7befaSAyush Singh firmware_upload_unregister(bg->fwl); 1089*0cf7befaSAyush Singh } 1090*0cf7befaSAyush Singh 1091ec558bbfSAyush Singh static int gb_beagleplay_probe(struct serdev_device *serdev) 1092ec558bbfSAyush Singh { 1093ec558bbfSAyush Singh int ret = 0; 1094ec558bbfSAyush Singh struct gb_beagleplay *bg; 1095ec558bbfSAyush Singh 1096ec558bbfSAyush Singh bg = devm_kmalloc(&serdev->dev, sizeof(*bg), GFP_KERNEL); 1097ec558bbfSAyush Singh if (!bg) 1098ec558bbfSAyush Singh return -ENOMEM; 1099ec558bbfSAyush Singh 1100ec558bbfSAyush Singh bg->sd = serdev; 1101ec558bbfSAyush Singh ret = gb_serdev_init(bg); 1102ec558bbfSAyush Singh if (ret) 1103ec558bbfSAyush Singh return ret; 1104ec558bbfSAyush Singh 1105ec558bbfSAyush Singh ret = hdlc_init(bg); 1106ec558bbfSAyush Singh if (ret) 1107ec558bbfSAyush Singh goto free_serdev; 1108ec558bbfSAyush Singh 1109*0cf7befaSAyush Singh ret = gb_fw_init(bg); 1110ec558bbfSAyush Singh if (ret) 1111ec558bbfSAyush Singh goto free_hdlc; 1112ec558bbfSAyush Singh 1113*0cf7befaSAyush Singh ret = gb_greybus_init(bg); 1114*0cf7befaSAyush Singh if (ret) 1115*0cf7befaSAyush Singh goto free_fw; 1116*0cf7befaSAyush Singh 1117ec558bbfSAyush Singh gb_beagleplay_start_svc(bg); 1118ec558bbfSAyush Singh 1119ec558bbfSAyush Singh return 0; 1120ec558bbfSAyush Singh 1121*0cf7befaSAyush Singh free_fw: 1122*0cf7befaSAyush Singh gb_fw_deinit(bg); 1123ec558bbfSAyush Singh free_hdlc: 1124ec558bbfSAyush Singh hdlc_deinit(bg); 1125ec558bbfSAyush Singh free_serdev: 1126ec558bbfSAyush Singh gb_serdev_deinit(bg); 1127ec558bbfSAyush Singh return ret; 1128ec558bbfSAyush Singh } 1129ec558bbfSAyush Singh 1130ec558bbfSAyush Singh static void gb_beagleplay_remove(struct serdev_device *serdev) 1131ec558bbfSAyush Singh { 1132ec558bbfSAyush Singh struct gb_beagleplay *bg = serdev_device_get_drvdata(serdev); 1133ec558bbfSAyush Singh 1134*0cf7befaSAyush Singh gb_fw_deinit(bg); 1135ec558bbfSAyush Singh gb_greybus_deinit(bg); 1136ec558bbfSAyush Singh gb_beagleplay_stop_svc(bg); 1137ec558bbfSAyush Singh hdlc_deinit(bg); 1138ec558bbfSAyush Singh gb_serdev_deinit(bg); 1139ec558bbfSAyush Singh } 1140ec558bbfSAyush Singh 1141ec558bbfSAyush Singh static const struct of_device_id gb_beagleplay_of_match[] = { 1142ec558bbfSAyush Singh { 1143ec558bbfSAyush Singh .compatible = "ti,cc1352p7", 1144ec558bbfSAyush Singh }, 1145ec558bbfSAyush Singh {}, 1146ec558bbfSAyush Singh }; 1147ec558bbfSAyush Singh MODULE_DEVICE_TABLE(of, gb_beagleplay_of_match); 1148ec558bbfSAyush Singh 1149ec558bbfSAyush Singh static struct serdev_device_driver gb_beagleplay_driver = { 1150ec558bbfSAyush Singh .probe = gb_beagleplay_probe, 1151ec558bbfSAyush Singh .remove = gb_beagleplay_remove, 1152ec558bbfSAyush Singh .driver = { 1153ec558bbfSAyush Singh .name = "gb_beagleplay", 1154ec558bbfSAyush Singh .of_match_table = gb_beagleplay_of_match, 1155ec558bbfSAyush Singh }, 1156ec558bbfSAyush Singh }; 1157ec558bbfSAyush Singh 1158ec558bbfSAyush Singh module_serdev_device_driver(gb_beagleplay_driver); 1159ec558bbfSAyush Singh 1160ec558bbfSAyush Singh MODULE_LICENSE("GPL"); 1161ec558bbfSAyush Singh MODULE_AUTHOR("Ayush Singh <ayushdevel1325@gmail.com>"); 1162ec558bbfSAyush Singh MODULE_DESCRIPTION("A Greybus driver for BeaglePlay"); 1163