flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * (C) Copyright 2002-2005
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
  6. * Add support for Am29F016D and dynamic switch setting.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * Ported from Ebony flash support
  28. * Travis B. Sawyer
  29. * Sandburst Corporation
  30. */
  31. #include <common.h>
  32. #include <ppc4xx.h>
  33. #include <asm/processor.h>
  34. #undef DEBUG
  35. #ifdef DEBUG
  36. #define DEBUGF(x...) printf(x)
  37. #else
  38. #define DEBUGF(x...)
  39. #endif /* DEBUG */
  40. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  41. static unsigned long flash_addr_table[8][CONFIG_SYS_MAX_FLASH_BANKS] = {
  42. {0xfff80000} /* Boot Flash */
  43. };
  44. /*-----------------------------------------------------------------------
  45. * Functions
  46. */
  47. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  48. static int write_word (flash_info_t *info, ulong dest, ulong data);
  49. #define ADDR0 0x5555
  50. #define ADDR1 0x2aaa
  51. #define FLASH_WORD_SIZE unsigned char
  52. /*-----------------------------------------------------------------------
  53. */
  54. unsigned long flash_init (void)
  55. {
  56. unsigned long total_b = 0;
  57. unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
  58. unsigned short index = 0;
  59. int i;
  60. DEBUGF("\n");
  61. DEBUGF("FLASH: Index: %d\n", index);
  62. /* Init: no FLASHes known */
  63. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  64. flash_info[i].flash_id = FLASH_UNKNOWN;
  65. flash_info[i].sector_count = -1;
  66. flash_info[i].size = 0;
  67. /* check whether the address is 0 */
  68. if (flash_addr_table[index][i] == 0) {
  69. continue;
  70. }
  71. /* call flash_get_size() to initialize sector address */
  72. size_b[i] = flash_get_size(
  73. (vu_long *)flash_addr_table[index][i], &flash_info[i]);
  74. flash_info[i].size = size_b[i];
  75. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  76. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  77. i, size_b[i], size_b[i]<<20);
  78. flash_info[i].sector_count = -1;
  79. flash_info[i].size = 0;
  80. }
  81. total_b += flash_info[i].size;
  82. }
  83. return total_b;
  84. }
  85. /*-----------------------------------------------------------------------
  86. */
  87. void flash_print_info (flash_info_t *info)
  88. {
  89. int i;
  90. int k;
  91. int size;
  92. int erased;
  93. volatile unsigned long *flash;
  94. if (info->flash_id == FLASH_UNKNOWN) {
  95. printf ("missing or unknown FLASH type\n");
  96. return;
  97. }
  98. switch (info->flash_id & FLASH_VENDMASK) {
  99. case FLASH_MAN_AMD: printf ("AMD "); break;
  100. default: printf ("Unknown Vendor "); break;
  101. }
  102. switch (info->flash_id & FLASH_TYPEMASK) {
  103. case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  104. break;
  105. default: printf ("Unknown Chip Type\n");
  106. break;
  107. }
  108. printf (" Size: %ld KB in %d Sectors\n",
  109. info->size >> 10, info->sector_count);
  110. printf (" Sector Start Addresses:");
  111. for (i=0; i<info->sector_count; ++i) {
  112. /*
  113. * Check if whole sector is erased
  114. */
  115. if (i != (info->sector_count-1))
  116. size = info->start[i+1] - info->start[i];
  117. else
  118. size = info->start[0] + info->size - info->start[i];
  119. erased = 1;
  120. flash = (volatile unsigned long *)info->start[i];
  121. size = size >> 2; /* divide by 4 for longword access */
  122. for (k=0; k<size; k++)
  123. {
  124. if (*flash++ != 0xffffffff)
  125. {
  126. erased = 0;
  127. break;
  128. }
  129. }
  130. if ((i % 5) == 0)
  131. printf ("\n ");
  132. printf (" %08lX%s%s",
  133. info->start[i],
  134. erased ? " E" : " ",
  135. info->protect[i] ? "RO " : " "
  136. );
  137. }
  138. printf ("\n");
  139. return;
  140. }
  141. /*-----------------------------------------------------------------------
  142. */
  143. /*-----------------------------------------------------------------------
  144. */
  145. /*
  146. * The following code cannot be run from FLASH!
  147. */
  148. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  149. {
  150. short i;
  151. FLASH_WORD_SIZE value;
  152. ulong base = (ulong)addr;
  153. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  154. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr );
  155. /* Write auto select command: read Manufacturer ID */
  156. udelay(10000);
  157. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  158. udelay(1000);
  159. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  160. udelay(1000);
  161. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  162. udelay(1000);
  163. value = addr2[0];
  164. DEBUGF("FLASH MANUFACT: %x\n", value);
  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. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  177. switch (value) {
  178. case (FLASH_WORD_SIZE)AMD_ID_LV040B:
  179. info->flash_id += FLASH_AM040;
  180. info->sector_count = 8;
  181. info->size = 0x00080000; /* => 512 kb */
  182. break;
  183. default:
  184. info->flash_id = FLASH_UNKNOWN;
  185. return (0); /* => no or unknown flash */
  186. }
  187. /* set up sector start address table */
  188. if (info->flash_id == FLASH_AM040) {
  189. for (i = 0; i < info->sector_count; i++)
  190. info->start[i] = base + (i * 0x00010000);
  191. } else {
  192. if (info->flash_id & FLASH_BTYPE) {
  193. /* set sector offsets for bottom boot block type */
  194. info->start[0] = base + 0x00000000;
  195. info->start[1] = base + 0x00004000;
  196. info->start[2] = base + 0x00006000;
  197. info->start[3] = base + 0x00008000;
  198. for (i = 4; i < info->sector_count; i++) {
  199. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  200. }
  201. } else {
  202. /* set sector offsets for top boot block type */
  203. i = info->sector_count - 1;
  204. info->start[i--] = base + info->size - 0x00004000;
  205. info->start[i--] = base + info->size - 0x00006000;
  206. info->start[i--] = base + info->size - 0x00008000;
  207. for (; i >= 0; i--) {
  208. info->start[i] = base + i * 0x00010000;
  209. }
  210. }
  211. }
  212. /* check for protected sectors */
  213. for (i = 0; i < info->sector_count; i++) {
  214. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  215. /* D0 = 1 if protected */
  216. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  217. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  218. info->protect[i] = 0;
  219. else
  220. info->protect[i] = addr2[2] & 1;
  221. }
  222. /* reset to return to reading data */
  223. addr2[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  224. /*
  225. * Prevent writes to uninitialized FLASH.
  226. */
  227. if (info->flash_id != FLASH_UNKNOWN) {
  228. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  229. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  230. }
  231. return (info->size);
  232. }
  233. int wait_for_DQ7(flash_info_t *info, int sect)
  234. {
  235. ulong start, now, last;
  236. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  237. start = get_timer (0);
  238. last = start;
  239. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  240. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  241. printf ("Timeout\n");
  242. return -1;
  243. }
  244. /* show that we're waiting */
  245. if ((now - last) > 1000) { /* every second */
  246. putc ('.');
  247. last = now;
  248. }
  249. }
  250. return 0;
  251. }
  252. /*-----------------------------------------------------------------------
  253. */
  254. int flash_erase (flash_info_t *info, int s_first, int s_last)
  255. {
  256. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  257. volatile FLASH_WORD_SIZE *addr2;
  258. int flag, prot, sect, l_sect;
  259. int i;
  260. if ((s_first < 0) || (s_first > s_last)) {
  261. if (info->flash_id == FLASH_UNKNOWN) {
  262. printf ("- missing\n");
  263. } else {
  264. printf ("- no sectors to erase\n");
  265. }
  266. return 1;
  267. }
  268. if (info->flash_id == FLASH_UNKNOWN) {
  269. printf ("Can't erase unknown flash type - aborted\n");
  270. return 1;
  271. }
  272. prot = 0;
  273. for (sect=s_first; sect<=s_last; ++sect) {
  274. if (info->protect[sect]) {
  275. prot++;
  276. }
  277. }
  278. if (prot) {
  279. printf ("- Warning: %d protected sectors will not be erased!\n",
  280. prot);
  281. } else {
  282. printf ("\n");
  283. }
  284. l_sect = -1;
  285. /* Disable interrupts which might cause a timeout here */
  286. flag = disable_interrupts();
  287. /* Start erase on unprotected sectors */
  288. for (sect = s_first; sect<=s_last; sect++) {
  289. if (info->protect[sect] == 0) { /* not protected */
  290. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  291. DEBUGF("Erasing sector %p\n", addr2);
  292. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  293. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  294. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  295. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  296. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  297. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  298. addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  299. for (i=0; i<50; i++)
  300. udelay(1000); /* wait 1 ms */
  301. } else {
  302. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  303. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  304. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  305. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  306. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  307. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  308. }
  309. l_sect = sect;
  310. /*
  311. * Wait for each sector to complete, it's more
  312. * reliable. According to AMD Spec, you must
  313. * issue all erase commands within a specified
  314. * timeout. This has been seen to fail, especially
  315. * if printf()s are included (for debug)!!
  316. */
  317. wait_for_DQ7(info, sect);
  318. }
  319. }
  320. /* re-enable interrupts if necessary */
  321. if (flag)
  322. enable_interrupts();
  323. /* wait at least 80us - let's wait 1 ms */
  324. udelay (1000);
  325. /* reset to read mode */
  326. addr = (FLASH_WORD_SIZE *)info->start[0];
  327. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  328. printf (" done\n");
  329. return 0;
  330. }
  331. /*-----------------------------------------------------------------------
  332. * Copy memory to flash, returns:
  333. * 0 - OK
  334. * 1 - write timeout
  335. * 2 - Flash not erased
  336. */
  337. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  338. {
  339. ulong cp, wp, data;
  340. int i, l, rc;
  341. wp = (addr & ~3); /* get lower word aligned address */
  342. /*
  343. * handle unaligned start bytes
  344. */
  345. if ((l = addr - wp) != 0) {
  346. data = 0;
  347. for (i=0, cp=wp; i<l; ++i, ++cp) {
  348. data = (data << 8) | (*(uchar *)cp);
  349. }
  350. for (; i<4 && cnt>0; ++i) {
  351. data = (data << 8) | *src++;
  352. --cnt;
  353. ++cp;
  354. }
  355. for (; cnt==0 && i<4; ++i, ++cp) {
  356. data = (data << 8) | (*(uchar *)cp);
  357. }
  358. if ((rc = write_word(info, wp, data)) != 0) {
  359. return (rc);
  360. }
  361. wp += 4;
  362. }
  363. /*
  364. * handle word aligned part
  365. */
  366. while (cnt >= 4) {
  367. data = 0;
  368. for (i=0; i<4; ++i) {
  369. data = (data << 8) | *src++;
  370. }
  371. if ((rc = write_word(info, wp, data)) != 0) {
  372. return (rc);
  373. }
  374. wp += 4;
  375. cnt -= 4;
  376. }
  377. if (cnt == 0) {
  378. return (0);
  379. }
  380. /*
  381. * handle unaligned tail bytes
  382. */
  383. data = 0;
  384. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  385. data = (data << 8) | *src++;
  386. --cnt;
  387. }
  388. for (; i<4; ++i, ++cp) {
  389. data = (data << 8) | (*(uchar *)cp);
  390. }
  391. return (write_word(info, wp, data));
  392. }
  393. /*-----------------------------------------------------------------------
  394. * Write a word to Flash, returns:
  395. * 0 - OK
  396. * 1 - write timeout
  397. * 2 - Flash not erased
  398. */
  399. static int write_word (flash_info_t * info, ulong dest, ulong data)
  400. {
  401. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
  402. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  403. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  404. ulong start;
  405. int i;
  406. /* Check if Flash is (sufficiently) erased */
  407. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  408. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  409. return (2);
  410. }
  411. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  412. int flag;
  413. /* Disable interrupts which might cause a timeout here */
  414. flag = disable_interrupts ();
  415. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  416. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  417. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  418. dest2[i] = data2[i];
  419. /* re-enable interrupts if necessary */
  420. if (flag)
  421. enable_interrupts ();
  422. /* data polling for D7 */
  423. start = get_timer (0);
  424. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  425. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  426. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  427. return (1);
  428. }
  429. }
  430. }
  431. return (0);
  432. }