aboutsummaryrefslogtreecommitdiff
path: root/hw/omap_gpmc.c
blob: 330826dc5a9cd3111e6094e18b47c67f276f17b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/*
 * TI OMAP general purpose memory controller emulation.
 *
 * Copyright (C) 2007-2009 Nokia Corporation
 * Original code written by Andrzej Zaborowski <andrew@openedhand.com>
 * Enhancements for OMAP3 and NAND support written by Juha Riihimäki
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 or
 * (at your option) any later version of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */
#include "hw.h"
#include "flash.h"
#include "omap.h"
#include "sysbus.h"

/* General-Purpose Memory Controller */
struct omap_gpmc_s {
    qemu_irq irq;
    qemu_irq drq;
    int accept_256;

    uint8_t revision;
    uint8_t sysconfig;
    uint16_t irqst;
    uint16_t irqen;
    uint16_t lastirq;
    uint16_t timeout;
    uint16_t config;
    struct omap_gpmc_cs_file_s {
        uint32_t config[7];
        DeviceState *dev;
        int mmio_index;
        int iomemtype;
    } cs_file[8];
    int ecc_cs;
    int ecc_ptr;
    uint32_t ecc_cfg;
    ECCState ecc[9];
    struct prefetch {
        uint32_t config1; /* GPMC_PREFETCH_CONFIG1 */
        uint32_t transfercount; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */
        int startengine; /* GPMC_PREFETCH_CONTROL:STARTENGINE */
        int fifopointer; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */
        int count; /* GPMC_PREFETCH_STATUS:COUNTVALUE */
        int iomemtype;
        uint8_t fifo[64];
    } prefetch;
};

/* Extract the chip-select value from the prefetch config1 register */
static int prefetch_cs(uint32_t config1)
{
    return (config1 >> 24) & 7;
}

static int prefetch_threshold(uint32_t config1)
{
    return (config1 >> 8) & 0x7f;
}

static void omap_gpmc_int_update(struct omap_gpmc_s *s)
{
    /* The TRM is a bit unclear, but it seems to say that
     * the TERMINALCOUNTSTATUS bit is set only on the
     * transition when the prefetch engine goes from
     * active to inactive, whereas the FIFOEVENTSTATUS
     * bit is held high as long as the fifo has at
     * least THRESHOLD bytes available.
     * So we do the latter here, but TERMINALCOUNTSTATUS
     * is set elsewhere.
     */
    if (s->prefetch.fifopointer >= prefetch_threshold(s->prefetch.config1)) {
        s->irqst |= 1;
    }
    if ((s->irqen & s->irqst) != s->lastirq) {
        s->lastirq = s->irqen & s->irqst;
        qemu_set_irq(s->irq, s->lastirq);
    }
}

static void omap_gpmc_dma_update(struct omap_gpmc_s *s, int value)
{
    if (s->prefetch.config1 & 4) {
        qemu_set_irq(s->drq, value);
    }
}

/* Access functions for when a NAND-like device is mapped into memory:
 * all addresses in the region behave like accesses to the relevant
 * GPMC_NAND_DATA_i register (which is actually implemented to call these)
 */
static uint32_t omap_nand_read8(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        return nand_getio(f->dev);
    case 1: /* 16bit */
        /* reading 8bits from a 16bit device?! */
        return nand_getio(f->dev);
    default:
        return 0;
    }
}

static uint32_t omap_nand_read16(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    uint32_t x1, x2;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        x1 = nand_getio(f->dev);
        x2 = nand_getio(f->dev);
        return (x2 << 8) | x1;
    case 1: /* 16bit */
        return nand_getio(f->dev);
    default:
        return 0;
    }
}

static uint32_t omap_nand_read32(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    uint32_t x1, x2, x3, x4;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        x1 = nand_getio(f->dev);
        x2 = nand_getio(f->dev);
        x3 = nand_getio(f->dev);
        x4 = nand_getio(f->dev);
        return (x4 << 24) | (x3 << 16) | (x2 << 8) | x1;
    case 1: /* 16bit */
        x1 = nand_getio(f->dev);
        x2 = nand_getio(f->dev);
        return (x2 << 16) | x1;
    default:
        return 0;
    }
}

static void omap_nand_write8(void *opaque, target_phys_addr_t addr, uint32_t value)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        nand_setio(f->dev, value & 0xff);
        break;
    case 1: /* 16bit */
        /* writing to a 16bit device with 8bit access!? */
        nand_setio(f->dev, value & 0xffff);
        break;
    default:
        break;
    }
}

static void omap_nand_write16(void *opaque, target_phys_addr_t addr, uint32_t value)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        nand_setio(f->dev, value & 0xff);
        nand_setio(f->dev, (value >> 8) & 0xff);
        break;
    case 1: /* 16bit */
        nand_setio(f->dev, value & 0xffff);
        break;
    default:
        break;
    }
}

static void omap_nand_write32(void *opaque, target_phys_addr_t addr, uint32_t value)
{
    struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque;
    nand_setpins(f->dev, 0, 0, 0, 1, 0);
    switch (((f->config[0] >> 12) & 3)) {
    case 0: /* 8bit */
        nand_setio(f->dev, value & 0xff);
        nand_setio(f->dev, (value >> 8) & 0xff);
        nand_setio(f->dev, (value >> 16) & 0xff);
        nand_setio(f->dev, (value >> 24) & 0xff);
        break;
    case 1: /* 16bit */
        nand_setio(f->dev, value & 0xffff);
        nand_setio(f->dev, (value >> 16) & 0xffff);
        break;
    default:
        break;
    }
}

static CPUReadMemoryFunc *omap_nand_readfn[] =
{
    omap_nand_read8,
    omap_nand_read16,
    omap_nand_read32,
};

static CPUWriteMemoryFunc *omap_nand_writefn[] =
{
    omap_nand_write8,
    omap_nand_write16,
    omap_nand_write32,
};

static void fill_prefetch_fifo(struct omap_gpmc_s *s)
{
    /* Fill the prefetch FIFO by reading data from NAND.
     * We do this synchronously, unlike the hardware which
     * will do this asynchronously. We refill when the
     * FIFO has THRESHOLD bytes free, and we always refill
     * as much data as possible starting at the top end
     * of the FIFO.
     * (We have to refill at THRESHOLD rather than waiting
     * for the FIFO to empty to allow for the case where
     * the FIFO size isn't an exact multiple of THRESHOLD
     * and we're doing DMA transfers.)
     * This means we never need to handle wrap-around in
     * the fifo-reading code, and the next byte of data
     * to read is always fifo[63 - fifopointer].
     */
    int fptr;
    int cs = prefetch_cs(s->prefetch.config1);
    int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);
    int bytes;
    /* Don't believe the bit of the OMAP TRM that says that COUNTVALUE
     * and TRANSFERCOUNT are in units of 16 bit words for 16 bit NAND.
     * Instead believe the bit that says it is always a byte count.
     */
    bytes = 64 - s->prefetch.fifopointer;
    if (bytes > s->prefetch.count) {
        bytes = s->prefetch.count;
    }
    s->prefetch.count -= bytes;
    s->prefetch.fifopointer += bytes;
    fptr = 64 - s->prefetch.fifopointer;
    /* Move the existing data in the FIFO so it sits just
     * before what we're about to read in
     */
    while (fptr < (64 - bytes)) {
        s->prefetch.fifo[fptr] = s->prefetch.fifo[fptr + bytes];
        fptr++;
    }
    while (fptr < 64) {
        if (is16bit) {
            uint32_t v = omap_nand_read16(&s->cs_file[cs], 0);
            s->prefetch.fifo[fptr++] = v & 0xff;
            s->prefetch.fifo[fptr++] = (v >> 8) & 0xff;
        } else {
            s->prefetch.fifo[fptr++] = omap_nand_read8(&s->cs_file[cs], 0);
        }
    }
    if (s->prefetch.startengine && (s->prefetch.count == 0)) {
        /* This was the final transfer: raise TERMINALCOUNTSTATUS */
        s->irqst |= 2;
        s->prefetch.startengine = 0;
    }
    /* If there are any bytes in the FIFO at this point then
     * we must raise a DMA request (either this is a final part
     * transfer, or we filled the FIFO in which case we certainly
     * have THRESHOLD bytes available)
     */
    if (s->prefetch.fifopointer != 0) {
        omap_gpmc_dma_update(s, 1);
    }
    omap_gpmc_int_update(s);
}

/* Access functions for a NAND-like device when the prefetch/postwrite
 * engine is enabled -- all addresses in the region behave alike:
 * data is read or written to the FIFO.
 */
static uint32_t omap_gpmc_prefetch_read8(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    uint32_t data;
    if (s->prefetch.config1 & 1) {
        /* The TRM doesn't define the behaviour if you read from the
         * FIFO when the prefetch engine is in write mode. We choose
         * to always return zero.
         */
        return 0;
    }
    /* Note that trying to read an empty fifo repeats the last byte */
    if (s->prefetch.fifopointer) {
        s->prefetch.fifopointer--;
    }
    data = s->prefetch.fifo[63 - s->prefetch.fifopointer];
    if (s->prefetch.fifopointer ==
        (64 - prefetch_threshold (s->prefetch.config1))) {
        /* We've drained THRESHOLD bytes now. So deassert the
         * DMA request, then refill the FIFO (which will probably
         * assert it again.)
         */
        omap_gpmc_dma_update(s, 0);
        fill_prefetch_fifo(s);
    }
    omap_gpmc_int_update(s);
    return data;
}
static uint32_t omap_gpmc_prefetch_read16(void *opaque, target_phys_addr_t addr)
{
    uint32_t r = omap_gpmc_prefetch_read8(opaque, addr);
    r |= (omap_gpmc_prefetch_read8(opaque, addr) << 8);
    return r;
}
static uint32_t omap_gpmc_prefetch_read32(void *opaque, target_phys_addr_t addr)
{
    uint32_t r = omap_gpmc_prefetch_read8(opaque, addr);
    r |= (omap_gpmc_prefetch_read8(opaque, addr) << 8);
    r |= (omap_gpmc_prefetch_read8(opaque, addr) << 16);
    r |= (omap_gpmc_prefetch_read8(opaque, addr) << 24);
    return r;
}

static void omap_gpmc_prefetch_write8(void *opaque, target_phys_addr_t addr,
                                      uint32_t value)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs = prefetch_cs(s->prefetch.config1);
    if ((s->prefetch.config1 & 1) == 0) {
        /* The TRM doesn't define the behaviour of writing to the
         * FIFO when the prefetch engine is in read mode. We
         * choose to ignore the write.
         */
        return;
    }
    if (s->prefetch.count == 0) {
        /* The TRM doesn't define the behaviour of writing to the
         * FIFO if the transfer is complete. We choose to ignore.
         */
        return;
    }
    /* The only reason we do any data buffering in postwrite
     * mode is if we are talking to a 16 bit NAND device, in
     * which case we need to buffer the first byte of the
     * 16 bit word until the other byte arrives.
     */
    int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0);
    if (is16bit) {
        /* fifopointer alternates between 64 (waiting for first
         * byte of word) and 63 (waiting for second byte)
         */
        if (s->prefetch.fifopointer == 64) {
            s->prefetch.fifo[0] = value;
            s->prefetch.fifopointer--;
        } else {
            value = (value << 8) | s->prefetch.fifo[0];
            omap_nand_write16(&s->cs_file[cs], 0, value);
            s->prefetch.count--;
            s->prefetch.fifopointer = 64;
        }
    } else {
        /* Just write the byte : fifopointer remains 64 at all times */
        omap_nand_write8(&s->cs_file[cs], 0, value);
        s->prefetch.count--;
    }
    if (s->prefetch.count == 0) {
        /* Final transfer: raise TERMINALCOUNTSTATUS */
        s->irqst |= 2;
        s->prefetch.startengine = 0;
    }
    omap_gpmc_int_update(s);
}
static void omap_gpmc_prefetch_write16(void *opaque, target_phys_addr_t addr,
                                       uint32_t value)
{
    omap_gpmc_prefetch_write8(opaque, addr, value & 0xff);
    omap_gpmc_prefetch_write8(opaque, addr, (value >> 8) & 0xff);
}
static void omap_gpmc_prefetch_write32(void *opaque, target_phys_addr_t addr,
                                       uint32_t value)
{
    omap_gpmc_prefetch_write8(opaque, addr, value & 0xff);
    omap_gpmc_prefetch_write8(opaque, addr, (value >> 8) & 0xff);
    omap_gpmc_prefetch_write8(opaque, addr, (value >> 16) & 0xff);
    omap_gpmc_prefetch_write8(opaque, addr, (value >> 24) & 0xff);
}

static CPUReadMemoryFunc *omap_gpmc_prefetch_readfn[] =
{
    omap_gpmc_prefetch_read8,
    omap_gpmc_prefetch_read16,
    omap_gpmc_prefetch_read32,
};

static CPUWriteMemoryFunc *omap_gpmc_prefetch_writefn[] =
{
    omap_gpmc_prefetch_write8,
    omap_gpmc_prefetch_write16,
    omap_gpmc_prefetch_write32,
};

static void omap_gpmc_cs_map(struct omap_gpmc_s *s, int cs)
{
    struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];
    if (!(f->config[6] & (1 << 6))) {
        /* Do nothing unless CSVALID */
        return;
    }

    uint32_t mask = (f->config[6] >> 8) & 0xf;
    uint32_t base = f->config[6] & 0x3f;
    uint32_t size;
    /* TODO: check for overlapping regions and report access errors */
    if (mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf
         && !(s->accept_256 && !mask)) {
        fprintf(stderr, "%s: invalid chip-select mask address (0x%x)\n",
                 __FUNCTION__, mask);
    }
    base <<= 24;
    size = (0x0fffffff & ~(mask << 24)) + 1;
    switch ((f->config[0] >> 10) & 3) {
    case 0: /* DEVICETYPE == NOR */
        /* TODO: rather than setting the size of the mapping (which should be
         * constant), the mask should cause wrapping of the address space, so
         * that the same memory becomes accessible at every <i>size</i> bytes
         * starting from <i>base</i>.  */
        if (f->dev && f->mmio_index >= 0) {
            sysbus_mmio_resize(sysbus_from_qdev(f->dev), f->mmio_index, size);
            sysbus_mmio_map(sysbus_from_qdev(f->dev), f->mmio_index, base);
        }
        break;
    case 2: /* DEVICETYPE == NAND */
        if ((s->prefetch.config1 & 0x80) &&
            (prefetch_cs(s->prefetch.config1) == cs)) {
            /* The prefetch engine is enabled for this CS: map the FIFO */
            cpu_register_physical_memory(base, size, s->prefetch.iomemtype);
        } else {
            cpu_register_physical_memory(base, size, f->iomemtype);
        }
        break;
    default:
        break;
    }
}

static void omap_gpmc_cs_unmap(struct omap_gpmc_s *s, int cs)
{
    struct omap_gpmc_cs_file_s *f = &s->cs_file[cs];
    if (!(f->config[6] & (1 << 6))) {
        /* Do nothing unless CSVALID */
        return;
    }

    switch ((f->config[0] >> 10) & 3) {
    case 0: /* DEVICETYPE == NOR */
        if (f->dev && f->mmio_index >= 0) {
            sysbus_mmio_unmap(sysbus_from_qdev(f->dev), f->mmio_index);
        }
        break;
    case 2: /* DEVICETYPE == NAND */
        {
            uint32_t mask = (f->config[6] >> 8) & 0xf;
            uint32_t base = (f->config[6] & 0x3f) << 24;
            uint32_t size = (0x0fffffff & ~(mask << 24)) + 1;
            cpu_register_physical_memory(base, size, IO_MEM_UNASSIGNED);
        }
        break;
    default:
        break;
    }
}

void omap_gpmc_reset(struct omap_gpmc_s *s)
{
    int i;

    s->sysconfig = 0;
    s->irqst = 0;
    s->irqen = 0;
    omap_gpmc_int_update(s);
    s->timeout = 0;
    s->config = 0xa00;
    s->prefetch.config1 = 0x00004000;
    s->prefetch.transfercount = 0x00000000;
    s->prefetch.startengine = 0;
    s->prefetch.fifopointer = 0;
    s->prefetch.count = 0;
    for (i = 0; i < 8; i ++) {
        omap_gpmc_cs_unmap(s, i);
        s->cs_file[i].config[1] = 0x00101001;
        s->cs_file[i].config[2] = 0x00020201;
        s->cs_file[i].config[3] = 0x10031003;
        s->cs_file[i].config[4] = 0x010f1111;
        s->cs_file[i].config[5] = 0;
        s->cs_file[i].config[6] = 0xf00;
        /* FIXME: attached devices should be probed for some of the CFG1 bits
         * for now we just keep those bits intact over resets as they are set
         * initially with omap_gpmc_attach function */
        if (i == 0) {
            s->cs_file[i].config[0] &= 0x00433e00;
            s->cs_file[i].config[6] |= 1 << 6; /* CSVALID */
            omap_gpmc_cs_map(s, i);
        } else {
            /* FIXME: again, this should force device size to 16bit but
             * here instead we keep what omap_gpmc_attach has done */
            s->cs_file[i].config[0] &= 0x00403c00;
        }
    }
    s->ecc_cs = 0;
    s->ecc_ptr = 0;
    s->ecc_cfg = 0x3fcff000;
    for (i = 0; i < 9; i ++) {
        ecc_reset(&s->ecc[i]);
    }
}

static uint32_t omap_gpmc_read32(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x000:	/* GPMC_REVISION */
        return s->revision;

    case 0x010:	/* GPMC_SYSCONFIG */
        return s->sysconfig;

    case 0x014:	/* GPMC_SYSSTATUS */
        return 1;						/* RESETDONE */

    case 0x018:	/* GPMC_IRQSTATUS */
        return s->irqst;

    case 0x01c:	/* GPMC_IRQENABLE */
        return s->irqen;

    case 0x040:	/* GPMC_TIMEOUT_CONTROL */
        return s->timeout;

    case 0x044:	/* GPMC_ERR_ADDRESS */
    case 0x048:	/* GPMC_ERR_TYPE */
        return 0;

    case 0x050:	/* GPMC_CONFIG */
        return s->config;

    case 0x054:	/* GPMC_STATUS */
        return 0x001;

    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x60:	/* GPMC_CONFIG1 */
            return f->config[0];
        case 0x64:	/* GPMC_CONFIG2 */
            return f->config[1];
        case 0x68:	/* GPMC_CONFIG3 */
            return f->config[2];
        case 0x6c:	/* GPMC_CONFIG4 */
            return f->config[3];
        case 0x70:	/* GPMC_CONFIG5 */
            return f->config[4];
        case 0x74:	/* GPMC_CONFIG6 */
            return f->config[5];
        case 0x78:	/* GPMC_CONFIG7 */
            return f->config[6];
        case 0x84:	/* GPMC_NAND_DATA */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                return omap_nand_read32(f, 0);
            }
            return 0;
        default:
            break;
        }
        break;

    case 0x1e0:	/* GPMC_PREFETCH_CONFIG1 */
        return s->prefetch.config1;
    case 0x1e4:	/* GPMC_PREFETCH_CONFIG2 */
        return s->prefetch.transfercount;
    case 0x1ec:	/* GPMC_PREFETCH_CONTROL */
        return s->prefetch.startengine;
    case 0x1f0:	/* GPMC_PREFETCH_STATUS */
        return (s->prefetch.fifopointer << 24) |
                ((s->prefetch.fifopointer >=
                  ((s->prefetch.config1 >> 8) & 0x7f) ? 1 : 0) << 16) |
                s->prefetch.count;

    case 0x1f4:	/* GPMC_ECC_CONFIG */
        return s->ecc_cs;
    case 0x1f8:	/* GPMC_ECC_CONTROL */
        return s->ecc_ptr;
    case 0x1fc:	/* GPMC_ECC_SIZE_CONFIG */
        return s->ecc_cfg;
    case 0x200 ... 0x220:	/* GPMC_ECC_RESULT */
        cs = (addr & 0x1f) >> 2;
        /* TODO: check correctness */
        return ((s->ecc[cs].cp    &  0x07) <<  0) |
                ((s->ecc[cs].cp    &  0x38) << 13) |
                ((s->ecc[cs].lp[0] & 0x1ff) <<  3) |
                ((s->ecc[cs].lp[1] & 0x1ff) << 19);

    case 0x230:	/* GPMC_TESTMODE_CTRL */
        return 0;
    case 0x234:	/* GPMC_PSA_LSB */
    case 0x238:	/* GPMC_PSA_MSB */
        return 0x00000000;
    }

    OMAP_BAD_REG(addr);
    return 0;
}

static uint32_t omap_gpmc_read8(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x84 ... 0x87:	/* GPMC_NAND_DATA */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                return omap_nand_read8(f, 0);
            }
            return 0;
        default:
            return 0;
        }
        break;
    default:
        break;
    }
    OMAP_BAD_REG(addr);
    return 0;
}

static uint32_t omap_gpmc_read16(void *opaque, target_phys_addr_t addr)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x84:	/* GPMC_NAND_DATA */
        case 0x86:
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                return omap_nand_read16(f, 0);
            }
            return 0;
        default:
            break;
        }
        break;
    default:
        break;
    }
    OMAP_BAD_REG(addr);
    return 0;
}

static void omap_gpmc_write32(void *opaque, target_phys_addr_t addr,
                              uint32_t value)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x000:	/* GPMC_REVISION */
    case 0x014:	/* GPMC_SYSSTATUS */
    case 0x054:	/* GPMC_STATUS */
    case 0x1f0:	/* GPMC_PREFETCH_STATUS */
    case 0x200 ... 0x220:	/* GPMC_ECC_RESULT */
    case 0x234:	/* GPMC_PSA_LSB */
    case 0x238:	/* GPMC_PSA_MSB */
        OMAP_RO_REGV(addr, value);
        break;

    case 0x010:	/* GPMC_SYSCONFIG */
        if ((value >> 3) == 0x3)
            fprintf(stderr, "%s: bad SDRAM idle mode %i\n",
                    __FUNCTION__, value >> 3);
        if (value & 2)
            omap_gpmc_reset(s);
        s->sysconfig = value & 0x19;
        break;

    case 0x018:	/* GPMC_IRQSTATUS */
        s->irqen &= ~value;
        omap_gpmc_int_update(s);
        break;

    case 0x01c:	/* GPMC_IRQENABLE */
        s->irqen = value & 0xf03;
        omap_gpmc_int_update(s);
        break;

    case 0x040:	/* GPMC_TIMEOUT_CONTROL */
        s->timeout = value & 0x1ff1;
        break;

    case 0x044:	/* GPMC_ERR_ADDRESS */
    case 0x048:	/* GPMC_ERR_TYPE */
        break;

    case 0x050:	/* GPMC_CONFIG */
        s->config = value & 0xf13;
        break;

    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x60:	/* GPMC_CONFIG1 */
            f->config[0] = value & 0xffef3e13;
            break;
        case 0x64:	/* GPMC_CONFIG2 */
            f->config[1] = value & 0x001f1f8f;
            break;
        case 0x68:	/* GPMC_CONFIG3 */
            f->config[2] = value & 0x001f1f8f;
            break;
        case 0x6c:	/* GPMC_CONFIG4 */
            f->config[3] = value & 0x1f8f1f8f;
            break;
        case 0x70:	/* GPMC_CONFIG5 */
            f->config[4] = value & 0x0f1f1f1f;
            break;
        case 0x74:	/* GPMC_CONFIG6 */
            f->config[5] = value & 0x00000fcf;
            break;
        case 0x78:	/* GPMC_CONFIG7 */
            if ((f->config[6] ^ value) & 0xf7f) {
                omap_gpmc_cs_unmap(s, cs);
                f->config[6] = value & 0x00000f7f;
                omap_gpmc_cs_map(s, cs);
            }
            break;
        case 0x7c:	/* GPMC_NAND_COMMAND */
        case 0x80:	/* GPMC_NAND_ADDRESS */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                switch (addr) {
                case 0x7c: nand_setpins(f->dev, 1, 0, 0, 1, 0); break; /* CLE */
                case 0x80: nand_setpins(f->dev, 0, 1, 0, 1, 0); break; /* ALE */
                default: break;
                }
                switch (((f->config[0] >> 12) & 3)) {
                case 0: /* 8bit */
                    nand_setio(f->dev, value & 0xff);
                    nand_setio(f->dev, (value >> 8) & 0xff);
                    nand_setio(f->dev, (value >> 16) & 0xff);
                    nand_setio(f->dev, (value >> 24) & 0xff);
                    break;
                case 1: /* 16bit */
                    nand_setio(f->dev, value & 0xffff);
                    nand_setio(f->dev, (value >> 16) & 0xffff);
                    break;
                default:
                    break;
                }
            }
            break;
        case 0x84:  /* GPMC_NAND_DATA */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                omap_nand_write32(f, 0, value);
            }
            break;
        default:
            goto bad_reg;
        }
        break;

    case 0x1e0:	/* GPMC_PREFETCH_CONFIG1 */
        if (!s->prefetch.startengine) {
            uint32_t oldconfig1 = s->prefetch.config1;
            uint32_t changed;
            s->prefetch.config1 = value & 0x7f8f7fbf;
            changed = oldconfig1 ^ s->prefetch.config1;
            if (changed & (0x80 | 0x7000000)) {
                /* Turning the engine on or off, or mapping it somewhere else.
                 * cs_map() and cs_unmap() check the prefetch config and
                 * overall CSVALID bits, so it is sufficient to unmap-and-map
                 * both the old cs and the new one.
                 */
                int oldcs = prefetch_cs(oldconfig1);
                int newcs = prefetch_cs(s->prefetch.config1);
                omap_gpmc_cs_unmap(s, oldcs);
                omap_gpmc_cs_map(s, oldcs);
                if (newcs != oldcs) {
                    omap_gpmc_cs_unmap(s, newcs);
                    omap_gpmc_cs_map(s, newcs);
                }
            }
        }
        break;

    case 0x1e4:	/* GPMC_PREFETCH_CONFIG2 */
        if (!s->prefetch.startengine) {
            s->prefetch.transfercount = value & 0x3fff;
        }
        break;

    case 0x1ec:	/* GPMC_PREFETCH_CONTROL */
        if (s->prefetch.startengine != (value & 1)) {
            s->prefetch.startengine = value & 1;
            if (s->prefetch.startengine) {
                /* Prefetch engine start */
                s->prefetch.count = s->prefetch.transfercount;
                if (s->prefetch.config1 & 1) {
                    /* Write */
                    s->prefetch.fifopointer = 64;
                } else {
                    /* Read */
                    s->prefetch.fifopointer = 0;
                    fill_prefetch_fifo(s);
                }
            } else {
                /* Prefetch engine forcibly stopped. The TRM
                 * doesn't define the behaviour if you do this.
                 * We clear the prefetch count, which means that
                 * we permit no more writes, and don't read any
                 * more data from NAND. The CPU can still drain
                 * the FIFO of unread data.
                 */
                s->prefetch.count = 0;
            }
            omap_gpmc_int_update(s);
        }
        break;

    case 0x1f4:	/* GPMC_ECC_CONFIG */
        s->ecc_cs = 0x8f;
        break;
    case 0x1f8:	/* GPMC_ECC_CONTROL */
        if (value & (1 << 8))
            for (cs = 0; cs < 9; cs ++)
                ecc_reset(&s->ecc[cs]);
        s->ecc_ptr = value & 0xf;
        if (s->ecc_ptr == 0 || s->ecc_ptr > 9) {
            s->ecc_ptr = 0;
            s->ecc_cs &= ~1;
        }
        break;
    case 0x1fc:	/* GPMC_ECC_SIZE_CONFIG */
        s->ecc_cfg = value & 0x3fcff1ff;
        break;
    case 0x230:	/* GPMC_TESTMODE_CTRL */
        if (value & 7)
            fprintf(stderr, "%s: test mode enable attempt\n", __FUNCTION__);
        break;

    default:
    bad_reg:
        OMAP_BAD_REGV(addr, value);
        return;
    }
}

static void omap_gpmc_write8(void *opaque, target_phys_addr_t addr,
                             uint32_t value)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x7c ... 0x7f:	/* GPMC_NAND_COMMAND */
        case 0x80 ... 0x83:	/* GPMC_NAND_ADDRESS */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                switch (addr) {
                case 0x7c ... 0x7f:
                    nand_setpins(f->dev, 1, 0, 0, 1, 0); /* CLE */
                    break;
                case 0x80 ... 0x83:
                    nand_setpins(f->dev, 0, 1, 0, 1, 0); /* ALE */
                    break;
                default:
                    break;
                }
                switch (((f->config[0] >> 12) & 3)) {
                case 0: /* 8bit */
                    nand_setio(f->dev, value & 0xff);
                    break;
                case 1: /* 16bit */
                    /* writing to a 16bit device with 8bit access!? */
                    nand_setio(f->dev, value & 0xffff);
                    break;
                default:
                    break;
                }
            }
            break;
        case 0x84 ... 0x87: /* GPMC_NAND_DATA */
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                omap_nand_write8(f, 0, value);
            }
            break;
        default:
            goto bad_reg;
        }
        break;
    default:
    bad_reg:
        OMAP_BAD_REGV(addr, value);
        return;
    }
}

static void omap_gpmc_write16(void *opaque, target_phys_addr_t addr,
                              uint32_t value)
{
    struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
    int cs;
    struct omap_gpmc_cs_file_s *f;

    switch (addr) {
    case 0x060 ... 0x1d4:
        cs = (addr - 0x060) / 0x30;
        addr -= cs * 0x30;
        f = s->cs_file + cs;
        switch (addr) {
        case 0x7c:	/* GPMC_NAND_COMMAND */
        case 0x7e:
        case 0x80:	/* GPMC_NAND_ADDRESS */
        case 0x82:
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                switch (addr) {
                case 0x7c:
                case 0x7e: 
                    nand_setpins(f->dev, 1, 0, 0, 1, 0); /* CLE */
                    break;
                case 0x80:
                case 0x82:
                    nand_setpins(f->dev, 0, 1, 0, 1, 0); /* ALE */
                    break;
                case 0x84:
                case 0x86:
                    nand_setpins(f->dev, 0, 0, 0, 1, 0);
                    break;
                default:
                    break;
                }
                switch (((f->config[0] >> 12) & 3)) {
                case 0: /* 8bit */
                    nand_setio(f->dev, value & 0xff);
                    nand_setio(f->dev, (value >> 8) & 0xff);
                    break;
                case 1: /* 16bit */
                    nand_setio(f->dev, value & 0xffff);
                    break;
                default:
                    break;
                }
            }
            break;
        case 0x84:  /* GPMC_NAND_DATA */
        case 0x86:
            if (((f->config[0] >> 10) & 3) == 2) { /* NAND like? */
                omap_nand_write16(f, 0, value);
            }
            break;
        default:
            goto bad_reg;
        }
        break;
    default:
    bad_reg:
        OMAP_BAD_REGV(addr, value);
        return;
    }
}

static CPUReadMemoryFunc * const omap_gpmc_readfn[] = {
    omap_gpmc_read8,
    omap_gpmc_read16,
    omap_gpmc_read32,
};

static CPUWriteMemoryFunc * const omap_gpmc_writefn[] = {
    omap_gpmc_write8,
    omap_gpmc_write16,
    omap_gpmc_write32,
};

struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu,
                                   target_phys_addr_t base,
                                   qemu_irq irq, qemu_irq drq)
{
    int iomemtype, cs;
    struct omap_gpmc_s *s = qemu_mallocz(sizeof(*s));

    s->irq = irq;
    s->drq = drq;
    s->accept_256 = cpu_is_omap3630(mpu);
    s->revision = cpu_class_omap3(mpu) ? 0x50 : 0x20;
    s->lastirq = 0;
    omap_gpmc_reset(s);

    iomemtype = cpu_register_io_memory(omap_gpmc_readfn,
                    omap_gpmc_writefn, s, DEVICE_NATIVE_ENDIAN);
    cpu_register_physical_memory(base, 0x1000, iomemtype);

    /* We have to register a different IO memory handler for each
     * chip select region in case a NAND device is mapped there.  */
    for (cs = 0; cs < 8; cs++) {
        s->cs_file[cs].iomemtype = cpu_register_io_memory(omap_nand_readfn,
                                                          omap_nand_writefn,
                                                          &s->cs_file[cs],
                                                          DEVICE_NATIVE_ENDIAN);
    }

    s->prefetch.iomemtype = cpu_register_io_memory(omap_gpmc_prefetch_readfn,
                                                   omap_gpmc_prefetch_writefn,
                                                   s, DEVICE_NATIVE_ENDIAN);
    return s;
}

void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, DeviceState *dev,
                      int mmio_index, int devicetype)
{
    struct omap_gpmc_cs_file_s *f;

    if (cs < 0 || cs >= 8) {
        hw_error("%s: bad chip-select %i\n", __FUNCTION__, cs);
    }
    f = &s->cs_file[cs];
    if (dev != f->dev || mmio_index != f->mmio_index ||
        devicetype != ((f->config[0] >> 10) & 3)) {
        omap_gpmc_cs_unmap(s, cs);
        f->dev = dev;
        f->mmio_index = mmio_index;
        f->config[0] &= ~(0x3 << 10);
        f->config[0] |= (devicetype & 3) << 10;
        if (devicetype == 2) { /* NAND */
            f->config[0] &= ~(0x3 << 12);
            if (nand_getbuswidth(f->dev) == 16)
                f->config[0] |= 1 << 12;
        }
        omap_gpmc_cs_map(s, cs);
    }
}