bouncebuf.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Generic bounce buffer implementation
  3. *
  4. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <comdef.h>
  9. #include <bouncebuf.h>
  10. //unsigned int mmc_data_buf[0x1400] = {0};// 20K
  11. #if 0
  12. static int addr_aligned(struct bounce_buffer *state)
  13. {
  14. const u32 align_mask = ARCH_DMA_MINALIGN - 1;
  15. /* Check if start is aligned */
  16. if ((u32)state->user_buffer & align_mask) {
  17. //printf("Unaligned buffer address %p\n", state->user_buffer);
  18. return 0;
  19. }
  20. /* Check if length is aligned */
  21. if (state->len != state->len_aligned) {
  22. //printf("Unaligned buffer length %zu\n", state->len);
  23. return 0;
  24. }
  25. /* Aligned */
  26. return 1;
  27. }
  28. #endif
  29. int bounce_buffer_start(struct bounce_buffer *state, void *data,
  30. unsigned int len, unsigned int flags)
  31. {
  32. state->user_buffer = data;
  33. state->bounce_buffer = data;
  34. state->len = len;
  35. state->len_aligned = roundup(len, 32);
  36. state->flags = flags;
  37. #if 0//libo
  38. if (!addr_aligned(state)) {
  39. state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
  40. state->len_aligned);
  41. if (!state->bounce_buffer)
  42. return -1;
  43. if (state->flags & GEN_BB_READ)
  44. memcpy(state->bounce_buffer, state->user_buffer,
  45. state->len);
  46. }
  47. /*
  48. * Flush data to RAM so DMA reads can pick it up,
  49. * and any CPU writebacks don't race with DMA writes
  50. */
  51. flush_dcache_range((unsigned long)state->bounce_buffer,
  52. (unsigned long)(state->bounce_buffer) +
  53. state->len_aligned);
  54. #endif
  55. return 0;
  56. }
  57. #if 0
  58. int bounce_buffer_stop(struct bounce_buffer *state)
  59. {
  60. if (state->flags & GEN_BB_WRITE) {
  61. /* Invalidate cache so that CPU can see any newly DMA'd data */
  62. invalidate_dcache_range((unsigned long)state->bounce_buffer,
  63. (unsigned long)(state->bounce_buffer) +
  64. state->len_aligned);
  65. }
  66. if (state->bounce_buffer == state->user_buffer)
  67. return 0;
  68. if (state->flags & GEN_BB_WRITE)
  69. sys_memcpy(state->user_buffer, state->bounce_buffer, state->len);
  70. //free(state->bounce_buffer);
  71. return 0;
  72. }
  73. #endif