flash.c 19 KB

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