flash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * 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/pxa-regs.h>
  32. #define FLASH_BANK_SIZE 0x02000000
  33. #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  35. /**
  36. * flash_init: - initialize data structures for flash chips
  37. *
  38. * @return: size of the flash
  39. */
  40. ulong flash_init(void)
  41. {
  42. int i, j;
  43. ulong size = 0;
  44. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  45. ulong flashbase = 0;
  46. flash_info[i].flash_id =
  47. (INTEL_MANUFACT & FLASH_VENDMASK) |
  48. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  49. flash_info[i].size = FLASH_BANK_SIZE;
  50. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  51. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  52. switch (i) {
  53. case 0:
  54. flashbase = PHYS_FLASH_1;
  55. break;
  56. default:
  57. panic("configured to many flash banks!\n");
  58. break;
  59. }
  60. for (j = 0; j < flash_info[i].sector_count; j++) {
  61. flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
  62. }
  63. size += flash_info[i].size;
  64. }
  65. /* Protect monitor and environment sectors */
  66. flash_protect(FLAG_PROTECT_SET,
  67. CFG_FLASH_BASE,
  68. CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
  69. &flash_info[0]);
  70. #ifdef CFG_ENV_IS_IN_FLASH
  71. flash_protect(FLAG_PROTECT_SET,
  72. CFG_ENV_ADDR,
  73. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  74. &flash_info[0]);
  75. #endif
  76. return size;
  77. }
  78. /**
  79. * flash_print_info: - print information about the flash situation
  80. *
  81. * @param info:
  82. */
  83. void flash_print_info (flash_info_t *info)
  84. {
  85. int i, j;
  86. for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
  87. switch (info->flash_id & FLASH_VENDMASK) {
  88. case (INTEL_MANUFACT & FLASH_VENDMASK):
  89. printf("Intel: ");
  90. break;
  91. default:
  92. printf("Unknown Vendor ");
  93. break;
  94. }
  95. switch (info->flash_id & FLASH_TYPEMASK) {
  96. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  97. printf("28F128J3 (128Mbit)\n");
  98. break;
  99. default:
  100. printf("Unknown Chip Type\n");
  101. return;
  102. }
  103. printf(" Size: %ld MB in %d Sectors\n",
  104. info->size >> 20, info->sector_count);
  105. printf(" Sector Start Addresses:");
  106. for (i = 0; i < info->sector_count; i++) {
  107. if ((i % 5) == 0) printf ("\n ");
  108. printf (" %08lX%s", info->start[i],
  109. info->protect[i] ? " (RO)" : " ");
  110. }
  111. printf ("\n");
  112. info++;
  113. }
  114. }
  115. /**
  116. * flash_erase: - erase flash sectors
  117. *
  118. */
  119. int flash_erase(flash_info_t *info, int s_first, int s_last)
  120. {
  121. int flag, prot, sect;
  122. int rc = ERR_OK;
  123. if (info->flash_id == FLASH_UNKNOWN)
  124. return ERR_UNKNOWN_FLASH_TYPE;
  125. if ((s_first < 0) || (s_first > s_last)) {
  126. return ERR_INVAL;
  127. }
  128. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
  129. return ERR_UNKNOWN_FLASH_VENDOR;
  130. prot = 0;
  131. for (sect=s_first; sect<=s_last; ++sect) {
  132. if (info->protect[sect]) prot++;
  133. }
  134. if (prot) return ERR_PROTECTED;
  135. /*
  136. * Disable interrupts which might cause a timeout
  137. * here. Remember that our exception vectors are
  138. * at address 0 in the flash, and we don't want a
  139. * (ticker) exception to happen while the flash
  140. * chip is in programming mode.
  141. */
  142. flag = disable_interrupts();
  143. /* Start erase on unprotected sectors */
  144. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  145. printf("Erasing sector %2d ... ", sect);
  146. /* arm simple, non interrupt dependent timer */
  147. reset_timer_masked();
  148. if (info->protect[sect] == 0) { /* not protected */
  149. u32 * volatile addr = (u32 * volatile)(info->start[sect]);
  150. /* erase sector: */
  151. /* The strata flashs are aligned side by side on */
  152. /* the data bus, so we have to write the commands */
  153. /* to both chips here: */
  154. *addr = 0x00200020; /* erase setup */
  155. *addr = 0x00D000D0; /* erase confirm */
  156. while ((*addr & 0x00800080) != 0x00800080) {
  157. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  158. *addr = 0x00B000B0; /* suspend erase*/
  159. *addr = 0x00FF00FF; /* read mode */
  160. rc = ERR_TIMOUT;
  161. goto outahere;
  162. }
  163. }
  164. *addr = 0x00500050; /* clear status register cmd. */
  165. *addr = 0x00FF00FF; /* resest to read mode */
  166. }
  167. printf("ok.\n");
  168. }
  169. if (ctrlc()) printf("User Interrupt!\n");
  170. outahere:
  171. /* allow flash to settle - wait 10 ms */
  172. udelay_masked(10000);
  173. if (flag) enable_interrupts();
  174. return rc;
  175. }
  176. /**
  177. * write_word: - copy memory to flash
  178. *
  179. * @param info:
  180. * @param dest:
  181. * @param data:
  182. * @return:
  183. */
  184. static int write_word (flash_info_t *info, ulong dest, ushort data)
  185. {
  186. ushort *addr = (ushort *)dest, val;
  187. int rc = ERR_OK;
  188. int flag;
  189. /* Check if Flash is (sufficiently) erased */
  190. if ((*addr & data) != data) return ERR_NOT_ERASED;
  191. /*
  192. * Disable interrupts which might cause a timeout
  193. * here. Remember that our exception vectors are
  194. * at address 0 in the flash, and we don't want a
  195. * (ticker) exception to happen while the flash
  196. * chip is in programming mode.
  197. */
  198. flag = disable_interrupts();
  199. /* clear status register command */
  200. *addr = 0x50;
  201. /* program set-up command */
  202. *addr = 0x40;
  203. /* latch address/data */
  204. *addr = data;
  205. /* arm simple, non interrupt dependent timer */
  206. reset_timer_masked();
  207. /* wait while polling the status register */
  208. while(((val = *addr) & 0x80) != 0x80) {
  209. if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
  210. rc = ERR_TIMOUT;
  211. *addr = 0xB0; /* suspend program command */
  212. goto outahere;
  213. }
  214. }
  215. if(val & 0x1A) { /* check for error */
  216. printf("\nFlash write error %02x at address %08lx\n",
  217. (int)val, (unsigned long)dest);
  218. if(val & (1<<3)) {
  219. printf("Voltage range error.\n");
  220. rc = ERR_PROG_ERROR;
  221. goto outahere;
  222. }
  223. if(val & (1<<1)) {
  224. printf("Device protect error.\n");
  225. rc = ERR_PROTECTED;
  226. goto outahere;
  227. }
  228. if(val & (1<<4)) {
  229. printf("Programming error.\n");
  230. rc = ERR_PROG_ERROR;
  231. goto outahere;
  232. }
  233. rc = ERR_PROG_ERROR;
  234. goto outahere;
  235. }
  236. outahere:
  237. *addr = 0xFF; /* read array command */
  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. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  252. {
  253. ulong cp, wp;
  254. ushort data;
  255. int l;
  256. int i, rc;
  257. wp = (addr & ~1); /* get lower word aligned address */
  258. /*
  259. * handle unaligned start bytes
  260. */
  261. if ((l = addr - wp) != 0) {
  262. data = 0;
  263. for (i=0, cp=wp; i<l; ++i, ++cp) {
  264. data = (data >> 8) | (*(uchar *)cp << 8);
  265. }
  266. for (; i<2 && cnt>0; ++i) {
  267. data = (data >> 8) | (*src++ << 8);
  268. --cnt;
  269. ++cp;
  270. }
  271. for (; cnt==0 && i<2; ++i, ++cp) {
  272. data = (data >> 8) | (*(uchar *)cp << 8);
  273. }
  274. if ((rc = write_word(info, wp, data)) != 0) {
  275. return (rc);
  276. }
  277. wp += 2;
  278. }
  279. /*
  280. * handle word aligned part
  281. */
  282. while (cnt >= 2) {
  283. /* data = *((vushort*)src); */
  284. data = *((ushort*)src);
  285. if ((rc = write_word(info, wp, data)) != 0) {
  286. return (rc);
  287. }
  288. src += 2;
  289. wp += 2;
  290. cnt -= 2;
  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<2 && cnt>0; ++i, ++cp) {
  298. data = (data >> 8) | (*src++ << 8);
  299. --cnt;
  300. }
  301. for (; i<2; ++i, ++cp) {
  302. data = (data >> 8) | (*(uchar *)cp << 8);
  303. }
  304. return write_word(info, wp, data);
  305. }