2 * U-boot - string.c Contains library routines.
4 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * (C) Copyright 2000-2004
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
30 #include <asm/blackfin.h>
34 char *strcpy(char *dest, const char *src)
39 __asm__ __volatile__ (
40 "1:\t%2 = B [%1++] (Z);\n\t"
43 "if cc jump 1b (bp);\n"
44 : "=a"(dest), "=a"(src), "=d"(temp)
45 : "0"(dest), "1"(src), "2"(temp)
51 char *strncpy(char *dest, const char *src, size_t n)
59 __asm__ __volatile__ (
60 "1:\t%3 = B [%1++] (Z);\n\t"
63 "if ! cc jump 2f;\n\t"
66 "if ! cc jump 1b (bp);\n"
68 : "=a"(dest), "=a"(src), "=da"(n), "=d"(temp)
69 : "0"(dest), "1"(src), "2"(n), "3"(temp)
75 int strcmp(const char *cs, const char *ct)
80 "1:\t%2 = B[%0++] (Z);\n\t" /* get *cs */
81 "%3 = B[%1++] (Z);\n\t" /* get *ct */
82 "CC = %2 == %3;\n\t" /* compare a byte */
83 "if ! cc jump 2f;\n\t" /* not equal, break out */
84 "CC = %2;\n\t" /* at end of cs? */
85 "if cc jump 1b (bp);\n\t" /* no, keep going */
86 "jump.s 3f;\n" /* strings are equal */
87 "2:\t%2 = %2 - %3;\n" /* *cs - *ct */
89 : "=a"(cs), "=a"(ct), "=d"(__res1), "=d"(__res2)
95 int strncmp(const char *cs, const char *ct, size_t count)
103 "1:\t%3 = B[%0++] (Z);\n\t" /* get *cs */
104 "%4 = B[%1++] (Z);\n\t" /* get *ct */
105 "CC = %3 == %4;\n\t" /* compare a byte */
106 "if ! cc jump 3f;\n\t" /* not equal, break out */
107 "CC = %3;\n\t" /* at end of cs? */
108 "if ! cc jump 4f;\n\t" /* yes, all done */
109 "%2 += -1;\n\t" /* no, adjust count */
110 "CC = %2 == 0;\n\t" "if ! cc jump 1b;\n" /* more to do, keep going */
111 "2:\t%3 = 0;\n\t" /* strings are equal */
112 "jump.s 4f;\n" "3:\t%3 = %3 - %4;\n" /* *cs - *ct */
114 : "=a"(cs), "=a"(ct), "=da"(count), "=d"(__res1), "=d"(__res2)
115 : "0"(cs), "1"(ct), "2"(count));
120 #ifdef MDMA1_D0_NEXT_DESC_PTR
121 # define MDMA_D0_NEXT_DESC_PTR MDMA1_D0_NEXT_DESC_PTR
122 # define MDMA_S0_NEXT_DESC_PTR MDMA1_S0_NEXT_DESC_PTR
125 static void dma_calc_size(unsigned long ldst, unsigned long lsrc, size_t count,
126 unsigned long *dshift, unsigned long *bpos)
138 *bpos = min(limit, ffs(ldst | lsrc | count)) - 1;
141 /* This version misbehaves for count values of 0 and 2^16+.
142 * Perhaps we should detect that ? Nowhere do we actually
143 * use dma memcpy for those types of lengths though ...
145 void dma_memcpy_nocache(void *dst, const void *src, size_t count)
147 struct dma_register *mdma_d0 = (void *)MDMA_D0_NEXT_DESC_PTR;
148 struct dma_register *mdma_s0 = (void *)MDMA_S0_NEXT_DESC_PTR;
149 unsigned long ldst = (unsigned long)dst;
150 unsigned long lsrc = (unsigned long)src;
151 unsigned long dshift, bpos;
154 /* Disable DMA in case it's still running (older u-boot's did not
155 * always turn them off). Do it before the if statement below so
156 * we can be cheap and not do a SSYNC() due to the forced abort.
158 bfin_write(&mdma_d0->config, 0);
159 bfin_write(&mdma_s0->config, 0);
160 bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
162 /* Scratchpad cannot be a DMA source or destination */
163 if ((lsrc >= L1_SRAM_SCRATCH && lsrc < L1_SRAM_SCRATCH_END) ||
164 (ldst >= L1_SRAM_SCRATCH && ldst < L1_SRAM_SCRATCH_END))
167 dma_calc_size(ldst, lsrc, count, &dshift, &bpos);
168 dsize = bpos << dshift;
173 dsize |= min(3, bpos) << PSIZE_P;
176 /* Copy sram functions from sdram to sram */
177 /* Setup destination start address */
178 bfin_write(&mdma_d0->start_addr, ldst);
179 /* Setup destination xcount */
180 bfin_write(&mdma_d0->x_count, count);
181 /* Setup destination xmodify */
182 bfin_write(&mdma_d0->x_modify, mod);
184 /* Setup Source start address */
185 bfin_write(&mdma_s0->start_addr, lsrc);
186 /* Setup Source xcount */
187 bfin_write(&mdma_s0->x_count, count);
188 /* Setup Source xmodify */
189 bfin_write(&mdma_s0->x_modify, mod);
191 /* Enable source DMA */
192 bfin_write(&mdma_s0->config, dsize | DMAEN);
193 bfin_write(&mdma_d0->config, dsize | DMAEN | WNR | DI_EN);
196 while (!(bfin_read(&mdma_d0->status) & DMA_DONE))
199 bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
200 bfin_write(&mdma_d0->config, 0);
201 bfin_write(&mdma_s0->config, 0);
203 /* We should do a dcache invalidate on the destination after the dma, but since
204 * we lack such hardware capability, we'll flush/invalidate the destination
205 * before the dma and bank on the idea that u-boot is single threaded.
207 void *dma_memcpy(void *dst, const void *src, size_t count)
209 if (dcache_status()) {
210 blackfin_dcache_flush_range(src, src + count);
211 blackfin_dcache_flush_invalidate_range(dst, dst + count);
214 dma_memcpy_nocache(dst, src, count);
217 blackfin_icache_flush_range(dst, dst + count);
223 * memcpy - Copy one area of memory to another
224 * @dest: Where to copy to
225 * @src: Where to copy from
226 * @count: The size of the area.
228 * We need to have this wrapper in memcpy() as common code may call memcpy()
229 * to load up L1 regions. Consider loading an ELF which has sections with
230 * LMA's pointing to L1. The common code ELF loader will simply use memcpy()
231 * to move the ELF's sections into the right place. We need to catch that
232 * here and redirect to dma_memcpy().
234 extern void *memcpy_ASM(void *dst, const void *src, size_t count);
235 void *memcpy(void *dst, const void *src, size_t count)
240 #ifdef CONFIG_CMD_KGDB
241 if (src >= (void *)SYSMMR_BASE) {
242 if (count == 2 && (unsigned long)src % 2 == 0) {
243 u16 mmr = bfin_read16(src);
244 memcpy(dst, &mmr, sizeof(mmr));
247 if (count == 4 && (unsigned long)src % 4 == 0) {
248 u32 mmr = bfin_read32(src);
249 memcpy(dst, &mmr, sizeof(mmr));
252 /* Failed for some reason */
253 memset(dst, 0xad, count);
256 if (dst >= (void *)SYSMMR_BASE) {
257 if (count == 2 && (unsigned long)dst % 2 == 0) {
259 memcpy(&mmr, src, sizeof(mmr));
260 bfin_write16(dst, mmr);
263 if (count == 4 && (unsigned long)dst % 4 == 0) {
265 memcpy(&mmr, src, sizeof(mmr));
266 bfin_write32(dst, mmr);
269 /* Failed for some reason */
270 memset(dst, 0xad, count);
275 /* if L1 is the source or dst, use DMA */
276 if (addr_bfin_on_chip_mem(dst) || addr_bfin_on_chip_mem(src))
277 return dma_memcpy(dst, src, count);
279 /* No L1 is involved, so just call regular memcpy */
280 return memcpy_ASM(dst, src, count);