flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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[CONFIG_SYS_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 < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  70. memset(flash_info[i].protect, 0, CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  92. CONFIG_SYS_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. ulong start;
  215. /* first look for protection bits */
  216. if (info->flash_id == FLASH_UNKNOWN)
  217. return ERR_UNKNOWN_FLASH_TYPE;
  218. if ((s_first < 0) || (s_first > s_last)) {
  219. return ERR_INVAL;
  220. }
  221. if ((info->flash_id & FLASH_VENDMASK) !=
  222. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  223. return ERR_UNKNOWN_FLASH_VENDOR;
  224. }
  225. prot = 0;
  226. for (sect=s_first; sect<=s_last; ++sect) {
  227. if (info->protect[sect]) {
  228. prot++;
  229. }
  230. }
  231. if (prot)
  232. return ERR_PROTECTED;
  233. /*
  234. * Disable interrupts which might cause a timeout
  235. * here. Remember that our exception vectors are
  236. * at address 0 in the flash, and we don't want a
  237. * (ticker) exception to happen while the flash
  238. * chip is in programming mode.
  239. */
  240. cflag = icache_status();
  241. icache_disable();
  242. iflag = disable_interrupts();
  243. /* Start erase on unprotected sectors */
  244. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  245. {
  246. printf("Erasing sector %2d ... ", sect);
  247. /* arm simple, non interrupt dependent timer */
  248. start = get_timer(0);
  249. if (info->protect[sect] == 0)
  250. { /* not protected */
  251. vu_long *addr = (vu_long *)(info->start[sect]);
  252. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  253. *addr = PUZZLE_TO_FLASH(CMD_ERASE_SETUP);
  254. *addr = PUZZLE_TO_FLASH(CMD_ERASE_CONFIRM);
  255. /* wait until flash is ready */
  256. do
  257. {
  258. /* check timeout */
  259. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT)
  260. {
  261. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  262. result = BIT_TIMEOUT;
  263. break;
  264. }
  265. result = PUZZLE_FROM_FLASH(*addr);
  266. } while (~result & BIT_BUSY);
  267. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  268. if ((rc = flash_error(result)) != ERR_OK)
  269. goto outahere;
  270. printf("ok.\n");
  271. }
  272. else /* it was protected */
  273. {
  274. printf("protected!\n");
  275. }
  276. }
  277. if (ctrlc())
  278. printf("User Interrupt!\n");
  279. outahere:
  280. /* allow flash to settle - wait 10 ms */
  281. udelay_masked(10000);
  282. if (iflag)
  283. enable_interrupts();
  284. if (cflag)
  285. icache_enable();
  286. return rc;
  287. }
  288. /*-----------------------------------------------------------------------
  289. * Copy memory to flash
  290. */
  291. static int write_word (flash_info_t *info, ulong dest, ulong data)
  292. {
  293. vu_long *addr = (vu_long *)dest;
  294. ulong result;
  295. int rc = ERR_OK;
  296. int cflag, iflag;
  297. ulong start;
  298. /* Check if Flash is (sufficiently) erased
  299. */
  300. result = PUZZLE_FROM_FLASH(*addr);
  301. if ((result & data) != data)
  302. return ERR_NOT_ERASED;
  303. /*
  304. * Disable interrupts which might cause a timeout
  305. * here. Remember that our exception vectors are
  306. * at address 0 in the flash, and we don't want a
  307. * (ticker) exception to happen while the flash
  308. * chip is in programming mode.
  309. */
  310. cflag = icache_status();
  311. icache_disable();
  312. iflag = disable_interrupts();
  313. *addr = PUZZLE_TO_FLASH(CMD_STATUS_RESET);
  314. *addr = PUZZLE_TO_FLASH(CMD_PROGRAM);
  315. *addr = data;
  316. /* arm simple, non interrupt dependent timer */
  317. start = get_timer(0);
  318. /* wait until flash is ready */
  319. do
  320. {
  321. /* check timeout */
  322. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT)
  323. {
  324. *addr = PUZZLE_TO_FLASH(CMD_SUSPEND);
  325. result = BIT_TIMEOUT;
  326. break;
  327. }
  328. result = PUZZLE_FROM_FLASH(*addr);
  329. } while (~result & BIT_BUSY);
  330. *addr = PUZZLE_TO_FLASH(CMD_READ_ARRAY);
  331. rc = flash_error(result);
  332. if (iflag)
  333. enable_interrupts();
  334. if (cflag)
  335. icache_enable();
  336. return rc;
  337. }
  338. /*-----------------------------------------------------------------------
  339. * Copy memory to flash.
  340. */
  341. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  342. {
  343. ulong cp, wp, data;
  344. int l;
  345. int i, rc;
  346. wp = (addr & ~3); /* get lower word aligned address */
  347. /*
  348. * handle unaligned start bytes
  349. */
  350. if ((l = addr - wp) != 0) {
  351. data = 0;
  352. for (i=0, cp=wp; i<l; ++i, ++cp) {
  353. data = (data >> 8) | (*(uchar *)cp << 24);
  354. }
  355. for (; i<4 && cnt>0; ++i) {
  356. data = (data >> 8) | (*src++ << 24);
  357. --cnt;
  358. ++cp;
  359. }
  360. for (; cnt==0 && i<4; ++i, ++cp) {
  361. data = (data >> 8) | (*(uchar *)cp << 24);
  362. }
  363. if ((rc = write_word(info, wp, data)) != 0) {
  364. return (rc);
  365. }
  366. wp += 4;
  367. }
  368. /*
  369. * handle word aligned part
  370. */
  371. while (cnt >= 4) {
  372. data = *((vu_long*)src);
  373. if ((rc = write_word(info, wp, data)) != 0) {
  374. return (rc);
  375. }
  376. src += 4;
  377. wp += 4;
  378. cnt -= 4;
  379. }
  380. if (cnt == 0) {
  381. return ERR_OK;
  382. }
  383. /*
  384. * handle unaligned tail bytes
  385. */
  386. data = 0;
  387. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  388. data = (data >> 8) | (*src++ << 24);
  389. --cnt;
  390. }
  391. for (; i<4; ++i, ++cp) {
  392. data = (data >> 8) | (*(uchar *)cp << 24);
  393. }
  394. return write_word(info, wp, data);
  395. }