Statistics
| Branch: | Revision:

root / hw / ppc4xx_devs.c @ a1e47211

History | View | Annotate | Download (20.5 kB)

1 008ff9d7 j_mayer
/*
2 008ff9d7 j_mayer
 * QEMU PowerPC 4xx embedded processors shared devices emulation
3 008ff9d7 j_mayer
 *
4 008ff9d7 j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
5 008ff9d7 j_mayer
 *
6 008ff9d7 j_mayer
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 008ff9d7 j_mayer
 * of this software and associated documentation files (the "Software"), to deal
8 008ff9d7 j_mayer
 * in the Software without restriction, including without limitation the rights
9 008ff9d7 j_mayer
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 008ff9d7 j_mayer
 * copies of the Software, and to permit persons to whom the Software is
11 008ff9d7 j_mayer
 * furnished to do so, subject to the following conditions:
12 008ff9d7 j_mayer
 *
13 008ff9d7 j_mayer
 * The above copyright notice and this permission notice shall be included in
14 008ff9d7 j_mayer
 * all copies or substantial portions of the Software.
15 008ff9d7 j_mayer
 *
16 008ff9d7 j_mayer
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 008ff9d7 j_mayer
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 008ff9d7 j_mayer
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 008ff9d7 j_mayer
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 008ff9d7 j_mayer
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 008ff9d7 j_mayer
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 008ff9d7 j_mayer
 * THE SOFTWARE.
23 008ff9d7 j_mayer
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "ppc.h"
26 008ff9d7 j_mayer
#include "ppc4xx.h"
27 3b3fb322 blueswir1
#include "qemu-log.h"
28 b6dcbe08 Avi Kivity
#include "exec-memory.h"
29 008ff9d7 j_mayer
30 008ff9d7 j_mayer
//#define DEBUG_MMIO
31 aae9366a j_mayer
//#define DEBUG_UNASSIGNED
32 008ff9d7 j_mayer
#define DEBUG_UIC
33 008ff9d7 j_mayer
34 d12d51d5 aliguori
35 d12d51d5 aliguori
#ifdef DEBUG_UIC
36 93fcfe39 aliguori
#  define LOG_UIC(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
37 d12d51d5 aliguori
#else
38 d12d51d5 aliguori
#  define LOG_UIC(...) do { } while (0)
39 d12d51d5 aliguori
#endif
40 d12d51d5 aliguori
41 1bba0dc9 Andreas Färber
static void ppc4xx_reset(void *opaque)
42 1bba0dc9 Andreas Färber
{
43 90cb09d9 Andreas Färber
    PowerPCCPU *cpu = opaque;
44 1bba0dc9 Andreas Färber
45 90cb09d9 Andreas Färber
    cpu_reset(CPU(cpu));
46 1bba0dc9 Andreas Färber
}
47 1bba0dc9 Andreas Färber
48 008ff9d7 j_mayer
/*****************************************************************************/
49 60b14d95 Stefan Weil
/* Generic PowerPC 4xx processor instantiation */
50 e2684c0b Andreas Färber
CPUPPCState *ppc4xx_init (const char *cpu_model,
51 c227f099 Anthony Liguori
                       clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
52 008ff9d7 j_mayer
                       uint32_t sysclk)
53 008ff9d7 j_mayer
{
54 57274713 Andreas Färber
    PowerPCCPU *cpu;
55 e2684c0b Andreas Färber
    CPUPPCState *env;
56 008ff9d7 j_mayer
57 008ff9d7 j_mayer
    /* init CPUs */
58 57274713 Andreas Färber
    cpu = cpu_ppc_init(cpu_model);
59 57274713 Andreas Färber
    if (cpu == NULL) {
60 aaed909a bellard
        fprintf(stderr, "Unable to find PowerPC %s CPU definition\n",
61 aaed909a bellard
                cpu_model);
62 aaed909a bellard
        exit(1);
63 008ff9d7 j_mayer
    }
64 57274713 Andreas Färber
    env = &cpu->env;
65 57274713 Andreas Färber
66 008ff9d7 j_mayer
    cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
67 008ff9d7 j_mayer
    cpu_clk->opaque = env;
68 008ff9d7 j_mayer
    /* Set time-base frequency to sysclk */
69 ddd1055b Fabien Chouteau
    tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
70 008ff9d7 j_mayer
    tb_clk->opaque = env;
71 008ff9d7 j_mayer
    ppc_dcr_init(env, NULL, NULL);
72 008ff9d7 j_mayer
    /* Register qemu callbacks */
73 90cb09d9 Andreas Färber
    qemu_register_reset(ppc4xx_reset, cpu);
74 008ff9d7 j_mayer
75 008ff9d7 j_mayer
    return env;
76 008ff9d7 j_mayer
}
77 008ff9d7 j_mayer
78 008ff9d7 j_mayer
/*****************************************************************************/
79 008ff9d7 j_mayer
/* "Universal" Interrupt controller */
80 008ff9d7 j_mayer
enum {
81 008ff9d7 j_mayer
    DCR_UICSR  = 0x000,
82 008ff9d7 j_mayer
    DCR_UICSRS = 0x001,
83 008ff9d7 j_mayer
    DCR_UICER  = 0x002,
84 008ff9d7 j_mayer
    DCR_UICCR  = 0x003,
85 008ff9d7 j_mayer
    DCR_UICPR  = 0x004,
86 008ff9d7 j_mayer
    DCR_UICTR  = 0x005,
87 008ff9d7 j_mayer
    DCR_UICMSR = 0x006,
88 008ff9d7 j_mayer
    DCR_UICVR  = 0x007,
89 008ff9d7 j_mayer
    DCR_UICVCR = 0x008,
90 008ff9d7 j_mayer
    DCR_UICMAX = 0x009,
91 008ff9d7 j_mayer
};
92 008ff9d7 j_mayer
93 008ff9d7 j_mayer
#define UIC_MAX_IRQ 32
94 c227f099 Anthony Liguori
typedef struct ppcuic_t ppcuic_t;
95 c227f099 Anthony Liguori
struct ppcuic_t {
96 008ff9d7 j_mayer
    uint32_t dcr_base;
97 008ff9d7 j_mayer
    int use_vectors;
98 4c54e875 aurel32
    uint32_t level;  /* Remembers the state of level-triggered interrupts. */
99 008ff9d7 j_mayer
    uint32_t uicsr;  /* Status register */
100 008ff9d7 j_mayer
    uint32_t uicer;  /* Enable register */
101 008ff9d7 j_mayer
    uint32_t uiccr;  /* Critical register */
102 008ff9d7 j_mayer
    uint32_t uicpr;  /* Polarity register */
103 008ff9d7 j_mayer
    uint32_t uictr;  /* Triggering register */
104 008ff9d7 j_mayer
    uint32_t uicvcr; /* Vector configuration register */
105 008ff9d7 j_mayer
    uint32_t uicvr;
106 008ff9d7 j_mayer
    qemu_irq *irqs;
107 008ff9d7 j_mayer
};
108 008ff9d7 j_mayer
109 c227f099 Anthony Liguori
static void ppcuic_trigger_irq (ppcuic_t *uic)
110 008ff9d7 j_mayer
{
111 008ff9d7 j_mayer
    uint32_t ir, cr;
112 008ff9d7 j_mayer
    int start, end, inc, i;
113 008ff9d7 j_mayer
114 008ff9d7 j_mayer
    /* Trigger interrupt if any is pending */
115 008ff9d7 j_mayer
    ir = uic->uicsr & uic->uicer & (~uic->uiccr);
116 008ff9d7 j_mayer
    cr = uic->uicsr & uic->uicer & uic->uiccr;
117 d12d51d5 aliguori
    LOG_UIC("%s: uicsr %08" PRIx32 " uicer %08" PRIx32
118 aae9366a j_mayer
                " uiccr %08" PRIx32 "\n"
119 aae9366a j_mayer
                "   %08" PRIx32 " ir %08" PRIx32 " cr %08" PRIx32 "\n",
120 aae9366a j_mayer
                __func__, uic->uicsr, uic->uicer, uic->uiccr,
121 008ff9d7 j_mayer
                uic->uicsr & uic->uicer, ir, cr);
122 008ff9d7 j_mayer
    if (ir != 0x0000000) {
123 d12d51d5 aliguori
        LOG_UIC("Raise UIC interrupt\n");
124 008ff9d7 j_mayer
        qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_INT]);
125 008ff9d7 j_mayer
    } else {
126 d12d51d5 aliguori
        LOG_UIC("Lower UIC interrupt\n");
127 008ff9d7 j_mayer
        qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_INT]);
128 008ff9d7 j_mayer
    }
129 008ff9d7 j_mayer
    /* Trigger critical interrupt if any is pending and update vector */
130 008ff9d7 j_mayer
    if (cr != 0x0000000) {
131 008ff9d7 j_mayer
        qemu_irq_raise(uic->irqs[PPCUIC_OUTPUT_CINT]);
132 008ff9d7 j_mayer
        if (uic->use_vectors) {
133 008ff9d7 j_mayer
            /* Compute critical IRQ vector */
134 008ff9d7 j_mayer
            if (uic->uicvcr & 1) {
135 008ff9d7 j_mayer
                start = 31;
136 008ff9d7 j_mayer
                end = 0;
137 008ff9d7 j_mayer
                inc = -1;
138 008ff9d7 j_mayer
            } else {
139 008ff9d7 j_mayer
                start = 0;
140 008ff9d7 j_mayer
                end = 31;
141 008ff9d7 j_mayer
                inc = 1;
142 008ff9d7 j_mayer
            }
143 008ff9d7 j_mayer
            uic->uicvr = uic->uicvcr & 0xFFFFFFFC;
144 008ff9d7 j_mayer
            for (i = start; i <= end; i += inc) {
145 008ff9d7 j_mayer
                if (cr & (1 << i)) {
146 008ff9d7 j_mayer
                    uic->uicvr += (i - start) * 512 * inc;
147 008ff9d7 j_mayer
                    break;
148 008ff9d7 j_mayer
                }
149 008ff9d7 j_mayer
            }
150 008ff9d7 j_mayer
        }
151 d12d51d5 aliguori
        LOG_UIC("Raise UIC critical interrupt - "
152 aae9366a j_mayer
                    "vector %08" PRIx32 "\n", uic->uicvr);
153 008ff9d7 j_mayer
    } else {
154 d12d51d5 aliguori
        LOG_UIC("Lower UIC critical interrupt\n");
155 008ff9d7 j_mayer
        qemu_irq_lower(uic->irqs[PPCUIC_OUTPUT_CINT]);
156 008ff9d7 j_mayer
        uic->uicvr = 0x00000000;
157 008ff9d7 j_mayer
    }
158 008ff9d7 j_mayer
}
159 008ff9d7 j_mayer
160 008ff9d7 j_mayer
static void ppcuic_set_irq (void *opaque, int irq_num, int level)
161 008ff9d7 j_mayer
{
162 c227f099 Anthony Liguori
    ppcuic_t *uic;
163 008ff9d7 j_mayer
    uint32_t mask, sr;
164 008ff9d7 j_mayer
165 008ff9d7 j_mayer
    uic = opaque;
166 923e5e33 aurel32
    mask = 1 << (31-irq_num);
167 d12d51d5 aliguori
    LOG_UIC("%s: irq %d level %d uicsr %08" PRIx32
168 aae9366a j_mayer
                " mask %08" PRIx32 " => %08" PRIx32 " %08" PRIx32 "\n",
169 aae9366a j_mayer
                __func__, irq_num, level,
170 008ff9d7 j_mayer
                uic->uicsr, mask, uic->uicsr & mask, level << irq_num);
171 008ff9d7 j_mayer
    if (irq_num < 0 || irq_num > 31)
172 008ff9d7 j_mayer
        return;
173 008ff9d7 j_mayer
    sr = uic->uicsr;
174 50bf72b3 aurel32
175 008ff9d7 j_mayer
    /* Update status register */
176 008ff9d7 j_mayer
    if (uic->uictr & mask) {
177 008ff9d7 j_mayer
        /* Edge sensitive interrupt */
178 008ff9d7 j_mayer
        if (level == 1)
179 008ff9d7 j_mayer
            uic->uicsr |= mask;
180 008ff9d7 j_mayer
    } else {
181 008ff9d7 j_mayer
        /* Level sensitive interrupt */
182 4c54e875 aurel32
        if (level == 1) {
183 008ff9d7 j_mayer
            uic->uicsr |= mask;
184 4c54e875 aurel32
            uic->level |= mask;
185 4c54e875 aurel32
        } else {
186 008ff9d7 j_mayer
            uic->uicsr &= ~mask;
187 4c54e875 aurel32
            uic->level &= ~mask;
188 4c54e875 aurel32
        }
189 008ff9d7 j_mayer
    }
190 d12d51d5 aliguori
    LOG_UIC("%s: irq %d level %d sr %" PRIx32 " => "
191 aae9366a j_mayer
                "%08" PRIx32 "\n", __func__, irq_num, level, uic->uicsr, sr);
192 008ff9d7 j_mayer
    if (sr != uic->uicsr)
193 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
194 008ff9d7 j_mayer
}
195 008ff9d7 j_mayer
196 73b01960 Alexander Graf
static uint32_t dcr_read_uic (void *opaque, int dcrn)
197 008ff9d7 j_mayer
{
198 c227f099 Anthony Liguori
    ppcuic_t *uic;
199 73b01960 Alexander Graf
    uint32_t ret;
200 008ff9d7 j_mayer
201 008ff9d7 j_mayer
    uic = opaque;
202 008ff9d7 j_mayer
    dcrn -= uic->dcr_base;
203 008ff9d7 j_mayer
    switch (dcrn) {
204 008ff9d7 j_mayer
    case DCR_UICSR:
205 008ff9d7 j_mayer
    case DCR_UICSRS:
206 008ff9d7 j_mayer
        ret = uic->uicsr;
207 008ff9d7 j_mayer
        break;
208 008ff9d7 j_mayer
    case DCR_UICER:
209 008ff9d7 j_mayer
        ret = uic->uicer;
210 008ff9d7 j_mayer
        break;
211 008ff9d7 j_mayer
    case DCR_UICCR:
212 008ff9d7 j_mayer
        ret = uic->uiccr;
213 008ff9d7 j_mayer
        break;
214 008ff9d7 j_mayer
    case DCR_UICPR:
215 008ff9d7 j_mayer
        ret = uic->uicpr;
216 008ff9d7 j_mayer
        break;
217 008ff9d7 j_mayer
    case DCR_UICTR:
218 008ff9d7 j_mayer
        ret = uic->uictr;
219 008ff9d7 j_mayer
        break;
220 008ff9d7 j_mayer
    case DCR_UICMSR:
221 008ff9d7 j_mayer
        ret = uic->uicsr & uic->uicer;
222 008ff9d7 j_mayer
        break;
223 008ff9d7 j_mayer
    case DCR_UICVR:
224 008ff9d7 j_mayer
        if (!uic->use_vectors)
225 008ff9d7 j_mayer
            goto no_read;
226 008ff9d7 j_mayer
        ret = uic->uicvr;
227 008ff9d7 j_mayer
        break;
228 008ff9d7 j_mayer
    case DCR_UICVCR:
229 008ff9d7 j_mayer
        if (!uic->use_vectors)
230 008ff9d7 j_mayer
            goto no_read;
231 008ff9d7 j_mayer
        ret = uic->uicvcr;
232 008ff9d7 j_mayer
        break;
233 008ff9d7 j_mayer
    default:
234 008ff9d7 j_mayer
    no_read:
235 008ff9d7 j_mayer
        ret = 0x00000000;
236 008ff9d7 j_mayer
        break;
237 008ff9d7 j_mayer
    }
238 008ff9d7 j_mayer
239 008ff9d7 j_mayer
    return ret;
240 008ff9d7 j_mayer
}
241 008ff9d7 j_mayer
242 73b01960 Alexander Graf
static void dcr_write_uic (void *opaque, int dcrn, uint32_t val)
243 008ff9d7 j_mayer
{
244 c227f099 Anthony Liguori
    ppcuic_t *uic;
245 008ff9d7 j_mayer
246 008ff9d7 j_mayer
    uic = opaque;
247 008ff9d7 j_mayer
    dcrn -= uic->dcr_base;
248 73b01960 Alexander Graf
    LOG_UIC("%s: dcr %d val 0x%x\n", __func__, dcrn, val);
249 008ff9d7 j_mayer
    switch (dcrn) {
250 008ff9d7 j_mayer
    case DCR_UICSR:
251 008ff9d7 j_mayer
        uic->uicsr &= ~val;
252 4c54e875 aurel32
        uic->uicsr |= uic->level;
253 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
254 008ff9d7 j_mayer
        break;
255 008ff9d7 j_mayer
    case DCR_UICSRS:
256 008ff9d7 j_mayer
        uic->uicsr |= val;
257 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
258 008ff9d7 j_mayer
        break;
259 008ff9d7 j_mayer
    case DCR_UICER:
260 008ff9d7 j_mayer
        uic->uicer = val;
261 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
262 008ff9d7 j_mayer
        break;
263 008ff9d7 j_mayer
    case DCR_UICCR:
264 008ff9d7 j_mayer
        uic->uiccr = val;
265 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
266 008ff9d7 j_mayer
        break;
267 008ff9d7 j_mayer
    case DCR_UICPR:
268 008ff9d7 j_mayer
        uic->uicpr = val;
269 008ff9d7 j_mayer
        break;
270 008ff9d7 j_mayer
    case DCR_UICTR:
271 008ff9d7 j_mayer
        uic->uictr = val;
272 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
273 008ff9d7 j_mayer
        break;
274 008ff9d7 j_mayer
    case DCR_UICMSR:
275 008ff9d7 j_mayer
        break;
276 008ff9d7 j_mayer
    case DCR_UICVR:
277 008ff9d7 j_mayer
        break;
278 008ff9d7 j_mayer
    case DCR_UICVCR:
279 008ff9d7 j_mayer
        uic->uicvcr = val & 0xFFFFFFFD;
280 008ff9d7 j_mayer
        ppcuic_trigger_irq(uic);
281 008ff9d7 j_mayer
        break;
282 008ff9d7 j_mayer
    }
283 008ff9d7 j_mayer
}
284 008ff9d7 j_mayer
285 008ff9d7 j_mayer
static void ppcuic_reset (void *opaque)
286 008ff9d7 j_mayer
{
287 c227f099 Anthony Liguori
    ppcuic_t *uic;
288 008ff9d7 j_mayer
289 008ff9d7 j_mayer
    uic = opaque;
290 008ff9d7 j_mayer
    uic->uiccr = 0x00000000;
291 008ff9d7 j_mayer
    uic->uicer = 0x00000000;
292 008ff9d7 j_mayer
    uic->uicpr = 0x00000000;
293 008ff9d7 j_mayer
    uic->uicsr = 0x00000000;
294 008ff9d7 j_mayer
    uic->uictr = 0x00000000;
295 008ff9d7 j_mayer
    if (uic->use_vectors) {
296 008ff9d7 j_mayer
        uic->uicvcr = 0x00000000;
297 008ff9d7 j_mayer
        uic->uicvr = 0x0000000;
298 008ff9d7 j_mayer
    }
299 008ff9d7 j_mayer
}
300 008ff9d7 j_mayer
301 e2684c0b Andreas Färber
qemu_irq *ppcuic_init (CPUPPCState *env, qemu_irq *irqs,
302 008ff9d7 j_mayer
                       uint32_t dcr_base, int has_ssr, int has_vr)
303 008ff9d7 j_mayer
{
304 c227f099 Anthony Liguori
    ppcuic_t *uic;
305 008ff9d7 j_mayer
    int i;
306 008ff9d7 j_mayer
307 7267c094 Anthony Liguori
    uic = g_malloc0(sizeof(ppcuic_t));
308 487414f1 aliguori
    uic->dcr_base = dcr_base;
309 487414f1 aliguori
    uic->irqs = irqs;
310 487414f1 aliguori
    if (has_vr)
311 487414f1 aliguori
        uic->use_vectors = 1;
312 487414f1 aliguori
    for (i = 0; i < DCR_UICMAX; i++) {
313 487414f1 aliguori
        ppc_dcr_register(env, dcr_base + i, uic,
314 487414f1 aliguori
                         &dcr_read_uic, &dcr_write_uic);
315 008ff9d7 j_mayer
    }
316 a08d4367 Jan Kiszka
    qemu_register_reset(ppcuic_reset, uic);
317 008ff9d7 j_mayer
318 008ff9d7 j_mayer
    return qemu_allocate_irqs(&ppcuic_set_irq, uic, UIC_MAX_IRQ);
319 008ff9d7 j_mayer
}
320 61b24405 aurel32
321 61b24405 aurel32
/*****************************************************************************/
322 61b24405 aurel32
/* SDRAM controller */
323 c227f099 Anthony Liguori
typedef struct ppc4xx_sdram_t ppc4xx_sdram_t;
324 c227f099 Anthony Liguori
struct ppc4xx_sdram_t {
325 61b24405 aurel32
    uint32_t addr;
326 61b24405 aurel32
    int nbanks;
327 b6dcbe08 Avi Kivity
    MemoryRegion containers[4]; /* used for clipping */
328 b6dcbe08 Avi Kivity
    MemoryRegion *ram_memories;
329 c227f099 Anthony Liguori
    target_phys_addr_t ram_bases[4];
330 c227f099 Anthony Liguori
    target_phys_addr_t ram_sizes[4];
331 61b24405 aurel32
    uint32_t besr0;
332 61b24405 aurel32
    uint32_t besr1;
333 61b24405 aurel32
    uint32_t bear;
334 61b24405 aurel32
    uint32_t cfg;
335 61b24405 aurel32
    uint32_t status;
336 61b24405 aurel32
    uint32_t rtr;
337 61b24405 aurel32
    uint32_t pmit;
338 61b24405 aurel32
    uint32_t bcr[4];
339 61b24405 aurel32
    uint32_t tr;
340 61b24405 aurel32
    uint32_t ecccfg;
341 61b24405 aurel32
    uint32_t eccesr;
342 61b24405 aurel32
    qemu_irq irq;
343 61b24405 aurel32
};
344 61b24405 aurel32
345 61b24405 aurel32
enum {
346 61b24405 aurel32
    SDRAM0_CFGADDR = 0x010,
347 61b24405 aurel32
    SDRAM0_CFGDATA = 0x011,
348 61b24405 aurel32
};
349 61b24405 aurel32
350 61b24405 aurel32
/* XXX: TOFIX: some patches have made this code become inconsistent:
351 c227f099 Anthony Liguori
 *      there are type inconsistencies, mixing target_phys_addr_t, target_ulong
352 61b24405 aurel32
 *      and uint32_t
353 61b24405 aurel32
 */
354 c227f099 Anthony Liguori
static uint32_t sdram_bcr (target_phys_addr_t ram_base,
355 c227f099 Anthony Liguori
                           target_phys_addr_t ram_size)
356 61b24405 aurel32
{
357 61b24405 aurel32
    uint32_t bcr;
358 61b24405 aurel32
359 61b24405 aurel32
    switch (ram_size) {
360 61b24405 aurel32
    case (4 * 1024 * 1024):
361 61b24405 aurel32
        bcr = 0x00000000;
362 61b24405 aurel32
        break;
363 61b24405 aurel32
    case (8 * 1024 * 1024):
364 61b24405 aurel32
        bcr = 0x00020000;
365 61b24405 aurel32
        break;
366 61b24405 aurel32
    case (16 * 1024 * 1024):
367 61b24405 aurel32
        bcr = 0x00040000;
368 61b24405 aurel32
        break;
369 61b24405 aurel32
    case (32 * 1024 * 1024):
370 61b24405 aurel32
        bcr = 0x00060000;
371 61b24405 aurel32
        break;
372 61b24405 aurel32
    case (64 * 1024 * 1024):
373 61b24405 aurel32
        bcr = 0x00080000;
374 61b24405 aurel32
        break;
375 61b24405 aurel32
    case (128 * 1024 * 1024):
376 61b24405 aurel32
        bcr = 0x000A0000;
377 61b24405 aurel32
        break;
378 61b24405 aurel32
    case (256 * 1024 * 1024):
379 61b24405 aurel32
        bcr = 0x000C0000;
380 61b24405 aurel32
        break;
381 61b24405 aurel32
    default:
382 90e189ec Blue Swirl
        printf("%s: invalid RAM size " TARGET_FMT_plx "\n", __func__,
383 90e189ec Blue Swirl
               ram_size);
384 61b24405 aurel32
        return 0x00000000;
385 61b24405 aurel32
    }
386 61b24405 aurel32
    bcr |= ram_base & 0xFF800000;
387 61b24405 aurel32
    bcr |= 1;
388 61b24405 aurel32
389 61b24405 aurel32
    return bcr;
390 61b24405 aurel32
}
391 61b24405 aurel32
392 c227f099 Anthony Liguori
static inline target_phys_addr_t sdram_base(uint32_t bcr)
393 61b24405 aurel32
{
394 61b24405 aurel32
    return bcr & 0xFF800000;
395 61b24405 aurel32
}
396 61b24405 aurel32
397 61b24405 aurel32
static target_ulong sdram_size (uint32_t bcr)
398 61b24405 aurel32
{
399 61b24405 aurel32
    target_ulong size;
400 61b24405 aurel32
    int sh;
401 61b24405 aurel32
402 61b24405 aurel32
    sh = (bcr >> 17) & 0x7;
403 61b24405 aurel32
    if (sh == 7)
404 61b24405 aurel32
        size = -1;
405 61b24405 aurel32
    else
406 61b24405 aurel32
        size = (4 * 1024 * 1024) << sh;
407 61b24405 aurel32
408 61b24405 aurel32
    return size;
409 61b24405 aurel32
}
410 61b24405 aurel32
411 b6dcbe08 Avi Kivity
static void sdram_set_bcr(ppc4xx_sdram_t *sdram,
412 b6dcbe08 Avi Kivity
                          uint32_t *bcrp, uint32_t bcr, int enabled)
413 61b24405 aurel32
{
414 b6dcbe08 Avi Kivity
    unsigned n = bcrp - sdram->bcr;
415 b6dcbe08 Avi Kivity
416 61b24405 aurel32
    if (*bcrp & 0x00000001) {
417 61b24405 aurel32
        /* Unmap RAM */
418 61b24405 aurel32
#ifdef DEBUG_SDRAM
419 90e189ec Blue Swirl
        printf("%s: unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
420 61b24405 aurel32
               __func__, sdram_base(*bcrp), sdram_size(*bcrp));
421 61b24405 aurel32
#endif
422 b6dcbe08 Avi Kivity
        memory_region_del_subregion(get_system_memory(),
423 b6dcbe08 Avi Kivity
                                    &sdram->containers[n]);
424 b6dcbe08 Avi Kivity
        memory_region_del_subregion(&sdram->containers[n],
425 b6dcbe08 Avi Kivity
                                    &sdram->ram_memories[n]);
426 b6dcbe08 Avi Kivity
        memory_region_destroy(&sdram->containers[n]);
427 61b24405 aurel32
    }
428 61b24405 aurel32
    *bcrp = bcr & 0xFFDEE001;
429 61b24405 aurel32
    if (enabled && (bcr & 0x00000001)) {
430 61b24405 aurel32
#ifdef DEBUG_SDRAM
431 90e189ec Blue Swirl
        printf("%s: Map RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
432 61b24405 aurel32
               __func__, sdram_base(bcr), sdram_size(bcr));
433 61b24405 aurel32
#endif
434 b6dcbe08 Avi Kivity
        memory_region_init(&sdram->containers[n], "sdram-containers",
435 b6dcbe08 Avi Kivity
                           sdram_size(bcr));
436 b6dcbe08 Avi Kivity
        memory_region_add_subregion(&sdram->containers[n], 0,
437 b6dcbe08 Avi Kivity
                                    &sdram->ram_memories[n]);
438 b6dcbe08 Avi Kivity
        memory_region_add_subregion(get_system_memory(),
439 b6dcbe08 Avi Kivity
                                    sdram_base(bcr),
440 b6dcbe08 Avi Kivity
                                    &sdram->containers[n]);
441 61b24405 aurel32
    }
442 61b24405 aurel32
}
443 61b24405 aurel32
444 c227f099 Anthony Liguori
static void sdram_map_bcr (ppc4xx_sdram_t *sdram)
445 61b24405 aurel32
{
446 61b24405 aurel32
    int i;
447 61b24405 aurel32
448 61b24405 aurel32
    for (i = 0; i < sdram->nbanks; i++) {
449 61b24405 aurel32
        if (sdram->ram_sizes[i] != 0) {
450 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram,
451 b6dcbe08 Avi Kivity
                          &sdram->bcr[i],
452 61b24405 aurel32
                          sdram_bcr(sdram->ram_bases[i], sdram->ram_sizes[i]),
453 61b24405 aurel32
                          1);
454 61b24405 aurel32
        } else {
455 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram, &sdram->bcr[i], 0x00000000, 0);
456 61b24405 aurel32
        }
457 61b24405 aurel32
    }
458 61b24405 aurel32
}
459 61b24405 aurel32
460 c227f099 Anthony Liguori
static void sdram_unmap_bcr (ppc4xx_sdram_t *sdram)
461 61b24405 aurel32
{
462 61b24405 aurel32
    int i;
463 61b24405 aurel32
464 61b24405 aurel32
    for (i = 0; i < sdram->nbanks; i++) {
465 61b24405 aurel32
#ifdef DEBUG_SDRAM
466 90e189ec Blue Swirl
        printf("%s: Unmap RAM area " TARGET_FMT_plx " " TARGET_FMT_lx "\n",
467 61b24405 aurel32
               __func__, sdram_base(sdram->bcr[i]), sdram_size(sdram->bcr[i]));
468 61b24405 aurel32
#endif
469 b6dcbe08 Avi Kivity
        memory_region_del_subregion(get_system_memory(),
470 b6dcbe08 Avi Kivity
                                    &sdram->ram_memories[i]);
471 61b24405 aurel32
    }
472 61b24405 aurel32
}
473 61b24405 aurel32
474 73b01960 Alexander Graf
static uint32_t dcr_read_sdram (void *opaque, int dcrn)
475 61b24405 aurel32
{
476 c227f099 Anthony Liguori
    ppc4xx_sdram_t *sdram;
477 73b01960 Alexander Graf
    uint32_t ret;
478 61b24405 aurel32
479 61b24405 aurel32
    sdram = opaque;
480 61b24405 aurel32
    switch (dcrn) {
481 61b24405 aurel32
    case SDRAM0_CFGADDR:
482 61b24405 aurel32
        ret = sdram->addr;
483 61b24405 aurel32
        break;
484 61b24405 aurel32
    case SDRAM0_CFGDATA:
485 61b24405 aurel32
        switch (sdram->addr) {
486 61b24405 aurel32
        case 0x00: /* SDRAM_BESR0 */
487 61b24405 aurel32
            ret = sdram->besr0;
488 61b24405 aurel32
            break;
489 61b24405 aurel32
        case 0x08: /* SDRAM_BESR1 */
490 61b24405 aurel32
            ret = sdram->besr1;
491 61b24405 aurel32
            break;
492 61b24405 aurel32
        case 0x10: /* SDRAM_BEAR */
493 61b24405 aurel32
            ret = sdram->bear;
494 61b24405 aurel32
            break;
495 61b24405 aurel32
        case 0x20: /* SDRAM_CFG */
496 61b24405 aurel32
            ret = sdram->cfg;
497 61b24405 aurel32
            break;
498 61b24405 aurel32
        case 0x24: /* SDRAM_STATUS */
499 61b24405 aurel32
            ret = sdram->status;
500 61b24405 aurel32
            break;
501 61b24405 aurel32
        case 0x30: /* SDRAM_RTR */
502 61b24405 aurel32
            ret = sdram->rtr;
503 61b24405 aurel32
            break;
504 61b24405 aurel32
        case 0x34: /* SDRAM_PMIT */
505 61b24405 aurel32
            ret = sdram->pmit;
506 61b24405 aurel32
            break;
507 61b24405 aurel32
        case 0x40: /* SDRAM_B0CR */
508 61b24405 aurel32
            ret = sdram->bcr[0];
509 61b24405 aurel32
            break;
510 61b24405 aurel32
        case 0x44: /* SDRAM_B1CR */
511 61b24405 aurel32
            ret = sdram->bcr[1];
512 61b24405 aurel32
            break;
513 61b24405 aurel32
        case 0x48: /* SDRAM_B2CR */
514 61b24405 aurel32
            ret = sdram->bcr[2];
515 61b24405 aurel32
            break;
516 61b24405 aurel32
        case 0x4C: /* SDRAM_B3CR */
517 61b24405 aurel32
            ret = sdram->bcr[3];
518 61b24405 aurel32
            break;
519 61b24405 aurel32
        case 0x80: /* SDRAM_TR */
520 61b24405 aurel32
            ret = -1; /* ? */
521 61b24405 aurel32
            break;
522 61b24405 aurel32
        case 0x94: /* SDRAM_ECCCFG */
523 61b24405 aurel32
            ret = sdram->ecccfg;
524 61b24405 aurel32
            break;
525 61b24405 aurel32
        case 0x98: /* SDRAM_ECCESR */
526 61b24405 aurel32
            ret = sdram->eccesr;
527 61b24405 aurel32
            break;
528 61b24405 aurel32
        default: /* Error */
529 61b24405 aurel32
            ret = -1;
530 61b24405 aurel32
            break;
531 61b24405 aurel32
        }
532 61b24405 aurel32
        break;
533 61b24405 aurel32
    default:
534 61b24405 aurel32
        /* Avoid gcc warning */
535 61b24405 aurel32
        ret = 0x00000000;
536 61b24405 aurel32
        break;
537 61b24405 aurel32
    }
538 61b24405 aurel32
539 61b24405 aurel32
    return ret;
540 61b24405 aurel32
}
541 61b24405 aurel32
542 73b01960 Alexander Graf
static void dcr_write_sdram (void *opaque, int dcrn, uint32_t val)
543 61b24405 aurel32
{
544 c227f099 Anthony Liguori
    ppc4xx_sdram_t *sdram;
545 61b24405 aurel32
546 61b24405 aurel32
    sdram = opaque;
547 61b24405 aurel32
    switch (dcrn) {
548 61b24405 aurel32
    case SDRAM0_CFGADDR:
549 61b24405 aurel32
        sdram->addr = val;
550 61b24405 aurel32
        break;
551 61b24405 aurel32
    case SDRAM0_CFGDATA:
552 61b24405 aurel32
        switch (sdram->addr) {
553 61b24405 aurel32
        case 0x00: /* SDRAM_BESR0 */
554 61b24405 aurel32
            sdram->besr0 &= ~val;
555 61b24405 aurel32
            break;
556 61b24405 aurel32
        case 0x08: /* SDRAM_BESR1 */
557 61b24405 aurel32
            sdram->besr1 &= ~val;
558 61b24405 aurel32
            break;
559 61b24405 aurel32
        case 0x10: /* SDRAM_BEAR */
560 61b24405 aurel32
            sdram->bear = val;
561 61b24405 aurel32
            break;
562 61b24405 aurel32
        case 0x20: /* SDRAM_CFG */
563 61b24405 aurel32
            val &= 0xFFE00000;
564 61b24405 aurel32
            if (!(sdram->cfg & 0x80000000) && (val & 0x80000000)) {
565 61b24405 aurel32
#ifdef DEBUG_SDRAM
566 61b24405 aurel32
                printf("%s: enable SDRAM controller\n", __func__);
567 61b24405 aurel32
#endif
568 61b24405 aurel32
                /* validate all RAM mappings */
569 61b24405 aurel32
                sdram_map_bcr(sdram);
570 61b24405 aurel32
                sdram->status &= ~0x80000000;
571 61b24405 aurel32
            } else if ((sdram->cfg & 0x80000000) && !(val & 0x80000000)) {
572 61b24405 aurel32
#ifdef DEBUG_SDRAM
573 61b24405 aurel32
                printf("%s: disable SDRAM controller\n", __func__);
574 61b24405 aurel32
#endif
575 61b24405 aurel32
                /* invalidate all RAM mappings */
576 61b24405 aurel32
                sdram_unmap_bcr(sdram);
577 61b24405 aurel32
                sdram->status |= 0x80000000;
578 61b24405 aurel32
            }
579 61b24405 aurel32
            if (!(sdram->cfg & 0x40000000) && (val & 0x40000000))
580 61b24405 aurel32
                sdram->status |= 0x40000000;
581 61b24405 aurel32
            else if ((sdram->cfg & 0x40000000) && !(val & 0x40000000))
582 61b24405 aurel32
                sdram->status &= ~0x40000000;
583 61b24405 aurel32
            sdram->cfg = val;
584 61b24405 aurel32
            break;
585 61b24405 aurel32
        case 0x24: /* SDRAM_STATUS */
586 61b24405 aurel32
            /* Read-only register */
587 61b24405 aurel32
            break;
588 61b24405 aurel32
        case 0x30: /* SDRAM_RTR */
589 61b24405 aurel32
            sdram->rtr = val & 0x3FF80000;
590 61b24405 aurel32
            break;
591 61b24405 aurel32
        case 0x34: /* SDRAM_PMIT */
592 61b24405 aurel32
            sdram->pmit = (val & 0xF8000000) | 0x07C00000;
593 61b24405 aurel32
            break;
594 61b24405 aurel32
        case 0x40: /* SDRAM_B0CR */
595 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram, &sdram->bcr[0], val, sdram->cfg & 0x80000000);
596 61b24405 aurel32
            break;
597 61b24405 aurel32
        case 0x44: /* SDRAM_B1CR */
598 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram, &sdram->bcr[1], val, sdram->cfg & 0x80000000);
599 61b24405 aurel32
            break;
600 61b24405 aurel32
        case 0x48: /* SDRAM_B2CR */
601 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram, &sdram->bcr[2], val, sdram->cfg & 0x80000000);
602 61b24405 aurel32
            break;
603 61b24405 aurel32
        case 0x4C: /* SDRAM_B3CR */
604 b6dcbe08 Avi Kivity
            sdram_set_bcr(sdram, &sdram->bcr[3], val, sdram->cfg & 0x80000000);
605 61b24405 aurel32
            break;
606 61b24405 aurel32
        case 0x80: /* SDRAM_TR */
607 61b24405 aurel32
            sdram->tr = val & 0x018FC01F;
608 61b24405 aurel32
            break;
609 61b24405 aurel32
        case 0x94: /* SDRAM_ECCCFG */
610 61b24405 aurel32
            sdram->ecccfg = val & 0x00F00000;
611 61b24405 aurel32
            break;
612 61b24405 aurel32
        case 0x98: /* SDRAM_ECCESR */
613 61b24405 aurel32
            val &= 0xFFF0F000;
614 61b24405 aurel32
            if (sdram->eccesr == 0 && val != 0)
615 61b24405 aurel32
                qemu_irq_raise(sdram->irq);
616 61b24405 aurel32
            else if (sdram->eccesr != 0 && val == 0)
617 61b24405 aurel32
                qemu_irq_lower(sdram->irq);
618 61b24405 aurel32
            sdram->eccesr = val;
619 61b24405 aurel32
            break;
620 61b24405 aurel32
        default: /* Error */
621 61b24405 aurel32
            break;
622 61b24405 aurel32
        }
623 61b24405 aurel32
        break;
624 61b24405 aurel32
    }
625 61b24405 aurel32
}
626 61b24405 aurel32
627 61b24405 aurel32
static void sdram_reset (void *opaque)
628 61b24405 aurel32
{
629 c227f099 Anthony Liguori
    ppc4xx_sdram_t *sdram;
630 61b24405 aurel32
631 61b24405 aurel32
    sdram = opaque;
632 61b24405 aurel32
    sdram->addr = 0x00000000;
633 61b24405 aurel32
    sdram->bear = 0x00000000;
634 61b24405 aurel32
    sdram->besr0 = 0x00000000; /* No error */
635 61b24405 aurel32
    sdram->besr1 = 0x00000000; /* No error */
636 61b24405 aurel32
    sdram->cfg = 0x00000000;
637 61b24405 aurel32
    sdram->ecccfg = 0x00000000; /* No ECC */
638 61b24405 aurel32
    sdram->eccesr = 0x00000000; /* No error */
639 61b24405 aurel32
    sdram->pmit = 0x07C00000;
640 61b24405 aurel32
    sdram->rtr = 0x05F00000;
641 61b24405 aurel32
    sdram->tr = 0x00854009;
642 61b24405 aurel32
    /* We pre-initialize RAM banks */
643 61b24405 aurel32
    sdram->status = 0x00000000;
644 61b24405 aurel32
    sdram->cfg = 0x00800000;
645 61b24405 aurel32
}
646 61b24405 aurel32
647 e2684c0b Andreas Färber
void ppc4xx_sdram_init (CPUPPCState *env, qemu_irq irq, int nbanks,
648 b6dcbe08 Avi Kivity
                        MemoryRegion *ram_memories,
649 c227f099 Anthony Liguori
                        target_phys_addr_t *ram_bases,
650 c227f099 Anthony Liguori
                        target_phys_addr_t *ram_sizes,
651 61b24405 aurel32
                        int do_init)
652 61b24405 aurel32
{
653 c227f099 Anthony Liguori
    ppc4xx_sdram_t *sdram;
654 61b24405 aurel32
655 7267c094 Anthony Liguori
    sdram = g_malloc0(sizeof(ppc4xx_sdram_t));
656 487414f1 aliguori
    sdram->irq = irq;
657 487414f1 aliguori
    sdram->nbanks = nbanks;
658 b6dcbe08 Avi Kivity
    sdram->ram_memories = ram_memories;
659 c227f099 Anthony Liguori
    memset(sdram->ram_bases, 0, 4 * sizeof(target_phys_addr_t));
660 487414f1 aliguori
    memcpy(sdram->ram_bases, ram_bases,
661 c227f099 Anthony Liguori
           nbanks * sizeof(target_phys_addr_t));
662 c227f099 Anthony Liguori
    memset(sdram->ram_sizes, 0, 4 * sizeof(target_phys_addr_t));
663 487414f1 aliguori
    memcpy(sdram->ram_sizes, ram_sizes,
664 c227f099 Anthony Liguori
           nbanks * sizeof(target_phys_addr_t));
665 a08d4367 Jan Kiszka
    qemu_register_reset(&sdram_reset, sdram);
666 487414f1 aliguori
    ppc_dcr_register(env, SDRAM0_CFGADDR,
667 487414f1 aliguori
                     sdram, &dcr_read_sdram, &dcr_write_sdram);
668 487414f1 aliguori
    ppc_dcr_register(env, SDRAM0_CFGDATA,
669 487414f1 aliguori
                     sdram, &dcr_read_sdram, &dcr_write_sdram);
670 487414f1 aliguori
    if (do_init)
671 487414f1 aliguori
        sdram_map_bcr(sdram);
672 61b24405 aurel32
}
673 b7da58fd aurel32
674 b7da58fd aurel32
/* Fill in consecutive SDRAM banks with 'ram_size' bytes of memory.
675 b7da58fd aurel32
 *
676 b7da58fd aurel32
 * sdram_bank_sizes[] must be 0-terminated.
677 b7da58fd aurel32
 *
678 b7da58fd aurel32
 * The 4xx SDRAM controller supports a small number of banks, and each bank
679 b7da58fd aurel32
 * must be one of a small set of sizes. The number of banks and the supported
680 b7da58fd aurel32
 * sizes varies by SoC. */
681 c227f099 Anthony Liguori
ram_addr_t ppc4xx_sdram_adjust(ram_addr_t ram_size, int nr_banks,
682 b6dcbe08 Avi Kivity
                               MemoryRegion ram_memories[],
683 c227f099 Anthony Liguori
                               target_phys_addr_t ram_bases[],
684 c227f099 Anthony Liguori
                               target_phys_addr_t ram_sizes[],
685 b7da58fd aurel32
                               const unsigned int sdram_bank_sizes[])
686 b7da58fd aurel32
{
687 c227f099 Anthony Liguori
    ram_addr_t size_left = ram_size;
688 b6dcbe08 Avi Kivity
    ram_addr_t base = 0;
689 b7da58fd aurel32
    int i;
690 b7da58fd aurel32
    int j;
691 b7da58fd aurel32
692 b7da58fd aurel32
    for (i = 0; i < nr_banks; i++) {
693 b7da58fd aurel32
        for (j = 0; sdram_bank_sizes[j] != 0; j++) {
694 b7da58fd aurel32
            unsigned int bank_size = sdram_bank_sizes[j];
695 b7da58fd aurel32
696 5c130f65 pbrook
            if (bank_size <= size_left) {
697 1724f049 Alex Williamson
                char name[32];
698 1724f049 Alex Williamson
                snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
699 c5705a77 Avi Kivity
                memory_region_init_ram(&ram_memories[i], name, bank_size);
700 c5705a77 Avi Kivity
                vmstate_register_ram_global(&ram_memories[i]);
701 b6dcbe08 Avi Kivity
                ram_bases[i] = base;
702 b7da58fd aurel32
                ram_sizes[i] = bank_size;
703 b6dcbe08 Avi Kivity
                base += ram_size;
704 5c130f65 pbrook
                size_left -= bank_size;
705 b7da58fd aurel32
                break;
706 b7da58fd aurel32
            }
707 b7da58fd aurel32
        }
708 b7da58fd aurel32
709 5c130f65 pbrook
        if (!size_left) {
710 b7da58fd aurel32
            /* No need to use the remaining banks. */
711 b7da58fd aurel32
            break;
712 b7da58fd aurel32
        }
713 b7da58fd aurel32
    }
714 b7da58fd aurel32
715 5c130f65 pbrook
    ram_size -= size_left;
716 d23ab920 Hollis Blanchard
    if (size_left)
717 b7da58fd aurel32
        printf("Truncating memory to %d MiB to fit SDRAM controller limits.\n",
718 5c130f65 pbrook
               (int)(ram_size >> 20));
719 b7da58fd aurel32
720 5c130f65 pbrook
    return ram_size;
721 b7da58fd aurel32
}