cam5200_flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * (C) Copyright 2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc5xxx.h>
  25. #include <asm/processor.h>
  26. #if defined(CONFIG_CAM5200) && defined(CONFIG_CAM5200_NIOSFLASH)
  27. #if 0
  28. #define DEBUGF(x...) printf(x)
  29. #else
  30. #define DEBUGF(x...)
  31. #endif
  32. #define swap16(x) __swab16(x)
  33. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  34. /*
  35. * CAM5200 is a TQM5200B based board. Additionally it also features
  36. * a NIOS cpu. The NIOS CPU peripherals are accessible through MPC5xxx
  37. * Local Bus on CS5. This includes 32 bit wide RAM and SRAM as well as
  38. * 16 bit wide flash device. Big Endian order on a 32 bit CS5 makes
  39. * access to flash chip slightly more complicated as additional byte
  40. * swapping is necessary within each 16 bit wide flash 'word'.
  41. *
  42. * This driver's task is to handle both flash devices: 32 bit TQM5200B
  43. * flash chip and 16 bit NIOS cpu flash chip. In the below
  44. * flash_addr_table table we use least significant address bit to mark
  45. * 16 bit flash bank and two sets of routines *_32 and *_16 to handle
  46. * specifics of both flashes.
  47. */
  48. static unsigned long flash_addr_table[][CFG_MAX_FLASH_BANKS] = {
  49. {CFG_BOOTCS_START, CFG_CS5_START | 1}
  50. };
  51. /*-----------------------------------------------------------------------
  52. * Functions
  53. */
  54. static int write_word(flash_info_t * info, ulong dest, ulong data);
  55. #ifdef CFG_FLASH_2ND_16BIT_DEV
  56. static int write_word_32(flash_info_t * info, ulong dest, ulong data);
  57. static int write_word_16(flash_info_t * info, ulong dest, ulong data);
  58. static int flash_erase_32(flash_info_t * info, int s_first, int s_last);
  59. static int flash_erase_16(flash_info_t * info, int s_first, int s_last);
  60. static ulong flash_get_size_32(vu_long * addr, flash_info_t * info);
  61. static ulong flash_get_size_16(vu_long * addr, flash_info_t * info);
  62. #endif
  63. void flash_print_info(flash_info_t * info)
  64. {
  65. int i, k;
  66. int size, erased;
  67. volatile unsigned long *flash;
  68. if (info->flash_id == FLASH_UNKNOWN) {
  69. printf("missing or unknown FLASH type\n");
  70. return;
  71. }
  72. switch (info->flash_id & FLASH_VENDMASK) {
  73. case FLASH_MAN_AMD:
  74. printf("AMD ");
  75. break;
  76. case FLASH_MAN_FUJ:
  77. printf("FUJITSU ");
  78. break;
  79. default:
  80. printf("Unknown Vendor ");
  81. break;
  82. }
  83. switch (info->flash_id & FLASH_TYPEMASK) {
  84. case FLASH_S29GL128N:
  85. printf ("S29GL128N (256 Mbit, uniform sector size)\n");
  86. break;
  87. case FLASH_AM320B:
  88. printf ("29LV320B (32 Mbit, bottom boot sect)\n");
  89. break;
  90. case FLASH_AM320T:
  91. printf ("29LV320T (32 Mbit, top boot sect)\n");
  92. break;
  93. default:
  94. printf("Unknown Chip Type\n");
  95. break;
  96. }
  97. printf(" Size: %ld KB in %d Sectors\n",
  98. info->size >> 10, info->sector_count);
  99. printf(" Sector Start Addresses:");
  100. for (i = 0; i < info->sector_count; ++i) {
  101. /*
  102. * Check if whole sector is erased
  103. */
  104. if (i != (info->sector_count - 1))
  105. size = info->start[i + 1] - info->start[i];
  106. else
  107. size = info->start[0] + info->size - info->start[i];
  108. erased = 1;
  109. flash = (volatile unsigned long *)info->start[i];
  110. size = size >> 2; /* divide by 4 for longword access */
  111. for (k = 0; k < size; k++) {
  112. if (*flash++ != 0xffffffff) {
  113. erased = 0;
  114. break;
  115. }
  116. }
  117. if ((i % 5) == 0)
  118. printf("\n ");
  119. printf(" %08lX%s%s", info->start[i],
  120. erased ? " E" : " ",
  121. info->protect[i] ? "RO " : " ");
  122. }
  123. printf("\n");
  124. return;
  125. }
  126. /*
  127. * The following code cannot be run from FLASH!
  128. */
  129. #ifdef CFG_FLASH_2ND_16BIT_DEV
  130. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  131. {
  132. DEBUGF("get_size: FLASH ADDR %08lx\n", addr);
  133. /* bit 0 used for big flash marking */
  134. if ((ulong)addr & 0x1)
  135. return flash_get_size_16((vu_long *)((ulong)addr & 0xfffffffe), info);
  136. else
  137. return flash_get_size_32(addr, info);
  138. }
  139. static ulong flash_get_size_32(vu_long * addr, flash_info_t * info)
  140. #else
  141. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  142. #endif
  143. {
  144. short i;
  145. CFG_FLASH_WORD_SIZE value;
  146. ulong base = (ulong) addr;
  147. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
  148. DEBUGF("get_size32: FLASH ADDR: %08x\n", (unsigned)addr);
  149. /* Write auto select command: read Manufacturer ID */
  150. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
  151. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
  152. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00900090;
  153. udelay(1000);
  154. value = addr2[0];
  155. DEBUGF("FLASH MANUFACT: %x\n", value);
  156. switch (value) {
  157. case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
  158. info->flash_id = FLASH_MAN_AMD;
  159. break;
  160. default:
  161. info->flash_id = FLASH_UNKNOWN;
  162. info->sector_count = 0;
  163. info->size = 0;
  164. return (0); /* no or unknown flash */
  165. }
  166. value = addr2[1]; /* device ID */
  167. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  168. switch (value) {
  169. case AMD_ID_MIRROR:
  170. DEBUGF("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
  171. addr[14], addr[15]);
  172. switch(addr[14]) {
  173. case AMD_ID_GL128N_2:
  174. if (addr[15] != AMD_ID_GL128N_3) {
  175. DEBUGF("Chip: S29GL128N -> unknown\n");
  176. info->flash_id = FLASH_UNKNOWN;
  177. } else {
  178. DEBUGF("Chip: S29GL128N\n");
  179. info->flash_id += FLASH_S29GL128N;
  180. info->sector_count = 128;
  181. info->size = 0x02000000;
  182. }
  183. break;
  184. default:
  185. info->flash_id = FLASH_UNKNOWN;
  186. return(0);
  187. }
  188. break;
  189. default:
  190. info->flash_id = FLASH_UNKNOWN;
  191. return (0); /* => no or unknown flash */
  192. }
  193. /* set up sector start address table */
  194. for (i = 0; i < info->sector_count; i++)
  195. info->start[i] = base + (i * 0x00040000);
  196. /* check for protected sectors */
  197. for (i = 0; i < info->sector_count; i++) {
  198. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  199. /* D0 = 1 if protected */
  200. addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
  201. info->protect[i] = addr2[2] & 1;
  202. }
  203. /* issue bank reset to return to read mode */
  204. addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0;
  205. return (info->size);
  206. }
  207. static int wait_for_DQ7_32(flash_info_t * info, int sect)
  208. {
  209. ulong start, now, last;
  210. volatile CFG_FLASH_WORD_SIZE *addr =
  211. (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
  212. start = get_timer(0);
  213. last = start;
  214. while ((addr[0] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
  215. (CFG_FLASH_WORD_SIZE) 0x00800080) {
  216. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  217. printf("Timeout\n");
  218. return -1;
  219. }
  220. /* show that we're waiting */
  221. if ((now - last) > 1000) { /* every second */
  222. putc('.');
  223. last = now;
  224. }
  225. }
  226. return 0;
  227. }
  228. #ifdef CFG_FLASH_2ND_16BIT_DEV
  229. int flash_erase(flash_info_t * info, int s_first, int s_last)
  230. {
  231. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) {
  232. return flash_erase_16(info, s_first, s_last);
  233. } else {
  234. return flash_erase_32(info, s_first, s_last);
  235. }
  236. }
  237. static int flash_erase_32(flash_info_t * info, int s_first, int s_last)
  238. #else
  239. int flash_erase(flash_info_t * info, int s_first, int s_last)
  240. #endif
  241. {
  242. volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
  243. volatile CFG_FLASH_WORD_SIZE *addr2;
  244. int flag, prot, sect, l_sect;
  245. if ((s_first < 0) || (s_first > s_last)) {
  246. if (info->flash_id == FLASH_UNKNOWN)
  247. printf("- missing\n");
  248. else
  249. printf("- no sectors to erase\n");
  250. return 1;
  251. }
  252. if (info->flash_id == FLASH_UNKNOWN) {
  253. printf("Can't erase unknown flash type - aborted\n");
  254. return 1;
  255. }
  256. prot = 0;
  257. for (sect = s_first; sect <= s_last; ++sect) {
  258. if (info->protect[sect])
  259. prot++;
  260. }
  261. if (prot)
  262. printf("- Warning: %d protected sectors will not be erased!", prot);
  263. printf("\n");
  264. l_sect = -1;
  265. /* Disable interrupts which might cause a timeout here */
  266. flag = disable_interrupts();
  267. /* Start erase on unprotected sectors */
  268. for (sect = s_first; sect <= s_last; sect++) {
  269. if (info->protect[sect] == 0) { /* not protected */
  270. addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
  271. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
  272. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
  273. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
  274. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
  275. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
  276. addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  277. l_sect = sect;
  278. /*
  279. * Wait for each sector to complete, it's more
  280. * reliable. According to AMD Spec, you must
  281. * issue all erase commands within a specified
  282. * timeout. This has been seen to fail, especially
  283. * if printf()s are included (for debug)!!
  284. */
  285. wait_for_DQ7_32(info, sect);
  286. }
  287. }
  288. /* re-enable interrupts if necessary */
  289. if (flag)
  290. enable_interrupts();
  291. /* wait at least 80us - let's wait 1 ms */
  292. udelay(1000);
  293. /* reset to read mode */
  294. addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
  295. addr[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  296. printf(" done\n");
  297. return 0;
  298. }
  299. /*-----------------------------------------------------------------------
  300. * Copy memory to flash, returns:
  301. * 0 - OK
  302. * 1 - write timeout
  303. * 2 - Flash not erased
  304. */
  305. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  306. {
  307. ulong cp, wp, data;
  308. int i, l, rc;
  309. wp = (addr & ~3); /* get lower word aligned address */
  310. /*
  311. * handle unaligned start bytes
  312. */
  313. if ((l = addr - wp) != 0) {
  314. data = 0;
  315. for (i = 0, cp = wp; i < l; ++i, ++cp)
  316. data = (data << 8) | (*(uchar *) cp);
  317. for (; i < 4 && cnt > 0; ++i) {
  318. data = (data << 8) | *src++;
  319. --cnt;
  320. ++cp;
  321. }
  322. for (; cnt == 0 && i < 4; ++i, ++cp)
  323. data = (data << 8) | (*(uchar *) cp);
  324. if ((rc = write_word(info, wp, data)) != 0)
  325. return (rc);
  326. wp += 4;
  327. }
  328. /*
  329. * handle word aligned part
  330. */
  331. while (cnt >= 4) {
  332. data = 0;
  333. for (i = 0; i < 4; ++i)
  334. data = (data << 8) | *src++;
  335. if ((rc = write_word(info, wp, data)) != 0)
  336. return (rc);
  337. wp += 4;
  338. cnt -= 4;
  339. }
  340. if (cnt == 0)
  341. return (0);
  342. /*
  343. * handle unaligned tail bytes
  344. */
  345. data = 0;
  346. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  347. data = (data << 8) | *src++;
  348. --cnt;
  349. }
  350. for (; i < 4; ++i, ++cp)
  351. data = (data << 8) | (*(uchar *) cp);
  352. return (write_word(info, wp, data));
  353. }
  354. /*-----------------------------------------------------------------------
  355. * Copy memory to flash, returns:
  356. * 0 - OK
  357. * 1 - write timeout
  358. * 2 - Flash not erased
  359. */
  360. #ifdef CFG_FLASH_2ND_16BIT_DEV
  361. static int write_word(flash_info_t * info, ulong dest, ulong data)
  362. {
  363. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) {
  364. return write_word_16(info, dest, data);
  365. } else {
  366. return write_word_32(info, dest, data);
  367. }
  368. }
  369. static int write_word_32(flash_info_t * info, ulong dest, ulong data)
  370. #else
  371. static int write_word(flash_info_t * info, ulong dest, ulong data)
  372. #endif
  373. {
  374. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
  375. volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
  376. volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) & data;
  377. ulong start;
  378. int i, flag;
  379. /* Check if Flash is (sufficiently) erased */
  380. if ((*((vu_long *)dest) & data) != data)
  381. return (2);
  382. for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
  383. /* Disable interrupts which might cause a timeout here */
  384. flag = disable_interrupts();
  385. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
  386. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
  387. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00A000A0;
  388. dest2[i] = data2[i];
  389. /* re-enable interrupts if necessary */
  390. if (flag)
  391. enable_interrupts();
  392. /* data polling for D7 */
  393. start = get_timer(0);
  394. while ((dest2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
  395. (data2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080)) {
  396. if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
  397. return (1);
  398. }
  399. }
  400. return (0);
  401. }
  402. #ifdef CFG_FLASH_2ND_16BIT_DEV
  403. #undef CFG_FLASH_WORD_SIZE
  404. #define CFG_FLASH_WORD_SIZE unsigned short
  405. /*
  406. * The following code cannot be run from FLASH!
  407. */
  408. static ulong flash_get_size_16(vu_long * addr, flash_info_t * info)
  409. {
  410. short i;
  411. CFG_FLASH_WORD_SIZE value;
  412. ulong base = (ulong) addr;
  413. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
  414. DEBUGF("get_size16: FLASH ADDR: %08x\n", (unsigned)addr);
  415. /* issue bank reset to return to read mode */
  416. addr2[0] = (CFG_FLASH_WORD_SIZE) 0xF000F000;
  417. /* Write auto select command: read Manufacturer ID */
  418. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAA00AA00;
  419. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55005500;
  420. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x90009000;
  421. udelay(1000);
  422. value = swap16(addr2[0]);
  423. DEBUGF("FLASH MANUFACT: %x\n", value);
  424. switch (value) {
  425. case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
  426. info->flash_id = FLASH_MAN_AMD;
  427. break;
  428. case (CFG_FLASH_WORD_SIZE) FUJ_MANUFACT:
  429. info->flash_id = FLASH_MAN_FUJ;
  430. break;
  431. default:
  432. info->flash_id = FLASH_UNKNOWN;
  433. info->sector_count = 0;
  434. info->size = 0;
  435. return (0); /* no or unknown flash */
  436. }
  437. value = swap16(addr2[1]); /* device ID */
  438. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  439. switch (value) {
  440. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
  441. info->flash_id += FLASH_AM320B;
  442. info->sector_count = 71;
  443. info->size = 0x00400000;
  444. break; /* => 4 MB */
  445. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
  446. info->flash_id += FLASH_AM320T;
  447. info->sector_count = 71;
  448. info->size = 0x00400000;
  449. break; /* => 4 MB */
  450. default:
  451. info->flash_id = FLASH_UNKNOWN;
  452. return (0); /* => no or unknown flash */
  453. }
  454. if (info->flash_id & FLASH_BTYPE) {
  455. /* set sector offsets for bottom boot block type */
  456. info->start[0] = base + 0x00000000;
  457. info->start[1] = base + 0x00002000;
  458. info->start[2] = base + 0x00004000;
  459. info->start[3] = base + 0x00006000;
  460. info->start[4] = base + 0x00008000;
  461. info->start[5] = base + 0x0000a000;
  462. info->start[6] = base + 0x0000c000;
  463. info->start[7] = base + 0x0000e000;
  464. for (i = 8; i < info->sector_count; i++)
  465. info->start[i] = base + (i * 0x00010000) - 0x00070000;
  466. } else {
  467. /* set sector offsets for top boot block type */
  468. i = info->sector_count - 1;
  469. info->start[i--] = base + info->size - 0x00002000;
  470. info->start[i--] = base + info->size - 0x00004000;
  471. info->start[i--] = base + info->size - 0x00006000;
  472. info->start[i--] = base + info->size - 0x00008000;
  473. info->start[i--] = base + info->size - 0x0000a000;
  474. info->start[i--] = base + info->size - 0x0000c000;
  475. info->start[i--] = base + info->size - 0x0000e000;
  476. for (; i >= 0; i--)
  477. info->start[i] = base + i * 0x00010000;
  478. }
  479. /* check for protected sectors */
  480. for (i = 0; i < info->sector_count; i++) {
  481. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  482. /* D0 = 1 if protected */
  483. addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
  484. info->protect[i] = addr2[2] & 1;
  485. }
  486. /* issue bank reset to return to read mode */
  487. addr2[0] = (CFG_FLASH_WORD_SIZE) 0xF000F000;
  488. return (info->size);
  489. }
  490. static int wait_for_DQ7_16(flash_info_t * info, int sect)
  491. {
  492. ulong start, now, last;
  493. volatile CFG_FLASH_WORD_SIZE *addr =
  494. (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
  495. start = get_timer(0);
  496. last = start;
  497. while ((addr[0] & (CFG_FLASH_WORD_SIZE) 0x80008000) !=
  498. (CFG_FLASH_WORD_SIZE) 0x80008000) {
  499. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  500. printf("Timeout\n");
  501. return -1;
  502. }
  503. /* show that we're waiting */
  504. if ((now - last) > 1000) { /* every second */
  505. putc('.');
  506. last = now;
  507. }
  508. }
  509. return 0;
  510. }
  511. static int flash_erase_16(flash_info_t * info, int s_first, int s_last)
  512. {
  513. volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
  514. volatile CFG_FLASH_WORD_SIZE *addr2;
  515. int flag, prot, sect, l_sect;
  516. if ((s_first < 0) || (s_first > s_last)) {
  517. if (info->flash_id == FLASH_UNKNOWN)
  518. printf("- missing\n");
  519. else
  520. printf("- no sectors to erase\n");
  521. return 1;
  522. }
  523. if (info->flash_id == FLASH_UNKNOWN) {
  524. printf("Can't erase unknown flash type - aborted\n");
  525. return 1;
  526. }
  527. prot = 0;
  528. for (sect = s_first; sect <= s_last; ++sect) {
  529. if (info->protect[sect])
  530. prot++;
  531. }
  532. if (prot)
  533. printf("- Warning: %d protected sectors will not be erased!", prot);
  534. printf("\n");
  535. l_sect = -1;
  536. /* Disable interrupts which might cause a timeout here */
  537. flag = disable_interrupts();
  538. /* Start erase on unprotected sectors */
  539. for (sect = s_first; sect <= s_last; sect++) {
  540. if (info->protect[sect] == 0) { /* not protected */
  541. addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
  542. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAA00AA00;
  543. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55005500;
  544. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x80008000;
  545. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAA00AA00;
  546. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55005500;
  547. addr2[0] = (CFG_FLASH_WORD_SIZE) 0x30003000; /* sector erase */
  548. l_sect = sect;
  549. /*
  550. * Wait for each sector to complete, it's more
  551. * reliable. According to AMD Spec, you must
  552. * issue all erase commands within a specified
  553. * timeout. This has been seen to fail, especially
  554. * if printf()s are included (for debug)!!
  555. */
  556. wait_for_DQ7_16(info, sect);
  557. }
  558. }
  559. /* re-enable interrupts if necessary */
  560. if (flag)
  561. enable_interrupts();
  562. /* wait at least 80us - let's wait 1 ms */
  563. udelay(1000);
  564. /* reset to read mode */
  565. addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
  566. addr[0] = (CFG_FLASH_WORD_SIZE) 0xF000F000; /* reset bank */
  567. printf(" done\n");
  568. return 0;
  569. }
  570. static int write_word_16(flash_info_t * info, ulong dest, ulong data)
  571. {
  572. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
  573. volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
  574. volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) & data;
  575. ulong start;
  576. int i;
  577. /* Check if Flash is (sufficiently) erased */
  578. for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
  579. if ((dest2[i] & swap16(data2[i])) != swap16(data2[i]))
  580. return (2);
  581. }
  582. for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
  583. int flag;
  584. /* Disable interrupts which might cause a timeout here */
  585. flag = disable_interrupts();
  586. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAA00AA00;
  587. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55005500;
  588. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xA000A000;
  589. dest2[i] = swap16(data2[i]);
  590. /* re-enable interrupts if necessary */
  591. if (flag)
  592. enable_interrupts();
  593. /* data polling for D7 */
  594. start = get_timer(0);
  595. while ((dest2[i] & (CFG_FLASH_WORD_SIZE) 0x80008000) !=
  596. (swap16(data2[i]) & (CFG_FLASH_WORD_SIZE) 0x80008000)) {
  597. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  598. return (1);
  599. }
  600. }
  601. }
  602. return (0);
  603. }
  604. #endif /* CFG_FLASH_2ND_16BIT_DEV */
  605. /*-----------------------------------------------------------------------
  606. * Functions
  607. */
  608. static ulong flash_get_size(vu_long * addr, flash_info_t * info);
  609. static int write_word(flash_info_t * info, ulong dest, ulong data);
  610. /*-----------------------------------------------------------------------
  611. */
  612. unsigned long flash_init(void)
  613. {
  614. unsigned long total_b = 0;
  615. unsigned long size_b[CFG_MAX_FLASH_BANKS];
  616. unsigned short index = 0;
  617. int i;
  618. DEBUGF("\n");
  619. DEBUGF("FLASH: Index: %d\n", index);
  620. /* Init: no FLASHes known */
  621. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  622. flash_info[i].flash_id = FLASH_UNKNOWN;
  623. flash_info[i].sector_count = -1;
  624. flash_info[i].size = 0;
  625. /* check whether the address is 0 */
  626. if (flash_addr_table[index][i] == 0)
  627. continue;
  628. /* call flash_get_size() to initialize sector address */
  629. size_b[i] = flash_get_size((vu_long *) flash_addr_table[index][i],
  630. &flash_info[i]);
  631. flash_info[i].size = size_b[i];
  632. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  633. printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  634. i+1, size_b[i], size_b[i] << 20);
  635. flash_info[i].sector_count = -1;
  636. flash_info[i].size = 0;
  637. }
  638. /* Monitor protection ON by default */
  639. (void)flash_protect(FLAG_PROTECT_SET, CFG_MONITOR_BASE,
  640. CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
  641. &flash_info[i]);
  642. #if defined(CONFIG_ENV_IS_IN_FLASH)
  643. (void)flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR,
  644. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  645. &flash_info[i]);
  646. #if defined(CFG_ENV_ADDR_REDUND)
  647. (void)flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR_REDUND,
  648. CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
  649. &flash_info[i]);
  650. #endif
  651. #endif
  652. total_b += flash_info[i].size;
  653. }
  654. return total_b;
  655. }
  656. #endif /* if defined(CONFIG_CAM5200) && defined(CONFIG_CAM5200_NIOSFLASH) */