2
0

omap_mmc.c 16 KB

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