flash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #ifdef CONFIG_ATSTK1000_EXT_FLASH
  24. #include <asm/arch/cacheflush.h>
  25. #include <asm/io.h>
  26. #include <asm/sections.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. flash_info_t flash_info[1];
  29. static void flash_identify(uint16_t *flash, flash_info_t *info)
  30. {
  31. unsigned long flags;
  32. flags = disable_interrupts();
  33. dcache_flush_unlocked();
  34. writew(0xaa, flash + 0x555);
  35. writew(0x55, flash + 0xaaa);
  36. writew(0x90, flash + 0x555);
  37. info->flash_id = readl(flash);
  38. writew(0xff, flash);
  39. readw(flash);
  40. if (flags)
  41. enable_interrupts();
  42. }
  43. unsigned long flash_init(void)
  44. {
  45. unsigned long addr;
  46. unsigned int i;
  47. flash_info[0].size = CONFIG_SYS_FLASH_SIZE;
  48. flash_info[0].sector_count = 135;
  49. flash_identify(uncached((void *)CONFIG_SYS_FLASH_BASE), &flash_info[0]);
  50. for (i = 0, addr = 0; i < 8; i++, addr += 0x2000)
  51. flash_info[0].start[i] = addr;
  52. for (; i < flash_info[0].sector_count; i++, addr += 0x10000)
  53. flash_info[0].start[i] = addr;
  54. return CONFIG_SYS_FLASH_SIZE;
  55. }
  56. void flash_print_info(flash_info_t *info)
  57. {
  58. printf("Flash: Vendor ID: 0x%02lx, Product ID: 0x%02lx\n",
  59. info->flash_id >> 16, info->flash_id & 0xffff);
  60. printf("Size: %ld MB in %d sectors\n",
  61. info->size >> 10, info->sector_count);
  62. }
  63. int flash_erase(flash_info_t *info, int s_first, int s_last)
  64. {
  65. unsigned long flags;
  66. unsigned long start_time;
  67. uint16_t *fb, *sb;
  68. unsigned int i;
  69. int ret;
  70. uint16_t status;
  71. if ((s_first < 0) || (s_first > s_last)
  72. || (s_last >= info->sector_count)) {
  73. puts("Error: first and/or last sector out of range\n");
  74. return ERR_INVAL;
  75. }
  76. for (i = s_first; i < s_last; i++)
  77. if (info->protect[i]) {
  78. printf("Error: sector %d is protected\n", i);
  79. return ERR_PROTECTED;
  80. }
  81. fb = (uint16_t *)uncached(info->start[0]);
  82. dcache_flush_unlocked();
  83. for (i = s_first; (i <= s_last) && !ctrlc(); i++) {
  84. printf("Erasing sector %3d...", i);
  85. sb = (uint16_t *)uncached(info->start[i]);
  86. flags = disable_interrupts();
  87. start_time = get_timer(0);
  88. /* Unlock sector */
  89. writew(0xaa, fb + 0x555);
  90. writew(0x70, sb);
  91. /* Erase sector */
  92. writew(0xaa, fb + 0x555);
  93. writew(0x55, fb + 0xaaa);
  94. writew(0x80, fb + 0x555);
  95. writew(0xaa, fb + 0x555);
  96. writew(0x55, fb + 0xaaa);
  97. writew(0x30, sb);
  98. /* Wait for completion */
  99. ret = ERR_OK;
  100. do {
  101. /* TODO: Timeout */
  102. status = readw(sb);
  103. } while ((status != 0xffff) && !(status & 0x28));
  104. writew(0xf0, fb);
  105. /*
  106. * Make sure the command actually makes it to the bus
  107. * before we re-enable interrupts.
  108. */
  109. readw(fb);
  110. if (flags)
  111. enable_interrupts();
  112. if (status != 0xffff) {
  113. printf("Flash erase error at address 0x%p: 0x%02x\n",
  114. sb, status);
  115. ret = ERR_PROG_ERROR;
  116. break;
  117. }
  118. }
  119. if (ctrlc())
  120. printf("User interrupt!\n");
  121. return ERR_OK;
  122. }
  123. int write_buff(flash_info_t *info, uchar *src,
  124. ulong addr, ulong count)
  125. {
  126. unsigned long flags;
  127. uint16_t *base, *p, *s, *end;
  128. uint16_t word, status, status1;
  129. int ret = ERR_OK;
  130. if (addr < info->start[0]
  131. || (addr + count) > (info->start[0] + info->size)
  132. || (addr + count) < addr) {
  133. puts("Error: invalid address range\n");
  134. return ERR_INVAL;
  135. }
  136. if (addr & 1 || count & 1 || (unsigned int)src & 1) {
  137. puts("Error: misaligned source, destination or count\n");
  138. return ERR_ALIGN;
  139. }
  140. base = (uint16_t *)uncached(info->start[0]);
  141. end = (uint16_t *)uncached(addr + count);
  142. flags = disable_interrupts();
  143. dcache_flush_unlocked();
  144. sync_write_buffer();
  145. for (p = (uint16_t *)uncached(addr), s = (uint16_t *)src;
  146. p < end && !ctrlc(); p++, s++) {
  147. word = *s;
  148. writew(0xaa, base + 0x555);
  149. writew(0x55, base + 0xaaa);
  150. writew(0xa0, base + 0x555);
  151. writew(word, p);
  152. sync_write_buffer();
  153. /* Wait for completion */
  154. status1 = readw(p);
  155. do {
  156. /* TODO: Timeout */
  157. status = status1;
  158. status1 = readw(p);
  159. } while (((status ^ status1) & 0x40) /* toggled */
  160. && !(status1 & 0x28)); /* error bits */
  161. /*
  162. * We'll need to check once again for toggle bit
  163. * because the toggle bit may stop toggling as I/O5
  164. * changes to "1" (ref at49bv642.pdf p9)
  165. */
  166. status1 = readw(p);
  167. status = readw(p);
  168. if ((status ^ status1) & 0x40) {
  169. printf("Flash write error at address 0x%p: "
  170. "0x%02x != 0x%02x\n",
  171. p, status,word);
  172. ret = ERR_PROG_ERROR;
  173. writew(0xf0, base);
  174. readw(base);
  175. break;
  176. }
  177. writew(0xf0, base);
  178. readw(base);
  179. }
  180. if (flags)
  181. enable_interrupts();
  182. return ret;
  183. }
  184. #endif /* CONFIG_ATSTK1000_EXT_FLASH */