flash.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * (C) Masami Komiya <mkomiya@sonare.it> 2004
  3. *
  4. * (C) Copyright 2001-2004
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <ppc4xx.h>
  27. #include <asm/processor.h>
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  29. #ifdef CFG_FLASH_16BIT
  30. #define FLASH_WORD_SIZE unsigned short
  31. #define FLASH_ID_MASK 0xFFFF
  32. #else
  33. #define FLASH_WORD_SIZE unsigned long
  34. #define FLASH_ID_MASK 0xFFFFFFFF
  35. #endif
  36. /*-----------------------------------------------------------------------
  37. * Functions
  38. */
  39. /* stolen from esteem192e/flash.c */
  40. ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
  41. #ifndef CFG_FLASH_16BIT
  42. static int write_word (flash_info_t * info, ulong dest, ulong data);
  43. #else
  44. static int write_short (flash_info_t * info, ulong dest, ushort data);
  45. #endif
  46. static void flash_get_offsets (ulong base, flash_info_t * info);
  47. /*-----------------------------------------------------------------------
  48. */
  49. unsigned long flash_init (void)
  50. {
  51. unsigned long size_b0, size_b1;
  52. int i;
  53. uint pbcr;
  54. unsigned long base_b0, base_b1;
  55. /* Init: no FLASHes known */
  56. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  57. flash_info[i].flash_id = FLASH_UNKNOWN;
  58. }
  59. /* Static FLASH Bank configuration here - FIXME XXX */
  60. size_b0 =
  61. flash_get_size ((volatile FLASH_WORD_SIZE *) CFG_FLASH_BASE,
  62. &flash_info[0]);
  63. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  64. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
  65. }
  66. /* Only one bank */
  67. if (CFG_MAX_FLASH_BANKS == 1) {
  68. /* Setup offsets */
  69. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  70. /* Monitor protection ON by default */
  71. #if 0 /* sand: */
  72. (void) flash_protect (FLAG_PROTECT_SET,
  73. FLASH_BASE0_PRELIM - monitor_flash_len +
  74. size_b0,
  75. FLASH_BASE0_PRELIM - 1 + size_b0,
  76. &flash_info[0]);
  77. #else
  78. (void) flash_protect (FLAG_PROTECT_SET,
  79. CFG_MONITOR_BASE,
  80. CFG_MONITOR_BASE + monitor_flash_len -
  81. 1, &flash_info[0]);
  82. #endif
  83. size_b1 = 0;
  84. flash_info[0].size = size_b0;
  85. }
  86. #ifdef CFG_FLASH_BASE_2
  87. /* 2 banks */
  88. else {
  89. size_b1 =
  90. flash_get_size ((volatile FLASH_WORD_SIZE *)
  91. CFG_FLASH_BASE_2, &flash_info[1]);
  92. /* Re-do sizing to get full correct info */
  93. if (size_b1) {
  94. mtdcr (ebccfga, pb0cr);
  95. pbcr = mfdcr (ebccfgd);
  96. mtdcr (ebccfga, pb0cr);
  97. base_b1 = -size_b1;
  98. pbcr = (pbcr & 0x0001ffff) | base_b1 |
  99. (((size_b1 / 1024 / 1024) - 1) << 17);
  100. mtdcr (ebccfgd, pbcr);
  101. /* printf("pb1cr = %x\n", pbcr); */
  102. }
  103. if (size_b0) {
  104. mtdcr (ebccfga, pb1cr);
  105. pbcr = mfdcr (ebccfgd);
  106. mtdcr (ebccfga, pb1cr);
  107. base_b0 = base_b1 - size_b0;
  108. pbcr = (pbcr & 0x0001ffff) | base_b0 |
  109. (((size_b0 / 1024 / 1024) - 1) << 17);
  110. mtdcr (ebccfgd, pbcr);
  111. /* printf("pb0cr = %x\n", pbcr); */
  112. }
  113. size_b0 =
  114. flash_get_size ((volatile FLASH_WORD_SIZE *) base_b0,
  115. &flash_info[0]);
  116. flash_get_offsets (base_b0, &flash_info[0]);
  117. /* monitor protection ON by default */
  118. #if 0 /* sand: */
  119. (void) flash_protect (FLAG_PROTECT_SET,
  120. FLASH_BASE0_PRELIM - monitor_flash_len +
  121. size_b0,
  122. FLASH_BASE0_PRELIM - 1 + size_b0,
  123. &flash_info[0]);
  124. #else
  125. (void) flash_protect (FLAG_PROTECT_SET,
  126. CFG_MONITOR_BASE,
  127. CFG_MONITOR_BASE + monitor_flash_len -
  128. 1, &flash_info[0]);
  129. #endif
  130. if (size_b1) {
  131. /* Re-do sizing to get full correct info */
  132. size_b1 =
  133. flash_get_size ((volatile FLASH_WORD_SIZE *)
  134. base_b1, &flash_info[1]);
  135. flash_get_offsets (base_b1, &flash_info[1]);
  136. /* monitor protection ON by default */
  137. (void) flash_protect (FLAG_PROTECT_SET,
  138. base_b1 + size_b1 -
  139. monitor_flash_len,
  140. base_b1 + size_b1 - 1,
  141. &flash_info[1]);
  142. /* monitor protection OFF by default (one is enough) */
  143. (void) flash_protect (FLAG_PROTECT_CLEAR,
  144. base_b0 + size_b0 -
  145. monitor_flash_len,
  146. base_b0 + size_b0 - 1,
  147. &flash_info[0]);
  148. } else {
  149. flash_info[1].flash_id = FLASH_UNKNOWN;
  150. flash_info[1].sector_count = -1;
  151. }
  152. flash_info[0].size = size_b0;
  153. flash_info[1].size = size_b1;
  154. } /* else 2 banks */
  155. #endif
  156. return (size_b0 + size_b1);
  157. }
  158. /*-----------------------------------------------------------------------
  159. */
  160. static void flash_get_offsets (ulong base, flash_info_t * info)
  161. {
  162. int i;
  163. /* set up sector start adress table */
  164. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
  165. (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
  166. (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
  167. for (i = 0; i < info->sector_count; i++) {
  168. info->start[i] =
  169. base + (i * info->size / info->sector_count);
  170. }
  171. } else if (info->flash_id & FLASH_BTYPE) {
  172. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  173. #ifndef CFG_FLASH_16BIT
  174. /* set sector offsets for bottom boot block type */
  175. info->start[0] = base + 0x00000000;
  176. info->start[1] = base + 0x00004000;
  177. info->start[2] = base + 0x00008000;
  178. info->start[3] = base + 0x0000C000;
  179. info->start[4] = base + 0x00010000;
  180. info->start[5] = base + 0x00014000;
  181. info->start[6] = base + 0x00018000;
  182. info->start[7] = base + 0x0001C000;
  183. for (i = 8; i < info->sector_count; i++) {
  184. info->start[i] =
  185. base + (i * 0x00020000) - 0x000E0000;
  186. }
  187. } else {
  188. /* set sector offsets for bottom boot block type */
  189. info->start[0] = base + 0x00000000;
  190. info->start[1] = base + 0x00008000;
  191. info->start[2] = base + 0x0000C000;
  192. info->start[3] = base + 0x00010000;
  193. for (i = 4; i < info->sector_count; i++) {
  194. info->start[i] =
  195. base + (i * 0x00020000) - 0x00060000;
  196. }
  197. }
  198. #else
  199. /* set sector offsets for bottom boot block type */
  200. info->start[0] = base + 0x00000000;
  201. info->start[1] = base + 0x00002000;
  202. info->start[2] = base + 0x00004000;
  203. info->start[3] = base + 0x00006000;
  204. info->start[4] = base + 0x00008000;
  205. info->start[5] = base + 0x0000A000;
  206. info->start[6] = base + 0x0000C000;
  207. info->start[7] = base + 0x0000E000;
  208. for (i = 8; i < info->sector_count; i++) {
  209. info->start[i] =
  210. base + (i * 0x00010000) - 0x00070000;
  211. }
  212. } else {
  213. /* set sector offsets for bottom boot block type */
  214. info->start[0] = base + 0x00000000;
  215. info->start[1] = base + 0x00004000;
  216. info->start[2] = base + 0x00006000;
  217. info->start[3] = base + 0x00008000;
  218. for (i = 4; i < info->sector_count; i++) {
  219. info->start[i] =
  220. base + (i * 0x00010000) - 0x00030000;
  221. }
  222. }
  223. #endif
  224. } else {
  225. /* set sector offsets for top boot block type */
  226. i = info->sector_count - 1;
  227. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  228. #ifndef CFG_FLASH_16BIT
  229. info->start[i--] = base + info->size - 0x00004000;
  230. info->start[i--] = base + info->size - 0x00008000;
  231. info->start[i--] = base + info->size - 0x0000C000;
  232. info->start[i--] = base + info->size - 0x00010000;
  233. info->start[i--] = base + info->size - 0x00014000;
  234. info->start[i--] = base + info->size - 0x00018000;
  235. info->start[i--] = base + info->size - 0x0001C000;
  236. for (; i >= 0; i--) {
  237. info->start[i] = base + i * 0x00020000;
  238. }
  239. } else {
  240. info->start[i--] = base + info->size - 0x00008000;
  241. info->start[i--] = base + info->size - 0x0000C000;
  242. info->start[i--] = base + info->size - 0x00010000;
  243. for (; i >= 0; i--) {
  244. info->start[i] = base + i * 0x00020000;
  245. }
  246. }
  247. #else
  248. info->start[i--] = base + info->size - 0x00002000;
  249. info->start[i--] = base + info->size - 0x00004000;
  250. info->start[i--] = base + info->size - 0x00006000;
  251. info->start[i--] = base + info->size - 0x00008000;
  252. info->start[i--] = base + info->size - 0x0000A000;
  253. info->start[i--] = base + info->size - 0x0000C000;
  254. info->start[i--] = base + info->size - 0x0000E000;
  255. for (; i >= 0; i--) {
  256. info->start[i] = base + i * 0x00010000;
  257. }
  258. } else {
  259. info->start[i--] = base + info->size - 0x00004000;
  260. info->start[i--] = base + info->size - 0x00006000;
  261. info->start[i--] = base + info->size - 0x00008000;
  262. for (; i >= 0; i--) {
  263. info->start[i] = base + i * 0x00010000;
  264. }
  265. }
  266. #endif
  267. }
  268. }
  269. /*-----------------------------------------------------------------------
  270. */
  271. void flash_print_info (flash_info_t * info)
  272. {
  273. int i;
  274. uchar *boottype;
  275. uchar botboot[] = ", bottom boot sect)\n";
  276. uchar topboot[] = ", top boot sector)\n";
  277. if (info->flash_id == FLASH_UNKNOWN) {
  278. printf ("missing or unknown FLASH type\n");
  279. return;
  280. }
  281. switch (info->flash_id & FLASH_VENDMASK) {
  282. case FLASH_MAN_AMD:
  283. printf ("AMD ");
  284. break;
  285. case FLASH_MAN_FUJ:
  286. printf ("FUJITSU ");
  287. break;
  288. case FLASH_MAN_SST:
  289. printf ("SST ");
  290. break;
  291. case FLASH_MAN_STM:
  292. printf ("STM ");
  293. break;
  294. case FLASH_MAN_INTEL:
  295. printf ("INTEL ");
  296. break;
  297. default:
  298. printf ("Unknown Vendor ");
  299. break;
  300. }
  301. if (info->flash_id & 0x0001) {
  302. boottype = botboot;
  303. } else {
  304. boottype = topboot;
  305. }
  306. switch (info->flash_id & FLASH_TYPEMASK) {
  307. case FLASH_AM400B:
  308. printf ("AM29LV400B (4 Mbit%s", boottype);
  309. break;
  310. case FLASH_AM400T:
  311. printf ("AM29LV400T (4 Mbit%s", boottype);
  312. break;
  313. case FLASH_AM800B:
  314. printf ("AM29LV800B (8 Mbit%s", boottype);
  315. break;
  316. case FLASH_AM800T:
  317. printf ("AM29LV800T (8 Mbit%s", boottype);
  318. break;
  319. case FLASH_AM160B:
  320. printf ("AM29LV160B (16 Mbit%s", boottype);
  321. break;
  322. case FLASH_AM160T:
  323. printf ("AM29LV160T (16 Mbit%s", boottype);
  324. break;
  325. case FLASH_AM320B:
  326. printf ("AM29LV320B (32 Mbit%s", boottype);
  327. break;
  328. case FLASH_AM320T:
  329. printf ("AM29LV320T (32 Mbit%s", boottype);
  330. break;
  331. case FLASH_INTEL800B:
  332. printf ("INTEL28F800B (8 Mbit%s", boottype);
  333. break;
  334. case FLASH_INTEL800T:
  335. printf ("INTEL28F800T (8 Mbit%s", boottype);
  336. break;
  337. case FLASH_INTEL160B:
  338. printf ("INTEL28F160B (16 Mbit%s", boottype);
  339. break;
  340. case FLASH_INTEL160T:
  341. printf ("INTEL28F160T (16 Mbit%s", boottype);
  342. break;
  343. case FLASH_INTEL320B:
  344. printf ("INTEL28F320B (32 Mbit%s", boottype);
  345. break;
  346. case FLASH_INTEL320T:
  347. printf ("INTEL28F320T (32 Mbit%s", boottype);
  348. break;
  349. #if 0 /* enable when devices are available */
  350. case FLASH_INTEL640B:
  351. printf ("INTEL28F640B (64 Mbit%s", boottype);
  352. break;
  353. case FLASH_INTEL640T:
  354. printf ("INTEL28F640T (64 Mbit%s", boottype);
  355. break;
  356. #endif
  357. case FLASH_28F320J3A:
  358. printf ("INTEL28F320J3A (32 Mbit%s", boottype);
  359. break;
  360. case FLASH_28F640J3A:
  361. printf ("INTEL28F640J3A (64 Mbit%s", boottype);
  362. break;
  363. case FLASH_28F128J3A:
  364. printf ("INTEL28F128J3A (128 Mbit%s", boottype);
  365. break;
  366. default:
  367. printf ("Unknown Chip Type\n");
  368. break;
  369. }
  370. printf (" Size: %ld MB in %d Sectors\n",
  371. info->size >> 20, info->sector_count);
  372. printf (" Sector Start Addresses:");
  373. for (i = 0; i < info->sector_count; ++i) {
  374. if ((i % 5) == 0)
  375. printf ("\n ");
  376. printf (" %08lX%s",
  377. info->start[i], info->protect[i] ? " (RO)" : " ");
  378. }
  379. printf ("\n");
  380. return;
  381. }
  382. /*-----------------------------------------------------------------------
  383. */
  384. /*-----------------------------------------------------------------------
  385. */
  386. /*
  387. * The following code cannot be run from FLASH!
  388. */
  389. ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
  390. {
  391. short i;
  392. ulong base = (ulong) addr;
  393. FLASH_WORD_SIZE value;
  394. /* Write auto select command: read Manufacturer ID */
  395. #ifndef CFG_FLASH_16BIT
  396. /*
  397. * Note: if it is an AMD flash and the word at addr[0000]
  398. * is 0x00890089 this routine will think it is an Intel
  399. * flash device and may(most likely) cause trouble.
  400. */
  401. addr[0x0000] = 0x00900090;
  402. if (addr[0x0000] != 0x00890089) {
  403. addr[0x0555] = 0x00AA00AA;
  404. addr[0x02AA] = 0x00550055;
  405. addr[0x0555] = 0x00900090;
  406. #else
  407. /*
  408. * Note: if it is an AMD flash and the word at addr[0000]
  409. * is 0x0089 this routine will think it is an Intel
  410. * flash device and may(most likely) cause trouble.
  411. */
  412. addr[0x0000] = 0x0090;
  413. if (addr[0x0000] != 0x0089) {
  414. addr[0x0555] = 0x00AA;
  415. addr[0x02AA] = 0x0055;
  416. addr[0x0555] = 0x0090;
  417. #endif
  418. }
  419. value = addr[0];
  420. switch (value) {
  421. case (AMD_MANUFACT & FLASH_ID_MASK):
  422. info->flash_id = FLASH_MAN_AMD;
  423. break;
  424. case (FUJ_MANUFACT & FLASH_ID_MASK):
  425. info->flash_id = FLASH_MAN_FUJ;
  426. break;
  427. case (STM_MANUFACT & FLASH_ID_MASK):
  428. info->flash_id = FLASH_MAN_STM;
  429. break;
  430. case (SST_MANUFACT & FLASH_ID_MASK):
  431. info->flash_id = FLASH_MAN_SST;
  432. break;
  433. case (INTEL_MANUFACT & FLASH_ID_MASK):
  434. info->flash_id = FLASH_MAN_INTEL;
  435. break;
  436. default:
  437. info->flash_id = FLASH_UNKNOWN;
  438. info->sector_count = 0;
  439. info->size = 0;
  440. return (0); /* no or unknown flash */
  441. }
  442. value = addr[1]; /* device ID */
  443. switch (value) {
  444. case (AMD_ID_LV400T & FLASH_ID_MASK):
  445. info->flash_id += FLASH_AM400T;
  446. info->sector_count = 11;
  447. info->size = 0x00100000;
  448. break; /* => 1 MB */
  449. case (AMD_ID_LV400B & FLASH_ID_MASK):
  450. info->flash_id += FLASH_AM400B;
  451. info->sector_count = 11;
  452. info->size = 0x00100000;
  453. break; /* => 1 MB */
  454. case (AMD_ID_LV800T & FLASH_ID_MASK):
  455. info->flash_id += FLASH_AM800T;
  456. info->sector_count = 19;
  457. info->size = 0x00200000;
  458. break; /* => 2 MB */
  459. case (AMD_ID_LV800B & FLASH_ID_MASK):
  460. info->flash_id += FLASH_AM800B;
  461. info->sector_count = 19;
  462. info->size = 0x00200000;
  463. break; /* => 2 MB */
  464. case (AMD_ID_LV160T & FLASH_ID_MASK):
  465. info->flash_id += FLASH_AM160T;
  466. info->sector_count = 35;
  467. info->size = 0x00400000;
  468. break; /* => 4 MB */
  469. case (AMD_ID_LV160B & FLASH_ID_MASK):
  470. info->flash_id += FLASH_AM160B;
  471. info->sector_count = 35;
  472. info->size = 0x00400000;
  473. break; /* => 4 MB */
  474. #if 0 /* enable when device IDs are available */
  475. case (AMD_ID_LV320T & FLASH_ID_MASK):
  476. info->flash_id += FLASH_AM320T;
  477. info->sector_count = 67;
  478. info->size = 0x00800000;
  479. break; /* => 8 MB */
  480. case (AMD_ID_LV320B & FLASH_ID_MASK):
  481. info->flash_id += FLASH_AM320B;
  482. info->sector_count = 67;
  483. info->size = 0x00800000;
  484. break; /* => 8 MB */
  485. #endif
  486. case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
  487. info->flash_id += FLASH_INTEL800T;
  488. info->sector_count = 23;
  489. info->size = 0x00200000;
  490. break; /* => 2 MB */
  491. case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
  492. info->flash_id += FLASH_INTEL800B;
  493. info->sector_count = 23;
  494. info->size = 0x00200000;
  495. break; /* => 2 MB */
  496. case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
  497. info->flash_id += FLASH_INTEL160T;
  498. info->sector_count = 39;
  499. info->size = 0x00400000;
  500. break; /* => 4 MB */
  501. case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
  502. info->flash_id += FLASH_INTEL160B;
  503. info->sector_count = 39;
  504. info->size = 0x00400000;
  505. break; /* => 4 MB */
  506. case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
  507. info->flash_id += FLASH_INTEL320T;
  508. info->sector_count = 71;
  509. info->size = 0x00800000;
  510. break; /* => 8 MB */
  511. case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
  512. info->flash_id += FLASH_AM320B;
  513. info->sector_count = 71;
  514. info->size = 0x00800000;
  515. break; /* => 8 MB */
  516. #if 0 /* enable when devices are available */
  517. case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
  518. info->flash_id += FLASH_INTEL320T;
  519. info->sector_count = 135;
  520. info->size = 0x01000000;
  521. break; /* => 16 MB */
  522. case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
  523. info->flash_id += FLASH_AM320B;
  524. info->sector_count = 135;
  525. info->size = 0x01000000;
  526. break; /* => 16 MB */
  527. #endif
  528. case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
  529. info->flash_id += FLASH_28F320J3A;
  530. info->sector_count = 32;
  531. info->size = 0x00400000;
  532. break; /* => 32 MBit */
  533. case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
  534. info->flash_id += FLASH_28F640J3A;
  535. info->sector_count = 64;
  536. info->size = 0x00800000;
  537. break; /* => 64 MBit */
  538. case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
  539. info->flash_id += FLASH_28F128J3A;
  540. info->sector_count = 128;
  541. info->size = 0x01000000;
  542. break; /* => 128 MBit */
  543. default:
  544. /* FIXME */
  545. info->flash_id = FLASH_UNKNOWN;
  546. return (0); /* => no or unknown flash */
  547. }
  548. flash_get_offsets (base, info);
  549. /* check for protected sectors */
  550. for (i = 0; i < info->sector_count; i++) {
  551. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  552. /* D0 = 1 if protected */
  553. addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
  554. info->protect[i] = addr[2] & 1;
  555. }
  556. /*
  557. * Prevent writes to uninitialized FLASH.
  558. */
  559. if (info->flash_id != FLASH_UNKNOWN) {
  560. addr = (volatile FLASH_WORD_SIZE *) info->start[0];
  561. if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
  562. *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
  563. } else {
  564. *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
  565. }
  566. }
  567. return (info->size);
  568. }
  569. /*-----------------------------------------------------------------------
  570. */
  571. int flash_erase (flash_info_t * info, int s_first, int s_last)
  572. {
  573. volatile FLASH_WORD_SIZE *addr =
  574. (volatile FLASH_WORD_SIZE *) (info->start[0]);
  575. int flag, prot, sect, l_sect, barf;
  576. ulong start, now, last;
  577. int rcode = 0;
  578. if ((s_first < 0) || (s_first > s_last)) {
  579. if (info->flash_id == FLASH_UNKNOWN) {
  580. printf ("- missing\n");
  581. } else {
  582. printf ("- no sectors to erase\n");
  583. }
  584. return 1;
  585. }
  586. if ((info->flash_id == FLASH_UNKNOWN) ||
  587. ((info->flash_id > FLASH_AMD_COMP) &&
  588. ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
  589. printf ("Can't erase unknown flash type - aborted\n");
  590. return 1;
  591. }
  592. prot = 0;
  593. for (sect = s_first; sect <= s_last; ++sect) {
  594. if (info->protect[sect]) {
  595. prot++;
  596. }
  597. }
  598. if (prot) {
  599. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  600. } else {
  601. printf ("\n");
  602. }
  603. l_sect = -1;
  604. /* Disable interrupts which might cause a timeout here */
  605. flag = disable_interrupts ();
  606. if (info->flash_id < FLASH_AMD_COMP) {
  607. #ifndef CFG_FLASH_16BIT
  608. addr[0x0555] = 0x00AA00AA;
  609. addr[0x02AA] = 0x00550055;
  610. addr[0x0555] = 0x00800080;
  611. addr[0x0555] = 0x00AA00AA;
  612. addr[0x02AA] = 0x00550055;
  613. #else
  614. addr[0x0555] = 0x00AA;
  615. addr[0x02AA] = 0x0055;
  616. addr[0x0555] = 0x0080;
  617. addr[0x0555] = 0x00AA;
  618. addr[0x02AA] = 0x0055;
  619. #endif
  620. /* Start erase on unprotected sectors */
  621. for (sect = s_first; sect <= s_last; sect++) {
  622. if (info->protect[sect] == 0) { /* not protected */
  623. addr = (volatile FLASH_WORD_SIZE *) (info->
  624. start
  625. [sect]);
  626. addr[0] = (0x00300030 & FLASH_ID_MASK);
  627. l_sect = sect;
  628. }
  629. }
  630. /* re-enable interrupts if necessary */
  631. if (flag)
  632. enable_interrupts ();
  633. /* wait at least 80us - let's wait 1 ms */
  634. udelay (1000);
  635. /*
  636. * We wait for the last triggered sector
  637. */
  638. if (l_sect < 0)
  639. goto DONE;
  640. start = get_timer (0);
  641. last = start;
  642. addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
  643. while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
  644. (0x00800080 & FLASH_ID_MASK)) {
  645. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  646. printf ("Timeout\n");
  647. return 1;
  648. }
  649. /* show that we're waiting */
  650. if ((now - last) > 1000000) { /* every second */
  651. serial_putc ('.');
  652. last = now;
  653. }
  654. }
  655. DONE:
  656. /* reset to read mode */
  657. addr = (volatile FLASH_WORD_SIZE *) info->start[0];
  658. addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
  659. } else {
  660. for (sect = s_first; sect <= s_last; sect++) {
  661. if (info->protect[sect] == 0) { /* not protected */
  662. barf = 0;
  663. #ifndef CFG_FLASH_16BIT
  664. addr = (vu_long *) (info->start[sect]);
  665. addr[0] = 0x00500050;
  666. addr[0] = 0x00200020;
  667. addr[0] = 0x00D000D0;
  668. while (!(addr[0] & 0x00800080)); /* wait for error or finish */
  669. if (addr[0] & 0x003A003A) { /* check for error */
  670. barf = addr[0] & 0x003A0000;
  671. if (barf) {
  672. barf >>= 16;
  673. } else {
  674. barf = addr[0] & 0x0000003A;
  675. }
  676. }
  677. #else
  678. addr = (vu_short *) (info->start[sect]);
  679. addr[0] = 0x0050; /* clear status register */
  680. addr[0] = 0x0020;
  681. addr[0] = 0x00D0;
  682. while (!(addr[0] & 0x0080)); /* wait for error or finish */
  683. if (addr[0] & 0x003A) /* check for error */
  684. barf = addr[0] & 0x003A;
  685. #endif
  686. if (barf) {
  687. printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
  688. if (barf & 0x0002)
  689. printf ("Block locked, not erased.\n");
  690. if ((barf & 0x0030) == 0x0030)
  691. printf ("Command Sequence error.\n");
  692. if ((barf & 0x0030) == 0x0020)
  693. printf ("Block Erase error.\n");
  694. if (barf & 0x0008)
  695. printf ("Vpp Low error.\n");
  696. rcode = 1;
  697. } else
  698. printf (".");
  699. l_sect = sect;
  700. }
  701. addr = (volatile FLASH_WORD_SIZE *) info->start[0];
  702. #ifndef CFG_FLASH_16BIT
  703. addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
  704. #else
  705. addr[0] = (0x00FF & FLASH_ID_MASK); /* reset bank */
  706. #endif
  707. }
  708. }
  709. printf (" done\n");
  710. return rcode;
  711. }
  712. /*-----------------------------------------------------------------------
  713. */
  714. /*flash_info_t *addr2info (ulong addr)
  715. {
  716. flash_info_t *info;
  717. int i;
  718. for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
  719. if ((addr >= info->start[0]) &&
  720. (addr < (info->start[0] + info->size)) ) {
  721. return (info);
  722. }
  723. }
  724. return (NULL);
  725. }
  726. */
  727. /*-----------------------------------------------------------------------
  728. * Copy memory to flash.
  729. * Make sure all target addresses are within Flash bounds,
  730. * and no protected sectors are hit.
  731. * Returns:
  732. * 0 - OK
  733. * 1 - write timeout
  734. * 2 - Flash not erased
  735. * 4 - target range includes protected sectors
  736. * 8 - target address not in Flash memory
  737. */
  738. /*int flash_write (uchar *src, ulong addr, ulong cnt)
  739. {
  740. int i;
  741. ulong end = addr + cnt - 1;
  742. flash_info_t *info_first = addr2info (addr);
  743. flash_info_t *info_last = addr2info (end );
  744. flash_info_t *info;
  745. if (cnt == 0) {
  746. return (0);
  747. }
  748. if (!info_first || !info_last) {
  749. return (8);
  750. }
  751. for (info = info_first; info <= info_last; ++info) {
  752. ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
  753. /* short s_end = info->sector_count - 1;
  754. for (i=0; i<info->sector_count; ++i) {
  755. ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
  756. if ((end >= info->start[i]) && (addr < e_addr) &&
  757. (info->protect[i] != 0) ) {
  758. return (4);
  759. }
  760. }
  761. }
  762. */ /* finally write data to flash */
  763. /* for (info = info_first; info <= info_last && cnt>0; ++info) {
  764. ulong len;
  765. len = info->start[0] + info->size - addr;
  766. if (len > cnt)
  767. len = cnt;
  768. if ((i = write_buff(info, src, addr, len)) != 0) {
  769. return (i);
  770. }
  771. cnt -= len;
  772. addr += len;
  773. src += len;
  774. }
  775. return (0);
  776. }
  777. */
  778. /*-----------------------------------------------------------------------
  779. * Copy memory to flash, returns:
  780. * 0 - OK
  781. * 1 - write timeout
  782. * 2 - Flash not erased
  783. */
  784. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  785. {
  786. #ifndef CFG_FLASH_16BIT
  787. ulong cp, wp, data;
  788. int l;
  789. #else
  790. ulong cp, wp;
  791. ushort data;
  792. #endif
  793. int i, rc;
  794. #ifndef CFG_FLASH_16BIT
  795. wp = (addr & ~3); /* get lower word aligned address */
  796. /*
  797. * handle unaligned start bytes
  798. */
  799. if ((l = addr - wp) != 0) {
  800. data = 0;
  801. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  802. data = (data << 8) | (*(uchar *) cp);
  803. }
  804. for (; i < 4 && cnt > 0; ++i) {
  805. data = (data << 8) | *src++;
  806. --cnt;
  807. ++cp;
  808. }
  809. for (; cnt == 0 && i < 4; ++i, ++cp) {
  810. data = (data << 8) | (*(uchar *) cp);
  811. }
  812. if ((rc = write_word (info, wp, data)) != 0) {
  813. return (rc);
  814. }
  815. wp += 4;
  816. }
  817. /*
  818. * handle word aligned part
  819. */
  820. while (cnt >= 4) {
  821. data = 0;
  822. for (i = 0; i < 4; ++i) {
  823. data = (data << 8) | *src++;
  824. }
  825. if ((rc = write_word (info, wp, data)) != 0) {
  826. return (rc);
  827. }
  828. wp += 4;
  829. cnt -= 4;
  830. }
  831. if (cnt == 0) {
  832. return (0);
  833. }
  834. /*
  835. * handle unaligned tail bytes
  836. */
  837. data = 0;
  838. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  839. data = (data << 8) | *src++;
  840. --cnt;
  841. }
  842. for (; i < 4; ++i, ++cp) {
  843. data = (data << 8) | (*(uchar *) cp);
  844. }
  845. return (write_word (info, wp, data));
  846. #else
  847. wp = (addr & ~1); /* get lower word aligned address */
  848. /*
  849. * handle unaligned start byte
  850. */
  851. if (addr - wp) {
  852. data = 0;
  853. data = (data << 8) | *src++;
  854. --cnt;
  855. if ((rc = write_short (info, wp, data)) != 0) {
  856. return (rc);
  857. }
  858. wp += 2;
  859. }
  860. /*
  861. * handle word aligned part
  862. */
  863. /* l = 0; used for debuging */
  864. while (cnt >= 2) {
  865. data = 0;
  866. for (i = 0; i < 2; ++i) {
  867. data = (data << 8) | *src++;
  868. }
  869. /* if(!l){
  870. printf("%x",data);
  871. l = 1;
  872. } used for debuging */
  873. if ((rc = write_short (info, wp, data)) != 0) {
  874. return (rc);
  875. }
  876. wp += 2;
  877. cnt -= 2;
  878. }
  879. if (cnt == 0) {
  880. return (0);
  881. }
  882. /*
  883. * handle unaligned tail bytes
  884. */
  885. data = 0;
  886. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  887. data = (data << 8) | *src++;
  888. --cnt;
  889. }
  890. for (; i < 2; ++i, ++cp) {
  891. data = (data << 8) | (*(uchar *) cp);
  892. }
  893. return (write_short (info, wp, data));
  894. #endif
  895. }
  896. /*-----------------------------------------------------------------------
  897. * Write a word to Flash, returns:
  898. * 0 - OK
  899. * 1 - write timeout
  900. * 2 - Flash not erased
  901. */
  902. #ifndef CFG_FLASH_16BIT
  903. static int write_word (flash_info_t * info, ulong dest, ulong data)
  904. {
  905. vu_long *addr = (vu_long *) (info->start[0]);
  906. ulong start, barf;
  907. int flag;
  908. #if defined (__MIPSEL__)
  909. data = cpu_to_be32 (data);
  910. #endif
  911. /* Check if Flash is (sufficiently) erased */
  912. if ((*((vu_long *) dest) & data) != data) {
  913. return (2);
  914. }
  915. /* Disable interrupts which might cause a timeout here */
  916. flag = disable_interrupts ();
  917. if (info->flash_id < FLASH_AMD_COMP) {
  918. /* AMD stuff */
  919. addr[0x0555] = 0x00AA00AA;
  920. addr[0x02AA] = 0x00550055;
  921. addr[0x0555] = 0x00A000A0;
  922. } else {
  923. /* intel stuff */
  924. *addr = 0x00400040;
  925. }
  926. *((vu_long *) dest) = data;
  927. /* re-enable interrupts if necessary */
  928. if (flag)
  929. enable_interrupts ();
  930. /* data polling for D7 */
  931. start = get_timer (0);
  932. if (info->flash_id < FLASH_AMD_COMP) {
  933. while ((*((vu_long *) dest) & 0x00800080) !=
  934. (data & 0x00800080)) {
  935. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  936. printf ("timeout\n");
  937. return (1);
  938. }
  939. }
  940. } else {
  941. while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
  942. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  943. printf ("timeout\n");
  944. return (1);
  945. }
  946. }
  947. if (addr[0] & 0x003A003A) { /* check for error */
  948. barf = addr[0] & 0x003A0000;
  949. if (barf) {
  950. barf >>= 16;
  951. } else {
  952. barf = addr[0] & 0x0000003A;
  953. }
  954. printf ("\nFlash write error at address %lx\n",
  955. (unsigned long) dest);
  956. if (barf & 0x0002)
  957. printf ("Block locked, not erased.\n");
  958. if (barf & 0x0010)
  959. printf ("Programming error.\n");
  960. if (barf & 0x0008)
  961. printf ("Vpp Low error.\n");
  962. return (2);
  963. }
  964. }
  965. return (0);
  966. }
  967. #else
  968. static int write_short (flash_info_t * info, ulong dest, ushort data)
  969. {
  970. vu_short *addr = (vu_short *) (info->start[0]);
  971. ulong start, barf;
  972. int flag;
  973. #if defined (__MIPSEL__)
  974. data = cpu_to_be16 (data);
  975. #endif
  976. /* Check if Flash is (sufficiently) erased */
  977. if ((*((vu_short *) dest) & data) != data) {
  978. return (2);
  979. }
  980. /* Disable interrupts which might cause a timeout here */
  981. flag = disable_interrupts ();
  982. if (info->flash_id < FLASH_AMD_COMP) {
  983. /* AMD stuff */
  984. addr[0x0555] = 0x00AA;
  985. addr[0x02AA] = 0x0055;
  986. addr[0x0555] = 0x00A0;
  987. } else {
  988. /* intel stuff */
  989. *addr = 0x00D0;
  990. *addr = 0x0040;
  991. }
  992. *((vu_short *) dest) = data;
  993. /* re-enable interrupts if necessary */
  994. if (flag)
  995. enable_interrupts ();
  996. /* data polling for D7 */
  997. start = get_timer (0);
  998. if (info->flash_id < FLASH_AMD_COMP) {
  999. /* AMD stuff */
  1000. while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
  1001. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  1002. return (1);
  1003. }
  1004. }
  1005. } else {
  1006. /* intel stuff */
  1007. while (!(addr[0] & 0x0080)) { /* wait for error or finish */
  1008. if (get_timer (start) > CFG_FLASH_WRITE_TOUT)
  1009. return (1);
  1010. }
  1011. if (addr[0] & 0x003A) { /* check for error */
  1012. barf = addr[0] & 0x003A;
  1013. printf ("\nFlash write error at address %lx\n",
  1014. (unsigned long) dest);
  1015. if (barf & 0x0002)
  1016. printf ("Block locked, not erased.\n");
  1017. if (barf & 0x0010)
  1018. printf ("Programming error.\n");
  1019. if (barf & 0x0008)
  1020. printf ("Vpp Low error.\n");
  1021. return (2);
  1022. }
  1023. *addr = 0x00B0;
  1024. *addr = 0x0070;
  1025. while (!(addr[0] & 0x0080)) { /* wait for error or finish */
  1026. if (get_timer (start) > CFG_FLASH_WRITE_TOUT)
  1027. return (1);
  1028. }
  1029. *addr = 0x00FF;
  1030. }
  1031. return (0);
  1032. }
  1033. #endif