flash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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[CONFIG_SYS_MAX_FLASH_BANKS];
  28. /*-----------------------------------------------------------------------
  29. */
  30. ulong flash_init (void)
  31. {
  32. int i, j;
  33. ulong size = 0;
  34. for (i = 0; i < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  41. memset (flash_info[i].protect, 0, CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  55. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  56. &flash_info[0]);
  57. flash_protect ( FLAG_PROTECT_SET,
  58. CONFIG_ENV_ADDR,
  59. CONFIG_ENV_ADDR + CONFIG_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. ulong start;
  104. if (info->flash_id == FLASH_UNKNOWN)
  105. return ERR_UNKNOWN_FLASH_TYPE;
  106. if ((s_first < 0) || (s_first > s_last)) {
  107. return ERR_INVAL;
  108. }
  109. if ((info->flash_id & FLASH_VENDMASK) !=
  110. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  111. return ERR_UNKNOWN_FLASH_VENDOR;
  112. }
  113. prot = 0;
  114. for (sect = s_first; sect <= s_last; ++sect) {
  115. if (info->protect[sect]) {
  116. prot++;
  117. }
  118. }
  119. if (prot)
  120. return ERR_PROTECTED;
  121. /*
  122. * Disable interrupts which might cause a timeout
  123. * here. Remember that our exception vectors are
  124. * at address 0 in the flash, and we don't want a
  125. * (ticker) exception to happen while the flash
  126. * chip is in programming mode.
  127. */
  128. flag = disable_interrupts ();
  129. /* Start erase on unprotected sectors */
  130. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  131. printf ("Erasing sector %2d ... ", sect);
  132. /* arm simple, non interrupt dependent timer */
  133. start = get_timer(0);
  134. if (info->protect[sect] == 0) { /* not protected */
  135. vu_short *addr = (vu_short *) (info->start[sect]);
  136. *addr = 0x20; /* erase setup */
  137. *addr = 0xD0; /* erase confirm */
  138. while ((*addr & 0x80) != 0x80) {
  139. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  140. *addr = 0xB0; /* suspend erase */
  141. *addr = 0xFF; /* reset to read mode */
  142. rc = ERR_TIMOUT;
  143. goto outahere;
  144. }
  145. }
  146. /* clear status register command */
  147. *addr = 0x50;
  148. /* reset to read mode */
  149. *addr = 0xFF;
  150. }
  151. printf ("ok.\n");
  152. }
  153. if (ctrlc ())
  154. printf ("User Interrupt!\n");
  155. outahere:
  156. /* allow flash to settle - wait 10 ms */
  157. udelay_masked (10000);
  158. if (flag)
  159. enable_interrupts ();
  160. return rc;
  161. }
  162. /*-----------------------------------------------------------------------
  163. * Copy memory to flash
  164. */
  165. static int write_word (flash_info_t * info, ulong dest, ushort data)
  166. {
  167. vu_short *addr = (vu_short *) dest, val;
  168. int rc = ERR_OK;
  169. int flag;
  170. ulong start;
  171. /* Check if Flash is (sufficiently) erased
  172. */
  173. if ((*addr & data) != data)
  174. return ERR_NOT_ERASED;
  175. /*
  176. * Disable interrupts which might cause a timeout
  177. * here. Remember that our exception vectors are
  178. * at address 0 in the flash, and we don't want a
  179. * (ticker) exception to happen while the flash
  180. * chip is in programming mode.
  181. */
  182. flag = disable_interrupts ();
  183. /* clear status register command */
  184. *addr = 0x50;
  185. /* program set-up command */
  186. *addr = 0x40;
  187. /* latch address/data */
  188. *addr = data;
  189. /* arm simple, non interrupt dependent timer */
  190. start = get_timer(0);
  191. /* wait while polling the status register */
  192. while (((val = *addr) & 0x80) != 0x80) {
  193. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  194. rc = ERR_TIMOUT;
  195. /* suspend program command */
  196. *addr = 0xB0;
  197. goto outahere;
  198. }
  199. }
  200. if (val & 0x1A) { /* check for error */
  201. printf ("\nFlash write error %02x at address %08lx\n",
  202. (int) val, (unsigned long) dest);
  203. if (val & (1 << 3)) {
  204. printf ("Voltage range error.\n");
  205. rc = ERR_PROG_ERROR;
  206. goto outahere;
  207. }
  208. if (val & (1 << 1)) {
  209. printf ("Device protect error.\n");
  210. rc = ERR_PROTECTED;
  211. goto outahere;
  212. }
  213. if (val & (1 << 4)) {
  214. printf ("Programming error.\n");
  215. rc = ERR_PROG_ERROR;
  216. goto outahere;
  217. }
  218. rc = ERR_PROG_ERROR;
  219. goto outahere;
  220. }
  221. outahere:
  222. /* read array command */
  223. *addr = 0xFF;
  224. if (flag)
  225. enable_interrupts ();
  226. return rc;
  227. }
  228. /*-----------------------------------------------------------------------
  229. * Copy memory to flash.
  230. */
  231. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  232. {
  233. ulong cp, wp;
  234. ushort data;
  235. int l;
  236. int i, rc;
  237. wp = (addr & ~1); /* get lower word aligned address */
  238. /*
  239. * handle unaligned start bytes
  240. */
  241. if ((l = addr - wp) != 0) {
  242. data = 0;
  243. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  244. data = (data >> 8) | (*(uchar *) cp << 8);
  245. }
  246. for (; i < 2 && cnt > 0; ++i) {
  247. data = (data >> 8) | (*src++ << 8);
  248. --cnt;
  249. ++cp;
  250. }
  251. for (; cnt == 0 && i < 2; ++i, ++cp) {
  252. data = (data >> 8) | (*(uchar *) cp << 8);
  253. }
  254. if ((rc = write_word (info, wp, data)) != 0) {
  255. return (rc);
  256. }
  257. wp += 2;
  258. }
  259. /*
  260. * handle word aligned part
  261. */
  262. while (cnt >= 2) {
  263. data = *((vu_short *) src);
  264. if ((rc = write_word (info, wp, data)) != 0) {
  265. return (rc);
  266. }
  267. src += 2;
  268. wp += 2;
  269. cnt -= 2;
  270. }
  271. if (cnt == 0) {
  272. return ERR_OK;
  273. }
  274. /*
  275. * handle unaligned tail bytes
  276. */
  277. data = 0;
  278. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  279. data = (data >> 8) | (*src++ << 8);
  280. --cnt;
  281. }
  282. for (; i < 2; ++i, ++cp) {
  283. data = (data >> 8) | (*(uchar *) cp << 8);
  284. }
  285. return write_word (info, wp, data);
  286. }