flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <asm/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;
  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. /* Disable interrupts which might cause a timeout here */
  256. flag = disable_interrupts();
  257. /* Start erase on unprotected sectors */
  258. for (sect = s_first; sect<=s_last; sect++) {
  259. if (info->protect[sect] == 0) { /* not protected */
  260. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  261. printf("Erasing sector %p\n", addr2);
  262. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  263. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  264. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  265. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  266. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  267. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  268. /*
  269. * Wait for each sector to complete, it's more
  270. * reliable. According to AMD Spec, you must
  271. * issue all erase commands within a specified
  272. * timeout. This has been seen to fail, especially
  273. * if printf()s are included (for debug)!!
  274. */
  275. wait_for_DQ7(info, sect);
  276. }
  277. }
  278. /* re-enable interrupts if necessary */
  279. if (flag)
  280. enable_interrupts();
  281. /* wait at least 80us - let's wait 1 ms */
  282. udelay (1000);
  283. /* reset to read mode */
  284. addr = (FLASH_WORD_SIZE *)info->start[0];
  285. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  286. printf (" done\n");
  287. return 0;
  288. }
  289. /*-----------------------------------------------------------------------
  290. * Copy memory to flash, returns:
  291. * 0 - OK
  292. * 1 - write timeout
  293. * 2 - Flash not erased
  294. */
  295. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  296. {
  297. ulong cp, wp, data;
  298. int i, l, rc;
  299. wp = (addr & ~3); /* get lower word aligned address */
  300. /*
  301. * handle unaligned start bytes
  302. */
  303. if ((l = addr - wp) != 0) {
  304. data = 0;
  305. for (i=0, cp=wp; i<l; ++i, ++cp) {
  306. data = (data << 8) | (*(uchar *)cp);
  307. }
  308. for (; i<4 && cnt>0; ++i) {
  309. data = (data << 8) | *src++;
  310. --cnt;
  311. ++cp;
  312. }
  313. for (; cnt==0 && i<4; ++i, ++cp) {
  314. data = (data << 8) | (*(uchar *)cp);
  315. }
  316. if ((rc = write_word(info, wp, data)) != 0) {
  317. return (rc);
  318. }
  319. wp += 4;
  320. }
  321. /*
  322. * handle word aligned part
  323. */
  324. while (cnt >= 4) {
  325. data = 0;
  326. for (i=0; i<4; ++i) {
  327. data = (data << 8) | *src++;
  328. }
  329. if ((rc = write_word(info, wp, data)) != 0) {
  330. return (rc);
  331. }
  332. wp += 4;
  333. cnt -= 4;
  334. }
  335. if (cnt == 0) {
  336. return (0);
  337. }
  338. /*
  339. * handle unaligned tail bytes
  340. */
  341. data = 0;
  342. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  343. data = (data << 8) | *src++;
  344. --cnt;
  345. }
  346. for (; i<4; ++i, ++cp) {
  347. data = (data << 8) | (*(uchar *)cp);
  348. }
  349. return (write_word(info, wp, data));
  350. }
  351. /*-----------------------------------------------------------------------
  352. * Write a word to Flash, returns:
  353. * 0 - OK
  354. * 1 - write timeout
  355. * 2 - Flash not erased
  356. */
  357. static int write_word (flash_info_t *info, ulong dest, ulong data)
  358. {
  359. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
  360. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  361. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  362. ulong start;
  363. int flag;
  364. int i;
  365. /* Check if Flash is (sufficiently) erased */
  366. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  367. (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  368. return (2);
  369. }
  370. /* Disable interrupts which might cause a timeout here */
  371. flag = disable_interrupts();
  372. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  373. {
  374. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  375. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  376. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
  377. dest2[i] = data2[i];
  378. /* re-enable interrupts if necessary */
  379. if (flag)
  380. enable_interrupts();
  381. /* data polling for D7 */
  382. start = get_timer (0);
  383. while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
  384. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  385. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  386. return (1);
  387. }
  388. }
  389. }
  390. return (0);
  391. }
  392. /*-----------------------------------------------------------------------
  393. */