omap_mmc.c 17 KB

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