Add target_run_algorithm wrapper:
[openocd.git] / src / flash / orion_nand.c
1 /***************************************************************************
2 * Copyright (C) 2009 by Marvell Semiconductors, Inc. *
3 * Written by Nicolas Pitre <nico at marvell.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 /*
22 * NAND controller interface for Marvell Orion/Kirkwood SoCs.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "nand.h"
30 #include "armv4_5.h"
31 #include "binarybuffer.h"
32
33
34 typedef struct orion_nand_controller_s
35 {
36 struct target_s *target;
37 working_area_t *copy_area;
38
39 u32 cmd;
40 u32 addr;
41 u32 data;
42 } orion_nand_controller_t;
43
44 #define CHECK_HALTED \
45 do { \
46 if (target->state != TARGET_HALTED) { \
47 LOG_ERROR("NAND flash access requires halted target"); \
48 return ERROR_NAND_OPERATION_FAILED; \
49 } \
50 } while (0)
51
52 static int orion_nand_command(struct nand_device_s *device, u8 command)
53 {
54 orion_nand_controller_t *hw = device->controller_priv;
55 target_t *target = hw->target;
56
57 CHECK_HALTED;
58 target_write_u8(target, hw->cmd, command);
59 return ERROR_OK;
60 }
61
62 static int orion_nand_address(struct nand_device_s *device, u8 address)
63 {
64 orion_nand_controller_t *hw = device->controller_priv;
65 target_t *target = hw->target;
66
67 CHECK_HALTED;
68 target_write_u8(target, hw->addr, address);
69 return ERROR_OK;
70 }
71
72 static int orion_nand_read(struct nand_device_s *device, void *data)
73 {
74 orion_nand_controller_t *hw = device->controller_priv;
75 target_t *target = hw->target;
76
77 CHECK_HALTED;
78 target_read_u8(target, hw->data, data);
79 return ERROR_OK;
80 }
81
82 static int orion_nand_write(struct nand_device_s *device, u16 data)
83 {
84 orion_nand_controller_t *hw = device->controller_priv;
85 target_t *target = hw->target;
86
87 CHECK_HALTED;
88 target_write_u8(target, hw->data, data);
89 return ERROR_OK;
90 }
91
92 static int orion_nand_slow_block_write(struct nand_device_s *device, u8 *data, int size)
93 {
94 while (size--)
95 orion_nand_write(device, *data++);
96 return ERROR_OK;
97 }
98
99 static int orion_nand_fast_block_write(struct nand_device_s *device, u8 *data, int size)
100 {
101 orion_nand_controller_t *hw = device->controller_priv;
102 target_t *target = hw->target;
103 armv4_5_algorithm_t algo;
104 reg_param_t reg_params[3];
105 u32 target_buf;
106 int retval;
107
108 static const u32 code[] = {
109 0xe4d13001, /* ldrb r3, [r1], #1 */
110 0xe5c03000, /* strb r3, [r0] */
111 0xe2522001, /* subs r2, r2, #1 */
112 0x1afffffb, /* bne 0 */
113 0xeafffffe, /* b . */
114 };
115 int code_size = sizeof(code);
116
117 if (!hw->copy_area) {
118 u8 code_buf[code_size];
119 int i;
120
121 /* make sure we have a working area */
122 if (target_alloc_working_area(target,
123 code_size + device->page_size,
124 &hw->copy_area) != ERROR_OK)
125 {
126 return orion_nand_slow_block_write(device, data, size);
127 }
128
129 /* copy target instructions to target endianness */
130 for (i = 0; i < code_size/4; i++)
131 target_buffer_set_u32(target, code_buf + i*4, code[i]);
132
133 /* write code to working area */
134 retval = target_write_memory(target,
135 hw->copy_area->address,
136 4, code_size/4, code_buf);
137 if (retval != ERROR_OK)
138 return retval;
139 }
140
141 /* copy data to target's memory */
142 target_buf = hw->copy_area->address + code_size;
143 retval = target->type->bulk_write_memory(target, target_buf,
144 size/4, data);
145 if (retval == ERROR_OK && size & 3) {
146 retval = target_write_memory(target,
147 target_buf + (size & ~3),
148 1, size & 3, data + (size & ~3));
149 }
150 if (retval != ERROR_OK)
151 return retval;
152
153 algo.common_magic = ARMV4_5_COMMON_MAGIC;
154 algo.core_mode = ARMV4_5_MODE_SVC;
155 algo.core_state = ARMV4_5_STATE_ARM;
156
157 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN);
158 init_reg_param(&reg_params[1], "r1", 32, PARAM_IN);
159 init_reg_param(&reg_params[2], "r2", 32, PARAM_IN);
160
161 buf_set_u32(reg_params[0].value, 0, 32, hw->data);
162 buf_set_u32(reg_params[1].value, 0, 32, target_buf);
163 buf_set_u32(reg_params[2].value, 0, 32, size);
164
165 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
166 hw->copy_area->address,
167 hw->copy_area->address + code_size - 4,
168 1000, &algo);
169 if (retval != ERROR_OK)
170 LOG_ERROR("error executing hosted NAND write");
171
172 destroy_reg_param(&reg_params[0]);
173 destroy_reg_param(&reg_params[1]);
174 destroy_reg_param(&reg_params[2]);
175 return retval;
176 }
177
178 static int orion_nand_reset(struct nand_device_s *device)
179 {
180 return orion_nand_command(device, NAND_CMD_RESET);
181 }
182
183 static int orion_nand_controller_ready(struct nand_device_s *device, int timeout)
184 {
185 return 1;
186 }
187
188 static int orion_nand_register_commands(struct command_context_s *cmd_ctx)
189 {
190 return ERROR_OK;
191 }
192
193 int orion_nand_device_command(struct command_context_s *cmd_ctx, char *cmd,
194 char **args, int argc,
195 struct nand_device_s *device)
196 {
197 orion_nand_controller_t *hw;
198 u32 base;
199 u8 ale, cle;
200
201 if (argc != 3) {
202 LOG_ERROR("arguments must be: <target_number> <NAND_address>\n");
203 return ERROR_NAND_DEVICE_INVALID;
204 }
205
206 hw = calloc(1, sizeof(*hw));
207 if (!hw) {
208 LOG_ERROR("no memory for nand controller\n");
209 return ERROR_NAND_DEVICE_INVALID;
210 }
211
212 device->controller_priv = hw;
213 hw->target = get_target(args[1]);
214 if (!hw->target) {
215 LOG_ERROR("target '%s' not defined", args[1]);
216 free(hw);
217 return ERROR_NAND_DEVICE_INVALID;
218 }
219
220 base = strtoul(args[2], NULL, 0);
221 cle = 0;
222 ale = 1;
223
224 hw->data = base;
225 hw->cmd = base + (1 << cle);
226 hw->addr = base + (1 << ale);
227
228 return ERROR_OK;
229 }
230
231 static int orion_nand_init(struct nand_device_s *device)
232 {
233 return ERROR_OK;
234 }
235
236 nand_flash_controller_t orion_nand_controller =
237 {
238 .name = "orion",
239 .command = orion_nand_command,
240 .address = orion_nand_address,
241 .read_data = orion_nand_read,
242 .write_data = orion_nand_write,
243 .write_block_data = orion_nand_fast_block_write,
244 .reset = orion_nand_reset,
245 .controller_ready = orion_nand_controller_ready,
246 .nand_device_command = orion_nand_device_command,
247 .register_commands = orion_nand_register_commands,
248 .init = orion_nand_init,
249 };
250

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)