flash.c 22 KB

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