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 */
37 uint32_t prefconfig
[2];
41 struct omap_gpmc_cs_file_s
{
43 target_phys_addr_t base
;
46 MemoryRegion container
;
47 void (*base_update
)(void *opaque
, target_phys_addr_t
new);
48 void (*unmap
)(void *opaque
);
57 static void omap_gpmc_int_update(struct omap_gpmc_s
*s
)
59 qemu_set_irq(s
->irq
, s
->irqen
& s
->irqst
);
62 static void omap_gpmc_cs_map(struct omap_gpmc_cs_file_s
*f
, int base
, int mask
)
64 /* TODO: check for overlapping regions and report access errors */
65 if ((mask
!= 0x8 && mask
!= 0xc && mask
!= 0xe && mask
!= 0xf) ||
66 (base
< 0 || base
>= 0x40) ||
67 (base
& 0x0f & ~mask
)) {
68 fprintf(stderr
, "%s: wrong cs address mapping/decoding!\n",
77 f
->size
= (0x0fffffff & ~(mask
<< 24)) + 1;
78 /* TODO: rather than setting the size of the mapping (which should be
79 * constant), the mask should cause wrapping of the address space, so
80 * that the same memory becomes accessible at every <i>size</i> bytes
81 * starting from <i>base</i>. */
83 memory_region_init(&f
->container
, "omap-gpmc-file", f
->size
);
84 memory_region_add_subregion(&f
->container
, 0, f
->iomem
);
85 memory_region_add_subregion(get_system_memory(), f
->base
,
90 f
->base_update(f
->opaque
, f
->base
);
93 static void omap_gpmc_cs_unmap(struct omap_gpmc_cs_file_s
*f
)
99 memory_region_del_subregion(get_system_memory(), &f
->container
);
100 memory_region_del_subregion(&f
->container
, f
->iomem
);
101 memory_region_destroy(&f
->container
);
108 void omap_gpmc_reset(struct omap_gpmc_s
*s
)
115 omap_gpmc_int_update(s
);
118 s
->prefconfig
[0] = 0x00004000;
119 s
->prefconfig
[1] = 0x00000000;
123 for (i
= 0; i
< 8; i
++) {
124 if (s
->cs_file
[i
].config
[6] & (1 << 6)) /* CSVALID */
125 omap_gpmc_cs_unmap(s
->cs_file
+ i
);
126 s
->cs_file
[i
].config
[0] = i
? 1 << 12 : 0;
127 s
->cs_file
[i
].config
[1] = 0x101001;
128 s
->cs_file
[i
].config
[2] = 0x020201;
129 s
->cs_file
[i
].config
[3] = 0x10031003;
130 s
->cs_file
[i
].config
[4] = 0x10f1111;
131 s
->cs_file
[i
].config
[5] = 0;
132 s
->cs_file
[i
].config
[6] = 0xf00 | (i
? 0 : 1 << 6);
133 if (s
->cs_file
[i
].config
[6] & (1 << 6)) /* CSVALID */
134 omap_gpmc_cs_map(&s
->cs_file
[i
],
135 s
->cs_file
[i
].config
[6] & 0x1f, /* MASKADDR */
136 (s
->cs_file
[i
].config
[6] >> 8 & 0xf)); /* BASEADDR */
140 s
->ecc_cfg
= 0x3fcff000;
141 for (i
= 0; i
< 9; i
++)
142 ecc_reset(&s
->ecc
[i
]);
145 static uint64_t omap_gpmc_read(void *opaque
, target_phys_addr_t addr
,
148 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
150 struct omap_gpmc_cs_file_s
*f
;
153 return omap_badwidth_read32(opaque
, addr
);
157 case 0x000: /* GPMC_REVISION */
160 case 0x010: /* GPMC_SYSCONFIG */
163 case 0x014: /* GPMC_SYSSTATUS */
164 return 1; /* RESETDONE */
166 case 0x018: /* GPMC_IRQSTATUS */
169 case 0x01c: /* GPMC_IRQENABLE */
172 case 0x040: /* GPMC_TIMEOUT_CONTROL */
175 case 0x044: /* GPMC_ERR_ADDRESS */
176 case 0x048: /* GPMC_ERR_TYPE */
179 case 0x050: /* GPMC_CONFIG */
182 case 0x054: /* GPMC_STATUS */
185 case 0x060 ... 0x1d4:
186 cs
= (addr
- 0x060) / 0x30;
190 case 0x60: /* GPMC_CONFIG1 */
192 case 0x64: /* GPMC_CONFIG2 */
194 case 0x68: /* GPMC_CONFIG3 */
196 case 0x6c: /* GPMC_CONFIG4 */
198 case 0x70: /* GPMC_CONFIG5 */
200 case 0x74: /* GPMC_CONFIG6 */
202 case 0x78: /* GPMC_CONFIG7 */
204 case 0x84: /* GPMC_NAND_DATA */
209 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
210 return s
->prefconfig
[0];
211 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
212 return s
->prefconfig
[1];
213 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
214 return s
->prefcontrol
;
215 case 0x1f0: /* GPMC_PREFETCH_STATUS */
216 return (s
->preffifo
<< 24) |
218 ((s
->prefconfig
[0] >> 8) & 0x7f) ? 1 : 0) << 16) |
221 case 0x1f4: /* GPMC_ECC_CONFIG */
223 case 0x1f8: /* GPMC_ECC_CONTROL */
225 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
227 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
228 cs
= (addr
& 0x1f) >> 2;
229 /* TODO: check correctness */
231 ((s
->ecc
[cs
].cp
& 0x07) << 0) |
232 ((s
->ecc
[cs
].cp
& 0x38) << 13) |
233 ((s
->ecc
[cs
].lp
[0] & 0x1ff) << 3) |
234 ((s
->ecc
[cs
].lp
[1] & 0x1ff) << 19);
236 case 0x230: /* GPMC_TESTMODE_CTRL */
238 case 0x234: /* GPMC_PSA_LSB */
239 case 0x238: /* GPMC_PSA_MSB */
247 static void omap_gpmc_write(void *opaque
, target_phys_addr_t addr
,
248 uint64_t value
, unsigned size
)
250 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*) opaque
;
252 struct omap_gpmc_cs_file_s
*f
;
255 return omap_badwidth_write32(opaque
, addr
, value
);
259 case 0x000: /* GPMC_REVISION */
260 case 0x014: /* GPMC_SYSSTATUS */
261 case 0x054: /* GPMC_STATUS */
262 case 0x1f0: /* GPMC_PREFETCH_STATUS */
263 case 0x200 ... 0x220: /* GPMC_ECC_RESULT */
264 case 0x234: /* GPMC_PSA_LSB */
265 case 0x238: /* GPMC_PSA_MSB */
269 case 0x010: /* GPMC_SYSCONFIG */
270 if ((value
>> 3) == 0x3)
271 fprintf(stderr
, "%s: bad SDRAM idle mode %"PRIi64
"\n",
272 __FUNCTION__
, value
>> 3);
275 s
->sysconfig
= value
& 0x19;
278 case 0x018: /* GPMC_IRQSTATUS */
280 omap_gpmc_int_update(s
);
283 case 0x01c: /* GPMC_IRQENABLE */
284 s
->irqen
= value
& 0xf03;
285 omap_gpmc_int_update(s
);
288 case 0x040: /* GPMC_TIMEOUT_CONTROL */
289 s
->timeout
= value
& 0x1ff1;
292 case 0x044: /* GPMC_ERR_ADDRESS */
293 case 0x048: /* GPMC_ERR_TYPE */
296 case 0x050: /* GPMC_CONFIG */
297 s
->config
= value
& 0xf13;
300 case 0x060 ... 0x1d4:
301 cs
= (addr
- 0x060) / 0x30;
305 case 0x60: /* GPMC_CONFIG1 */
306 f
->config
[0] = value
& 0xffef3e13;
308 case 0x64: /* GPMC_CONFIG2 */
309 f
->config
[1] = value
& 0x001f1f8f;
311 case 0x68: /* GPMC_CONFIG3 */
312 f
->config
[2] = value
& 0x001f1f8f;
314 case 0x6c: /* GPMC_CONFIG4 */
315 f
->config
[3] = value
& 0x1f8f1f8f;
317 case 0x70: /* GPMC_CONFIG5 */
318 f
->config
[4] = value
& 0x0f1f1f1f;
320 case 0x74: /* GPMC_CONFIG6 */
321 f
->config
[5] = value
& 0x00000fcf;
323 case 0x78: /* GPMC_CONFIG7 */
324 if ((f
->config
[6] ^ value
) & 0xf7f) {
325 if (f
->config
[6] & (1 << 6)) /* CSVALID */
326 omap_gpmc_cs_unmap(f
);
327 if (value
& (1 << 6)) /* CSVALID */
328 omap_gpmc_cs_map(f
, value
& 0x1f, /* MASKADDR */
329 (value
>> 8 & 0xf)); /* BASEADDR */
331 f
->config
[6] = value
& 0x00000f7f;
333 case 0x7c: /* GPMC_NAND_COMMAND */
334 case 0x80: /* GPMC_NAND_ADDRESS */
335 case 0x84: /* GPMC_NAND_DATA */
343 case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
344 s
->prefconfig
[0] = value
& 0x7f8f7fbf;
345 /* TODO: update interrupts, fifos, dmas */
348 case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
349 s
->prefconfig
[1] = value
& 0x3fff;
352 case 0x1ec: /* GPMC_PREFETCH_CONTROL */
353 s
->prefcontrol
= value
& 1;
354 if (s
->prefcontrol
) {
355 if (s
->prefconfig
[0] & 1)
363 case 0x1f4: /* GPMC_ECC_CONFIG */
366 case 0x1f8: /* GPMC_ECC_CONTROL */
367 if (value
& (1 << 8))
368 for (cs
= 0; cs
< 9; cs
++)
369 ecc_reset(&s
->ecc
[cs
]);
370 s
->ecc_ptr
= value
& 0xf;
371 if (s
->ecc_ptr
== 0 || s
->ecc_ptr
> 9) {
376 case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
377 s
->ecc_cfg
= value
& 0x3fcff1ff;
379 case 0x230: /* GPMC_TESTMODE_CTRL */
381 fprintf(stderr
, "%s: test mode enable attempt\n", __FUNCTION__
);
391 static const MemoryRegionOps omap_gpmc_ops
= {
392 .read
= omap_gpmc_read
,
393 .write
= omap_gpmc_write
,
394 .endianness
= DEVICE_NATIVE_ENDIAN
,
397 struct omap_gpmc_s
*omap_gpmc_init(target_phys_addr_t base
, qemu_irq irq
)
399 struct omap_gpmc_s
*s
= (struct omap_gpmc_s
*)
400 g_malloc0(sizeof(struct omap_gpmc_s
));
404 memory_region_init_io(&s
->iomem
, &omap_gpmc_ops
, s
, "omap-gpmc", 0x1000);
405 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
410 void omap_gpmc_attach(struct omap_gpmc_s
*s
, int cs
, MemoryRegion
*iomem
,
411 void (*base_upd
)(void *opaque
, target_phys_addr_t
new),
412 void (*unmap
)(void *opaque
), void *opaque
)
414 struct omap_gpmc_cs_file_s
*f
;
416 if (cs
< 0 || cs
>= 8) {
417 fprintf(stderr
, "%s: bad chip-select %i\n", __FUNCTION__
, cs
);
423 f
->base_update
= base_upd
;
427 if (f
->config
[6] & (1 << 6)) /* CSVALID */
428 omap_gpmc_cs_map(f
, f
->config
[6] & 0x1f, /* MASKADDR */
429 (f
->config
[6] >> 8 & 0xf)); /* BASEADDR */