hcu_flash.c 12 KB

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