Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 /*
0004  * Multifunction core driver for Zodiac Inflight Innovations RAVE
0005  * Supervisory Processor(SP) MCU that is connected via dedicated UART
0006  * port
0007  *
0008  * Copyright (C) 2017 Zodiac Inflight Innovations
0009  */
0010 
0011 #include <linux/atomic.h>
0012 #include <linux/crc-ccitt.h>
0013 #include <linux/delay.h>
0014 #include <linux/export.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/kernel.h>
0018 #include <linux/mfd/rave-sp.h>
0019 #include <linux/module.h>
0020 #include <linux/of.h>
0021 #include <linux/of_device.h>
0022 #include <linux/sched.h>
0023 #include <linux/serdev.h>
0024 #include <asm/unaligned.h>
0025 
0026 /*
0027  * UART protocol using following entities:
0028  *  - message to MCU => ACK response
0029  *  - event from MCU => event ACK
0030  *
0031  * Frame structure:
0032  * <STX> <DATA> <CHECKSUM> <ETX>
0033  * Where:
0034  * - STX - is start of transmission character
0035  * - ETX - end of transmission
0036  * - DATA - payload
0037  * - CHECKSUM - checksum calculated on <DATA>
0038  *
0039  * If <DATA> or <CHECKSUM> contain one of control characters, then it is
0040  * escaped using <DLE> control code. Added <DLE> does not participate in
0041  * checksum calculation.
0042  */
0043 #define RAVE_SP_STX         0x02
0044 #define RAVE_SP_ETX         0x03
0045 #define RAVE_SP_DLE         0x10
0046 
0047 #define RAVE_SP_MAX_DATA_SIZE       64
0048 #define RAVE_SP_CHECKSUM_8B2C       1
0049 #define RAVE_SP_CHECKSUM_CCITT      2
0050 #define RAVE_SP_CHECKSUM_SIZE       RAVE_SP_CHECKSUM_CCITT
0051 /*
0052  * We don't store STX, ETX and unescaped bytes, so Rx is only
0053  * DATA + CSUM
0054  */
0055 #define RAVE_SP_RX_BUFFER_SIZE              \
0056     (RAVE_SP_MAX_DATA_SIZE + RAVE_SP_CHECKSUM_SIZE)
0057 
0058 #define RAVE_SP_STX_ETX_SIZE        2
0059 /*
0060  * For Tx we have to have space for everything, STX, EXT and
0061  * potentially stuffed DATA + CSUM data + csum
0062  */
0063 #define RAVE_SP_TX_BUFFER_SIZE              \
0064     (RAVE_SP_STX_ETX_SIZE + 2 * RAVE_SP_RX_BUFFER_SIZE)
0065 
0066 /**
0067  * enum rave_sp_deframer_state - Possible state for de-framer
0068  *
0069  * @RAVE_SP_EXPECT_SOF:      Scanning input for start-of-frame marker
0070  * @RAVE_SP_EXPECT_DATA:     Got start of frame marker, collecting frame
0071  * @RAVE_SP_EXPECT_ESCAPED_DATA: Got escape character, collecting escaped byte
0072  */
0073 enum rave_sp_deframer_state {
0074     RAVE_SP_EXPECT_SOF,
0075     RAVE_SP_EXPECT_DATA,
0076     RAVE_SP_EXPECT_ESCAPED_DATA,
0077 };
0078 
0079 /**
0080  * struct rave_sp_deframer - Device protocol deframer
0081  *
0082  * @state:  Current state of the deframer
0083  * @data:   Buffer used to collect deframed data
0084  * @length: Number of bytes de-framed so far
0085  */
0086 struct rave_sp_deframer {
0087     enum rave_sp_deframer_state state;
0088     unsigned char data[RAVE_SP_RX_BUFFER_SIZE];
0089     size_t length;
0090 };
0091 
0092 /**
0093  * struct rave_sp_reply - Reply as per RAVE device protocol
0094  *
0095  * @length: Expected reply length
0096  * @data:   Buffer to store reply payload in
0097  * @code:   Expected reply code
0098  * @ackid:  Expected reply ACK ID
0099  * @received:   Successful reply reception completion
0100  */
0101 struct rave_sp_reply {
0102     size_t length;
0103     void  *data;
0104     u8     code;
0105     u8     ackid;
0106     struct completion received;
0107 };
0108 
0109 /**
0110  * struct rave_sp_checksum - Variant specific checksum implementation details
0111  *
0112  * @length: Calculated checksum length
0113  * @subroutine: Utilized checksum algorithm implementation
0114  */
0115 struct rave_sp_checksum {
0116     size_t length;
0117     void (*subroutine)(const u8 *, size_t, u8 *);
0118 };
0119 
0120 struct rave_sp_version {
0121     u8     hardware;
0122     __le16 major;
0123     u8     minor;
0124     u8     letter[2];
0125 } __packed;
0126 
0127 struct rave_sp_status {
0128     struct rave_sp_version bootloader_version;
0129     struct rave_sp_version firmware_version;
0130     u16 rdu_eeprom_flag;
0131     u16 dds_eeprom_flag;
0132     u8  pic_flag;
0133     u8  orientation;
0134     u32 etc;
0135     s16 temp[2];
0136     u8  backlight_current[3];
0137     u8  dip_switch;
0138     u8  host_interrupt;
0139     u16 voltage_28;
0140     u8  i2c_device_status;
0141     u8  power_status;
0142     u8  general_status;
0143     u8  deprecated1;
0144     u8  power_led_status;
0145     u8  deprecated2;
0146     u8  periph_power_shutoff;
0147 } __packed;
0148 
0149 /**
0150  * struct rave_sp_variant_cmds - Variant specific command routines
0151  *
0152  * @translate:  Generic to variant specific command mapping routine
0153  * @get_status: Variant specific implementation of CMD_GET_STATUS
0154  */
0155 struct rave_sp_variant_cmds {
0156     int (*translate)(enum rave_sp_command);
0157     int (*get_status)(struct rave_sp *sp, struct rave_sp_status *);
0158 };
0159 
0160 /**
0161  * struct rave_sp_variant - RAVE supervisory processor core variant
0162  *
0163  * @checksum:   Variant specific checksum implementation
0164  * @cmd:    Variant specific command pointer table
0165  *
0166  */
0167 struct rave_sp_variant {
0168     const struct rave_sp_checksum *checksum;
0169     struct rave_sp_variant_cmds cmd;
0170 };
0171 
0172 /**
0173  * struct rave_sp - RAVE supervisory processor core
0174  *
0175  * @serdev:         Pointer to underlying serdev
0176  * @deframer:           Stored state of the protocol deframer
0177  * @ackid:          ACK ID used in last reply sent to the device
0178  * @bus_lock:           Lock to serialize access to the device
0179  * @reply_lock:         Lock protecting @reply
0180  * @reply:          Pointer to memory to store reply payload
0181  *
0182  * @variant:            Device variant specific information
0183  * @event_notifier_list:    Input event notification chain
0184  *
0185  * @part_number_firmware:   Firmware version
0186  * @part_number_bootloader: Bootloader version
0187  */
0188 struct rave_sp {
0189     struct serdev_device *serdev;
0190     struct rave_sp_deframer deframer;
0191     atomic_t ackid;
0192     struct mutex bus_lock;
0193     struct mutex reply_lock;
0194     struct rave_sp_reply *reply;
0195 
0196     const struct rave_sp_variant *variant;
0197     struct blocking_notifier_head event_notifier_list;
0198 
0199     const char *part_number_firmware;
0200     const char *part_number_bootloader;
0201 };
0202 
0203 static bool rave_sp_id_is_event(u8 code)
0204 {
0205     return (code & 0xF0) == RAVE_SP_EVNT_BASE;
0206 }
0207 
0208 static void rave_sp_unregister_event_notifier(struct device *dev, void *res)
0209 {
0210     struct rave_sp *sp = dev_get_drvdata(dev->parent);
0211     struct notifier_block *nb = *(struct notifier_block **)res;
0212     struct blocking_notifier_head *bnh = &sp->event_notifier_list;
0213 
0214     WARN_ON(blocking_notifier_chain_unregister(bnh, nb));
0215 }
0216 
0217 int devm_rave_sp_register_event_notifier(struct device *dev,
0218                      struct notifier_block *nb)
0219 {
0220     struct rave_sp *sp = dev_get_drvdata(dev->parent);
0221     struct notifier_block **rcnb;
0222     int ret;
0223 
0224     rcnb = devres_alloc(rave_sp_unregister_event_notifier,
0225                 sizeof(*rcnb), GFP_KERNEL);
0226     if (!rcnb)
0227         return -ENOMEM;
0228 
0229     ret = blocking_notifier_chain_register(&sp->event_notifier_list, nb);
0230     if (!ret) {
0231         *rcnb = nb;
0232         devres_add(dev, rcnb);
0233     } else {
0234         devres_free(rcnb);
0235     }
0236 
0237     return ret;
0238 }
0239 EXPORT_SYMBOL_GPL(devm_rave_sp_register_event_notifier);
0240 
0241 static void csum_8b2c(const u8 *buf, size_t size, u8 *crc)
0242 {
0243     *crc = *buf++;
0244     size--;
0245 
0246     while (size--)
0247         *crc += *buf++;
0248 
0249     *crc = 1 + ~(*crc);
0250 }
0251 
0252 static void csum_ccitt(const u8 *buf, size_t size, u8 *crc)
0253 {
0254     const u16 calculated = crc_ccitt_false(0xffff, buf, size);
0255 
0256     /*
0257      * While the rest of the wire protocol is little-endian,
0258      * CCITT-16 CRC in RDU2 device is sent out in big-endian order.
0259      */
0260     put_unaligned_be16(calculated, crc);
0261 }
0262 
0263 static void *stuff(unsigned char *dest, const unsigned char *src, size_t n)
0264 {
0265     while (n--) {
0266         const unsigned char byte = *src++;
0267 
0268         switch (byte) {
0269         case RAVE_SP_STX:
0270         case RAVE_SP_ETX:
0271         case RAVE_SP_DLE:
0272             *dest++ = RAVE_SP_DLE;
0273             fallthrough;
0274         default:
0275             *dest++ = byte;
0276         }
0277     }
0278 
0279     return dest;
0280 }
0281 
0282 static int rave_sp_write(struct rave_sp *sp, const u8 *data, u8 data_size)
0283 {
0284     const size_t checksum_length = sp->variant->checksum->length;
0285     unsigned char frame[RAVE_SP_TX_BUFFER_SIZE];
0286     unsigned char crc[RAVE_SP_CHECKSUM_SIZE];
0287     unsigned char *dest = frame;
0288     size_t length;
0289 
0290     if (WARN_ON(checksum_length > sizeof(crc)))
0291         return -ENOMEM;
0292 
0293     if (WARN_ON(data_size > sizeof(frame)))
0294         return -ENOMEM;
0295 
0296     sp->variant->checksum->subroutine(data, data_size, crc);
0297 
0298     *dest++ = RAVE_SP_STX;
0299     dest = stuff(dest, data, data_size);
0300     dest = stuff(dest, crc, checksum_length);
0301     *dest++ = RAVE_SP_ETX;
0302 
0303     length = dest - frame;
0304 
0305     print_hex_dump_debug("rave-sp tx: ", DUMP_PREFIX_NONE,
0306                  16, 1, frame, length, false);
0307 
0308     return serdev_device_write(sp->serdev, frame, length, HZ);
0309 }
0310 
0311 static u8 rave_sp_reply_code(u8 command)
0312 {
0313     /*
0314      * There isn't a single rule that describes command code ->
0315      * ACK code transformation, but, going through various
0316      * versions of ICDs, there appear to be three distinct groups
0317      * that can be described by simple transformation.
0318      */
0319     switch (command) {
0320     case 0xA0 ... 0xBE:
0321         /*
0322          * Commands implemented by firmware found in RDU1 and
0323          * older devices all seem to obey the following rule
0324          */
0325         return command + 0x20;
0326     case 0xE0 ... 0xEF:
0327         /*
0328          * Events emitted by all versions of the firmare use
0329          * least significant bit to get an ACK code
0330          */
0331         return command | 0x01;
0332     default:
0333         /*
0334          * Commands implemented by firmware found in RDU2 are
0335          * similar to "old" commands, but they use slightly
0336          * different offset
0337          */
0338         return command + 0x40;
0339     }
0340 }
0341 
0342 int rave_sp_exec(struct rave_sp *sp,
0343          void *__data,  size_t data_size,
0344          void *reply_data, size_t reply_data_size)
0345 {
0346     struct rave_sp_reply reply = {
0347         .data     = reply_data,
0348         .length   = reply_data_size,
0349         .received = COMPLETION_INITIALIZER_ONSTACK(reply.received),
0350     };
0351     unsigned char *data = __data;
0352     int command, ret = 0;
0353     u8 ackid;
0354 
0355     command = sp->variant->cmd.translate(data[0]);
0356     if (command < 0)
0357         return command;
0358 
0359     ackid       = atomic_inc_return(&sp->ackid);
0360     reply.ackid = ackid;
0361     reply.code  = rave_sp_reply_code((u8)command),
0362 
0363     mutex_lock(&sp->bus_lock);
0364 
0365     mutex_lock(&sp->reply_lock);
0366     sp->reply = &reply;
0367     mutex_unlock(&sp->reply_lock);
0368 
0369     data[0] = command;
0370     data[1] = ackid;
0371 
0372     rave_sp_write(sp, data, data_size);
0373 
0374     if (!wait_for_completion_timeout(&reply.received, HZ)) {
0375         dev_err(&sp->serdev->dev, "Command timeout\n");
0376         ret = -ETIMEDOUT;
0377 
0378         mutex_lock(&sp->reply_lock);
0379         sp->reply = NULL;
0380         mutex_unlock(&sp->reply_lock);
0381     }
0382 
0383     mutex_unlock(&sp->bus_lock);
0384     return ret;
0385 }
0386 EXPORT_SYMBOL_GPL(rave_sp_exec);
0387 
0388 static void rave_sp_receive_event(struct rave_sp *sp,
0389                   const unsigned char *data, size_t length)
0390 {
0391     u8 cmd[] = {
0392         [0] = rave_sp_reply_code(data[0]),
0393         [1] = data[1],
0394     };
0395 
0396     rave_sp_write(sp, cmd, sizeof(cmd));
0397 
0398     blocking_notifier_call_chain(&sp->event_notifier_list,
0399                      rave_sp_action_pack(data[0], data[2]),
0400                      NULL);
0401 }
0402 
0403 static void rave_sp_receive_reply(struct rave_sp *sp,
0404                   const unsigned char *data, size_t length)
0405 {
0406     struct device *dev = &sp->serdev->dev;
0407     struct rave_sp_reply *reply;
0408     const  size_t payload_length = length - 2;
0409 
0410     mutex_lock(&sp->reply_lock);
0411     reply = sp->reply;
0412 
0413     if (reply) {
0414         if (reply->code == data[0] && reply->ackid == data[1] &&
0415             payload_length >= reply->length) {
0416             /*
0417              * We are relying on memcpy(dst, src, 0) to be a no-op
0418              * when handling commands that have a no-payload reply
0419              */
0420             memcpy(reply->data, &data[2], reply->length);
0421             complete(&reply->received);
0422             sp->reply = NULL;
0423         } else {
0424             dev_err(dev, "Ignoring incorrect reply\n");
0425             dev_dbg(dev, "Code:   expected = 0x%08x received = 0x%08x\n",
0426                 reply->code, data[0]);
0427             dev_dbg(dev, "ACK ID: expected = 0x%08x received = 0x%08x\n",
0428                 reply->ackid, data[1]);
0429             dev_dbg(dev, "Length: expected = %zu received = %zu\n",
0430                 reply->length, payload_length);
0431         }
0432     }
0433 
0434     mutex_unlock(&sp->reply_lock);
0435 }
0436 
0437 static void rave_sp_receive_frame(struct rave_sp *sp,
0438                   const unsigned char *data,
0439                   size_t length)
0440 {
0441     const size_t checksum_length = sp->variant->checksum->length;
0442     const size_t payload_length  = length - checksum_length;
0443     const u8 *crc_reported       = &data[payload_length];
0444     struct device *dev           = &sp->serdev->dev;
0445     u8 crc_calculated[RAVE_SP_CHECKSUM_SIZE];
0446 
0447     if (unlikely(checksum_length > sizeof(crc_calculated))) {
0448         dev_warn(dev, "Checksum too long, dropping\n");
0449         return;
0450     }
0451 
0452     print_hex_dump_debug("rave-sp rx: ", DUMP_PREFIX_NONE,
0453                  16, 1, data, length, false);
0454 
0455     if (unlikely(length <= checksum_length)) {
0456         dev_warn(dev, "Dropping short frame\n");
0457         return;
0458     }
0459 
0460     sp->variant->checksum->subroutine(data, payload_length,
0461                       crc_calculated);
0462 
0463     if (memcmp(crc_calculated, crc_reported, checksum_length)) {
0464         dev_warn(dev, "Dropping bad frame\n");
0465         return;
0466     }
0467 
0468     if (rave_sp_id_is_event(data[0]))
0469         rave_sp_receive_event(sp, data, length);
0470     else
0471         rave_sp_receive_reply(sp, data, length);
0472 }
0473 
0474 static int rave_sp_receive_buf(struct serdev_device *serdev,
0475                    const unsigned char *buf, size_t size)
0476 {
0477     struct device *dev = &serdev->dev;
0478     struct rave_sp *sp = dev_get_drvdata(dev);
0479     struct rave_sp_deframer *deframer = &sp->deframer;
0480     const unsigned char *src = buf;
0481     const unsigned char *end = buf + size;
0482 
0483     while (src < end) {
0484         const unsigned char byte = *src++;
0485 
0486         switch (deframer->state) {
0487         case RAVE_SP_EXPECT_SOF:
0488             if (byte == RAVE_SP_STX)
0489                 deframer->state = RAVE_SP_EXPECT_DATA;
0490             break;
0491 
0492         case RAVE_SP_EXPECT_DATA:
0493             /*
0494              * Treat special byte values first
0495              */
0496             switch (byte) {
0497             case RAVE_SP_ETX:
0498                 rave_sp_receive_frame(sp,
0499                               deframer->data,
0500                               deframer->length);
0501                 /*
0502                  * Once we extracted a complete frame
0503                  * out of a stream, we call it done
0504                  * and proceed to bailing out while
0505                  * resetting the framer to initial
0506                  * state, regardless if we've consumed
0507                  * all of the stream or not.
0508                  */
0509                 goto reset_framer;
0510             case RAVE_SP_STX:
0511                 dev_warn(dev, "Bad frame: STX before ETX\n");
0512                 /*
0513                  * If we encounter second "start of
0514                  * the frame" marker before seeing
0515                  * corresponding "end of frame", we
0516                  * reset the framer and ignore both:
0517                  * frame started by first SOF and
0518                  * frame started by current SOF.
0519                  *
0520                  * NOTE: The above means that only the
0521                  * frame started by third SOF, sent
0522                  * after this one will have a chance
0523                  * to get throught.
0524                  */
0525                 goto reset_framer;
0526             case RAVE_SP_DLE:
0527                 deframer->state = RAVE_SP_EXPECT_ESCAPED_DATA;
0528                 /*
0529                  * If we encounter escape sequence we
0530                  * need to skip it and collect the
0531                  * byte that follows. We do it by
0532                  * forcing the next iteration of the
0533                  * encompassing while loop.
0534                  */
0535                 continue;
0536             }
0537             /*
0538              * For the rest of the bytes, that are not
0539              * speical snoflakes, we do the same thing
0540              * that we do to escaped data - collect it in
0541              * deframer buffer
0542              */
0543 
0544             fallthrough;
0545 
0546         case RAVE_SP_EXPECT_ESCAPED_DATA:
0547             if (deframer->length == sizeof(deframer->data)) {
0548                 dev_warn(dev, "Bad frame: Too long\n");
0549                 /*
0550                  * If the amount of data we've
0551                  * accumulated for current frame so
0552                  * far starts to exceed the capacity
0553                  * of deframer's buffer, there's
0554                  * nothing else we can do but to
0555                  * discard that data and start
0556                  * assemblying a new frame again
0557                  */
0558                 goto reset_framer;
0559             }
0560 
0561             deframer->data[deframer->length++] = byte;
0562 
0563             /*
0564              * We've extracted out special byte, now we
0565              * can go back to regular data collecting
0566              */
0567             deframer->state = RAVE_SP_EXPECT_DATA;
0568             break;
0569         }
0570     }
0571 
0572     /*
0573      * The only way to get out of the above loop and end up here
0574      * is throught consuming all of the supplied data, so here we
0575      * report that we processed it all.
0576      */
0577     return size;
0578 
0579 reset_framer:
0580     /*
0581      * NOTE: A number of codepaths that will drop us here will do
0582      * so before consuming all 'size' bytes of the data passed by
0583      * serdev layer. We rely on the fact that serdev layer will
0584      * re-execute this handler with the remainder of the Rx bytes
0585      * once we report actual number of bytes that we processed.
0586      */
0587     deframer->state  = RAVE_SP_EXPECT_SOF;
0588     deframer->length = 0;
0589 
0590     return src - buf;
0591 }
0592 
0593 static int rave_sp_rdu1_cmd_translate(enum rave_sp_command command)
0594 {
0595     if (command >= RAVE_SP_CMD_STATUS &&
0596         command <= RAVE_SP_CMD_CONTROL_EVENTS)
0597         return command;
0598 
0599     return -EINVAL;
0600 }
0601 
0602 static int rave_sp_rdu2_cmd_translate(enum rave_sp_command command)
0603 {
0604     if (command >= RAVE_SP_CMD_GET_FIRMWARE_VERSION &&
0605         command <= RAVE_SP_CMD_GET_GPIO_STATE)
0606         return command;
0607 
0608     if (command == RAVE_SP_CMD_REQ_COPPER_REV) {
0609         /*
0610          * As per RDU2 ICD 3.4.47 CMD_GET_COPPER_REV code is
0611          * different from that for RDU1 and it is set to 0x28.
0612          */
0613         return 0x28;
0614     }
0615 
0616     return rave_sp_rdu1_cmd_translate(command);
0617 }
0618 
0619 static int rave_sp_default_cmd_translate(enum rave_sp_command command)
0620 {
0621     /*
0622      * All of the following command codes were taken from "Table :
0623      * Communications Protocol Message Types" in section 3.3
0624      * "MESSAGE TYPES" of Rave PIC24 ICD.
0625      */
0626     switch (command) {
0627     case RAVE_SP_CMD_GET_FIRMWARE_VERSION:
0628         return 0x11;
0629     case RAVE_SP_CMD_GET_BOOTLOADER_VERSION:
0630         return 0x12;
0631     case RAVE_SP_CMD_BOOT_SOURCE:
0632         return 0x14;
0633     case RAVE_SP_CMD_SW_WDT:
0634         return 0x1C;
0635     case RAVE_SP_CMD_PET_WDT:
0636         return 0x1D;
0637     case RAVE_SP_CMD_RESET:
0638         return 0x1E;
0639     case RAVE_SP_CMD_RESET_REASON:
0640         return 0x1F;
0641     case RAVE_SP_CMD_RMB_EEPROM:
0642         return 0x20;
0643     default:
0644         return -EINVAL;
0645     }
0646 }
0647 
0648 static const char *devm_rave_sp_version(struct device *dev,
0649                     struct rave_sp_version *version)
0650 {
0651     /*
0652      * NOTE: The format string below uses %02d to display u16
0653      * intentionally for the sake of backwards compatibility with
0654      * legacy software.
0655      */
0656     return devm_kasprintf(dev, GFP_KERNEL, "%02d%02d%02d.%c%c\n",
0657                   version->hardware,
0658                   le16_to_cpu(version->major),
0659                   version->minor,
0660                   version->letter[0],
0661                   version->letter[1]);
0662 }
0663 
0664 static int rave_sp_rdu1_get_status(struct rave_sp *sp,
0665                    struct rave_sp_status *status)
0666 {
0667     u8 cmd[] = {
0668         [0] = RAVE_SP_CMD_STATUS,
0669         [1] = 0
0670     };
0671 
0672     return rave_sp_exec(sp, cmd, sizeof(cmd), status, sizeof(*status));
0673 }
0674 
0675 static int rave_sp_emulated_get_status(struct rave_sp *sp,
0676                        struct rave_sp_status *status)
0677 {
0678     u8 cmd[] = {
0679         [0] = RAVE_SP_CMD_GET_FIRMWARE_VERSION,
0680         [1] = 0,
0681     };
0682     int ret;
0683 
0684     ret = rave_sp_exec(sp, cmd, sizeof(cmd), &status->firmware_version,
0685                sizeof(status->firmware_version));
0686     if (ret)
0687         return ret;
0688 
0689     cmd[0] = RAVE_SP_CMD_GET_BOOTLOADER_VERSION;
0690     return rave_sp_exec(sp, cmd, sizeof(cmd), &status->bootloader_version,
0691                 sizeof(status->bootloader_version));
0692 }
0693 
0694 static int rave_sp_get_status(struct rave_sp *sp)
0695 {
0696     struct device *dev = &sp->serdev->dev;
0697     struct rave_sp_status status;
0698     const char *version;
0699     int ret;
0700 
0701     ret = sp->variant->cmd.get_status(sp, &status);
0702     if (ret)
0703         return ret;
0704 
0705     version = devm_rave_sp_version(dev, &status.firmware_version);
0706     if (!version)
0707         return -ENOMEM;
0708 
0709     sp->part_number_firmware = version;
0710 
0711     version = devm_rave_sp_version(dev, &status.bootloader_version);
0712     if (!version)
0713         return -ENOMEM;
0714 
0715     sp->part_number_bootloader = version;
0716 
0717     return 0;
0718 }
0719 
0720 static const struct rave_sp_checksum rave_sp_checksum_8b2c = {
0721     .length     = 1,
0722     .subroutine = csum_8b2c,
0723 };
0724 
0725 static const struct rave_sp_checksum rave_sp_checksum_ccitt = {
0726     .length     = 2,
0727     .subroutine = csum_ccitt,
0728 };
0729 
0730 static const struct rave_sp_variant rave_sp_legacy = {
0731     .checksum = &rave_sp_checksum_ccitt,
0732     .cmd = {
0733         .translate = rave_sp_default_cmd_translate,
0734         .get_status = rave_sp_emulated_get_status,
0735     },
0736 };
0737 
0738 static const struct rave_sp_variant rave_sp_rdu1 = {
0739     .checksum = &rave_sp_checksum_8b2c,
0740     .cmd = {
0741         .translate = rave_sp_rdu1_cmd_translate,
0742         .get_status = rave_sp_rdu1_get_status,
0743     },
0744 };
0745 
0746 static const struct rave_sp_variant rave_sp_rdu2 = {
0747     .checksum = &rave_sp_checksum_ccitt,
0748     .cmd = {
0749         .translate = rave_sp_rdu2_cmd_translate,
0750         .get_status = rave_sp_emulated_get_status,
0751     },
0752 };
0753 
0754 static const struct of_device_id rave_sp_dt_ids[] = {
0755     { .compatible = "zii,rave-sp-niu",  .data = &rave_sp_legacy },
0756     { .compatible = "zii,rave-sp-mezz", .data = &rave_sp_legacy },
0757     { .compatible = "zii,rave-sp-esb",  .data = &rave_sp_legacy },
0758     { .compatible = "zii,rave-sp-rdu1", .data = &rave_sp_rdu1   },
0759     { .compatible = "zii,rave-sp-rdu2", .data = &rave_sp_rdu2   },
0760     { /* sentinel */ }
0761 };
0762 
0763 static const struct serdev_device_ops rave_sp_serdev_device_ops = {
0764     .receive_buf  = rave_sp_receive_buf,
0765     .write_wakeup = serdev_device_write_wakeup,
0766 };
0767 
0768 static int rave_sp_probe(struct serdev_device *serdev)
0769 {
0770     struct device *dev = &serdev->dev;
0771     const char *unknown = "unknown\n";
0772     struct rave_sp *sp;
0773     u32 baud;
0774     int ret;
0775 
0776     if (of_property_read_u32(dev->of_node, "current-speed", &baud)) {
0777         dev_err(dev,
0778             "'current-speed' is not specified in device node\n");
0779         return -EINVAL;
0780     }
0781 
0782     sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
0783     if (!sp)
0784         return -ENOMEM;
0785 
0786     sp->serdev = serdev;
0787     dev_set_drvdata(dev, sp);
0788 
0789     sp->variant = of_device_get_match_data(dev);
0790     if (!sp->variant)
0791         return -ENODEV;
0792 
0793     mutex_init(&sp->bus_lock);
0794     mutex_init(&sp->reply_lock);
0795     BLOCKING_INIT_NOTIFIER_HEAD(&sp->event_notifier_list);
0796 
0797     serdev_device_set_client_ops(serdev, &rave_sp_serdev_device_ops);
0798     ret = devm_serdev_device_open(dev, serdev);
0799     if (ret)
0800         return ret;
0801 
0802     serdev_device_set_baudrate(serdev, baud);
0803     serdev_device_set_flow_control(serdev, false);
0804 
0805     ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
0806     if (ret) {
0807         dev_err(dev, "Failed to set parity\n");
0808         return ret;
0809     }
0810 
0811     ret = rave_sp_get_status(sp);
0812     if (ret) {
0813         dev_warn(dev, "Failed to get firmware status: %d\n", ret);
0814         sp->part_number_firmware   = unknown;
0815         sp->part_number_bootloader = unknown;
0816     }
0817 
0818     /*
0819      * Those strings already have a \n embedded, so there's no
0820      * need to have one in format string.
0821      */
0822     dev_info(dev, "Firmware version: %s",   sp->part_number_firmware);
0823     dev_info(dev, "Bootloader version: %s", sp->part_number_bootloader);
0824 
0825     return devm_of_platform_populate(dev);
0826 }
0827 
0828 MODULE_DEVICE_TABLE(of, rave_sp_dt_ids);
0829 
0830 static struct serdev_device_driver rave_sp_drv = {
0831     .probe          = rave_sp_probe,
0832     .driver = {
0833         .name       = "rave-sp",
0834         .of_match_table = rave_sp_dt_ids,
0835     },
0836 };
0837 module_serdev_device_driver(rave_sp_drv);
0838 
0839 MODULE_LICENSE("GPL");
0840 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
0841 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
0842 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
0843 MODULE_DESCRIPTION("RAVE SP core driver");