cam5200_flash.c 21 KB

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