flash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 CONFIG_SYS_FLASH_BASE
  25. #define FLASH_BANK_SIZE 0x800000
  26. #define EN29LV640 0x227e227e
  27. flash_info_t flash_info[CONFIG_SYS_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 < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  73. memset (flash_info[i].protect, 0, CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  85. CONFIG_SYS_FLASH_BASE + 0x2ffff, &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 *)(CONFIG_SYS_FLASH_BASE + (0x00000555<<1)))
  96. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_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. ulong start;
  111. /* first look for protection bits */
  112. if (info->flash_id == FLASH_UNKNOWN)
  113. return ERR_UNKNOWN_FLASH_TYPE;
  114. if ((s_first < 0) || (s_first > s_last)) {
  115. return ERR_INVAL;
  116. }
  117. if ((info->flash_id & FLASH_VENDMASK) !=
  118. (AMD_MANUFACT & FLASH_VENDMASK)) {
  119. return ERR_UNKNOWN_FLASH_VENDOR;
  120. }
  121. prot = 0;
  122. for (sect = s_first; sect <= s_last; ++sect) {
  123. if (info->protect[sect]) {
  124. prot++;
  125. }
  126. }
  127. if (prot)
  128. return ERR_PROTECTED;
  129. /*
  130. * Disable interrupts which might cause a timeout
  131. * here. Remember that our exception vectors are
  132. * at address 0 in the flash, and we don't want a
  133. * (ticker) exception to happen while the flash
  134. * chip is in programming mode.
  135. */
  136. iflag = disable_interrupts ();
  137. printf ("\n");
  138. /* Start erase on unprotected sectors */
  139. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  140. printf ("Erasing sector %2d ... ", sect);
  141. /* arm simple, non interrupt dependent timer */
  142. start = get_timer(0);
  143. if (info->protect[sect] == 0) { /* not protected */
  144. volatile u16 *addr =
  145. (volatile u16 *) (info->start[sect]);
  146. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  147. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  148. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  149. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  150. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  151. *addr = CMD_ERASE_CONFIRM;
  152. /* wait until flash is ready */
  153. chip1 = 0;
  154. do {
  155. result = *addr;
  156. /* check timeout */
  157. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
  158. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  159. chip1 = TMO;
  160. break;
  161. }
  162. if (!chip1
  163. && (result & 0xFFFF) & BIT_ERASE_DONE)
  164. chip1 = READY;
  165. } while (!chip1);
  166. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  167. if (chip1 == ERR) {
  168. rc = ERR_PROG_ERROR;
  169. goto outahere;
  170. }
  171. if (chip1 == TMO) {
  172. rc = ERR_TIMOUT;
  173. goto outahere;
  174. }
  175. printf ("ok.\n");
  176. } else { /* it was protected */
  177. printf ("protected!\n");
  178. }
  179. }
  180. if (ctrlc ())
  181. printf ("User Interrupt!\n");
  182. outahere:
  183. /* allow flash to settle - wait 10 ms */
  184. printf("Waiting 10 ms...");
  185. udelay (10000);
  186. /* for (i = 0; i < 10 * 1000 * 1000; ++i)
  187. asm(" nop");
  188. */
  189. printf("done\n");
  190. if (iflag)
  191. enable_interrupts ();
  192. return rc;
  193. }
  194. static int write_word (flash_info_t * info, ulong dest, ulong data)
  195. {
  196. volatile u16 *addr = (volatile u16 *) dest;
  197. ulong result;
  198. int rc = ERR_OK;
  199. int iflag;
  200. int chip1;
  201. ulong start;
  202. /*
  203. * Check if Flash is (sufficiently) erased
  204. */
  205. result = *addr;
  206. if ((result & data) != data)
  207. return ERR_NOT_ERASED;
  208. /*
  209. * Disable interrupts which might cause a timeout
  210. * here. Remember that our exception vectors are
  211. * at address 0 in the flash, and we don't want a
  212. * (ticker) exception to happen while the flash
  213. * chip is in programming mode.
  214. */
  215. iflag = disable_interrupts ();
  216. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  217. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  218. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  219. *addr = data;
  220. /* arm simple, non interrupt dependent timer */
  221. start = get_timer(0);
  222. /* wait until flash is ready */
  223. chip1 = 0;
  224. do {
  225. result = *addr;
  226. /* check timeout */
  227. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
  228. chip1 = ERR | TMO;
  229. break;
  230. }
  231. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  232. chip1 = READY;
  233. } while (!chip1);
  234. *addr = CMD_READ_ARRAY;
  235. if (chip1 == ERR || *addr != data)
  236. rc = ERR_PROG_ERROR;
  237. if (iflag)
  238. enable_interrupts ();
  239. return rc;
  240. }
  241. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  242. {
  243. ulong wp, data;
  244. int rc;
  245. if (addr & 1) {
  246. printf ("unaligned destination not supported\n");
  247. return ERR_ALIGN;
  248. }
  249. #if 0
  250. if (cnt & 1) {
  251. printf ("odd transfer sizes not supported\n");
  252. return ERR_ALIGN;
  253. }
  254. #endif
  255. wp = addr;
  256. if (addr & 1) {
  257. data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
  258. src);
  259. if ((rc = write_word (info, wp - 1, data)) != 0) {
  260. return (rc);
  261. }
  262. src += 1;
  263. wp += 1;
  264. cnt -= 1;
  265. }
  266. while (cnt >= 2) {
  267. data = *((volatile u16 *) src);
  268. if ((rc = write_word (info, wp, data)) != 0) {
  269. return (rc);
  270. }
  271. src += 2;
  272. wp += 2;
  273. cnt -= 2;
  274. }
  275. if (cnt == 1) {
  276. data = (*((volatile u8 *) src) << 8) |
  277. *((volatile u8 *) (wp + 1));
  278. if ((rc = write_word (info, wp, data)) != 0) {
  279. return (rc);
  280. }
  281. src += 1;
  282. wp += 1;
  283. cnt -= 1;
  284. }
  285. return ERR_OK;
  286. }