flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * (C) Copyright 2002
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  11. *
  12. * (C) Copyright 2002
  13. * Auerswald GmbH & Co KG, Germany
  14. * Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation; either version 2 of
  22. * the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. */
  34. #include <common.h>
  35. #include <asm/arch/pxa-regs.h>
  36. #if defined CFG_JFFS_CUSTOM_PART
  37. #include <jffs2/jffs2.h>
  38. #endif
  39. /* Debugging macros ------------------------------------------------------ */
  40. #undef FLASH_DEBUG
  41. //#define FLASH_DEBUG 1
  42. /* Some debug macros */
  43. #if (FLASH_DEBUG > 2 )
  44. #define PRINTK3(args...) printf(args)
  45. #else
  46. #define PRINTK3(args...)
  47. #endif
  48. #if FLASH_DEBUG > 1
  49. #define PRINTK2(args...) printf(args)
  50. #else
  51. #define PRINTK2(args...)
  52. #endif
  53. #ifdef FLASH_DEBUG
  54. #define PRINTK(args...) printf(args)
  55. #else
  56. #define PRINTK(args...)
  57. #endif
  58. /* ------------------------------------------------------------------------ */
  59. /* Development system: we have only 16 MB Flash */
  60. #ifdef CONFIG_MTD_INNOKOM_16MB
  61. #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
  62. #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
  63. #endif
  64. /* Production system: we have 64 MB Flash */
  65. #ifdef CONFIG_MTD_INNOKOM_64MB
  66. #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
  67. #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
  68. #endif
  69. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  70. #if defined CFG_JFFS_CUSTOM_PART
  71. /**
  72. * jffs2_part_info - get information about a JFFS2 partition
  73. *
  74. * @part_num: number of the partition you want to get info about
  75. * @return: struct part_info* in case of success, 0 if failure
  76. */
  77. static struct part_info part;
  78. static int current_part = -1;
  79. #ifdef CONFIG_MTD_INNOKOM_16MB
  80. #ifdef CONFIG_MTD_INNOKOM_64MB
  81. #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
  82. #endif
  83. struct part_info* jffs2_part_info(int part_num) {
  84. void *jffs2_priv_saved = part.jffs2_priv;
  85. PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
  86. if (current_part == part_num)
  87. return &part;
  88. /* u-boot partition */
  89. if(part_num==0){
  90. memset(&part, 0, sizeof(part));
  91. part.offset=(char*)0x00000000;
  92. part.size=256*1024;
  93. /* Mark the struct as ready */
  94. current_part = part_num;
  95. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  96. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  97. }
  98. /* primary OS+firmware partition */
  99. if(part_num==1){
  100. memset(&part, 0, sizeof(part));
  101. part.offset=(char*)0x00040000;
  102. part.size=768*1024;
  103. /* Mark the struct as ready */
  104. current_part = part_num;
  105. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  106. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  107. }
  108. /* secondary OS+firmware partition */
  109. if(part_num==2){
  110. memset(&part, 0, sizeof(part));
  111. part.offset=(char*)0x00100000;
  112. part.size=8*1024*1024;
  113. /* Mark the struct as ready */
  114. current_part = part_num;
  115. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  116. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  117. }
  118. /* data partition */
  119. if(part_num==3){
  120. memset(&part, 0, sizeof(part));
  121. part.offset=(char*)0x00900000;
  122. part.size=7*1024*1024;
  123. /* Mark the struct as ready */
  124. current_part = part_num;
  125. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  126. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  127. }
  128. if (current_part == part_num) {
  129. part.usr_priv = &current_part;
  130. part.jffs2_priv = jffs2_priv_saved;
  131. return &part;
  132. }
  133. PRINTK("jffs2_part_info: end of partition table\n");
  134. return 0;
  135. }
  136. #endif /* CONFIG_MTD_INNOKOM_16MB */
  137. #ifdef CONFIG_MTD_INNOKOM_64MB
  138. #ifdef CONFIG_MTD_INNOKOM_16MB
  139. #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
  140. #endif
  141. struct part_info* jffs2_part_info(int part_num) {
  142. void *jffs2_priv_saved = part.jffs2_priv;
  143. PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
  144. if (current_part == part_num)
  145. return &part;
  146. /* u-boot partition */
  147. if(part_num==0){
  148. memset(&part, 0, sizeof(part));
  149. part.offset=(char*)0x00000000;
  150. part.size=256*1024;
  151. /* Mark the struct as ready */
  152. current_part = part_num;
  153. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  154. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  155. }
  156. /* primary OS+firmware partition */
  157. if(part_num==1){
  158. memset(&part, 0, sizeof(part));
  159. part.offset=(char*)0x00040000;
  160. part.size=16*1024*1024-128*1024;
  161. /* Mark the struct as ready */
  162. current_part = part_num;
  163. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  164. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  165. }
  166. /* secondary OS+firmware partition */
  167. if(part_num==2){
  168. memset(&part, 0, sizeof(part));
  169. part.offset=(char*)0x01020000;
  170. part.size=16*1024*1024-128*1024;
  171. /* Mark the struct as ready */
  172. current_part = part_num;
  173. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  174. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  175. }
  176. /* data partition */
  177. if(part_num==3){
  178. memset(&part, 0, sizeof(part));
  179. part.offset=(char*)0x02000000;
  180. part.size=32*1024*1024;
  181. /* Mark the struct as ready */
  182. current_part = part_num;
  183. PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
  184. PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
  185. }
  186. if (current_part == part_num) {
  187. part.usr_priv = &current_part;
  188. part.jffs2_priv = jffs2_priv_saved;
  189. return &part;
  190. }
  191. PRINTK("jffs2_part_info: end of partition table\n");
  192. return 0;
  193. }
  194. #endif /* CONFIG_MTD_INNOKOM_64MB */
  195. #endif /* defined CFG_JFFS_CUSTOM_PART */
  196. /**
  197. * flash_init: - initialize data structures for flash chips
  198. *
  199. * @return: size of the flash
  200. */
  201. ulong flash_init(void)
  202. {
  203. int i, j;
  204. ulong size = 0;
  205. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  206. ulong flashbase = 0;
  207. flash_info[i].flash_id =
  208. (INTEL_MANUFACT & FLASH_VENDMASK) |
  209. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  210. flash_info[i].size = FLASH_BANK_SIZE;
  211. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  212. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  213. switch (i) {
  214. case 0:
  215. flashbase = PHYS_FLASH_1;
  216. break;
  217. default:
  218. panic("configured to many flash banks!\n");
  219. break;
  220. }
  221. for (j = 0; j < flash_info[i].sector_count; j++) {
  222. flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
  223. }
  224. size += flash_info[i].size;
  225. }
  226. /* Protect u-boot sectors */
  227. flash_protect(FLAG_PROTECT_SET,
  228. CFG_FLASH_BASE,
  229. CFG_FLASH_BASE + (256*1024) - 1,
  230. &flash_info[0]);
  231. #ifdef CFG_ENV_IS_IN_FLASH
  232. flash_protect(FLAG_PROTECT_SET,
  233. CFG_ENV_ADDR,
  234. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  235. &flash_info[0]);
  236. #endif
  237. return size;
  238. }
  239. /**
  240. * flash_print_info: - print information about the flash situation
  241. *
  242. * @param info:
  243. */
  244. void flash_print_info (flash_info_t *info)
  245. {
  246. int i, j;
  247. for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
  248. switch (info->flash_id & FLASH_VENDMASK) {
  249. case (INTEL_MANUFACT & FLASH_VENDMASK):
  250. printf("Intel: ");
  251. break;
  252. default:
  253. printf("Unknown Vendor ");
  254. break;
  255. }
  256. switch (info->flash_id & FLASH_TYPEMASK) {
  257. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  258. printf("28F128J3 (128Mbit)\n");
  259. break;
  260. default:
  261. printf("Unknown Chip Type\n");
  262. return;
  263. }
  264. printf(" Size: %ld MB in %d Sectors\n",
  265. info->size >> 20, info->sector_count);
  266. printf(" Sector Start Addresses:");
  267. for (i = 0; i < info->sector_count; i++) {
  268. if ((i % 5) == 0) printf ("\n ");
  269. printf (" %08lX%s", info->start[i],
  270. info->protect[i] ? " (RO)" : " ");
  271. }
  272. printf ("\n");
  273. info++;
  274. }
  275. }
  276. /**
  277. * flash_erase: - erase flash sectors
  278. *
  279. */
  280. int flash_erase(flash_info_t *info, int s_first, int s_last)
  281. {
  282. int flag, prot, sect;
  283. int rc = ERR_OK;
  284. if (info->flash_id == FLASH_UNKNOWN)
  285. return ERR_UNKNOWN_FLASH_TYPE;
  286. if ((s_first < 0) || (s_first > s_last)) {
  287. return ERR_INVAL;
  288. }
  289. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
  290. return ERR_UNKNOWN_FLASH_VENDOR;
  291. prot = 0;
  292. for (sect=s_first; sect<=s_last; ++sect) {
  293. if (info->protect[sect]) prot++;
  294. }
  295. if (prot) return ERR_PROTECTED;
  296. /*
  297. * Disable interrupts which might cause a timeout
  298. * here. Remember that our exception vectors are
  299. * at address 0 in the flash, and we don't want a
  300. * (ticker) exception to happen while the flash
  301. * chip is in programming mode.
  302. */
  303. flag = disable_interrupts();
  304. /* Start erase on unprotected sectors */
  305. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  306. printf("Erasing sector %2d ... ", sect);
  307. PRINTK("\n");
  308. /* arm simple, non interrupt dependent timer */
  309. reset_timer_masked();
  310. if (info->protect[sect] == 0) { /* not protected */
  311. u16 * volatile addr = (u16 * volatile)(info->start[sect]);
  312. PRINTK("unlocking sector\n");
  313. *addr = 0x0060;
  314. *addr = 0x00d0;
  315. *addr = 0x00ff;
  316. PRINTK("erasing sector\n");
  317. *addr = 0x0020;
  318. PRINTK("confirming erase\n");
  319. *addr = 0x00D0;
  320. while ((*addr & 0x0080) != 0x0080) {
  321. PRINTK(".");
  322. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  323. *addr = 0x00B0; /* suspend erase*/
  324. *addr = 0x00FF; /* read mode */
  325. rc = ERR_TIMOUT;
  326. goto outahere;
  327. }
  328. }
  329. PRINTK("clearing status register\n");
  330. *addr = 0x0050;
  331. PRINTK("resetting to read mode");
  332. *addr = 0x00FF;
  333. }
  334. printf("ok.\n");
  335. }
  336. if (ctrlc()) printf("User Interrupt!\n");
  337. outahere:
  338. /* allow flash to settle - wait 10 ms */
  339. udelay_masked(10000);
  340. if (flag) enable_interrupts();
  341. return rc;
  342. }
  343. /**
  344. * write_word: - copy memory to flash
  345. *
  346. * @param info:
  347. * @param dest:
  348. * @param data:
  349. * @return:
  350. */
  351. static int write_word (flash_info_t *info, ulong dest, ushort data)
  352. {
  353. volatile u16 *addr = (u16 *)dest, val;
  354. int rc = ERR_OK;
  355. int flag;
  356. /* Check if Flash is (sufficiently) erased */
  357. if ((*addr & data) != data) return ERR_NOT_ERASED;
  358. /*
  359. * Disable interrupts which might cause a timeout
  360. * here. Remember that our exception vectors are
  361. * at address 0 in the flash, and we don't want a
  362. * (ticker) exception to happen while the flash
  363. * chip is in programming mode.
  364. */
  365. flag = disable_interrupts();
  366. /* clear status register command */
  367. *addr = 0x50;
  368. /* program set-up command */
  369. *addr = 0x40;
  370. /* latch address/data */
  371. *addr = data;
  372. /* arm simple, non interrupt dependent timer */
  373. reset_timer_masked();
  374. /* wait while polling the status register */
  375. while(((val = *addr) & 0x80) != 0x80) {
  376. if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
  377. rc = ERR_TIMOUT;
  378. *addr = 0xB0; /* suspend program command */
  379. goto outahere;
  380. }
  381. }
  382. if(val & 0x1A) { /* check for error */
  383. printf("\nFlash write error %02x at address %08lx\n",
  384. (int)val, (unsigned long)dest);
  385. if(val & (1<<3)) {
  386. printf("Voltage range error.\n");
  387. rc = ERR_PROG_ERROR;
  388. goto outahere;
  389. }
  390. if(val & (1<<1)) {
  391. printf("Device protect error.\n");
  392. rc = ERR_PROTECTED;
  393. goto outahere;
  394. }
  395. if(val & (1<<4)) {
  396. printf("Programming error.\n");
  397. rc = ERR_PROG_ERROR;
  398. goto outahere;
  399. }
  400. rc = ERR_PROG_ERROR;
  401. goto outahere;
  402. }
  403. outahere:
  404. *addr = 0xFF; /* read array command */
  405. if (flag) enable_interrupts();
  406. return rc;
  407. }
  408. /**
  409. * write_buf: - Copy memory to flash.
  410. *
  411. * @param info:
  412. * @param src: source of copy transaction
  413. * @param addr: where to copy to
  414. * @param cnt: number of bytes to copy
  415. *
  416. * @return error code
  417. */
  418. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  419. {
  420. ulong cp, wp;
  421. ushort data;
  422. int l;
  423. int i, rc;
  424. wp = (addr & ~1); /* get lower word aligned address */
  425. /*
  426. * handle unaligned start bytes
  427. */
  428. if ((l = addr - wp) != 0) {
  429. data = 0;
  430. for (i=0, cp=wp; i<l; ++i, ++cp) {
  431. data = (data >> 8) | (*(uchar *)cp << 8);
  432. }
  433. for (; i<2 && cnt>0; ++i) {
  434. data = (data >> 8) | (*src++ << 8);
  435. --cnt;
  436. ++cp;
  437. }
  438. for (; cnt==0 && i<2; ++i, ++cp) {
  439. data = (data >> 8) | (*(uchar *)cp << 8);
  440. }
  441. if ((rc = write_word(info, wp, data)) != 0) {
  442. return (rc);
  443. }
  444. wp += 2;
  445. }
  446. /*
  447. * handle word aligned part
  448. */
  449. while (cnt >= 2) {
  450. /* data = *((vushort*)src); */
  451. data = *((ushort*)src);
  452. if ((rc = write_word(info, wp, data)) != 0) {
  453. return (rc);
  454. }
  455. src += 2;
  456. wp += 2;
  457. cnt -= 2;
  458. }
  459. if (cnt == 0) return ERR_OK;
  460. /*
  461. * handle unaligned tail bytes
  462. */
  463. data = 0;
  464. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  465. data = (data >> 8) | (*src++ << 8);
  466. --cnt;
  467. }
  468. for (; i<2; ++i, ++cp) {
  469. data = (data >> 8) | (*(uchar *)cp << 8);
  470. }
  471. return write_word(info, wp, data);
  472. }