Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
dma.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_DMA_H_
14#define ZEPHYR_INCLUDE_DRIVERS_DMA_H_
15
16#include <zephyr/kernel.h>
17#include <zephyr/device.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
31
65
79
87
101
169
171#define DMA_STATUS_COMPLETE 0
173#define DMA_STATUS_BLOCK 1
175#define DMA_STATUS_HALF_COMPLETE 2
176
194typedef void (*dma_callback_t)(const struct device *dev, void *user_data,
195 uint32_t channel, int status);
196
277
297
311
313#define DMA_MAGIC 0x47494749
314
321typedef int (*dma_api_config)(const struct device *dev, uint32_t channel,
322 struct dma_config *config);
323
324#ifdef CONFIG_DMA_64BIT
325typedef int (*dma_api_reload)(const struct device *dev, uint32_t channel,
326 uint64_t src, uint64_t dst, size_t size);
327#else
328typedef int (*dma_api_reload)(const struct device *dev, uint32_t channel,
329 uint32_t src, uint32_t dst, size_t size);
330#endif
331
332typedef int (*dma_api_start)(const struct device *dev, uint32_t channel);
333
334typedef int (*dma_api_stop)(const struct device *dev, uint32_t channel);
335
336typedef int (*dma_api_suspend)(const struct device *dev, uint32_t channel);
337
338typedef int (*dma_api_resume)(const struct device *dev, uint32_t channel);
339
340typedef int (*dma_api_get_status)(const struct device *dev, uint32_t channel,
341 struct dma_status *status);
342
343typedef int (*dma_api_get_attribute)(const struct device *dev, uint32_t type, uint32_t *value);
344
357typedef bool (*dma_api_chan_filter)(const struct device *dev,
358 int channel, void *filter_param);
359
370typedef void (*dma_api_chan_release)(const struct device *dev,
371 uint32_t channel);
372
373__subsystem struct dma_driver_api {
374 dma_api_config config;
375 dma_api_reload reload;
376 dma_api_start start;
377 dma_api_stop stop;
378 dma_api_suspend suspend;
379 dma_api_resume resume;
380 dma_api_get_status get_status;
381 dma_api_get_attribute get_attribute;
382 dma_api_chan_filter chan_filter;
383 dma_api_chan_release chan_release;
384};
388
399static inline int dma_config(const struct device *dev, uint32_t channel,
400 struct dma_config *config)
401{
402 return DEVICE_API_GET(dma, dev)->config(dev, channel, config);
403}
404
417#ifdef CONFIG_DMA_64BIT
418static inline int dma_reload(const struct device *dev, uint32_t channel,
419 uint64_t src, uint64_t dst, size_t size)
420#else
421static inline int dma_reload(const struct device *dev, uint32_t channel,
422 uint32_t src, uint32_t dst, size_t size)
423#endif
424{
425 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
426
427 if (api->reload) {
428 return api->reload(dev, channel, src, dst, size);
429 }
430
431 return -ENOSYS;
432}
433
452static inline int dma_start(const struct device *dev, uint32_t channel)
453{
454 return DEVICE_API_GET(dma, dev)->start(dev, channel);
455}
456
476static inline int dma_stop(const struct device *dev, uint32_t channel)
477{
478 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
479
480 if (api->stop == NULL) {
481 return -ENOSYS;
482 }
483 return api->stop(dev, channel);
484}
485
501static inline int dma_suspend(const struct device *dev, uint32_t channel)
502{
503 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
504
505 if (api->suspend == NULL) {
506 return -ENOSYS;
507 }
508 return api->suspend(dev, channel);
509}
510
526static inline int dma_resume(const struct device *dev, uint32_t channel)
527{
528 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
529
530 if (api->resume == NULL) {
531 return -ENOSYS;
532 }
533 return api->resume(dev, channel);
534}
535
551static inline int dma_request_channel(const struct device *dev, void *filter_param)
552{
553 int i = 0;
554 int channel = -EINVAL;
555 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
556 /* dma_context shall be the first one in dev data */
557 struct dma_context *dma_ctx = (struct dma_context *)dev->data;
558
559 if (dma_ctx->magic != DMA_MAGIC) {
560 return channel;
561 }
562
563 for (i = 0; i < dma_ctx->dma_channels; i++) {
564 if (!atomic_test_and_set_bit(dma_ctx->atomic, i)) {
565 if (api->chan_filter &&
566 !api->chan_filter(dev, i, filter_param)) {
567 atomic_clear_bit(dma_ctx->atomic, i);
568 continue;
569 }
570 channel = i;
571 break;
572 }
573 }
574
575 return channel;
576}
577
591static inline void dma_release_channel(const struct device *dev, uint32_t channel)
592{
593 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
594 struct dma_context *dma_ctx = (struct dma_context *)dev->data;
595
596 if (dma_ctx->magic != DMA_MAGIC) {
597 return;
598 }
599
600 if ((int)channel < dma_ctx->dma_channels) {
601 if (api->chan_release) {
602 api->chan_release(dev, channel);
603 }
604
605 atomic_clear_bit(dma_ctx->atomic, channel);
606 }
607
608}
609
621static inline int dma_chan_filter(const struct device *dev, int channel, void *filter_param)
622{
623 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
624
625 if (api->chan_filter) {
626 return api->chan_filter(dev, channel, filter_param);
627 }
628
629 return -ENOSYS;
630}
631
647static inline int dma_get_status(const struct device *dev, uint32_t channel,
648 struct dma_status *stat)
649{
650 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
651
652 if (api->get_status) {
653 return api->get_status(dev, channel, stat);
654 }
655
656 return -ENOSYS;
657}
658
675static inline int dma_get_attribute(const struct device *dev, uint32_t type, uint32_t *value)
676{
677 const struct dma_driver_api *api = DEVICE_API_GET(dma, dev);
678
679 if (api->get_attribute) {
680 return api->get_attribute(dev, type, value);
681 }
682
683 return -ENOSYS;
684}
685
700{
701 /* Check boundaries (max supported width is 32 Bytes) */
702 if (size < 1 || size > 32) {
703 return 0; /* Zero is the default (8 Bytes) */
704 }
705
706 /* Ensure size is a power of 2 */
707 if (!is_power_of_two(size)) {
708 return 0; /* Zero is the default (8 Bytes) */
709 }
710
711 /* Convert to bit pattern for writing to a register */
712 return find_msb_set(size);
713}
714
729{
730 /* Check boundaries (max supported burst length is 256) */
731 if (burst < 1 || burst > 256) {
732 return 0; /* Zero is the default (1 burst length) */
733 }
734
735 /* Ensure burst is a power of 2 */
736 if (!(burst & (burst - 1))) {
737 return 0; /* Zero is the default (1 burst length) */
738 }
739
740 /* Convert to bit pattern for writing to a register */
741 return find_msb_set(burst);
742}
743
753#define DMA_BUF_ADDR_ALIGNMENT(node) DT_PROP(node, dma_buf_addr_alignment)
754
764#define DMA_BUF_SIZE_ALIGNMENT(node) DT_PROP(node, dma_buf_size_alignment)
765
772#define DMA_COPY_ALIGNMENT(node) DT_PROP(node, dma_copy_alignment)
773
777
778#ifdef __cplusplus
779}
780#endif
781
782#endif /* ZEPHYR_INCLUDE_DRIVERS_DMA_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
static ALWAYS_INLINE unsigned int find_msb_set(uint32_t op)
find most significant bit set in a 32-bit word
Definition ffs.h:32
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition atomic.h:224
static bool atomic_test_and_set_bit(atomic_t *target, int bit)
Atomically set a bit and test it.
Definition atomic.h:178
dma_attribute_type
DMA attributes.
Definition dma.h:91
static int dma_resume(const struct device *dev, uint32_t channel)
Resume a DMA channel transfer.
Definition dma.h:526
static int dma_get_status(const struct device *dev, uint32_t channel, struct dma_status *stat)
get current runtime status of DMA transfer
Definition dma.h:647
static int dma_reload(const struct device *dev, uint32_t channel, uint32_t src, uint32_t dst, size_t size)
Reload buffer(s) for a DMA channel.
Definition dma.h:421
static int dma_get_attribute(const struct device *dev, uint32_t type, uint32_t *value)
get attribute of a dma controller
Definition dma.h:675
static int dma_chan_filter(const struct device *dev, int channel, void *filter_param)
DMA channel filter.
Definition dma.h:621
static int dma_config(const struct device *dev, uint32_t channel, struct dma_config *config)
Configure individual channel for DMA transfer.
Definition dma.h:399
static void dma_release_channel(const struct device *dev, uint32_t channel)
release DMA channel.
Definition dma.h:591
void(* dma_callback_t)(const struct device *dev, void *user_data, uint32_t channel, int status)
Callback function for DMA transfer completion.
Definition dma.h:194
static int dma_suspend(const struct device *dev, uint32_t channel)
Suspend a DMA channel transfer.
Definition dma.h:501
static uint32_t dma_burst_index(uint32_t burst)
Look-up generic burst index to be used in registers.
Definition dma.h:728
static int dma_request_channel(const struct device *dev, void *filter_param)
request DMA channel.
Definition dma.h:551
static uint32_t dma_width_index(uint32_t size)
Look-up generic width index to be used in registers.
Definition dma.h:699
dma_channel_filter
DMA channel attributes.
Definition dma.h:83
#define DMA_MAGIC
Magic code to identify context content.
Definition dma.h:313
dma_channel_direction
DMA channel direction.
Definition dma.h:35
static int dma_start(const struct device *dev, uint32_t channel)
Enables DMA channel and starts the transfer, the channel must be configured beforehand.
Definition dma.h:452
static int dma_stop(const struct device *dev, uint32_t channel)
Stops the DMA transfer and disables the channel.
Definition dma.h:476
dma_addr_adj
DMA address adjustment.
Definition dma.h:71
@ DMA_ATTR_BUFFER_ADDRESS_ALIGNMENT
Required alignment of buffer addresses, in bytes.
Definition dma.h:93
@ DMA_ATTR_COPY_ALIGNMENT
Required alignment of memory-to-memory copies, in bytes.
Definition dma.h:97
@ DMA_ATTR_BUFFER_SIZE_ALIGNMENT
Required alignment of buffer sizes, in bytes.
Definition dma.h:95
@ DMA_ATTR_MAX_BLOCK_COUNT
Maximum number of blocks in a single transfer.
Definition dma.h:99
@ DMA_CHANNEL_NORMAL
Normal DMA channel.
Definition dma.h:84
@ DMA_CHANNEL_PERIODIC
Channel that can be triggered by periodic sources.
Definition dma.h:85
@ DMA_CHANNEL_DIRECTION_PRIV_START
This and higher values are dma controller or soc specific.
Definition dma.h:58
@ MEMORY_TO_PERIPHERAL
Memory to peripheral.
Definition dma.h:39
@ MEMORY_TO_MEMORY
Memory to memory.
Definition dma.h:37
@ PERIPHERAL_TO_MEMORY
Peripheral to memory.
Definition dma.h:41
@ MEMORY_TO_HOST
Memory to host.
Definition dma.h:47
@ HOST_TO_MEMORY
Host to memory.
Definition dma.h:45
@ DMA_CHANNEL_DIRECTION_MAX
Maximum allowed value (3 bit field!).
Definition dma.h:63
@ PERIPHERAL_TO_PERIPHERAL
Peripheral to peripheral.
Definition dma.h:43
@ DMA_CHANNEL_DIRECTION_COMMON_COUNT
Number of all common channel directions.
Definition dma.h:52
@ DMA_ADDR_ADJ_DECREMENT
Decrement the address.
Definition dma.h:75
@ DMA_ADDR_ADJ_INCREMENT
Increment the address.
Definition dma.h:73
@ DMA_ADDR_ADJ_NO_CHANGE
No change the address.
Definition dma.h:77
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition util.h:581
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
Public kernel APIs.
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
void * data
Address of the device instance private data.
Definition device.h:523
DMA block configuration structure.
Definition dma.h:109
uint32_t dest_scatter_interval
Address adjustment at scatter boundary.
Definition dma.h:124
uint32_t source_gather_interval
Address adjustment at gather boundary.
Definition dma.h:122
uint32_t block_size
Number of bytes to be transferred for this block.
Definition dma.h:130
uint16_t source_reload_en
Reload source address at the end of block transfer.
Definition dma.h:154
uint16_t dest_scatter_en
Enable destination scattering when set to 1.
Definition dma.h:136
uint16_t dest_reload_en
Reload destination address at the end of block transfer.
Definition dma.h:156
uint16_t fifo_mode_control
FIFO fill before starting transfer, HW specific meaning.
Definition dma.h:158
uint16_t source_gather_count
Continuous transfer count between gather boundaries.
Definition dma.h:128
uint16_t source_addr_adj
Source address adjustment option.
Definition dma.h:144
struct dma_block_config * next_block
Pointer to next block in a transfer list.
Definition dma.h:132
uint32_t dest_address
block starting address at destination
Definition dma.h:119
uint32_t source_address
block starting address at source
Definition dma.h:117
uint16_t source_gather_en
Enable source gathering when set to 1.
Definition dma.h:134
uint16_t dest_addr_adj
Destination address adjustment.
Definition dma.h:152
uint16_t dest_scatter_count
Continuous transfer count between scatter boundaries.
Definition dma.h:126
uint16_t flow_control_mode
Transfer flow control mode.
Definition dma.h:165
DMA configuration structure.
Definition dma.h:201
uint32_t half_complete_callback_en
enable half completion callback when set to 1
Definition dma.h:217
uint32_t channel_priority
Channel priority for arbitration, HW specific.
Definition dma.h:249
uint32_t source_handshake
Source handshake, HW specific.
Definition dma.h:238
uint32_t complete_callback_en
Completion callback enable.
Definition dma.h:224
uint32_t error_callback_dis
Error callback disable.
Definition dma.h:231
void * user_data
Optional attached user data for callbacks.
Definition dma.h:273
dma_callback_t dma_callback
Optional callback for completion and error events.
Definition dma.h:275
uint32_t source_chaining_en
Source chaining enable, HW specific.
Definition dma.h:251
uint32_t dest_chaining_en
Destination chaining enable, HW specific.
Definition dma.h:253
uint32_t dma_slot
Which peripheral and direction, HW specific.
Definition dma.h:203
uint32_t channel_direction
Direction the transfers are occurring.
Definition dma.h:215
uint32_t source_data_size
Width of source data (in bytes).
Definition dma.h:261
uint32_t dest_burst_length
Destination burst length in bytes.
Definition dma.h:267
struct dma_block_config * head_block
Pointer to the first block in the transfer list.
Definition dma.h:271
uint32_t linked_channel
Linked channel, HW specific.
Definition dma.h:255
uint32_t source_burst_length
Source burst length in bytes.
Definition dma.h:265
uint32_t block_count
Number of blocks in transfer list.
Definition dma.h:269
uint32_t dest_data_size
Width of destination data (in bytes).
Definition dma.h:263
uint32_t dest_handshake
Destination handshake, HW specific.
Definition dma.h:245
uint32_t cyclic
Cyclic transfer list, HW specific.
Definition dma.h:257
DMA context structure Note: the dma_context shall be the first member of DMA client driver Data,...
Definition dma.h:303
int32_t magic
magic code to identify the context
Definition dma.h:305
atomic_t * atomic
atomic holding bit flags for each channel to mark as used/unused
Definition dma.h:309
int dma_channels
number of dma channels
Definition dma.h:307
DMA runtime status structure.
Definition dma.h:281
uint32_t free
Available buffers space, HW specific.
Definition dma.h:289
uint32_t pending_length
Pending length to be transferred in bytes, HW specific.
Definition dma.h:287
bool busy
Is the current DMA transfer busy or idle.
Definition dma.h:283
uint64_t total_copied
Total copied, HW specific.
Definition dma.h:295
uint32_t write_position
Write position in circular DMA buffer, HW specific.
Definition dma.h:291
enum dma_channel_direction dir
Direction for the transfer.
Definition dma.h:285
uint32_t read_position
Read position in circular DMA buffer, HW specific.
Definition dma.h:293
Definition stat.h:57