flash.c 6.0 KB

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