rave-sp.c 21 KB

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