flash.c 5.4 KB

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