flash.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /* #define DEBUG */
  8. #include <common.h>
  9. #include <flash.h>
  10. #if !defined(CONFIG_SYS_NO_FLASH)
  11. #include <mtd/cfi_flash.h>
  12. extern flash_info_t flash_info[]; /* info for FLASH chips */
  13. /*-----------------------------------------------------------------------
  14. * Functions
  15. */
  16. /*-----------------------------------------------------------------------
  17. * Set protection status for monitor sectors
  18. *
  19. * The monitor is always located in the _first_ Flash bank.
  20. * If necessary you have to map the second bank at lower addresses.
  21. */
  22. void
  23. flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
  24. {
  25. ulong b_end;
  26. short s_end;
  27. int i;
  28. /* Do nothing if input data is bad. */
  29. if (!info || info->sector_count == 0 || info->size == 0 || to < from) {
  30. return;
  31. }
  32. s_end = info->sector_count - 1; /* index of last sector */
  33. b_end = info->start[0] + info->size - 1; /* bank end address */
  34. debug ("flash_protect %s: from 0x%08lX to 0x%08lX\n",
  35. (flag & FLAG_PROTECT_SET) ? "ON" :
  36. (flag & FLAG_PROTECT_CLEAR) ? "OFF" : "???",
  37. from, to);
  38. /* There is nothing to do if we have no data about the flash
  39. * or the protect range and flash range don't overlap.
  40. */
  41. if (info->flash_id == FLASH_UNKNOWN ||
  42. to < info->start[0] || from > b_end) {
  43. return;
  44. }
  45. for (i=0; i<info->sector_count; ++i) {
  46. ulong end; /* last address in current sect */
  47. end = (i == s_end) ? b_end : info->start[i + 1] - 1;
  48. /* Update protection if any part of the sector
  49. * is in the specified range.
  50. */
  51. if (from <= end && to >= info->start[i]) {
  52. if (flag & FLAG_PROTECT_CLEAR) {
  53. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  54. flash_real_protect(info, i, 0);
  55. #else
  56. info->protect[i] = 0;
  57. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  58. debug ("protect off %d\n", i);
  59. }
  60. else if (flag & FLAG_PROTECT_SET) {
  61. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  62. flash_real_protect(info, i, 1);
  63. #else
  64. info->protect[i] = 1;
  65. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  66. debug ("protect on %d\n", i);
  67. }
  68. }
  69. }
  70. }
  71. /*-----------------------------------------------------------------------
  72. */
  73. flash_info_t *
  74. addr2info (ulong addr)
  75. {
  76. #ifndef CONFIG_SPD823TS
  77. flash_info_t *info;
  78. int i;
  79. for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  80. if (info->flash_id != FLASH_UNKNOWN &&
  81. addr >= info->start[0] &&
  82. /* WARNING - The '- 1' is needed if the flash
  83. * is at the end of the address space, since
  84. * info->start[0] + info->size wraps back to 0.
  85. * Please don't change this unless you understand this.
  86. */
  87. addr <= info->start[0] + info->size - 1) {
  88. return (info);
  89. }
  90. }
  91. #endif /* CONFIG_SPD823TS */
  92. return (NULL);
  93. }
  94. /*-----------------------------------------------------------------------
  95. * Copy memory to flash.
  96. * Make sure all target addresses are within Flash bounds,
  97. * and no protected sectors are hit.
  98. * Returns:
  99. * ERR_OK 0 - OK
  100. * ERR_TIMOUT 1 - write timeout
  101. * ERR_NOT_ERASED 2 - Flash not erased
  102. * ERR_PROTECTED 4 - target range includes protected sectors
  103. * ERR_INVAL 8 - target address not in Flash memory
  104. * ERR_ALIGN 16 - target address not aligned on boundary
  105. * (only some targets require alignment)
  106. */
  107. int
  108. flash_write (char *src, ulong addr, ulong cnt)
  109. {
  110. #ifdef CONFIG_SPD823TS
  111. return (ERR_TIMOUT); /* any other error codes are possible as well */
  112. #else
  113. int i;
  114. ulong end = addr + cnt - 1;
  115. flash_info_t *info_first = addr2info (addr);
  116. flash_info_t *info_last = addr2info (end );
  117. flash_info_t *info;
  118. __maybe_unused char *src_orig = src;
  119. __maybe_unused char *addr_orig = (char *)addr;
  120. __maybe_unused ulong cnt_orig = cnt;
  121. if (cnt == 0) {
  122. return (ERR_OK);
  123. }
  124. if (!info_first || !info_last) {
  125. return (ERR_INVAL);
  126. }
  127. for (info = info_first; info <= info_last; ++info) {
  128. ulong b_end = info->start[0] + info->size; /* bank end addr */
  129. short s_end = info->sector_count - 1;
  130. for (i=0; i<info->sector_count; ++i) {
  131. ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
  132. if ((end >= info->start[i]) && (addr < e_addr) &&
  133. (info->protect[i] != 0) ) {
  134. return (ERR_PROTECTED);
  135. }
  136. }
  137. }
  138. /* finally write data to flash */
  139. for (info = info_first; info <= info_last && cnt>0; ++info) {
  140. ulong len;
  141. len = info->start[0] + info->size - addr;
  142. if (len > cnt)
  143. len = cnt;
  144. if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
  145. return (i);
  146. }
  147. cnt -= len;
  148. addr += len;
  149. src += len;
  150. }
  151. #if defined(CONFIG_FLASH_VERIFY)
  152. if (memcmp(src_orig, addr_orig, cnt_orig)) {
  153. printf("\nVerify failed!\n");
  154. return ERR_PROG_ERROR;
  155. }
  156. #endif /* CONFIG_SYS_FLASH_VERIFY_AFTER_WRITE */
  157. return (ERR_OK);
  158. #endif /* CONFIG_SPD823TS */
  159. }
  160. /*-----------------------------------------------------------------------
  161. */
  162. void flash_perror (int err)
  163. {
  164. switch (err) {
  165. case ERR_OK:
  166. break;
  167. case ERR_TIMOUT:
  168. puts ("Timeout writing to Flash\n");
  169. break;
  170. case ERR_NOT_ERASED:
  171. puts ("Flash not Erased\n");
  172. break;
  173. case ERR_PROTECTED:
  174. puts ("Can't write to protected Flash sectors\n");
  175. break;
  176. case ERR_INVAL:
  177. puts ("Outside available Flash\n");
  178. break;
  179. case ERR_ALIGN:
  180. puts ("Start and/or end address not on sector boundary\n");
  181. break;
  182. case ERR_UNKNOWN_FLASH_VENDOR:
  183. puts ("Unknown Vendor of Flash\n");
  184. break;
  185. case ERR_UNKNOWN_FLASH_TYPE:
  186. puts ("Unknown Type of Flash\n");
  187. break;
  188. case ERR_PROG_ERROR:
  189. puts ("General Flash Programming Error\n");
  190. break;
  191. case ERR_ABORTED:
  192. puts("Flash Programming Aborted\n");
  193. break;
  194. default:
  195. printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  196. break;
  197. }
  198. }
  199. /*-----------------------------------------------------------------------
  200. */
  201. #endif /* !CONFIG_SYS_NO_FLASH */