flash.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2008 Atmel Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #ifdef CONFIG_FAVR32_EZKIT_EXT_FLASH
  8. #include <asm/arch/cacheflush.h>
  9. #include <asm/io.h>
  10. #include <asm/sections.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. flash_info_t flash_info[1];
  13. static void flash_identify(uint16_t *flash, flash_info_t *info)
  14. {
  15. unsigned long flags;
  16. flags = disable_interrupts();
  17. dcache_flush_unlocked();
  18. writew(0xaa, flash + 0x555);
  19. writew(0x55, flash + 0xaaa);
  20. writew(0x90, flash + 0x555);
  21. info->flash_id = readl(flash);
  22. writew(0xff, flash);
  23. readw(flash);
  24. if (flags)
  25. enable_interrupts();
  26. }
  27. unsigned long flash_init(void)
  28. {
  29. unsigned long addr;
  30. unsigned int i;
  31. flash_info[0].size = CONFIG_SYS_FLASH_SIZE;
  32. flash_info[0].sector_count = 135;
  33. flash_identify(uncached((void *)CONFIG_SYS_FLASH_BASE), &flash_info[0]);
  34. for (i = 0, addr = 0; i < 8; i++, addr += 0x2000)
  35. flash_info[0].start[i] = addr;
  36. for (; i < flash_info[0].sector_count; i++, addr += 0x10000)
  37. flash_info[0].start[i] = addr;
  38. return CONFIG_SYS_FLASH_SIZE;
  39. }
  40. void flash_print_info(flash_info_t *info)
  41. {
  42. printf("Flash: Vendor ID: 0x%02lx, Product ID: 0x%02lx\n",
  43. info->flash_id >> 16, info->flash_id & 0xffff);
  44. printf("Size: %ld MB in %d sectors\n",
  45. info->size >> 10, info->sector_count);
  46. }
  47. int flash_erase(flash_info_t *info, int s_first, int s_last)
  48. {
  49. unsigned long flags;
  50. unsigned long start_time;
  51. uint16_t *fb, *sb;
  52. unsigned int i;
  53. int ret;
  54. uint16_t status;
  55. if ((s_first < 0) || (s_first > s_last)
  56. || (s_last >= info->sector_count)) {
  57. puts("Error: first and/or last sector out of range\n");
  58. return ERR_INVAL;
  59. }
  60. for (i = s_first; i < s_last; i++)
  61. if (info->protect[i]) {
  62. printf("Error: sector %d is protected\n", i);
  63. return ERR_PROTECTED;
  64. }
  65. fb = (uint16_t *)uncached(info->start[0]);
  66. dcache_flush_unlocked();
  67. for (i = s_first; (i <= s_last) && !ctrlc(); i++) {
  68. printf("Erasing sector %3d...", i);
  69. sb = (uint16_t *)uncached(info->start[i]);
  70. flags = disable_interrupts();
  71. start_time = get_timer(0);
  72. /* Unlock sector */
  73. writew(0xaa, fb + 0x555);
  74. writew(0x70, sb);
  75. /* Erase sector */
  76. writew(0xaa, fb + 0x555);
  77. writew(0x55, fb + 0xaaa);
  78. writew(0x80, fb + 0x555);
  79. writew(0xaa, fb + 0x555);
  80. writew(0x55, fb + 0xaaa);
  81. writew(0x30, sb);
  82. /* Wait for completion */
  83. ret = ERR_OK;
  84. do {
  85. /* TODO: Timeout */
  86. status = readw(sb);
  87. } while ((status != 0xffff) && !(status & 0x28));
  88. writew(0xf0, fb);
  89. /*
  90. * Make sure the command actually makes it to the bus
  91. * before we re-enable interrupts.
  92. */
  93. readw(fb);
  94. if (flags)
  95. enable_interrupts();
  96. if (status != 0xffff) {
  97. printf("Flash erase error at address 0x%p: 0x%02x\n",
  98. sb, status);
  99. ret = ERR_PROG_ERROR;
  100. break;
  101. }
  102. }
  103. if (ctrlc())
  104. printf("User interrupt!\n");
  105. return ERR_OK;
  106. }
  107. int write_buff(flash_info_t *info, uchar *src,
  108. ulong addr, ulong count)
  109. {
  110. unsigned long flags;
  111. uint16_t *base, *p, *s, *end;
  112. uint16_t word, status, status1;
  113. int ret = ERR_OK;
  114. if (addr < info->start[0]
  115. || (addr + count) > (info->start[0] + info->size)
  116. || (addr + count) < addr) {
  117. puts("Error: invalid address range\n");
  118. return ERR_INVAL;
  119. }
  120. if (addr & 1 || count & 1 || (unsigned int)src & 1) {
  121. puts("Error: misaligned source, destination or count\n");
  122. return ERR_ALIGN;
  123. }
  124. base = (uint16_t *)uncached(info->start[0]);
  125. end = (uint16_t *)uncached(addr + count);
  126. flags = disable_interrupts();
  127. dcache_flush_unlocked();
  128. sync_write_buffer();
  129. for (p = (uint16_t *)uncached(addr), s = (uint16_t *)src;
  130. p < end && !ctrlc(); p++, s++) {
  131. word = *s;
  132. writew(0xaa, base + 0x555);
  133. writew(0x55, base + 0xaaa);
  134. writew(0xa0, base + 0x555);
  135. writew(word, p);
  136. sync_write_buffer();
  137. /* Wait for completion */
  138. status1 = readw(p);
  139. do {
  140. /* TODO: Timeout */
  141. status = status1;
  142. status1 = readw(p);
  143. } while (((status ^ status1) & 0x40) /* toggled */
  144. && !(status1 & 0x28)); /* error bits */
  145. /*
  146. * We'll need to check once again for toggle bit
  147. * because the toggle bit may stop toggling as I/O5
  148. * changes to "1" (ref at49bv642.pdf p9)
  149. */
  150. status1 = readw(p);
  151. status = readw(p);
  152. if ((status ^ status1) & 0x40) {
  153. printf("Flash write error at address 0x%p: "
  154. "0x%02x != 0x%02x\n",
  155. p, status,word);
  156. ret = ERR_PROG_ERROR;
  157. writew(0xf0, base);
  158. readw(base);
  159. break;
  160. }
  161. writew(0xf0, base);
  162. readw(base);
  163. }
  164. if (flags)
  165. enable_interrupts();
  166. return ret;
  167. }
  168. #endif /* CONFIG_FAVR32_EZKIT_EXT_FLASH */