2 * TI OMAP general purpose memory controller emulation.
4 * Copyright (C) 2007-2009 Nokia Corporation
5 * Original code written by Andrzej Zaborowski <andrew@openedhand.com>
6 * Enhancements for OMAP3 and NAND support written by Juha Riihimäki
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) any later version of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "exec-memory.h"
27 /* General-Purpose Memory Controller */
39 struct omap_gpmc_cs_file_s
{
42 MemoryRegion container
;
43 MemoryRegion nandiomem
;
51 uint32_t config1
; /* GPMC_PREFETCH_CONFIG1 */
52 uint32_t transfercount
; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */
53 int startengine
; /* GPMC_PREFETCH_CONTROL:STARTENGINE */
54 int fifopointer
; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */
55 int count
; /* GPMC_PREFETCH_STATUS:COUNTVALUE */
59 #define OMAP_GPMC_8BIT 0
60 #define OMAP_GPMC_16BIT 1
61 #define OMAP_GPMC_NOR 0
62 #define OMAP_GPMC_NAND 2
64 static int omap_gpmc_devtype(struct omap_gpmc_cs_file_s
*f
)
66 return (f
->config
[0] >> 10) & 3;
69 static int omap_gpmc_devsize(struct omap_gpmc_cs_file_s
*f
)
71 /* devsize field is really 2 bits but we ignore the high
72 * bit to ensure consistent behaviour if the guest sets
73 * it (values 2 and 3 are reserved in the TRM)
75 return (f
->config
[0] >> 12) & 1;
78 static void omap_gpmc_int_update(struct omap_gpmc_s
*s
)
80 qemu_set_irq(s
->irq
, s
->irqen
& s
->irqst
);
83 /* Access functions for when a NAND-like device is mapped into memory:
84 * all addresses in the region behave like accesses to the relevant
85 * GPMC_NAND_DATA_i register (which is actually implemented to call these)
87 static uint64_t omap_nand_read(void *opaque
, target_phys_addr_t addr
,
90 struct omap_gpmc_cs_file_s
*f
= (struct omap_gpmc_cs_file_s
*)opaque
;
92 nand_setpins(f
->dev
, 0, 0, 0, 1, 0);
93 switch (omap_gpmc_devsize(f
)) {
95 v
= nand_getio(f
->dev
);
99 v
|= (nand_getio(f
->dev
) << 8);
103 v
|= (nand_getio(f
->dev
) << 16);
104 v
|= (nand_getio(f
->dev
) << 24);
106 case OMAP_GPMC_16BIT
:
107 v
= nand_getio(f
->dev
);
109 /* 8 bit read from 16 bit device : probably a guest bug */
115 v
|= (nand_getio(f
->dev
) << 16);
122 static void omap_nand_setio(DeviceState
*dev
, uint64_t value
,
123 int nandsize
, int size
)
125 /* Write the specified value to the NAND device, respecting
126 * both size of the NAND device and size of the write access.
132 nand_setio(dev
, value
& 0xff);
135 nand_setio(dev
, value
& 0xff);
136 nand_setio(dev
, (value
>> 8) & 0xff);
140 nand_setio(dev
, value
& 0xff);
141 nand_setio(dev
, (value
>> 8) & 0xff);
142 nand_setio(dev
, (value
>> 16) & 0xff);
143 nand_setio(dev
, (value
>> 24) & 0xff);
146 case OMAP_GPMC_16BIT
:
149 /* writing to a 16bit device with 8bit access is probably a guest
150 * bug; pass the value through anyway.
153 nand_setio(dev
, value
& 0xffff);
157 nand_setio(dev
, value
& 0xffff);
158 nand_setio(dev
, (value
>> 16) & 0xffff);
164 static void omap_nand_write(void *opaque
, target_phys_addr_t addr
,
165 uint64_t value
, unsigned size
)
167 struct omap_gpmc_cs_file_s
*f
= (struct omap_gpmc_cs_file_s
*)opaque
;
168 nand_setpins(f
->dev
, 0, 0, 0, 1, 0);
169 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
172 static const MemoryRegionOps omap_nand_ops
= {
173 .read
= omap_nand_read
,
174 .write
= omap_nand_write
,
175 .endianness
= DEVICE_NATIVE_ENDIAN
,
178 static MemoryRegion
*omap_gpmc_cs_memregion(struct omap_gpmc_s
*s
, int cs
)
180 /* Return the MemoryRegion* to map/unmap for this chipselect */
181 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
182 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NOR
) {
185 return &f
->nandiomem
;
188 static void omap_gpmc_cs_map(struct omap_gpmc_s
*s
, int cs
)
190 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
191 uint32_t mask
= (f
->config
[6] >> 8) & 0xf;
192 uint32_t base
= f
->config
[6] & 0x3f;
195 if (!f
->iomem
&& !f
->dev
) {
199 if (!(f
->config
[6] & (1 << 6))) {
200 /* Do nothing unless CSVALID */
204 /* TODO: check for overlapping regions and report access errors */
205 if (mask
!= 0x8 && mask
!= 0xc && mask
!= 0xe && mask
!= 0xf
206 && !(s
->accept_256
&& !mask
)) {
207 fprintf(stderr
, "%s: invalid chip-select mask address (0x%x)\n",
212 size
= (0x0fffffff & ~(mask
<< 24)) + 1;
213 /* TODO: rather than setting the size of the mapping (which should be
214 * constant), the mask should cause wrapping of the address space, so
215 * that the same memory becomes accessible at every <i>size</i> bytes
216 * starting from <i>base</i>. */
217 memory_region_init(&f
->container
, "omap-gpmc-file", size
);
218 memory_region_add_subregion(&f
->container
, 0,
219 omap_gpmc_cs_memregion(s
, cs
));
220 memory_region_add_subregion(get_system_memory(), base
,
224 static void omap_gpmc_cs_unmap(struct omap_gpmc_s
*s
, int cs
)
226 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
227 if (!(f
->config
[6] & (1 << 6))) {
228 /* Do nothing unless CSVALID */
231 if (!f
->iomem
&& !f
->dev
) {
234 memory_region_del_subregion(get_system_memory(), &f
->container
);
235 memory_region_del_subregion(&f
->container
, omap_gpmc_cs_memregion(s
, cs
));
236 memory_region_destroy(&f
->container
);
239 void omap_gpmc_reset(struct omap_gpmc_s
*s
)
246 omap_gpmc_int_update(s
);
249 s
->prefetch
.config1
= 0x00004000;
250 s
->prefetch
.transfercount
= 0x00000000;
251 s
->prefetch
.startengine
= 0;
252 s
->prefetch
.fifopointer
= 0;
253 s
->prefetch
.count
= 0;
254 for (i
= 0; i
< 8; i
++) {
255 omap_gpmc_cs_unmap(s
, i
);
256 s
->cs_file
[i
].config
[1] = 0x101001;
257 s
->cs_file
[i
].config
[2] = 0x020201;
258 s
->cs_file
[i
].config
[3] = 0x10031003;
259 s
->cs_file
[i
].config
[4] = 0x10f1111;
260 s
->cs_file
[i
].config
[5] = 0;
261 s
->cs_file
[i
].config
[6] = 0xf00 | (i
? 0 : 1 << 6);
263 s
->cs_file
[i
].config
[6] = 0xf00;
264 /* In theory we could probe attached devices for some CFG1
265 * bits here, but we just retain them across resets as they
266 * were set initially by omap_gpmc_attach().
269 s
->cs_file
[i
].config
[0] &= 0x00433e00;
270 s
->cs_file
[i
].config
[6] |= 1 << 6; /* CSVALID */
271 omap_gpmc_cs_map(s
, i
);
273 s
->cs_file
[i
].config
[0] &= 0x00403c00;
278 s
->ecc_cfg
= 0x3fcff000;
279 for (i
= 0; i
< 9; i
++)
280 ecc_reset(&s
->ecc
[i
]);
283 static int gpmc_wordaccess_only(target_phys_addr_t addr
)
285 /* Return true if the register offset is to a register that
286 * only permits word width accesses.
287 * Non-word accesses are only OK for GPMC_NAND_DATA/ADDRESS/COMMAND
288 * for any chipselect.
290 if (addr
>= 0x60 && addr
<= 0x1d4) {
291 int cs
= (addr
- 0x60) / 0x30;
293 if (addr
>= 0x7c && addr
< 0x88) {
294 /* GPMC_NAND_COMMAND, GPMC_NAND_ADDRESS, GPMC_NAND_DATA */
301 static uint64_t omap_gpmc_read(void *opaque
, target_phys_addr_t addr
,
304 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
306 struct omap_gpmc_cs_file_s
*f
;
308 if (size
!= 4 && gpmc_wordaccess_only(addr
)) {
309 return omap_badwidth_read32(opaque
, addr
);
313 case 0x000: /* GPMC_REVISION */
316 case 0x010: /* GPMC_SYSCONFIG */
319 case 0x014: /* GPMC_SYSSTATUS */
320 return 1; /* RESETDONE */
322 case 0x018: /* GPMC_IRQSTATUS */
325 case 0x01c: /* GPMC_IRQENABLE */
328 case 0x040: /* GPMC_TIMEOUT_CONTROL */
331 case 0x044: /* GPMC_ERR_ADDRESS */
332 case 0x048: /* GPMC_ERR_TYPE */
335 case 0x050: /* GPMC_CONFIG */
338 case 0x054: /* GPMC_STATUS */
341 case 0x060 ... 0x1d4:
342 cs
= (addr
- 0x060) / 0x30;
346 case 0x60: /* GPMC_CONFIG1 */
348 case 0x64: /* GPMC_CONFIG2 */
350 case 0x68: /* GPMC_CONFIG3 */
352 case 0x6c: /* GPMC_CONFIG4 */
354 case 0x70: /* GPMC_CONFIG5 */
356 case 0x74: /* GPMC_CONFIG6 */
358 case 0x78: /* GPMC_CONFIG7 */
360 case 0x84 ... 0x87: /* GPMC_NAND_DATA */
361 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
362 return omap_nand_read(f
, 0, size
);
368 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
369 return s
->prefetch
.config1
;
370 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
371 return s
->prefetch
.transfercount
;
372 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
373 return s
->prefetch
.startengine
;
374 case 0x1f0: /* GPMC_PREFETCH_STATUS */
375 return (s
->prefetch
.fifopointer
<< 24) |
376 ((s
->prefetch
.fifopointer
>=
377 ((s
->prefetch
.config1
>> 8) & 0x7f) ? 1 : 0) << 16) |
380 case 0x1f4: /* GPMC_ECC_CONFIG */
382 case 0x1f8: /* GPMC_ECC_CONTROL */
384 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
386 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
387 cs
= (addr
& 0x1f) >> 2;
388 /* TODO: check correctness */
390 ((s
->ecc
[cs
].cp
& 0x07) << 0) |
391 ((s
->ecc
[cs
].cp
& 0x38) << 13) |
392 ((s
->ecc
[cs
].lp
[0] & 0x1ff) << 3) |
393 ((s
->ecc
[cs
].lp
[1] & 0x1ff) << 19);
395 case 0x230: /* GPMC_TESTMODE_CTRL */
397 case 0x234: /* GPMC_PSA_LSB */
398 case 0x238: /* GPMC_PSA_MSB */
406 static void omap_gpmc_write(void *opaque
, target_phys_addr_t addr
,
407 uint64_t value
, unsigned size
)
409 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
411 struct omap_gpmc_cs_file_s
*f
;
413 if (size
!= 4 && gpmc_wordaccess_only(addr
)) {
414 return omap_badwidth_write32(opaque
, addr
, value
);
418 case 0x000: /* GPMC_REVISION */
419 case 0x014: /* GPMC_SYSSTATUS */
420 case 0x054: /* GPMC_STATUS */
421 case 0x1f0: /* GPMC_PREFETCH_STATUS */
422 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
423 case 0x234: /* GPMC_PSA_LSB */
424 case 0x238: /* GPMC_PSA_MSB */
428 case 0x010: /* GPMC_SYSCONFIG */
429 if ((value
>> 3) == 0x3)
430 fprintf(stderr
, "%s: bad SDRAM idle mode %"PRIi64
"\n",
431 __FUNCTION__
, value
>> 3);
434 s
->sysconfig
= value
& 0x19;
437 case 0x018: /* GPMC_IRQSTATUS */
439 omap_gpmc_int_update(s
);
442 case 0x01c: /* GPMC_IRQENABLE */
443 s
->irqen
= value
& 0xf03;
444 omap_gpmc_int_update(s
);
447 case 0x040: /* GPMC_TIMEOUT_CONTROL */
448 s
->timeout
= value
& 0x1ff1;
451 case 0x044: /* GPMC_ERR_ADDRESS */
452 case 0x048: /* GPMC_ERR_TYPE */
455 case 0x050: /* GPMC_CONFIG */
456 s
->config
= value
& 0xf13;
459 case 0x060 ... 0x1d4:
460 cs
= (addr
- 0x060) / 0x30;
464 case 0x60: /* GPMC_CONFIG1 */
465 f
->config
[0] = value
& 0xffef3e13;
467 case 0x64: /* GPMC_CONFIG2 */
468 f
->config
[1] = value
& 0x001f1f8f;
470 case 0x68: /* GPMC_CONFIG3 */
471 f
->config
[2] = value
& 0x001f1f8f;
473 case 0x6c: /* GPMC_CONFIG4 */
474 f
->config
[3] = value
& 0x1f8f1f8f;
476 case 0x70: /* GPMC_CONFIG5 */
477 f
->config
[4] = value
& 0x0f1f1f1f;
479 case 0x74: /* GPMC_CONFIG6 */
480 f
->config
[5] = value
& 0x00000fcf;
482 case 0x78: /* GPMC_CONFIG7 */
483 if ((f
->config
[6] ^ value
) & 0xf7f) {
484 omap_gpmc_cs_unmap(s
, cs
);
485 f
->config
[6] = value
& 0x00000f7f;
486 omap_gpmc_cs_map(s
, cs
);
489 case 0x7c ... 0x7f: /* GPMC_NAND_COMMAND */
490 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
491 nand_setpins(f
->dev
, 1, 0, 0, 1, 0); /* CLE */
492 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
495 case 0x80 ... 0x83: /* GPMC_NAND_ADDRESS */
496 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
497 nand_setpins(f
->dev
, 0, 1, 0, 1, 0); /* ALE */
498 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
501 case 0x84 ... 0x87: /* GPMC_NAND_DATA */
502 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
503 omap_nand_write(f
, 0, value
, size
);
511 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
512 s
->prefetch
.config1
= value
& 0x7f8f7fbf;
513 /* TODO: update interrupts, fifos, dmas */
516 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
517 s
->prefetch
.transfercount
= value
& 0x3fff;
520 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
521 s
->prefetch
.startengine
= value
& 1;
522 if (s
->prefetch
.startengine
) {
523 if (s
->prefetch
.config1
& 1) {
524 s
->prefetch
.fifopointer
= 0x40;
526 s
->prefetch
.fifopointer
= 0x00;
532 case 0x1f4: /* GPMC_ECC_CONFIG */
535 case 0x1f8: /* GPMC_ECC_CONTROL */
536 if (value
& (1 << 8))
537 for (cs
= 0; cs
< 9; cs
++)
538 ecc_reset(&s
->ecc
[cs
]);
539 s
->ecc_ptr
= value
& 0xf;
540 if (s
->ecc_ptr
== 0 || s
->ecc_ptr
> 9) {
545 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
546 s
->ecc_cfg
= value
& 0x3fcff1ff;
548 case 0x230: /* GPMC_TESTMODE_CTRL */
550 fprintf(stderr
, "%s: test mode enable attempt\n", __FUNCTION__
);
560 static const MemoryRegionOps omap_gpmc_ops
= {
561 .read
= omap_gpmc_read
,
562 .write
= omap_gpmc_write
,
563 .endianness
= DEVICE_NATIVE_ENDIAN
,
566 struct omap_gpmc_s
*omap_gpmc_init(struct omap_mpu_state_s
*mpu
,
567 target_phys_addr_t base
, qemu_irq irq
)
570 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*)
571 g_malloc0(sizeof(struct omap_gpmc_s
));
573 memory_region_init_io(&s
->iomem
, &omap_gpmc_ops
, s
, "omap-gpmc", 0x1000);
574 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
577 s
->accept_256
= cpu_is_omap3630(mpu
);
578 s
->revision
= cpu_class_omap3(mpu
) ? 0x50 : 0x20;
581 /* We have to register a different IO memory handler for each
582 * chip select region in case a NAND device is mapped there. We
583 * make the region the worst-case size of 256MB and rely on the
584 * container memory region in cs_map to chop it down to the actual
585 * guest-requested size.
587 for (cs
= 0; cs
< 8; cs
++) {
588 memory_region_init_io(&s
->cs_file
[cs
].nandiomem
,
597 void omap_gpmc_attach(struct omap_gpmc_s
*s
, int cs
, MemoryRegion
*iomem
)
599 struct omap_gpmc_cs_file_s
*f
;
602 if (cs
< 0 || cs
>= 8) {
603 fprintf(stderr
, "%s: bad chip-select %i\n", __FUNCTION__
, cs
);
608 omap_gpmc_cs_unmap(s
, cs
);
609 f
->config
[0] &= ~(0xf << 10);
611 omap_gpmc_cs_map(s
, cs
);
614 void omap_gpmc_attach_nand(struct omap_gpmc_s
*s
, int cs
, DeviceState
*nand
)
616 struct omap_gpmc_cs_file_s
*f
;
619 if (cs
< 0 || cs
>= 8) {
620 fprintf(stderr
, "%s: bad chip-select %i\n", __func__
, cs
);
625 omap_gpmc_cs_unmap(s
, cs
);
626 f
->config
[0] &= ~(0xf << 10);
627 f
->config
[0] |= (OMAP_GPMC_NAND
<< 10);
629 if (nand_getbuswidth(f
->dev
) == 16) {
630 f
->config
[0] |= OMAP_GPMC_16BIT
<< 12;
632 omap_gpmc_cs_map(s
, cs
);