flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 * 8 kBytes sectors */
  40. { 2, 32*1024 }, /* 2 * 32 kBytes sectors */
  41. { 30, 64*1024 }, /* 30 * 64 kBytes sectors */
  42. };
  43. OrgDef OrgAT49BV16x4A[] =
  44. {
  45. { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
  46. { 31, 64*1024 }, /* 31 * 64 kBytes sectors */
  47. };
  48. OrgDef OrgAT49BV6416[] =
  49. {
  50. { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
  51. { 127, 64*1024 }, /* 127 * 64 kBytes sectors */
  52. };
  53. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  54. /* AT49BV1614A Codes */
  55. #define FLASH_CODE1 0xAA
  56. #define FLASH_CODE2 0x55
  57. #define ID_IN_CODE 0x90
  58. #define ID_OUT_CODE 0xF0
  59. #define CMD_READ_ARRAY 0x00F0
  60. #define CMD_UNLOCK1 0x00AA
  61. #define CMD_UNLOCK2 0x0055
  62. #define CMD_ERASE_SETUP 0x0080
  63. #define CMD_ERASE_CONFIRM 0x0030
  64. #define CMD_PROGRAM 0x00A0
  65. #define CMD_UNLOCK_BYPASS 0x0020
  66. #define CMD_SECTOR_UNLOCK 0x0070
  67. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00005555<<1)))
  68. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00002AAA<<1)))
  69. #define BIT_ERASE_DONE 0x0080
  70. #define BIT_RDY_MASK 0x0080
  71. #define BIT_PROGRAM_ERROR 0x0020
  72. #define BIT_TIMEOUT 0x80000000 /* our flag */
  73. #define READY 1
  74. #define ERR 2
  75. #define TMO 4
  76. /*-----------------------------------------------------------------------
  77. */
  78. void flash_identification (flash_info_t * info)
  79. {
  80. volatile u16 manuf_code, device_code, add_device_code;
  81. MEM_FLASH_ADDR1 = FLASH_CODE1;
  82. MEM_FLASH_ADDR2 = FLASH_CODE2;
  83. MEM_FLASH_ADDR1 = ID_IN_CODE;
  84. manuf_code = *(volatile u16 *) CONFIG_SYS_FLASH_BASE;
  85. device_code = *(volatile u16 *) (CONFIG_SYS_FLASH_BASE + 2);
  86. add_device_code = *(volatile u16 *) (CONFIG_SYS_FLASH_BASE + (3 << 1));
  87. MEM_FLASH_ADDR1 = FLASH_CODE1;
  88. MEM_FLASH_ADDR2 = FLASH_CODE2;
  89. MEM_FLASH_ADDR1 = ID_OUT_CODE;
  90. /* Vendor type */
  91. info->flash_id = ATM_MANUFACT & FLASH_VENDMASK;
  92. printf ("Atmel: ");
  93. if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
  94. if ((add_device_code & FLASH_TYPEMASK) ==
  95. (ATM_ID_BV1614A & FLASH_TYPEMASK)) {
  96. info->flash_id |= ATM_ID_BV1614A & FLASH_TYPEMASK;
  97. printf ("AT49BV1614A (16Mbit)\n");
  98. } else { /* AT49BV1614 Flash */
  99. info->flash_id |= ATM_ID_BV1614 & FLASH_TYPEMASK;
  100. printf ("AT49BV1614 (16Mbit)\n");
  101. }
  102. } else if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV6416 & FLASH_TYPEMASK)) {
  103. info->flash_id |= ATM_ID_BV6416 & FLASH_TYPEMASK;
  104. printf ("AT49BV6416 (64Mbit)\n");
  105. }
  106. }
  107. ushort flash_number_sector(OrgDef *pOrgDef, unsigned int nb_blocks)
  108. {
  109. int i, nb_sectors = 0;
  110. for (i=0; i<nb_blocks; i++){
  111. nb_sectors += pOrgDef[i].sector_number;
  112. }
  113. return nb_sectors;
  114. }
  115. void flash_unlock_sector(flash_info_t * info, unsigned int sector)
  116. {
  117. volatile u16 *addr = (volatile u16 *) (info->start[sector]);
  118. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  119. *addr = CMD_SECTOR_UNLOCK;
  120. }
  121. ulong flash_init (void)
  122. {
  123. int i, j, k;
  124. unsigned int flash_nb_blocks, sector;
  125. unsigned int start_address;
  126. OrgDef *pOrgDef;
  127. ulong size = 0;
  128. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  129. ulong flashbase = 0;
  130. flash_identification (&flash_info[i]);
  131. if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  132. (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
  133. pOrgDef = OrgAT49BV16x4;
  134. flash_nb_blocks = sizeof (OrgAT49BV16x4) / sizeof (OrgDef);
  135. } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  136. (ATM_ID_BV1614A & FLASH_TYPEMASK)){ /* AT49BV1614A Flash */
  137. pOrgDef = OrgAT49BV16x4A;
  138. flash_nb_blocks = sizeof (OrgAT49BV16x4A) / sizeof (OrgDef);
  139. } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  140. (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */
  141. pOrgDef = OrgAT49BV6416;
  142. flash_nb_blocks = sizeof (OrgAT49BV6416) / sizeof (OrgDef);
  143. } else {
  144. flash_nb_blocks = 0;
  145. pOrgDef = OrgAT49BV16x4;
  146. }
  147. flash_info[i].sector_count = flash_number_sector(pOrgDef, flash_nb_blocks);
  148. memset (flash_info[i].protect, 0, flash_info[i].sector_count);
  149. if (i == 0)
  150. flashbase = PHYS_FLASH_1;
  151. else
  152. panic ("configured too many flash banks!\n");
  153. sector = 0;
  154. start_address = flashbase;
  155. flash_info[i].size = 0;
  156. for (j = 0; j < flash_nb_blocks; j++) {
  157. for (k = 0; k < pOrgDef[j].sector_number; k++) {
  158. flash_info[i].start[sector++] = start_address;
  159. start_address += pOrgDef[j].sector_size;
  160. flash_info[i].size += pOrgDef[j].sector_size;
  161. }
  162. }
  163. size += flash_info[i].size;
  164. if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  165. (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */
  166. /* Unlock all sectors at reset */
  167. for (j=0; j<flash_info[i].sector_count; j++){
  168. flash_unlock_sector(&flash_info[i], j);
  169. }
  170. }
  171. }
  172. /* Protect binary boot image */
  173. flash_protect (FLAG_PROTECT_SET,
  174. CONFIG_SYS_FLASH_BASE,
  175. CONFIG_SYS_FLASH_BASE + CONFIG_SYS_BOOT_SIZE - 1, &flash_info[0]);
  176. /* Protect environment variables */
  177. flash_protect (FLAG_PROTECT_SET,
  178. CONFIG_ENV_ADDR,
  179. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  180. /* Protect U-Boot gzipped image */
  181. flash_protect (FLAG_PROTECT_SET,
  182. CONFIG_SYS_U_BOOT_BASE,
  183. CONFIG_SYS_U_BOOT_BASE + CONFIG_SYS_U_BOOT_SIZE - 1, &flash_info[0]);
  184. return size;
  185. }
  186. /*-----------------------------------------------------------------------
  187. */
  188. void flash_print_info (flash_info_t * info)
  189. {
  190. int i;
  191. switch (info->flash_id & FLASH_VENDMASK) {
  192. case (ATM_MANUFACT & FLASH_VENDMASK):
  193. printf ("Atmel: ");
  194. break;
  195. default:
  196. printf ("Unknown Vendor ");
  197. break;
  198. }
  199. switch (info->flash_id & FLASH_TYPEMASK) {
  200. case (ATM_ID_BV1614 & FLASH_TYPEMASK):
  201. printf ("AT49BV1614 (16Mbit)\n");
  202. break;
  203. case (ATM_ID_BV1614A & FLASH_TYPEMASK):
  204. printf ("AT49BV1614A (16Mbit)\n");
  205. break;
  206. case (ATM_ID_BV6416 & FLASH_TYPEMASK):
  207. printf ("AT49BV6416 (64Mbit)\n");
  208. break;
  209. default:
  210. printf ("Unknown Chip Type\n");
  211. return;
  212. }
  213. printf (" Size: %ld MB in %d Sectors\n",
  214. info->size >> 20, info->sector_count);
  215. printf (" Sector Start Addresses:");
  216. for (i = 0; i < info->sector_count; i++) {
  217. if ((i % 5) == 0) {
  218. printf ("\n ");
  219. }
  220. printf (" %08lX%s", info->start[i],
  221. info->protect[i] ? " (RO)" : " ");
  222. }
  223. printf ("\n");
  224. }
  225. /*-----------------------------------------------------------------------
  226. */
  227. int flash_erase (flash_info_t * info, int s_first, int s_last)
  228. {
  229. ulong result;
  230. int iflag, cflag, prot, sect;
  231. int rc = ERR_OK;
  232. int chip1;
  233. ulong start;
  234. /* first look for protection bits */
  235. if (info->flash_id == FLASH_UNKNOWN)
  236. return ERR_UNKNOWN_FLASH_TYPE;
  237. if ((s_first < 0) || (s_first > s_last)) {
  238. return ERR_INVAL;
  239. }
  240. if ((info->flash_id & FLASH_VENDMASK) !=
  241. (ATM_MANUFACT & FLASH_VENDMASK)) {
  242. return ERR_UNKNOWN_FLASH_VENDOR;
  243. }
  244. prot = 0;
  245. for (sect = s_first; sect <= s_last; ++sect) {
  246. if (info->protect[sect]) {
  247. prot++;
  248. }
  249. }
  250. if (prot)
  251. return ERR_PROTECTED;
  252. /*
  253. * Disable interrupts which might cause a timeout
  254. * here. Remember that our exception vectors are
  255. * at address 0 in the flash, and we don't want a
  256. * (ticker) exception to happen while the flash
  257. * chip is in programming mode.
  258. */
  259. cflag = icache_status ();
  260. icache_disable ();
  261. iflag = disable_interrupts ();
  262. /* Start erase on unprotected sectors */
  263. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  264. printf ("Erasing sector %2d ... ", sect);
  265. /* arm simple, non interrupt dependent timer */
  266. start = get_timer(0);
  267. if (info->protect[sect] == 0) { /* not protected */
  268. volatile u16 *addr = (volatile u16 *) (info->start[sect]);
  269. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  270. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  271. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  272. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  273. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  274. *addr = CMD_ERASE_CONFIRM;
  275. /* wait until flash is ready */
  276. chip1 = 0;
  277. do {
  278. result = *addr;
  279. /* check timeout */
  280. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  281. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  282. chip1 = TMO;
  283. break;
  284. }
  285. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  286. chip1 = READY;
  287. } while (!chip1);
  288. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  289. if (chip1 == ERR) {
  290. rc = ERR_PROG_ERROR;
  291. goto outahere;
  292. }
  293. if (chip1 == TMO) {
  294. rc = ERR_TIMOUT;
  295. goto outahere;
  296. }
  297. printf ("ok.\n");
  298. } else { /* it was protected */
  299. printf ("protected!\n");
  300. }
  301. }
  302. if (ctrlc ())
  303. printf ("User Interrupt!\n");
  304. outahere:
  305. /* allow flash to settle - wait 10 ms */
  306. udelay_masked (10000);
  307. if (iflag)
  308. enable_interrupts ();
  309. if (cflag)
  310. icache_enable ();
  311. return rc;
  312. }
  313. /*-----------------------------------------------------------------------
  314. * Copy memory to flash
  315. */
  316. static int write_word (flash_info_t * info, ulong dest, ulong data)
  317. {
  318. volatile u16 *addr = (volatile u16 *) dest;
  319. ulong result;
  320. int rc = ERR_OK;
  321. int cflag, iflag;
  322. int chip1;
  323. ulong start;
  324. /*
  325. * Check if Flash is (sufficiently) erased
  326. */
  327. result = *addr;
  328. if ((result & data) != data)
  329. return ERR_NOT_ERASED;
  330. /*
  331. * Disable interrupts which might cause a timeout
  332. * here. Remember that our exception vectors are
  333. * at address 0 in the flash, and we don't want a
  334. * (ticker) exception to happen while the flash
  335. * chip is in programming mode.
  336. */
  337. cflag = icache_status ();
  338. icache_disable ();
  339. iflag = disable_interrupts ();
  340. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  341. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  342. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  343. *addr = data;
  344. /* arm simple, non interrupt dependent timer */
  345. start = get_timer(0);
  346. /* wait until flash is ready */
  347. chip1 = 0;
  348. do {
  349. result = *addr;
  350. /* check timeout */
  351. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  352. chip1 = ERR | TMO;
  353. break;
  354. }
  355. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  356. chip1 = READY;
  357. } while (!chip1);
  358. *addr = CMD_READ_ARRAY;
  359. if (chip1 == ERR || *addr != data)
  360. rc = ERR_PROG_ERROR;
  361. if (iflag)
  362. enable_interrupts ();
  363. if (cflag)
  364. icache_enable ();
  365. return rc;
  366. }
  367. /*-----------------------------------------------------------------------
  368. * Copy memory to flash.
  369. */
  370. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  371. {
  372. ulong wp, data;
  373. int rc;
  374. if (addr & 1) {
  375. printf ("unaligned destination not supported\n");
  376. return ERR_ALIGN;
  377. };
  378. if ((int) src & 1) {
  379. printf ("unaligned source not supported\n");
  380. return ERR_ALIGN;
  381. };
  382. wp = addr;
  383. while (cnt >= 2) {
  384. data = *((volatile u16 *) src);
  385. if ((rc = write_word (info, wp, data)) != 0) {
  386. return (rc);
  387. }
  388. src += 2;
  389. wp += 2;
  390. cnt -= 2;
  391. }
  392. if (cnt == 1) {
  393. data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) <<
  394. 8);
  395. if ((rc = write_word (info, wp, data)) != 0) {
  396. return (rc);
  397. }
  398. src += 1;
  399. wp += 1;
  400. cnt -= 1;
  401. };
  402. return ERR_OK;
  403. }