KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
dmac.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dmac.h
4 Copyright (C) 2024 Paul Cercueil
5
6*/
7
8/** \file arch/dmac.h
9 \brief SH4 DMA Controller API
10 \ingroup system_dmac
11
12 This header provies an API to use the DMA controller of the SH4.
13
14 \author Paul Cercueil
15*/
16
17#ifndef __ARCH_DMAC_H
18#define __ARCH_DMAC_H
19
20#include <sys/cdefs.h>
21__BEGIN_DECLS
22
23#include <stdint.h>
24#include <stdbool.h>
25
26/** \defgroup dmac DMA Controller API
27 \brief API to use the SH4's DMA Controller
28 \ingroup system
29
30 This API can be used to program DMA transfers from memory to memory,
31 from hardware to memory or from memory to hardware.
32
33 @{
34*/
35
36/** \brief DMA callback type.
37 \ingroup dmac
38
39 Function type for DMA callbacks.
40 Those registered callbacks will be called when a DMA transfer completes.
41 They will be called in an interrupt context, so don't try anything funny.
42*/
43typedef void (*dma_callback_t)(void *data);
44
45/** \brief DMA channel enum.
46 \ingroup dmac
47
48 Represents one of the 4 DMA channels available on the SH4.
49*/
50typedef enum dma_channel {
51 DMA_CHANNEL_0, /**< Channel #0: On Dreamcast, reserved for hardware. */
52 DMA_CHANNEL_1, /**< Channel #1: External, hardware or mem-to-mem requests. */
53 DMA_CHANNEL_2, /**< Channel #2: on Dreamcast, reserved for PVR use. */
54 DMA_CHANNEL_3, /**< Channel #3: mem-to-mem requests only. */
56
57/** \brief DMA request.
58 \ingroup dmac
59
60 List of the possible DMA requests.
61
62 "Auto" requests are started as soon as the DMA transfer is programmed to
63 the DMA controller. On the other hand, SCI/SCIF/TMU2 requests are only
64 started when the given hardware event occurs.
65
66 "External" requests are controlled by external pins of the SH4 CPU. These
67 can be wired to other parts of the mother board.
68*/
85
86/** \brief DMA unit size.
87 \ingroup dmac
88
89 The unit size controls the granularity at which the DMA will transfer data.
90 For instance, copying data to a 16-bit bus will require a 16-bit unit size.
91
92 For memory-to-memory transfers, it is recommended to use 32-byte transfers
93 for the maximum speed.
94*/
102
103/** \brief DMA address mode.
104 \ingroup dmac
105
106 The "address mode" specifies how the source or destination address of a DMA
107 transfer is modified as the transfer goes on. It is only valid when the DMA
108 transfer is configured as pointing to memory for that source or destination.
109*/
110typedef enum dma_addrmode {
111 DMA_ADDRMODE_FIXED, /**< The source/destination address is not modified. */
112 DMA_ADDRMODE_INCREMENT, /**< The source/destination address is incremented. */
113 DMA_ADDRMODE_DECREMENT, /**< The source/destination address is decremented. */
115
116/** \brief DMA transmit mode.
117 \ingroup dmac
118
119 In "Cycle steal" mode, the DMA controller will release the bus at the end of
120 each transfer unit (configured by the unit size). This allows the CPU to
121 access the bus if it needs to.
122
123 In "Burst" mode, the DMA controller will hold the bus until the DMA transfer
124 completes.
125*/
130
131/** \brief DMA transfer configuration.
132 \ingroup dmac
133
134 This structure represents the configuration used for a given DMA channel.
135*/
136typedef struct dma_config {
137 dma_channel_t channel; /**< DMA channel used for the transfer. */
138 dma_request_t request; /**< DMA request type. */
139 dma_unitsize_t unit_size; /**< Unit size used for the DMA transfer. */
140 dma_addrmode_t src_mode, dst_mode; /**< Source/destination address mode. */
141 dma_transmitmode_t transmit_mode; /**< DMA Transfer transmit mode. */
142 dma_callback_t callback; /**< Optional callback function for
143 end-of-transfer notification. */
145
146/** \brief DMA address.
147 \ingroup dmac
148
149 This type represents an address that can be used for DMA transfers.
150*/
151typedef uint32_t dma_addr_t;
152
153/** \brief Convert a hardware address to a DMA address.
154 \ingroup dmac
155
156 This function will convert a hardware address (pointing to a device's FIFO,
157 or to one of the various mapped memories) to an address suitable for use
158 with the DMA controller.
159
160 \param hw_addr The hardware address.
161 \return The converted DMA address.
162*/
163dma_addr_t hw_to_dma_addr(uintptr_t hw_addr);
164
165/** \brief Prepare a source memory buffer for a DMA transfer.
166 \ingroup dmac
167
168 This function will flush the data cache for the memory area covered by the
169 buffer to ensure memory coherency, and return an address suitable for use
170 with the DMA controller.
171
172 \param ptr A pointer to the source buffer.
173 \param len The size in bytes of the source buffer.
174 \return The converted DMA address.
175
176 \sa dma_map_dst()
177*/
178dma_addr_t dma_map_src(const void *ptr, size_t len);
179
180/** \brief Prepare a destination memory buffer for a DMA transfer.
181 \ingroup dmac
182
183 This function will invalidate the data cache for the memory area covered by
184 the buffer to ensure memory coherency, and return an address suitable for
185 use with the DMA controller.
186
187 \param ptr A pointer to the destination buffer.
188 \param len The size in bytes of the destination buffer.
189 \return The converted DMA address.
190
191 \sa dma_map_src()
192*/
193dma_addr_t dma_map_dst(void *ptr, size_t len);
194
195/** \brief Program a DMA transfer.
196 \ingroup dmac
197
198 This function will program a DMA transfer using the specified configuration,
199 source/destination addresses and transfer length.
200
201 It will return as soon as the DMA transfer is programmed, which means that
202 it will not work for the DMA transfer to complete before returning.
203
204 \param cfg A pointer to the configuration structure.
205 \param dst The destination address, if targetting memory;
206 can be 0 otherwise.
207 \param src The source address, if targetting memory;
208 can be 0 otherwise.
209 \param len The size in bytes of the DMA transfer.
210 \param cb_data The parameter that will be passed to the
211 end-of-transfer callback, if a callback has been
212 specified in the configuration structure.
213 \retval 0 On success.
214 \retval -1 On error.
215
216 \sa dma_wait_complete()
217*/
219 size_t len, void *cb_data);
220
221/** \brief Wait for a DMA transfer to complete
222 \ingroup dmac
223
224 This function will block until any previously programmed DMA transfer for
225 the given DMA channel has completed.
226
227 \param channel The DMA channel to wait for.
228*/
230
231/** \brief Check if a DMA transfer is running
232 \ingroup dmac
233
234 This function will return true if a DMA transfer is running for the given
235 DMA channel.
236
237 \param channel The DMA channel to check.
238 \return True if a DMA transfer is running, false otherwise.
239*/
241
242/** \brief Get the remaining size of a DMA transfer
243 \ingroup dmac
244
245 This function will return the number of bytes remaining to transfer, if a
246 transfer was previously programmed.
247
248 \param channel The DMA channel to wait for.
249 \return The number of bytes remaining to transfer, or zero
250 if the previous transfer completed.
251*/
253
254/** \brief Abort a DMA transfer
255 \ingroup dmac
256
257 This function will abort a DMA transfer for the given DMA channel.
258
259 \param channel The DMA channel to abort.
260*/
262
263/** @} */
264
265__END_DECLS
266
267#endif /* __ARCH_DMAC_H */
dma_channel_t
DMA channel enum.
Definition dmac.h:50
void dma_transfer_abort(dma_channel_t channel)
Abort a DMA transfer.
int dma_transfer(const dma_config_t *cfg, dma_addr_t dst, dma_addr_t src, size_t len, void *cb_data)
Program a DMA transfer.
size_t dma_transfer_get_remaining(dma_channel_t channel)
Get the remaining size of a DMA transfer.
dma_addr_t hw_to_dma_addr(uintptr_t hw_addr)
Convert a hardware address to a DMA address.
dma_request_t
DMA request.
Definition dmac.h:69
uint32_t dma_addr_t
DMA address.
Definition dmac.h:151
void dma_wait_complete(dma_channel_t channel)
Wait for a DMA transfer to complete.
dma_addrmode_t
DMA address mode.
Definition dmac.h:110
dma_unitsize_t
DMA unit size.
Definition dmac.h:95
dma_addr_t dma_map_dst(void *ptr, size_t len)
Prepare a destination memory buffer for a DMA transfer.
bool dma_is_running(dma_channel_t channel)
Check if a DMA transfer is running.
dma_addr_t dma_map_src(const void *ptr, size_t len)
Prepare a source memory buffer for a DMA transfer.
void(* dma_callback_t)(void *data)
DMA callback type.
Definition dmac.h:43
dma_transmitmode_t
DMA transmit mode.
Definition dmac.h:126
@ DMA_CHANNEL_0
Channel #0: On Dreamcast, reserved for hardware.
Definition dmac.h:51
@ DMA_CHANNEL_1
Channel #1: External, hardware or mem-to-mem requests.
Definition dmac.h:52
@ DMA_CHANNEL_3
Channel #3: mem-to-mem requests only.
Definition dmac.h:54
@ DMA_CHANNEL_2
Channel #2: on Dreamcast, reserved for PVR use.
Definition dmac.h:53
@ DMA_REQUEST_AUTO_MEM_TO_MEM
Definition dmac.h:73
@ DMA_REQUEST_AUTO_DEV_TO_MEM
Definition dmac.h:75
@ DMA_REQUEST_SCI_RECEIVE
Definition dmac.h:78
@ DMA_REQUEST_AUTO_MEM_TO_DEV
Definition dmac.h:74
@ DMA_REQUEST_EXTERNAL_MEM_TO_MEM
Definition dmac.h:70
@ DMA_REQUEST_TMU2_MEM_TO_DEV
Definition dmac.h:82
@ DMA_REQUEST_TMU2_MEM_TO_MEM
Definition dmac.h:81
@ DMA_REQUEST_EXTERNAL_MEM_TO_DEV
Definition dmac.h:71
@ DMA_REQUEST_SCI_TRANSMIT
Definition dmac.h:77
@ DMA_REQUEST_TMU2_DEV_TO_MEM
Definition dmac.h:83
@ DMA_REQUEST_SCIF_RECEIVE
Definition dmac.h:80
@ DMA_REQUEST_SCIF_TRANSMIT
Definition dmac.h:79
@ DMA_REQUEST_EXTERNAL_DEV_TO_MEM
Definition dmac.h:72
@ DMA_ADDRMODE_DECREMENT
The source/destination address is decremented.
Definition dmac.h:113
@ DMA_ADDRMODE_INCREMENT
The source/destination address is incremented.
Definition dmac.h:112
@ DMA_ADDRMODE_FIXED
The source/destination address is not modified.
Definition dmac.h:111
@ DMA_UNITSIZE_32BYTE
Definition dmac.h:100
@ DMA_UNITSIZE_16BIT
Definition dmac.h:98
@ DMA_UNITSIZE_8BIT
Definition dmac.h:97
@ DMA_UNITSIZE_32BIT
Definition dmac.h:99
@ DMA_UNITSIZE_64BIT
Definition dmac.h:96
@ DMA_TRANSMITMODE_BURST
Definition dmac.h:128
@ DMA_TRANSMITMODE_CYCLE_STEAL
Definition dmac.h:127
DMA transfer configuration.
Definition dmac.h:136
dma_addrmode_t dst_mode
Source/destination address mode.
Definition dmac.h:140
dma_channel_t channel
DMA channel used for the transfer.
Definition dmac.h:137
dma_request_t request
DMA request type.
Definition dmac.h:138
dma_transmitmode_t transmit_mode
DMA Transfer transmit mode.
Definition dmac.h:141
dma_callback_t callback
Optional callback function for end-of-transfer notification.
Definition dmac.h:142
dma_unitsize_t unit_size
Unit size used for the DMA transfer.
Definition dmac.h:139