flash.c 12 KB

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