blob: be2274362c11cc8bcc350a3ebf97d2387c08ec9f [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
2 * (C) Copyright 2002
3 * Lineo, Inc. <www.lineo.com>
4 * Bernhard Kuhn <bkuhn@lineo.com>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30
31ulong myflush(void);
32
33
wdenk2abbe072003-06-16 23:50:08 +000034/* Flash Organization Structure */
35typedef struct OrgDef
36{
37 unsigned int sector_number;
38 unsigned int sector_size;
39} OrgDef;
40
41
42/* Flash Organizations */
43OrgDef OrgAT49BV16x4[] =
44{
wdenk8b07a112004-07-10 21:45:47 +000045 { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
46 { 2, 32*1024 }, /* 2 * 32 kBytes sectors */
47 { 30, 64*1024 }, /* 30 * 64 kBytes sectors */
wdenk2abbe072003-06-16 23:50:08 +000048};
49
50OrgDef OrgAT49BV16x4A[] =
51{
wdenk8b07a112004-07-10 21:45:47 +000052 { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
53 { 31, 64*1024 }, /* 31 * 64 kBytes sectors */
wdenk2abbe072003-06-16 23:50:08 +000054};
55
wdenk8b07a112004-07-10 21:45:47 +000056OrgDef OrgAT49BV6416[] =
57{
58 { 8, 8*1024 }, /* 8 * 8 kBytes sectors */
59 { 127, 64*1024 }, /* 127 * 64 kBytes sectors */
60};
wdenkdc7c9a12003-03-26 06:55:25 +000061
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkdc7c9a12003-03-26 06:55:25 +000063
wdenk2abbe072003-06-16 23:50:08 +000064/* AT49BV1614A Codes */
65#define FLASH_CODE1 0xAA
66#define FLASH_CODE2 0x55
67#define ID_IN_CODE 0x90
68#define ID_OUT_CODE 0xF0
69
wdenkdc7c9a12003-03-26 06:55:25 +000070
71#define CMD_READ_ARRAY 0x00F0
72#define CMD_UNLOCK1 0x00AA
73#define CMD_UNLOCK2 0x0055
74#define CMD_ERASE_SETUP 0x0080
75#define CMD_ERASE_CONFIRM 0x0030
76#define CMD_PROGRAM 0x00A0
77#define CMD_UNLOCK_BYPASS 0x0020
wdenk8b07a112004-07-10 21:45:47 +000078#define CMD_SECTOR_UNLOCK 0x0070
wdenkdc7c9a12003-03-26 06:55:25 +000079
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080#define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00005555<<1)))
81#define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00002AAA<<1)))
wdenkdc7c9a12003-03-26 06:55:25 +000082
83#define BIT_ERASE_DONE 0x0080
84#define BIT_RDY_MASK 0x0080
85#define BIT_PROGRAM_ERROR 0x0020
86#define BIT_TIMEOUT 0x80000000 /* our flag */
87
88#define READY 1
89#define ERR 2
90#define TMO 4
91
92/*-----------------------------------------------------------------------
93 */
wdenk2abbe072003-06-16 23:50:08 +000094void flash_identification (flash_info_t * info)
wdenkdc7c9a12003-03-26 06:55:25 +000095{
wdenk2abbe072003-06-16 23:50:08 +000096 volatile u16 manuf_code, device_code, add_device_code;
wdenkdc7c9a12003-03-26 06:55:25 +000097
wdenk8b07a112004-07-10 21:45:47 +000098 MEM_FLASH_ADDR1 = FLASH_CODE1;
99 MEM_FLASH_ADDR2 = FLASH_CODE2;
100 MEM_FLASH_ADDR1 = ID_IN_CODE;
wdenkdc7c9a12003-03-26 06:55:25 +0000101
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102 manuf_code = *(volatile u16 *) CONFIG_SYS_FLASH_BASE;
103 device_code = *(volatile u16 *) (CONFIG_SYS_FLASH_BASE + 2);
104 add_device_code = *(volatile u16 *) (CONFIG_SYS_FLASH_BASE + (3 << 1));
wdenk2abbe072003-06-16 23:50:08 +0000105
wdenk8b07a112004-07-10 21:45:47 +0000106 MEM_FLASH_ADDR1 = FLASH_CODE1;
107 MEM_FLASH_ADDR2 = FLASH_CODE2;
108 MEM_FLASH_ADDR1 = ID_OUT_CODE;
wdenk2abbe072003-06-16 23:50:08 +0000109
110 /* Vendor type */
111 info->flash_id = ATM_MANUFACT & FLASH_VENDMASK;
112 printf ("Atmel: ");
113
114 if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
115
116 if ((add_device_code & FLASH_TYPEMASK) ==
117 (ATM_ID_BV1614A & FLASH_TYPEMASK)) {
118 info->flash_id |= ATM_ID_BV1614A & FLASH_TYPEMASK;
119 printf ("AT49BV1614A (16Mbit)\n");
wdenk8b07a112004-07-10 21:45:47 +0000120 } else { /* AT49BV1614 Flash */
121 info->flash_id |= ATM_ID_BV1614 & FLASH_TYPEMASK;
122 printf ("AT49BV1614 (16Mbit)\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000123 }
124
wdenk8b07a112004-07-10 21:45:47 +0000125 } else if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV6416 & FLASH_TYPEMASK)) {
126 info->flash_id |= ATM_ID_BV6416 & FLASH_TYPEMASK;
127 printf ("AT49BV6416 (64Mbit)\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000128 }
wdenk2abbe072003-06-16 23:50:08 +0000129}
wdenkdc7c9a12003-03-26 06:55:25 +0000130
wdenk8b07a112004-07-10 21:45:47 +0000131ushort flash_number_sector(OrgDef *pOrgDef, unsigned int nb_blocks)
132{
133 int i, nb_sectors = 0;
134
135 for (i=0; i<nb_blocks; i++){
136 nb_sectors += pOrgDef[i].sector_number;
137 }
138
139 return nb_sectors;
140}
141
142void flash_unlock_sector(flash_info_t * info, unsigned int sector)
143{
144 volatile u16 *addr = (volatile u16 *) (info->start[sector]);
145
146 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
147 *addr = CMD_SECTOR_UNLOCK;
148}
149
wdenkdc7c9a12003-03-26 06:55:25 +0000150
wdenk2abbe072003-06-16 23:50:08 +0000151ulong flash_init (void)
152{
153 int i, j, k;
154 unsigned int flash_nb_blocks, sector;
155 unsigned int start_address;
156 OrgDef *pOrgDef;
wdenkdc7c9a12003-03-26 06:55:25 +0000157
wdenk2abbe072003-06-16 23:50:08 +0000158 ulong size = 0;
159
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200160 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000161 ulong flashbase = 0;
162
163 flash_identification (&flash_info[i]);
164
wdenk2abbe072003-06-16 23:50:08 +0000165 if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
166 (ATM_ID_BV1614 & FLASH_TYPEMASK)) {
wdenk2abbe072003-06-16 23:50:08 +0000167
168 pOrgDef = OrgAT49BV16x4;
169 flash_nb_blocks = sizeof (OrgAT49BV16x4) / sizeof (OrgDef);
wdenk8b07a112004-07-10 21:45:47 +0000170 } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
171 (ATM_ID_BV1614A & FLASH_TYPEMASK)){ /* AT49BV1614A Flash */
wdenk2abbe072003-06-16 23:50:08 +0000172
173 pOrgDef = OrgAT49BV16x4A;
174 flash_nb_blocks = sizeof (OrgAT49BV16x4A) / sizeof (OrgDef);
wdenk8b07a112004-07-10 21:45:47 +0000175 } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
176 (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */
177
178 pOrgDef = OrgAT49BV6416;
179 flash_nb_blocks = sizeof (OrgAT49BV6416) / sizeof (OrgDef);
180 } else {
181 flash_nb_blocks = 0;
182 pOrgDef = OrgAT49BV16x4;
wdenk2abbe072003-06-16 23:50:08 +0000183 }
184
wdenk8b07a112004-07-10 21:45:47 +0000185 flash_info[i].sector_count = flash_number_sector(pOrgDef, flash_nb_blocks);
186 memset (flash_info[i].protect, 0, flash_info[i].sector_count);
187
wdenk2abbe072003-06-16 23:50:08 +0000188 if (i == 0)
189 flashbase = PHYS_FLASH_1;
190 else
wdenk5f535fe2003-09-18 09:21:33 +0000191 panic ("configured too many flash banks!\n");
wdenk2abbe072003-06-16 23:50:08 +0000192
193 sector = 0;
194 start_address = flashbase;
wdenk8b07a112004-07-10 21:45:47 +0000195 flash_info[i].size = 0;
wdenk2abbe072003-06-16 23:50:08 +0000196
197 for (j = 0; j < flash_nb_blocks; j++) {
198 for (k = 0; k < pOrgDef[j].sector_number; k++) {
199 flash_info[i].start[sector++] = start_address;
200 start_address += pOrgDef[j].sector_size;
wdenk8b07a112004-07-10 21:45:47 +0000201 flash_info[i].size += pOrgDef[j].sector_size;
wdenk2abbe072003-06-16 23:50:08 +0000202 }
203 }
204
205 size += flash_info[i].size;
wdenk8b07a112004-07-10 21:45:47 +0000206
207 if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
208 (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */
209
210 /* Unlock all sectors at reset */
211 for (j=0; j<flash_info[i].sector_count; j++){
212 flash_unlock_sector(&flash_info[i], j);
213 }
214 }
wdenk2abbe072003-06-16 23:50:08 +0000215 }
216
217 /* Protect binary boot image */
218 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200219 CONFIG_SYS_FLASH_BASE,
220 CONFIG_SYS_FLASH_BASE + CONFIG_SYS_BOOT_SIZE - 1, &flash_info[0]);
wdenk2abbe072003-06-16 23:50:08 +0000221
222 /* Protect environment variables */
223 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200224 CONFIG_ENV_ADDR,
225 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenk2abbe072003-06-16 23:50:08 +0000226
227 /* Protect U-Boot gzipped image */
228 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200229 CONFIG_SYS_U_BOOT_BASE,
230 CONFIG_SYS_U_BOOT_BASE + CONFIG_SYS_U_BOOT_SIZE - 1, &flash_info[0]);
wdenk2abbe072003-06-16 23:50:08 +0000231
232 return size;
wdenkdc7c9a12003-03-26 06:55:25 +0000233}
234
235/*-----------------------------------------------------------------------
236 */
wdenk2abbe072003-06-16 23:50:08 +0000237void flash_print_info (flash_info_t * info)
wdenkdc7c9a12003-03-26 06:55:25 +0000238{
wdenk2abbe072003-06-16 23:50:08 +0000239 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000240
wdenk2abbe072003-06-16 23:50:08 +0000241 switch (info->flash_id & FLASH_VENDMASK) {
242 case (ATM_MANUFACT & FLASH_VENDMASK):
243 printf ("Atmel: ");
244 break;
245 default:
246 printf ("Unknown Vendor ");
247 break;
wdenkdc7c9a12003-03-26 06:55:25 +0000248 }
wdenkdc7c9a12003-03-26 06:55:25 +0000249
wdenk2abbe072003-06-16 23:50:08 +0000250 switch (info->flash_id & FLASH_TYPEMASK) {
251 case (ATM_ID_BV1614 & FLASH_TYPEMASK):
252 printf ("AT49BV1614 (16Mbit)\n");
253 break;
254 case (ATM_ID_BV1614A & FLASH_TYPEMASK):
255 printf ("AT49BV1614A (16Mbit)\n");
256 break;
wdenk8b07a112004-07-10 21:45:47 +0000257 case (ATM_ID_BV6416 & FLASH_TYPEMASK):
258 printf ("AT49BV6416 (64Mbit)\n");
259 break;
wdenk2abbe072003-06-16 23:50:08 +0000260 default:
261 printf ("Unknown Chip Type\n");
Wolfgang Denk5ad2af32005-08-13 23:13:17 +0200262 return;
wdenk2abbe072003-06-16 23:50:08 +0000263 }
264
265 printf (" Size: %ld MB in %d Sectors\n",
266 info->size >> 20, info->sector_count);
267
268 printf (" Sector Start Addresses:");
269 for (i = 0; i < info->sector_count; i++) {
270 if ((i % 5) == 0) {
271 printf ("\n ");
272 }
273 printf (" %08lX%s", info->start[i],
274 info->protect[i] ? " (RO)" : " ");
275 }
276 printf ("\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000277}
278
279/*-----------------------------------------------------------------------
280 */
281
wdenk2abbe072003-06-16 23:50:08 +0000282int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenkdc7c9a12003-03-26 06:55:25 +0000283{
wdenk2abbe072003-06-16 23:50:08 +0000284 ulong result;
285 int iflag, cflag, prot, sect;
286 int rc = ERR_OK;
287 int chip1;
Graeme Russa60d1e52011-07-15 23:31:37 +0000288 ulong start;
wdenkdc7c9a12003-03-26 06:55:25 +0000289
wdenk2abbe072003-06-16 23:50:08 +0000290 /* first look for protection bits */
wdenkdc7c9a12003-03-26 06:55:25 +0000291
wdenk2abbe072003-06-16 23:50:08 +0000292 if (info->flash_id == FLASH_UNKNOWN)
293 return ERR_UNKNOWN_FLASH_TYPE;
wdenkdc7c9a12003-03-26 06:55:25 +0000294
wdenk2abbe072003-06-16 23:50:08 +0000295 if ((s_first < 0) || (s_first > s_last)) {
296 return ERR_INVAL;
wdenkdc7c9a12003-03-26 06:55:25 +0000297 }
wdenkdc7c9a12003-03-26 06:55:25 +0000298
wdenk2abbe072003-06-16 23:50:08 +0000299 if ((info->flash_id & FLASH_VENDMASK) !=
300 (ATM_MANUFACT & FLASH_VENDMASK)) {
301 return ERR_UNKNOWN_FLASH_VENDOR;
302 }
wdenkdc7c9a12003-03-26 06:55:25 +0000303
wdenk2abbe072003-06-16 23:50:08 +0000304 prot = 0;
305 for (sect = s_first; sect <= s_last; ++sect) {
306 if (info->protect[sect]) {
307 prot++;
wdenkdc7c9a12003-03-26 06:55:25 +0000308 }
wdenkdc7c9a12003-03-26 06:55:25 +0000309 }
wdenk2abbe072003-06-16 23:50:08 +0000310 if (prot)
311 return ERR_PROTECTED;
wdenkdc7c9a12003-03-26 06:55:25 +0000312
wdenk2abbe072003-06-16 23:50:08 +0000313 /*
314 * Disable interrupts which might cause a timeout
315 * here. Remember that our exception vectors are
316 * at address 0 in the flash, and we don't want a
317 * (ticker) exception to happen while the flash
318 * chip is in programming mode.
319 */
320 cflag = icache_status ();
321 icache_disable ();
322 iflag = disable_interrupts ();
323
324 /* Start erase on unprotected sectors */
325 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
326 printf ("Erasing sector %2d ... ", sect);
327
328 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000329 start = get_timer(0);
wdenk2abbe072003-06-16 23:50:08 +0000330
331 if (info->protect[sect] == 0) { /* not protected */
332 volatile u16 *addr = (volatile u16 *) (info->start[sect]);
333
334 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
335 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
336 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
337
338 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
339 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
340 *addr = CMD_ERASE_CONFIRM;
341
342 /* wait until flash is ready */
343 chip1 = 0;
344
345 do {
346 result = *addr;
347
348 /* check timeout */
Graeme Russa60d1e52011-07-15 23:31:37 +0000349 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk2abbe072003-06-16 23:50:08 +0000350 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
351 chip1 = TMO;
352 break;
353 }
354
355 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
356 chip1 = READY;
357
358 } while (!chip1);
359
360 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
361
362 if (chip1 == ERR) {
363 rc = ERR_PROG_ERROR;
364 goto outahere;
365 }
366 if (chip1 == TMO) {
367 rc = ERR_TIMOUT;
368 goto outahere;
369 }
370
371 printf ("ok.\n");
372 } else { /* it was protected */
373 printf ("protected!\n");
374 }
375 }
376
377 if (ctrlc ())
378 printf ("User Interrupt!\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000379
380outahere:
wdenk2abbe072003-06-16 23:50:08 +0000381 /* allow flash to settle - wait 10 ms */
382 udelay_masked (10000);
wdenkdc7c9a12003-03-26 06:55:25 +0000383
wdenk2abbe072003-06-16 23:50:08 +0000384 if (iflag)
385 enable_interrupts ();
wdenkdc7c9a12003-03-26 06:55:25 +0000386
wdenk2abbe072003-06-16 23:50:08 +0000387 if (cflag)
388 icache_enable ();
wdenkdc7c9a12003-03-26 06:55:25 +0000389
wdenk2abbe072003-06-16 23:50:08 +0000390 return rc;
wdenkdc7c9a12003-03-26 06:55:25 +0000391}
392
393/*-----------------------------------------------------------------------
394 * Copy memory to flash
395 */
396
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100397static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkdc7c9a12003-03-26 06:55:25 +0000398{
wdenk2abbe072003-06-16 23:50:08 +0000399 volatile u16 *addr = (volatile u16 *) dest;
400 ulong result;
401 int rc = ERR_OK;
402 int cflag, iflag;
403 int chip1;
Graeme Russa60d1e52011-07-15 23:31:37 +0000404 ulong start;
wdenkdc7c9a12003-03-26 06:55:25 +0000405
wdenk2abbe072003-06-16 23:50:08 +0000406 /*
407 * Check if Flash is (sufficiently) erased
408 */
wdenkdc7c9a12003-03-26 06:55:25 +0000409 result = *addr;
wdenk2abbe072003-06-16 23:50:08 +0000410 if ((result & data) != data)
411 return ERR_NOT_ERASED;
wdenkdc7c9a12003-03-26 06:55:25 +0000412
wdenk2abbe072003-06-16 23:50:08 +0000413 /*
414 * Disable interrupts which might cause a timeout
415 * here. Remember that our exception vectors are
416 * at address 0 in the flash, and we don't want a
417 * (ticker) exception to happen while the flash
418 * chip is in programming mode.
419 */
420 cflag = icache_status ();
421 icache_disable ();
422 iflag = disable_interrupts ();
wdenkdc7c9a12003-03-26 06:55:25 +0000423
wdenk2abbe072003-06-16 23:50:08 +0000424 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
425 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
426 MEM_FLASH_ADDR1 = CMD_PROGRAM;
427 *addr = data;
wdenkdc7c9a12003-03-26 06:55:25 +0000428
wdenk2abbe072003-06-16 23:50:08 +0000429 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000430 start = get_timer(0);
wdenkdc7c9a12003-03-26 06:55:25 +0000431
wdenk2abbe072003-06-16 23:50:08 +0000432 /* wait until flash is ready */
433 chip1 = 0;
434 do {
435 result = *addr;
wdenkdc7c9a12003-03-26 06:55:25 +0000436
wdenk2abbe072003-06-16 23:50:08 +0000437 /* check timeout */
Graeme Russa60d1e52011-07-15 23:31:37 +0000438 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk2abbe072003-06-16 23:50:08 +0000439 chip1 = ERR | TMO;
440 break;
441 }
442 if (!chip1 && ((result & 0x80) == (data & 0x80)))
443 chip1 = READY;
wdenkdc7c9a12003-03-26 06:55:25 +0000444
wdenk2abbe072003-06-16 23:50:08 +0000445 } while (!chip1);
446
447 *addr = CMD_READ_ARRAY;
448
449 if (chip1 == ERR || *addr != data)
450 rc = ERR_PROG_ERROR;
451
452 if (iflag)
453 enable_interrupts ();
454
455 if (cflag)
456 icache_enable ();
457
458 return rc;
wdenkdc7c9a12003-03-26 06:55:25 +0000459}
460
461/*-----------------------------------------------------------------------
462 * Copy memory to flash.
463 */
464
wdenk2abbe072003-06-16 23:50:08 +0000465int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenkdc7c9a12003-03-26 06:55:25 +0000466{
wdenk2abbe072003-06-16 23:50:08 +0000467 ulong wp, data;
468 int rc;
wdenkdc7c9a12003-03-26 06:55:25 +0000469
wdenk2abbe072003-06-16 23:50:08 +0000470 if (addr & 1) {
471 printf ("unaligned destination not supported\n");
472 return ERR_ALIGN;
473 };
wdenkdc7c9a12003-03-26 06:55:25 +0000474
wdenk2abbe072003-06-16 23:50:08 +0000475 if ((int) src & 1) {
476 printf ("unaligned source not supported\n");
477 return ERR_ALIGN;
478 };
wdenkdc7c9a12003-03-26 06:55:25 +0000479
wdenk2abbe072003-06-16 23:50:08 +0000480 wp = addr;
wdenkdc7c9a12003-03-26 06:55:25 +0000481
wdenk2abbe072003-06-16 23:50:08 +0000482 while (cnt >= 2) {
483 data = *((volatile u16 *) src);
484 if ((rc = write_word (info, wp, data)) != 0) {
485 return (rc);
486 }
487 src += 2;
488 wp += 2;
489 cnt -= 2;
wdenkdc7c9a12003-03-26 06:55:25 +0000490 }
wdenkdc7c9a12003-03-26 06:55:25 +0000491
wdenk2abbe072003-06-16 23:50:08 +0000492 if (cnt == 1) {
493 data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) <<
494 8);
495 if ((rc = write_word (info, wp, data)) != 0) {
496 return (rc);
497 }
498 src += 1;
499 wp += 1;
500 cnt -= 1;
501 };
wdenkdc7c9a12003-03-26 06:55:25 +0000502
wdenk2abbe072003-06-16 23:50:08 +0000503 return ERR_OK;
wdenkdc7c9a12003-03-26 06:55:25 +0000504}