flash.c 10 KB

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