blob: 6ff591977b1445f46d1c80ab74b53c2821417c83 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Mugunthan V Na0594ce2016-02-15 15:31:37 +05302/*
Álvaro Fernández Rojas27ab27f2018-11-28 19:17:50 +01003 * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
4 * Copyright (C) 2015 - 2018 Texas Instruments Incorporated <www.ti.com>
5 * Written by Mugunthan V N <mugunthanvnm@ti.com>
6 *
Mugunthan V Na0594ce2016-02-15 15:31:37 +05307 */
8
9#ifndef _DMA_H_
10#define _DMA_H_
11
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Álvaro Fernández Rojas27ab27f2018-11-28 19:17:50 +010013#include <linux/errno.h>
14#include <linux/types.h>
15
Mugunthan V Na0594ce2016-02-15 15:31:37 +053016/*
17 * enum dma_direction - dma transfer direction indicator
18 * @DMA_MEM_TO_MEM: Memcpy mode
19 * @DMA_MEM_TO_DEV: From Memory to Device
20 * @DMA_DEV_TO_MEM: From Device to Memory
21 * @DMA_DEV_TO_DEV: From Device to Device
22 */
23enum dma_direction {
24 DMA_MEM_TO_MEM,
25 DMA_MEM_TO_DEV,
26 DMA_DEV_TO_MEM,
27 DMA_DEV_TO_DEV,
28};
29
30#define DMA_SUPPORTS_MEM_TO_MEM BIT(0)
31#define DMA_SUPPORTS_MEM_TO_DEV BIT(1)
32#define DMA_SUPPORTS_DEV_TO_MEM BIT(2)
33#define DMA_SUPPORTS_DEV_TO_DEV BIT(3)
34
35/*
Mugunthan V Na0594ce2016-02-15 15:31:37 +053036 * struct dma_dev_priv - information about a device used by the uclass
37 *
38 * @supported: mode of transfers that DMA can support, should be
39 * one/multiple of DMA_SUPPORTS_*
40 */
41struct dma_dev_priv {
42 u32 supported;
43};
44
Álvaro Fernández Rojas27ab27f2018-11-28 19:17:50 +010045#ifdef CONFIG_DMA_CHANNELS
46/**
47 * A DMA is a feature of computer systems that allows certain hardware
48 * subsystems to access main system memory, independent of the CPU.
49 * DMA channels are typically generated externally to the HW module
50 * consuming them, by an entity this API calls a DMA provider. This API
51 * provides a standard means for drivers to enable and disable DMAs, and to
52 * copy, send and receive data using DMA.
53 *
54 * A driver that implements UCLASS_DMA is a DMA provider. A provider will
55 * often implement multiple separate DMAs, since the hardware it manages
56 * often has this capability. dma_uclass.h describes the interface which
57 * DMA providers must implement.
58 *
59 * DMA consumers/clients are the HW modules driven by the DMA channels. This
60 * header file describes the API used by drivers for those HW modules.
61 *
62 * DMA consumer DMA_MEM_TO_DEV (transmit) usage example (based on networking).
63 * Note. dma_send() is sync operation always - it'll start transfer and will
64 * poll for it to complete:
65 * - get/request dma channel
66 * struct dma dma_tx;
67 * ret = dma_get_by_name(common->dev, "tx0", &dma_tx);
68 * if (ret) ...
69 *
70 * - enable dma channel
71 * ret = dma_enable(&dma_tx);
72 * if (ret) ...
73 *
74 * - dma transmit DMA_MEM_TO_DEV.
75 * struct ti_drv_packet_data packet_data;
76 *
77 * packet_data.opt1 = val1;
78 * packet_data.opt2 = val2;
79 * ret = dma_send(&dma_tx, packet, length, &packet_data);
80 * if (ret) ..
81 *
82 * DMA consumer DMA_DEV_TO_MEM (receive) usage example (based on networking).
83 * Note. dma_receive() is sync operation always - it'll start transfer
84 * (if required) and will poll for it to complete (or for any previously
85 * configured dev2mem transfer to complete):
86 * - get/request dma channel
87 * struct dma dma_rx;
88 * ret = dma_get_by_name(common->dev, "rx0", &dma_rx);
89 * if (ret) ...
90 *
91 * - enable dma channel
92 * ret = dma_enable(&dma_rx);
93 * if (ret) ...
94 *
95 * - dma receive DMA_DEV_TO_MEM.
96 * struct ti_drv_packet_data packet_data;
97 *
98 * len = dma_receive(&dma_rx, (void **)packet, &packet_data);
99 * if (ret < 0) ...
100 *
101 * DMA consumer DMA_DEV_TO_MEM (receive) zero-copy usage example (based on
102 * networking). Networking subsystem allows to configure and use few receive
103 * buffers (dev2mem), as Networking RX DMA channels usually implemented
104 * as streaming interface
105 * - get/request dma channel
106 * struct dma dma_rx;
107 * ret = dma_get_by_name(common->dev, "rx0", &dma_rx);
108 * if (ret) ...
109 *
110 * for (i = 0; i < RX_DESC_NUM; i++) {
111 * ret = dma_prepare_rcv_buf(&dma_rx,
112 * net_rx_packets[i],
113 * RX_BUF_SIZE);
114 * if (ret) ...
115 * }
116 *
117 * - enable dma channel
118 * ret = dma_enable(&dma_rx);
119 * if (ret) ...
120 *
121 * - dma receive DMA_DEV_TO_MEM.
122 * struct ti_drv_packet_data packet_data;
123 *
124 * len = dma_receive(&dma_rx, (void **)packet, &packet_data);
125 * if (ret < 0) ..
126 *
127 * -- process packet --
128 *
129 * - return buffer back to DAM channel
130 * ret = dma_prepare_rcv_buf(&dma_rx,
131 * net_rx_packets[rx_next],
132 * RX_BUF_SIZE);
133 */
134
135struct udevice;
136
137/**
138 * struct dma - A handle to (allowing control of) a single DMA.
139 *
140 * Clients provide storage for DMA handles. The content of the structure is
141 * managed solely by the DMA API and DMA drivers. A DMA struct is
142 * initialized by "get"ing the DMA struct. The DMA struct is passed to all
143 * other DMA APIs to identify which DMA channel to operate upon.
144 *
145 * @dev: The device which implements the DMA channel.
146 * @id: The DMA channel ID within the provider.
147 *
148 * Currently, the DMA API assumes that a single integer ID is enough to
149 * identify and configure any DMA channel for any DMA provider. If this
150 * assumption becomes invalid in the future, the struct could be expanded to
151 * either (a) add more fields to allow DMA providers to store additional
152 * information, or (b) replace the id field with an opaque pointer, which the
153 * provider would dynamically allocated during its .of_xlate op, and process
154 * during is .request op. This may require the addition of an extra op to clean
155 * up the allocation.
156 */
157struct dma {
158 struct udevice *dev;
159 /*
160 * Written by of_xlate. We assume a single id is enough for now. In the
161 * future, we might add more fields here.
162 */
163 unsigned long id;
164};
165
166# if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DMA)
167/**
168 * dma_get_by_index - Get/request a DMA by integer index.
169 *
170 * This looks up and requests a DMA. The index is relative to the client
171 * device; each device is assumed to have n DMAs associated with it somehow,
172 * and this function finds and requests one of them. The mapping of client
173 * device DMA indices to provider DMAs may be via device-tree properties,
174 * board-provided mapping tables, or some other mechanism.
175 *
176 * @dev: The client device.
177 * @index: The index of the DMA to request, within the client's list of
178 * DMA channels.
179 * @dma: A pointer to a DMA struct to initialize.
180 * @return 0 if OK, or a negative error code.
181 */
182int dma_get_by_index(struct udevice *dev, int index, struct dma *dma);
183
184/**
185 * dma_get_by_name - Get/request a DMA by name.
186 *
187 * This looks up and requests a DMA. The name is relative to the client
188 * device; each device is assumed to have n DMAs associated with it somehow,
189 * and this function finds and requests one of them. The mapping of client
190 * device DMA names to provider DMAs may be via device-tree properties,
191 * board-provided mapping tables, or some other mechanism.
192 *
193 * @dev: The client device.
194 * @name: The name of the DMA to request, within the client's list of
195 * DMA channels.
196 * @dma: A pointer to a DMA struct to initialize.
197 * @return 0 if OK, or a negative error code.
198 */
199int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma);
200# else
201static inline int dma_get_by_index(struct udevice *dev, int index,
202 struct dma *dma)
203{
204 return -ENOSYS;
205}
206
207static inline int dma_get_by_name(struct udevice *dev, const char *name,
208 struct dma *dma)
209{
210 return -ENOSYS;
211}
212# endif
213
214/**
215 * dma_request - Request a DMA by provider-specific ID.
216 *
217 * This requests a DMA using a provider-specific ID. Generally, this function
218 * should not be used, since dma_get_by_index/name() provide an interface that
219 * better separates clients from intimate knowledge of DMA providers.
220 * However, this function may be useful in core SoC-specific code.
221 *
222 * @dev: The DMA provider device.
223 * @dma: A pointer to a DMA struct to initialize. The caller must
224 * have already initialized any field in this struct which the
225 * DMA provider uses to identify the DMA channel.
226 * @return 0 if OK, or a negative error code.
227 */
228int dma_request(struct udevice *dev, struct dma *dma);
229
230/**
231 * dma_free - Free a previously requested DMA.
232 *
233 * @dma: A DMA struct that was previously successfully requested by
234 * dma_request/get_by_*().
235 * @return 0 if OK, or a negative error code.
236 */
237int dma_free(struct dma *dma);
238
239/**
240 * dma_enable() - Enable (turn on) a DMA channel.
241 *
242 * @dma: A DMA struct that was previously successfully requested by
243 * dma_request/get_by_*().
244 * @return zero on success, or -ve error code.
245 */
246int dma_enable(struct dma *dma);
247
248/**
249 * dma_disable() - Disable (turn off) a DMA channel.
250 *
251 * @dma: A DMA struct that was previously successfully requested by
252 * dma_request/get_by_*().
253 * @return zero on success, or -ve error code.
254 */
255int dma_disable(struct dma *dma);
256
257/**
258 * dma_prepare_rcv_buf() - Prepare/add receive DMA buffer.
259 *
260 * It allows to implement zero-copy async DMA_DEV_TO_MEM (receive) transactions
261 * if supported by DMA providers.
262 *
263 * @dma: A DMA struct that was previously successfully requested by
264 * dma_request/get_by_*().
265 * @dst: The receive buffer pointer.
266 * @size: The receive buffer size
267 * @return zero on success, or -ve error code.
268 */
269int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size);
270
271/**
272 * dma_receive() - Receive a DMA transfer.
273 *
274 * @dma: A DMA struct that was previously successfully requested by
275 * dma_request/get_by_*().
276 * @dst: The destination pointer.
277 * @metadata: DMA driver's channel specific data
278 * @return length of received data on success, or zero - no data,
279 * or -ve error code.
280 */
281int dma_receive(struct dma *dma, void **dst, void *metadata);
282
283/**
284 * dma_send() - Send a DMA transfer.
285 *
286 * @dma: A DMA struct that was previously successfully requested by
287 * dma_request/get_by_*().
288 * @src: The source pointer.
289 * @len: Length of the data to be sent (number of bytes).
290 * @metadata: DMA driver's channel specific data
291 * @return zero on success, or -ve error code.
292 */
293int dma_send(struct dma *dma, void *src, size_t len, void *metadata);
Vignesh Raghavendrab8a4dd22019-12-04 22:17:20 +0530294
295/**
296 * dma_get_cfg() - Get DMA channel configuration for client's use
297 *
298 * @dma: The DMA Channel to manipulate
299 * @cfg_id: DMA provider specific ID to identify what
300 * configuration data client needs
301 * @cfg_data: Pointer to store pointer to DMA driver specific
302 * configuration data for the given cfg_id (output param)
303 * @return zero on success, or -ve error code.
304 */
305int dma_get_cfg(struct dma *dma, u32 cfg_id, void **cfg_data);
Álvaro Fernández Rojas27ab27f2018-11-28 19:17:50 +0100306#endif /* CONFIG_DMA_CHANNELS */
307
Vignesh Raghavendra1e373302019-11-15 17:00:42 +0530308#if CONFIG_IS_ENABLED(DMA)
Mugunthan V Na0594ce2016-02-15 15:31:37 +0530309/*
310 * dma_get_device - get a DMA device which supports transfer
311 * type of transfer_type
312 *
313 * @transfer_type - transfer type should be one/multiple of
314 * DMA_SUPPORTS_*
315 * @devp - udevice pointer to return the found device
316 * @return - will return on success and devp will hold the
317 * pointer to the device
318 */
319int dma_get_device(u32 transfer_type, struct udevice **devp);
320
321/*
322 * dma_memcpy - try to use DMA to do a mem copy which will be
323 * much faster than CPU mem copy
324 *
325 * @dst - destination pointer
326 * @src - souce pointer
327 * @len - data length to be copied
328 * @return - on successful transfer returns no of bytes
329 transferred and on failure return error code.
330 */
331int dma_memcpy(void *dst, void *src, size_t len);
Vignesh Raghavendra1e373302019-11-15 17:00:42 +0530332#else
333static inline int dma_get_device(u32 transfer_type, struct udevice **devp)
334{
335 return -ENOSYS;
336}
Mugunthan V Na0594ce2016-02-15 15:31:37 +0530337
Vignesh Raghavendra1e373302019-11-15 17:00:42 +0530338static inline int dma_memcpy(void *dst, void *src, size_t len)
339{
340 return -ENOSYS;
341}
342#endif /* CONFIG_DMA */
Mugunthan V Na0594ce2016-02-15 15:31:37 +0530343#endif /* _DMA_H_ */