flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * 07-10-2002 Frank Gottschling: added 29F032 flash (ELPPC).
  25. * fixed monitor protection part
  26. *
  27. * 09-18-2001 Andreas Heppel: Reduced the code in here to the usage
  28. * of AMD's 29F040 and 29F016 flashes, since the BAB7xx does use
  29. * any other.
  30. */
  31. #include <common.h>
  32. #include <asm/processor.h>
  33. #include <asm/pci_io.h>
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. ulong flash_get_size (vu_long *addr, flash_info_t *info);
  36. static int write_word (flash_info_t *info, ulong dest, ulong data);
  37. /*flash command address offsets*/
  38. #define ADDR0 (0x555)
  39. #define ADDR1 (0x2AA)
  40. #define ADDR3 (0x001)
  41. #define FLASH_WORD_SIZE unsigned char
  42. /*----------------------------------------------------------------------------*/
  43. unsigned long flash_init (void)
  44. {
  45. unsigned long size1, size2;
  46. int i;
  47. /* Init: no FLASHes known */
  48. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
  49. {
  50. flash_info[i].flash_id = FLASH_UNKNOWN;
  51. }
  52. /* initialise 1st flash */
  53. size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  54. if (flash_info[0].flash_id == FLASH_UNKNOWN)
  55. {
  56. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  57. size1, size1<<20);
  58. }
  59. /* initialise 2nd flash */
  60. size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  61. if (flash_info[1].flash_id == FLASH_UNKNOWN)
  62. {
  63. printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
  64. size2, size2<<20);
  65. }
  66. /* monitor protection ON by default */
  67. if (size1 == 512*1024)
  68. {
  69. (void)flash_protect(FLAG_PROTECT_SET,
  70. FLASH_BASE0_PRELIM,
  71. FLASH_BASE0_PRELIM+monitor_flash_len-1,
  72. &flash_info[0]);
  73. }
  74. if (size2 == 512*1024)
  75. {
  76. (void)flash_protect(FLAG_PROTECT_SET,
  77. FLASH_BASE1_PRELIM,
  78. FLASH_BASE1_PRELIM+monitor_flash_len-1,
  79. &flash_info[1]);
  80. }
  81. if (size2 == 4*1024*1024)
  82. {
  83. (void)flash_protect(FLAG_PROTECT_SET,
  84. CFG_FLASH_BASE,
  85. CFG_FLASH_BASE+monitor_flash_len-1,
  86. &flash_info[1]);
  87. }
  88. return (size1 + size2);
  89. }
  90. /*----------------------------------------------------------------------------*/
  91. void flash_print_info (flash_info_t *info)
  92. {
  93. int i;
  94. int k;
  95. int size;
  96. int erased;
  97. volatile unsigned long *flash;
  98. if (info->flash_id == FLASH_UNKNOWN) {
  99. printf ("missing or unknown FLASH type\n");
  100. flash_init();
  101. }
  102. if (info->flash_id == FLASH_UNKNOWN) {
  103. printf ("missing or unknown FLASH type\n");
  104. return;
  105. }
  106. switch (info->flash_id & FLASH_VENDMASK) {
  107. case FLASH_MAN_AMD:
  108. printf ("AMD ");
  109. break;
  110. default:
  111. printf ("Unknown Vendor ");
  112. break;
  113. }
  114. switch (info->flash_id & FLASH_TYPEMASK) {
  115. case AMD_ID_F040B:
  116. printf ("AM29F040B (4 Mbit)\n");
  117. break;
  118. case AMD_ID_F016D:
  119. printf ("AM29F016D (16 Mbit)\n");
  120. break;
  121. case AMD_ID_F032B:
  122. printf ("AM29F032B (32 Mbit)\n");
  123. break;
  124. default:
  125. printf ("Unknown Chip Type\n");
  126. break;
  127. }
  128. if (info->size >= (1 << 20)) {
  129. printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
  130. } else {
  131. printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
  132. }
  133. printf (" Sector Start Addresses:");
  134. for (i=0; i<info->sector_count; ++i) {
  135. /*
  136. * Check if whole sector is erased
  137. */
  138. if (i != (info->sector_count-1))
  139. size = info->start[i+1] - info->start[i];
  140. else
  141. size = info->start[0] + info->size - info->start[i];
  142. erased = 1;
  143. flash = (volatile unsigned long *)info->start[i];
  144. size = size >> 2; /* divide by 4 for longword access */
  145. for (k=0; k<size; k++) {
  146. if (*flash++ != 0xffffffff) {
  147. erased = 0;
  148. break;
  149. }
  150. }
  151. if ((i % 5) == 0)
  152. printf ("\n ");
  153. printf (" %08lX%s%s",
  154. info->start[i],
  155. erased ? " E" : " ",
  156. info->protect[i] ? "RO " : " ");
  157. }
  158. printf ("\n");
  159. }
  160. /*----------------------------------------------------------------------------*/
  161. /*
  162. * The following code cannot be run from FLASH!
  163. */
  164. ulong flash_get_size (vu_long *addr, flash_info_t *info)
  165. {
  166. short i;
  167. ulong vendor, devid;
  168. ulong base = (ulong)addr;
  169. volatile unsigned char *caddr = (unsigned char *)addr;
  170. #ifdef DEBUG
  171. printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr);
  172. #endif
  173. /* Write auto select command: read Manufacturer ID */
  174. caddr[0] = 0xF0; /* reset bank */
  175. udelay(10);
  176. eieio();
  177. caddr[0x555] = 0xAA;
  178. udelay(10);
  179. caddr[0x2AA] = 0x55;
  180. udelay(10);
  181. caddr[0x555] = 0x90;
  182. udelay(10);
  183. vendor = caddr[0];
  184. devid = caddr[1];
  185. #ifdef DEBUG
  186. printf("Manufacturer: 0x%lx\n", vendor);
  187. #endif
  188. vendor &= 0xff;
  189. devid &= 0xff;
  190. /* We accept only two AMD types */
  191. switch (vendor) {
  192. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  193. info->flash_id = FLASH_MAN_AMD;
  194. break;
  195. default:
  196. info->flash_id = FLASH_UNKNOWN;
  197. info->sector_count = 0;
  198. info->size = 0;
  199. return (0); /* no or unknown flash */
  200. }
  201. switch (devid) {
  202. case (FLASH_WORD_SIZE)AMD_ID_F040B:
  203. info->flash_id |= AMD_ID_F040B;
  204. info->sector_count = 8;
  205. info->size = 0x00080000;
  206. break; /* => 0.5 MB */
  207. case (FLASH_WORD_SIZE)AMD_ID_F016D:
  208. info->flash_id |= AMD_ID_F016D;
  209. info->sector_count = 32;
  210. info->size = 0x00200000;
  211. break; /* => 2 MB */
  212. case (FLASH_WORD_SIZE)AMD_ID_F032B:
  213. info->flash_id |= AMD_ID_F032B;
  214. info->sector_count = 64;
  215. info->size = 0x00400000;
  216. break; /* => 4 MB */
  217. default:
  218. info->flash_id = FLASH_UNKNOWN;
  219. return (0); /* => no or unknown flash */
  220. }
  221. #ifdef DEBUG
  222. printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size);
  223. #endif
  224. /* check for protected sectors */
  225. for (i = 0; i < info->sector_count; i++) {
  226. /* sector base address */
  227. info->start[i] = base + i * (info->size / info->sector_count);
  228. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  229. /* D0 = 1 if protected */
  230. caddr = (volatile unsigned char *)(info->start[i]);
  231. info->protect[i] = caddr[2] & 1;
  232. }
  233. /*
  234. * Prevent writes to uninitialized FLASH.
  235. */
  236. if (info->flash_id != FLASH_UNKNOWN) {
  237. caddr = (volatile unsigned char *)info->start[0];
  238. caddr[0] = 0xF0; /* reset bank */
  239. }
  240. return (info->size);
  241. }
  242. /*----------------------------------------------------------------------------*/
  243. int flash_erase (flash_info_t *info, int s_first, int s_last)
  244. {
  245. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  246. int flag, prot, sect, l_sect;
  247. ulong start, now, last;
  248. int rc = 0;
  249. if ((s_first < 0) || (s_first > s_last)) {
  250. if (info->flash_id == FLASH_UNKNOWN) {
  251. printf ("- missing\n");
  252. } else {
  253. printf ("- no sectors to erase\n");
  254. }
  255. return 1;
  256. }
  257. if ((info->flash_id == FLASH_UNKNOWN) ||
  258. (info->flash_id > FLASH_AMD_COMP)) {
  259. printf ("Can't erase unknown flash type - aborted\n");
  260. return 1;
  261. }
  262. prot = 0;
  263. for (sect=s_first; sect<=s_last; ++sect) {
  264. if (info->protect[sect]) {
  265. prot++;
  266. }
  267. }
  268. if (prot) {
  269. printf ("- Warning: %d protected sectors will not be erased!\n",
  270. prot);
  271. } else {
  272. printf ("\n");
  273. }
  274. l_sect = -1;
  275. /* Disable interrupts which might cause a timeout here */
  276. flag = disable_interrupts();
  277. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  278. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  279. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  280. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  281. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  282. /* Start erase on unprotected sectors */
  283. for (sect = s_first; sect<=s_last; sect++) {
  284. if (info->protect[sect] == 0) { /* not protected */
  285. addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  286. if (info->flash_id & FLASH_MAN_SST) {
  287. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  288. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  289. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  290. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  291. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  292. addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  293. udelay(30000); /* wait 30 ms */
  294. }
  295. else
  296. addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  297. l_sect = sect;
  298. }
  299. }
  300. /* re-enable interrupts if necessary */
  301. if (flag)
  302. enable_interrupts();
  303. /* wait at least 80us - let's wait 1 ms */
  304. udelay (1000);
  305. /*
  306. * We wait for the last triggered sector
  307. */
  308. if (l_sect < 0)
  309. goto DONE;
  310. start = get_timer (0);
  311. last = start;
  312. addr = (FLASH_WORD_SIZE *)(info->start[l_sect]);
  313. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  314. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  315. printf ("Timeout\n");
  316. return 1;
  317. }
  318. /* show that we're waiting */
  319. if ((now - last) > 1000) { /* every second */
  320. serial_putc ('.');
  321. last = now;
  322. }
  323. }
  324. DONE:
  325. /* reset to read mode */
  326. addr = (FLASH_WORD_SIZE *)info->start[0];
  327. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  328. printf (" done\n");
  329. return rc;
  330. }
  331. /*----------------------------------------------------------------------------*/
  332. /*
  333. * Copy memory to flash, returns:
  334. * 0 - OK
  335. * 1 - write timeout
  336. * 2 - Flash not erased
  337. */
  338. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  339. {
  340. ulong cp, wp, data;
  341. int i, l, rc;
  342. wp = (addr & ~3); /* get lower word aligned address */
  343. /*
  344. * handle unaligned start bytes
  345. */
  346. if ((l = addr - wp) != 0) {
  347. data = 0;
  348. for (i=0, cp=wp; i<l; ++i, ++cp) {
  349. data = (data << 8) | (*(uchar *)cp);
  350. }
  351. for (; i<4 && cnt>0; ++i) {
  352. data = (data << 8) | *src++;
  353. --cnt;
  354. ++cp;
  355. }
  356. for (; cnt==0 && i<4; ++i, ++cp) {
  357. data = (data << 8) | (*(uchar *)cp);
  358. }
  359. if ((rc = write_word(info, wp, data)) != 0) {
  360. return (rc);
  361. }
  362. wp += 4;
  363. }
  364. /*
  365. * handle word aligned part
  366. */
  367. while (cnt >= 4) {
  368. data = 0;
  369. for (i=0; i<4; ++i) {
  370. data = (data << 8) | *src++;
  371. }
  372. if ((rc = write_word(info, wp, data)) != 0) {
  373. return (rc);
  374. }
  375. wp += 4;
  376. cnt -= 4;
  377. }
  378. if (cnt == 0) {
  379. return (0);
  380. }
  381. /*
  382. * handle unaligned tail bytes
  383. */
  384. data = 0;
  385. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  386. data = (data << 8) | *src++;
  387. --cnt;
  388. }
  389. for (; i<4; ++i, ++cp) {
  390. data = (data << 8) | (*(uchar *)cp);
  391. }
  392. return (write_word(info, wp, data));
  393. }
  394. /*----------------------------------------------------------------------------*/
  395. /* Write a word to Flash, returns:
  396. * 0 - OK
  397. * 1 - write timeout
  398. * 2 - Flash not erased
  399. */
  400. static int write_word (flash_info_t *info, ulong dest, ulong data)
  401. {
  402. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
  403. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  404. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  405. ulong start;
  406. int flag;
  407. int i;
  408. /* Check if Flash is (sufficiently) erased */
  409. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  410. (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  411. return (2);
  412. }
  413. /* Disable interrupts which might cause a timeout here */
  414. flag = disable_interrupts();
  415. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  416. {
  417. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  418. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  419. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
  420. dest2[i] = data2[i];
  421. /* re-enable interrupts if necessary */
  422. if (flag)
  423. enable_interrupts();
  424. /* data polling for D7 */
  425. start = get_timer (0);
  426. while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
  427. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  428. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  429. return (1);
  430. }
  431. }
  432. }
  433. return (0);
  434. }
  435. /*----------------------------------------------------------------------------*/