flash.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define PHYS_FLASH_1 CFG_FLASH_BASE
  25. #define FLASH_BANK_SIZE 0x800000
  26. #define EN29LV640 0x227e227e
  27. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  28. void flash_print_info (flash_info_t * info)
  29. {
  30. int i;
  31. switch (info->flash_id & FLASH_VENDMASK) {
  32. case (AMD_MANUFACT & FLASH_VENDMASK):
  33. printf ("AMD: ");
  34. break;
  35. default:
  36. printf ("Unknown Vendor ");
  37. break;
  38. }
  39. switch (info->flash_id & FLASH_TYPEMASK) {
  40. case (EN29LV640 & FLASH_TYPEMASK):
  41. printf ("EN29LV640 (16Mbit)\n");
  42. break;
  43. default:
  44. printf ("Unknown Chip Type\n");
  45. goto Done;
  46. break;
  47. }
  48. printf (" Size: %ld MB in %d Sectors\n",
  49. info->size >> 20, info->sector_count);
  50. printf (" Sector Start Addresses:");
  51. for (i = 0; i < info->sector_count; i++) {
  52. if ((i % 5) == 0) {
  53. printf ("\n ");
  54. }
  55. printf (" %08lX%s", info->start[i],
  56. info->protect[i] ? " (RO)" : " ");
  57. }
  58. printf ("\n");
  59. Done:
  60. return;
  61. }
  62. unsigned long flash_init (void)
  63. {
  64. int i, j;
  65. ulong size = 0;
  66. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  67. ulong flashbase = 0;
  68. flash_info[i].flash_id =
  69. (AMD_MANUFACT & FLASH_VENDMASK) |
  70. (EN29LV640 & FLASH_TYPEMASK);
  71. flash_info[i].size = FLASH_BANK_SIZE;
  72. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  73. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  74. if (i == 0)
  75. flashbase = PHYS_FLASH_1;
  76. else
  77. panic ("configured to many flash banks!\n");
  78. for (j = 0; j < flash_info[i].sector_count; j++) {
  79. flash_info[i].start[j] = flashbase + 0x10000 * j;
  80. }
  81. size += flash_info[i].size;
  82. }
  83. flash_protect (FLAG_PROTECT_SET,
  84. CFG_FLASH_BASE,
  85. CFG_FLASH_BASE + 0x30000, &flash_info[0]);
  86. return size;
  87. }
  88. #define CMD_READ_ARRAY 0x00F0
  89. #define CMD_UNLOCK1 0x00AA
  90. #define CMD_UNLOCK2 0x0055
  91. #define CMD_ERASE_SETUP 0x0080
  92. #define CMD_ERASE_CONFIRM 0x0030
  93. #define CMD_PROGRAM 0x00A0
  94. #define CMD_UNLOCK_BYPASS 0x0020
  95. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
  96. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
  97. #define BIT_ERASE_DONE 0x0080
  98. #define BIT_RDY_MASK 0x0080
  99. #define BIT_PROGRAM_ERROR 0x0020
  100. #define BIT_TIMEOUT 0x80000000 /* our flag */
  101. #define READY 1
  102. #define ERR 2
  103. #define TMO 4
  104. int flash_erase (flash_info_t * info, int s_first, int s_last)
  105. {
  106. ulong result;
  107. int iflag, prot, sect;
  108. int rc = ERR_OK;
  109. int chip1;
  110. /* first look for protection bits */
  111. if (info->flash_id == FLASH_UNKNOWN)
  112. return ERR_UNKNOWN_FLASH_TYPE;
  113. if ((s_first < 0) || (s_first > s_last)) {
  114. return ERR_INVAL;
  115. }
  116. if ((info->flash_id & FLASH_VENDMASK) !=
  117. (AMD_MANUFACT & FLASH_VENDMASK)) {
  118. return ERR_UNKNOWN_FLASH_VENDOR;
  119. }
  120. prot = 0;
  121. for (sect = s_first; sect <= s_last; ++sect) {
  122. if (info->protect[sect]) {
  123. prot++;
  124. }
  125. }
  126. if (prot)
  127. return ERR_PROTECTED;
  128. /*
  129. * Disable interrupts which might cause a timeout
  130. * here. Remember that our exception vectors are
  131. * at address 0 in the flash, and we don't want a
  132. * (ticker) exception to happen while the flash
  133. * chip is in programming mode.
  134. */
  135. iflag = disable_interrupts ();
  136. printf ("\n");
  137. /* Start erase on unprotected sectors */
  138. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  139. printf ("Erasing sector %2d ... ", sect);
  140. /* arm simple, non interrupt dependent timer */
  141. set_timer (0);
  142. if (info->protect[sect] == 0) { /* not protected */
  143. volatile u16 *addr =
  144. (volatile u16 *) (info->start[sect]);
  145. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  146. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  147. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  148. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  149. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  150. *addr = CMD_ERASE_CONFIRM;
  151. /* wait until flash is ready */
  152. chip1 = 0;
  153. do {
  154. result = *addr;
  155. /* check timeout */
  156. if (get_timer (0) > CFG_FLASH_ERASE_TOUT * CFG_HZ / 1000) {
  157. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  158. chip1 = TMO;
  159. break;
  160. }
  161. if (!chip1
  162. && (result & 0xFFFF) & BIT_ERASE_DONE)
  163. chip1 = READY;
  164. } while (!chip1);
  165. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  166. if (chip1 == ERR) {
  167. rc = ERR_PROG_ERROR;
  168. goto outahere;
  169. }
  170. if (chip1 == TMO) {
  171. rc = ERR_TIMOUT;
  172. goto outahere;
  173. }
  174. printf ("ok.\n");
  175. } else { /* it was protected */
  176. printf ("protected!\n");
  177. }
  178. }
  179. if (ctrlc ())
  180. printf ("User Interrupt!\n");
  181. outahere:
  182. /* allow flash to settle - wait 10 ms */
  183. printf("Waiting 10 ms...");
  184. udelay (10000);
  185. /* for (i = 0; i < 10 * 1000 * 1000; ++i)
  186. asm(" nop");
  187. */
  188. printf("done\n");
  189. if (iflag)
  190. enable_interrupts ();
  191. return rc;
  192. }
  193. static int write_word (flash_info_t * info, ulong dest, ulong data)
  194. {
  195. volatile u16 *addr = (volatile u16 *) dest;
  196. ulong result;
  197. int rc = ERR_OK;
  198. int iflag;
  199. int chip1;
  200. /*
  201. * Check if Flash is (sufficiently) erased
  202. */
  203. result = *addr;
  204. if ((result & data) != data)
  205. return ERR_NOT_ERASED;
  206. /*
  207. * Disable interrupts which might cause a timeout
  208. * here. Remember that our exception vectors are
  209. * at address 0 in the flash, and we don't want a
  210. * (ticker) exception to happen while the flash
  211. * chip is in programming mode.
  212. */
  213. iflag = disable_interrupts ();
  214. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  215. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  216. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  217. *addr = data;
  218. /* arm simple, non interrupt dependent timer */
  219. set_timer (0);
  220. /* wait until flash is ready */
  221. chip1 = 0;
  222. do {
  223. result = *addr;
  224. /* check timeout */
  225. if (get_timer (0) > CFG_FLASH_ERASE_TOUT * CFG_HZ / 1000) {
  226. chip1 = ERR | TMO;
  227. break;
  228. }
  229. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  230. chip1 = READY;
  231. } while (!chip1);
  232. *addr = CMD_READ_ARRAY;
  233. if (chip1 == ERR || *addr != data)
  234. rc = ERR_PROG_ERROR;
  235. if (iflag)
  236. enable_interrupts ();
  237. return rc;
  238. }
  239. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  240. {
  241. ulong wp, data;
  242. int rc;
  243. if (addr & 1) {
  244. printf ("unaligned destination not supported\n");
  245. return ERR_ALIGN;
  246. }
  247. #if 0
  248. if (cnt & 1) {
  249. printf ("odd transfer sizes not supported\n");
  250. return ERR_ALIGN;
  251. }
  252. #endif
  253. wp = addr;
  254. if (addr & 1) {
  255. data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
  256. src);
  257. if ((rc = write_word (info, wp - 1, data)) != 0) {
  258. return (rc);
  259. }
  260. src += 1;
  261. wp += 1;
  262. cnt -= 1;
  263. }
  264. while (cnt >= 2) {
  265. data = *((volatile u16 *) src);
  266. if ((rc = write_word (info, wp, data)) != 0) {
  267. return (rc);
  268. }
  269. src += 2;
  270. wp += 2;
  271. cnt -= 2;
  272. }
  273. if (cnt == 1) {
  274. data = (*((volatile u8 *) src) << 8) |
  275. *((volatile u8 *) (wp + 1));
  276. if ((rc = write_word (info, wp, data)) != 0) {
  277. return (rc);
  278. }
  279. src += 1;
  280. wp += 1;
  281. cnt -= 1;
  282. }
  283. return ERR_OK;
  284. }