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.

352 lines
9.7KB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Pavel Chromy *
  3. * chromy@asix.cz *
  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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "imp.h"
  24. #include "ocl.h"
  25. #include <target/embeddedice.h>
  26. struct ocl_priv {
  27. struct arm_jtag *jtag_info;
  28. unsigned int buflen;
  29. unsigned int bufalign;
  30. };
  31. static int ocl_erase_check(struct flash_bank *bank)
  32. {
  33. return ERROR_OK;
  34. }
  35. static int ocl_protect_check(struct flash_bank *bank)
  36. {
  37. return ERROR_OK;
  38. }
  39. /* flash_bank ocl 0 0 0 0 <target#> */
  40. FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
  41. {
  42. struct arm7_9_common *arm7_9;
  43. struct ocl_priv *ocl;
  44. if (CMD_ARGC < 6)
  45. return ERROR_COMMAND_SYNTAX_ERROR;
  46. arm7_9 = target_to_arm7_9(bank->target);
  47. if (!is_arm7_9(arm7_9))
  48. return ERROR_TARGET_INVALID;
  49. ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
  50. ocl->jtag_info = &arm7_9->jtag_info;
  51. ocl->buflen = 0;
  52. ocl->bufalign = 1;
  53. return ERROR_OK;
  54. }
  55. static int ocl_erase(struct flash_bank *bank, int first, int last)
  56. {
  57. struct ocl_priv *ocl = bank->driver_priv;
  58. int retval;
  59. uint32_t dcc_buffer[3];
  60. /* check preconditions */
  61. if (bank->num_sectors == 0)
  62. return ERROR_FLASH_BANK_NOT_PROBED;
  63. if (bank->target->state != TARGET_RUNNING) {
  64. LOG_ERROR("target has to be running to communicate with the loader");
  65. return ERROR_TARGET_NOT_RUNNING;
  66. }
  67. if ((first == 0) && (last == bank->num_sectors - 1)) {
  68. dcc_buffer[0] = OCL_ERASE_ALL;
  69. retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
  70. if (retval != ERROR_OK)
  71. return retval;
  72. } else {
  73. dcc_buffer[0] = OCL_ERASE_BLOCK;
  74. dcc_buffer[1] = first;
  75. dcc_buffer[2] = last;
  76. retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3);
  77. if (retval != ERROR_OK)
  78. return retval;
  79. }
  80. /* wait for response, fixed timeout of 1 s */
  81. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
  82. if (retval != ERROR_OK)
  83. return retval;
  84. /* receive response */
  85. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1);
  86. if (retval != ERROR_OK)
  87. return retval;
  88. if (dcc_buffer[1] != OCL_CMD_DONE) {
  89. if (dcc_buffer[0] == OCL_ERASE_ALL)
  90. LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
  91. else
  92. LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
  93. return ERROR_FLASH_OPERATION_FAILED;
  94. }
  95. return ERROR_OK;
  96. }
  97. static int ocl_protect(struct flash_bank *bank, int set, int first, int last)
  98. {
  99. return ERROR_OK;
  100. }
  101. static int ocl_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  102. {
  103. struct ocl_priv *ocl = bank->driver_priv;
  104. int retval;
  105. uint32_t *dcc_buffer;
  106. uint32_t *dcc_bufptr;
  107. int byteofs;
  108. int runlen;
  109. uint32_t chksum;
  110. int i;
  111. /* check preconditions */
  112. if (ocl->buflen == 0 || ocl->bufalign == 0)
  113. return ERROR_FLASH_BANK_NOT_PROBED;
  114. if (bank->target->state != TARGET_RUNNING) {
  115. LOG_ERROR("target has to be running to communicate with the loader");
  116. return ERROR_TARGET_NOT_RUNNING;
  117. }
  118. /* allocate buffer for max. ocl buffer + overhead */
  119. dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
  120. while (count) {
  121. if (count + (offset % ocl->bufalign) > ocl->buflen)
  122. runlen = ocl->buflen - (offset % ocl->bufalign);
  123. else
  124. runlen = count;
  125. dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
  126. dcc_buffer[1] = offset;
  127. dcc_bufptr = &dcc_buffer[2];
  128. *dcc_bufptr = 0xffffffff;
  129. byteofs = (offset % ocl->bufalign) % 4;
  130. chksum = OCL_CHKS_INIT;
  131. /* copy data to DCC buffer in proper byte order and properly aligned */
  132. for (i = 0; i < runlen; i++) {
  133. switch (byteofs++) {
  134. case 0:
  135. *dcc_bufptr &= *(buffer++) | 0xffffff00;
  136. break;
  137. case 1:
  138. *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
  139. break;
  140. case 2:
  141. *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
  142. break;
  143. case 3:
  144. *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
  145. chksum ^= *(dcc_bufptr++);
  146. *dcc_bufptr = 0xffffffff;
  147. byteofs = 0;
  148. break;
  149. }
  150. }
  151. /* add the remaining word to checksum */
  152. if (byteofs)
  153. chksum ^= *(dcc_bufptr++);
  154. *(dcc_bufptr++) = chksum;
  155. /* send the data */
  156. retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer);
  157. if (retval != ERROR_OK) {
  158. free(dcc_buffer);
  159. return retval;
  160. }
  161. /* wait for response, fixed timeout of 1 s */
  162. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
  163. if (retval != ERROR_OK) {
  164. free(dcc_buffer);
  165. return retval;
  166. }
  167. /* receive response */
  168. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  169. if (retval != ERROR_OK) {
  170. free(dcc_buffer);
  171. return retval;
  172. }
  173. if (dcc_buffer[0] != OCL_CMD_DONE) {
  174. LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
  175. free(dcc_buffer);
  176. return ERROR_FLASH_OPERATION_FAILED;
  177. }
  178. count -= runlen;
  179. offset += runlen;
  180. }
  181. free(dcc_buffer);
  182. return ERROR_OK;
  183. }
  184. static int ocl_probe(struct flash_bank *bank)
  185. {
  186. struct ocl_priv *ocl = bank->driver_priv;
  187. int retval;
  188. uint32_t dcc_buffer[1];
  189. int sectsize;
  190. int i;
  191. /* purge pending data in DCC */
  192. embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  193. dcc_buffer[0] = OCL_PROBE;
  194. retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
  195. if (retval != ERROR_OK)
  196. return retval;
  197. /* wait for response, fixed timeout of 1 s */
  198. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
  199. if (retval != ERROR_OK)
  200. return retval;
  201. /* receive response */
  202. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  203. if (retval != ERROR_OK)
  204. return retval;
  205. if (dcc_buffer[0] != OCL_CMD_DONE) {
  206. LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
  207. return ERROR_FLASH_OPERATION_FAILED;
  208. }
  209. /* receive and fill in parameters, detection of loader is important, receive it one by one */
  210. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
  211. if (retval != ERROR_OK)
  212. return retval;
  213. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  214. if (retval != ERROR_OK)
  215. return retval;
  216. bank->base = dcc_buffer[0];
  217. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
  218. if (retval != ERROR_OK)
  219. return retval;
  220. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  221. if (retval != ERROR_OK)
  222. return retval;
  223. bank->size = dcc_buffer[0];
  224. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
  225. if (retval != ERROR_OK)
  226. return retval;
  227. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  228. if (retval != ERROR_OK)
  229. return retval;
  230. bank->num_sectors = dcc_buffer[0];
  231. retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
  232. if (retval != ERROR_OK)
  233. return retval;
  234. retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
  235. if (retval != ERROR_OK)
  236. return retval;
  237. ocl->buflen = dcc_buffer[0] & 0xffff;
  238. ocl->bufalign = dcc_buffer[0] >> 16;
  239. bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
  240. if (bank->num_sectors == 0) {
  241. LOG_ERROR("number of sectors shall be non zero value");
  242. return ERROR_FLASH_BANK_INVALID;
  243. }
  244. if (bank->size % bank->num_sectors) {
  245. LOG_ERROR("bank size not divisible by number of sectors");
  246. return ERROR_FLASH_BANK_INVALID;
  247. }
  248. sectsize = bank->size / bank->num_sectors;
  249. for (i = 0; i < bank->num_sectors; i++) {
  250. bank->sectors[i].offset = i * sectsize;
  251. bank->sectors[i].size = sectsize;
  252. bank->sectors[i].is_erased = -1;
  253. bank->sectors[i].is_protected = -1;
  254. }
  255. if (ocl->bufalign == 0)
  256. ocl->bufalign = 1;
  257. if (ocl->buflen == 0) {
  258. LOG_ERROR("buflen shall be non zero value");
  259. return ERROR_FLASH_BANK_INVALID;
  260. }
  261. if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign)) {
  262. LOG_ERROR("buflen is not multiple of bufalign");
  263. return ERROR_FLASH_BANK_INVALID;
  264. }
  265. if (ocl->buflen % 4) {
  266. LOG_ERROR("buflen shall be divisible by 4");
  267. return ERROR_FLASH_BANK_INVALID;
  268. }
  269. return ERROR_OK;
  270. }
  271. static int ocl_info(struct flash_bank *bank, char *buf, int buf_size)
  272. {
  273. return ERROR_OK;
  274. }
  275. static int ocl_auto_probe(struct flash_bank *bank)
  276. {
  277. struct ocl_priv *ocl = bank->driver_priv;
  278. if (ocl->buflen == 0 || ocl->bufalign == 0)
  279. return ERROR_FLASH_BANK_NOT_PROBED;
  280. return ERROR_OK;
  281. }
  282. struct flash_driver ocl_flash = {
  283. .name = "ocl",
  284. .flash_bank_command = ocl_flash_bank_command,
  285. .erase = ocl_erase,
  286. .protect = ocl_protect,
  287. .write = ocl_write,
  288. .read = default_flash_read,
  289. .probe = ocl_probe,
  290. .erase_check = ocl_erase_check,
  291. .protect_check = ocl_protect_check,
  292. .info = ocl_info,
  293. .auto_probe = ocl_auto_probe,
  294. };