]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/dataflash.c
env: Rename setenv() to env_set()
[people/ms/u-boot.git] / drivers / mtd / dataflash.c
1 /*
2 * LowLevel function for ATMEL DataFlash support
3 * Author : Hamid Ikdoumi (Atmel)
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7 #include <common.h>
8 #include <config.h>
9 #include <asm/hardware.h>
10 #include <dataflash.h>
11
12 static AT91S_DataFlash DataFlashInst;
13
14 extern void AT91F_SpiInit (void);
15 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
16 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
17 unsigned long addr,
18 unsigned long size, char *buffer);
19 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
20 unsigned char *src,
21 int dest,
22 int size );
23
24 int AT91F_DataflashInit (void)
25 {
26 int i, j;
27 int dfcode;
28 int part;
29 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
30 unsigned char protected;
31
32 AT91F_SpiInit ();
33
34 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
35 found[i] = 0;
36 dataflash_info[i].Desc.state = IDLE;
37 dataflash_info[i].id = 0;
38 dataflash_info[i].Device.pages_number = 0;
39 dfcode = AT91F_DataflashProbe (cs[i].cs,
40 &dataflash_info[i].Desc);
41
42 switch (dfcode) {
43 case AT45DB021:
44 dataflash_info[i].Device.pages_number = 1024;
45 dataflash_info[i].Device.pages_size = 264;
46 dataflash_info[i].Device.page_offset = 9;
47 dataflash_info[i].Device.byte_mask = 0x300;
48 dataflash_info[i].Device.cs = cs[i].cs;
49 dataflash_info[i].Desc.DataFlash_state = IDLE;
50 dataflash_info[i].logical_address = cs[i].addr;
51 dataflash_info[i].id = dfcode;
52 found[i] += dfcode;
53 break;
54
55 case AT45DB081:
56 dataflash_info[i].Device.pages_number = 4096;
57 dataflash_info[i].Device.pages_size = 264;
58 dataflash_info[i].Device.page_offset = 9;
59 dataflash_info[i].Device.byte_mask = 0x300;
60 dataflash_info[i].Device.cs = cs[i].cs;
61 dataflash_info[i].Desc.DataFlash_state = IDLE;
62 dataflash_info[i].logical_address = cs[i].addr;
63 dataflash_info[i].id = dfcode;
64 found[i] += dfcode;
65 break;
66
67 case AT45DB161:
68 dataflash_info[i].Device.pages_number = 4096;
69 dataflash_info[i].Device.pages_size = 528;
70 dataflash_info[i].Device.page_offset = 10;
71 dataflash_info[i].Device.byte_mask = 0x300;
72 dataflash_info[i].Device.cs = cs[i].cs;
73 dataflash_info[i].Desc.DataFlash_state = IDLE;
74 dataflash_info[i].logical_address = cs[i].addr;
75 dataflash_info[i].id = dfcode;
76 found[i] += dfcode;
77 break;
78
79 case AT45DB321:
80 dataflash_info[i].Device.pages_number = 8192;
81 dataflash_info[i].Device.pages_size = 528;
82 dataflash_info[i].Device.page_offset = 10;
83 dataflash_info[i].Device.byte_mask = 0x300;
84 dataflash_info[i].Device.cs = cs[i].cs;
85 dataflash_info[i].Desc.DataFlash_state = IDLE;
86 dataflash_info[i].logical_address = cs[i].addr;
87 dataflash_info[i].id = dfcode;
88 found[i] += dfcode;
89 break;
90
91 case AT45DB642:
92 dataflash_info[i].Device.pages_number = 8192;
93 dataflash_info[i].Device.pages_size = 1056;
94 dataflash_info[i].Device.page_offset = 11;
95 dataflash_info[i].Device.byte_mask = 0x700;
96 dataflash_info[i].Device.cs = cs[i].cs;
97 dataflash_info[i].Desc.DataFlash_state = IDLE;
98 dataflash_info[i].logical_address = cs[i].addr;
99 dataflash_info[i].id = dfcode;
100 found[i] += dfcode;
101 break;
102
103 case AT45DB128:
104 dataflash_info[i].Device.pages_number = 16384;
105 dataflash_info[i].Device.pages_size = 1056;
106 dataflash_info[i].Device.page_offset = 11;
107 dataflash_info[i].Device.byte_mask = 0x700;
108 dataflash_info[i].Device.cs = cs[i].cs;
109 dataflash_info[i].Desc.DataFlash_state = IDLE;
110 dataflash_info[i].logical_address = cs[i].addr;
111 dataflash_info[i].id = dfcode;
112 found[i] += dfcode;
113 break;
114
115 default:
116 dfcode = 0;
117 break;
118 }
119 /* set the last area end to the dataflash size*/
120 dataflash_info[i].end_address =
121 (dataflash_info[i].Device.pages_number *
122 dataflash_info[i].Device.pages_size) - 1;
123
124 part = 0;
125 /* set the area addresses */
126 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
127 if(found[i]!=0) {
128 dataflash_info[i].Device.area_list[j].start =
129 area_list[part].start +
130 dataflash_info[i].logical_address;
131 if(area_list[part].end == 0xffffffff) {
132 dataflash_info[i].Device.area_list[j].end =
133 dataflash_info[i].end_address +
134 dataflash_info[i].logical_address;
135 } else {
136 dataflash_info[i].Device.area_list[j].end =
137 area_list[part].end +
138 dataflash_info[i].logical_address;
139 }
140 protected = area_list[part].protected;
141 /* Set the environment according to the label...*/
142 if(protected == FLAG_PROTECT_INVALID) {
143 dataflash_info[i].Device.area_list[j].protected =
144 FLAG_PROTECT_INVALID;
145 } else {
146 dataflash_info[i].Device.area_list[j].protected =
147 protected;
148 }
149 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
150 (const char *)area_list[part].label);
151 }
152 part++;
153 }
154 }
155 return found[0];
156 }
157
158 void AT91F_Dataflashenv_set(void)
159 {
160 int i, j;
161 int part;
162 unsigned char env;
163 unsigned char s[32]; /* Will fit a long int in hex */
164 unsigned long start;
165
166 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
167 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
168 env = area_list[part].setenv;
169 /* Set the environment according to the label...*/
170 if((env & FLAG_SETENV) == FLAG_SETENV) {
171 start = dataflash_info[i].Device.area_list[j].start;
172 sprintf((char *)s, "%lX", start);
173 env_set((char *)area_list[part].label,
174 (char *)s);
175 }
176 part++;
177 }
178 }
179 }
180
181 void dataflash_print_info (void)
182 {
183 int i, j;
184
185 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
186 if (dataflash_info[i].id != 0) {
187 printf("DataFlash:");
188 switch (dataflash_info[i].id) {
189 case AT45DB021:
190 printf("AT45DB021\n");
191 break;
192 case AT45DB161:
193 printf("AT45DB161\n");
194 break;
195
196 case AT45DB321:
197 printf("AT45DB321\n");
198 break;
199
200 case AT45DB642:
201 printf("AT45DB642\n");
202 break;
203 case AT45DB128:
204 printf("AT45DB128\n");
205 break;
206 }
207
208 printf("Nb pages: %6d\n"
209 "Page Size: %6d\n"
210 "Size=%8d bytes\n"
211 "Logical address: 0x%08X\n",
212 (unsigned int) dataflash_info[i].Device.pages_number,
213 (unsigned int) dataflash_info[i].Device.pages_size,
214 (unsigned int) dataflash_info[i].Device.pages_number *
215 dataflash_info[i].Device.pages_size,
216 (unsigned int) dataflash_info[i].logical_address);
217 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
218 switch(dataflash_info[i].Device.area_list[j].protected) {
219 case FLAG_PROTECT_SET:
220 case FLAG_PROTECT_CLEAR:
221 printf("Area %i:\t%08lX to %08lX %s", j,
222 dataflash_info[i].Device.area_list[j].start,
223 dataflash_info[i].Device.area_list[j].end,
224 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
225 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
226 break;
227 case FLAG_PROTECT_INVALID:
228 break;
229 }
230 }
231 }
232 }
233 }
234
235 /*---------------------------------------------------------------------------*/
236 /* Function Name : AT91F_DataflashSelect */
237 /* Object : Select the correct device */
238 /*---------------------------------------------------------------------------*/
239 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
240 unsigned long *addr)
241 {
242 char addr_valid = 0;
243 int i;
244
245 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
246 if ( dataflash_info[i].id
247 && ((((int) *addr) & 0xFF000000) ==
248 dataflash_info[i].logical_address)) {
249 addr_valid = 1;
250 break;
251 }
252 if (!addr_valid) {
253 pFlash = (AT91PS_DataFlash) 0;
254 return pFlash;
255 }
256 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
257 pFlash->pDevice = &(dataflash_info[i].Device);
258 *addr -= dataflash_info[i].logical_address;
259 return (pFlash);
260 }
261
262 /*---------------------------------------------------------------------------*/
263 /* Function Name : addr_dataflash */
264 /* Object : Test if address is valid */
265 /*---------------------------------------------------------------------------*/
266 int addr_dataflash (unsigned long addr)
267 {
268 int addr_valid = 0;
269 int i;
270
271 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
272 if ((((int) addr) & 0xFF000000) ==
273 dataflash_info[i].logical_address) {
274 addr_valid = 1;
275 break;
276 }
277 }
278
279 return addr_valid;
280 }
281
282 /*---------------------------------------------------------------------------*/
283 /* Function Name : size_dataflash */
284 /* Object : Test if address is valid regarding the size */
285 /*---------------------------------------------------------------------------*/
286 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
287 unsigned long size)
288 {
289 /* is outside the dataflash */
290 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
291 pdataFlash->pDevice->pages_number)) return 0;
292 /* is too large for the dataflash */
293 if (size > ((pdataFlash->pDevice->pages_size *
294 pdataFlash->pDevice->pages_number) -
295 ((int)addr & 0x0FFFFFFF))) return 0;
296
297 return 1;
298 }
299
300 /*---------------------------------------------------------------------------*/
301 /* Function Name : prot_dataflash */
302 /* Object : Test if destination area is protected */
303 /*---------------------------------------------------------------------------*/
304 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
305 {
306 int area;
307
308 /* find area */
309 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
310 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
311 (addr < pdataFlash->pDevice->area_list[area].end))
312 break;
313 }
314 if (area == NB_DATAFLASH_AREA)
315 return -1;
316
317 /*test protection value*/
318 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
319 return 0;
320 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
321 return 0;
322
323 return 1;
324 }
325
326 /*--------------------------------------------------------------------------*/
327 /* Function Name : dataflash_real_protect */
328 /* Object : protect/unprotect area */
329 /*--------------------------------------------------------------------------*/
330 int dataflash_real_protect (int flag, unsigned long start_addr,
331 unsigned long end_addr)
332 {
333 int i,j, area1, area2, addr_valid = 0;
334
335 /* find dataflash */
336 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
337 if ((((int) start_addr) & 0xF0000000) ==
338 dataflash_info[i].logical_address) {
339 addr_valid = 1;
340 break;
341 }
342 }
343 if (!addr_valid) {
344 return -1;
345 }
346 /* find start area */
347 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
348 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
349 break;
350 }
351 if (area1 == NB_DATAFLASH_AREA) return -1;
352 /* find end area */
353 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
354 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
355 break;
356 }
357 if (area2 == NB_DATAFLASH_AREA)
358 return -1;
359
360 /*set protection value*/
361 for(j = area1; j < area2 + 1 ; j++)
362 if(dataflash_info[i].Device.area_list[j].protected
363 != FLAG_PROTECT_INVALID) {
364 if (flag == 0) {
365 dataflash_info[i].Device.area_list[j].protected
366 = FLAG_PROTECT_CLEAR;
367 } else {
368 dataflash_info[i].Device.area_list[j].protected
369 = FLAG_PROTECT_SET;
370 }
371 }
372
373 return (area2 - area1 + 1);
374 }
375
376 /*---------------------------------------------------------------------------*/
377 /* Function Name : read_dataflash */
378 /* Object : dataflash memory read */
379 /*---------------------------------------------------------------------------*/
380 int read_dataflash (unsigned long addr, unsigned long size, char *result)
381 {
382 unsigned long AddrToRead = addr;
383 AT91PS_DataFlash pFlash = &DataFlashInst;
384
385 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
386
387 if (pFlash == 0)
388 return ERR_UNKNOWN_FLASH_TYPE;
389
390 if (size_dataflash(pFlash,addr,size) == 0)
391 return ERR_INVAL;
392
393 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
394 }
395
396 /*---------------------------------------------------------------------------*/
397 /* Function Name : write_dataflash */
398 /* Object : write a block in dataflash */
399 /*---------------------------------------------------------------------------*/
400 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
401 unsigned long size)
402 {
403 unsigned long AddrToWrite = addr_dest;
404 AT91PS_DataFlash pFlash = &DataFlashInst;
405
406 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
407
408 if (pFlash == 0)
409 return ERR_UNKNOWN_FLASH_TYPE;
410
411 if (size_dataflash(pFlash,addr_dest,size) == 0)
412 return ERR_INVAL;
413
414 if (prot_dataflash(pFlash,addr_dest) == 0)
415 return ERR_PROTECTED;
416
417 if (AddrToWrite == -1)
418 return -1;
419
420 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
421 AddrToWrite, size);
422 }
423
424 void dataflash_perror (int err)
425 {
426 switch (err) {
427 case ERR_OK:
428 break;
429 case ERR_TIMOUT:
430 printf("Timeout writing to DataFlash\n");
431 break;
432 case ERR_PROTECTED:
433 printf("Can't write to protected/invalid DataFlash sectors\n");
434 break;
435 case ERR_INVAL:
436 printf("Outside available DataFlash\n");
437 break;
438 case ERR_UNKNOWN_FLASH_TYPE:
439 printf("Unknown Type of DataFlash\n");
440 break;
441 case ERR_PROG_ERROR:
442 printf("General DataFlash Programming Error\n");
443 break;
444 default:
445 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
446 break;
447 }
448 }