flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * (C) Copyright 2001
  3. * Stäubli Faverges - <www.staubli.com>
  4. * Pierre AUBERT p.aubert@staubli.com
  5. * U-Boot port on RPXClassic LF (CLLF_BW31) board
  6. *
  7. * RPXClassic uses Am29DL323B flash memory with 2 banks
  8. *
  9. *
  10. * (C) Copyright 2000
  11. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. flash_info_t flash_info[CONFIG_SYS_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. static void flash_get_offsets (ulong base, flash_info_t *info);
  40. /*-----------------------------------------------------------------------
  41. */
  42. unsigned long flash_init (void)
  43. {
  44. unsigned long size_b0 ;
  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. }
  50. size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  51. flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  52. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  53. /* monitor protection ON by default */
  54. flash_protect(FLAG_PROTECT_SET,
  55. CONFIG_SYS_MONITOR_BASE,
  56. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  57. &flash_info[0]);
  58. #endif
  59. flash_info[0].size = size_b0;
  60. return (size_b0);
  61. }
  62. /*-----------------------------------------------------------------------
  63. */
  64. static void flash_get_offsets (ulong base, flash_info_t *info)
  65. {
  66. int i;
  67. if (info->flash_id & FLASH_BTYPE) {
  68. /* set sector offsets for bottom boot block type */
  69. info->start[0] = base + 0x00000000;
  70. info->start[1] = base + 0x00008000;
  71. info->start[2] = base + 0x00010000;
  72. info->start[3] = base + 0x00018000;
  73. info->start[4] = base + 0x00020000;
  74. info->start[5] = base + 0x00028000;
  75. info->start[6] = base + 0x00030000;
  76. info->start[7] = base + 0x00038000;
  77. for (i = 8; i < info->sector_count; i++) {
  78. info->start[i] = base + ((i-7) * 0x00040000) ;
  79. }
  80. }
  81. }
  82. /*-----------------------------------------------------------------------
  83. */
  84. void flash_print_info (flash_info_t *info)
  85. {
  86. int i;
  87. if (info->flash_id == FLASH_UNKNOWN) {
  88. printf ("missing or unknown FLASH type\n");
  89. return;
  90. }
  91. switch (info->flash_id & FLASH_VENDMASK) {
  92. case FLASH_MAN_AMD: printf ("AMD "); break;
  93. default: printf ("Unknown Vendor "); break;
  94. }
  95. switch (info->flash_id & FLASH_TYPEMASK) {
  96. case FLASH_AMDL323B:
  97. printf ("AMDL323DB (16 Mbytes, bottom boot sect)\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)
  108. printf ("\n ");
  109. printf (" %08lX%s",
  110. info->start[i],
  111. info->protect[i] ? " (RO)" : " "
  112. );
  113. }
  114. printf ("\n");
  115. }
  116. /*-----------------------------------------------------------------------
  117. */
  118. /*-----------------------------------------------------------------------
  119. */
  120. /*
  121. * The following code cannot be run from FLASH!
  122. */
  123. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  124. {
  125. short i;
  126. ulong value;
  127. ulong base = (ulong)addr;
  128. /* Reset flash componeny */
  129. addr [0] = 0xf0f0f0f0;
  130. /* Write auto select command: read Manufacturer ID */
  131. addr[0xAAA] = 0xAAAAAAAA ;
  132. addr[0x555] = 0x55555555 ;
  133. addr[0xAAA] = 0x90909090 ;
  134. value = addr[0] ;
  135. switch (value & 0x00FF00FF) {
  136. case AMD_MANUFACT:
  137. info->flash_id = FLASH_MAN_AMD;
  138. break;
  139. default:
  140. info->flash_id = FLASH_UNKNOWN;
  141. info->sector_count = 0;
  142. info->size = 0;
  143. return (0); /* no or unknown flash */
  144. }
  145. value = addr[2] ; /* device ID */
  146. switch (value & 0x00FF00FF) {
  147. case (AMD_ID_DL323B & 0x00FF00FF):
  148. info->flash_id += FLASH_AMDL323B;
  149. info->sector_count = 71;
  150. info->size = 0x01000000; /* 16 Mb */
  151. break;
  152. default:
  153. info->flash_id = FLASH_UNKNOWN;
  154. return (0); /* => no or unknown flash */
  155. }
  156. /* set up sector start address table */
  157. /* set sector offsets for bottom boot block type */
  158. info->start[0] = base + 0x00000000;
  159. info->start[1] = base + 0x00008000;
  160. info->start[2] = base + 0x00010000;
  161. info->start[3] = base + 0x00018000;
  162. info->start[4] = base + 0x00020000;
  163. info->start[5] = base + 0x00028000;
  164. info->start[6] = base + 0x00030000;
  165. info->start[7] = base + 0x00038000;
  166. for (i = 8; i < info->sector_count; i++) {
  167. info->start[i] = base + ((i-7) * 0x00040000) ;
  168. }
  169. /* check for protected sectors */
  170. for (i = 0; i < 23; i++) {
  171. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  172. /* D0 = 1 if protected */
  173. addr = (volatile unsigned long *)(info->start[i]);
  174. info->protect[i] = addr[4] & 1 ;
  175. }
  176. /* Check for protected sectors in the 2nd bank */
  177. addr[0x100AAA] = 0xAAAAAAAA ;
  178. addr[0x100555] = 0x55555555 ;
  179. addr[0x100AAA] = 0x90909090 ;
  180. for (i = 23; i < info->sector_count; i++) {
  181. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  182. /* D0 = 1 if protected */
  183. addr = (volatile unsigned long *)(info->start[i]);
  184. info->protect[i] = addr[4] & 1 ;
  185. }
  186. /*
  187. * Prevent writes to uninitialized FLASH.
  188. */
  189. if (info->flash_id != FLASH_UNKNOWN) {
  190. addr = (volatile unsigned long *)info->start[0];
  191. *addr = 0xF0F0F0F0; /* reset bank 1 */
  192. addr = (volatile unsigned long *)info->start[23];
  193. *addr = 0xF0F0F0F0; /* reset bank 2 */
  194. }
  195. return (info->size);
  196. }
  197. /*-----------------------------------------------------------------------
  198. */
  199. int flash_erase (flash_info_t *info, int s_first, int s_last)
  200. {
  201. vu_long *addr = (vu_long*)(info->start[0]);
  202. int flag, prot, sect, l_sect;
  203. ulong start, now, last;
  204. if ((s_first < 0) || (s_first > s_last)) {
  205. if (info->flash_id == FLASH_UNKNOWN) {
  206. printf ("- missing\n");
  207. } else {
  208. printf ("- no sectors to erase\n");
  209. }
  210. return 1;
  211. }
  212. if ((info->flash_id == FLASH_UNKNOWN) ||
  213. (info->flash_id > FLASH_AMD_COMP)) {
  214. printf ("Can't erase unknown flash type %08lx - aborted\n",
  215. info->flash_id);
  216. return 1;
  217. }
  218. prot = 0;
  219. for (sect=s_first; sect<=s_last; ++sect) {
  220. if (info->protect[sect]) {
  221. prot++;
  222. }
  223. }
  224. if (prot) {
  225. printf ("- Warning: %d protected sectors will not be erased!\n",
  226. prot);
  227. } else {
  228. printf ("\n");
  229. }
  230. l_sect = -1;
  231. /* Disable interrupts which might cause a timeout here */
  232. flag = disable_interrupts();
  233. addr[0xAAA] = 0xAAAAAAAA;
  234. addr[0x555] = 0x55555555;
  235. addr[0xAAA] = 0x80808080;
  236. addr[0xAAA] = 0xAAAAAAAA;
  237. addr[0x555] = 0x55555555;
  238. /* Start erase on unprotected sectors */
  239. for (sect = s_first; sect<=s_last; sect++) {
  240. if (info->protect[sect] == 0) { /* not protected */
  241. addr = (vu_long *)(info->start[sect]) ;
  242. addr[0] = 0x30303030 ;
  243. l_sect = sect;
  244. }
  245. }
  246. /* re-enable interrupts if necessary */
  247. if (flag)
  248. enable_interrupts();
  249. /* wait at least 80us - let's wait 1 ms */
  250. udelay (1000);
  251. /*
  252. * We wait for the last triggered sector
  253. */
  254. if (l_sect < 0)
  255. goto DONE;
  256. start = get_timer (0);
  257. last = start;
  258. addr = (vu_long *)(info->start[l_sect]);
  259. while ((addr[0] & 0x80808080) != 0x80808080) {
  260. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  261. printf ("Timeout\n");
  262. return 1;
  263. }
  264. /* show that we're waiting */
  265. if ((now - last) > 1000) { /* every second */
  266. putc ('.');
  267. last = now;
  268. }
  269. }
  270. DONE:
  271. /* reset to read mode */
  272. addr = (vu_long *)info->start[0];
  273. addr[0] = 0xF0F0F0F0; /* reset bank */
  274. printf (" done\n");
  275. return 0;
  276. }
  277. /*-----------------------------------------------------------------------
  278. * Copy memory to flash, returns:
  279. * 0 - OK
  280. * 1 - write timeout
  281. * 2 - Flash not erased
  282. */
  283. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  284. {
  285. ulong cp, wp, data;
  286. int i, l, rc;
  287. wp = (addr & ~3); /* get lower word aligned address */
  288. /*
  289. * handle unaligned start bytes
  290. */
  291. if ((l = addr - wp) != 0) {
  292. data = 0;
  293. for (i=0, cp=wp; i<l; ++i, ++cp) {
  294. data = (data << 8) | (*(uchar *)cp);
  295. }
  296. for (; i<4 && cnt>0; ++i) {
  297. data = (data << 8) | *src++;
  298. --cnt;
  299. ++cp;
  300. }
  301. for (; cnt==0 && i<4; ++i, ++cp) {
  302. data = (data << 8) | (*(uchar *)cp);
  303. }
  304. if ((rc = write_word(info, wp, data)) != 0) {
  305. return (rc);
  306. }
  307. wp += 4;
  308. }
  309. /*
  310. * handle word aligned part
  311. */
  312. while (cnt >= 4) {
  313. data = 0;
  314. for (i=0; i<4; ++i) {
  315. data = (data << 8) | *src++;
  316. }
  317. if ((rc = write_word(info, wp, data)) != 0) {
  318. return (rc);
  319. }
  320. wp += 4;
  321. cnt -= 4;
  322. }
  323. if (cnt == 0) {
  324. return (0);
  325. }
  326. /*
  327. * handle unaligned tail bytes
  328. */
  329. data = 0;
  330. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  331. data = (data << 8) | *src++;
  332. --cnt;
  333. }
  334. for (; i<4; ++i, ++cp) {
  335. data = (data << 8) | (*(uchar *)cp);
  336. }
  337. return (write_word(info, wp, data));
  338. }
  339. /*-----------------------------------------------------------------------
  340. * Write a word to Flash, returns:
  341. * 0 - OK
  342. * 1 - write timeout
  343. * 2 - Flash not erased
  344. */
  345. static int write_word (flash_info_t *info, ulong dest, ulong data)
  346. {
  347. vu_long *addr = (vu_long *)(info->start[0]);
  348. ulong start;
  349. int flag;
  350. /* Check if Flash is (sufficiently) erased */
  351. if ((*((vu_long *)dest) & data) != data) {
  352. return (2);
  353. }
  354. /* Disable interrupts which might cause a timeout here */
  355. flag = disable_interrupts();
  356. addr[0xAAA] = 0xAAAAAAAA;
  357. addr[0x555] = 0x55555555;
  358. addr[0xAAA] = 0xA0A0A0A0;
  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) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  367. return (1);
  368. }
  369. }
  370. return (0);
  371. }
  372. /*-----------------------------------------------------------------------
  373. */