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