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