flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * (C) Copyright 2000-2006
  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. extern flash_info_t flash_info[]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. /*-----------------------------------------------------------------------
  30. * Set protection status for monitor sectors
  31. *
  32. * The monitor is always located in the _first_ Flash bank.
  33. * If necessary you have to map the second bank at lower addresses.
  34. */
  35. void
  36. flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
  37. {
  38. ulong b_end = info->start[0] + info->size - 1; /* bank end address */
  39. short s_end = info->sector_count - 1; /* index of last sector */
  40. int i;
  41. /* Do nothing if input data is bad. */
  42. if (info->sector_count == 0 || info->size == 0 || to < from) {
  43. return;
  44. }
  45. /* There is nothing to do if we have no data about the flash
  46. * or the protect range and flash range don't overlap.
  47. */
  48. if (info->flash_id == FLASH_UNKNOWN ||
  49. to < info->start[0] || from > b_end) {
  50. return;
  51. }
  52. for (i=0; i<info->sector_count; ++i) {
  53. ulong end; /* last address in current sect */
  54. end = (i == s_end) ? b_end : info->start[i + 1] - 1;
  55. /* Update protection if any part of the sector
  56. * is in the specified range.
  57. */
  58. if (from <= end && to >= info->start[i]) {
  59. if (flag & FLAG_PROTECT_CLEAR) {
  60. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  61. flash_real_protect(info, i, 0);
  62. #else
  63. info->protect[i] = 0;
  64. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  65. }
  66. else if (flag & FLAG_PROTECT_SET) {
  67. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  68. flash_real_protect(info, i, 1);
  69. #else
  70. info->protect[i] = 1;
  71. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  72. }
  73. }
  74. }
  75. }
  76. /*-----------------------------------------------------------------------
  77. */
  78. flash_info_t *
  79. addr2info (ulong addr)
  80. {
  81. #ifndef CONFIG_SPD823TS
  82. flash_info_t *info;
  83. int i;
  84. for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  85. if (info->flash_id != FLASH_UNKNOWN &&
  86. addr >= info->start[0] &&
  87. /* WARNING - The '- 1' is needed if the flash
  88. * is at the end of the address space, since
  89. * info->start[0] + info->size wraps back to 0.
  90. * Please don't change this unless you understand this.
  91. */
  92. addr <= info->start[0] + info->size - 1) {
  93. return (info);
  94. }
  95. }
  96. #endif /* CONFIG_SPD823TS */
  97. return (NULL);
  98. }
  99. /*-----------------------------------------------------------------------
  100. * Copy memory to flash.
  101. * Make sure all target addresses are within Flash bounds,
  102. * and no protected sectors are hit.
  103. * Returns:
  104. * ERR_OK 0 - OK
  105. * ERR_TIMOUT 1 - write timeout
  106. * ERR_NOT_ERASED 2 - Flash not erased
  107. * ERR_PROTECTED 4 - target range includes protected sectors
  108. * ERR_INVAL 8 - target address not in Flash memory
  109. * ERR_ALIGN 16 - target address not aligned on boundary
  110. * (only some targets require alignment)
  111. */
  112. int
  113. flash_write (char *src, ulong addr, ulong cnt)
  114. {
  115. #ifdef CONFIG_SPD823TS
  116. return (ERR_TIMOUT); /* any other error codes are possible as well */
  117. #else
  118. int i;
  119. ulong end = addr + cnt - 1;
  120. flash_info_t *info_first = addr2info (addr);
  121. flash_info_t *info_last = addr2info (end );
  122. flash_info_t *info;
  123. int j;
  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. printf("\rWriting ");
  142. for (j=0; j<20; j++) putc(177);
  143. printf("\rWriting ");
  144. /* finally write data to flash */
  145. for (info = info_first; info <= info_last && cnt>0; ++info) {
  146. ulong len;
  147. len = info->start[0] + info->size - addr;
  148. if (len > cnt)
  149. len = cnt;
  150. if ((i = write_buff(info, src, addr, len)) != 0) {
  151. return (i);
  152. }
  153. cnt -= len;
  154. addr += len;
  155. src += len;
  156. }
  157. return (ERR_OK);
  158. #endif /* CONFIG_SPD823TS */
  159. }
  160. /*-----------------------------------------------------------------------
  161. */