2 * Copyright (C) 2009 by Marvell Semiconductors, Inc.
3 * Written by Nicolas Pitre <nico at marvell.com>
5 * Copyright (C) 2009 by David Brownell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the
19 * Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <helper/binarybuffer.h>
30 #include <target/arm.h>
31 #include <target/armv7m.h>
32 #include <target/algorithm.h>
35 * Copies code to a working area. This will allocate room for the code plus the
36 * additional amount requested if the working area pointer is null.
38 * @param target Pointer to the target to copy code to
39 * @param code Pointer to the code area to be copied
40 * @param code_size Size of the code being copied
41 * @param additional Size of the additional area to be allocated in addition to
43 * @param area Pointer to a pointer to a working area to copy code to
44 * @return Success or failure of the operation
46 static int arm_code_to_working_area(struct target
*target
,
47 const uint32_t *code
, unsigned code_size
,
48 unsigned additional
, struct working_area
**area
)
50 uint8_t code_buf
[code_size
];
53 unsigned size
= code_size
+ additional
;
55 /* REVISIT this assumes size doesn't ever change.
56 * That's usually correct; but there are boards with
57 * both large and small page chips, where it won't be...
60 /* make sure we have a working area */
62 retval
= target_alloc_working_area(target
, size
, area
);
63 if (retval
!= ERROR_OK
) {
64 LOG_DEBUG("%s: no %d byte buffer", __func__
, (int) size
);
65 return ERROR_NAND_NO_BUFFER
;
69 /* buffer code in target endianness */
70 for (i
= 0; i
< code_size
/ 4; i
++)
71 target_buffer_set_u32(target
, code_buf
+ i
* 4, code
[i
]);
73 /* copy code to work area */
74 retval
= target_write_memory(target
, (*area
)->address
,
75 4, code_size
/ 4, code_buf
);
81 * ARM-specific bulk write from buffer to address of 8-bit wide NAND.
82 * For now this supports ARMv4,ARMv5 and ARMv7-M cores.
84 * Enhancements to target_run_algorithm() could enable:
85 * - ARMv6 and ARMv7 cores in ARM mode
87 * Different code fragments could handle:
88 * - 16-bit wide data (needs different setup)
90 * @param nand Pointer to the arm_nand_data struct that defines the I/O
91 * @param data Pointer to the data to be copied to flash
92 * @param size Size of the data being copied
93 * @return Success or failure of the operation
95 int arm_nandwrite(struct arm_nand_data
*nand
, uint8_t *data
, int size
)
97 struct target
*target
= nand
->target
;
98 struct arm_algorithm armv4_5_algo
;
99 struct armv7m_algorithm armv7m_algo
;
101 struct arm
*arm
= target
->arch_info
;
102 struct reg_param reg_params
[3];
104 uint32_t exit_var
= 0;
108 * r0 NAND data address (byte wide)
112 static const uint32_t code_armv4_5
[] = {
113 0xe4d13001, /* s: ldrb r3, [r1], #1 */
114 0xe5c03000, /* strb r3, [r0] */
115 0xe2522001, /* subs r2, r2, #1 */
116 0x1afffffb, /* bne s */
118 /* exit: ARMv4 needs hardware breakpoint */
119 0xe1200070, /* e: bkpt #0 */
123 * r0 NAND data address (byte wide)
127 * see contrib/loaders/flash/armv7m_io.s for src
129 static const uint32_t code_armv7m
[] = {
136 int target_code_size
= 0;
137 const uint32_t *target_code_src
= NULL
;
139 /* set up algorithm */
140 if (is_armv7m(target_to_armv7m(target
))) { /* armv7m target */
141 armv7m_algo
.common_magic
= ARMV7M_COMMON_MAGIC
;
142 armv7m_algo
.core_mode
= ARM_MODE_THREAD
;
143 arm_algo
= &armv7m_algo
;
144 target_code_size
= sizeof(code_armv7m
);
145 target_code_src
= code_armv7m
;
147 armv4_5_algo
.common_magic
= ARM_COMMON_MAGIC
;
148 armv4_5_algo
.core_mode
= ARM_MODE_SVC
;
149 armv4_5_algo
.core_state
= ARM_STATE_ARM
;
150 arm_algo
= &armv4_5_algo
;
151 target_code_size
= sizeof(code_armv4_5
);
152 target_code_src
= code_armv4_5
;
155 if (nand
->op
!= ARM_NAND_WRITE
|| !nand
->copy_area
) {
156 retval
= arm_code_to_working_area(target
, target_code_src
, target_code_size
,
157 nand
->chunk_size
, &nand
->copy_area
);
158 if (retval
!= ERROR_OK
)
162 nand
->op
= ARM_NAND_WRITE
;
164 /* copy data to work area */
165 target_buf
= nand
->copy_area
->address
+ target_code_size
;
166 retval
= target_write_buffer(target
, target_buf
, size
, data
);
167 if (retval
!= ERROR_OK
)
170 /* set up parameters */
171 init_reg_param(®_params
[0], "r0", 32, PARAM_IN
);
172 init_reg_param(®_params
[1], "r1", 32, PARAM_IN
);
173 init_reg_param(®_params
[2], "r2", 32, PARAM_IN
);
175 buf_set_u32(reg_params
[0].value
, 0, 32, nand
->data
);
176 buf_set_u32(reg_params
[1].value
, 0, 32, target_buf
);
177 buf_set_u32(reg_params
[2].value
, 0, 32, size
);
179 /* armv4 must exit using a hardware breakpoint */
181 exit_var
= nand
->copy_area
->address
+ target_code_size
- 4;
183 /* use alg to write data from work area to NAND chip */
184 retval
= target_run_algorithm(target
, 0, NULL
, 3, reg_params
,
185 nand
->copy_area
->address
, exit_var
, 1000, arm_algo
);
186 if (retval
!= ERROR_OK
)
187 LOG_ERROR("error executing hosted NAND write");
189 destroy_reg_param(®_params
[0]);
190 destroy_reg_param(®_params
[1]);
191 destroy_reg_param(®_params
[2]);
197 * Uses an on-chip algorithm for an ARM device to read from a NAND device and
198 * store the data into the host machine's memory.
200 * @param nand Pointer to the arm_nand_data struct that defines the I/O
201 * @param data Pointer to the data buffer to store the read data
202 * @param size Amount of data to be stored to the buffer.
203 * @return Success or failure of the operation
205 int arm_nandread(struct arm_nand_data
*nand
, uint8_t *data
, uint32_t size
)
207 struct target
*target
= nand
->target
;
208 struct arm_algorithm armv4_5_algo
;
209 struct armv7m_algorithm armv7m_algo
;
211 struct arm
*arm
= target
->arch_info
;
212 struct reg_param reg_params
[3];
214 uint32_t exit_var
= 0;
219 * r1 NAND data address (byte wide)
222 static const uint32_t code_armv4_5
[] = {
223 0xe5d13000, /* s: ldrb r3, [r1] */
224 0xe4c03001, /* strb r3, [r0], #1 */
225 0xe2522001, /* subs r2, r2, #1 */
226 0x1afffffb, /* bne s */
228 /* exit: ARMv4 needs hardware breakpoint */
229 0xe1200070, /* e: bkpt #0 */
234 * r1 NAND data address (byte wide)
237 * see contrib/loaders/flash/armv7m_io.s for src
239 static const uint32_t code_armv7m
[] = {
246 int target_code_size
= 0;
247 const uint32_t *target_code_src
= NULL
;
249 /* set up algorithm */
250 if (is_armv7m(target_to_armv7m(target
))) { /* armv7m target */
251 armv7m_algo
.common_magic
= ARMV7M_COMMON_MAGIC
;
252 armv7m_algo
.core_mode
= ARM_MODE_THREAD
;
253 arm_algo
= &armv7m_algo
;
254 target_code_size
= sizeof(code_armv7m
);
255 target_code_src
= code_armv7m
;
257 armv4_5_algo
.common_magic
= ARM_COMMON_MAGIC
;
258 armv4_5_algo
.core_mode
= ARM_MODE_SVC
;
259 armv4_5_algo
.core_state
= ARM_STATE_ARM
;
260 arm_algo
= &armv4_5_algo
;
261 target_code_size
= sizeof(code_armv4_5
);
262 target_code_src
= code_armv4_5
;
265 /* create the copy area if not yet available */
266 if (nand
->op
!= ARM_NAND_READ
|| !nand
->copy_area
) {
267 retval
= arm_code_to_working_area(target
, target_code_src
, target_code_size
,
268 nand
->chunk_size
, &nand
->copy_area
);
269 if (retval
!= ERROR_OK
)
273 nand
->op
= ARM_NAND_READ
;
274 target_buf
= nand
->copy_area
->address
+ target_code_size
;
276 /* set up parameters */
277 init_reg_param(®_params
[0], "r0", 32, PARAM_IN
);
278 init_reg_param(®_params
[1], "r1", 32, PARAM_IN
);
279 init_reg_param(®_params
[2], "r2", 32, PARAM_IN
);
281 buf_set_u32(reg_params
[0].value
, 0, 32, target_buf
);
282 buf_set_u32(reg_params
[1].value
, 0, 32, nand
->data
);
283 buf_set_u32(reg_params
[2].value
, 0, 32, size
);
285 /* armv4 must exit using a hardware breakpoint */
287 exit_var
= nand
->copy_area
->address
+ target_code_size
- 4;
289 /* use alg to write data from NAND chip to work area */
290 retval
= target_run_algorithm(target
, 0, NULL
, 3, reg_params
,
291 nand
->copy_area
->address
, exit_var
, 1000, arm_algo
);
292 if (retval
!= ERROR_OK
)
293 LOG_ERROR("error executing hosted NAND read");
295 destroy_reg_param(®_params
[0]);
296 destroy_reg_param(®_params
[1]);
297 destroy_reg_param(®_params
[2]);
299 /* read from work area to the host's memory */
300 retval
= target_read_buffer(target
, target_buf
, size
, data
);