flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. ulong myflush(void);
  26. #define FLASH_BANK_SIZE 0x800000
  27. #define MAIN_SECT_SIZE 0x20000
  28. #define PARAM_SECT_SIZE 0x4000
  29. /* puzzle magic for lart
  30. * data_*_flash are def'd in flashasm.S
  31. */
  32. extern u32 data_from_flash(u32);
  33. extern u32 data_to_flash(u32);
  34. #define PUZZLE_FROM_FLASH(x) data_from_flash((x))
  35. #define PUZZLE_TO_FLASH(x) data_to_flash((x))
  36. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  37. #define CMD_READ_ARRAY 0x00FF00FF
  38. #define CMD_IDENTIFY 0x00900090
  39. #define CMD_ERASE_SETUP 0x00200020
  40. #define CMD_ERASE_CONFIRM 0x00D000D0
  41. #define CMD_PROGRAM 0x00400040
  42. #define CMD_RESUME 0x00D000D0
  43. #define CMD_SUSPEND 0x00B000B0
  44. #define CMD_STATUS_READ 0x00700070
  45. #define CMD_STATUS_RESET 0x00500050
  46. #define BIT_BUSY 0x00800080
  47. #define BIT_ERASE_SUSPEND 0x00400040
  48. #define BIT_ERASE_ERROR 0x00200020
  49. #define BIT_PROGRAM_ERROR 0x00100010
  50. #define BIT_VPP_RANGE_ERROR 0x00080008
  51. #define BIT_PROGRAM_SUSPEND 0x00040004
  52. #define BIT_PROTECT_ERROR 0x00020002
  53. #define BIT_UNDEFINED 0x00010001
  54. #define BIT_SEQUENCE_ERROR 0x00300030
  55. #define BIT_TIMEOUT 0x80000000
  56. /*-----------------------------------------------------------------------
  57. */
  58. ulong flash_init(void)
  59. {
  60. int i, j;
  61. ulong size = 0;
  62. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  63. {
  64. ulong flashbase = 0;
  65. flash_info[i].flash_id =
  66. (INTEL_MANUFACT & FLASH_VENDMASK) |
  67. (INTEL_ID_28F160F3B & FLASH_TYPEMASK);
  68. flash_info[i].size = FLASH_BANK_SIZE;
  69. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  70. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  71. if (i == 0)
  72. flashbase = PHYS_FLASH_1;
  73. else
  74. panic("configured to many flash banks!\n");
  75. for (j = 0; j < flash_info[i].sector_count; j++)
  76. {
  77. if (j <= 7)
  78. {
  79. flash_info[i].start[j] = flashbase + j * PARAM_SECT_SIZE;
  80. }
  81. else
  82. {
  83. flash_info[i].start[j] = flashbase + (j - 7)*MAIN_SECT_SIZE;
  84. }
  85. }
  86. size += flash_info[i].size;
  87. }
  88. /* Protect monitor and environment sectors
  89. */
  90. flash_protect(FLAG_PROTECT_SET,
  91. CFG_FLASH_BASE,
  92. CFG_FLASH_BASE + monitor_flash_len - 1,
  93. &flash_info[0]);
  94. flash_protect(FLAG_PROTECT_SET,
  95. CFG_ENV_ADDR,
  96. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  97. &flash_info[0]);
  98. return size;
  99. }
  100. /*-----------------------------------------------------------------------
  101. */
  102. void flash_print_info (flash_info_t *info)
  103. {
  104. int i;
  105. switch (info->flash_id & FLASH_VENDMASK)
  106. {
  107. case (INTEL_MANUFACT & FLASH_VENDMASK):
  108. printf("Intel: ");
  109. break;
  110. default:
  111. printf("Unknown Vendor ");
  112. break;
  113. }
  114. switch (info->flash_id & FLASH_TYPEMASK)
  115. {
  116. case (INTEL_ID_28F160F3B & FLASH_TYPEMASK):
  117. printf("2x 28F160F3B (16Mbit)\n");
  118. break;
  119. default:
  120. printf("Unknown Chip Type\n");
  121. goto Done;
  122. break;
  123. }
  124. printf(" Size: %ld MB in %d Sectors\n",
  125. info->size >> 20, info->sector_count);
  126. printf(" Sector Start Addresses:");
  127. for (i = 0; i < info->sector_count; i++)
  128. {
  129. if ((i % 5) == 0)
  130. {
  131. printf ("\n ");
  132. }
  133. printf (" %08lX%s", info->start[i],
  134. info->protect[i] ? " (RO)" : " ");
  135. }
  136. printf ("\n");
  137. Done:
  138. }
  139. /*-----------------------------------------------------------------------
  140. */
  141. int flash_error (ulong code)
  142. {
  143. /* Check bit patterns */
  144. /* SR.7=0 is busy, SR.7=1 is ready */
  145. /* all other flags indicate error on 1 */
  146. /* SR.0 is undefined */
  147. /* Timeout is our faked flag */
  148. /* sequence is described in Intel 290644-005 document */
  149. /* check Timeout */
  150. if (code & BIT_TIMEOUT)
  151. {
  152. printf ("Timeout\n");
  153. return ERR_TIMOUT;
  154. }
  155. /* check Busy, SR.7 */
  156. if (~code & BIT_BUSY)
  157. {
  158. printf ("Busy\n");
  159. return ERR_PROG_ERROR;
  160. }
  161. /* check Vpp low, SR.3 */
  162. if (code & BIT_VPP_RANGE_ERROR)
  163. {
  164. printf ("Vpp range error\n");
  165. return ERR_PROG_ERROR;
  166. }
  167. /* check Device Protect Error, SR.1 */
  168. if (code & BIT_PROTECT_ERROR)
  169. {
  170. printf ("Device protect error\n");
  171. return ERR_PROG_ERROR;
  172. }
  173. /* check Command Seq Error, SR.4 & SR.5 */
  174. if (code & BIT_SEQUENCE_ERROR)
  175. {
  176. printf ("Command seqence error\n");
  177. return ERR_PROG_ERROR;
  178. }
  179. /* check Block Erase Error, SR.5 */
  180. if (code & BIT_ERASE_ERROR)
  181. {
  182. printf ("Block erase error\n");
  183. return ERR_PROG_ERROR;
  184. }
  185. /* check Program Error, SR.4 */
  186. if (code & BIT_PROGRAM_ERROR)
  187. {
  188. printf ("Program error\n");
  189. return ERR_PROG_ERROR;
  190. }
  191. /* check Block Erase Suspended, SR.6 */
  192. if (code & BIT_ERASE_SUSPEND)
  193. {
  194. printf ("Block erase suspended\n");
  195. return ERR_PROG_ERROR;
  196. }
  197. /* check Program Suspended, SR.2 */
  198. if (code & BIT_PROGRAM_SUSPEND)
  199. {
  200. printf ("Program suspended\n");
  201. return ERR_PROG_ERROR;
  202. }
  203. /* OK, no error */
  204. return ERR_OK;
  205. }
  206. /*-----------------------------------------------------------------------
  207. */
  208. int flash_erase (flash_info_t *info, int s_first, int s_last)
  209. {
  210. ulong result;
  211. int iflag, cflag, prot, sect;
  212. int rc = ERR_OK;
  213. /* first look for protection bits */
  214. if (info->flash_id == FLASH_UNKNOWN)
  215. return ERR_UNKNOWN_FLASH_TYPE;
  216. if ((s_first < 0) || (s_first > s_last)) {
  217. return ERR_INVAL;
  218. }
  219. if ((info->flash_id & FLASH_VENDMASK) !=
  220. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  221. return ERR_UNKNOWN_FLASH_VENDOR;
  222. }
  223. prot = 0;
  224. for (sect=s_first; sect<=s_last; ++sect) {
  225. if (info->protect[sect]) {
  226. prot++;
  227. }
  228. }
  229. if (prot)
  230. return ERR_PROTECTED;
  231. /*
  232. * Disable interrupts which might cause a timeout
  233. * here. Remember that our exception vectors are
  234. * at address 0 in the flash, and we don't want a
  235. * (ticker) exception to happen while the flash
  236. * chip is in programming mode.
  237. */
  238. cflag = icache_status();
  239. icache_disable();
  240. iflag = disable_interrupts();
  241. /* Start erase on unprotected sectors */
  242. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  243. {
  244. printf("Erasing sector %2d ... ", sect);
  245. /* arm simple, non interrupt dependent timer */
  246. reset_timer_masked();
  247. if (info->protect[sect] == 0)
  248. { /* not protected */
  249. vu_long *addr = (vu_long *)(info->start[sect]);
  250. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  251. *addr = PUZZLE_TO_FLASH(CMD_ERASE_SETUP);
  252. *addr = PUZZLE_TO_FLASH(CMD_ERASE_CONFIRM);
  253. /* wait until flash is ready */
  254. do
  255. {
  256. /* check timeout */
  257. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  258. {
  259. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  260. result = BIT_TIMEOUT;
  261. break;
  262. }
  263. result = PUZZLE_FROM_FLASH(*addr);
  264. } while (~result & BIT_BUSY);
  265. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  266. if ((rc = flash_error(result)) != ERR_OK)
  267. goto outahere;
  268. printf("ok.\n");
  269. }
  270. else /* it was protected */
  271. {
  272. printf("protected!\n");
  273. }
  274. }
  275. if (ctrlc())
  276. printf("User Interrupt!\n");
  277. outahere:
  278. /* allow flash to settle - wait 10 ms */
  279. udelay_masked(10000);
  280. if (iflag)
  281. enable_interrupts();
  282. if (cflag)
  283. icache_enable();
  284. return rc;
  285. }
  286. /*-----------------------------------------------------------------------
  287. * Copy memory to flash
  288. */
  289. volatile static int write_word (flash_info_t *info, ulong dest, ulong data)
  290. {
  291. vu_long *addr = (vu_long *)dest;
  292. ulong result;
  293. int rc = ERR_OK;
  294. int cflag, iflag;
  295. /* Check if Flash is (sufficiently) erased
  296. */
  297. result = PUZZLE_FROM_FLASH(*addr);
  298. if ((result & data) != data)
  299. return ERR_NOT_ERASED;
  300. /*
  301. * Disable interrupts which might cause a timeout
  302. * here. Remember that our exception vectors are
  303. * at address 0 in the flash, and we don't want a
  304. * (ticker) exception to happen while the flash
  305. * chip is in programming mode.
  306. */
  307. cflag = icache_status();
  308. icache_disable();
  309. iflag = disable_interrupts();
  310. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  311. *addr = PUZZLE_TO_FLASH(CMD_PROGRAM);
  312. *addr = data;
  313. /* arm simple, non interrupt dependent timer */
  314. reset_timer_masked();
  315. /* wait until flash is ready */
  316. do
  317. {
  318. /* check timeout */
  319. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  320. {
  321. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  322. result = BIT_TIMEOUT;
  323. break;
  324. }
  325. result = PUZZLE_FROM_FLASH(*addr);
  326. } while (~result & BIT_BUSY);
  327. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  328. rc = flash_error(result);
  329. if (iflag)
  330. enable_interrupts();
  331. if (cflag)
  332. icache_enable();
  333. return rc;
  334. }
  335. /*-----------------------------------------------------------------------
  336. * Copy memory to flash.
  337. */
  338. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  339. {
  340. ulong cp, wp, data;
  341. int l;
  342. int i, rc;
  343. wp = (addr & ~3); /* get lower word aligned address */
  344. /*
  345. * handle unaligned start bytes
  346. */
  347. if ((l = addr - wp) != 0) {
  348. data = 0;
  349. for (i=0, cp=wp; i<l; ++i, ++cp) {
  350. data = (data >> 8) | (*(uchar *)cp << 24);
  351. }
  352. for (; i<4 && cnt>0; ++i) {
  353. data = (data >> 8) | (*src++ << 24);
  354. --cnt;
  355. ++cp;
  356. }
  357. for (; cnt==0 && i<4; ++i, ++cp) {
  358. data = (data >> 8) | (*(uchar *)cp << 24);
  359. }
  360. if ((rc = write_word(info, wp, data)) != 0) {
  361. return (rc);
  362. }
  363. wp += 4;
  364. }
  365. /*
  366. * handle word aligned part
  367. */
  368. while (cnt >= 4) {
  369. data = *((vu_long*)src);
  370. if ((rc = write_word(info, wp, data)) != 0) {
  371. return (rc);
  372. }
  373. src += 4;
  374. wp += 4;
  375. cnt -= 4;
  376. }
  377. if (cnt == 0) {
  378. return ERR_OK;
  379. }
  380. /*
  381. * handle unaligned tail bytes
  382. */
  383. data = 0;
  384. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  385. data = (data >> 8) | (*src++ << 24);
  386. --cnt;
  387. }
  388. for (; i<4; ++i, ++cp) {
  389. data = (data >> 8) | (*(uchar *)cp << 24);
  390. }
  391. return write_word(info, wp, data);
  392. }