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