flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. int i;
  288. if ((s_first < 0) || (s_first > s_last)) {
  289. if (info->flash_id == FLASH_UNKNOWN) {
  290. printf("- missing\n");
  291. } else {
  292. printf("- no sectors to erase\n");
  293. }
  294. return 1;
  295. }
  296. if (info->flash_id == FLASH_UNKNOWN) {
  297. printf("Can't erase unknown flash type - aborted\n");
  298. return 1;
  299. }
  300. prot = 0;
  301. for (sect = s_first; sect <= s_last; ++sect) {
  302. if (info->protect[sect]) {
  303. prot++;
  304. }
  305. }
  306. if (prot) {
  307. printf("- Warning: %d protected sectors will not be erased!\n",
  308. prot);
  309. } else {
  310. printf("\n");
  311. }
  312. l_sect = -1;
  313. /* Disable interrupts which might cause a timeout here */
  314. flag = disable_interrupts();
  315. /* Start erase on unprotected sectors */
  316. for (sect = s_first; sect <= s_last; sect++) {
  317. if (info->protect[sect] == 0) { /* not protected */
  318. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  319. printf("Erasing sector %p\n", addr2);
  320. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  321. (FLASH_WORD_SIZE) 0x00AA;
  322. asm("sync");
  323. asm("isync");
  324. *(FLASH_WORD_SIZE *) ((int)addr + ADDR1) =
  325. (FLASH_WORD_SIZE) 0x0055;
  326. asm("sync");
  327. asm("isync");
  328. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  329. (FLASH_WORD_SIZE) 0x0080;
  330. asm("sync");
  331. asm("isync");
  332. *(FLASH_WORD_SIZE *) ((int)addr + ADDR0) =
  333. (FLASH_WORD_SIZE) 0x00AA;
  334. asm("sync");
  335. asm("isync");
  336. *(FLASH_WORD_SIZE *) ((int)addr + ADDR1) =
  337. (FLASH_WORD_SIZE) 0x0055;
  338. asm("sync");
  339. asm("isync");
  340. addr2[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  341. asm("sync");
  342. asm("isync");
  343. l_sect = sect;
  344. /*
  345. * Wait for each sector to complete, it's more
  346. * reliable. According to AMD Spec, you must
  347. * issue all erase commands within a specified
  348. * timeout. This has been seen to fail, especially
  349. * if printf()s are included (for debug)!!
  350. */
  351. wait_for_DQ7(info, sect);
  352. }
  353. }
  354. /* re-enable interrupts if necessary */
  355. if (flag)
  356. enable_interrupts();
  357. /* wait at least 80us - let's wait 1 ms */
  358. udelay(1000);
  359. #if 0
  360. /*
  361. * We wait for the last triggered sector
  362. */
  363. if (l_sect < 0)
  364. goto DONE;
  365. wait_for_DQ7(info, l_sect);
  366. DONE:
  367. #endif
  368. /* reset to read mode */
  369. addr = (FLASH_WORD_SIZE *) info->start[0];
  370. addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  371. printf(" done\n");
  372. return 0;
  373. }
  374. /*-----------------------------------------------------------------------
  375. * Copy memory to flash, returns:
  376. * 0 - OK
  377. * 1 - write timeout
  378. * 2 - Flash not erased
  379. */
  380. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  381. {
  382. ulong cp, wp, data;
  383. int i, l, rc;
  384. ulong status_value = 0;
  385. wp = (addr & ~3); /* get lower word aligned address */
  386. /*
  387. * handle unaligned start bytes
  388. */
  389. if ((l = addr - wp) != 0) {
  390. data = 0;
  391. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  392. data = (data << 8) | (*(uchar *) cp);
  393. }
  394. for (; i < 4 && cnt > 0; ++i) {
  395. data = (data << 8) | *src++;
  396. --cnt;
  397. ++cp;
  398. }
  399. for (; cnt == 0 && i < 4; ++i, ++cp) {
  400. data = (data << 8) | (*(uchar *) cp);
  401. }
  402. if ((rc = write_word(info, wp, data)) != 0) {
  403. return (rc);
  404. }
  405. wp += 4;
  406. }
  407. /*
  408. * handle word aligned part
  409. */
  410. while (cnt >= 4) {
  411. /*print status if needed */
  412. if ((wp >= (status_value + 0x20000))
  413. && (status_value < 0xFFFE0000)) {
  414. status_value = wp;
  415. printf("writing to sector 0x%X\n", status_value);
  416. }
  417. data = 0;
  418. for (i = 0; i < 4; ++i) {
  419. data = (data << 8) | *src++;
  420. }
  421. if ((rc = write_word(info, wp, data)) != 0) {
  422. return (rc);
  423. }
  424. wp += 4;
  425. cnt -= 4;
  426. }
  427. if (cnt == 0) {
  428. return (0);
  429. }
  430. /*
  431. * handle unaligned tail bytes
  432. */
  433. data = 0;
  434. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  435. data = (data << 8) | *src++;
  436. --cnt;
  437. }
  438. for (; i < 4; ++i, ++cp) {
  439. data = (data << 8) | (*(uchar *) cp);
  440. }
  441. return (write_word(info, wp, data));
  442. }
  443. /*-----------------------------------------------------------------------
  444. * Write a word to Flash, returns:
  445. * 0 - OK
  446. * 1 - write timeout
  447. * 2 - Flash not erased
  448. */
  449. static int write_word(flash_info_t * info, ulong dest, ulong data)
  450. {
  451. volatile vu_long *addr2 = (vu_long *) (info->start[0]);
  452. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  453. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  454. ulong start;
  455. int i;
  456. /* Check if Flash is (sufficiently) erased */
  457. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  458. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  459. return (2);
  460. }
  461. for (i = 0; i < 4 / sizeof(FLASH_WORD_SIZE); i++) {
  462. int flag;
  463. /* Disable interrupts which might cause a timeout here */
  464. flag = disable_interrupts();
  465. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR0) =
  466. (FLASH_WORD_SIZE) 0x00AA;
  467. asm("sync");
  468. asm("isync");
  469. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR1) =
  470. (FLASH_WORD_SIZE) 0x0055;
  471. asm("sync");
  472. asm("isync");
  473. *(FLASH_WORD_SIZE *) ((int)addr2 + ADDR0) =
  474. (FLASH_WORD_SIZE) 0x00A0;
  475. asm("sync");
  476. asm("isync");
  477. dest2[i] = data2[i];
  478. /* re-enable interrupts if necessary */
  479. if (flag)
  480. enable_interrupts();
  481. /* data polling for D7 */
  482. start = get_timer(0);
  483. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  484. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  485. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  486. return (1);
  487. }
  488. }
  489. }
  490. return (0);
  491. }
  492. /*-----------------------------------------------------------------------
  493. */