musbfsh_dma.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * MUSB OTG driver DMA controller abstraction
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. *
  8. * Copyright 2015 Mediatek Inc.
  9. * Marvin Lin <marvin.lin@mediatek.com>
  10. * Arvin Wang <arvin.wang@mediatek.com>
  11. * Vincent Fan <vincent.fan@mediatek.com>
  12. * Bryant Lu <bryant.lu@mediatek.com>
  13. * Yu-Chang Wang <yu-chang.wang@mediatek.com>
  14. * Macpaul Lin <macpaul.lin@mediatek.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * version 2 as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program.
  27. *
  28. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  31. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  35. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  37. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef __MUSBFSH_DMA_H__
  41. #define __MUSBFSH_DMA_H__
  42. struct musbfsh_hw_ep;
  43. /*
  44. * DMA Controller Abstraction
  45. *
  46. * DMA Controllers are abstracted to allow use of a variety of different
  47. * implementations of DMA, as allowed by the Inventra USB cores. On the
  48. * host side, usbcore sets up the DMA mappings and flushes caches; on the
  49. * peripheral side, the gadget controller driver does. Responsibilities
  50. * of a DMA controller driver include:
  51. *
  52. * - Handling the details of moving multiple USB packets
  53. * in cooperation with the Inventra USB core, including especially
  54. * the correct RX side treatment of short packets and buffer-full
  55. * states (both of which terminate transfers).
  56. *
  57. * - Knowing the correlation between dma channels and the
  58. * Inventra core's local endpoint resources and data direction.
  59. *
  60. * - Maintaining a list of allocated/available channels.
  61. *
  62. * - Updating channel status on interrupts,
  63. * whether shared with the Inventra core or separate.
  64. */
  65. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  66. #ifndef CONFIG_MUSBFSH_PIO_ONLY
  67. #define is_dma_capable() (1)
  68. #else
  69. #define is_dma_capable() (0)
  70. #endif
  71. /*
  72. * DMA channel status ... updated by the dma controller driver whenever that
  73. * status changes, and protected by the overall controller spinlock.
  74. */
  75. enum dma_channel_status {
  76. /* unallocated */
  77. MUSBFSH_DMA_STATUS_UNKNOWN,
  78. /* allocated ... but not busy, no errors */
  79. MUSBFSH_DMA_STATUS_FREE,
  80. /* busy ... transactions are active */
  81. MUSBFSH_DMA_STATUS_BUSY,
  82. /* transaction(s) aborted due to ... dma or memory bus error */
  83. MUSBFSH_DMA_STATUS_BUS_ABORT,
  84. /* transaction(s) aborted due to ... core error or USB fault */
  85. MUSBFSH_DMA_STATUS_CORE_ABORT
  86. };
  87. struct dma_controller;
  88. /**
  89. * struct dma_channel - A DMA channel.
  90. * @private_data: channel-private data
  91. * @max_len: the maximum number of bytes the channel can move in one
  92. * transaction (typically representing many USB maximum-sized packets)
  93. * @actual_len: how many bytes have been transferred
  94. * @status: current channel status (updated e.g. on interrupt)
  95. * @desired_mode: true if mode 1 is desired; false if mode 0 is desired
  96. *
  97. * channels are associated with an endpoint for the duration of at least
  98. * one usb transfer.
  99. */
  100. struct dma_channel {
  101. void *private_data;
  102. /* FIXME not void* private_data, but a dma_controller * */
  103. size_t max_len;
  104. size_t actual_len;
  105. enum dma_channel_status status;
  106. bool desired_mode;
  107. };
  108. /*
  109. * dma_channel_status - return status of dma channel
  110. * @c: the channel
  111. *
  112. * Returns the software's view of the channel status. If that status is BUSY
  113. * then it's possible that the hardware has completed (or aborted) a transfer,
  114. * so the driver needs to update that status.
  115. */
  116. static inline enum dma_channel_status dma_channel_status(struct dma_channel *c)
  117. {
  118. return (is_dma_capable() && c) ? c->status : MUSBFSH_DMA_STATUS_UNKNOWN;
  119. }
  120. /**
  121. * struct dma_controller - A DMA Controller.
  122. * @start: call this to start a DMA controller;
  123. * return 0 on success, else negative errno
  124. * @stop: call this to stop a DMA controller
  125. * return 0 on success, else negative errno
  126. * @channel_alloc: call this to allocate a DMA channel
  127. * @channel_release: call this to release a DMA channel
  128. * @channel_abort: call this to abort a pending DMA transaction,
  129. * returning it to FREE (but allocated) state
  130. *
  131. * Controllers manage dma channels.
  132. */
  133. struct dma_controller {
  134. int (*start)(struct dma_controller *);
  135. int (*stop)(struct dma_controller *);
  136. struct dma_channel *(*channel_alloc)(struct dma_controller *,
  137. struct musbfsh_hw_ep *, u8 is_tx);
  138. void (*channel_release)(struct dma_channel *);
  139. int (*channel_program)(struct dma_channel *channel,
  140. u16 maxpacket, u8 mode, dma_addr_t dma_addr,
  141. u32 length);
  142. int (*channel_abort)(struct dma_channel *);
  143. };
  144. /* called after channel_program(), may indicate a fault */
  145. extern void musbfsh_dma_completion(struct musbfsh *musb, u8 epnum, u8 transmit);
  146. extern struct dma_controller *__init
  147. musbfsh_dma_controller_create(struct musbfsh *, void __iomem *);
  148. extern void musbfsh_dma_controller_destroy(struct dma_controller *);
  149. #endif /* __MUSBFSH_DMA_H__ */