flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 <asm/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;
  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. /* Disable interrupts which might cause a timeout here */
  285. flag = disable_interrupts();
  286. /* Start erase on unprotected sectors */
  287. for (sect = s_first; sect<=s_last; sect++) {
  288. if (info->protect[sect] == 0) { /* not protected */
  289. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  290. DEBUGF("Erasing sector %p\n", addr2);
  291. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  292. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  293. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  294. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  295. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  296. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  297. addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  298. for (i=0; i<50; i++)
  299. udelay(1000); /* wait 1 ms */
  300. } else {
  301. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  302. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  303. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  304. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  305. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  306. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  307. }
  308. /*
  309. * Wait for each sector to complete, it's more
  310. * reliable. According to AMD Spec, you must
  311. * issue all erase commands within a specified
  312. * timeout. This has been seen to fail, especially
  313. * if printf()s are included (for debug)!!
  314. */
  315. wait_for_DQ7(info, sect);
  316. }
  317. }
  318. /* re-enable interrupts if necessary */
  319. if (flag)
  320. enable_interrupts();
  321. /* wait at least 80us - let's wait 1 ms */
  322. udelay (1000);
  323. /* reset to read mode */
  324. addr = (FLASH_WORD_SIZE *)info->start[0];
  325. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  326. printf (" done\n");
  327. return 0;
  328. }
  329. /*-----------------------------------------------------------------------
  330. * Copy memory to flash, returns:
  331. * 0 - OK
  332. * 1 - write timeout
  333. * 2 - Flash not erased
  334. */
  335. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  336. {
  337. ulong cp, wp, data;
  338. int i, l, rc;
  339. wp = (addr & ~3); /* get lower word aligned address */
  340. /*
  341. * handle unaligned start bytes
  342. */
  343. if ((l = addr - wp) != 0) {
  344. data = 0;
  345. for (i=0, cp=wp; i<l; ++i, ++cp) {
  346. data = (data << 8) | (*(uchar *)cp);
  347. }
  348. for (; i<4 && cnt>0; ++i) {
  349. data = (data << 8) | *src++;
  350. --cnt;
  351. ++cp;
  352. }
  353. for (; cnt==0 && i<4; ++i, ++cp) {
  354. data = (data << 8) | (*(uchar *)cp);
  355. }
  356. if ((rc = write_word(info, wp, data)) != 0) {
  357. return (rc);
  358. }
  359. wp += 4;
  360. }
  361. /*
  362. * handle word aligned part
  363. */
  364. while (cnt >= 4) {
  365. data = 0;
  366. for (i=0; i<4; ++i) {
  367. data = (data << 8) | *src++;
  368. }
  369. if ((rc = write_word(info, wp, data)) != 0) {
  370. return (rc);
  371. }
  372. wp += 4;
  373. cnt -= 4;
  374. }
  375. if (cnt == 0) {
  376. return (0);
  377. }
  378. /*
  379. * handle unaligned tail bytes
  380. */
  381. data = 0;
  382. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  383. data = (data << 8) | *src++;
  384. --cnt;
  385. }
  386. for (; i<4; ++i, ++cp) {
  387. data = (data << 8) | (*(uchar *)cp);
  388. }
  389. return (write_word(info, wp, data));
  390. }
  391. /*-----------------------------------------------------------------------
  392. * Write a word to Flash, returns:
  393. * 0 - OK
  394. * 1 - write timeout
  395. * 2 - Flash not erased
  396. */
  397. static int write_word (flash_info_t * info, ulong dest, ulong data)
  398. {
  399. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
  400. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  401. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  402. ulong start;
  403. int i;
  404. /* Check if Flash is (sufficiently) erased */
  405. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  406. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  407. return (2);
  408. }
  409. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  410. int flag;
  411. /* Disable interrupts which might cause a timeout here */
  412. flag = disable_interrupts ();
  413. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  414. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  415. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  416. dest2[i] = data2[i];
  417. /* re-enable interrupts if necessary */
  418. if (flag)
  419. enable_interrupts ();
  420. /* data polling for D7 */
  421. start = get_timer (0);
  422. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  423. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  424. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  425. return (1);
  426. }
  427. }
  428. }
  429. return (0);
  430. }