flash.c 20 KB

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