Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*	$NetBSD: sdmmc_io.c,v 1.14.4.2 2020/08/09 14:03:07 martin Exp $	*/
/*	$OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw Exp $	*/

/*
 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Routines for SD I/O cards. */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.14.4.2 2020/08/09 14:03:07 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_sdmmc.h"
#endif

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <dev/sdmmc/sdmmc_ioreg.h>
#include <dev/sdmmc/sdmmcchip.h>
#include <dev/sdmmc/sdmmcreg.h>
#include <dev/sdmmc/sdmmcvar.h>

#ifdef SDMMC_DEBUG
#define DPRINTF(s)	do { printf s; } while (0)
#else
#define DPRINTF(s)	do {} while (0)
#endif

struct sdmmc_intr_handler {
	struct sdmmc_softc *ih_softc;
	char *ih_name;
	int (*ih_fun)(void *);
	void *ih_arg;
	TAILQ_ENTRY(sdmmc_intr_handler) entry;
};

static int	sdmmc_io_rw_direct(struct sdmmc_softc *,
		    struct sdmmc_function *, int, u_char *, int, bool);
static int	sdmmc_io_rw_extended(struct sdmmc_softc *,
		    struct sdmmc_function *, int, u_char *, int, int);
#if 0
static int	sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
		    int, u_char *);
#endif
static void	sdmmc_io_reset(struct sdmmc_softc *);
static int	sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
		    uint32_t *);

/*
 * Initialize SD I/O card functions (before memory cards).  The host
 * system and controller must support card interrupts in order to use
 * I/O functions.
 */
int
sdmmc_io_enable(struct sdmmc_softc *sc)
{
	uint32_t host_ocr;
	uint32_t card_ocr;
	int error;

	SDMMC_LOCK(sc);

	/* Set host mode to SD "combo" card. */
	SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);

	/* Reset I/O functions. */
	sdmmc_io_reset(sc);

	/*
	 * Read the I/O OCR value, determine the number of I/O
	 * functions and whether memory is also present (a "combo
	 * card") by issuing CMD5.  SD memory-only and MMC cards
	 * do not respond to CMD5.
	 */
	error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
	if (error) {
		/* No SDIO card; switch to SD memory-only mode. */
		CLR(sc->sc_flags, SMF_IO_MODE);
		error = 0;
		goto out;
	}

	/* Parse the additional bits in the I/O OCR value. */
	if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
		/* SDIO card without memory (not a "combo card"). */
		DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
		CLR(sc->sc_flags, SMF_MEM_MODE);
	}
	sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
	if (sc->sc_function_count == 0) {
		/* Useless SDIO card without any I/O functions. */
		DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
		CLR(sc->sc_flags, SMF_IO_MODE);
		error = 0;
		goto out;
	}
	card_ocr &= SD_IO_OCR_MASK;

	/* Set the lowest voltage supported by the card and host. */
	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
	if (error) {
		aprint_error_dev(sc->sc_dev,
		    "couldn't supply voltage requested by card\n");
		goto out;
	}

	/* Send the new OCR value until all cards are ready. */
	error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
	if (error) {
		aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
		goto out;
	}

out:
	SDMMC_UNLOCK(sc);

	return error;
}

/*
 * Allocate sdmmc_function structures for SD card I/O function
 * (including function 0).
 */
void
sdmmc_io_scan(struct sdmmc_softc *sc)
{
	struct sdmmc_function *sf0, *sf;
	int error;
	int i;

	SDMMC_LOCK(sc);

	sf0 = sdmmc_function_alloc(sc);
	sf0->number = 0;
	error = sdmmc_set_relative_addr(sc, sf0);
	if (error) {
		aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
		SET(sf0->flags, SFF_ERROR);
		goto out;
	}
	sc->sc_fn0 = sf0;
	SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);

	/* Go to Data Transfer Mode, if possible. */
	sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);

	/* Verify that the RCA has been set by selecting the card. */
	error = sdmmc_select_card(sc, sf0);
	if (error) {
		aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
		    sf0->rca);
		SET(sf0->flags, SFF_ERROR);
		goto out;
	}

	for (i = 1; i <= sc->sc_function_count; i++) {
		sf = sdmmc_function_alloc(sc);
		sf->number = i;
		sf->rca = sf0->rca;
		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
	}

out:
	SDMMC_UNLOCK(sc);
}

/*
 * Initialize SDIO card functions.
 */
int
sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
{
	struct sdmmc_function *sf0 = sc->sc_fn0;
	int error = 0;
	uint8_t reg;

	SDMMC_LOCK(sc);

	sf->blklen = sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch);

	if (sf->number == 0) {
		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_CAPABILITY);
		if (!(reg & CCCR_CAPS_LSC) || (reg & CCCR_CAPS_4BLS)) {
			sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH,
			    CCCR_BUS_WIDTH_4);
			sf->width = 4;
			error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch,
			    sf->width);
			if (error)
				aprint_error_dev(sc->sc_dev,
				    "can't change bus width\n");
		}

		error = sdmmc_read_cis(sf, &sf->cis);
		if (error) {
			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
			SET(sf->flags, SFF_ERROR);
			goto out;
		}

		sdmmc_check_cis_quirks(sf);

#ifdef SDMMC_DEBUG
		if (sdmmcdebug)
			sdmmc_print_cis(sf);
#endif

		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_HIGH_SPEED);
		if (reg & CCCR_HIGH_SPEED_SHS) {
			reg |= CCCR_HIGH_SPEED_EHS;
			sdmmc_io_write_1(sf, SD_IO_CCCR_HIGH_SPEED, reg);
			sf->csd.tran_speed = 50000;	/* 50MHz */

			/* Wait 400KHz x 8 clock */
			sdmmc_delay(20);
		}
		if (sc->sc_busclk > sf->csd.tran_speed)
			sc->sc_busclk = sf->csd.tran_speed;
		error =
		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk,
			false);
		if (error)
			aprint_error_dev(sc->sc_dev,
			    "can't change bus clock\n");

		aprint_normal_dev(sc->sc_dev, "%u-bit width,", sf->width);
		if ((sc->sc_busclk / 1000) != 0)
			aprint_normal(" %u.%03u MHz\n",
			    sc->sc_busclk / 1000, sc->sc_busclk % 1000);
		else
			aprint_normal(" %u KHz\n", sc->sc_busclk % 1000);


	} else {
		reg = sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x000);
		sf->interface = FBR_STD_FUNC_IF_CODE(reg);
		if (sf->interface == 0x0f)
			sf->interface =
			    sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x001);
		error = sdmmc_read_cis(sf, &sf->cis);
		if (error) {
			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
			SET(sf->flags, SFF_ERROR);
			goto out;
		}

		sdmmc_check_cis_quirks(sf);

#ifdef SDMMC_DEBUG
		if (sdmmcdebug)
			sdmmc_print_cis(sf);
#endif
	}

out:
	SDMMC_UNLOCK(sc);

	return error;
}

/*
 * Indicate whether the function is ready to operate.
 */
static int
sdmmc_io_function_ready(struct sdmmc_function *sf)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	uint8_t reg;

	if (sf->number == 0)
		return 1;	/* FN0 is always ready */

	SDMMC_LOCK(sc);
	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
	SDMMC_UNLOCK(sc);
	return (reg & (1 << sf->number)) != 0;
}

int
sdmmc_io_function_enable(struct sdmmc_function *sf)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	uint8_t reg;
	int retry;

	if (sf->number == 0)
		return 0;	/* FN0 is always enabled */

	SDMMC_LOCK(sc);
	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
	SET(reg, (1U << sf->number));
	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
	SDMMC_UNLOCK(sc);

	retry = 5;
	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
		kpause("pause", false, hz, NULL);
	return (retry >= 0) ? 0 : ETIMEDOUT;
}

/*
 * Disable the I/O function.  Return zero if the function was
 * disabled successfully.
 */
void
sdmmc_io_function_disable(struct sdmmc_function *sf)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	uint8_t reg;

	if (sf->number == 0)
		return;		/* FN0 is always enabled */

	SDMMC_LOCK(sc);
	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
	CLR(reg, (1U << sf->number));
	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
	SDMMC_UNLOCK(sc);
}

static int
sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    int reg, u_char *datap, int arg, bool toutok)
{
	struct sdmmc_command cmd;
	int error;

	/* Don't lock */

	/* Make sure the card is selected. */
	error = sdmmc_select_card(sc, sf);
	if (error)
		return error;

	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
	    SD_ARG_CMD52_FUNC_SHIFT;
	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
	    SD_ARG_CMD52_REG_SHIFT;
	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
	    SD_ARG_CMD52_DATA_SHIFT;

	memset(&cmd, 0, sizeof cmd);
	cmd.c_opcode = SD_IO_RW_DIRECT;
	cmd.c_arg = arg;
	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
	if (toutok)
		cmd.c_flags |= SCF_TOUT_OK;

	error = sdmmc_mmc_command(sc, &cmd);
	if (error == 0)
		*datap = SD_R5_DATA(cmd.c_resp);

	if (error && error != ETIMEDOUT) {
		device_printf(sc->sc_dev,
		    "direct I/O error %d, r=%d p=%p %s\n",
		    error, reg, datap,
		    ISSET(arg, SD_ARG_CMD53_WRITE) ? "write" : "read");
	}

	return error;
}

/*
 * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
 * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
 * to access successive register locations instead of accessing the
 * same register many times.
 */
static int
sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    int reg, u_char *datap, int datalen, int arg)
{
	struct sdmmc_command cmd;
	int error;

	/* Don't lock */

#if 0
	/* Make sure the card is selected. */
	error = sdmmc_select_card(sc, sf);
	if (error)
		return error;
#endif

	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
	    SD_ARG_CMD53_FUNC_SHIFT;
	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
	    SD_ARG_CMD53_REG_SHIFT;
	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
	    SD_ARG_CMD53_LENGTH_SHIFT;

	memset(&cmd, 0, sizeof cmd);
	cmd.c_opcode = SD_IO_RW_EXTENDED;
	cmd.c_arg = arg;
	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R5;
	cmd.c_data = datap;
	cmd.c_datalen = datalen;
	cmd.c_blklen = MIN(datalen, sf->blklen);

	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
		cmd.c_flags |= SCF_CMD_READ;

	error = sdmmc_mmc_command(sc, &cmd);

	if (error) {
		device_printf(sc->sc_dev,
		    "extended I/O error %d, r=%d p=%p l=%d %s\n",
		    error, reg, datap, datalen,
		    ISSET(arg, SD_ARG_CMD53_WRITE) ? "write" : "read");
	}

	return error;
}

uint8_t
sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
{
	uint8_t data = 0;

	/* Don't lock */

	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
	    SD_ARG_CMD52_READ, false);
	return data;
}

void
sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
{

	/* Don't lock */

	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
	    SD_ARG_CMD52_WRITE, false);
}

uint16_t
sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
{
	uint16_t data = 0;

	/* Don't lock */

	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
	return data;
}

void
sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
{

	/* Don't lock */

	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
}

uint32_t
sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
{
	uint32_t data = 0;

	/* Don't lock */

	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
	return data;
}

void
sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
{

	/* Don't lock */

	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
}


int
sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    int datalen)
{
	int blocks, bytes, error = 0;

	/* Don't lock */

	while (datalen >= sf->blklen) {
		//blocks = imin(datalen / sf->blklen,
		//              SD_ARG_CMD53_LENGTH_MAX);
		blocks = 1;
		bytes = blocks * sf->blklen;
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
		    bytes, SD_ARG_CMD53_READ);
		if (error)
			goto error;
		data += bytes;
		datalen -= bytes;
	}

	if (datalen)
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
		    SD_ARG_CMD53_READ);
error:
	return error;
}

int
sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    int datalen)
{
	int blocks, bytes, error = 0;

	/* Don't lock */

	while (datalen >= sf->blklen) {
		//blocks = imin(datalen / sf->blklen,
		//             SD_ARG_CMD53_LENGTH_MAX);
		blocks = 1;
		bytes = blocks * sf->blklen;
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
		    bytes, SD_ARG_CMD53_WRITE);
		if (error)
			goto error;
		data += bytes;
		datalen -= bytes;
	}

	if (datalen)
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
		    SD_ARG_CMD53_WRITE);
error:
	return error;
}


int
sdmmc_io_read_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    int datalen)
{
	int blocks, bytes, error = 0;

	/* Don't lock */

	while (datalen >= sf->blklen) {
		//blocks = imin(datalen / sf->blklen,
		//              SD_ARG_CMD53_LENGTH_MAX);
		blocks = 1;
		bytes = blocks * sf->blklen;
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
		    bytes, SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
		if (error)
			goto error;
		reg += bytes;
		data += bytes;
		datalen -= bytes;
	}

	if (datalen)
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
		    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
error:
	return error;
}

int
sdmmc_io_write_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    int datalen)
{
	int blocks, bytes, error = 0;

	/* Don't lock */

	while (datalen >= sf->blklen) {
		//blocks = imin(datalen / sf->blklen,
		//              SD_ARG_CMD53_LENGTH_MAX);
		blocks = 1;
		bytes = blocks * sf->blklen;
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
		    bytes, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
		if (error)
			goto error;
		reg += bytes;
		data += bytes;
		datalen -= bytes;
	}

	if (datalen)
		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
		    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
error:
	return error;
}

#if 0
static int
sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    int reg, u_char *datap)
{

	/* Don't lock */

	return sdmmc_io_rw_direct(sc, sf, reg, datap,
	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE, false);
}
#endif

/*
 * Abort I/O function of the card
 */
int
sdmmc_io_function_abort(struct sdmmc_function *sf)
{
	u_char data = CCCR_CTL_AS(sf->number);

	return sdmmc_io_rw_direct(sf->sc, NULL, SD_IO_CCCR_CTL, &data,
	    SD_ARG_CMD52_WRITE, true);
}

/*
 * Reset the I/O functions of the card.
 */
static void
sdmmc_io_reset(struct sdmmc_softc *sc)
{
	u_char data = CCCR_CTL_RES;

	if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data,
	    SD_ARG_CMD52_WRITE, true) == 0)
		sdmmc_pause(100000, NULL); /* XXX SDMMC_LOCK */
}

/*
 * Get or set the card's I/O OCR value (SDIO).
 */
static int
sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
{
	struct sdmmc_command cmd;
	int error;
	int retry;

	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));

	/* Don't lock */

	/*
	 * If we change the OCR value, retry the command until the OCR
	 * we receive in response has the "CARD BUSY" bit set, meaning
	 * that all cards are ready for identification.
	 */
	for (retry = 0; retry < 100; retry++) {
		memset(&cmd, 0, sizeof cmd);
		cmd.c_opcode = SD_IO_SEND_OP_COND;
		cmd.c_arg = ocr;
		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;

		error = sdmmc_mmc_command(sc, &cmd);
		if (error)
			break;
		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
			break;

		error = ETIMEDOUT;
		sdmmc_pause(10000, NULL);
	}
	if (error == 0 && ocrp != NULL)
		*ocrp = MMC_R4(cmd.c_resp);

	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));

	return error;
}

/*
 * Card interrupt handling
 */

void
sdmmc_intr_enable(struct sdmmc_function *sf)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	uint8_t reg;

	SDMMC_LOCK(sc);
	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
	reg |= 1 << sf->number;
	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
	SDMMC_UNLOCK(sc);
}

void
sdmmc_intr_disable(struct sdmmc_function *sf)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	uint8_t reg;

	SDMMC_LOCK(sc);
	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
	reg &= ~(1 << sf->number);
	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
	SDMMC_UNLOCK(sc);
}

/*
 * Establish a handler for the SDIO card interrupt.  Because the
 * interrupt may be shared with different SDIO functions, multiple
 * handlers can be established.
 */
void *
sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    const char *name)
{
	struct sdmmc_softc *sc = device_private(dev);
	struct sdmmc_intr_handler *ih;

	if (sc->sc_sct->card_enable_intr == NULL)
		return NULL;

	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
	if (ih == NULL)
		return NULL;

	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
	if (ih->ih_name == NULL) {
		free(ih, M_DEVBUF);
		return NULL;
	}
	strlcpy(ih->ih_name, name, strlen(name));
	ih->ih_softc = sc;
	ih->ih_fun = fun;
	ih->ih_arg = arg;

	mutex_enter(&sc->sc_mtx);
	if (TAILQ_EMPTY(&sc->sc_intrq)) {
		sdmmc_intr_enable(sc->sc_fn0);
		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
	}
	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
	mutex_exit(&sc->sc_mtx);

	return ih;
}

/*
 * Disestablish the given handler.
 */
void
sdmmc_intr_disestablish(void *cookie)
{
	struct sdmmc_intr_handler *ih = cookie;
	struct sdmmc_softc *sc = ih->ih_softc;

	if (sc->sc_sct->card_enable_intr == NULL)
		return;

	mutex_enter(&sc->sc_mtx);
	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
	if (TAILQ_EMPTY(&sc->sc_intrq)) {
		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
		sdmmc_intr_disable(sc->sc_fn0);
	}
	mutex_exit(&sc->sc_mtx);

	free(ih->ih_name, M_DEVBUF);
	free(ih, M_DEVBUF);
}

/*
 * Call established SDIO card interrupt handlers.  The host controller
 * must call this function from its own interrupt handler to handle an
 * SDIO interrupt from the card.
 */
void
sdmmc_card_intr(device_t dev)
{
	struct sdmmc_softc *sc = device_private(dev);

	if (sc->sc_sct->card_enable_intr == NULL)
		return;

	sdmmc_add_task(sc, &sc->sc_intr_task);
}

void
sdmmc_intr_task(void *arg)
{
	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
	struct sdmmc_intr_handler *ih;

	mutex_enter(&sc->sc_mtx);
	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
		/* XXX examine return value and do evcount stuff*/
		(void)(*ih->ih_fun)(ih->ih_arg);
	}
	mutex_exit(&sc->sc_mtx);

	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
}

int
sdmmc_io_set_blocklen(struct sdmmc_function *sf,
     int blklen)
{
	struct sdmmc_softc *sc = sf->sc;
	struct sdmmc_function *sf0 = sc->sc_fn0;
	int error = EINVAL;

	SDMMC_LOCK(sc);

	if (blklen <= 0 ||
	    blklen > sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch))
		goto err;

	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
	    SD_IO_FBR_BLOCKLEN, blklen & 0xff);
	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
	    SD_IO_FBR_BLOCKLEN + 1, (blklen >> 8) & 0xff);

	sf->blklen = blklen;
	error = 0;

err:
	SDMMC_UNLOCK(sc);

	return error;
}