flash.c 21 KB

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