flash.c 28 KB

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