string.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * U-boot - string.c Contains library routines.
  3. *
  4. * Copyright (c) 2005-2007 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. static void *dma_memcpy(void *dst, const void *src, size_t count)
  118. {
  119. if (dcache_status())
  120. blackfin_dcache_flush_range(src, src + count);
  121. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  122. /* Copy sram functions from sdram to sram */
  123. /* Setup destination start address */
  124. bfin_write_MDMA_D0_START_ADDR(dst);
  125. /* Setup destination xcount */
  126. bfin_write_MDMA_D0_X_COUNT(count);
  127. /* Setup destination xmodify */
  128. bfin_write_MDMA_D0_X_MODIFY(1);
  129. /* Setup Source start address */
  130. bfin_write_MDMA_S0_START_ADDR(src);
  131. /* Setup Source xcount */
  132. bfin_write_MDMA_S0_X_COUNT(count);
  133. /* Setup Source xmodify */
  134. bfin_write_MDMA_S0_X_MODIFY(1);
  135. /* Enable source DMA */
  136. bfin_write_MDMA_S0_CONFIG(DMAEN);
  137. SSYNC();
  138. bfin_write_MDMA_D0_CONFIG(WNR | DMAEN);
  139. while (bfin_read_MDMA_D0_IRQ_STATUS() & DMA_RUN)
  140. bfin_write_MDMA_D0_IRQ_STATUS(bfin_read_MDMA_D0_IRQ_STATUS() | DMA_DONE | DMA_ERR);
  141. bfin_write_MDMA_D0_IRQ_STATUS(bfin_read_MDMA_D0_IRQ_STATUS() | DMA_DONE | DMA_ERR);
  142. if (icache_status())
  143. blackfin_icache_flush_range(dst, dst + count);
  144. if (dcache_status())
  145. blackfin_dcache_invalidate_range(dst, dst + count);
  146. return dst;
  147. }
  148. /*
  149. * memcpy - Copy one area of memory to another
  150. * @dest: Where to copy to
  151. * @src: Where to copy from
  152. * @count: The size of the area.
  153. *
  154. * We need to have this wrapper in memcpy() as common code may call memcpy()
  155. * to load up L1 regions. Consider loading an ELF which has sections with
  156. * LMA's pointing to L1. The common code ELF loader will simply use memcpy()
  157. * to move the ELF's sections into the right place. We need to catch that
  158. * here and redirect to dma_memcpy().
  159. */
  160. extern void *memcpy_ASM(void *dst, const void *src, size_t count);
  161. void *memcpy(void *dst, const void *src, size_t count)
  162. {
  163. if (!count)
  164. return dst;
  165. if (addr_bfin_on_chip_mem(dst)) {
  166. /* L1 is the destination */
  167. return dma_memcpy(dst, src, count);
  168. } else if (addr_bfin_on_chip_mem(src)) {
  169. /* L1 is the source */
  170. return dma_memcpy(dst, src, count);
  171. } else
  172. /* No L1 is involved, so just call regular memcpy */
  173. return memcpy_ASM(dst, src, count);
  174. }