flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * (C) Copyright 2002-2004
  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. * Modified 4/5/2001
  28. * Wait for completion of each sector erase command issued
  29. * 4/5/2001
  30. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  31. */
  32. /*
  33. * Ported to XPedite1000, 1/2 mb boot flash only
  34. * Travis B. Sawyer, <travis.sawyer@sandburst.com>
  35. */
  36. #include <common.h>
  37. #include <ppc4xx.h>
  38. #include <asm/processor.h>
  39. #undef DEBUG
  40. #ifdef DEBUG
  41. #define DEBUGF(x...) printf(x)
  42. #else
  43. #define DEBUGF(x...)
  44. #endif /* DEBUG */
  45. #define BOOT_SMALL_FLASH 32 /* 00100000 */
  46. #define FLASH_ONBD_N 2 /* 00000010 */
  47. #define FLASH_SRAM_SEL 1 /* 00000001 */
  48. #define BOOT_SMALL_FLASH_VAL 4
  49. #define FLASH_ONBD_N_VAL 2
  50. #define FLASH_SRAM_SEL_VAL 1
  51. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  52. unsigned long flash_addr_table[512][CFG_MAX_FLASH_BANKS] = {
  53. {0xfe000000}
  54. };
  55. /*-----------------------------------------------------------------------
  56. * Functions
  57. */
  58. static ulong flash_get_size(vu_long * addr, flash_info_t * info);
  59. static int write_word(flash_info_t * info, ulong dest, ulong data);
  60. #define ADDR0 0xaaaa
  61. #define ADDR1 0x5554
  62. #define FLASH_WORD_SIZE unsigned short
  63. /*-----------------------------------------------------------------------
  64. */
  65. unsigned long flash_init(void)
  66. {
  67. unsigned long total_b = 0;
  68. unsigned long size_b[CFG_MAX_FLASH_BANKS];
  69. unsigned short index = 0;
  70. int i;
  71. DEBUGF("\n");
  72. DEBUGF("FLASH: Index: %d\n", index);
  73. /* Init: no FLASHes known */
  74. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  75. flash_info[i].flash_id = FLASH_UNKNOWN;
  76. flash_info[i].sector_count = -1;
  77. flash_info[i].size = 0;
  78. /* check whether the address is 0 */
  79. if (flash_addr_table[index][i] == 0) {
  80. continue;
  81. }
  82. /* call flash_get_size() to initialize sector address */
  83. size_b[i] = flash_get_size((vu_long *)
  84. flash_addr_table[index][i],
  85. &flash_info[i]);
  86. flash_info[i].size = size_b[i];
  87. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  88. printf
  89. ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  90. i, size_b[i], size_b[i] << 20);
  91. flash_info[i].sector_count = -1;
  92. flash_info[i].size = 0;
  93. }
  94. total_b += flash_info[i].size;
  95. }
  96. /* FLASH protect Monitor */
  97. flash_protect(FLAG_PROTECT_SET,
  98. CFG_MONITOR_BASE, 0xFFFFFFFF, &flash_info[0]);
  99. return total_b;
  100. }
  101. /*-----------------------------------------------------------------------
  102. */
  103. void flash_print_info(flash_info_t * info)
  104. {
  105. int i;
  106. int k;
  107. int size;
  108. int erased;
  109. volatile unsigned long *flash;
  110. if (info->flash_id == FLASH_UNKNOWN) {
  111. printf("missing or unknown FLASH type\n");
  112. return;
  113. }
  114. switch (info->flash_id & FLASH_VENDMASK) {
  115. case FLASH_MAN_AMD:
  116. printf("AMD ");
  117. break;
  118. case FLASH_MAN_FUJ:
  119. printf("FUJITSU ");
  120. break;
  121. case FLASH_MAN_SST:
  122. printf("SST ");
  123. break;
  124. default:
  125. printf("Unknown Vendor ");
  126. break;
  127. }
  128. switch (info->flash_id & FLASH_TYPEMASK) {
  129. case FLASH_AMD016:
  130. printf("AM29F016D (16 Mbit, uniform sector size)\n");
  131. break;
  132. case FLASH_AM040:
  133. printf("AM29F040 (512 Kbit, uniform sector size)\n");
  134. break;
  135. case FLASH_AM400B:
  136. printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
  137. break;
  138. case FLASH_AM400T:
  139. printf("AM29LV400T (4 Mbit, top boot sector)\n");
  140. break;
  141. case FLASH_AM800B:
  142. printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
  143. break;
  144. case FLASH_AM800T:
  145. printf("AM29LV800T (8 Mbit, top boot sector)\n");
  146. break;
  147. case FLASH_AM160B:
  148. printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
  149. break;
  150. case FLASH_AM160T:
  151. printf("AM29LV160T (16 Mbit, top boot sector)\n");
  152. break;
  153. case FLASH_AM320B:
  154. printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
  155. break;
  156. case FLASH_AM320T:
  157. printf("AM29LV320T (32 Mbit, top boot sector)\n");
  158. break;
  159. case FLASH_SST800A:
  160. printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  161. break;
  162. case FLASH_SST160A:
  163. printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  164. break;
  165. default:
  166. printf("Unknown Chip Type\n");
  167. break;
  168. }
  169. printf(" Size: %ld KB in %d Sectors\n",
  170. info->size >> 10, info->sector_count);
  171. printf(" Sector Start Addresses:");
  172. for (i = 0; i < info->sector_count; ++i) {
  173. /*
  174. * Check if whole sector is erased
  175. */
  176. if (i != (info->sector_count - 1))
  177. size = info->start[i + 1] - info->start[i];
  178. else
  179. size = info->start[0] + info->size - info->start[i];
  180. erased = 1;
  181. flash = (volatile unsigned long *)info->start[i];
  182. size = size >> 2; /* divide by 4 for longword access */
  183. for (k = 0; k < size; k++) {
  184. if (*flash++ != 0xffffffff) {
  185. erased = 0;
  186. break;
  187. }
  188. }
  189. if ((i % 5) == 0)
  190. printf("\n ");
  191. printf(" %08lX%s%s",
  192. info->start[i],
  193. erased ? " E" : " ", info->protect[i] ? "RO " : " ");
  194. }
  195. printf("\n");
  196. return;
  197. }
  198. /*-----------------------------------------------------------------------
  199. */
  200. /*-----------------------------------------------------------------------
  201. */
  202. /*
  203. * The following code cannot be run from FLASH!
  204. */
  205. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  206. {
  207. short i;
  208. FLASH_WORD_SIZE value;
  209. ulong base = (ulong) addr;
  210. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) addr;
  211. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
  212. /* Write auto select command: read Manufacturer ID */
  213. udelay(10000);
  214. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) = (FLASH_WORD_SIZE) 0x00AA;
  215. udelay(1000);
  216. *(FLASH_WORD_SIZE *) ((int)addr + ADDR1) = (FLASH_WORD_SIZE) 0x0055;
  217. udelay(1000);
  218. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) = (FLASH_WORD_SIZE) 0x0090;
  219. udelay(1000);
  220. value = addr2[0];
  221. DEBUGF("FLASH MANUFACT: %x\n", value);
  222. switch (value) {
  223. case (FLASH_WORD_SIZE) AMD_MANUFACT:
  224. info->flash_id = FLASH_MAN_AMD;
  225. break;
  226. case (FLASH_WORD_SIZE) FUJ_MANUFACT:
  227. info->flash_id = FLASH_MAN_FUJ;
  228. break;
  229. case (FLASH_WORD_SIZE) SST_MANUFACT:
  230. info->flash_id = FLASH_MAN_SST;
  231. break;
  232. case (FLASH_WORD_SIZE) STM_MANUFACT:
  233. info->flash_id = FLASH_MAN_STM;
  234. break;
  235. default:
  236. info->flash_id = FLASH_UNKNOWN;
  237. info->sector_count = 0;
  238. info->size = 0;
  239. return (0); /* no or unknown flash */
  240. }
  241. #ifdef CONFIG_ADCIOP
  242. value = addr2[0]; /* device ID */
  243. debug("\ndev_code=%x\n", value);
  244. #else
  245. value = addr2[1]; /* device ID */
  246. #endif
  247. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  248. info->flash_id = 0;
  249. info->sector_count = CFG_MAX_FLASH_SECT;
  250. info->size = 0x02000000;
  251. /* set up sector start address table */
  252. for (i = 0; i < info->sector_count; i++) {
  253. info->start[i] = (int)base + (i * 0x00020000);
  254. info->protect[i] = 0;
  255. }
  256. *(FLASH_WORD_SIZE *) ((int)addr) = (FLASH_WORD_SIZE) 0x00F0; /* reset bank */
  257. return (info->size);
  258. }
  259. int wait_for_DQ7(flash_info_t * info, int sect)
  260. {
  261. ulong start, now, last;
  262. volatile FLASH_WORD_SIZE *addr =
  263. (FLASH_WORD_SIZE *) (info->start[sect]);
  264. start = get_timer(0);
  265. last = start;
  266. while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
  267. (FLASH_WORD_SIZE) 0x00800080) {
  268. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  269. printf("Timeout\n");
  270. return -1;
  271. }
  272. /* show that we're waiting */
  273. if ((now - last) > 1000) { /* every second */
  274. putc('.');
  275. last = now;
  276. }
  277. }
  278. return 0;
  279. }
  280. /*-----------------------------------------------------------------------
  281. */
  282. int flash_erase(flash_info_t * info, int s_first, int s_last)
  283. {
  284. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
  285. volatile FLASH_WORD_SIZE *addr2;
  286. int flag, prot, sect, l_sect;
  287. if ((s_first < 0) || (s_first > s_last)) {
  288. if (info->flash_id == FLASH_UNKNOWN) {
  289. printf("- missing\n");
  290. } else {
  291. printf("- no sectors to erase\n");
  292. }
  293. return 1;
  294. }
  295. if (info->flash_id == FLASH_UNKNOWN) {
  296. printf("Can't erase unknown flash type - aborted\n");
  297. return 1;
  298. }
  299. prot = 0;
  300. for (sect = s_first; sect <= s_last; ++sect) {
  301. if (info->protect[sect]) {
  302. prot++;
  303. }
  304. }
  305. if (prot) {
  306. printf("- Warning: %d protected sectors will not be erased!\n",
  307. prot);
  308. } else {
  309. printf("\n");
  310. }
  311. l_sect = -1;
  312. /* Disable interrupts which might cause a timeout here */
  313. flag = disable_interrupts();
  314. /* Start erase on unprotected sectors */
  315. for (sect = s_first; sect <= s_last; sect++) {
  316. if (info->protect[sect] == 0) { /* not protected */
  317. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  318. printf("Erasing sector %p\n", addr2);
  319. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  320. (FLASH_WORD_SIZE) 0x00AA;
  321. asm("sync");
  322. asm("isync");
  323. *(FLASH_WORD_SIZE *) ((int)addr + ADDR1) =
  324. (FLASH_WORD_SIZE) 0x0055;
  325. asm("sync");
  326. asm("isync");
  327. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  328. (FLASH_WORD_SIZE) 0x0080;
  329. asm("sync");
  330. asm("isync");
  331. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  332. (FLASH_WORD_SIZE) 0x00AA;
  333. asm("sync");
  334. asm("isync");
  335. *(FLASH_WORD_SIZE *) ((int)addr + ADDR1) =
  336. (FLASH_WORD_SIZE) 0x0055;
  337. asm("sync");
  338. asm("isync");
  339. addr2[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  340. asm("sync");
  341. asm("isync");
  342. l_sect = sect;
  343. /*
  344. * Wait for each sector to complete, it's more
  345. * reliable. According to AMD Spec, you must
  346. * issue all erase commands within a specified
  347. * timeout. This has been seen to fail, especially
  348. * if printf()s are included (for debug)!!
  349. */
  350. wait_for_DQ7(info, sect);
  351. }
  352. }
  353. /* re-enable interrupts if necessary */
  354. if (flag)
  355. enable_interrupts();
  356. /* wait at least 80us - let's wait 1 ms */
  357. udelay(1000);
  358. #if 0
  359. /*
  360. * We wait for the last triggered sector
  361. */
  362. if (l_sect < 0)
  363. goto DONE;
  364. wait_for_DQ7(info, l_sect);
  365. DONE:
  366. #endif
  367. /* reset to read mode */
  368. addr = (FLASH_WORD_SIZE *) info->start[0];
  369. addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  370. printf(" done\n");
  371. return 0;
  372. }
  373. /*-----------------------------------------------------------------------
  374. * Copy memory to flash, returns:
  375. * 0 - OK
  376. * 1 - write timeout
  377. * 2 - Flash not erased
  378. */
  379. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  380. {
  381. ulong cp, wp, data;
  382. int i, l, rc;
  383. ulong status_value = 0;
  384. wp = (addr & ~3); /* get lower word aligned address */
  385. /*
  386. * handle unaligned start bytes
  387. */
  388. if ((l = addr - wp) != 0) {
  389. data = 0;
  390. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  391. data = (data << 8) | (*(uchar *) cp);
  392. }
  393. for (; i < 4 && cnt > 0; ++i) {
  394. data = (data << 8) | *src++;
  395. --cnt;
  396. ++cp;
  397. }
  398. for (; cnt == 0 && i < 4; ++i, ++cp) {
  399. data = (data << 8) | (*(uchar *) cp);
  400. }
  401. if ((rc = write_word(info, wp, data)) != 0) {
  402. return (rc);
  403. }
  404. wp += 4;
  405. }
  406. /*
  407. * handle word aligned part
  408. */
  409. while (cnt >= 4) {
  410. /*print status if needed */
  411. if ((wp >= (status_value + 0x20000))
  412. && (status_value < 0xFFFE0000)) {
  413. status_value = wp;
  414. printf("writing to sector 0x%X\n", status_value);
  415. }
  416. data = 0;
  417. for (i = 0; i < 4; ++i) {
  418. data = (data << 8) | *src++;
  419. }
  420. if ((rc = write_word(info, wp, data)) != 0) {
  421. return (rc);
  422. }
  423. wp += 4;
  424. cnt -= 4;
  425. }
  426. if (cnt == 0) {
  427. return (0);
  428. }
  429. /*
  430. * handle unaligned tail bytes
  431. */
  432. data = 0;
  433. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  434. data = (data << 8) | *src++;
  435. --cnt;
  436. }
  437. for (; i < 4; ++i, ++cp) {
  438. data = (data << 8) | (*(uchar *) cp);
  439. }
  440. return (write_word(info, wp, data));
  441. }
  442. /*-----------------------------------------------------------------------
  443. * Write a word to Flash, returns:
  444. * 0 - OK
  445. * 1 - write timeout
  446. * 2 - Flash not erased
  447. */
  448. static int write_word(flash_info_t * info, ulong dest, ulong data)
  449. {
  450. vu_long *addr2 = (vu_long *) (info->start[0]);
  451. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  452. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  453. ulong start;
  454. int i;
  455. /* Check if Flash is (sufficiently) erased */
  456. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  457. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  458. return (2);
  459. }
  460. for (i = 0; i < 4 / sizeof(FLASH_WORD_SIZE); i++) {
  461. int flag;
  462. /* Disable interrupts which might cause a timeout here */
  463. flag = disable_interrupts();
  464. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR0) =
  465. (FLASH_WORD_SIZE) 0x00AA;
  466. asm("sync");
  467. asm("isync");
  468. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR1) =
  469. (FLASH_WORD_SIZE) 0x0055;
  470. asm("sync");
  471. asm("isync");
  472. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR0) =
  473. (FLASH_WORD_SIZE) 0x00A0;
  474. asm("sync");
  475. asm("isync");
  476. dest2[i] = data2[i];
  477. /* re-enable interrupts if necessary */
  478. if (flag)
  479. enable_interrupts();
  480. /* data polling for D7 */
  481. start = get_timer(0);
  482. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  483. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  484. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  485. return (1);
  486. }
  487. }
  488. }
  489. return (0);
  490. }
  491. /*-----------------------------------------------------------------------
  492. */