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, see <http://www.gnu.org/licenses/>.
27 #include <helper/binarybuffer.h>
28 #include <target/arm.h>
29 #include <target/armv7m.h>
30 #include <target/algorithm.h>
33 * Copies code to a working area. This will allocate room for the code plus the
34 * additional amount requested if the working area pointer is null.
36 * @param target Pointer to the target to copy code to
37 * @param code Pointer to the code area to be copied
38 * @param code_size Size of the code being copied
39 * @param additional Size of the additional area to be allocated in addition to
41 * @param area Pointer to a pointer to a working area to copy code to
42 * @return Success or failure of the operation
44 static int arm_code_to_working_area(struct target *target,
45 const uint32_t *code, unsigned code_size,
46 unsigned additional, struct working_area **area)
48 uint8_t code_buf[code_size];
50 unsigned size = code_size + additional;
52 /* REVISIT this assumes size doesn't ever change.
53 * That's usually correct; but there are boards with
54 * both large and small page chips, where it won't be...
57 /* make sure we have a working area */
59 retval = target_alloc_working_area(target, size, area);
60 if (retval != ERROR_OK) {
61 LOG_DEBUG("%s: no %d byte buffer", __func__, (int) size);
62 return ERROR_NAND_NO_BUFFER;
66 /* buffer code in target endianness */
67 target_buffer_set_u32_array(target, code_buf, code_size / 4, code);
69 /* copy code to work area */
70 retval = target_write_memory(target, (*area)->address,
71 4, code_size / 4, code_buf);
77 * ARM-specific bulk write from buffer to address of 8-bit wide NAND.
78 * For now this supports ARMv4,ARMv5 and ARMv7-M cores.
80 * Enhancements to target_run_algorithm() could enable:
81 * - ARMv6 and ARMv7 cores in ARM mode
83 * Different code fragments could handle:
84 * - 16-bit wide data (needs different setup)
86 * @param nand Pointer to the arm_nand_data struct that defines the I/O
87 * @param data Pointer to the data to be copied to flash
88 * @param size Size of the data being copied
89 * @return Success or failure of the operation
91 int arm_nandwrite(struct arm_nand_data *nand, uint8_t *data, int size)
93 struct target *target = nand->target;
94 struct arm_algorithm armv4_5_algo;
95 struct armv7m_algorithm armv7m_algo;
97 struct arm *arm = target->arch_info;
98 struct reg_param reg_params[3];
100 uint32_t exit_var = 0;
104 * r0 NAND data address (byte wide)
108 static const uint32_t code_armv4_5[] = {
109 0xe4d13001, /* s: ldrb r3, [r1], #1 */
110 0xe5c03000, /* strb r3, [r0] */
111 0xe2522001, /* subs r2, r2, #1 */
112 0x1afffffb, /* bne s */
114 /* exit: ARMv4 needs hardware breakpoint */
115 0xe1200070, /* e: bkpt #0 */
119 * r0 NAND data address (byte wide)
123 * see contrib/loaders/flash/armv7m_io.s for src
125 static const uint32_t code_armv7m[] = {
132 int target_code_size = 0;
133 const uint32_t *target_code_src = NULL;
135 /* set up algorithm */
136 if (is_armv7m(target_to_armv7m(target))) { /* armv7m target */
137 armv7m_algo.common_magic = ARMV7M_COMMON_MAGIC;
138 armv7m_algo.core_mode = ARM_MODE_THREAD;
139 arm_algo = &armv7m_algo;
140 target_code_size = sizeof(code_armv7m);
141 target_code_src = code_armv7m;
143 armv4_5_algo.common_magic = ARM_COMMON_MAGIC;
144 armv4_5_algo.core_mode = ARM_MODE_SVC;
145 armv4_5_algo.core_state = ARM_STATE_ARM;
146 arm_algo = &armv4_5_algo;
147 target_code_size = sizeof(code_armv4_5);
148 target_code_src = code_armv4_5;
151 if (nand->op != ARM_NAND_WRITE || !nand->copy_area) {
152 retval = arm_code_to_working_area(target, target_code_src, target_code_size,
153 nand->chunk_size, &nand->copy_area);
154 if (retval != ERROR_OK)
158 nand->op = ARM_NAND_WRITE;
160 /* copy data to work area */
161 target_buf = nand->copy_area->address + target_code_size;
162 retval = target_write_buffer(target, target_buf, size, data);
163 if (retval != ERROR_OK)
166 /* set up parameters */
167 init_reg_param(®_params[0], "r0", 32, PARAM_IN);
168 init_reg_param(®_params[1], "r1", 32, PARAM_IN);
169 init_reg_param(®_params[2], "r2", 32, PARAM_IN);
171 buf_set_u32(reg_params[0].value, 0, 32, nand->data);
172 buf_set_u32(reg_params[1].value, 0, 32, target_buf);
173 buf_set_u32(reg_params[2].value, 0, 32, size);
175 /* armv4 must exit using a hardware breakpoint */
176 if (arm->arch == ARM_ARCH_V4)
177 exit_var = nand->copy_area->address + target_code_size - 4;
179 /* use alg to write data from work area to NAND chip */
180 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
181 nand->copy_area->address, exit_var, 1000, arm_algo);
182 if (retval != ERROR_OK)
183 LOG_ERROR("error executing hosted NAND write");
185 destroy_reg_param(®_params[0]);
186 destroy_reg_param(®_params[1]);
187 destroy_reg_param(®_params[2]);
193 * Uses an on-chip algorithm for an ARM device to read from a NAND device and
194 * store the data into the host machine's memory.
196 * @param nand Pointer to the arm_nand_data struct that defines the I/O
197 * @param data Pointer to the data buffer to store the read data
198 * @param size Amount of data to be stored to the buffer.
199 * @return Success or failure of the operation
201 int arm_nandread(struct arm_nand_data *nand, uint8_t *data, uint32_t size)
203 struct target *target = nand->target;
204 struct arm_algorithm armv4_5_algo;
205 struct armv7m_algorithm armv7m_algo;
207 struct arm *arm = target->arch_info;
208 struct reg_param reg_params[3];
210 uint32_t exit_var = 0;
215 * r1 NAND data address (byte wide)
218 static const uint32_t code_armv4_5[] = {
219 0xe5d13000, /* s: ldrb r3, [r1] */
220 0xe4c03001, /* strb r3, [r0], #1 */
221 0xe2522001, /* subs r2, r2, #1 */
222 0x1afffffb, /* bne s */
224 /* exit: ARMv4 needs hardware breakpoint */
225 0xe1200070, /* e: bkpt #0 */
230 * r1 NAND data address (byte wide)
233 * see contrib/loaders/flash/armv7m_io.s for src
235 static const uint32_t code_armv7m[] = {
242 int target_code_size = 0;
243 const uint32_t *target_code_src = NULL;
245 /* set up algorithm */
246 if (is_armv7m(target_to_armv7m(target))) { /* armv7m target */
247 armv7m_algo.common_magic = ARMV7M_COMMON_MAGIC;
248 armv7m_algo.core_mode = ARM_MODE_THREAD;
249 arm_algo = &armv7m_algo;
250 target_code_size = sizeof(code_armv7m);
251 target_code_src = code_armv7m;
253 armv4_5_algo.common_magic = ARM_COMMON_MAGIC;
254 armv4_5_algo.core_mode = ARM_MODE_SVC;
255 armv4_5_algo.core_state = ARM_STATE_ARM;
256 arm_algo = &armv4_5_algo;
257 target_code_size = sizeof(code_armv4_5);
258 target_code_src = code_armv4_5;
261 /* create the copy area if not yet available */
262 if (nand->op != ARM_NAND_READ || !nand->copy_area) {
263 retval = arm_code_to_working_area(target, target_code_src, target_code_size,
264 nand->chunk_size, &nand->copy_area);
265 if (retval != ERROR_OK)
269 nand->op = ARM_NAND_READ;
270 target_buf = nand->copy_area->address + target_code_size;
272 /* set up parameters */
273 init_reg_param(®_params[0], "r0", 32, PARAM_IN);
274 init_reg_param(®_params[1], "r1", 32, PARAM_IN);
275 init_reg_param(®_params[2], "r2", 32, PARAM_IN);
277 buf_set_u32(reg_params[0].value, 0, 32, target_buf);
278 buf_set_u32(reg_params[1].value, 0, 32, nand->data);
279 buf_set_u32(reg_params[2].value, 0, 32, size);
281 /* armv4 must exit using a hardware breakpoint */
282 if (arm->arch == ARM_ARCH_V4)
283 exit_var = nand->copy_area->address + target_code_size - 4;
285 /* use alg to write data from NAND chip to work area */
286 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
287 nand->copy_area->address, exit_var, 1000, arm_algo);
288 if (retval != ERROR_OK)
289 LOG_ERROR("error executing hosted NAND read");
291 destroy_reg_param(®_params[0]);
292 destroy_reg_param(®_params[1]);
293 destroy_reg_param(®_params[2]);
295 /* read from work area to the host's memory */
296 retval = target_read_buffer(target, target_buf, size, data);