blob: 09503548aa92e0eb490621537891afb7b9c8255c [file] [log] [blame]
Wolfgang Denk460c2ce2010-06-21 22:29:59 +02001/*
2 * (C) Copyright 2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * This is a workaround for issues on the MPC5200, where unaligned
26 * 32-bit-accesses to the local bus will deliver corrupted data. This
27 * happens for example when trying to use memcpy() from an odd NOR
28 * flash address; the behaviour can be also seen when using "md" on an
29 * odd NOR flash address (but there it is not a bug in U-Boot, which
30 * only shows the behaviour of this processor).
31 *
32 * For memcpy(), we test if either the source or the target address
33 * are not 32 bit aligned, and - if so - if the source address is in
34 * NOR flash: in this case we perform a byte-wise (slow) then; for
35 * aligned operations of non-flash areas we use the optimized (fast)
36 * real __memcpy(). This way we minimize the performance impact of
37 * this workaround.
38 *
39 */
40
41#include <common.h>
42#include <flash.h>
43#include <linux/types.h>
44
45void *memcpy(void *trg, const void *src, size_t len)
46{
47 extern void* __memcpy(void *, const void *, size_t);
48 char *s = (char *)src;
49 char *t = (char *)trg;
50 void *dest = (void *)src;
51
52 /*
53 * Check is source address is in flash:
54 * If not, we use the fast assembler code
55 */
56 if (((((unsigned long)s & 3) == 0) /* source aligned */
57 && /* AND */
58 (((unsigned long)t & 3) == 0)) /* target aligned, */
59 || /* or */
60 (addr2info((ulong)s) == NULL)) { /* source not in flash */
61 return __memcpy(trg, src, len);
62 }
63
64 /*
65 * Copying from flash, perform byte by byte copy.
66 */
67 while (len-- > 0)
68 *t++ = *s++;
69
70 return dest;
71}