Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 |
Kyle Spiers | 7169483 | 2018-04-27 15:30:23 -0700 | [diff] [blame] | 48 | #define RAVE_SP_CHECKSUM_8B2C 1 |
| 49 | #define RAVE_SP_CHECKSUM_CCITT 2 |
| 50 | #define RAVE_SP_CHECKSUM_SIZE RAVE_SP_CHECKSUM_CCITT |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 | |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 | * |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 155 | * @part_number_firmware: Firmware version |
| 156 | * @part_number_bootloader: Bootloader version |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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; |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 168 | |
| 169 | const char *part_number_firmware; |
| 170 | const char *part_number_bootloader; |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 171 | }; |
| 172 | |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 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 | |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 | |
Andrey Smirnov | 44564bc | 2018-03-08 09:37:55 -0800 | [diff] [blame] | 304 | print_hex_dump_debug("rave-sp tx: ", DUMP_PREFIX_NONE, |
| 305 | 16, 1, frame, length, false); |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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; |
Kyle Spiers | 7169483 | 2018-04-27 15:30:23 -0700 | [diff] [blame] | 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 | } |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 450 | |
Andrey Smirnov | 44564bc | 2018-03-08 09:37:55 -0800 | [diff] [blame] | 451 | print_hex_dump_debug("rave-sp rx: ", DUMP_PREFIX_NONE, |
| 452 | 16, 1, data, length, false); |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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: |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 | |
Andrey Smirnov | 5112cab | 2018-03-08 09:37:56 -0800 | [diff] [blame] | 560 | deframer->data[deframer->length++] = byte; |
| 561 | |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 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 | default: |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | } |
| 642 | |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 643 | static const char *devm_rave_sp_version(struct device *dev, |
| 644 | struct rave_sp_version *version) |
| 645 | { |
| 646 | /* |
| 647 | * NOTE: The format string below uses %02d to display u16 |
| 648 | * intentionally for the sake of backwards compatibility with |
| 649 | * legacy software. |
| 650 | */ |
| 651 | return devm_kasprintf(dev, GFP_KERNEL, "%02d%02d%02d.%c%c\n", |
| 652 | version->hardware, |
| 653 | le16_to_cpu(version->major), |
| 654 | version->minor, |
| 655 | version->letter[0], |
| 656 | version->letter[1]); |
| 657 | } |
| 658 | |
| 659 | static int rave_sp_get_status(struct rave_sp *sp) |
| 660 | { |
| 661 | struct device *dev = &sp->serdev->dev; |
| 662 | u8 cmd[] = { |
| 663 | [0] = RAVE_SP_CMD_STATUS, |
| 664 | [1] = 0 |
| 665 | }; |
| 666 | struct rave_sp_status status; |
| 667 | const char *version; |
| 668 | int ret; |
| 669 | |
| 670 | ret = rave_sp_exec(sp, cmd, sizeof(cmd), &status, sizeof(status)); |
| 671 | if (ret) |
| 672 | return ret; |
| 673 | |
| 674 | version = devm_rave_sp_version(dev, &status.firmware_version); |
| 675 | if (!version) |
| 676 | return -ENOMEM; |
| 677 | |
| 678 | sp->part_number_firmware = version; |
| 679 | |
| 680 | version = devm_rave_sp_version(dev, &status.bootloader_version); |
| 681 | if (!version) |
| 682 | return -ENOMEM; |
| 683 | |
| 684 | sp->part_number_bootloader = version; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 689 | static const struct rave_sp_checksum rave_sp_checksum_8b2c = { |
| 690 | .length = 1, |
| 691 | .subroutine = csum_8b2c, |
| 692 | }; |
| 693 | |
| 694 | static const struct rave_sp_checksum rave_sp_checksum_ccitt = { |
| 695 | .length = 2, |
| 696 | .subroutine = csum_ccitt, |
| 697 | }; |
| 698 | |
| 699 | static const struct rave_sp_variant rave_sp_legacy = { |
Andrey Smirnov | a6e3bb0 | 2018-07-06 19:41:04 -0700 | [diff] [blame^] | 700 | .checksum = &rave_sp_checksum_ccitt, |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 701 | .cmd = { |
| 702 | .translate = rave_sp_default_cmd_translate, |
| 703 | }, |
| 704 | }; |
| 705 | |
| 706 | static const struct rave_sp_variant rave_sp_rdu1 = { |
| 707 | .checksum = &rave_sp_checksum_8b2c, |
| 708 | .cmd = { |
| 709 | .translate = rave_sp_rdu1_cmd_translate, |
| 710 | }, |
| 711 | }; |
| 712 | |
| 713 | static const struct rave_sp_variant rave_sp_rdu2 = { |
| 714 | .checksum = &rave_sp_checksum_ccitt, |
| 715 | .cmd = { |
| 716 | .translate = rave_sp_rdu2_cmd_translate, |
| 717 | }, |
| 718 | }; |
| 719 | |
| 720 | static const struct of_device_id rave_sp_dt_ids[] = { |
| 721 | { .compatible = "zii,rave-sp-niu", .data = &rave_sp_legacy }, |
| 722 | { .compatible = "zii,rave-sp-mezz", .data = &rave_sp_legacy }, |
| 723 | { .compatible = "zii,rave-sp-esb", .data = &rave_sp_legacy }, |
| 724 | { .compatible = "zii,rave-sp-rdu1", .data = &rave_sp_rdu1 }, |
| 725 | { .compatible = "zii,rave-sp-rdu2", .data = &rave_sp_rdu2 }, |
| 726 | { /* sentinel */ } |
| 727 | }; |
| 728 | |
| 729 | static const struct serdev_device_ops rave_sp_serdev_device_ops = { |
| 730 | .receive_buf = rave_sp_receive_buf, |
| 731 | .write_wakeup = serdev_device_write_wakeup, |
| 732 | }; |
| 733 | |
| 734 | static int rave_sp_probe(struct serdev_device *serdev) |
| 735 | { |
| 736 | struct device *dev = &serdev->dev; |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 737 | const char *unknown = "unknown\n"; |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 738 | struct rave_sp *sp; |
| 739 | u32 baud; |
| 740 | int ret; |
| 741 | |
| 742 | if (of_property_read_u32(dev->of_node, "current-speed", &baud)) { |
| 743 | dev_err(dev, |
| 744 | "'current-speed' is not specified in device node\n"); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | |
| 748 | sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL); |
| 749 | if (!sp) |
| 750 | return -ENOMEM; |
| 751 | |
| 752 | sp->serdev = serdev; |
| 753 | dev_set_drvdata(dev, sp); |
| 754 | |
| 755 | sp->variant = of_device_get_match_data(dev); |
| 756 | if (!sp->variant) |
| 757 | return -ENODEV; |
| 758 | |
| 759 | mutex_init(&sp->bus_lock); |
| 760 | mutex_init(&sp->reply_lock); |
| 761 | BLOCKING_INIT_NOTIFIER_HEAD(&sp->event_notifier_list); |
| 762 | |
| 763 | serdev_device_set_client_ops(serdev, &rave_sp_serdev_device_ops); |
| 764 | ret = devm_serdev_device_open(dev, serdev); |
| 765 | if (ret) |
| 766 | return ret; |
| 767 | |
| 768 | serdev_device_set_baudrate(serdev, baud); |
| 769 | |
Andrey Smirnov | 6d97b6f | 2018-03-08 09:37:54 -0800 | [diff] [blame] | 770 | ret = rave_sp_get_status(sp); |
| 771 | if (ret) { |
| 772 | dev_warn(dev, "Failed to get firmware status: %d\n", ret); |
| 773 | sp->part_number_firmware = unknown; |
| 774 | sp->part_number_bootloader = unknown; |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Those strings already have a \n embedded, so there's no |
| 779 | * need to have one in format string. |
| 780 | */ |
| 781 | dev_info(dev, "Firmware version: %s", sp->part_number_firmware); |
| 782 | dev_info(dev, "Bootloader version: %s", sp->part_number_bootloader); |
| 783 | |
Andrey Smirnov | 538ee27 | 2017-12-20 22:51:16 -0800 | [diff] [blame] | 784 | return devm_of_platform_populate(dev); |
| 785 | } |
| 786 | |
| 787 | MODULE_DEVICE_TABLE(of, rave_sp_dt_ids); |
| 788 | |
| 789 | static struct serdev_device_driver rave_sp_drv = { |
| 790 | .probe = rave_sp_probe, |
| 791 | .driver = { |
| 792 | .name = "rave-sp", |
| 793 | .of_match_table = rave_sp_dt_ids, |
| 794 | }, |
| 795 | }; |
| 796 | module_serdev_device_driver(rave_sp_drv); |
| 797 | |
| 798 | MODULE_LICENSE("GPL"); |
| 799 | MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>"); |
| 800 | MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>"); |
| 801 | MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>"); |
| 802 | MODULE_DESCRIPTION("RAVE SP core driver"); |