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 | |
| 29 | int cs[][CFG_MAX_DATAFLASH_BANKS] = { |
| 30 | {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */ |
| 31 | {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3} |
| 32 | }; |
| 33 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 34 | /*define the area offsets*/ |
| 35 | dataflash_protect_t area_list[NB_DATAFLASH_AREA] = { |
| 36 | {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */ |
| 37 | {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */ |
| 38 | {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */ |
| 39 | {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */ |
| 40 | }; |
| 41 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 42 | extern void AT91F_SpiInit (void); |
| 43 | extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc); |
| 44 | extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash, |
| 45 | unsigned long addr, |
| 46 | unsigned long size, char *buffer); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 47 | extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash, |
| 48 | unsigned char *src, |
| 49 | int dest, |
| 50 | int size ); |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 51 | |
| 52 | int AT91F_DataflashInit (void) |
| 53 | { |
| 54 | int i, j; |
| 55 | int dfcode; |
| 56 | |
| 57 | AT91F_SpiInit (); |
| 58 | |
| 59 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 60 | dataflash_info[i].Desc.state = IDLE; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 61 | dataflash_info[i].id = 0; |
| 62 | dataflash_info[i].Device.pages_number = 0; |
| 63 | dfcode = AT91F_DataflashProbe (cs[i][1], &dataflash_info[i].Desc); |
| 64 | |
| 65 | switch (dfcode) { |
| 66 | case AT45DB161: |
| 67 | dataflash_info[i].Device.pages_number = 4096; |
| 68 | dataflash_info[i].Device.pages_size = 528; |
| 69 | dataflash_info[i].Device.page_offset = 10; |
| 70 | dataflash_info[i].Device.byte_mask = 0x300; |
| 71 | dataflash_info[i].Device.cs = cs[i][1]; |
| 72 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 73 | dataflash_info[i].logical_address = cs[i][0]; |
| 74 | dataflash_info[i].id = dfcode; |
| 75 | break; |
| 76 | |
| 77 | case AT45DB321: |
| 78 | dataflash_info[i].Device.pages_number = 8192; |
| 79 | dataflash_info[i].Device.pages_size = 528; |
| 80 | dataflash_info[i].Device.page_offset = 10; |
| 81 | dataflash_info[i].Device.byte_mask = 0x300; |
| 82 | dataflash_info[i].Device.cs = cs[i][1]; |
| 83 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 84 | dataflash_info[i].logical_address = cs[i][0]; |
| 85 | dataflash_info[i].id = dfcode; |
| 86 | break; |
| 87 | |
| 88 | case AT45DB642: |
| 89 | dataflash_info[i].Device.pages_number = 8192; |
| 90 | dataflash_info[i].Device.pages_size = 1056; |
| 91 | dataflash_info[i].Device.page_offset = 11; |
| 92 | dataflash_info[i].Device.byte_mask = 0x700; |
| 93 | dataflash_info[i].Device.cs = cs[i][1]; |
| 94 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 95 | dataflash_info[i].logical_address = cs[i][0]; |
| 96 | dataflash_info[i].id = dfcode; |
| 97 | break; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 98 | case AT45DB128: |
| 99 | dataflash_info[i].Device.pages_number = 16384; |
| 100 | dataflash_info[i].Device.pages_size = 1056; |
| 101 | dataflash_info[i].Device.page_offset = 11; |
| 102 | dataflash_info[i].Device.byte_mask = 0x700; |
| 103 | dataflash_info[i].Device.cs = cs[i][1]; |
| 104 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 105 | dataflash_info[i].logical_address = cs[i][0]; |
| 106 | dataflash_info[i].id = dfcode; |
| 107 | break; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 108 | |
| 109 | default: |
| 110 | break; |
| 111 | } |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 112 | /* set the last area end to the dataflash size*/ |
| 113 | area_list[NB_DATAFLASH_AREA -1].end = |
| 114 | (dataflash_info[i].Device.pages_number * |
| 115 | dataflash_info[i].Device.pages_size)-1; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 116 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 117 | /* set the area addresses */ |
| 118 | for(j = 0; j<NB_DATAFLASH_AREA; j++) { |
| 119 | dataflash_info[i].Device.area_list[j].start = area_list[j].start + dataflash_info[i].logical_address; |
| 120 | dataflash_info[i].Device.area_list[j].end = area_list[j].end + dataflash_info[i].logical_address; |
| 121 | dataflash_info[i].Device.area_list[j].protected = area_list[j].protected; |
| 122 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 123 | } |
| 124 | return (1); |
| 125 | } |
| 126 | |
| 127 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 128 | void dataflash_print_info (void) |
| 129 | { |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 130 | int i, j; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 131 | |
| 132 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 133 | if (dataflash_info[i].id != 0) { |
| 134 | printf ("DataFlash:"); |
| 135 | switch (dataflash_info[i].id) { |
| 136 | case AT45DB161: |
| 137 | printf ("AT45DB161\n"); |
| 138 | break; |
| 139 | |
| 140 | case AT45DB321: |
| 141 | printf ("AT45DB321\n"); |
| 142 | break; |
| 143 | |
| 144 | case AT45DB642: |
| 145 | printf ("AT45DB642\n"); |
| 146 | break; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 147 | case AT45DB128: |
| 148 | printf ("AT45DB128\n"); |
| 149 | break; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | printf ("Nb pages: %6d\n" |
| 153 | "Page Size: %6d\n" |
| 154 | "Size=%8d bytes\n" |
| 155 | "Logical address: 0x%08X\n", |
| 156 | (unsigned int) dataflash_info[i].Device.pages_number, |
| 157 | (unsigned int) dataflash_info[i].Device.pages_size, |
| 158 | (unsigned int) dataflash_info[i].Device.pages_number * |
| 159 | dataflash_info[i].Device.pages_size, |
| 160 | (unsigned int) dataflash_info[i].logical_address); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 161 | for (j=0; j< NB_DATAFLASH_AREA; j++) { |
| 162 | printf ("Area %i:\t%08X to %08X %s\n", j, |
| 163 | dataflash_info[i].Device.area_list[j].start, |
| 164 | dataflash_info[i].Device.area_list[j].end, |
| 165 | (dataflash_info[i].Device.area_list[j].protected == |
| 166 | FLAG_PROTECT_SET) ? "(RO)" : ""); |
| 167 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /*------------------------------------------------------------------------------*/ |
| 174 | /* Function Name : AT91F_DataflashSelect */ |
| 175 | /* Object : Select the correct device */ |
| 176 | /*------------------------------------------------------------------------------*/ |
| 177 | AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash, |
| 178 | unsigned int *addr) |
| 179 | { |
| 180 | char addr_valid = 0; |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) |
| 184 | if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) { |
| 185 | addr_valid = 1; |
| 186 | break; |
| 187 | } |
| 188 | if (!addr_valid) { |
| 189 | pFlash = (AT91PS_DataFlash) 0; |
| 190 | return pFlash; |
| 191 | } |
| 192 | pFlash->pDataFlashDesc = &(dataflash_info[i].Desc); |
| 193 | pFlash->pDevice = &(dataflash_info[i].Device); |
| 194 | *addr -= dataflash_info[i].logical_address; |
| 195 | return (pFlash); |
| 196 | } |
| 197 | |
| 198 | /*------------------------------------------------------------------------------*/ |
| 199 | /* Function Name : addr_dataflash */ |
| 200 | /* Object : Test if address is valid */ |
| 201 | /*------------------------------------------------------------------------------*/ |
| 202 | int addr_dataflash (unsigned long addr) |
| 203 | { |
| 204 | int addr_valid = 0; |
| 205 | int i; |
| 206 | |
| 207 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 208 | if ((((int) addr) & 0xFF000000) == |
| 209 | dataflash_info[i].logical_address) { |
| 210 | addr_valid = 1; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return addr_valid; |
| 216 | } |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 217 | /*-----------------------------------------------------------------------------*/ |
| 218 | /* Function Name : size_dataflash */ |
| 219 | /* Object : Test if address is valid regarding the size */ |
| 220 | /*-----------------------------------------------------------------------------*/ |
| 221 | int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr, unsigned long size) |
| 222 | { |
| 223 | /* is outside the dataflash */ |
| 224 | if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size * |
| 225 | pdataFlash->pDevice->pages_number)) return 0; |
| 226 | /* is too large for the dataflash */ |
| 227 | if (size > ((pdataFlash->pDevice->pages_size * |
| 228 | pdataFlash->pDevice->pages_number) - ((int)addr & 0x0FFFFFFF))) return 0; |
| 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | /*-----------------------------------------------------------------------------*/ |
| 233 | /* Function Name : prot_dataflash */ |
| 234 | /* Object : Test if destination area is protected */ |
| 235 | /*-----------------------------------------------------------------------------*/ |
| 236 | int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr) |
| 237 | { |
| 238 | int area; |
| 239 | /* find area */ |
| 240 | for (area=0; area < NB_DATAFLASH_AREA; area++) { |
| 241 | if ((addr >= pdataFlash->pDevice->area_list[area].start) && |
| 242 | (addr < pdataFlash->pDevice->area_list[area].end)) |
| 243 | break; |
| 244 | } |
| 245 | if (area == NB_DATAFLASH_AREA) return -1; |
| 246 | /*test protection value*/ |
| 247 | if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET) return 0; |
| 248 | |
| 249 | return 1; |
| 250 | } |
| 251 | /*-----------------------------------------------------------------------------*/ |
| 252 | /* Function Name : dataflash_real_protect */ |
| 253 | /* Object : protect/unprotect area */ |
| 254 | /*-----------------------------------------------------------------------------*/ |
| 255 | int dataflash_real_protect (int flag, unsigned long start_addr, unsigned long end_addr) |
| 256 | { |
| 257 | int i,j, area1, area2, addr_valid = 0; |
| 258 | /* find dataflash */ |
| 259 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 260 | if ((((int) start_addr) & 0xF0000000) == |
| 261 | dataflash_info[i].logical_address) { |
| 262 | addr_valid = 1; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | if (!addr_valid) { |
| 267 | return -1; |
| 268 | } |
| 269 | /* find start area */ |
| 270 | for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) { |
| 271 | if (start_addr == dataflash_info[i].Device.area_list[area1].start) break; |
| 272 | } |
| 273 | if (area1 == NB_DATAFLASH_AREA) return -1; |
| 274 | /* find end area */ |
| 275 | for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) { |
| 276 | if (end_addr == dataflash_info[i].Device.area_list[area2].end) break; |
| 277 | } |
| 278 | if (area2 == NB_DATAFLASH_AREA) return -1; |
| 279 | |
| 280 | /*set protection value*/ |
| 281 | for(j = area1; j < area2+1 ; j++) |
| 282 | if (flag == 0) dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_CLEAR; |
| 283 | else dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_SET; |
| 284 | |
| 285 | return (area2-area1+1); |
| 286 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 287 | |
| 288 | /*------------------------------------------------------------------------------*/ |
| 289 | /* Function Name : read_dataflash */ |
| 290 | /* Object : dataflash memory read */ |
| 291 | /*------------------------------------------------------------------------------*/ |
| 292 | int read_dataflash (unsigned long addr, unsigned long size, char *result) |
| 293 | { |
| 294 | int AddrToRead = addr; |
| 295 | AT91PS_DataFlash pFlash = &DataFlashInst; |
| 296 | |
| 297 | pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 298 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 299 | if (pFlash == 0) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 300 | return ERR_UNKNOWN_FLASH_TYPE; |
| 301 | |
| 302 | if (size_dataflash(pFlash,addr,size) == 0) |
| 303 | return ERR_INVAL; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 304 | |
| 305 | return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result)); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /*-----------------------------------------------------------------------------*/ |
| 310 | /* Function Name : write_dataflash */ |
| 311 | /* Object : write a block in dataflash */ |
| 312 | /*-----------------------------------------------------------------------------*/ |
| 313 | int write_dataflash (unsigned long addr_dest, unsigned long addr_src, |
| 314 | unsigned long size) |
| 315 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 316 | int AddrToWrite = addr_dest; |
| 317 | AT91PS_DataFlash pFlash = &DataFlashInst; |
| 318 | |
| 319 | pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 320 | |
| 321 | if (pFlash == 0) |
| 322 | return ERR_UNKNOWN_FLASH_TYPE; |
| 323 | |
| 324 | if (size_dataflash(pFlash,addr_dest,size) == 0) |
| 325 | return ERR_INVAL; |
| 326 | |
| 327 | if (prot_dataflash(pFlash,addr_dest) == 0) |
| 328 | return ERR_PROTECTED; |
| 329 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 330 | if (AddrToWrite == -1) |
| 331 | return -1; |
| 332 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame^] | 333 | return AT91F_DataFlashWrite (pFlash, (char *) addr_src, AddrToWrite, size); |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | void dataflash_perror (int err) |
| 338 | { |
| 339 | switch (err) { |
| 340 | case ERR_OK: |
| 341 | break; |
| 342 | case ERR_TIMOUT: |
| 343 | printf ("Timeout writing to DataFlash\n"); |
| 344 | break; |
| 345 | case ERR_PROTECTED: |
| 346 | printf ("Can't write to protected DataFlash sectors\n"); |
| 347 | break; |
| 348 | case ERR_INVAL: |
| 349 | printf ("Outside available DataFlash\n"); |
| 350 | break; |
| 351 | case ERR_UNKNOWN_FLASH_TYPE: |
| 352 | printf ("Unknown Type of DataFlash\n"); |
| 353 | break; |
| 354 | case ERR_PROG_ERROR: |
| 355 | printf ("General DataFlash Programming Error\n"); |
| 356 | break; |
| 357 | default: |
| 358 | printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | #endif |