You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

246 lines
7.7 KiB

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