flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * (C) Copyright 2000-2004
  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. #if 0
  24. #define DEBUG
  25. #endif
  26. #include <common.h>
  27. #include <mpc8xx.h>
  28. #include <environment.h>
  29. #ifndef CFG_ENV_ADDR
  30. #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  31. #endif
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  33. /*-----------------------------------------------------------------------
  34. * Functions
  35. */
  36. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  37. static int write_word (flash_info_t *info, ulong dest, ulong data);
  38. /*-----------------------------------------------------------------------
  39. */
  40. unsigned long flash_init (void)
  41. {
  42. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  43. volatile memctl8xx_t *memctl = &immap->im_memctl;
  44. unsigned long size_b0, size_b1;
  45. int i;
  46. /* Init: no FLASHes known */
  47. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  48. flash_info[i].flash_id = FLASH_UNKNOWN;
  49. }
  50. /* Static FLASH Bank configuration here - FIXME XXX */
  51. debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
  52. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  53. debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
  54. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  55. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  56. size_b0, size_b0<<20);
  57. }
  58. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  59. debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  60. if (size_b1 > size_b0) {
  61. printf ("## ERROR: "
  62. "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  63. size_b1, size_b1<<20,
  64. size_b0, size_b0<<20
  65. );
  66. flash_info[0].flash_id = FLASH_UNKNOWN;
  67. flash_info[1].flash_id = FLASH_UNKNOWN;
  68. flash_info[0].sector_count = -1;
  69. flash_info[1].sector_count = -1;
  70. flash_info[0].size = 0;
  71. flash_info[1].size = 0;
  72. return (0);
  73. }
  74. debug ("## Before remap: "
  75. "BR0: 0x%08x OR0: 0x%08x "
  76. "BR1: 0x%08x OR1: 0x%08x\n",
  77. memctl->memc_br0, memctl->memc_or0,
  78. memctl->memc_br1, memctl->memc_or1);
  79. /* Remap FLASH according to real size */
  80. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
  81. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
  82. debug ("## BR0: 0x%08x OR0: 0x%08x\n",
  83. memctl->memc_br0, memctl->memc_or0);
  84. /* Re-do sizing to get full correct info */
  85. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  86. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  87. /* monitor protection ON by default */
  88. debug ("Protect monitor: %08lx ... %08lx\n",
  89. (ulong)CFG_MONITOR_BASE,
  90. (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
  91. flash_protect(FLAG_PROTECT_SET,
  92. CFG_MONITOR_BASE,
  93. CFG_MONITOR_BASE + monitor_flash_len - 1,
  94. &flash_info[0]);
  95. #endif
  96. #ifdef CFG_ENV_IS_IN_FLASH
  97. /* ENV protection ON by default */
  98. # ifdef CFG_ENV_ADDR_REDUND
  99. debug ("Protect primary environment: %08lx ... %08lx\n",
  100. (ulong)CFG_ENV_ADDR,
  101. (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
  102. # else
  103. debug ("Protect environment: %08lx ... %08lx\n",
  104. (ulong)CFG_ENV_ADDR,
  105. (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
  106. # endif
  107. flash_protect(FLAG_PROTECT_SET,
  108. CFG_ENV_ADDR,
  109. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  110. &flash_info[0]);
  111. #endif
  112. #ifdef CFG_ENV_ADDR_REDUND
  113. debug ("Protect redundand environment: %08lx ... %08lx\n",
  114. (ulong)CFG_ENV_ADDR_REDUND,
  115. (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
  116. flash_protect(FLAG_PROTECT_SET,
  117. CFG_ENV_ADDR_REDUND,
  118. CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
  119. &flash_info[0]);
  120. #endif
  121. if (size_b1) {
  122. memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
  123. memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
  124. BR_MS_GPCM | BR_V;
  125. debug ("## BR1: 0x%08x OR1: 0x%08x\n",
  126. memctl->memc_br1, memctl->memc_or1);
  127. /* Re-do sizing to get full correct info */
  128. size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
  129. &flash_info[1]);
  130. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  131. /* monitor protection ON by default */
  132. flash_protect(FLAG_PROTECT_SET,
  133. CFG_MONITOR_BASE,
  134. CFG_MONITOR_BASE+monitor_flash_len-1,
  135. &flash_info[1]);
  136. #endif
  137. #ifdef CFG_ENV_IS_IN_FLASH
  138. /* ENV protection ON by default */
  139. flash_protect(FLAG_PROTECT_SET,
  140. CFG_ENV_ADDR,
  141. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  142. &flash_info[1]);
  143. #endif
  144. } else {
  145. memctl->memc_br1 = 0; /* invalidate bank */
  146. flash_info[1].flash_id = FLASH_UNKNOWN;
  147. flash_info[1].sector_count = -1;
  148. flash_info[1].size = 0;
  149. debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
  150. memctl->memc_br1, memctl->memc_or1);
  151. }
  152. debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  153. flash_info[0].size = size_b0;
  154. flash_info[1].size = size_b1;
  155. return (size_b0 + size_b1);
  156. }
  157. /*-----------------------------------------------------------------------
  158. */
  159. void flash_print_info (flash_info_t *info)
  160. {
  161. int i;
  162. if (info->flash_id == FLASH_UNKNOWN) {
  163. printf ("missing or unknown FLASH type\n");
  164. return;
  165. }
  166. switch (info->flash_id & FLASH_VENDMASK) {
  167. case FLASH_MAN_AMD: printf ("AMD "); break;
  168. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  169. default: printf ("Unknown Vendor "); break;
  170. }
  171. switch (info->flash_id & FLASH_TYPEMASK) {
  172. #ifdef CONFIG_TQM8xxM /* mirror bit flash */
  173. case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
  174. break;
  175. case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
  176. break;
  177. case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
  178. break;
  179. case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
  180. break;
  181. # else /* ! TQM8xxM */
  182. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  183. break;
  184. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  185. break;
  186. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  187. break;
  188. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  189. break;
  190. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  191. break;
  192. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  193. break;
  194. #endif /* TQM8xxM */
  195. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  196. break;
  197. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  198. break;
  199. case FLASH_AMDL163B: printf ("AM29DL163B (16 Mbit, bottom boot sect)\n");
  200. break;
  201. default: printf ("Unknown Chip Type\n");
  202. break;
  203. }
  204. printf (" Size: %ld MB in %d Sectors\n",
  205. info->size >> 20, info->sector_count);
  206. printf (" Sector Start Addresses:");
  207. for (i=0; i<info->sector_count; ++i) {
  208. if ((i % 5) == 0)
  209. printf ("\n ");
  210. printf (" %08lX%s",
  211. info->start[i],
  212. info->protect[i] ? " (RO)" : " "
  213. );
  214. }
  215. printf ("\n");
  216. return;
  217. }
  218. /*-----------------------------------------------------------------------
  219. */
  220. /*-----------------------------------------------------------------------
  221. */
  222. /*
  223. * The following code cannot be run from FLASH!
  224. */
  225. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  226. {
  227. short i;
  228. ulong value;
  229. ulong base = (ulong)addr;
  230. /* Write auto select command: read Manufacturer ID */
  231. addr[0x0555] = 0x00AA00AA;
  232. addr[0x02AA] = 0x00550055;
  233. addr[0x0555] = 0x00900090;
  234. value = addr[0];
  235. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  236. switch (value) {
  237. case AMD_MANUFACT:
  238. debug ("Manufacturer: AMD\n");
  239. info->flash_id = FLASH_MAN_AMD;
  240. break;
  241. case FUJ_MANUFACT:
  242. debug ("Manufacturer: FUJITSU\n");
  243. info->flash_id = FLASH_MAN_FUJ;
  244. break;
  245. default:
  246. debug ("Manufacturer: *** unknown ***\n");
  247. info->flash_id = FLASH_UNKNOWN;
  248. info->sector_count = 0;
  249. info->size = 0;
  250. return (0); /* no or unknown flash */
  251. }
  252. value = addr[1]; /* device ID */
  253. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  254. switch (value) {
  255. #ifdef CONFIG_TQM8xxM /* mirror bit flash */
  256. case AMD_ID_MIRROR:
  257. debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
  258. addr[14], addr[15]);
  259. /* Special case for AMLV320MH/L */
  260. if ((addr[14] & 0x00ff00ff) == 0x001d001d &&
  261. (addr[15] & 0x00ff00ff) == 0x00000000) {
  262. debug ("Chip: AMLV320MH/L\n");
  263. info->flash_id += FLASH_AMLV320U;
  264. info->sector_count = 64;
  265. info->size = 0x00800000; /* => 8 MB */
  266. break;
  267. }
  268. switch(addr[14]) {
  269. case AMD_ID_LV128U_2:
  270. if (addr[15] != AMD_ID_LV128U_3) {
  271. debug ("Chip: AMLV128U -> unknown\n");
  272. info->flash_id = FLASH_UNKNOWN;
  273. } else {
  274. debug ("Chip: AMLV128U\n");
  275. info->flash_id += FLASH_AMLV128U;
  276. info->sector_count = 256;
  277. info->size = 0x02000000;
  278. }
  279. break; /* => 32 MB */
  280. case AMD_ID_LV640U_2:
  281. if (addr[15] != AMD_ID_LV640U_3) {
  282. debug ("Chip: AMLV640U -> unknown\n");
  283. info->flash_id = FLASH_UNKNOWN;
  284. } else {
  285. debug ("Chip: AMLV640U\n");
  286. info->flash_id += FLASH_AMLV640U;
  287. info->sector_count = 128;
  288. info->size = 0x01000000;
  289. }
  290. break; /* => 16 MB */
  291. case AMD_ID_LV320B_2:
  292. if (addr[15] != AMD_ID_LV320B_3) {
  293. debug ("Chip: AMLV320B -> unknown\n");
  294. info->flash_id = FLASH_UNKNOWN;
  295. } else {
  296. debug ("Chip: AMLV320B\n");
  297. info->flash_id += FLASH_AMLV320B;
  298. info->sector_count = 71;
  299. info->size = 0x00800000;
  300. }
  301. break; /* => 8 MB */
  302. default:
  303. debug ("Chip: *** unknown ***\n");
  304. info->flash_id = FLASH_UNKNOWN;
  305. break;
  306. }
  307. break;
  308. # else /* ! TQM8xxM */
  309. case AMD_ID_LV400T:
  310. info->flash_id += FLASH_AM400T;
  311. info->sector_count = 11;
  312. info->size = 0x00100000;
  313. break; /* => 1 MB */
  314. case AMD_ID_LV400B:
  315. info->flash_id += FLASH_AM400B;
  316. info->sector_count = 11;
  317. info->size = 0x00100000;
  318. break; /* => 1 MB */
  319. case AMD_ID_LV800T:
  320. info->flash_id += FLASH_AM800T;
  321. info->sector_count = 19;
  322. info->size = 0x00200000;
  323. break; /* => 2 MB */
  324. case AMD_ID_LV800B:
  325. info->flash_id += FLASH_AM800B;
  326. info->sector_count = 19;
  327. info->size = 0x00200000;
  328. break; /* => 2 MB */
  329. case AMD_ID_LV320T:
  330. info->flash_id += FLASH_AM320T;
  331. info->sector_count = 71;
  332. info->size = 0x00800000;
  333. break; /* => 8 MB */
  334. case AMD_ID_LV320B:
  335. info->flash_id += FLASH_AM320B;
  336. info->sector_count = 71;
  337. info->size = 0x00800000;
  338. break; /* => 8 MB */
  339. #endif /* TQM8xxM */
  340. case AMD_ID_LV160T:
  341. info->flash_id += FLASH_AM160T;
  342. info->sector_count = 35;
  343. info->size = 0x00400000;
  344. break; /* => 4 MB */
  345. case AMD_ID_LV160B:
  346. info->flash_id += FLASH_AM160B;
  347. info->sector_count = 35;
  348. info->size = 0x00400000;
  349. break; /* => 4 MB */
  350. case AMD_ID_DL163B:
  351. info->flash_id += FLASH_AMDL163B;
  352. info->sector_count = 39;
  353. info->size = 0x00400000;
  354. break; /* => 4 MB */
  355. default:
  356. info->flash_id = FLASH_UNKNOWN;
  357. return (0); /* => no or unknown flash */
  358. }
  359. /* set up sector start address table */
  360. switch (value) {
  361. #ifdef CONFIG_TQM8xxM /* mirror bit flash */
  362. case AMD_ID_MIRROR:
  363. switch (info->flash_id & FLASH_TYPEMASK) {
  364. /* only known types here - no default */
  365. case FLASH_AMLV128U:
  366. case FLASH_AMLV640U:
  367. case FLASH_AMLV320U:
  368. for (i = 0; i < info->sector_count; i++) {
  369. info->start[i] = base;
  370. base += 0x20000;
  371. }
  372. break;
  373. case FLASH_AMLV320B:
  374. for (i = 0; i < info->sector_count; i++) {
  375. info->start[i] = base;
  376. /*
  377. * The first 8 sectors are 8 kB,
  378. * all the other ones are 64 kB
  379. */
  380. base += (i < 8)
  381. ? 2 * ( 8 << 10)
  382. : 2 * (64 << 10);
  383. }
  384. break;
  385. }
  386. break;
  387. # else /* ! TQM8xxM */
  388. case AMD_ID_LV400B:
  389. case AMD_ID_LV800B:
  390. /* set sector offsets for bottom boot block type */
  391. info->start[0] = base + 0x00000000;
  392. info->start[1] = base + 0x00008000;
  393. info->start[2] = base + 0x0000C000;
  394. info->start[3] = base + 0x00010000;
  395. for (i = 4; i < info->sector_count; i++) {
  396. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  397. }
  398. break;
  399. case AMD_ID_LV400T:
  400. case AMD_ID_LV800T:
  401. /* set sector offsets for top boot block type */
  402. i = info->sector_count - 1;
  403. info->start[i--] = base + info->size - 0x00008000;
  404. info->start[i--] = base + info->size - 0x0000C000;
  405. info->start[i--] = base + info->size - 0x00010000;
  406. for (; i >= 0; i--) {
  407. info->start[i] = base + i * 0x00020000;
  408. }
  409. break;
  410. case AMD_ID_LV320B:
  411. for (i = 0; i < info->sector_count; i++) {
  412. info->start[i] = base;
  413. /*
  414. * The first 8 sectors are 8 kB,
  415. * all the other ones are 64 kB
  416. */
  417. base += (i < 8)
  418. ? 2 * ( 8 << 10)
  419. : 2 * (64 << 10);
  420. }
  421. break;
  422. case AMD_ID_LV320T:
  423. for (i = 0; i < info->sector_count; i++) {
  424. info->start[i] = base;
  425. /*
  426. * The last 8 sectors are 8 kB,
  427. * all the other ones are 64 kB
  428. */
  429. base += (i < (info->sector_count - 8))
  430. ? 2 * (64 << 10)
  431. : 2 * ( 8 << 10);
  432. }
  433. break;
  434. #endif /* TQM8xxM */
  435. case AMD_ID_LV160B:
  436. /* set sector offsets for bottom boot block type */
  437. info->start[0] = base + 0x00000000;
  438. info->start[1] = base + 0x00008000;
  439. info->start[2] = base + 0x0000C000;
  440. info->start[3] = base + 0x00010000;
  441. for (i = 4; i < info->sector_count; i++) {
  442. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  443. }
  444. break;
  445. case AMD_ID_LV160T:
  446. /* set sector offsets for top boot block type */
  447. i = info->sector_count - 1;
  448. info->start[i--] = base + info->size - 0x00008000;
  449. info->start[i--] = base + info->size - 0x0000C000;
  450. info->start[i--] = base + info->size - 0x00010000;
  451. for (; i >= 0; i--) {
  452. info->start[i] = base + i * 0x00020000;
  453. }
  454. break;
  455. case AMD_ID_DL163B:
  456. for (i = 0; i < info->sector_count; i++) {
  457. info->start[i] = base;
  458. /*
  459. * The first 8 sectors are 8 kB,
  460. * all the other ones are 64 kB
  461. */
  462. base += (i < 8)
  463. ? 2 * ( 8 << 10)
  464. : 2 * (64 << 10);
  465. }
  466. break;
  467. default:
  468. return (0);
  469. break;
  470. }
  471. #if 0
  472. /* check for protected sectors */
  473. for (i = 0; i < info->sector_count; i++) {
  474. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  475. /* D0 = 1 if protected */
  476. addr = (volatile unsigned long *)(info->start[i]);
  477. info->protect[i] = addr[2] & 1;
  478. }
  479. #endif
  480. /*
  481. * Prevent writes to uninitialized FLASH.
  482. */
  483. if (info->flash_id != FLASH_UNKNOWN) {
  484. addr = (volatile unsigned long *)info->start[0];
  485. *addr = 0x00F000F0; /* reset bank */
  486. }
  487. return (info->size);
  488. }
  489. /*-----------------------------------------------------------------------
  490. */
  491. int flash_erase (flash_info_t *info, int s_first, int s_last)
  492. {
  493. vu_long *addr = (vu_long*)(info->start[0]);
  494. int flag, prot, sect, l_sect;
  495. ulong start, now, last;
  496. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  497. if ((s_first < 0) || (s_first > s_last)) {
  498. if (info->flash_id == FLASH_UNKNOWN) {
  499. printf ("- missing\n");
  500. } else {
  501. printf ("- no sectors to erase\n");
  502. }
  503. return 1;
  504. }
  505. if ((info->flash_id == FLASH_UNKNOWN) ||
  506. (info->flash_id > FLASH_AMD_COMP)) {
  507. printf ("Can't erase unknown flash type %08lx - aborted\n",
  508. info->flash_id);
  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. }
  517. if (prot) {
  518. printf ("- Warning: %d protected sectors will not be erased!\n",
  519. prot);
  520. } else {
  521. printf ("\n");
  522. }
  523. l_sect = -1;
  524. /* Disable interrupts which might cause a timeout here */
  525. flag = disable_interrupts();
  526. addr[0x0555] = 0x00AA00AA;
  527. addr[0x02AA] = 0x00550055;
  528. addr[0x0555] = 0x00800080;
  529. addr[0x0555] = 0x00AA00AA;
  530. addr[0x02AA] = 0x00550055;
  531. /* Start erase on unprotected sectors */
  532. for (sect = s_first; sect<=s_last; sect++) {
  533. if (info->protect[sect] == 0) { /* not protected */
  534. addr = (vu_long*)(info->start[sect]);
  535. addr[0] = 0x00300030;
  536. l_sect = sect;
  537. }
  538. }
  539. /* re-enable interrupts if necessary */
  540. if (flag)
  541. enable_interrupts();
  542. /* wait at least 80us - let's wait 1 ms */
  543. udelay (1000);
  544. /*
  545. * We wait for the last triggered sector
  546. */
  547. if (l_sect < 0)
  548. goto DONE;
  549. start = get_timer (0);
  550. last = start;
  551. addr = (vu_long*)(info->start[l_sect]);
  552. while ((addr[0] & 0x00800080) != 0x00800080) {
  553. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  554. printf ("Timeout\n");
  555. return 1;
  556. }
  557. /* show that we're waiting */
  558. if ((now - last) > 1000) { /* every second */
  559. putc ('.');
  560. last = now;
  561. }
  562. }
  563. DONE:
  564. /* reset to read mode */
  565. addr = (volatile unsigned long *)info->start[0];
  566. addr[0] = 0x00F000F0; /* reset bank */
  567. printf (" done\n");
  568. return 0;
  569. }
  570. /*-----------------------------------------------------------------------
  571. * Copy memory to flash, returns:
  572. * 0 - OK
  573. * 1 - write timeout
  574. * 2 - Flash not erased
  575. */
  576. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  577. {
  578. ulong cp, wp, data;
  579. int i, l, rc;
  580. wp = (addr & ~3); /* get lower word aligned address */
  581. /*
  582. * handle unaligned start bytes
  583. */
  584. if ((l = addr - wp) != 0) {
  585. data = 0;
  586. for (i=0, cp=wp; i<l; ++i, ++cp) {
  587. data = (data << 8) | (*(uchar *)cp);
  588. }
  589. for (; i<4 && cnt>0; ++i) {
  590. data = (data << 8) | *src++;
  591. --cnt;
  592. ++cp;
  593. }
  594. for (; cnt==0 && i<4; ++i, ++cp) {
  595. data = (data << 8) | (*(uchar *)cp);
  596. }
  597. if ((rc = write_word(info, wp, data)) != 0) {
  598. return (rc);
  599. }
  600. wp += 4;
  601. }
  602. /*
  603. * handle word aligned part
  604. */
  605. while (cnt >= 4) {
  606. data = 0;
  607. for (i=0; i<4; ++i) {
  608. data = (data << 8) | *src++;
  609. }
  610. if ((rc = write_word(info, wp, data)) != 0) {
  611. return (rc);
  612. }
  613. wp += 4;
  614. cnt -= 4;
  615. }
  616. if (cnt == 0) {
  617. return (0);
  618. }
  619. /*
  620. * handle unaligned tail bytes
  621. */
  622. data = 0;
  623. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  624. data = (data << 8) | *src++;
  625. --cnt;
  626. }
  627. for (; i<4; ++i, ++cp) {
  628. data = (data << 8) | (*(uchar *)cp);
  629. }
  630. return (write_word(info, wp, data));
  631. }
  632. /*-----------------------------------------------------------------------
  633. * Write a word to Flash, returns:
  634. * 0 - OK
  635. * 1 - write timeout
  636. * 2 - Flash not erased
  637. */
  638. static int write_word (flash_info_t *info, ulong dest, ulong data)
  639. {
  640. vu_long *addr = (vu_long*)(info->start[0]);
  641. ulong start;
  642. int flag;
  643. /* Check if Flash is (sufficiently) erased */
  644. if ((*((vu_long *)dest) & data) != data) {
  645. return (2);
  646. }
  647. /* Disable interrupts which might cause a timeout here */
  648. flag = disable_interrupts();
  649. addr[0x0555] = 0x00AA00AA;
  650. addr[0x02AA] = 0x00550055;
  651. addr[0x0555] = 0x00A000A0;
  652. *((vu_long *)dest) = data;
  653. /* re-enable interrupts if necessary */
  654. if (flag)
  655. enable_interrupts();
  656. /* data polling for D7 */
  657. start = get_timer (0);
  658. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  659. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  660. return (1);
  661. }
  662. }
  663. return (0);
  664. }
  665. /*-----------------------------------------------------------------------
  666. */