qemu
1/*
2* TI OMAP general purpose memory controller emulation.
3*
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
7*
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.
12*
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.
17*
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/>.
20*/
21
22#include "qemu/osdep.h"23#include "hw/irq.h"24#include "hw/block/flash.h"25#include "hw/arm/omap.h"26#include "exec/memory.h"27#include "exec/address-spaces.h"28
29/* General-Purpose Memory Controller */
30struct omap_gpmc_s {31qemu_irq irq;32qemu_irq drq;33MemoryRegion iomem;34int accept_256;35
36uint8_t revision;37uint8_t sysconfig;38uint16_t irqst;39uint16_t irqen;40uint16_t lastirq;41uint16_t timeout;42uint16_t config;43struct omap_gpmc_cs_file_s {44uint32_t config[7];45MemoryRegion *iomem;46MemoryRegion container;47MemoryRegion nandiomem;48DeviceState *dev;49} cs_file[8];50int ecc_cs;51int ecc_ptr;52uint32_t ecc_cfg;53ECCState ecc[9];54struct prefetch {55uint32_t config1; /* GPMC_PREFETCH_CONFIG1 */56uint32_t transfercount; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */57int startengine; /* GPMC_PREFETCH_CONTROL:STARTENGINE */58int fifopointer; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */59int count; /* GPMC_PREFETCH_STATUS:COUNTVALUE */60MemoryRegion iomem;61uint8_t fifo[64];62} prefetch;63};64
65#define OMAP_GPMC_8BIT 066#define OMAP_GPMC_16BIT 167#define OMAP_GPMC_NOR 068#define OMAP_GPMC_NAND 269
70static int omap_gpmc_devtype(struct omap_gpmc_cs_file_s *f)71{
72return (f->config[0] >> 10) & 3;73}
74
75static int omap_gpmc_devsize(struct omap_gpmc_cs_file_s *f)76{
77/* devsize field is really 2 bits but we ignore the high78* bit to ensure consistent behaviour if the guest sets
79* it (values 2 and 3 are reserved in the TRM)
80*/
81return (f->config[0] >> 12) & 1;82}
83
84/* Extract the chip-select value from the prefetch config1 register */
85static int prefetch_cs(uint32_t config1)86{
87return (config1 >> 24) & 7;88}
89
90static int prefetch_threshold(uint32_t config1)91{
92return (config1 >> 8) & 0x7f;93}
94
95static void omap_gpmc_int_update(struct omap_gpmc_s *s)96{
97/* The TRM is a bit unclear, but it seems to say that98* the TERMINALCOUNTSTATUS bit is set only on the
99* transition when the prefetch engine goes from
100* active to inactive, whereas the FIFOEVENTSTATUS
101* bit is held high as long as the fifo has at
102* least THRESHOLD bytes available.
103* So we do the latter here, but TERMINALCOUNTSTATUS
104* is set elsewhere.
105*/
106if (s->prefetch.fifopointer >= prefetch_threshold(s->prefetch.config1)) {107s->irqst |= 1;108}109if ((s->irqen & s->irqst) != s->lastirq) {110s->lastirq = s->irqen & s->irqst;111qemu_set_irq(s->irq, s->lastirq);112}113}
114
115static void omap_gpmc_dma_update(struct omap_gpmc_s *s, int value)116{
117if (s->prefetch.config1 & 4) {118qemu_set_irq(s->drq, value);119}120}
121
122/* Access functions for when a NAND-like device is mapped into memory:
123* all addresses in the region behave like accesses to the relevant
124* GPMC_NAND_DATA_i register (which is actually implemented to call these)
125*/
126static uint64_t omap_nand_read(void *opaque, hwaddr addr,127unsigned size)128{
129struct omap_gpmc_cs_file_s *f = opaque;130uint64_t v;131nand_setpins(f->dev, 0, 0, 0, 1, 0);132switch (omap_gpmc_devsize(f)) {133case OMAP_GPMC_8BIT:134v = nand_getio(f->dev);135if (size == 1) {136return v;137}138v |= (nand_getio(f->dev) << 8);139if (size == 2) {140return v;141}142v |= (nand_getio(f->dev) << 16);143v |= (nand_getio(f->dev) << 24);144return v;145case OMAP_GPMC_16BIT:146v = nand_getio(f->dev);147if (size == 1) {148/* 8 bit read from 16 bit device : probably a guest bug */149return v & 0xff;150}151if (size == 2) {152return v;153}154v |= (nand_getio(f->dev) << 16);155return v;156default:157abort();158}159}
160
161static void omap_nand_setio(DeviceState *dev, uint64_t value,162int nandsize, int size)163{
164/* Write the specified value to the NAND device, respecting165* both size of the NAND device and size of the write access.
166*/
167switch (nandsize) {168case OMAP_GPMC_8BIT:169switch (size) {170case 1:171nand_setio(dev, value & 0xff);172break;173case 2:174nand_setio(dev, value & 0xff);175nand_setio(dev, (value >> 8) & 0xff);176break;177case 4:178default:179nand_setio(dev, value & 0xff);180nand_setio(dev, (value >> 8) & 0xff);181nand_setio(dev, (value >> 16) & 0xff);182nand_setio(dev, (value >> 24) & 0xff);183break;184}185break;186case OMAP_GPMC_16BIT:187switch (size) {188case 1:189/* writing to a 16bit device with 8bit access is probably a guest190* bug; pass the value through anyway.
191*/
192case 2:193nand_setio(dev, value & 0xffff);194break;195case 4:196default:197nand_setio(dev, value & 0xffff);198nand_setio(dev, (value >> 16) & 0xffff);199break;200}201break;202}203}
204
205static void omap_nand_write(void *opaque, hwaddr addr,206uint64_t value, unsigned size)207{
208struct omap_gpmc_cs_file_s *f = opaque;209nand_setpins(f->dev, 0, 0, 0, 1, 0);210omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);211}
212
213static const MemoryRegionOps omap_nand_ops = {214.read = omap_nand_read,215.write = omap_nand_write,216.endianness = DEVICE_NATIVE_ENDIAN,217};218
219static void fill_prefetch_fifo(struct omap_gpmc_s *s)220{
221/* Fill the prefetch FIFO by reading data from NAND.222* We do this synchronously, unlike the hardware which
223* will do this asynchronously. We refill when the
224* FIFO has THRESHOLD bytes free, and we always refill
225* as much data as possible starting at the top end
226* of the FIFO.
227* (We have to refill at THRESHOLD rather than waiting
228* for the FIFO to empty to allow for the case where
229* the FIFO size isn't an exact multiple of THRESHOLD
230* and we're doing DMA transfers.)
231* This means we never need to handle wrap-around in
232* the fifo-reading code, and the next byte of data
233* to read is always fifo[63 - fifopointer].
234*/
235int fptr;236int cs = prefetch_cs(s->prefetch.config1);237int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);238int bytes;239/* Don't believe the bit of the OMAP TRM that says that COUNTVALUE240* and TRANSFERCOUNT are in units of 16 bit words for 16 bit NAND.
241* Instead believe the bit that says it is always a byte count.
242*/
243bytes = 64 - s->prefetch.fifopointer;244if (bytes > s->prefetch.count) {245bytes = s->prefetch.count;246}247if (is16bit) {248bytes &= ~1;249}250
251s->prefetch.count -= bytes;252s->prefetch.fifopointer += bytes;253fptr = 64 - s->prefetch.fifopointer;254/* Move the existing data in the FIFO so it sits just255* before what we're about to read in
256*/
257while (fptr < (64 - bytes)) {258s->prefetch.fifo[fptr] = s->prefetch.fifo[fptr + bytes];259fptr++;260}261while (fptr < 64) {262if (is16bit) {263uint32_t v = omap_nand_read(&s->cs_file[cs], 0, 2);264s->prefetch.fifo[fptr++] = v & 0xff;265s->prefetch.fifo[fptr++] = (v >> 8) & 0xff;266} else {267s->prefetch.fifo[fptr++] = omap_nand_read(&s->cs_file[cs], 0, 1);268}269}270if (s->prefetch.startengine && (s->prefetch.count == 0)) {271/* This was the final transfer: raise TERMINALCOUNTSTATUS */272s->irqst |= 2;273s->prefetch.startengine = 0;274}275/* If there are any bytes in the FIFO at this point then276* we must raise a DMA request (either this is a final part
277* transfer, or we filled the FIFO in which case we certainly
278* have THRESHOLD bytes available)
279*/
280if (s->prefetch.fifopointer != 0) {281omap_gpmc_dma_update(s, 1);282}283omap_gpmc_int_update(s);284}
285
286/* Access functions for a NAND-like device when the prefetch/postwrite
287* engine is enabled -- all addresses in the region behave alike:
288* data is read or written to the FIFO.
289*/
290static uint64_t omap_gpmc_prefetch_read(void *opaque, hwaddr addr,291unsigned size)292{
293struct omap_gpmc_s *s = opaque;294uint32_t data;295if (s->prefetch.config1 & 1) {296/* The TRM doesn't define the behaviour if you read from the297* FIFO when the prefetch engine is in write mode. We choose
298* to always return zero.
299*/
300return 0;301}302/* Note that trying to read an empty fifo repeats the last byte */303if (s->prefetch.fifopointer) {304s->prefetch.fifopointer--;305}306data = s->prefetch.fifo[63 - s->prefetch.fifopointer];307if (s->prefetch.fifopointer ==308(64 - prefetch_threshold(s->prefetch.config1))) {309/* We've drained THRESHOLD bytes now. So deassert the310* DMA request, then refill the FIFO (which will probably
311* assert it again.)
312*/
313omap_gpmc_dma_update(s, 0);314fill_prefetch_fifo(s);315}316omap_gpmc_int_update(s);317return data;318}
319
320static void omap_gpmc_prefetch_write(void *opaque, hwaddr addr,321uint64_t value, unsigned size)322{
323struct omap_gpmc_s *s = opaque;324int cs = prefetch_cs(s->prefetch.config1);325if ((s->prefetch.config1 & 1) == 0) {326/* The TRM doesn't define the behaviour of writing to the327* FIFO when the prefetch engine is in read mode. We
328* choose to ignore the write.
329*/
330return;331}332if (s->prefetch.count == 0) {333/* The TRM doesn't define the behaviour of writing to the334* FIFO if the transfer is complete. We choose to ignore.
335*/
336return;337}338/* The only reason we do any data buffering in postwrite339* mode is if we are talking to a 16 bit NAND device, in
340* which case we need to buffer the first byte of the
341* 16 bit word until the other byte arrives.
342*/
343int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);344if (is16bit) {345/* fifopointer alternates between 64 (waiting for first346* byte of word) and 63 (waiting for second byte)
347*/
348if (s->prefetch.fifopointer == 64) {349s->prefetch.fifo[0] = value;350s->prefetch.fifopointer--;351} else {352value = (value << 8) | s->prefetch.fifo[0];353omap_nand_write(&s->cs_file[cs], 0, value, 2);354s->prefetch.count--;355s->prefetch.fifopointer = 64;356}357} else {358/* Just write the byte : fifopointer remains 64 at all times */359omap_nand_write(&s->cs_file[cs], 0, value, 1);360s->prefetch.count--;361}362if (s->prefetch.count == 0) {363/* Final transfer: raise TERMINALCOUNTSTATUS */364s->irqst |= 2;365s->prefetch.startengine = 0;366}367omap_gpmc_int_update(s);368}
369
370static const MemoryRegionOps omap_prefetch_ops = {371.read = omap_gpmc_prefetch_read,372.write = omap_gpmc_prefetch_write,373.endianness = DEVICE_NATIVE_ENDIAN,374.impl.min_access_size = 1,375.impl.max_access_size = 1,376};377
378static MemoryRegion *omap_gpmc_cs_memregion(struct omap_gpmc_s *s, int cs)379{
380/* Return the MemoryRegion* to map/unmap for this chipselect */381struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];382if (omap_gpmc_devtype(f) == OMAP_GPMC_NOR) {383return f->iomem;384}385if ((s->prefetch.config1 & 0x80) &&386(prefetch_cs(s->prefetch.config1) == cs)) {387/* The prefetch engine is enabled for this CS: map the FIFO */388return &s->prefetch.iomem;389}390return &f->nandiomem;391}
392
393static void omap_gpmc_cs_map(struct omap_gpmc_s *s, int cs)394{
395struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];396uint32_t mask = (f->config[6] >> 8) & 0xf;397uint32_t base = f->config[6] & 0x3f;398uint32_t size;399
400if (!f->iomem && !f->dev) {401return;402}403
404if (!(f->config[6] & (1 << 6))) {405/* Do nothing unless CSVALID */406return;407}408
409/* TODO: check for overlapping regions and report access errors */410if (mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf411&& !(s->accept_256 && !mask)) {412fprintf(stderr, "%s: invalid chip-select mask address (0x%x)\n",413__func__, mask);414}415
416base <<= 24;417size = (0x0fffffff & ~(mask << 24)) + 1;418/* TODO: rather than setting the size of the mapping (which should be419* constant), the mask should cause wrapping of the address space, so
420* that the same memory becomes accessible at every <i>size</i> bytes
421* starting from <i>base</i>. */
422memory_region_init(&f->container, NULL, "omap-gpmc-file", size);423memory_region_add_subregion(&f->container, 0,424omap_gpmc_cs_memregion(s, cs));425memory_region_add_subregion(get_system_memory(), base,426&f->container);427}
428
429static void omap_gpmc_cs_unmap(struct omap_gpmc_s *s, int cs)430{
431struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];432if (!(f->config[6] & (1 << 6))) {433/* Do nothing unless CSVALID */434return;435}436if (!f->iomem && !f->dev) {437return;438}439memory_region_del_subregion(get_system_memory(), &f->container);440memory_region_del_subregion(&f->container, omap_gpmc_cs_memregion(s, cs));441object_unparent(OBJECT(&f->container));442}
443
444void omap_gpmc_reset(struct omap_gpmc_s *s)445{
446int i;447
448s->sysconfig = 0;449s->irqst = 0;450s->irqen = 0;451omap_gpmc_int_update(s);452for (i = 0; i < 8; i++) {453/* This has to happen before we change any of the config454* used to determine which memory regions are mapped or unmapped.
455*/
456omap_gpmc_cs_unmap(s, i);457}458s->timeout = 0;459s->config = 0xa00;460s->prefetch.config1 = 0x00004000;461s->prefetch.transfercount = 0x00000000;462s->prefetch.startengine = 0;463s->prefetch.fifopointer = 0;464s->prefetch.count = 0;465for (i = 0; i < 8; i ++) {466s->cs_file[i].config[1] = 0x101001;467s->cs_file[i].config[2] = 0x020201;468s->cs_file[i].config[3] = 0x10031003;469s->cs_file[i].config[4] = 0x10f1111;470s->cs_file[i].config[5] = 0;471s->cs_file[i].config[6] = 0xf00;472/* In theory we could probe attached devices for some CFG1473* bits here, but we just retain them across resets as they
474* were set initially by omap_gpmc_attach().
475*/
476if (i == 0) {477s->cs_file[i].config[0] &= 0x00433e00;478s->cs_file[i].config[6] |= 1 << 6; /* CSVALID */479omap_gpmc_cs_map(s, i);480} else {481s->cs_file[i].config[0] &= 0x00403c00;482}483}484s->ecc_cs = 0;485s->ecc_ptr = 0;486s->ecc_cfg = 0x3fcff000;487for (i = 0; i < 9; i ++)488ecc_reset(&s->ecc[i]);489}
490
491static int gpmc_wordaccess_only(hwaddr addr)492{
493/* Return true if the register offset is to a register that494* only permits word width accesses.
495* Non-word accesses are only OK for GPMC_NAND_DATA/ADDRESS/COMMAND
496* for any chipselect.
497*/
498if (addr >= 0x60 && addr <= 0x1d4) {499int cs = (addr - 0x60) / 0x30;500addr -= cs * 0x30;501if (addr >= 0x7c && addr < 0x88) {502/* GPMC_NAND_COMMAND, GPMC_NAND_ADDRESS, GPMC_NAND_DATA */503return 0;504}505}506return 1;507}
508
509static uint64_t omap_gpmc_read(void *opaque, hwaddr addr,510unsigned size)511{
512struct omap_gpmc_s *s = opaque;513int cs;514struct omap_gpmc_cs_file_s *f;515
516if (size != 4 && gpmc_wordaccess_only(addr)) {517return omap_badwidth_read32(opaque, addr);518}519
520switch (addr) {521case 0x000: /* GPMC_REVISION */522return s->revision;523
524case 0x010: /* GPMC_SYSCONFIG */525return s->sysconfig;526
527case 0x014: /* GPMC_SYSSTATUS */528return 1; /* RESETDONE */529
530case 0x018: /* GPMC_IRQSTATUS */531return s->irqst;532
533case 0x01c: /* GPMC_IRQENABLE */534return s->irqen;535
536case 0x040: /* GPMC_TIMEOUT_CONTROL */537return s->timeout;538
539case 0x044: /* GPMC_ERR_ADDRESS */540case 0x048: /* GPMC_ERR_TYPE */541return 0;542
543case 0x050: /* GPMC_CONFIG */544return s->config;545
546case 0x054: /* GPMC_STATUS */547return 0x001;548
549case 0x060 ... 0x1d4:550cs = (addr - 0x060) / 0x30;551addr -= cs * 0x30;552f = s->cs_file + cs;553switch (addr) {554case 0x60: /* GPMC_CONFIG1 */555return f->config[0];556case 0x64: /* GPMC_CONFIG2 */557return f->config[1];558case 0x68: /* GPMC_CONFIG3 */559return f->config[2];560case 0x6c: /* GPMC_CONFIG4 */561return f->config[3];562case 0x70: /* GPMC_CONFIG5 */563return f->config[4];564case 0x74: /* GPMC_CONFIG6 */565return f->config[5];566case 0x78: /* GPMC_CONFIG7 */567return f->config[6];568case 0x84 ... 0x87: /* GPMC_NAND_DATA */569if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {570return omap_nand_read(f, 0, size);571}572return 0;573}574break;575
576case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */577return s->prefetch.config1;578case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */579return s->prefetch.transfercount;580case 0x1ec: /* GPMC_PREFETCH_CONTROL */581return s->prefetch.startengine;582case 0x1f0: /* GPMC_PREFETCH_STATUS */583/* NB: The OMAP3 TRM is inconsistent about whether the GPMC584* FIFOTHRESHOLDSTATUS bit should be set when
585* FIFOPOINTER > FIFOTHRESHOLD or when it is >= FIFOTHRESHOLD.
586* Apparently the underlying functional spec from which the TRM was
587* created states that the behaviour is ">=", and this also
588* makes more conceptual sense.
589*/
590return (s->prefetch.fifopointer << 24) |591((s->prefetch.fifopointer >=592((s->prefetch.config1 >> 8) & 0x7f) ? 1 : 0) << 16) |593s->prefetch.count;594
595case 0x1f4: /* GPMC_ECC_CONFIG */596return s->ecc_cs;597case 0x1f8: /* GPMC_ECC_CONTROL */598return s->ecc_ptr;599case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */600return s->ecc_cfg;601case 0x200 ... 0x220: /* GPMC_ECC_RESULT */602cs = (addr & 0x1f) >> 2;603/* TODO: check correctness */604return605((s->ecc[cs].cp & 0x07) << 0) |606((s->ecc[cs].cp & 0x38) << 13) |607((s->ecc[cs].lp[0] & 0x1ff) << 3) |608((s->ecc[cs].lp[1] & 0x1ff) << 19);609
610case 0x230: /* GPMC_TESTMODE_CTRL */611return 0;612case 0x234: /* GPMC_PSA_LSB */613case 0x238: /* GPMC_PSA_MSB */614return 0x00000000;615}616
617OMAP_BAD_REG(addr);618return 0;619}
620
621static void omap_gpmc_write(void *opaque, hwaddr addr,622uint64_t value, unsigned size)623{
624struct omap_gpmc_s *s = opaque;625int cs;626struct omap_gpmc_cs_file_s *f;627
628if (size != 4 && gpmc_wordaccess_only(addr)) {629omap_badwidth_write32(opaque, addr, value);630return;631}632
633switch (addr) {634case 0x000: /* GPMC_REVISION */635case 0x014: /* GPMC_SYSSTATUS */636case 0x054: /* GPMC_STATUS */637case 0x1f0: /* GPMC_PREFETCH_STATUS */638case 0x200 ... 0x220: /* GPMC_ECC_RESULT */639case 0x234: /* GPMC_PSA_LSB */640case 0x238: /* GPMC_PSA_MSB */641OMAP_RO_REG(addr);642break;643
644case 0x010: /* GPMC_SYSCONFIG */645if ((value >> 3) == 0x3)646fprintf(stderr, "%s: bad SDRAM idle mode %"PRIi64"\n",647__func__, value >> 3);648if (value & 2)649omap_gpmc_reset(s);650s->sysconfig = value & 0x19;651break;652
653case 0x018: /* GPMC_IRQSTATUS */654s->irqst &= ~value;655omap_gpmc_int_update(s);656break;657
658case 0x01c: /* GPMC_IRQENABLE */659s->irqen = value & 0xf03;660omap_gpmc_int_update(s);661break;662
663case 0x040: /* GPMC_TIMEOUT_CONTROL */664s->timeout = value & 0x1ff1;665break;666
667case 0x044: /* GPMC_ERR_ADDRESS */668case 0x048: /* GPMC_ERR_TYPE */669break;670
671case 0x050: /* GPMC_CONFIG */672s->config = value & 0xf13;673break;674
675case 0x060 ... 0x1d4:676cs = (addr - 0x060) / 0x30;677addr -= cs * 0x30;678f = s->cs_file + cs;679switch (addr) {680case 0x60: /* GPMC_CONFIG1 */681f->config[0] = value & 0xffef3e13;682break;683case 0x64: /* GPMC_CONFIG2 */684f->config[1] = value & 0x001f1f8f;685break;686case 0x68: /* GPMC_CONFIG3 */687f->config[2] = value & 0x001f1f8f;688break;689case 0x6c: /* GPMC_CONFIG4 */690f->config[3] = value & 0x1f8f1f8f;691break;692case 0x70: /* GPMC_CONFIG5 */693f->config[4] = value & 0x0f1f1f1f;694break;695case 0x74: /* GPMC_CONFIG6 */696f->config[5] = value & 0x00000fcf;697break;698case 0x78: /* GPMC_CONFIG7 */699if ((f->config[6] ^ value) & 0xf7f) {700omap_gpmc_cs_unmap(s, cs);701f->config[6] = value & 0x00000f7f;702omap_gpmc_cs_map(s, cs);703}704break;705case 0x7c ... 0x7f: /* GPMC_NAND_COMMAND */706if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {707nand_setpins(f->dev, 1, 0, 0, 1, 0); /* CLE */708omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);709}710break;711case 0x80 ... 0x83: /* GPMC_NAND_ADDRESS */712if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {713nand_setpins(f->dev, 0, 1, 0, 1, 0); /* ALE */714omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size);715}716break;717case 0x84 ... 0x87: /* GPMC_NAND_DATA */718if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) {719omap_nand_write(f, 0, value, size);720}721break;722default:723goto bad_reg;724}725break;726
727case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */728if (!s->prefetch.startengine) {729uint32_t newconfig1 = value & 0x7f8f7fbf;730uint32_t changed;731changed = newconfig1 ^ s->prefetch.config1;732if (changed & (0x80 | 0x7000000)) {733/* Turning the engine on or off, or mapping it somewhere else.734* cs_map() and cs_unmap() check the prefetch config and
735* overall CSVALID bits, so it is sufficient to unmap-and-map
736* both the old cs and the new one. Note that we adhere to
737* the "unmap/change config/map" order (and not unmap twice
738* if newcs == oldcs), otherwise we'll try to delete the wrong
739* memory region.
740*/
741int oldcs = prefetch_cs(s->prefetch.config1);742int newcs = prefetch_cs(newconfig1);743omap_gpmc_cs_unmap(s, oldcs);744if (oldcs != newcs) {745omap_gpmc_cs_unmap(s, newcs);746}747s->prefetch.config1 = newconfig1;748omap_gpmc_cs_map(s, oldcs);749if (oldcs != newcs) {750omap_gpmc_cs_map(s, newcs);751}752} else {753s->prefetch.config1 = newconfig1;754}755}756break;757
758case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */759if (!s->prefetch.startengine) {760s->prefetch.transfercount = value & 0x3fff;761}762break;763
764case 0x1ec: /* GPMC_PREFETCH_CONTROL */765if (s->prefetch.startengine != (value & 1)) {766s->prefetch.startengine = value & 1;767if (s->prefetch.startengine) {768/* Prefetch engine start */769s->prefetch.count = s->prefetch.transfercount;770if (s->prefetch.config1 & 1) {771/* Write */772s->prefetch.fifopointer = 64;773} else {774/* Read */775s->prefetch.fifopointer = 0;776fill_prefetch_fifo(s);777}778} else {779/* Prefetch engine forcibly stopped. The TRM780* doesn't define the behaviour if you do this.
781* We clear the prefetch count, which means that
782* we permit no more writes, and don't read any
783* more data from NAND. The CPU can still drain
784* the FIFO of unread data.
785*/
786s->prefetch.count = 0;787}788omap_gpmc_int_update(s);789}790break;791
792case 0x1f4: /* GPMC_ECC_CONFIG */793s->ecc_cs = 0x8f;794break;795case 0x1f8: /* GPMC_ECC_CONTROL */796if (value & (1 << 8))797for (cs = 0; cs < 9; cs ++)798ecc_reset(&s->ecc[cs]);799s->ecc_ptr = value & 0xf;800if (s->ecc_ptr == 0 || s->ecc_ptr > 9) {801s->ecc_ptr = 0;802s->ecc_cs &= ~1;803}804break;805case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */806s->ecc_cfg = value & 0x3fcff1ff;807break;808case 0x230: /* GPMC_TESTMODE_CTRL */809if (value & 7)810fprintf(stderr, "%s: test mode enable attempt\n", __func__);811break;812
813default:814bad_reg:815OMAP_BAD_REG(addr);816return;817}818}
819
820static const MemoryRegionOps omap_gpmc_ops = {821.read = omap_gpmc_read,822.write = omap_gpmc_write,823.endianness = DEVICE_NATIVE_ENDIAN,824};825
826struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu,827hwaddr base,828qemu_irq irq, qemu_irq drq)829{
830int cs;831struct omap_gpmc_s *s = g_new0(struct omap_gpmc_s, 1);832
833memory_region_init_io(&s->iomem, NULL, &omap_gpmc_ops, s, "omap-gpmc", 0x1000);834memory_region_add_subregion(get_system_memory(), base, &s->iomem);835
836s->irq = irq;837s->drq = drq;838s->accept_256 = cpu_is_omap3630(mpu);839s->revision = cpu_class_omap3(mpu) ? 0x50 : 0x20;840s->lastirq = 0;841omap_gpmc_reset(s);842
843/* We have to register a different IO memory handler for each844* chip select region in case a NAND device is mapped there. We
845* make the region the worst-case size of 256MB and rely on the
846* container memory region in cs_map to chop it down to the actual
847* guest-requested size.
848*/
849for (cs = 0; cs < 8; cs++) {850memory_region_init_io(&s->cs_file[cs].nandiomem, NULL,851&omap_nand_ops,852&s->cs_file[cs],853"omap-nand",854256 * 1024 * 1024);855}856
857memory_region_init_io(&s->prefetch.iomem, NULL, &omap_prefetch_ops, s,858"omap-gpmc-prefetch", 256 * 1024 * 1024);859return s;860}
861
862void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, MemoryRegion *iomem)863{
864struct omap_gpmc_cs_file_s *f;865assert(iomem);866
867if (cs < 0 || cs >= 8) {868fprintf(stderr, "%s: bad chip-select %i\n", __func__, cs);869exit(-1);870}871f = &s->cs_file[cs];872
873omap_gpmc_cs_unmap(s, cs);874f->config[0] &= ~(0xf << 10);875f->iomem = iomem;876omap_gpmc_cs_map(s, cs);877}
878
879void omap_gpmc_attach_nand(struct omap_gpmc_s *s, int cs, DeviceState *nand)880{
881struct omap_gpmc_cs_file_s *f;882assert(nand);883
884if (cs < 0 || cs >= 8) {885fprintf(stderr, "%s: bad chip-select %i\n", __func__, cs);886exit(-1);887}888f = &s->cs_file[cs];889
890omap_gpmc_cs_unmap(s, cs);891f->config[0] &= ~(0xf << 10);892f->config[0] |= (OMAP_GPMC_NAND << 10);893f->dev = nand;894if (nand_getbuswidth(f->dev) == 16) {895f->config[0] |= OMAP_GPMC_16BIT << 12;896}897omap_gpmc_cs_map(s, cs);898}
899