flash.c 7.2 KB

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