flash_new.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * (C) Copyright 2001
  3. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  4. *
  5. * (C) Copyright 2002
  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 <flash.h>
  28. #include <asm/io.h>
  29. #include "memio.h"
  30. /*---------------------------------------------------------------------*/
  31. #undef DEBUG_FLASH
  32. #ifdef DEBUG_FLASH
  33. #define DEBUGF(fmt,args...) printf(fmt ,##args)
  34. #else
  35. #define DEBUGF(fmt,args...)
  36. #endif
  37. /*---------------------------------------------------------------------*/
  38. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  39. static ulong flash_get_size (ulong addr, flash_info_t *info);
  40. static int flash_get_offsets (ulong base, flash_info_t *info);
  41. static int write_word (flash_info_t *info, ulong dest, ulong data);
  42. static void flash_reset (ulong addr);
  43. int flash_xd_nest;
  44. static void flash_to_xd(void)
  45. {
  46. unsigned char x;
  47. flash_xd_nest ++;
  48. if (flash_xd_nest == 1)
  49. {
  50. DEBUGF("Flash on XD\n");
  51. x = pci_read_cfg_byte(0, 0, 0x74);
  52. pci_write_cfg_byte(0, 0, 0x74, x|1);
  53. }
  54. }
  55. static void flash_to_mem(void)
  56. {
  57. unsigned char x;
  58. flash_xd_nest --;
  59. if (flash_xd_nest == 0)
  60. {
  61. DEBUGF("Flash on memory bus\n");
  62. x = pci_read_cfg_byte(0, 0, 0x74);
  63. pci_write_cfg_byte(0, 0, 0x74, x&0xFE);
  64. }
  65. }
  66. unsigned long flash_init_old(void)
  67. {
  68. int i;
  69. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++)
  70. {
  71. flash_info[i].flash_id = FLASH_UNKNOWN;
  72. flash_info[i].sector_count = 0;
  73. flash_info[i].size = 0;
  74. }
  75. return 1;
  76. }
  77. unsigned long flash_init (void)
  78. {
  79. unsigned int i;
  80. unsigned long flash_size = 0;
  81. flash_xd_nest = 0;
  82. flash_to_xd();
  83. /* Init: no FLASHes known */
  84. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  85. flash_info[i].flash_id = FLASH_UNKNOWN;
  86. flash_info[i].sector_count = 0;
  87. flash_info[i].size = 0;
  88. }
  89. DEBUGF("\n## Get flash size @ 0x%08x\n", CONFIG_SYS_FLASH_BASE);
  90. flash_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info);
  91. DEBUGF("## Flash bank size: %08lx\n", flash_size);
  92. if (flash_size) {
  93. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
  94. CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_FLASH_MAX_SIZE
  95. /* monitor protection ON by default */
  96. flash_protect(FLAG_PROTECT_SET,
  97. CONFIG_SYS_MONITOR_BASE,
  98. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  99. &flash_info[0]);
  100. #endif
  101. #ifdef CONFIG_ENV_IS_IN_FLASH
  102. /* ENV protection ON by default */
  103. flash_protect(FLAG_PROTECT_SET,
  104. CONFIG_ENV_ADDR,
  105. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  106. &flash_info[0]);
  107. #endif
  108. } else {
  109. puts ("Warning: the BOOT Flash is not initialised !");
  110. }
  111. flash_to_mem();
  112. return flash_size;
  113. }
  114. /*
  115. * The following code cannot be run from FLASH!
  116. */
  117. static ulong flash_get_size (ulong addr, flash_info_t *info)
  118. {
  119. short i;
  120. uchar value;
  121. uchar *x = (uchar *)addr;
  122. flash_to_xd();
  123. /* Write auto select command: read Manufacturer ID */
  124. x[0x0555] = 0xAA;
  125. __asm volatile ("sync\n eieio");
  126. x[0x02AA] = 0x55;
  127. __asm volatile ("sync\n eieio");
  128. x[0x0555] = 0x90;
  129. __asm volatile ("sync\n eieio");
  130. value = x[0];
  131. __asm volatile ("sync\n eieio");
  132. DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
  133. switch (value | (value << 16)) {
  134. case AMD_MANUFACT:
  135. info->flash_id = FLASH_MAN_AMD;
  136. break;
  137. case FUJ_MANUFACT:
  138. info->flash_id = FLASH_MAN_FUJ;
  139. break;
  140. case STM_MANUFACT:
  141. info->flash_id = FLASH_MAN_STM;
  142. break;
  143. default:
  144. info->flash_id = FLASH_UNKNOWN;
  145. info->sector_count = 0;
  146. info->size = 0;
  147. flash_reset (addr);
  148. return 0;
  149. }
  150. value = x[1];
  151. __asm volatile ("sync\n eieio");
  152. DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", addr+1, value);
  153. switch (value) {
  154. case AMD_ID_F040B:
  155. DEBUGF("Am29F040B\n");
  156. info->flash_id += FLASH_AM040;
  157. info->sector_count = 8;
  158. info->size = 0x00080000;
  159. break; /* => 512 kB */
  160. case AMD_ID_LV040B:
  161. DEBUGF("Am29LV040B\n");
  162. info->flash_id += FLASH_AM040;
  163. info->sector_count = 8;
  164. info->size = 0x00080000;
  165. break; /* => 512 kB */
  166. case AMD_ID_LV400T:
  167. DEBUGF("Am29LV400T\n");
  168. info->flash_id += FLASH_AM400T;
  169. info->sector_count = 11;
  170. info->size = 0x00100000;
  171. break; /* => 1 MB */
  172. case AMD_ID_LV400B:
  173. DEBUGF("Am29LV400B\n");
  174. info->flash_id += FLASH_AM400B;
  175. info->sector_count = 11;
  176. info->size = 0x00100000;
  177. break; /* => 1 MB */
  178. case AMD_ID_LV800T:
  179. DEBUGF("Am29LV800T\n");
  180. info->flash_id += FLASH_AM800T;
  181. info->sector_count = 19;
  182. info->size = 0x00200000;
  183. break; /* => 2 MB */
  184. case AMD_ID_LV800B:
  185. DEBUGF("Am29LV400B\n");
  186. info->flash_id += FLASH_AM800B;
  187. info->sector_count = 19;
  188. info->size = 0x00200000;
  189. break; /* => 2 MB */
  190. case AMD_ID_LV160T:
  191. DEBUGF("Am29LV160T\n");
  192. info->flash_id += FLASH_AM160T;
  193. info->sector_count = 35;
  194. info->size = 0x00400000;
  195. break; /* => 4 MB */
  196. case AMD_ID_LV160B:
  197. DEBUGF("Am29LV160B\n");
  198. info->flash_id += FLASH_AM160B;
  199. info->sector_count = 35;
  200. info->size = 0x00400000;
  201. break; /* => 4 MB */
  202. case AMD_ID_LV320T:
  203. DEBUGF("Am29LV320T\n");
  204. info->flash_id += FLASH_AM320T;
  205. info->sector_count = 67;
  206. info->size = 0x00800000;
  207. break; /* => 8 MB */
  208. #if 0
  209. /* Has the same ID as AMD_ID_LV320T, to be fixed */
  210. case AMD_ID_LV320B:
  211. DEBUGF("Am29LV320B\n");
  212. info->flash_id += FLASH_AM320B;
  213. info->sector_count = 67;
  214. info->size = 0x00800000;
  215. break; /* => 8 MB */
  216. #endif
  217. case AMD_ID_LV033C:
  218. DEBUGF("Am29LV033C\n");
  219. info->flash_id += FLASH_AM033C;
  220. info->sector_count = 64;
  221. info->size = 0x01000000;
  222. break; /* => 16Mb */
  223. case STM_ID_F040B:
  224. DEBUGF("M29F040B\n");
  225. info->flash_id += FLASH_AM040;
  226. info->sector_count = 8;
  227. info->size = 0x00080000;
  228. break; /* => 512 kB */
  229. default:
  230. info->flash_id = FLASH_UNKNOWN;
  231. flash_reset (addr);
  232. flash_to_mem();
  233. return (0); /* => no or unknown flash */
  234. }
  235. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  236. printf ("** ERROR: sector count %d > max (%d) **\n",
  237. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  238. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  239. }
  240. if (! flash_get_offsets (addr, info)) {
  241. flash_reset (addr);
  242. flash_to_mem();
  243. return 0;
  244. }
  245. /* check for protected sectors */
  246. for (i = 0; i < info->sector_count; i++) {
  247. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  248. /* D0 = 1 if protected */
  249. value = in8(info->start[i] + 2);
  250. iobarrier_rw();
  251. info->protect[i] = (value & 1) != 0;
  252. }
  253. /*
  254. * Reset bank to read mode
  255. */
  256. flash_reset (addr);
  257. flash_to_mem();
  258. return (info->size);
  259. }
  260. static int flash_get_offsets (ulong base, flash_info_t *info)
  261. {
  262. unsigned int i;
  263. switch (info->flash_id & FLASH_TYPEMASK) {
  264. case FLASH_AM040:
  265. /* set sector offsets for uniform sector type */
  266. for (i = 0; i < info->sector_count; i++) {
  267. info->start[i] = base + i * info->size /
  268. info->sector_count;
  269. }
  270. break;
  271. default:
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. int flash_erase (flash_info_t *info, int s_first, int s_last)
  277. {
  278. volatile ulong addr = info->start[0];
  279. int flag, prot, sect, l_sect;
  280. ulong start, now, last;
  281. flash_to_xd();
  282. if (s_first < 0 || s_first > s_last) {
  283. if (info->flash_id == FLASH_UNKNOWN) {
  284. printf ("- missing\n");
  285. } else {
  286. printf ("- no sectors to erase\n");
  287. }
  288. flash_to_mem();
  289. return 1;
  290. }
  291. if (info->flash_id == FLASH_UNKNOWN) {
  292. printf ("Can't erase unknown flash type %08lx - aborted\n",
  293. info->flash_id);
  294. flash_to_mem();
  295. return 1;
  296. }
  297. prot = 0;
  298. for (sect=s_first; sect<=s_last; ++sect) {
  299. if (info->protect[sect]) {
  300. prot++;
  301. }
  302. }
  303. if (prot) {
  304. printf ("- Warning: %d protected sectors will not be erased!\n",
  305. prot);
  306. } else {
  307. printf ("\n");
  308. }
  309. l_sect = -1;
  310. /* Disable interrupts which might cause a timeout here */
  311. flag = disable_interrupts();
  312. out8(addr + 0x555, 0xAA);
  313. iobarrier_rw();
  314. out8(addr + 0x2AA, 0x55);
  315. iobarrier_rw();
  316. out8(addr + 0x555, 0x80);
  317. iobarrier_rw();
  318. out8(addr + 0x555, 0xAA);
  319. iobarrier_rw();
  320. out8(addr + 0x2AA, 0x55);
  321. iobarrier_rw();
  322. /* Start erase on unprotected sectors */
  323. for (sect = s_first; sect<=s_last; sect++) {
  324. if (info->protect[sect] == 0) { /* not protected */
  325. addr = info->start[sect];
  326. out8(addr, 0x30);
  327. iobarrier_rw();
  328. l_sect = sect;
  329. }
  330. }
  331. /* re-enable interrupts if necessary */
  332. if (flag)
  333. enable_interrupts();
  334. /* wait at least 80us - let's wait 1 ms */
  335. udelay (1000);
  336. /*
  337. * We wait for the last triggered sector
  338. */
  339. if (l_sect < 0)
  340. goto DONE;
  341. start = get_timer (0);
  342. last = start;
  343. addr = info->start[l_sect];
  344. DEBUGF ("Start erase timeout: %d\n", CONFIG_SYS_FLASH_ERASE_TOUT);
  345. while ((in8(addr) & 0x80) != 0x80) {
  346. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  347. printf ("Timeout\n");
  348. flash_reset (info->start[0]);
  349. flash_to_mem();
  350. return 1;
  351. }
  352. /* show that we're waiting */
  353. if ((now - last) > 1000) { /* every second */
  354. putc ('.');
  355. last = now;
  356. }
  357. iobarrier_rw();
  358. }
  359. DONE:
  360. /* reset to read mode */
  361. flash_reset (info->start[0]);
  362. flash_to_mem();
  363. printf (" done\n");
  364. return 0;
  365. }
  366. /*
  367. * Copy memory to flash, returns:
  368. * 0 - OK
  369. * 1 - write timeout
  370. * 2 - Flash not erased
  371. */
  372. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  373. {
  374. ulong cp, wp, data;
  375. int i, l, rc;
  376. flash_to_xd();
  377. wp = (addr & ~3); /* get lower word aligned address */
  378. /*
  379. * handle unaligned start bytes
  380. */
  381. if ((l = addr - wp) != 0) {
  382. data = 0;
  383. for (i=0, cp=wp; i<l; ++i, ++cp) {
  384. data = (data << 8) | (*(uchar *)cp);
  385. }
  386. for (; i<4 && cnt>0; ++i) {
  387. data = (data << 8) | *src++;
  388. --cnt;
  389. ++cp;
  390. }
  391. for (; cnt==0 && i<4; ++i, ++cp) {
  392. data = (data << 8) | (*(uchar *)cp);
  393. }
  394. if ((rc = write_word(info, wp, data)) != 0) {
  395. flash_to_mem();
  396. return (rc);
  397. }
  398. wp += 4;
  399. }
  400. /*
  401. * handle word aligned part
  402. */
  403. while (cnt >= 4) {
  404. data = 0;
  405. for (i=0; i<4; ++i) {
  406. data = (data << 8) | *src++;
  407. }
  408. if ((rc = write_word(info, wp, data)) != 0) {
  409. flash_to_mem();
  410. return (rc);
  411. }
  412. wp += 4;
  413. cnt -= 4;
  414. }
  415. if (cnt == 0) {
  416. flash_to_mem();
  417. return (0);
  418. }
  419. /*
  420. * handle unaligned tail bytes
  421. */
  422. data = 0;
  423. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  424. data = (data << 8) | *src++;
  425. --cnt;
  426. }
  427. for (; i<4; ++i, ++cp) {
  428. data = (data << 8) | (*(uchar *)cp);
  429. }
  430. flash_to_mem();
  431. return (write_word(info, wp, data));
  432. }
  433. /*
  434. * Write a word to Flash, returns:
  435. * 0 - OK
  436. * 1 - write timeout
  437. * 2 - Flash not erased
  438. */
  439. static int write_word (flash_info_t *info, ulong dest, ulong data)
  440. {
  441. volatile ulong addr = info->start[0];
  442. ulong start;
  443. int i;
  444. flash_to_xd();
  445. /* Check if Flash is (sufficiently) erased */
  446. if ((in32(dest) & data) != data) {
  447. flash_to_mem();
  448. return (2);
  449. }
  450. /* write each byte out */
  451. for (i = 0; i < 4; i++) {
  452. char *data_ch = (char *)&data;
  453. int flag = disable_interrupts();
  454. out8(addr + 0x555, 0xAA);
  455. iobarrier_rw();
  456. out8(addr + 0x2AA, 0x55);
  457. iobarrier_rw();
  458. out8(addr + 0x555, 0xA0);
  459. iobarrier_rw();
  460. out8(dest+i, data_ch[i]);
  461. iobarrier_rw();
  462. /* re-enable interrupts if necessary */
  463. if (flag)
  464. enable_interrupts();
  465. /* data polling for D7 */
  466. start = get_timer (0);
  467. while ((in8(dest+i) & 0x80) != (data_ch[i] & 0x80)) {
  468. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  469. flash_reset (addr);
  470. flash_to_mem();
  471. return (1);
  472. }
  473. iobarrier_rw();
  474. }
  475. }
  476. flash_reset (addr);
  477. flash_to_mem();
  478. return (0);
  479. }
  480. /*
  481. * Reset bank to read mode
  482. */
  483. static void flash_reset (ulong addr)
  484. {
  485. flash_to_xd();
  486. out8(addr, 0xF0); /* reset bank */
  487. iobarrier_rw();
  488. flash_to_mem();
  489. }
  490. void flash_print_info (flash_info_t *info)
  491. {
  492. int i;
  493. if (info->flash_id == FLASH_UNKNOWN) {
  494. printf ("missing or unknown FLASH type\n");
  495. return;
  496. }
  497. switch (info->flash_id & FLASH_VENDMASK) {
  498. case FLASH_MAN_AMD: printf ("AMD "); break;
  499. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  500. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  501. case FLASH_MAN_STM: printf ("SGS THOMSON "); break;
  502. default: printf ("Unknown Vendor "); break;
  503. }
  504. switch (info->flash_id & FLASH_TYPEMASK) {
  505. case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
  506. break;
  507. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  508. break;
  509. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  510. break;
  511. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  512. break;
  513. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  514. break;
  515. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  516. break;
  517. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  518. break;
  519. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  520. break;
  521. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  522. break;
  523. default: printf ("Unknown Chip Type\n");
  524. break;
  525. }
  526. if (info->size % 0x100000 == 0) {
  527. printf (" Size: %ld MB in %d Sectors\n",
  528. info->size / 0x100000, info->sector_count);
  529. } else if (info->size % 0x400 == 0) {
  530. printf (" Size: %ld KB in %d Sectors\n",
  531. info->size / 0x400, info->sector_count);
  532. } else {
  533. printf (" Size: %ld B in %d Sectors\n",
  534. info->size, info->sector_count);
  535. }
  536. printf (" Sector Start Addresses:");
  537. for (i=0; i<info->sector_count; ++i) {
  538. if ((i % 5) == 0)
  539. printf ("\n ");
  540. printf (" %08lX%s",
  541. info->start[i],
  542. info->protect[i] ? " (RO)" : " "
  543. );
  544. }
  545. printf ("\n");
  546. }