flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001-2004
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * (C) Copyright 2003
  9. * Texas Instruments, <www.ti.com>
  10. * Kshitij Gupta <Kshitij@ti.com>
  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 <asm/arch/sizes.h>
  32. #include <linux/byteorder/swab.h>
  33. #define PHYS_FLASH_SECT_SIZE SZ_128K
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. /* Board support for 1 or 2 flash devices */
  36. #undef FLASH_PORT_WIDTH32
  37. #define FLASH_PORT_WIDTH16
  38. #ifdef FLASH_PORT_WIDTH16
  39. # define FLASH_PORT_WIDTH ushort
  40. # define FLASH_PORT_WIDTHV vu_short
  41. # define SWAP(x) __swab16(x)
  42. #else
  43. # define FLASH_PORT_WIDTH ulong
  44. # define FLASH_PORT_WIDTHV vu_long
  45. # define SWAP(x) __swab32(x)
  46. #endif
  47. #define FPW FLASH_PORT_WIDTH
  48. #define FPWV FLASH_PORT_WIDTHV
  49. #define mb() __asm__ __volatile__ ("" : : : "memory")
  50. /* Flash Organization Structure */
  51. typedef struct OrgDef {
  52. unsigned int sector_number;
  53. unsigned int sector_size;
  54. } OrgDef;
  55. /* Flash Organizations */
  56. OrgDef OrgIntel_28F256L18T[] = {
  57. {4, SZ_32K}, /* 4 * 32kBytes sectors */
  58. {255, SZ_128K}, /* 255 * 128kBytes sectors */
  59. };
  60. /*-----------------------------------------------------------------------
  61. * Functions
  62. */
  63. unsigned long flash_init (void);
  64. static ulong flash_get_size (FPW * addr, flash_info_t * info);
  65. static int write_data (flash_info_t * info, ulong dest, FPW data);
  66. static void flash_get_offsets (ulong base, flash_info_t * info);
  67. void inline spin_wheel (void);
  68. void flash_print_info (flash_info_t * info);
  69. void flash_unprotect_sectors (FPWV * addr);
  70. int flash_erase (flash_info_t * info, int s_first, int s_last);
  71. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
  72. void flash_unlock(flash_info_t * info, int bank);
  73. int flash_probe(void);
  74. /*-----------------------------------------------------------------------
  75. */
  76. /* see if flash is ok */
  77. int flash_probe(void)
  78. {
  79. return(flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[0]));
  80. }
  81. unsigned long flash_init (void)
  82. {
  83. int i;
  84. ulong size = 0;
  85. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  86. switch (i) {
  87. case 0:
  88. flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
  89. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  90. /* to reset the lock bit */
  91. flash_unlock(&flash_info[i],i);
  92. break;
  93. case 1:
  94. flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
  95. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  96. /* to reset the lock bit */
  97. flash_unlock(&flash_info[i],i);
  98. break;
  99. default:
  100. panic ("configured too many flash banks!\n");
  101. break;
  102. }
  103. size += flash_info[i].size;
  104. }
  105. /* Protect monitor and environment sectors
  106. */
  107. flash_protect (FLAG_PROTECT_SET,
  108. CFG_FLASH_BASE,
  109. CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
  110. flash_protect (FLAG_PROTECT_SET,
  111. CFG_ENV_ADDR,
  112. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  113. return size;
  114. }
  115. /*-----------------------------------------------------------------------
  116. */
  117. void flash_unlock(flash_info_t * info, int bank)
  118. {
  119. int j;
  120. if (!bank)
  121. j=2; /* leave 0,1 locked for boot bank */
  122. else
  123. j=0; /* get the whole bank for #2 */
  124. for (;j<CFG_MAX_FLASH_SECT;j++) {
  125. FPWV *addr = (FPWV *) (info->start[j]);
  126. if (addr == NULL) {
  127. printf("Warning Flash probe failed\n");
  128. break;
  129. }
  130. flash_unprotect_sectors (addr);
  131. *addr = (FPW) 0x00500050;/* clear status register */
  132. *addr = (FPW) 0x00FF00FF;/* resest to read mode */
  133. }
  134. }
  135. /*-----------------------------------------------------------------------
  136. */
  137. static void flash_get_offsets (ulong base, flash_info_t * info)
  138. {
  139. int i;
  140. volatile int r; /* gcc 3.4.0-1 strangeness, need to follow up.*/
  141. if (info->flash_id == FLASH_UNKNOWN) {
  142. return;
  143. }
  144. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  145. for (i = 0; i < info->sector_count; i++) {
  146. if (i > 254) { /* 255,256,257,258 */
  147. r=i;
  148. info->start[i] = base + (((r-(int)255) * SZ_32K) + (255*PHYS_FLASH_SECT_SIZE));
  149. info->protect[i] = 0;
  150. } else {
  151. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  152. info->protect[i] = 0;
  153. }
  154. }
  155. }
  156. }
  157. /*-----------------------------------------------------------------------
  158. */
  159. void flash_print_info (flash_info_t * info)
  160. {
  161. int i;
  162. if (info->flash_id == FLASH_UNKNOWN) {
  163. printf ("missing or unknown FLASH type\n");
  164. return;
  165. }
  166. switch (info->flash_id & FLASH_VENDMASK) {
  167. case FLASH_MAN_INTEL:
  168. printf ("INTEL ");
  169. break;
  170. default:
  171. printf ("Unknown Vendor ");
  172. break;
  173. }
  174. switch (info->flash_id & FLASH_TYPEMASK) {
  175. case FLASH_28F256L18T:
  176. printf ("FLASH 28F256L18T\n");
  177. break;
  178. default:
  179. printf ("Unknown Chip Type\n");
  180. break;
  181. }
  182. printf (" Size: %ld MB in %d Sectors\n",
  183. info->size >> 20, info->sector_count);
  184. printf (" Sector Start Addresses:");
  185. for (i = 0; i < info->sector_count; ++i) {
  186. if ((i % 5) == 0)
  187. printf ("\n ");
  188. printf (" %08lX%s",
  189. info->start[i], info->protect[i] ? " (RO)" : " ");
  190. }
  191. printf ("\n");
  192. return;
  193. }
  194. /*
  195. * The following code cannot be run from FLASH!
  196. */
  197. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  198. {
  199. volatile FPW value;
  200. /* mb(); this one makes ARM11 err go away, but I want it :) as a guide to problems */
  201. /* Write auto select command: read Manufacturer ID */
  202. addr[0x5555] = (FPW) 0x00AA00AA;
  203. addr[0x2AAA] = (FPW) 0x00550055;
  204. addr[0x5555] = (FPW) 0x00900090;
  205. mb ();
  206. value = addr[0] & 0xFF; /* just looking for 89 (8989 is hw pat)*/
  207. switch (value) {
  208. case (FPW) INTEL_MANUFACT:
  209. info->flash_id = FLASH_MAN_INTEL;
  210. break;
  211. default:
  212. info->flash_id = FLASH_UNKNOWN;
  213. info->sector_count = 0;
  214. info->size = 0;
  215. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  216. return(0); /* no or unknown flash */
  217. }
  218. mb ();
  219. value = addr[1]; /* device ID */
  220. switch (value) {
  221. case (FPW) (INTEL_ID_28F256L18T): /* 880D */
  222. info->flash_id += FLASH_28F256L18T;
  223. info->sector_count = 259; /*0-258*/
  224. info->size = SZ_32M;
  225. break; /* => 32 MB */
  226. default:
  227. info->flash_id = FLASH_UNKNOWN;
  228. break;
  229. }
  230. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  231. printf ("** ERROR: sector count %d > max (%d) **\n",
  232. info->sector_count, CFG_MAX_FLASH_SECT);
  233. info->sector_count = CFG_MAX_FLASH_SECT;
  234. }
  235. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  236. return(info->size);
  237. }
  238. /* unprotects a sector for write and erase
  239. * on some intel parts, this unprotects the entire chip, but it
  240. * wont hurt to call this additional times per sector...
  241. */
  242. void flash_unprotect_sectors (FPWV * addr)
  243. {
  244. #define PD_FINTEL_WSMS_READY_MASK 0x0080
  245. *addr = (FPW) 0x00500050; /* clear status register */
  246. /* this sends the clear lock bit command */
  247. *addr = (FPW) 0x00600060;
  248. *addr = (FPW) 0x00D000D0;
  249. }
  250. /*-----------------------------------------------------------------------
  251. */
  252. int flash_erase (flash_info_t * info, int s_first, int s_last)
  253. {
  254. int prot, sect;
  255. ulong type, start, last;
  256. int rcode = 0;
  257. #ifdef CONFIG_USE_IRQ
  258. int iflag;
  259. #endif
  260. if ((s_first < 0) || (s_first > s_last)) {
  261. if (info->flash_id == FLASH_UNKNOWN) {
  262. printf ("- missing\n");
  263. } else {
  264. printf ("- no sectors to erase\n");
  265. }
  266. return 1;
  267. }
  268. type = (info->flash_id & FLASH_VENDMASK);
  269. if ((type != FLASH_MAN_INTEL)) {
  270. printf ("Can't erase unknown flash type %08lx - aborted\n",
  271. info->flash_id);
  272. return 1;
  273. }
  274. prot = 0;
  275. for (sect = s_first; sect <= s_last; ++sect) {
  276. if (info->protect[sect]) {
  277. prot++;
  278. }
  279. }
  280. if (prot) {
  281. printf ("- Warning: %d protected sectors will not be erased!\n",
  282. prot);
  283. } else {
  284. printf ("\n");
  285. }
  286. start = get_timer (0);
  287. last = start;
  288. #ifdef CONFIG_USE_IRQ
  289. /* Disable interrupts which might cause a timeout here */
  290. iflag = disable_interrupts ();
  291. #endif
  292. /* Start erase on unprotected sectors */
  293. for (sect = s_first; sect <= s_last; sect++) {
  294. if (info->protect[sect] == 0) { /* not protected */
  295. FPWV *addr = (FPWV *) (info->start[sect]);
  296. FPW status;
  297. printf ("Erasing sector %2d ... ", sect);
  298. flash_unprotect_sectors (addr);
  299. /* arm simple, non interrupt dependent timer */
  300. reset_timer_masked ();
  301. *addr = (FPW) 0x00500050;/* clear status register */
  302. *addr = (FPW) 0x00200020;/* erase setup */
  303. *addr = (FPW) 0x00D000D0;/* erase confirm */
  304. while (((status =
  305. *addr) & (FPW) 0x00800080) !=
  306. (FPW) 0x00800080) {
  307. if (get_timer_masked () >
  308. CFG_FLASH_ERASE_TOUT) {
  309. printf ("Timeout\n");
  310. /* suspend erase */
  311. *addr = (FPW) 0x00B000B0;
  312. /* reset to read mode */
  313. *addr = (FPW) 0x00FF00FF;
  314. rcode = 1;
  315. break;
  316. }
  317. }
  318. /* clear status register cmd. */
  319. *addr = (FPW) 0x00500050;
  320. *addr = (FPW) 0x00FF00FF;/* resest to read mode */
  321. printf (" done\n");
  322. }
  323. }
  324. #ifdef CONFIG_USE_IRQ
  325. if (iflag)
  326. enable_interrupts();
  327. #endif
  328. return rcode;
  329. }
  330. /*-----------------------------------------------------------------------
  331. * Copy memory to flash, returns:
  332. * 0 - OK
  333. * 1 - write timeout
  334. * 2 - Flash not erased
  335. * 4 - Flash not identified
  336. */
  337. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  338. {
  339. ulong cp, wp;
  340. FPW data;
  341. int count, i, l, rc, port_width;
  342. if (info->flash_id == FLASH_UNKNOWN) {
  343. return 4;
  344. }
  345. /* get lower word aligned address */
  346. #ifdef FLASH_PORT_WIDTH16
  347. wp = (addr & ~1);
  348. port_width = 2;
  349. #else
  350. wp = (addr & ~3);
  351. port_width = 4;
  352. #endif
  353. /*
  354. * handle unaligned start bytes
  355. */
  356. if ((l = addr - wp) != 0) {
  357. data = 0;
  358. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  359. data = (data << 8) | (*(uchar *) cp);
  360. }
  361. for (; i < port_width && cnt > 0; ++i) {
  362. data = (data << 8) | *src++;
  363. --cnt;
  364. ++cp;
  365. }
  366. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  367. data = (data << 8) | (*(uchar *) cp);
  368. }
  369. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  370. return(rc);
  371. }
  372. wp += port_width;
  373. }
  374. /*
  375. * handle word aligned part
  376. */
  377. count = 0;
  378. while (cnt >= port_width) {
  379. data = 0;
  380. for (i = 0; i < port_width; ++i) {
  381. data = (data << 8) | *src++;
  382. }
  383. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  384. return(rc);
  385. }
  386. wp += port_width;
  387. cnt -= port_width;
  388. if (count++ > 0x800) {
  389. spin_wheel ();
  390. count = 0;
  391. }
  392. }
  393. if (cnt == 0) {
  394. return(0);
  395. }
  396. /*
  397. * handle unaligned tail bytes
  398. */
  399. data = 0;
  400. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  401. data = (data << 8) | *src++;
  402. --cnt;
  403. }
  404. for (; i < port_width; ++i, ++cp) {
  405. data = (data << 8) | (*(uchar *) cp);
  406. }
  407. return(write_data (info, wp, SWAP (data)));
  408. }
  409. /*-----------------------------------------------------------------------
  410. * Write a word or halfword to Flash, returns:
  411. * 0 - OK
  412. * 1 - write timeout
  413. * 2 - Flash not erased
  414. */
  415. static int write_data (flash_info_t * info, ulong dest, FPW data)
  416. {
  417. FPWV *addr = (FPWV *) dest;
  418. ulong status;
  419. #ifdef CONFIG_USE_IRQ
  420. int iflag;
  421. #endif
  422. /* Check if Flash is (sufficiently) erased */
  423. if ((*addr & data) != data) {
  424. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  425. return(2);
  426. }
  427. /* Disable interrupts which might cause a timeout here */
  428. #ifdef CONFIG_USE_IRQ
  429. iflag = disable_interrupts ();
  430. #endif
  431. *addr = (FPW) 0x00400040; /* write setup */
  432. *addr = data;
  433. /* arm simple, non interrupt dependent timer */
  434. reset_timer_masked ();
  435. /* wait while polling the status register */
  436. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  437. if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
  438. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  439. return(1);
  440. }
  441. }
  442. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  443. #ifdef CONFIG_USE_IRQ
  444. if (iflag)
  445. enable_interrupts();
  446. #endif
  447. return(0);
  448. }
  449. void inline spin_wheel (void)
  450. {
  451. static int p = 0;
  452. static char w[] = "\\/-";
  453. printf ("\010%c", w[p]);
  454. (++p == 3) ? (p = 0) : 0;
  455. }