flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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[CFG_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 *)(CFG_FLASH_BASE + (0x00005555<<1)))
  68. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_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 *) CFG_FLASH_BASE;
  85. device_code = *(volatile u16 *) (CFG_FLASH_BASE + 2);
  86. add_device_code = *(volatile u16 *) (CFG_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 < CFG_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. CFG_FLASH_BASE,
  175. CFG_FLASH_BASE + CFG_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. CFG_U_BOOT_BASE,
  183. CFG_U_BOOT_BASE + CFG_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. /* first look for protection bits */
  234. if (info->flash_id == FLASH_UNKNOWN)
  235. return ERR_UNKNOWN_FLASH_TYPE;
  236. if ((s_first < 0) || (s_first > s_last)) {
  237. return ERR_INVAL;
  238. }
  239. if ((info->flash_id & FLASH_VENDMASK) !=
  240. (ATM_MANUFACT & FLASH_VENDMASK)) {
  241. return ERR_UNKNOWN_FLASH_VENDOR;
  242. }
  243. prot = 0;
  244. for (sect = s_first; sect <= s_last; ++sect) {
  245. if (info->protect[sect]) {
  246. prot++;
  247. }
  248. }
  249. if (prot)
  250. return ERR_PROTECTED;
  251. /*
  252. * Disable interrupts which might cause a timeout
  253. * here. Remember that our exception vectors are
  254. * at address 0 in the flash, and we don't want a
  255. * (ticker) exception to happen while the flash
  256. * chip is in programming mode.
  257. */
  258. cflag = icache_status ();
  259. icache_disable ();
  260. iflag = disable_interrupts ();
  261. /* Start erase on unprotected sectors */
  262. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  263. printf ("Erasing sector %2d ... ", sect);
  264. /* arm simple, non interrupt dependent timer */
  265. reset_timer_masked ();
  266. if (info->protect[sect] == 0) { /* not protected */
  267. volatile u16 *addr = (volatile u16 *) (info->start[sect]);
  268. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  269. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  270. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  271. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  272. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  273. *addr = CMD_ERASE_CONFIRM;
  274. /* wait until flash is ready */
  275. chip1 = 0;
  276. do {
  277. result = *addr;
  278. /* check timeout */
  279. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  280. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  281. chip1 = TMO;
  282. break;
  283. }
  284. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  285. chip1 = READY;
  286. } while (!chip1);
  287. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  288. if (chip1 == ERR) {
  289. rc = ERR_PROG_ERROR;
  290. goto outahere;
  291. }
  292. if (chip1 == TMO) {
  293. rc = ERR_TIMOUT;
  294. goto outahere;
  295. }
  296. printf ("ok.\n");
  297. } else { /* it was protected */
  298. printf ("protected!\n");
  299. }
  300. }
  301. if (ctrlc ())
  302. printf ("User Interrupt!\n");
  303. outahere:
  304. /* allow flash to settle - wait 10 ms */
  305. udelay_masked (10000);
  306. if (iflag)
  307. enable_interrupts ();
  308. if (cflag)
  309. icache_enable ();
  310. return rc;
  311. }
  312. /*-----------------------------------------------------------------------
  313. * Copy memory to flash
  314. */
  315. static int write_word (flash_info_t * info, ulong dest, ulong data)
  316. {
  317. volatile u16 *addr = (volatile u16 *) dest;
  318. ulong result;
  319. int rc = ERR_OK;
  320. int cflag, iflag;
  321. int chip1;
  322. /*
  323. * Check if Flash is (sufficiently) erased
  324. */
  325. result = *addr;
  326. if ((result & data) != data)
  327. return ERR_NOT_ERASED;
  328. /*
  329. * Disable interrupts which might cause a timeout
  330. * here. Remember that our exception vectors are
  331. * at address 0 in the flash, and we don't want a
  332. * (ticker) exception to happen while the flash
  333. * chip is in programming mode.
  334. */
  335. cflag = icache_status ();
  336. icache_disable ();
  337. iflag = disable_interrupts ();
  338. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  339. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  340. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  341. *addr = data;
  342. /* arm simple, non interrupt dependent timer */
  343. reset_timer_masked ();
  344. /* wait until flash is ready */
  345. chip1 = 0;
  346. do {
  347. result = *addr;
  348. /* check timeout */
  349. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  350. chip1 = ERR | TMO;
  351. break;
  352. }
  353. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  354. chip1 = READY;
  355. } while (!chip1);
  356. *addr = CMD_READ_ARRAY;
  357. if (chip1 == ERR || *addr != data)
  358. rc = ERR_PROG_ERROR;
  359. if (iflag)
  360. enable_interrupts ();
  361. if (cflag)
  362. icache_enable ();
  363. return rc;
  364. }
  365. /*-----------------------------------------------------------------------
  366. * Copy memory to flash.
  367. */
  368. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  369. {
  370. ulong wp, data;
  371. int rc;
  372. if (addr & 1) {
  373. printf ("unaligned destination not supported\n");
  374. return ERR_ALIGN;
  375. };
  376. if ((int) src & 1) {
  377. printf ("unaligned source not supported\n");
  378. return ERR_ALIGN;
  379. };
  380. wp = addr;
  381. while (cnt >= 2) {
  382. data = *((volatile u16 *) src);
  383. if ((rc = write_word (info, wp, data)) != 0) {
  384. return (rc);
  385. }
  386. src += 2;
  387. wp += 2;
  388. cnt -= 2;
  389. }
  390. if (cnt == 1) {
  391. data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) <<
  392. 8);
  393. if ((rc = write_word (info, wp, data)) != 0) {
  394. return (rc);
  395. }
  396. src += 1;
  397. wp += 1;
  398. cnt -= 1;
  399. };
  400. return ERR_OK;
  401. }