flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * (C) Copyright 2000
  3. * Marius Groeger <mgroeger@sysgo.de>
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * Flash Routines for AM290[48]0B devices
  10. *
  11. *--------------------------------------------------------------------
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. */
  30. #include <common.h>
  31. #include <mpc8xx.h>
  32. /* flash hardware ids */
  33. #define VENDOR_AMD 0x0001
  34. #define AMD_29DL323C_B 0x2253
  35. /* Define this to include autoselect sequence in flash_init(). Does NOT
  36. * work when executing from flash itself, so this should be turned
  37. * on only when debugging the RAM version.
  38. */
  39. #undef WITH_AUTOSELECT
  40. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  41. #if 1
  42. #define D(x)
  43. #else
  44. #define D(x) printf x
  45. #endif
  46. /*-----------------------------------------------------------------------
  47. * Functions
  48. */
  49. static unsigned char write_ull(flash_info_t *info,
  50. unsigned long address,
  51. volatile unsigned long long data);
  52. /* from flash_asm.S */
  53. extern void ull_write(unsigned long long volatile *address,
  54. unsigned long long volatile *data);
  55. extern void ull_read(unsigned long long volatile *address,
  56. unsigned long long volatile *data);
  57. /*-----------------------------------------------------------------------
  58. */
  59. unsigned long flash_init (void)
  60. {
  61. int i;
  62. ulong addr;
  63. #ifdef WITH_AUTOSELECT
  64. {
  65. unsigned long long *f_addr = (unsigned long long *)PHYS_FLASH;
  66. unsigned long long f_command, vendor, device;
  67. /* Perform Autoselect */
  68. f_command = 0x00AA00AA00AA00AAULL;
  69. ull_write(&f_addr[0x555], &f_command);
  70. f_command = 0x0055005500550055ULL;
  71. ull_write(&f_addr[0x2AA], &f_command);
  72. f_command = 0x0090009000900090ULL;
  73. ull_write(&f_addr[0x555], &f_command);
  74. ull_read(&f_addr[0], &vendor);
  75. vendor &= 0xffff;
  76. ull_read(&f_addr[1], &device);
  77. device &= 0xffff;
  78. f_command = 0x00F000F000F000F0ULL;
  79. ull_write(&f_addr[0x555], &f_command);
  80. if (vendor != VENDOR_AMD || device != AMD_29DL323C_B)
  81. return 0;
  82. }
  83. #endif
  84. /* Init: no FLASHes known */
  85. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  86. flash_info[i].flash_id = FLASH_UNKNOWN;
  87. }
  88. /* 1st bank: 8 x 32 KB sectors */
  89. flash_info[0].flash_id = VENDOR_AMD << 16 | AMD_29DL323C_B;
  90. flash_info[0].sector_count = 8;
  91. flash_info[0].size = flash_info[0].sector_count * 32 * 1024;
  92. addr = PHYS_FLASH;
  93. for(i = 0; i < flash_info[0].sector_count; i++) {
  94. flash_info[0].start[i] = addr;
  95. addr += flash_info[0].size / flash_info[0].sector_count;
  96. }
  97. /* 1st bank: 63 x 256 KB sectors */
  98. flash_info[1].flash_id = VENDOR_AMD << 16 | AMD_29DL323C_B;
  99. flash_info[1].sector_count = 63;
  100. flash_info[1].size = flash_info[1].sector_count * 256 * 1024;
  101. for(i = 0; i < flash_info[1].sector_count; i++) {
  102. flash_info[1].start[i] = addr;
  103. addr += flash_info[1].size / flash_info[1].sector_count;
  104. }
  105. /*
  106. * protect monitor and environment sectors
  107. */
  108. #if CFG_MONITOR_BASE >= PHYS_FLASH
  109. flash_protect(FLAG_PROTECT_SET,
  110. CFG_MONITOR_BASE,
  111. CFG_MONITOR_BASE+monitor_flash_len-1,
  112. &flash_info[0]);
  113. flash_protect(FLAG_PROTECT_SET,
  114. CFG_MONITOR_BASE,
  115. CFG_MONITOR_BASE+monitor_flash_len-1,
  116. &flash_info[1]);
  117. #endif
  118. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  119. # ifndef CFG_ENV_SIZE
  120. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  121. # endif
  122. flash_protect(FLAG_PROTECT_SET,
  123. CFG_ENV_ADDR,
  124. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  125. &flash_info[0]);
  126. flash_protect(FLAG_PROTECT_SET,
  127. CFG_ENV_ADDR,
  128. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  129. &flash_info[1]);
  130. #endif
  131. return flash_info[0].size + flash_info[1].size;
  132. }
  133. /*-----------------------------------------------------------------------
  134. */
  135. void flash_print_info (flash_info_t *info)
  136. {
  137. int i;
  138. if (info->flash_id == FLASH_UNKNOWN) {
  139. printf ("missing or unknown FLASH type\n");
  140. return;
  141. }
  142. switch (info->flash_id >> 16) {
  143. case VENDOR_AMD:
  144. printf ("AMD ");
  145. break;
  146. default:
  147. printf ("Unknown Vendor ");
  148. break;
  149. }
  150. switch (info->flash_id & FLASH_TYPEMASK) {
  151. case AMD_29DL323C_B:
  152. printf ("AM29DL323CB (32 Mbit)\n");
  153. break;
  154. default:
  155. printf ("Unknown Chip Type\n");
  156. break;
  157. }
  158. printf (" Size: %ld MB in %d Sectors\n",
  159. info->size >> 20, info->sector_count);
  160. printf (" Sector Start Addresses:");
  161. for (i=0; i<info->sector_count; ++i) {
  162. if ((i % 5) == 0)
  163. printf ("\n ");
  164. printf (" %08lX%s",
  165. info->start[i],
  166. info->protect[i] ? " (RO)" : " "
  167. );
  168. }
  169. printf ("\n");
  170. return;
  171. }
  172. /*-----------------------------------------------------------------------
  173. */
  174. int flash_erase (flash_info_t *info, int s_first, int s_last)
  175. {
  176. int flag, prot, sect, l_sect;
  177. ulong start;
  178. unsigned long long volatile *f_addr;
  179. unsigned long long volatile f_command;
  180. if ((s_first < 0) || (s_first > s_last)) {
  181. if (info->flash_id == FLASH_UNKNOWN) {
  182. printf ("- missing\n");
  183. } else {
  184. printf ("- no sectors to erase\n");
  185. }
  186. return 1;
  187. }
  188. prot = 0;
  189. for (sect = s_first; sect <= s_last; sect++) {
  190. if (info->protect[sect]) {
  191. prot++;
  192. }
  193. }
  194. if (prot) {
  195. printf ("- Warning: %d protected sectors will not be erased!\n",
  196. prot);
  197. } else {
  198. printf ("\n");
  199. }
  200. f_addr = (unsigned long long *)info->start[0];
  201. f_command = 0x00AA00AA00AA00AAULL;
  202. ull_write(&f_addr[0x555], &f_command);
  203. f_command = 0x0055005500550055ULL;
  204. ull_write(&f_addr[0x2AA], &f_command);
  205. f_command = 0x0080008000800080ULL;
  206. ull_write(&f_addr[0x555], &f_command);
  207. f_command = 0x00AA00AA00AA00AAULL;
  208. ull_write(&f_addr[0x555], &f_command);
  209. f_command = 0x0055005500550055ULL;
  210. ull_write(&f_addr[0x2AA], &f_command);
  211. /* Disable interrupts which might cause a timeout here */
  212. flag = disable_interrupts();
  213. /* Start erase on unprotected sectors */
  214. for (l_sect = -1, sect = s_first; sect<=s_last; sect++) {
  215. if (info->protect[sect] == 0) { /* not protected */
  216. f_addr =
  217. (unsigned long long *)(info->start[sect]);
  218. f_command = 0x0030003000300030ULL;
  219. ull_write(f_addr, &f_command);
  220. l_sect = sect;
  221. }
  222. }
  223. /* re-enable interrupts if necessary */
  224. if (flag)
  225. enable_interrupts();
  226. start = get_timer (0);
  227. do
  228. {
  229. if (get_timer(start) > CFG_FLASH_ERASE_TOUT)
  230. { /* write reset command, command address is unimportant */
  231. /* this command turns the flash back to read mode */
  232. f_addr =
  233. (unsigned long long *)(info->start[l_sect]);
  234. f_command = 0x00F000F000F000F0ULL;
  235. ull_write(f_addr, &f_command);
  236. printf (" timeout\n");
  237. return 1;
  238. }
  239. } while(*f_addr != 0xFFFFFFFFFFFFFFFFULL);
  240. printf (" done\n");
  241. return 0;
  242. }
  243. /*-----------------------------------------------------------------------
  244. * Copy memory to flash, returns:
  245. * 0 - OK
  246. * 1 - write timeout
  247. * 2 - Flash not erased
  248. */
  249. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  250. {
  251. unsigned long cp, wp;
  252. unsigned long long data;
  253. int i, l, rc;
  254. wp = (addr & ~7); /* get lower long long aligned address */
  255. /*
  256. * handle unaligned start bytes
  257. */
  258. if ((l = addr - wp) != 0) {
  259. data = 0;
  260. for (i=0, cp=wp; i<l; ++i, ++cp) {
  261. data = (data << 8) | (*(uchar *)cp);
  262. }
  263. for (; i<8 && cnt>0; ++i) {
  264. data = (data << 8) | *src++;
  265. --cnt;
  266. ++cp;
  267. }
  268. for (; cnt==0 && i<8; ++i, ++cp) {
  269. data = (data << 8) | (*(uchar *)cp);
  270. }
  271. if ((rc = write_ull(info, wp, data)) != 0) {
  272. return rc;
  273. }
  274. wp += 4;
  275. }
  276. /*
  277. * handle long long aligned part
  278. */
  279. while (cnt >= 8) {
  280. data = 0;
  281. for (i=0; i<8; ++i) {
  282. data = (data << 8) | *src++;
  283. }
  284. if ((rc = write_ull(info, wp, data)) != 0) {
  285. return rc;
  286. }
  287. wp += 8;
  288. cnt -= 8;
  289. }
  290. if (cnt == 0) {
  291. return ERR_OK;
  292. }
  293. /*
  294. * handle unaligned tail bytes
  295. */
  296. data = 0;
  297. for (i=0, cp=wp; i<8 && cnt>0; ++i, ++cp) {
  298. data = (data << 8) | *src++;
  299. --cnt;
  300. }
  301. for (; i<8; ++i, ++cp) {
  302. data = (data << 8) | (*(uchar *)cp);
  303. }
  304. return write_ull(info, wp, data);
  305. }
  306. /*---------------------------------------------------------------------------
  307. *
  308. * FUNCTION NAME: write_ull
  309. *
  310. * DESCRIPTION: writes 8 bytes to flash
  311. *
  312. * EXTERNAL EFFECT: nothing
  313. *
  314. * PARAMETERS: 32 bit long pointer to address, 64 bit long pointer to data
  315. *
  316. * RETURNS: 0 if OK, 1 if timeout, 4 if parameter error
  317. *--------------------------------------------------------------------------*/
  318. static unsigned char write_ull(flash_info_t *info,
  319. unsigned long address,
  320. volatile unsigned long long data)
  321. {
  322. static unsigned long long f_command;
  323. static unsigned long long *f_addr;
  324. ulong start;
  325. /* address muss be 8-aligned! */
  326. if (address & 0x7)
  327. return ERR_ALIGN;
  328. f_addr = (unsigned long long *)info->start[0];
  329. f_command = 0x00AA00AA00AA00AAULL;
  330. ull_write(&f_addr[0x555], &f_command);
  331. f_command = 0x0055005500550055ULL;
  332. ull_write(&f_addr[0x2AA], &f_command);
  333. f_command = 0x00A000A000A000A0ULL;
  334. ull_write(&f_addr[0x555], &f_command);
  335. f_addr = (unsigned long long *)address;
  336. f_command = data;
  337. ull_write(f_addr, &f_command);
  338. start = get_timer (0);
  339. do
  340. {
  341. if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
  342. {
  343. /* write reset command, command address is unimportant */
  344. /* this command turns the flash back to read mode */
  345. f_addr = (unsigned long long *)info->start[0];
  346. f_command = 0x00F000F000F000F0ULL;
  347. ull_write(f_addr, &f_command);
  348. return ERR_TIMOUT;
  349. }
  350. } while(*((unsigned long long *)address) != data);
  351. return 0;
  352. }