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