flash.c 29 KB

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