cf494766dddb46744e7db6f92a2ac482adbe24d1
[openocd.git] / src / flash / nand / arm_io.c
1 /*
2 * Copyright (C) 2009 by Marvell Semiconductors, Inc.
3 * Written by Nicolas Pitre <nico at marvell.com>
4 *
5 * Copyright (C) 2009 by David Brownell
6 *
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.
11 *
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.
16 *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "core.h"
28 #include "arm_io.h"
29 #include <helper/binarybuffer.h>
30 #include <target/arm.h>
31 #include <target/algorithm.h>
32
33 /**
34 * Copies code to a working area. This will allocate room for the code plus the
35 * additional amount requested if the working area pointer is null.
36 *
37 * @param target Pointer to the target to copy code to
38 * @param code Pointer to the code area to be copied
39 * @param code_size Size of the code being copied
40 * @param additional Size of the additional area to be allocated in addition to
41 * code
42 * @param area Pointer to a pointer to a working area to copy code to
43 * @return Success or failure of the operation
44 */
45 static int arm_code_to_working_area(struct target *target,
46 const uint32_t *code, unsigned code_size,
47 unsigned additional, struct working_area **area)
48 {
49 uint8_t code_buf[code_size];
50 unsigned i;
51 int retval;
52 unsigned size = code_size + additional;
53
54 /* REVISIT this assumes size doesn't ever change.
55 * That's usually correct; but there are boards with
56 * both large and small page chips, where it won't be...
57 */
58
59 /* make sure we have a working area */
60 if (NULL == *area) {
61 retval = target_alloc_working_area(target, size, area);
62 if (retval != ERROR_OK) {
63 LOG_DEBUG("%s: no %d byte buffer", __func__, (int) size);
64 return ERROR_NAND_NO_BUFFER;
65 }
66 }
67
68 /* buffer code in target endianness */
69 for (i = 0; i < code_size / 4; i++)
70 target_buffer_set_u32(target, code_buf + i * 4, code[i]);
71
72 /* copy code to work area */
73 retval = target_write_memory(target, (*area)->address,
74 4, code_size / 4, code_buf);
75
76 return retval;
77 }
78
79 /**
80 * ARM-specific bulk write from buffer to address of 8-bit wide NAND.
81 * For now this only supports ARMv4 and ARMv5 cores.
82 *
83 * Enhancements to target_run_algorithm() could enable:
84 * - ARMv6 and ARMv7 cores in ARM mode
85 *
86 * Different code fragments could handle:
87 * - Thumb2 cores like Cortex-M (needs different byteswapping)
88 * - 16-bit wide data (needs different setup too)
89 *
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
94 */
95 int arm_nandwrite(struct arm_nand_data *nand, uint8_t *data, int size)
96 {
97 struct target *target = nand->target;
98 struct arm_algorithm algo;
99 struct arm *arm = target->arch_info;
100 struct reg_param reg_params[3];
101 uint32_t target_buf;
102 uint32_t exit_var = 0;
103 int retval;
104
105 /* Inputs:
106 * r0 NAND data address (byte wide)
107 * r1 buffer address
108 * r2 buffer length
109 */
110 static const uint32_t code[] = {
111 0xe4d13001, /* s: ldrb r3, [r1], #1 */
112 0xe5c03000, /* strb r3, [r0] */
113 0xe2522001, /* subs r2, r2, #1 */
114 0x1afffffb, /* bne s */
115
116 /* exit: ARMv4 needs hardware breakpoint */
117 0xe1200070, /* e: bkpt #0 */
118 };
119
120 if (nand->op != ARM_NAND_WRITE || !nand->copy_area) {
121 retval = arm_code_to_working_area(target, code, sizeof(code),
122 nand->chunk_size, &nand->copy_area);
123 if (retval != ERROR_OK)
124 return retval;
125 }
126
127 nand->op = ARM_NAND_WRITE;
128
129 /* copy data to work area */
130 target_buf = nand->copy_area->address + sizeof(code);
131 retval = target_bulk_write_memory(target, target_buf, size / 4, data);
132 if (retval == ERROR_OK && (size & 3) != 0)
133 retval = target_write_memory(target,
134 target_buf + (size & ~3),
135 1, size & 3, data + (size & ~3));
136 if (retval != ERROR_OK)
137 return retval;
138
139 /* set up algorithm and parameters */
140 algo.common_magic = ARM_COMMON_MAGIC;
141 algo.core_mode = ARM_MODE_SVC;
142 algo.core_state = ARM_STATE_ARM;
143
144 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN);
145 init_reg_param(&reg_params[1], "r1", 32, PARAM_IN);
146 init_reg_param(&reg_params[2], "r2", 32, PARAM_IN);
147
148 buf_set_u32(reg_params[0].value, 0, 32, nand->data);
149 buf_set_u32(reg_params[1].value, 0, 32, target_buf);
150 buf_set_u32(reg_params[2].value, 0, 32, size);
151
152 /* armv4 must exit using a hardware breakpoint */
153 if (arm->is_armv4)
154 exit_var = nand->copy_area->address + sizeof(code) - 4;
155
156 /* use alg to write data from work area to NAND chip */
157 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
158 nand->copy_area->address, exit_var, 1000, &algo);
159 if (retval != ERROR_OK)
160 LOG_ERROR("error executing hosted NAND write");
161
162 destroy_reg_param(&reg_params[0]);
163 destroy_reg_param(&reg_params[1]);
164 destroy_reg_param(&reg_params[2]);
165
166 return retval;
167 }
168
169 /**
170 * Uses an on-chip algorithm for an ARM device to read from a NAND device and
171 * store the data into the host machine's memory.
172 *
173 * @param nand Pointer to the arm_nand_data struct that defines the I/O
174 * @param data Pointer to the data buffer to store the read data
175 * @param size Amount of data to be stored to the buffer.
176 * @return Success or failure of the operation
177 */
178 int arm_nandread(struct arm_nand_data *nand, uint8_t *data, uint32_t size)
179 {
180 struct target *target = nand->target;
181 struct arm_algorithm algo;
182 struct arm *arm = target->arch_info;
183 struct reg_param reg_params[3];
184 uint32_t target_buf;
185 uint32_t exit_var = 0;
186 int retval;
187
188 /* Inputs:
189 * r0 buffer address
190 * r1 NAND data address (byte wide)
191 * r2 buffer length
192 */
193 static const uint32_t code[] = {
194 0xe5d13000, /* s: ldrb r3, [r1] */
195 0xe4c03001, /* strb r3, [r0], #1 */
196 0xe2522001, /* subs r2, r2, #1 */
197 0x1afffffb, /* bne s */
198
199 /* exit: ARMv4 needs hardware breakpoint */
200 0xe1200070, /* e: bkpt #0 */
201 };
202
203 /* create the copy area if not yet available */
204 if (nand->op != ARM_NAND_READ || !nand->copy_area) {
205 retval = arm_code_to_working_area(target, code, sizeof(code),
206 nand->chunk_size, &nand->copy_area);
207 if (retval != ERROR_OK)
208 return retval;
209 }
210
211 nand->op = ARM_NAND_READ;
212 target_buf = nand->copy_area->address + sizeof(code);
213
214 /* set up algorithm and parameters */
215 algo.common_magic = ARM_COMMON_MAGIC;
216 algo.core_mode = ARM_MODE_SVC;
217 algo.core_state = ARM_STATE_ARM;
218
219 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN);
220 init_reg_param(&reg_params[1], "r1", 32, PARAM_IN);
221 init_reg_param(&reg_params[2], "r2", 32, PARAM_IN);
222
223 buf_set_u32(reg_params[0].value, 0, 32, target_buf);
224 buf_set_u32(reg_params[1].value, 0, 32, nand->data);
225 buf_set_u32(reg_params[2].value, 0, 32, size);
226
227 /* armv4 must exit using a hardware breakpoint */
228 if (arm->is_armv4)
229 exit_var = nand->copy_area->address + sizeof(code) - 4;
230
231 /* use alg to write data from NAND chip to work area */
232 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
233 nand->copy_area->address, exit_var, 1000, &algo);
234 if (retval != ERROR_OK)
235 LOG_ERROR("error executing hosted NAND read");
236
237 destroy_reg_param(&reg_params[0]);
238 destroy_reg_param(&reg_params[1]);
239 destroy_reg_param(&reg_params[2]);
240
241 /* read from work area to the host's memory */
242 retval = target_read_buffer(target, target_buf, size, data);
243
244 return retval;
245 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)