2 * linux/drivers/mmc/sdio.c
4 * Copyright 2006-2007 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/err.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sdio_func.h>
26 static int sdio_init_func(struct mmc_card
*card
, unsigned int fn
)
28 struct sdio_func
*func
;
30 BUG_ON(fn
> SDIO_MAX_FUNCS
);
32 func
= sdio_alloc_func(card
);
38 card
->sdio_func
[fn
- 1] = func
;
43 static int sdio_read_cccr(struct mmc_card
*card
)
49 memset(&card
->cccr
, 0, sizeof(struct sdio_cccr
));
51 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CCCR
, 0, &data
);
55 cccr_vsn
= data
& 0x0f;
57 if (cccr_vsn
> SDIO_CCCR_REV_1_20
) {
58 printk(KERN_ERR
"%s: unrecognised CCCR structure version %d\n",
59 mmc_hostname(card
->host
), cccr_vsn
);
63 card
->cccr
.sdio_vsn
= (data
& 0xf0) >> 4;
65 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_CAPS
, 0, &data
);
69 if (data
& SDIO_CCCR_CAP_SMB
)
70 card
->cccr
.multi_block
= 1;
71 if (data
& SDIO_CCCR_CAP_LSC
)
72 card
->cccr
.low_speed
= 1;
73 if (data
& SDIO_CCCR_CAP_4BLS
)
74 card
->cccr
.wide_bus
= 1;
76 if (cccr_vsn
>= SDIO_CCCR_REV_1_10
) {
77 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_POWER
, 0, &data
);
81 if (data
& SDIO_POWER_SMPC
)
82 card
->cccr
.high_power
= 1;
85 if (cccr_vsn
>= SDIO_CCCR_REV_1_20
) {
86 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &data
);
90 if (data
& SDIO_SPEED_SHS
)
91 card
->cccr
.high_speed
= 1;
99 * Host is being removed. Free up the current card.
101 static void mmc_sdio_remove(struct mmc_host
*host
)
108 for (i
= 0;i
< host
->card
->sdio_funcs
;i
++) {
109 if (host
->card
->sdio_func
[i
]) {
110 sdio_remove_func(host
->card
->sdio_func
[i
]);
111 host
->card
->sdio_func
[i
] = NULL
;
115 mmc_remove_card(host
->card
);
120 * Card detection callback from host.
122 static void mmc_sdio_detect(struct mmc_host
*host
)
129 mmc_claim_host(host
);
132 * Just check if our card has been removed.
134 err
= mmc_select_card(host
->card
);
136 mmc_release_host(host
);
139 mmc_sdio_remove(host
);
141 mmc_claim_host(host
);
142 mmc_detach_bus(host
);
143 mmc_release_host(host
);
148 static const struct mmc_bus_ops mmc_sdio_ops
= {
149 .remove
= mmc_sdio_remove
,
150 .detect
= mmc_sdio_detect
,
155 * Starting point for SDIO card init.
157 int mmc_attach_sdio(struct mmc_host
*host
, u32 ocr
)
161 struct mmc_card
*card
;
164 BUG_ON(!host
->claimed
);
166 mmc_attach_bus(host
, &mmc_sdio_ops
);
169 * Sanity check the voltages that the card claims to
173 printk(KERN_WARNING
"%s: card claims to support voltages "
174 "below the defined range. These will be ignored.\n",
179 if (ocr
& MMC_VDD_165_195
) {
180 printk(KERN_WARNING
"%s: SDIO card claims to support the "
181 "incompletely defined 'low voltage range'. This "
182 "will be ignored.\n", mmc_hostname(host
));
183 ocr
&= ~MMC_VDD_165_195
;
186 host
->ocr
= mmc_select_voltage(host
, ocr
);
189 * Can we support the voltage(s) of the card(s)?
197 * Inform the card of the voltage
199 err
= mmc_send_io_op_cond(host
, host
->ocr
, &ocr
);
204 * The number of functions on the card is encoded inside
207 funcs
= (ocr
& 0x70000000) >> 28;
210 * Allocate card structure.
212 card
= mmc_alloc_card(host
);
218 card
->type
= MMC_TYPE_SDIO
;
219 card
->sdio_funcs
= funcs
;
226 err
= mmc_send_relative_addr(host
, &card
->rca
);
230 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
233 * Select card, as all following commands rely on that.
235 err
= mmc_select_card(card
);
240 * Read the common registers.
242 err
= sdio_read_cccr(card
);
247 * Initialize (but don't add) all present functions.
249 for (i
= 0;i
< funcs
;i
++) {
250 err
= sdio_init_func(host
->card
, i
+ 1);
255 mmc_release_host(host
);
258 * First add the card to the driver model...
260 err
= mmc_add_card(host
->card
);
265 * ...then the SDIO functions.
267 for (i
= 0;i
< funcs
;i
++) {
268 err
= sdio_add_func(host
->card
->sdio_func
[i
]);
277 /* Remove without lock if the device has been added. */
278 mmc_sdio_remove(host
);
279 mmc_claim_host(host
);
281 /* And with lock if it hasn't been added. */
283 mmc_sdio_remove(host
);
285 mmc_detach_bus(host
);
286 mmc_release_host(host
);
288 printk(KERN_ERR
"%s: error %d whilst initialising SDIO card\n",
289 mmc_hostname(host
), err
);