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