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 */
40 struct omap_gpmc_cs_file_s
{
43 MemoryRegion container
;
44 MemoryRegion nandiomem
;
52 uint32_t config1
; /* GPMC_PREFETCH_CONFIG1 */
53 uint32_t transfercount
; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */
54 int startengine
; /* GPMC_PREFETCH_CONTROL:STARTENGINE */
55 int fifopointer
; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */
56 int count
; /* GPMC_PREFETCH_STATUS:COUNTVALUE */
60 #define OMAP_GPMC_8BIT 0
61 #define OMAP_GPMC_16BIT 1
62 #define OMAP_GPMC_NOR 0
63 #define OMAP_GPMC_NAND 2
65 static int omap_gpmc_devtype(struct omap_gpmc_cs_file_s
*f
)
67 return (f
->config
[0] >> 10) & 3;
70 static int omap_gpmc_devsize(struct omap_gpmc_cs_file_s
*f
)
72 /* devsize field is really 2 bits but we ignore the high
73 * bit to ensure consistent behaviour if the guest sets
74 * it (values 2 and 3 are reserved in the TRM)
76 return (f
->config
[0] >> 12) & 1;
79 static void omap_gpmc_int_update(struct omap_gpmc_s
*s
)
81 qemu_set_irq(s
->irq
, s
->irqen
& s
->irqst
);
84 /* Access functions for when a NAND-like device is mapped into memory:
85 * all addresses in the region behave like accesses to the relevant
86 * GPMC_NAND_DATA_i register (which is actually implemented to call these)
88 static uint64_t omap_nand_read(void *opaque
, target_phys_addr_t addr
,
91 struct omap_gpmc_cs_file_s
*f
= (struct omap_gpmc_cs_file_s
*)opaque
;
93 nand_setpins(f
->dev
, 0, 0, 0, 1, 0);
94 switch (omap_gpmc_devsize(f
)) {
96 v
= nand_getio(f
->dev
);
100 v
|= (nand_getio(f
->dev
) << 8);
104 v
|= (nand_getio(f
->dev
) << 16);
105 v
|= (nand_getio(f
->dev
) << 24);
107 case OMAP_GPMC_16BIT
:
108 v
= nand_getio(f
->dev
);
110 /* 8 bit read from 16 bit device : probably a guest bug */
116 v
|= (nand_getio(f
->dev
) << 16);
123 static void omap_nand_setio(DeviceState
*dev
, uint64_t value
,
124 int nandsize
, int size
)
126 /* Write the specified value to the NAND device, respecting
127 * both size of the NAND device and size of the write access.
133 nand_setio(dev
, value
& 0xff);
136 nand_setio(dev
, value
& 0xff);
137 nand_setio(dev
, (value
>> 8) & 0xff);
141 nand_setio(dev
, value
& 0xff);
142 nand_setio(dev
, (value
>> 8) & 0xff);
143 nand_setio(dev
, (value
>> 16) & 0xff);
144 nand_setio(dev
, (value
>> 24) & 0xff);
147 case OMAP_GPMC_16BIT
:
150 /* writing to a 16bit device with 8bit access is probably a guest
151 * bug; pass the value through anyway.
154 nand_setio(dev
, value
& 0xffff);
158 nand_setio(dev
, value
& 0xffff);
159 nand_setio(dev
, (value
>> 16) & 0xffff);
165 static void omap_nand_write(void *opaque
, target_phys_addr_t addr
,
166 uint64_t value
, unsigned size
)
168 struct omap_gpmc_cs_file_s
*f
= (struct omap_gpmc_cs_file_s
*)opaque
;
169 nand_setpins(f
->dev
, 0, 0, 0, 1, 0);
170 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
173 static const MemoryRegionOps omap_nand_ops
= {
174 .read
= omap_nand_read
,
175 .write
= omap_nand_write
,
176 .endianness
= DEVICE_NATIVE_ENDIAN
,
179 static MemoryRegion
*omap_gpmc_cs_memregion(struct omap_gpmc_s
*s
, int cs
)
181 /* Return the MemoryRegion* to map/unmap for this chipselect */
182 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
183 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NOR
) {
186 return &f
->nandiomem
;
189 static void omap_gpmc_cs_map(struct omap_gpmc_s
*s
, int cs
)
191 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
192 uint32_t mask
= (f
->config
[6] >> 8) & 0xf;
193 uint32_t base
= f
->config
[6] & 0x3f;
196 if (!f
->iomem
&& !f
->dev
) {
200 if (!(f
->config
[6] & (1 << 6))) {
201 /* Do nothing unless CSVALID */
205 /* TODO: check for overlapping regions and report access errors */
206 if (mask
!= 0x8 && mask
!= 0xc && mask
!= 0xe && mask
!= 0xf
207 && !(s
->accept_256
&& !mask
)) {
208 fprintf(stderr
, "%s: invalid chip-select mask address (0x%x)\n",
213 size
= (0x0fffffff & ~(mask
<< 24)) + 1;
214 /* TODO: rather than setting the size of the mapping (which should be
215 * constant), the mask should cause wrapping of the address space, so
216 * that the same memory becomes accessible at every <i>size</i> bytes
217 * starting from <i>base</i>. */
218 memory_region_init(&f
->container
, "omap-gpmc-file", size
);
219 memory_region_add_subregion(&f
->container
, 0,
220 omap_gpmc_cs_memregion(s
, cs
));
221 memory_region_add_subregion(get_system_memory(), base
,
225 static void omap_gpmc_cs_unmap(struct omap_gpmc_s
*s
, int cs
)
227 struct omap_gpmc_cs_file_s
*f
= &s
->cs_file
[cs
];
228 if (!(f
->config
[6] & (1 << 6))) {
229 /* Do nothing unless CSVALID */
232 if (!f
->iomem
&& !f
->dev
) {
235 memory_region_del_subregion(get_system_memory(), &f
->container
);
236 memory_region_del_subregion(&f
->container
, omap_gpmc_cs_memregion(s
, cs
));
237 memory_region_destroy(&f
->container
);
240 void omap_gpmc_reset(struct omap_gpmc_s
*s
)
247 omap_gpmc_int_update(s
);
250 s
->prefetch
.config1
= 0x00004000;
251 s
->prefetch
.transfercount
= 0x00000000;
252 s
->prefetch
.startengine
= 0;
253 s
->prefetch
.fifopointer
= 0;
254 s
->prefetch
.count
= 0;
255 for (i
= 0; i
< 8; i
++) {
256 omap_gpmc_cs_unmap(s
, i
);
257 s
->cs_file
[i
].config
[1] = 0x101001;
258 s
->cs_file
[i
].config
[2] = 0x020201;
259 s
->cs_file
[i
].config
[3] = 0x10031003;
260 s
->cs_file
[i
].config
[4] = 0x10f1111;
261 s
->cs_file
[i
].config
[5] = 0;
262 s
->cs_file
[i
].config
[6] = 0xf00 | (i
? 0 : 1 << 6);
264 s
->cs_file
[i
].config
[6] = 0xf00;
265 /* In theory we could probe attached devices for some CFG1
266 * bits here, but we just retain them across resets as they
267 * were set initially by omap_gpmc_attach().
270 s
->cs_file
[i
].config
[0] &= 0x00433e00;
271 s
->cs_file
[i
].config
[6] |= 1 << 6; /* CSVALID */
272 omap_gpmc_cs_map(s
, i
);
274 s
->cs_file
[i
].config
[0] &= 0x00403c00;
279 s
->ecc_cfg
= 0x3fcff000;
280 for (i
= 0; i
< 9; i
++)
281 ecc_reset(&s
->ecc
[i
]);
284 static int gpmc_wordaccess_only(target_phys_addr_t addr
)
286 /* Return true if the register offset is to a register that
287 * only permits word width accesses.
288 * Non-word accesses are only OK for GPMC_NAND_DATA/ADDRESS/COMMAND
289 * for any chipselect.
291 if (addr
>= 0x60 && addr
<= 0x1d4) {
292 int cs
= (addr
- 0x60) / 0x30;
294 if (addr
>= 0x7c && addr
< 0x88) {
295 /* GPMC_NAND_COMMAND, GPMC_NAND_ADDRESS, GPMC_NAND_DATA */
302 static uint64_t omap_gpmc_read(void *opaque
, target_phys_addr_t addr
,
305 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
307 struct omap_gpmc_cs_file_s
*f
;
309 if (size
!= 4 && gpmc_wordaccess_only(addr
)) {
310 return omap_badwidth_read32(opaque
, addr
);
314 case 0x000: /* GPMC_REVISION */
317 case 0x010: /* GPMC_SYSCONFIG */
320 case 0x014: /* GPMC_SYSSTATUS */
321 return 1; /* RESETDONE */
323 case 0x018: /* GPMC_IRQSTATUS */
326 case 0x01c: /* GPMC_IRQENABLE */
329 case 0x040: /* GPMC_TIMEOUT_CONTROL */
332 case 0x044: /* GPMC_ERR_ADDRESS */
333 case 0x048: /* GPMC_ERR_TYPE */
336 case 0x050: /* GPMC_CONFIG */
339 case 0x054: /* GPMC_STATUS */
342 case 0x060 ... 0x1d4:
343 cs
= (addr
- 0x060) / 0x30;
347 case 0x60: /* GPMC_CONFIG1 */
349 case 0x64: /* GPMC_CONFIG2 */
351 case 0x68: /* GPMC_CONFIG3 */
353 case 0x6c: /* GPMC_CONFIG4 */
355 case 0x70: /* GPMC_CONFIG5 */
357 case 0x74: /* GPMC_CONFIG6 */
359 case 0x78: /* GPMC_CONFIG7 */
361 case 0x84 ... 0x87: /* GPMC_NAND_DATA */
362 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
363 return omap_nand_read(f
, 0, size
);
369 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
370 return s
->prefetch
.config1
;
371 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
372 return s
->prefetch
.transfercount
;
373 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
374 return s
->prefetch
.startengine
;
375 case 0x1f0: /* GPMC_PREFETCH_STATUS */
376 return (s
->prefetch
.fifopointer
<< 24) |
377 ((s
->prefetch
.fifopointer
>=
378 ((s
->prefetch
.config1
>> 8) & 0x7f) ? 1 : 0) << 16) |
381 case 0x1f4: /* GPMC_ECC_CONFIG */
383 case 0x1f8: /* GPMC_ECC_CONTROL */
385 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
387 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
388 cs
= (addr
& 0x1f) >> 2;
389 /* TODO: check correctness */
391 ((s
->ecc
[cs
].cp
& 0x07) << 0) |
392 ((s
->ecc
[cs
].cp
& 0x38) << 13) |
393 ((s
->ecc
[cs
].lp
[0] & 0x1ff) << 3) |
394 ((s
->ecc
[cs
].lp
[1] & 0x1ff) << 19);
396 case 0x230: /* GPMC_TESTMODE_CTRL */
398 case 0x234: /* GPMC_PSA_LSB */
399 case 0x238: /* GPMC_PSA_MSB */
407 static void omap_gpmc_write(void *opaque
, target_phys_addr_t addr
,
408 uint64_t value
, unsigned size
)
410 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
412 struct omap_gpmc_cs_file_s
*f
;
414 if (size
!= 4 && gpmc_wordaccess_only(addr
)) {
415 return omap_badwidth_write32(opaque
, addr
, value
);
419 case 0x000: /* GPMC_REVISION */
420 case 0x014: /* GPMC_SYSSTATUS */
421 case 0x054: /* GPMC_STATUS */
422 case 0x1f0: /* GPMC_PREFETCH_STATUS */
423 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
424 case 0x234: /* GPMC_PSA_LSB */
425 case 0x238: /* GPMC_PSA_MSB */
429 case 0x010: /* GPMC_SYSCONFIG */
430 if ((value
>> 3) == 0x3)
431 fprintf(stderr
, "%s: bad SDRAM idle mode %"PRIi64
"\n",
432 __FUNCTION__
, value
>> 3);
435 s
->sysconfig
= value
& 0x19;
438 case 0x018: /* GPMC_IRQSTATUS */
440 omap_gpmc_int_update(s
);
443 case 0x01c: /* GPMC_IRQENABLE */
444 s
->irqen
= value
& 0xf03;
445 omap_gpmc_int_update(s
);
448 case 0x040: /* GPMC_TIMEOUT_CONTROL */
449 s
->timeout
= value
& 0x1ff1;
452 case 0x044: /* GPMC_ERR_ADDRESS */
453 case 0x048: /* GPMC_ERR_TYPE */
456 case 0x050: /* GPMC_CONFIG */
457 s
->config
= value
& 0xf13;
460 case 0x060 ... 0x1d4:
461 cs
= (addr
- 0x060) / 0x30;
465 case 0x60: /* GPMC_CONFIG1 */
466 f
->config
[0] = value
& 0xffef3e13;
468 case 0x64: /* GPMC_CONFIG2 */
469 f
->config
[1] = value
& 0x001f1f8f;
471 case 0x68: /* GPMC_CONFIG3 */
472 f
->config
[2] = value
& 0x001f1f8f;
474 case 0x6c: /* GPMC_CONFIG4 */
475 f
->config
[3] = value
& 0x1f8f1f8f;
477 case 0x70: /* GPMC_CONFIG5 */
478 f
->config
[4] = value
& 0x0f1f1f1f;
480 case 0x74: /* GPMC_CONFIG6 */
481 f
->config
[5] = value
& 0x00000fcf;
483 case 0x78: /* GPMC_CONFIG7 */
484 if ((f
->config
[6] ^ value
) & 0xf7f) {
485 omap_gpmc_cs_unmap(s
, cs
);
486 f
->config
[6] = value
& 0x00000f7f;
487 omap_gpmc_cs_map(s
, cs
);
490 case 0x7c ... 0x7f: /* GPMC_NAND_COMMAND */
491 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
492 nand_setpins(f
->dev
, 1, 0, 0, 1, 0); /* CLE */
493 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
496 case 0x80 ... 0x83: /* GPMC_NAND_ADDRESS */
497 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
498 nand_setpins(f
->dev
, 0, 1, 0, 1, 0); /* ALE */
499 omap_nand_setio(f
->dev
, value
, omap_gpmc_devsize(f
), size
);
502 case 0x84 ... 0x87: /* GPMC_NAND_DATA */
503 if (omap_gpmc_devtype(f
) == OMAP_GPMC_NAND
) {
504 omap_nand_write(f
, 0, value
, size
);
512 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
513 s
->prefetch
.config1
= value
& 0x7f8f7fbf;
514 /* TODO: update interrupts, fifos, dmas */
517 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
518 s
->prefetch
.transfercount
= value
& 0x3fff;
521 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
522 s
->prefetch
.startengine
= value
& 1;
523 if (s
->prefetch
.startengine
) {
524 if (s
->prefetch
.config1
& 1) {
525 s
->prefetch
.fifopointer
= 0x40;
527 s
->prefetch
.fifopointer
= 0x00;
533 case 0x1f4: /* GPMC_ECC_CONFIG */
536 case 0x1f8: /* GPMC_ECC_CONTROL */
537 if (value
& (1 << 8))
538 for (cs
= 0; cs
< 9; cs
++)
539 ecc_reset(&s
->ecc
[cs
]);
540 s
->ecc_ptr
= value
& 0xf;
541 if (s
->ecc_ptr
== 0 || s
->ecc_ptr
> 9) {
546 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
547 s
->ecc_cfg
= value
& 0x3fcff1ff;
549 case 0x230: /* GPMC_TESTMODE_CTRL */
551 fprintf(stderr
, "%s: test mode enable attempt\n", __FUNCTION__
);
561 static const MemoryRegionOps omap_gpmc_ops
= {
562 .read
= omap_gpmc_read
,
563 .write
= omap_gpmc_write
,
564 .endianness
= DEVICE_NATIVE_ENDIAN
,
567 struct omap_gpmc_s
*omap_gpmc_init(struct omap_mpu_state_s
*mpu
,
568 target_phys_addr_t base
,
569 qemu_irq irq
, qemu_irq drq
)
572 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*)
573 g_malloc0(sizeof(struct omap_gpmc_s
));
575 memory_region_init_io(&s
->iomem
, &omap_gpmc_ops
, s
, "omap-gpmc", 0x1000);
576 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
580 s
->accept_256
= cpu_is_omap3630(mpu
);
581 s
->revision
= cpu_class_omap3(mpu
) ? 0x50 : 0x20;
584 /* We have to register a different IO memory handler for each
585 * chip select region in case a NAND device is mapped there. We
586 * make the region the worst-case size of 256MB and rely on the
587 * container memory region in cs_map to chop it down to the actual
588 * guest-requested size.
590 for (cs
= 0; cs
< 8; cs
++) {
591 memory_region_init_io(&s
->cs_file
[cs
].nandiomem
,
600 void omap_gpmc_attach(struct omap_gpmc_s
*s
, int cs
, MemoryRegion
*iomem
)
602 struct omap_gpmc_cs_file_s
*f
;
605 if (cs
< 0 || cs
>= 8) {
606 fprintf(stderr
, "%s: bad chip-select %i\n", __FUNCTION__
, cs
);
611 omap_gpmc_cs_unmap(s
, cs
);
612 f
->config
[0] &= ~(0xf << 10);
614 omap_gpmc_cs_map(s
, cs
);
617 void omap_gpmc_attach_nand(struct omap_gpmc_s
*s
, int cs
, DeviceState
*nand
)
619 struct omap_gpmc_cs_file_s
*f
;
622 if (cs
< 0 || cs
>= 8) {
623 fprintf(stderr
, "%s: bad chip-select %i\n", __func__
, cs
);
628 omap_gpmc_cs_unmap(s
, cs
);
629 f
->config
[0] &= ~(0xf << 10);
630 f
->config
[0] |= (OMAP_GPMC_NAND
<< 10);
632 if (nand_getbuswidth(f
->dev
) == 16) {
633 f
->config
[0] |= OMAP_GPMC_16BIT
<< 12;
635 omap_gpmc_cs_map(s
, cs
);