flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. /*
  312. * Prevent writes to uninitialized FLASH.
  313. */
  314. if (info->flash_id != FLASH_UNKNOWN) {
  315. }
  316. return (info->size);
  317. }
  318. int wait_for_DQ7(flash_info_t * info, int sect)
  319. {
  320. ulong start, now, last;
  321. volatile FLASH_WORD_SIZE *addr =
  322. (FLASH_WORD_SIZE *) (info->start[sect]);
  323. start = get_timer(0);
  324. last = start;
  325. while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
  326. (FLASH_WORD_SIZE) 0x00800080) {
  327. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  328. printf("Timeout\n");
  329. return -1;
  330. }
  331. /* show that we're waiting */
  332. if ((now - last) > 1000) { /* every second */
  333. putc('.');
  334. last = now;
  335. }
  336. }
  337. return 0;
  338. }
  339. /*-----------------------------------------------------------------------
  340. */
  341. int flash_erase(flash_info_t * info, int s_first, int s_last)
  342. {
  343. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
  344. volatile FLASH_WORD_SIZE *addr2;
  345. int flag, prot, sect, l_sect;
  346. int i;
  347. if ((s_first < 0) || (s_first > s_last)) {
  348. if (info->flash_id == FLASH_UNKNOWN) {
  349. printf("- missing\n");
  350. } else {
  351. printf("- no sectors to erase\n");
  352. }
  353. return 1;
  354. }
  355. if (info->flash_id == FLASH_UNKNOWN) {
  356. printf("Can't erase unknown flash type - aborted\n");
  357. return 1;
  358. }
  359. prot = 0;
  360. for (sect = s_first; sect <= s_last; ++sect) {
  361. if (info->protect[sect]) {
  362. prot++;
  363. }
  364. }
  365. if (prot) {
  366. printf("- Warning: %d protected sectors will not be erased!\n",
  367. prot);
  368. } else {
  369. printf("\n");
  370. }
  371. l_sect = -1;
  372. /* Disable interrupts which might cause a timeout here */
  373. flag = disable_interrupts();
  374. /* Start erase on unprotected sectors */
  375. for (sect = s_first; sect <= s_last; sect++) {
  376. if (info->protect[sect] == 0) { /* not protected */
  377. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  378. printf("Erasing sector %p\n", addr2);
  379. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  380. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  381. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  382. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
  383. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  384. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  385. addr2[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
  386. for (i = 0; i < 50; i++)
  387. udelay(1000); /* wait 1 ms */
  388. } else {
  389. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  390. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  391. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
  392. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  393. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  394. addr2[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  395. }
  396. l_sect = sect;
  397. /*
  398. * Wait for each sector to complete, it's more
  399. * reliable. According to AMD Spec, you must
  400. * issue all erase commands within a specified
  401. * timeout. This has been seen to fail, especially
  402. * if printf()s are included (for debug)!!
  403. */
  404. wait_for_DQ7(info, sect);
  405. }
  406. }
  407. /* re-enable interrupts if necessary */
  408. if (flag)
  409. enable_interrupts();
  410. /* wait at least 80us - let's wait 1 ms */
  411. udelay(1000);
  412. /* reset to read mode */
  413. addr = (FLASH_WORD_SIZE *) info->start[0];
  414. addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  415. printf(" done\n");
  416. return 0;
  417. }
  418. /*-----------------------------------------------------------------------
  419. * Copy memory to flash, returns:
  420. * 0 - OK
  421. * 1 - write timeout
  422. * 2 - Flash not erased
  423. */
  424. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  425. {
  426. ulong cp, wp, data;
  427. int i, l, rc;
  428. wp = (addr & ~3); /* get lower word aligned address */
  429. /*
  430. * handle unaligned start bytes
  431. */
  432. if ((l = addr - wp) != 0) {
  433. data = 0;
  434. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  435. data = (data << 8) | (*(uchar *) cp);
  436. }
  437. for (; i < 4 && cnt > 0; ++i) {
  438. data = (data << 8) | *src++;
  439. --cnt;
  440. ++cp;
  441. }
  442. for (; cnt == 0 && i < 4; ++i, ++cp) {
  443. data = (data << 8) | (*(uchar *) cp);
  444. }
  445. if ((rc = write_word(info, wp, data)) != 0) {
  446. return (rc);
  447. }
  448. wp += 4;
  449. }
  450. /*
  451. * handle word aligned part
  452. */
  453. while (cnt >= 4) {
  454. data = 0;
  455. for (i = 0; i < 4; ++i) {
  456. data = (data << 8) | *src++;
  457. }
  458. if ((rc = write_word(info, wp, data)) != 0) {
  459. return (rc);
  460. }
  461. wp += 4;
  462. cnt -= 4;
  463. }
  464. if (cnt == 0) {
  465. return (0);
  466. }
  467. /*
  468. * handle unaligned tail bytes
  469. */
  470. data = 0;
  471. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  472. data = (data << 8) | *src++;
  473. --cnt;
  474. }
  475. for (; i < 4; ++i, ++cp) {
  476. data = (data << 8) | (*(uchar *) cp);
  477. }
  478. return (write_word(info, wp, data));
  479. }
  480. /*-----------------------------------------------------------------------
  481. * Write a word to Flash, returns:
  482. * 0 - OK
  483. * 1 - write timeout
  484. * 2 - Flash not erased
  485. */
  486. static int write_word(flash_info_t * info, ulong dest, ulong data)
  487. {
  488. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
  489. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  490. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  491. ulong start;
  492. int i;
  493. /* Check if Flash is (sufficiently) erased */
  494. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  495. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  496. return (2);
  497. }
  498. for (i = 0; i < 4 / sizeof(FLASH_WORD_SIZE); i++) {
  499. int flag;
  500. /* Disable interrupts which might cause a timeout here */
  501. flag = disable_interrupts();
  502. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  503. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  504. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  505. dest2[i] = data2[i];
  506. /* re-enable interrupts if necessary */
  507. if (flag)
  508. enable_interrupts();
  509. /* data polling for D7 */
  510. start = get_timer(0);
  511. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  512. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  513. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  514. return (1);
  515. }
  516. }
  517. }
  518. return (0);
  519. }