sdmmc_io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /* $OpenBSD: sdmmc_io.c,v 1.22 2015/03/14 03:38:49 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 I/O 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/sdmmc_ioreg.h>
  24. #include <dev/sdmmc/sdmmcchip.h>
  25. #include <dev/sdmmc/sdmmcvar.h>
  26. struct sdmmc_intr_handler {
  27. struct sdmmc_softc *ih_softc;
  28. const char *ih_name;
  29. int (*ih_fun)(void *);
  30. void *ih_arg;
  31. TAILQ_ENTRY(sdmmc_intr_handler) entry;
  32. };
  33. int sdmmc_submatch(struct device *, void *, void *);
  34. int sdmmc_print(void *, const char *);
  35. int sdmmc_io_rw_direct(struct sdmmc_softc *, struct sdmmc_function *,
  36. int, u_char *, int);
  37. int sdmmc_io_rw_extended(struct sdmmc_softc *, struct sdmmc_function *,
  38. int, u_char *, int, int);
  39. int sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
  40. int, u_char *);
  41. void sdmmc_io_reset(struct sdmmc_softc *);
  42. int sdmmc_io_send_op_cond(struct sdmmc_softc *, u_int32_t, u_int32_t *);
  43. #ifdef SDMMC_DEBUG
  44. #define DPRINTF(s) printf s
  45. #else
  46. #define DPRINTF(s) /**/
  47. #endif
  48. #ifdef SDMMC_DEBUG
  49. int sdmmc_verbose = 1;
  50. #else
  51. int sdmmc_verbose = 0;
  52. #endif
  53. /*
  54. * Initialize SD I/O card functions (before memory cards). The host
  55. * system and controller must support card interrupts in order to use
  56. * I/O functions.
  57. */
  58. int
  59. sdmmc_io_enable(struct sdmmc_softc *sc)
  60. {
  61. u_int32_t host_ocr;
  62. u_int32_t card_ocr;
  63. rw_assert_wrlock(&sc->sc_lock);
  64. /* Set host mode to SD "combo" card. */
  65. SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
  66. /* Reset I/O functions. */
  67. sdmmc_io_reset(sc);
  68. /*
  69. * Read the I/O OCR value, determine the number of I/O
  70. * functions and whether memory is also present (a "combo
  71. * card") by issuing CMD5. SD memory-only and MMC cards
  72. * do not respond to CMD5.
  73. */
  74. if (sdmmc_io_send_op_cond(sc, 0, &card_ocr) != 0) {
  75. /* No SDIO card; switch to SD memory-only mode. */
  76. CLR(sc->sc_flags, SMF_IO_MODE);
  77. return 0;
  78. }
  79. /* Parse the additional bits in the I/O OCR value. */
  80. if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
  81. /* SDIO card without memory (not a "combo card"). */
  82. DPRINTF(("%s: no memory present\n", DEVNAME(sc)));
  83. CLR(sc->sc_flags, SMF_MEM_MODE);
  84. }
  85. sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
  86. if (sc->sc_function_count == 0) {
  87. /* Useless SDIO card without any I/O functions. */
  88. DPRINTF(("%s: no I/O functions\n", DEVNAME(sc)));
  89. CLR(sc->sc_flags, SMF_IO_MODE);
  90. return 0;
  91. }
  92. card_ocr &= SD_IO_OCR_MASK;
  93. /* Set the lowest voltage supported by the card and host. */
  94. host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch);
  95. if (sdmmc_set_bus_power(sc, host_ocr, card_ocr) != 0) {
  96. printf("%s: can't supply voltage requested by card\n",
  97. DEVNAME(sc));
  98. return 1;
  99. }
  100. /* Reset I/O functions (again). */
  101. sdmmc_io_reset(sc);
  102. /* Send the new OCR value until all cards are ready. */
  103. if (sdmmc_io_send_op_cond(sc, host_ocr, NULL) != 0) {
  104. printf("%s: can't send I/O OCR\n", DEVNAME(sc));
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Allocate sdmmc_function structures for SD card I/O function
  111. * (including function 0).
  112. */
  113. void
  114. sdmmc_io_scan(struct sdmmc_softc *sc)
  115. {
  116. struct sdmmc_function *sf0, *sf;
  117. int i;
  118. rw_assert_wrlock(&sc->sc_lock);
  119. sf0 = sdmmc_function_alloc(sc);
  120. sf0->number = 0;
  121. if (sdmmc_set_relative_addr(sc, sf0) != 0) {
  122. printf("%s: can't set I/O RCA\n", DEVNAME(sc));
  123. SET(sf0->flags, SFF_ERROR);
  124. return;
  125. }
  126. sc->sc_fn0 = sf0;
  127. SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
  128. /* Verify that the RCA has been set by selecting the card. */
  129. if (sdmmc_select_card(sc, sf0) != 0) {
  130. printf("%s: can't select I/O RCA %d\n", DEVNAME(sc),
  131. sf0->rca);
  132. SET(sf0->flags, SFF_ERROR);
  133. return;
  134. }
  135. for (i = 1; i <= sc->sc_function_count; i++) {
  136. sf = sdmmc_function_alloc(sc);
  137. sf->number = i;
  138. sf->rca = sf0->rca;
  139. SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
  140. }
  141. }
  142. /*
  143. * Initialize SDIO card functions.
  144. */
  145. int
  146. sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
  147. {
  148. rw_assert_wrlock(&sc->sc_lock);
  149. if (sf->number == 0) {
  150. sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH,
  151. CCCR_BUS_WIDTH_1);
  152. if (sdmmc_read_cis(sf, &sf->cis) != 0) {
  153. printf("%s: can't read CIS\n", DEVNAME(sc));
  154. SET(sf->flags, SFF_ERROR);
  155. return 1;
  156. }
  157. sdmmc_check_cis_quirks(sf);
  158. if (sdmmc_verbose)
  159. sdmmc_print_cis(sf);
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Indicate whether the function is ready to operate.
  165. */
  166. int
  167. sdmmc_io_function_ready(struct sdmmc_function *sf)
  168. {
  169. struct sdmmc_softc *sc = sf->sc;
  170. struct sdmmc_function *sf0 = sc->sc_fn0;
  171. u_int8_t rv;
  172. rw_assert_wrlock(&sc->sc_lock);
  173. if (sf->number == 0)
  174. return 1; /* FN0 is always ready */
  175. rv = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_READY);
  176. return (rv & (1 << sf->number)) != 0;
  177. }
  178. /*
  179. * Enable the I/O function. Return zero if the function was
  180. * enabled successfully.
  181. */
  182. int
  183. sdmmc_io_function_enable(struct sdmmc_function *sf)
  184. {
  185. struct sdmmc_softc *sc = sf->sc;
  186. struct sdmmc_function *sf0 = sc->sc_fn0;
  187. u_int8_t rv;
  188. int retry = 5;
  189. if (sf->number == 0)
  190. return 0; /* FN0 is always enabled */
  191. rw_enter_write(&sc->sc_lock);
  192. rv = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
  193. rv |= (1<<sf->number);
  194. sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, rv);
  195. rw_exit(&sc->sc_lock);
  196. while (!sdmmc_io_function_ready(sf) && retry-- > 0)
  197. tsleep(&lbolt, PPAUSE, "pause", 0);
  198. return (retry >= 0) ? 0 : ETIMEDOUT;
  199. }
  200. /*
  201. * Disable the I/O function. Return zero if the function was
  202. * disabled successfully.
  203. */
  204. void
  205. sdmmc_io_function_disable(struct sdmmc_function *sf)
  206. {
  207. struct sdmmc_softc *sc = sf->sc;
  208. struct sdmmc_function *sf0 = sc->sc_fn0;
  209. u_int8_t rv;
  210. rw_assert_wrlock(&sc->sc_lock);
  211. if (sf->number == 0)
  212. return; /* FN0 is always enabled */
  213. rv = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
  214. rv &= ~(1<<sf->number);
  215. sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, rv);
  216. }
  217. void
  218. sdmmc_io_attach(struct sdmmc_softc *sc)
  219. {
  220. struct sdmmc_function *sf;
  221. struct sdmmc_attach_args saa;
  222. rw_assert_wrlock(&sc->sc_lock);
  223. SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
  224. if (sf->number < 1)
  225. continue;
  226. bzero(&saa, sizeof saa);
  227. saa.sf = sf;
  228. sf->child = config_found_sm(&sc->sc_dev, &saa, sdmmc_print,
  229. sdmmc_submatch);
  230. }
  231. }
  232. int
  233. sdmmc_submatch(struct device *parent, void *match, void *aux)
  234. {
  235. struct cfdata *cf = match;
  236. /* Skip the scsibus, it is configured directly. */
  237. if (strcmp(cf->cf_driver->cd_name, "scsibus") == 0)
  238. return 0;
  239. return cf->cf_attach->ca_match(parent, cf, aux);
  240. }
  241. int
  242. sdmmc_print(void *aux, const char *pnp)
  243. {
  244. struct sdmmc_attach_args *sa = aux;
  245. struct sdmmc_function *sf = sa->sf;
  246. struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
  247. int i;
  248. if (pnp) {
  249. if (sf->number == 0)
  250. return QUIET;
  251. for (i = 0; i < 4 && cis->cis1_info[i]; i++)
  252. printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
  253. if (i != 0)
  254. printf("\"");
  255. if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
  256. cis->product != SDMMC_PRODUCT_INVALID) {
  257. printf("%s(", i ? " " : "");
  258. if (cis->manufacturer != SDMMC_VENDOR_INVALID)
  259. printf("manufacturer 0x%x%s",
  260. cis->manufacturer,
  261. cis->product == SDMMC_PRODUCT_INVALID ?
  262. "" : ", ");
  263. if (cis->product != SDMMC_PRODUCT_INVALID)
  264. printf("product 0x%x", cis->product);
  265. printf(")");
  266. }
  267. printf("%sat %s", i ? " " : "", pnp);
  268. }
  269. printf(" function %d", sf->number);
  270. if (!pnp) {
  271. for (i = 0; i < 3 && cis->cis1_info[i]; i++)
  272. printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
  273. if (i != 0)
  274. printf("\"");
  275. }
  276. return UNCONF;
  277. }
  278. void
  279. sdmmc_io_detach(struct sdmmc_softc *sc)
  280. {
  281. struct sdmmc_function *sf;
  282. rw_assert_wrlock(&sc->sc_lock);
  283. SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
  284. if (sf->child != NULL) {
  285. config_detach(sf->child, DETACH_FORCE);
  286. sf->child = NULL;
  287. }
  288. }
  289. KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
  290. }
  291. int
  292. sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
  293. int reg, u_char *datap, int arg)
  294. {
  295. struct sdmmc_command cmd;
  296. int error;
  297. rw_assert_wrlock(&sc->sc_lock);
  298. /* Make sure the card is selected. */
  299. if ((error = sdmmc_select_card(sc, sf)) != 0) {
  300. rw_exit(&sc->sc_lock);
  301. return error;
  302. }
  303. arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
  304. SD_ARG_CMD52_FUNC_SHIFT;
  305. arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
  306. SD_ARG_CMD52_REG_SHIFT;
  307. arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
  308. SD_ARG_CMD52_DATA_SHIFT;
  309. bzero(&cmd, sizeof cmd);
  310. cmd.c_opcode = SD_IO_RW_DIRECT;
  311. cmd.c_arg = arg;
  312. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
  313. error = sdmmc_mmc_command(sc, &cmd);
  314. *datap = SD_R5_DATA(cmd.c_resp);
  315. return error;
  316. }
  317. /*
  318. * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
  319. * SD_ARG_CMD53_WRITE. SD_ARG_CMD53_INCREMENT may be ORed into `arg'
  320. * to access successive register locations instead of accessing the
  321. * same register many times.
  322. */
  323. int
  324. sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
  325. int reg, u_char *datap, int datalen, int arg)
  326. {
  327. struct sdmmc_command cmd;
  328. int error;
  329. rw_assert_wrlock(&sc->sc_lock);
  330. #if 0
  331. /* Make sure the card is selected. */
  332. if ((error = sdmmc_select_card(sc, sf)) != 0) {
  333. rw_exit(&sc->sc_lock);
  334. return error;
  335. }
  336. #endif
  337. arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
  338. SD_ARG_CMD53_FUNC_SHIFT;
  339. arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
  340. SD_ARG_CMD53_REG_SHIFT;
  341. arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
  342. SD_ARG_CMD53_LENGTH_SHIFT;
  343. bzero(&cmd, sizeof cmd);
  344. cmd.c_opcode = SD_IO_RW_EXTENDED;
  345. cmd.c_arg = arg;
  346. cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
  347. cmd.c_data = datap;
  348. cmd.c_datalen = datalen;
  349. cmd.c_blklen = MIN(datalen, sdmmc_chip_host_maxblklen(sc->sct, sc->sch));
  350. if (!ISSET(arg, SD_ARG_CMD53_WRITE))
  351. cmd.c_flags |= SCF_CMD_READ;
  352. error = sdmmc_mmc_command(sc, &cmd);
  353. rw_exit(&sc->sc_lock);
  354. return error;
  355. }
  356. u_int8_t
  357. sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
  358. {
  359. u_int8_t data = 0;
  360. rw_assert_wrlock(&sf->sc->sc_lock);
  361. (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
  362. SD_ARG_CMD52_READ);
  363. return data;
  364. }
  365. void
  366. sdmmc_io_write_1(struct sdmmc_function *sf, int reg, u_int8_t data)
  367. {
  368. rw_assert_wrlock(&sf->sc->sc_lock);
  369. (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
  370. SD_ARG_CMD52_WRITE);
  371. }
  372. u_int16_t
  373. sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
  374. {
  375. u_int16_t data = 0;
  376. rw_assert_wrlock(&sf->sc->sc_lock);
  377. (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
  378. SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
  379. return data;
  380. }
  381. void
  382. sdmmc_io_write_2(struct sdmmc_function *sf, int reg, u_int16_t data)
  383. {
  384. rw_assert_wrlock(&sf->sc->sc_lock);
  385. (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
  386. SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
  387. }
  388. u_int32_t
  389. sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
  390. {
  391. u_int32_t data = 0;
  392. rw_assert_wrlock(&sf->sc->sc_lock);
  393. (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
  394. SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
  395. return data;
  396. }
  397. void
  398. sdmmc_io_write_4(struct sdmmc_function *sf, int reg, u_int32_t data)
  399. {
  400. rw_assert_wrlock(&sf->sc->sc_lock);
  401. (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
  402. SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
  403. }
  404. int
  405. sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
  406. int datalen)
  407. {
  408. int error;
  409. rw_assert_wrlock(&sf->sc->sc_lock);
  410. while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
  411. error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
  412. SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_READ);
  413. if (error)
  414. return error;
  415. data += SD_ARG_CMD53_LENGTH_MAX;
  416. datalen -= SD_ARG_CMD53_LENGTH_MAX;
  417. }
  418. return sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
  419. SD_ARG_CMD53_READ);
  420. }
  421. int
  422. sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
  423. int datalen)
  424. {
  425. int error;
  426. rw_assert_wrlock(&sf->sc->sc_lock);
  427. while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
  428. error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
  429. SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_WRITE);
  430. if (error)
  431. return error;
  432. data += SD_ARG_CMD53_LENGTH_MAX;
  433. datalen -= SD_ARG_CMD53_LENGTH_MAX;
  434. }
  435. return sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
  436. SD_ARG_CMD53_WRITE);
  437. }
  438. int
  439. sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
  440. int reg, u_char *datap)
  441. {
  442. rw_assert_wrlock(&sc->sc_lock);
  443. return sdmmc_io_rw_direct(sc, sf, reg, datap,
  444. SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
  445. }
  446. /*
  447. * Reset the I/O functions of the card.
  448. */
  449. void
  450. sdmmc_io_reset(struct sdmmc_softc *sc)
  451. {
  452. #if 0 /* XXX command fails */
  453. (void)sdmmc_io_write(sc, NULL, SD_IO_REG_CCCR_CTL, CCCR_CTL_RES);
  454. sdmmc_delay(100000);
  455. #endif
  456. }
  457. /*
  458. * Get or set the card's I/O OCR value (SDIO).
  459. */
  460. int
  461. sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
  462. {
  463. struct sdmmc_command cmd;
  464. int error;
  465. int i;
  466. rw_assert_wrlock(&sc->sc_lock);
  467. /*
  468. * If we change the OCR value, retry the command until the OCR
  469. * we receive in response has the "CARD BUSY" bit set, meaning
  470. * that all cards are ready for identification.
  471. */
  472. for (i = 0; i < 100; i++) {
  473. bzero(&cmd, sizeof cmd);
  474. cmd.c_opcode = SD_IO_SEND_OP_COND;
  475. cmd.c_arg = ocr;
  476. cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4;
  477. error = sdmmc_mmc_command(sc, &cmd);
  478. if (error != 0)
  479. break;
  480. if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) ||
  481. ocr == 0)
  482. break;
  483. error = ETIMEDOUT;
  484. sdmmc_delay(10000);
  485. }
  486. if (error == 0 && ocrp != NULL)
  487. *ocrp = MMC_R4(cmd.c_resp);
  488. return error;
  489. }
  490. /*
  491. * Card interrupt handling
  492. */
  493. void
  494. sdmmc_intr_enable(struct sdmmc_function *sf)
  495. {
  496. struct sdmmc_softc *sc = sf->sc;
  497. struct sdmmc_function *sf0 = sc->sc_fn0;
  498. u_int8_t imask;
  499. rw_enter_write(&sc->sc_lock);
  500. imask = sdmmc_io_read_1(sf0, SD_IO_CCCR_INT_ENABLE);
  501. imask |= 1 << sf->number;
  502. sdmmc_io_write_1(sf0, SD_IO_CCCR_INT_ENABLE, imask);
  503. rw_exit(&sc->sc_lock);
  504. }
  505. void
  506. sdmmc_intr_disable(struct sdmmc_function *sf)
  507. {
  508. struct sdmmc_softc *sc = sf->sc;
  509. struct sdmmc_function *sf0 = sc->sc_fn0;
  510. u_int8_t imask;
  511. rw_enter_write(&sc->sc_lock);
  512. imask = sdmmc_io_read_1(sf0, SD_IO_CCCR_INT_ENABLE);
  513. imask &= ~(1 << sf->number);
  514. sdmmc_io_write_1(sf0, SD_IO_CCCR_INT_ENABLE, imask);
  515. rw_exit(&sc->sc_lock);
  516. }
  517. /*
  518. * Establish a handler for the SDIO card interrupt. Because the
  519. * interrupt may be shared with different SDIO functions, multiple
  520. * handlers can be established.
  521. */
  522. void *
  523. sdmmc_intr_establish(struct device *sdmmc, int (*fun)(void *),
  524. void *arg, const char *name)
  525. {
  526. struct sdmmc_softc *sc = (struct sdmmc_softc *)sdmmc;
  527. struct sdmmc_intr_handler *ih;
  528. int s;
  529. if (sc->sct->card_intr_mask == NULL)
  530. return NULL;
  531. ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK | M_CANFAIL | M_ZERO);
  532. if (ih == NULL)
  533. return NULL;
  534. ih->ih_name = name;
  535. ih->ih_softc = sc;
  536. ih->ih_fun = fun;
  537. ih->ih_arg = arg;
  538. s = splhigh();
  539. if (TAILQ_EMPTY(&sc->sc_intrq)) {
  540. sdmmc_intr_enable(sc->sc_fn0);
  541. sdmmc_chip_card_intr_mask(sc->sct, sc->sch, 1);
  542. }
  543. TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
  544. splx(s);
  545. return ih;
  546. }
  547. /*
  548. * Disestablish the given handler.
  549. */
  550. void
  551. sdmmc_intr_disestablish(void *cookie)
  552. {
  553. struct sdmmc_intr_handler *ih = cookie;
  554. struct sdmmc_softc *sc = ih->ih_softc;
  555. int s;
  556. if (sc->sct->card_intr_mask == NULL)
  557. return;
  558. s = splhigh();
  559. TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
  560. if (TAILQ_EMPTY(&sc->sc_intrq)) {
  561. sdmmc_chip_card_intr_mask(sc->sct, sc->sch, 0);
  562. sdmmc_intr_disable(sc->sc_fn0);
  563. }
  564. splx(s);
  565. free(ih, M_DEVBUF, 0);
  566. }
  567. /*
  568. * Call established SDIO card interrupt handlers. The host controller
  569. * must call this function from its own interrupt handler to handle an
  570. * SDIO interrupt from the card.
  571. */
  572. void
  573. sdmmc_card_intr(struct device *sdmmc)
  574. {
  575. struct sdmmc_softc *sc = (struct sdmmc_softc *)sdmmc;
  576. if (sc->sct->card_intr_mask == NULL)
  577. return;
  578. if (!sdmmc_task_pending(&sc->sc_intr_task))
  579. sdmmc_add_task(sc, &sc->sc_intr_task);
  580. }
  581. void
  582. sdmmc_intr_task(void *arg)
  583. {
  584. struct sdmmc_softc *sc = arg;
  585. struct sdmmc_intr_handler *ih;
  586. int s;
  587. s = splhigh();
  588. TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
  589. splx(s);
  590. /* XXX examine return value and do evcount stuff*/
  591. (void)ih->ih_fun(ih->ih_arg);
  592. s = splhigh();
  593. }
  594. sdmmc_chip_card_intr_ack(sc->sct, sc->sch);
  595. splx(s);
  596. }