ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
dma.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (c) 2009 Lion
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
11 #include <errno.h>
12 #include <stdio.h>
13 #include <kernel.h>
14 #include <libgs.h>
15 
16 #include "internal.h"
17 
18 // Miscellaneous
19 
21 #define gif_chcr 0x1000a000
23 #define gif_madr 0x1000a010
25 #define gif_qwc 0x1000a020
26 #define gif_tadr 0x1000a030
27 
28  #define DMA_TAG_REFE 0x00
29  #define DMA_TAG_CNT 0x01
30  #define DMA_TAG_NEXT 0x02
31  #define DMA_TAG_REF 0x03
32  #define DMA_TAG_REFS 0x04
33  #define DMA_TAG_CALL 0x05
34  #define DMA_TAG_RET 0x06
35  #define DMA_TAG_END 0x07
36 
37 typedef struct {
39  unsigned direction :1;
41  unsigned pad1 :1;
43  unsigned mode :2;
45  unsigned asp :2;
47  unsigned tte :1;
49  unsigned tie :1;
51  unsigned start_flag :1;
53  unsigned pad2 :7;
55  unsigned tag :16;
56 }DMA_CHCR;
57 
58 void GsDmaInit(void)
59 {
60  /* This appears to have been based on code from Sony that initializes DMA channels 0-9, in bulk.
61  Reset/init DMA CH 2 (GIF) only. */
62  __asm__(
63  "li $2,0x1000A000 \n"
64  "sw $0,0x80($2) \n" // D2_SADR = 0. Documented to not exist, but is done.
65  "sw $0,0($2) \n" // D2_CHCR = 0
66  "sw $0,0x30($2) \n" // D2_TADR = 0
67  "sw $0,0x10($2) \n" // D2_MADR = 0
68  "sw $0,0x50($2) \n" // D2_ASR1 = 0
69  "sw $0,0x40($2) \n" // D2_ASR0 = 0
70  "li $2,0xFF1F \n" // Clear all interrupt status under D_STAT, other than SIF0, SIF1 & SIF2.
71  "sw $2,0x1000E010 \n"
72  "lw $2,0x1000E010 \n"
73  "lui $3,0xFF1F \n" // Clear all interrupt masks under D_STAT, other SIF0, SIF1 & SIF2. Writing a 1 reverses the bit.
74  "and $2,$3 \n"
75  "sw $2,0x1000E010 \n"
76  "sw $0,0x1000E000 \n" // D_CTRL = 0
77  "sw $0,0x1000E020 \n" // D_PCR = 0
78  "sw $0,0x1000E030 \n" // D_SQWC = 0
79  "sw $0,0x1000E050 \n" // D_RBOR = 0
80  "sw $0,0x1000E040 \n" // D_RBSR = 0
81  "li $3,1 \n"
82  "lw $2,0x1000E000 \n"
83  "ori $3,$2,1 \n" // D_CTRL (DMAE 1)
84  "sw $3,0x1000E000 \n"
85  );
86 }
87 
88 void GsDmaSend(const void *addr, u32 qwords)
89 {
90  DMA_CHCR chcr;
91  static char spr;
92 
93  if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
94  {
95  spr = 1;
96  }
97  else
98  {
99  spr = 0;
100  }
101 
102  *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;;
103 
104  *((vu32 *)(gif_qwc)) = qwords;
105 
106  chcr.direction =1;
107  chcr.mode =0;
108  chcr.asp =0;
109  chcr.tte =0;
110  chcr.tie =0;
111  chcr.start_flag =1;
112  chcr.tag =0;
113  chcr.pad1 =0;
114  chcr.pad2 =0;
115  *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
116 }
117 
118 void GsDmaSend_tag(const void *addr, u32 qwords, const GS_GIF_DMACHAIN_TAG *tag)
119 {
120  DMA_CHCR chcr;
121  static char spr;
122 
123  if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
124  {
125  spr = 1;
126  }
127  else
128  {
129  spr = 0;
130  }
131 
132  *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;
133  *((vu32 *)(gif_qwc)) = qwords;
134  *((vu32 *)(gif_tadr)) = ( u32 )((( u32 )tag) & 0x7FFFFFFF) << 0 | (u32)((0) & 0x00000001) << 31;
135 
136  chcr.direction =1;
137  chcr.mode =1; //chain
138  chcr.asp =0;
139  chcr.tte =0;
140  chcr.tie =0;
141  chcr.start_flag =1;
142  chcr.tag =0;
143  chcr.pad1 =0;
144  chcr.pad2 =0;
145  *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
146 }
147 
148 void GsDmaWait(void)
149 {
150  while(*((vu32 *)(0x1000a000)) & ((u32)1<<8));
151 }
void GsDmaWait(void)
Definition: dma.c:148
#define gif_madr
Definition: dma.c:23
void GsDmaInit(void)
Definition: dma.c:58
#define gif_chcr
Definition: dma.c:21
void GsDmaSend_tag(const void *addr, u32 qwords, const GS_GIF_DMACHAIN_TAG *tag)
Definition: dma.c:118
#define gif_tadr
Definition: dma.c:26
void GsDmaSend(const void *addr, u32 qwords)
Definition: dma.c:88
#define gif_qwc
Definition: dma.c:25
Definition: dma.c:37
unsigned direction
Definition: dma.c:39
unsigned tag
Definition: dma.c:55
unsigned tte
Definition: dma.c:47
unsigned pad1
Definition: dma.c:41
unsigned tie
Definition: dma.c:49
unsigned start_flag
Definition: dma.c:51
unsigned mode
Definition: dma.c:43
unsigned pad2
Definition: dma.c:53
unsigned asp
Definition: dma.c:45
unsigned int u32
Definition: tamtypes.h:30
volatile u32 vu32
Definition: tamtypes.h:38