flash.c 13 KB

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