xref: /openbmc/qemu/hw/ppc/ppc4xx_sdram.c (revision 54a3527e)
1 /*
2  * QEMU PowerPC 4xx embedded processors SDRAM controller emulation
3  *
4  * DDR SDRAM controller:
5  * Copyright (c) 2007 Jocelyn Mayer
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  * DDR2 SDRAM controller:
26  * Copyright (c) 2012 François Revol
27  * Copyright (c) 2016-2019 BALATON Zoltan
28  *
29  * This work is licensed under the GNU GPL license version 2 or later.
30  */
31 
32 #include "qemu/osdep.h"
33 #include "qemu/units.h"
34 #include "qapi/error.h"
35 #include "qemu/log.h"
36 #include "exec/address-spaces.h" /* get_system_memory() */
37 #include "hw/irq.h"
38 #include "hw/qdev-properties.h"
39 #include "hw/ppc/ppc4xx.h"
40 #include "trace.h"
41 
42 /*****************************************************************************/
43 /* Shared functions */
44 
45 /*
46  * Split RAM between SDRAM banks.
47  *
48  * sdram_bank_sizes[] must be in descending order, that is sizes[i] > sizes[i+1]
49  * and must be 0-terminated.
50  *
51  * The 4xx SDRAM controller supports a small number of banks, and each bank
52  * must be one of a small set of sizes. The number of banks and the supported
53  * sizes varies by SoC.
54  */
55 static void ppc4xx_sdram_banks(MemoryRegion *ram, int nr_banks,
56                                Ppc4xxSdramBank ram_banks[],
57                                const ram_addr_t sdram_bank_sizes[])
58 {
59     ram_addr_t size_left = memory_region_size(ram);
60     ram_addr_t base = 0;
61     ram_addr_t bank_size;
62     int i;
63     int j;
64 
65     for (i = 0; i < nr_banks; i++) {
66         for (j = 0; sdram_bank_sizes[j] != 0; j++) {
67             bank_size = sdram_bank_sizes[j];
68             if (bank_size <= size_left) {
69                 char name[32];
70 
71                 ram_banks[i].base = base;
72                 ram_banks[i].size = bank_size;
73                 base += bank_size;
74                 size_left -= bank_size;
75                 snprintf(name, sizeof(name), "ppc4xx.sdram%d", i);
76                 memory_region_init_alias(&ram_banks[i].ram, NULL, name, ram,
77                                          ram_banks[i].base, ram_banks[i].size);
78                 break;
79             }
80         }
81         if (!size_left) {
82             /* No need to use the remaining banks. */
83             break;
84         }
85     }
86 
87     if (size_left) {
88         ram_addr_t used_size = memory_region_size(ram) - size_left;
89         GString *s = g_string_new(NULL);
90 
91         for (i = 0; sdram_bank_sizes[i]; i++) {
92             g_string_append_printf(s, "%" PRIi64 "%s",
93                                    sdram_bank_sizes[i] / MiB,
94                                    sdram_bank_sizes[i + 1] ? ", " : "");
95         }
96         error_report("at most %d bank%s of %s MiB each supported",
97                      nr_banks, nr_banks == 1 ? "" : "s", s->str);
98         error_printf("Possible valid RAM size: %" PRIi64 " MiB\n",
99             used_size ? used_size / MiB : sdram_bank_sizes[i - 1] / MiB);
100 
101         g_string_free(s, true);
102         exit(EXIT_FAILURE);
103     }
104 }
105 
106 static void sdram_bank_map(Ppc4xxSdramBank *bank)
107 {
108     trace_ppc4xx_sdram_map(bank->base, bank->size);
109     memory_region_init(&bank->container, NULL, "sdram-container", bank->size);
110     memory_region_add_subregion(&bank->container, 0, &bank->ram);
111     memory_region_add_subregion(get_system_memory(), bank->base,
112                                 &bank->container);
113 }
114 
115 static void sdram_bank_unmap(Ppc4xxSdramBank *bank)
116 {
117     trace_ppc4xx_sdram_unmap(bank->base, bank->size);
118     memory_region_del_subregion(get_system_memory(), &bank->container);
119     memory_region_del_subregion(&bank->container, &bank->ram);
120     object_unparent(OBJECT(&bank->container));
121 }
122 
123 static void sdram_bank_set_bcr(Ppc4xxSdramBank *bank, uint32_t bcr,
124                                hwaddr base, hwaddr size, int enabled)
125 {
126     if (memory_region_is_mapped(&bank->container)) {
127         sdram_bank_unmap(bank);
128     }
129     bank->bcr = bcr;
130     bank->base = base;
131     bank->size = size;
132     if (enabled && (bcr & 1)) {
133         sdram_bank_map(bank);
134     }
135 }
136 
137 enum {
138     SDRAM0_CFGADDR = 0x010,
139     SDRAM0_CFGDATA = 0x011,
140 };
141 
142 /*****************************************************************************/
143 /* DDR SDRAM controller */
144 #define SDRAM_DDR_BCR_MASK 0xFFDEE001
145 
146 static uint32_t sdram_ddr_bcr(hwaddr ram_base, hwaddr ram_size)
147 {
148     uint32_t bcr;
149 
150     switch (ram_size) {
151     case 4 * MiB:
152         bcr = 0;
153         break;
154     case 8 * MiB:
155         bcr = 0x20000;
156         break;
157     case 16 * MiB:
158         bcr = 0x40000;
159         break;
160     case 32 * MiB:
161         bcr = 0x60000;
162         break;
163     case 64 * MiB:
164         bcr = 0x80000;
165         break;
166     case 128 * MiB:
167         bcr = 0xA0000;
168         break;
169     case 256 * MiB:
170         bcr = 0xC0000;
171         break;
172     default:
173         qemu_log_mask(LOG_GUEST_ERROR,
174                       "%s: invalid RAM size 0x%" HWADDR_PRIx "\n", __func__,
175                       ram_size);
176         return 0;
177     }
178     bcr |= ram_base & 0xFF800000;
179     bcr |= 1;
180 
181     return bcr;
182 }
183 
184 static inline hwaddr sdram_ddr_base(uint32_t bcr)
185 {
186     return bcr & 0xFF800000;
187 }
188 
189 static hwaddr sdram_ddr_size(uint32_t bcr)
190 {
191     hwaddr size;
192     int sh;
193 
194     sh = (bcr >> 17) & 0x7;
195     if (sh == 7) {
196         size = -1;
197     } else {
198         size = (4 * MiB) << sh;
199     }
200 
201     return size;
202 }
203 
204 static uint32_t sdram_ddr_dcr_read(void *opaque, int dcrn)
205 {
206     Ppc4xxSdramDdrState *s = opaque;
207     uint32_t ret;
208 
209     switch (dcrn) {
210     case SDRAM0_CFGADDR:
211         ret = s->addr;
212         break;
213     case SDRAM0_CFGDATA:
214         switch (s->addr) {
215         case 0x00: /* SDRAM_BESR0 */
216             ret = s->besr0;
217             break;
218         case 0x08: /* SDRAM_BESR1 */
219             ret = s->besr1;
220             break;
221         case 0x10: /* SDRAM_BEAR */
222             ret = s->bear;
223             break;
224         case 0x20: /* SDRAM_CFG */
225             ret = s->cfg;
226             break;
227         case 0x24: /* SDRAM_STATUS */
228             ret = s->status;
229             break;
230         case 0x30: /* SDRAM_RTR */
231             ret = s->rtr;
232             break;
233         case 0x34: /* SDRAM_PMIT */
234             ret = s->pmit;
235             break;
236         case 0x40: /* SDRAM_B0CR */
237             ret = s->bank[0].bcr;
238             break;
239         case 0x44: /* SDRAM_B1CR */
240             ret = s->bank[1].bcr;
241             break;
242         case 0x48: /* SDRAM_B2CR */
243             ret = s->bank[2].bcr;
244             break;
245         case 0x4C: /* SDRAM_B3CR */
246             ret = s->bank[3].bcr;
247             break;
248         case 0x80: /* SDRAM_TR */
249             ret = -1; /* ? */
250             break;
251         case 0x94: /* SDRAM_ECCCFG */
252             ret = s->ecccfg;
253             break;
254         case 0x98: /* SDRAM_ECCESR */
255             ret = s->eccesr;
256             break;
257         default: /* Error */
258             ret = -1;
259             break;
260         }
261         break;
262     default:
263         /* Avoid gcc warning */
264         ret = 0;
265         break;
266     }
267 
268     return ret;
269 }
270 
271 static void sdram_ddr_dcr_write(void *opaque, int dcrn, uint32_t val)
272 {
273     Ppc4xxSdramDdrState *s = opaque;
274     int i;
275 
276     switch (dcrn) {
277     case SDRAM0_CFGADDR:
278         s->addr = val;
279         break;
280     case SDRAM0_CFGDATA:
281         switch (s->addr) {
282         case 0x00: /* SDRAM_BESR0 */
283             s->besr0 &= ~val;
284             break;
285         case 0x08: /* SDRAM_BESR1 */
286             s->besr1 &= ~val;
287             break;
288         case 0x10: /* SDRAM_BEAR */
289             s->bear = val;
290             break;
291         case 0x20: /* SDRAM_CFG */
292             val &= 0xFFE00000;
293             if (!(s->cfg & 0x80000000) && (val & 0x80000000)) {
294                 trace_ppc4xx_sdram_enable("enable");
295                 /* validate all RAM mappings */
296                 for (i = 0; i < s->nbanks; i++) {
297                     if (s->bank[i].size) {
298                         sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
299                                            s->bank[i].base, s->bank[i].size,
300                                            1);
301                     }
302                 }
303                 s->status &= ~0x80000000;
304             } else if ((s->cfg & 0x80000000) && !(val & 0x80000000)) {
305                 trace_ppc4xx_sdram_enable("disable");
306                 /* invalidate all RAM mappings */
307                 for (i = 0; i < s->nbanks; i++) {
308                     if (s->bank[i].size) {
309                         sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
310                                            s->bank[i].base, s->bank[i].size,
311                                            0);
312                     }
313                 }
314                 s->status |= 0x80000000;
315             }
316             if (!(s->cfg & 0x40000000) && (val & 0x40000000)) {
317                 s->status |= 0x40000000;
318             } else if ((s->cfg & 0x40000000) && !(val & 0x40000000)) {
319                 s->status &= ~0x40000000;
320             }
321             s->cfg = val;
322             break;
323         case 0x24: /* SDRAM_STATUS */
324             /* Read-only register */
325             break;
326         case 0x30: /* SDRAM_RTR */
327             s->rtr = val & 0x3FF80000;
328             break;
329         case 0x34: /* SDRAM_PMIT */
330             s->pmit = (val & 0xF8000000) | 0x07C00000;
331             break;
332         case 0x40: /* SDRAM_B0CR */
333         case 0x44: /* SDRAM_B1CR */
334         case 0x48: /* SDRAM_B2CR */
335         case 0x4C: /* SDRAM_B3CR */
336             i = (s->addr - 0x40) / 4;
337             val &= SDRAM_DDR_BCR_MASK;
338             if (s->bank[i].size) {
339                 sdram_bank_set_bcr(&s->bank[i], val,
340                                    sdram_ddr_base(val), sdram_ddr_size(val),
341                                    s->cfg & 0x80000000);
342             }
343             break;
344         case 0x80: /* SDRAM_TR */
345             s->tr = val & 0x018FC01F;
346             break;
347         case 0x94: /* SDRAM_ECCCFG */
348             s->ecccfg = val & 0x00F00000;
349             break;
350         case 0x98: /* SDRAM_ECCESR */
351             val &= 0xFFF0F000;
352             if (s->eccesr == 0 && val != 0) {
353                 qemu_irq_raise(s->irq);
354             } else if (s->eccesr != 0 && val == 0) {
355                 qemu_irq_lower(s->irq);
356             }
357             s->eccesr = val;
358             break;
359         default: /* Error */
360             break;
361         }
362         break;
363     }
364 }
365 
366 static void ppc4xx_sdram_ddr_reset(DeviceState *dev)
367 {
368     Ppc4xxSdramDdrState *s = PPC4xx_SDRAM_DDR(dev);
369 
370     s->addr = 0;
371     s->bear = 0;
372     s->besr0 = 0; /* No error */
373     s->besr1 = 0; /* No error */
374     s->cfg = 0;
375     s->ecccfg = 0; /* No ECC */
376     s->eccesr = 0; /* No error */
377     s->pmit = 0x07C00000;
378     s->rtr = 0x05F00000;
379     s->tr = 0x00854009;
380     /* We pre-initialize RAM banks */
381     s->status = 0;
382     s->cfg = 0x00800000;
383 }
384 
385 static void ppc4xx_sdram_ddr_realize(DeviceState *dev, Error **errp)
386 {
387     Ppc4xxSdramDdrState *s = PPC4xx_SDRAM_DDR(dev);
388     Ppc4xxDcrDeviceState *dcr = PPC4xx_DCR_DEVICE(dev);
389     const ram_addr_t valid_bank_sizes[] = {
390         256 * MiB, 128 * MiB, 64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 4 * MiB, 0
391     };
392     int i;
393 
394     if (s->nbanks < 1 || s->nbanks > 4) {
395         error_setg(errp, "Invalid number of RAM banks");
396         return;
397     }
398     if (!s->dram_mr) {
399         error_setg(errp, "Missing dram memory region");
400         return;
401     }
402     ppc4xx_sdram_banks(s->dram_mr, s->nbanks, s->bank, valid_bank_sizes);
403     for (i = 0; i < s->nbanks; i++) {
404         if (s->bank[i].size) {
405             s->bank[i].bcr = sdram_ddr_bcr(s->bank[i].base, s->bank[i].size);
406             sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
407                                s->bank[i].base, s->bank[i].size, 0);
408         } else {
409             sdram_bank_set_bcr(&s->bank[i], 0, 0, 0, 0);
410         }
411         trace_ppc4xx_sdram_init(sdram_ddr_base(s->bank[i].bcr),
412                                 sdram_ddr_size(s->bank[i].bcr),
413                                 s->bank[i].bcr);
414     }
415 
416     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
417 
418     ppc4xx_dcr_register(dcr, SDRAM0_CFGADDR,
419                         s, &sdram_ddr_dcr_read, &sdram_ddr_dcr_write);
420     ppc4xx_dcr_register(dcr, SDRAM0_CFGDATA,
421                         s, &sdram_ddr_dcr_read, &sdram_ddr_dcr_write);
422 }
423 
424 static Property ppc4xx_sdram_ddr_props[] = {
425     DEFINE_PROP_LINK("dram", Ppc4xxSdramDdrState, dram_mr, TYPE_MEMORY_REGION,
426                      MemoryRegion *),
427     DEFINE_PROP_UINT32("nbanks", Ppc4xxSdramDdrState, nbanks, 4),
428     DEFINE_PROP_END_OF_LIST(),
429 };
430 
431 static void ppc4xx_sdram_ddr_class_init(ObjectClass *oc, void *data)
432 {
433     DeviceClass *dc = DEVICE_CLASS(oc);
434 
435     dc->realize = ppc4xx_sdram_ddr_realize;
436     dc->reset = ppc4xx_sdram_ddr_reset;
437     /* Reason: only works as function of a ppc4xx SoC */
438     dc->user_creatable = false;
439     device_class_set_props(dc, ppc4xx_sdram_ddr_props);
440 }
441 
442 void ppc4xx_sdram_ddr_enable(Ppc4xxSdramDdrState *s)
443 {
444     sdram_ddr_dcr_write(s, SDRAM0_CFGADDR, 0x20);
445     sdram_ddr_dcr_write(s, SDRAM0_CFGDATA, 0x80000000);
446 }
447 
448 /*****************************************************************************/
449 /* DDR2 SDRAM controller */
450 #define SDRAM_DDR2_BCR_MASK 0xffe0ffc1
451 
452 enum {
453     SDRAM_R0BAS = 0x40,
454     SDRAM_R1BAS,
455     SDRAM_R2BAS,
456     SDRAM_R3BAS,
457     SDRAM_CONF1HB = 0x45,
458     SDRAM_PLBADDULL = 0x4a,
459     SDRAM_CONF1LL = 0x4b,
460     SDRAM_CONFPATHB = 0x4f,
461     SDRAM_PLBADDUHB = 0x50,
462 };
463 
464 static uint32_t sdram_ddr2_bcr(hwaddr ram_base, hwaddr ram_size)
465 {
466     uint32_t bcr;
467 
468     switch (ram_size) {
469     case 8 * MiB:
470         bcr = 0xffc0;
471         break;
472     case 16 * MiB:
473         bcr = 0xff80;
474         break;
475     case 32 * MiB:
476         bcr = 0xff00;
477         break;
478     case 64 * MiB:
479         bcr = 0xfe00;
480         break;
481     case 128 * MiB:
482         bcr = 0xfc00;
483         break;
484     case 256 * MiB:
485         bcr = 0xf800;
486         break;
487     case 512 * MiB:
488         bcr = 0xf000;
489         break;
490     case 1 * GiB:
491         bcr = 0xe000;
492         break;
493     case 2 * GiB:
494         bcr = 0xc000;
495         break;
496     case 4 * GiB:
497         bcr = 0x8000;
498         break;
499     default:
500         error_report("invalid RAM size " TARGET_FMT_plx, ram_size);
501         return 0;
502     }
503     bcr |= ram_base >> 2 & 0xffe00000;
504     bcr |= 1;
505 
506     return bcr;
507 }
508 
509 static inline hwaddr sdram_ddr2_base(uint32_t bcr)
510 {
511     return (bcr & 0xffe00000) << 2;
512 }
513 
514 static hwaddr sdram_ddr2_size(uint32_t bcr)
515 {
516     hwaddr size;
517     int sh;
518 
519     sh = 1024 - ((bcr >> 6) & 0x3ff);
520     size = 8 * MiB * sh;
521 
522     return size;
523 }
524 
525 static uint32_t sdram_ddr2_dcr_read(void *opaque, int dcrn)
526 {
527     Ppc4xxSdramDdr2State *s = opaque;
528     uint32_t ret = 0;
529 
530     switch (dcrn) {
531     case SDRAM_R0BAS:
532     case SDRAM_R1BAS:
533     case SDRAM_R2BAS:
534     case SDRAM_R3BAS:
535         if (s->bank[dcrn - SDRAM_R0BAS].size) {
536             ret = sdram_ddr2_bcr(s->bank[dcrn - SDRAM_R0BAS].base,
537                                  s->bank[dcrn - SDRAM_R0BAS].size);
538         }
539         break;
540     case SDRAM_CONF1HB:
541     case SDRAM_CONF1LL:
542     case SDRAM_CONFPATHB:
543     case SDRAM_PLBADDULL:
544     case SDRAM_PLBADDUHB:
545         break;
546     case SDRAM0_CFGADDR:
547         ret = s->addr;
548         break;
549     case SDRAM0_CFGDATA:
550         switch (s->addr) {
551         case 0x14: /* SDRAM_MCSTAT (405EX) */
552         case 0x1F:
553             ret = 0x80000000;
554             break;
555         case 0x21: /* SDRAM_MCOPT2 */
556             ret = s->mcopt2;
557             break;
558         case 0x40: /* SDRAM_MB0CF */
559             ret = 0x00008001;
560             break;
561         case 0x7A: /* SDRAM_DLCR */
562             ret = 0x02000000;
563             break;
564         case 0xE1: /* SDR0_DDR0 */
565             ret = SDR0_DDR0_DDRM_ENCODE(1) | SDR0_DDR0_DDRM_DDR1;
566             break;
567         default:
568             break;
569         }
570         break;
571     default:
572         break;
573     }
574 
575     return ret;
576 }
577 
578 #define SDRAM_DDR2_MCOPT2_DCEN BIT(27)
579 
580 static void sdram_ddr2_dcr_write(void *opaque, int dcrn, uint32_t val)
581 {
582     Ppc4xxSdramDdr2State *s = opaque;
583     int i;
584 
585     switch (dcrn) {
586     case SDRAM_R0BAS:
587     case SDRAM_R1BAS:
588     case SDRAM_R2BAS:
589     case SDRAM_R3BAS:
590     case SDRAM_CONF1HB:
591     case SDRAM_CONF1LL:
592     case SDRAM_CONFPATHB:
593     case SDRAM_PLBADDULL:
594     case SDRAM_PLBADDUHB:
595         break;
596     case SDRAM0_CFGADDR:
597         s->addr = val;
598         break;
599     case SDRAM0_CFGDATA:
600         switch (s->addr) {
601         case 0x00: /* B0CR */
602             break;
603         case 0x21: /* SDRAM_MCOPT2 */
604             if (!(s->mcopt2 & SDRAM_DDR2_MCOPT2_DCEN) &&
605                 (val & SDRAM_DDR2_MCOPT2_DCEN)) {
606                 trace_ppc4xx_sdram_enable("enable");
607                 /* validate all RAM mappings */
608                 for (i = 0; i < s->nbanks; i++) {
609                     if (s->bank[i].size) {
610                         sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
611                                            s->bank[i].base, s->bank[i].size,
612                                            1);
613                     }
614                 }
615                 s->mcopt2 |= SDRAM_DDR2_MCOPT2_DCEN;
616             } else if ((s->mcopt2 & SDRAM_DDR2_MCOPT2_DCEN) &&
617                        !(val & SDRAM_DDR2_MCOPT2_DCEN)) {
618                 trace_ppc4xx_sdram_enable("disable");
619                 /* invalidate all RAM mappings */
620                 for (i = 0; i < s->nbanks; i++) {
621                     if (s->bank[i].size) {
622                         sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
623                                            s->bank[i].base, s->bank[i].size,
624                                            0);
625                     }
626                 }
627                 s->mcopt2 &= ~SDRAM_DDR2_MCOPT2_DCEN;
628             }
629             break;
630         default:
631             break;
632         }
633         break;
634     default:
635         break;
636     }
637 }
638 
639 static void ppc4xx_sdram_ddr2_reset(DeviceState *dev)
640 {
641     Ppc4xxSdramDdr2State *s = PPC4xx_SDRAM_DDR2(dev);
642 
643     s->addr = 0;
644     s->mcopt2 = 0;
645 }
646 
647 static void ppc4xx_sdram_ddr2_realize(DeviceState *dev, Error **errp)
648 {
649     Ppc4xxSdramDdr2State *s = PPC4xx_SDRAM_DDR2(dev);
650     Ppc4xxDcrDeviceState *dcr = PPC4xx_DCR_DEVICE(dev);
651     /*
652      * SoC also has 4 GiB but that causes problem with 32 bit
653      * builds (4*GiB overflows the 32 bit ram_addr_t).
654      */
655     const ram_addr_t valid_bank_sizes[] = {
656         2 * GiB, 1 * GiB, 512 * MiB, 256 * MiB, 128 * MiB,
657         64 * MiB, 32 * MiB, 16 * MiB, 8 * MiB, 0
658     };
659     int i;
660 
661     if (s->nbanks < 1 || s->nbanks > 4) {
662         error_setg(errp, "Invalid number of RAM banks");
663         return;
664     }
665     if (!s->dram_mr) {
666         error_setg(errp, "Missing dram memory region");
667         return;
668     }
669     ppc4xx_sdram_banks(s->dram_mr, s->nbanks, s->bank, valid_bank_sizes);
670     for (i = 0; i < s->nbanks; i++) {
671         if (s->bank[i].size) {
672             s->bank[i].bcr = sdram_ddr2_bcr(s->bank[i].base, s->bank[i].size);
673             s->bank[i].bcr &= SDRAM_DDR2_BCR_MASK;
674             sdram_bank_set_bcr(&s->bank[i], s->bank[i].bcr,
675                                s->bank[i].base, s->bank[i].size, 0);
676         } else {
677             sdram_bank_set_bcr(&s->bank[i], 0, 0, 0, 0);
678         }
679         trace_ppc4xx_sdram_init(sdram_ddr2_base(s->bank[i].bcr),
680                                 sdram_ddr2_size(s->bank[i].bcr),
681                                 s->bank[i].bcr);
682     }
683 
684     ppc4xx_dcr_register(dcr, SDRAM0_CFGADDR,
685                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
686     ppc4xx_dcr_register(dcr, SDRAM0_CFGDATA,
687                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
688 
689     ppc4xx_dcr_register(dcr, SDRAM_R0BAS,
690                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
691     ppc4xx_dcr_register(dcr, SDRAM_R1BAS,
692                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
693     ppc4xx_dcr_register(dcr, SDRAM_R2BAS,
694                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
695     ppc4xx_dcr_register(dcr, SDRAM_R3BAS,
696                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
697     ppc4xx_dcr_register(dcr, SDRAM_CONF1HB,
698                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
699     ppc4xx_dcr_register(dcr, SDRAM_PLBADDULL,
700                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
701     ppc4xx_dcr_register(dcr, SDRAM_CONF1LL,
702                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
703     ppc4xx_dcr_register(dcr, SDRAM_CONFPATHB,
704                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
705     ppc4xx_dcr_register(dcr, SDRAM_PLBADDUHB,
706                         s, &sdram_ddr2_dcr_read, &sdram_ddr2_dcr_write);
707 }
708 
709 static Property ppc4xx_sdram_ddr2_props[] = {
710     DEFINE_PROP_LINK("dram", Ppc4xxSdramDdr2State, dram_mr, TYPE_MEMORY_REGION,
711                      MemoryRegion *),
712     DEFINE_PROP_UINT32("nbanks", Ppc4xxSdramDdr2State, nbanks, 4),
713     DEFINE_PROP_END_OF_LIST(),
714 };
715 
716 static void ppc4xx_sdram_ddr2_class_init(ObjectClass *oc, void *data)
717 {
718     DeviceClass *dc = DEVICE_CLASS(oc);
719 
720     dc->realize = ppc4xx_sdram_ddr2_realize;
721     dc->reset = ppc4xx_sdram_ddr2_reset;
722     /* Reason: only works as function of a ppc4xx SoC */
723     dc->user_creatable = false;
724     device_class_set_props(dc, ppc4xx_sdram_ddr2_props);
725 }
726 
727 void ppc4xx_sdram_ddr2_enable(Ppc4xxSdramDdr2State *s)
728 {
729     sdram_ddr2_dcr_write(s, SDRAM0_CFGADDR, 0x21);
730     sdram_ddr2_dcr_write(s, SDRAM0_CFGDATA, 0x08000000);
731 }
732 
733 static const TypeInfo ppc4xx_sdram_types[] = {
734     {
735         .name           = TYPE_PPC4xx_SDRAM_DDR,
736         .parent         = TYPE_PPC4xx_DCR_DEVICE,
737         .instance_size  = sizeof(Ppc4xxSdramDdrState),
738         .class_init     = ppc4xx_sdram_ddr_class_init,
739     }, {
740         .name           = TYPE_PPC4xx_SDRAM_DDR2,
741         .parent         = TYPE_PPC4xx_DCR_DEVICE,
742         .instance_size  = sizeof(Ppc4xxSdramDdr2State),
743         .class_init     = ppc4xx_sdram_ddr2_class_init,
744     }
745 };
746 
747 DEFINE_TYPES(ppc4xx_sdram_types)
748