flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. * Yoo. Jonghoon, IPone, yooth@ipone.co.kr
  25. * U-Boot port on RPXlite board
  26. *
  27. * Some of flash control words are modified. (from 2x16bit device
  28. * to 4x8bit device)
  29. * RPXLite board I tested has only 4 AM29LV800BB devices. Other devices
  30. * are not tested.
  31. *
  32. * (?) Does an RPXLite board which
  33. * does not use AM29LV800 flash memory exist ?
  34. * I don't know...
  35. */
  36. #include <common.h>
  37. #include <mpc8xx.h>
  38. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  39. /*-----------------------------------------------------------------------
  40. * Functions
  41. */
  42. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  43. static int write_word (flash_info_t *info, ulong dest, ulong data);
  44. static void flash_get_offsets (ulong base, flash_info_t *info);
  45. /*-----------------------------------------------------------------------
  46. */
  47. unsigned long flash_init (void)
  48. {
  49. /* volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; */
  50. /* volatile memctl8xx_t *memctl = &immap->im_memctl; */
  51. unsigned long size_b0 ;
  52. int i;
  53. /* Init: no FLASHes known */
  54. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  55. flash_info[i].flash_id = FLASH_UNKNOWN;
  56. }
  57. /* Static FLASH Bank configuration here - FIXME XXX */
  58. /*
  59. size_b0 = flash_get_size((vu_long *)FLASH_BASE_DEBUG, &flash_info[0]);
  60. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  61. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  62. size_b0, size_b0<<20);
  63. }
  64. */
  65. /* Remap FLASH according to real size */
  66. /*%%%
  67. memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  68. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
  69. %%%*/
  70. /* Re-do sizing to get full correct info */
  71. size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  72. flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  73. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  74. /* monitor protection ON by default */
  75. flash_protect(FLAG_PROTECT_SET,
  76. CONFIG_SYS_MONITOR_BASE,
  77. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  78. &flash_info[0]);
  79. #endif
  80. flash_info[0].size = size_b0;
  81. return (size_b0);
  82. }
  83. /*-----------------------------------------------------------------------
  84. */
  85. static void flash_get_offsets (ulong base, flash_info_t *info)
  86. {
  87. int i;
  88. /* set up sector start address table */
  89. if (info->flash_id & FLASH_BTYPE) {
  90. /* set sector offsets for bottom boot block type */
  91. info->start[0] = base + 0x00000000;
  92. info->start[1] = base + 0x00010000;
  93. info->start[2] = base + 0x00018000;
  94. info->start[3] = base + 0x00020000;
  95. for (i = 4; i < info->sector_count; i++) {
  96. info->start[i] = base + ((i-3) * 0x00040000) ;
  97. }
  98. } else {
  99. /* set sector offsets for top boot block type */
  100. i = info->sector_count - 1;
  101. info->start[i--] = base + info->size - 0x00010000;
  102. info->start[i--] = base + info->size - 0x00018000;
  103. info->start[i--] = base + info->size - 0x00020000;
  104. for (; i >= 0; i--) {
  105. info->start[i] = base + i * 0x00040000;
  106. }
  107. }
  108. }
  109. /*-----------------------------------------------------------------------
  110. */
  111. void flash_print_info (flash_info_t *info)
  112. {
  113. int i;
  114. if (info->flash_id == FLASH_UNKNOWN) {
  115. printf ("missing or unknown FLASH type\n");
  116. return;
  117. }
  118. switch (info->flash_id & FLASH_VENDMASK) {
  119. case FLASH_MAN_AMD: printf ("AMD "); break;
  120. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  121. default: printf ("Unknown Vendor "); break;
  122. }
  123. switch (info->flash_id & FLASH_TYPEMASK) {
  124. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  125. break;
  126. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  127. break;
  128. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  129. break;
  130. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  131. break;
  132. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  133. break;
  134. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  135. break;
  136. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  137. break;
  138. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  139. break;
  140. default: printf ("Unknown Chip Type\n");
  141. break;
  142. }
  143. printf (" Size: %ld MB in %d Sectors\n",
  144. info->size >> 20, info->sector_count);
  145. printf (" Sector Start Addresses:");
  146. for (i=0; i<info->sector_count; ++i) {
  147. if ((i % 5) == 0)
  148. printf ("\n ");
  149. printf (" %08lX%s",
  150. info->start[i],
  151. info->protect[i] ? " (RO)" : " "
  152. );
  153. }
  154. printf ("\n");
  155. return;
  156. }
  157. /*-----------------------------------------------------------------------
  158. */
  159. /*-----------------------------------------------------------------------
  160. */
  161. /*
  162. * The following code cannot be run from FLASH!
  163. */
  164. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  165. {
  166. short i;
  167. ulong value;
  168. ulong base = (ulong)addr;
  169. /* Write auto select command: read Manufacturer ID */
  170. addr[0xAAA] = 0x00AA00AA ;
  171. addr[0x555] = 0x00550055 ;
  172. addr[0xAAA] = 0x00900090 ;
  173. value = addr[0] ;
  174. switch (value & 0x00FF00FF) {
  175. case AMD_MANUFACT:
  176. info->flash_id = FLASH_MAN_AMD;
  177. break;
  178. case FUJ_MANUFACT:
  179. info->flash_id = FLASH_MAN_FUJ;
  180. break;
  181. default:
  182. info->flash_id = FLASH_UNKNOWN;
  183. info->sector_count = 0;
  184. info->size = 0;
  185. return (0); /* no or unknown flash */
  186. }
  187. value = addr[2] ; /* device ID */
  188. switch (value & 0x00FF00FF) {
  189. case (AMD_ID_LV400T & 0x00FF00FF):
  190. info->flash_id += FLASH_AM400T;
  191. info->sector_count = 11;
  192. info->size = 0x00100000;
  193. break; /* => 1 MB */
  194. case (AMD_ID_LV400B & 0x00FF00FF):
  195. info->flash_id += FLASH_AM400B;
  196. info->sector_count = 11;
  197. info->size = 0x00100000;
  198. break; /* => 1 MB */
  199. case (AMD_ID_LV800T & 0x00FF00FF):
  200. info->flash_id += FLASH_AM800T;
  201. info->sector_count = 19;
  202. info->size = 0x00200000;
  203. break; /* => 2 MB */
  204. case (AMD_ID_LV800B & 0x00FF00FF):
  205. info->flash_id += FLASH_AM800B;
  206. info->sector_count = 19;
  207. info->size = 0x00400000; /*%%% Size doubled by yooth */
  208. break; /* => 4 MB */
  209. case (AMD_ID_LV160T & 0x00FF00FF):
  210. info->flash_id += FLASH_AM160T;
  211. info->sector_count = 35;
  212. info->size = 0x00400000;
  213. break; /* => 4 MB */
  214. case (AMD_ID_LV160B & 0x00FF00FF):
  215. info->flash_id += FLASH_AM160B;
  216. info->sector_count = 35;
  217. info->size = 0x00400000;
  218. break; /* => 4 MB */
  219. #if 0 /* enable when device IDs are available */
  220. case AMD_ID_LV320T:
  221. info->flash_id += FLASH_AM320T;
  222. info->sector_count = 67;
  223. info->size = 0x00800000;
  224. break; /* => 8 MB */
  225. case AMD_ID_LV320B:
  226. info->flash_id += FLASH_AM320B;
  227. info->sector_count = 67;
  228. info->size = 0x00800000;
  229. break; /* => 8 MB */
  230. #endif
  231. default:
  232. info->flash_id = FLASH_UNKNOWN;
  233. return (0); /* => no or unknown flash */
  234. }
  235. /*%%% sector start address modified */
  236. /* set up sector start address table */
  237. if (info->flash_id & FLASH_BTYPE) {
  238. /* set sector offsets for bottom boot block type */
  239. info->start[0] = base + 0x00000000;
  240. info->start[1] = base + 0x00010000;
  241. info->start[2] = base + 0x00018000;
  242. info->start[3] = base + 0x00020000;
  243. for (i = 4; i < info->sector_count; i++) {
  244. info->start[i] = base + ((i-3) * 0x00040000) ;
  245. }
  246. } else {
  247. /* set sector offsets for top boot block type */
  248. i = info->sector_count - 1;
  249. info->start[i--] = base + info->size - 0x00010000;
  250. info->start[i--] = base + info->size - 0x00018000;
  251. info->start[i--] = base + info->size - 0x00020000;
  252. for (; i >= 0; i--) {
  253. info->start[i] = base + i * 0x00040000;
  254. }
  255. }
  256. /* check for protected sectors */
  257. for (i = 0; i < info->sector_count; i++) {
  258. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  259. /* D0 = 1 if protected */
  260. addr = (volatile unsigned long *)(info->start[i]);
  261. info->protect[i] = addr[4] & 1 ;
  262. }
  263. /*
  264. * Prevent writes to uninitialized FLASH.
  265. */
  266. if (info->flash_id != FLASH_UNKNOWN) {
  267. addr = (volatile unsigned long *)info->start[0];
  268. *addr = 0xF0F0F0F0; /* reset bank */
  269. }
  270. return (info->size);
  271. }
  272. /*-----------------------------------------------------------------------
  273. */
  274. int flash_erase (flash_info_t *info, int s_first, int s_last)
  275. {
  276. vu_long *addr = (vu_long*)(info->start[0]);
  277. int flag, prot, sect, l_sect;
  278. ulong start, now, last;
  279. if ((s_first < 0) || (s_first > s_last)) {
  280. if (info->flash_id == FLASH_UNKNOWN) {
  281. printf ("- missing\n");
  282. } else {
  283. printf ("- no sectors to erase\n");
  284. }
  285. return 1;
  286. }
  287. if ((info->flash_id == FLASH_UNKNOWN) ||
  288. (info->flash_id > FLASH_AMD_COMP)) {
  289. printf ("Can't erase unknown flash type %08lx - aborted\n",
  290. info->flash_id);
  291. return 1;
  292. }
  293. prot = 0;
  294. for (sect=s_first; sect<=s_last; ++sect) {
  295. if (info->protect[sect]) {
  296. prot++;
  297. }
  298. }
  299. if (prot) {
  300. printf ("- Warning: %d protected sectors will not be erased!\n",
  301. prot);
  302. } else {
  303. printf ("\n");
  304. }
  305. l_sect = -1;
  306. /* Disable interrupts which might cause a timeout here */
  307. flag = disable_interrupts();
  308. addr[0xAAA] = 0xAAAAAAAA;
  309. addr[0x555] = 0x55555555;
  310. addr[0xAAA] = 0x80808080;
  311. addr[0xAAA] = 0xAAAAAAAA;
  312. addr[0x555] = 0x55555555;
  313. /* Start erase on unprotected sectors */
  314. for (sect = s_first; sect<=s_last; sect++) {
  315. if (info->protect[sect] == 0) { /* not protected */
  316. addr = (vu_long *)(info->start[sect]) ;
  317. addr[0] = 0x30303030 ;
  318. l_sect = sect;
  319. }
  320. }
  321. /* re-enable interrupts if necessary */
  322. if (flag)
  323. enable_interrupts();
  324. /* wait at least 80us - let's wait 1 ms */
  325. udelay (1000);
  326. /*
  327. * We wait for the last triggered sector
  328. */
  329. if (l_sect < 0)
  330. goto DONE;
  331. start = get_timer (0);
  332. last = start;
  333. addr = (vu_long *)(info->start[l_sect]);
  334. while ((addr[0] & 0x80808080) != 0x80808080) {
  335. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  336. printf ("Timeout\n");
  337. return 1;
  338. }
  339. /* show that we're waiting */
  340. if ((now - last) > 1000) { /* every second */
  341. putc ('.');
  342. last = now;
  343. }
  344. }
  345. DONE:
  346. /* reset to read mode */
  347. addr = (vu_long *)info->start[0];
  348. addr[0] = 0xF0F0F0F0; /* reset bank */
  349. printf (" done\n");
  350. return 0;
  351. }
  352. /*-----------------------------------------------------------------------
  353. * Copy memory to flash, returns:
  354. * 0 - OK
  355. * 1 - write timeout
  356. * 2 - Flash not erased
  357. */
  358. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  359. {
  360. ulong cp, wp, data;
  361. int i, l, rc;
  362. wp = (addr & ~3); /* get lower word aligned address */
  363. /*
  364. * handle unaligned start bytes
  365. */
  366. if ((l = addr - wp) != 0) {
  367. data = 0;
  368. for (i=0, cp=wp; i<l; ++i, ++cp) {
  369. data = (data << 8) | (*(uchar *)cp);
  370. }
  371. for (; i<4 && cnt>0; ++i) {
  372. data = (data << 8) | *src++;
  373. --cnt;
  374. ++cp;
  375. }
  376. for (; cnt==0 && i<4; ++i, ++cp) {
  377. data = (data << 8) | (*(uchar *)cp);
  378. }
  379. if ((rc = write_word(info, wp, data)) != 0) {
  380. return (rc);
  381. }
  382. wp += 4;
  383. }
  384. /*
  385. * handle word aligned part
  386. */
  387. while (cnt >= 4) {
  388. data = 0;
  389. for (i=0; i<4; ++i) {
  390. data = (data << 8) | *src++;
  391. }
  392. if ((rc = write_word(info, wp, data)) != 0) {
  393. return (rc);
  394. }
  395. wp += 4;
  396. cnt -= 4;
  397. }
  398. if (cnt == 0) {
  399. return (0);
  400. }
  401. /*
  402. * handle unaligned tail bytes
  403. */
  404. data = 0;
  405. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  406. data = (data << 8) | *src++;
  407. --cnt;
  408. }
  409. for (; i<4; ++i, ++cp) {
  410. data = (data << 8) | (*(uchar *)cp);
  411. }
  412. return (write_word(info, wp, data));
  413. }
  414. /*-----------------------------------------------------------------------
  415. * Write a word to Flash, returns:
  416. * 0 - OK
  417. * 1 - write timeout
  418. * 2 - Flash not erased
  419. */
  420. static int write_word (flash_info_t *info, ulong dest, ulong data)
  421. {
  422. vu_long *addr = (vu_long *)(info->start[0]);
  423. ulong start;
  424. int flag;
  425. /* Check if Flash is (sufficiently) erased */
  426. if ((*((vu_long *)dest) & data) != data) {
  427. return (2);
  428. }
  429. /* Disable interrupts which might cause a timeout here */
  430. flag = disable_interrupts();
  431. addr[0xAAA] = 0xAAAAAAAA;
  432. addr[0x555] = 0x55555555;
  433. addr[0xAAA] = 0xA0A0A0A0;
  434. *((vu_long *)dest) = data;
  435. /* re-enable interrupts if necessary */
  436. if (flag)
  437. enable_interrupts();
  438. /* data polling for D7 */
  439. start = get_timer (0);
  440. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  441. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  442. return (1);
  443. }
  444. }
  445. return (0);
  446. }
  447. /*-----------------------------------------------------------------------
  448. */