~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to debian/patches/ubuntu/linaro/0043-add-hw-omap3_mmc.c.patch

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-04 12:13:08 UTC
  • mfrom: (10.1.45 sid)
  • Revision ID: package-import@ubuntu.com-20140204121308-1xq92lrfs75agw2g
Tags: 1.7.0+dfsg-3ubuntu1~ppa1
* Merge 1.7.0+dfsg-3 from debian.  Remaining changes:
  - debian/patches/ubuntu:
    * expose-vmx_qemu64cpu.patch
    * linaro (omap3) and arm64 patches
    * ubuntu/target-ppc-add-stubs-for-kvm-breakpoints: fix FTBFS
      on ppc
    * ubuntu/CVE-2013-4377.patch: fix denial of service via virtio
  - debian/qemu-system-x86.modprobe: set kvm_intel nested=1 options
  - debian/control:
    * add arm64 to Architectures
    * add qemu-common and qemu-system-aarch64 packages
  - debian/qemu-system-common.install: add debian/tmp/usr/lib
  - debian/qemu-system-common.preinst: add kvm group
  - debian/qemu-system-common.postinst: remove acl placed by udev,
    and add udevadm trigger.
  - qemu-system-x86.links: add eepro100.rom, remove pxe-virtio,
    pxe-e1000 and pxe-rtl8139.
  - add qemu-system-x86.qemu-kvm.upstart and .default
  - qemu-user-static.postinst-in: remove arm64 binfmt
  - debian/rules:
    * allow parallel build
    * add aarch64 to system_targets and sys_systems
    * add qemu-kvm-spice links
    * install qemu-system-x86.modprobe
  - add debian/qemu-system-common.links for OVMF.fd link
* Remove kvm-img, kvm-nbd, kvm-ifup and kvm-ifdown symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
From f0a8899fc97a07be328a903ba2e055a772461baa Mon Sep 17 00:00:00 2001
 
2
From: Peter Maydell <peter.maydell@linaro.org>
 
3
Date: Mon, 18 Feb 2013 16:58:30 +0000
 
4
Subject: [PATCH 43/70] add hw/omap3_mmc.c
 
5
MIME-Version: 1.0
 
6
Content-Type: text/plain; charset=UTF-8
 
7
Content-Transfer-Encoding: 8bit
 
8
 
 
9
Add omap3 specific mmc/sd driver
 
10
 
 
11
omap3_mmc: fix cmd12/52 handling during inactive data transfer
 
12
 
 
13
Signed-off-by: Juha Riihimäki <juha.riihimaki@nokia.com>
 
14
 
 
15
omap3_mmc qdev conversion
 
16
 
 
17
Signed-off-by: Juha Riihimäki <juha.riihimaki@nokia.com>
 
18
 
 
19
make-omap3_mmc-status-register
 
20
 
 
21
make omap3_mmc status register directly accessible
 
22
 
 
23
Changes in the status register are immediately in effect, i.e. the guest
 
24
does not need to read the status register first before writing changes to it.
 
25
This change may cause problems if guest code does not handle properly the
 
26
case where transfers get completed immediately when initiated.
 
27
 
 
28
Signed-off-by: Juha Riihimäki <juha.riihimaki@nokia.com>
 
29
 
 
30
hw-omap3_mmc-c-use-symbolic-co
 
31
 
 
32
hw/omap3_mmc.c: Use symbolic constants for status register bits
 
33
 
 
34
Use symbolic constants rather than hardcoded numeric values for the
 
35
OMAP3 MMC status register bits, to improve readability.
 
36
 
 
37
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
 
38
 
 
39
hw-omap3_mmc-c-set-tc-status-b
 
40
 
 
41
hw/omap3_mmc.c: Set TC status bit for R1b no-data commands
 
42
 
 
43
We emulate MMC commands with response type R1b and no data transfer
 
44
instantaneously, so we must set the TC bit as well as the CC bit,
 
45
to indicate that the "busy" period has ended.
 
46
 
 
47
Without this fix the Linux driver will hang waiting for a TC interrupt
 
48
that never happens when it tries to do a block erase.
 
49
 
 
50
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
 
51
---
 
52
 hw/sd/Makefile.objs   |   1 +
 
53
 hw/sd/omap3_mmc.c     | 801 ++++++++++++++++++++++++++++++++++++++++++++++++++
 
54
 include/hw/arm/omap.h |   4 +
 
55
 3 files changed, 806 insertions(+)
 
56
 create mode 100644 hw/sd/omap3_mmc.c
 
57
 
 
58
diff --git a/hw/sd/Makefile.objs b/hw/sd/Makefile.objs
 
59
index f1aed83..aee6af2 100644
 
60
--- a/hw/sd/Makefile.objs
 
61
+++ b/hw/sd/Makefile.objs
 
62
@@ -5,4 +5,5 @@ common-obj-$(CONFIG_SDHCI) += sdhci.o
 
63
 
 
64
 obj-$(CONFIG_MILKYMIST) += milkymist-memcard.o
 
65
 obj-$(CONFIG_OMAP) += omap_mmc.o
 
66
+obj-$(CONFIG_OMAP) += omap3_mmc.o
 
67
 obj-$(CONFIG_PXA2XX) += pxa2xx_mmci.o
 
68
diff --git a/hw/sd/omap3_mmc.c b/hw/sd/omap3_mmc.c
 
69
new file mode 100644
 
70
index 0000000..1aa233d
 
71
--- /dev/null
 
72
+++ b/hw/sd/omap3_mmc.c
 
73
@@ -0,0 +1,801 @@
 
74
+/*
 
75
+ * OMAP3 MMC/SD/SDIO interface emulation
 
76
+ *
 
77
+ * Copyright (C) 2008 yajin  <yajin@vm-kernel.org>
 
78
+ * Copyright (C) 2009 Nokia Corporation
 
79
+ *
 
80
+ * This program is free software; you can redistribute it and/or
 
81
+ * modify it under the terms of the GNU General Public License as
 
82
+ * published by the Free Software Foundation; either version 2 or
 
83
+ * (at your option) version 3 of the License.
 
84
+ *
 
85
+ * This program is distributed in the hope that it will be useful,
 
86
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
87
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
88
+ * GNU General Public License for more details.
 
89
+ *
 
90
+ * You should have received a copy of the GNU General Public License
 
91
+ * along with this program; if not, write to the Free Software
 
92
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 
93
+ * MA 02111-1307 USA
 
94
+ */
 
95
+#include "hw/hw.h"
 
96
+#include "hw/arm/omap.h"
 
97
+#include "hw/sd.h"
 
98
+#include "hw/sysbus.h"
 
99
+
 
100
+/* debug levels:
 
101
+   0 - no debug
 
102
+   1 - print non-fatal errors
 
103
+   2 - print out all commands in processing order
 
104
+   3 - dump all register accesses and buffer management */
 
105
+#define MMC_DEBUG_LEVEL 0
 
106
+
 
107
+#if MMC_DEBUG_LEVEL>0
 
108
+#define TRACE(fmt,...) fprintf(stderr, "%s: " fmt "\n", \
 
109
+                               __FUNCTION__, ##__VA_ARGS__)
 
110
+#else
 
111
+#define TRACE(...)
 
112
+#endif
 
113
+
 
114
+#if MMC_DEBUG_LEVEL>1
 
115
+#define TRACE1(...) TRACE(__VA_ARGS__)
 
116
+#else
 
117
+#define TRACE1(...)
 
118
+#endif
 
119
+
 
120
+#if MMC_DEBUG_LEVEL>2
 
121
+#define TRACE2(...) TRACE(__VA_ARGS__)
 
122
+#else
 
123
+#define TRACE2(...)
 
124
+#endif
 
125
+
 
126
+#define TYPE_OMAP3_MMC "omap3_mmc"
 
127
+#define OMAP3_MMC(obj) OBJECT_CHECK(OMAP3MMCState, (obj), TYPE_OMAP3_MMC)
 
128
+
 
129
+typedef struct OMAP3MMCState {
 
130
+    SysBusDevice parent_obj;
 
131
+
 
132
+    MemoryRegion iomem;
 
133
+    qemu_irq irq;
 
134
+    qemu_irq dma[2];
 
135
+    qemu_irq coverswitch;
 
136
+    omap_clk clk;
 
137
+    SDState *card;
 
138
+
 
139
+    uint32_t sysconfig;
 
140
+    uint32_t sysstatus;
 
141
+    uint32_t csre;
 
142
+    uint32_t systest;
 
143
+    uint32_t con;
 
144
+    uint32_t pwcnt;
 
145
+    uint32_t blk;
 
146
+    uint32_t arg;
 
147
+    uint32_t cmd;
 
148
+    uint32_t rsp10;
 
149
+    uint32_t rsp32;
 
150
+    uint32_t rsp54;
 
151
+    uint32_t rsp76;
 
152
+    uint32_t data;
 
153
+    uint32_t pstate;
 
154
+    uint32_t hctl;
 
155
+    uint32_t sysctl;
 
156
+    uint32_t stat;
 
157
+    uint32_t ie;
 
158
+    uint32_t ise;
 
159
+    uint32_t ac12;
 
160
+    uint32_t capa;
 
161
+    uint32_t cur_capa;
 
162
+    uint32_t rev;
 
163
+
 
164
+    uint16_t blen_counter;
 
165
+    uint16_t nblk_counter;
 
166
+
 
167
+    uint32_t fifo[256];
 
168
+    int fifo_start;
 
169
+    int fifo_len;
 
170
+
 
171
+    int ddir;
 
172
+    int transfer;
 
173
+    int stop;
 
174
+} OMAP3MMCState;
 
175
+
 
176
+/* Bit names for STAT/IC/IE registers */
 
177
+#define STAT_CC (1 << 0)
 
178
+#define STAT_TC (1 << 1)
 
179
+#define STAT_BGE (1 << 2)
 
180
+#define STAT_BWR (1 << 4)
 
181
+#define STAT_BRR (1 << 5)
 
182
+#define STAT_CIRQ (1 << 8)
 
183
+#define STAT_OBI (1 << 9)
 
184
+#define STAT_ERRI (1 << 15)
 
185
+#define STAT_CTO (1 << 16)
 
186
+#define STAT_CCRC (1 << 17)
 
187
+#define STAT_CEB (1 << 18)
 
188
+#define STAT_CIE (1 << 19)
 
189
+#define STAT_DTO (1 << 20)
 
190
+#define STAT_DCRC (1 << 21)
 
191
+#define STAT_DEB (1 << 22)
 
192
+#define STAT_ACE (1 << 24)
 
193
+#define STAT_CERR (1 << 28)
 
194
+#define STAT_BADA (1 << 29)
 
195
+
 
196
+#define STAT_MASK \
 
197
+    (STAT_CC|STAT_TC|STAT_BGE| \
 
198
+    STAT_BWR|STAT_BRR| \
 
199
+    STAT_CIRQ|STAT_OBI| \
 
200
+    STAT_ERRI| \
 
201
+    STAT_CTO|STAT_CCRC|STAT_CEB|STAT_CIE| \
 
202
+    STAT_DTO|STAT_DCRC|STAT_DEB| \
 
203
+    STAT_ACE|STAT_CERR|STAT_BADA)
 
204
+
 
205
+static void omap3_mmc_reset(DeviceState *dev)
 
206
+{
 
207
+    OMAP3MMCState *s = OMAP3_MMC(dev);
 
208
+
 
209
+    s->sysconfig = 0x00000015;
 
210
+    s->sysstatus = 0;
 
211
+    s->csre      = 0;
 
212
+    s->systest   = 0;
 
213
+    s->con       = 0x00000500;
 
214
+    s->pwcnt     = 0;
 
215
+    s->blk       = 0;
 
216
+    s->arg       = 0;
 
217
+    s->cmd       = 0;
 
218
+    s->rsp10     = 0;
 
219
+    s->rsp32     = 0;
 
220
+    s->rsp54     = 0;
 
221
+    s->rsp76     = 0;
 
222
+    s->data      = 0;
 
223
+    s->pstate    = 0x00040000;
 
224
+    s->hctl      = 0;
 
225
+    s->sysctl    = 0;
 
226
+    s->stat      = 0;
 
227
+    s->ie        = 0;
 
228
+    s->ise       = 0;
 
229
+    s->ac12      = 0;
 
230
+    s->capa      = 0x00e10080;
 
231
+    s->cur_capa  = 0;
 
232
+    s->rev       = 0x26000000;
 
233
+    
 
234
+    s->blen_counter = 0;
 
235
+    s->nblk_counter = 0;
 
236
+    
 
237
+    memset(s->fifo, 0, sizeof(s->fifo));
 
238
+    s->fifo_start = 0;
 
239
+    s->fifo_len   = 0;
 
240
+    
 
241
+    s->ddir       = 0;
 
242
+    s->transfer   = 0;
 
243
+    s->stop       = 0;
 
244
+
 
245
+    if (s->card) {
 
246
+        sd_reset(s->card);
 
247
+    }
 
248
+}
 
249
+
 
250
+typedef enum
 
251
+{
 
252
+    sd_nore = 0,     /* no response */
 
253
+    sd_136_bits = 1, /* response length 136 bits */
 
254
+    sd_48_bits = 2,  /* response length 48 bits */
 
255
+    sd_48b_bits = 3, /* response length 48 bits with busy after response */
 
256
+} omap3_sd_rsp_type_t;
 
257
+
 
258
+static void omap3_mmc_command(OMAP3MMCState *host);
 
259
+
 
260
+static void omap3_mmc_interrupts_update(OMAP3MMCState *s)
 
261
+{
 
262
+    qemu_set_irq(s->irq, !!(s->stat & s->ie & s->ise));
 
263
+}
 
264
+
 
265
+static void omap3_mmc_fifolevel_update(OMAP3MMCState *host)
 
266
+{
 
267
+    enum { ongoing, ready, aborted } state = ongoing;
 
268
+    
 
269
+    if ((host->cmd & (1 << 21))) { /* DP */
 
270
+        if (host->ddir) {
 
271
+            TRACE2("receive, dma=%d, fifo_len=%d bytes",
 
272
+                   host->cmd & 1, host->fifo_len * 4);
 
273
+            
 
274
+            /* omap3_mmc_transfer ensures we always have data in FIFO
 
275
+               during receive as long as all data has not been transferred -
 
276
+               NOTE that the actual transfer may be finished already (i.e.
 
277
+               host->transfer is cleared) but not all data has been read out
 
278
+               from FIFO yet */
 
279
+            if (host->fifo_len) {
 
280
+                if (host->cmd & 1) { /* DE */
 
281
+                    if (host->fifo_len * 4 == (host->blk & 0x7ff)) { /* BLEN */
 
282
+                        if (host->stop)
 
283
+                            state = aborted;
 
284
+                        else
 
285
+                            qemu_irq_raise(host->dma[1]);
 
286
+                    } else
 
287
+                        qemu_irq_lower(host->dma[1]);
 
288
+                } else {
 
289
+                    if (host->stop 
 
290
+                        && host->fifo_len * 4 == (host->blk & 0x7ff))
 
291
+                        state = aborted;
 
292
+                    else {
 
293
+                        host->pstate |= 0x0800; /* BRE */
 
294
+                        host->stat   |= STAT_BRR;
 
295
+                    }
 
296
+                }
 
297
+            }
 
298
+            else
 
299
+                state = host->stop ? aborted : ready;
 
300
+        } else {
 
301
+            /* omap3_mmc_transfer keeps FIFO empty during transmit so
 
302
+               we just check all blocks have been transferred or not */
 
303
+            if (host->transfer) {
 
304
+                if (host->cmd & 1) { /* DE */
 
305
+                    if (host->blen_counter == (host->blk & 0x7ff)) { /* BLEN */
 
306
+                        if (host->stop)
 
307
+                            state = aborted;
 
308
+                        else
 
309
+                            qemu_irq_raise(host->dma[0]);
 
310
+                    } else
 
311
+                        qemu_irq_lower(host->dma[0]);
 
312
+                } else {
 
313
+                    if (host->stop
 
314
+                        && host->blen_counter == (host->blk & 0x7ff))
 
315
+                        state = aborted;
 
316
+                    else {
 
317
+                        host->pstate |= 0x0400; /* BWE */
 
318
+                        host->stat   |= STAT_BWR;
 
319
+                    }
 
320
+                }
 
321
+            } else
 
322
+                state = host->stop ? aborted : ready;
 
323
+        }
 
324
+
 
325
+        if ((host->cmd & 1) || state != ongoing) { /* DE */
 
326
+            host->pstate &= ~0x0c00;               /* BRE | BWE */
 
327
+            host->stat &= ~(STAT_BRR | STAT_BWR);
 
328
+            if (state != ongoing) {
 
329
+                TRACE2("transfer %s", 
 
330
+                       state == ready
 
331
+                       ? "complete"
 
332
+                       : "aborted --> complete");
 
333
+                host->stat |= STAT_TC;
 
334
+                if (host->cmd & 0x04) {            /* ACEN */
 
335
+                    host->stop = 0x0cc30000;
 
336
+                    state = aborted;
 
337
+                }
 
338
+                if (state == aborted) {
 
339
+                    host->cmd = host->stop;
 
340
+                    host->stop = 0;
 
341
+                    omap3_mmc_command(host);
 
342
+                }
 
343
+            }
 
344
+        }
 
345
+    }
 
346
+}
 
347
+
 
348
+static void omap3_mmc_transfer(OMAP3MMCState *host)
 
349
+{
 
350
+    int i;
 
351
+    uint32_t x;
 
352
+#if MMC_DEBUG_LEVEL>1
 
353
+    int j;
 
354
+    uint8_t c, sym[17];
 
355
+#endif
 
356
+
 
357
+    /* IF data transfer is inactive
 
358
+       OR block count enabled with zero block count
 
359
+       OR in receive mode and we have unread data in FIFO
 
360
+       OR in transmit mode and we have no data in FIFO,
 
361
+       THEN don't do anything */
 
362
+    if (!host->transfer
 
363
+        || ((host->cmd & 2) && !host->nblk_counter)
 
364
+        || (host->ddir && host->fifo_len)
 
365
+        || (!host->ddir && !host->fifo_len))
 
366
+        return;
 
367
+    
 
368
+    if (host->ddir) {
 
369
+        TRACE2("begin, %d blocks (%d bytes/block) left to receive, %d bytes in FIFO",
 
370
+               (host->cmd & 2) ? host->nblk_counter : 1,
 
371
+               host->blk & 0x7ff, 
 
372
+               host->fifo_len * 4);
 
373
+        while (host->blen_counter && host->fifo_len < 255) {
 
374
+            for (i = 0, x = 0; i < 32 && host->blen_counter; i += 8, host->blen_counter--)
 
375
+                x |= sd_read_data(host->card) << i;
 
376
+            host->fifo[(host->fifo_start + host->fifo_len) & 0xff] = x;
 
377
+            host->fifo_len++;
 
378
+        }
 
379
+        TRACE2("end, %d bytes in FIFO:", host->fifo_len * 4);
 
380
+#if MMC_DEBUG_LEVEL>1
 
381
+        for (i = 0; i < host->fifo_len; ) {
 
382
+            fprintf(stderr, "%s: [0x%03x] ", __FUNCTION__, i * 4);
 
383
+            do {
 
384
+                x = host->fifo[(host->fifo_start + i) & 0xff];
 
385
+                for (j = 0; j < 4; j++) {
 
386
+                    c = (x >> (j * 8)) & 0xff;
 
387
+                    fprintf(stderr, "%02x ", c);
 
388
+                    sym[(i & 3) * 4 + j] = (c < 32 || c > 126) ? '.' : c;
 
389
+                }
 
390
+            } while (((++i) & 3));
 
391
+            sym[16] = 0;
 
392
+            fprintf(stderr, "%s\n", sym);
 
393
+        }
 
394
+#endif
 
395
+    } else {
 
396
+        TRACE2("%d bytes left to transmit in current block", host->blen_counter);
 
397
+        while (host->blen_counter && host->fifo_len) {
 
398
+            for (i = 0; i < 32 && host->blen_counter; i += 8, host->blen_counter--)
 
399
+                sd_write_data(host->card, (host->fifo[host->fifo_start] >> i) & 0xff);
 
400
+            host->fifo_start++;
 
401
+            host->fifo_len--;
 
402
+            host->fifo_start &= 0xff;
 
403
+        }
 
404
+    }
 
405
+
 
406
+    if (!host->blen_counter) {
 
407
+        if (host->cmd & 2) /* BCE */
 
408
+            host->nblk_counter--;
 
409
+        TRACE2("block done, %d blocks left",
 
410
+               (host->cmd & (1 << 5)) ? host->nblk_counter : 0);
 
411
+        host->blen_counter = host->blk & 0x7ff;
 
412
+        if (!(host->cmd & (1 << 5)) /* MSBS */
 
413
+            || !host->nblk_counter) {
 
414
+            host->nblk_counter = (host->blk >> 16) & 0xffff;
 
415
+            host->transfer = 0;
 
416
+            host->pstate &= ~0x0306; /* RTA | WTA | DLA | DATI */
 
417
+        }
 
418
+    }
 
419
+}
 
420
+
 
421
+static void omap3_mmc_command(OMAP3MMCState *s)
 
422
+{
 
423
+    uint32_t rspstatus, mask;
 
424
+    int rsplen, timeout;
 
425
+    SDRequest request;
 
426
+    uint8_t response[16];
 
427
+    int cmd = (s->cmd >> 24) & 0x3f; /* INDX */
 
428
+    int rsptype = (s->cmd >> 16) & 3;
 
429
+    int dp = s->cmd & (1 << 21);
 
430
+    
 
431
+    TRACE1("%d type=%d rsp=%d arg=0x%08x blk=0x%08x, fifo=%d/%d",
 
432
+           cmd, (s->cmd >> 22) & 3, (s->cmd >> 16) & 3, s->arg,
 
433
+           s->blk, s->fifo_start, s->fifo_len);
 
434
+
 
435
+    if ((s->con & 2) && !cmd) { /* INIT and CMD0 */
 
436
+        s->stat   |= STAT_CC;
 
437
+        s->pstate &= 0xfffffffe;
 
438
+        return;
 
439
+    }
 
440
+    
 
441
+    if (dp) {
 
442
+        s->fifo_start = 0;
 
443
+        s->fifo_len = 0;
 
444
+        s->transfer = 1;
 
445
+        s->ddir = (s->cmd >> 4) & 1;
 
446
+        /* DLA | DATI | (RTA/WTA) */
 
447
+        s->pstate |= 0x6 | (s->ddir ? 0x200 : 0x100);
 
448
+    } else {
 
449
+        s->transfer = 0;
 
450
+        s->pstate &= ~0x306; /* RTA | WTA | DLA | DATI */
 
451
+    }
 
452
+    
 
453
+    timeout = 0;
 
454
+    mask = 0;
 
455
+    rspstatus = 0;
 
456
+    
 
457
+    request.cmd = cmd;
 
458
+    request.arg = s->arg;
 
459
+    request.crc = 0; /* FIXME */
 
460
+    
 
461
+    rsplen = sd_do_command(s->card, &request, response);
 
462
+    
 
463
+    switch (rsptype) {
 
464
+        case sd_nore:
 
465
+            rsplen = 0;
 
466
+            break;
 
467
+        case sd_136_bits:
 
468
+            if (rsplen < 16) {
 
469
+                timeout = 1;
 
470
+                break;
 
471
+            }
 
472
+            rsplen = 16;
 
473
+            s->rsp76 = (response[0] << 24) | (response[1] << 16) |
 
474
+                       (response[2] << 8) | (response[3] << 0);
 
475
+            s->rsp54 = (response[4] << 24) | (response[5] << 16) |
 
476
+                       (response[6] << 8) | (response[7] << 0);
 
477
+            s->rsp32 = (response[8] << 24) | (response[9] << 16) |
 
478
+                       (response[10] << 8) | (response[11] << 0);
 
479
+            s->rsp10 = (response[12] << 24) | (response[13] << 16) |
 
480
+                       (response[14] << 8) | (response[15] << 0);
 
481
+            break;
 
482
+        case sd_48_bits:
 
483
+        case sd_48b_bits:
 
484
+            if (rsplen < 4) {
 
485
+                timeout = 1;
 
486
+                break;
 
487
+            }
 
488
+            rsplen = 4;
 
489
+            s->rsp10 = (response[0] << 24) | (response[1] << 16) |
 
490
+                       (response[2] << 8) | (response[3] << 0);
 
491
+            switch (cmd) {
 
492
+                case 41: /* r3 */
 
493
+                    break;
 
494
+                case 3:  /* r6 */
 
495
+                    mask = 0xe00;
 
496
+                    rspstatus = (response[2] << 8) | response[3];
 
497
+                    break;
 
498
+                default:
 
499
+                    if (cmd == 8 && !sd_is_mmc(s->card)) {
 
500
+                        /* r7 */
 
501
+                        break;
 
502
+                    }
 
503
+                    mask = OUT_OF_RANGE | ADDRESS_ERROR | BLOCK_LEN_ERROR |
 
504
+                        ERASE_SEQ_ERROR | ERASE_PARAM | WP_VIOLATION |
 
505
+                        LOCK_UNLOCK_FAILED | COM_CRC_ERROR | ILLEGAL_COMMAND |
 
506
+                        CARD_ECC_FAILED | CC_ERROR | SD_ERROR |
 
507
+                        CID_CSD_OVERWRITE | WP_ERASE_SKIP;
 
508
+                    rspstatus = (response[0] << 24) | (response[1] << 16) |
 
509
+                                (response[2] << 8) | (response[3] << 0);
 
510
+                    break;
 
511
+            }
 
512
+        default:
 
513
+            break;
 
514
+    }
 
515
+    
 
516
+    if (cmd == 12 || cmd == 52) { /* stop transfer commands */
 
517
+        /*s->fifo_start = 0;*/
 
518
+        /*s->fifo_len = 0;*/
 
519
+        s->transfer = 0;
 
520
+        s->pstate &= ~0x0f06;     /* BRE | BWE | RTA | WTA | DLA | DATI */
 
521
+        s->stat &= ~(STAT_BRR | STAT_BWR);
 
522
+        s->stat |= STAT_TC;
 
523
+        qemu_irq_lower(s->dma[0]);
 
524
+        qemu_irq_lower(s->dma[1]);
 
525
+    }
 
526
+    
 
527
+    if (rspstatus & mask & s->csre) {
 
528
+        s->stat |= STAT_CERR;
 
529
+        s->pstate &= ~0x306; /* RTA | WTA | DLA | DATI */
 
530
+        s->transfer = 0;
 
531
+    } else {
 
532
+        s->stat &= ~STAT_CERR;
 
533
+        /* If this is an R1b command with no data transfer and
 
534
+         * there wasn't an error, then we have effectively
 
535
+         * emulated the command as having a zero length "busy"
 
536
+         * response. Set TC to tell the driver the "busy" period
 
537
+         * is over.
 
538
+         */
 
539
+        if (!timeout && !dp && rsptype == sd_48b_bits) {
 
540
+            s->stat |= STAT_TC;
 
541
+        }
 
542
+    }
 
543
+    s->stat |= timeout ? STAT_CTO : STAT_CC;
 
544
+}
 
545
+
 
546
+static uint32_t omap3_mmc_read(void *opaque, hwaddr addr)
 
547
+{
 
548
+    OMAP3MMCState *s = (OMAP3MMCState *) opaque;
 
549
+    uint32_t i ;
 
550
+
 
551
+    switch (addr) {
 
552
+        case 0x10:
 
553
+            TRACE2("SYSCONFIG = %08x", s->sysconfig);
 
554
+            return s->sysconfig;
 
555
+        case 0x14:
 
556
+            TRACE2("SYSSTATUS = %08x", s->sysstatus | 0x1);
 
557
+            return s->sysstatus | 0x1; /*reset completed */
 
558
+        case 0x24:
 
559
+            TRACE2("CSRE = %08x", s->csre);
 
560
+            return s->csre;
 
561
+        case 0x28:
 
562
+            TRACE2("SYSTEST = %08x", s->systest);
 
563
+            return s->systest;
 
564
+        case 0x2c: /* MMCHS_CON */
 
565
+            TRACE2("CON = %08x", s->con);
 
566
+            return s->con;
 
567
+        case 0x30:
 
568
+            TRACE2("PWCNT = %08x", s->pwcnt);
 
569
+            return s->pwcnt;
 
570
+        case 0x104: /* MMCHS_BLK */
 
571
+            TRACE2("BLK = %08x", s->blk);
 
572
+            return s->blk;
 
573
+        case 0x108: /* MMCHS_ARG */
 
574
+            TRACE2("ARG = %08x", s->arg);
 
575
+            return s->arg;
 
576
+        case 0x10c:
 
577
+            TRACE2("CMD = %08x", s->cmd);
 
578
+            return s->cmd;
 
579
+        case 0x110:
 
580
+            TRACE2("RSP10 = %08x", s->rsp10);
 
581
+            return s->rsp10;
 
582
+        case 0x114:
 
583
+            TRACE2("RSP32 = %08x", s->rsp32);
 
584
+            return s->rsp32;
 
585
+        case 0x118:
 
586
+            TRACE2("RSP54 = %08x", s->rsp54);
 
587
+            return s->rsp54;
 
588
+        case 0x11c:
 
589
+            TRACE2("RSP76 = %08x", s->rsp76);
 
590
+            return s->rsp76;
 
591
+        case 0x120:
 
592
+            /* in PIO mode, access allowed only when BRE is set */
 
593
+            if (!(s->cmd & 1) && !(s->pstate & 0x0800)) {
 
594
+                s->stat |= STAT_BADA;
 
595
+                i = 0;
 
596
+            } else {
 
597
+                i = s->fifo[s->fifo_start];
 
598
+                s->fifo[s->fifo_start] = 0;
 
599
+                if (s->fifo_len == 0) {
 
600
+                    TRACE("FIFO underrun");
 
601
+                    return i;
 
602
+                }
 
603
+                s->fifo_start++;
 
604
+                s->fifo_len--;
 
605
+                s->fifo_start &= 255;
 
606
+                omap3_mmc_transfer(s);
 
607
+                omap3_mmc_fifolevel_update(s);
 
608
+            }
 
609
+            omap3_mmc_interrupts_update(s);
 
610
+            return i;
 
611
+        case 0x124: /* MMCHS_PSTATE */
 
612
+            TRACE2("PSTATE = %08x", s->pstate);
 
613
+            return s->pstate;
 
614
+        case 0x128:
 
615
+            TRACE2("HCTL = %08x", s->hctl);
 
616
+            return s->hctl;
 
617
+        case 0x12c: /* MMCHS_SYSCTL */
 
618
+            TRACE2("SYSCTL = %08x", s->sysctl);
 
619
+            return s->sysctl;
 
620
+        case 0x130: /* MMCHS_STAT */
 
621
+            if (s->stat & 0xffff0000)
 
622
+                s->stat |= STAT_ERRI;
 
623
+            else
 
624
+                s->stat &= ~STAT_ERRI;
 
625
+            TRACE2("STAT = %08x", s->stat);
 
626
+            return s->stat;
 
627
+        case 0x134:
 
628
+            TRACE2("IE = %08x", s->ie);
 
629
+            return s->ie;
 
630
+        case 0x138:
 
631
+            TRACE2("ISE = %08x", s->ise);
 
632
+            return s->ise;
 
633
+        case 0x13c:
 
634
+            TRACE2("AC12 = %08x", s->ac12);
 
635
+            return s->ac12;
 
636
+        case 0x140: /* MMCHS_CAPA */
 
637
+            TRACE2("CAPA = %08x", s->capa);
 
638
+            return s->capa;
 
639
+        case 0x148:
 
640
+            TRACE2("CUR_CAPA = %08x", s->cur_capa);
 
641
+            return s->cur_capa;
 
642
+        case 0x1fc:
 
643
+            TRACE2("REV = %08x", s->rev);
 
644
+            return s->rev;
 
645
+        default:
 
646
+            OMAP_BAD_REG(addr);
 
647
+            exit(-1);
 
648
+            return 0;
 
649
+    }
 
650
+}
 
651
+
 
652
+static void omap3_mmc_write(void *opaque, hwaddr addr,
 
653
+                            uint32_t value)
 
654
+{
 
655
+    OMAP3MMCState *s = (OMAP3MMCState *) opaque;
 
656
+    
 
657
+    switch (addr) {
 
658
+        case 0x014:
 
659
+        case 0x110:
 
660
+        case 0x114:
 
661
+        case 0x118:
 
662
+        case 0x11c:
 
663
+        case 0x124:
 
664
+        case 0x13c:
 
665
+        case 0x1fc:
 
666
+            OMAP_RO_REG(addr);
 
667
+            break;
 
668
+        case 0x010:
 
669
+            TRACE2("SYSCONFIG = %08x", value);
 
670
+            if (value & 2)
 
671
+                omap3_mmc_reset(DEVICE(s));
 
672
+            s->sysconfig = value & 0x31d;
 
673
+            break;
 
674
+        case 0x024:
 
675
+            TRACE2("CSRE = %08x", value);
 
676
+            s->csre = value;
 
677
+            break;
 
678
+        case 0x028:
 
679
+            TRACE2("SYSTEST = %08x", value);
 
680
+            s->systest = value;
 
681
+            break;
 
682
+        case 0x02c: /* MMCHS_CON */
 
683
+            TRACE2("CON = %08x", value);
 
684
+            if (value & 0x10) {   /* MODE */
 
685
+                TRACE("SYSTEST mode is not supported");
 
686
+            }
 
687
+            if ((value & 0x20) && !sd_is_mmc(s->card)) { /* DW8 */
 
688
+                TRACE("8-bit data width is not supported for SD cards");
 
689
+            }
 
690
+            if (value & 0x1000) { /* CEATA */
 
691
+                TRACE("CE-ATA control mode not supported");
 
692
+            }
 
693
+            s->con = value & 0x1ffff;
 
694
+            break;
 
695
+        case 0x030:
 
696
+            TRACE2("PWCNT = %08x", value);
 
697
+            s->pwcnt = value;
 
698
+            break;
 
699
+        case 0x104: /* MMCHS_BLK */
 
700
+            TRACE2("BLK = %08x", value);
 
701
+            s->blk = value & 0xffff07ff;
 
702
+            s->blen_counter = value & 0x7ff;
 
703
+            s->nblk_counter = (value >> 16) & 0xffff;
 
704
+            break;
 
705
+        case 0x108: /* MMCHS_ARG */
 
706
+            TRACE2("ARG = %08x", value);
 
707
+            s->arg = value;
 
708
+            break;
 
709
+        case 0x10c: /* MMCHS_CMD */
 
710
+            TRACE2("CMD = %08x", value);
 
711
+            if (!s->card) {
 
712
+                s->stat |= STAT_CTO;
 
713
+            } else {
 
714
+                /* TODO: writing to bits 0-15 should have no effect during
 
715
+                   an active data transfer */
 
716
+                if (s->transfer && !s->stop
 
717
+                    && (((value >> 24) & 0x3f) == 12
 
718
+                        || ((value >> 24) & 0x3f) == 52)) {
 
719
+                    s->stop = value & 0x3ffb0037;
 
720
+                } else {
 
721
+                    s->cmd = value & 0x3ffb0037;
 
722
+                    omap3_mmc_command(s);
 
723
+                }
 
724
+                omap3_mmc_transfer(s);
 
725
+                omap3_mmc_fifolevel_update(s);
 
726
+            }
 
727
+            omap3_mmc_interrupts_update(s);
 
728
+            break;
 
729
+        case 0x120:
 
730
+            /* in PIO mode, access allowed only when BWE is set */
 
731
+            if (!(s->cmd & 1) && !(s->pstate & 0x0400)) {
 
732
+                s->stat |= STAT_BADA;
 
733
+            } else {
 
734
+                if (s->fifo_len == 256) {
 
735
+                    TRACE("FIFO overrun");
 
736
+                    break;
 
737
+                }
 
738
+                s->fifo[(s->fifo_start + s->fifo_len) & 255] = value;
 
739
+                s->fifo_len++;
 
740
+                omap3_mmc_transfer(s);
 
741
+                omap3_mmc_fifolevel_update(s);
 
742
+            }
 
743
+            omap3_mmc_interrupts_update(s);
 
744
+            break;
 
745
+        case 0x128: /* MMCHS_HCTL */
 
746
+            TRACE2("HCTL = %08x", value);
 
747
+            s->hctl = value & 0xf0f0f02;
 
748
+            if (s->hctl & (1 << 16)) { /* SBGR */
 
749
+                TRACE("Stop at block gap feature not implemented!");
 
750
+            }
 
751
+            break;
 
752
+        case 0x12c: /* MMCHS_SYSCTL */
 
753
+            TRACE2("SYSCTL = %08x", value);
 
754
+            if (value & 0x04000000) { /* SRD */
 
755
+                s->data    = 0;
 
756
+                s->pstate &= ~0x00000f06; /* BRE, BWE, RTA, WTA, DLA, DATI */
 
757
+                s->hctl   &= ~0x00030000; /* SGBR, CR */
 
758
+                s->stat   &= ~(STAT_BRR|STAT_BWR|STAT_BGE);
 
759
+                s->fifo_start = 0;
 
760
+                s->fifo_len = 0;
 
761
+            }
 
762
+            if (value & 0x02000000) { /* SRC */
 
763
+                s->pstate &= ~0x00000001; /* CMDI */
 
764
+            }
 
765
+            if (value & 0x01000000) { /* SRA */
 
766
+                uint32_t capa = s->capa;
 
767
+                uint32_t cur_capa = s->cur_capa;
 
768
+                omap3_mmc_reset(DEVICE(s));
 
769
+                s->capa = capa;
 
770
+                s->cur_capa = cur_capa;
 
771
+            }
 
772
+            value = (value & ~2) | ((value & 1) << 1); /* copy ICE directly to ICS */
 
773
+            s->sysctl = value & 0x000fffc7;
 
774
+            break;
 
775
+        case 0x130:
 
776
+            TRACE2("STAT = %08x", value);
 
777
+            /* STAT_CIRQ and STAT_ERRI are write-ignored */
 
778
+            value = value & (STAT_MASK & ~(STAT_CIRQ|STAT_ERRI));
 
779
+            s->stat &= ~value;
 
780
+            omap3_mmc_interrupts_update(s);
 
781
+            break;
 
782
+        case 0x134: /* MMCHS_IE */
 
783
+            TRACE2("IE = %08x", value);
 
784
+            if (!(s->con & 0x4000)) {
 
785
+                /* if CON:OBIE is clear, ignore write to OBI_ENABLE */
 
786
+                value = (value & ~STAT_OBI) | (s->ie & STAT_OBI);
 
787
+            }
 
788
+            s->ie = value & (STAT_MASK & ~STAT_ERRI);
 
789
+            if (!(s->ie & STAT_CIRQ)) {
 
790
+                s->stat &= ~STAT_CIRQ;
 
791
+            }
 
792
+            omap3_mmc_interrupts_update(s);
 
793
+            break;
 
794
+        case 0x138:
 
795
+            TRACE2("ISE = %08x", value);
 
796
+            s->ise = value & (STAT_MASK & ~STAT_ERRI);
 
797
+            omap3_mmc_interrupts_update(s);
 
798
+            break;
 
799
+        case 0x140: /* MMCHS_CAPA */
 
800
+            TRACE2("CAPA = %08x", value);
 
801
+            s->capa &= ~0x07000000;
 
802
+            s->capa |= value & 0x07000000;
 
803
+            break;
 
804
+        case 0x148:
 
805
+            TRACE2("CUR_CAPA = %08x", value);
 
806
+            s->cur_capa = value & 0xffffff;
 
807
+            break;
 
808
+        default:
 
809
+            OMAP_BAD_REG(addr);
 
810
+            exit(-1);
 
811
+    }
 
812
+}
 
813
+
 
814
+static const MemoryRegionOps omap3_mmc_ops = {
 
815
+    .old_mmio = {
 
816
+        .read = {
 
817
+            omap_badwidth_read32,
 
818
+            omap_badwidth_read32,
 
819
+            omap3_mmc_read,
 
820
+        },
 
821
+        .write = {
 
822
+            omap_badwidth_write32,
 
823
+            omap_badwidth_write32,
 
824
+            omap3_mmc_write,
 
825
+        },
 
826
+    },
 
827
+    .endianness = DEVICE_NATIVE_ENDIAN,
 
828
+};
 
829
+
 
830
+static int omap3_mmc_init(SysBusDevice *dev)
 
831
+{
 
832
+    OMAP3MMCState *s = OMAP3_MMC(dev);
 
833
+
 
834
+    sysbus_init_irq(dev, &s->irq);
 
835
+    sysbus_init_irq(dev, &s->dma[0]);
 
836
+    sysbus_init_irq(dev, &s->dma[1]);
 
837
+    memory_region_init_io(&s->iomem, OBJECT(s), &omap3_mmc_ops, s,
 
838
+                          "omap3_mmc", 0x1000);
 
839
+    sysbus_init_mmio(dev, &s->iomem);
 
840
+    return 0;
 
841
+}
 
842
+
 
843
+static void omap3_mmc_class_init(ObjectClass *klass, void *data)
 
844
+{
 
845
+    DeviceClass *dc = DEVICE_CLASS(klass);
 
846
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
 
847
+    k->init = omap3_mmc_init;
 
848
+    dc->reset = omap3_mmc_reset;
 
849
+}
 
850
+
 
851
+static TypeInfo omap3_mmc_info = {
 
852
+    .name = TYPE_OMAP3_MMC,
 
853
+    .parent = TYPE_SYS_BUS_DEVICE,
 
854
+    .instance_size = sizeof(OMAP3MMCState),
 
855
+    .class_init = omap3_mmc_class_init,
 
856
+};
 
857
+
 
858
+static void omap3_mmc_register_types(void)
 
859
+{
 
860
+    type_register_static(&omap3_mmc_info);
 
861
+}
 
862
+
 
863
+void omap3_mmc_attach(DeviceState *dev, BlockDriverState *bs,
 
864
+                      int is_spi, int is_mmc)
 
865
+{
 
866
+    OMAP3MMCState *s = OMAP3_MMC(dev);
 
867
+
 
868
+    if (s->card) {
 
869
+        hw_error("%s: card already attached!", __FUNCTION__);
 
870
+    }
 
871
+    s->card = sd_init(bs, is_spi, is_mmc);
 
872
+}
 
873
+
 
874
+type_init(omap3_mmc_register_types)
 
875
diff --git a/include/hw/arm/omap.h b/include/hw/arm/omap.h
 
876
index 30046d1..a357510 100644
 
877
--- a/include/hw/arm/omap.h
 
878
+++ b/include/hw/arm/omap.h
 
879
@@ -960,6 +960,10 @@ void omap_mmc_reset(struct omap_mmc_s *s);
 
880
 void omap_mmc_handlers(struct omap_mmc_s *s, qemu_irq ro, qemu_irq cover);
 
881
 void omap_mmc_enable(struct omap_mmc_s *s, int enable);
 
882
 
 
883
+/* omap3_mmc.c */
 
884
+void omap3_mmc_attach(DeviceState *dev, BlockDriverState *bs,
 
885
+                      int is_spi, int is_mmc);
 
886
+
 
887
 /* omap_i2c.c */
 
888
 i2c_bus *omap_i2c_bus(DeviceState *omap_i2c);
 
889
 
 
890
-- 
 
891
1.8.5.2
 
892