sdmmc_io.c revision 1.14 1 /* $NetBSD: sdmmc_io.c,v 1.14 2018/10/14 17:37:40 jdolecek Exp $ */
2 /* $OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw Exp $ */
3
4 /*
5 * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Routines for SD I/O cards. */
21
22 #include <sys/cdefs.h>
23 __KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.14 2018/10/14 17:37:40 jdolecek Exp $");
24
25 #ifdef _KERNEL_OPT
26 #include "opt_sdmmc.h"
27 #endif
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/proc.h>
33 #include <sys/systm.h>
34
35 #include <dev/sdmmc/sdmmc_ioreg.h>
36 #include <dev/sdmmc/sdmmcchip.h>
37 #include <dev/sdmmc/sdmmcreg.h>
38 #include <dev/sdmmc/sdmmcvar.h>
39
40 #ifdef SDMMC_DEBUG
41 #define DPRINTF(s) do { printf s; } while (0)
42 #else
43 #define DPRINTF(s) do {} while (0)
44 #endif
45
46 struct sdmmc_intr_handler {
47 struct sdmmc_softc *ih_softc;
48 char *ih_name;
49 int (*ih_fun)(void *);
50 void *ih_arg;
51 TAILQ_ENTRY(sdmmc_intr_handler) entry;
52 };
53
54 static int sdmmc_io_rw_direct(struct sdmmc_softc *,
55 struct sdmmc_function *, int, u_char *, int);
56 static int sdmmc_io_rw_extended(struct sdmmc_softc *,
57 struct sdmmc_function *, int, u_char *, int, int);
58 #if 0
59 static int sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
60 int, u_char *);
61 #endif
62 static void sdmmc_io_reset(struct sdmmc_softc *);
63 static int sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
64 uint32_t *);
65
66 /*
67 * Initialize SD I/O card functions (before memory cards). The host
68 * system and controller must support card interrupts in order to use
69 * I/O functions.
70 */
71 int
72 sdmmc_io_enable(struct sdmmc_softc *sc)
73 {
74 uint32_t host_ocr;
75 uint32_t card_ocr;
76 int error;
77
78 SDMMC_LOCK(sc);
79
80 /* Set host mode to SD "combo" card. */
81 SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
82
83 /* Reset I/O functions. */
84 sdmmc_io_reset(sc);
85
86 /*
87 * Read the I/O OCR value, determine the number of I/O
88 * functions and whether memory is also present (a "combo
89 * card") by issuing CMD5. SD memory-only and MMC cards
90 * do not respond to CMD5.
91 */
92 error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
93 if (error) {
94 /* No SDIO card; switch to SD memory-only mode. */
95 CLR(sc->sc_flags, SMF_IO_MODE);
96 error = 0;
97 goto out;
98 }
99
100 /* Parse the additional bits in the I/O OCR value. */
101 if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
102 /* SDIO card without memory (not a "combo card"). */
103 DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
104 CLR(sc->sc_flags, SMF_MEM_MODE);
105 }
106 sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
107 if (sc->sc_function_count == 0) {
108 /* Useless SDIO card without any I/O functions. */
109 DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
110 CLR(sc->sc_flags, SMF_IO_MODE);
111 error = 0;
112 goto out;
113 }
114 card_ocr &= SD_IO_OCR_MASK;
115
116 /* Set the lowest voltage supported by the card and host. */
117 host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
118 error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
119 if (error) {
120 aprint_error_dev(sc->sc_dev,
121 "couldn't supply voltage requested by card\n");
122 goto out;
123 }
124
125 /* Send the new OCR value until all cards are ready. */
126 error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
127 if (error) {
128 aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
129 goto out;
130 }
131
132 out:
133 SDMMC_UNLOCK(sc);
134
135 return error;
136 }
137
138 /*
139 * Allocate sdmmc_function structures for SD card I/O function
140 * (including function 0).
141 */
142 void
143 sdmmc_io_scan(struct sdmmc_softc *sc)
144 {
145 struct sdmmc_function *sf0, *sf;
146 int error;
147 int i;
148
149 SDMMC_LOCK(sc);
150
151 sf0 = sdmmc_function_alloc(sc);
152 sf0->number = 0;
153 error = sdmmc_set_relative_addr(sc, sf0);
154 if (error) {
155 aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
156 SET(sf0->flags, SFF_ERROR);
157 goto out;
158 }
159 sc->sc_fn0 = sf0;
160 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
161
162 /* Go to Data Transfer Mode, if possible. */
163 sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);
164
165 /* Verify that the RCA has been set by selecting the card. */
166 error = sdmmc_select_card(sc, sf0);
167 if (error) {
168 aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
169 sf0->rca);
170 SET(sf0->flags, SFF_ERROR);
171 goto out;
172 }
173
174 for (i = 1; i <= sc->sc_function_count; i++) {
175 sf = sdmmc_function_alloc(sc);
176 sf->number = i;
177 sf->rca = sf0->rca;
178 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
179 }
180
181 out:
182 SDMMC_UNLOCK(sc);
183 }
184
185 /*
186 * Initialize SDIO card functions.
187 */
188 int
189 sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
190 {
191 struct sdmmc_function *sf0 = sc->sc_fn0;
192 int error = 0;
193 uint8_t reg;
194
195 SDMMC_LOCK(sc);
196
197 if (sf->number == 0) {
198 reg = sdmmc_io_read_1(sf, SD_IO_CCCR_CAPABILITY);
199 if (!(reg & CCCR_CAPS_LSC) || (reg & CCCR_CAPS_4BLS)) {
200 sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH,
201 CCCR_BUS_WIDTH_4);
202 sf->width = 4;
203 error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch,
204 sf->width);
205 if (error)
206 aprint_error_dev(sc->sc_dev,
207 "can't change bus width\n");
208 }
209
210 error = sdmmc_read_cis(sf, &sf->cis);
211 if (error) {
212 aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
213 SET(sf->flags, SFF_ERROR);
214 goto out;
215 }
216
217 sdmmc_check_cis_quirks(sf);
218
219 #ifdef SDMMC_DEBUG
220 if (sdmmcdebug)
221 sdmmc_print_cis(sf);
222 #endif
223
224 reg = sdmmc_io_read_1(sf, SD_IO_CCCR_HIGH_SPEED);
225 if (reg & CCCR_HIGH_SPEED_SHS) {
226 reg |= CCCR_HIGH_SPEED_EHS;
227 sdmmc_io_write_1(sf, SD_IO_CCCR_HIGH_SPEED, reg);
228 sf->csd.tran_speed = 50000; /* 50MHz */
229
230 /* Wait 400KHz x 8 clock */
231 delay(1);
232 }
233 if (sc->sc_busclk > sf->csd.tran_speed)
234 sc->sc_busclk = sf->csd.tran_speed;
235 error =
236 sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk,
237 false);
238 if (error)
239 aprint_error_dev(sc->sc_dev,
240 "can't change bus clock\n");
241 } else {
242 reg = sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x000);
243 sf->interface = FBR_STD_FUNC_IF_CODE(reg);
244 if (sf->interface == 0x0f)
245 sf->interface =
246 sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x001);
247 error = sdmmc_read_cis(sf, &sf->cis);
248 if (error) {
249 aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
250 SET(sf->flags, SFF_ERROR);
251 goto out;
252 }
253
254 sdmmc_check_cis_quirks(sf);
255
256 #ifdef SDMMC_DEBUG
257 if (sdmmcdebug)
258 sdmmc_print_cis(sf);
259 #endif
260 }
261
262 out:
263 SDMMC_UNLOCK(sc);
264
265 return error;
266 }
267
268 /*
269 * Indicate whether the function is ready to operate.
270 */
271 static int
272 sdmmc_io_function_ready(struct sdmmc_function *sf)
273 {
274 struct sdmmc_softc *sc = sf->sc;
275 struct sdmmc_function *sf0 = sc->sc_fn0;
276 uint8_t reg;
277
278 if (sf->number == 0)
279 return 1; /* FN0 is always ready */
280
281 SDMMC_LOCK(sc);
282 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
283 SDMMC_UNLOCK(sc);
284 return (reg & (1 << sf->number)) != 0;
285 }
286
287 int
288 sdmmc_io_function_enable(struct sdmmc_function *sf)
289 {
290 struct sdmmc_softc *sc = sf->sc;
291 struct sdmmc_function *sf0 = sc->sc_fn0;
292 uint8_t reg;
293 int retry;
294
295 if (sf->number == 0)
296 return 0; /* FN0 is always enabled */
297
298 SDMMC_LOCK(sc);
299 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
300 SET(reg, (1U << sf->number));
301 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
302 SDMMC_UNLOCK(sc);
303
304 retry = 5;
305 while (!sdmmc_io_function_ready(sf) && retry-- > 0)
306 kpause("pause", false, hz, NULL);
307 return (retry >= 0) ? 0 : ETIMEDOUT;
308 }
309
310 /*
311 * Disable the I/O function. Return zero if the function was
312 * disabled successfully.
313 */
314 void
315 sdmmc_io_function_disable(struct sdmmc_function *sf)
316 {
317 struct sdmmc_softc *sc = sf->sc;
318 struct sdmmc_function *sf0 = sc->sc_fn0;
319 uint8_t reg;
320
321 if (sf->number == 0)
322 return; /* FN0 is always enabled */
323
324 SDMMC_LOCK(sc);
325 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
326 CLR(reg, (1U << sf->number));
327 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
328 SDMMC_UNLOCK(sc);
329 }
330
331 static int
332 sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
333 int reg, u_char *datap, int arg)
334 {
335 struct sdmmc_command cmd;
336 int error;
337
338 /* Don't lock */
339
340 /* Make sure the card is selected. */
341 error = sdmmc_select_card(sc, sf);
342 if (error)
343 return error;
344
345 arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
346 SD_ARG_CMD52_FUNC_SHIFT;
347 arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
348 SD_ARG_CMD52_REG_SHIFT;
349 arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
350 SD_ARG_CMD52_DATA_SHIFT;
351
352 memset(&cmd, 0, sizeof cmd);
353 cmd.c_opcode = SD_IO_RW_DIRECT;
354 cmd.c_arg = arg;
355 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
356
357 error = sdmmc_mmc_command(sc, &cmd);
358 *datap = SD_R5_DATA(cmd.c_resp);
359
360 return error;
361 }
362
363 /*
364 * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
365 * SD_ARG_CMD53_WRITE. SD_ARG_CMD53_INCREMENT may be ORed into `arg'
366 * to access successive register locations instead of accessing the
367 * same register many times.
368 */
369 static int
370 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
371 int reg, u_char *datap, int datalen, int arg)
372 {
373 struct sdmmc_command cmd;
374 int error;
375
376 /* Don't lock */
377
378 #if 0
379 /* Make sure the card is selected. */
380 error = sdmmc_select_card(sc, sf);
381 if (error)
382 return error;
383 #endif
384
385 arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
386 SD_ARG_CMD53_FUNC_SHIFT;
387 arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
388 SD_ARG_CMD53_REG_SHIFT;
389 arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
390 SD_ARG_CMD53_LENGTH_SHIFT;
391
392 memset(&cmd, 0, sizeof cmd);
393 cmd.c_opcode = SD_IO_RW_EXTENDED;
394 cmd.c_arg = arg;
395 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
396 cmd.c_data = datap;
397 cmd.c_datalen = datalen;
398 cmd.c_blklen = MIN(datalen,
399 sdmmc_chip_host_maxblklen(sc->sc_sct,sc->sc_sch));
400 if (!ISSET(arg, SD_ARG_CMD53_WRITE))
401 cmd.c_flags |= SCF_CMD_READ;
402
403 error = sdmmc_mmc_command(sc, &cmd);
404
405 return error;
406 }
407
408 uint8_t
409 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
410 {
411 uint8_t data = 0;
412
413 /* Don't lock */
414
415 (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
416 SD_ARG_CMD52_READ);
417 return data;
418 }
419
420 void
421 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
422 {
423
424 /* Don't lock */
425
426 (void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
427 SD_ARG_CMD52_WRITE);
428 }
429
430 uint16_t
431 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
432 {
433 uint16_t data = 0;
434
435 /* Don't lock */
436
437 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
438 SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
439 return data;
440 }
441
442 void
443 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
444 {
445
446 /* Don't lock */
447
448 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
449 SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
450 }
451
452 uint32_t
453 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
454 {
455 uint32_t data = 0;
456
457 /* Don't lock */
458
459 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
460 SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
461 return data;
462 }
463
464 void
465 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
466 {
467
468 /* Don't lock */
469
470 (void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
471 SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
472 }
473
474
475 int
476 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
477 int datalen)
478 {
479 int error;
480
481 /* Don't lock */
482
483 while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
484 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
485 SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_READ);
486 if (error)
487 goto error;
488 data += SD_ARG_CMD53_LENGTH_MAX;
489 datalen -= SD_ARG_CMD53_LENGTH_MAX;
490 }
491
492 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
493 SD_ARG_CMD53_READ);
494 error:
495 return error;
496 }
497
498 int
499 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
500 int datalen)
501 {
502 int error;
503
504 /* Don't lock */
505
506 while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
507 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
508 SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_WRITE);
509 if (error)
510 goto error;
511 data += SD_ARG_CMD53_LENGTH_MAX;
512 datalen -= SD_ARG_CMD53_LENGTH_MAX;
513 }
514
515 error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
516 SD_ARG_CMD53_WRITE);
517 error:
518 return error;
519 }
520
521 #if 0
522 static int
523 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
524 int reg, u_char *datap)
525 {
526
527 /* Don't lock */
528
529 return sdmmc_io_rw_direct(sc, sf, reg, datap,
530 SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
531 }
532 #endif
533
534 /*
535 * Reset the I/O functions of the card.
536 */
537 static void
538 sdmmc_io_reset(struct sdmmc_softc *sc)
539 {
540 u_char data = CCCR_CTL_RES;
541
542 if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data, SD_ARG_CMD52_WRITE) == 0)
543 sdmmc_delay(100000);
544 }
545
546 /*
547 * Get or set the card's I/O OCR value (SDIO).
548 */
549 static int
550 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
551 {
552 struct sdmmc_command cmd;
553 int error;
554 int retry;
555
556 DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
557
558 /* Don't lock */
559
560 /*
561 * If we change the OCR value, retry the command until the OCR
562 * we receive in response has the "CARD BUSY" bit set, meaning
563 * that all cards are ready for identification.
564 */
565 for (retry = 0; retry < 100; retry++) {
566 memset(&cmd, 0, sizeof cmd);
567 cmd.c_opcode = SD_IO_SEND_OP_COND;
568 cmd.c_arg = ocr;
569 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;
570
571 error = sdmmc_mmc_command(sc, &cmd);
572 if (error)
573 break;
574 if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
575 break;
576
577 error = ETIMEDOUT;
578 sdmmc_delay(10000);
579 }
580 if (error == 0 && ocrp != NULL)
581 *ocrp = MMC_R4(cmd.c_resp);
582
583 DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
584
585 return error;
586 }
587
588 /*
589 * Card interrupt handling
590 */
591
592 void
593 sdmmc_intr_enable(struct sdmmc_function *sf)
594 {
595 struct sdmmc_softc *sc = sf->sc;
596 struct sdmmc_function *sf0 = sc->sc_fn0;
597 uint8_t reg;
598
599 SDMMC_LOCK(sc);
600 mutex_enter(&sc->sc_intr_task_mtx);
601 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
602 reg |= 1 << sf->number;
603 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
604 mutex_exit(&sc->sc_intr_task_mtx);
605 SDMMC_UNLOCK(sc);
606 }
607
608 void
609 sdmmc_intr_disable(struct sdmmc_function *sf)
610 {
611 struct sdmmc_softc *sc = sf->sc;
612 struct sdmmc_function *sf0 = sc->sc_fn0;
613 uint8_t reg;
614
615 SDMMC_LOCK(sc);
616 mutex_enter(&sc->sc_intr_task_mtx);
617 reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
618 reg &= ~(1 << sf->number);
619 sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
620 mutex_exit(&sc->sc_intr_task_mtx);
621 SDMMC_UNLOCK(sc);
622 }
623
624 /*
625 * Establish a handler for the SDIO card interrupt. Because the
626 * interrupt may be shared with different SDIO functions, multiple
627 * handlers can be established.
628 */
629 void *
630 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
631 const char *name)
632 {
633 struct sdmmc_softc *sc = device_private(dev);
634 struct sdmmc_intr_handler *ih;
635
636 if (sc->sc_sct->card_enable_intr == NULL)
637 return NULL;
638
639 ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
640 if (ih == NULL)
641 return NULL;
642
643 ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
644 if (ih->ih_name == NULL) {
645 free(ih, M_DEVBUF);
646 return NULL;
647 }
648 strlcpy(ih->ih_name, name, strlen(name));
649 ih->ih_softc = sc;
650 ih->ih_fun = fun;
651 ih->ih_arg = arg;
652
653 mutex_enter(&sc->sc_mtx);
654 if (TAILQ_EMPTY(&sc->sc_intrq)) {
655 sdmmc_intr_enable(sc->sc_fn0);
656 sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
657 }
658 TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
659 mutex_exit(&sc->sc_mtx);
660
661 return ih;
662 }
663
664 /*
665 * Disestablish the given handler.
666 */
667 void
668 sdmmc_intr_disestablish(void *cookie)
669 {
670 struct sdmmc_intr_handler *ih = cookie;
671 struct sdmmc_softc *sc = ih->ih_softc;
672
673 if (sc->sc_sct->card_enable_intr == NULL)
674 return;
675
676 mutex_enter(&sc->sc_mtx);
677 TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
678 if (TAILQ_EMPTY(&sc->sc_intrq)) {
679 sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
680 sdmmc_intr_disable(sc->sc_fn0);
681 }
682 mutex_exit(&sc->sc_mtx);
683
684 free(ih->ih_name, M_DEVBUF);
685 free(ih, M_DEVBUF);
686 }
687
688 /*
689 * Call established SDIO card interrupt handlers. The host controller
690 * must call this function from its own interrupt handler to handle an
691 * SDIO interrupt from the card.
692 */
693 void
694 sdmmc_card_intr(device_t dev)
695 {
696 struct sdmmc_softc *sc = device_private(dev);
697
698 if (sc->sc_sct->card_enable_intr == NULL)
699 return;
700
701 mutex_enter(&sc->sc_intr_task_mtx);
702 if (!sdmmc_task_pending(&sc->sc_intr_task))
703 sdmmc_add_task(sc, &sc->sc_intr_task);
704 mutex_exit(&sc->sc_intr_task_mtx);
705 }
706
707 void
708 sdmmc_intr_task(void *arg)
709 {
710 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
711 struct sdmmc_intr_handler *ih;
712
713 mutex_enter(&sc->sc_mtx);
714 TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
715 /* XXX examine return value and do evcount stuff*/
716 (void)(*ih->ih_fun)(ih->ih_arg);
717 }
718 mutex_exit(&sc->sc_mtx);
719
720 sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
721 }
722