eflash.c 7.1 KB

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