KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
g2bus.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 g2bus.h
4 Copyright (C) 2002 Megan Potter
5 Copyright (C) 2023 Andy Barajas
6 Copyright (C) 2024 Ruslan Rostovtsev
7
8*/
9
10/** \file dc/g2bus.h
11 \brief G2 bus memory interface.
12 \ingroup system_g2bus
13
14 This file provides low-level support for accessing devices on the G2 bus in
15 the Dreamcast. The G2 bus contains the AICA, as well as the expansion port.
16 Generally, you won't be dealing with things at this level, but rather on the
17 level of the device you're actually interested in working with. Most of the
18 expansion port devices (the modem, bba, and lan adapter) all have their own
19 drivers that work off of this functionality.
20
21 The G2 bus is notoroiously picky about a lot of things. You have to be
22 careful to use the right access size for whatever you're working with. Also
23 you can't be doing PIO and DMA at the same time. Finally, there's a FIFO to
24 contend with when you're doing PIO stuff as well. Generally, G2 is a pain in
25 the rear, so you really do want to be using the higher-level stuff related
26 to each device if at all possible!
27
28 \author Megan Potter
29 \author Andy Barajas
30*/
31
32#ifndef __DC_G2BUS_H
33#define __DC_G2BUS_H
34
35#include <kos/cdefs.h>
36__BEGIN_DECLS
37
38#include <stdint.h>
39#include <kos/irq.h>
40
41#include <dc/fifo.h>
42
43/** \defgroup system_g2bus G2 Bus
44 \brief Driver for accessing the devices on the G2 Bus
45 \ingroup system
46
47 @{
48*/
49
50/** \name List of G2 Bus channels
51
52 AICA (SPU) is channel 0, BBA uses channel 1. CH2_DMA_G2CHN and
53 CH3_DMA_G2CHN are not currently tied to any specific device.
54
55 \note
56 A change in the implementation has rendered *_DMA_MODE and *_DMA_SHCHN
57 obsolete.
58
59 In the current implementation, *_DMA_MODE should always be set to zero
60 (representing CPU_TRIGGER). There is also no involvement of SH4-DMA with
61 G2-DMA; therefore, the *_DMA_SHCHN values have been deprecated.
62
63 @{
64*/
65#define G2_DMA_CHAN_SPU 0 /**< \brief AICA: G2 channel 0 */
66#define G2_DMA_CHAN_BBA 1 /**< \brief BBA: G2 channel 1 */
67#define G2_DMA_CHAN_CH2 2 /**< \brief CH2: G2 channel 2 */
68#define G2_DMA_CHAN_CH3 3 /**< \brief CH3: G2 channel 3 */
69/** @} */
70
71/** \brief G2Bus DMA direction
72
73 The direction you want the data to go. SH4 to AICA, BBA, etc use
74 SH4TOG2BUS, otherwise G2BUSTOSH4.
75*/
76#define G2_DMA_TO_G2 0
77#define G2_DMA_TO_SH4 1
78
79/** \brief G2Bus DMA interrupt callback type.
80
81 Functions that act as callbacks when G2-DMA completes should be of this type.
82 These functions will be called inside an interrupt context, so don't try to
83 use anything that might stall.
84
85 \param data User data passed in to the g2_dma_transfer()
86 function.
87*/
88typedef void (*g2_dma_callback_t)(void *data);
89
90/** \brief Perform a DMA transfer between SH-4 RAM and G2 Bus
91
92 This function copies a block of data between SH-4 RAM and G2 Bus via DMA.
93 You specify the direction of the copy (SH4TOG2BUS/G2BUSTOSH4). There are all
94 kinds of constraints that must be fulfilled to actually do this, so
95 make sure to read all the fine print with the parameter list.
96
97 If a callback is specified, it will be called in an interrupt context, so
98 keep that in mind in writing the callback.
99
100 \param sh4 Where to copy from/to. Must be 32-byte aligned.
101 \param g2bus Where to copy from/to. Must be 32-byte aligned.
102 \param length The number of bytes to copy. Must be a multiple of
103 32.
104 \param block Non-zero if you want the function to block until the
105 DMA completes.
106 \param callback A function to call upon completion of the DMA.
107 \param cbdata Data to pass to the callback function.
108 \param dir SH4TOG2BUS or G2BUSTOSH4.
109 \param mode Ignored; for compatibility only.
110 \param g2chn See g2b_channels.
111 \param sh4chn Ignored; for compatibility only.
112 \retval 0 On success.
113 \retval -1 On failure. Sets errno as appropriate.
114
115 \par Error Conditions:
116 \em EINPROGRESS - DMA already in progress \n
117 \em EFAULT - sh4 and/or g2bus is not 32-byte aligned \n
118 \em EINVAL - Invalid g2chn
119 \em EIO - I/O error
120
121*/
122int g2_dma_transfer(void *sh4, void *g2bus, size_t length, uint32_t block,
123 g2_dma_callback_t callback, void *cbdata,
124 uint32_t dir, uint32_t mode, uint32_t g2chn, uint32_t sh4chn);
125
126/** \brief Initialize DMA support.
127
128 This function sets up the DMA support for transfers to/from the G2 Bus.
129
130 \retval 0 On success (no error conditions defined).
131*/
132int g2_dma_init(void);
133
134/** \brief Shutdown DMA support. */
136
137/** \brief G2 context
138
139 A G2 context containing the states of IRQs and G2 DMA. This struct
140 is used in with g2_lock() and g2_unlock().
141*/
142typedef struct {
143 irq_mask_t irq_state; /** \brief IRQ state when entering a G2 critical block */
144} g2_ctx_t;
145
146/* Internal constants to access suspend registers for G2 DMA. They are not meant for
147 user-code use. */
148/** \cond */
149#define G2_DMA_SUSPEND_SPU (*((volatile uint32_t *)0xa05f781C))
150#define G2_DMA_SUSPEND_BBA (*((volatile uint32_t *)0xa05f783C))
151#define G2_DMA_SUSPEND_CH2 (*((volatile uint32_t *)0xa05f785C))
152/** \endcond */
153
154/** \brief Disable IRQs and G2 DMA
155
156 This function makes the following g2_read_*()/g2_write_*() functions atomic
157 by disabling IRQs and G2 DMA and storing their states. Pass the context
158 created by this function to g2_unlock() to re-enable IRQs and G2 DMA.
159
160 \return The context containing the IRQ and G2 DMA states.
161*/
162static inline g2_ctx_t g2_lock(void) {
163 g2_ctx_t ctx;
164
165 ctx.irq_state = irq_disable();
166
167 /* Suspend any G2 DMA */
168 G2_DMA_SUSPEND_SPU = 1;
169 G2_DMA_SUSPEND_BBA = 1;
170 G2_DMA_SUSPEND_CH2 = 1;
171
172 while(FIFO_STATUS & (FIFO_SH4 | FIFO_G2));
173
174 return ctx;
175}
176
177/** \brief Enable IRQs and G2 DMA
178
179 This function restores the IRQ and G2 DMA states using the context value
180 generated by g2_lock().
181
182 \param ctx The context containing IRQ and G2 DMA states.
183*/
184static inline void g2_unlock(g2_ctx_t ctx) {
185 /* Restore suspended G2 DMA */
186 G2_DMA_SUSPEND_SPU = 0;
187 G2_DMA_SUSPEND_BBA = 0;
188 G2_DMA_SUSPEND_CH2 = 0;
189
191}
192
193/** \cond */
194static inline void __g2_scoped_cleanup(g2_ctx_t *state) {
195 g2_unlock(*state);
196}
197
198#define ___g2_lock_scoped(l) \
199 g2_ctx_t __scoped_g2_lock_##l __attribute__((cleanup(__g2_scoped_cleanup))) = g2_lock()
200
201#define __g2_lock_scoped(l) ___g2_lock_scoped(l)
202/** \endcond */
203
204/** \brief Disable IRQs and G2 DMA with scope management
205
206 This function makes the following g2_read_*()/g2_write_*() functions atomic
207 by disabling IRQs and G2 DMA and storing their states. The state will
208 automatically be restored once the execution exits the functional block in
209 which the macro was called.
210*/
211#define g2_lock_scoped() __g2_lock_scoped(__LINE__)
212
213#undef G2_DMA_SUSPEND_SPU
214#undef G2_DMA_SUSPEND_BBA
215#undef G2_DMA_SUSPEND_CH2
216
217/** \brief Read one byte from G2.
218
219 This function reads a single byte from the specified address, taking all
220 necessary precautions that are required for accessing G2.
221
222 \param address The address in memory to read.
223 \return The byte read from the address specified.
224*/
225uint8_t g2_read_8(uintptr_t address);
226
227/** \brief Write a single byte to G2.
228
229 This function writes one byte to the specified address, taking all the
230 necessary precautions to ensure your write actually succeeds.
231
232 \param address The address in memory to write to.
233 \param value The value to write to that address.
234*/
235void g2_write_8(uintptr_t address, uint8_t value);
236
237/** \brief Read one 16-bit word from G2.
238
239 This function reads a single word from the specified address, taking all
240 necessary precautions that are required for accessing G2.
241
242 \param address The address in memory to read.
243 \return The word read from the address specified.
244*/
245uint16_t g2_read_16(uintptr_t address);
246
247/** \brief Write a 16-bit word to G2.
248
249 This function writes one word to the specified address, taking all the
250 necessary precautions to ensure your write actually succeeds.
251
252 \param address The address in memory to write to.
253 \param value The value to write to that address.
254*/
255void g2_write_16(uintptr_t address, uint16_t value);
256
257/** \brief Read one 32-bit dword from G2.
258
259 This function reads a single dword from the specified address, taking all
260 necessary precautions that are required for accessing G2.
261
262 \param address The address in memory to read.
263 \return The dword read from the address specified.
264*/
265uint32_t g2_read_32(uintptr_t address);
266
267/** \brief Non-blocked read one 32-bit dword from G2.
268
269 This function reads a single dword from the specified address, without all
270 necessary precautions that are required for accessing G2.
271
272 \param address The address in memory to read.
273 \return The dword read from the address specified.
274*/
275static inline uint32_t g2_read_32_raw(uintptr_t address) {
276 return *((volatile uint32_t *)address);
277}
278
279/** \brief Write a 32-bit dword to G2.
280
281 This function writes one dword to the specified address, taking all the
282 necessary precautions to ensure your write actually succeeds.
283
284 \param address The address in memory to write to.
285 \param value The value to write to that address.
286*/
287void g2_write_32(uintptr_t address, uint32_t value);
288
289/** \brief Non-blocked write a 32-bit dword to G2.
290
291 This function writes one dword to the specified address, without all the
292 necessary precautions to ensure your write actually succeeds.
293
294 \param address The address in memory to write to.
295 \param value The value to write to that address.
296*/
297static inline void g2_write_32_raw(uintptr_t address, uint32_t value) {
298 *((volatile uint32_t *)address) = value;
299}
300
301/** \brief Read a block of bytes from G2.
302
303 This function acts as memcpy() for copying data from G2 to system memory. It
304 will take the necessary precautions before accessing G2 for you as well.
305
306 \param output Pointer in system memory to write to.
307 \param address The address in G2-space to read from.
308 \param amt The number of bytes to read.
309*/
310void g2_read_block_8(uint8_t * output, uintptr_t address, size_t amt);
311
312/** \brief Write a block of bytes to G2.
313
314 This function acts as memcpy() for copying data to G2 from system memory. It
315 will take the necessary precautions for accessing G2.
316
317 \param input The pointer in system memory to read from.
318 \param address The address in G2-space to write to.
319 \param amt The number of bytes to write.
320*/
321void g2_write_block_8(const uint8_t * input, uintptr_t address, size_t amt);
322
323/** \brief Read a block of words from G2.
324
325 This function acts as memcpy() for copying data from G2 to system memory,
326 but it copies 16 bits at a time. It will take the necessary precautions
327 before accessing G2 for you as well.
328
329 \param output Pointer in system memory to write to.
330 \param address The address in G2-space to read from.
331 \param amt The number of words to read.
332*/
333void g2_read_block_16(uint16_t * output, uintptr_t address, size_t amt);
334
335/** \brief Write a block of words to G2.
336
337 This function acts as memcpy() for copying data to G2 from system memory,
338 copying 16 bits at a time. It will take the necessary precautions for
339 accessing G2.
340
341 \param input The pointer in system memory to read from.
342 \param address The address in G2-space to write to.
343 \param amt The number of words to write.
344*/
345void g2_write_block_16(const uint16_t * input, uintptr_t address, size_t amt);
346
347/** \brief Read a block of dwords from G2.
348
349 This function acts as memcpy() for copying data from G2 to system memory,
350 but it copies 32 bits at a time. It will take the necessary precautions
351 before accessing G2 for you as well.
352
353 \param output Pointer in system memory to write to.
354 \param address The address in G2-space to read from.
355 \param amt The number of dwords to read.
356*/
357void g2_read_block_32(uint32_t * output, uintptr_t address, size_t amt);
358
359/** \brief Write a block of dwords to G2.
360
361 This function acts as memcpy() for copying data to G2 from system memory,
362 copying 32 bits at a time. It will take the necessary precautions for
363 accessing G2.
364
365 \param input The pointer in system memory to read from.
366 \param address The address in G2-space to write to.
367 \param amt The number of dwords to write.
368*/
369void g2_write_block_32(const uint32_t * input, uintptr_t address, size_t amt);
370
371/** \brief Set a block of bytes to G2.
372
373 This function acts as memset() for setting a block of bytes on G2. It will
374 take the necessary precautions for accessing G2.
375
376 \param address The address in G2-space to write to.
377 \param c The byte to write.
378 \param amt The number of bytes to write.
379*/
380void g2_memset_8(uintptr_t address, uint8_t c, size_t amt);
381
382/** \brief Wait for the G2 write FIFO to empty.
383
384 This function will spinwait until the G2 FIFO indicates that it has been
385 drained. The FIFO is 32 bytes in length, and thus when accessing AICA you
386 must do this at least for every 8 32-bit writes that you execute.
387*/
388void g2_fifo_wait(void);
389
390/** @} */
391
392__END_DECLS
393
394#endif /* __DC_G2BUS_H */
395
int mode
Definition 2ndmix.c:539
static struct @68 data[BARRIER_COUNT]
Various common macros used throughout the codebase.
Macros to assess FIFO status.
static irq_mask_t irq_disable(void)
Disable interrupts.
Definition irq.h:107
static void irq_restore(irq_mask_t state)
Restore interrupt state.
Definition irq.h:121
#define FIFO_G2
Definition fifo.h:55
#define FIFO_SH4
Definition fifo.h:56
#define FIFO_STATUS
Address of the FIFO status register.
Definition fifo.h:37
void g2_write_32(uintptr_t address, uint32_t value)
Write a 32-bit dword to G2.
uint16_t g2_read_16(uintptr_t address)
Read one 16-bit word from G2.
static uint32_t g2_read_32_raw(uintptr_t address)
Non-blocked read one 32-bit dword from G2.
Definition g2bus.h:275
static void g2_unlock(g2_ctx_t ctx)
Enable IRQs and G2 DMA.
Definition g2bus.h:184
void g2_read_block_16(uint16_t *output, uintptr_t address, size_t amt)
Read a block of words from G2.
void g2_write_block_8(const uint8_t *input, uintptr_t address, size_t amt)
Write a block of bytes to G2.
int g2_dma_init(void)
Initialize DMA support.
void g2_memset_8(uintptr_t address, uint8_t c, size_t amt)
Set a block of bytes to G2.
static g2_ctx_t g2_lock(void)
Disable IRQs and G2 DMA.
Definition g2bus.h:162
static void g2_write_32_raw(uintptr_t address, uint32_t value)
Non-blocked write a 32-bit dword to G2.
Definition g2bus.h:297
void(* g2_dma_callback_t)(void *data)
G2Bus DMA interrupt callback type.
Definition g2bus.h:88
void g2_write_8(uintptr_t address, uint8_t value)
Write a single byte to G2.
int g2_dma_transfer(void *sh4, void *g2bus, size_t length, uint32_t block, g2_dma_callback_t callback, void *cbdata, uint32_t dir, uint32_t mode, uint32_t g2chn, uint32_t sh4chn)
Perform a DMA transfer between SH-4 RAM and G2 Bus.
void g2_write_16(uintptr_t address, uint16_t value)
Write a 16-bit word to G2.
uint32_t g2_read_32(uintptr_t address)
Read one 32-bit dword from G2.
uint8_t g2_read_8(uintptr_t address)
Read one byte from G2.
void g2_read_block_8(uint8_t *output, uintptr_t address, size_t amt)
Read a block of bytes from G2.
void g2_dma_shutdown(void)
Shutdown DMA support.
void g2_write_block_16(const uint16_t *input, uintptr_t address, size_t amt)
Write a block of words to G2.
void g2_write_block_32(const uint32_t *input, uintptr_t address, size_t amt)
Write a block of dwords to G2.
void g2_read_block_32(uint32_t *output, uintptr_t address, size_t amt)
Read a block of dwords from G2.
void g2_fifo_wait(void)
Wait for the G2 write FIFO to empty.
Timer functionality.
uint32_t irq_mask_t
Type representing an interrupt mask state.
Definition irq.h:54
static uint8_t block[4096]
Definition mke2fs.c:45
G2 context.
Definition g2bus.h:142
irq_mask_t irq_state
Definition g2bus.h:143