string.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/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 MDMA1_D0_NEXT_DESC_PTR
  105. # define MDMA_D0_NEXT_DESC_PTR MDMA1_D0_NEXT_DESC_PTR
  106. # define MDMA_S0_NEXT_DESC_PTR MDMA1_S0_NEXT_DESC_PTR
  107. #endif
  108. static void dma_calc_size(unsigned long ldst, unsigned long lsrc, size_t count,
  109. unsigned long *dshift, unsigned long *bpos)
  110. {
  111. unsigned long limit;
  112. #ifdef MSIZE
  113. /* The max memory DMA memory transfer size is 32 bytes. */
  114. limit = 5;
  115. *dshift = MSIZE_P;
  116. #else
  117. /* The max memory DMA memory transfer size is 4 bytes. */
  118. limit = 2;
  119. *dshift = WDSIZE_P;
  120. #endif
  121. *bpos = min(limit, ffs(ldst | lsrc | count)) - 1;
  122. }
  123. /* This version misbehaves for count values of 0 and 2^16+.
  124. * Perhaps we should detect that ? Nowhere do we actually
  125. * use dma memcpy for those types of lengths though ...
  126. */
  127. void dma_memcpy_nocache(void *dst, const void *src, size_t count)
  128. {
  129. struct dma_register *mdma_d0 = (void *)MDMA_D0_NEXT_DESC_PTR;
  130. struct dma_register *mdma_s0 = (void *)MDMA_S0_NEXT_DESC_PTR;
  131. unsigned long ldst = (unsigned long)dst;
  132. unsigned long lsrc = (unsigned long)src;
  133. unsigned long dshift, bpos;
  134. uint32_t dsize, mod;
  135. /* Disable DMA in case it's still running (older u-boot's did not
  136. * always turn them off). Do it before the if statement below so
  137. * we can be cheap and not do a SSYNC() due to the forced abort.
  138. */
  139. bfin_write(&mdma_d0->config, 0);
  140. bfin_write(&mdma_s0->config, 0);
  141. bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
  142. /* Scratchpad cannot be a DMA source or destination */
  143. if ((lsrc >= L1_SRAM_SCRATCH && lsrc < L1_SRAM_SCRATCH_END) ||
  144. (ldst >= L1_SRAM_SCRATCH && ldst < L1_SRAM_SCRATCH_END))
  145. hang();
  146. dma_calc_size(ldst, lsrc, count, &dshift, &bpos);
  147. dsize = bpos << dshift;
  148. count >>= bpos;
  149. mod = 1 << bpos;
  150. #ifdef PSIZE
  151. /* The max memory DMA peripheral transfer size is 4 bytes. */
  152. dsize |= min(2, bpos) << PSIZE_P;
  153. #endif
  154. /* Copy sram functions from sdram to sram */
  155. /* Setup destination start address */
  156. bfin_write(&mdma_d0->start_addr, ldst);
  157. /* Setup destination xcount */
  158. bfin_write(&mdma_d0->x_count, count);
  159. /* Setup destination xmodify */
  160. bfin_write(&mdma_d0->x_modify, mod);
  161. /* Setup Source start address */
  162. bfin_write(&mdma_s0->start_addr, lsrc);
  163. /* Setup Source xcount */
  164. bfin_write(&mdma_s0->x_count, count);
  165. /* Setup Source xmodify */
  166. bfin_write(&mdma_s0->x_modify, mod);
  167. /* Enable source DMA */
  168. bfin_write(&mdma_s0->config, dsize | DMAEN);
  169. bfin_write(&mdma_d0->config, dsize | DMAEN | WNR | DI_EN);
  170. SSYNC();
  171. while (!(bfin_read(&mdma_d0->status) & DMA_DONE))
  172. continue;
  173. bfin_write(&mdma_d0->status, DMA_RUN | DMA_DONE | DMA_ERR);
  174. bfin_write(&mdma_d0->config, 0);
  175. bfin_write(&mdma_s0->config, 0);
  176. }
  177. /* We should do a dcache invalidate on the destination after the dma, but since
  178. * we lack such hardware capability, we'll flush/invalidate the destination
  179. * before the dma and bank on the idea that u-boot is single threaded.
  180. */
  181. void *dma_memcpy(void *dst, const void *src, size_t count)
  182. {
  183. if (dcache_status()) {
  184. blackfin_dcache_flush_range(src, src + count);
  185. blackfin_dcache_flush_invalidate_range(dst, dst + count);
  186. }
  187. dma_memcpy_nocache(dst, src, count);
  188. if (icache_status())
  189. blackfin_icache_flush_range(dst, dst + count);
  190. return dst;
  191. }
  192. /*
  193. * memcpy - Copy one area of memory to another
  194. * @dest: Where to copy to
  195. * @src: Where to copy from
  196. * @count: The size of the area.
  197. *
  198. * We need to have this wrapper in memcpy() as common code may call memcpy()
  199. * to load up L1 regions. Consider loading an ELF which has sections with
  200. * LMA's pointing to L1. The common code ELF loader will simply use memcpy()
  201. * to move the ELF's sections into the right place. We need to catch that
  202. * here and redirect to dma_memcpy().
  203. */
  204. extern void *memcpy_ASM(void *dst, const void *src, size_t count);
  205. void *memcpy(void *dst, const void *src, size_t count)
  206. {
  207. if (!count)
  208. return dst;
  209. #ifdef CONFIG_CMD_KGDB
  210. if (src >= (void *)SYSMMR_BASE) {
  211. if (count == 2 && (unsigned long)src % 2 == 0) {
  212. u16 mmr = bfin_read16(src);
  213. memcpy(dst, &mmr, sizeof(mmr));
  214. return dst;
  215. }
  216. if (count == 4 && (unsigned long)src % 4 == 0) {
  217. u32 mmr = bfin_read32(src);
  218. memcpy(dst, &mmr, sizeof(mmr));
  219. return dst;
  220. }
  221. /* Failed for some reason */
  222. memset(dst, 0xad, count);
  223. return dst;
  224. }
  225. if (dst >= (void *)SYSMMR_BASE) {
  226. if (count == 2 && (unsigned long)dst % 2 == 0) {
  227. u16 mmr;
  228. memcpy(&mmr, src, sizeof(mmr));
  229. bfin_write16(dst, mmr);
  230. return dst;
  231. }
  232. if (count == 4 && (unsigned long)dst % 4 == 0) {
  233. u32 mmr;
  234. memcpy(&mmr, src, sizeof(mmr));
  235. bfin_write32(dst, mmr);
  236. return dst;
  237. }
  238. /* Failed for some reason */
  239. memset(dst, 0xad, count);
  240. return dst;
  241. }
  242. #endif
  243. /* if L1 is the source or dst, use DMA */
  244. if (addr_bfin_on_chip_mem(dst) || addr_bfin_on_chip_mem(src))
  245. return dma_memcpy(dst, src, count);
  246. else
  247. /* No L1 is involved, so just call regular memcpy */
  248. return memcpy_ASM(dst, src, count);
  249. }