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