flash.c 5.8 KB

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