1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
13 #include <dm/device-internal.h>
14 #include <linux/delay.h>
15 #include "stm32prog.h"
17 /* - configuration part -----------------------------*/
18 #define USART_BL_VERSION 0x40 /* USART bootloader version V4.0*/
19 #define UBOOT_BL_VERSION 0x03 /* bootloader version V0.3*/
20 #define DEVICE_ID_BYTE1 0x05 /* MSB byte of device ID*/
21 #define DEVICE_ID_BYTE2 0x00 /* LSB byte of device ID*/
22 #define USART_RAM_BUFFER_SIZE 256 /* Size of USART_RAM_Buf buffer*/
24 /* - Commands -----------------------------*/
25 #define GET_CMD_COMMAND 0x00 /* Get CMD command*/
26 #define GET_VER_COMMAND 0x01 /* Get Version command*/
27 #define GET_ID_COMMAND 0x02 /* Get ID command*/
28 #define GET_PHASE_COMMAND 0x03 /* Get Phase command*/
29 #define RM_COMMAND 0x11 /* Read Memory command*/
30 #define READ_PART_COMMAND 0x12 /* Read Partition command*/
31 #define START_COMMAND 0x21 /* START command (Go)*/
32 #define DOWNLOAD_COMMAND 0x31 /* Download command*/
33 /* existing command for other STM32 but not used */
35 /* EXTENDED_ERASE 0x44 */
36 /* WRITE_UNPROTECTED 0x73 */
37 /* READOUT_PROTECT 0x82 */
38 /* READOUT_UNPROTECT 0x92 */
40 /* - miscellaneous defines ----------------------------------------*/
41 #define INIT_BYTE 0x7F /*Init Byte ID*/
42 #define ACK_BYTE 0x79 /*Acknowlede Byte ID*/
43 #define NACK_BYTE 0x1F /*No Acknowlede Byte ID*/
44 #define ABORT_BYTE 0x5F /*ABORT*/
46 struct udevice *down_serial_dev;
59 #define NB_CMD sizeof(cmd_id)
61 /* DFU support for serial *********************************************/
62 static struct dfu_entity *stm32prog_get_entity(struct stm32prog_data *data)
67 if (data->phase == PHASE_FLASHLAYOUT)
72 alt_id = data->cur_part->alt_id;
74 return dfu_get_entity(alt_id);
77 static int stm32prog_write(struct stm32prog_data *data, u8 *buffer,
80 struct dfu_entity *dfu_entity;
83 dfu_entity = stm32prog_get_entity(data);
87 ret = dfu_write(dfu_entity,
93 stm32prog_err("DFU write failed [%d] cnt: %d",
97 /* handle rollover as in driver/dfu/dfu.c */
98 data->dfu_seq &= 0xffff;
100 data->dfu_seq = 0; /* flush done */
105 static int stm32prog_read(struct stm32prog_data *data, u8 phase, u32 offset,
106 u8 *buffer, u32 buffer_size)
108 struct dfu_entity *dfu_entity;
109 struct stm32prog_part_t *part;
114 stm32prog_err("DFU write pending for phase %d, seq %d",
115 data->phase, data->dfu_seq);
118 if (phase == PHASE_FLASHLAYOUT || phase > PHASE_LAST_USER) {
119 stm32prog_err("read failed : phase %d is invalid", phase);
122 if (data->read_phase <= PHASE_LAST_USER &&
123 phase != data->read_phase) {
124 /* clear previous read session */
125 dfu_entity = dfu_get_entity(data->read_phase - 1);
127 dfu_transaction_cleanup(dfu_entity);
131 /* found partition for the expected phase */
132 for (i = 0; i < data->part_nb; i++) {
133 part = &data->part_array[i];
134 if (part->id == phase)
135 dfu_entity = dfu_get_entity(part->alt_id);
138 stm32prog_err("read failed : phase %d is unknown", phase);
142 /* clear pending read before to force offset */
143 if (dfu_entity->inited &&
144 (data->read_phase != phase || data->offset != offset))
145 dfu_transaction_cleanup(dfu_entity);
147 /* initiate before to force offset */
148 if (!dfu_entity->inited) {
149 ret = dfu_transaction_initiate(dfu_entity, true);
151 stm32prog_err("DFU read init failed [%d] phase = %d offset = 0x%08x",
156 /* force new offset */
157 if (dfu_entity->offset != offset)
158 dfu_entity->offset = offset;
159 data->offset = offset;
160 data->read_phase = phase;
161 pr_debug("\nSTM32 download read %s offset=0x%x\n",
162 dfu_entity->name, offset);
163 ret = dfu_read(dfu_entity, buffer, buffer_size,
164 dfu_entity->i_blk_seq_num);
166 stm32prog_err("DFU read failed [%d] phase = %d offset = 0x%08x",
173 if (size < buffer_size) {
175 data->read_phase = PHASE_END;
176 memset(buffer + size, 0, buffer_size - size);
178 data->offset += size;
184 /* UART access ***************************************************/
185 int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
187 struct udevice *dev = NULL;
191 struct dm_serial_ops *ops;
192 /* no parity, 8 bits, 1 stop */
193 u32 serial_config = SERIAL_DEFAULT_CONFIG;
195 down_serial_dev = NULL;
197 sprintf(alias, "serial%d", link_dev);
198 path = fdt_get_alias(gd->fdt_blob, alias);
200 pr_err("%s alias not found", alias);
203 node = fdt_path_offset(gd->fdt_blob, path);
204 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
206 down_serial_dev = dev;
207 } else if (node > 0 &&
208 !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
210 if (!device_probe(dev))
211 down_serial_dev = dev;
213 if (!down_serial_dev) {
214 pr_err("%s = %s device not found", alias, path);
218 /* force silent console on uart only when used */
219 if (gd->cur_serial_dev == down_serial_dev)
220 gd->flags |= GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT;
222 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
224 ops = serial_get_ops(down_serial_dev);
227 pr_err("%s = %s missing ops", alias, path);
230 if (!ops->setconfig) {
231 pr_err("%s = %s missing setconfig", alias, path);
235 clrsetbits_le32(&serial_config, SERIAL_PAR_MASK, SERIAL_PAR_EVEN);
237 data->buffer = memalign(CONFIG_SYS_CACHELINE_SIZE,
238 USART_RAM_BUFFER_SIZE);
240 return ops->setconfig(down_serial_dev, serial_config);
243 static void stm32prog_serial_flush(void)
245 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
249 err = ops->getc(down_serial_dev);
250 } while (err != -EAGAIN);
253 static int stm32prog_serial_getc_err(void)
255 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
259 err = ops->getc(down_serial_dev);
260 if (err == -EAGAIN) {
264 } while ((err == -EAGAIN) && (!had_ctrlc()));
269 static u8 stm32prog_serial_getc(void)
273 err = stm32prog_serial_getc_err();
275 return err >= 0 ? err : 0;
278 static bool stm32prog_serial_get_buffer(u8 *buffer, u32 *count)
280 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
284 err = ops->getc(down_serial_dev);
288 } else if (err == -EAGAIN) {
294 } while (*count && !had_ctrlc());
299 static void stm32prog_serial_putc(u8 w_byte)
301 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
305 err = ops->putc(down_serial_dev, w_byte);
306 } while (err == -EAGAIN);
309 /* Helper function ************************************************/
311 static u8 stm32prog_header(struct stm32prog_data *data)
315 struct dfu_entity *dfu_entity;
318 dfu_entity = stm32prog_get_entity(data);
322 printf("\nSTM32 download write %s\n", dfu_entity->name);
324 /* force cleanup to avoid issue with previous read */
325 dfu_transaction_cleanup(dfu_entity);
327 ret = stm32prog_header_check(data->header_data,
330 /* no header : max size is partition size */
332 dfu_entity->get_medium_size(dfu_entity, &size);
333 data->header.image_length = size;
336 /**** Flash the header if necessary for boot partition */
337 if (data->phase < PHASE_FIRST_USER)
340 /* write header if boot partition */
343 stm32prog_err("invalid header (error %d)", ret);
345 ret = stm32prog_write(data,
346 (u8 *)data->header_data,
351 printf(" partition without checksum\n");
355 free(data->header_data);
356 data->header_data = NULL;
361 static u8 stm32prog_start(struct stm32prog_data *data, u32 address)
364 struct dfu_entity *dfu_entity;
366 if (address < 0x100) {
367 if (address == PHASE_OTP)
368 return stm32prog_otp_start(data);
370 if (address == PHASE_PMIC)
371 return stm32prog_pmic_start(data);
373 if (address == PHASE_RESET || address == PHASE_END) {
374 data->cur_part = NULL;
376 data->phase = address;
379 if (address != data->phase) {
380 stm32prog_err("invalid received phase id %d, current phase is %d",
381 (u8)address, (u8)data->phase);
385 /* check the last loaded partition */
386 if (address == DEFAULT_ADDRESS || address == data->phase) {
387 switch (data->phase) {
391 data->cur_part = NULL;
392 data->phase = PHASE_DO_RESET;
395 dfu_entity = stm32prog_get_entity(data);
400 ret = dfu_flush(dfu_entity, NULL, 0, data->dfu_seq);
403 stm32prog_err("DFU flush failed [%d]", ret);
407 printf("\n received length = 0x%x\n", data->cursor);
408 if (data->header.present) {
410 (data->header.image_length + BL_HEADER_SIZE)) {
411 stm32prog_err("transmission interrupted (length=0x%x expected=0x%x)",
413 data->header.image_length +
417 if (data->header.image_checksum != data->checksum) {
418 stm32prog_err("invalid checksum received (0x%x expected 0x%x)",
420 data->header.image_checksum);
423 printf("\n checksum OK (0x%x)\n", data->checksum);
426 /* update DFU with received flashlayout */
427 if (data->phase == PHASE_FLASHLAYOUT)
428 stm32prog_dfu_init(data);
430 void (*entry)(void) = (void *)address;
432 printf("## Starting application at 0x%x ...\n", address);
434 printf("## Application terminated\n");
442 * get_address() - Get address if it is valid
444 * @tmp_xor: Current xor value to update
445 * @return The address area
447 static u32 get_address(u8 *tmp_xor)
452 data = stm32prog_serial_getc();
454 address |= ((u32)data) << 24;
456 data = stm32prog_serial_getc();
457 address |= ((u32)data) << 16;
460 data = stm32prog_serial_getc();
461 address |= ((u32)data) << 8;
464 data = stm32prog_serial_getc();
465 address |= ((u32)data);
471 static void stm32prog_serial_result(u8 result)
473 /* always flush fifo before to send result */
474 stm32prog_serial_flush();
475 stm32prog_serial_putc(result);
478 /* Command -----------------------------------------------*/
480 * get_cmd_command() - Respond to Get command
482 * @data: Current command context
484 static void get_cmd_command(struct stm32prog_data *data)
488 stm32prog_serial_putc(NB_CMD);
489 stm32prog_serial_putc(USART_BL_VERSION);
491 for (counter = 0; counter < NB_CMD; counter++)
492 stm32prog_serial_putc(cmd_id[counter]);
494 stm32prog_serial_result(ACK_BYTE);
498 * get_version_command() - Respond to Get Version command
500 * @data: Current command context
502 static void get_version_command(struct stm32prog_data *data)
504 stm32prog_serial_putc(UBOOT_BL_VERSION);
505 stm32prog_serial_result(ACK_BYTE);
509 * get_id_command() - Respond to Get ID command
511 * @data: Current command context
513 static void get_id_command(struct stm32prog_data *data)
515 /* Send Device IDCode */
516 stm32prog_serial_putc(0x1);
517 stm32prog_serial_putc(DEVICE_ID_BYTE1);
518 stm32prog_serial_putc(DEVICE_ID_BYTE2);
519 stm32prog_serial_result(ACK_BYTE);
523 * get_phase_command() - Respond to Get phase
525 * @data: Current command context
527 static void get_phase_command(struct stm32prog_data *data)
529 char *err_msg = NULL;
531 u32 destination = DEFAULT_ADDRESS; /* destination address */
532 int phase = data->phase;
534 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
535 err_msg = stm32prog_get_error(data);
536 length = strlen(err_msg);
538 if (phase == PHASE_FLASHLAYOUT)
539 destination = STM32_DDR_BASE;
541 stm32prog_serial_putc(length + 5); /* Total length */
542 stm32prog_serial_putc(phase & 0xFF); /* partition ID */
543 stm32prog_serial_putc(destination); /* byte 1 of address */
544 stm32prog_serial_putc(destination >> 8); /* byte 2 of address */
545 stm32prog_serial_putc(destination >> 16); /* byte 3 of address */
546 stm32prog_serial_putc(destination >> 24); /* byte 4 of address */
548 stm32prog_serial_putc(length); /* Information length */
549 for (i = 0; i < length; i++)
550 stm32prog_serial_putc(err_msg[i]);
551 stm32prog_serial_result(ACK_BYTE);
553 if (phase == PHASE_RESET)
554 stm32prog_do_reset(data);
558 * read_memory_command() - Read data from memory
560 * @data: Current command context
562 static void read_memory_command(struct stm32prog_data *data)
565 u8 rcv_data = 0x0, tmp_xor = 0x0;
568 /* Read memory address */
569 address = get_address(&tmp_xor);
571 /* If address memory is not received correctly */
572 rcv_data = stm32prog_serial_getc();
573 if (rcv_data != tmp_xor) {
574 stm32prog_serial_result(NACK_BYTE);
578 stm32prog_serial_result(ACK_BYTE);
580 /* Read the number of bytes to be received:
581 * Max NbrOfData = Data + 1 = 256
583 rcv_data = stm32prog_serial_getc();
585 if (stm32prog_serial_getc() != tmp_xor) {
586 stm32prog_serial_result(NACK_BYTE);
590 /* If checksum is correct send ACK */
591 stm32prog_serial_result(ACK_BYTE);
593 /* Send data to the host:
594 * Number of data to read = data + 1
596 for (counter = (rcv_data + 1); counter != 0; counter--)
597 stm32prog_serial_putc(*(u8 *)(address++));
601 * start_command() - Respond to start command
603 * Jump to user application in RAM or partition check
605 * @data: Current command context
607 static void start_command(struct stm32prog_data *data)
613 /* Read memory address */
614 address = get_address(&tmp_xor);
616 /* If address memory is not received correctly */
617 rcv_data = stm32prog_serial_getc();
618 if (rcv_data != tmp_xor) {
619 stm32prog_serial_result(NACK_BYTE);
622 /* validate partition */
623 ret = stm32prog_start(data,
627 stm32prog_serial_result(ABORT_BYTE);
629 stm32prog_serial_result(ACK_BYTE);
633 * download_command() - Respond to download command
635 * Write data to not volatile memory, Flash
637 * @data: Current command context
639 static void download_command(struct stm32prog_data *data)
644 u32 counter = 0x0, codesize = 0x0;
647 struct image_header_s *image_header = &data->header;
648 u32 cursor = data->cursor;
652 u32 result = ACK_BYTE;
658 address = get_address(&my_xor);
660 /* If address memory is not received correctly */
661 rcv_xor = stm32prog_serial_getc();
662 if (rcv_xor != my_xor) {
667 /* If address valid send ACK */
668 stm32prog_serial_result(ACK_BYTE);
670 /* get packet number and operation type */
671 operation = (u8)((u32)address >> 24);
672 packet_number = ((u32)(((u32)address << 8))) >> 8;
675 /* supported operation */
676 case PHASE_FLASHLAYOUT:
684 /* check the packet number */
685 if (packet_number == 0) {
686 /* erase: re-initialize the image_header struct */
687 data->packet_number = 0;
688 if (data->header_data)
689 memset(data->header_data, 0, BL_HEADER_SIZE);
691 data->header_data = calloc(1, BL_HEADER_SIZE);
697 data->packet_number++;
700 /* Check with the number of current packet if the device receive
703 if (packet_number != data->packet_number) {
704 data->packet_number--;
709 /*-- Read number of bytes to be written and data -----------*/
711 /* Read the number of bytes to be written:
712 * Max NbrOfData = data + 1 <= 256
714 rcv_data = stm32prog_serial_getc();
716 /* NbrOfData to write = data + 1 */
717 codesize = rcv_data + 0x01;
719 if (codesize > USART_RAM_BUFFER_SIZE) {
724 /* Checksum Initialization */
727 /* UART receive data and send to Buffer */
729 error = stm32prog_serial_get_buffer(data->buffer, &counter);
733 rcv = stm32prog_serial_getc_err();
739 printf("transmission error on packet %d, byte %d\n",
740 packet_number, codesize - counter);
741 /* waiting end of packet before flush & NACK */
743 data->packet_number--;
748 /* Compute Checksum */
749 ramaddress = data->buffer;
750 for (counter = codesize; counter != 0; counter--)
751 my_xor ^= *(ramaddress++);
753 /* If Checksum is incorrect */
754 if (rcv_xor != my_xor) {
755 printf("checksum error on packet %d\n",
757 /* wait to be sure that all data are received
758 * in the FIFO before flush
761 data->packet_number--;
766 /* Update current position in buffer */
767 data->cursor += codesize;
769 if (operation == PHASE_OTP) {
770 size = data->cursor - cursor;
771 /* no header for OTP */
772 if (stm32prog_otp_write(data, cursor,
773 data->buffer, &size))
778 if (operation == PHASE_PMIC) {
779 size = data->cursor - cursor;
780 /* no header for PMIC */
781 if (stm32prog_pmic_write(data, cursor,
782 data->buffer, &size))
787 if (cursor < BL_HEADER_SIZE) {
788 /* size = portion of header in this chunck */
789 if (data->cursor >= BL_HEADER_SIZE)
790 size = BL_HEADER_SIZE - cursor;
792 size = data->cursor - cursor;
793 memcpy((void *)((u32)(data->header_data) + cursor),
797 if (cursor == BL_HEADER_SIZE) {
798 /* Check and Write the header */
799 if (stm32prog_header(data)) {
808 if (image_header->present) {
809 if (data->cursor <= BL_HEADER_SIZE)
811 /* compute checksum on payload */
812 for (i = (unsigned long)size; i < codesize; i++)
813 data->checksum += data->buffer[i];
816 image_header->image_length + BL_HEADER_SIZE) {
817 pr_err("expected size exceeded\n");
822 /* write data (payload) */
823 ret = stm32prog_write(data,
828 ret = stm32prog_write(data,
836 stm32prog_serial_result(result);
840 * read_partition() - Respond to read command
842 * Read data from not volatile memory, Flash
844 * @data: Current command context
846 static void read_partition_command(struct stm32prog_data *data)
848 u32 i, part_id, codesize, offset = 0, rcv_data;
854 part_id = stm32prog_serial_getc();
857 offset = get_address(&tmp_xor);
859 rcv_data = stm32prog_serial_getc();
860 if (rcv_data != tmp_xor) {
861 pr_debug("1st checksum received = %x, computed %x\n",
865 stm32prog_serial_putc(ACK_BYTE);
867 /* NbrOfData to read = data + 1 */
868 rcv_data = stm32prog_serial_getc();
869 codesize = rcv_data + 0x01;
872 rcv_data = stm32prog_serial_getc();
873 if ((rcv_data ^ tmp_xor) != 0xFF) {
874 pr_debug("2nd checksum received = %x, computed %x\n",
879 pr_debug("%s : %x\n", __func__, part_id);
884 if (!stm32prog_otp_read(data, offset, buffer, &size))
889 if (!stm32prog_pmic_read(data, offset, buffer, &size))
893 res = stm32prog_read(data, part_id, offset,
900 stm32prog_serial_putc(ACK_BYTE);
901 /*----------- Send data to the host -----------*/
902 for (i = 0; i < rcv_data; i++)
903 stm32prog_serial_putc(buffer[i]);
904 /*----------- Send filler to the host -----------*/
905 for (; i < codesize; i++)
906 stm32prog_serial_putc(0x0);
909 stm32prog_serial_result(ABORT_BYTE);
913 stm32prog_serial_result(NACK_BYTE);
916 /* MAIN function = SERIAL LOOP ***********************************************/
919 * stm32prog_serial_loop() - USART bootloader Loop routine
921 * @data: Current command context
922 * @return true if reset is needed after loop
924 bool stm32prog_serial_loop(struct stm32prog_data *data)
929 int phase = data->phase;
931 /* element of cmd_func need to aligned with cmd_id[]*/
932 void (*cmd_func[NB_CMD])(struct stm32prog_data *) = {
933 /* GET_CMD_COMMAND */ get_cmd_command,
934 /* GET_VER_COMMAND */ get_version_command,
935 /* GET_ID_COMMAND */ get_id_command,
936 /* GET_PHASE_COMMAND */ get_phase_command,
937 /* RM_COMMAND */ read_memory_command,
938 /* READ_PART_COMMAND */ read_partition_command,
939 /* START_COMMAND */ start_command,
940 /* DOWNLOAD_COMMAND */ download_command
943 /* flush and NACK pending command received during u-boot init
944 * request command reemit
946 stm32prog_serial_result(NACK_BYTE);
948 clear_ctrlc(); /* forget any previous Control C */
949 while (!had_ctrlc()) {
952 if (phase == PHASE_DO_RESET)
955 /* Get the user command: read first byte */
956 command = stm32prog_serial_getc();
958 if (command == INIT_BYTE) {
959 puts("\nConnected\n");
960 stm32prog_serial_result(ACK_BYTE);
965 for (counter = 0; counter < NB_CMD; counter++)
966 if (cmd_id[counter] == command) {
971 if ((command ^ stm32prog_serial_getc()) != 0xFF)
974 /* wait to be sure that all data are received
975 * in the FIFO before flush (CMD and XOR)
978 stm32prog_serial_result(NACK_BYTE);
980 stm32prog_serial_result(ACK_BYTE);
981 cmd_func[counter](data);
987 if (gd->cur_serial_dev == down_serial_dev) {
988 /* restore console on uart */
989 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
991 down_serial_dev = NULL;
993 return false; /* no reset after ctrlc */