1 // SPDX-License-Identifier: GPL-2.0+
4 * Multifunction core driver for Zodiac Inflight Innovations RAVE
5 * Supervisory Processor(SP) MCU that is connected via dedicated UART
8 * Copyright (C) 2017 Zodiac Inflight Innovations
11 #include <linux/atomic.h>
12 #include <linux/crc-ccitt.h>
13 #include <linux/delay.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/rave-sp.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/sched.h>
23 #include <linux/serdev.h>
24 #include <asm/unaligned.h>
27 * UART protocol using following entities:
28 * - message to MCU => ACK response
29 * - event from MCU => event ACK
32 * <STX> <DATA> <CHECKSUM> <ETX>
34 * - STX - is start of transmission character
35 * - ETX - end of transmission
37 * - CHECKSUM - checksum calculated on <DATA>
39 * If <DATA> or <CHECKSUM> contain one of control characters, then it is
40 * escaped using <DLE> control code. Added <DLE> does not participate in
41 * checksum calculation.
43 #define RAVE_SP_STX 0x02
44 #define RAVE_SP_ETX 0x03
45 #define RAVE_SP_DLE 0x10
47 #define RAVE_SP_MAX_DATA_SIZE 64
48 #define RAVE_SP_CHECKSUM_8B2C 1
49 #define RAVE_SP_CHECKSUM_CCITT 2
50 #define RAVE_SP_CHECKSUM_SIZE RAVE_SP_CHECKSUM_CCITT
52 * We don't store STX, ETX and unescaped bytes, so Rx is only
55 #define RAVE_SP_RX_BUFFER_SIZE \
56 (RAVE_SP_MAX_DATA_SIZE + RAVE_SP_CHECKSUM_SIZE)
58 #define RAVE_SP_STX_ETX_SIZE 2
60 * For Tx we have to have space for everything, STX, EXT and
61 * potentially stuffed DATA + CSUM data + csum
63 #define RAVE_SP_TX_BUFFER_SIZE \
64 (RAVE_SP_STX_ETX_SIZE + 2 * RAVE_SP_RX_BUFFER_SIZE)
66 #define RAVE_SP_BOOT_SOURCE_GET 0
67 #define RAVE_SP_BOOT_SOURCE_SET 1
69 #define RAVE_SP_RDU2_BOARD_TYPE_RMB 0
70 #define RAVE_SP_RDU2_BOARD_TYPE_DEB 1
72 #define RAVE_SP_BOOT_SOURCE_SD 0
73 #define RAVE_SP_BOOT_SOURCE_EMMC 1
74 #define RAVE_SP_BOOT_SOURCE_NOR 2
77 * enum rave_sp_deframer_state - Possible state for de-framer
79 * @RAVE_SP_EXPECT_SOF: Scanning input for start-of-frame marker
80 * @RAVE_SP_EXPECT_DATA: Got start of frame marker, collecting frame
81 * @RAVE_SP_EXPECT_ESCAPED_DATA: Got escape character, collecting escaped byte
83 enum rave_sp_deframer_state
{
86 RAVE_SP_EXPECT_ESCAPED_DATA
,
90 * struct rave_sp_deframer - Device protocol deframer
92 * @state: Current state of the deframer
93 * @data: Buffer used to collect deframed data
94 * @length: Number of bytes de-framed so far
96 struct rave_sp_deframer
{
97 enum rave_sp_deframer_state state
;
98 unsigned char data
[RAVE_SP_RX_BUFFER_SIZE
];
103 * struct rave_sp_reply - Reply as per RAVE device protocol
105 * @length: Expected reply length
106 * @data: Buffer to store reply payload in
107 * @code: Expected reply code
108 * @ackid: Expected reply ACK ID
109 * @completion: Successful reply reception completion
111 struct rave_sp_reply
{
116 struct completion received
;
120 * struct rave_sp_checksum - Variant specific checksum implementation details
122 * @length: Caculated checksum length
123 * @subroutine: Utilized checksum algorithm implementation
125 struct rave_sp_checksum
{
127 void (*subroutine
)(const u8
*, size_t, u8
*);
131 * struct rave_sp_variant_cmds - Variant specific command routines
133 * @translate: Generic to variant specific command mapping routine
136 struct rave_sp_variant_cmds
{
137 int (*translate
)(enum rave_sp_command
);
141 * struct rave_sp_variant - RAVE supervisory processor core variant
143 * @checksum: Variant specific checksum implementation
144 * @cmd: Variant specific command pointer table
147 struct rave_sp_variant
{
148 const struct rave_sp_checksum
*checksum
;
149 struct rave_sp_variant_cmds cmd
;
153 * struct rave_sp - RAVE supervisory processor core
155 * @serdev: Pointer to underlying serdev
156 * @deframer: Stored state of the protocol deframer
157 * @ackid: ACK ID used in last reply sent to the device
158 * @bus_lock: Lock to serialize access to the device
159 * @reply_lock: Lock protecting @reply
160 * @reply: Pointer to memory to store reply payload
162 * @variant: Device variant specific information
163 * @event_notifier_list: Input event notification chain
165 * @part_number_firmware: Firmware version
166 * @part_number_bootloader: Bootloader version
169 struct serdev_device
*serdev
;
170 struct rave_sp_deframer deframer
;
172 struct mutex bus_lock
;
173 struct mutex reply_lock
;
174 struct rave_sp_reply
*reply
;
176 const struct rave_sp_variant
*variant
;
177 struct blocking_notifier_head event_notifier_list
;
179 const char *part_number_firmware
;
180 const char *part_number_bootloader
;
183 struct rave_sp_version
{
190 struct rave_sp_status
{
191 struct rave_sp_version bootloader_version
;
192 struct rave_sp_version firmware_version
;
199 u8 backlight_current
[3];
203 u8 i2c_device_status
;
209 u8 periph_power_shutoff
;
212 static bool rave_sp_id_is_event(u8 code
)
214 return (code
& 0xF0) == RAVE_SP_EVNT_BASE
;
217 static void rave_sp_unregister_event_notifier(struct device
*dev
, void *res
)
219 struct rave_sp
*sp
= dev_get_drvdata(dev
->parent
);
220 struct notifier_block
*nb
= *(struct notifier_block
**)res
;
221 struct blocking_notifier_head
*bnh
= &sp
->event_notifier_list
;
223 WARN_ON(blocking_notifier_chain_unregister(bnh
, nb
));
226 int devm_rave_sp_register_event_notifier(struct device
*dev
,
227 struct notifier_block
*nb
)
229 struct rave_sp
*sp
= dev_get_drvdata(dev
->parent
);
230 struct notifier_block
**rcnb
;
233 rcnb
= devres_alloc(rave_sp_unregister_event_notifier
,
234 sizeof(*rcnb
), GFP_KERNEL
);
238 ret
= blocking_notifier_chain_register(&sp
->event_notifier_list
, nb
);
241 devres_add(dev
, rcnb
);
248 EXPORT_SYMBOL_GPL(devm_rave_sp_register_event_notifier
);
250 static void csum_8b2c(const u8
*buf
, size_t size
, u8
*crc
)
261 static void csum_ccitt(const u8
*buf
, size_t size
, u8
*crc
)
263 const u16 calculated
= crc_ccitt_false(0xffff, buf
, size
);
266 * While the rest of the wire protocol is little-endian,
267 * CCITT-16 CRC in RDU2 device is sent out in big-endian order.
269 put_unaligned_be16(calculated
, crc
);
272 static void *stuff(unsigned char *dest
, const unsigned char *src
, size_t n
)
275 const unsigned char byte
= *src
++;
281 *dest
++ = RAVE_SP_DLE
;
291 static int rave_sp_write(struct rave_sp
*sp
, const u8
*data
, u8 data_size
)
293 const size_t checksum_length
= sp
->variant
->checksum
->length
;
294 unsigned char frame
[RAVE_SP_TX_BUFFER_SIZE
];
295 unsigned char crc
[RAVE_SP_CHECKSUM_SIZE
];
296 unsigned char *dest
= frame
;
299 if (WARN_ON(checksum_length
> sizeof(crc
)))
302 if (WARN_ON(data_size
> sizeof(frame
)))
305 sp
->variant
->checksum
->subroutine(data
, data_size
, crc
);
307 *dest
++ = RAVE_SP_STX
;
308 dest
= stuff(dest
, data
, data_size
);
309 dest
= stuff(dest
, crc
, checksum_length
);
310 *dest
++ = RAVE_SP_ETX
;
312 length
= dest
- frame
;
314 print_hex_dump_debug("rave-sp tx: ", DUMP_PREFIX_NONE
,
315 16, 1, frame
, length
, false);
317 return serdev_device_write(sp
->serdev
, frame
, length
, HZ
);
320 static u8
rave_sp_reply_code(u8 command
)
323 * There isn't a single rule that describes command code ->
324 * ACK code transformation, but, going through various
325 * versions of ICDs, there appear to be three distinct groups
326 * that can be described by simple transformation.
331 * Commands implemented by firmware found in RDU1 and
332 * older devices all seem to obey the following rule
334 return command
+ 0x20;
337 * Events emitted by all versions of the firmare use
338 * least significant bit to get an ACK code
340 return command
| 0x01;
343 * Commands implemented by firmware found in RDU2 are
344 * similar to "old" commands, but they use slightly
347 return command
+ 0x40;
351 int rave_sp_exec(struct rave_sp
*sp
,
352 void *__data
, size_t data_size
,
353 void *reply_data
, size_t reply_data_size
)
355 struct rave_sp_reply reply
= {
357 .length
= reply_data_size
,
358 .received
= COMPLETION_INITIALIZER_ONSTACK(reply
.received
),
360 unsigned char *data
= __data
;
361 int command
, ret
= 0;
364 command
= sp
->variant
->cmd
.translate(data
[0]);
368 ackid
= atomic_inc_return(&sp
->ackid
);
370 reply
.code
= rave_sp_reply_code((u8
)command
),
372 mutex_lock(&sp
->bus_lock
);
374 mutex_lock(&sp
->reply_lock
);
376 mutex_unlock(&sp
->reply_lock
);
381 rave_sp_write(sp
, data
, data_size
);
383 if (!wait_for_completion_timeout(&reply
.received
, HZ
)) {
384 dev_err(&sp
->serdev
->dev
, "Command timeout\n");
387 mutex_lock(&sp
->reply_lock
);
389 mutex_unlock(&sp
->reply_lock
);
392 mutex_unlock(&sp
->bus_lock
);
395 EXPORT_SYMBOL_GPL(rave_sp_exec
);
397 static void rave_sp_receive_event(struct rave_sp
*sp
,
398 const unsigned char *data
, size_t length
)
401 [0] = rave_sp_reply_code(data
[0]),
405 rave_sp_write(sp
, cmd
, sizeof(cmd
));
407 blocking_notifier_call_chain(&sp
->event_notifier_list
,
408 rave_sp_action_pack(data
[0], data
[2]),
412 static void rave_sp_receive_reply(struct rave_sp
*sp
,
413 const unsigned char *data
, size_t length
)
415 struct device
*dev
= &sp
->serdev
->dev
;
416 struct rave_sp_reply
*reply
;
417 const size_t payload_length
= length
- 2;
419 mutex_lock(&sp
->reply_lock
);
423 if (reply
->code
== data
[0] && reply
->ackid
== data
[1] &&
424 payload_length
>= reply
->length
) {
426 * We are relying on memcpy(dst, src, 0) to be a no-op
427 * when handling commands that have a no-payload reply
429 memcpy(reply
->data
, &data
[2], reply
->length
);
430 complete(&reply
->received
);
433 dev_err(dev
, "Ignoring incorrect reply\n");
434 dev_dbg(dev
, "Code: expected = 0x%08x received = 0x%08x\n",
435 reply
->code
, data
[0]);
436 dev_dbg(dev
, "ACK ID: expected = 0x%08x received = 0x%08x\n",
437 reply
->ackid
, data
[1]);
438 dev_dbg(dev
, "Length: expected = %zu received = %zu\n",
439 reply
->length
, payload_length
);
443 mutex_unlock(&sp
->reply_lock
);
446 static void rave_sp_receive_frame(struct rave_sp
*sp
,
447 const unsigned char *data
,
450 const size_t checksum_length
= sp
->variant
->checksum
->length
;
451 const size_t payload_length
= length
- checksum_length
;
452 const u8
*crc_reported
= &data
[payload_length
];
453 struct device
*dev
= &sp
->serdev
->dev
;
454 u8 crc_calculated
[RAVE_SP_CHECKSUM_SIZE
];
456 if (unlikely(checksum_length
> sizeof(crc_calculated
))) {
457 dev_warn(dev
, "Checksum too long, dropping\n");
461 print_hex_dump_debug("rave-sp rx: ", DUMP_PREFIX_NONE
,
462 16, 1, data
, length
, false);
464 if (unlikely(length
<= checksum_length
)) {
465 dev_warn(dev
, "Dropping short frame\n");
469 sp
->variant
->checksum
->subroutine(data
, payload_length
,
472 if (memcmp(crc_calculated
, crc_reported
, checksum_length
)) {
473 dev_warn(dev
, "Dropping bad frame\n");
477 if (rave_sp_id_is_event(data
[0]))
478 rave_sp_receive_event(sp
, data
, length
);
480 rave_sp_receive_reply(sp
, data
, length
);
483 static int rave_sp_receive_buf(struct serdev_device
*serdev
,
484 const unsigned char *buf
, size_t size
)
486 struct device
*dev
= &serdev
->dev
;
487 struct rave_sp
*sp
= dev_get_drvdata(dev
);
488 struct rave_sp_deframer
*deframer
= &sp
->deframer
;
489 const unsigned char *src
= buf
;
490 const unsigned char *end
= buf
+ size
;
493 const unsigned char byte
= *src
++;
495 switch (deframer
->state
) {
496 case RAVE_SP_EXPECT_SOF
:
497 if (byte
== RAVE_SP_STX
)
498 deframer
->state
= RAVE_SP_EXPECT_DATA
;
501 case RAVE_SP_EXPECT_DATA
:
503 * Treat special byte values first
507 rave_sp_receive_frame(sp
,
511 * Once we extracted a complete frame
512 * out of a stream, we call it done
513 * and proceed to bailing out while
514 * resetting the framer to initial
515 * state, regardless if we've consumed
516 * all of the stream or not.
520 dev_warn(dev
, "Bad frame: STX before ETX\n");
522 * If we encounter second "start of
523 * the frame" marker before seeing
524 * corresponding "end of frame", we
525 * reset the framer and ignore both:
526 * frame started by first SOF and
527 * frame started by current SOF.
529 * NOTE: The above means that only the
530 * frame started by third SOF, sent
531 * after this one will have a chance
536 deframer
->state
= RAVE_SP_EXPECT_ESCAPED_DATA
;
538 * If we encounter escape sequence we
539 * need to skip it and collect the
540 * byte that follows. We do it by
541 * forcing the next iteration of the
542 * encompassing while loop.
547 * For the rest of the bytes, that are not
548 * speical snoflakes, we do the same thing
549 * that we do to escaped data - collect it in
555 case RAVE_SP_EXPECT_ESCAPED_DATA
:
556 if (deframer
->length
== sizeof(deframer
->data
)) {
557 dev_warn(dev
, "Bad frame: Too long\n");
559 * If the amount of data we've
560 * accumulated for current frame so
561 * far starts to exceed the capacity
562 * of deframer's buffer, there's
563 * nothing else we can do but to
564 * discard that data and start
565 * assemblying a new frame again
570 deframer
->data
[deframer
->length
++] = byte
;
573 * We've extracted out special byte, now we
574 * can go back to regular data collecting
576 deframer
->state
= RAVE_SP_EXPECT_DATA
;
582 * The only way to get out of the above loop and end up here
583 * is throught consuming all of the supplied data, so here we
584 * report that we processed it all.
590 * NOTE: A number of codepaths that will drop us here will do
591 * so before consuming all 'size' bytes of the data passed by
592 * serdev layer. We rely on the fact that serdev layer will
593 * re-execute this handler with the remainder of the Rx bytes
594 * once we report actual number of bytes that we processed.
596 deframer
->state
= RAVE_SP_EXPECT_SOF
;
597 deframer
->length
= 0;
602 static int rave_sp_rdu1_cmd_translate(enum rave_sp_command command
)
604 if (command
>= RAVE_SP_CMD_STATUS
&&
605 command
<= RAVE_SP_CMD_CONTROL_EVENTS
)
611 static int rave_sp_rdu2_cmd_translate(enum rave_sp_command command
)
613 if (command
>= RAVE_SP_CMD_GET_FIRMWARE_VERSION
&&
614 command
<= RAVE_SP_CMD_GET_GPIO_STATE
)
617 if (command
== RAVE_SP_CMD_REQ_COPPER_REV
) {
619 * As per RDU2 ICD 3.4.47 CMD_GET_COPPER_REV code is
620 * different from that for RDU1 and it is set to 0x28.
625 return rave_sp_rdu1_cmd_translate(command
);
628 static int rave_sp_default_cmd_translate(enum rave_sp_command command
)
631 * All of the following command codes were taken from "Table :
632 * Communications Protocol Message Types" in section 3.3
633 * "MESSAGE TYPES" of Rave PIC24 ICD.
636 case RAVE_SP_CMD_GET_FIRMWARE_VERSION
:
638 case RAVE_SP_CMD_GET_BOOTLOADER_VERSION
:
640 case RAVE_SP_CMD_BOOT_SOURCE
:
642 case RAVE_SP_CMD_SW_WDT
:
644 case RAVE_SP_CMD_RESET
:
646 case RAVE_SP_CMD_RESET_REASON
:
653 static const char *devm_rave_sp_version(struct device
*dev
,
654 struct rave_sp_version
*version
)
657 * NOTE: The format string below uses %02d to display u16
658 * intentionally for the sake of backwards compatibility with
661 return devm_kasprintf(dev
, GFP_KERNEL
, "%02d%02d%02d.%c%c\n",
663 le16_to_cpu(version
->major
),
669 static int rave_sp_get_status(struct rave_sp
*sp
)
671 struct device
*dev
= &sp
->serdev
->dev
;
673 [0] = RAVE_SP_CMD_STATUS
,
676 struct rave_sp_status status
;
680 ret
= rave_sp_exec(sp
, cmd
, sizeof(cmd
), &status
, sizeof(status
));
684 version
= devm_rave_sp_version(dev
, &status
.firmware_version
);
688 sp
->part_number_firmware
= version
;
690 version
= devm_rave_sp_version(dev
, &status
.bootloader_version
);
694 sp
->part_number_bootloader
= version
;
699 static const struct rave_sp_checksum rave_sp_checksum_8b2c
= {
701 .subroutine
= csum_8b2c
,
704 static const struct rave_sp_checksum rave_sp_checksum_ccitt
= {
706 .subroutine
= csum_ccitt
,
709 static const struct rave_sp_variant rave_sp_legacy
= {
710 .checksum
= &rave_sp_checksum_8b2c
,
712 .translate
= rave_sp_default_cmd_translate
,
716 static const struct rave_sp_variant rave_sp_rdu1
= {
717 .checksum
= &rave_sp_checksum_8b2c
,
719 .translate
= rave_sp_rdu1_cmd_translate
,
723 static const struct rave_sp_variant rave_sp_rdu2
= {
724 .checksum
= &rave_sp_checksum_ccitt
,
726 .translate
= rave_sp_rdu2_cmd_translate
,
730 static const struct of_device_id rave_sp_dt_ids
[] = {
731 { .compatible
= "zii,rave-sp-niu", .data
= &rave_sp_legacy
},
732 { .compatible
= "zii,rave-sp-mezz", .data
= &rave_sp_legacy
},
733 { .compatible
= "zii,rave-sp-esb", .data
= &rave_sp_legacy
},
734 { .compatible
= "zii,rave-sp-rdu1", .data
= &rave_sp_rdu1
},
735 { .compatible
= "zii,rave-sp-rdu2", .data
= &rave_sp_rdu2
},
739 static const struct serdev_device_ops rave_sp_serdev_device_ops
= {
740 .receive_buf
= rave_sp_receive_buf
,
741 .write_wakeup
= serdev_device_write_wakeup
,
744 static int rave_sp_probe(struct serdev_device
*serdev
)
746 struct device
*dev
= &serdev
->dev
;
747 const char *unknown
= "unknown\n";
752 if (of_property_read_u32(dev
->of_node
, "current-speed", &baud
)) {
754 "'current-speed' is not specified in device node\n");
758 sp
= devm_kzalloc(dev
, sizeof(*sp
), GFP_KERNEL
);
763 dev_set_drvdata(dev
, sp
);
765 sp
->variant
= of_device_get_match_data(dev
);
769 mutex_init(&sp
->bus_lock
);
770 mutex_init(&sp
->reply_lock
);
771 BLOCKING_INIT_NOTIFIER_HEAD(&sp
->event_notifier_list
);
773 serdev_device_set_client_ops(serdev
, &rave_sp_serdev_device_ops
);
774 ret
= devm_serdev_device_open(dev
, serdev
);
778 serdev_device_set_baudrate(serdev
, baud
);
780 ret
= rave_sp_get_status(sp
);
782 dev_warn(dev
, "Failed to get firmware status: %d\n", ret
);
783 sp
->part_number_firmware
= unknown
;
784 sp
->part_number_bootloader
= unknown
;
788 * Those strings already have a \n embedded, so there's no
789 * need to have one in format string.
791 dev_info(dev
, "Firmware version: %s", sp
->part_number_firmware
);
792 dev_info(dev
, "Bootloader version: %s", sp
->part_number_bootloader
);
794 return devm_of_platform_populate(dev
);
797 MODULE_DEVICE_TABLE(of
, rave_sp_dt_ids
);
799 static struct serdev_device_driver rave_sp_drv
= {
800 .probe
= rave_sp_probe
,
803 .of_match_table
= rave_sp_dt_ids
,
806 module_serdev_device_driver(rave_sp_drv
);
808 MODULE_LICENSE("GPL");
809 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
810 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
811 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
812 MODULE_DESCRIPTION("RAVE SP core driver");