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 * - ARMv6 and ARMv7 cores in ARM mode
38 * Different code fragments could handle:
39 * - Thumb2 cores like Cortex-M (needs different byteswapping)
40 * - 16-bit wide data (needs different setup too)
42 int arm_nandwrite(struct arm_nand_data
*nand
, uint8_t *data
, int size
)
44 target_t
*target
= nand
->target
;
45 armv4_5_algorithm_t algo
;
46 armv4_5_common_t
*armv4_5
= target
->arch_info
;
47 reg_param_t reg_params
[3];
53 * r0 NAND data address (byte wide)
57 static const uint32_t code
[] = {
58 0xe4d13001, /* s: ldrb r3, [r1], #1 */
59 0xe5c03000, /* strb r3, [r0] */
60 0xe2522001, /* subs r2, r2, #1 */
61 0x1afffffb, /* bne s */
63 /* exit: ARMv4 needs hardware breakpoint */
64 0xe1200070, /* e: bkpt #0 */
67 if (!nand
->copy_area
) {
68 uint8_t code_buf
[sizeof(code
)];
71 /* make sure we have a working area */
72 if (target_alloc_working_area(target
,
73 sizeof(code
) + nand
->chunk_size
,
74 &nand
->copy_area
) != ERROR_OK
) {
75 LOG_DEBUG("%s: no %d byte buffer",
77 (int) sizeof(code
) + nand
->chunk_size
);
78 return ERROR_NAND_NO_BUFFER
;
81 /* buffer code in target endianness */
82 for (i
= 0; i
< sizeof(code
) / 4; i
++)
83 target_buffer_set_u32(target
, code_buf
+ i
* 4, code
[i
]);
85 /* copy code to work area */
86 retval
= target_write_memory(target
,
87 nand
->copy_area
->address
,
88 4, sizeof(code
) / 4, code_buf
);
89 if (retval
!= ERROR_OK
)
93 /* copy data to work area */
94 target_buf
= nand
->copy_area
->address
+ sizeof(code
);
95 retval
= target_bulk_write_memory(target
, target_buf
, size
/ 4, data
);
96 if (retval
== ERROR_OK
&& (size
& 3) != 0)
97 retval
= target_write_memory(target
,
98 target_buf
+ (size
& ~3),
99 1, size
& 3, data
+ (size
& ~3));
100 if (retval
!= ERROR_OK
)
103 /* set up algorithm and parameters */
104 algo
.common_magic
= ARMV4_5_COMMON_MAGIC
;
105 algo
.core_mode
= ARMV4_5_MODE_SVC
;
106 algo
.core_state
= ARMV4_5_STATE_ARM
;
108 init_reg_param(®_params
[0], "r0", 32, PARAM_IN
);
109 init_reg_param(®_params
[1], "r1", 32, PARAM_IN
);
110 init_reg_param(®_params
[2], "r2", 32, PARAM_IN
);
112 buf_set_u32(reg_params
[0].value
, 0, 32, nand
->data
);
113 buf_set_u32(reg_params
[1].value
, 0, 32, target_buf
);
114 buf_set_u32(reg_params
[2].value
, 0, 32, size
);
116 /* armv4 must exit using a hardware breakpoint */
117 if (armv4_5
->is_armv4
)
118 exit
= nand
->copy_area
->address
+ sizeof(code
) - 4;
120 /* use alg to write data from work area to NAND chip */
121 retval
= target_run_algorithm(target
, 0, NULL
, 3, reg_params
,
122 nand
->copy_area
->address
, exit
, 1000, &algo
);
123 if (retval
!= ERROR_OK
)
124 LOG_ERROR("error executing hosted NAND write");
126 destroy_reg_param(®_params
[0]);
127 destroy_reg_param(®_params
[1]);
128 destroy_reg_param(®_params
[2]);
133 /* REVISIT do the same for bulk *read* too ... */