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