eflash.c 7.8 KB

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