flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * (C) Copyright 2000
  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. /*
  24. * Modified 4/5/2001
  25. * Wait for completion of each sector erase command issued
  26. * 4/5/2001
  27. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  28. */
  29. /*
  30. * Modified July 20, 2001
  31. * Strip down to support ONLY the AMD29F032B.
  32. * Dave Updegraff - Cray, Inc. dave@cray.com
  33. */
  34. #include <common.h>
  35. #include <ppc4xx.h>
  36. #include <asm/processor.h>
  37. /* The flash chip we use... */
  38. #define AMD_ID_F032B 0x41 /* 29F032B ID 32 Mbit,64 64Kx8 sectors */
  39. #define FLASH_AM320B 0x0009
  40. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  41. /*-----------------------------------------------------------------------
  42. * Functions
  43. */
  44. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  45. static int write_word (flash_info_t *info, ulong dest, ulong data);
  46. static void flash_get_offsets (ulong base, flash_info_t *info);
  47. #define ADDR0 0x5555
  48. #define ADDR1 0x2aaa
  49. #define FLASH_WORD_SIZE unsigned char
  50. /*-----------------------------------------------------------------------
  51. */
  52. unsigned long flash_init (void)
  53. {
  54. unsigned long size_b0, size_b1;
  55. int i;
  56. /* Init: no FLASHes known */
  57. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  58. flash_info[i].flash_id = FLASH_UNKNOWN;
  59. }
  60. /* Static FLASH Bank configuration here - FIXME XXX */
  61. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  62. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  63. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  64. size_b0, size_b0<<20);
  65. }
  66. /* Only one bank */
  67. if (CONFIG_SYS_MAX_FLASH_BANKS == 1)
  68. {
  69. /* Setup offsets */
  70. flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
  71. #if 0
  72. /* Monitor protection ON by default */
  73. (void)flash_protect(FLAG_PROTECT_SET,
  74. FLASH_BASE0_PRELIM,
  75. FLASH_BASE0_PRELIM+monitor_flash_len-1,
  76. &flash_info[0]);
  77. #endif
  78. size_b1 = 0 ;
  79. flash_info[0].size = size_b0;
  80. }
  81. return (size_b0 + size_b1);
  82. }
  83. /*-----------------------------------------------------------------------
  84. */
  85. static void flash_get_offsets (ulong base, flash_info_t *info)
  86. {
  87. int i;
  88. /* set up sector start address table */
  89. for (i = 0; i < info->sector_count; i++)
  90. info->start[i] = base + (i * 0x00010000);
  91. }
  92. /*-----------------------------------------------------------------------
  93. */
  94. void flash_print_info (flash_info_t *info)
  95. {
  96. int i;
  97. int k;
  98. int size;
  99. int erased;
  100. volatile unsigned long *flash;
  101. if (info->flash_id == FLASH_UNKNOWN) {
  102. printf ("missing or unknown FLASH type\n");
  103. return;
  104. }
  105. switch (info->flash_id & FLASH_VENDMASK) {
  106. case FLASH_MAN_AMD: printf ("AMD "); break;
  107. default: printf ("Unknown Vendor "); break;
  108. }
  109. switch (info->flash_id & FLASH_TYPEMASK) {
  110. case FLASH_AM320B:printf ("AM29F032B (32 Mbit 64x64KB uniform sectors)\n");
  111. break;
  112. default: printf ("Unknown Chip Type\n");
  113. break;
  114. }
  115. printf (" Size: %ld KB in %d Sectors\n",
  116. info->size >> 10, info->sector_count);
  117. printf (" Sector Start Addresses:");
  118. for (i=0; i<info->sector_count; ++i) {
  119. /*
  120. * Check if whole sector is erased
  121. */
  122. if (i != (info->sector_count-1))
  123. size = info->start[i+1] - info->start[i];
  124. else
  125. size = info->start[0] + info->size - info->start[i];
  126. erased = 1;
  127. flash = (volatile unsigned long *)info->start[i];
  128. size = size >> 2; /* divide by 4 for longword access */
  129. for (k=0; k<size; k++)
  130. {
  131. if (*flash++ != 0xffffffff)
  132. {
  133. erased = 0;
  134. break;
  135. }
  136. }
  137. if ((i % 5) == 0)
  138. printf ("\n ");
  139. printf (" %08lX%s%s",
  140. info->start[i],
  141. erased ? " E" : " ",
  142. info->protect[i] ? "RO " : " "
  143. );
  144. }
  145. printf ("\n");
  146. }
  147. /*-----------------------------------------------------------------------
  148. */
  149. /*-----------------------------------------------------------------------
  150. */
  151. /*
  152. * The following code cannot be run from FLASH!
  153. */
  154. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  155. {
  156. short i;
  157. FLASH_WORD_SIZE value;
  158. ulong base = (ulong)addr;
  159. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  160. /* Write auto select command: read Manufacturer ID */
  161. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  162. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  163. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  164. value = addr2[0];
  165. switch (value) {
  166. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  167. info->flash_id = FLASH_MAN_AMD;
  168. break;
  169. default:
  170. info->flash_id = FLASH_UNKNOWN;
  171. info->sector_count = 0;
  172. info->size = 0;
  173. return (0); /* no or unknown flash */
  174. }
  175. value = addr2[1]; /* device ID */
  176. switch (value) {
  177. case (FLASH_WORD_SIZE)AMD_ID_F032B:
  178. info->flash_id += FLASH_AM320B;
  179. info->sector_count = 64;
  180. info->size = 0x0400000; /* => 4 MB */
  181. break;
  182. default:
  183. info->flash_id = FLASH_UNKNOWN;
  184. return (0); /* => no or unknown flash */
  185. }
  186. /* set up sector start address table */
  187. for (i = 0; i < info->sector_count; i++)
  188. info->start[i] = base + (i * 0x00010000);
  189. /* check for protected sectors */
  190. for (i = 0; i < info->sector_count; i++) {
  191. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  192. /* D0 = 1 if protected */
  193. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  194. info->protect[i] = addr2[2] & 1;
  195. }
  196. /*
  197. * Prevent writes to uninitialized FLASH.
  198. */
  199. if (info->flash_id != FLASH_UNKNOWN) {
  200. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  201. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  202. }
  203. return (info->size);
  204. }
  205. int wait_for_DQ7(flash_info_t *info, int sect)
  206. {
  207. ulong start, now, last;
  208. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  209. start = get_timer (0);
  210. last = start;
  211. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  212. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  213. printf ("Timeout\n");
  214. return -1;
  215. }
  216. /* show that we're waiting */
  217. if ((now - last) > 1000) { /* every second */
  218. putc ('.');
  219. last = now;
  220. }
  221. }
  222. return 0;
  223. }
  224. /*-----------------------------------------------------------------------
  225. */
  226. int flash_erase (flash_info_t *info, int s_first, int s_last)
  227. {
  228. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  229. volatile FLASH_WORD_SIZE *addr2;
  230. int flag, prot, sect, l_sect;
  231. if ((s_first < 0) || (s_first > s_last)) {
  232. if (info->flash_id == FLASH_UNKNOWN) {
  233. printf ("- missing\n");
  234. } else {
  235. printf ("- no sectors to erase\n");
  236. }
  237. return 1;
  238. }
  239. if (info->flash_id == FLASH_UNKNOWN) {
  240. printf ("Can't erase unknown flash type - aborted\n");
  241. return 1;
  242. }
  243. prot = 0;
  244. for (sect=s_first; sect<=s_last; ++sect) {
  245. if (info->protect[sect]) {
  246. prot++;
  247. }
  248. }
  249. if (prot) {
  250. printf ("- Warning: %d protected sectors will not be erased!\n",
  251. prot);
  252. } else {
  253. printf ("\n");
  254. }
  255. l_sect = -1;
  256. /* Disable interrupts which might cause a timeout here */
  257. flag = disable_interrupts();
  258. /* Start erase on unprotected sectors */
  259. for (sect = s_first; sect<=s_last; sect++) {
  260. if (info->protect[sect] == 0) { /* not protected */
  261. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  262. printf("Erasing sector %p\n", addr2);
  263. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  264. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  265. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  266. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  267. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  268. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  269. l_sect = sect;
  270. /*
  271. * Wait for each sector to complete, it's more
  272. * reliable. According to AMD Spec, you must
  273. * issue all erase commands within a specified
  274. * timeout. This has been seen to fail, especially
  275. * if printf()s are included (for debug)!!
  276. */
  277. wait_for_DQ7(info, sect);
  278. }
  279. }
  280. /* re-enable interrupts if necessary */
  281. if (flag)
  282. enable_interrupts();
  283. /* wait at least 80us - let's wait 1 ms */
  284. udelay (1000);
  285. /* reset to read mode */
  286. addr = (FLASH_WORD_SIZE *)info->start[0];
  287. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  288. printf (" done\n");
  289. return 0;
  290. }
  291. /*-----------------------------------------------------------------------
  292. * Copy memory to flash, returns:
  293. * 0 - OK
  294. * 1 - write timeout
  295. * 2 - Flash not erased
  296. */
  297. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  298. {
  299. ulong cp, wp, data;
  300. int i, l, rc;
  301. wp = (addr & ~3); /* get lower word aligned address */
  302. /*
  303. * handle unaligned start bytes
  304. */
  305. if ((l = addr - wp) != 0) {
  306. data = 0;
  307. for (i=0, cp=wp; i<l; ++i, ++cp) {
  308. data = (data << 8) | (*(uchar *)cp);
  309. }
  310. for (; i<4 && cnt>0; ++i) {
  311. data = (data << 8) | *src++;
  312. --cnt;
  313. ++cp;
  314. }
  315. for (; cnt==0 && i<4; ++i, ++cp) {
  316. data = (data << 8) | (*(uchar *)cp);
  317. }
  318. if ((rc = write_word(info, wp, data)) != 0) {
  319. return (rc);
  320. }
  321. wp += 4;
  322. }
  323. /*
  324. * handle word aligned part
  325. */
  326. while (cnt >= 4) {
  327. data = 0;
  328. for (i=0; i<4; ++i) {
  329. data = (data << 8) | *src++;
  330. }
  331. if ((rc = write_word(info, wp, data)) != 0) {
  332. return (rc);
  333. }
  334. wp += 4;
  335. cnt -= 4;
  336. }
  337. if (cnt == 0) {
  338. return (0);
  339. }
  340. /*
  341. * handle unaligned tail bytes
  342. */
  343. data = 0;
  344. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  345. data = (data << 8) | *src++;
  346. --cnt;
  347. }
  348. for (; i<4; ++i, ++cp) {
  349. data = (data << 8) | (*(uchar *)cp);
  350. }
  351. return (write_word(info, wp, data));
  352. }
  353. /*-----------------------------------------------------------------------
  354. * Write a word to Flash, returns:
  355. * 0 - OK
  356. * 1 - write timeout
  357. * 2 - Flash not erased
  358. */
  359. static int write_word (flash_info_t *info, ulong dest, ulong data)
  360. {
  361. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
  362. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  363. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  364. ulong start;
  365. int flag;
  366. int i;
  367. /* Check if Flash is (sufficiently) erased */
  368. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  369. (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  370. return (2);
  371. }
  372. /* Disable interrupts which might cause a timeout here */
  373. flag = disable_interrupts();
  374. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  375. {
  376. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  377. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  378. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
  379. dest2[i] = data2[i];
  380. /* re-enable interrupts if necessary */
  381. if (flag)
  382. enable_interrupts();
  383. /* data polling for D7 */
  384. start = get_timer (0);
  385. while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
  386. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  387. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  388. return (1);
  389. }
  390. }
  391. }
  392. return (0);
  393. }
  394. /*-----------------------------------------------------------------------
  395. */