hcu_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * (C) Copyright 2000-2004
  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. * Modified 4/5/2001
  25. * Wait for completion of each sector erase command issued
  26. * 4/5/2001
  27. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  28. *
  29. * Modified 6/6/2007
  30. * Added isync
  31. * Niklaus Giger, Netstal Maschinen, niklaus.giger@netstal.com
  32. *
  33. */
  34. #include <common.h>
  35. #include <ppc4xx.h>
  36. #include <asm/processor.h>
  37. #if CFG_MAX_FLASH_BANKS != 1
  38. #error "CFG_MAX_FLASH_BANKS must be 1"
  39. #endif
  40. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  41. /*-----------------------------------------------------------------------
  42. * Functions
  43. */
  44. static ulong flash_get_size (vu_long * addr, flash_info_t * info);
  45. static int write_word (flash_info_t * info, ulong dest, ulong data);
  46. static void flash_get_offsets (ulong base, flash_info_t * info);
  47. #define ADDR0 0x5555
  48. #define ADDR1 0x2aaa
  49. #define FLASH_WORD_SIZE unsigned char
  50. /*-----------------------------------------------------------------------*/
  51. unsigned long flash_init (void)
  52. {
  53. unsigned long size_b0;
  54. /* Init: no FLASHes known */
  55. flash_info[0].flash_id = FLASH_UNKNOWN;
  56. /* Static FLASH Bank configuration here - FIXME XXX */
  57. size_b0 = flash_get_size ((vu_long *) FLASH_BASE0_PRELIM,
  58. &flash_info[0]);
  59. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  60. printf ("## Unknown FLASH on Bank 0- Size=0x%08lx=%ld MB\n",
  61. size_b0, size_b0 << 20);
  62. }
  63. /* Only one bank */
  64. /* Setup offsets */
  65. flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
  66. /* Monitor protection ON by default */
  67. (void) flash_protect (FLAG_PROTECT_SET,
  68. FLASH_BASE0_PRELIM,
  69. FLASH_BASE0_PRELIM + monitor_flash_len - 1,
  70. &flash_info[0]);
  71. flash_info[0].size = size_b0;
  72. return size_b0;
  73. }
  74. /*-----------------------------------------------------------------------*/
  75. /*
  76. * This implementation assumes that the flash chips are uniform sector
  77. * devices. This is true for all likely flash devices on a HCUx.
  78. */
  79. static void flash_get_offsets (ulong base, flash_info_t * info)
  80. {
  81. unsigned idx;
  82. unsigned long sector_size = info->size / info->sector_count;
  83. for (idx = 0; idx < info->sector_count; idx += 1) {
  84. info->start[idx] = base + (idx * sector_size);
  85. }
  86. }
  87. /*-----------------------------------------------------------------------*/
  88. void flash_print_info (flash_info_t * info)
  89. {
  90. int i;
  91. int k;
  92. int size;
  93. int erased;
  94. volatile unsigned long *flash;
  95. if (info->flash_id == FLASH_UNKNOWN) {
  96. printf ("missing or unknown FLASH type\n");
  97. return;
  98. }
  99. switch (info->flash_id & FLASH_VENDMASK) {
  100. case FLASH_MAN_AMD:
  101. printf ("AMD ");
  102. break;
  103. case FLASH_MAN_FUJ:
  104. printf ("FUJITSU ");
  105. break;
  106. case FLASH_MAN_SST:
  107. printf ("SST ");
  108. break;
  109. case FLASH_MAN_STM:
  110. printf ("ST Micro ");
  111. break;
  112. default:
  113. printf ("Unknown Vendor ");
  114. break;
  115. }
  116. /* (Reduced table of only parts expected in HCUx boards.) */
  117. switch (info->flash_id) {
  118. case FLASH_MAN_AMD | FLASH_AM040:
  119. printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  120. break;
  121. case FLASH_MAN_STM | FLASH_AM040:
  122. printf ("MM29W040W (512 Kbit, uniform sector size)\n");
  123. break;
  124. default:
  125. printf ("Unknown Chip Type\n");
  126. break;
  127. }
  128. printf (" Size: %ld KB in %d Sectors\n",
  129. info->size >> 10, info->sector_count);
  130. printf (" Sector Start Addresses:");
  131. for (i = 0; i < info->sector_count; ++i) {
  132. /*
  133. * Check if whole sector is erased
  134. */
  135. if (i != (info->sector_count - 1))
  136. size = info->start[i + 1] - info->start[i];
  137. else
  138. size = info->start[0] + info->size - info->start[i];
  139. erased = 1;
  140. flash = (volatile unsigned long *) info->start[i];
  141. size = size >> 2; /* divide by 4 for longword access */
  142. for (k = 0; k < size; k++) {
  143. if (*flash++ != 0xffffffff) {
  144. erased = 0;
  145. break;
  146. }
  147. }
  148. if ((i % 5) == 0)
  149. printf ("\n ");
  150. printf (" %08lX%s%s",
  151. info->start[i],
  152. erased ? " E" : " ", info->protect[i] ? "RO " : " "
  153. );
  154. }
  155. printf ("\n");
  156. return;
  157. }
  158. /*-----------------------------------------------------------------------*/
  159. /*
  160. * The following code cannot be run from FLASH!
  161. */
  162. static ulong flash_get_size (vu_long * addr, flash_info_t * info)
  163. {
  164. short i;
  165. FLASH_WORD_SIZE value;
  166. ulong base = (ulong) addr;
  167. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) addr;
  168. /* Write auto select command: read Manufacturer ID */
  169. asm("isync");
  170. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  171. asm("isync");
  172. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  173. asm("isync");
  174. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00900090;
  175. asm("isync");
  176. value = addr2[0];
  177. asm("isync");
  178. switch (value) {
  179. case (FLASH_WORD_SIZE) AMD_MANUFACT:
  180. info->flash_id = FLASH_MAN_AMD;
  181. break;
  182. case (FLASH_WORD_SIZE) FUJ_MANUFACT:
  183. info->flash_id = FLASH_MAN_FUJ;
  184. break;
  185. case (FLASH_WORD_SIZE) SST_MANUFACT:
  186. info->flash_id = FLASH_MAN_SST;
  187. break;
  188. case (FLASH_WORD_SIZE)STM_MANUFACT:
  189. info->flash_id = FLASH_MAN_STM;
  190. break;
  191. default:
  192. info->flash_id = FLASH_UNKNOWN;
  193. info->sector_count = 0;
  194. info->size = 0;
  195. printf("Unknown flash manufacturer code: 0x%x at %p\n",
  196. value, addr);
  197. addr2[ADDR0] = (FLASH_WORD_SIZE) 0;
  198. return (0); /* no or unknown flash */
  199. }
  200. value = addr2[1]; /* device ID */
  201. switch (value) {
  202. case (FLASH_WORD_SIZE) AMD_ID_F040B:
  203. info->flash_id += FLASH_AM040;
  204. info->sector_count = 8;
  205. info->size = 0x0080000; /* => 512 ko */
  206. break;
  207. case (FLASH_WORD_SIZE) AMD_ID_LV040B:
  208. info->flash_id += FLASH_AM040;
  209. info->sector_count = 8;
  210. info->size = 0x0080000; /* => 512 ko */
  211. break;
  212. case (FLASH_WORD_SIZE)STM_ID_M29W040B: /* most likele HCU5 chip */
  213. info->flash_id += FLASH_AM040;
  214. info->sector_count = 8;
  215. info->size = 0x0080000; /* => 512 ko */
  216. break;
  217. default:
  218. info->flash_id = FLASH_UNKNOWN;
  219. return (0); /* => no or unknown flash */
  220. }
  221. /* Calculate the sector offsets (Use HCUx Optimized code). */
  222. flash_get_offsets(base, info);
  223. /* check for protected sectors */
  224. for (i = 0; i < info->sector_count; i++) {
  225. /* read sector protection at sector address,
  226. *(A7 .. A0) = 0x02
  227. * D0 = 1 if protected
  228. */
  229. addr2 = (volatile FLASH_WORD_SIZE *) (info->start[i]);
  230. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  231. info->protect[i] = 0;
  232. else
  233. info->protect[i] = addr2[2] & 1;
  234. }
  235. /*
  236. * Prevent writes to uninitialized FLASH.
  237. */
  238. if (info->flash_id != FLASH_UNKNOWN) {
  239. addr2 = (FLASH_WORD_SIZE *) info->start[0];
  240. *addr2 = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  241. }
  242. return (info->size);
  243. }
  244. int wait_for_DQ7 (flash_info_t * info, int sect)
  245. {
  246. ulong start, now, last;
  247. volatile FLASH_WORD_SIZE *addr =
  248. (FLASH_WORD_SIZE *) (info->start[sect]);
  249. start = get_timer (0);
  250. last = start;
  251. while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
  252. (FLASH_WORD_SIZE) 0x00800080) {
  253. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  254. printf ("Timeout\n");
  255. return -1;
  256. }
  257. /* show that we're waiting */
  258. if ((now - last) > 1000) { /* every second */
  259. putc ('.');
  260. last = now;
  261. }
  262. }
  263. return 0;
  264. }
  265. /*-----------------------------------------------------------------------*/
  266. int flash_erase (flash_info_t * info, int s_first, int s_last)
  267. {
  268. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
  269. volatile FLASH_WORD_SIZE *addr2;
  270. int flag, prot, sect, l_sect;
  271. int i;
  272. if ((s_first < 0) || (s_first > s_last)) {
  273. if (info->flash_id == FLASH_UNKNOWN) {
  274. printf ("- missing\n");
  275. } else {
  276. printf ("- no sectors to erase\n");
  277. }
  278. return 1;
  279. }
  280. if (info->flash_id == FLASH_UNKNOWN) {
  281. printf ("Can't erase unknown flash type - aborted\n");
  282. return 1;
  283. }
  284. prot = 0;
  285. for (sect = s_first; sect <= s_last; ++sect) {
  286. if (info->protect[sect]) {
  287. prot++;
  288. }
  289. }
  290. if (prot) {
  291. printf ("- Warning: %d protected sectors not erased!\n",
  292. prot);
  293. } else {
  294. printf ("\n");
  295. }
  296. l_sect = -1;
  297. /* Disable interrupts which might cause a timeout here */
  298. flag = disable_interrupts ();
  299. /* Start erase on unprotected sectors */
  300. for (sect = s_first; sect <= s_last; sect++) {
  301. if (info->protect[sect] == 0) { /* not protected */
  302. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  303. printf ("Erasing sector %p\n", addr2); /* CLH */
  304. if ((info->flash_id & FLASH_VENDMASK) ==
  305. FLASH_MAN_SST) {
  306. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  307. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  308. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
  309. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  310. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  311. /* block erase */
  312. addr2[0] = (FLASH_WORD_SIZE) 0x00500050;
  313. for (i = 0; i < 50; i++) udelay (1000);
  314. } else {
  315. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  316. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  317. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
  318. addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  319. addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  320. /* sector erase */
  321. addr2[0] = (FLASH_WORD_SIZE) 0x00300030;
  322. }
  323. l_sect = sect;
  324. /*
  325. * Wait for each sector to complete, it's more
  326. * reliable. According to AMD Spec, you must
  327. * issue all erase commands within a specified
  328. * timeout. This has been seen to fail, especially
  329. * if printf()s are included (for debug)!!
  330. */
  331. wait_for_DQ7 (info, sect);
  332. }
  333. }
  334. /* re-enable interrupts if necessary */
  335. if (flag)
  336. enable_interrupts ();
  337. /* wait at least 80us - let's wait 1 ms */
  338. udelay (1000);
  339. #if 0
  340. /*
  341. * We wait for the last triggered sector
  342. */
  343. if (l_sect < 0)
  344. goto DONE;
  345. wait_for_DQ7 (info, l_sect);
  346. DONE:
  347. #endif
  348. /* reset to read mode */
  349. addr = (FLASH_WORD_SIZE *) info->start[0];
  350. addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  351. printf (" done\n");
  352. return 0;
  353. }
  354. /*-----------------------------------------------------------------------
  355. * Copy memory to flash, returns:
  356. * 0 - OK
  357. * 1 - write timeout
  358. * 2 - Flash not erased
  359. */
  360. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  361. {
  362. ulong cp, wp, data;
  363. int i, l, rc;
  364. wp = (addr & ~3); /* get lower word aligned address */
  365. /*
  366. * handle unaligned start bytes
  367. */
  368. if ((l = addr - wp) != 0) {
  369. data = 0;
  370. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  371. data = (data << 8) | (*(uchar *) cp);
  372. }
  373. for (; i < 4 && cnt > 0; ++i) {
  374. data = (data << 8) | *src++;
  375. --cnt;
  376. ++cp;
  377. }
  378. for (; cnt == 0 && i < 4; ++i, ++cp) {
  379. data = (data << 8) | (*(uchar *) cp);
  380. }
  381. if ((rc = write_word (info, wp, data)) != 0) {
  382. return (rc);
  383. }
  384. wp += 4;
  385. }
  386. /*
  387. * handle word aligned part
  388. */
  389. while (cnt >= 4) {
  390. data = 0;
  391. for (i = 0; i < 4; ++i) {
  392. data = (data << 8) | *src++;
  393. }
  394. if ((rc = write_word (info, wp, data)) != 0) {
  395. return (rc);
  396. }
  397. wp += 4;
  398. cnt -= 4;
  399. }
  400. if (cnt == 0) {
  401. return (0);
  402. }
  403. /*
  404. * handle unaligned tail bytes
  405. */
  406. data = 0;
  407. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  408. data = (data << 8) | *src++;
  409. --cnt;
  410. }
  411. for (; i < 4; ++i, ++cp) {
  412. data = (data << 8) | (*(uchar *) cp);
  413. }
  414. return (write_word (info, wp, data));
  415. }
  416. /*-----------------------------------------------------------------------
  417. * Write a word to Flash, returns:
  418. * 0 - OK
  419. * 1 - write timeout
  420. * 2 - Flash not erased
  421. */
  422. static int write_word (flash_info_t * info, ulong dest, ulong data)
  423. {
  424. volatile FLASH_WORD_SIZE *addr2 =
  425. (FLASH_WORD_SIZE *) (info->start[0]);
  426. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  427. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  428. ulong start;
  429. int i;
  430. /* Check if Flash is (sufficiently) erased */
  431. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  432. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  433. return (2);
  434. }
  435. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  436. int flag;
  437. /* Disable interrupts which might cause a timeout here */
  438. flag = disable_interrupts ();
  439. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  440. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  441. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  442. dest2[i] = data2[i];
  443. /* re-enable interrupts if necessary */
  444. if (flag)
  445. enable_interrupts ();
  446. /* data polling for D7 */
  447. start = get_timer (0);
  448. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  449. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  450. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  451. return (1);
  452. }
  453. }
  454. }
  455. return (0);
  456. }