blob: d18455bf36f1d90f7b3a6ec014ff735eff7f2869 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010013#include "stm32prog.h"
14
15static int stm32prog_set_phase(struct stm32prog_data *data, u8 phase,
16 u32 offset)
17{
18 struct stm32prog_part_t *part;
19 int i;
20
21 if (phase == data->phase) {
22 data->offset = offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +010023 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010024 return 0;
25 }
26
27 /* found partition for phase */
28 for (i = 0; i < data->part_nb; i++) {
29 part = &data->part_array[i];
30 if (part->id == phase) {
31 data->cur_part = part;
32 data->phase = phase;
33 data->offset = offset;
Patrick Delaunayb823d992020-03-18 09:25:00 +010034 data->dfu_seq = 0;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010035 return 0;
36 }
37 }
38
39 return -EINVAL;
40}
41
42static int stm32prog_cmd_write(u64 offset, void *buf, long *len)
43{
44 u8 phase;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +020045 uintptr_t address;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010046 u8 *pt = buf;
47 void (*entry)(void);
48 int ret;
49
50 if (*len < 5) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010051 log_err("size not allowed\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010052 return -EINVAL;
53 }
54 if (offset) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010055 log_err("invalid offset\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010056 return -EINVAL;
57 }
58 phase = pt[0];
59 address = (pt[1] << 24) | (pt[2] << 16) | (pt[3] << 8) | pt[4];
60 if (phase == PHASE_RESET) {
61 entry = (void *)address;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +020062 printf("## Starting application at 0x%p ...\n", entry);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010063 (*entry)();
64 printf("## Application terminated\n");
65 return 0;
66 }
67 /* set phase and offset */
68 ret = stm32prog_set_phase(stm32prog_data, phase, address);
69 if (ret)
Patrick Delaunay2b15af52020-11-06 19:01:30 +010070 log_err("failed: %d\n", ret);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010071 return ret;
72}
73
74#define PHASE_MIN_SIZE 9
75static int stm32prog_cmd_read(u64 offset, void *buf, long *len)
76{
77 u32 destination = DEFAULT_ADDRESS; /* destination address */
78 u32 dfu_offset;
79 u8 *pt_buf = buf;
80 int phase;
81 char *err_msg;
82 int length;
83
84 if (*len < PHASE_MIN_SIZE) {
Patrick Delaunay2b15af52020-11-06 19:01:30 +010085 log_err("request exceeds allowed area\n");
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010086 return -EINVAL;
87 }
88 if (offset) {
89 *len = 0; /* EOF for second request */
90 return 0;
91 }
92 phase = stm32prog_data->phase;
93 if (phase == PHASE_FLASHLAYOUT)
Patrick Delaunaye334d322022-09-06 18:53:18 +020094 destination = CONFIG_SYS_LOAD_ADDR;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010095 dfu_offset = stm32prog_data->offset;
96
97 /* mandatory header, size = PHASE_MIN_SIZE */
98 *pt_buf++ = (u8)(phase & 0xFF);
99 *pt_buf++ = (u8)(destination);
100 *pt_buf++ = (u8)(destination >> 8);
101 *pt_buf++ = (u8)(destination >> 16);
102 *pt_buf++ = (u8)(destination >> 24);
103 *pt_buf++ = (u8)(dfu_offset);
104 *pt_buf++ = (u8)(dfu_offset >> 8);
105 *pt_buf++ = (u8)(dfu_offset >> 16);
106 *pt_buf++ = (u8)(dfu_offset >> 24);
107
108 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
109 err_msg = stm32prog_get_error(stm32prog_data);
110 length = strlen(err_msg);
111 if (length + PHASE_MIN_SIZE > *len)
112 length = *len - PHASE_MIN_SIZE;
113
114 memcpy(pt_buf, err_msg, length);
115 *len = PHASE_MIN_SIZE + length;
116 stm32prog_do_reset(stm32prog_data);
117 } else if (phase == PHASE_FLASHLAYOUT) {
118 *pt_buf++ = stm32prog_data->part_nb ? 1 : 0;
119 *len = PHASE_MIN_SIZE + 1;
120 } else {
121 *len = PHASE_MIN_SIZE;
122 }
123
124 return 0;
125}
126
127int stm32prog_write_medium_virt(struct dfu_entity *dfu, u64 offset,
128 void *buf, long *len)
129{
130 if (dfu->dev_type != DFU_DEV_VIRT)
131 return -EINVAL;
132
133 switch (dfu->data.virt.dev_num) {
134 case PHASE_CMD:
135 return stm32prog_cmd_write(offset, buf, len);
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100136
137 case PHASE_OTP:
138 return stm32prog_otp_write(stm32prog_data, (u32)offset,
139 buf, len);
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100140
141 case PHASE_PMIC:
142 return stm32prog_pmic_write(stm32prog_data, (u32)offset,
143 buf, len);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100144 }
145 *len = 0;
146 return 0;
147}
148
149int stm32prog_read_medium_virt(struct dfu_entity *dfu, u64 offset,
150 void *buf, long *len)
151{
152 if (dfu->dev_type != DFU_DEV_VIRT)
153 return -EINVAL;
154
155 switch (dfu->data.virt.dev_num) {
156 case PHASE_CMD:
157 return stm32prog_cmd_read(offset, buf, len);
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100158
159 case PHASE_OTP:
160 return stm32prog_otp_read(stm32prog_data, (u32)offset,
161 buf, len);
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100162
163 case PHASE_PMIC:
164 return stm32prog_pmic_read(stm32prog_data, (u32)offset,
165 buf, len);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100166 }
167 *len = 0;
168 return 0;
169}
170
171int stm32prog_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
172{
173 if (dfu->dev_type != DFU_DEV_VIRT) {
174 *size = 0;
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100175 log_debug("%s, invalid dev_type = %d\n",
176 __func__, dfu->dev_type);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100177 return -EINVAL;
178 }
179
180 switch (dfu->data.virt.dev_num) {
181 case PHASE_CMD:
Patrick Delaunaybd577492021-07-05 09:39:01 +0200182 *size = CMD_SIZE;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100183 break;
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100184 case PHASE_OTP:
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200185 *size = stm32prog_data->tee ? OTP_SIZE_TA : OTP_SIZE_SMC;
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100186 break;
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100187 case PHASE_PMIC:
188 *size = PMIC_SIZE;
189 break;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100190 }
191
192 return 0;
193}
194
195bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
196{
197 int ret;
198 bool result;
199 /* USB download gadget for STM32 Programmer */
200 char product[128];
Patrick Delaunaydd178382020-06-16 18:25:14 +0200201 char name[SOC_NAME_SIZE];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100202
Patrick Delaunaydd178382020-06-16 18:25:14 +0200203 get_soc_name(name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100204 snprintf(product, sizeof(product),
Patrick Delaunaydd178382020-06-16 18:25:14 +0200205 "USB download gadget@Device ID /0x%03X, @Revision ID /0x%04X, @Name /%s,",
206 get_cpu_dev(), get_cpu_rev(), name);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100207 g_dnl_set_product(product);
208
209 if (stm32prog_data->phase == PHASE_FLASHLAYOUT) {
Patrick Delaunay0f0daba2022-03-28 19:25:31 +0200210 /* forget any previous Control C */
211 clear_ctrlc();
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100212 ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
Patrick Delaunay0f0daba2022-03-28 19:25:31 +0200213 /* DFU reset received, no error or CtrlC */
214 if (ret || stm32prog_data->phase != PHASE_FLASHLAYOUT || had_ctrlc())
215 return ret; /* true = reset on DFU error */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100216 /* prepare the second enumeration with the FlashLayout */
Patrick Delaunaye23d98e2021-05-18 15:12:08 +0200217 stm32prog_dfu_init(data);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100218 }
219
220 ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
221
222 result = !!(ret) || (stm32prog_data->phase == PHASE_DO_RESET);
223
224 g_dnl_set_product(NULL);
225
226 return result;
227}
228
229int g_dnl_get_board_bcd_device_number(int gcnum)
230{
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100231 log_debug("%s\n", __func__);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100232 return 0x200;
233}