string.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * U-boot - string.c Contains library routines.
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  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. #include <common.h>
  28. #include <config.h>
  29. #include <asm/blackfin.h>
  30. #include <asm/io.h>
  31. #include <asm/mach-common/bits/dma.h>
  32. char *strcpy(char *dest, const char *src)
  33. {
  34. char *xdest = dest;
  35. char temp = 0;
  36. __asm__ __volatile__ (
  37. "1:\t%2 = B [%1++] (Z);\n\t"
  38. "B [%0++] = %2;\n\t"
  39. "CC = %2;\n\t"
  40. "if cc jump 1b (bp);\n"
  41. : "=a"(dest), "=a"(src), "=d"(temp)
  42. : "0"(dest), "1"(src), "2"(temp)
  43. : "memory");
  44. return xdest;
  45. }
  46. char *strncpy(char *dest, const char *src, size_t n)
  47. {
  48. char *xdest = dest;
  49. char temp = 0;
  50. if (n == 0)
  51. return xdest;
  52. __asm__ __volatile__ (
  53. "1:\t%3 = B [%1++] (Z);\n\t"
  54. "B [%0++] = %3;\n\t"
  55. "CC = %3;\n\t"
  56. "if ! cc jump 2f;\n\t"
  57. "%2 += -1;\n\t"
  58. "CC = %2 == 0;\n\t"
  59. "if ! cc jump 1b (bp);\n"
  60. "2:\n"
  61. : "=a"(dest), "=a"(src), "=da"(n), "=d"(temp)
  62. : "0"(dest), "1"(src), "2"(n), "3"(temp)
  63. : "memory");
  64. return xdest;
  65. }
  66. int strcmp(const char *cs, const char *ct)
  67. {
  68. char __res1, __res2;
  69. __asm__ (
  70. "1:\t%2 = B[%0++] (Z);\n\t" /* get *cs */
  71. "%3 = B[%1++] (Z);\n\t" /* get *ct */
  72. "CC = %2 == %3;\n\t" /* compare a byte */
  73. "if ! cc jump 2f;\n\t" /* not equal, break out */
  74. "CC = %2;\n\t" /* at end of cs? */
  75. "if cc jump 1b (bp);\n\t" /* no, keep going */
  76. "jump.s 3f;\n" /* strings are equal */
  77. "2:\t%2 = %2 - %3;\n" /* *cs - *ct */
  78. "3:\n"
  79. : "=a"(cs), "=a"(ct), "=d"(__res1), "=d"(__res2)
  80. : "0"(cs), "1"(ct));
  81. return __res1;
  82. }
  83. int strncmp(const char *cs, const char *ct, size_t count)
  84. {
  85. char __res1, __res2;
  86. if (!count)
  87. return 0;
  88. __asm__(
  89. "1:\t%3 = B[%0++] (Z);\n\t" /* get *cs */
  90. "%4 = B[%1++] (Z);\n\t" /* get *ct */
  91. "CC = %3 == %4;\n\t" /* compare a byte */
  92. "if ! cc jump 3f;\n\t" /* not equal, break out */
  93. "CC = %3;\n\t" /* at end of cs? */
  94. "if ! cc jump 4f;\n\t" /* yes, all done */
  95. "%2 += -1;\n\t" /* no, adjust count */
  96. "CC = %2 == 0;\n\t" "if ! cc jump 1b;\n" /* more to do, keep going */
  97. "2:\t%3 = 0;\n\t" /* strings are equal */
  98. "jump.s 4f;\n" "3:\t%3 = %3 - %4;\n" /* *cs - *ct */
  99. "4:"
  100. : "=a"(cs), "=a"(ct), "=da"(count), "=d"(__res1), "=d"(__res2)
  101. : "0"(cs), "1"(ct), "2"(count));
  102. return __res1;
  103. }
  104. #ifdef bfin_write_MDMA1_D0_IRQ_STATUS
  105. # define bfin_write_MDMA_D0_IRQ_STATUS bfin_write_MDMA1_D0_IRQ_STATUS
  106. # define bfin_write_MDMA_D0_START_ADDR bfin_write_MDMA1_D0_START_ADDR
  107. # define bfin_write_MDMA_D0_X_COUNT bfin_write_MDMA1_D0_X_COUNT
  108. # define bfin_write_MDMA_D0_X_MODIFY bfin_write_MDMA1_D0_X_MODIFY
  109. # define bfin_write_MDMA_D0_CONFIG bfin_write_MDMA1_D0_CONFIG
  110. # define bfin_write_MDMA_S0_START_ADDR bfin_write_MDMA1_S0_START_ADDR
  111. # define bfin_write_MDMA_S0_X_COUNT bfin_write_MDMA1_S0_X_COUNT
  112. # define bfin_write_MDMA_S0_X_MODIFY bfin_write_MDMA1_S0_X_MODIFY
  113. # define bfin_write_MDMA_S0_CONFIG bfin_write_MDMA1_S0_CONFIG
  114. # define bfin_write_MDMA_D0_IRQ_STATUS bfin_write_MDMA1_D0_IRQ_STATUS
  115. # define bfin_read_MDMA_D0_IRQ_STATUS bfin_read_MDMA1_D0_IRQ_STATUS
  116. #endif
  117. /* This version misbehaves for count values of 0 and 2^16+.
  118. * Perhaps we should detect that ? Nowhere do we actually
  119. * use dma memcpy for those types of lengths though ...
  120. */
  121. void dma_memcpy_nocache(void *dst, const void *src, size_t count)
  122. {
  123. uint16_t wdsize, mod;
  124. /* Disable DMA in case it's still running (older u-boot's did not
  125. * always turn them off). Do it before the if statement below so
  126. * we can be cheap and not do a SSYNC() due to the forced abort.
  127. */
  128. bfin_write_MDMA_D0_CONFIG(0);
  129. bfin_write_MDMA_S0_CONFIG(0);
  130. bfin_write_MDMA_D0_IRQ_STATUS(DMA_RUN | DMA_DONE | DMA_ERR);
  131. /* Scratchpad cannot be a DMA source or destination */
  132. if (((unsigned long)src >= L1_SRAM_SCRATCH &&
  133. (unsigned long)src < L1_SRAM_SCRATCH_END) ||
  134. ((unsigned long)dst >= L1_SRAM_SCRATCH &&
  135. (unsigned long)dst < L1_SRAM_SCRATCH_END))
  136. hang();
  137. if (((unsigned long)dst | (unsigned long)src | count) & 0x1) {
  138. wdsize = WDSIZE_8;
  139. mod = 1;
  140. } else if (((unsigned long)dst | (unsigned long)src | count) & 0x2) {
  141. wdsize = WDSIZE_16;
  142. count >>= 1;
  143. mod = 2;
  144. } else {
  145. wdsize = WDSIZE_32;
  146. count >>= 2;
  147. mod = 4;
  148. }
  149. /* Copy sram functions from sdram to sram */
  150. /* Setup destination start address */
  151. bfin_write_MDMA_D0_START_ADDR(dst);
  152. /* Setup destination xcount */
  153. bfin_write_MDMA_D0_X_COUNT(count);
  154. /* Setup destination xmodify */
  155. bfin_write_MDMA_D0_X_MODIFY(mod);
  156. /* Setup Source start address */
  157. bfin_write_MDMA_S0_START_ADDR(src);
  158. /* Setup Source xcount */
  159. bfin_write_MDMA_S0_X_COUNT(count);
  160. /* Setup Source xmodify */
  161. bfin_write_MDMA_S0_X_MODIFY(mod);
  162. /* Enable source DMA */
  163. bfin_write_MDMA_S0_CONFIG(wdsize | DMAEN);
  164. bfin_write_MDMA_D0_CONFIG(wdsize | DMAEN | WNR | DI_EN);
  165. SSYNC();
  166. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  167. continue;
  168. bfin_write_MDMA_D0_IRQ_STATUS(DMA_RUN | DMA_DONE | DMA_ERR);
  169. bfin_write_MDMA_D0_CONFIG(0);
  170. bfin_write_MDMA_S0_CONFIG(0);
  171. }
  172. /* We should do a dcache invalidate on the destination after the dma, but since
  173. * we lack such hardware capability, we'll flush/invalidate the destination
  174. * before the dma and bank on the idea that u-boot is single threaded.
  175. */
  176. void *dma_memcpy(void *dst, const void *src, size_t count)
  177. {
  178. if (dcache_status()) {
  179. blackfin_dcache_flush_range(src, src + count);
  180. blackfin_dcache_flush_invalidate_range(dst, dst + count);
  181. }
  182. dma_memcpy_nocache(dst, src, count);
  183. if (icache_status())
  184. blackfin_icache_flush_range(dst, dst + count);
  185. return dst;
  186. }
  187. /*
  188. * memcpy - Copy one area of memory to another
  189. * @dest: Where to copy to
  190. * @src: Where to copy from
  191. * @count: The size of the area.
  192. *
  193. * We need to have this wrapper in memcpy() as common code may call memcpy()
  194. * to load up L1 regions. Consider loading an ELF which has sections with
  195. * LMA's pointing to L1. The common code ELF loader will simply use memcpy()
  196. * to move the ELF's sections into the right place. We need to catch that
  197. * here and redirect to dma_memcpy().
  198. */
  199. extern void *memcpy_ASM(void *dst, const void *src, size_t count);
  200. void *memcpy(void *dst, const void *src, size_t count)
  201. {
  202. if (!count)
  203. return dst;
  204. #ifdef CONFIG_CMD_KGDB
  205. if (src >= (void *)SYSMMR_BASE) {
  206. if (count == 2 && (unsigned long)src % 2 == 0) {
  207. u16 mmr = bfin_read16(src);
  208. memcpy(dst, &mmr, sizeof(mmr));
  209. return dst;
  210. }
  211. if (count == 4 && (unsigned long)src % 4 == 0) {
  212. u32 mmr = bfin_read32(src);
  213. memcpy(dst, &mmr, sizeof(mmr));
  214. return dst;
  215. }
  216. /* Failed for some reason */
  217. memset(dst, 0xad, count);
  218. return dst;
  219. }
  220. if (dst >= (void *)SYSMMR_BASE) {
  221. if (count == 2 && (unsigned long)dst % 2 == 0) {
  222. u16 mmr;
  223. memcpy(&mmr, src, sizeof(mmr));
  224. bfin_write16(dst, mmr);
  225. return dst;
  226. }
  227. if (count == 4 && (unsigned long)dst % 4 == 0) {
  228. u32 mmr;
  229. memcpy(&mmr, src, sizeof(mmr));
  230. bfin_write32(dst, mmr);
  231. return dst;
  232. }
  233. /* Failed for some reason */
  234. memset(dst, 0xad, count);
  235. return dst;
  236. }
  237. #endif
  238. /* if L1 is the source or dst, use DMA */
  239. if (addr_bfin_on_chip_mem(dst) || addr_bfin_on_chip_mem(src))
  240. return dma_memcpy(dst, src, count);
  241. else
  242. /* No L1 is involved, so just call regular memcpy */
  243. return memcpy_ASM(dst, src, count);
  244. }