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