flash.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #define PHYS_FLASH_1 CFG_FLASH_BASE
  25. #define FLASH_BANK_SIZE 0x200000
  26. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  27. void flash_print_info (flash_info_t * info)
  28. {
  29. int i;
  30. switch (info->flash_id & FLASH_VENDMASK) {
  31. case (AMD_MANUFACT & FLASH_VENDMASK):
  32. printf ("AMD: ");
  33. break;
  34. default:
  35. printf ("Unknown Vendor ");
  36. break;
  37. }
  38. switch (info->flash_id & FLASH_TYPEMASK) {
  39. case (AMD_ID_PL160CB & FLASH_TYPEMASK):
  40. printf ("AM29PL160CB (16Mbit)\n");
  41. break;
  42. default:
  43. printf ("Unknown Chip Type\n");
  44. goto Done;
  45. break;
  46. }
  47. printf (" Size: %ld MB in %d Sectors\n",
  48. info->size >> 20, info->sector_count);
  49. printf (" Sector Start Addresses:");
  50. for (i = 0; i < info->sector_count; i++) {
  51. if ((i % 5) == 0) {
  52. printf ("\n ");
  53. }
  54. printf (" %08lX%s", info->start[i],
  55. info->protect[i] ? " (RO)" : " ");
  56. }
  57. printf ("\n");
  58. Done:
  59. }
  60. unsigned long flash_init (void)
  61. {
  62. int i, j;
  63. ulong size = 0;
  64. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  65. ulong flashbase = 0;
  66. flash_info[i].flash_id =
  67. (AMD_MANUFACT & FLASH_VENDMASK) |
  68. (AMD_ID_PL160CB & FLASH_TYPEMASK);
  69. flash_info[i].size = FLASH_BANK_SIZE;
  70. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  71. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  72. if (i == 0)
  73. flashbase = PHYS_FLASH_1;
  74. else
  75. panic ("configured to many flash banks!\n");
  76. for (j = 0; j < flash_info[i].sector_count; j++) {
  77. if (j == 0) {
  78. /* 1st is 16 KiB */
  79. flash_info[i].start[j] = flashbase;
  80. }
  81. if ((j >= 1) && (j <= 2)) {
  82. /* 2nd and 3rd are 8 KiB */
  83. flash_info[i].start[j] =
  84. flashbase + 0x4000 + 0x2000 * (j - 1);
  85. }
  86. if (j == 3) {
  87. /* 4th is 32 KiB */
  88. flash_info[i].start[j] = flashbase + 0x8000;
  89. }
  90. if ((j >= 4) && (j <= 34)) {
  91. /* rest is 256 KiB */
  92. flash_info[i].start[j] =
  93. flashbase + 0x10000 + 0x10000 * (j -
  94. 4);
  95. }
  96. }
  97. size += flash_info[i].size;
  98. }
  99. flash_protect (FLAG_PROTECT_SET,
  100. CFG_FLASH_BASE,
  101. CFG_FLASH_BASE + 0xffff, &flash_info[0]);
  102. return size;
  103. }
  104. #define CMD_READ_ARRAY 0x00F0
  105. #define CMD_UNLOCK1 0x00AA
  106. #define CMD_UNLOCK2 0x0055
  107. #define CMD_ERASE_SETUP 0x0080
  108. #define CMD_ERASE_CONFIRM 0x0030
  109. #define CMD_PROGRAM 0x00A0
  110. #define CMD_UNLOCK_BYPASS 0x0020
  111. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
  112. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
  113. #define BIT_ERASE_DONE 0x0080
  114. #define BIT_RDY_MASK 0x0080
  115. #define BIT_PROGRAM_ERROR 0x0020
  116. #define BIT_TIMEOUT 0x80000000 /* our flag */
  117. #define READY 1
  118. #define ERR 2
  119. #define TMO 4
  120. int flash_erase (flash_info_t * info, int s_first, int s_last)
  121. {
  122. ulong result;
  123. int iflag, cflag, prot, sect;
  124. int rc = ERR_OK;
  125. int chip1;
  126. /* first look for protection bits */
  127. if (info->flash_id == FLASH_UNKNOWN)
  128. return ERR_UNKNOWN_FLASH_TYPE;
  129. if ((s_first < 0) || (s_first > s_last)) {
  130. return ERR_INVAL;
  131. }
  132. if ((info->flash_id & FLASH_VENDMASK) !=
  133. (AMD_MANUFACT & FLASH_VENDMASK)) {
  134. return ERR_UNKNOWN_FLASH_VENDOR;
  135. }
  136. prot = 0;
  137. for (sect = s_first; sect <= s_last; ++sect) {
  138. if (info->protect[sect]) {
  139. prot++;
  140. }
  141. }
  142. if (prot)
  143. return ERR_PROTECTED;
  144. /*
  145. * Disable interrupts which might cause a timeout
  146. * here. Remember that our exception vectors are
  147. * at address 0 in the flash, and we don't want a
  148. * (ticker) exception to happen while the flash
  149. * chip is in programming mode.
  150. */
  151. cflag = icache_status ();
  152. icache_disable ();
  153. iflag = disable_interrupts ();
  154. printf ("\n");
  155. /* Start erase on unprotected sectors */
  156. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  157. printf ("Erasing sector %2d ... ", sect);
  158. /* arm simple, non interrupt dependent timer */
  159. set_timer (0);
  160. if (info->protect[sect] == 0) { /* not protected */
  161. volatile u16 *addr =
  162. (volatile u16 *) (info->start[sect]);
  163. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  164. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  165. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  166. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  167. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  168. *addr = CMD_ERASE_CONFIRM;
  169. /* wait until flash is ready */
  170. chip1 = 0;
  171. do {
  172. result = *addr;
  173. /* check timeout */
  174. if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
  175. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  176. chip1 = TMO;
  177. break;
  178. }
  179. if (!chip1
  180. && (result & 0xFFFF) & BIT_ERASE_DONE)
  181. chip1 = READY;
  182. } while (!chip1);
  183. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  184. if (chip1 == ERR) {
  185. rc = ERR_PROG_ERROR;
  186. goto outahere;
  187. }
  188. if (chip1 == TMO) {
  189. rc = ERR_TIMOUT;
  190. goto outahere;
  191. }
  192. printf ("ok.\n");
  193. } else { /* it was protected */
  194. printf ("protected!\n");
  195. }
  196. }
  197. if (ctrlc ())
  198. printf ("User Interrupt!\n");
  199. outahere:
  200. /* allow flash to settle - wait 10 ms */
  201. udelay (10000);
  202. if (iflag)
  203. enable_interrupts ();
  204. if (cflag)
  205. icache_enable ();
  206. return rc;
  207. }
  208. volatile static int write_word (flash_info_t * info, ulong dest, ulong data)
  209. {
  210. volatile u16 *addr = (volatile u16 *) dest;
  211. ulong result;
  212. int rc = ERR_OK;
  213. int cflag, iflag;
  214. int chip1;
  215. /*
  216. * Check if Flash is (sufficiently) erased
  217. */
  218. result = *addr;
  219. if ((result & data) != data)
  220. return ERR_NOT_ERASED;
  221. /*
  222. * Disable interrupts which might cause a timeout
  223. * here. Remember that our exception vectors are
  224. * at address 0 in the flash, and we don't want a
  225. * (ticker) exception to happen while the flash
  226. * chip is in programming mode.
  227. */
  228. cflag = icache_status ();
  229. icache_disable ();
  230. iflag = disable_interrupts ();
  231. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  232. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  233. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  234. *addr = data;
  235. /* arm simple, non interrupt dependent timer */
  236. set_timer (0);
  237. /* wait until flash is ready */
  238. chip1 = 0;
  239. do {
  240. result = *addr;
  241. /* check timeout */
  242. if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
  243. chip1 = ERR | TMO;
  244. break;
  245. }
  246. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  247. chip1 = READY;
  248. } while (!chip1);
  249. *addr = CMD_READ_ARRAY;
  250. if (chip1 == ERR || *addr != data)
  251. rc = ERR_PROG_ERROR;
  252. if (iflag)
  253. enable_interrupts ();
  254. if (cflag)
  255. icache_enable ();
  256. return rc;
  257. }
  258. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  259. {
  260. ulong wp, data;
  261. int rc;
  262. if (addr & 1) {
  263. printf ("unaligned destination not supported\n");
  264. return ERR_ALIGN;
  265. }
  266. #if 0
  267. if (cnt & 1) {
  268. printf ("odd transfer sizes not supported\n");
  269. return ERR_ALIGN;
  270. }
  271. #endif
  272. wp = addr;
  273. if (addr & 1) {
  274. data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
  275. src);
  276. if ((rc = write_word (info, wp - 1, data)) != 0) {
  277. return (rc);
  278. }
  279. src += 1;
  280. wp += 1;
  281. cnt -= 1;
  282. }
  283. while (cnt >= 2) {
  284. data = *((volatile u16 *) src);
  285. if ((rc = write_word (info, wp, data)) != 0) {
  286. return (rc);
  287. }
  288. src += 2;
  289. wp += 2;
  290. cnt -= 2;
  291. }
  292. if (cnt == 1) {
  293. data = (*((volatile u8 *) src) << 8) |
  294. *((volatile u8 *) (wp + 1));
  295. if ((rc = write_word (info, wp, data)) != 0) {
  296. return (rc);
  297. }
  298. src += 1;
  299. wp += 1;
  300. cnt -= 1;
  301. }
  302. return ERR_OK;
  303. }