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