xref: /qemu/hw/sd/omap_mmc.c (revision 4cadcb6b5f90bf0e9d0d23ad7552a0857dc593e7)
1 /*
2  * OMAP on-chip MMC/SD host emulation.
3  *
4  * Datasheet: TI Multimedia Card (MMC/SD/SDIO) Interface (SPRU765A)
5  *
6  * Copyright (C) 2006-2007 Andrzej Zaborowski  <balrog@zabor.org>
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) version 3 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 "qemu/log.h"
24 #include "qapi/error.h"
25 #include "hw/irq.h"
26 #include "hw/sysbus.h"
27 #include "hw/arm/omap.h"
28 #include "hw/sd/sdcard_legacy.h"
29 
30 typedef struct omap_mmc_s {
31     SysBusDevice parent_obj;
32 
33     qemu_irq irq;
34     qemu_irq *dma;
35     qemu_irq coverswitch;
36     MemoryRegion iomem;
37     omap_clk clk;
38     SDState *card;
39     uint16_t last_cmd;
40     uint16_t sdio;
41     uint16_t rsp[8];
42     uint32_t arg;
43     int lines;
44     int dw;
45     int mode;
46     int enable;
47     int be;
48     int rev;
49     uint16_t status;
50     uint16_t mask;
51     uint8_t cto;
52     uint16_t dto;
53     int clkdiv;
54     uint16_t fifo[32];
55     int fifo_start;
56     int fifo_len;
57     uint16_t blen;
58     uint16_t blen_counter;
59     uint16_t nblk;
60     uint16_t nblk_counter;
61     int tx_dma;
62     int rx_dma;
63     int af_level;
64     int ae_level;
65 
66     int ddir;
67     int transfer;
68 
69     int cdet_wakeup;
70     int cdet_enable;
71     int cdet_state;
72     qemu_irq cdet;
73 } OMAPMMCState;
74 
75 static void omap_mmc_interrupts_update(struct omap_mmc_s *s)
76 {
77     qemu_set_irq(s->irq, !!(s->status & s->mask));
78 }
79 
80 static void omap_mmc_fifolevel_update(struct omap_mmc_s *host)
81 {
82     if (!host->transfer && !host->fifo_len) {
83         host->status &= 0xf3ff;
84         return;
85     }
86 
87     if (host->fifo_len > host->af_level && host->ddir) {
88         if (host->rx_dma) {
89             host->status &= 0xfbff;
90             qemu_irq_raise(host->dma[1]);
91         } else
92             host->status |= 0x0400;
93     } else {
94         host->status &= 0xfbff;
95         qemu_irq_lower(host->dma[1]);
96     }
97 
98     if (host->fifo_len < host->ae_level && !host->ddir) {
99         if (host->tx_dma) {
100             host->status &= 0xf7ff;
101             qemu_irq_raise(host->dma[0]);
102         } else
103             host->status |= 0x0800;
104     } else {
105         qemu_irq_lower(host->dma[0]);
106         host->status &= 0xf7ff;
107     }
108 }
109 
110 /* These must match the encoding of the MMC_CMD Response field */
111 typedef enum {
112     sd_nore = 0,	/* no response */
113     sd_r1,		/* normal response command */
114     sd_r2,		/* CID, CSD registers */
115     sd_r3,		/* OCR register */
116     sd_r6 = 6,		/* Published RCA response */
117     sd_r1b = -1,
118 } sd_rsp_type_t;
119 
120 /* These must match the encoding of the MMC_CMD Type field */
121 typedef enum {
122     SD_TYPE_BC = 0,     /* broadcast -- no response */
123     SD_TYPE_BCR = 1,    /* broadcast with response */
124     SD_TYPE_AC = 2,     /* addressed -- no data transfer */
125     SD_TYPE_ADTC = 3,   /* addressed with data transfer */
126 } MMCCmdType;
127 
128 static void omap_mmc_command(struct omap_mmc_s *host, int cmd, int dir,
129                              MMCCmdType type, int busy,
130                              sd_rsp_type_t resptype, int init)
131 {
132     uint32_t rspstatus, mask;
133     int rsplen, timeout;
134     SDRequest request;
135     uint8_t response[16];
136 
137     if (init && cmd == 0) {
138         host->status |= 0x0001;
139         return;
140     }
141 
142     if (resptype == sd_r1 && busy)
143         resptype = sd_r1b;
144 
145     if (type == SD_TYPE_ADTC) {
146         host->fifo_start = 0;
147         host->fifo_len = 0;
148         host->transfer = 1;
149         host->ddir = dir;
150     } else
151         host->transfer = 0;
152     timeout = 0;
153     mask = 0;
154     rspstatus = 0;
155 
156     request.cmd = cmd;
157     request.arg = host->arg;
158     request.crc = 0; /* FIXME */
159 
160     rsplen = sd_do_command(host->card, &request, response);
161 
162     /* TODO: validate CRCs */
163     switch (resptype) {
164     case sd_nore:
165         rsplen = 0;
166         break;
167 
168     case sd_r1:
169     case sd_r1b:
170         if (rsplen < 4) {
171             timeout = 1;
172             break;
173         }
174         rsplen = 4;
175 
176         mask = OUT_OF_RANGE | ADDRESS_ERROR | BLOCK_LEN_ERROR |
177                 ERASE_SEQ_ERROR | ERASE_PARAM | WP_VIOLATION |
178                 LOCK_UNLOCK_FAILED | COM_CRC_ERROR | ILLEGAL_COMMAND |
179                 CARD_ECC_FAILED | CC_ERROR | SD_ERROR |
180                 CID_CSD_OVERWRITE;
181         if (host->sdio & (1 << 13))
182             mask |= AKE_SEQ_ERROR;
183         rspstatus = ldl_be_p(response);
184         break;
185 
186     case sd_r2:
187         if (rsplen < 16) {
188             timeout = 1;
189             break;
190         }
191         rsplen = 16;
192         break;
193 
194     case sd_r3:
195         if (rsplen < 4) {
196             timeout = 1;
197             break;
198         }
199         rsplen = 4;
200 
201         rspstatus = ldl_be_p(response);
202         if (rspstatus & 0x80000000)
203             host->status &= 0xe000;
204         else
205             host->status |= 0x1000;
206         break;
207 
208     case sd_r6:
209         if (rsplen < 4) {
210             timeout = 1;
211             break;
212         }
213         rsplen = 4;
214 
215         mask = 0xe000 | AKE_SEQ_ERROR;
216         rspstatus = (response[2] << 8) | (response[3] << 0);
217     }
218 
219     if (rspstatus & mask)
220         host->status |= 0x4000;
221     else
222         host->status &= 0xb000;
223 
224     if (rsplen)
225         for (rsplen = 0; rsplen < 8; rsplen ++)
226             host->rsp[~rsplen & 7] = response[(rsplen << 1) | 1] |
227                     (response[(rsplen << 1) | 0] << 8);
228 
229     if (timeout)
230         host->status |= 0x0080;
231     else if (cmd == 12)
232         host->status |= 0x0005;	/* Makes it more real */
233     else
234         host->status |= 0x0001;
235 }
236 
237 static void omap_mmc_transfer(struct omap_mmc_s *host)
238 {
239     uint8_t value;
240 
241     if (!host->transfer)
242         return;
243 
244     while (1) {
245         if (host->ddir) {
246             if (host->fifo_len > host->af_level)
247                 break;
248 
249             value = sd_read_byte(host->card);
250             host->fifo[(host->fifo_start + host->fifo_len) & 31] = value;
251             if (-- host->blen_counter) {
252                 value = sd_read_byte(host->card);
253                 host->fifo[(host->fifo_start + host->fifo_len) & 31] |=
254                         value << 8;
255                 host->blen_counter --;
256             }
257 
258             host->fifo_len ++;
259         } else {
260             if (!host->fifo_len)
261                 break;
262 
263             value = host->fifo[host->fifo_start] & 0xff;
264             sd_write_byte(host->card, value);
265             if (-- host->blen_counter) {
266                 value = host->fifo[host->fifo_start] >> 8;
267                 sd_write_byte(host->card, value);
268                 host->blen_counter --;
269             }
270 
271             host->fifo_start ++;
272             host->fifo_len --;
273             host->fifo_start &= 31;
274         }
275 
276         if (host->blen_counter == 0) {
277             host->nblk_counter --;
278             host->blen_counter = host->blen;
279 
280             if (host->nblk_counter == 0) {
281                 host->nblk_counter = host->nblk;
282                 host->transfer = 0;
283                 host->status |= 0x0008;
284                 break;
285             }
286         }
287     }
288 }
289 
290 static void omap_mmc_update(void *opaque)
291 {
292     struct omap_mmc_s *s = opaque;
293     omap_mmc_transfer(s);
294     omap_mmc_fifolevel_update(s);
295     omap_mmc_interrupts_update(s);
296 }
297 
298 static void omap_mmc_pseudo_reset(struct omap_mmc_s *host)
299 {
300     host->status = 0;
301     host->fifo_len = 0;
302 }
303 
304 static void omap_mmc_reset(struct omap_mmc_s *host)
305 {
306     host->last_cmd = 0;
307     memset(host->rsp, 0, sizeof(host->rsp));
308     host->arg = 0;
309     host->dw = 0;
310     host->mode = 0;
311     host->enable = 0;
312     host->mask = 0;
313     host->cto = 0;
314     host->dto = 0;
315     host->blen = 0;
316     host->blen_counter = 0;
317     host->nblk = 0;
318     host->nblk_counter = 0;
319     host->tx_dma = 0;
320     host->rx_dma = 0;
321     host->ae_level = 0x00;
322     host->af_level = 0x1f;
323     host->transfer = 0;
324     host->cdet_wakeup = 0;
325     host->cdet_enable = 0;
326     qemu_set_irq(host->coverswitch, host->cdet_state);
327     host->clkdiv = 0;
328 
329     omap_mmc_pseudo_reset(host);
330 
331     /* Since we're still using the legacy SD API the card is not plugged
332      * into any bus, and we must reset it manually. When omap_mmc is
333      * QOMified this must move into the QOM reset function.
334      */
335     if (host->card) {
336         device_cold_reset(DEVICE(host->card));
337     }
338 }
339 
340 static uint64_t omap_mmc_read(void *opaque, hwaddr offset, unsigned size)
341 {
342     uint16_t i;
343     struct omap_mmc_s *s = opaque;
344 
345     if (size != 2) {
346         return omap_badwidth_read16(opaque, offset);
347     }
348 
349     switch (offset) {
350     case 0x00:	/* MMC_CMD */
351         return s->last_cmd;
352 
353     case 0x04:	/* MMC_ARGL */
354         return s->arg & 0x0000ffff;
355 
356     case 0x08:	/* MMC_ARGH */
357         return s->arg >> 16;
358 
359     case 0x0c:	/* MMC_CON */
360         return (s->dw << 15) | (s->mode << 12) | (s->enable << 11) |
361                 (s->be << 10) | s->clkdiv;
362 
363     case 0x10:	/* MMC_STAT */
364         return s->status;
365 
366     case 0x14:	/* MMC_IE */
367         return s->mask;
368 
369     case 0x18:	/* MMC_CTO */
370         return s->cto;
371 
372     case 0x1c:	/* MMC_DTO */
373         return s->dto;
374 
375     case 0x20:	/* MMC_DATA */
376         /* TODO: support 8-bit access */
377         i = s->fifo[s->fifo_start];
378         if (s->fifo_len == 0) {
379             printf("MMC: FIFO underrun\n");
380             return i;
381         }
382         s->fifo_start ++;
383         s->fifo_len --;
384         s->fifo_start &= 31;
385         omap_mmc_transfer(s);
386         omap_mmc_fifolevel_update(s);
387         omap_mmc_interrupts_update(s);
388         return i;
389 
390     case 0x24:	/* MMC_BLEN */
391         return s->blen_counter;
392 
393     case 0x28:	/* MMC_NBLK */
394         return s->nblk_counter;
395 
396     case 0x2c:	/* MMC_BUF */
397         return (s->rx_dma << 15) | (s->af_level << 8) |
398             (s->tx_dma << 7) | s->ae_level;
399 
400     case 0x30:	/* MMC_SPI */
401         return 0x0000;
402     case 0x34:	/* MMC_SDIO */
403         return (s->cdet_wakeup << 2) | (s->cdet_enable) | s->sdio;
404     case 0x38:	/* MMC_SYST */
405         return 0x0000;
406 
407     case 0x3c:	/* MMC_REV */
408         return s->rev;
409 
410     case 0x40:	/* MMC_RSP0 */
411     case 0x44:	/* MMC_RSP1 */
412     case 0x48:	/* MMC_RSP2 */
413     case 0x4c:	/* MMC_RSP3 */
414     case 0x50:	/* MMC_RSP4 */
415     case 0x54:	/* MMC_RSP5 */
416     case 0x58:	/* MMC_RSP6 */
417     case 0x5c:	/* MMC_RSP7 */
418         return s->rsp[(offset - 0x40) >> 2];
419 
420     /* OMAP2-specific */
421     case 0x60:	/* MMC_IOSR */
422     case 0x64:	/* MMC_SYSC */
423         return 0;
424     case 0x68:	/* MMC_SYSS */
425         return 1;						/* RSTD */
426     }
427 
428     OMAP_BAD_REG(offset);
429     return 0;
430 }
431 
432 static void omap_mmc_write(void *opaque, hwaddr offset,
433                            uint64_t value, unsigned size)
434 {
435     int i;
436     struct omap_mmc_s *s = opaque;
437 
438     if (size != 2) {
439         omap_badwidth_write16(opaque, offset, value);
440         return;
441     }
442 
443     switch (offset) {
444     case 0x00:	/* MMC_CMD */
445         if (!s->enable)
446             break;
447 
448         s->last_cmd = value;
449         for (i = 0; i < 8; i ++)
450             s->rsp[i] = 0x0000;
451         omap_mmc_command(s, value & 63, (value >> 15) & 1,
452                          (MMCCmdType)((value >> 12) & 3),
453                          (value >> 11) & 1,
454                          (sd_rsp_type_t) ((value >> 8) & 7),
455                          (value >> 7) & 1);
456         omap_mmc_update(s);
457         break;
458 
459     case 0x04:	/* MMC_ARGL */
460         s->arg &= 0xffff0000;
461         s->arg |= 0x0000ffff & value;
462         break;
463 
464     case 0x08:	/* MMC_ARGH */
465         s->arg &= 0x0000ffff;
466         s->arg |= value << 16;
467         break;
468 
469     case 0x0c:	/* MMC_CON */
470         s->dw = (value >> 15) & 1;
471         s->mode = (value >> 12) & 3;
472         s->enable = (value >> 11) & 1;
473         s->be = (value >> 10) & 1;
474         s->clkdiv = (value >> 0) & (s->rev >= 2 ? 0x3ff : 0xff);
475         if (s->mode != 0) {
476             qemu_log_mask(LOG_UNIMP,
477                           "omap_mmc_wr: mode #%i unimplemented\n", s->mode);
478         }
479         if (s->be != 0) {
480             qemu_log_mask(LOG_UNIMP,
481                           "omap_mmc_wr: Big Endian not implemented\n");
482         }
483         if (s->dw != 0 && s->lines < 4)
484             printf("4-bit SD bus enabled\n");
485         if (!s->enable)
486             omap_mmc_pseudo_reset(s);
487         break;
488 
489     case 0x10:	/* MMC_STAT */
490         s->status &= ~value;
491         omap_mmc_interrupts_update(s);
492         break;
493 
494     case 0x14:	/* MMC_IE */
495         s->mask = value & 0x7fff;
496         omap_mmc_interrupts_update(s);
497         break;
498 
499     case 0x18:	/* MMC_CTO */
500         s->cto = value & 0xff;
501         if (s->cto > 0xfd && s->rev <= 1)
502             printf("MMC: CTO of 0xff and 0xfe cannot be used!\n");
503         break;
504 
505     case 0x1c:	/* MMC_DTO */
506         s->dto = value & 0xffff;
507         break;
508 
509     case 0x20:	/* MMC_DATA */
510         /* TODO: support 8-bit access */
511         if (s->fifo_len == 32)
512             break;
513         s->fifo[(s->fifo_start + s->fifo_len) & 31] = value;
514         s->fifo_len ++;
515         omap_mmc_transfer(s);
516         omap_mmc_fifolevel_update(s);
517         omap_mmc_interrupts_update(s);
518         break;
519 
520     case 0x24:	/* MMC_BLEN */
521         s->blen = (value & 0x07ff) + 1;
522         s->blen_counter = s->blen;
523         break;
524 
525     case 0x28:	/* MMC_NBLK */
526         s->nblk = (value & 0x07ff) + 1;
527         s->nblk_counter = s->nblk;
528         s->blen_counter = s->blen;
529         break;
530 
531     case 0x2c:	/* MMC_BUF */
532         s->rx_dma = (value >> 15) & 1;
533         s->af_level = (value >> 8) & 0x1f;
534         s->tx_dma = (value >> 7) & 1;
535         s->ae_level = value & 0x1f;
536 
537         if (s->rx_dma)
538             s->status &= 0xfbff;
539         if (s->tx_dma)
540             s->status &= 0xf7ff;
541         omap_mmc_fifolevel_update(s);
542         omap_mmc_interrupts_update(s);
543         break;
544 
545     /* SPI, SDIO and TEST modes unimplemented */
546     case 0x30:	/* MMC_SPI (OMAP1 only) */
547         break;
548     case 0x34:	/* MMC_SDIO */
549         s->sdio = value & (s->rev >= 2 ? 0xfbf3 : 0x2020);
550         s->cdet_wakeup = (value >> 9) & 1;
551         s->cdet_enable = (value >> 2) & 1;
552         break;
553     case 0x38:	/* MMC_SYST */
554         break;
555 
556     case 0x3c:	/* MMC_REV */
557     case 0x40:	/* MMC_RSP0 */
558     case 0x44:	/* MMC_RSP1 */
559     case 0x48:	/* MMC_RSP2 */
560     case 0x4c:	/* MMC_RSP3 */
561     case 0x50:	/* MMC_RSP4 */
562     case 0x54:	/* MMC_RSP5 */
563     case 0x58:	/* MMC_RSP6 */
564     case 0x5c:	/* MMC_RSP7 */
565         OMAP_RO_REG(offset);
566         break;
567 
568     /* OMAP2-specific */
569     case 0x60:	/* MMC_IOSR */
570         if (value & 0xf)
571             printf("MMC: SDIO bits used!\n");
572         break;
573     case 0x64:	/* MMC_SYSC */
574         if (value & (1 << 2))					/* SRTS */
575             omap_mmc_reset(s);
576         break;
577     case 0x68:	/* MMC_SYSS */
578         OMAP_RO_REG(offset);
579         break;
580 
581     default:
582         OMAP_BAD_REG(offset);
583     }
584 }
585 
586 static const MemoryRegionOps omap_mmc_ops = {
587     .read = omap_mmc_read,
588     .write = omap_mmc_write,
589     .endianness = DEVICE_NATIVE_ENDIAN,
590 };
591 
592 DeviceState *omap_mmc_init(hwaddr base,
593                            MemoryRegion *sysmem,
594                            BlockBackend *blk,
595                            qemu_irq irq, qemu_irq dma[], omap_clk clk)
596 {
597     DeviceState *dev;
598     OMAPMMCState *s;
599 
600     dev = qdev_new(TYPE_OMAP_MMC);
601     s = OMAP_MMC(dev);
602     sysbus_realize_and_unref(SYS_BUS_DEVICE(s), &error_fatal);
603 
604     s->irq = irq;
605     s->dma = dma;
606     s->clk = clk;
607 
608     memory_region_add_subregion(sysmem, base,
609                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(s), 0));
610 
611     /* Instantiate the storage */
612     s->card = sd_init(blk, false);
613     if (s->card == NULL) {
614         exit(1);
615     }
616     return dev;
617 }
618 
619 static void omap_mmc_reset_hold(Object *obj, ResetType type)
620 {
621     OMAPMMCState *s = OMAP_MMC(obj);
622 
623     omap_mmc_reset(s);
624 }
625 
626 static void omap_mmc_initfn(Object *obj)
627 {
628     OMAPMMCState *s = OMAP_MMC(obj);
629 
630     /* In theory these could be settable per-board */
631     s->lines = 1;
632     s->rev = 1;
633 
634     memory_region_init_io(&s->iomem, obj, &omap_mmc_ops, s, "omap.mmc", 0x800);
635     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
636 }
637 
638 static void omap_mmc_class_init(ObjectClass *oc, void *data)
639 {
640     ResettableClass *rc = RESETTABLE_CLASS(oc);
641 
642     rc->phases.hold = omap_mmc_reset_hold;
643 }
644 
645 static const TypeInfo omap_mmc_info = {
646     .name = TYPE_OMAP_MMC,
647     .parent = TYPE_SYS_BUS_DEVICE,
648     .instance_size = sizeof(OMAPMMCState),
649     .instance_init = omap_mmc_initfn,
650     .class_init = omap_mmc_class_init,
651 };
652 
653 static void omap_mmc_register_types(void)
654 {
655     type_register_static(&omap_mmc_info);
656 }
657 
658 type_init(omap_mmc_register_types)
659