Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - bf533_string.c Contains library routines. |
| 3 | * |
Aubrey Li | 314d22f | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 4 | * Copyright (c) 2005-2007 Analog Devices Inc. |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [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 |
Aubrey Li | 314d22f | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 25 | * MA 02110-1301 USA |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <asm/setup.h> |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 30 | #include <config.h> |
| 31 | #include <asm/blackfin.h> |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 32 | #include <asm/io.h> |
Aubrey Li | 7158ebd | 2007-04-05 18:34:06 +0800 | [diff] [blame] | 33 | #include "cache.h" |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 34 | |
| 35 | char *strcpy(char *dest, const char *src) |
| 36 | { |
| 37 | char *xdest = dest; |
| 38 | char temp = 0; |
| 39 | |
| 40 | __asm__ __volatile__ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 41 | ("1:\t%2 = B [%1++] (Z);\n\t" |
| 42 | "B [%0++] = %2;\n\t" |
| 43 | "CC = %2;\n\t" |
| 44 | "if cc jump 1b (bp);\n":"=a"(dest), "=a"(src), "=d"(temp) |
| 45 | :"0"(dest), "1"(src), "2"(temp):"memory"); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 46 | |
| 47 | return xdest; |
| 48 | } |
| 49 | |
| 50 | char *strncpy(char *dest, const char *src, size_t n) |
| 51 | { |
| 52 | char *xdest = dest; |
| 53 | char temp = 0; |
| 54 | |
| 55 | if (n == 0) |
| 56 | return xdest; |
| 57 | |
| 58 | __asm__ __volatile__ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 59 | ("1:\t%3 = B [%1++] (Z);\n\t" |
| 60 | "B [%0++] = %3;\n\t" |
| 61 | "CC = %3;\n\t" |
| 62 | "if ! cc jump 2f;\n\t" |
| 63 | "%2 += -1;\n\t" |
| 64 | "CC = %2 == 0;\n\t" |
| 65 | "if ! cc jump 1b (bp);\n" |
| 66 | "2:\n":"=a"(dest), "=a"(src), "=da"(n), "=d"(temp) |
| 67 | :"0"(dest), "1"(src), "2"(n), "3"(temp) |
| 68 | :"memory"); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 69 | |
| 70 | return xdest; |
| 71 | } |
| 72 | |
| 73 | int strcmp(const char *cs, const char *ct) |
| 74 | { |
| 75 | char __res1, __res2; |
| 76 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 77 | __asm__("1:\t%2 = B[%0++] (Z);\n\t" /* get *cs */ |
| 78 | "%3 = B[%1++] (Z);\n\t" /* get *ct */ |
| 79 | "CC = %2 == %3;\n\t" /* compare a byte */ |
| 80 | "if ! cc jump 2f;\n\t" /* not equal, break out */ |
| 81 | "CC = %2;\n\t" /* at end of cs? */ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 82 | "if cc jump 1b (bp);\n\t" /* no, keep going */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 83 | "jump.s 3f;\n" /* strings are equal */ |
| 84 | "2:\t%2 = %2 - %3;\n" /* *cs - *ct */ |
| 85 | "3:\n": "=a"(cs), "=a"(ct), "=d"(__res1), "=d"(__res2) |
| 86 | : "0"(cs), "1"(ct)); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 87 | |
| 88 | return __res1; |
| 89 | } |
| 90 | |
| 91 | int strncmp(const char *cs, const char *ct, size_t count) |
| 92 | { |
| 93 | char __res1, __res2; |
| 94 | |
| 95 | if (!count) |
| 96 | return 0; |
| 97 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 98 | __asm__("1:\t%3 = B[%0++] (Z);\n\t" /* get *cs */ |
| 99 | "%4 = B[%1++] (Z);\n\t" /* get *ct */ |
| 100 | "CC = %3 == %4;\n\t" /* compare a byte */ |
| 101 | "if ! cc jump 3f;\n\t" /* not equal, break out */ |
| 102 | "CC = %3;\n\t" /* at end of cs? */ |
| 103 | "if ! cc jump 4f;\n\t" /* yes, all done */ |
| 104 | "%2 += -1;\n\t" /* no, adjust count */ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 105 | "CC = %2 == 0;\n\t" "if ! cc jump 1b;\n" /* more to do, keep going */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 106 | "2:\t%3 = 0;\n\t" /* strings are equal */ |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 107 | "jump.s 4f;\n" "3:\t%3 = %3 - %4;\n" /* *cs - *ct */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 108 | "4:": "=a"(cs), "=a"(ct), "=da"(count), "=d"(__res1), |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 109 | "=d"(__res2) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 110 | : "0"(cs), "1"(ct), "2"(count)); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 111 | |
| 112 | return __res1; |
| 113 | } |
| 114 | |
Aubrey Li | 7158ebd | 2007-04-05 18:34:06 +0800 | [diff] [blame] | 115 | static void *dma_memcpy(void *dest, const void *src, size_t count) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 116 | { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 117 | *pMDMA_D0_IRQ_STATUS = DMA_DONE | DMA_ERR; |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 118 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 119 | /* Copy sram functions from sdram to sram */ |
| 120 | /* Setup destination start address */ |
| 121 | *pMDMA_D0_START_ADDR = (volatile void **)dest; |
| 122 | /* Setup destination xcount */ |
| 123 | *pMDMA_D0_X_COUNT = count; |
| 124 | /* Setup destination xmodify */ |
| 125 | *pMDMA_D0_X_MODIFY = 1; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 126 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 127 | /* Setup Source start address */ |
| 128 | *pMDMA_S0_START_ADDR = (volatile void **)src; |
| 129 | /* Setup Source xcount */ |
| 130 | *pMDMA_S0_X_COUNT = count; |
| 131 | /* Setup Source xmodify */ |
| 132 | *pMDMA_S0_X_MODIFY = 1; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 133 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 134 | /* Enable source DMA */ |
| 135 | *pMDMA_S0_CONFIG = (DMAEN); |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 136 | sync(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 137 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 138 | *pMDMA_D0_CONFIG = (WNR | DMAEN); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 139 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 140 | while (*pMDMA_D0_IRQ_STATUS & DMA_RUN) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 141 | *pMDMA_D0_IRQ_STATUS |= (DMA_DONE | DMA_ERR); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 142 | } |
| 143 | *pMDMA_D0_IRQ_STATUS |= (DMA_DONE | DMA_ERR); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 144 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 145 | dest += count; |
| 146 | src += count; |
| 147 | return dest; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 148 | } |
Aubrey Li | 7158ebd | 2007-04-05 18:34:06 +0800 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * memcpy - Copy one area of memory to another |
| 152 | * @dest: Where to copy to |
| 153 | * @src: Where to copy from |
| 154 | * @count: The size of the area. |
| 155 | * |
| 156 | * You should not use this function to access IO space, use memcpy_toio() |
| 157 | * or memcpy_fromio() instead. |
| 158 | */ |
| 159 | extern void *memcpy_ASM(void *dest, const void *src, size_t count); |
| 160 | void *memcpy(void *dest, const void *src, size_t count) |
| 161 | { |
| 162 | char *tmp = (char *) dest, *s = (char *) src; |
| 163 | |
| 164 | if (dcache_status()) { |
| 165 | blackfin_dcache_flush_range(src, src+count); |
| 166 | } |
| 167 | /* L1_ISRAM can only be accessed via dma */ |
| 168 | if ((tmp >= (char *)L1_ISRAM) && (tmp < (char *)L1_ISRAM_END)) { |
| 169 | /* L1 is the destination */ |
| 170 | dma_memcpy(dest,src,count); |
| 171 | } else if ((s >= (char *)L1_ISRAM) && (s < (char *)L1_ISRAM_END)) { |
| 172 | /* L1 is the source */ |
| 173 | dma_memcpy(dest,src,count); |
| 174 | |
| 175 | if (icache_status()) { |
| 176 | blackfin_icache_flush_range(dest, dest+count); |
| 177 | } |
| 178 | if (dcache_status()) { |
| 179 | blackfin_dcache_invalidate_range(dest, dest+count); |
| 180 | } |
| 181 | } else { |
| 182 | memcpy_ASM(dest,src,count); |
| 183 | } |
| 184 | return dest; |
| 185 | } |