flash.c 15 KB

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