1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
13 #include <asm/arch/sys_proto.h>
15 #include <dm/device-internal.h>
16 #include <linux/delay.h>
17 #include <asm/global_data.h>
18 #include "stm32prog.h"
20 /* - configuration part -----------------------------*/
21 #define USART_BL_VERSION 0x40 /* USART bootloader version V4.0*/
22 #define UBOOT_BL_VERSION 0x03 /* bootloader version V0.3*/
24 #define USART_RAM_BUFFER_SIZE 256 /* Size of USART_RAM_Buf buffer*/
26 /* - Commands -----------------------------*/
27 #define GET_CMD_COMMAND 0x00 /* Get CMD command*/
28 #define GET_VER_COMMAND 0x01 /* Get Version command*/
29 #define GET_ID_COMMAND 0x02 /* Get ID command*/
30 #define GET_PHASE_COMMAND 0x03 /* Get Phase command*/
31 #define RM_COMMAND 0x11 /* Read Memory command*/
32 #define READ_PART_COMMAND 0x12 /* Read Partition command*/
33 #define START_COMMAND 0x21 /* START command (Go)*/
34 #define DOWNLOAD_COMMAND 0x31 /* Download command*/
35 /* existing command for other STM32 but not used */
37 /* EXTENDED_ERASE 0x44 */
38 /* WRITE_UNPROTECTED 0x73 */
39 /* READOUT_PROTECT 0x82 */
40 /* READOUT_UNPROTECT 0x92 */
42 /* - miscellaneous defines ----------------------------------------*/
43 #define INIT_BYTE 0x7F /*Init Byte ID*/
44 #define ACK_BYTE 0x79 /*Acknowlede Byte ID*/
45 #define NACK_BYTE 0x1F /*No Acknowlede Byte ID*/
46 #define ABORT_BYTE 0x5F /*ABORT*/
48 struct udevice *down_serial_dev;
61 #define NB_CMD sizeof(cmd_id)
63 /* with 115200 bauds, 20 ms allow to receive the 256 bytes buffer */
64 #define TIMEOUT_SERIAL_BUFFER 30
66 /* DFU support for serial *********************************************/
67 static struct dfu_entity *stm32prog_get_entity(struct stm32prog_data *data)
72 if (data->phase == PHASE_FLASHLAYOUT)
77 alt_id = data->cur_part->alt_id;
79 return dfu_get_entity(alt_id);
82 static int stm32prog_write(struct stm32prog_data *data, u8 *buffer,
85 struct dfu_entity *dfu_entity;
88 dfu_entity = stm32prog_get_entity(data);
92 ret = dfu_write(dfu_entity,
98 stm32prog_err("DFU write failed [%d] cnt: %d",
102 /* handle rollover as in driver/dfu/dfu.c */
103 data->dfu_seq &= 0xffff;
104 if (buffer_size == 0)
105 data->dfu_seq = 0; /* flush done */
110 static int stm32prog_read(struct stm32prog_data *data, u8 phase, u32 offset,
111 u8 *buffer, u32 buffer_size)
113 struct dfu_entity *dfu_entity;
114 struct stm32prog_part_t *part;
119 stm32prog_err("DFU write pending for phase %d, seq %d",
120 data->phase, data->dfu_seq);
123 if (phase == PHASE_FLASHLAYOUT || phase > PHASE_LAST_USER) {
124 stm32prog_err("read failed : phase %d is invalid", phase);
127 if (data->read_phase <= PHASE_LAST_USER &&
128 phase != data->read_phase) {
129 /* clear previous read session */
130 dfu_entity = dfu_get_entity(data->read_phase - 1);
132 dfu_transaction_cleanup(dfu_entity);
136 /* found partition for the expected phase */
137 for (i = 0; i < data->part_nb; i++) {
138 part = &data->part_array[i];
139 if (part->id == phase)
140 dfu_entity = dfu_get_entity(part->alt_id);
143 stm32prog_err("read failed : phase %d is unknown", phase);
147 /* clear pending read before to force offset */
148 if (dfu_entity->inited &&
149 (data->read_phase != phase || data->offset != offset))
150 dfu_transaction_cleanup(dfu_entity);
152 /* initiate before to force offset */
153 if (!dfu_entity->inited) {
154 ret = dfu_transaction_initiate(dfu_entity, true);
156 stm32prog_err("DFU read init failed [%d] phase = %d offset = 0x%08x",
161 /* force new offset */
162 if (dfu_entity->offset != offset)
163 dfu_entity->offset = offset;
164 data->offset = offset;
165 data->read_phase = phase;
166 log_debug("\nSTM32 download read %s offset=0x%x\n",
167 dfu_entity->name, offset);
168 ret = dfu_read(dfu_entity, buffer, buffer_size,
169 dfu_entity->i_blk_seq_num);
171 stm32prog_err("DFU read failed [%d] phase = %d offset = 0x%08x",
178 if (size < buffer_size) {
180 data->read_phase = PHASE_END;
181 memset(buffer + size, 0, buffer_size - size);
183 data->offset += size;
189 /* UART access ***************************************************/
190 int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
192 struct udevice *dev = NULL;
193 struct dm_serial_ops *ops;
194 /* no parity, 8 bits, 1 stop */
195 u32 serial_config = SERIAL_DEFAULT_CONFIG;
197 down_serial_dev = NULL;
199 if (uclass_get_device_by_seq(UCLASS_SERIAL, link_dev, &dev)) {
200 log_err("serial %d device not found\n", link_dev);
204 down_serial_dev = dev;
206 /* force silent console on uart only when used */
207 if (gd->cur_serial_dev == down_serial_dev)
208 gd->flags |= GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT;
210 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
212 ops = serial_get_ops(down_serial_dev);
215 log_err("serial %d = %s missing ops\n", link_dev, dev->name);
218 if (!ops->setconfig) {
219 log_err("serial %d = %s missing setconfig\n", link_dev, dev->name);
223 clrsetbits_le32(&serial_config, SERIAL_PAR_MASK, SERIAL_PAR_EVEN);
225 data->buffer = memalign(CONFIG_SYS_CACHELINE_SIZE,
226 USART_RAM_BUFFER_SIZE);
228 return ops->setconfig(down_serial_dev, serial_config);
231 static void stm32prog_serial_flush(void)
233 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
237 err = ops->getc(down_serial_dev);
238 } while (err != -EAGAIN);
241 static int stm32prog_serial_getc_err(void)
243 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
247 err = ops->getc(down_serial_dev);
248 if (err == -EAGAIN) {
252 } while ((err == -EAGAIN) && (!had_ctrlc()));
257 static u8 stm32prog_serial_getc(void)
261 err = stm32prog_serial_getc_err();
263 return err >= 0 ? err : 0;
266 static bool stm32prog_serial_get_buffer(u8 *buffer, u32 *count)
268 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
270 ulong start = get_timer(0);
273 err = ops->getc(down_serial_dev);
277 } else if (err == -EAGAIN) {
280 if (get_timer(start) > TIMEOUT_SERIAL_BUFFER) {
287 } while (*count && !had_ctrlc());
292 static void stm32prog_serial_putc(u8 w_byte)
294 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
298 err = ops->putc(down_serial_dev, w_byte);
299 } while (err == -EAGAIN);
302 /* Helper function ************************************************/
303 static u8 stm32prog_start(struct stm32prog_data *data, u32 address)
306 struct dfu_entity *dfu_entity;
308 if (address < 0x100) {
309 if (address == PHASE_OTP)
310 return stm32prog_otp_start(data);
312 if (address == PHASE_PMIC)
313 return stm32prog_pmic_start(data);
315 if (address == PHASE_RESET || address == PHASE_END) {
316 data->cur_part = NULL;
318 data->phase = address;
321 if (address != data->phase) {
322 stm32prog_err("invalid received phase id %d, current phase is %d",
323 (u8)address, (u8)data->phase);
327 /* check the last loaded partition */
328 if (address == DEFAULT_ADDRESS || address == data->phase) {
329 switch (data->phase) {
333 data->cur_part = NULL;
334 data->phase = PHASE_DO_RESET;
337 dfu_entity = stm32prog_get_entity(data);
341 ret = dfu_flush(dfu_entity, NULL, 0, data->dfu_seq);
343 stm32prog_err("DFU flush failed [%d]", ret);
348 printf("\n received length = 0x%x\n", data->cursor);
350 /* update DFU with received flashlayout */
351 if (data->phase == PHASE_FLASHLAYOUT)
352 stm32prog_dfu_init(data);
354 void (*entry)(void) = (void *)address;
356 printf("## Starting application at 0x%x ...\n", address);
358 printf("## Application terminated\n");
366 * get_address() - Get address if it is valid
368 * @tmp_xor: Current xor value to update
369 * Return: The address area
371 static u32 get_address(u8 *tmp_xor)
376 data = stm32prog_serial_getc();
378 address |= ((u32)data) << 24;
380 data = stm32prog_serial_getc();
381 address |= ((u32)data) << 16;
384 data = stm32prog_serial_getc();
385 address |= ((u32)data) << 8;
388 data = stm32prog_serial_getc();
389 address |= ((u32)data);
395 static void stm32prog_serial_result(u8 result)
397 /* always flush fifo before to send result */
398 stm32prog_serial_flush();
399 stm32prog_serial_putc(result);
402 /* Command -----------------------------------------------*/
404 * get_cmd_command() - Respond to Get command
406 * @data: Current command context
408 static void get_cmd_command(struct stm32prog_data *data)
412 stm32prog_serial_putc(NB_CMD);
413 stm32prog_serial_putc(USART_BL_VERSION);
415 for (counter = 0; counter < NB_CMD; counter++)
416 stm32prog_serial_putc(cmd_id[counter]);
418 stm32prog_serial_result(ACK_BYTE);
422 * get_version_command() - Respond to Get Version command
424 * @data: Current command context
426 static void get_version_command(struct stm32prog_data *data)
428 stm32prog_serial_putc(UBOOT_BL_VERSION);
429 stm32prog_serial_result(ACK_BYTE);
433 * get_id_command() - Respond to Get ID command
435 * @data: Current command context
437 static void get_id_command(struct stm32prog_data *data)
439 u32 cpu = get_cpu_dev();
441 /* Send Device IDCode */
442 stm32prog_serial_putc(0x1);
443 stm32prog_serial_putc((cpu >> 8) & 0xFF);
444 stm32prog_serial_putc(cpu & 0xFF);
445 stm32prog_serial_result(ACK_BYTE);
449 * get_phase_command() - Respond to Get phase
451 * @data: Current command context
453 static void get_phase_command(struct stm32prog_data *data)
455 char *err_msg = NULL;
457 u32 destination = DEFAULT_ADDRESS; /* destination address */
458 int phase = data->phase;
460 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
461 err_msg = stm32prog_get_error(data);
462 length = strlen(err_msg);
464 if (phase == PHASE_FLASHLAYOUT)
465 destination = STM32_DDR_BASE;
467 stm32prog_serial_putc(length + 5); /* Total length */
468 stm32prog_serial_putc(phase & 0xFF); /* partition ID */
469 stm32prog_serial_putc(destination); /* byte 1 of address */
470 stm32prog_serial_putc(destination >> 8); /* byte 2 of address */
471 stm32prog_serial_putc(destination >> 16); /* byte 3 of address */
472 stm32prog_serial_putc(destination >> 24); /* byte 4 of address */
474 stm32prog_serial_putc(length); /* Information length */
475 for (i = 0; i < length; i++)
476 stm32prog_serial_putc(err_msg[i]);
477 stm32prog_serial_result(ACK_BYTE);
479 if (phase == PHASE_RESET)
480 stm32prog_do_reset(data);
484 * read_memory_command() - Read data from memory
486 * @data: Current command context
488 static void read_memory_command(struct stm32prog_data *data)
491 u8 rcv_data = 0x0, tmp_xor = 0x0;
494 /* Read memory address */
495 address = get_address(&tmp_xor);
497 /* If address memory is not received correctly */
498 rcv_data = stm32prog_serial_getc();
499 if (rcv_data != tmp_xor) {
500 stm32prog_serial_result(NACK_BYTE);
504 stm32prog_serial_result(ACK_BYTE);
506 /* Read the number of bytes to be received:
507 * Max NbrOfData = Data + 1 = 256
509 rcv_data = stm32prog_serial_getc();
511 if (stm32prog_serial_getc() != tmp_xor) {
512 stm32prog_serial_result(NACK_BYTE);
516 /* If checksum is correct send ACK */
517 stm32prog_serial_result(ACK_BYTE);
519 /* Send data to the host:
520 * Number of data to read = data + 1
522 for (counter = (rcv_data + 1); counter != 0; counter--)
523 stm32prog_serial_putc(*(u8 *)(address++));
527 * start_command() - Respond to start command
529 * Jump to user application in RAM or partition check
531 * @data: Current command context
533 static void start_command(struct stm32prog_data *data)
539 /* Read memory address */
540 address = get_address(&tmp_xor);
542 /* If address memory is not received correctly */
543 rcv_data = stm32prog_serial_getc();
544 if (rcv_data != tmp_xor) {
545 stm32prog_serial_result(NACK_BYTE);
548 /* validate partition */
549 ret = stm32prog_start(data,
553 stm32prog_serial_result(ABORT_BYTE);
555 stm32prog_serial_result(ACK_BYTE);
559 * download_command() - Respond to download command
561 * Write data to not volatile memory, Flash
563 * @data: Current command context
565 static void download_command(struct stm32prog_data *data)
570 u32 counter = 0x0, codesize = 0x0;
573 u32 cursor = data->cursor;
577 u32 result = ACK_BYTE;
582 address = get_address(&my_xor);
584 /* If address memory is not received correctly */
585 rcv_xor = stm32prog_serial_getc();
586 if (rcv_xor != my_xor) {
591 /* If address valid send ACK */
592 stm32prog_serial_result(ACK_BYTE);
594 /* get packet number and operation type */
595 operation = (u8)((u32)address >> 24);
596 packet_number = ((u32)(((u32)address << 8))) >> 8;
599 /* supported operation */
600 case PHASE_FLASHLAYOUT:
608 /* check the packet number */
609 if (packet_number == 0) {
610 /* erase: re-initialize the image_header struct */
611 data->packet_number = 0;
616 data->packet_number++;
619 /* Check with the number of current packet if the device receive
622 if (packet_number != data->packet_number) {
623 data->packet_number--;
628 /*-- Read number of bytes to be written and data -----------*/
630 /* Read the number of bytes to be written:
631 * Max NbrOfData = data + 1 <= 256
633 rcv_data = stm32prog_serial_getc();
635 /* NbrOfData to write = data + 1 */
636 codesize = rcv_data + 0x01;
638 if (codesize > USART_RAM_BUFFER_SIZE) {
643 /* Checksum Initialization */
646 /* UART receive data and send to Buffer */
648 error = stm32prog_serial_get_buffer(data->buffer, &counter);
652 rcv = stm32prog_serial_getc_err();
658 printf("transmission error on packet %d, byte %d\n",
659 packet_number, codesize - counter);
660 /* waiting end of packet before flush & NACK */
661 mdelay(TIMEOUT_SERIAL_BUFFER);
662 data->packet_number--;
667 /* Compute Checksum */
668 ramaddress = data->buffer;
669 for (counter = codesize; counter != 0; counter--)
670 my_xor ^= *(ramaddress++);
672 /* If Checksum is incorrect */
673 if (rcv_xor != my_xor) {
674 printf("checksum error on packet %d\n",
676 /* wait to be sure that all data are received
677 * in the FIFO before flush
679 mdelay(TIMEOUT_SERIAL_BUFFER);
680 data->packet_number--;
688 ret = stm32prog_otp_write(data, cursor, data->buffer, &size);
693 ret = stm32prog_pmic_write(data, cursor, data->buffer, &size);
697 ret = stm32prog_write(data, data->buffer, codesize);
704 /* Update current position in buffer */
705 data->cursor += codesize;
708 stm32prog_serial_result(result);
712 * read_partition() - Respond to read command
714 * Read data from not volatile memory, Flash
716 * @data: Current command context
718 static void read_partition_command(struct stm32prog_data *data)
720 u32 i, part_id, codesize, offset = 0, rcv_data;
726 part_id = stm32prog_serial_getc();
729 offset = get_address(&tmp_xor);
731 rcv_data = stm32prog_serial_getc();
732 if (rcv_data != tmp_xor) {
733 log_debug("1st checksum received = %x, computed %x\n",
737 stm32prog_serial_putc(ACK_BYTE);
739 /* NbrOfData to read = data + 1 */
740 rcv_data = stm32prog_serial_getc();
741 codesize = rcv_data + 0x01;
744 rcv_data = stm32prog_serial_getc();
745 if ((rcv_data ^ tmp_xor) != 0xFF) {
746 log_debug("2nd checksum received = %x, computed %x\n",
751 log_debug("%s : %x\n", __func__, part_id);
756 if (!stm32prog_otp_read(data, offset, buffer, &size))
761 if (!stm32prog_pmic_read(data, offset, buffer, &size))
765 res = stm32prog_read(data, part_id, offset,
772 stm32prog_serial_putc(ACK_BYTE);
773 /*----------- Send data to the host -----------*/
774 for (i = 0; i < rcv_data; i++)
775 stm32prog_serial_putc(buffer[i]);
776 /*----------- Send filler to the host -----------*/
777 for (; i < codesize; i++)
778 stm32prog_serial_putc(0x0);
781 stm32prog_serial_result(ABORT_BYTE);
785 stm32prog_serial_result(NACK_BYTE);
788 /* MAIN function = SERIAL LOOP ***********************************************/
791 * stm32prog_serial_loop() - USART bootloader Loop routine
793 * @data: Current command context
794 * Return: true if reset is needed after loop
796 bool stm32prog_serial_loop(struct stm32prog_data *data)
801 int phase = data->phase;
803 /* element of cmd_func need to aligned with cmd_id[]*/
804 void (*cmd_func[NB_CMD])(struct stm32prog_data *) = {
805 /* GET_CMD_COMMAND */ get_cmd_command,
806 /* GET_VER_COMMAND */ get_version_command,
807 /* GET_ID_COMMAND */ get_id_command,
808 /* GET_PHASE_COMMAND */ get_phase_command,
809 /* RM_COMMAND */ read_memory_command,
810 /* READ_PART_COMMAND */ read_partition_command,
811 /* START_COMMAND */ start_command,
812 /* DOWNLOAD_COMMAND */ download_command
815 /* flush and NACK pending command received during u-boot init
816 * request command reemit
818 stm32prog_serial_result(NACK_BYTE);
820 clear_ctrlc(); /* forget any previous Control C */
821 while (!had_ctrlc()) {
824 if (phase == PHASE_DO_RESET)
827 /* Get the user command: read first byte */
828 command = stm32prog_serial_getc();
830 if (command == INIT_BYTE) {
831 puts("\nConnected\n");
832 stm32prog_serial_result(ACK_BYTE);
837 for (counter = 0; counter < NB_CMD; counter++)
838 if (cmd_id[counter] == command) {
843 if ((command ^ stm32prog_serial_getc()) != 0xFF)
846 /* wait to be sure that all data are received
847 * in the FIFO before flush (CMD and XOR)
850 stm32prog_serial_result(NACK_BYTE);
852 stm32prog_serial_result(ACK_BYTE);
853 cmd_func[counter](data);
859 if (gd->cur_serial_dev == down_serial_dev) {
860 /* restore console on uart */
861 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
863 down_serial_dev = NULL;
865 return false; /* no reset after ctrlc */