Debian Codegraph @

drivers/greybus/gb-beagleplay.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Beagleplay Linux Driver for Greybus
4 *
5 * Copyright (c) 2023 Ayush Singh <[email protected]>
6 * Copyright (c) 2023 BeagleBoard.org Foundation
7 */
8
9 #include <linux/unaligned.h>
10 #include <linux/crc32.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/firmware.h>
13 #include <linux/greybus.h>
14 #include <linux/serdev.h>
15 #include <linux/crc-ccitt.h>
16 #include <linux/circ_buf.h>
17
18 #define CC1352_FIRMWARE_SIZE (704 * 1024)
19 #define CC1352_BOOTLOADER_TIMEOUT 2000
20 #define CC1352_BOOTLOADER_ACK 0xcc
21 #define CC1352_BOOTLOADER_NACK 0x33
22
23 #define RX_HDLC_PAYLOAD 256
24 #define CRC_LEN 2
25 #define MAX_RX_HDLC (1 + RX_HDLC_PAYLOAD + CRC_LEN)
26 #define TX_CIRC_BUF_SIZE 1024
27
28 #define ADDRESS_GREYBUS 0x01
29 #define ADDRESS_DBG 0x02
30 #define ADDRESS_CONTROL 0x03
31
32 #define HDLC_FRAME 0x7E
33 #define HDLC_ESC 0x7D
34 #define HDLC_XOR 0x20
35
36 #define CONTROL_SVC_START 0x01
37 #define CONTROL_SVC_STOP 0x02
38
39 /* The maximum number of CPorts supported by Greybus Host Device */
40 #define GB_MAX_CPORTS 32
41
42 /**
43 * struct gb_beagleplay - BeaglePlay Greybus driver
44 *
45 * @sd: underlying serdev device
46 *
47 * @gb_hd: greybus host device
48 *
49 * @tx_work: hdlc transmit work
50 * @tx_producer_lock: hdlc transmit data producer lock. acquired when appending data to buffer.
51 * @tx_consumer_lock: hdlc transmit data consumer lock. acquired when sending data over uart.
52 * @tx_circ_buf: hdlc transmit circular buffer.
53 * @tx_crc: hdlc transmit crc-ccitt fcs
54 *
55 * @rx_buffer_len: length of receive buffer filled.
56 * @rx_buffer: hdlc frame receive buffer
57 * @rx_in_esc: hdlc rx flag to indicate ESC frame
58 *
59 * @fwl: underlying firmware upload device
60 * @bootloader_backdoor_gpio: cc1352p7 boot gpio
61 * @rst_gpio: cc1352p7 reset gpio
62 * @flashing_mode: flag to indicate that flashing is currently in progress
63 * @fwl_ack_com: completion to signal an Ack/Nack
64 * @fwl_ack: Ack/Nack byte received
65 * @fwl_cmd_response_com: completion to signal a bootloader command response
66 * @fwl_cmd_response: bootloader command response data
67 * @fwl_crc32: crc32 of firmware to flash
68 * @fwl_reset_addr: flag to indicate if we need to send COMMAND_DOWNLOAD again
69 */
70 struct gb_beagleplay {
71 struct serdev_device *sd;
72
73 struct gb_host_device *gb_hd;
74
75 struct work_struct tx_work;
76 spinlock_t tx_producer_lock;
77 spinlock_t tx_consumer_lock;
78 struct circ_buf tx_circ_buf;
79 u16 tx_crc;
80
81 u16 rx_buffer_len;
82 bool rx_in_esc;
83 u8 rx_buffer[MAX_RX_HDLC];
84
85 struct fw_upload *fwl;
86 struct gpio_desc *bootloader_backdoor_gpio;
87 struct gpio_desc *rst_gpio;
88 bool flashing_mode;
89 struct completion fwl_ack_com;
90 u8 fwl_ack;
91 struct completion fwl_cmd_response_com;
92 u32 fwl_cmd_response;
93 u32 fwl_crc32;
94 bool fwl_reset_addr;
95 };
96
97 /**
98 * struct hdlc_payload - Structure to represent part of HDCL frame payload data.
99 *
100 * @len: buffer length in bytes
101 * @buf: payload buffer
102 */
103 struct hdlc_payload {
104 u16 len;
105 void *buf;
106 };
107
108 /**
109 * struct hdlc_greybus_frame - Structure to represent greybus HDLC frame payload
110 *
111 * @cport: cport id
112 * @hdr: greybus operation header
113 * @payload: greybus message payload
114 *
115 * The HDLC payload sent over UART for greybus address has cport preappended to greybus message
116 */
117 struct hdlc_greybus_frame {
118 __le16 cport;
119 struct gb_operation_msg_hdr hdr;
120 u8 payload[];
121 } __packed;
122
123 /**
124 * enum cc1352_bootloader_cmd: CC1352 Bootloader Commands
125 *
126 * @COMMAND_DOWNLOAD: Prepares flash programming
127 * @COMMAND_GET_STATUS: Returns the status of the last command that was issued
128 * @COMMAND_SEND_DATA: Transfers data and programs flash
129 * @COMMAND_RESET: Performs a system reset
130 * @COMMAND_CRC32: Calculates CRC32 over a specified memory area
131 * @COMMAND_BANK_ERASE: Performs an erase of all of the customer-accessible
132 * flash sectors not protected by FCFG1 and CCFG
133 * writeprotect bits.
134 *
135 * CC1352 Bootloader serial bus commands
136 */
137 enum cc1352_bootloader_cmd {
138 COMMAND_DOWNLOAD = 0x21,
139 COMMAND_GET_STATUS = 0x23,
140 COMMAND_SEND_DATA = 0x24,
141 COMMAND_RESET = 0x25,
142 COMMAND_CRC32 = 0x27,
143 COMMAND_BANK_ERASE = 0x2c,
144 };
145
146 /**
147 * enum cc1352_bootloader_status: CC1352 Bootloader COMMAND_GET_STATUS response
148 *
149 * @COMMAND_RET_SUCCESS: Status for successful command
150 * @COMMAND_RET_UNKNOWN_CMD: Status for unknown command
151 * @COMMAND_RET_INVALID_CMD: Status for invalid command (in other words,
152 * incorrect packet size)
153 * @COMMAND_RET_INVALID_ADR: Status for invalid input address
154 * @COMMAND_RET_FLASH_FAIL: Status for failing flash erase or program operation
155 */
156 enum cc1352_bootloader_status {
157 COMMAND_RET_SUCCESS = 0x40,
158 COMMAND_RET_UNKNOWN_CMD = 0x41,
159 COMMAND_RET_INVALID_CMD = 0x42,
160 COMMAND_RET_INVALID_ADR = 0x43,
161 COMMAND_RET_FLASH_FAIL = 0x44,
162 };
163
164 /**
165 * struct cc1352_bootloader_packet: CC1352 Bootloader Request Packet
166 *
167 * @len: length of packet + optional request data
168 * @checksum: 8-bit checksum excluding len
169 * @cmd: bootloader command
170 */
171 struct cc1352_bootloader_packet {
172 u8 len;
173 u8 checksum;
174 u8 cmd;
175 } __packed;
176
177 #define CC1352_BOOTLOADER_PKT_MAX_SIZE \
178 (U8_MAX - sizeof(struct cc1352_bootloader_packet))
179
180 /**
181 * struct cc1352_bootloader_download_cmd_data: CC1352 Bootloader COMMAND_DOWNLOAD request data
182 *
183 * @addr: address to start programming data into
184 * @size: size of data that will be sent
185 */
186 struct cc1352_bootloader_download_cmd_data {
187 __be32 addr;
188 __be32 size;
189 } __packed;
190
191 /**
192 * struct cc1352_bootloader_crc32_cmd_data: CC1352 Bootloader COMMAND_CRC32 request data
193 *
194 * @addr: address where crc32 calculation starts
195 * @size: number of bytes comprised by crc32 calculation
196 * @read_repeat: number of read repeats for each data location
197 */
198 struct cc1352_bootloader_crc32_cmd_data {
199 __be32 addr;
200 __be32 size;
201 __be32 read_repeat;
202 } __packed;
203
204 static void hdlc_rx_greybus_frame(struct gb_beagleplay *bg, u8 *buf, u16 len)
205 {
206 struct hdlc_greybus_frame *gb_frame = (struct hdlc_greybus_frame *)buf;
207 u16 cport_id = le16_to_cpu(gb_frame->cport);
208 u16 gb_msg_len = le16_to_cpu(gb_frame->hdr.size);
209
210 dev_dbg(&bg->sd->dev, "Greybus Operation %u type %X cport %u status %u received",
211 gb_frame->hdr.operation_id, gb_frame->hdr.type, cport_id, gb_frame->hdr.result);
212
213 greybus_data_rcvd(bg->gb_hd, cport_id, (u8 *)&gb_frame->hdr, gb_msg_len);
214 }
215
216 static void hdlc_rx_dbg_frame(const struct gb_beagleplay *bg, const char *buf, u16 len)
217 {
218 dev_dbg(&bg->sd->dev, "CC1352 Log: %.*s", (int)len, buf);
219 }
220
221 /**
222 * hdlc_write() - Consume HDLC Buffer.
223 * @bg: beagleplay greybus driver
224 *
225 * Assumes that consumer lock has been acquired.
226 */
227 static void hdlc_write(struct gb_beagleplay *bg)
228 {
229 int written;
230 /* Start consuming HDLC data */
231 int head = smp_load_acquire(&bg->tx_circ_buf.head);
232 int tail = bg->tx_circ_buf.tail;
233 int count = CIRC_CNT_TO_END(head, tail, TX_CIRC_BUF_SIZE);
234 const unsigned char *buf = &bg->tx_circ_buf.buf[tail];
235
236 if (count > 0) {
237 written = serdev_device_write_buf(bg->sd, buf, count);
238
239 /* Finish consuming HDLC data */
240 smp_store_release(&bg->tx_circ_buf.tail, (tail + written) & (TX_CIRC_BUF_SIZE - 1));
241 }
242 }
243
244 /**
245 * hdlc_append() - Queue a single HDLC byte for sending.
246 * @bg: beagleplay greybus driver
247 * @value: hdlc byte to transmit
248 *
249 * Caller must hold tx_producer_lock and must have ensured sufficient
250 * space in the circular buffer before calling (see hdlc_tx_frames()).
251 */
252 static void hdlc_append(struct gb_beagleplay *bg, u8 value)
253 {
254 int head = bg->tx_circ_buf.head;
255 int tail = READ_ONCE(bg->tx_circ_buf.tail);
256
257 lockdep_assert_held(&bg->tx_producer_lock);
258 if (WARN_ON_ONCE(CIRC_SPACE(head, tail, TX_CIRC_BUF_SIZE) < 1))
259 return;
260
261 bg->tx_circ_buf.buf[head] = value;
262 /* Ensure buffer write is visible before advancing head. */
263 smp_store_release(&bg->tx_circ_buf.head,
264 (head + 1) & (TX_CIRC_BUF_SIZE - 1));
265 }
266
267 static void hdlc_append_escaped(struct gb_beagleplay *bg, u8 value)
268 {
269 if (value == HDLC_FRAME || value == HDLC_ESC) {
270 hdlc_append(bg, HDLC_ESC);
271 value ^= HDLC_XOR;
272 }
273 hdlc_append(bg, value);
274 }
275
276 static void hdlc_append_tx_frame(struct gb_beagleplay *bg)
277 {
278 bg->tx_crc = 0xFFFF;
279 hdlc_append(bg, HDLC_FRAME);
280 }
281
282 static void hdlc_append_tx_u8(struct gb_beagleplay *bg, u8 value)
283 {
284 bg->tx_crc = crc_ccitt(bg->tx_crc, &value, 1);
285 hdlc_append_escaped(bg, value);
286 }
287
288 static void hdlc_append_tx_buf(struct gb_beagleplay *bg, const u8 *buf, u16 len)
289 {
290 size_t i;
291
292 for (i = 0; i < len; i++)
293 hdlc_append_tx_u8(bg, buf[i]);
294 }
295
296 static void hdlc_append_tx_crc(struct gb_beagleplay *bg)
297 {
298 bg->tx_crc ^= 0xffff;
299 hdlc_append_escaped(bg, bg->tx_crc & 0xff);
300 hdlc_append_escaped(bg, (bg->tx_crc >> 8) & 0xff);
301 }
302
303 static void hdlc_transmit(struct work_struct *work)
304 {
305 struct gb_beagleplay *bg = container_of(work, struct gb_beagleplay, tx_work);
306
307 spin_lock_bh(&bg->tx_consumer_lock);
308 hdlc_write(bg);
309 spin_unlock_bh(&bg->tx_consumer_lock);
310 }
311
312 /**
313 * hdlc_encoded_length() - Calculate worst-case encoded length of an HDLC frame.
314 * @payloads: array of payload buffers
315 * @count: number of payloads
316 *
317 * Returns the maximum number of bytes needed in the circular buffer.
318 */
319 static size_t hdlc_encoded_length(const struct hdlc_payload payloads[],
320 size_t count)
321 {
322 size_t i, payload_len = 0;
323
324 for (i = 0; i < count; i++)
325 payload_len += payloads[i].len;
326
327 /*
328 * Worst case: every data byte needs escaping (doubles in size).
329 * data bytes = address(1) + control(1) + payload + crc(2)
330 * framing = opening flag(1) + closing flag(1)
331 */
332 return 2 + (1 + 1 + payload_len + 2) * 2;
333 }
334
335 #define HDLC_TX_BUF_WAIT_RETRIES 500
336 #define HDLC_TX_BUF_WAIT_US_MIN 3000
337 #define HDLC_TX_BUF_WAIT_US_MAX 5000
338
339 /**
340 * hdlc_tx_frames() - Encode and queue an HDLC frame for transmission.
341 * @bg: beagleplay greybus driver
342 * @address: HDLC address field
343 * @control: HDLC control field
344 * @payloads: array of payload buffers
345 * @count: number of payloads
346 *
347 * Sleeps outside the spinlock until enough circular-buffer space is
348 * available, then verifies space under the lock and writes the entire
349 * frame atomically. Either a complete frame is enqueued or nothing is
350 * written, avoiding both sleeping in atomic context and partial frames.
351 */
352 static void hdlc_tx_frames(struct gb_beagleplay *bg, u8 address, u8 control,
353 const struct hdlc_payload payloads[], size_t count)
354 {
355 size_t needed = hdlc_encoded_length(payloads, count);
356 int retries = HDLC_TX_BUF_WAIT_RETRIES;
357 size_t i;
358 int head, tail;
359
360 /* Wait outside the lock for sufficient buffer space. */
361 while (retries--) {
362 /* Pairs with smp_store_release() in hdlc_append(). */
363 head = smp_load_acquire(&bg->tx_circ_buf.head);
364 tail = READ_ONCE(bg->tx_circ_buf.tail);
365
366 if (CIRC_SPACE(head, tail, TX_CIRC_BUF_SIZE) >= needed)
367 break;
368
369 /* Kick the consumer and sleep — no lock held. */
370 schedule_work(&bg->tx_work);
371 usleep_range(HDLC_TX_BUF_WAIT_US_MIN, HDLC_TX_BUF_WAIT_US_MAX);
372 }
373
374 if (retries < 0) {
375 dev_warn_ratelimited(&bg->sd->dev,
376 "Tx circ buf full, dropping frame\n");
377 return;
378 }
379
380 spin_lock(&bg->tx_producer_lock);
381
382 /*
383 * Re-check under the lock. Should not fail since
384 * tx_producer_lock serialises all producers and the
385 * consumer only frees space, but guard against it.
386 */
387 head = bg->tx_circ_buf.head;
388 tail = READ_ONCE(bg->tx_circ_buf.tail);
389 if (unlikely(CIRC_SPACE(head, tail, TX_CIRC_BUF_SIZE) < needed)) {
390 spin_unlock(&bg->tx_producer_lock);
391 dev_warn_ratelimited(&bg->sd->dev,
392 "Tx circ buf space lost, dropping frame\n");
393 return;
394 }
395
396 hdlc_append_tx_frame(bg);
397 hdlc_append_tx_u8(bg, address);
398 hdlc_append_tx_u8(bg, control);
399
400 for (i = 0; i < count; ++i)
401 hdlc_append_tx_buf(bg, payloads[i].buf, payloads[i].len);
402
403 hdlc_append_tx_crc(bg);
404 hdlc_append_tx_frame(bg);
405
406 spin_unlock(&bg->tx_producer_lock);
407
408 schedule_work(&bg->tx_work);
409 }
410
411 static void hdlc_tx_s_frame_ack(struct gb_beagleplay *bg)
412 {
413 hdlc_tx_frames(bg, bg->rx_buffer[0], (bg->rx_buffer[1] >> 1) & 0x7, NULL, 0);
414 }
415
416 static void hdlc_rx_frame(struct gb_beagleplay *bg)
417 {
418 u16 crc, len;
419 u8 ctrl, *buf;
420 u8 address = bg->rx_buffer[0];
421
422 crc = crc_ccitt(0xffff, bg->rx_buffer, bg->rx_buffer_len);
423 if (crc != 0xf0b8) {
424 dev_warn_ratelimited(&bg->sd->dev, "CRC failed from %02x: 0x%04x", address, crc);
425 return;
426 }
427
428 ctrl = bg->rx_buffer[1];
429 buf = &bg->rx_buffer[2];
430 len = bg->rx_buffer_len - 4;
431
432 /* I-Frame, send S-Frame ACK */
433 if ((ctrl & 1) == 0)
434 hdlc_tx_s_frame_ack(bg);
435
436 switch (address) {
437 case ADDRESS_DBG:
438 hdlc_rx_dbg_frame(bg, buf, len);
439 break;
440 case ADDRESS_GREYBUS:
441 hdlc_rx_greybus_frame(bg, buf, len);
442 break;
443 default:
444 dev_warn_ratelimited(&bg->sd->dev, "unknown frame %u", address);
445 }
446 }
447
448 static size_t hdlc_rx(struct gb_beagleplay *bg, const u8 *data, size_t count)
449 {
450 size_t i;
451 u8 c;
452
453 for (i = 0; i < count; ++i) {
454 c = data[i];
455
456 switch (c) {
457 case HDLC_FRAME:
458 if (bg->rx_buffer_len)
459 hdlc_rx_frame(bg);
460
461 bg->rx_buffer_len = 0;
462 break;
463 case HDLC_ESC:
464 bg->rx_in_esc = true;
465 break;
466 default:
467 if (bg->rx_in_esc) {
468 c ^= 0x20;
469 bg->rx_in_esc = false;
470 }
471
472 if (bg->rx_buffer_len < MAX_RX_HDLC) {
473 bg->rx_buffer[bg->rx_buffer_len] = c;
474 bg->rx_buffer_len++;
475 } else {
476 dev_err_ratelimited(&bg->sd->dev, "RX Buffer Overflow");
477 bg->rx_buffer_len = 0;
478 }
479 }
480 }
481
482 return count;
483 }
484
485 static int hdlc_init(struct gb_beagleplay *bg)
486 {
487 INIT_WORK(&bg->tx_work, hdlc_transmit);
488 spin_lock_init(&bg->tx_producer_lock);
489 spin_lock_init(&bg->tx_consumer_lock);
490 bg->tx_circ_buf.head = 0;
491 bg->tx_circ_buf.tail = 0;
492
493 bg->tx_circ_buf.buf = devm_kmalloc(&bg->sd->dev, TX_CIRC_BUF_SIZE, GFP_KERNEL);
494 if (!bg->tx_circ_buf.buf)
495 return -ENOMEM;
496
497 bg->rx_buffer_len = 0;
498 bg->rx_in_esc = false;
499
500 return 0;
501 }
502
503 static void hdlc_deinit(struct gb_beagleplay *bg)
504 {
505 flush_work(&bg->tx_work);
506 }
507
508 /**
509 * csum8: Calculate 8-bit checksum on data
510 *
511 * @data: bytes to calculate 8-bit checksum of
512 * @size: number of bytes
513 * @base: starting value for checksum
514 */
515 static u8 csum8(const u8 *data, size_t size, u8 base)
516 {
517 size_t i;
518 u8 sum = base;
519
520 for (i = 0; i < size; ++i)
521 sum += data[i];
522
523 return sum;
524 }
525
526 static void cc1352_bootloader_send_ack(struct gb_beagleplay *bg)
527 {
528 static const u8 ack[] = { 0x00, CC1352_BOOTLOADER_ACK };
529
530 serdev_device_write_buf(bg->sd, ack, sizeof(ack));
531 }
532
533 static void cc1352_bootloader_send_nack(struct gb_beagleplay *bg)
534 {
535 static const u8 nack[] = { 0x00, CC1352_BOOTLOADER_NACK };
536
537 serdev_device_write_buf(bg->sd, nack, sizeof(nack));
538 }
539
540 /**
541 * cc1352_bootloader_pkt_rx: Process a CC1352 Bootloader Packet
542 *
543 * @bg: beagleplay greybus driver
544 * @data: packet buffer
545 * @count: packet buffer size
546 *
547 * @return: number of bytes processed
548 *
549 * Here are the steps to successfully receive a packet from cc1352 bootloader
550 * according to the docs:
551 * 1. Wait for nonzero data to be returned from the device. This is important
552 * as the device may send zero bytes between a sent and a received data
553 * packet. The first nonzero byte received is the size of the packet that is
554 * being received.
555 * 2. Read the next byte, which is the checksum for the packet.
556 * 3. Read the data bytes from the device. During the data phase, packet size
557 * minus 2 bytes is sent.
558 * 4. Calculate the checksum of the data bytes and verify it matches the
559 * checksum received in the packet.
560 * 5. Send an acknowledge byte or a not-acknowledge byte to the device to
561 * indicate the successful or unsuccessful reception of the packet.
562 */
563 static int cc1352_bootloader_pkt_rx(struct gb_beagleplay *bg, const u8 *data,
564 size_t count)
565 {
566 bool is_valid = false;
567
568 switch (data[0]) {
569 /* Skip 0x00 bytes. */
570 case 0x00:
571 return 1;
572 case CC1352_BOOTLOADER_ACK:
573 case CC1352_BOOTLOADER_NACK:
574 WRITE_ONCE(bg->fwl_ack, data[0]);
575 complete(&bg->fwl_ack_com);
576 return 1;
577 case 3:
578 if (count < 3)
579 return 0;
580 is_valid = data[1] == data[2];
581 WRITE_ONCE(bg->fwl_cmd_response, (u32)data[2]);
582 break;
583 case 6:
584 if (count < 6)
585 return 0;
586 is_valid = csum8(&data[2], sizeof(__be32), 0) == data[1];
587 WRITE_ONCE(bg->fwl_cmd_response, get_unaligned_be32(&data[2]));
588 break;
589 default:
590 return -EINVAL;
591 }
592
593 if (is_valid) {
594 cc1352_bootloader_send_ack(bg);
595 complete(&bg->fwl_cmd_response_com);
596 } else {
597 dev_warn(&bg->sd->dev,
598 "Dropping bootloader packet with invalid checksum");
599 cc1352_bootloader_send_nack(bg);
600 }
601
602 return data[0];
603 }
604
605 static size_t cc1352_bootloader_rx(struct gb_beagleplay *bg, const u8 *data,
606 size_t count)
607 {
608 int ret;
609 size_t off = 0;
610
611 if (count > sizeof(bg->rx_buffer) - bg->rx_buffer_len) {
612 dev_warn(&bg->sd->dev,
613 "dropping oversized bootloader receive chunk");
614 bg->rx_buffer_len = 0;
615 return count;
616 }
617
618 memcpy(bg->rx_buffer + bg->rx_buffer_len, data, count);
619 bg->rx_buffer_len += count;
620
621 do {
622 ret = cc1352_bootloader_pkt_rx(bg, bg->rx_buffer + off,
623 bg->rx_buffer_len - off);
624 if (ret < 0)
625 return dev_err_probe(&bg->sd->dev, ret,
626 "Invalid Packet");
627 off += ret;
628 } while (ret > 0 && off < count);
629
630 bg->rx_buffer_len -= off;
631 memmove(bg->rx_buffer, bg->rx_buffer + off, bg->rx_buffer_len);
632
633 return count;
634 }
635
636 static size_t gb_tty_receive(struct serdev_device *sd, const u8 *data,
637 size_t count)
638 {
639 struct gb_beagleplay *bg = serdev_device_get_drvdata(sd);
640
641 if (READ_ONCE(bg->flashing_mode))
642 return cc1352_bootloader_rx(bg, data, count);
643
644 return hdlc_rx(bg, data, count);
645 }
646
647 static void gb_tty_wakeup(struct serdev_device *serdev)
648 {
649 struct gb_beagleplay *bg = serdev_device_get_drvdata(serdev);
650
651 if (!READ_ONCE(bg->flashing_mode))
652 schedule_work(&bg->tx_work);
653 }
654
655 static struct serdev_device_ops gb_beagleplay_ops = {
656 .receive_buf = gb_tty_receive,
657 .write_wakeup = gb_tty_wakeup,
658 };
659
660 /**
661 * gb_message_send() - Send greybus message using HDLC over UART
662 *
663 * @hd: pointer to greybus host device
664 * @cport: AP cport where message originates
665 * @msg: greybus message to send
666 * @mask: gfp mask
667 *
668 * Greybus HDLC frame has the following payload:
669 * 1. le16 cport
670 * 2. gb_operation_msg_hdr msg_header
671 * 3. u8 *msg_payload
672 */
673 static int gb_message_send(struct gb_host_device *hd, u16 cport, struct gb_message *msg, gfp_t mask)
674 {
675 struct gb_beagleplay *bg = dev_get_drvdata(&hd->dev);
676 struct hdlc_payload payloads[3];
677 __le16 cport_id = cpu_to_le16(cport);
678
679 dev_dbg(&hd->dev, "Sending greybus message with Operation %u, Type: %X on Cport %u",
680 msg->header->operation_id, msg->header->type, cport);
681
682 if (le16_to_cpu(msg->header->size) > RX_HDLC_PAYLOAD)
683 return dev_err_probe(&hd->dev, -E2BIG, "Greybus message too big");
684
685 payloads[0].buf = &cport_id;
686 payloads[0].len = sizeof(cport_id);
687 payloads[1].buf = msg->header;
688 payloads[1].len = sizeof(*msg->header);
689 payloads[2].buf = msg->payload;
690 payloads[2].len = msg->payload_size;
691
692 hdlc_tx_frames(bg, ADDRESS_GREYBUS, 0x03, payloads, 3);
693 greybus_message_sent(bg->gb_hd, msg, 0);
694
695 return 0;
696 }
697
698 static void gb_message_cancel(struct gb_message *message)
699 {
700 }
701
702 static struct gb_hd_driver gb_hdlc_driver = { .message_send = gb_message_send,
703 .message_cancel = gb_message_cancel };
704
705 static void gb_beagleplay_start_svc(struct gb_beagleplay *bg)
706 {
707 const u8 command = CONTROL_SVC_START;
708 const struct hdlc_payload payload = { .len = 1, .buf = (void *)&command };
709
710 hdlc_tx_frames(bg, ADDRESS_CONTROL, 0x03, &payload, 1);
711 }
712
713 static void gb_beagleplay_stop_svc(struct gb_beagleplay *bg)
714 {
715 const u8 command = CONTROL_SVC_STOP;
716 const struct hdlc_payload payload = { .len = 1, .buf = (void *)&command };
717
718 hdlc_tx_frames(bg, ADDRESS_CONTROL, 0x03, &payload, 1);
719 }
720
721 static int cc1352_bootloader_wait_for_ack(struct gb_beagleplay *bg)
722 {
723 int ret;
724
725 ret = wait_for_completion_timeout(
726 &bg->fwl_ack_com, msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT));
727 if (!ret)
728 return dev_err_probe(&bg->sd->dev, -ETIMEDOUT,
729 "Failed to acquire ack semaphore");
730
731 switch (READ_ONCE(bg->fwl_ack)) {
732 case CC1352_BOOTLOADER_ACK:
733 return 0;
734 case CC1352_BOOTLOADER_NACK:
735 return -EAGAIN;
736 default:
737 return -EINVAL;
738 }
739 }
740
741 static int cc1352_bootloader_sync(struct gb_beagleplay *bg)
742 {
743 static const u8 sync_bytes[] = { 0x55, 0x55 };
744
745 serdev_device_write_buf(bg->sd, sync_bytes, sizeof(sync_bytes));
746 return cc1352_bootloader_wait_for_ack(bg);
747 }
748
749 static int cc1352_bootloader_get_status(struct gb_beagleplay *bg)
750 {
751 int ret;
752 static const struct cc1352_bootloader_packet pkt = {
753 .len = sizeof(pkt),
754 .checksum = COMMAND_GET_STATUS,
755 .cmd = COMMAND_GET_STATUS
756 };
757
758 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
759 ret = cc1352_bootloader_wait_for_ack(bg);
760 if (ret < 0)
761 return ret;
762
763 ret = wait_for_completion_timeout(
764 &bg->fwl_cmd_response_com,
765 msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT));
766 if (!ret)
767 return dev_err_probe(&bg->sd->dev, -ETIMEDOUT,
768 "Failed to acquire last status semaphore");
769
770 switch (READ_ONCE(bg->fwl_cmd_response)) {
771 case COMMAND_RET_SUCCESS:
772 return 0;
773 default:
774 return -EINVAL;
775 }
776
777 return 0;
778 }
779
780 static int cc1352_bootloader_erase(struct gb_beagleplay *bg)
781 {
782 int ret;
783 static const struct cc1352_bootloader_packet pkt = {
784 .len = sizeof(pkt),
785 .checksum = COMMAND_BANK_ERASE,
786 .cmd = COMMAND_BANK_ERASE
787 };
788
789 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
790
791 ret = cc1352_bootloader_wait_for_ack(bg);
792 if (ret < 0)
793 return ret;
794
795 return cc1352_bootloader_get_status(bg);
796 }
797
798 static int cc1352_bootloader_reset(struct gb_beagleplay *bg)
799 {
800 static const struct cc1352_bootloader_packet pkt = {
801 .len = sizeof(pkt),
802 .checksum = COMMAND_RESET,
803 .cmd = COMMAND_RESET
804 };
805
806 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
807
808 return cc1352_bootloader_wait_for_ack(bg);
809 }
810
811 /**
812 * cc1352_bootloader_empty_pkt: Calculate the number of empty bytes in the current packet
813 *
814 * @data: packet bytes array to check
815 * @size: number of bytes in array
816 */
817 static size_t cc1352_bootloader_empty_pkt(const u8 *data, size_t size)
818 {
819 size_t i;
820
821 for (i = 0; i < size && data[i] == 0xff; ++i)
822 continue;
823
824 return i;
825 }
826
827 static int cc1352_bootloader_crc32(struct gb_beagleplay *bg, u32 *crc32)
828 {
829 int ret;
830 static const struct cc1352_bootloader_crc32_cmd_data cmd_data = {
831 .addr = 0, .size = cpu_to_be32(704 * 1024), .read_repeat = 0
832 };
833 const struct cc1352_bootloader_packet pkt = {
834 .len = sizeof(pkt) + sizeof(cmd_data),
835 .checksum = csum8((const void *)&cmd_data, sizeof(cmd_data),
836 COMMAND_CRC32),
837 .cmd = COMMAND_CRC32
838 };
839
840 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
841 serdev_device_write_buf(bg->sd, (const u8 *)&cmd_data,
842 sizeof(cmd_data));
843
844 ret = cc1352_bootloader_wait_for_ack(bg);
845 if (ret < 0)
846 return ret;
847
848 ret = wait_for_completion_timeout(
849 &bg->fwl_cmd_response_com,
850 msecs_to_jiffies(CC1352_BOOTLOADER_TIMEOUT));
851 if (!ret)
852 return dev_err_probe(&bg->sd->dev, -ETIMEDOUT,
853 "Failed to acquire last status semaphore");
854
855 *crc32 = READ_ONCE(bg->fwl_cmd_response);
856
857 return 0;
858 }
859
860 static int cc1352_bootloader_download(struct gb_beagleplay *bg, u32 size,
861 u32 addr)
862 {
863 int ret;
864 const struct cc1352_bootloader_download_cmd_data cmd_data = {
865 .addr = cpu_to_be32(addr),
866 .size = cpu_to_be32(size),
867 };
868 const struct cc1352_bootloader_packet pkt = {
869 .len = sizeof(pkt) + sizeof(cmd_data),
870 .checksum = csum8((const void *)&cmd_data, sizeof(cmd_data),
871 COMMAND_DOWNLOAD),
872 .cmd = COMMAND_DOWNLOAD
873 };
874
875 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
876 serdev_device_write_buf(bg->sd, (const u8 *)&cmd_data,
877 sizeof(cmd_data));
878
879 ret = cc1352_bootloader_wait_for_ack(bg);
880 if (ret < 0)
881 return ret;
882
883 return cc1352_bootloader_get_status(bg);
884 }
885
886 static int cc1352_bootloader_send_data(struct gb_beagleplay *bg, const u8 *data,
887 size_t size)
888 {
889 int ret, rem = min(size, CC1352_BOOTLOADER_PKT_MAX_SIZE);
890 const struct cc1352_bootloader_packet pkt = {
891 .len = sizeof(pkt) + rem,
892 .checksum = csum8(data, rem, COMMAND_SEND_DATA),
893 .cmd = COMMAND_SEND_DATA
894 };
895
896 serdev_device_write_buf(bg->sd, (const u8 *)&pkt, sizeof(pkt));
897 serdev_device_write_buf(bg->sd, data, rem);
898
899 ret = cc1352_bootloader_wait_for_ack(bg);
900 if (ret < 0)
901 return ret;
902
903 ret = cc1352_bootloader_get_status(bg);
904 if (ret < 0)
905 return ret;
906
907 return rem;
908 }
909
910 static void gb_greybus_deinit(struct gb_beagleplay *bg)
911 {
912 gb_hd_del(bg->gb_hd);
913 gb_hd_put(bg->gb_hd);
914 }
915
916 static int gb_greybus_init(struct gb_beagleplay *bg)
917 {
918 int ret;
919
920 bg->gb_hd = gb_hd_create(&gb_hdlc_driver, &bg->sd->dev, TX_CIRC_BUF_SIZE, GB_MAX_CPORTS);
921 if (IS_ERR(bg->gb_hd)) {
922 dev_err(&bg->sd->dev, "Failed to create greybus host device");
923 return PTR_ERR(bg->gb_hd);
924 }
925
926 ret = gb_hd_add(bg->gb_hd);
927 if (ret) {
928 dev_err(&bg->sd->dev, "Failed to add greybus host device");
929 goto free_gb_hd;
930 }
931 dev_set_drvdata(&bg->gb_hd->dev, bg);
932
933 return 0;
934
935 free_gb_hd:
936 gb_greybus_deinit(bg);
937 return ret;
938 }
939
940 static enum fw_upload_err cc1352_prepare(struct fw_upload *fw_upload,
941 const u8 *data, u32 size)
942 {
943 int ret;
944 u32 curr_crc32;
945 struct gb_beagleplay *bg = fw_upload->dd_handle;
946
947 dev_info(&bg->sd->dev, "CC1352 Start Flashing...");
948
949 if (size != CC1352_FIRMWARE_SIZE)
950 return FW_UPLOAD_ERR_INVALID_SIZE;
951
952 /* Might involve network calls */
953 gb_greybus_deinit(bg);
954 msleep(5 * MSEC_PER_SEC);
955
956 gb_beagleplay_stop_svc(bg);
957 msleep(200);
958 flush_work(&bg->tx_work);
959
960 serdev_device_wait_until_sent(bg->sd, CC1352_BOOTLOADER_TIMEOUT);
961
962 WRITE_ONCE(bg->flashing_mode, true);
963
964 gpiod_direction_output(bg->bootloader_backdoor_gpio, 0);
965 gpiod_direction_output(bg->rst_gpio, 0);
966 msleep(200);
967
968 gpiod_set_value(bg->rst_gpio, 1);
969 msleep(200);
970
971 gpiod_set_value(bg->bootloader_backdoor_gpio, 1);
972 msleep(200);
973
974 gpiod_direction_input(bg->bootloader_backdoor_gpio);
975 gpiod_direction_input(bg->rst_gpio);
976
977 ret = cc1352_bootloader_sync(bg);
978 if (ret < 0)
979 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
980 "Failed to sync");
981
982 ret = cc1352_bootloader_crc32(bg, &curr_crc32);
983 if (ret < 0)
984 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
985 "Failed to fetch crc32");
986
987 bg->fwl_crc32 = crc32(0xffffffff, data, size) ^ 0xffffffff;
988
989 /* Check if attempting to reflash same firmware */
990 if (bg->fwl_crc32 == curr_crc32) {
991 dev_warn(&bg->sd->dev, "Skipping reflashing same image");
992 cc1352_bootloader_reset(bg);
993 WRITE_ONCE(bg->flashing_mode, false);
994 msleep(200);
995 if (gb_greybus_init(bg) < 0)
996 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_RW_ERROR,
997 "Failed to initialize greybus");
998 gb_beagleplay_start_svc(bg);
999 return FW_UPLOAD_ERR_FW_INVALID;
1000 }
1001
1002 ret = cc1352_bootloader_erase(bg);
1003 if (ret < 0)
1004 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
1005 "Failed to erase");
1006
1007 bg->fwl_reset_addr = true;
1008
1009 return FW_UPLOAD_ERR_NONE;
1010 }
1011
1012 static void cc1352_cleanup(struct fw_upload *fw_upload)
1013 {
1014 struct gb_beagleplay *bg = fw_upload->dd_handle;
1015
1016 WRITE_ONCE(bg->flashing_mode, false);
1017 }
1018
1019 static enum fw_upload_err cc1352_write(struct fw_upload *fw_upload,
1020 const u8 *data, u32 offset, u32 size,
1021 u32 *written)
1022 {
1023 int ret;
1024 size_t empty_bytes;
1025 struct gb_beagleplay *bg = fw_upload->dd_handle;
1026
1027 /* Skip 0xff packets. Significant performance improvement */
1028 empty_bytes = cc1352_bootloader_empty_pkt(data + offset, size);
1029 if (empty_bytes >= CC1352_BOOTLOADER_PKT_MAX_SIZE) {
1030 bg->fwl_reset_addr = true;
1031 *written = empty_bytes;
1032 return FW_UPLOAD_ERR_NONE;
1033 }
1034
1035 if (bg->fwl_reset_addr) {
1036 ret = cc1352_bootloader_download(bg, size, offset);
1037 if (ret < 0)
1038 return dev_err_probe(&bg->sd->dev,
1039 FW_UPLOAD_ERR_HW_ERROR,
1040 "Failed to send download cmd");
1041
1042 bg->fwl_reset_addr = false;
1043 }
1044
1045 ret = cc1352_bootloader_send_data(bg, data + offset, size);
1046 if (ret < 0)
1047 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
1048 "Failed to flash firmware");
1049 *written = ret;
1050
1051 return FW_UPLOAD_ERR_NONE;
1052 }
1053
1054 static enum fw_upload_err cc1352_poll_complete(struct fw_upload *fw_upload)
1055 {
1056 u32 curr_crc32;
1057 struct gb_beagleplay *bg = fw_upload->dd_handle;
1058
1059 if (cc1352_bootloader_crc32(bg, &curr_crc32) < 0)
1060 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
1061 "Failed to fetch crc32");
1062
1063 if (bg->fwl_crc32 != curr_crc32)
1064 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_FW_INVALID,
1065 "Invalid CRC32");
1066
1067 if (cc1352_bootloader_reset(bg) < 0)
1068 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_HW_ERROR,
1069 "Failed to reset");
1070
1071 dev_info(&bg->sd->dev, "CC1352 Flashing Successful");
1072 WRITE_ONCE(bg->flashing_mode, false);
1073 msleep(200);
1074
1075 if (gb_greybus_init(bg) < 0)
1076 return dev_err_probe(&bg->sd->dev, FW_UPLOAD_ERR_RW_ERROR,
1077 "Failed to initialize greybus");
1078
1079 gb_beagleplay_start_svc(bg);
1080
1081 return FW_UPLOAD_ERR_NONE;
1082 }
1083
1084 static void cc1352_cancel(struct fw_upload *fw_upload)
1085 {
1086 struct gb_beagleplay *bg = fw_upload->dd_handle;
1087
1088 dev_info(&bg->sd->dev, "CC1352 Bootloader Cancel");
1089
1090 cc1352_bootloader_reset(bg);
1091 }
1092
1093 static void gb_serdev_deinit(struct gb_beagleplay *bg)
1094 {
1095 serdev_device_close(bg->sd);
1096 }
1097
1098 static int gb_serdev_init(struct gb_beagleplay *bg)
1099 {
1100 int ret;
1101
1102 serdev_device_set_drvdata(bg->sd, bg);
1103 serdev_device_set_client_ops(bg->sd, &gb_beagleplay_ops);
1104 ret = serdev_device_open(bg->sd);
1105 if (ret)
1106 return dev_err_probe(&bg->sd->dev, ret, "Unable to open serial device");
1107
1108 serdev_device_set_baudrate(bg->sd, 115200);
1109 serdev_device_set_flow_control(bg->sd, false);
1110
1111 return 0;
1112 }
1113
1114 static const struct fw_upload_ops cc1352_bootloader_ops = {
1115 .prepare = cc1352_prepare,
1116 .write = cc1352_write,
1117 .poll_complete = cc1352_poll_complete,
1118 .cancel = cc1352_cancel,
1119 .cleanup = cc1352_cleanup
1120 };
1121
1122 /*
1123 * Must only be called from probe() as the devres resources allocated here
1124 * will only be released on driver detach.
1125 */
1126 static int gb_fw_init(struct gb_beagleplay *bg)
1127 {
1128 struct fw_upload *fwl;
1129 struct gpio_desc *desc;
1130
1131 bg->fwl = NULL;
1132 bg->bootloader_backdoor_gpio = NULL;
1133 bg->rst_gpio = NULL;
1134 bg->flashing_mode = false;
1135 bg->fwl_cmd_response = 0;
1136 bg->fwl_ack = 0;
1137 init_completion(&bg->fwl_ack_com);
1138 init_completion(&bg->fwl_cmd_response_com);
1139
1140 desc = devm_gpiod_get(&bg->sd->dev, "bootloader-backdoor", GPIOD_IN);
1141 if (IS_ERR(desc))
1142 return PTR_ERR(desc);
1143 bg->bootloader_backdoor_gpio = desc;
1144
1145 desc = devm_gpiod_get(&bg->sd->dev, "reset", GPIOD_IN);
1146 if (IS_ERR(desc))
1147 return PTR_ERR(desc);
1148 bg->rst_gpio = desc;
1149
1150 fwl = firmware_upload_register(THIS_MODULE, &bg->sd->dev, "cc1352p7",
1151 &cc1352_bootloader_ops, bg);
1152 if (IS_ERR(fwl))
1153 return PTR_ERR(fwl);
1154 bg->fwl = fwl;
1155
1156 return 0;
1157 }
1158
1159 static void gb_fw_deinit(struct gb_beagleplay *bg)
1160 {
1161 firmware_upload_unregister(bg->fwl);
1162 }
1163
1164 static int gb_beagleplay_probe(struct serdev_device *serdev)
1165 {
1166 int ret = 0;
1167 struct gb_beagleplay *bg;
1168
1169 bg = devm_kmalloc(&serdev->dev, sizeof(*bg), GFP_KERNEL);
1170 if (!bg)
1171 return -ENOMEM;
1172
1173 bg->sd = serdev;
1174 ret = gb_serdev_init(bg);
1175 if (ret)
1176 return ret;
1177
1178 ret = hdlc_init(bg);
1179 if (ret)
1180 goto free_serdev;
1181
1182 ret = gb_fw_init(bg);
1183 if (ret)
1184 goto free_hdlc;
1185
1186 ret = gb_greybus_init(bg);
1187 if (ret)
1188 goto free_fw;
1189
1190 gb_beagleplay_start_svc(bg);
1191
1192 return 0;
1193
1194 free_fw:
1195 gb_fw_deinit(bg);
1196 free_hdlc:
1197 hdlc_deinit(bg);
1198 free_serdev:
1199 gb_serdev_deinit(bg);
1200 return ret;
1201 }
1202
1203 static void gb_beagleplay_remove(struct serdev_device *serdev)
1204 {
1205 struct gb_beagleplay *bg = serdev_device_get_drvdata(serdev);
1206
1207 gb_fw_deinit(bg);
1208 gb_greybus_deinit(bg);
1209 gb_beagleplay_stop_svc(bg);
1210 hdlc_deinit(bg);
1211 gb_serdev_deinit(bg);
1212 }
1213
1214 static const struct of_device_id gb_beagleplay_of_match[] = {
1215 {
1216 .compatible = "ti,cc1352p7",
1217 },
1218 {},
1219 };
1220 MODULE_DEVICE_TABLE(of, gb_beagleplay_of_match);
1221
1222 static struct serdev_device_driver gb_beagleplay_driver = {
1223 .probe = gb_beagleplay_probe,
1224 .remove = gb_beagleplay_remove,
1225 .driver = {
1226 .name = "gb_beagleplay",
1227 .of_match_table = gb_beagleplay_of_match,
1228 },
1229 };
1230
1231 module_serdev_device_driver(gb_beagleplay_driver);
1232
1233 MODULE_LICENSE("GPL");
1234 MODULE_AUTHOR("Ayush Singh <[email protected]>");
1235 MODULE_DESCRIPTION("A Greybus driver for BeaglePlay");