root / hw / sparc32_dma.c @ 513f789f
History | View | Annotate | Download (7.1 kB)
1 | 67e999be | bellard | /*
|
---|---|---|---|
2 | 67e999be | bellard | * QEMU Sparc32 DMA controller emulation
|
3 | 67e999be | bellard | *
|
4 | 67e999be | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 67e999be | bellard | *
|
6 | 67e999be | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 67e999be | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 67e999be | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 67e999be | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 67e999be | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 67e999be | bellard | * furnished to do so, subject to the following conditions:
|
12 | 67e999be | bellard | *
|
13 | 67e999be | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 67e999be | bellard | * all copies or substantial portions of the Software.
|
15 | 67e999be | bellard | *
|
16 | 67e999be | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 67e999be | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 67e999be | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 67e999be | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 67e999be | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 67e999be | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 67e999be | bellard | * THE SOFTWARE.
|
23 | 67e999be | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "sparc32_dma.h" |
26 | 87ecb68b | pbrook | #include "sun4m.h" |
27 | 67e999be | bellard | |
28 | 67e999be | bellard | /* debug DMA */
|
29 | 67e999be | bellard | //#define DEBUG_DMA
|
30 | 67e999be | bellard | |
31 | 67e999be | bellard | /*
|
32 | 67e999be | bellard | * This is the DMA controller part of chip STP2000 (Master I/O), also
|
33 | 67e999be | bellard | * produced as NCR89C100. See
|
34 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
|
35 | 67e999be | bellard | * and
|
36 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
|
37 | 67e999be | bellard | */
|
38 | 67e999be | bellard | |
39 | 67e999be | bellard | #ifdef DEBUG_DMA
|
40 | 67e999be | bellard | #define DPRINTF(fmt, args...) \
|
41 | 67e999be | bellard | do { printf("DMA: " fmt , ##args); } while (0) |
42 | 67e999be | bellard | #else
|
43 | 67e999be | bellard | #define DPRINTF(fmt, args...)
|
44 | 67e999be | bellard | #endif
|
45 | 67e999be | bellard | |
46 | 5aca8c3b | blueswir1 | #define DMA_REGS 4 |
47 | 5aca8c3b | blueswir1 | #define DMA_SIZE (4 * sizeof(uint32_t)) |
48 | 09723aa1 | blueswir1 | /* We need the mask, because one instance of the device is not page
|
49 | 09723aa1 | blueswir1 | aligned (ledma, start address 0x0010) */
|
50 | 09723aa1 | blueswir1 | #define DMA_MASK (DMA_SIZE - 1) |
51 | 67e999be | bellard | |
52 | 67e999be | bellard | #define DMA_VER 0xa0000000 |
53 | 67e999be | bellard | #define DMA_INTR 1 |
54 | 67e999be | bellard | #define DMA_INTREN 0x10 |
55 | 67e999be | bellard | #define DMA_WRITE_MEM 0x100 |
56 | 67e999be | bellard | #define DMA_LOADED 0x04000000 |
57 | 5aca8c3b | blueswir1 | #define DMA_DRAIN_FIFO 0x40 |
58 | 67e999be | bellard | #define DMA_RESET 0x80 |
59 | 67e999be | bellard | |
60 | 67e999be | bellard | typedef struct DMAState DMAState; |
61 | 67e999be | bellard | |
62 | 67e999be | bellard | struct DMAState {
|
63 | 67e999be | bellard | uint32_t dmaregs[DMA_REGS]; |
64 | 5aca8c3b | blueswir1 | qemu_irq irq; |
65 | 2d069bab | blueswir1 | void *iommu;
|
66 | 2d069bab | blueswir1 | qemu_irq dev_reset; |
67 | 67e999be | bellard | }; |
68 | 67e999be | bellard | |
69 | 9b94dc32 | bellard | /* Note: on sparc, the lance 16 bit bus is swapped */
|
70 | 5fafdf24 | ths | void ledma_memory_read(void *opaque, target_phys_addr_t addr, |
71 | 9b94dc32 | bellard | uint8_t *buf, int len, int do_bswap) |
72 | 67e999be | bellard | { |
73 | 67e999be | bellard | DMAState *s = opaque; |
74 | 9b94dc32 | bellard | int i;
|
75 | 67e999be | bellard | |
76 | 67e999be | bellard | DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
77 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
78 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
79 | 9b94dc32 | bellard | if (do_bswap) {
|
80 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
81 | 9b94dc32 | bellard | } else {
|
82 | 9b94dc32 | bellard | addr &= ~1;
|
83 | 9b94dc32 | bellard | len &= ~1;
|
84 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
85 | 9b94dc32 | bellard | for(i = 0; i < len; i += 2) { |
86 | 9b94dc32 | bellard | bswap16s((uint16_t *)(buf + i)); |
87 | 9b94dc32 | bellard | } |
88 | 9b94dc32 | bellard | } |
89 | 67e999be | bellard | } |
90 | 67e999be | bellard | |
91 | 5fafdf24 | ths | void ledma_memory_write(void *opaque, target_phys_addr_t addr, |
92 | 9b94dc32 | bellard | uint8_t *buf, int len, int do_bswap) |
93 | 67e999be | bellard | { |
94 | 67e999be | bellard | DMAState *s = opaque; |
95 | 9b94dc32 | bellard | int l, i;
|
96 | 9b94dc32 | bellard | uint16_t tmp_buf[32];
|
97 | 67e999be | bellard | |
98 | 67e999be | bellard | DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
99 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
100 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
101 | 9b94dc32 | bellard | if (do_bswap) {
|
102 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, buf, len); |
103 | 9b94dc32 | bellard | } else {
|
104 | 9b94dc32 | bellard | addr &= ~1;
|
105 | 9b94dc32 | bellard | len &= ~1;
|
106 | 9b94dc32 | bellard | while (len > 0) { |
107 | 9b94dc32 | bellard | l = len; |
108 | 9b94dc32 | bellard | if (l > sizeof(tmp_buf)) |
109 | 9b94dc32 | bellard | l = sizeof(tmp_buf);
|
110 | 9b94dc32 | bellard | for(i = 0; i < l; i += 2) { |
111 | 9b94dc32 | bellard | tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
|
112 | 9b94dc32 | bellard | } |
113 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l); |
114 | 9b94dc32 | bellard | len -= l; |
115 | 9b94dc32 | bellard | buf += l; |
116 | 9b94dc32 | bellard | addr += l; |
117 | 9b94dc32 | bellard | } |
118 | 9b94dc32 | bellard | } |
119 | 67e999be | bellard | } |
120 | 67e999be | bellard | |
121 | 70c0de96 | blueswir1 | static void dma_set_irq(void *opaque, int irq, int level) |
122 | 67e999be | bellard | { |
123 | 67e999be | bellard | DMAState *s = opaque; |
124 | 70c0de96 | blueswir1 | if (level) {
|
125 | 9b5207aa | blueswir1 | DPRINTF("Raise IRQ\n");
|
126 | 70c0de96 | blueswir1 | s->dmaregs[0] |= DMA_INTR;
|
127 | 70c0de96 | blueswir1 | qemu_irq_raise(s->irq); |
128 | 70c0de96 | blueswir1 | } else {
|
129 | 70c0de96 | blueswir1 | s->dmaregs[0] &= ~DMA_INTR;
|
130 | 9b5207aa | blueswir1 | DPRINTF("Lower IRQ\n");
|
131 | 70c0de96 | blueswir1 | qemu_irq_lower(s->irq); |
132 | 70c0de96 | blueswir1 | } |
133 | 67e999be | bellard | } |
134 | 67e999be | bellard | |
135 | 67e999be | bellard | void espdma_memory_read(void *opaque, uint8_t *buf, int len) |
136 | 67e999be | bellard | { |
137 | 67e999be | bellard | DMAState *s = opaque; |
138 | 67e999be | bellard | |
139 | 67e999be | bellard | DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
140 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
141 | 67e999be | bellard | sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
|
142 | 67e999be | bellard | s->dmaregs[0] |= DMA_INTR;
|
143 | 67e999be | bellard | s->dmaregs[1] += len;
|
144 | 67e999be | bellard | } |
145 | 67e999be | bellard | |
146 | 67e999be | bellard | void espdma_memory_write(void *opaque, uint8_t *buf, int len) |
147 | 67e999be | bellard | { |
148 | 67e999be | bellard | DMAState *s = opaque; |
149 | 67e999be | bellard | |
150 | 67e999be | bellard | DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
151 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
152 | 67e999be | bellard | sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
|
153 | 67e999be | bellard | s->dmaregs[0] |= DMA_INTR;
|
154 | 67e999be | bellard | s->dmaregs[1] += len;
|
155 | 67e999be | bellard | } |
156 | 67e999be | bellard | |
157 | 67e999be | bellard | static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr) |
158 | 67e999be | bellard | { |
159 | 67e999be | bellard | DMAState *s = opaque; |
160 | 67e999be | bellard | uint32_t saddr; |
161 | 67e999be | bellard | |
162 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
163 | 5aca8c3b | blueswir1 | DPRINTF("read dmareg " TARGET_FMT_plx ": 0x%8.8x\n", addr, |
164 | 5aca8c3b | blueswir1 | s->dmaregs[saddr]); |
165 | 67e999be | bellard | |
166 | 67e999be | bellard | return s->dmaregs[saddr];
|
167 | 67e999be | bellard | } |
168 | 67e999be | bellard | |
169 | 67e999be | bellard | static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
170 | 67e999be | bellard | { |
171 | 67e999be | bellard | DMAState *s = opaque; |
172 | 67e999be | bellard | uint32_t saddr; |
173 | 67e999be | bellard | |
174 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
175 | 5aca8c3b | blueswir1 | DPRINTF("write dmareg " TARGET_FMT_plx ": 0x%8.8x -> 0x%8.8x\n", addr, |
176 | 5aca8c3b | blueswir1 | s->dmaregs[saddr], val); |
177 | 67e999be | bellard | switch (saddr) {
|
178 | 67e999be | bellard | case 0: |
179 | d537cf6c | pbrook | if (!(val & DMA_INTREN)) {
|
180 | 5aca8c3b | blueswir1 | DPRINTF("Lower IRQ\n");
|
181 | 5aca8c3b | blueswir1 | qemu_irq_lower(s->irq); |
182 | d537cf6c | pbrook | } |
183 | 67e999be | bellard | if (val & DMA_RESET) {
|
184 | 2d069bab | blueswir1 | qemu_irq_raise(s->dev_reset); |
185 | 2d069bab | blueswir1 | qemu_irq_lower(s->dev_reset); |
186 | 5aca8c3b | blueswir1 | } else if (val & DMA_DRAIN_FIFO) { |
187 | 5aca8c3b | blueswir1 | val &= ~DMA_DRAIN_FIFO; |
188 | 67e999be | bellard | } else if (val == 0) |
189 | 5aca8c3b | blueswir1 | val = DMA_DRAIN_FIFO; |
190 | 67e999be | bellard | val &= 0x0fffffff;
|
191 | 67e999be | bellard | val |= DMA_VER; |
192 | 67e999be | bellard | break;
|
193 | 67e999be | bellard | case 1: |
194 | 67e999be | bellard | s->dmaregs[0] |= DMA_LOADED;
|
195 | 67e999be | bellard | break;
|
196 | 67e999be | bellard | default:
|
197 | 67e999be | bellard | break;
|
198 | 67e999be | bellard | } |
199 | 67e999be | bellard | s->dmaregs[saddr] = val; |
200 | 67e999be | bellard | } |
201 | 67e999be | bellard | |
202 | 67e999be | bellard | static CPUReadMemoryFunc *dma_mem_read[3] = { |
203 | 7c560456 | blueswir1 | NULL,
|
204 | 7c560456 | blueswir1 | NULL,
|
205 | 67e999be | bellard | dma_mem_readl, |
206 | 67e999be | bellard | }; |
207 | 67e999be | bellard | |
208 | 67e999be | bellard | static CPUWriteMemoryFunc *dma_mem_write[3] = { |
209 | 7c560456 | blueswir1 | NULL,
|
210 | 7c560456 | blueswir1 | NULL,
|
211 | 67e999be | bellard | dma_mem_writel, |
212 | 67e999be | bellard | }; |
213 | 67e999be | bellard | |
214 | 67e999be | bellard | static void dma_reset(void *opaque) |
215 | 67e999be | bellard | { |
216 | 67e999be | bellard | DMAState *s = opaque; |
217 | 67e999be | bellard | |
218 | 5aca8c3b | blueswir1 | memset(s->dmaregs, 0, DMA_SIZE);
|
219 | 67e999be | bellard | s->dmaregs[0] = DMA_VER;
|
220 | 67e999be | bellard | } |
221 | 67e999be | bellard | |
222 | 67e999be | bellard | static void dma_save(QEMUFile *f, void *opaque) |
223 | 67e999be | bellard | { |
224 | 67e999be | bellard | DMAState *s = opaque; |
225 | 67e999be | bellard | unsigned int i; |
226 | 67e999be | bellard | |
227 | 67e999be | bellard | for (i = 0; i < DMA_REGS; i++) |
228 | 67e999be | bellard | qemu_put_be32s(f, &s->dmaregs[i]); |
229 | 67e999be | bellard | } |
230 | 67e999be | bellard | |
231 | 67e999be | bellard | static int dma_load(QEMUFile *f, void *opaque, int version_id) |
232 | 67e999be | bellard | { |
233 | 67e999be | bellard | DMAState *s = opaque; |
234 | 67e999be | bellard | unsigned int i; |
235 | 67e999be | bellard | |
236 | 5aca8c3b | blueswir1 | if (version_id != 2) |
237 | 67e999be | bellard | return -EINVAL;
|
238 | 67e999be | bellard | for (i = 0; i < DMA_REGS; i++) |
239 | 67e999be | bellard | qemu_get_be32s(f, &s->dmaregs[i]); |
240 | 67e999be | bellard | |
241 | 67e999be | bellard | return 0; |
242 | 67e999be | bellard | } |
243 | 67e999be | bellard | |
244 | 70c0de96 | blueswir1 | void *sparc32_dma_init(target_phys_addr_t daddr, qemu_irq parent_irq,
|
245 | 2d069bab | blueswir1 | void *iommu, qemu_irq **dev_irq, qemu_irq **reset)
|
246 | 67e999be | bellard | { |
247 | 67e999be | bellard | DMAState *s; |
248 | 67e999be | bellard | int dma_io_memory;
|
249 | 67e999be | bellard | |
250 | 67e999be | bellard | s = qemu_mallocz(sizeof(DMAState));
|
251 | 67e999be | bellard | |
252 | 70c0de96 | blueswir1 | s->irq = parent_irq; |
253 | 67e999be | bellard | s->iommu = iommu; |
254 | 67e999be | bellard | |
255 | 67e999be | bellard | dma_io_memory = cpu_register_io_memory(0, dma_mem_read, dma_mem_write, s);
|
256 | 5aca8c3b | blueswir1 | cpu_register_physical_memory(daddr, DMA_SIZE, dma_io_memory); |
257 | 67e999be | bellard | |
258 | 5aca8c3b | blueswir1 | register_savevm("sparc32_dma", daddr, 2, dma_save, dma_load, s); |
259 | 67e999be | bellard | qemu_register_reset(dma_reset, s); |
260 | 70c0de96 | blueswir1 | *dev_irq = qemu_allocate_irqs(dma_set_irq, s, 1);
|
261 | 67e999be | bellard | |
262 | 2d069bab | blueswir1 | *reset = &s->dev_reset; |
263 | 67e999be | bellard | |
264 | 2d069bab | blueswir1 | return s;
|
265 | 67e999be | bellard | } |