flash.c 5.4 KB

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