flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 too 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. CONFIG_ENV_ADDR,
  96. CONFIG_ENV_ADDR + CONFIG_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. */
  142. int flash_error (ulong code)
  143. {
  144. /* Check bit patterns */
  145. /* SR.7=0 is busy, SR.7=1 is ready */
  146. /* all other flags indicate error on 1 */
  147. /* SR.0 is undefined */
  148. /* Timeout is our faked flag */
  149. /* sequence is described in Intel 290644-005 document */
  150. /* check Timeout */
  151. if (code & BIT_TIMEOUT)
  152. {
  153. printf ("Timeout\n");
  154. return ERR_TIMOUT;
  155. }
  156. /* check Busy, SR.7 */
  157. if (~code & BIT_BUSY)
  158. {
  159. printf ("Busy\n");
  160. return ERR_PROG_ERROR;
  161. }
  162. /* check Vpp low, SR.3 */
  163. if (code & BIT_VPP_RANGE_ERROR)
  164. {
  165. printf ("Vpp range error\n");
  166. return ERR_PROG_ERROR;
  167. }
  168. /* check Device Protect Error, SR.1 */
  169. if (code & BIT_PROTECT_ERROR)
  170. {
  171. printf ("Device protect error\n");
  172. return ERR_PROG_ERROR;
  173. }
  174. /* check Command Seq Error, SR.4 & SR.5 */
  175. if (code & BIT_SEQUENCE_ERROR)
  176. {
  177. printf ("Command seqence error\n");
  178. return ERR_PROG_ERROR;
  179. }
  180. /* check Block Erase Error, SR.5 */
  181. if (code & BIT_ERASE_ERROR)
  182. {
  183. printf ("Block erase error\n");
  184. return ERR_PROG_ERROR;
  185. }
  186. /* check Program Error, SR.4 */
  187. if (code & BIT_PROGRAM_ERROR)
  188. {
  189. printf ("Program error\n");
  190. return ERR_PROG_ERROR;
  191. }
  192. /* check Block Erase Suspended, SR.6 */
  193. if (code & BIT_ERASE_SUSPEND)
  194. {
  195. printf ("Block erase suspended\n");
  196. return ERR_PROG_ERROR;
  197. }
  198. /* check Program Suspended, SR.2 */
  199. if (code & BIT_PROGRAM_SUSPEND)
  200. {
  201. printf ("Program suspended\n");
  202. return ERR_PROG_ERROR;
  203. }
  204. /* OK, no error */
  205. return ERR_OK;
  206. }
  207. /*-----------------------------------------------------------------------
  208. */
  209. int flash_erase (flash_info_t *info, int s_first, int s_last)
  210. {
  211. ulong result;
  212. int iflag, cflag, prot, sect;
  213. int rc = ERR_OK;
  214. /* first look for protection bits */
  215. if (info->flash_id == FLASH_UNKNOWN)
  216. return ERR_UNKNOWN_FLASH_TYPE;
  217. if ((s_first < 0) || (s_first > s_last)) {
  218. return ERR_INVAL;
  219. }
  220. if ((info->flash_id & FLASH_VENDMASK) !=
  221. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  222. return ERR_UNKNOWN_FLASH_VENDOR;
  223. }
  224. prot = 0;
  225. for (sect=s_first; sect<=s_last; ++sect) {
  226. if (info->protect[sect]) {
  227. prot++;
  228. }
  229. }
  230. if (prot)
  231. return ERR_PROTECTED;
  232. /*
  233. * Disable interrupts which might cause a timeout
  234. * here. Remember that our exception vectors are
  235. * at address 0 in the flash, and we don't want a
  236. * (ticker) exception to happen while the flash
  237. * chip is in programming mode.
  238. */
  239. cflag = icache_status();
  240. icache_disable();
  241. iflag = disable_interrupts();
  242. /* Start erase on unprotected sectors */
  243. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  244. {
  245. printf("Erasing sector %2d ... ", sect);
  246. /* arm simple, non interrupt dependent timer */
  247. reset_timer_masked();
  248. if (info->protect[sect] == 0)
  249. { /* not protected */
  250. vu_long *addr = (vu_long *)(info->start[sect]);
  251. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  252. *addr = PUZZLE_TO_FLASH(CMD_ERASE_SETUP);
  253. *addr = PUZZLE_TO_FLASH(CMD_ERASE_CONFIRM);
  254. /* wait until flash is ready */
  255. do
  256. {
  257. /* check timeout */
  258. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  259. {
  260. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  261. result = BIT_TIMEOUT;
  262. break;
  263. }
  264. result = PUZZLE_FROM_FLASH(*addr);
  265. } while (~result & BIT_BUSY);
  266. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  267. if ((rc = flash_error(result)) != ERR_OK)
  268. goto outahere;
  269. printf("ok.\n");
  270. }
  271. else /* it was protected */
  272. {
  273. printf("protected!\n");
  274. }
  275. }
  276. if (ctrlc())
  277. printf("User Interrupt!\n");
  278. outahere:
  279. /* allow flash to settle - wait 10 ms */
  280. udelay_masked(10000);
  281. if (iflag)
  282. enable_interrupts();
  283. if (cflag)
  284. icache_enable();
  285. return rc;
  286. }
  287. /*-----------------------------------------------------------------------
  288. * Copy memory to flash
  289. */
  290. static int write_word (flash_info_t *info, ulong dest, ulong data)
  291. {
  292. vu_long *addr = (vu_long *)dest;
  293. ulong result;
  294. int rc = ERR_OK;
  295. int cflag, iflag;
  296. /* Check if Flash is (sufficiently) erased
  297. */
  298. result = PUZZLE_FROM_FLASH(*addr);
  299. if ((result & data) != data)
  300. return ERR_NOT_ERASED;
  301. /*
  302. * Disable interrupts which might cause a timeout
  303. * here. Remember that our exception vectors are
  304. * at address 0 in the flash, and we don't want a
  305. * (ticker) exception to happen while the flash
  306. * chip is in programming mode.
  307. */
  308. cflag = icache_status();
  309. icache_disable();
  310. iflag = disable_interrupts();
  311. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  312. *addr = PUZZLE_TO_FLASH(CMD_PROGRAM);
  313. *addr = data;
  314. /* arm simple, non interrupt dependent timer */
  315. reset_timer_masked();
  316. /* wait until flash is ready */
  317. do
  318. {
  319. /* check timeout */
  320. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  321. {
  322. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  323. result = BIT_TIMEOUT;
  324. break;
  325. }
  326. result = PUZZLE_FROM_FLASH(*addr);
  327. } while (~result & BIT_BUSY);
  328. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  329. rc = flash_error(result);
  330. if (iflag)
  331. enable_interrupts();
  332. if (cflag)
  333. icache_enable();
  334. return rc;
  335. }
  336. /*-----------------------------------------------------------------------
  337. * Copy memory to flash.
  338. */
  339. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  340. {
  341. ulong cp, wp, data;
  342. int l;
  343. int i, rc;
  344. wp = (addr & ~3); /* get lower word aligned address */
  345. /*
  346. * handle unaligned start bytes
  347. */
  348. if ((l = addr - wp) != 0) {
  349. data = 0;
  350. for (i=0, cp=wp; i<l; ++i, ++cp) {
  351. data = (data >> 8) | (*(uchar *)cp << 24);
  352. }
  353. for (; i<4 && cnt>0; ++i) {
  354. data = (data >> 8) | (*src++ << 24);
  355. --cnt;
  356. ++cp;
  357. }
  358. for (; cnt==0 && i<4; ++i, ++cp) {
  359. data = (data >> 8) | (*(uchar *)cp << 24);
  360. }
  361. if ((rc = write_word(info, wp, data)) != 0) {
  362. return (rc);
  363. }
  364. wp += 4;
  365. }
  366. /*
  367. * handle word aligned part
  368. */
  369. while (cnt >= 4) {
  370. data = *((vu_long*)src);
  371. if ((rc = write_word(info, wp, data)) != 0) {
  372. return (rc);
  373. }
  374. src += 4;
  375. wp += 4;
  376. cnt -= 4;
  377. }
  378. if (cnt == 0) {
  379. return ERR_OK;
  380. }
  381. /*
  382. * handle unaligned tail bytes
  383. */
  384. data = 0;
  385. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  386. data = (data >> 8) | (*src++ << 24);
  387. --cnt;
  388. }
  389. for (; i<4; ++i, ++cp) {
  390. data = (data >> 8) | (*(uchar *)cp << 24);
  391. }
  392. return write_word(info, wp, data);
  393. }