flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * (C) Copyright 2000
  3. * Marius Groeger <mgroeger@sysgo.de>
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * Flash Routines for AMD 29F080B devices
  10. * Added support for 64bit and AMD 29DL323B
  11. *
  12. *--------------------------------------------------------------------
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #include <mpc8xx.h>
  33. #include <asm/io.h>
  34. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  35. #define RD_SWP32(x) in_le32((volatile u32*)x)
  36. /*-----------------------------------------------------------------------
  37. * Functions
  38. */
  39. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  40. static int write_word (flash_info_t *info, ulong dest, ulong data);
  41. /*-----------------------------------------------------------------------
  42. */
  43. unsigned long flash_init(void)
  44. {
  45. unsigned long size;
  46. int i;
  47. /* Init: no FLASHes known */
  48. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
  49. flash_info[i].flash_id = FLASH_UNKNOWN;
  50. /* for now, only support the 4 MB Flash SIMM */
  51. size = flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
  52. &flash_info[0]);
  53. /*
  54. * protect monitor and environment sectors
  55. */
  56. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
  57. flash_protect(FLAG_PROTECT_SET,
  58. CONFIG_SYS_MONITOR_BASE,
  59. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  60. &flash_info[0]);
  61. #endif
  62. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  63. #ifndef CONFIG_ENV_SIZE
  64. #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  65. #endif
  66. flash_protect(FLAG_PROTECT_SET,
  67. CONFIG_ENV_ADDR,
  68. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  69. #endif
  70. return /*size */ (CONFIG_SYS_FLASH0_SIZE * 1024 * 1024);
  71. }
  72. /*-----------------------------------------------------------------------
  73. */
  74. void flash_print_info (flash_info_t *info)
  75. {
  76. int i;
  77. if (info->flash_id == FLASH_UNKNOWN) {
  78. printf ("missing or unknown FLASH type\n");
  79. return;
  80. }
  81. switch (info->flash_id & FLASH_VENDMASK) {
  82. case (AMD_MANUFACT & FLASH_VENDMASK):
  83. printf ("AMD ");
  84. break;
  85. case (FUJ_MANUFACT & FLASH_VENDMASK):
  86. printf ("FUJITSU ");
  87. break;
  88. case (SST_MANUFACT & FLASH_VENDMASK):
  89. printf ("SST ");
  90. break;
  91. default:
  92. printf ("Unknown Vendor ");
  93. break;
  94. }
  95. switch (info->flash_id & FLASH_TYPEMASK) {
  96. case (AMD_ID_DL323B & FLASH_TYPEMASK):
  97. printf("AM29DL323B (32 MBit)\n");
  98. break;
  99. default:
  100. printf ("Unknown Chip Type\n");
  101. break;
  102. }
  103. printf (" Size: %ld MB in %d Sectors\n",
  104. info->size >> 20, info->sector_count);
  105. printf (" Sector Start Addresses:");
  106. for (i = 0; i < info->sector_count; ++i) {
  107. if ((i % 5) == 0) printf ("\n ");
  108. printf (" %08lX%s",
  109. info->start[i],
  110. info->protect[i] ? " (RO)" : " "
  111. );
  112. }
  113. printf ("\n");
  114. return;
  115. }
  116. /*
  117. * The following code cannot be run from FLASH!
  118. */
  119. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  120. {
  121. short i;
  122. vu_long vendor[2], devid[2];
  123. ulong base = (ulong)addr;
  124. /* Reset and Write auto select command: read Manufacturer ID */
  125. addr[0] = 0xf0f0f0f0;
  126. addr[2 * 0x0555] = 0xAAAAAAAA;
  127. addr[2 * 0x02AA] = 0x55555555;
  128. addr[2 * 0x0555] = 0x90909090;
  129. addr[1] = 0xf0f0f0f0;
  130. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  131. addr[2 * 0x02AA + 1] = 0x55555555;
  132. addr[2 * 0x0555 + 1] = 0x90909090;
  133. udelay (1000);
  134. vendor[0] = RD_SWP32(&addr[0]);
  135. vendor[1] = RD_SWP32(&addr[1]);
  136. if (vendor[0] != vendor[1] || vendor[0] != AMD_MANUFACT) {
  137. info->size = 0;
  138. goto out;
  139. }
  140. devid[0] = RD_SWP32(&addr[2]);
  141. devid[1] = RD_SWP32(&addr[3]);
  142. if (devid[0] == AMD_ID_DL323B) {
  143. /*
  144. * we have 2 Banks
  145. * Bank 1 (23 Sectors): 0-7=8kbyte, 8-22=64kbyte
  146. * Bank 2 (48 Sectors): 23-70=64kbyte
  147. */
  148. info->flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
  149. (AMD_ID_DL323B & FLASH_TYPEMASK);
  150. info->sector_count = 71;
  151. info->size = 4 * (8 * 8 + 63 * 64) * 1024;
  152. }
  153. else {
  154. info->size = 0;
  155. goto out;
  156. }
  157. /* set up sector start address table */
  158. for (i = 0; i < 8; i++) {
  159. info->start[i] = base + (i * 0x8000);
  160. }
  161. for (i = 8; i < info->sector_count; i++) {
  162. info->start[i] = base + (i * 0x40000) + 8 * 0x8000 - 8 * 0x40000;
  163. }
  164. /* check for protected sectors */
  165. for (i = 0; i < info->sector_count; i++) {
  166. /* read sector protection at sector address */
  167. addr = (volatile unsigned long *)(info->start[i]);
  168. addr[2 * 0x0555] = 0xAAAAAAAA;
  169. addr[2 * 0x02AA] = 0x55555555;
  170. addr[2 * 0x0555] = 0x90909090;
  171. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  172. addr[2 * 0x02AA + 1] = 0x55555555;
  173. addr[2 * 0x0555 + 1] = 0x90909090;
  174. udelay (1000);
  175. base = RD_SWP32(&addr[4]);
  176. base |= RD_SWP32(&addr[5]);
  177. info->protect[i] = base & 0x00010001 ? 1 : 0;
  178. }
  179. addr = (vu_long*)info->start[0];
  180. out:
  181. /* reset command */
  182. addr[0] = 0xf0f0f0f0;
  183. addr[1] = 0xf0f0f0f0;
  184. return info->size;
  185. }
  186. /*-----------------------------------------------------------------------
  187. */
  188. int flash_erase (flash_info_t *info, int s_first, int s_last)
  189. {
  190. vu_long *addr = (vu_long*)(info->start[0]);
  191. int flag, prot, sect, l_sect;
  192. ulong start, now, last;
  193. if ((s_first < 0) || (s_first > s_last)) {
  194. if (info->flash_id == FLASH_UNKNOWN) {
  195. printf ("- missing\n");
  196. } else {
  197. printf ("- no sectors to erase\n");
  198. }
  199. return 1;
  200. }
  201. prot = 0;
  202. for (sect = s_first; sect <= s_last; sect++) {
  203. if (info->protect[sect]) {
  204. prot++;
  205. }
  206. }
  207. if (prot) {
  208. printf ("- Warning: %d protected sectors will not be erased!\n",
  209. prot);
  210. } else {
  211. printf ("\n");
  212. }
  213. l_sect = -1;
  214. /* Disable interrupts which might cause a timeout here */
  215. flag = disable_interrupts();
  216. addr[2 * 0x0555] = 0xAAAAAAAA;
  217. addr[2 * 0x02AA] = 0x55555555;
  218. addr[2 * 0x0555] = 0x80808080;
  219. addr[2 * 0x0555] = 0xAAAAAAAA;
  220. addr[2 * 0x02AA] = 0x55555555;
  221. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  222. addr[2 * 0x02AA + 1] = 0x55555555;
  223. addr[2 * 0x0555 + 1] = 0x80808080;
  224. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  225. addr[2 * 0x02AA + 1] = 0x55555555;
  226. udelay (100);
  227. /* Start erase on unprotected sectors */
  228. for (sect = s_first; sect<=s_last; sect++) {
  229. if (info->protect[sect] == 0) { /* not protected */
  230. addr = (vu_long*)(info->start[sect]);
  231. addr[0] = 0x30303030;
  232. addr[1] = 0x30303030;
  233. l_sect = sect;
  234. }
  235. }
  236. /* re-enable interrupts if necessary */
  237. if (flag)
  238. enable_interrupts();
  239. /* wait at least 80us - let's wait 1 ms */
  240. udelay (1000);
  241. /*
  242. * We wait for the last triggered sector
  243. */
  244. if (l_sect < 0)
  245. goto DONE;
  246. start = get_timer (0);
  247. last = start;
  248. addr = (vu_long*)(info->start[l_sect]);
  249. while ( (addr[0] & 0x80808080) != 0x80808080 ||
  250. (addr[1] & 0x80808080) != 0x80808080) {
  251. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  252. printf ("Timeout\n");
  253. return 1;
  254. }
  255. /* show that we're waiting */
  256. if ((now - last) > 1000) { /* every second */
  257. serial_putc ('.');
  258. last = now;
  259. }
  260. }
  261. DONE:
  262. /* reset to read mode */
  263. addr = (volatile unsigned long *)info->start[0];
  264. addr[0] = 0xF0F0F0F0; /* reset bank */
  265. addr[1] = 0xF0F0F0F0; /* reset bank */
  266. printf (" done\n");
  267. return 0;
  268. }
  269. /*-----------------------------------------------------------------------
  270. * Copy memory to flash, returns:
  271. * 0 - OK
  272. * 1 - write timeout
  273. * 2 - Flash not erased
  274. */
  275. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  276. {
  277. ulong cp, wp, data;
  278. int i, l, rc;
  279. wp = (addr & ~3); /* get lower word aligned address */
  280. /*
  281. * handle unaligned start bytes
  282. */
  283. if ((l = addr - wp) != 0) {
  284. data = 0;
  285. for (i=0, cp=wp; i<l; ++i, ++cp) {
  286. data = (data << 8) | (*(uchar *)cp);
  287. }
  288. for (; i<4 && cnt>0; ++i) {
  289. data = (data << 8) | *src++;
  290. --cnt;
  291. ++cp;
  292. }
  293. for (; cnt==0 && i<4; ++i, ++cp) {
  294. data = (data << 8) | (*(uchar *)cp);
  295. }
  296. if ((rc = write_word(info, wp, data)) != 0) {
  297. return (rc);
  298. }
  299. wp += 4;
  300. }
  301. /*
  302. * handle word aligned part
  303. */
  304. while (cnt >= 4) {
  305. data = 0;
  306. for (i=0; i<4; ++i) {
  307. data = (data << 8) | *src++;
  308. }
  309. if ((rc = write_word(info, wp, data)) != 0) {
  310. return (rc);
  311. }
  312. wp += 4;
  313. cnt -= 4;
  314. }
  315. if (cnt == 0) {
  316. return (0);
  317. }
  318. /*
  319. * handle unaligned tail bytes
  320. */
  321. data = 0;
  322. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  323. data = (data << 8) | *src++;
  324. --cnt;
  325. }
  326. for (; i<4; ++i, ++cp) {
  327. data = (data << 8) | (*(uchar *)cp);
  328. }
  329. return (write_word(info, wp, data));
  330. }
  331. /*-----------------------------------------------------------------------
  332. * Write a word to Flash, returns:
  333. * 0 - OK
  334. * 1 - write timeout
  335. * 2 - Flash not erased
  336. */
  337. static int write_word (flash_info_t *info, ulong dest, ulong data)
  338. {
  339. vu_long *addr = (vu_long*)(info->start[0]);
  340. ulong start;
  341. int flag;
  342. /* Check if Flash is (sufficiently) erased */
  343. if ((*((vu_long *)dest) & data) != data) {
  344. return (2);
  345. }
  346. /* Disable interrupts which might cause a timeout here */
  347. flag = disable_interrupts();
  348. if ((dest & 0x00000004) == 0) {
  349. addr[2 * 0x0555] = 0xAAAAAAAA;
  350. addr[2 * 0x02AA] = 0x55555555;
  351. addr[2 * 0x0555] = 0xA0A0A0A0;
  352. }
  353. else {
  354. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  355. addr[2 * 0x02AA + 1] = 0x55555555;
  356. addr[2 * 0x0555 + 1] = 0xA0A0A0A0;
  357. }
  358. *((vu_long *)dest) = data;
  359. /* re-enable interrupts if necessary */
  360. if (flag)
  361. enable_interrupts();
  362. /* data polling for D7 */
  363. start = get_timer (0);
  364. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  365. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  366. return (1);
  367. }
  368. }
  369. return (0);
  370. }
  371. /*-----------------------------------------------------------------------
  372. */