cfm_flash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Basic Flash Driver for Freescale MCF 5281/5282 internal FLASH
  3. *
  4. * (C) Copyright 2005 BuS Elektronik GmbH & Co.KG <esw@bus-elektonik.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/m5282.h>
  26. #include "cfm_flash.h"
  27. #if defined(CONFIG_M5281) || defined(CONFIG_M5282)
  28. #if (CONFIG_SYS_CLK>20000000)
  29. #define CFM_CLK (((long) CONFIG_SYS_CLK / (400000 * 8) + 1) | 0x40)
  30. #else
  31. #define CFM_CLK ((long) CONFIG_SYS_CLK / 400000 + 1)
  32. #endif
  33. #define cmf_backdoor_address(addr) (((addr) & 0x0007FFFF) | 0x04000000 | \
  34. (CONFIG_SYS_MBAR & 0xC0000000))
  35. void cfm_flash_print_info (flash_info_t * info)
  36. {
  37. printf ("Freescale: ");
  38. switch (info->flash_id & FLASH_TYPEMASK) {
  39. case FREESCALE_ID_MCF5281 & FLASH_TYPEMASK:
  40. printf ("MCF5281 internal FLASH\n");
  41. break;
  42. case FREESCALE_ID_MCF5282 & FLASH_TYPEMASK:
  43. printf ("MCF5282 internal FLASH\n");
  44. break;
  45. default:
  46. printf ("Unknown Chip Type\n");
  47. break;
  48. }
  49. }
  50. void cfm_flash_init (flash_info_t * info)
  51. {
  52. int sector;
  53. ulong protection;
  54. MCFCFM_MCR = 0;
  55. MCFCFM_CLKD = CFM_CLK;
  56. debug ("CFM Clock divider: %ld (%d Hz @ %ld Hz)\n",CFM_CLK,\
  57. CONFIG_SYS_CLK / (2* ((CFM_CLK & 0x3F)+1) * (1+((CFM_CLK & 0x40)>>6)*7)),\
  58. CONFIG_SYS_CLK);
  59. MCFCFM_SACC = 0;
  60. MCFCFM_DACC = 0;
  61. if (MCFCFM_SEC & MCFCFM_SEC_KEYEN)
  62. puts("CFM backdoor access is enabled\n");
  63. if (MCFCFM_SEC & MCFCFM_SEC_SECSTAT)
  64. puts("CFM securety is enabled\n");
  65. #ifdef CONFIG_M5281
  66. info->flash_id = (FREESCALE_MANUFACT & FLASH_VENDMASK) |
  67. (FREESCALE_ID_MCF5281 & FLASH_TYPEMASK);
  68. info->size = 256*1024;
  69. info->sector_count = 16;
  70. #else
  71. info->flash_id = (FREESCALE_MANUFACT & FLASH_VENDMASK) |
  72. (FREESCALE_ID_MCF5282 & FLASH_TYPEMASK);
  73. info->size = 512*1024;
  74. info->sector_count = 32;
  75. #endif
  76. protection = MCFCFM_PROT;
  77. for (sector = 0; sector < info->sector_count; sector++)
  78. {
  79. if (sector == 0)
  80. {
  81. info->start[sector] = CONFIG_SYS_INT_FLASH_BASE;
  82. }
  83. else
  84. {
  85. info->start[sector] = info->start[sector-1] + 0x04000;
  86. }
  87. info->protect[sector] = protection & 1;
  88. protection >>= 1;
  89. }
  90. }
  91. int cfm_flash_readycheck(int checkblank)
  92. {
  93. int rc;
  94. unsigned char state;
  95. rc = ERR_OK;
  96. while (!(MCFCFM_USTAT & MCFCFM_USTAT_CCIF));
  97. state = MCFCFM_USTAT;
  98. if (state & MCFCFM_USTAT_ACCERR)
  99. {
  100. debug ("%s(): CFM access error",__FUNCTION__);
  101. rc = ERR_PROG_ERROR;
  102. }
  103. if (state & MCFCFM_USTAT_PVIOL)
  104. {
  105. debug ("%s(): CFM protection violation",__FUNCTION__);
  106. rc = ERR_PROTECTED;
  107. }
  108. if (checkblank)
  109. {
  110. if (!(state & MCFCFM_USTAT_BLANK))
  111. {
  112. debug ("%s(): CFM erras error",__FUNCTION__);
  113. rc = ERR_NOT_ERASED;
  114. }
  115. }
  116. MCFCFM_USTAT = state & 0x34; /* reset state */
  117. return rc;
  118. }
  119. /* Erase 16KiB = 8 2KiB pages */
  120. int cfm_flash_erase_sector (flash_info_t * info, int sector)
  121. {
  122. ulong address;
  123. int page;
  124. int rc;
  125. rc= ERR_OK;
  126. address = cmf_backdoor_address(info->start[sector]);
  127. for (page=0; (page<8) && (rc==ERR_OK); page++)
  128. {
  129. *(volatile __u32*) address = 0;
  130. MCFCFM_CMD = MCFCFM_CMD_PGERS;
  131. MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
  132. rc = cfm_flash_readycheck(0);
  133. if (rc==ERR_OK)
  134. {
  135. *(volatile __u32*) address = 0;
  136. MCFCFM_CMD = MCFCFM_CMD_PGERSVER;
  137. MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
  138. rc = cfm_flash_readycheck(1);
  139. }
  140. address += 0x800;
  141. }
  142. return rc;
  143. }
  144. int cfm_flash_write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  145. {
  146. int rc;
  147. ulong dest, data;
  148. rc = ERR_OK;
  149. if (addr & 3)
  150. {
  151. debug ("Byte and Word alignment not supported\n");
  152. rc = ERR_ALIGN;
  153. }
  154. if (cnt & 3)
  155. {
  156. debug ("Byte and Word transfer not supported\n");
  157. rc = ERR_ALIGN;
  158. }
  159. dest = cmf_backdoor_address(addr);
  160. while ((cnt>=4) && (rc == ERR_OK))
  161. {
  162. data = *((volatile u32 *) src);
  163. *(volatile u32*) dest = data;
  164. MCFCFM_CMD = MCFCFM_CMD_PGM;
  165. MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
  166. rc = cfm_flash_readycheck(0);
  167. if (*(volatile u32*) addr != data) rc = ERR_PROG_ERROR;
  168. src +=4;
  169. dest +=4;
  170. addr +=4;
  171. cnt -=4;
  172. }
  173. return rc;
  174. }
  175. #ifdef CONFIG_SYS_FLASH_PROTECTION
  176. int cfm_flash_protect(flash_info_t * info,long sector,int prot)
  177. {
  178. int rc;
  179. rc= ERR_OK;
  180. if (prot)
  181. {
  182. MCFCFM_PROT |= (1<<sector);
  183. info->protect[sector]=1;
  184. }
  185. else
  186. {
  187. MCFCFM_PROT &= ~(1<<sector);
  188. info->protect[sector]=0;
  189. }
  190. return rc;
  191. }
  192. #endif
  193. #endif