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