Statistics
| Branch: | Revision:

root / hw / ppc4xx_devs.c @ fc4f0754

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