string.c 7.1 KB

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