AMDLV065D.c 4.5 KB

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