Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - string.c Contains library routines. |
| 3 | * |
Mike Frysinger | c83b9e0 | 2008-08-07 15:30:49 -0400 | [diff] [blame] | 4 | * Copyright (c) 2005-2008 Analog Devices Inc. |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2000-2004 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <config.h> |
| 30 | #include <asm/blackfin.h> |
| 31 | #include <asm/io.h> |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 32 | #include <asm/dma.h> |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 33 | |
| 34 | char *strcpy(char *dest, const char *src) |
| 35 | { |
| 36 | char *xdest = dest; |
| 37 | char temp = 0; |
| 38 | |
| 39 | __asm__ __volatile__ ( |
| 40 | "1:\t%2 = B [%1++] (Z);\n\t" |
| 41 | "B [%0++] = %2;\n\t" |
| 42 | "CC = %2;\n\t" |
| 43 | "if cc jump 1b (bp);\n" |
| 44 | : "=a"(dest), "=a"(src), "=d"(temp) |
| 45 | : "0"(dest), "1"(src), "2"(temp) |
| 46 | : "memory"); |
| 47 | |
| 48 | return xdest; |
| 49 | } |
| 50 | |
| 51 | char *strncpy(char *dest, const char *src, size_t n) |
| 52 | { |
| 53 | char *xdest = dest; |
| 54 | char temp = 0; |
| 55 | |
| 56 | if (n == 0) |
| 57 | return xdest; |
| 58 | |
| 59 | __asm__ __volatile__ ( |
| 60 | "1:\t%3 = B [%1++] (Z);\n\t" |
| 61 | "B [%0++] = %3;\n\t" |
| 62 | "CC = %3;\n\t" |
| 63 | "if ! cc jump 2f;\n\t" |
| 64 | "%2 += -1;\n\t" |
| 65 | "CC = %2 == 0;\n\t" |
| 66 | "if ! cc jump 1b (bp);\n" |
| 67 | "2:\n" |
| 68 | : "=a"(dest), "=a"(src), "=da"(n), "=d"(temp) |
| 69 | : "0"(dest), "1"(src), "2"(n), "3"(temp) |
| 70 | : "memory"); |
| 71 | |
| 72 | return xdest; |
| 73 | } |
| 74 | |
| 75 | int strcmp(const char *cs, const char *ct) |
| 76 | { |
| 77 | char __res1, __res2; |
| 78 | |
| 79 | __asm__ ( |
| 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 */ |
| 88 | "3:\n" |
| 89 | : "=a"(cs), "=a"(ct), "=d"(__res1), "=d"(__res2) |
| 90 | : "0"(cs), "1"(ct)); |
| 91 | |
| 92 | return __res1; |
| 93 | } |
| 94 | |
| 95 | int strncmp(const char *cs, const char *ct, size_t count) |
| 96 | { |
| 97 | char __res1, __res2; |
| 98 | |
| 99 | if (!count) |
| 100 | return 0; |
| 101 | |
| 102 | __asm__( |
| 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 */ |
| 113 | "4:" |
| 114 | : "=a"(cs), "=a"(ct), "=da"(count), "=d"(__res1), "=d"(__res2) |
| 115 | : "0"(cs), "1"(ct), "2"(count)); |
| 116 | |
| 117 | return __res1; |
| 118 | } |
| 119 | |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 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 |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 123 | #endif |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 124 | |
| 125 | static void dma_calc_size(unsigned long ldst, unsigned long lsrc, size_t count, |
| 126 | unsigned long *dshift, unsigned long *bpos) |
| 127 | { |
| 128 | unsigned long limit; |
| 129 | |
| 130 | #ifdef MSIZE |
Sonic Zhang | 5a3ab3a | 2012-12-11 16:51:23 +0800 | [diff] [blame] | 131 | /* The max memory DMA memory transfer size is 32 bytes. */ |
| 132 | limit = 5; |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 133 | *dshift = MSIZE_P; |
| 134 | #else |
Sonic Zhang | 5a3ab3a | 2012-12-11 16:51:23 +0800 | [diff] [blame] | 135 | /* The max memory DMA memory transfer size is 4 bytes. */ |
| 136 | limit = 2; |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 137 | *dshift = WDSIZE_P; |
| 138 | #endif |
| 139 | |
| 140 | *bpos = min(limit, ffs(ldst | lsrc | count)) - 1; |
| 141 | } |
| 142 | |
Mike Frysinger | c83b9e0 | 2008-08-07 15:30:49 -0400 | [diff] [blame] | 143 | /* This version misbehaves for count values of 0 and 2^16+. |
| 144 | * Perhaps we should detect that ? Nowhere do we actually |
| 145 | * use dma memcpy for those types of lengths though ... |
| 146 | */ |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 147 | void dma_memcpy_nocache(void *dst, const void *src, size_t count) |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 148 | { |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 149 | struct dma_register *mdma_d0 = (void *)MDMA_D0_NEXT_DESC_PTR; |
| 150 | struct dma_register *mdma_s0 = (void *)MDMA_S0_NEXT_DESC_PTR; |
| 151 | unsigned long ldst = (unsigned long)dst; |
| 152 | unsigned long lsrc = (unsigned long)src; |
| 153 | unsigned long dshift, bpos; |
| 154 | uint32_t dsize, mod; |
Mike Frysinger | 26f6034 | 2008-11-05 12:45:24 -0500 | [diff] [blame] | 155 | |
Mike Frysinger | 3114a87 | 2008-11-05 07:20:37 -0500 | [diff] [blame] | 156 | /* Disable DMA in case it's still running (older u-boot's did not |
| 157 | * always turn them off). Do it before the if statement below so |
| 158 | * we can be cheap and not do a SSYNC() due to the forced abort. |
| 159 | */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 160 | bfin_write(&mdma_d0->config, 0); |
| 161 | bfin_write(&mdma_s0->config, 0); |
| 162 | bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR); |
Mike Frysinger | 3114a87 | 2008-11-05 07:20:37 -0500 | [diff] [blame] | 163 | |
Mike Frysinger | c83b9e0 | 2008-08-07 15:30:49 -0400 | [diff] [blame] | 164 | /* Scratchpad cannot be a DMA source or destination */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 165 | if ((lsrc >= L1_SRAM_SCRATCH && lsrc < L1_SRAM_SCRATCH_END) || |
| 166 | (ldst >= L1_SRAM_SCRATCH && ldst < L1_SRAM_SCRATCH_END)) |
Mike Frysinger | c83b9e0 | 2008-08-07 15:30:49 -0400 | [diff] [blame] | 167 | hang(); |
| 168 | |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 169 | dma_calc_size(ldst, lsrc, count, &dshift, &bpos); |
| 170 | dsize = bpos << dshift; |
| 171 | count >>= bpos; |
| 172 | mod = 1 << bpos; |
| 173 | |
| 174 | #ifdef PSIZE |
Sonic Zhang | 5a3ab3a | 2012-12-11 16:51:23 +0800 | [diff] [blame] | 175 | /* The max memory DMA peripheral transfer size is 4 bytes. */ |
| 176 | dsize |= min(2, bpos) << PSIZE_P; |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 177 | #endif |
Mike Frysinger | 26f6034 | 2008-11-05 12:45:24 -0500 | [diff] [blame] | 178 | |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 179 | /* Copy sram functions from sdram to sram */ |
| 180 | /* Setup destination start address */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 181 | bfin_write(&mdma_d0->start_addr, ldst); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 182 | /* Setup destination xcount */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 183 | bfin_write(&mdma_d0->x_count, count); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 184 | /* Setup destination xmodify */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 185 | bfin_write(&mdma_d0->x_modify, mod); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 186 | |
| 187 | /* Setup Source start address */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 188 | bfin_write(&mdma_s0->start_addr, lsrc); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 189 | /* Setup Source xcount */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 190 | bfin_write(&mdma_s0->x_count, count); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 191 | /* Setup Source xmodify */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 192 | bfin_write(&mdma_s0->x_modify, mod); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 193 | |
| 194 | /* Enable source DMA */ |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 195 | bfin_write(&mdma_s0->config, dsize | DMAEN); |
| 196 | bfin_write(&mdma_d0->config, dsize | DMAEN | WNR | DI_EN); |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 197 | SSYNC(); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 198 | |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 199 | while (!(bfin_read(&mdma_d0->status) & DMA_DONE)) |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 200 | continue; |
| 201 | |
Bob Liu | 6e24a61 | 2012-08-16 11:19:08 +0800 | [diff] [blame] | 202 | bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR); |
| 203 | bfin_write(&mdma_d0->config, 0); |
| 204 | bfin_write(&mdma_s0->config, 0); |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 205 | } |
Mike Frysinger | 343e9f7 | 2008-10-06 03:35:44 -0400 | [diff] [blame] | 206 | /* We should do a dcache invalidate on the destination after the dma, but since |
| 207 | * we lack such hardware capability, we'll flush/invalidate the destination |
| 208 | * before the dma and bank on the idea that u-boot is single threaded. |
| 209 | */ |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 210 | void *dma_memcpy(void *dst, const void *src, size_t count) |
| 211 | { |
Mike Frysinger | 343e9f7 | 2008-10-06 03:35:44 -0400 | [diff] [blame] | 212 | if (dcache_status()) { |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 213 | blackfin_dcache_flush_range(src, src + count); |
Mike Frysinger | 343e9f7 | 2008-10-06 03:35:44 -0400 | [diff] [blame] | 214 | blackfin_dcache_flush_invalidate_range(dst, dst + count); |
| 215 | } |
Mike Frysinger | 4e906a0 | 2008-08-07 15:31:13 -0400 | [diff] [blame] | 216 | |
| 217 | dma_memcpy_nocache(dst, src, count); |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 218 | |
| 219 | if (icache_status()) |
| 220 | blackfin_icache_flush_range(dst, dst + count); |
| 221 | |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 222 | return dst; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * memcpy - Copy one area of memory to another |
| 227 | * @dest: Where to copy to |
| 228 | * @src: Where to copy from |
| 229 | * @count: The size of the area. |
| 230 | * |
| 231 | * We need to have this wrapper in memcpy() as common code may call memcpy() |
| 232 | * to load up L1 regions. Consider loading an ELF which has sections with |
| 233 | * LMA's pointing to L1. The common code ELF loader will simply use memcpy() |
| 234 | * to move the ELF's sections into the right place. We need to catch that |
| 235 | * here and redirect to dma_memcpy(). |
| 236 | */ |
| 237 | extern void *memcpy_ASM(void *dst, const void *src, size_t count); |
| 238 | void *memcpy(void *dst, const void *src, size_t count) |
| 239 | { |
| 240 | if (!count) |
| 241 | return dst; |
| 242 | |
Robin Getz | 230d001 | 2009-12-21 16:35:48 -0500 | [diff] [blame] | 243 | #ifdef CONFIG_CMD_KGDB |
| 244 | if (src >= (void *)SYSMMR_BASE) { |
| 245 | if (count == 2 && (unsigned long)src % 2 == 0) { |
| 246 | u16 mmr = bfin_read16(src); |
| 247 | memcpy(dst, &mmr, sizeof(mmr)); |
| 248 | return dst; |
| 249 | } |
| 250 | if (count == 4 && (unsigned long)src % 4 == 0) { |
| 251 | u32 mmr = bfin_read32(src); |
| 252 | memcpy(dst, &mmr, sizeof(mmr)); |
| 253 | return dst; |
| 254 | } |
| 255 | /* Failed for some reason */ |
| 256 | memset(dst, 0xad, count); |
| 257 | return dst; |
| 258 | } |
| 259 | if (dst >= (void *)SYSMMR_BASE) { |
| 260 | if (count == 2 && (unsigned long)dst % 2 == 0) { |
| 261 | u16 mmr; |
| 262 | memcpy(&mmr, src, sizeof(mmr)); |
| 263 | bfin_write16(dst, mmr); |
| 264 | return dst; |
| 265 | } |
| 266 | if (count == 4 && (unsigned long)dst % 4 == 0) { |
| 267 | u32 mmr; |
| 268 | memcpy(&mmr, src, sizeof(mmr)); |
| 269 | bfin_write32(dst, mmr); |
| 270 | return dst; |
| 271 | } |
| 272 | /* Failed for some reason */ |
| 273 | memset(dst, 0xad, count); |
| 274 | return dst; |
| 275 | } |
| 276 | #endif |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 277 | |
Robin Getz | 230d001 | 2009-12-21 16:35:48 -0500 | [diff] [blame] | 278 | /* if L1 is the source or dst, use DMA */ |
| 279 | if (addr_bfin_on_chip_mem(dst) || addr_bfin_on_chip_mem(src)) |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 280 | return dma_memcpy(dst, src, count); |
Robin Getz | 230d001 | 2009-12-21 16:35:48 -0500 | [diff] [blame] | 281 | else |
Mike Frysinger | 94bae5c | 2008-03-30 15:46:13 -0400 | [diff] [blame] | 282 | /* No L1 is involved, so just call regular memcpy */ |
| 283 | return memcpy_ASM(dst, src, count); |
| 284 | } |