flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/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 __flashprog 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. gd->bd->bi_flashstart = CFG_FLASH_BASE;
  48. gd->bd->bi_flashsize = CFG_FLASH_SIZE;
  49. gd->bd->bi_flashoffset = _edata - _text;
  50. flash_info[0].size = CFG_FLASH_SIZE;
  51. flash_info[0].sector_count = 135;
  52. flash_identify(uncached((void *)CFG_FLASH_BASE), &flash_info[0]);
  53. for (i = 0, addr = 0; i < 8; i++, addr += 0x2000)
  54. flash_info[0].start[i] = addr;
  55. for (; i < flash_info[0].sector_count; i++, addr += 0x10000)
  56. flash_info[0].start[i] = addr;
  57. return CFG_FLASH_SIZE;
  58. }
  59. void flash_print_info(flash_info_t *info)
  60. {
  61. printf("Flash: Vendor ID: 0x%02x, Product ID: 0x%02x\n",
  62. info->flash_id >> 16, info->flash_id & 0xffff);
  63. printf("Size: %ld MB in %d sectors\n",
  64. info->size >> 10, info->sector_count);
  65. }
  66. int __flashprog flash_erase(flash_info_t *info, int s_first, int s_last)
  67. {
  68. unsigned long flags;
  69. unsigned long start_time;
  70. uint16_t *fb, *sb;
  71. unsigned int i;
  72. int ret;
  73. uint16_t status;
  74. if ((s_first < 0) || (s_first > s_last)
  75. || (s_last >= info->sector_count)) {
  76. puts("Error: first and/or last sector out of range\n");
  77. return ERR_INVAL;
  78. }
  79. for (i = s_first; i < s_last; i++)
  80. if (info->protect[i]) {
  81. printf("Error: sector %d is protected\n", i);
  82. return ERR_PROTECTED;
  83. }
  84. fb = (uint16_t *)uncached(info->start[0]);
  85. dcache_flush_unlocked();
  86. for (i = s_first; (i <= s_last) && !ctrlc(); i++) {
  87. printf("Erasing sector %3d...", i);
  88. sb = (uint16_t *)uncached(info->start[i]);
  89. flags = disable_interrupts();
  90. start_time = get_timer(0);
  91. /* Unlock sector */
  92. writew(0xaa, fb + 0x555);
  93. writew(0x70, sb);
  94. /* Erase sector */
  95. writew(0xaa, fb + 0x555);
  96. writew(0x55, fb + 0xaaa);
  97. writew(0x80, fb + 0x555);
  98. writew(0xaa, fb + 0x555);
  99. writew(0x55, fb + 0xaaa);
  100. writew(0x30, sb);
  101. /* Wait for completion */
  102. ret = ERR_OK;
  103. do {
  104. /* TODO: Timeout */
  105. status = readw(sb);
  106. } while ((status != 0xffff) && !(status & 0x28));
  107. writew(0xf0, fb);
  108. /*
  109. * Make sure the command actually makes it to the bus
  110. * before we re-enable interrupts.
  111. */
  112. readw(fb);
  113. if (flags)
  114. enable_interrupts();
  115. if (status != 0xffff) {
  116. printf("Flash erase error at address 0x%p: 0x%02x\n",
  117. sb, status);
  118. ret = ERR_PROG_ERROR;
  119. break;
  120. }
  121. }
  122. if (ctrlc())
  123. printf("User interrupt!\n");
  124. return ERR_OK;
  125. }
  126. int __flashprog write_buff(flash_info_t *info, uchar *src,
  127. ulong addr, ulong count)
  128. {
  129. unsigned long flags;
  130. uint16_t *base, *p, *s, *end;
  131. uint16_t word, status;
  132. int ret = ERR_OK;
  133. if (addr < info->start[0]
  134. || (addr + count) > (info->start[0] + info->size)
  135. || (addr + count) < addr) {
  136. puts("Error: invalid address range\n");
  137. return ERR_INVAL;
  138. }
  139. if (addr & 1 || count & 1 || (unsigned int)src & 1) {
  140. puts("Error: misaligned source, destination or count\n");
  141. return ERR_ALIGN;
  142. }
  143. base = (uint16_t *)uncached(info->start[0]);
  144. end = (uint16_t *)uncached(addr + count);
  145. flags = disable_interrupts();
  146. dcache_flush_unlocked();
  147. sync_write_buffer();
  148. for (p = (uint16_t *)uncached(addr), s = (uint16_t *)src;
  149. p < end && !ctrlc(); p++, s++) {
  150. word = *s;
  151. writew(0xaa, base + 0x555);
  152. writew(0x55, base + 0xaaa);
  153. writew(0xa0, base + 0x555);
  154. writew(word, p);
  155. sync_write_buffer();
  156. /* Wait for completion */
  157. do {
  158. /* TODO: Timeout */
  159. status = readw(p);
  160. } while ((status != word) && !(status & 0x28));
  161. writew(0xf0, base);
  162. readw(base);
  163. if (status != word) {
  164. printf("Flash write error at address 0x%p: 0x%02x\n",
  165. p, status);
  166. ret = ERR_PROG_ERROR;
  167. break;
  168. }
  169. }
  170. if (flags)
  171. enable_interrupts();
  172. return ret;
  173. }
  174. #endif /* CONFIG_ATSTK1000_EXT_FLASH */