flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * (C) Copyright 2002
  3. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  4. *
  5. * (C) Copyright 2000-2004
  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 <linux/byteorder/swab.h>
  28. #if defined CFG_JFFS_CUSTOM_PART
  29. #include <jffs2/jffs2.h>
  30. #endif
  31. #define SWAP(x) __swab32(x)
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  33. /* Functions */
  34. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  35. static int write_word (flash_info_t *info, ulong dest, ulong data);
  36. static void flash_get_offsets (ulong base, flash_info_t *info);
  37. #if defined CFG_JFFS_CUSTOM_PART
  38. /*
  39. * jffs2_part_info - get information about a JFFS2 partition
  40. *
  41. * @part_num: number of the partition you want to get info about
  42. * @return: struct part_info* in case of success, 0 if failure
  43. */
  44. static struct part_info part;
  45. static int current_part = -1;
  46. struct part_info* jffs2_part_info(int part_num) {
  47. void *jffs2_priv_saved = part.jffs2_priv;
  48. printf("jffs2_part_info: part_num=%i\n",part_num);
  49. if (current_part == part_num)
  50. return &part;
  51. /* u-boot partition */
  52. if(part_num==0){
  53. memset(&part, 0, sizeof(part));
  54. part.offset=(char*)0x00000000;
  55. part.size=256*1024;
  56. /* Mark the struct as ready */
  57. current_part = part_num;
  58. printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
  59. printf("part.size = 0x%08x\n",(unsigned int)part.size);
  60. }
  61. /* primary OS+firmware partition */
  62. if(part_num==1){
  63. memset(&part, 0, sizeof(part));
  64. part.offset=(char*)0x00040000;
  65. part.size=1024*1024;
  66. /* Mark the struct as ready */
  67. current_part = part_num;
  68. printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
  69. printf("part.size = 0x%08x\n",(unsigned int)part.size);
  70. }
  71. /* secondary OS+firmware partition */
  72. if(part_num==2){
  73. memset(&part, 0, sizeof(part));
  74. part.offset=(char*)0x00140000;
  75. part.size=8*1024*1024;
  76. /* Mark the struct as ready */
  77. current_part = part_num;
  78. printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
  79. printf("part.size = 0x%08x\n",(unsigned int)part.size);
  80. }
  81. if (current_part == part_num) {
  82. part.usr_priv = &current_part;
  83. part.jffs2_priv = jffs2_priv_saved;
  84. return &part;
  85. }
  86. printf("jffs2_part_info: end of partition table\n");
  87. return 0;
  88. }
  89. #endif
  90. /*-----------------------------------------------------------------------
  91. */
  92. unsigned long flash_init (void)
  93. {
  94. int i;
  95. ulong size = 0;
  96. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  97. switch (i) {
  98. case 0:
  99. flash_get_size ((long *) PHYS_FLASH_1, &flash_info[i]);
  100. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  101. break;
  102. case 1:
  103. flash_get_size ((long *) PHYS_FLASH_2, &flash_info[i]);
  104. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  105. break;
  106. default:
  107. panic ("configured too many flash banks!\n");
  108. break;
  109. }
  110. size += flash_info[i].size;
  111. }
  112. /* Protect monitor and environment sectors */
  113. flash_protect ( FLAG_PROTECT_SET,CFG_FLASH_BASE,CFG_FLASH_BASE + monitor_flash_len - 1,&flash_info[0] );
  114. flash_protect ( FLAG_PROTECT_SET,CFG_ENV_ADDR,CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
  115. return size;
  116. }
  117. /*-----------------------------------------------------------------------
  118. */
  119. static void flash_get_offsets (ulong base, flash_info_t *info)
  120. {
  121. int i;
  122. if (info->flash_id == FLASH_UNKNOWN) return;
  123. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  124. for (i = 0; i < info->sector_count; i++) {
  125. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  126. info->protect[i] = 0;
  127. }
  128. }
  129. }
  130. /*-----------------------------------------------------------------------
  131. */
  132. void flash_print_info (flash_info_t *info)
  133. {
  134. int i;
  135. if (info->flash_id == FLASH_UNKNOWN) {
  136. printf ("missing or unknown FLASH type\n");
  137. return;
  138. }
  139. switch (info->flash_id & FLASH_VENDMASK) {
  140. case FLASH_MAN_AMD: printf ("AMD "); break;
  141. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  142. default: printf ("Unknown Vendor "); break;
  143. }
  144. switch (info->flash_id & FLASH_TYPEMASK) {
  145. case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
  146. break;
  147. case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
  148. break;
  149. case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
  150. break;
  151. case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
  152. break;
  153. default: printf ("Unknown Chip Type\n");
  154. break;
  155. }
  156. printf (" Size: %ld MB in %d Sectors\n",
  157. info->size >> 20, info->sector_count);
  158. printf (" Sector Start Addresses:");
  159. for (i=0; i<info->sector_count; ++i) {
  160. if ((i % 5) == 0)
  161. printf ("\n ");
  162. printf (" %08lX%s",
  163. info->start[i],
  164. info->protect[i] ? " (RO)" : " "
  165. );
  166. }
  167. printf ("\n");
  168. return;
  169. }
  170. /*
  171. * The following code cannot be run from FLASH!
  172. */
  173. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  174. {
  175. short i;
  176. ulong value;
  177. ulong base = (ulong)addr;
  178. /* Write auto select command: read Manufacturer ID */
  179. addr[0x0555] = 0x00AA00AA;
  180. addr[0x02AA] = 0x00550055;
  181. addr[0x0555] = 0x00900090;
  182. value = addr[0];
  183. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  184. switch (value) {
  185. case AMD_MANUFACT:
  186. debug ("Manufacturer: AMD\n");
  187. info->flash_id = FLASH_MAN_AMD;
  188. break;
  189. case FUJ_MANUFACT:
  190. debug ("Manufacturer: FUJITSU\n");
  191. info->flash_id = FLASH_MAN_FUJ;
  192. break;
  193. default:
  194. debug ("Manufacturer: *** unknown ***\n");
  195. info->flash_id = FLASH_UNKNOWN;
  196. info->sector_count = 0;
  197. info->size = 0;
  198. return (0); /* no or unknown flash */
  199. }
  200. value = addr[1]; /* device ID */
  201. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  202. switch (value) {
  203. case AMD_ID_MIRROR:
  204. debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
  205. addr[14], addr[15]);
  206. switch(addr[14]) {
  207. case AMD_ID_LV128U_2:
  208. if (addr[15] != AMD_ID_LV128U_3) {
  209. debug ("Chip: AMLV128U -> unknown\n");
  210. info->flash_id = FLASH_UNKNOWN;
  211. } else {
  212. debug ("Chip: AMLV128U\n");
  213. info->flash_id += FLASH_AMLV128U;
  214. info->sector_count = 256;
  215. info->size = 0x02000000;
  216. }
  217. break; /* => 32 MB */
  218. case AMD_ID_LV640U_2:
  219. if (addr[15] != AMD_ID_LV640U_3) {
  220. debug ("Chip: AMLV640U -> unknown\n");
  221. info->flash_id = FLASH_UNKNOWN;
  222. } else {
  223. debug ("Chip: AMLV640U\n");
  224. info->flash_id += FLASH_AMLV640U;
  225. info->sector_count = 128;
  226. info->size = 0x01000000;
  227. }
  228. break; /* => 16 MB */
  229. case AMD_ID_LV320B_2:
  230. if (addr[15] != AMD_ID_LV320B_3) {
  231. debug ("Chip: AMLV320B -> unknown\n");
  232. info->flash_id = FLASH_UNKNOWN;
  233. } else {
  234. debug ("Chip: AMLV320B\n");
  235. info->flash_id += FLASH_AMLV320B;
  236. info->sector_count = 71;
  237. info->size = 0x00800000;
  238. }
  239. break; /* => 8 MB */
  240. default:
  241. debug ("Chip: *** unknown ***\n");
  242. info->flash_id = FLASH_UNKNOWN;
  243. break;
  244. }
  245. break;
  246. default:
  247. info->flash_id = FLASH_UNKNOWN;
  248. return (0); /* => no or unknown flash */
  249. }
  250. /* set up sector start address table */
  251. switch (value) {
  252. case AMD_ID_MIRROR:
  253. switch (info->flash_id & FLASH_TYPEMASK) {
  254. /* only known types here - no default */
  255. case FLASH_AMLV128U:
  256. case FLASH_AMLV640U:
  257. case FLASH_AMLV320U:
  258. for (i = 0; i < info->sector_count; i++) {
  259. info->start[i] = base;
  260. base += 0x20000;
  261. }
  262. break;
  263. case FLASH_AMLV320B:
  264. for (i = 0; i < info->sector_count; i++) {
  265. info->start[i] = base;
  266. /*
  267. * The first 8 sectors are 8 kB,
  268. * all the other ones are 64 kB
  269. */
  270. base += (i < 8)
  271. ? 2 * ( 8 << 10)
  272. : 2 * (64 << 10);
  273. }
  274. break;
  275. }
  276. break;
  277. default:
  278. return (0);
  279. break;
  280. }
  281. #if 0
  282. /* check for protected sectors */
  283. for (i = 0; i < info->sector_count; i++) {
  284. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  285. /* D0 = 1 if protected */
  286. addr = (volatile unsigned long *)(info->start[i]);
  287. info->protect[i] = addr[2] & 1;
  288. }
  289. #endif
  290. /*
  291. * Prevent writes to uninitialized FLASH.
  292. */
  293. if (info->flash_id != FLASH_UNKNOWN) {
  294. addr = (volatile unsigned long *)info->start[0];
  295. *addr = 0x00F000F0; /* reset bank */
  296. }
  297. return (info->size);
  298. }
  299. /*-----------------------------------------------------------------------
  300. */
  301. int flash_erase (flash_info_t *info, int s_first, int s_last)
  302. {
  303. vu_long *addr = (vu_long*)(info->start[0]);
  304. int flag, prot, sect, l_sect;
  305. ulong start, now, last;
  306. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  307. if ((s_first < 0) || (s_first > s_last)) {
  308. if (info->flash_id == FLASH_UNKNOWN) {
  309. printf ("- missing\n");
  310. } else {
  311. printf ("- no sectors to erase\n");
  312. }
  313. return 1;
  314. }
  315. if ((info->flash_id == FLASH_UNKNOWN) ||
  316. (info->flash_id > FLASH_AMD_COMP)) {
  317. printf ("Can't erase unknown flash type %08lx - aborted\n",
  318. info->flash_id);
  319. return 1;
  320. }
  321. prot = 0;
  322. for (sect=s_first; sect<=s_last; ++sect) {
  323. if (info->protect[sect]) {
  324. prot++;
  325. }
  326. }
  327. if (prot) {
  328. printf ("- Warning: %d protected sectors will not be erased!\n",
  329. prot);
  330. } else {
  331. printf ("\n");
  332. }
  333. l_sect = -1;
  334. /* Disable interrupts which might cause a timeout here */
  335. flag = disable_interrupts();
  336. addr[0x0555] = 0x00AA00AA;
  337. addr[0x02AA] = 0x00550055;
  338. addr[0x0555] = 0x00800080;
  339. addr[0x0555] = 0x00AA00AA;
  340. addr[0x02AA] = 0x00550055;
  341. /* Start erase on unprotected sectors */
  342. for (sect = s_first; sect<=s_last; sect++) {
  343. if (info->protect[sect] == 0) { /* not protected */
  344. addr = (vu_long*)(info->start[sect]);
  345. addr[0] = 0x00300030;
  346. l_sect = sect;
  347. }
  348. }
  349. /* re-enable interrupts if necessary */
  350. if (flag)
  351. enable_interrupts();
  352. /* wait at least 80us - let's wait 1 ms */
  353. udelay (1000);
  354. /*
  355. * We wait for the last triggered sector
  356. */
  357. if (l_sect < 0)
  358. goto DONE;
  359. start = get_timer (0);
  360. last = start;
  361. addr = (vu_long*)(info->start[l_sect]);
  362. while ((addr[0] & 0x00800080) != 0x00800080) {
  363. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  364. printf ("Timeout\n");
  365. return 1;
  366. }
  367. /* show that we're waiting */
  368. if ((now - last) > 100000) { /* every second */
  369. putc ('.');
  370. last = now;
  371. }
  372. }
  373. DONE:
  374. /* reset to read mode */
  375. addr = (volatile unsigned long *)info->start[0];
  376. addr[0] = 0x00F000F0; /* reset bank */
  377. printf (" done\n");
  378. return 0;
  379. }
  380. /*-----------------------------------------------------------------------
  381. * Copy memory to flash, returns:
  382. * 0 - OK
  383. * 1 - write timeout
  384. * 2 - Flash not erased
  385. */
  386. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  387. {
  388. ulong cp, wp, data;
  389. int i, l, rc;
  390. wp = (addr & ~3); /* get lower word aligned address */
  391. /*
  392. * handle unaligned start bytes
  393. */
  394. if ((l = addr - wp) != 0) {
  395. data = 0;
  396. for (i=0, cp=wp; i<l; ++i, ++cp) {
  397. data = (data << 8) | (*(uchar *)cp);
  398. }
  399. for (; i<4 && cnt>0; ++i) {
  400. data = (data << 8) | *src++;
  401. --cnt;
  402. ++cp;
  403. }
  404. for (; cnt==0 && i<4; ++i, ++cp) {
  405. data = (data << 8) | (*(uchar *)cp);
  406. }
  407. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  408. return (rc);
  409. }
  410. wp += 4;
  411. }
  412. /*
  413. * handle word aligned part
  414. */
  415. while (cnt >= 4) {
  416. data = 0;
  417. for (i=0; i<4; ++i) {
  418. data = (data << 8) | *src++;
  419. }
  420. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  421. return (rc);
  422. }
  423. wp += 4;
  424. cnt -= 4;
  425. }
  426. if (cnt == 0) {
  427. return (0);
  428. }
  429. /*
  430. * handle unaligned tail bytes
  431. */
  432. data = 0;
  433. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  434. data = (data << 8) | *src++;
  435. --cnt;
  436. }
  437. for (; i<4; ++i, ++cp) {
  438. data = (data << 8) | (*(uchar *)cp);
  439. }
  440. return (write_word(info, wp, SWAP(data)));
  441. }
  442. /*-----------------------------------------------------------------------
  443. * Write a word to Flash, returns:
  444. * 0 - OK
  445. * 1 - write timeout
  446. * 2 - Flash not erased
  447. */
  448. static int write_word (flash_info_t *info, ulong dest, ulong data)
  449. {
  450. vu_long *addr = (vu_long*)(info->start[0]);
  451. ulong start;
  452. int flag;
  453. /* Check if Flash is (sufficiently) erased */
  454. if ((*((vu_long *)dest) & data) != data) {
  455. return (2);
  456. }
  457. /* Disable interrupts which might cause a timeout here */
  458. flag = disable_interrupts();
  459. addr[0x0555] = 0x00AA00AA;
  460. addr[0x02AA] = 0x00550055;
  461. addr[0x0555] = 0x00A000A0;
  462. *((vu_long *)dest) = data;
  463. /* re-enable interrupts if necessary */
  464. if (flag)
  465. enable_interrupts();
  466. /* data polling for D7 */
  467. start = get_timer (0);
  468. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  469. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  470. return (1);
  471. }
  472. }
  473. return (0);
  474. }