flash.c 27 KB

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