flash.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /* Debugging macros ------------------------------------------------------ */
  37. #undef FLASH_DEBUG
  38. /* Some debug macros */
  39. #if (FLASH_DEBUG > 2 )
  40. #define PRINTK3(args...) printf(args)
  41. #else
  42. #define PRINTK3(args...)
  43. #endif
  44. #if FLASH_DEBUG > 1
  45. #define PRINTK2(args...) printf(args)
  46. #else
  47. #define PRINTK2(args...)
  48. #endif
  49. #ifdef FLASH_DEBUG
  50. #define PRINTK(args...) printf(args)
  51. #else
  52. #define PRINTK(args...)
  53. #endif
  54. /* ------------------------------------------------------------------------ */
  55. /* Development system: we have only 16 MB Flash */
  56. #ifdef CONFIG_MTD_INNOKOM_16MB
  57. #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
  58. #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
  59. #endif
  60. /* Production system: we have 64 MB Flash */
  61. #ifdef CONFIG_MTD_INNOKOM_64MB
  62. #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
  63. #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
  64. #endif
  65. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  66. /**
  67. * flash_init: - initialize data structures for flash chips
  68. *
  69. * @return: size of the flash
  70. */
  71. ulong flash_init(void)
  72. {
  73. int i, j;
  74. ulong size = 0;
  75. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  76. ulong flashbase = 0;
  77. flash_info[i].flash_id =
  78. (INTEL_MANUFACT & FLASH_VENDMASK) |
  79. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  80. flash_info[i].size = FLASH_BANK_SIZE;
  81. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  82. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  83. switch (i) {
  84. case 0:
  85. flashbase = PHYS_FLASH_1;
  86. break;
  87. default:
  88. panic("configured too many flash banks!\n");
  89. break;
  90. }
  91. for (j = 0; j < flash_info[i].sector_count; j++) {
  92. flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
  93. }
  94. size += flash_info[i].size;
  95. }
  96. /* Protect u-boot sectors */
  97. flash_protect(FLAG_PROTECT_SET,
  98. CFG_FLASH_BASE,
  99. CFG_FLASH_BASE + (256*1024) - 1,
  100. &flash_info[0]);
  101. #ifdef CONFIG_ENV_IS_IN_FLASH
  102. flash_protect(FLAG_PROTECT_SET,
  103. CONFIG_ENV_ADDR,
  104. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  105. &flash_info[0]);
  106. #endif
  107. return size;
  108. }
  109. /**
  110. * flash_print_info: - print information about the flash situation
  111. *
  112. * @param info:
  113. */
  114. void flash_print_info (flash_info_t *info)
  115. {
  116. int i, j;
  117. for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
  118. switch (info->flash_id & FLASH_VENDMASK) {
  119. case (INTEL_MANUFACT & FLASH_VENDMASK):
  120. printf("Intel: ");
  121. break;
  122. default:
  123. printf("Unknown Vendor ");
  124. break;
  125. }
  126. switch (info->flash_id & FLASH_TYPEMASK) {
  127. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  128. printf("28F128J3 (128Mbit)\n");
  129. break;
  130. default:
  131. printf("Unknown Chip Type\n");
  132. return;
  133. }
  134. printf(" Size: %ld MB in %d Sectors\n",
  135. info->size >> 20, info->sector_count);
  136. printf(" Sector Start Addresses:");
  137. for (i = 0; i < info->sector_count; i++) {
  138. if ((i % 5) == 0) printf ("\n ");
  139. printf (" %08lX%s", info->start[i],
  140. info->protect[i] ? " (RO)" : " ");
  141. }
  142. printf ("\n");
  143. info++;
  144. }
  145. }
  146. /**
  147. * flash_erase: - erase flash sectors
  148. *
  149. */
  150. int flash_erase(flash_info_t *info, int s_first, int s_last)
  151. {
  152. int flag, prot, sect;
  153. int rc = ERR_OK;
  154. if (info->flash_id == FLASH_UNKNOWN)
  155. return ERR_UNKNOWN_FLASH_TYPE;
  156. if ((s_first < 0) || (s_first > s_last)) {
  157. return ERR_INVAL;
  158. }
  159. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
  160. return ERR_UNKNOWN_FLASH_VENDOR;
  161. prot = 0;
  162. for (sect=s_first; sect<=s_last; ++sect) {
  163. if (info->protect[sect]) prot++;
  164. }
  165. if (prot) return ERR_PROTECTED;
  166. /*
  167. * Disable interrupts which might cause a timeout
  168. * here. Remember that our exception vectors are
  169. * at address 0 in the flash, and we don't want a
  170. * (ticker) exception to happen while the flash
  171. * chip is in programming mode.
  172. */
  173. flag = disable_interrupts();
  174. /* Start erase on unprotected sectors */
  175. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  176. printf("Erasing sector %2d ... ", sect);
  177. PRINTK("\n");
  178. /* arm simple, non interrupt dependent timer */
  179. reset_timer_masked();
  180. if (info->protect[sect] == 0) { /* not protected */
  181. u16 * volatile addr = (u16 * volatile)(info->start[sect]);
  182. PRINTK("unlocking sector\n");
  183. *addr = 0x0060;
  184. *addr = 0x00d0;
  185. *addr = 0x00ff;
  186. PRINTK("erasing sector\n");
  187. *addr = 0x0020;
  188. PRINTK("confirming erase\n");
  189. *addr = 0x00D0;
  190. while ((*addr & 0x0080) != 0x0080) {
  191. PRINTK(".");
  192. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  193. *addr = 0x00B0; /* suspend erase*/
  194. *addr = 0x00FF; /* read mode */
  195. rc = ERR_TIMOUT;
  196. goto outahere;
  197. }
  198. }
  199. PRINTK("clearing status register\n");
  200. *addr = 0x0050;
  201. PRINTK("resetting to read mode");
  202. *addr = 0x00FF;
  203. }
  204. printf("ok.\n");
  205. }
  206. if (ctrlc()) printf("User Interrupt!\n");
  207. outahere:
  208. /* allow flash to settle - wait 10 ms */
  209. udelay_masked(10000);
  210. if (flag) enable_interrupts();
  211. return rc;
  212. }
  213. /**
  214. * write_word: - copy memory to flash
  215. *
  216. * @param info:
  217. * @param dest:
  218. * @param data:
  219. * @return:
  220. */
  221. static int write_word (flash_info_t *info, ulong dest, ushort data)
  222. {
  223. volatile u16 *addr = (u16 *)dest, val;
  224. int rc = ERR_OK;
  225. int flag;
  226. /* Check if Flash is (sufficiently) erased */
  227. if ((*addr & data) != data) return ERR_NOT_ERASED;
  228. /*
  229. * Disable interrupts which might cause a timeout
  230. * here. Remember that our exception vectors are
  231. * at address 0 in the flash, and we don't want a
  232. * (ticker) exception to happen while the flash
  233. * chip is in programming mode.
  234. */
  235. flag = disable_interrupts();
  236. /* clear status register command */
  237. *addr = 0x50;
  238. /* program set-up command */
  239. *addr = 0x40;
  240. /* latch address/data */
  241. *addr = data;
  242. /* arm simple, non interrupt dependent timer */
  243. reset_timer_masked();
  244. /* wait while polling the status register */
  245. while(((val = *addr) & 0x80) != 0x80) {
  246. if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
  247. rc = ERR_TIMOUT;
  248. *addr = 0xB0; /* suspend program command */
  249. goto outahere;
  250. }
  251. }
  252. if(val & 0x1A) { /* check for error */
  253. printf("\nFlash write error %02x at address %08lx\n",
  254. (int)val, (unsigned long)dest);
  255. if(val & (1<<3)) {
  256. printf("Voltage range error.\n");
  257. rc = ERR_PROG_ERROR;
  258. goto outahere;
  259. }
  260. if(val & (1<<1)) {
  261. printf("Device protect error.\n");
  262. rc = ERR_PROTECTED;
  263. goto outahere;
  264. }
  265. if(val & (1<<4)) {
  266. printf("Programming error.\n");
  267. rc = ERR_PROG_ERROR;
  268. goto outahere;
  269. }
  270. rc = ERR_PROG_ERROR;
  271. goto outahere;
  272. }
  273. outahere:
  274. *addr = 0xFF; /* read array command */
  275. if (flag) enable_interrupts();
  276. return rc;
  277. }
  278. /**
  279. * write_buf: - Copy memory to flash.
  280. *
  281. * @param info:
  282. * @param src: source of copy transaction
  283. * @param addr: where to copy to
  284. * @param cnt: number of bytes to copy
  285. *
  286. * @return error code
  287. */
  288. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  289. {
  290. ulong cp, wp;
  291. ushort data;
  292. int l;
  293. int i, rc;
  294. wp = (addr & ~1); /* get lower word aligned address */
  295. /*
  296. * handle unaligned start bytes
  297. */
  298. if ((l = addr - wp) != 0) {
  299. data = 0;
  300. for (i=0, cp=wp; i<l; ++i, ++cp) {
  301. data = (data >> 8) | (*(uchar *)cp << 8);
  302. }
  303. for (; i<2 && cnt>0; ++i) {
  304. data = (data >> 8) | (*src++ << 8);
  305. --cnt;
  306. ++cp;
  307. }
  308. for (; cnt==0 && i<2; ++i, ++cp) {
  309. data = (data >> 8) | (*(uchar *)cp << 8);
  310. }
  311. if ((rc = write_word(info, wp, data)) != 0) {
  312. return (rc);
  313. }
  314. wp += 2;
  315. }
  316. /*
  317. * handle word aligned part
  318. */
  319. while (cnt >= 2) {
  320. /* data = *((vushort*)src); */
  321. data = *((ushort*)src);
  322. if ((rc = write_word(info, wp, data)) != 0) {
  323. return (rc);
  324. }
  325. src += 2;
  326. wp += 2;
  327. cnt -= 2;
  328. }
  329. if (cnt == 0) return ERR_OK;
  330. /*
  331. * handle unaligned tail bytes
  332. */
  333. data = 0;
  334. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  335. data = (data >> 8) | (*src++ << 8);
  336. --cnt;
  337. }
  338. for (; i<2; ++i, ++cp) {
  339. data = (data >> 8) | (*(uchar *)cp << 8);
  340. }
  341. return write_word(info, wp, data);
  342. }