eflash.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <flash.h>
  45. #include <asm/io.h>
  46. #include <asm/arch/hardware.h>
  47. #include <asm/arch/at91_common.h>
  48. #include <asm/arch/at91_eefc.h>
  49. #include <asm/arch/at91_dbu.h>
  50. /* checks to detect configuration errors */
  51. #if CONFIG_SYS_MAX_FLASH_BANKS!=1
  52. #error eflash: this driver can only handle 1 bank
  53. #endif
  54. /* global structure */
  55. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  56. static u32 pagesize;
  57. unsigned long flash_init(void)
  58. {
  59. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  60. at91_dbu_t *dbu = (at91_dbu_t *) ATMEL_BASE_DBGU;
  61. u32 id, size, nplanes, planesize, nlocks;
  62. u32 addr, i, tmp=0;
  63. debug("eflash: init\n");
  64. flash_info[0].flash_id = FLASH_UNKNOWN;
  65. /* check if its an AT91ARM9XE SoC */
  66. if ((readl(&dbu->cidr) & AT91_DBU_CID_ARCH_MASK) != AT91_DBU_CID_ARCH_9XExx) {
  67. puts("eflash: not an AT91SAM9XE\n");
  68. return 0;
  69. }
  70. /* now query the eflash for its structure */
  71. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GETD, &eefc->fcr);
  72. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  73. ;
  74. id = readl(&eefc->frr); /* word 0 */
  75. size = readl(&eefc->frr); /* word 1 */
  76. pagesize = readl(&eefc->frr); /* word 2 */
  77. nplanes = readl(&eefc->frr); /* word 3 */
  78. planesize = readl(&eefc->frr); /* word 4 */
  79. debug("id=%08x size=%u pagesize=%u planes=%u planesize=%u\n",
  80. id, size, pagesize, nplanes, planesize);
  81. for (i=1; i<nplanes; i++) {
  82. tmp = readl(&eefc->frr); /* words 5..4+nplanes-1 */
  83. };
  84. nlocks = readl(&eefc->frr); /* word 4+nplanes */
  85. debug("nlocks=%u\n", nlocks);
  86. /* since we are going to use the lock regions as sectors, check count */
  87. if (nlocks > CONFIG_SYS_MAX_FLASH_SECT) {
  88. printf("eflash: number of lock regions(%u) "\
  89. "> CONFIG_SYS_MAX_FLASH_SECT. reducing...\n",
  90. nlocks);
  91. nlocks = CONFIG_SYS_MAX_FLASH_SECT;
  92. }
  93. flash_info[0].size = size;
  94. flash_info[0].sector_count = nlocks;
  95. flash_info[0].flash_id = id;
  96. addr = ATMEL_BASE_FLASH;
  97. for (i=0; i<nlocks; i++) {
  98. tmp = readl(&eefc->frr); /* words 4+nplanes+1.. */
  99. flash_info[0].start[i] = addr;
  100. flash_info[0].protect[i] = 0;
  101. addr += tmp;
  102. };
  103. /* now read the protection information for all regions */
  104. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
  105. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  106. ;
  107. for (i=0; i<flash_info[0].sector_count; i++) {
  108. if (i%32 == 0)
  109. tmp = readl(&eefc->frr);
  110. flash_info[0].protect[i] = (tmp >> (i%32)) & 1;
  111. #if defined(CONFIG_EFLASH_PROTSECTORS)
  112. if (i < CONFIG_EFLASH_PROTSECTORS)
  113. flash_info[0].protect[i] = 1;
  114. #endif
  115. }
  116. return size;
  117. }
  118. void flash_print_info(flash_info_t *info)
  119. {
  120. int i;
  121. puts("AT91SAM9XE embedded flash\n Size: ");
  122. print_size(info->size, " in ");
  123. printf("%d Sectors\n", info->sector_count);
  124. printf(" Sector Start Addresses:");
  125. for (i=0; i<info->sector_count; ++i) {
  126. if ((i % 5) == 0)
  127. printf("\n ");
  128. printf(" %08lX%s",
  129. info->start[i],
  130. info->protect[i] ? " (RO)" : " "
  131. );
  132. }
  133. printf ("\n");
  134. return;
  135. }
  136. int flash_real_protect (flash_info_t *info, long sector, int prot)
  137. {
  138. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  139. u32 pagenum = (info->start[sector]-ATMEL_BASE_FLASH)/pagesize;
  140. u32 i, tmp=0;
  141. debug("protect sector=%ld prot=%d\n", sector, prot);
  142. #if defined(CONFIG_EFLASH_PROTSECTORS)
  143. if (sector < CONFIG_EFLASH_PROTSECTORS) {
  144. if (!prot) {
  145. printf("eflash: sector %lu cannot be unprotected\n",
  146. sector);
  147. }
  148. return 1; /* return anyway, caller does not care for result */
  149. }
  150. #endif
  151. if (prot) {
  152. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_SLB |
  153. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  154. } else {
  155. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_CLB |
  156. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  157. }
  158. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  159. ;
  160. /* now re-read the protection information for all regions */
  161. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
  162. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  163. ;
  164. for (i=0; i<info->sector_count; i++) {
  165. if (i%32 == 0)
  166. tmp = readl(&eefc->frr);
  167. info->protect[i] = (tmp >> (i%32)) & 1;
  168. }
  169. return 0;
  170. }
  171. static u32 erase_write_page (u32 pagenum)
  172. {
  173. at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
  174. debug("erase+write page=%u\n", pagenum);
  175. /* give erase and write page command */
  176. writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_EWP |
  177. (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
  178. while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
  179. ;
  180. /* return status */
  181. return readl(&eefc->fsr)
  182. & (AT91_EEFC_FSR_FCMDE | AT91_EEFC_FSR_FLOCKE);
  183. }
  184. int flash_erase(flash_info_t *info, int s_first, int s_last)
  185. {
  186. debug("erase first=%d last=%d\n", s_first, s_last);
  187. puts("this flash does not need and support erasing!\n");
  188. return 0;
  189. }
  190. /*
  191. * Copy memory to flash, returns:
  192. * 0 - OK
  193. * 1 - write timeout
  194. */
  195. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  196. {
  197. u32 pagenum;
  198. u32 *src32, *dst32;
  199. u32 i;
  200. debug("write src=%08lx addr=%08lx cnt=%lx\n",
  201. (ulong)src, addr, cnt);
  202. /* REQUIRE addr to be on a page start, abort if not */
  203. if (addr % pagesize) {
  204. printf ("eflash: start %08lx is not on page start\n"\
  205. " write aborted\n", addr);
  206. return 1;
  207. }
  208. /* now start copying data */
  209. pagenum = (addr-ATMEL_BASE_FLASH)/pagesize;
  210. src32 = (u32 *) src;
  211. dst32 = (u32 *) addr;
  212. while (cnt > 0) {
  213. i = pagesize / 4;
  214. /* fill page buffer */
  215. while (i--)
  216. *dst32++ = *src32++;
  217. /* write page */
  218. if (erase_write_page(pagenum))
  219. return 1;
  220. pagenum++;
  221. if (cnt > pagesize)
  222. cnt -= pagesize;
  223. else
  224. cnt = 0;
  225. }
  226. return 0;
  227. }