^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Multifunction core driver for Zodiac Inflight Innovations RAVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Supervisory Processor(SP) MCU that is connected via dedicated UART
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2017 Zodiac Inflight Innovations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crc-ccitt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/rave-sp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/serdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * UART protocol using following entities:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * - message to MCU => ACK response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * - event from MCU => event ACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Frame structure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * <STX> <DATA> <CHECKSUM> <ETX>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * - STX - is start of transmission character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * - ETX - end of transmission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * - DATA - payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * - CHECKSUM - checksum calculated on <DATA>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * If <DATA> or <CHECKSUM> contain one of control characters, then it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * escaped using <DLE> control code. Added <DLE> does not participate in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * checksum calculation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define RAVE_SP_STX 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define RAVE_SP_ETX 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define RAVE_SP_DLE 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define RAVE_SP_MAX_DATA_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define RAVE_SP_CHECKSUM_8B2C 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define RAVE_SP_CHECKSUM_CCITT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define RAVE_SP_CHECKSUM_SIZE RAVE_SP_CHECKSUM_CCITT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * We don't store STX, ETX and unescaped bytes, so Rx is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * DATA + CSUM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define RAVE_SP_RX_BUFFER_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) (RAVE_SP_MAX_DATA_SIZE + RAVE_SP_CHECKSUM_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define RAVE_SP_STX_ETX_SIZE 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * For Tx we have to have space for everything, STX, EXT and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * potentially stuffed DATA + CSUM data + csum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define RAVE_SP_TX_BUFFER_SIZE \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) (RAVE_SP_STX_ETX_SIZE + 2 * RAVE_SP_RX_BUFFER_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * enum rave_sp_deframer_state - Possible state for de-framer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @RAVE_SP_EXPECT_SOF: Scanning input for start-of-frame marker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * @RAVE_SP_EXPECT_DATA: Got start of frame marker, collecting frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * @RAVE_SP_EXPECT_ESCAPED_DATA: Got escape character, collecting escaped byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) enum rave_sp_deframer_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) RAVE_SP_EXPECT_SOF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) RAVE_SP_EXPECT_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) RAVE_SP_EXPECT_ESCAPED_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * struct rave_sp_deframer - Device protocol deframer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @state: Current state of the deframer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @data: Buffer used to collect deframed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @length: Number of bytes de-framed so far
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct rave_sp_deframer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) enum rave_sp_deframer_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned char data[RAVE_SP_RX_BUFFER_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * struct rave_sp_reply - Reply as per RAVE device protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @length: Expected reply length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @data: Buffer to store reply payload in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @code: Expected reply code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @ackid: Expected reply ACK ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @received: Successful reply reception completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct rave_sp_reply {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u8 code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u8 ackid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct completion received;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * struct rave_sp_checksum - Variant specific checksum implementation details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * @length: Calculated checksum length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * @subroutine: Utilized checksum algorithm implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct rave_sp_checksum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) void (*subroutine)(const u8 *, size_t, u8 *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct rave_sp_version {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u8 hardware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) __le16 major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u8 minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u8 letter[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct rave_sp_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct rave_sp_version bootloader_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct rave_sp_version firmware_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u16 rdu_eeprom_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u16 dds_eeprom_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u8 pic_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u8 orientation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u32 etc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) s16 temp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u8 backlight_current[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) u8 dip_switch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) u8 host_interrupt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) u16 voltage_28;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) u8 i2c_device_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u8 power_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) u8 general_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) u8 deprecated1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) u8 power_led_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u8 deprecated2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u8 periph_power_shutoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * struct rave_sp_variant_cmds - Variant specific command routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @translate: Generic to variant specific command mapping routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @get_status: Variant specific implementation of CMD_GET_STATUS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct rave_sp_variant_cmds {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int (*translate)(enum rave_sp_command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int (*get_status)(struct rave_sp *sp, struct rave_sp_status *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * struct rave_sp_variant - RAVE supervisory processor core variant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @checksum: Variant specific checksum implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @cmd: Variant specific command pointer table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct rave_sp_variant {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) const struct rave_sp_checksum *checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct rave_sp_variant_cmds cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * struct rave_sp - RAVE supervisory processor core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * @serdev: Pointer to underlying serdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @deframer: Stored state of the protocol deframer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @ackid: ACK ID used in last reply sent to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @bus_lock: Lock to serialize access to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @reply_lock: Lock protecting @reply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @reply: Pointer to memory to store reply payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @variant: Device variant specific information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @event_notifier_list: Input event notification chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @part_number_firmware: Firmware version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @part_number_bootloader: Bootloader version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct rave_sp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct serdev_device *serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct rave_sp_deframer deframer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) atomic_t ackid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct mutex bus_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct mutex reply_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct rave_sp_reply *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) const struct rave_sp_variant *variant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct blocking_notifier_head event_notifier_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) const char *part_number_firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) const char *part_number_bootloader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static bool rave_sp_id_is_event(u8 code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return (code & 0xF0) == RAVE_SP_EVNT_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void rave_sp_unregister_event_notifier(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct rave_sp *sp = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct notifier_block *nb = *(struct notifier_block **)res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct blocking_notifier_head *bnh = &sp->event_notifier_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) WARN_ON(blocking_notifier_chain_unregister(bnh, nb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int devm_rave_sp_register_event_notifier(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct rave_sp *sp = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct notifier_block **rcnb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) rcnb = devres_alloc(rave_sp_unregister_event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) sizeof(*rcnb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!rcnb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = blocking_notifier_chain_register(&sp->event_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) *rcnb = nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) devres_add(dev, rcnb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) devres_free(rcnb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) EXPORT_SYMBOL_GPL(devm_rave_sp_register_event_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static void csum_8b2c(const u8 *buf, size_t size, u8 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) *crc = *buf++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) size--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) while (size--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *crc += *buf++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *crc = 1 + ~(*crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void csum_ccitt(const u8 *buf, size_t size, u8 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) const u16 calculated = crc_ccitt_false(0xffff, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * While the rest of the wire protocol is little-endian,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * CCITT-16 CRC in RDU2 device is sent out in big-endian order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) put_unaligned_be16(calculated, crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void *stuff(unsigned char *dest, const unsigned char *src, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) while (n--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) const unsigned char byte = *src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) switch (byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case RAVE_SP_STX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case RAVE_SP_ETX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case RAVE_SP_DLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *dest++ = RAVE_SP_DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) *dest++ = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int rave_sp_write(struct rave_sp *sp, const u8 *data, u8 data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) const size_t checksum_length = sp->variant->checksum->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) unsigned char frame[RAVE_SP_TX_BUFFER_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unsigned char crc[RAVE_SP_CHECKSUM_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) unsigned char *dest = frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (WARN_ON(checksum_length > sizeof(crc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (WARN_ON(data_size > sizeof(frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) sp->variant->checksum->subroutine(data, data_size, crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *dest++ = RAVE_SP_STX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) dest = stuff(dest, data, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) dest = stuff(dest, crc, checksum_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *dest++ = RAVE_SP_ETX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) length = dest - frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) print_hex_dump_debug("rave-sp tx: ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 16, 1, frame, length, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return serdev_device_write(sp->serdev, frame, length, HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static u8 rave_sp_reply_code(u8 command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * There isn't a single rule that describes command code ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * ACK code transformation, but, going through various
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * versions of ICDs, there appear to be three distinct groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * that can be described by simple transformation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) switch (command) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) case 0xA0 ... 0xBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * Commands implemented by firmware found in RDU1 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * older devices all seem to obey the following rule
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return command + 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case 0xE0 ... 0xEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Events emitted by all versions of the firmare use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * least significant bit to get an ACK code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return command | 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Commands implemented by firmware found in RDU2 are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * similar to "old" commands, but they use slightly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * different offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return command + 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int rave_sp_exec(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) void *__data, size_t data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) void *reply_data, size_t reply_data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) struct rave_sp_reply reply = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .data = reply_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .length = reply_data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .received = COMPLETION_INITIALIZER_ONSTACK(reply.received),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned char *data = __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int command, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) u8 ackid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) command = sp->variant->cmd.translate(data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (command < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ackid = atomic_inc_return(&sp->ackid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) reply.ackid = ackid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) reply.code = rave_sp_reply_code((u8)command),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) mutex_lock(&sp->bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) mutex_lock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) sp->reply = &reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) mutex_unlock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) data[0] = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) data[1] = ackid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) rave_sp_write(sp, data, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (!wait_for_completion_timeout(&reply.received, HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) dev_err(&sp->serdev->dev, "Command timeout\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) mutex_lock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) sp->reply = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) mutex_unlock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) mutex_unlock(&sp->bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) EXPORT_SYMBOL_GPL(rave_sp_exec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void rave_sp_receive_event(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) const unsigned char *data, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) [0] = rave_sp_reply_code(data[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) [1] = data[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) rave_sp_write(sp, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) blocking_notifier_call_chain(&sp->event_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) rave_sp_action_pack(data[0], data[2]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static void rave_sp_receive_reply(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) const unsigned char *data, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct device *dev = &sp->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct rave_sp_reply *reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) const size_t payload_length = length - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) mutex_lock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) reply = sp->reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (reply) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (reply->code == data[0] && reply->ackid == data[1] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) payload_length >= reply->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * We are relying on memcpy(dst, src, 0) to be a no-op
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * when handling commands that have a no-payload reply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) memcpy(reply->data, &data[2], reply->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) complete(&reply->received);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) sp->reply = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) dev_err(dev, "Ignoring incorrect reply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_dbg(dev, "Code: expected = 0x%08x received = 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) reply->code, data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) dev_dbg(dev, "ACK ID: expected = 0x%08x received = 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) reply->ackid, data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dev_dbg(dev, "Length: expected = %zu received = %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) reply->length, payload_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) mutex_unlock(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static void rave_sp_receive_frame(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) const unsigned char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) const size_t checksum_length = sp->variant->checksum->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) const size_t payload_length = length - checksum_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) const u8 *crc_reported = &data[payload_length];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct device *dev = &sp->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) u8 crc_calculated[RAVE_SP_CHECKSUM_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (unlikely(checksum_length > sizeof(crc_calculated))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) dev_warn(dev, "Checksum too long, dropping\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) print_hex_dump_debug("rave-sp rx: ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 16, 1, data, length, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (unlikely(length <= checksum_length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dev_warn(dev, "Dropping short frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) sp->variant->checksum->subroutine(data, payload_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) crc_calculated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (memcmp(crc_calculated, crc_reported, checksum_length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dev_warn(dev, "Dropping bad frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (rave_sp_id_is_event(data[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) rave_sp_receive_event(sp, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) rave_sp_receive_reply(sp, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static int rave_sp_receive_buf(struct serdev_device *serdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) const unsigned char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct device *dev = &serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct rave_sp *sp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct rave_sp_deframer *deframer = &sp->deframer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) const unsigned char *src = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) const unsigned char *end = buf + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) while (src < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) const unsigned char byte = *src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) switch (deframer->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) case RAVE_SP_EXPECT_SOF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (byte == RAVE_SP_STX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) deframer->state = RAVE_SP_EXPECT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) case RAVE_SP_EXPECT_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * Treat special byte values first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) switch (byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) case RAVE_SP_ETX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) rave_sp_receive_frame(sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) deframer->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) deframer->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * Once we extracted a complete frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * out of a stream, we call it done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * and proceed to bailing out while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * resetting the framer to initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * state, regardless if we've consumed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * all of the stream or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) goto reset_framer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) case RAVE_SP_STX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) dev_warn(dev, "Bad frame: STX before ETX\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * If we encounter second "start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * the frame" marker before seeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * corresponding "end of frame", we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * reset the framer and ignore both:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * frame started by first SOF and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * frame started by current SOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * NOTE: The above means that only the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * frame started by third SOF, sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * after this one will have a chance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * to get throught.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) goto reset_framer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) case RAVE_SP_DLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) deframer->state = RAVE_SP_EXPECT_ESCAPED_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * If we encounter escape sequence we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * need to skip it and collect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * byte that follows. We do it by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * forcing the next iteration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * encompassing while loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * For the rest of the bytes, that are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * speical snoflakes, we do the same thing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * that we do to escaped data - collect it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * deframer buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) case RAVE_SP_EXPECT_ESCAPED_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (deframer->length == sizeof(deframer->data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) dev_warn(dev, "Bad frame: Too long\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * If the amount of data we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * accumulated for current frame so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * far starts to exceed the capacity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * of deframer's buffer, there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * nothing else we can do but to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * discard that data and start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * assemblying a new frame again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) goto reset_framer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) deframer->data[deframer->length++] = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * We've extracted out special byte, now we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * can go back to regular data collecting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) deframer->state = RAVE_SP_EXPECT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * The only way to get out of the above loop and end up here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * is throught consuming all of the supplied data, so here we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * report that we processed it all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) reset_framer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * NOTE: A number of codepaths that will drop us here will do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * so before consuming all 'size' bytes of the data passed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * serdev layer. We rely on the fact that serdev layer will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * re-execute this handler with the remainder of the Rx bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * once we report actual number of bytes that we processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) deframer->state = RAVE_SP_EXPECT_SOF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) deframer->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return src - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static int rave_sp_rdu1_cmd_translate(enum rave_sp_command command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (command >= RAVE_SP_CMD_STATUS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) command <= RAVE_SP_CMD_CONTROL_EVENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static int rave_sp_rdu2_cmd_translate(enum rave_sp_command command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (command >= RAVE_SP_CMD_GET_FIRMWARE_VERSION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) command <= RAVE_SP_CMD_GET_GPIO_STATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (command == RAVE_SP_CMD_REQ_COPPER_REV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * As per RDU2 ICD 3.4.47 CMD_GET_COPPER_REV code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * different from that for RDU1 and it is set to 0x28.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return 0x28;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return rave_sp_rdu1_cmd_translate(command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) static int rave_sp_default_cmd_translate(enum rave_sp_command command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * All of the following command codes were taken from "Table :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * Communications Protocol Message Types" in section 3.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * "MESSAGE TYPES" of Rave PIC24 ICD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) switch (command) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) case RAVE_SP_CMD_GET_FIRMWARE_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return 0x11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) case RAVE_SP_CMD_GET_BOOTLOADER_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return 0x12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) case RAVE_SP_CMD_BOOT_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return 0x14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) case RAVE_SP_CMD_SW_WDT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return 0x1C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) case RAVE_SP_CMD_PET_WDT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return 0x1D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) case RAVE_SP_CMD_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return 0x1E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) case RAVE_SP_CMD_RESET_REASON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return 0x1F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) case RAVE_SP_CMD_RMB_EEPROM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static const char *devm_rave_sp_version(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct rave_sp_version *version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * NOTE: The format string below uses %02d to display u16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * intentionally for the sake of backwards compatibility with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * legacy software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return devm_kasprintf(dev, GFP_KERNEL, "%02d%02d%02d.%c%c\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) version->hardware,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) le16_to_cpu(version->major),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) version->minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) version->letter[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) version->letter[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static int rave_sp_rdu1_get_status(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct rave_sp_status *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) [0] = RAVE_SP_CMD_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) [1] = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return rave_sp_exec(sp, cmd, sizeof(cmd), status, sizeof(*status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static int rave_sp_emulated_get_status(struct rave_sp *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct rave_sp_status *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) [0] = RAVE_SP_CMD_GET_FIRMWARE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) [1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) ret = rave_sp_exec(sp, cmd, sizeof(cmd), &status->firmware_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) sizeof(status->firmware_version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) cmd[0] = RAVE_SP_CMD_GET_BOOTLOADER_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return rave_sp_exec(sp, cmd, sizeof(cmd), &status->bootloader_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) sizeof(status->bootloader_version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int rave_sp_get_status(struct rave_sp *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct device *dev = &sp->serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct rave_sp_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) const char *version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ret = sp->variant->cmd.get_status(sp, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) version = devm_rave_sp_version(dev, &status.firmware_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) sp->part_number_firmware = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) version = devm_rave_sp_version(dev, &status.bootloader_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) sp->part_number_bootloader = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static const struct rave_sp_checksum rave_sp_checksum_8b2c = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) .length = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) .subroutine = csum_8b2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) static const struct rave_sp_checksum rave_sp_checksum_ccitt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) .length = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) .subroutine = csum_ccitt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static const struct rave_sp_variant rave_sp_legacy = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) .checksum = &rave_sp_checksum_ccitt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) .cmd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) .translate = rave_sp_default_cmd_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) .get_status = rave_sp_emulated_get_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static const struct rave_sp_variant rave_sp_rdu1 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) .checksum = &rave_sp_checksum_8b2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) .cmd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) .translate = rave_sp_rdu1_cmd_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) .get_status = rave_sp_rdu1_get_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static const struct rave_sp_variant rave_sp_rdu2 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) .checksum = &rave_sp_checksum_ccitt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) .cmd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) .translate = rave_sp_rdu2_cmd_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) .get_status = rave_sp_emulated_get_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) static const struct of_device_id rave_sp_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) { .compatible = "zii,rave-sp-niu", .data = &rave_sp_legacy },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) { .compatible = "zii,rave-sp-mezz", .data = &rave_sp_legacy },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) { .compatible = "zii,rave-sp-esb", .data = &rave_sp_legacy },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) { .compatible = "zii,rave-sp-rdu1", .data = &rave_sp_rdu1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) { .compatible = "zii,rave-sp-rdu2", .data = &rave_sp_rdu2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static const struct serdev_device_ops rave_sp_serdev_device_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) .receive_buf = rave_sp_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) .write_wakeup = serdev_device_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static int rave_sp_probe(struct serdev_device *serdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct device *dev = &serdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) const char *unknown = "unknown\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct rave_sp *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) u32 baud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (of_property_read_u32(dev->of_node, "current-speed", &baud)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) "'current-speed' is not specified in device node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (!sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) sp->serdev = serdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) dev_set_drvdata(dev, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) sp->variant = of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (!sp->variant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) mutex_init(&sp->bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) mutex_init(&sp->reply_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) BLOCKING_INIT_NOTIFIER_HEAD(&sp->event_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) serdev_device_set_client_ops(serdev, &rave_sp_serdev_device_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) ret = devm_serdev_device_open(dev, serdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) serdev_device_set_baudrate(serdev, baud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) serdev_device_set_flow_control(serdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) dev_err(dev, "Failed to set parity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) ret = rave_sp_get_status(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) dev_warn(dev, "Failed to get firmware status: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) sp->part_number_firmware = unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) sp->part_number_bootloader = unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * Those strings already have a \n embedded, so there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * need to have one in format string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) dev_info(dev, "Firmware version: %s", sp->part_number_firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) dev_info(dev, "Bootloader version: %s", sp->part_number_bootloader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return devm_of_platform_populate(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) MODULE_DEVICE_TABLE(of, rave_sp_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) static struct serdev_device_driver rave_sp_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) .probe = rave_sp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) .name = "rave-sp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) .of_match_table = rave_sp_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) module_serdev_device_driver(rave_sp_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) MODULE_DESCRIPTION("RAVE SP core driver");