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