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