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. return;
  60. }
  61. unsigned long flash_init (void)
  62. {
  63. int i, j;
  64. ulong size = 0;
  65. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  66. ulong flashbase = 0;
  67. flash_info[i].flash_id =
  68. (AMD_MANUFACT & FLASH_VENDMASK) |
  69. (AMD_ID_PL160CB & FLASH_TYPEMASK);
  70. flash_info[i].size = FLASH_BANK_SIZE;
  71. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  72. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  73. if (i == 0)
  74. flashbase = PHYS_FLASH_1;
  75. else
  76. panic ("configured to many flash banks!\n");
  77. for (j = 0; j < flash_info[i].sector_count; j++) {
  78. if (j == 0) {
  79. /* 1st is 16 KiB */
  80. flash_info[i].start[j] = flashbase;
  81. }
  82. if ((j >= 1) && (j <= 2)) {
  83. /* 2nd and 3rd are 8 KiB */
  84. flash_info[i].start[j] =
  85. flashbase + 0x4000 + 0x2000 * (j - 1);
  86. }
  87. if (j == 3) {
  88. /* 4th is 32 KiB */
  89. flash_info[i].start[j] = flashbase + 0x8000;
  90. }
  91. if ((j >= 4) && (j <= 34)) {
  92. /* rest is 256 KiB */
  93. flash_info[i].start[j] =
  94. flashbase + 0x10000 + 0x10000 * (j -
  95. 4);
  96. }
  97. }
  98. size += flash_info[i].size;
  99. }
  100. flash_protect (FLAG_PROTECT_SET,
  101. CFG_FLASH_BASE,
  102. CFG_FLASH_BASE + 0xffff, &flash_info[0]);
  103. return size;
  104. }
  105. #define CMD_READ_ARRAY 0x00F0
  106. #define CMD_UNLOCK1 0x00AA
  107. #define CMD_UNLOCK2 0x0055
  108. #define CMD_ERASE_SETUP 0x0080
  109. #define CMD_ERASE_CONFIRM 0x0030
  110. #define CMD_PROGRAM 0x00A0
  111. #define CMD_UNLOCK_BYPASS 0x0020
  112. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
  113. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
  114. #define BIT_ERASE_DONE 0x0080
  115. #define BIT_RDY_MASK 0x0080
  116. #define BIT_PROGRAM_ERROR 0x0020
  117. #define BIT_TIMEOUT 0x80000000 /* our flag */
  118. #define READY 1
  119. #define ERR 2
  120. #define TMO 4
  121. int flash_erase (flash_info_t * info, int s_first, int s_last)
  122. {
  123. ulong result;
  124. int iflag, cflag, prot, sect;
  125. int rc = ERR_OK;
  126. int chip1;
  127. /* first look for protection bits */
  128. if (info->flash_id == FLASH_UNKNOWN)
  129. return ERR_UNKNOWN_FLASH_TYPE;
  130. if ((s_first < 0) || (s_first > s_last)) {
  131. return ERR_INVAL;
  132. }
  133. if ((info->flash_id & FLASH_VENDMASK) !=
  134. (AMD_MANUFACT & FLASH_VENDMASK)) {
  135. return ERR_UNKNOWN_FLASH_VENDOR;
  136. }
  137. prot = 0;
  138. for (sect = s_first; sect <= s_last; ++sect) {
  139. if (info->protect[sect]) {
  140. prot++;
  141. }
  142. }
  143. if (prot)
  144. return ERR_PROTECTED;
  145. /*
  146. * Disable interrupts which might cause a timeout
  147. * here. Remember that our exception vectors are
  148. * at address 0 in the flash, and we don't want a
  149. * (ticker) exception to happen while the flash
  150. * chip is in programming mode.
  151. */
  152. cflag = icache_status ();
  153. icache_disable ();
  154. iflag = disable_interrupts ();
  155. printf ("\n");
  156. /* Start erase on unprotected sectors */
  157. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  158. printf ("Erasing sector %2d ... ", sect);
  159. /* arm simple, non interrupt dependent timer */
  160. set_timer (0);
  161. if (info->protect[sect] == 0) { /* not protected */
  162. volatile u16 *addr =
  163. (volatile u16 *) (info->start[sect]);
  164. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  165. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  166. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  167. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  168. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  169. *addr = CMD_ERASE_CONFIRM;
  170. /* wait until flash is ready */
  171. chip1 = 0;
  172. do {
  173. result = *addr;
  174. /* check timeout */
  175. if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
  176. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  177. chip1 = TMO;
  178. break;
  179. }
  180. if (!chip1
  181. && (result & 0xFFFF) & BIT_ERASE_DONE)
  182. chip1 = READY;
  183. } while (!chip1);
  184. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  185. if (chip1 == ERR) {
  186. rc = ERR_PROG_ERROR;
  187. goto outahere;
  188. }
  189. if (chip1 == TMO) {
  190. rc = ERR_TIMOUT;
  191. goto outahere;
  192. }
  193. printf ("ok.\n");
  194. } else { /* it was protected */
  195. printf ("protected!\n");
  196. }
  197. }
  198. if (ctrlc ())
  199. printf ("User Interrupt!\n");
  200. outahere:
  201. /* allow flash to settle - wait 10 ms */
  202. udelay (10000);
  203. if (iflag)
  204. enable_interrupts ();
  205. if (cflag)
  206. icache_enable ();
  207. return rc;
  208. }
  209. static int write_word (flash_info_t * info, ulong dest, ulong data)
  210. {
  211. volatile u16 *addr = (volatile u16 *) dest;
  212. ulong result;
  213. int rc = ERR_OK;
  214. int cflag, iflag;
  215. int chip1;
  216. /*
  217. * Check if Flash is (sufficiently) erased
  218. */
  219. result = *addr;
  220. if ((result & data) != data)
  221. return ERR_NOT_ERASED;
  222. /*
  223. * Disable interrupts which might cause a timeout
  224. * here. Remember that our exception vectors are
  225. * at address 0 in the flash, and we don't want a
  226. * (ticker) exception to happen while the flash
  227. * chip is in programming mode.
  228. */
  229. cflag = icache_status ();
  230. icache_disable ();
  231. iflag = disable_interrupts ();
  232. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  233. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  234. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  235. *addr = data;
  236. /* arm simple, non interrupt dependent timer */
  237. set_timer (0);
  238. /* wait until flash is ready */
  239. chip1 = 0;
  240. do {
  241. result = *addr;
  242. /* check timeout */
  243. if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
  244. chip1 = ERR | TMO;
  245. break;
  246. }
  247. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  248. chip1 = READY;
  249. } while (!chip1);
  250. *addr = CMD_READ_ARRAY;
  251. if (chip1 == ERR || *addr != data)
  252. rc = ERR_PROG_ERROR;
  253. if (iflag)
  254. enable_interrupts ();
  255. if (cflag)
  256. icache_enable ();
  257. return rc;
  258. }
  259. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  260. {
  261. ulong wp, data;
  262. int rc;
  263. if (addr & 1) {
  264. printf ("unaligned destination not supported\n");
  265. return ERR_ALIGN;
  266. }
  267. #if 0
  268. if (cnt & 1) {
  269. printf ("odd transfer sizes not supported\n");
  270. return ERR_ALIGN;
  271. }
  272. #endif
  273. wp = addr;
  274. if (addr & 1) {
  275. data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
  276. src);
  277. if ((rc = write_word (info, wp - 1, data)) != 0) {
  278. return (rc);
  279. }
  280. src += 1;
  281. wp += 1;
  282. cnt -= 1;
  283. }
  284. while (cnt >= 2) {
  285. data = *((volatile u16 *) src);
  286. if ((rc = write_word (info, wp, data)) != 0) {
  287. return (rc);
  288. }
  289. src += 2;
  290. wp += 2;
  291. cnt -= 2;
  292. }
  293. if (cnt == 1) {
  294. data = (*((volatile u8 *) src) << 8) |
  295. *((volatile u8 *) (wp + 1));
  296. if ((rc = write_word (info, wp, data)) != 0) {
  297. return (rc);
  298. }
  299. src += 1;
  300. wp += 1;
  301. cnt -= 1;
  302. }
  303. return ERR_OK;
  304. }