1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <common.h>
47#include <asm/io.h>
48#include <asm/arch/hardware.h>
49#include <asm/arch/at91_common.h>
50#include <asm/arch/at91_eefc.h>
51#include <asm/arch/at91_dbu.h>
52
53
54#if CONFIG_SYS_MAX_FLASH_BANKS!=1
55#error eflash: this driver can only handle 1 bank
56#endif
57
58
59flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
60static u32 pagesize;
61
62unsigned long flash_init (void)
63{
64 at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
65 at91_dbu_t *dbu = (at91_dbu_t *) ATMEL_BASE_DBGU;
66 u32 id, size, nplanes, planesize, nlocks;
67 u32 addr, i, tmp=0;
68
69 debug("eflash: init\n");
70
71 flash_info[0].flash_id = FLASH_UNKNOWN;
72
73
74 if ((readl(&dbu->cidr) & AT91_DBU_CID_ARCH_MASK) != AT91_DBU_CID_ARCH_9XExx) {
75 puts("eflash: not an AT91SAM9XE\n");
76 return 0;
77 }
78
79
80 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GETD, &eefc->fcr);
81 while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
82 ;
83 id = readl(&eefc->frr);
84 size = readl(&eefc->frr);
85 pagesize = readl(&eefc->frr);
86 nplanes = readl(&eefc->frr);
87 planesize = readl(&eefc->frr);
88 debug("id=%08x size=%u pagesize=%u planes=%u planesize=%u\n",
89 id, size, pagesize, nplanes, planesize);
90 for (i=1; i<nplanes; i++) {
91 tmp = readl(&eefc->frr);
92 };
93 nlocks = readl(&eefc->frr);
94 debug("nlocks=%u\n", nlocks);
95
96 if (nlocks > CONFIG_SYS_MAX_FLASH_SECT) {
97 printf("eflash: number of lock regions(%u) "\
98 "> CONFIG_SYS_MAX_FLASH_SECT. reducing...\n",
99 nlocks);
100 nlocks = CONFIG_SYS_MAX_FLASH_SECT;
101 }
102 flash_info[0].size = size;
103 flash_info[0].sector_count = nlocks;
104 flash_info[0].flash_id = id;
105
106 addr = ATMEL_BASE_FLASH;
107 for (i=0; i<nlocks; i++) {
108 tmp = readl(&eefc->frr);
109 flash_info[0].start[i] = addr;
110 flash_info[0].protect[i] = 0;
111 addr += tmp;
112 };
113
114
115 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
116 while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
117 ;
118 for (i=0; i<flash_info[0].sector_count; i++) {
119 if (i%32 == 0)
120 tmp = readl(&eefc->frr);
121 flash_info[0].protect[i] = (tmp >> (i%32)) & 1;
122#if defined(CONFIG_EFLASH_PROTSECTORS)
123 if (i < CONFIG_EFLASH_PROTSECTORS)
124 flash_info[0].protect[i] = 1;
125#endif
126 }
127
128 return size;
129}
130
131void flash_print_info (flash_info_t *info)
132{
133 int i;
134
135 puts("AT91SAM9XE embedded flash\n Size: ");
136 print_size(info->size, " in ");
137 printf("%d Sectors\n", info->sector_count);
138
139 printf(" Sector Start Addresses:");
140 for (i=0; i<info->sector_count; ++i) {
141 if ((i % 5) == 0)
142 printf("\n ");
143 printf(" %08lX%s",
144 info->start[i],
145 info->protect[i] ? " (RO)" : " "
146 );
147 }
148 printf ("\n");
149 return;
150}
151
152int flash_real_protect (flash_info_t *info, long sector, int prot)
153{
154 at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
155 u32 pagenum = (info->start[sector]-ATMEL_BASE_FLASH)/pagesize;
156 u32 i, tmp=0;
157
158 debug("protect sector=%ld prot=%d\n", sector, prot);
159
160#if defined(CONFIG_EFLASH_PROTSECTORS)
161 if (sector < CONFIG_EFLASH_PROTSECTORS) {
162 if (!prot) {
163 printf("eflash: sector %lu cannot be unprotected\n",
164 sector);
165 }
166 return 1;
167 }
168#endif
169 if (prot) {
170 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_SLB |
171 (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
172 } else {
173 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_CLB |
174 (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
175 }
176 while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
177 ;
178
179 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_GLB, &eefc->fcr);
180 while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
181 ;
182 for (i=0; i<info->sector_count; i++) {
183 if (i%32 == 0)
184 tmp = readl(&eefc->frr);
185 info->protect[i] = (tmp >> (i%32)) & 1;
186 }
187 return 0;
188}
189
190static u32 erase_write_page (u32 pagenum)
191{
192 at91_eefc_t *eefc = (at91_eefc_t *) ATMEL_BASE_EEFC;
193
194 debug("erase+write page=%u\n", pagenum);
195
196
197 writel(AT91_EEFC_FCR_KEY | AT91_EEFC_FCR_FCMD_EWP |
198 (pagenum << AT91_EEFC_FCR_FARG_SHIFT), &eefc->fcr);
199 while ((readl(&eefc->fsr) & AT91_EEFC_FSR_FRDY) == 0)
200 ;
201
202 return readl(&eefc->fsr)
203 & (AT91_EEFC_FSR_FCMDE | AT91_EEFC_FSR_FLOCKE);
204}
205
206int flash_erase (flash_info_t *info, int s_first, int s_last)
207{
208 debug("erase first=%d last=%d\n", s_first, s_last);
209 puts("this flash does not need and support erasing!\n");
210 return 0;
211}
212
213
214
215
216
217
218
219int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
220{
221 u32 pagenum;
222 u32 *src32, *dst32;
223 u32 i;
224
225 debug("write src=%08lx addr=%08lx cnt=%lx\n",
226 (ulong)src, addr, cnt);
227
228
229 if (addr % pagesize) {
230 printf ("eflash: start %08lx is not on page start\n"\
231 " write aborted\n", addr);
232 return 1;
233 }
234
235
236 pagenum = (addr-ATMEL_BASE_FLASH)/pagesize;
237 src32 = (u32 *) src;
238 dst32 = (u32 *) addr;
239 while (cnt > 0) {
240 i = pagesize / 4;
241
242 while (i--)
243 *dst32++ = *src32++;
244
245 if (erase_write_page(pagenum))
246 return 1;
247 pagenum++;
248 if (cnt > pagesize)
249 cnt -= pagesize;
250 else
251 cnt = 0;
252 }
253 return 0;
254}
255