2
0

omap_mmc.c 16 KB

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