AMDLV065D.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * (C) Copyright 2000-2004
  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. #if defined(CONFIG_NIOS)
  25. #include <nios.h>
  26. #else
  27. #include <nios2.h>
  28. #endif
  29. #define SECTSZ (64 * 1024)
  30. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  31. /*----------------------------------------------------------------------*/
  32. unsigned long flash_init (void)
  33. {
  34. int i;
  35. unsigned long addr;
  36. flash_info_t *fli = &flash_info[0];
  37. fli->size = CFG_FLASH_SIZE;
  38. fli->sector_count = CFG_MAX_FLASH_SECT;
  39. fli->flash_id = FLASH_MAN_AMD + FLASH_AMDLV065D;
  40. addr = CFG_FLASH_BASE;
  41. for (i = 0; i < fli->sector_count; ++i) {
  42. fli->start[i] = addr;
  43. addr += SECTSZ;
  44. fli->protect[i] = 1;
  45. }
  46. return (CFG_FLASH_SIZE);
  47. }
  48. /*--------------------------------------------------------------------*/
  49. void flash_print_info (flash_info_t * info)
  50. {
  51. int i, k;
  52. unsigned long size;
  53. int erased;
  54. volatile unsigned char *flash;
  55. printf (" Size: %ld KB in %d Sectors\n",
  56. info->size >> 10, info->sector_count);
  57. printf (" Sector Start Addresses:");
  58. for (i = 0; i < info->sector_count; ++i) {
  59. /* Check if whole sector is erased */
  60. if (i != (info->sector_count - 1))
  61. size = info->start[i + 1] - info->start[i];
  62. else
  63. size = info->start[0] + info->size - info->start[i];
  64. erased = 1;
  65. flash = (volatile unsigned char *) CACHE_BYPASS(info->start[i]);
  66. for (k = 0; k < size; k++) {
  67. if (*flash++ != 0xff) {
  68. erased = 0;
  69. break;
  70. }
  71. }
  72. /* Print the info */
  73. if ((i % 5) == 0)
  74. printf ("\n ");
  75. printf (" %08lX%s%s",
  76. CACHE_NO_BYPASS(info->start[i]),
  77. erased ? " E" : " ",
  78. info->protect[i] ? "RO " : " ");
  79. }
  80. printf ("\n");
  81. }
  82. /*-------------------------------------------------------------------*/
  83. int flash_erase (flash_info_t * info, int s_first, int s_last)
  84. {
  85. volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)
  86. CACHE_BYPASS(info->start[0]);
  87. volatile CFG_FLASH_WORD_SIZE *addr2;
  88. int prot, sect;
  89. ulong start;
  90. /* Some sanity checking */
  91. if ((s_first < 0) || (s_first > s_last)) {
  92. printf ("- no sectors to erase\n");
  93. return 1;
  94. }
  95. prot = 0;
  96. for (sect = s_first; sect <= s_last; ++sect) {
  97. if (info->protect[sect]) {
  98. prot++;
  99. }
  100. }
  101. if (prot) {
  102. printf ("- Warning: %d protected sectors will not be erased!\n",
  103. prot);
  104. } else {
  105. printf ("\n");
  106. }
  107. /* It's ok to erase multiple sectors provided we don't delay more
  108. * than 50 usec between cmds ... at which point the erase time-out
  109. * occurs. So don't go and put printf() calls in the loop ... it
  110. * won't be very helpful ;-)
  111. */
  112. for (sect = s_first; sect <= s_last; sect++) {
  113. if (info->protect[sect] == 0) { /* not protected */
  114. addr2 = (CFG_FLASH_WORD_SIZE *)
  115. CACHE_BYPASS((info->start[sect]));
  116. *addr = 0xaa;
  117. *addr = 0x55;
  118. *addr = 0x80;
  119. *addr = 0xaa;
  120. *addr = 0x55;
  121. *addr2 = 0x30;
  122. /* Now just wait for 0xff & provide some user
  123. * feedback while we wait.
  124. */
  125. start = get_timer (0);
  126. while (*addr2 != 0xff) {
  127. udelay (1000 * 1000);
  128. putc ('.');
  129. if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
  130. printf ("timeout\n");
  131. return 1;
  132. }
  133. }
  134. }
  135. }
  136. printf ("\n");
  137. return 0;
  138. }
  139. /*-----------------------------------------------------------------------
  140. * Copy memory to flash, returns:
  141. * 0 - OK
  142. * 1 - write timeout
  143. * 2 - Flash not erased
  144. */
  145. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  146. {
  147. vu_char *cmd = (vu_char *) CACHE_BYPASS(info->start[0]);
  148. vu_char *dst = (vu_char *) CACHE_BYPASS(addr);
  149. unsigned char b;
  150. ulong start;
  151. while (cnt) {
  152. /* Check for sufficient erase */
  153. b = *src;
  154. if ((*dst & b) != b) {
  155. printf ("%02x : %02x\n", *dst, b);
  156. return (2);
  157. }
  158. *cmd = 0xaa;
  159. *cmd = 0x55;
  160. *cmd = 0xa0;
  161. *dst = b;
  162. /* Verify write */
  163. start = get_timer (0);
  164. while (*dst != b) {
  165. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  166. return 1;
  167. }
  168. }
  169. dst++;
  170. src++;
  171. cnt--;
  172. }
  173. return (0);
  174. }