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 <linux/printk.h>
18 #include <asm/global_data.h>
19 #include "stm32prog.h"
21 /* - configuration part -----------------------------*/
22 #define USART_BL_VERSION 0x40 /* USART bootloader version V4.0*/
23 #define UBOOT_BL_VERSION 0x03 /* bootloader version V0.3*/
25 #define USART_RAM_BUFFER_SIZE 256 /* Size of USART_RAM_Buf buffer*/
27 /* - Commands -----------------------------*/
28 #define GET_CMD_COMMAND 0x00 /* Get CMD command*/
29 #define GET_VER_COMMAND 0x01 /* Get Version command*/
30 #define GET_ID_COMMAND 0x02 /* Get ID command*/
31 #define GET_PHASE_COMMAND 0x03 /* Get Phase command*/
32 #define RM_COMMAND 0x11 /* Read Memory command*/
33 #define READ_PART_COMMAND 0x12 /* Read Partition command*/
34 #define START_COMMAND 0x21 /* START command (Go)*/
35 #define DOWNLOAD_COMMAND 0x31 /* Download command*/
36 /* existing command for other STM32 but not used */
38 /* EXTENDED_ERASE 0x44 */
39 /* WRITE_UNPROTECTED 0x73 */
40 /* READOUT_PROTECT 0x82 */
41 /* READOUT_UNPROTECT 0x92 */
43 /* - miscellaneous defines ----------------------------------------*/
44 #define INIT_BYTE 0x7F /*Init Byte ID*/
45 #define ACK_BYTE 0x79 /*Acknowlede Byte ID*/
46 #define NACK_BYTE 0x1F /*No Acknowlede Byte ID*/
47 #define ABORT_BYTE 0x5F /*ABORT*/
49 struct udevice *down_serial_dev;
62 #define NB_CMD sizeof(cmd_id)
64 /* with 115200 bauds, 20 ms allow to receive the 256 bytes buffer */
65 #define TIMEOUT_SERIAL_BUFFER 30
67 /* DFU support for serial *********************************************/
68 static struct dfu_entity *stm32prog_get_entity(struct stm32prog_data *data)
73 if (data->phase == PHASE_FLASHLAYOUT)
78 alt_id = data->cur_part->alt_id;
80 return dfu_get_entity(alt_id);
83 static int stm32prog_write(struct stm32prog_data *data, u8 *buffer,
86 struct dfu_entity *dfu_entity;
89 dfu_entity = stm32prog_get_entity(data);
93 ret = dfu_write(dfu_entity,
99 stm32prog_err("DFU write failed [%d] cnt: %d",
103 /* handle rollover as in driver/dfu/dfu.c */
104 data->dfu_seq &= 0xffff;
105 if (buffer_size == 0)
106 data->dfu_seq = 0; /* flush done */
111 static int stm32prog_read(struct stm32prog_data *data, u8 phase, u32 offset,
112 u8 *buffer, u32 buffer_size)
114 struct dfu_entity *dfu_entity;
115 struct stm32prog_part_t *part;
120 stm32prog_err("DFU write pending for phase %d, seq %d",
121 data->phase, data->dfu_seq);
124 if (phase == PHASE_FLASHLAYOUT || phase > PHASE_LAST_USER) {
125 stm32prog_err("read failed : phase %d is invalid", phase);
128 if (data->read_phase <= PHASE_LAST_USER &&
129 phase != data->read_phase) {
130 /* clear previous read session */
131 dfu_entity = dfu_get_entity(data->read_phase - 1);
133 dfu_transaction_cleanup(dfu_entity);
137 /* found partition for the expected phase */
138 for (i = 0; i < data->part_nb; i++) {
139 part = &data->part_array[i];
140 if (part->id == phase)
141 dfu_entity = dfu_get_entity(part->alt_id);
144 stm32prog_err("read failed : phase %d is unknown", phase);
148 /* clear pending read before to force offset */
149 if (dfu_entity->inited &&
150 (data->read_phase != phase || data->offset != offset))
151 dfu_transaction_cleanup(dfu_entity);
153 /* initiate before to force offset */
154 if (!dfu_entity->inited) {
155 ret = dfu_transaction_initiate(dfu_entity, true);
157 stm32prog_err("DFU read init failed [%d] phase = %d offset = 0x%08x",
162 /* force new offset */
163 if (dfu_entity->offset != offset)
164 dfu_entity->offset = offset;
165 data->offset = offset;
166 data->read_phase = phase;
167 log_debug("\nSTM32 download read %s offset=0x%x\n",
168 dfu_entity->name, offset);
169 ret = dfu_read(dfu_entity, buffer, buffer_size,
170 dfu_entity->i_blk_seq_num);
172 stm32prog_err("DFU read failed [%d] phase = %d offset = 0x%08x",
179 if (size < buffer_size) {
181 data->read_phase = PHASE_END;
182 memset(buffer + size, 0, buffer_size - size);
184 data->offset += size;
190 /* UART access ***************************************************/
191 int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
193 struct udevice *dev = NULL;
194 struct dm_serial_ops *ops;
195 /* no parity, 8 bits, 1 stop */
196 u32 serial_config = SERIAL_DEFAULT_CONFIG;
198 down_serial_dev = NULL;
200 if (uclass_get_device_by_seq(UCLASS_SERIAL, link_dev, &dev)) {
201 log_err("serial %d device not found\n", link_dev);
205 down_serial_dev = dev;
207 /* force silent console on uart only when used */
208 if (gd->cur_serial_dev == down_serial_dev)
209 gd->flags |= GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT;
211 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
213 ops = serial_get_ops(down_serial_dev);
216 log_err("serial %d = %s missing ops\n", link_dev, dev->name);
219 if (!ops->setconfig) {
220 log_err("serial %d = %s missing setconfig\n", link_dev, dev->name);
224 clrsetbits_le32(&serial_config, SERIAL_PAR_MASK, SERIAL_PAR_EVEN);
226 data->buffer = memalign(CONFIG_SYS_CACHELINE_SIZE,
227 USART_RAM_BUFFER_SIZE);
229 return ops->setconfig(down_serial_dev, serial_config);
232 static void stm32prog_serial_flush(void)
234 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
238 err = ops->getc(down_serial_dev);
239 } while (err != -EAGAIN);
242 static int stm32prog_serial_getc_err(void)
244 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
248 err = ops->getc(down_serial_dev);
249 if (err == -EAGAIN) {
253 } while ((err == -EAGAIN) && (!had_ctrlc()));
258 static u8 stm32prog_serial_getc(void)
262 err = stm32prog_serial_getc_err();
264 return err >= 0 ? err : 0;
267 static bool stm32prog_serial_get_buffer(u8 *buffer, u32 *count)
269 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
271 ulong start = get_timer(0);
274 err = ops->getc(down_serial_dev);
278 } else if (err == -EAGAIN) {
281 if (get_timer(start) > TIMEOUT_SERIAL_BUFFER) {
288 } while (*count && !had_ctrlc());
293 static void stm32prog_serial_putc(u8 w_byte)
295 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
299 err = ops->putc(down_serial_dev, w_byte);
300 } while (err == -EAGAIN);
303 /* Helper function ************************************************/
304 static u8 stm32prog_start(struct stm32prog_data *data, uintptr_t address)
307 struct dfu_entity *dfu_entity;
309 if (address < 0x100) {
310 if (address == PHASE_OTP)
311 return stm32prog_otp_start(data);
313 if (address == PHASE_PMIC)
314 return stm32prog_pmic_start(data);
316 if (address == PHASE_RESET || address == PHASE_END) {
317 data->cur_part = NULL;
319 data->phase = address;
322 if (address != data->phase) {
323 stm32prog_err("invalid received phase id %d, current phase is %d",
324 (u8)address, (u8)data->phase);
328 /* check the last loaded partition */
329 if (address == DEFAULT_ADDRESS || address == data->phase) {
330 switch (data->phase) {
334 data->cur_part = NULL;
335 data->phase = PHASE_DO_RESET;
338 dfu_entity = stm32prog_get_entity(data);
342 ret = dfu_flush(dfu_entity, NULL, 0, data->dfu_seq);
344 stm32prog_err("DFU flush failed [%d]", ret);
349 printf("\n received length = 0x%x\n", data->cursor);
351 /* update DFU with received flashlayout */
352 if (data->phase == PHASE_FLASHLAYOUT)
353 stm32prog_dfu_init(data);
355 void (*entry)(void) = (void *)address;
357 printf("## Starting application at 0x%p ...\n", (void *)address);
359 printf("## Application terminated\n");
367 * get_address() - Get address if it is valid
369 * @tmp_xor: Current xor value to update
370 * Return: The address area
372 static uintptr_t get_address(u8 *tmp_xor)
374 uintptr_t address = 0x0;
377 data = stm32prog_serial_getc();
379 address |= ((u32)data) << 24;
381 data = stm32prog_serial_getc();
382 address |= ((u32)data) << 16;
385 data = stm32prog_serial_getc();
386 address |= ((u32)data) << 8;
389 data = stm32prog_serial_getc();
390 address |= ((u32)data);
396 static void stm32prog_serial_result(u8 result)
398 /* always flush fifo before to send result */
399 stm32prog_serial_flush();
400 stm32prog_serial_putc(result);
403 /* Command -----------------------------------------------*/
405 * get_cmd_command() - Respond to Get command
407 * @data: Current command context
409 static void get_cmd_command(struct stm32prog_data *data)
413 stm32prog_serial_putc(NB_CMD);
414 stm32prog_serial_putc(USART_BL_VERSION);
416 for (counter = 0; counter < NB_CMD; counter++)
417 stm32prog_serial_putc(cmd_id[counter]);
419 stm32prog_serial_result(ACK_BYTE);
423 * get_version_command() - Respond to Get Version command
425 * @data: Current command context
427 static void get_version_command(struct stm32prog_data *data)
429 stm32prog_serial_putc(UBOOT_BL_VERSION);
430 stm32prog_serial_result(ACK_BYTE);
434 * get_id_command() - Respond to Get ID command
436 * @data: Current command context
438 static void get_id_command(struct stm32prog_data *data)
440 u32 cpu = get_cpu_dev();
442 /* Send Device IDCode */
443 stm32prog_serial_putc(0x1);
444 stm32prog_serial_putc((cpu >> 8) & 0xFF);
445 stm32prog_serial_putc(cpu & 0xFF);
446 stm32prog_serial_result(ACK_BYTE);
450 * get_phase_command() - Respond to Get phase
452 * @data: Current command context
454 static void get_phase_command(struct stm32prog_data *data)
456 char *err_msg = NULL;
458 u32 destination = DEFAULT_ADDRESS; /* destination address */
459 int phase = data->phase;
461 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
462 err_msg = stm32prog_get_error(data);
463 length = strlen(err_msg);
465 if (phase == PHASE_FLASHLAYOUT)
466 destination = CONFIG_SYS_LOAD_ADDR;
468 stm32prog_serial_putc(length + 5); /* Total length */
469 stm32prog_serial_putc(phase & 0xFF); /* partition ID */
470 stm32prog_serial_putc(destination); /* byte 1 of address */
471 stm32prog_serial_putc(destination >> 8); /* byte 2 of address */
472 stm32prog_serial_putc(destination >> 16); /* byte 3 of address */
473 stm32prog_serial_putc(destination >> 24); /* byte 4 of address */
475 stm32prog_serial_putc(length); /* Information length */
476 for (i = 0; i < length; i++)
477 stm32prog_serial_putc(err_msg[i]);
478 stm32prog_serial_result(ACK_BYTE);
480 if (phase == PHASE_RESET)
481 stm32prog_do_reset(data);
485 * read_memory_command() - Read data from memory
487 * @data: Current command context
489 static void read_memory_command(struct stm32prog_data *data)
491 uintptr_t address = 0x0;
492 u8 rcv_data = 0x0, tmp_xor = 0x0;
495 /* Read memory address */
496 address = get_address(&tmp_xor);
498 /* If address memory is not received correctly */
499 rcv_data = stm32prog_serial_getc();
500 if (rcv_data != tmp_xor) {
501 stm32prog_serial_result(NACK_BYTE);
505 stm32prog_serial_result(ACK_BYTE);
507 /* Read the number of bytes to be received:
508 * Max NbrOfData = Data + 1 = 256
510 rcv_data = stm32prog_serial_getc();
512 if (stm32prog_serial_getc() != tmp_xor) {
513 stm32prog_serial_result(NACK_BYTE);
517 /* If checksum is correct send ACK */
518 stm32prog_serial_result(ACK_BYTE);
520 /* Send data to the host:
521 * Number of data to read = data + 1
523 for (counter = (rcv_data + 1); counter != 0; counter--)
524 stm32prog_serial_putc(*(u8 *)(address++));
528 * start_command() - Respond to start command
530 * Jump to user application in RAM or partition check
532 * @data: Current command context
534 static void start_command(struct stm32prog_data *data)
536 uintptr_t address = 0;
540 /* Read memory address */
541 address = get_address(&tmp_xor);
543 /* If address memory is not received correctly */
544 rcv_data = stm32prog_serial_getc();
545 if (rcv_data != tmp_xor) {
546 stm32prog_serial_result(NACK_BYTE);
549 /* validate partition */
550 ret = stm32prog_start(data, address);
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 */