flash_hw.c 13 KB

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