flash.c 12 KB

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