flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * (C) Copyright 2006 Embedded Artists AB <www.embeddedartists.com>
  3. *
  4. * (C) Copyright 2007 Gary Jennejohn garyj@denx.de
  5. * Modified to use the routines in cpu/arm720t/lpc2292/flash.c.
  6. * Heavily modified to support the SMN42 board from Siemens
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/arch/hardware.h>
  26. static unsigned long flash_addr_table[CONFIG_SYS_MAX_FLASH_BANKS] = CONFIG_SYS_FLASH_BANKS_LIST;
  27. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  28. extern int lpc2292_copy_buffer_to_flash(flash_info_t *, ulong);
  29. extern int lpc2292_flash_erase(flash_info_t *, int, int);
  30. extern int lpc2292_write_buff (flash_info_t *, uchar *, ulong, ulong);
  31. static unsigned long ext_flash_init(void);
  32. static int ext_flash_erase(flash_info_t *, int, int);
  33. static int ext_write_buff(flash_info_t *, uchar *, ulong, ulong);
  34. /*-----------------------------------------------------------------------
  35. */
  36. ulong flash_init (void)
  37. {
  38. int j, k;
  39. ulong size = 0;
  40. ulong flashbase = 0;
  41. flash_info[0].flash_id = PHILIPS_LPC2292;
  42. flash_info[0].size = 0x003E000; /* 256 - 8 KB */
  43. flash_info[0].sector_count = 17;
  44. memset (flash_info[0].protect, 0, 17);
  45. flashbase = 0x00000000;
  46. for (j = 0, k = 0; j < 8; j++, k++) {
  47. flash_info[0].start[k] = flashbase;
  48. flashbase += 0x00002000;
  49. }
  50. for (j = 0; j < 2; j++, k++) {
  51. flash_info[0].start[k] = flashbase;
  52. flashbase += 0x00010000;
  53. }
  54. for (j = 0; j < 7; j++, k++) {
  55. flash_info[0].start[k] = flashbase;
  56. flashbase += 0x00002000;
  57. }
  58. size += flash_info[0].size;
  59. /* Protect monitor and environment sectors */
  60. flash_protect (FLAG_PROTECT_SET,
  61. 0x0,
  62. 0x0 + monitor_flash_len - 1,
  63. &flash_info[0]);
  64. flash_protect (FLAG_PROTECT_SET,
  65. CONFIG_ENV_ADDR,
  66. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  67. &flash_info[0]);
  68. size += ext_flash_init();
  69. return size;
  70. }
  71. /*-----------------------------------------------------------------------
  72. */
  73. void flash_print_info (flash_info_t * info)
  74. {
  75. int i;
  76. int erased = 0;
  77. unsigned long j;
  78. unsigned long count;
  79. unsigned char *p;
  80. switch (info->flash_id & FLASH_VENDMASK) {
  81. case (PHILIPS_LPC2292 & FLASH_VENDMASK):
  82. printf("Philips: ");
  83. break;
  84. case FLASH_MAN_AMD:
  85. printf("AMD: ");
  86. break;
  87. default:
  88. printf ("Unknown Vendor ");
  89. break;
  90. }
  91. switch (info->flash_id & FLASH_TYPEMASK) {
  92. case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
  93. printf("LPC2292 internal flash\n");
  94. break;
  95. case FLASH_S29GL128N:
  96. printf ("S29GL128N (128 Mbit, uniform sector size)\n");
  97. break;
  98. default:
  99. printf("Unknown Chip Type\n");
  100. return;
  101. }
  102. printf (" Size: %ld KB in %d Sectors\n",
  103. info->size >> 10, info->sector_count);
  104. printf (" Sector Start Addresses:");
  105. for (i = 0; i < info->sector_count; i++) {
  106. if ((i % 5) == 0) {
  107. printf ("\n ");
  108. }
  109. if (i < (info->sector_count - 1)) {
  110. count = info->start[i+1] - info->start[i];
  111. }
  112. else {
  113. count = info->start[0] + info->size - info->start[i];
  114. }
  115. p = (unsigned char*)(info->start[i]);
  116. erased = 1;
  117. for (j = 0; j < count; j++) {
  118. if (*p != 0xFF) {
  119. erased = 0;
  120. break;
  121. }
  122. p++;
  123. }
  124. printf (" %08lX%s%s", info->start[i], info->protect[i] ? " RO" : " ",
  125. erased ? " E" : " ");
  126. }
  127. printf ("\n");
  128. }
  129. int flash_erase (flash_info_t * info, int s_first, int s_last)
  130. {
  131. switch (info->flash_id & FLASH_TYPEMASK) {
  132. case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
  133. return lpc2292_flash_erase(info, s_first, s_last);
  134. case FLASH_S29GL128N:
  135. return ext_flash_erase(info, s_first, s_last);
  136. default:
  137. return ERR_PROTECTED;
  138. }
  139. return ERR_PROTECTED;
  140. }
  141. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  142. {
  143. switch (info->flash_id & FLASH_TYPEMASK) {
  144. case (PHILIPS_LPC2292 & FLASH_TYPEMASK):
  145. return lpc2292_write_buff(info, src, addr, cnt);
  146. case FLASH_S29GL128N:
  147. return ext_write_buff(info, src, addr, cnt);
  148. default:
  149. return ERR_PROG_ERROR;
  150. }
  151. return ERR_PROG_ERROR;
  152. }
  153. /*--------------------------------------------------------------------------
  154. * From here on is code for the external S29GL128N taken from cam5200_flash.c
  155. */
  156. #define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
  157. static int wait_for_DQ7_32(flash_info_t * info, int sect)
  158. {
  159. ulong start, now, last;
  160. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
  161. (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  162. start = get_timer(0);
  163. last = start;
  164. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  165. (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
  166. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  167. printf("Timeout\n");
  168. return -1;
  169. }
  170. /* show that we're waiting */
  171. if ((now - last) > 1000) { /* every second */
  172. putc('.');
  173. last = now;
  174. }
  175. }
  176. return 0;
  177. }
  178. int ext_flash_erase(flash_info_t * info, int s_first, int s_last)
  179. {
  180. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  181. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  182. int flag, prot, sect, l_sect, ret;
  183. ret = 0;
  184. if ((s_first < 0) || (s_first > s_last)) {
  185. if (info->flash_id == FLASH_UNKNOWN)
  186. printf("- missing\n");
  187. else
  188. printf("- no sectors to erase\n");
  189. return 1;
  190. }
  191. if (info->flash_id == FLASH_UNKNOWN) {
  192. printf("Can't erase unknown flash type - aborted\n");
  193. return 1;
  194. }
  195. prot = 0;
  196. for (sect = s_first; sect <= s_last; ++sect) {
  197. if (info->protect[sect])
  198. prot++;
  199. }
  200. if (prot)
  201. printf("- Warning: %d protected sectors will not be erased!", prot);
  202. printf("\n");
  203. l_sect = -1;
  204. /* Disable interrupts which might cause a timeout here */
  205. flag = disable_interrupts();
  206. /* Start erase on unprotected sectors */
  207. for (sect = s_first; sect <= s_last; sect++) {
  208. if (info->protect[sect] == 0) { /* not protected */
  209. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  210. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  211. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  212. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  213. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  214. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  215. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  216. l_sect = sect;
  217. /*
  218. * Wait for each sector to complete, it's more
  219. * reliable. According to AMD Spec, you must
  220. * issue all erase commands within a specified
  221. * timeout. This has been seen to fail, especially
  222. * if printf()s are included (for debug)!!
  223. */
  224. ret = wait_for_DQ7_32(info, sect);
  225. if (ret) {
  226. ret = ERR_PROTECTED;
  227. break;
  228. }
  229. }
  230. }
  231. /* re-enable interrupts if necessary */
  232. if (flag)
  233. enable_interrupts();
  234. /* wait at least 80us - let's wait 1 ms */
  235. udelay(1000);
  236. /* reset to read mode */
  237. addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
  238. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  239. if (ret)
  240. printf(" error\n");
  241. else
  242. printf(" done\n");
  243. return ret;
  244. }
  245. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  246. {
  247. short i;
  248. CONFIG_SYS_FLASH_WORD_SIZE value;
  249. ulong base = (ulong) addr;
  250. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
  251. /* Write auto select command: read Manufacturer ID */
  252. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  253. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  254. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
  255. udelay(1000);
  256. value = addr2[0];
  257. switch (value) {
  258. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
  259. info->flash_id = FLASH_MAN_AMD;
  260. break;
  261. default:
  262. info->flash_id = FLASH_UNKNOWN;
  263. info->sector_count = 0;
  264. info->size = 0;
  265. return (0); /* no or unknown flash */
  266. }
  267. value = addr2[1]; /* device ID */
  268. switch (value) {
  269. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_MIRROR:
  270. value = addr2[14];
  271. switch(value) {
  272. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_GL128N_2:
  273. value = addr2[15];
  274. if (value != (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_GL128N_3) {
  275. info->flash_id = FLASH_UNKNOWN;
  276. } else {
  277. info->flash_id += FLASH_S29GL128N;
  278. info->sector_count = 128;
  279. info->size = 0x01000000;
  280. }
  281. break;
  282. default:
  283. info->flash_id = FLASH_UNKNOWN;
  284. return(0);
  285. }
  286. break;
  287. default:
  288. info->flash_id = FLASH_UNKNOWN;
  289. return (0); /* => no or unknown flash */
  290. }
  291. /* set up sector start address table */
  292. for (i = 0; i < info->sector_count; i++)
  293. info->start[i] = base + (i * 0x00020000);
  294. /* check for protected sectors */
  295. for (i = 0; i < info->sector_count; i++) {
  296. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  297. /* D0 = 1 if protected */
  298. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  299. info->protect[i] = addr2[2] & 1;
  300. }
  301. /* issue bank reset to return to read mode */
  302. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  303. return (info->size);
  304. }
  305. static unsigned long ext_flash_init(void)
  306. {
  307. unsigned long total_b = 0;
  308. unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
  309. int i;
  310. /* Init: no FLASHes known */
  311. for (i = 1; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  312. flash_info[i].flash_id = FLASH_UNKNOWN;
  313. flash_info[i].sector_count = -1;
  314. flash_info[i].size = 0;
  315. /* call flash_get_size() to initialize sector address */
  316. size_b[i] = flash_get_size((vu_long *) flash_addr_table[i],
  317. &flash_info[i]);
  318. flash_info[i].size = size_b[i];
  319. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  320. printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  321. i+1, size_b[i], size_b[i] << 20);
  322. flash_info[i].sector_count = -1;
  323. flash_info[i].size = 0;
  324. }
  325. total_b += flash_info[i].size;
  326. }
  327. return total_b;
  328. }
  329. static int write_word(flash_info_t * info, ulong dest, ushort data)
  330. {
  331. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  332. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
  333. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) &data;
  334. ulong start;
  335. int flag;
  336. /* Check if Flash is (sufficiently) erased */
  337. if ((*dest2 & *data2) != *data2) {
  338. return (2);
  339. }
  340. /* Disable interrupts which might cause a timeout here */
  341. flag = disable_interrupts();
  342. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  343. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  344. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
  345. *dest2 = *data2;
  346. /* re-enable interrupts if necessary */
  347. if (flag)
  348. enable_interrupts();
  349. /* data polling for D7 */
  350. start = get_timer(0);
  351. while ((*dest2 & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  352. (*data2 & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
  353. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  354. printf("WRITE_TOUT\n");
  355. return (1);
  356. }
  357. }
  358. return (0);
  359. }
  360. /*-----------------------------------------------------------------------
  361. * This is taken from the original flash.c for the LPC2292 SODIMM board
  362. * and modified to suit.
  363. */
  364. int ext_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  365. {
  366. ushort tmp;
  367. ulong i;
  368. uchar* src_org;
  369. uchar* dst_org;
  370. ulong cnt_org = cnt;
  371. int ret = ERR_OK;
  372. src_org = src;
  373. dst_org = (uchar*)addr;
  374. if (addr & 1) { /* if odd address */
  375. tmp = *((uchar*)(addr - 1)); /* little endian */
  376. tmp |= (*src << 8);
  377. if (write_word(info, addr - 1, tmp))
  378. return ERR_PROG_ERROR;
  379. addr += 1;
  380. cnt -= 1;
  381. src++;
  382. }
  383. while (cnt > 1) {
  384. tmp = ((*(src+1)) << 8) + (*src); /* little endian */
  385. if (write_word(info, addr, tmp))
  386. return ERR_PROG_ERROR;
  387. addr += 2;
  388. src += 2;
  389. cnt -= 2;
  390. }
  391. if (cnt > 0) {
  392. tmp = (*((uchar*)(addr + 1))) << 8;
  393. tmp |= *src;
  394. if (write_word(info, addr, tmp))
  395. return ERR_PROG_ERROR;
  396. }
  397. for (i = 0; i < cnt_org; i++) {
  398. if (*dst_org != *src_org) {
  399. printf("Write failed. Byte %lX differs\n", i);
  400. ret = ERR_PROG_ERROR;
  401. break;
  402. }
  403. dst_org++;
  404. src_org++;
  405. }
  406. return ret;
  407. }