flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. * Adapted for Interphase 4539 by Wolfgang Grandegger <wg@denx.de>.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <config.h>
  29. #include <common.h>
  30. #include <flash.h>
  31. #include <asm/io.h>
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  33. extern int hwc_flash_size(void);
  34. static ulong flash_get_size (u32 addr, flash_info_t *info);
  35. static int flash_get_offsets (u32 base, flash_info_t *info);
  36. static int write_word (flash_info_t *info, ulong dest, ulong data);
  37. static void flash_reset (u32 addr);
  38. #define out8(a,v) *(volatile unsigned char*)(a) = v
  39. #define in8(a) *(volatile unsigned char*)(a)
  40. #define in32(a) *(volatile unsigned long*)(a)
  41. #define iobarrier_rw() eieio()
  42. unsigned long flash_init (void)
  43. {
  44. unsigned int i;
  45. unsigned long flash_size = 0;
  46. unsigned long bank_size;
  47. unsigned int bank = 0;
  48. /* Init: no FLASHes known */
  49. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
  50. flash_info[i].flash_id = FLASH_UNKNOWN;
  51. flash_info[i].sector_count = 0;
  52. flash_info[i].size = 0;
  53. }
  54. /* Initialise the BOOT Flash */
  55. if (bank == CFG_MAX_FLASH_BANKS) {
  56. puts ("Warning: not all Flashes are initialised !");
  57. return flash_size;
  58. }
  59. bank_size = flash_get_size (CFG_FLASH_BASE, flash_info + bank);
  60. if (bank_size) {
  61. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE && \
  62. CFG_MONITOR_BASE < CFG_FLASH_BASE + CFG_MAX_FLASH_SIZE
  63. /* monitor protection ON by default */
  64. flash_protect(FLAG_PROTECT_SET,
  65. CFG_MONITOR_BASE,
  66. CFG_MONITOR_BASE + monitor_flash_len - 1,
  67. flash_info + bank);
  68. #endif
  69. #ifdef CONFIG_ENV_IS_IN_FLASH
  70. /* ENV protection ON by default */
  71. flash_protect(FLAG_PROTECT_SET,
  72. CFG_ENV_ADDR,
  73. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  74. flash_info + bank);
  75. #endif
  76. /* HWC protection ON by default */
  77. flash_protect(FLAG_PROTECT_SET,
  78. CFG_FLASH_BASE,
  79. CFG_FLASH_BASE + 0x10000 - 1,
  80. flash_info + bank);
  81. flash_size += bank_size;
  82. bank++;
  83. } else {
  84. puts ("Warning: the BOOT Flash is not initialised !");
  85. }
  86. return flash_size;
  87. }
  88. /*
  89. * The following code cannot be run from FLASH!
  90. */
  91. static ulong flash_get_size (u32 addr, flash_info_t *info)
  92. {
  93. volatile uchar value;
  94. #if 0
  95. int i;
  96. #endif
  97. /* Write auto select command: read Manufacturer ID */
  98. out8(addr + 0x0555, 0xAA);
  99. iobarrier_rw();
  100. udelay(10);
  101. out8(addr + 0x02AA, 0x55);
  102. iobarrier_rw();
  103. udelay(10);
  104. out8(addr + 0x0555, 0x90);
  105. iobarrier_rw();
  106. udelay(10);
  107. value = in8(addr);
  108. iobarrier_rw();
  109. udelay(10);
  110. switch (value | (value << 16)) {
  111. case AMD_MANUFACT:
  112. info->flash_id = FLASH_MAN_AMD;
  113. break;
  114. case FUJ_MANUFACT:
  115. info->flash_id = FLASH_MAN_FUJ;
  116. break;
  117. default:
  118. info->flash_id = FLASH_UNKNOWN;
  119. flash_reset (addr);
  120. return 0;
  121. }
  122. value = in8(addr + 1); /* device ID */
  123. iobarrier_rw();
  124. switch (value) {
  125. case AMD_ID_LV033C:
  126. info->flash_id += FLASH_AM033C;
  127. info->size = hwc_flash_size();
  128. if (info->size > CFG_MAX_FLASH_SIZE) {
  129. printf("U-Boot supports only %d MB\n",
  130. CFG_MAX_FLASH_SIZE);
  131. info->size = CFG_MAX_FLASH_SIZE;
  132. }
  133. info->sector_count = info->size / 0x10000;
  134. break; /* => 4 MB */
  135. default:
  136. info->flash_id = FLASH_UNKNOWN;
  137. flash_reset (addr);
  138. return (0); /* => no or unknown flash */
  139. }
  140. if (!flash_get_offsets (addr, info)) {
  141. flash_reset (addr);
  142. return 0;
  143. }
  144. #if 0
  145. /* check for protected sectors */
  146. for (i = 0; i < info->sector_count; i++) {
  147. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  148. /* D0 = 1 if protected */
  149. value = in8(info->start[i] + 2);
  150. iobarrier_rw();
  151. info->protect[i] = (value & 1) != 0;
  152. }
  153. #endif
  154. /*
  155. * Reset bank to read mode
  156. */
  157. flash_reset (addr);
  158. return (info->size);
  159. }
  160. static int flash_get_offsets (u32 base, flash_info_t *info)
  161. {
  162. unsigned int i, size;
  163. switch (info->flash_id & FLASH_TYPEMASK) {
  164. case FLASH_AM033C:
  165. /* set sector offsets for uniform sector type */
  166. size = info->size / info->sector_count;
  167. for (i = 0; i < info->sector_count; i++) {
  168. info->start[i] = base + i * size;
  169. }
  170. break;
  171. default:
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. int flash_erase (flash_info_t *info, int s_first, int s_last)
  177. {
  178. volatile u32 addr = info->start[0];
  179. int flag, prot, sect, l_sect;
  180. ulong start, now, last;
  181. if (s_first < 0 || s_first > s_last) {
  182. if (info->flash_id == FLASH_UNKNOWN) {
  183. printf ("- missing\n");
  184. } else {
  185. printf ("- no sectors to erase\n");
  186. }
  187. return 1;
  188. }
  189. if (info->flash_id == FLASH_UNKNOWN ||
  190. info->flash_id > FLASH_AMD_COMP) {
  191. printf ("Can't erase unknown flash type %08lx - aborted\n",
  192. info->flash_id);
  193. return 1;
  194. }
  195. prot = 0;
  196. for (sect=s_first; sect<=s_last; ++sect) {
  197. if (info->protect[sect]) {
  198. prot++;
  199. }
  200. }
  201. if (prot) {
  202. printf ("- Warning: %d protected sectors will not be erased!\n",
  203. prot);
  204. } else {
  205. printf ("\n");
  206. }
  207. l_sect = -1;
  208. /* Disable interrupts which might cause a timeout here */
  209. flag = disable_interrupts();
  210. out8(addr + 0x555, 0xAA);
  211. iobarrier_rw();
  212. out8(addr + 0x2AA, 0x55);
  213. iobarrier_rw();
  214. out8(addr + 0x555, 0x80);
  215. iobarrier_rw();
  216. out8(addr + 0x555, 0xAA);
  217. iobarrier_rw();
  218. out8(addr + 0x2AA, 0x55);
  219. iobarrier_rw();
  220. /* Start erase on unprotected sectors */
  221. for (sect = s_first; sect<=s_last; sect++) {
  222. if (info->protect[sect] == 0) { /* not protected */
  223. addr = info->start[sect];
  224. out8(addr, 0x30);
  225. iobarrier_rw();
  226. l_sect = sect;
  227. }
  228. }
  229. /* re-enable interrupts if necessary */
  230. if (flag)
  231. enable_interrupts();
  232. /* wait at least 80us - let's wait 1 ms */
  233. udelay (1000);
  234. /*
  235. * We wait for the last triggered sector
  236. */
  237. if (l_sect < 0)
  238. goto DONE;
  239. start = get_timer (0);
  240. last = start;
  241. addr = info->start[l_sect];
  242. while ((in8(addr) & 0x80) != 0x80) {
  243. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  244. printf ("Timeout\n");
  245. return 1;
  246. }
  247. /* show that we're waiting */
  248. if ((now - last) > 1000) { /* every second */
  249. putc ('.');
  250. last = now;
  251. }
  252. iobarrier_rw();
  253. }
  254. DONE:
  255. /* reset to read mode */
  256. flash_reset (info->start[0]);
  257. printf (" done\n");
  258. return 0;
  259. }
  260. /*
  261. * Copy memory to flash, returns:
  262. * 0 - OK
  263. * 1 - write timeout
  264. * 2 - Flash not erased
  265. */
  266. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  267. {
  268. ulong cp, wp, data;
  269. int i, l, rc;
  270. wp = (addr & ~3); /* get lower word aligned address */
  271. /*
  272. * handle unaligned start bytes
  273. */
  274. if ((l = addr - wp) != 0) {
  275. data = 0;
  276. for (i=0, cp=wp; i<l; ++i, ++cp) {
  277. data = (data << 8) | (*(uchar *)cp);
  278. }
  279. for (; i<4 && cnt>0; ++i) {
  280. data = (data << 8) | *src++;
  281. --cnt;
  282. ++cp;
  283. }
  284. for (; cnt==0 && i<4; ++i, ++cp) {
  285. data = (data << 8) | (*(uchar *)cp);
  286. }
  287. if ((rc = write_word(info, wp, data)) != 0) {
  288. return (rc);
  289. }
  290. wp += 4;
  291. }
  292. /*
  293. * handle word aligned part
  294. */
  295. while (cnt >= 4) {
  296. data = 0;
  297. for (i=0; i<4; ++i) {
  298. data = (data << 8) | *src++;
  299. }
  300. if ((rc = write_word(info, wp, data)) != 0) {
  301. return (rc);
  302. }
  303. wp += 4;
  304. cnt -= 4;
  305. }
  306. if (cnt == 0) {
  307. return (0);
  308. }
  309. /*
  310. * handle unaligned tail bytes
  311. */
  312. data = 0;
  313. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  314. data = (data << 8) | *src++;
  315. --cnt;
  316. }
  317. for (; i<4; ++i, ++cp) {
  318. data = (data << 8) | (*(uchar *)cp);
  319. }
  320. return (write_word(info, wp, data));
  321. }
  322. /*
  323. * Write a word to Flash, returns:
  324. * 0 - OK
  325. * 1 - write timeout
  326. * 2 - Flash not erased
  327. */
  328. static int write_word (flash_info_t *info, ulong dest, ulong data)
  329. {
  330. volatile u32 addr = info->start[0];
  331. ulong start;
  332. int flag, i;
  333. /* Check if Flash is (sufficiently) erased */
  334. if ((in32(dest) & data) != data) {
  335. return (2);
  336. }
  337. /* Disable interrupts which might cause a timeout here */
  338. flag = disable_interrupts();
  339. /* first, perform an unlock bypass command to speed up flash writes */
  340. out8(addr + 0x555, 0xAA);
  341. iobarrier_rw();
  342. out8(addr + 0x2AA, 0x55);
  343. iobarrier_rw();
  344. out8(addr + 0x555, 0x20);
  345. iobarrier_rw();
  346. /* write each byte out */
  347. for (i = 0; i < 4; i++) {
  348. char *data_ch = (char *)&data;
  349. out8(addr, 0xA0);
  350. iobarrier_rw();
  351. out8(dest+i, data_ch[i]);
  352. iobarrier_rw();
  353. udelay(10); /* XXX */
  354. }
  355. /* we're done, now do an unlock bypass reset */
  356. out8(addr, 0x90);
  357. iobarrier_rw();
  358. out8(addr, 0x00);
  359. iobarrier_rw();
  360. /* re-enable interrupts if necessary */
  361. if (flag)
  362. enable_interrupts();
  363. /* data polling for D7 */
  364. start = get_timer (0);
  365. while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
  366. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  367. return (1);
  368. }
  369. iobarrier_rw();
  370. }
  371. flash_reset (addr);
  372. return (0);
  373. }
  374. /*
  375. * Reset bank to read mode
  376. */
  377. static void flash_reset (u32 addr)
  378. {
  379. out8(addr, 0xF0); /* reset bank */
  380. iobarrier_rw();
  381. }
  382. void flash_print_info (flash_info_t *info)
  383. {
  384. int i;
  385. if (info->flash_id == FLASH_UNKNOWN) {
  386. printf ("missing or unknown FLASH type\n");
  387. return;
  388. }
  389. switch (info->flash_id & FLASH_VENDMASK) {
  390. case FLASH_MAN_AMD: printf ("AMD "); break;
  391. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  392. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  393. default: printf ("Unknown Vendor "); break;
  394. }
  395. switch (info->flash_id & FLASH_TYPEMASK) {
  396. case FLASH_AM033C: printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
  397. break;
  398. default: printf ("Unknown Chip Type\n");
  399. break;
  400. }
  401. if (info->size % 0x100000 == 0) {
  402. printf (" Size: %ld MB in %d Sectors\n",
  403. info->size / 0x100000, info->sector_count);
  404. }
  405. else if (info->size % 0x400 == 0) {
  406. printf (" Size: %ld KB in %d Sectors\n",
  407. info->size / 0x400, info->sector_count);
  408. }
  409. else {
  410. printf (" Size: %ld B in %d Sectors\n",
  411. info->size, info->sector_count);
  412. }
  413. printf (" Sector Start Addresses:");
  414. for (i=0; i<info->sector_count; ++i) {
  415. if ((i % 5) == 0)
  416. printf ("\n ");
  417. printf (" %08lX%s",
  418. info->start[i],
  419. info->protect[i] ? " (RO)" : " "
  420. );
  421. }
  422. printf ("\n");
  423. }