flash.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 STM29W320DB/STM29W800D flash chips
  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_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  33. /*-----------------------------------------------------------------------
  34. * Functions
  35. */
  36. static ulong flash_get_size (vu_char *addr, flash_info_t *info);
  37. static int write_byte (flash_info_t *info, ulong dest, uchar data);
  38. /*-----------------------------------------------------------------------
  39. */
  40. unsigned long flash_init (void)
  41. {
  42. unsigned long size;
  43. int i;
  44. /* Init: no FLASHes known */
  45. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  46. flash_info[i].flash_id = FLASH_UNKNOWN;
  47. }
  48. /*
  49. * We use the following trick here: since flash is cyclically
  50. * mapped in the 0xFF800000-0xFFFFFFFF area, we detect the type
  51. * and the size of flash using 0xFF800000 as the base address,
  52. * and then call flash_get_size() again to fill flash_info.
  53. */
  54. size = flash_get_size((vu_char *)CFG_FLASH_PRELIMBASE, &flash_info[0]);
  55. if (size)
  56. {
  57. flash_get_size((vu_char *)(-size), &flash_info[0]);
  58. }
  59. #if (CFG_MONITOR_BASE >= CFG_FLASH_PRELIMBASE)
  60. /* monitor protection ON by default */
  61. flash_protect(FLAG_PROTECT_SET,
  62. CFG_MONITOR_BASE,
  63. CFG_MONITOR_BASE+monitor_flash_len-1,
  64. &flash_info[0]);
  65. #endif
  66. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  67. # ifndef CFG_ENV_SIZE
  68. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  69. # endif
  70. flash_protect(FLAG_PROTECT_SET,
  71. CFG_ENV_ADDR,
  72. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  73. &flash_info[0]);
  74. #endif
  75. return (size);
  76. }
  77. /*-----------------------------------------------------------------------
  78. */
  79. void flash_print_info (flash_info_t *info)
  80. {
  81. int i;
  82. if (info->flash_id == FLASH_UNKNOWN) {
  83. printf ("missing or unknown FLASH type\n");
  84. return;
  85. }
  86. switch (info->flash_id & FLASH_VENDMASK) {
  87. case FLASH_MAN_STM:
  88. printf ("ST ");
  89. break;
  90. default:
  91. printf ("Unknown Vendor ");
  92. break;
  93. }
  94. switch (info->flash_id & FLASH_TYPEMASK) {
  95. case FLASH_STM320DB:
  96. printf ("M29W320DB (32 Mbit)\n");
  97. break;
  98. case FLASH_STM800DB:
  99. printf ("M29W800DB (8 Mbit, bottom boot block)\n");
  100. break;
  101. case FLASH_STM800DT:
  102. printf ("M29W800DT (8 Mbit, top boot block)\n");
  103. break;
  104. default:
  105. printf ("Unknown Chip Type\n");
  106. break;
  107. }
  108. printf (" Size: %ld KB in %d Sectors\n",
  109. info->size >> 10, info->sector_count);
  110. printf (" Sector Start Addresses:");
  111. for (i=0; i<info->sector_count; ++i) {
  112. if ((i % 5) == 0)
  113. printf ("\n ");
  114. printf (" %08lX%s",
  115. info->start[i],
  116. info->protect[i] ? " (RO)" : " "
  117. );
  118. }
  119. printf ("\n");
  120. return;
  121. }
  122. /*
  123. * The following code cannot be run from FLASH!
  124. */
  125. static ulong flash_get_size (vu_char *addr, flash_info_t *info)
  126. {
  127. short i;
  128. uchar vendor, devid;
  129. ulong base = (ulong)addr;
  130. /* Write auto select command: read Manufacturer ID */
  131. addr[0x0AAA] = 0xAA;
  132. addr[0x0555] = 0x55;
  133. addr[0x0AAA] = 0x90;
  134. udelay(1000);
  135. vendor = addr[0];
  136. devid = addr[2];
  137. /* only support STM */
  138. if ((vendor << 16) != FLASH_MAN_STM) {
  139. return 0;
  140. }
  141. if (devid == FLASH_STM320DB) {
  142. /* MPC8240 can address maximum 2Mb of flash, that is why the MSB
  143. * lead is grounded and we can access only 2 first Mb */
  144. info->flash_id = vendor << 16 | devid;
  145. info->sector_count = 32;
  146. info->size = info->sector_count * 0x10000;
  147. for (i = 0; i < info->sector_count; i++) {
  148. info->start[i] = base + i * 0x10000;
  149. }
  150. }
  151. else if (devid == FLASH_STM800DB) {
  152. info->flash_id = vendor << 16 | devid;
  153. info->sector_count = 19;
  154. info->size = 0x100000;
  155. info->start[0] = 0x0000;
  156. info->start[1] = 0x4000;
  157. info->start[2] = 0x6000;
  158. info->start[3] = 0x8000;
  159. for (i = 4; i < info->sector_count; i++) {
  160. info->start[i] = base + (i-3) * 0x10000;
  161. }
  162. }
  163. else if (devid == FLASH_STM800DT) {
  164. info->flash_id = vendor << 16 | devid;
  165. info->sector_count = 19;
  166. info->size = 0x100000;
  167. for (i = 0; i < info->sector_count-4; i++) {
  168. info->start[i] = base + i * 0x10000;
  169. }
  170. info->start[i] = base + i * 0x10000;
  171. info->start[i+1] = base + i * 0x10000 + 0x8000;
  172. info->start[i+2] = base + i * 0x10000 + 0xa000;
  173. info->start[i+3] = base + i * 0x10000 + 0xc000;
  174. }
  175. else {
  176. return 0;
  177. }
  178. /* mark all sectors as unprotected */
  179. for (i = 0; i < info->sector_count; i++) {
  180. info->protect[i] = 0;
  181. }
  182. /* Issue the reset command */
  183. if (info->flash_id != FLASH_UNKNOWN) {
  184. addr[0] = 0xF0; /* reset bank */
  185. }
  186. return (info->size);
  187. }
  188. /*-----------------------------------------------------------------------
  189. */
  190. int flash_erase (flash_info_t *info, int s_first, int s_last)
  191. {
  192. vu_char *addr = (vu_char *)(info->start[0]);
  193. int flag, prot, sect, l_sect;
  194. ulong start, now, last;
  195. if ((s_first < 0) || (s_first > s_last)) {
  196. if (info->flash_id == FLASH_UNKNOWN) {
  197. printf ("- missing\n");
  198. } else {
  199. printf ("- no sectors to erase\n");
  200. }
  201. return 1;
  202. }
  203. prot = 0;
  204. for (sect = s_first; sect <= s_last; sect++) {
  205. if (info->protect[sect]) {
  206. prot++;
  207. }
  208. }
  209. if (prot) {
  210. printf ("- Warning: %d protected sectors will not be erased!\n",
  211. prot);
  212. } else {
  213. printf ("\n");
  214. }
  215. l_sect = -1;
  216. /* Disable interrupts which might cause a timeout here */
  217. flag = disable_interrupts();
  218. addr[0x0AAA] = 0xAA;
  219. addr[0x0555] = 0x55;
  220. addr[0x0AAA] = 0x80;
  221. addr[0x0AAA] = 0xAA;
  222. addr[0x0555] = 0x55;
  223. /* wait at least 80us - let's wait 1 ms */
  224. udelay (1000);
  225. /* Start erase on unprotected sectors */
  226. for (sect = s_first; sect<=s_last; sect++) {
  227. if (info->protect[sect] == 0) { /* not protected */
  228. addr = (vu_char *)(info->start[sect]);
  229. addr[0] = 0x30;
  230. l_sect = sect;
  231. }
  232. }
  233. /* re-enable interrupts if necessary */
  234. if (flag)
  235. enable_interrupts();
  236. /* wait at least 80us - let's wait 1 ms */
  237. udelay (1000);
  238. /*
  239. * We wait for the last triggered sector
  240. */
  241. if (l_sect < 0)
  242. goto DONE;
  243. start = get_timer (0);
  244. last = start;
  245. addr = (vu_char *)(info->start[l_sect]);
  246. while ((addr[0] & 0x80) != 0x80) {
  247. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  248. printf ("Timeout\n");
  249. return 1;
  250. }
  251. /* show that we're waiting */
  252. if ((now - last) > 1000) { /* every second */
  253. serial_putc ('.');
  254. last = now;
  255. }
  256. }
  257. DONE:
  258. /* reset to read mode */
  259. addr = (volatile unsigned char *)info->start[0];
  260. addr[0] = 0xF0; /* reset bank */
  261. printf (" done\n");
  262. return 0;
  263. }
  264. /*-----------------------------------------------------------------------
  265. * Copy memory to flash, returns:
  266. * 0 - OK
  267. * 1 - write timeout
  268. * 2 - Flash not erased
  269. */
  270. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  271. {
  272. int rc;
  273. while (cnt > 0) {
  274. if ((rc = write_byte(info, addr, *src)) != 0) {
  275. return (rc);
  276. }
  277. addr++;
  278. src++;
  279. cnt--;
  280. }
  281. return (0);
  282. }
  283. /*-----------------------------------------------------------------------
  284. * Write a byte to Flash, returns:
  285. * 0 - OK
  286. * 1 - write timeout
  287. * 2 - Flash not erased
  288. */
  289. static int write_byte (flash_info_t *info, ulong dest, uchar data)
  290. {
  291. vu_char *addr = (vu_char *)(info->start[0]);
  292. ulong start;
  293. int flag;
  294. /* Check if Flash is (sufficiently) erased */
  295. if ((*((vu_char *)dest) & data) != data) {
  296. return (2);
  297. }
  298. /* Disable interrupts which might cause a timeout here */
  299. flag = disable_interrupts();
  300. addr[0x0AAA] = 0xAA;
  301. addr[0x0555] = 0x55;
  302. addr[0x0AAA] = 0xA0;
  303. *((vu_char *)dest) = data;
  304. /* re-enable interrupts if necessary */
  305. if (flag)
  306. enable_interrupts();
  307. /* data polling for D7 */
  308. start = get_timer (0);
  309. while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
  310. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  311. return (1);
  312. }
  313. }
  314. return (0);
  315. }
  316. /*-----------------------------------------------------------------------
  317. */