Statistics
| Branch: | Revision:

root / hw / ppc4xx_devs.c @ 06113719

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