PageRenderTime 146ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/board/pcippc2/flash.c

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