Merge branch 'support_for_dragino2'
[oweals/u-boot_mod.git] / u-boot / common / flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.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 /* #define DEBUG */
25
26 #include <common.h>
27 #include <flash.h>
28
29 #if !defined(CFG_NO_FLASH)
30
31 extern flash_info_t flash_info[]; /* info for FLASH chips */
32
33 /*-----------------------------------------------------------------------
34  * Functions
35  */
36 flash_info_t * addr2info(ulong addr){
37         flash_info_t *info;
38         int i;
39
40         for(i = 0, info = &flash_info[0]; i < CFG_MAX_FLASH_BANKS; ++i, ++info){
41                 /* WARNING - The '- 1' is needed if the flash
42                  * is at the end of the address space, since
43                  * info->start[0] + info->size wraps back to 0.
44                  * Please don't change this unless you understand this.
45                  */
46                 if(info->flash_id != FLASH_UNKNOWN && addr >= info->start[0] && addr <= info->start[0] + info->size - 1){
47                         return(info);
48                 }
49         }
50         return(NULL);
51 }
52
53 /*-----------------------------------------------------------------------
54  * Copy memory to flash.
55  * Make sure all target addresses are within Flash bounds,
56  * and no protected sectors are hit.
57  * Returns:
58  * ERR_OK          0 - OK
59  * ERR_TIMOUT      1 - write timeout
60  * ERR_NOT_ERASED  2 - Flash not erased
61  * ERR_PROTECTED   4 - target range includes protected sectors
62  * ERR_INVAL       8 - target address not in Flash memory
63  * ERR_ALIGN       16 - target address not aligned on boundary
64  *                      (only some targets require alignment)
65  */
66 int flash_write(char *src, ulong addr, ulong cnt){
67         int i;
68         ulong end = addr + cnt - 1;
69         flash_info_t *info_first = addr2info(addr);
70         flash_info_t *info_last = addr2info(end);
71         flash_info_t *info;
72
73         if(cnt == 0){
74                 return(ERR_OK);
75         }
76
77         if(!info_first || !info_last){
78                 return(ERR_INVAL);
79         }
80
81         for(info = info_first; info <= info_last; ++info){
82                 ulong b_end = info->start[0] + info->size; /* bank end addr */
83                 short s_end = info->sector_count - 1;
84                 for(i = 0; i < info->sector_count; ++i){
85                         ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
86
87                         if((end >= info->start[i]) && (addr < e_addr) && (info->protect[i] != 0)){
88                                 return(ERR_PROTECTED);
89                         }
90                 }
91         }
92
93         /* finally write data to flash */
94         for(info = info_first; info <= info_last && cnt > 0; ++info){
95                 ulong len;
96
97                 len = info->start[0] + info->size - addr;
98                 if(len > cnt){
99                         len = cnt;
100                 }
101                 if((i = write_buff(info, (uchar *)src, addr, len)) != 0){
102                         return(i);
103                 }
104                 cnt -= len;
105                 addr += len;
106                 src += len;
107         }
108         return(ERR_OK);
109 }
110
111 /*-----------------------------------------------------------------------
112  */
113
114 void flash_perror(int err){
115         switch(err){
116                 case ERR_OK:
117                         break;
118                 case ERR_TIMOUT:
119                         puts("## Error: timeout writing to flash\n");
120                         break;
121                 case ERR_NOT_ERASED:
122                         puts("## Error: flash not erased\n");
123                         break;
124                 case ERR_PROTECTED:
125                         puts("## Error: can't write to protected sectors\n");
126                         break;
127                 case ERR_INVAL:
128                         puts("## Error: outside available flash\n");
129                         break;
130                 case ERR_ALIGN:
131                         puts("## Error: start and/or end address not on sector boundary\n");
132                         break;
133                 case ERR_UNKNOWN_FLASH_VENDOR:
134                         puts("## Error: unknown vendor of flash\n");
135                         break;
136                 case ERR_UNKNOWN_FLASH_TYPE:
137                         puts("## Error: unknown type of flash\n");
138                         break;
139                 case ERR_PROG_ERROR:
140                         puts("## Error: general flash programming error\n");
141                         break;
142                 default:
143                         printf("## Error: %s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
144                         break;
145         }
146 }
147
148 /*-----------------------------------------------------------------------
149  */
150 #endif /* !CFG_NO_FLASH */