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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "arm_nandio.h"
32 * ARM-specific bulk write from buffer to address of 8-bit wide NAND.
33 * For now this only supports ARMv4 and ARMv5 cores.
35 * Enhancements to target_run_algorithm() could enable:
36 * - faster writes: on ARMv5+ don't setup/teardown hardware breakpoint
37 * - ARMv6 and ARMv7 cores in ARM mode
39 * Different code fragments could handle:
40 * - Thumb2 cores like Cortex-M (needs different byteswapping)
41 * - 16-bit wide data (needs different setup too)
43 int arm_nandwrite(struct arm_nand_data *nand, uint8_t *data, int size)
45 target_t *target = nand->target;
46 armv4_5_algorithm_t algo;
47 reg_param_t reg_params[3];
52 * r0 NAND data address (byte wide)
56 static const uint32_t code[] = {
57 0xe4d13001, /* s: ldrb r3, [r1], #1 */
58 0xe5c03000, /* strb r3, [r0] */
59 0xe2522001, /* subs r2, r2, #1 */
60 0x1afffffb, /* bne s */
62 /* exit: ARMv4 needs hardware breakpoint */
63 0xe1200070, /* e: bkpt #0 */
66 if (!nand->copy_area) {
67 uint8_t code_buf[sizeof(code)];
70 /* make sure we have a working area */
71 if (target_alloc_working_area(target,
72 sizeof(code) + nand->chunk_size,
73 &nand->copy_area) != ERROR_OK) {
74 LOG_DEBUG("%s: no %d byte buffer",
76 (int) sizeof(code) + nand->chunk_size);
77 return ERROR_NAND_NO_BUFFER;
80 /* buffer code in target endianness */
81 for (i = 0; i < sizeof(code) / 4; i++)
82 target_buffer_set_u32(target, code_buf + i * 4, code[i]);
84 /* copy code to work area */
85 retval = target_write_memory(target,
86 nand->copy_area->address,
87 4, sizeof(code) / 4, code_buf);
88 if (retval != ERROR_OK)
92 /* copy data to work area */
93 target_buf = nand->copy_area->address + sizeof(code);
94 retval = target_bulk_write_memory(target, target_buf, size / 4, data);
95 if (retval == ERROR_OK && (size & 3) != 0)
96 retval = target_write_memory(target,
97 target_buf + (size & ~3),
98 1, size & 3, data + (size & ~3));
99 if (retval != ERROR_OK)
102 /* set up algorithm and parameters */
103 algo.common_magic = ARMV4_5_COMMON_MAGIC;
104 algo.core_mode = ARMV4_5_MODE_SVC;
105 algo.core_state = ARMV4_5_STATE_ARM;
107 init_reg_param(®_params[0], "r0", 32, PARAM_IN);
108 init_reg_param(®_params[1], "r1", 32, PARAM_IN);
109 init_reg_param(®_params[2], "r2", 32, PARAM_IN);
111 buf_set_u32(reg_params[0].value, 0, 32, nand->data);
112 buf_set_u32(reg_params[1].value, 0, 32, target_buf);
113 buf_set_u32(reg_params[2].value, 0, 32, size);
115 /* use alg to write data from work area to NAND chip */
116 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
117 nand->copy_area->address,
118 nand->copy_area->address + sizeof(code) - 4,
120 if (retval != ERROR_OK)
121 LOG_ERROR("error executing hosted NAND write");
123 destroy_reg_param(®_params[0]);
124 destroy_reg_param(®_params[1]);
125 destroy_reg_param(®_params[2]);
130 /* REVISIT do the same for bulk *read* too ... */