flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * (C) Copyright 2003
  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. * PPCboot 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[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  39. /*-----------------------------------------------------------------------
  40. * Functions
  41. */
  42. static ulong flash_get_size ( short manu, short dev_id, 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, int two_chips);
  45. static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id);
  46. static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id);
  47. /*-----------------------------------------------------------------------
  48. */
  49. unsigned long flash_init (void)
  50. {
  51. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  52. volatile memctl8xx_t *memctl = &immap->im_memctl;
  53. unsigned long size_b0, size_b1;
  54. short manu, dev_id;
  55. int i;
  56. /* Init: no FLASHes known */
  57. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  58. flash_info[i].flash_id = FLASH_UNKNOWN;
  59. }
  60. /* Do sizing to get full correct info */
  61. flash_get_id_word((void*)CFG_FLASH_BASE0,&manu,&dev_id);
  62. size_b0 = flash_get_size(manu, dev_id, &flash_info[0]);
  63. flash_get_offsets (CFG_FLASH_BASE0, &flash_info[0],0);
  64. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (0 - size_b0);
  65. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE0
  66. /* monitor protection ON by default */
  67. flash_protect(FLAG_PROTECT_SET,
  68. CFG_MONITOR_BASE,
  69. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  70. &flash_info[0]);
  71. #endif
  72. flash_get_id_long((void*)CFG_FLASH_BASE1,&manu,&dev_id);
  73. size_b1 = 2 * flash_get_size(manu, dev_id, &flash_info[1]);
  74. flash_get_offsets(CFG_FLASH_BASE1, &flash_info[1],1);
  75. memctl->memc_or1 = CFG_OR_TIMING_FLASH | (0 - size_b1);
  76. flash_info[0].size = size_b0;
  77. flash_info[1].size = size_b1;
  78. return (size_b0+size_b1);
  79. }
  80. /*-----------------------------------------------------------------------
  81. */
  82. static void flash_get_offsets (ulong base, flash_info_t *info, int two_chips)
  83. {
  84. int i, addr_shift;
  85. vu_short *addr = (vu_short*)base;
  86. addr[0x555] = 0x00AA ;
  87. addr[0xAAA] = 0x0055 ;
  88. addr[0x555] = 0x0090 ;
  89. addr_shift = (two_chips ? 2 : 1 );
  90. /* set up sector start address table */
  91. if (info->flash_id & FLASH_BTYPE) {
  92. /* set sector offsets for bottom boot block type */
  93. info->start[0] = base + (0x00000000<<addr_shift);
  94. info->start[1] = base + (0x00002000<<addr_shift);
  95. info->start[2] = base + (0x00003000<<addr_shift);
  96. info->start[3] = base + (0x00004000<<addr_shift);
  97. for (i = 4; i < info->sector_count; i++) {
  98. info->start[i] = base + ((i-3) * (0x00008000<<addr_shift)) ;
  99. }
  100. } else {
  101. /* set sector offsets for top boot block type */
  102. i = info->sector_count - 1;
  103. info->start[i--] = base + info->size - (0x00002000<<addr_shift);
  104. info->start[i--] = base + info->size - (0x00003000<<addr_shift);
  105. info->start[i--] = base + info->size - (0x00004000<<addr_shift);
  106. for (; i >= 0; i--) {
  107. info->start[i] = base + i * (0x00008000<<addr_shift);
  108. }
  109. }
  110. /* check for protected sectors */
  111. for (i = 0; i < info->sector_count; i++) {
  112. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  113. /* D0 = 1 if protected */
  114. addr = (vu_short *)(info->start[i]);
  115. info->protect[i] = addr[1<<addr_shift] & 1 ;
  116. }
  117. addr = (vu_short *)info->start[0];
  118. *addr = 0xF0F0; /* reset bank */
  119. }
  120. /*-----------------------------------------------------------------------
  121. */
  122. void flash_print_info (flash_info_t *info)
  123. {
  124. int i;
  125. if (info->flash_id == FLASH_UNKNOWN) {
  126. printf ("missing or unknown FLASH type\n");
  127. return;
  128. }
  129. switch (info->flash_id & FLASH_VENDMASK) {
  130. case FLASH_MAN_AMD: printf ("AMD "); break;
  131. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  132. case FLASH_MAN_TOSH: printf ("TOSHIBA "); break;
  133. default: printf ("Unknown Vendor "); break;
  134. }
  135. switch (info->flash_id & FLASH_TYPEMASK) {
  136. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  137. break;
  138. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  139. break;
  140. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  141. break;
  142. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  143. break;
  144. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  145. break;
  146. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  147. break;
  148. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  149. break;
  150. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  151. break;
  152. default: printf ("Unknown Chip Type\n");
  153. break;
  154. }
  155. printf (" Size: %ld MB in %d Sectors\n",
  156. info->size >> 20, info->sector_count);
  157. printf (" Sector Start Addresses:");
  158. for (i=0; i<info->sector_count; ++i) {
  159. if ((i % 5) == 0)
  160. printf ("\n ");
  161. printf (" %08lX%s",
  162. info->start[i],
  163. info->protect[i] ? " (RO)" : " "
  164. );
  165. }
  166. printf ("\n");
  167. }
  168. /*-----------------------------------------------------------------------
  169. */
  170. /*-----------------------------------------------------------------------
  171. */
  172. /*
  173. * The following code cannot be run from FLASH!
  174. */
  175. static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id)
  176. {
  177. vu_short *addr = (vu_short*)ptr;
  178. addr[0x555] = 0x00AA ;
  179. addr[0xAAA] = 0x0055 ;
  180. addr[0x555] = 0x0090 ;
  181. *ptr_manuf = addr[0];
  182. *ptr_dev_id = addr[1];
  183. addr[0] = 0xf0f0; /* return to normal */
  184. }
  185. static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id)
  186. {
  187. vu_short *addr = (vu_short*)ptr;
  188. vu_short *addr1, *addr2, *addr3;
  189. addr1 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
  190. addr2 = (vu_short*) ( ((int)ptr) + (0x2AAA<<2) );
  191. addr3 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
  192. *addr1 = 0xAAAA;
  193. *addr2 = 0x5555;
  194. *addr3 = 0x9090;
  195. *ptr_manuf = addr[0];
  196. *ptr_dev_id = addr[2];
  197. addr[0] = 0xf0f0; /* return to normal */
  198. }
  199. static ulong flash_get_size ( short manu, short dev_id, flash_info_t *info)
  200. {
  201. switch (manu) {
  202. case ((short)AMD_MANUFACT):
  203. info->flash_id = FLASH_MAN_AMD;
  204. break;
  205. case ((short)FUJ_MANUFACT):
  206. info->flash_id = FLASH_MAN_FUJ;
  207. break;
  208. case ((short)TOSH_MANUFACT):
  209. info->flash_id = FLASH_MAN_TOSH;
  210. break;
  211. default:
  212. info->flash_id = FLASH_UNKNOWN;
  213. info->sector_count = 0;
  214. info->size = 0;
  215. return (0); /* no or unknown flash */
  216. }
  217. switch (dev_id) {
  218. case ((short)TOSH_ID_FVT160):
  219. info->flash_id += FLASH_AM160T;
  220. info->sector_count = 35;
  221. info->size = 0x00200000;
  222. break; /* => 1 MB */
  223. case ((short)TOSH_ID_FVB160):
  224. info->flash_id += FLASH_AM160B;
  225. info->sector_count = 35;
  226. info->size = 0x00200000;
  227. break; /* => 1 MB */
  228. case ((short)AMD_ID_LV400T):
  229. info->flash_id += FLASH_AM400T;
  230. info->sector_count = 11;
  231. info->size = 0x00100000;
  232. break; /* => 1 MB */
  233. case ((short)AMD_ID_LV400B):
  234. info->flash_id += FLASH_AM400B;
  235. info->sector_count = 11;
  236. info->size = 0x00100000;
  237. break; /* => 1 MB */
  238. case ((short)AMD_ID_LV800T):
  239. info->flash_id += FLASH_AM800T;
  240. info->sector_count = 19;
  241. info->size = 0x00200000;
  242. break; /* => 2 MB */
  243. case ((short)AMD_ID_LV800B):
  244. info->flash_id += FLASH_AM800B;
  245. info->sector_count = 19;
  246. info->size = 0x00400000; //%%% Size doubled by yooth
  247. break; /* => 4 MB */
  248. case ((short)AMD_ID_LV160T):
  249. info->flash_id += FLASH_AM160T;
  250. info->sector_count = 35;
  251. info->size = 0x00200000;
  252. break; /* => 4 MB */
  253. case ((short)AMD_ID_LV160B):
  254. info->flash_id += FLASH_AM160B;
  255. info->sector_count = 35;
  256. info->size = 0x00200000;
  257. break; /* => 4 MB */
  258. default:
  259. info->flash_id = FLASH_UNKNOWN;
  260. return (0); /* => no or unknown flash */
  261. }
  262. return(info->size);
  263. }
  264. /*-----------------------------------------------------------------------
  265. */
  266. int flash_erase (flash_info_t *info, int s_first, int s_last)
  267. {
  268. vu_short *addr = (vu_short*)(info->start[0]);
  269. int flag, prot, sect, l_sect;
  270. ulong start, now, last;
  271. if ((s_first < 0) || (s_first > s_last)) {
  272. if (info->flash_id == FLASH_UNKNOWN) {
  273. printf ("- missing\n");
  274. } else {
  275. printf ("- no sectors to erase\n");
  276. }
  277. return 1;
  278. }
  279. if ((info->flash_id == FLASH_UNKNOWN) ||
  280. (info->flash_id > FLASH_AMD_COMP)) {
  281. printf ("Can't erase unknown flash type %08lx - aborted\n",
  282. info->flash_id);
  283. return 1;
  284. }
  285. prot = 0;
  286. for (sect=s_first; sect<=s_last; ++sect) {
  287. if (info->protect[sect]) {
  288. prot++;
  289. }
  290. }
  291. if (prot) {
  292. printf ("- Warning: %d protected sectors will not be erased!\n",
  293. prot);
  294. } else {
  295. printf ("\n");
  296. }
  297. l_sect = -1;
  298. /* Disable interrupts which might cause a timeout here */
  299. flag = disable_interrupts();
  300. addr[0x555] = (vu_short)0xAAAAAAAA;
  301. addr[0xAAA] = (vu_short)0x55555555;
  302. addr[0x555] = (vu_short)0x80808080;
  303. addr[0x555] = (vu_short)0xAAAAAAAA;
  304. addr[0xAAA] = (vu_short)0x55555555;
  305. /* Start erase on unprotected sectors */
  306. for (sect = s_first; sect<=s_last; sect++) {
  307. if (info->protect[sect] == 0) { /* not protected */
  308. addr = (vu_short *)(info->start[sect]) ;
  309. addr[0] = (vu_short)0x30303030 ;
  310. l_sect = sect;
  311. }
  312. }
  313. /* re-enable interrupts if necessary */
  314. if (flag)
  315. enable_interrupts();
  316. /* wait at least 80us - let's wait 1 ms */
  317. udelay (1000);
  318. /*
  319. * We wait for the last triggered sector
  320. */
  321. if (l_sect < 0)
  322. goto DONE;
  323. start = get_timer (0);
  324. last = start;
  325. addr = (vu_short *)(info->start[l_sect]);
  326. while ((addr[0] & 0x8080) != 0x8080) {
  327. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  328. printf ("Timeout\n");
  329. return 1;
  330. }
  331. /* show that we're waiting */
  332. if ((now - last) > 1000) { /* every second */
  333. putc ('.');
  334. last = now;
  335. }
  336. }
  337. DONE:
  338. /* reset to read mode */
  339. addr = (vu_short *)info->start[0];
  340. addr[0] = (vu_short)0xF0F0F0F0; /* reset bank */
  341. printf (" done\n");
  342. return 0;
  343. }
  344. /*-----------------------------------------------------------------------
  345. * Copy memory to flash, returns:
  346. * 0 - OK
  347. * 1 - write timeout
  348. * 2 - Flash not erased
  349. */
  350. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  351. {
  352. ulong cp, wp, data;
  353. int i, l, rc;
  354. wp = (addr & ~3); /* get lower word aligned address */
  355. /*
  356. * handle unaligned start bytes
  357. */
  358. if ((l = addr - wp) != 0) {
  359. data = 0;
  360. for (i=0, cp=wp; i<l; ++i, ++cp) {
  361. data = (data << 8) | (*(uchar *)cp);
  362. }
  363. for (; i<4 && cnt>0; ++i) {
  364. data = (data << 8) | *src++;
  365. --cnt;
  366. ++cp;
  367. }
  368. for (; cnt==0 && i<4; ++i, ++cp) {
  369. data = (data << 8) | (*(uchar *)cp);
  370. }
  371. if ((rc = write_word(info, wp, data)) != 0) {
  372. return (rc);
  373. }
  374. wp += 4;
  375. }
  376. /*
  377. * handle word aligned part
  378. */
  379. while (cnt >= 4) {
  380. data = 0;
  381. for (i=0; i<4; ++i) {
  382. data = (data << 8) | *src++;
  383. }
  384. if ((rc = write_word(info, wp, data)) != 0) {
  385. return (rc);
  386. }
  387. wp += 4;
  388. cnt -= 4;
  389. }
  390. if (cnt == 0) {
  391. return (0);
  392. }
  393. /*
  394. * handle unaligned tail bytes
  395. */
  396. data = 0;
  397. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  398. data = (data << 8) | *src++;
  399. --cnt;
  400. }
  401. for (; i<4; ++i, ++cp) {
  402. data = (data << 8) | (*(uchar *)cp);
  403. }
  404. return (write_word(info, wp, data));
  405. }
  406. /*-----------------------------------------------------------------------
  407. * Write a word to Flash, returns:
  408. * 0 - OK
  409. * 1 - write timeout
  410. * 2 - Flash not erased
  411. */
  412. static int write_word (flash_info_t *info, ulong dest, ulong data)
  413. {
  414. vu_short *addr = (vu_short *)(info->start[0]);
  415. vu_short sdata;
  416. ulong start;
  417. int flag;
  418. /* Check if Flash is (sufficiently) erased */
  419. if ((*((vu_long *)dest) & data) != data) {
  420. return (2);
  421. }
  422. /* First write upper 16 bits */
  423. sdata = (short)(data>>16);
  424. /* Disable interrupts which might cause a timeout here */
  425. flag = disable_interrupts();
  426. addr[0x555] = 0xAAAA;
  427. addr[0xAAA] = 0x5555;
  428. addr[0x555] = 0xA0A0;
  429. *((vu_short *)dest) = sdata;
  430. /* re-enable interrupts if necessary */
  431. if (flag)
  432. enable_interrupts();
  433. /* data polling for D7 */
  434. start = get_timer (0);
  435. while ((*((vu_short *)dest) & 0x8080) != (sdata & 0x8080)) {
  436. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  437. return (1);
  438. }
  439. }
  440. /* Now write lower 16 bits */
  441. sdata = (short)(data&0xffff);
  442. /* Disable interrupts which might cause a timeout here */
  443. flag = disable_interrupts();
  444. addr[0x555] = 0xAAAA;
  445. addr[0xAAA] = 0x5555;
  446. addr[0x555] = 0xA0A0;
  447. *((vu_short *)dest + 1) = sdata;
  448. /* re-enable interrupts if necessary */
  449. if (flag)
  450. enable_interrupts();
  451. /* data polling for D7 */
  452. start = get_timer (0);
  453. while ((*((vu_short *)dest + 1) & 0x8080) != (sdata & 0x8080)) {
  454. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  455. return (1);
  456. }
  457. }
  458. return (0);
  459. }
  460. /*-----------------------------------------------------------------------
  461. */