flash.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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[CONFIG_SYS_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 < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  82. memset(flash_info[i].protect, 0, CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  99. CONFIG_SYS_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<CONFIG_SYS_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. ulong start;
  155. if (info->flash_id == FLASH_UNKNOWN)
  156. return ERR_UNKNOWN_FLASH_TYPE;
  157. if ((s_first < 0) || (s_first > s_last)) {
  158. return ERR_INVAL;
  159. }
  160. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
  161. return ERR_UNKNOWN_FLASH_VENDOR;
  162. prot = 0;
  163. for (sect=s_first; sect<=s_last; ++sect) {
  164. if (info->protect[sect]) prot++;
  165. }
  166. if (prot) return ERR_PROTECTED;
  167. /*
  168. * Disable interrupts which might cause a timeout
  169. * here. Remember that our exception vectors are
  170. * at address 0 in the flash, and we don't want a
  171. * (ticker) exception to happen while the flash
  172. * chip is in programming mode.
  173. */
  174. flag = disable_interrupts();
  175. /* Start erase on unprotected sectors */
  176. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  177. printf("Erasing sector %2d ... ", sect);
  178. PRINTK("\n");
  179. /* arm simple, non interrupt dependent timer */
  180. start = get_timer(0);
  181. if (info->protect[sect] == 0) { /* not protected */
  182. u16 * volatile addr = (u16 * volatile)(info->start[sect]);
  183. PRINTK("unlocking sector\n");
  184. *addr = 0x0060;
  185. *addr = 0x00d0;
  186. *addr = 0x00ff;
  187. PRINTK("erasing sector\n");
  188. *addr = 0x0020;
  189. PRINTK("confirming erase\n");
  190. *addr = 0x00D0;
  191. while ((*addr & 0x0080) != 0x0080) {
  192. PRINTK(".");
  193. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  194. *addr = 0x00B0; /* suspend erase*/
  195. *addr = 0x00FF; /* read mode */
  196. rc = ERR_TIMOUT;
  197. goto outahere;
  198. }
  199. }
  200. PRINTK("clearing status register\n");
  201. *addr = 0x0050;
  202. PRINTK("resetting to read mode");
  203. *addr = 0x00FF;
  204. }
  205. printf("ok.\n");
  206. }
  207. if (ctrlc()) printf("User Interrupt!\n");
  208. outahere:
  209. /* allow flash to settle - wait 10 ms */
  210. udelay_masked(10000);
  211. if (flag) enable_interrupts();
  212. return rc;
  213. }
  214. /**
  215. * write_word: - copy memory to flash
  216. *
  217. * @param info:
  218. * @param dest:
  219. * @param data:
  220. * @return:
  221. */
  222. static int write_word (flash_info_t *info, ulong dest, ushort data)
  223. {
  224. volatile u16 *addr = (u16 *)dest, val;
  225. int rc = ERR_OK;
  226. int flag;
  227. ulong start;
  228. /* Check if Flash is (sufficiently) erased */
  229. if ((*addr & data) != data) return ERR_NOT_ERASED;
  230. /*
  231. * Disable interrupts which might cause a timeout
  232. * here. Remember that our exception vectors are
  233. * at address 0 in the flash, and we don't want a
  234. * (ticker) exception to happen while the flash
  235. * chip is in programming mode.
  236. */
  237. flag = disable_interrupts();
  238. /* clear status register command */
  239. *addr = 0x50;
  240. /* program set-up command */
  241. *addr = 0x40;
  242. /* latch address/data */
  243. *addr = data;
  244. /* arm simple, non interrupt dependent timer */
  245. start = get_timer(0);
  246. /* wait while polling the status register */
  247. while(((val = *addr) & 0x80) != 0x80) {
  248. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  249. rc = ERR_TIMOUT;
  250. *addr = 0xB0; /* suspend program command */
  251. goto outahere;
  252. }
  253. }
  254. if(val & 0x1A) { /* check for error */
  255. printf("\nFlash write error %02x at address %08lx\n",
  256. (int)val, (unsigned long)dest);
  257. if(val & (1<<3)) {
  258. printf("Voltage range error.\n");
  259. rc = ERR_PROG_ERROR;
  260. goto outahere;
  261. }
  262. if(val & (1<<1)) {
  263. printf("Device protect error.\n");
  264. rc = ERR_PROTECTED;
  265. goto outahere;
  266. }
  267. if(val & (1<<4)) {
  268. printf("Programming error.\n");
  269. rc = ERR_PROG_ERROR;
  270. goto outahere;
  271. }
  272. rc = ERR_PROG_ERROR;
  273. goto outahere;
  274. }
  275. outahere:
  276. *addr = 0xFF; /* read array command */
  277. if (flag) enable_interrupts();
  278. return rc;
  279. }
  280. /**
  281. * write_buf: - Copy memory to flash.
  282. *
  283. * @param info:
  284. * @param src: source of copy transaction
  285. * @param addr: where to copy to
  286. * @param cnt: number of bytes to copy
  287. *
  288. * @return error code
  289. */
  290. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  291. {
  292. ulong cp, wp;
  293. ushort data;
  294. int l;
  295. int i, rc;
  296. wp = (addr & ~1); /* get lower word aligned address */
  297. /*
  298. * handle unaligned start bytes
  299. */
  300. if ((l = addr - wp) != 0) {
  301. data = 0;
  302. for (i=0, cp=wp; i<l; ++i, ++cp) {
  303. data = (data >> 8) | (*(uchar *)cp << 8);
  304. }
  305. for (; i<2 && cnt>0; ++i) {
  306. data = (data >> 8) | (*src++ << 8);
  307. --cnt;
  308. ++cp;
  309. }
  310. for (; cnt==0 && i<2; ++i, ++cp) {
  311. data = (data >> 8) | (*(uchar *)cp << 8);
  312. }
  313. if ((rc = write_word(info, wp, data)) != 0) {
  314. return (rc);
  315. }
  316. wp += 2;
  317. }
  318. /*
  319. * handle word aligned part
  320. */
  321. while (cnt >= 2) {
  322. /* data = *((vushort*)src); */
  323. data = *((ushort*)src);
  324. if ((rc = write_word(info, wp, data)) != 0) {
  325. return (rc);
  326. }
  327. src += 2;
  328. wp += 2;
  329. cnt -= 2;
  330. }
  331. if (cnt == 0) return ERR_OK;
  332. /*
  333. * handle unaligned tail bytes
  334. */
  335. data = 0;
  336. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  337. data = (data >> 8) | (*src++ << 8);
  338. --cnt;
  339. }
  340. for (; i<2; ++i, ++cp) {
  341. data = (data >> 8) | (*(uchar *)cp << 8);
  342. }
  343. return write_word(info, wp, data);
  344. }