blob: be38ff239b28025a859106fcac9c96eedd04919b [file] [log] [blame]
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4 */
5
6#include <common.h>
7#include <dfu.h>
8#include <g_dnl.h>
9#include <usb.h>
10#include <asm/arch/stm32prog.h>
11#include <asm/arch/sys_proto.h>
12#include "stm32prog.h"
13
14static int stm32prog_set_phase(struct stm32prog_data *data, u8 phase,
15 u32 offset)
16{
17 struct stm32prog_part_t *part;
18 int i;
19
20 if (phase == data->phase) {
21 data->offset = offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +010022 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010023 return 0;
24 }
25
26 /* found partition for phase */
27 for (i = 0; i < data->part_nb; i++) {
28 part = &data->part_array[i];
29 if (part->id == phase) {
30 data->cur_part = part;
31 data->phase = phase;
32 data->offset = offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +010033 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010034 return 0;
35 }
36 }
37
38 return -EINVAL;
39}
40
41static int stm32prog_cmd_write(u64 offset, void *buf, long *len)
42{
43 u8 phase;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +020044 uintptr_t address;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010045 u8 *pt = buf;
46 void (*entry)(void);
47 int ret;
48
49 if (*len < 5) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010050 log_err("size not allowed\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010051 return -EINVAL;
52 }
53 if (offset) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010054 log_err("invalid offset\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010055 return -EINVAL;
56 }
57 phase = pt[0];
58 address = (pt[1] << 24) | (pt[2] << 16) | (pt[3] << 8) | pt[4];
59 if (phase == PHASE_RESET) {
60 entry = (void *)address;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +020061 printf("## Starting application at 0x%p ...\n", entry);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010062 (*entry)();
63 printf("## Application terminated\n");
64 return 0;
65 }
66 /* set phase and offset */
67 ret = stm32prog_set_phase(stm32prog_data, phase, address);
68 if (ret)
Patrick Delaunay2b15af52020-11-06 19:01:30 +010069 log_err("failed: %d\n", ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010070 return ret;
71}
72
73#define PHASE_MIN_SIZE 9
74static int stm32prog_cmd_read(u64 offset, void *buf, long *len)
75{
76 u32 destination = DEFAULT_ADDRESS; /* destination address */
77 u32 dfu_offset;
78 u8 *pt_buf = buf;
79 int phase;
80 char *err_msg;
81 int length;
82
83 if (*len < PHASE_MIN_SIZE) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010084 log_err("request exceeds allowed area\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010085 return -EINVAL;
86 }
87 if (offset) {
88 *len = 0; /* EOF for second request */
89 return 0;
90 }
91 phase = stm32prog_data->phase;
92 if (phase == PHASE_FLASHLAYOUT)
Patrick Delaunaye334d322022-09-06 18:53:18 +020093 destination = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010094 dfu_offset = stm32prog_data->offset;
95
96 /* mandatory header, size = PHASE_MIN_SIZE */
97 *pt_buf++ = (u8)(phase & 0xFF);
98 *pt_buf++ = (u8)(destination);
99 *pt_buf++ = (u8)(destination >> 8);
100 *pt_buf++ = (u8)(destination >> 16);
101 *pt_buf++ = (u8)(destination >> 24);
102 *pt_buf++ = (u8)(dfu_offset);
103 *pt_buf++ = (u8)(dfu_offset >> 8);
104 *pt_buf++ = (u8)(dfu_offset >> 16);
105 *pt_buf++ = (u8)(dfu_offset >> 24);
106
107 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
108 err_msg = stm32prog_get_error(stm32prog_data);
109 length = strlen(err_msg);
110 if (length + PHASE_MIN_SIZE > *len)
111 length = *len - PHASE_MIN_SIZE;
112
113 memcpy(pt_buf, err_msg, length);
114 *len = PHASE_MIN_SIZE + length;
115 stm32prog_do_reset(stm32prog_data);
116 } else if (phase == PHASE_FLASHLAYOUT) {
117 *pt_buf++ = stm32prog_data->part_nb ? 1 : 0;
118 *len = PHASE_MIN_SIZE + 1;
119 } else {
120 *len = PHASE_MIN_SIZE;
121 }
122
123 return 0;
124}
125
126int stm32prog_write_medium_virt(struct dfu_entity *dfu, u64 offset,
127 void *buf, long *len)
128{
129 if (dfu->dev_type != DFU_DEV_VIRT)
130 return -EINVAL;
131
132 switch (dfu->data.virt.dev_num) {
133 case PHASE_CMD:
134 return stm32prog_cmd_write(offset, buf, len);
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100135
136 case PHASE_OTP:
137 return stm32prog_otp_write(stm32prog_data, (u32)offset,
138 buf, len);
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100139
140 case PHASE_PMIC:
141 return stm32prog_pmic_write(stm32prog_data, (u32)offset,
142 buf, len);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100143 }
144 *len = 0;
145 return 0;
146}
147
148int stm32prog_read_medium_virt(struct dfu_entity *dfu, u64 offset,
149 void *buf, long *len)
150{
151 if (dfu->dev_type != DFU_DEV_VIRT)
152 return -EINVAL;
153
154 switch (dfu->data.virt.dev_num) {
155 case PHASE_CMD:
156 return stm32prog_cmd_read(offset, buf, len);
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100157
158 case PHASE_OTP:
159 return stm32prog_otp_read(stm32prog_data, (u32)offset,
160 buf, len);
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100161
162 case PHASE_PMIC:
163 return stm32prog_pmic_read(stm32prog_data, (u32)offset,
164 buf, len);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100165 }
166 *len = 0;
167 return 0;
168}
169
170int stm32prog_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
171{
172 if (dfu->dev_type != DFU_DEV_VIRT) {
173 *size = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100174 log_debug("%s, invalid dev_type = %d\n",
175 __func__, dfu->dev_type);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100176 return -EINVAL;
177 }
178
179 switch (dfu->data.virt.dev_num) {
180 case PHASE_CMD:
Patrick Delaunaybd577492021-07-05 09:39:01 +0200181 *size = CMD_SIZE;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100182 break;
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100183 case PHASE_OTP:
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200184 *size = stm32prog_data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100185 break;
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100186 case PHASE_PMIC:
187 *size = PMIC_SIZE;
188 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100189 }
190
191 return 0;
192}
193
194bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
195{
196 int ret;
197 bool result;
198 /* USB download gadget for STM32 Programmer */
199 char product[128];
Patrick Delaunaydd178382020-06-16 18:25:14 +0200200 char name[SOC_NAME_SIZE];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100201
Patrick Delaunaydd178382020-06-16 18:25:14 +0200202 get_soc_name(name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100203 snprintf(product, sizeof(product),
Patrick Delaunaydd178382020-06-16 18:25:14 +0200204 "USB download gadget@Device ID /0x%03X, @Revision ID /0x%04X, @Name /%s,",
205 get_cpu_dev(), get_cpu_rev(), name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100206 g_dnl_set_product(product);
207
208 if (stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunay0f0daba2022-03-28 19:25:31 +0200209 /* forget any previous Control C */
210 clear_ctrlc();
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100211 ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
Patrick Delaunay0f0daba2022-03-28 19:25:31 +0200212 /* DFU reset received, no error or CtrlC */
213 if (ret || stm32prog_data->phase != PHASE_FLASHLAYOUT || had_ctrlc())
214 return ret; /* true = reset on DFU error */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100215 /* prepare the second enumeration with the FlashLayout */
Patrick Delaunaye23d98e2021-05-18 15:12:08 +0200216 stm32prog_dfu_init(data);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100217 }
218
219 ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
220
221 result = !!(ret) || (stm32prog_data->phase == PHASE_DO_RESET);
222
223 g_dnl_set_product(NULL);
224
225 return result;
226}
227
228int g_dnl_get_board_bcd_device_number(int gcnum)
229{
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100230 log_debug("%s\n", __func__);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100231 return 0x200;
232}