flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * (C) Copyright 2000
  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 <mpc824x.h>
  25. #include <asm/processor.h>
  26. #include <asm/pci_io.h>
  27. #define ROM_CS0_START 0xFF800000
  28. #define ROM_CS1_START 0xFF000000
  29. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  30. #if defined(CFG_ENV_IS_IN_FLASH)
  31. # ifndef CFG_ENV_ADDR
  32. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  33. # endif
  34. # ifndef CFG_ENV_SIZE
  35. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  36. # endif
  37. # ifndef CFG_ENV_SECT_SIZE
  38. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  39. # endif
  40. #endif
  41. /*-----------------------------------------------------------------------
  42. * Functions
  43. */
  44. static int write_word (flash_info_t *info, ulong dest, ulong data);
  45. #if 0
  46. static void flash_get_offsets (ulong base, flash_info_t *info);
  47. #endif /* 0 */
  48. /*flash command address offsets*/
  49. #if 0
  50. #define ADDR0 (0x555)
  51. #define ADDR1 (0x2AA)
  52. #define ADDR3 (0x001)
  53. #else
  54. #define ADDR0 (0xAAA)
  55. #define ADDR1 (0x555)
  56. #define ADDR3 (0x001)
  57. #endif
  58. #define FLASH_WORD_SIZE unsigned char
  59. /*-----------------------------------------------------------------------
  60. */
  61. #if 0
  62. static int byte_parity_odd(unsigned char x) __attribute__ ((const));
  63. #endif /* 0 */
  64. static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
  65. typedef struct
  66. {
  67. FLASH_WORD_SIZE extval;
  68. unsigned short intval;
  69. } map_entry;
  70. #if 0
  71. static int
  72. byte_parity_odd(unsigned char x)
  73. {
  74. x ^= x >> 4;
  75. x ^= x >> 2;
  76. x ^= x >> 1;
  77. return (x & 0x1) != 0;
  78. }
  79. #endif /* 0 */
  80. static unsigned long
  81. flash_id(unsigned char mfct, unsigned char chip)
  82. {
  83. static const map_entry mfct_map[] =
  84. {
  85. {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
  86. {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
  87. {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
  88. {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
  89. {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
  90. {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
  91. };
  92. static const map_entry chip_map[] =
  93. {
  94. {AMD_ID_F040B, FLASH_AM040},
  95. {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
  96. };
  97. const map_entry *p;
  98. unsigned long result = FLASH_UNKNOWN;
  99. /* find chip id */
  100. for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
  101. if(p->extval == chip)
  102. {
  103. result = FLASH_VENDMASK | p->intval;
  104. break;
  105. }
  106. /* find vendor id */
  107. for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
  108. if(p->extval == mfct)
  109. {
  110. result &= ~FLASH_VENDMASK;
  111. result |= (unsigned long) p->intval << 16;
  112. break;
  113. }
  114. return result;
  115. }
  116. unsigned long
  117. flash_init(void)
  118. {
  119. unsigned long i;
  120. unsigned char j;
  121. static const ulong flash_banks[] = CFG_FLASH_BANKS;
  122. /* Init: no FLASHes known */
  123. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  124. {
  125. flash_info_t * const pflinfo = &flash_info[i];
  126. pflinfo->flash_id = FLASH_UNKNOWN;
  127. pflinfo->size = 0;
  128. pflinfo->sector_count = 0;
  129. }
  130. for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
  131. {
  132. flash_info_t * const pflinfo = &flash_info[i];
  133. const unsigned long base_address = flash_banks[i];
  134. volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
  135. #if 0
  136. volatile FLASH_WORD_SIZE * addr2;
  137. #endif
  138. #if 0
  139. /* write autoselect sequence */
  140. flash[0x5555] = 0xaa;
  141. flash[0x2aaa] = 0x55;
  142. flash[0x5555] = 0x90;
  143. #else
  144. flash[0xAAA << (3 * i)] = 0xaa;
  145. flash[0x555 << (3 * i)] = 0x55;
  146. flash[0xAAA << (3 * i)] = 0x90;
  147. #endif
  148. __asm__ __volatile__("sync");
  149. #if 0
  150. pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
  151. #else
  152. pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
  153. #endif
  154. switch(pflinfo->flash_id & FLASH_TYPEMASK)
  155. {
  156. case FLASH_AM040:
  157. pflinfo->size = 0x00080000;
  158. pflinfo->sector_count = 8;
  159. for(j = 0; j < 8; j++)
  160. {
  161. pflinfo->start[j] = base_address + 0x00010000 * j;
  162. pflinfo->protect[j] = flash[(j << 16) | 0x2];
  163. }
  164. break;
  165. case FLASH_STM800AB:
  166. pflinfo->size = 0x00100000;
  167. pflinfo->sector_count = 19;
  168. pflinfo->start[0] = base_address;
  169. pflinfo->start[1] = base_address + 0x4000;
  170. pflinfo->start[2] = base_address + 0x6000;
  171. pflinfo->start[3] = base_address + 0x8000;
  172. for(j = 1; j < 16; j++)
  173. {
  174. pflinfo->start[j+3] = base_address + 0x00010000 * j;
  175. }
  176. #if 0
  177. /* check for protected sectors */
  178. for (j = 0; j < pflinfo->sector_count; j++) {
  179. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  180. /* D0 = 1 if protected */
  181. addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
  182. if (pflinfo->flash_id & FLASH_MAN_SST)
  183. pflinfo->protect[j] = 0;
  184. else
  185. pflinfo->protect[j] = addr2[2] & 1;
  186. }
  187. #endif
  188. break;
  189. }
  190. /* Protect monitor and environment sectors
  191. */
  192. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  193. flash_protect(FLAG_PROTECT_SET,
  194. CFG_MONITOR_BASE,
  195. CFG_MONITOR_BASE + monitor_flash_len - 1,
  196. &flash_info[0]);
  197. #endif
  198. #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
  199. flash_protect(FLAG_PROTECT_SET,
  200. CFG_ENV_ADDR,
  201. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  202. &flash_info[0]);
  203. #endif
  204. /* reset device to read mode */
  205. flash[0x0000] = 0xf0;
  206. __asm__ __volatile__("sync");
  207. }
  208. return flash_info[0].size + flash_info[1].size;
  209. }
  210. #if 0
  211. static void
  212. flash_get_offsets (ulong base, flash_info_t *info)
  213. {
  214. int i;
  215. /* set up sector start address table */
  216. if (info->flash_id & FLASH_MAN_SST)
  217. {
  218. for (i = 0; i < info->sector_count; i++)
  219. info->start[i] = base + (i * 0x00010000);
  220. }
  221. else
  222. if (info->flash_id & FLASH_BTYPE) {
  223. /* set sector offsets for bottom boot block type */
  224. info->start[0] = base + 0x00000000;
  225. info->start[1] = base + 0x00004000;
  226. info->start[2] = base + 0x00006000;
  227. info->start[3] = base + 0x00008000;
  228. for (i = 4; i < info->sector_count; i++) {
  229. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  230. }
  231. } else {
  232. /* set sector offsets for top boot block type */
  233. i = info->sector_count - 1;
  234. info->start[i--] = base + info->size - 0x00004000;
  235. info->start[i--] = base + info->size - 0x00006000;
  236. info->start[i--] = base + info->size - 0x00008000;
  237. for (; i >= 0; i--) {
  238. info->start[i] = base + i * 0x00010000;
  239. }
  240. }
  241. }
  242. #endif /* 0 */
  243. /*-----------------------------------------------------------------------
  244. */
  245. void
  246. flash_print_info(flash_info_t *info)
  247. {
  248. static const char unk[] = "Unknown";
  249. const char *mfct = unk, *type = unk;
  250. unsigned int i;
  251. if(info->flash_id != FLASH_UNKNOWN)
  252. {
  253. switch(info->flash_id & FLASH_VENDMASK)
  254. {
  255. case FLASH_MAN_AMD: mfct = "AMD"; break;
  256. case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
  257. case FLASH_MAN_STM: mfct = "STM"; break;
  258. case FLASH_MAN_SST: mfct = "SST"; break;
  259. case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
  260. case FLASH_MAN_INTEL: mfct = "Intel"; break;
  261. }
  262. switch(info->flash_id & FLASH_TYPEMASK)
  263. {
  264. case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
  265. case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
  266. case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
  267. case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
  268. case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
  269. case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
  270. case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
  271. case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
  272. case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
  273. case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
  274. case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
  275. }
  276. }
  277. printf(
  278. "\n Brand: %s Type: %s\n"
  279. " Size: %lu KB in %d Sectors\n",
  280. mfct,
  281. type,
  282. info->size >> 10,
  283. info->sector_count
  284. );
  285. printf (" Sector Start Addresses:");
  286. for (i = 0; i < info->sector_count; i++)
  287. {
  288. unsigned long size;
  289. unsigned int erased;
  290. unsigned long * flash = (unsigned long *) info->start[i];
  291. /*
  292. * Check if whole sector is erased
  293. */
  294. size =
  295. (i != (info->sector_count - 1)) ?
  296. (info->start[i + 1] - info->start[i]) >> 2 :
  297. (info->start[0] + info->size - info->start[i]) >> 2;
  298. for(
  299. flash = (unsigned long *) info->start[i], erased = 1;
  300. (flash != (unsigned long *) info->start[i] + size) && erased;
  301. flash++
  302. )
  303. erased = *flash == ~0x0UL;
  304. printf(
  305. "%s %08lX %s %s",
  306. (i % 5) ? "" : "\n ",
  307. info->start[i],
  308. erased ? "E" : " ",
  309. info->protect[i] ? "RO" : " "
  310. );
  311. }
  312. puts("\n");
  313. return;
  314. }
  315. #if 0
  316. /*
  317. * The following code cannot be run from FLASH!
  318. */
  319. ulong
  320. flash_get_size (vu_long *addr, flash_info_t *info)
  321. {
  322. short i;
  323. FLASH_WORD_SIZE value;
  324. ulong base = (ulong)addr;
  325. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  326. printf("flash_get_size: \n");
  327. /* Write auto select command: read Manufacturer ID */
  328. eieio();
  329. addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
  330. addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
  331. addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
  332. value = addr2[0];
  333. switch (value) {
  334. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  335. info->flash_id = FLASH_MAN_AMD;
  336. break;
  337. case (FLASH_WORD_SIZE)FUJ_MANUFACT:
  338. info->flash_id = FLASH_MAN_FUJ;
  339. break;
  340. case (FLASH_WORD_SIZE)SST_MANUFACT:
  341. info->flash_id = FLASH_MAN_SST;
  342. break;
  343. default:
  344. info->flash_id = FLASH_UNKNOWN;
  345. info->sector_count = 0;
  346. info->size = 0;
  347. return (0); /* no or unknown flash */
  348. }
  349. printf("recognised manufacturer");
  350. value = addr2[ADDR3]; /* device ID */
  351. debug ("\ndev_code=%x\n", value);
  352. switch (value) {
  353. case (FLASH_WORD_SIZE)AMD_ID_LV400T:
  354. info->flash_id += FLASH_AM400T;
  355. info->sector_count = 11;
  356. info->size = 0x00080000;
  357. break; /* => 0.5 MB */
  358. case (FLASH_WORD_SIZE)AMD_ID_LV400B:
  359. info->flash_id += FLASH_AM400B;
  360. info->sector_count = 11;
  361. info->size = 0x00080000;
  362. break; /* => 0.5 MB */
  363. case (FLASH_WORD_SIZE)AMD_ID_LV800T:
  364. info->flash_id += FLASH_AM800T;
  365. info->sector_count = 19;
  366. info->size = 0x00100000;
  367. break; /* => 1 MB */
  368. case (FLASH_WORD_SIZE)AMD_ID_LV800B:
  369. info->flash_id += FLASH_AM800B;
  370. info->sector_count = 19;
  371. info->size = 0x00100000;
  372. break; /* => 1 MB */
  373. case (FLASH_WORD_SIZE)AMD_ID_LV160T:
  374. info->flash_id += FLASH_AM160T;
  375. info->sector_count = 35;
  376. info->size = 0x00200000;
  377. break; /* => 2 MB */
  378. case (FLASH_WORD_SIZE)AMD_ID_LV160B:
  379. info->flash_id += FLASH_AM160B;
  380. info->sector_count = 35;
  381. info->size = 0x00200000;
  382. break; /* => 2 MB */
  383. case (FLASH_WORD_SIZE)SST_ID_xF800A:
  384. info->flash_id += FLASH_SST800A;
  385. info->sector_count = 16;
  386. info->size = 0x00100000;
  387. break; /* => 1 MB */
  388. case (FLASH_WORD_SIZE)SST_ID_xF160A:
  389. info->flash_id += FLASH_SST160A;
  390. info->sector_count = 32;
  391. info->size = 0x00200000;
  392. break; /* => 2 MB */
  393. case (FLASH_WORD_SIZE)AMD_ID_F040B:
  394. info->flash_id += FLASH_AM040;
  395. info->sector_count = 8;
  396. info->size = 0x00080000;
  397. break; /* => 0.5 MB */
  398. default:
  399. info->flash_id = FLASH_UNKNOWN;
  400. return (0); /* => no or unknown flash */
  401. }
  402. printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
  403. /* set up sector start address table */
  404. if (info->flash_id & FLASH_MAN_SST)
  405. {
  406. for (i = 0; i < info->sector_count; i++)
  407. info->start[i] = base + (i * 0x00010000);
  408. }
  409. else
  410. if (info->flash_id & FLASH_BTYPE) {
  411. /* set sector offsets for bottom boot block type */
  412. info->start[0] = base + 0x00000000;
  413. info->start[1] = base + 0x00004000;
  414. info->start[2] = base + 0x00006000;
  415. info->start[3] = base + 0x00008000;
  416. for (i = 4; i < info->sector_count; i++) {
  417. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  418. }
  419. } else {
  420. /* set sector offsets for top boot block type */
  421. i = info->sector_count - 1;
  422. info->start[i--] = base + info->size - 0x00004000;
  423. info->start[i--] = base + info->size - 0x00006000;
  424. info->start[i--] = base + info->size - 0x00008000;
  425. for (; i >= 0; i--) {
  426. info->start[i] = base + i * 0x00010000;
  427. }
  428. }
  429. /* check for protected sectors */
  430. for (i = 0; i < info->sector_count; i++) {
  431. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  432. /* D0 = 1 if protected */
  433. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  434. if (info->flash_id & FLASH_MAN_SST)
  435. info->protect[i] = 0;
  436. else
  437. info->protect[i] = addr2[2] & 1;
  438. }
  439. /*
  440. * Prevent writes to uninitialized FLASH.
  441. */
  442. if (info->flash_id != FLASH_UNKNOWN) {
  443. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  444. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  445. }
  446. return (info->size);
  447. }
  448. #endif
  449. int
  450. flash_erase(flash_info_t *info, int s_first, int s_last)
  451. {
  452. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  453. int flag, prot, sect, l_sect;
  454. ulong start, now, last;
  455. unsigned char sh8b;
  456. if ((s_first < 0) || (s_first > s_last)) {
  457. if (info->flash_id == FLASH_UNKNOWN) {
  458. printf ("- missing\n");
  459. } else {
  460. printf ("- no sectors to erase\n");
  461. }
  462. return 1;
  463. }
  464. if ((info->flash_id == FLASH_UNKNOWN) ||
  465. (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
  466. printf ("Can't erase unknown flash type - aborted\n");
  467. return 1;
  468. }
  469. prot = 0;
  470. for (sect=s_first; sect<=s_last; ++sect) {
  471. if (info->protect[sect]) {
  472. prot++;
  473. }
  474. }
  475. if (prot) {
  476. printf ("- Warning: %d protected sectors will not be erased!\n",
  477. prot);
  478. } else {
  479. printf ("\n");
  480. }
  481. l_sect = -1;
  482. /* Check the ROM CS */
  483. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
  484. sh8b = 3;
  485. else
  486. sh8b = 0;
  487. /* Disable interrupts which might cause a timeout here */
  488. flag = disable_interrupts();
  489. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  490. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  491. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  492. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  493. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  494. /* Start erase on unprotected sectors */
  495. for (sect = s_first; sect<=s_last; sect++) {
  496. if (info->protect[sect] == 0) { /* not protected */
  497. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  498. (info->start[sect] - info->start[0]) << sh8b));
  499. if (info->flash_id & FLASH_MAN_SST)
  500. {
  501. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  502. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  503. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  504. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  505. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  506. addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  507. udelay(30000); /* wait 30 ms */
  508. }
  509. else
  510. addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  511. l_sect = sect;
  512. }
  513. }
  514. /* re-enable interrupts if necessary */
  515. if (flag)
  516. enable_interrupts();
  517. /* wait at least 80us - let's wait 1 ms */
  518. udelay (1000);
  519. /*
  520. * We wait for the last triggered sector
  521. */
  522. if (l_sect < 0)
  523. goto DONE;
  524. start = get_timer (0);
  525. last = start;
  526. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  527. (info->start[l_sect] - info->start[0]) << sh8b));
  528. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  529. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  530. printf ("Timeout\n");
  531. return 1;
  532. }
  533. /* show that we're waiting */
  534. if ((now - last) > 1000) { /* every second */
  535. serial_putc ('.');
  536. last = now;
  537. }
  538. }
  539. DONE:
  540. /* reset to read mode */
  541. addr = (FLASH_WORD_SIZE *)info->start[0];
  542. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  543. printf (" done\n");
  544. return 0;
  545. }
  546. /*-----------------------------------------------------------------------
  547. * Copy memory to flash, returns:
  548. * 0 - OK
  549. * 1 - write timeout
  550. * 2 - Flash not erased
  551. */
  552. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  553. {
  554. ulong cp, wp, data;
  555. int i, l, rc;
  556. wp = (addr & ~3); /* get lower word aligned address */
  557. /*
  558. * handle unaligned start bytes
  559. */
  560. if ((l = addr - wp) != 0) {
  561. data = 0;
  562. for (i=0, cp=wp; i<l; ++i, ++cp) {
  563. data = (data << 8) | (*(uchar *)cp);
  564. }
  565. for (; i<4 && cnt>0; ++i) {
  566. data = (data << 8) | *src++;
  567. --cnt;
  568. ++cp;
  569. }
  570. for (; cnt==0 && i<4; ++i, ++cp) {
  571. data = (data << 8) | (*(uchar *)cp);
  572. }
  573. if ((rc = write_word(info, wp, data)) != 0) {
  574. return (rc);
  575. }
  576. wp += 4;
  577. }
  578. /*
  579. * handle word aligned part
  580. */
  581. while (cnt >= 4) {
  582. data = 0;
  583. for (i=0; i<4; ++i) {
  584. data = (data << 8) | *src++;
  585. }
  586. if ((rc = write_word(info, wp, data)) != 0) {
  587. return (rc);
  588. }
  589. wp += 4;
  590. cnt -= 4;
  591. }
  592. if (cnt == 0) {
  593. return (0);
  594. }
  595. /*
  596. * handle unaligned tail bytes
  597. */
  598. data = 0;
  599. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  600. data = (data << 8) | *src++;
  601. --cnt;
  602. }
  603. for (; i<4; ++i, ++cp) {
  604. data = (data << 8) | (*(uchar *)cp);
  605. }
  606. return (write_word(info, wp, data));
  607. }
  608. /*-----------------------------------------------------------------------
  609. * Write a word to Flash, returns:
  610. * 0 - OK
  611. * 1 - write timeout
  612. * 2 - Flash not erased
  613. */
  614. static int write_word (flash_info_t *info, ulong dest, ulong data)
  615. {
  616. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
  617. volatile FLASH_WORD_SIZE *dest2;
  618. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  619. ulong start;
  620. int flag;
  621. int i;
  622. unsigned char sh8b;
  623. /* Check the ROM CS */
  624. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
  625. sh8b = 3;
  626. else
  627. sh8b = 0;
  628. dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
  629. info->start[0]);
  630. /* Check if Flash is (sufficiently) erased */
  631. if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  632. return (2);
  633. }
  634. /* Disable interrupts which might cause a timeout here */
  635. flag = disable_interrupts();
  636. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  637. {
  638. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  639. addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  640. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
  641. dest2[i << sh8b] = data2[i];
  642. /* re-enable interrupts if necessary */
  643. if (flag)
  644. enable_interrupts();
  645. /* data polling for D7 */
  646. start = get_timer (0);
  647. while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
  648. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  649. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  650. return (1);
  651. }
  652. }
  653. }
  654. return (0);
  655. }
  656. /*-----------------------------------------------------------------------
  657. */