flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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[CFG_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<CFG_MAX_FLASH_BANKS; ++i) {
  49. flash_info[i].flash_id = FLASH_UNKNOWN;
  50. }
  51. /* for now, only support the 4 MB Flash SIMM */
  52. size = flash_get_size((vu_long *)CFG_FLASH0_BASE, &flash_info[0]);
  53. /*
  54. * protect monitor and environment sectors
  55. */
  56. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  57. flash_protect(FLAG_PROTECT_SET,
  58. CFG_MONITOR_BASE,
  59. CFG_MONITOR_BASE+monitor_flash_len-1,
  60. &flash_info[0]);
  61. #endif
  62. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  63. # ifndef CFG_ENV_SIZE
  64. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  65. # endif
  66. flash_protect(FLAG_PROTECT_SET,
  67. CFG_ENV_ADDR,
  68. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  69. &flash_info[0]);
  70. #endif
  71. return /*size*/ (CFG_FLASH0_SIZE * 1024 * 1024);
  72. }
  73. /*-----------------------------------------------------------------------
  74. */
  75. void flash_print_info (flash_info_t *info)
  76. {
  77. int i;
  78. if (info->flash_id == FLASH_UNKNOWN) {
  79. printf ("missing or unknown FLASH type\n");
  80. return;
  81. }
  82. switch (info->flash_id & FLASH_VENDMASK) {
  83. case (AMD_MANUFACT & FLASH_VENDMASK):
  84. printf ("AMD ");
  85. break;
  86. case (FUJ_MANUFACT & FLASH_VENDMASK):
  87. printf ("FUJITSU ");
  88. break;
  89. case (SST_MANUFACT & FLASH_VENDMASK):
  90. printf ("SST ");
  91. break;
  92. default:
  93. printf ("Unknown Vendor ");
  94. break;
  95. }
  96. switch (info->flash_id & FLASH_TYPEMASK) {
  97. case (AMD_ID_DL323B & FLASH_TYPEMASK):
  98. printf("AM29DL323B (32 MBit)\n");
  99. break;
  100. default:
  101. printf ("Unknown Chip Type\n");
  102. break;
  103. }
  104. printf (" Size: %ld MB in %d Sectors\n",
  105. info->size >> 20, info->sector_count);
  106. printf (" Sector Start Addresses:");
  107. for (i = 0; i < info->sector_count; ++i) {
  108. if ((i % 5) == 0) printf ("\n ");
  109. printf (" %08lX%s",
  110. info->start[i],
  111. info->protect[i] ? " (RO)" : " "
  112. );
  113. }
  114. printf ("\n");
  115. return;
  116. }
  117. /*
  118. * The following code cannot be run from FLASH!
  119. */
  120. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  121. {
  122. short i;
  123. vu_long vendor[2], devid[2];
  124. ulong base = (ulong)addr;
  125. /* Reset and Write auto select command: read Manufacturer ID */
  126. addr[0] = 0xf0f0f0f0;
  127. addr[2 * 0x0555] = 0xAAAAAAAA;
  128. addr[2 * 0x02AA] = 0x55555555;
  129. addr[2 * 0x0555] = 0x90909090;
  130. addr[1] = 0xf0f0f0f0;
  131. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  132. addr[2 * 0x02AA + 1] = 0x55555555;
  133. addr[2 * 0x0555 + 1] = 0x90909090;
  134. udelay (1000);
  135. vendor[0] = RD_SWP32(&addr[0]);
  136. vendor[1] = RD_SWP32(&addr[1]);
  137. if (vendor[0] != vendor[1] || vendor[0] != AMD_MANUFACT) {
  138. info->size = 0;
  139. goto out;
  140. }
  141. devid[0] = RD_SWP32(&addr[2]);
  142. devid[1] = RD_SWP32(&addr[3]);
  143. if (devid[0] == AMD_ID_DL323B) {
  144. /*
  145. * we have 2 Banks
  146. * Bank 1 (23 Sectors): 0-7=8kbyte, 8-22=64kbyte
  147. * Bank 2 (48 Sectors): 23-70=64kbyte
  148. */
  149. info->flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
  150. (AMD_ID_DL323B & FLASH_TYPEMASK);
  151. info->sector_count = 71;
  152. info->size = 4 * (8 * 8 + 63 * 64) * 1024;
  153. }
  154. else {
  155. info->size = 0;
  156. goto out;
  157. }
  158. /* set up sector start address table */
  159. for (i = 0; i < 8; i++) {
  160. info->start[i] = base + (i * 0x8000);
  161. }
  162. for (i = 8; i < info->sector_count; i++) {
  163. info->start[i] = base + (i * 0x40000) + 8 * 0x8000 - 8 * 0x40000;
  164. }
  165. /* check for protected sectors */
  166. for (i = 0; i < info->sector_count; i++) {
  167. /* read sector protection at sector address */
  168. addr = (volatile unsigned long *)(info->start[i]);
  169. addr[2 * 0x0555] = 0xAAAAAAAA;
  170. addr[2 * 0x02AA] = 0x55555555;
  171. addr[2 * 0x0555] = 0x90909090;
  172. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  173. addr[2 * 0x02AA + 1] = 0x55555555;
  174. addr[2 * 0x0555 + 1] = 0x90909090;
  175. udelay (1000);
  176. base = RD_SWP32(&addr[4]);
  177. base |= RD_SWP32(&addr[5]);
  178. info->protect[i] = base & 0x00010001 ? 1 : 0;
  179. }
  180. addr = (vu_long*)info->start[0];
  181. out:
  182. /* reset command */
  183. addr[0] = 0xf0f0f0f0;
  184. addr[1] = 0xf0f0f0f0;
  185. return info->size;
  186. }
  187. /*-----------------------------------------------------------------------
  188. */
  189. int flash_erase (flash_info_t *info, int s_first, int s_last)
  190. {
  191. vu_long *addr = (vu_long*)(info->start[0]);
  192. int flag, prot, sect, l_sect;
  193. ulong start, now, last;
  194. if ((s_first < 0) || (s_first > s_last)) {
  195. if (info->flash_id == FLASH_UNKNOWN) {
  196. printf ("- missing\n");
  197. } else {
  198. printf ("- no sectors to erase\n");
  199. }
  200. return 1;
  201. }
  202. prot = 0;
  203. for (sect = s_first; sect <= s_last; sect++) {
  204. if (info->protect[sect]) {
  205. prot++;
  206. }
  207. }
  208. if (prot) {
  209. printf ("- Warning: %d protected sectors will not be erased!\n",
  210. prot);
  211. } else {
  212. printf ("\n");
  213. }
  214. l_sect = -1;
  215. /* Disable interrupts which might cause a timeout here */
  216. flag = disable_interrupts();
  217. addr[2 * 0x0555] = 0xAAAAAAAA;
  218. addr[2 * 0x02AA] = 0x55555555;
  219. addr[2 * 0x0555] = 0x80808080;
  220. addr[2 * 0x0555] = 0xAAAAAAAA;
  221. addr[2 * 0x02AA] = 0x55555555;
  222. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  223. addr[2 * 0x02AA + 1] = 0x55555555;
  224. addr[2 * 0x0555 + 1] = 0x80808080;
  225. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  226. addr[2 * 0x02AA + 1] = 0x55555555;
  227. udelay (100);
  228. /* Start erase on unprotected sectors */
  229. for (sect = s_first; sect<=s_last; sect++) {
  230. if (info->protect[sect] == 0) { /* not protected */
  231. addr = (vu_long*)(info->start[sect]);
  232. addr[0] = 0x30303030;
  233. addr[1] = 0x30303030;
  234. l_sect = sect;
  235. }
  236. }
  237. /* re-enable interrupts if necessary */
  238. if (flag)
  239. enable_interrupts();
  240. /* wait at least 80us - let's wait 1 ms */
  241. udelay (1000);
  242. /*
  243. * We wait for the last triggered sector
  244. */
  245. if (l_sect < 0)
  246. goto DONE;
  247. start = get_timer (0);
  248. last = start;
  249. addr = (vu_long*)(info->start[l_sect]);
  250. while ( (addr[0] & 0x80808080) != 0x80808080 ||
  251. (addr[1] & 0x80808080) != 0x80808080) {
  252. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  253. printf ("Timeout\n");
  254. return 1;
  255. }
  256. /* show that we're waiting */
  257. if ((now - last) > 1000) { /* every second */
  258. serial_putc ('.');
  259. last = now;
  260. }
  261. }
  262. DONE:
  263. /* reset to read mode */
  264. addr = (volatile unsigned long *)info->start[0];
  265. addr[0] = 0xF0F0F0F0; /* reset bank */
  266. addr[1] = 0xF0F0F0F0; /* reset bank */
  267. printf (" done\n");
  268. return 0;
  269. }
  270. /*-----------------------------------------------------------------------
  271. * Copy memory to flash, returns:
  272. * 0 - OK
  273. * 1 - write timeout
  274. * 2 - Flash not erased
  275. */
  276. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  277. {
  278. ulong cp, wp, data;
  279. int i, l, rc;
  280. wp = (addr & ~3); /* get lower word aligned address */
  281. /*
  282. * handle unaligned start bytes
  283. */
  284. if ((l = addr - wp) != 0) {
  285. data = 0;
  286. for (i=0, cp=wp; i<l; ++i, ++cp) {
  287. data = (data << 8) | (*(uchar *)cp);
  288. }
  289. for (; i<4 && cnt>0; ++i) {
  290. data = (data << 8) | *src++;
  291. --cnt;
  292. ++cp;
  293. }
  294. for (; cnt==0 && i<4; ++i, ++cp) {
  295. data = (data << 8) | (*(uchar *)cp);
  296. }
  297. if ((rc = write_word(info, wp, data)) != 0) {
  298. return (rc);
  299. }
  300. wp += 4;
  301. }
  302. /*
  303. * handle word aligned part
  304. */
  305. while (cnt >= 4) {
  306. data = 0;
  307. for (i=0; i<4; ++i) {
  308. data = (data << 8) | *src++;
  309. }
  310. if ((rc = write_word(info, wp, data)) != 0) {
  311. return (rc);
  312. }
  313. wp += 4;
  314. cnt -= 4;
  315. }
  316. if (cnt == 0) {
  317. return (0);
  318. }
  319. /*
  320. * handle unaligned tail bytes
  321. */
  322. data = 0;
  323. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  324. data = (data << 8) | *src++;
  325. --cnt;
  326. }
  327. for (; i<4; ++i, ++cp) {
  328. data = (data << 8) | (*(uchar *)cp);
  329. }
  330. return (write_word(info, wp, data));
  331. }
  332. /*-----------------------------------------------------------------------
  333. * Write a word to Flash, returns:
  334. * 0 - OK
  335. * 1 - write timeout
  336. * 2 - Flash not erased
  337. */
  338. static int write_word (flash_info_t *info, ulong dest, ulong data)
  339. {
  340. vu_long *addr = (vu_long*)(info->start[0]);
  341. ulong start;
  342. int flag;
  343. /* Check if Flash is (sufficiently) erased */
  344. if ((*((vu_long *)dest) & data) != data) {
  345. return (2);
  346. }
  347. /* Disable interrupts which might cause a timeout here */
  348. flag = disable_interrupts();
  349. if ((dest & 0x00000004) == 0) {
  350. addr[2 * 0x0555] = 0xAAAAAAAA;
  351. addr[2 * 0x02AA] = 0x55555555;
  352. addr[2 * 0x0555] = 0xA0A0A0A0;
  353. }
  354. else {
  355. addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
  356. addr[2 * 0x02AA + 1] = 0x55555555;
  357. addr[2 * 0x0555 + 1] = 0xA0A0A0A0;
  358. }
  359. *((vu_long *)dest) = data;
  360. /* re-enable interrupts if necessary */
  361. if (flag)
  362. enable_interrupts();
  363. /* data polling for D7 */
  364. start = get_timer (0);
  365. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  366. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  367. return (1);
  368. }
  369. }
  370. return (0);
  371. }
  372. /*-----------------------------------------------------------------------
  373. */