flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <log.h>
  10. #include <uuid.h>
  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("%s %s: from 0x%08lX to 0x%08lX\n", __func__,
  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. flash_info_t *info;
  77. int i;
  78. for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  79. if (info->flash_id != FLASH_UNKNOWN &&
  80. addr >= info->start[0] &&
  81. /* WARNING - The '- 1' is needed if the flash
  82. * is at the end of the address space, since
  83. * info->start[0] + info->size wraps back to 0.
  84. * Please don't change this unless you understand this.
  85. */
  86. addr <= info->start[0] + info->size - 1) {
  87. return (info);
  88. }
  89. }
  90. return (NULL);
  91. }
  92. /*-----------------------------------------------------------------------
  93. * Copy memory to flash.
  94. * Make sure all target addresses are within Flash bounds,
  95. * and no protected sectors are hit.
  96. * Returns:
  97. * ERR_OK 0 - OK
  98. * ERR_TIMEOUT 1 - write timeout
  99. * ERR_NOT_ERASED 2 - Flash not erased
  100. * ERR_PROTECTED 4 - target range includes protected sectors
  101. * ERR_INVAL 8 - target address not in Flash memory
  102. * ERR_ALIGN 16 - target address not aligned on boundary
  103. * (only some targets require alignment)
  104. */
  105. int
  106. flash_write(char *src, ulong addr, ulong cnt)
  107. {
  108. int i;
  109. ulong end = addr + cnt - 1;
  110. flash_info_t *info_first = addr2info(addr);
  111. flash_info_t *info_last = addr2info(end);
  112. flash_info_t *info;
  113. __maybe_unused char *src_orig = src;
  114. __maybe_unused char *addr_orig = (char *)addr;
  115. __maybe_unused ulong cnt_orig = cnt;
  116. if (cnt == 0) {
  117. return (ERR_OK);
  118. }
  119. if (!info_first || !info_last) {
  120. return (ERR_INVAL);
  121. }
  122. for (info = info_first; info <= info_last; ++info) {
  123. ulong b_end = info->start[0] + info->size; /* bank end addr */
  124. short s_end = info->sector_count - 1;
  125. for (i=0; i<info->sector_count; ++i) {
  126. ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
  127. if ((end >= info->start[i]) && (addr < e_addr) &&
  128. (info->protect[i] != 0) ) {
  129. return (ERR_PROTECTED);
  130. }
  131. }
  132. }
  133. /* finally write data to flash */
  134. for (info = info_first; info <= info_last && cnt>0; ++info) {
  135. ulong len;
  136. len = info->start[0] + info->size - addr;
  137. if (len > cnt)
  138. len = cnt;
  139. if ((i = write_buff(info, (uchar *)src, addr, len)) != 0) {
  140. return (i);
  141. }
  142. cnt -= len;
  143. addr += len;
  144. src += len;
  145. }
  146. #if defined(CONFIG_FLASH_VERIFY)
  147. if (memcmp(src_orig, addr_orig, cnt_orig)) {
  148. printf("\nVerify failed!\n");
  149. return ERR_PROG_ERROR;
  150. }
  151. #endif /* CONFIG_SYS_FLASH_VERIFY_AFTER_WRITE */
  152. return (ERR_OK);
  153. }
  154. /*-----------------------------------------------------------------------
  155. */
  156. void flash_perror(int err)
  157. {
  158. switch (err) {
  159. case ERR_OK:
  160. break;
  161. case ERR_TIMEOUT:
  162. puts ("Timeout writing to Flash\n");
  163. break;
  164. case ERR_NOT_ERASED:
  165. puts ("Flash not Erased\n");
  166. break;
  167. case ERR_PROTECTED:
  168. puts ("Can't write to protected Flash sectors\n");
  169. break;
  170. case ERR_INVAL:
  171. puts ("Outside available Flash\n");
  172. break;
  173. case ERR_ALIGN:
  174. puts ("Start and/or end address not on sector boundary\n");
  175. break;
  176. case ERR_UNKNOWN_FLASH_VENDOR:
  177. puts ("Unknown Vendor of Flash\n");
  178. break;
  179. case ERR_UNKNOWN_FLASH_TYPE:
  180. puts ("Unknown Type of Flash\n");
  181. break;
  182. case ERR_PROG_ERROR:
  183. puts ("General Flash Programming Error\n");
  184. break;
  185. case ERR_ABORTED:
  186. puts("Flash Programming Aborted\n");
  187. break;
  188. default:
  189. printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  190. break;
  191. }
  192. }