flash.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 2003 (2 x 16 bit Flash bank patches)
  13. * Rolf Peukert, IMMS gGmbH, <rolf.peukert@imms.de>
  14. *
  15. * See file CREDITS for list of people who contributed to this
  16. * project.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  31. * MA 02111-1307 USA
  32. */
  33. #include <common.h>
  34. #include <asm/arch/pxa-regs.h>
  35. #define FLASH_BANK_SIZE 0x02000000
  36. #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
  37. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  38. /**
  39. * flash_init: - initialize data structures for flash chips
  40. *
  41. * @return: size of the flash
  42. */
  43. ulong flash_init(void)
  44. {
  45. int i, j;
  46. ulong size = 0;
  47. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  48. ulong flashbase = 0;
  49. flash_info[i].flash_id =
  50. (INTEL_MANUFACT & FLASH_VENDMASK) |
  51. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  52. flash_info[i].size = FLASH_BANK_SIZE;
  53. flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  54. memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  55. switch (i) {
  56. case 0:
  57. flashbase = PHYS_FLASH_1;
  58. break;
  59. default:
  60. panic("configured too many flash banks!\n");
  61. break;
  62. }
  63. for (j = 0; j < flash_info[i].sector_count; j++) {
  64. flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
  65. }
  66. size += flash_info[i].size;
  67. }
  68. /* Protect monitor and environment sectors */
  69. flash_protect(FLAG_PROTECT_SET,
  70. CONFIG_SYS_FLASH_BASE,
  71. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  72. &flash_info[0]);
  73. flash_protect(FLAG_PROTECT_SET,
  74. CONFIG_ENV_ADDR,
  75. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  76. &flash_info[0]);
  77. return size;
  78. }
  79. /**
  80. * flash_print_info: - print information about the flash situation
  81. */
  82. void flash_print_info (flash_info_t *info)
  83. {
  84. int i, j;
  85. for (j=0; j<CONFIG_SYS_MAX_FLASH_BANKS; j++) {
  86. switch (info->flash_id & FLASH_VENDMASK) {
  87. case (INTEL_MANUFACT & FLASH_VENDMASK):
  88. printf ("Intel: ");
  89. break;
  90. default:
  91. printf ("Unknown Vendor ");
  92. break;
  93. }
  94. switch (info->flash_id & FLASH_TYPEMASK) {
  95. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  96. printf("28F128J3 (128Mbit)\n");
  97. break;
  98. default:
  99. printf("Unknown Chip Type\n");
  100. return;
  101. }
  102. printf(" Size: %ld MB in %d Sectors\n",
  103. info->size >> 20, info->sector_count);
  104. printf(" Sector Start Addresses:");
  105. for (i = 0; i < info->sector_count; i++) {
  106. if ((i % 5) == 0) printf ("\n ");
  107. printf (" %08lX%s", info->start[i],
  108. info->protect[i] ? " (RO)" : " ");
  109. }
  110. printf ("\n");
  111. info++;
  112. }
  113. }
  114. /**
  115. * flash_erase: - erase flash sectors
  116. */
  117. int flash_erase(flash_info_t *info, int s_first, int s_last)
  118. {
  119. int flag, prot, sect;
  120. int rc = ERR_OK;
  121. ulong start;
  122. if (info->flash_id == FLASH_UNKNOWN)
  123. return ERR_UNKNOWN_FLASH_TYPE;
  124. if ((s_first < 0) || (s_first > s_last)) {
  125. return ERR_INVAL;
  126. }
  127. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
  128. return ERR_UNKNOWN_FLASH_VENDOR;
  129. prot = 0;
  130. for (sect=s_first; sect<=s_last; ++sect) {
  131. if (info->protect[sect]) prot++;
  132. }
  133. if (prot) return ERR_PROTECTED;
  134. /*
  135. * Disable interrupts which might cause a timeout
  136. * here. Remember that our exception vectors are
  137. * at address 0 in the flash, and we don't want a
  138. * (ticker) exception to happen while the flash
  139. * chip is in programming mode.
  140. */
  141. flag = disable_interrupts();
  142. /* Start erase on unprotected sectors */
  143. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  144. printf("Erasing sector %2d ... ", sect);
  145. /* arm simple, non interrupt dependent timer */
  146. start = get_timer(0);
  147. if (info->protect[sect] == 0) { /* not protected */
  148. u32 * volatile addr = (u32 * volatile)(info->start[sect]);
  149. /* erase sector: */
  150. /* The strata flashs are aligned side by side on */
  151. /* the data bus, so we have to write the commands */
  152. /* to both chips here: */
  153. *addr = 0x00200020; /* erase setup */
  154. *addr = 0x00D000D0; /* erase confirm */
  155. while ((*addr & 0x00800080) != 0x00800080) {
  156. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  157. *addr = 0x00B000B0; /* suspend erase*/
  158. *addr = 0x00FF00FF; /* read mode */
  159. rc = ERR_TIMOUT;
  160. goto outahere;
  161. }
  162. }
  163. *addr = 0x00500050; /* clear status register cmd. */
  164. *addr = 0x00FF00FF; /* reset to read mode */
  165. }
  166. printf("ok.\n");
  167. }
  168. if (ctrlc()) printf("User Interrupt!\n");
  169. outahere:
  170. /* allow flash to settle - wait 10 ms */
  171. udelay_masked(10000);
  172. if (flag) enable_interrupts();
  173. return rc;
  174. }
  175. /**
  176. * write_long: - copy memory to flash, assume a bank of 2 devices with 16bit each
  177. */
  178. static int write_long (flash_info_t *info, ulong dest, ulong data)
  179. {
  180. u32 * volatile addr = (u32 * volatile)dest, val;
  181. int rc = ERR_OK;
  182. int flag;
  183. ulong start;
  184. /* read array command - just for the case... */
  185. *addr = 0x00FF00FF;
  186. /* Check if Flash is (sufficiently) erased */
  187. if ((*addr & data) != data) return ERR_NOT_ERASED;
  188. /*
  189. * Disable interrupts which might cause a timeout
  190. * here. Remember that our exception vectors are
  191. * at address 0 in the flash, and we don't want a
  192. * (ticker) exception to happen while the flash
  193. * chip is in programming mode.
  194. */
  195. flag = disable_interrupts();
  196. /* clear status register command */
  197. *addr = 0x00500050;
  198. /* program set-up command */
  199. *addr = 0x00400040;
  200. /* latch address/data */
  201. *addr = data;
  202. /* arm simple, non interrupt dependent timer */
  203. start = get_timer(0);
  204. /* wait while polling the status register */
  205. while(((val = *addr) & 0x00800080) != 0x00800080) {
  206. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  207. rc = ERR_TIMOUT;
  208. /* suspend program command */
  209. *addr = 0x00B000B0;
  210. goto outahere;
  211. }
  212. }
  213. /* check for errors */
  214. if(val & 0x001A001A) {
  215. printf("\nFlash write error %02x at address %08lx\n",
  216. (int)val, (unsigned long)dest);
  217. if(val & 0x00080008) {
  218. printf("Voltage range error.\n");
  219. rc = ERR_PROG_ERROR;
  220. goto outahere;
  221. }
  222. if(val & 0x00020002) {
  223. printf("Device protect error.\n");
  224. rc = ERR_PROTECTED;
  225. goto outahere;
  226. }
  227. if(val & 0x00100010) {
  228. printf("Programming error.\n");
  229. rc = ERR_PROG_ERROR;
  230. goto outahere;
  231. }
  232. rc = ERR_PROG_ERROR;
  233. goto outahere;
  234. }
  235. outahere:
  236. /* read array command */
  237. *addr = 0x00FF00FF;
  238. if (flag) enable_interrupts();
  239. return rc;
  240. }
  241. /**
  242. * write_buf: - Copy memory to flash.
  243. *
  244. * @param info:
  245. * @param src: source of copy transaction
  246. * @param addr: where to copy to
  247. * @param cnt: number of bytes to copy
  248. *
  249. * @return error code
  250. */
  251. /* "long" version, uses 32bit words */
  252. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  253. {
  254. ulong cp, wp;
  255. ulong data;
  256. int l;
  257. int i, rc;
  258. wp = (addr & ~3); /* get lower word aligned address */
  259. /*
  260. * handle unaligned start bytes
  261. */
  262. if ((l = addr - wp) != 0) {
  263. data = 0;
  264. for (i=0, cp=wp; i<l; ++i, ++cp) {
  265. data = (data >> 8) | (*(uchar *)cp << 24);
  266. }
  267. for (; i<4 && cnt>0; ++i) {
  268. data = (data >> 8) | (*src++ << 24);
  269. --cnt;
  270. ++cp;
  271. }
  272. for (; cnt==0 && i<4; ++i, ++cp) {
  273. data = (data >> 8) | (*(uchar *)cp << 24);
  274. }
  275. if ((rc = write_long(info, wp, data)) != 0) {
  276. return (rc);
  277. }
  278. wp += 4;
  279. }
  280. /*
  281. * handle word aligned part
  282. */
  283. while (cnt >= 4) {
  284. data = *((ulong*)src);
  285. if ((rc = write_long(info, wp, data)) != 0) {
  286. return (rc);
  287. }
  288. src += 4;
  289. wp += 4;
  290. cnt -= 4;
  291. }
  292. if (cnt == 0) return ERR_OK;
  293. /*
  294. * handle unaligned tail bytes
  295. */
  296. data = 0;
  297. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  298. data = (data >> 8) | (*src++ << 24);
  299. --cnt;
  300. }
  301. for (; i<4; ++i, ++cp) {
  302. data = (data >> 8) | (*(uchar *)cp << 24);
  303. }
  304. return write_long(info, wp, data);
  305. }