eflash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010
  4. * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
  5. */
  6. /*
  7. * this driver supports the enhanced embedded flash in the Atmel
  8. * AT91SAM9XE devices with the following geometry:
  9. *
  10. * AT91SAM9XE128: 1 plane of 8 regions of 32 pages (total 256 pages)
  11. * AT91SAM9XE256: 1 plane of 16 regions of 32 pages (total 512 pages)
  12. * AT91SAM9XE512: 1 plane of 32 regions of 32 pages (total 1024 pages)
  13. * (the exact geometry is read from the flash at runtime, so any
  14. * future devices should already be covered)
  15. *
  16. * Regions can be write/erase protected.
  17. * Whole (!) pages can be individually written with erase on the fly.
  18. * Writing partial pages will corrupt the rest of the page.
  19. *
  20. * The flash is presented to u-boot with each region being a sector,
  21. * having the following effects:
  22. * Each sector can be hardware protected (protect on/off).
  23. * Each page in a sector can be rewritten anytime.
  24. * Since pages are erased when written, the "erase" does nothing.
  25. * The first "CONFIG_EFLASH_PROTSECTORS" cannot be unprotected
  26. * by u-Boot commands.
  27. *
  28. * Note: Redundant environment will not work in this flash since
  29. * it does use partial page writes. Make sure the environment spans
  30. * whole pages!
  31. */
  32. /*
  33. * optional TODOs (nice to have features):
  34. *
  35. * make the driver coexist with other NOR flash drivers
  36. * (use an index into flash_info[], requires work
  37. * in those other drivers, too)
  38. * Make the erase command fill the sectors with 0xff
  39. * (if the flashes grow larger in the future and
  40. * someone puts a jffs2 into them)
  41. * do a read-modify-write for partially programmed pages
  42. */
  43. #include <common.h>
  44. #include <asm/io.h>
  45. #include <asm/arch/hardware.h>
  46. #include <asm/arch/at91_common.h>
  47. #include <asm/arch/at91_eefc.h>
  48. #include <asm/arch/at91_dbu.h>
  49. /* checks to detect configuration errors */
  50. #if CONFIG_SYS_MAX_FLASH_BANKS!=1
  51. #error eflash: this driver can only handle 1 bank
  52. #endif
  53. /* global structure */
  54. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  55. static u32 pagesize;
  56. unsigned long flash_init(void)
  57. {
  58. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  59. at91_dbu_t *dbu = (at91_dbu_t *) ATMEL_BASE_DBGU;
  60. u32 id, size, nplanes, planesize, nlocks;
  61. u32 addr, i, tmp=0;
  62. debug("eflash: init\n");
  63. flash_info[0].flash_id = FLASH_UNKNOWN;
  64. /* check if its an AT91ARM9XE SoC */
  65. if ((readl(&dbu->cidr) & AT91_DBU_CID_ARCH_MASK) != AT91_DBU_CID_ARCH_9XExx) {
  66. puts("eflash: not an AT91SAM9XE\n");
  67. return 0;
  68. }
  69. /* now query the eflash for its structure */
  70. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GETD, &eefc->fcr);
  71. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  72. ;
  73. id = readl(&eefc->frr); /* word 0 */
  74. size = readl(&eefc->frr); /* word 1 */
  75. pagesize = readl(&eefc->frr); /* word 2 */
  76. nplanes = readl(&eefc->frr); /* word 3 */
  77. planesize = readl(&eefc->frr); /* word 4 */
  78. debug("id=%08x size=%u pagesize=%u planes=%u planesize=%u\n",
  79. id, size, pagesize, nplanes, planesize);
  80. for (i=1; i<nplanes; i++) {
  81. tmp = readl(&eefc->frr); /* words 5..4+nplanes-1 */
  82. };
  83. nlocks = readl(&eefc->frr); /* word 4+nplanes */
  84. debug("nlocks=%u\n", nlocks);
  85. /* since we are going to use the lock regions as sectors, check count */
  86. if (nlocks > CONFIG_SYS_MAX_FLASH_SECT) {
  87. printf("eflash: number of lock regions(%u) "\
  88. "> CONFIG_SYS_MAX_FLASH_SECT. reducing...\n",
  89. nlocks);
  90. nlocks = CONFIG_SYS_MAX_FLASH_SECT;
  91. }
  92. flash_info[0].size = size;
  93. flash_info[0].sector_count = nlocks;
  94. flash_info[0].flash_id = id;
  95. addr = ATMEL_BASE_FLASH;
  96. for (i=0; i<nlocks; i++) {
  97. tmp = readl(&eefc->frr); /* words 4+nplanes+1.. */
  98. flash_info[0].start[i] = addr;
  99. flash_info[0].protect[i] = 0;
  100. addr += tmp;
  101. };
  102. /* now read the protection information for all regions */
  103. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
  104. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  105. ;
  106. for (i=0; i<flash_info[0].sector_count; i++) {
  107. if (i%32 == 0)
  108. tmp = readl(&eefc->frr);
  109. flash_info[0].protect[i] = (tmp >> (i%32)) & 1;
  110. #if defined(CONFIG_EFLASH_PROTSECTORS)
  111. if (i < CONFIG_EFLASH_PROTSECTORS)
  112. flash_info[0].protect[i] = 1;
  113. #endif
  114. }
  115. return size;
  116. }
  117. void flash_print_info(flash_info_t *info)
  118. {
  119. int i;
  120. puts("AT91SAM9XE embedded flash\n Size: ");
  121. print_size(info->size, " in ");
  122. printf("%d Sectors\n", info->sector_count);
  123. printf(" Sector Start Addresses:");
  124. for (i=0; i<info->sector_count; ++i) {
  125. if ((i % 5) == 0)
  126. printf("\n ");
  127. printf(" %08lX%s",
  128. info->start[i],
  129. info->protect[i] ? " (RO)" : " "
  130. );
  131. }
  132. printf ("\n");
  133. return;
  134. }
  135. int flash_real_protect (flash_info_t *info, long sector, int prot)
  136. {
  137. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  138. u32 pagenum = (info->start[sector]-ATMEL_BASE_FLASH)/pagesize;
  139. u32 i, tmp=0;
  140. debug("protect sector=%ld prot=%d\n", sector, prot);
  141. #if defined(CONFIG_EFLASH_PROTSECTORS)
  142. if (sector < CONFIG_EFLASH_PROTSECTORS) {
  143. if (!prot) {
  144. printf("eflash: sector %lu cannot be unprotected\n",
  145. sector);
  146. }
  147. return 1; /* return anyway, caller does not care for result */
  148. }
  149. #endif
  150. if (prot) {
  151. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_SLB |
  152. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  153. } else {
  154. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_CLB |
  155. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  156. }
  157. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  158. ;
  159. /* now re-read the protection information for all regions */
  160. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
  161. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  162. ;
  163. for (i=0; i<info->sector_count; i++) {
  164. if (i%32 == 0)
  165. tmp = readl(&eefc->frr);
  166. info->protect[i] = (tmp >> (i%32)) & 1;
  167. }
  168. return 0;
  169. }
  170. static u32 erase_write_page (u32 pagenum)
  171. {
  172. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  173. debug("erase+write page=%u\n", pagenum);
  174. /* give erase and write page command */
  175. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_EWP |
  176. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  177. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  178. ;
  179. /* return status */
  180. return readl(&eefc->fsr)
  181. & (AT91_EEFC_FSR_FCMDE | AT91_EEFC_FSR_FLOCKE);
  182. }
  183. int flash_erase(flash_info_t *info, int s_first, int s_last)
  184. {
  185. debug("erase first=%d last=%d\n", s_first, s_last);
  186. puts("this flash does not need and support erasing!\n");
  187. return 0;
  188. }
  189. /*
  190. * Copy memory to flash, returns:
  191. * 0 - OK
  192. * 1 - write timeout
  193. */
  194. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  195. {
  196. u32 pagenum;
  197. u32 *src32, *dst32;
  198. u32 i;
  199. debug("write src=%08lx addr=%08lx cnt=%lx\n",
  200. (ulong)src, addr, cnt);
  201. /* REQUIRE addr to be on a page start, abort if not */
  202. if (addr % pagesize) {
  203. printf ("eflash: start %08lx is not on page start\n"\
  204. " write aborted\n", addr);
  205. return 1;
  206. }
  207. /* now start copying data */
  208. pagenum = (addr-ATMEL_BASE_FLASH)/pagesize;
  209. src32 = (u32 *) src;
  210. dst32 = (u32 *) addr;
  211. while (cnt > 0) {
  212. i = pagesize / 4;
  213. /* fill page buffer */
  214. while (i--)
  215. *dst32++ = *src32++;
  216. /* write page */
  217. if (erase_write_page(pagenum))
  218. return 1;
  219. pagenum++;
  220. if (cnt > pagesize)
  221. cnt -= pagesize;
  222. else
  223. cnt = 0;
  224. }
  225. return 0;
  226. }