flash.c 13 KB

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