flash.c 6.1 KB

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