flash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #define FLASH_BANK_SIZE 0x1000000
  26. #define MAIN_SECT_SIZE 0x20000
  27. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  28. /*-----------------------------------------------------------------------
  29. */
  30. ulong flash_init (void)
  31. {
  32. int i, j;
  33. ulong size = 0;
  34. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  35. ulong flashbase = 0;
  36. flash_info[i].flash_id =
  37. (INTEL_MANUFACT & FLASH_VENDMASK) |
  38. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  39. flash_info[i].size = FLASH_BANK_SIZE;
  40. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  41. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  42. if (i == 0)
  43. flashbase = PHYS_FLASH_1;
  44. else
  45. panic ("configured too many flash banks!\n");
  46. for (j = 0; j < flash_info[i].sector_count; j++) {
  47. flash_info[i].start[j] = flashbase + j * MAIN_SECT_SIZE;
  48. }
  49. size += flash_info[i].size;
  50. }
  51. /* Protect monitor and environment sectors
  52. */
  53. flash_protect ( FLAG_PROTECT_SET,
  54. CFG_FLASH_BASE,
  55. CFG_FLASH_BASE + monitor_flash_len - 1,
  56. &flash_info[0]);
  57. flash_protect ( FLAG_PROTECT_SET,
  58. CFG_ENV_ADDR,
  59. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  60. return size;
  61. }
  62. /*-----------------------------------------------------------------------
  63. */
  64. void flash_print_info (flash_info_t * info)
  65. {
  66. int i;
  67. switch (info->flash_id & FLASH_VENDMASK) {
  68. case (INTEL_MANUFACT & FLASH_VENDMASK):
  69. printf ("Intel: ");
  70. break;
  71. default:
  72. printf ("Unknown Vendor ");
  73. break;
  74. }
  75. switch (info->flash_id & FLASH_TYPEMASK) {
  76. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  77. printf ("28F128J3 (128Mbit)\n");
  78. break;
  79. default:
  80. printf ("Unknown Chip Type\n");
  81. goto Done;
  82. break;
  83. }
  84. printf (" Size: %ld MB in %d Sectors\n",
  85. info->size >> 20, info->sector_count);
  86. printf (" Sector Start Addresses:");
  87. for (i = 0; i < info->sector_count; i++) {
  88. if ((i % 5) == 0) {
  89. printf ("\n ");
  90. }
  91. printf (" %08lX%s", info->start[i],
  92. info->protect[i] ? " (RO)" : " ");
  93. }
  94. printf ("\n");
  95. Done: ;
  96. }
  97. /*-----------------------------------------------------------------------
  98. */
  99. int flash_erase (flash_info_t * info, int s_first, int s_last)
  100. {
  101. int flag, prot, sect;
  102. int rc = ERR_OK;
  103. if (info->flash_id == FLASH_UNKNOWN)
  104. return ERR_UNKNOWN_FLASH_TYPE;
  105. if ((s_first < 0) || (s_first > s_last)) {
  106. return ERR_INVAL;
  107. }
  108. if ((info->flash_id & FLASH_VENDMASK) !=
  109. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  110. return ERR_UNKNOWN_FLASH_VENDOR;
  111. }
  112. prot = 0;
  113. for (sect = s_first; sect <= s_last; ++sect) {
  114. if (info->protect[sect]) {
  115. prot++;
  116. }
  117. }
  118. if (prot)
  119. return ERR_PROTECTED;
  120. /*
  121. * Disable interrupts which might cause a timeout
  122. * here. Remember that our exception vectors are
  123. * at address 0 in the flash, and we don't want a
  124. * (ticker) exception to happen while the flash
  125. * chip is in programming mode.
  126. */
  127. flag = disable_interrupts ();
  128. /* Start erase on unprotected sectors */
  129. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  130. printf ("Erasing sector %2d ... ", sect);
  131. /* arm simple, non interrupt dependent timer */
  132. reset_timer_masked ();
  133. if (info->protect[sect] == 0) { /* not protected */
  134. vu_short *addr = (vu_short *) (info->start[sect]);
  135. *addr = 0x20; /* erase setup */
  136. *addr = 0xD0; /* erase confirm */
  137. while ((*addr & 0x80) != 0x80) {
  138. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  139. *addr = 0xB0; /* suspend erase */
  140. *addr = 0xFF; /* reset to read mode */
  141. rc = ERR_TIMOUT;
  142. goto outahere;
  143. }
  144. }
  145. /* clear status register command */
  146. *addr = 0x50;
  147. /* reset to read mode */
  148. *addr = 0xFF;
  149. }
  150. printf ("ok.\n");
  151. }
  152. if (ctrlc ())
  153. printf ("User Interrupt!\n");
  154. outahere:
  155. /* allow flash to settle - wait 10 ms */
  156. udelay_masked (10000);
  157. if (flag)
  158. enable_interrupts ();
  159. return rc;
  160. }
  161. /*-----------------------------------------------------------------------
  162. * Copy memory to flash
  163. */
  164. static int write_word (flash_info_t * info, ulong dest, ushort data)
  165. {
  166. vu_short *addr = (vu_short *) dest, val;
  167. int rc = ERR_OK;
  168. int flag;
  169. /* Check if Flash is (sufficiently) erased
  170. */
  171. if ((*addr & data) != data)
  172. return ERR_NOT_ERASED;
  173. /*
  174. * Disable interrupts which might cause a timeout
  175. * here. Remember that our exception vectors are
  176. * at address 0 in the flash, and we don't want a
  177. * (ticker) exception to happen while the flash
  178. * chip is in programming mode.
  179. */
  180. flag = disable_interrupts ();
  181. /* clear status register command */
  182. *addr = 0x50;
  183. /* program set-up command */
  184. *addr = 0x40;
  185. /* latch address/data */
  186. *addr = data;
  187. /* arm simple, non interrupt dependent timer */
  188. reset_timer_masked ();
  189. /* wait while polling the status register */
  190. while (((val = *addr) & 0x80) != 0x80) {
  191. if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
  192. rc = ERR_TIMOUT;
  193. /* suspend program command */
  194. *addr = 0xB0;
  195. goto outahere;
  196. }
  197. }
  198. if (val & 0x1A) { /* check for error */
  199. printf ("\nFlash write error %02x at address %08lx\n",
  200. (int) val, (unsigned long) dest);
  201. if (val & (1 << 3)) {
  202. printf ("Voltage range error.\n");
  203. rc = ERR_PROG_ERROR;
  204. goto outahere;
  205. }
  206. if (val & (1 << 1)) {
  207. printf ("Device protect error.\n");
  208. rc = ERR_PROTECTED;
  209. goto outahere;
  210. }
  211. if (val & (1 << 4)) {
  212. printf ("Programming error.\n");
  213. rc = ERR_PROG_ERROR;
  214. goto outahere;
  215. }
  216. rc = ERR_PROG_ERROR;
  217. goto outahere;
  218. }
  219. outahere:
  220. /* read array command */
  221. *addr = 0xFF;
  222. if (flag)
  223. enable_interrupts ();
  224. return rc;
  225. }
  226. /*-----------------------------------------------------------------------
  227. * Copy memory to flash.
  228. */
  229. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  230. {
  231. ulong cp, wp;
  232. ushort data;
  233. int l;
  234. int i, rc;
  235. wp = (addr & ~1); /* get lower word aligned address */
  236. /*
  237. * handle unaligned start bytes
  238. */
  239. if ((l = addr - wp) != 0) {
  240. data = 0;
  241. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  242. data = (data >> 8) | (*(uchar *) cp << 8);
  243. }
  244. for (; i < 2 && cnt > 0; ++i) {
  245. data = (data >> 8) | (*src++ << 8);
  246. --cnt;
  247. ++cp;
  248. }
  249. for (; cnt == 0 && i < 2; ++i, ++cp) {
  250. data = (data >> 8) | (*(uchar *) cp << 8);
  251. }
  252. if ((rc = write_word (info, wp, data)) != 0) {
  253. return (rc);
  254. }
  255. wp += 2;
  256. }
  257. /*
  258. * handle word aligned part
  259. */
  260. while (cnt >= 2) {
  261. data = *((vu_short *) src);
  262. if ((rc = write_word (info, wp, data)) != 0) {
  263. return (rc);
  264. }
  265. src += 2;
  266. wp += 2;
  267. cnt -= 2;
  268. }
  269. if (cnt == 0) {
  270. return ERR_OK;
  271. }
  272. /*
  273. * handle unaligned tail bytes
  274. */
  275. data = 0;
  276. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  277. data = (data >> 8) | (*src++ << 8);
  278. --cnt;
  279. }
  280. for (; i < 2; ++i, ++cp) {
  281. data = (data >> 8) | (*(uchar *) cp << 8);
  282. }
  283. return write_word (info, wp, data);
  284. }