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> |
| 22 | #ifdef CONFIG_HAS_DATAFLASH |
| 23 | #include <asm/hardware.h> |
| 24 | #include <dataflash.h> |
| 25 | |
| 26 | AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS]; |
| 27 | static AT91S_DataFlash DataFlashInst; |
| 28 | |
| 29 | int cs[][CFG_MAX_DATAFLASH_BANKS] = { |
| 30 | {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */ |
| 31 | {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3} |
| 32 | }; |
| 33 | |
| 34 | extern void AT91F_SpiInit (void); |
| 35 | extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc); |
| 36 | extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash, |
| 37 | unsigned long addr, |
| 38 | unsigned long size, char *buffer); |
| 39 | |
| 40 | |
| 41 | int AT91F_DataflashInit (void) |
| 42 | { |
| 43 | int i, j; |
| 44 | int dfcode; |
| 45 | |
| 46 | AT91F_SpiInit (); |
| 47 | |
| 48 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 49 | |
| 50 | dataflash_info[i].id = 0; |
| 51 | dataflash_info[i].Device.pages_number = 0; |
| 52 | dfcode = AT91F_DataflashProbe (cs[i][1], &dataflash_info[i].Desc); |
| 53 | |
| 54 | switch (dfcode) { |
| 55 | case AT45DB161: |
| 56 | dataflash_info[i].Device.pages_number = 4096; |
| 57 | dataflash_info[i].Device.pages_size = 528; |
| 58 | dataflash_info[i].Device.page_offset = 10; |
| 59 | dataflash_info[i].Device.byte_mask = 0x300; |
| 60 | dataflash_info[i].Device.cs = cs[i][1]; |
| 61 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 62 | dataflash_info[i].logical_address = cs[i][0]; |
| 63 | dataflash_info[i].id = dfcode; |
| 64 | break; |
| 65 | |
| 66 | case AT45DB321: |
| 67 | dataflash_info[i].Device.pages_number = 8192; |
| 68 | dataflash_info[i].Device.pages_size = 528; |
| 69 | dataflash_info[i].Device.page_offset = 10; |
| 70 | dataflash_info[i].Device.byte_mask = 0x300; |
| 71 | dataflash_info[i].Device.cs = cs[i][1]; |
| 72 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 73 | dataflash_info[i].logical_address = cs[i][0]; |
| 74 | dataflash_info[i].id = dfcode; |
| 75 | break; |
| 76 | |
| 77 | case AT45DB642: |
| 78 | dataflash_info[i].Device.pages_number = 8192; |
| 79 | dataflash_info[i].Device.pages_size = 1056; |
| 80 | dataflash_info[i].Device.page_offset = 11; |
| 81 | dataflash_info[i].Device.byte_mask = 0x700; |
| 82 | dataflash_info[i].Device.cs = cs[i][1]; |
| 83 | dataflash_info[i].Desc.DataFlash_state = IDLE; |
| 84 | dataflash_info[i].logical_address = cs[i][0]; |
| 85 | dataflash_info[i].id = dfcode; |
| 86 | break; |
| 87 | |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | for (j = 0; j < dataflash_info[i].Device.pages_number; j++) |
| 93 | dataflash_info[i].protect[j] = FLAG_PROTECT_SET; |
| 94 | |
| 95 | } |
| 96 | return (1); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | void dataflash_print_info (void) |
| 102 | { |
| 103 | int i; |
| 104 | |
| 105 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 106 | if (dataflash_info[i].id != 0) { |
| 107 | printf ("DataFlash:"); |
| 108 | switch (dataflash_info[i].id) { |
| 109 | case AT45DB161: |
| 110 | printf ("AT45DB161\n"); |
| 111 | break; |
| 112 | |
| 113 | case AT45DB321: |
| 114 | printf ("AT45DB321\n"); |
| 115 | break; |
| 116 | |
| 117 | case AT45DB642: |
| 118 | printf ("AT45DB642\n"); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | printf ("Nb pages: %6d\n" |
| 123 | "Page Size: %6d\n" |
| 124 | "Size=%8d bytes\n" |
| 125 | "Logical address: 0x%08X\n", |
| 126 | (unsigned int) dataflash_info[i].Device.pages_number, |
| 127 | (unsigned int) dataflash_info[i].Device.pages_size, |
| 128 | (unsigned int) dataflash_info[i].Device.pages_number * |
| 129 | dataflash_info[i].Device.pages_size, |
| 130 | (unsigned int) dataflash_info[i].logical_address); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /*------------------------------------------------------------------------------*/ |
| 137 | /* Function Name : AT91F_DataflashSelect */ |
| 138 | /* Object : Select the correct device */ |
| 139 | /*------------------------------------------------------------------------------*/ |
| 140 | AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash, |
| 141 | unsigned int *addr) |
| 142 | { |
| 143 | char addr_valid = 0; |
| 144 | int i; |
| 145 | |
| 146 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) |
| 147 | if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) { |
| 148 | addr_valid = 1; |
| 149 | break; |
| 150 | } |
| 151 | if (!addr_valid) { |
| 152 | pFlash = (AT91PS_DataFlash) 0; |
| 153 | return pFlash; |
| 154 | } |
| 155 | pFlash->pDataFlashDesc = &(dataflash_info[i].Desc); |
| 156 | pFlash->pDevice = &(dataflash_info[i].Device); |
| 157 | *addr -= dataflash_info[i].logical_address; |
| 158 | return (pFlash); |
| 159 | } |
| 160 | |
| 161 | /*------------------------------------------------------------------------------*/ |
| 162 | /* Function Name : addr_dataflash */ |
| 163 | /* Object : Test if address is valid */ |
| 164 | /*------------------------------------------------------------------------------*/ |
| 165 | int addr_dataflash (unsigned long addr) |
| 166 | { |
| 167 | int addr_valid = 0; |
| 168 | int i; |
| 169 | |
| 170 | for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) { |
| 171 | if ((((int) addr) & 0xFF000000) == |
| 172 | dataflash_info[i].logical_address) { |
| 173 | addr_valid = 1; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return addr_valid; |
| 179 | } |
| 180 | |
| 181 | /*------------------------------------------------------------------------------*/ |
| 182 | /* Function Name : read_dataflash */ |
| 183 | /* Object : dataflash memory read */ |
| 184 | /*------------------------------------------------------------------------------*/ |
| 185 | int read_dataflash (unsigned long addr, unsigned long size, char *result) |
| 186 | { |
| 187 | int AddrToRead = addr; |
| 188 | AT91PS_DataFlash pFlash = &DataFlashInst; |
| 189 | |
| 190 | pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead); |
| 191 | if (pFlash == 0) |
| 192 | return -1; |
| 193 | |
| 194 | return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result)); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /*-----------------------------------------------------------------------------*/ |
| 199 | /* Function Name : write_dataflash */ |
| 200 | /* Object : write a block in dataflash */ |
| 201 | /*-----------------------------------------------------------------------------*/ |
| 202 | int write_dataflash (unsigned long addr_dest, unsigned long addr_src, |
| 203 | unsigned long size) |
| 204 | { |
| 205 | extern AT91S_DataFlashStatus AT91F_DataFlashWrite( |
| 206 | AT91PS_DataFlash, uchar *, int, int); |
| 207 | int AddrToWrite = addr_dest; |
| 208 | AT91PS_DataFlash pFlash = &DataFlashInst; |
| 209 | |
| 210 | pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite); |
| 211 | if (AddrToWrite == -1) |
| 212 | return -1; |
| 213 | |
| 214 | return AT91F_DataFlashWrite (pFlash, (char *) addr_src, AddrToWrite, |
| 215 | size); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void dataflash_perror (int err) |
| 220 | { |
| 221 | switch (err) { |
| 222 | case ERR_OK: |
| 223 | break; |
| 224 | case ERR_TIMOUT: |
| 225 | printf ("Timeout writing to DataFlash\n"); |
| 226 | break; |
| 227 | case ERR_PROTECTED: |
| 228 | printf ("Can't write to protected DataFlash sectors\n"); |
| 229 | break; |
| 230 | case ERR_INVAL: |
| 231 | printf ("Outside available DataFlash\n"); |
| 232 | break; |
| 233 | case ERR_UNKNOWN_FLASH_TYPE: |
| 234 | printf ("Unknown Type of DataFlash\n"); |
| 235 | break; |
| 236 | case ERR_PROG_ERROR: |
| 237 | printf ("General DataFlash Programming Error\n"); |
| 238 | break; |
| 239 | default: |
| 240 | printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | #endif |