flash.c 5.2 KB

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