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