flash.c 6.5 KB

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