blob: c904a88916ac0c363e5ee7a5f91ccf9ace278bb4 [file] [log] [blame]
Mike Frysinger94bae5c2008-03-30 15:46:13 -04001/*
Bin Meng75574052016-02-05 19:30:11 -08002 * U-Boot - string.c Contains library routines.
Mike Frysinger94bae5c2008-03-30 15:46:13 -04003 *
Mike Frysingerc83b9e02008-08-07 15:30:49 -04004 * Copyright (c) 2005-2008 Analog Devices Inc.
Mike Frysinger94bae5c2008-03-30 15:46:13 -04005 *
6 * (C) Copyright 2000-2004
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Mike Frysinger94bae5c2008-03-30 15:46:13 -040010 */
11
12#include <common.h>
13#include <config.h>
14#include <asm/blackfin.h>
15#include <asm/io.h>
Bob Liu6e24a612012-08-16 11:19:08 +080016#include <asm/dma.h>
Mike Frysinger94bae5c2008-03-30 15:46:13 -040017
18char *strcpy(char *dest, const char *src)
19{
20 char *xdest = dest;
21 char temp = 0;
22
23 __asm__ __volatile__ (
24 "1:\t%2 = B [%1++] (Z);\n\t"
25 "B [%0++] = %2;\n\t"
26 "CC = %2;\n\t"
27 "if cc jump 1b (bp);\n"
28 : "=a"(dest), "=a"(src), "=d"(temp)
29 : "0"(dest), "1"(src), "2"(temp)
30 : "memory");
31
32 return xdest;
33}
34
35char *strncpy(char *dest, const char *src, size_t n)
36{
37 char *xdest = dest;
38 char temp = 0;
39
40 if (n == 0)
41 return xdest;
42
43 __asm__ __volatile__ (
44 "1:\t%3 = B [%1++] (Z);\n\t"
45 "B [%0++] = %3;\n\t"
46 "CC = %3;\n\t"
47 "if ! cc jump 2f;\n\t"
48 "%2 += -1;\n\t"
49 "CC = %2 == 0;\n\t"
50 "if ! cc jump 1b (bp);\n"
51 "2:\n"
52 : "=a"(dest), "=a"(src), "=da"(n), "=d"(temp)
53 : "0"(dest), "1"(src), "2"(n), "3"(temp)
54 : "memory");
55
56 return xdest;
57}
58
59int strcmp(const char *cs, const char *ct)
60{
61 char __res1, __res2;
62
63 __asm__ (
64 "1:\t%2 = B[%0++] (Z);\n\t" /* get *cs */
65 "%3 = B[%1++] (Z);\n\t" /* get *ct */
66 "CC = %2 == %3;\n\t" /* compare a byte */
67 "if ! cc jump 2f;\n\t" /* not equal, break out */
68 "CC = %2;\n\t" /* at end of cs? */
69 "if cc jump 1b (bp);\n\t" /* no, keep going */
70 "jump.s 3f;\n" /* strings are equal */
71 "2:\t%2 = %2 - %3;\n" /* *cs - *ct */
72 "3:\n"
73 : "=a"(cs), "=a"(ct), "=d"(__res1), "=d"(__res2)
74 : "0"(cs), "1"(ct));
75
76 return __res1;
77}
78
79int strncmp(const char *cs, const char *ct, size_t count)
80{
81 char __res1, __res2;
82
83 if (!count)
84 return 0;
85
86 __asm__(
87 "1:\t%3 = B[%0++] (Z);\n\t" /* get *cs */
88 "%4 = B[%1++] (Z);\n\t" /* get *ct */
89 "CC = %3 == %4;\n\t" /* compare a byte */
90 "if ! cc jump 3f;\n\t" /* not equal, break out */
91 "CC = %3;\n\t" /* at end of cs? */
92 "if ! cc jump 4f;\n\t" /* yes, all done */
93 "%2 += -1;\n\t" /* no, adjust count */
94 "CC = %2 == 0;\n\t" "if ! cc jump 1b;\n" /* more to do, keep going */
95 "2:\t%3 = 0;\n\t" /* strings are equal */
96 "jump.s 4f;\n" "3:\t%3 = %3 - %4;\n" /* *cs - *ct */
97 "4:"
98 : "=a"(cs), "=a"(ct), "=da"(count), "=d"(__res1), "=d"(__res2)
99 : "0"(cs), "1"(ct), "2"(count));
100
101 return __res1;
102}
103
Bob Liu6e24a612012-08-16 11:19:08 +0800104#ifdef MDMA1_D0_NEXT_DESC_PTR
105# define MDMA_D0_NEXT_DESC_PTR MDMA1_D0_NEXT_DESC_PTR
106# define MDMA_S0_NEXT_DESC_PTR MDMA1_S0_NEXT_DESC_PTR
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400107#endif
Bob Liu6e24a612012-08-16 11:19:08 +0800108
109static void dma_calc_size(unsigned long ldst, unsigned long lsrc, size_t count,
110 unsigned long *dshift, unsigned long *bpos)
111{
112 unsigned long limit;
113
114#ifdef MSIZE
Sonic Zhang5a3ab3a2012-12-11 16:51:23 +0800115 /* The max memory DMA memory transfer size is 32 bytes. */
116 limit = 5;
Bob Liu6e24a612012-08-16 11:19:08 +0800117 *dshift = MSIZE_P;
118#else
Sonic Zhang5a3ab3a2012-12-11 16:51:23 +0800119 /* The max memory DMA memory transfer size is 4 bytes. */
120 limit = 2;
Bob Liu6e24a612012-08-16 11:19:08 +0800121 *dshift = WDSIZE_P;
122#endif
123
Masahiro Yamadadb204642014-11-07 03:03:31 +0900124 *bpos = min(limit, (unsigned long)ffs(ldst | lsrc | count)) - 1;
Bob Liu6e24a612012-08-16 11:19:08 +0800125}
126
Mike Frysingerc83b9e02008-08-07 15:30:49 -0400127/* This version misbehaves for count values of 0 and 2^16+.
128 * Perhaps we should detect that ? Nowhere do we actually
129 * use dma memcpy for those types of lengths though ...
130 */
Mike Frysinger4e906a02008-08-07 15:31:13 -0400131void dma_memcpy_nocache(void *dst, const void *src, size_t count)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400132{
Bob Liu6e24a612012-08-16 11:19:08 +0800133 struct dma_register *mdma_d0 = (void *)MDMA_D0_NEXT_DESC_PTR;
134 struct dma_register *mdma_s0 = (void *)MDMA_S0_NEXT_DESC_PTR;
135 unsigned long ldst = (unsigned long)dst;
136 unsigned long lsrc = (unsigned long)src;
137 unsigned long dshift, bpos;
138 uint32_t dsize, mod;
Mike Frysinger26f60342008-11-05 12:45:24 -0500139
Mike Frysinger3114a872008-11-05 07:20:37 -0500140 /* Disable DMA in case it's still running (older u-boot's did not
141 * always turn them off). Do it before the if statement below so
142 * we can be cheap and not do a SSYNC() due to the forced abort.
143 */
Bob Liu6e24a612012-08-16 11:19:08 +0800144 bfin_write(&mdma_d0->config, 0);
145 bfin_write(&mdma_s0->config, 0);
146 bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
Mike Frysinger3114a872008-11-05 07:20:37 -0500147
Mike Frysingerc83b9e02008-08-07 15:30:49 -0400148 /* Scratchpad cannot be a DMA source or destination */
Bob Liu6e24a612012-08-16 11:19:08 +0800149 if ((lsrc >= L1_SRAM_SCRATCH && lsrc < L1_SRAM_SCRATCH_END) ||
150 (ldst >= L1_SRAM_SCRATCH && ldst < L1_SRAM_SCRATCH_END))
Mike Frysingerc83b9e02008-08-07 15:30:49 -0400151 hang();
152
Bob Liu6e24a612012-08-16 11:19:08 +0800153 dma_calc_size(ldst, lsrc, count, &dshift, &bpos);
154 dsize = bpos << dshift;
155 count >>= bpos;
156 mod = 1 << bpos;
157
158#ifdef PSIZE
Sonic Zhang5a3ab3a2012-12-11 16:51:23 +0800159 /* The max memory DMA peripheral transfer size is 4 bytes. */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900160 dsize |= min(2UL, bpos) << PSIZE_P;
Bob Liu6e24a612012-08-16 11:19:08 +0800161#endif
Mike Frysinger26f60342008-11-05 12:45:24 -0500162
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400163 /* Copy sram functions from sdram to sram */
164 /* Setup destination start address */
Bob Liu6e24a612012-08-16 11:19:08 +0800165 bfin_write(&mdma_d0->start_addr, ldst);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400166 /* Setup destination xcount */
Bob Liu6e24a612012-08-16 11:19:08 +0800167 bfin_write(&mdma_d0->x_count, count);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400168 /* Setup destination xmodify */
Bob Liu6e24a612012-08-16 11:19:08 +0800169 bfin_write(&mdma_d0->x_modify, mod);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400170
171 /* Setup Source start address */
Bob Liu6e24a612012-08-16 11:19:08 +0800172 bfin_write(&mdma_s0->start_addr, lsrc);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400173 /* Setup Source xcount */
Bob Liu6e24a612012-08-16 11:19:08 +0800174 bfin_write(&mdma_s0->x_count, count);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400175 /* Setup Source xmodify */
Bob Liu6e24a612012-08-16 11:19:08 +0800176 bfin_write(&mdma_s0->x_modify, mod);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400177
178 /* Enable source DMA */
Bob Liu6e24a612012-08-16 11:19:08 +0800179 bfin_write(&mdma_s0->config, dsize | DMAEN);
180 bfin_write(&mdma_d0->config, dsize | DMAEN | WNR | DI_EN);
Mike Frysinger4e906a02008-08-07 15:31:13 -0400181 SSYNC();
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400182
Bob Liu6e24a612012-08-16 11:19:08 +0800183 while (!(bfin_read(&mdma_d0->status) & DMA_DONE))
Mike Frysinger4e906a02008-08-07 15:31:13 -0400184 continue;
185
Bob Liu6e24a612012-08-16 11:19:08 +0800186 bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
187 bfin_write(&mdma_d0->config, 0);
188 bfin_write(&mdma_s0->config, 0);
Mike Frysinger4e906a02008-08-07 15:31:13 -0400189}
Mike Frysinger343e9f72008-10-06 03:35:44 -0400190/* We should do a dcache invalidate on the destination after the dma, but since
191 * we lack such hardware capability, we'll flush/invalidate the destination
192 * before the dma and bank on the idea that u-boot is single threaded.
193 */
Mike Frysinger4e906a02008-08-07 15:31:13 -0400194void *dma_memcpy(void *dst, const void *src, size_t count)
195{
Mike Frysinger343e9f72008-10-06 03:35:44 -0400196 if (dcache_status()) {
Mike Frysinger4e906a02008-08-07 15:31:13 -0400197 blackfin_dcache_flush_range(src, src + count);
Mike Frysinger343e9f72008-10-06 03:35:44 -0400198 blackfin_dcache_flush_invalidate_range(dst, dst + count);
199 }
Mike Frysinger4e906a02008-08-07 15:31:13 -0400200
201 dma_memcpy_nocache(dst, src, count);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400202
203 if (icache_status())
204 blackfin_icache_flush_range(dst, dst + count);
205
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400206 return dst;
207}
208
209/*
210 * memcpy - Copy one area of memory to another
211 * @dest: Where to copy to
212 * @src: Where to copy from
213 * @count: The size of the area.
214 *
215 * We need to have this wrapper in memcpy() as common code may call memcpy()
216 * to load up L1 regions. Consider loading an ELF which has sections with
217 * LMA's pointing to L1. The common code ELF loader will simply use memcpy()
218 * to move the ELF's sections into the right place. We need to catch that
219 * here and redirect to dma_memcpy().
220 */
221extern void *memcpy_ASM(void *dst, const void *src, size_t count);
222void *memcpy(void *dst, const void *src, size_t count)
223{
224 if (!count)
225 return dst;
226
Robin Getz230d0012009-12-21 16:35:48 -0500227#ifdef CONFIG_CMD_KGDB
228 if (src >= (void *)SYSMMR_BASE) {
229 if (count == 2 && (unsigned long)src % 2 == 0) {
230 u16 mmr = bfin_read16(src);
231 memcpy(dst, &mmr, sizeof(mmr));
232 return dst;
233 }
234 if (count == 4 && (unsigned long)src % 4 == 0) {
235 u32 mmr = bfin_read32(src);
236 memcpy(dst, &mmr, sizeof(mmr));
237 return dst;
238 }
239 /* Failed for some reason */
240 memset(dst, 0xad, count);
241 return dst;
242 }
243 if (dst >= (void *)SYSMMR_BASE) {
244 if (count == 2 && (unsigned long)dst % 2 == 0) {
245 u16 mmr;
246 memcpy(&mmr, src, sizeof(mmr));
247 bfin_write16(dst, mmr);
248 return dst;
249 }
250 if (count == 4 && (unsigned long)dst % 4 == 0) {
251 u32 mmr;
252 memcpy(&mmr, src, sizeof(mmr));
253 bfin_write32(dst, mmr);
254 return dst;
255 }
256 /* Failed for some reason */
257 memset(dst, 0xad, count);
258 return dst;
259 }
260#endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400261
Robin Getz230d0012009-12-21 16:35:48 -0500262 /* if L1 is the source or dst, use DMA */
263 if (addr_bfin_on_chip_mem(dst) || addr_bfin_on_chip_mem(src))
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400264 return dma_memcpy(dst, src, count);
Robin Getz230d0012009-12-21 16:35:48 -0500265 else
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400266 /* No L1 is involved, so just call regular memcpy */
267 return memcpy_ASM(dst, src, count);
268}