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