flash.c 9.3 KB

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