flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * (C) Copyright 2002
  3. * Lineo, Inc. <www.lineo.com>
  4. * Bernhard Kuhn <bkuhn@lineo.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Alex Zuepke <azu@sysgo.de>
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. ulong myflush(void);
  30. /* Flash Organization Structure */
  31. typedef struct OrgDef
  32. {
  33. unsigned int sector_number;
  34. unsigned int sector_size;
  35. } OrgDef;
  36. /* Flash Organizations */
  37. OrgDef OrgAT49BV16x4[] =
  38. {
  39. { 8, 8*1024 }, /* 8 * 8kBytes sectors */
  40. { 2, 32*1024 }, /* 2 * 32kBytes sectors */
  41. { 30, 64*1024 } /* 30 * 64kBytes sectors */
  42. };
  43. OrgDef OrgAT49BV16x4A[] =
  44. {
  45. { 8, 8*1024 }, /* 8 * 8kBytes sectors */
  46. { 31, 64*1024 } /* 31 * 64kBytes sectors */
  47. };
  48. #define FLASH_BANK_SIZE 0x200000 /* 2 MB */
  49. #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
  50. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  51. /* AT49BV1614A Codes */
  52. #define FLASH_CODE1 0xAA
  53. #define FLASH_CODE2 0x55
  54. #define ID_IN_CODE 0x90
  55. #define ID_OUT_CODE 0xF0
  56. #define CMD_READ_ARRAY 0x00F0
  57. #define CMD_UNLOCK1 0x00AA
  58. #define CMD_UNLOCK2 0x0055
  59. #define CMD_ERASE_SETUP 0x0080
  60. #define CMD_ERASE_CONFIRM 0x0030
  61. #define CMD_PROGRAM 0x00A0
  62. #define CMD_UNLOCK_BYPASS 0x0020
  63. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00005555<<1)))
  64. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00002AAA<<1)))
  65. #define IDENT_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x0000555<<1)))
  66. #define IDENT_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x0000AAA<<1)))
  67. #define BIT_ERASE_DONE 0x0080
  68. #define BIT_RDY_MASK 0x0080
  69. #define BIT_PROGRAM_ERROR 0x0020
  70. #define BIT_TIMEOUT 0x80000000 /* our flag */
  71. #define READY 1
  72. #define ERR 2
  73. #define TMO 4
  74. /*-----------------------------------------------------------------------
  75. */
  76. void flash_identification (flash_info_t * info)
  77. {
  78. volatile u16 manuf_code, device_code, add_device_code;
  79. IDENT_FLASH_ADDR1 = FLASH_CODE1;
  80. IDENT_FLASH_ADDR2 = FLASH_CODE2;
  81. IDENT_FLASH_ADDR1 = ID_IN_CODE;
  82. manuf_code = *(volatile u16 *) CFG_FLASH_BASE;
  83. device_code = *(volatile u16 *) (CFG_FLASH_BASE + 2);
  84. add_device_code = *(volatile u16 *) (CFG_FLASH_BASE + (3 << 1));
  85. IDENT_FLASH_ADDR1 = FLASH_CODE1;
  86. IDENT_FLASH_ADDR2 = FLASH_CODE2;
  87. IDENT_FLASH_ADDR1 = ID_OUT_CODE;
  88. /* Vendor type */
  89. info->flash_id = ATM_MANUFACT & FLASH_VENDMASK;
  90. printf ("Atmel: ");
  91. if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
  92. if ((add_device_code & FLASH_TYPEMASK) ==
  93. (ATM_ID_BV1614A & FLASH_TYPEMASK)) {
  94. info->flash_id |= ATM_ID_BV1614A & FLASH_TYPEMASK;
  95. printf ("AT49BV1614A (16Mbit)\n");
  96. }
  97. } else { /* AT49BV1614 Flash */
  98. info->flash_id |= ATM_ID_BV1614 & FLASH_TYPEMASK;
  99. printf ("AT49BV1614 (16Mbit)\n");
  100. }
  101. }
  102. ulong flash_init (void)
  103. {
  104. int i, j, k;
  105. unsigned int flash_nb_blocks, sector;
  106. unsigned int start_address;
  107. OrgDef *pOrgDef;
  108. ulong size = 0;
  109. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  110. ulong flashbase = 0;
  111. flash_identification (&flash_info[i]);
  112. flash_info[i].size = FLASH_BANK_SIZE;
  113. if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  114. (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
  115. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  116. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  117. pOrgDef = OrgAT49BV16x4;
  118. flash_nb_blocks = sizeof (OrgAT49BV16x4) / sizeof (OrgDef);
  119. } else { /* AT49BV1614A Flash */
  120. flash_info[i].sector_count = CFG_MAX_FLASH_SECT - 1;
  121. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT - 1);
  122. pOrgDef = OrgAT49BV16x4A;
  123. flash_nb_blocks = sizeof (OrgAT49BV16x4A) / sizeof (OrgDef);
  124. }
  125. if (i == 0)
  126. flashbase = PHYS_FLASH_1;
  127. else
  128. panic ("configured to many flash banks!\n");
  129. sector = 0;
  130. start_address = flashbase;
  131. for (j = 0; j < flash_nb_blocks; j++) {
  132. for (k = 0; k < pOrgDef[j].sector_number; k++) {
  133. flash_info[i].start[sector++] = start_address;
  134. start_address += pOrgDef[j].sector_size;
  135. }
  136. }
  137. size += flash_info[i].size;
  138. }
  139. /* Protect binary boot image */
  140. flash_protect (FLAG_PROTECT_SET,
  141. CFG_FLASH_BASE,
  142. CFG_FLASH_BASE + CFG_BOOT_SIZE - 1, &flash_info[0]);
  143. /* Protect environment variables */
  144. flash_protect (FLAG_PROTECT_SET,
  145. CFG_ENV_ADDR,
  146. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  147. /* Protect U-Boot gzipped image */
  148. flash_protect (FLAG_PROTECT_SET,
  149. CFG_U_BOOT_BASE,
  150. CFG_U_BOOT_BASE + CFG_U_BOOT_SIZE - 1, &flash_info[0]);
  151. return size;
  152. }
  153. /*-----------------------------------------------------------------------
  154. */
  155. void flash_print_info (flash_info_t * info)
  156. {
  157. int i;
  158. switch (info->flash_id & FLASH_VENDMASK) {
  159. case (ATM_MANUFACT & FLASH_VENDMASK):
  160. printf ("Atmel: ");
  161. break;
  162. default:
  163. printf ("Unknown Vendor ");
  164. break;
  165. }
  166. switch (info->flash_id & FLASH_TYPEMASK) {
  167. case (ATM_ID_BV1614 & FLASH_TYPEMASK):
  168. printf ("AT49BV1614 (16Mbit)\n");
  169. break;
  170. case (ATM_ID_BV1614A & FLASH_TYPEMASK):
  171. printf ("AT49BV1614A (16Mbit)\n");
  172. break;
  173. default:
  174. printf ("Unknown Chip Type\n");
  175. goto Done;
  176. break;
  177. }
  178. printf (" Size: %ld MB in %d Sectors\n",
  179. info->size >> 20, info->sector_count);
  180. printf (" Sector Start Addresses:");
  181. for (i = 0; i < info->sector_count; i++) {
  182. if ((i % 5) == 0) {
  183. printf ("\n ");
  184. }
  185. printf (" %08lX%s", info->start[i],
  186. info->protect[i] ? " (RO)" : " ");
  187. }
  188. printf ("\n");
  189. Done:
  190. }
  191. /*-----------------------------------------------------------------------
  192. */
  193. int flash_erase (flash_info_t * info, int s_first, int s_last)
  194. {
  195. ulong result;
  196. int iflag, cflag, prot, sect;
  197. int rc = ERR_OK;
  198. int chip1;
  199. /* first look for protection bits */
  200. if (info->flash_id == FLASH_UNKNOWN)
  201. return ERR_UNKNOWN_FLASH_TYPE;
  202. if ((s_first < 0) || (s_first > s_last)) {
  203. return ERR_INVAL;
  204. }
  205. if ((info->flash_id & FLASH_VENDMASK) !=
  206. (ATM_MANUFACT & FLASH_VENDMASK)) {
  207. return ERR_UNKNOWN_FLASH_VENDOR;
  208. }
  209. prot = 0;
  210. for (sect = s_first; sect <= s_last; ++sect) {
  211. if (info->protect[sect]) {
  212. prot++;
  213. }
  214. }
  215. if (prot)
  216. return ERR_PROTECTED;
  217. /*
  218. * Disable interrupts which might cause a timeout
  219. * here. Remember that our exception vectors are
  220. * at address 0 in the flash, and we don't want a
  221. * (ticker) exception to happen while the flash
  222. * chip is in programming mode.
  223. */
  224. cflag = icache_status ();
  225. icache_disable ();
  226. iflag = disable_interrupts ();
  227. /* Start erase on unprotected sectors */
  228. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  229. printf ("Erasing sector %2d ... ", sect);
  230. /* arm simple, non interrupt dependent timer */
  231. reset_timer_masked ();
  232. if (info->protect[sect] == 0) { /* not protected */
  233. volatile u16 *addr = (volatile u16 *) (info->start[sect]);
  234. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  235. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  236. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  237. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  238. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  239. *addr = CMD_ERASE_CONFIRM;
  240. /* wait until flash is ready */
  241. chip1 = 0;
  242. do {
  243. result = *addr;
  244. /* check timeout */
  245. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  246. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  247. chip1 = TMO;
  248. break;
  249. }
  250. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  251. chip1 = READY;
  252. } while (!chip1);
  253. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  254. if (chip1 == ERR) {
  255. rc = ERR_PROG_ERROR;
  256. goto outahere;
  257. }
  258. if (chip1 == TMO) {
  259. rc = ERR_TIMOUT;
  260. goto outahere;
  261. }
  262. printf ("ok.\n");
  263. } else { /* it was protected */
  264. printf ("protected!\n");
  265. }
  266. }
  267. if (ctrlc ())
  268. printf ("User Interrupt!\n");
  269. outahere:
  270. /* allow flash to settle - wait 10 ms */
  271. udelay_masked (10000);
  272. if (iflag)
  273. enable_interrupts ();
  274. if (cflag)
  275. icache_enable ();
  276. return rc;
  277. }
  278. /*-----------------------------------------------------------------------
  279. * Copy memory to flash
  280. */
  281. volatile static int write_word (flash_info_t * info, ulong dest,
  282. ulong data)
  283. {
  284. volatile u16 *addr = (volatile u16 *) dest;
  285. ulong result;
  286. int rc = ERR_OK;
  287. int cflag, iflag;
  288. int chip1;
  289. /*
  290. * Check if Flash is (sufficiently) erased
  291. */
  292. result = *addr;
  293. if ((result & data) != data)
  294. return ERR_NOT_ERASED;
  295. /*
  296. * Disable interrupts which might cause a timeout
  297. * here. Remember that our exception vectors are
  298. * at address 0 in the flash, and we don't want a
  299. * (ticker) exception to happen while the flash
  300. * chip is in programming mode.
  301. */
  302. cflag = icache_status ();
  303. icache_disable ();
  304. iflag = disable_interrupts ();
  305. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  306. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  307. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  308. *addr = data;
  309. /* arm simple, non interrupt dependent timer */
  310. reset_timer_masked ();
  311. /* wait until flash is ready */
  312. chip1 = 0;
  313. do {
  314. result = *addr;
  315. /* check timeout */
  316. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  317. chip1 = ERR | TMO;
  318. break;
  319. }
  320. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  321. chip1 = READY;
  322. } while (!chip1);
  323. *addr = CMD_READ_ARRAY;
  324. if (chip1 == ERR || *addr != data)
  325. rc = ERR_PROG_ERROR;
  326. if (iflag)
  327. enable_interrupts ();
  328. if (cflag)
  329. icache_enable ();
  330. return rc;
  331. }
  332. /*-----------------------------------------------------------------------
  333. * Copy memory to flash.
  334. */
  335. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  336. {
  337. ulong wp, data;
  338. int rc;
  339. if (addr & 1) {
  340. printf ("unaligned destination not supported\n");
  341. return ERR_ALIGN;
  342. };
  343. if ((int) src & 1) {
  344. printf ("unaligned source not supported\n");
  345. return ERR_ALIGN;
  346. };
  347. wp = addr;
  348. while (cnt >= 2) {
  349. data = *((volatile u16 *) src);
  350. if ((rc = write_word (info, wp, data)) != 0) {
  351. return (rc);
  352. }
  353. src += 2;
  354. wp += 2;
  355. cnt -= 2;
  356. }
  357. if (cnt == 1) {
  358. data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) <<
  359. 8);
  360. if ((rc = write_word (info, wp, data)) != 0) {
  361. return (rc);
  362. }
  363. src += 1;
  364. wp += 1;
  365. cnt -= 1;
  366. };
  367. return ERR_OK;
  368. }