sdmmc_mem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* $OpenBSD: sdmmc_mem.c,v 1.21 2015/04/22 04:02:06 jsg Exp $ */
  2. /*
  3. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* Routines for SD/MMC memory cards. */
  18. #include <sys/param.h>
  19. #include <sys/device.h>
  20. #include <sys/kernel.h>
  21. #include <sys/malloc.h>
  22. #include <sys/systm.h>
  23. #include <dev/sdmmc/sdmmcchip.h>
  24. #include <dev/sdmmc/sdmmcreg.h>
  25. #include <dev/sdmmc/sdmmcvar.h>
  26. int sdmmc_decode_csd(struct sdmmc_softc *, sdmmc_response,
  27. struct sdmmc_function *);
  28. int sdmmc_decode_cid(struct sdmmc_softc *, sdmmc_response,
  29. struct sdmmc_function *);
  30. void sdmmc_print_cid(struct sdmmc_cid *);
  31. int sdmmc_mem_send_op_cond(struct sdmmc_softc *, u_int32_t, u_int32_t *);
  32. int sdmmc_mem_set_blocklen(struct sdmmc_softc *, struct sdmmc_function *);
  33. int sdmmc_mem_send_cxd_data(struct sdmmc_softc *, int, void *, size_t);
  34. int sdmmc_mem_mmc_switch(struct sdmmc_function *, uint8_t, uint8_t, uint8_t);
  35. int sdmmc_mem_sd_init(struct sdmmc_softc *, struct sdmmc_function *);
  36. int sdmmc_mem_mmc_init(struct sdmmc_softc *, struct sdmmc_function *);
  37. int sdmmc_mem_single_read_block(struct sdmmc_function *, int, u_char *,
  38. size_t);
  39. int sdmmc_mem_read_block_subr(struct sdmmc_function *, int, u_char *,
  40. size_t);
  41. int sdmmc_mem_single_write_block(struct sdmmc_function *, int, u_char *,
  42. size_t);
  43. int sdmmc_mem_write_block_subr(struct sdmmc_function *, int, u_char *,
  44. size_t);
  45. #ifdef SDMMC_DEBUG
  46. #define DPRINTF(s) printf s
  47. #else
  48. #define DPRINTF(s) /**/
  49. #endif
  50. /*
  51. * Initialize SD/MMC memory cards and memory in SDIO "combo" cards.
  52. */
  53. int
  54. sdmmc_mem_enable(struct sdmmc_softc *sc)
  55. {
  56. u_int32_t host_ocr;
  57. u_int32_t card_ocr;
  58. rw_assert_wrlock(&sc->sc_lock);
  59. /* Set host mode to SD "combo" card or SD memory-only. */
  60. SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE);
  61. /* Reset memory (*must* do that before CMD55 or CMD1). */
  62. sdmmc_go_idle_state(sc);
  63. /*
  64. * Read the SD/MMC memory OCR value by issuing CMD55 followed
  65. * by ACMD41 to read the OCR value from memory-only SD cards.
  66. * MMC cards will not respond to CMD55 or ACMD41 and this is
  67. * how we distinguish them from SD cards.
  68. */
  69. mmc_mode:
  70. if (sdmmc_mem_send_op_cond(sc, 0, &card_ocr) != 0) {
  71. if (ISSET(sc->sc_flags, SMF_SD_MODE) &&
  72. !ISSET(sc->sc_flags, SMF_IO_MODE)) {
  73. /* Not a SD card, switch to MMC mode. */
  74. CLR(sc->sc_flags, SMF_SD_MODE);
  75. goto mmc_mode;
  76. }
  77. if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
  78. DPRINTF(("%s: can't read memory OCR\n",
  79. DEVNAME(sc)));
  80. return 1;
  81. } else {
  82. /* Not a "combo" card. */
  83. CLR(sc->sc_flags, SMF_MEM_MODE);
  84. return 0;
  85. }
  86. }
  87. /* Set the lowest voltage supported by the card and host. */
  88. host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch);
  89. if (sdmmc_set_bus_power(sc, host_ocr, card_ocr) != 0) {
  90. DPRINTF(("%s: can't supply voltage requested by card\n",
  91. DEVNAME(sc)));
  92. return 1;
  93. }
  94. /* Tell the card(s) to enter the idle state (again). */
  95. sdmmc_go_idle_state(sc);
  96. host_ocr &= card_ocr; /* only allow the common voltages */
  97. if (sdmmc_send_if_cond(sc, card_ocr) == 0)
  98. host_ocr |= SD_OCR_SDHC_CAP;
  99. /* Send the new OCR value until all cards are ready. */
  100. if (sdmmc_mem_send_op_cond(sc, host_ocr, NULL) != 0) {
  101. DPRINTF(("%s: can't send memory OCR\n", DEVNAME(sc)));
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * Read the CSD and CID from all cards and assign each card a unique
  108. * relative card address (RCA). CMD2 is ignored by SDIO-only cards.
  109. */
  110. void
  111. sdmmc_mem_scan(struct sdmmc_softc *sc)
  112. {
  113. struct sdmmc_command cmd;
  114. struct sdmmc_function *sf;
  115. u_int16_t next_rca;
  116. int error;
  117. int i;
  118. rw_assert_wrlock(&sc->sc_lock);
  119. /*
  120. * CMD2 is a broadcast command understood by SD cards and MMC
  121. * cards. All cards begin to respond to the command, but back
  122. * off if another card drives the CMD line to a different level.
  123. * Only one card will get its entire response through. That
  124. * card remains silent once it has been assigned a RCA.
  125. */
  126. for (i = 0; i < 100; i++) {
  127. bzero(&cmd, sizeof cmd);
  128. cmd.c_opcode = MMC_ALL_SEND_CID;
  129. cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
  130. error = sdmmc_mmc_command(sc, &cmd);
  131. if (error == ETIMEDOUT) {
  132. /* No more cards there. */
  133. break;
  134. } else if (error != 0) {
  135. DPRINTF(("%s: can't read CID\n", DEVNAME(sc)));
  136. break;
  137. }
  138. /* In MMC mode, find the next available RCA. */
  139. next_rca = 1;
  140. if (!ISSET(sc->sc_flags, SMF_SD_MODE))
  141. SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
  142. next_rca++;
  143. /* Allocate a sdmmc_function structure. */
  144. sf = sdmmc_function_alloc(sc);
  145. sf->rca = next_rca;
  146. /*
  147. * Remember the CID returned in the CMD2 response for
  148. * later decoding.
  149. */
  150. bcopy(cmd.c_resp, sf->raw_cid, sizeof sf->raw_cid);
  151. /*
  152. * Silence the card by assigning it a unique RCA, or
  153. * querying it for its RCA in the case of SD.
  154. */
  155. if (sdmmc_set_relative_addr(sc, sf) != 0) {
  156. printf("%s: can't set mem RCA\n", DEVNAME(sc));
  157. sdmmc_function_free(sf);
  158. break;
  159. }
  160. #if 0
  161. /* Verify that the RCA has been set by selecting the card. */
  162. if (sdmmc_select_card(sc, sf) != 0) {
  163. printf("%s: can't select mem RCA %d\n",
  164. DEVNAME(sc), sf->rca);
  165. sdmmc_function_free(sf);
  166. break;
  167. }
  168. /* Deselect. */
  169. (void)sdmmc_select_card(sc, NULL);
  170. #endif
  171. /*
  172. * If this is a memory-only card, the card responding
  173. * first becomes an alias for SDIO function 0.
  174. */
  175. if (sc->sc_fn0 == NULL)
  176. sc->sc_fn0 = sf;
  177. SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
  178. }
  179. /*
  180. * All cards are either inactive or awaiting further commands.
  181. * Read the CSDs and decode the raw CID for each card.
  182. */
  183. SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
  184. bzero(&cmd, sizeof cmd);
  185. cmd.c_opcode = MMC_SEND_CSD;
  186. cmd.c_arg = MMC_ARG_RCA(sf->rca);
  187. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
  188. if (sdmmc_mmc_command(sc, &cmd) != 0) {
  189. SET(sf->flags, SFF_ERROR);
  190. continue;
  191. }
  192. if (sdmmc_decode_csd(sc, cmd.c_resp, sf) != 0 ||
  193. sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) {
  194. SET(sf->flags, SFF_ERROR);
  195. continue;
  196. }
  197. #ifdef SDMMC_DEBUG
  198. printf("%s: CID: ", DEVNAME(sc));
  199. sdmmc_print_cid(&sf->cid);
  200. #endif
  201. }
  202. }
  203. int
  204. sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp,
  205. struct sdmmc_function *sf)
  206. {
  207. struct sdmmc_csd *csd = &sf->csd;
  208. if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
  209. /*
  210. * CSD version 1.0 corresponds to SD system
  211. * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
  212. */
  213. csd->csdver = SD_CSD_CSDVER(resp);
  214. switch (csd->csdver) {
  215. case SD_CSD_CSDVER_2_0:
  216. sf->flags |= SFF_SDHC;
  217. csd->capacity = SD_CSD_V2_CAPACITY(resp);
  218. csd->read_bl_len = SD_CSD_V2_BL_LEN;
  219. break;
  220. case SD_CSD_CSDVER_1_0:
  221. csd->capacity = SD_CSD_CAPACITY(resp);
  222. csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
  223. break;
  224. default:
  225. printf("%s: unknown SD CSD structure version 0x%x\n",
  226. DEVNAME(sc), csd->csdver);
  227. return 1;
  228. break;
  229. }
  230. } else {
  231. csd->csdver = MMC_CSD_CSDVER(resp);
  232. if (csd->csdver == MMC_CSD_CSDVER_1_0 ||
  233. csd->csdver == MMC_CSD_CSDVER_2_0 ||
  234. csd->csdver == MMC_CSD_CSDVER_EXT_CSD) {
  235. csd->mmcver = MMC_CSD_MMCVER(resp);
  236. csd->capacity = MMC_CSD_CAPACITY(resp);
  237. csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
  238. } else {
  239. printf("%s: unknown MMC CSD structure version 0x%x\n",
  240. DEVNAME(sc), csd->csdver);
  241. return 1;
  242. }
  243. }
  244. csd->sector_size = MIN(1 << csd->read_bl_len,
  245. sdmmc_chip_host_maxblklen(sc->sct, sc->sch));
  246. if (csd->sector_size < (1<<csd->read_bl_len))
  247. csd->capacity *= (1<<csd->read_bl_len) /
  248. csd->sector_size;
  249. return 0;
  250. }
  251. int
  252. sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp,
  253. struct sdmmc_function *sf)
  254. {
  255. struct sdmmc_cid *cid = &sf->cid;
  256. if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
  257. cid->mid = SD_CID_MID(resp);
  258. cid->oid = SD_CID_OID(resp);
  259. SD_CID_PNM_CPY(resp, cid->pnm);
  260. cid->rev = SD_CID_REV(resp);
  261. cid->psn = SD_CID_PSN(resp);
  262. cid->mdt = SD_CID_MDT(resp);
  263. } else {
  264. switch(sf->csd.mmcver) {
  265. case MMC_CSD_MMCVER_1_0:
  266. case MMC_CSD_MMCVER_1_4:
  267. cid->mid = MMC_CID_MID_V1(resp);
  268. MMC_CID_PNM_V1_CPY(resp, cid->pnm);
  269. cid->rev = MMC_CID_REV_V1(resp);
  270. cid->psn = MMC_CID_PSN_V1(resp);
  271. cid->mdt = MMC_CID_MDT_V1(resp);
  272. break;
  273. case MMC_CSD_MMCVER_2_0:
  274. case MMC_CSD_MMCVER_3_1:
  275. case MMC_CSD_MMCVER_4_0:
  276. cid->mid = MMC_CID_MID_V2(resp);
  277. cid->oid = MMC_CID_OID_V2(resp);
  278. MMC_CID_PNM_V2_CPY(resp, cid->pnm);
  279. cid->psn = MMC_CID_PSN_V2(resp);
  280. break;
  281. default:
  282. printf("%s: unknown MMC version %d\n",
  283. DEVNAME(sc), sf->csd.mmcver);
  284. return 1;
  285. }
  286. }
  287. return 0;
  288. }
  289. #ifdef SDMMC_DEBUG
  290. void
  291. sdmmc_print_cid(struct sdmmc_cid *cid)
  292. {
  293. printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
  294. " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
  295. cid->mdt);
  296. }
  297. #endif
  298. int
  299. sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data,
  300. size_t datalen)
  301. {
  302. struct sdmmc_command cmd;
  303. void *ptr = NULL;
  304. int error = 0;
  305. ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
  306. if (ptr == NULL) {
  307. error = ENOMEM;
  308. goto out;
  309. }
  310. memset(&cmd, 0, sizeof(cmd));
  311. cmd.c_data = ptr;
  312. cmd.c_datalen = datalen;
  313. cmd.c_blklen = datalen;
  314. cmd.c_opcode = opcode;
  315. cmd.c_arg = 0;
  316. cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ;
  317. if (opcode == MMC_SEND_EXT_CSD)
  318. SET(cmd.c_flags, SCF_RSP_R1);
  319. else
  320. SET(cmd.c_flags, SCF_RSP_R2);
  321. error = sdmmc_mmc_command(sc, &cmd);
  322. if (error == 0)
  323. memcpy(data, ptr, datalen);
  324. out:
  325. if (ptr != NULL)
  326. free(ptr, M_DEVBUF, 0);
  327. return error;
  328. }
  329. int
  330. sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index,
  331. uint8_t value)
  332. {
  333. struct sdmmc_softc *sc = sf->sc;
  334. struct sdmmc_command cmd;
  335. memset(&cmd, 0, sizeof(cmd));
  336. cmd.c_opcode = MMC_SWITCH;
  337. cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  338. (index << 16) | (value << 8) | set;
  339. cmd.c_flags = SCF_RSP_R1B | SCF_CMD_AC;
  340. return sdmmc_mmc_command(sc, &cmd);
  341. }
  342. /*
  343. * Initialize a SD/MMC memory card.
  344. */
  345. int
  346. sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
  347. {
  348. int error = 0;
  349. rw_assert_wrlock(&sc->sc_lock);
  350. if (sdmmc_select_card(sc, sf) != 0 ||
  351. sdmmc_mem_set_blocklen(sc, sf) != 0)
  352. error = 1;
  353. if (ISSET(sc->sc_flags, SMF_SD_MODE))
  354. error = sdmmc_mem_sd_init(sc, sf);
  355. else
  356. error = sdmmc_mem_mmc_init(sc, sf);
  357. return error;
  358. }
  359. int
  360. sdmmc_mem_sd_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
  361. {
  362. /* XXX */
  363. return 0;
  364. }
  365. int
  366. sdmmc_mem_mmc_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
  367. {
  368. int error = 0;
  369. u_int8_t ext_csd[512];
  370. int speed = 0;
  371. int hs_timing = 0;
  372. u_int32_t sectors = 0;
  373. if (sf->csd.mmcver >= MMC_CSD_MMCVER_4_0) {
  374. /* read EXT_CSD */
  375. error = sdmmc_mem_send_cxd_data(sc,
  376. MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd));
  377. if (error != 0) {
  378. SET(sf->flags, SFF_ERROR);
  379. printf("%s: can't read EXT_CSD\n", DEVNAME(sc));
  380. return error;
  381. }
  382. if (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_F_52M) {
  383. speed = 52000;
  384. hs_timing = 1;
  385. } else if (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_F_26M) {
  386. speed = 26000;
  387. } else {
  388. printf("%s: unknown CARD_TYPE 0x%x\n", DEVNAME(sc),
  389. ext_csd[EXT_CSD_CARD_TYPE]);
  390. }
  391. if (!ISSET(sc->sc_caps, SMC_CAPS_MMC_HIGHSPEED))
  392. hs_timing = 0;
  393. if (hs_timing) {
  394. /* switch to high speed timing */
  395. error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL,
  396. EXT_CSD_HS_TIMING, hs_timing);
  397. if (error != 0) {
  398. printf("%s: can't change high speed\n",
  399. DEVNAME(sc));
  400. return error;
  401. }
  402. }
  403. error =
  404. sdmmc_chip_bus_clock(sc->sct, sc->sch, speed);
  405. if (error != 0) {
  406. printf("%s: can't change bus clock\n", DEVNAME(sc));
  407. return error;
  408. }
  409. if (hs_timing) {
  410. /* read EXT_CSD again */
  411. error = sdmmc_mem_send_cxd_data(sc,
  412. MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd));
  413. if (error != 0) {
  414. printf("%s: can't re-read EXT_CSD\n", DEVNAME(sc));
  415. return error;
  416. }
  417. if (ext_csd[EXT_CSD_HS_TIMING] != 1) {
  418. printf("%s, HS_TIMING set failed\n", DEVNAME(sc));
  419. return EINVAL;
  420. }
  421. }
  422. sectors = ext_csd[EXT_CSD_SEC_COUNT + 0] << 0 |
  423. ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
  424. ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
  425. ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;
  426. if (sectors > (2u * 1024 * 1024 * 1024) / 512) {
  427. sf->flags |= SFF_SDHC;
  428. sf->csd.capacity = sectors;
  429. }
  430. }
  431. return error;
  432. }
  433. /*
  434. * Get or set the card's memory OCR value (SD or MMC).
  435. */
  436. int
  437. sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr,
  438. u_int32_t *ocrp)
  439. {
  440. struct sdmmc_command cmd;
  441. int error;
  442. int i;
  443. rw_assert_wrlock(&sc->sc_lock);
  444. /*
  445. * If we change the OCR value, retry the command until the OCR
  446. * we receive in response has the "CARD BUSY" bit set, meaning
  447. * that all cards are ready for identification.
  448. */
  449. for (i = 0; i < 100; i++) {
  450. bzero(&cmd, sizeof cmd);
  451. cmd.c_arg = ocr;
  452. cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3;
  453. if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
  454. cmd.c_opcode = SD_APP_OP_COND;
  455. error = sdmmc_app_command(sc, &cmd);
  456. } else {
  457. cmd.c_opcode = MMC_SEND_OP_COND;
  458. error = sdmmc_mmc_command(sc, &cmd);
  459. }
  460. if (error != 0)
  461. break;
  462. if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
  463. ocr == 0)
  464. break;
  465. error = ETIMEDOUT;
  466. sdmmc_delay(10000);
  467. }
  468. if (error == 0 && ocrp != NULL)
  469. *ocrp = MMC_R3(cmd.c_resp);
  470. return error;
  471. }
  472. /*
  473. * Set the read block length appropriately for this card, according to
  474. * the card CSD register value.
  475. */
  476. int
  477. sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf)
  478. {
  479. struct sdmmc_command cmd;
  480. rw_assert_wrlock(&sc->sc_lock);
  481. bzero(&cmd, sizeof cmd);
  482. cmd.c_opcode = MMC_SET_BLOCKLEN;
  483. cmd.c_arg = sf->csd.sector_size;
  484. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
  485. DPRINTF(("%s: read_bl_len=%d sector_size=%d\n", DEVNAME(sc),
  486. 1 << sf->csd.read_bl_len, sf->csd.sector_size));
  487. return sdmmc_mmc_command(sc, &cmd);
  488. }
  489. int
  490. sdmmc_mem_read_block_subr(struct sdmmc_function *sf, int blkno, u_char *data,
  491. size_t datalen)
  492. {
  493. struct sdmmc_softc *sc = sf->sc;
  494. struct sdmmc_command cmd;
  495. int error;
  496. if ((error = sdmmc_select_card(sc, sf)) != 0)
  497. goto err;
  498. bzero(&cmd, sizeof cmd);
  499. cmd.c_data = data;
  500. cmd.c_datalen = datalen;
  501. cmd.c_blklen = sf->csd.sector_size;
  502. cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ?
  503. MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
  504. if (sf->flags & SFF_SDHC)
  505. cmd.c_arg = blkno;
  506. else
  507. cmd.c_arg = blkno << 9;
  508. cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1;
  509. error = sdmmc_mmc_command(sc, &cmd);
  510. if (error != 0)
  511. goto err;
  512. if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) &&
  513. cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
  514. bzero(&cmd, sizeof cmd);
  515. cmd.c_opcode = MMC_STOP_TRANSMISSION;
  516. cmd.c_arg = MMC_ARG_RCA(sf->rca);
  517. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B;
  518. error = sdmmc_mmc_command(sc, &cmd);
  519. if (error != 0)
  520. goto err;
  521. }
  522. do {
  523. bzero(&cmd, sizeof cmd);
  524. cmd.c_opcode = MMC_SEND_STATUS;
  525. cmd.c_arg = MMC_ARG_RCA(sf->rca);
  526. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
  527. error = sdmmc_mmc_command(sc, &cmd);
  528. if (error != 0)
  529. break;
  530. /* XXX time out */
  531. } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
  532. err:
  533. return (error);
  534. }
  535. int
  536. sdmmc_mem_single_read_block(struct sdmmc_function *sf, int blkno, u_char *data,
  537. size_t datalen)
  538. {
  539. int error = 0;
  540. int i;
  541. for (i = 0; i < datalen / sf->csd.sector_size; i++) {
  542. error = sdmmc_mem_read_block_subr(sf, blkno + i, data + i *
  543. sf->csd.sector_size, sf->csd.sector_size);
  544. if (error)
  545. break;
  546. }
  547. return (error);
  548. }
  549. int
  550. sdmmc_mem_read_block(struct sdmmc_function *sf, int blkno, u_char *data,
  551. size_t datalen)
  552. {
  553. struct sdmmc_softc *sc = sf->sc;
  554. int error;
  555. rw_enter_write(&sc->sc_lock);
  556. if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
  557. error = sdmmc_mem_single_read_block(sf, blkno, data, datalen);
  558. } else {
  559. error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
  560. }
  561. rw_exit(&sc->sc_lock);
  562. return (error);
  563. }
  564. int
  565. sdmmc_mem_write_block_subr(struct sdmmc_function *sf, int blkno, u_char *data,
  566. size_t datalen)
  567. {
  568. struct sdmmc_softc *sc = sf->sc;
  569. struct sdmmc_command cmd;
  570. int error;
  571. if ((error = sdmmc_select_card(sc, sf)) != 0)
  572. goto err;
  573. bzero(&cmd, sizeof cmd);
  574. cmd.c_data = data;
  575. cmd.c_datalen = datalen;
  576. cmd.c_blklen = sf->csd.sector_size;
  577. cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ?
  578. MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
  579. if (sf->flags & SFF_SDHC)
  580. cmd.c_arg = blkno;
  581. else
  582. cmd.c_arg = blkno << 9;
  583. cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
  584. error = sdmmc_mmc_command(sc, &cmd);
  585. if (error != 0)
  586. goto err;
  587. if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) &&
  588. cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
  589. bzero(&cmd, sizeof cmd);
  590. cmd.c_opcode = MMC_STOP_TRANSMISSION;
  591. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B;
  592. error = sdmmc_mmc_command(sc, &cmd);
  593. if (error != 0)
  594. goto err;
  595. }
  596. do {
  597. bzero(&cmd, sizeof cmd);
  598. cmd.c_opcode = MMC_SEND_STATUS;
  599. cmd.c_arg = MMC_ARG_RCA(sf->rca);
  600. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
  601. error = sdmmc_mmc_command(sc, &cmd);
  602. if (error != 0)
  603. break;
  604. /* XXX time out */
  605. } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
  606. err:
  607. return (error);
  608. }
  609. int
  610. sdmmc_mem_single_write_block(struct sdmmc_function *sf, int blkno, u_char *data,
  611. size_t datalen)
  612. {
  613. int error = 0;
  614. int i;
  615. for (i = 0; i < datalen / sf->csd.sector_size; i++) {
  616. error = sdmmc_mem_write_block_subr(sf, blkno + i, data + i *
  617. sf->csd.sector_size, sf->csd.sector_size);
  618. if (error)
  619. break;
  620. }
  621. return (error);
  622. }
  623. int
  624. sdmmc_mem_write_block(struct sdmmc_function *sf, int blkno, u_char *data,
  625. size_t datalen)
  626. {
  627. struct sdmmc_softc *sc = sf->sc;
  628. int error;
  629. rw_enter_write(&sc->sc_lock);
  630. if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
  631. error = sdmmc_mem_single_write_block(sf, blkno, data, datalen);
  632. } else {
  633. error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
  634. }
  635. rw_exit(&sc->sc_lock);
  636. return (error);
  637. }