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_func.h>
25 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
27 struct sdio_func *func;
29 BUG_ON(fn > SDIO_MAX_FUNCS);
31 func = sdio_alloc_func(card);
37 card->sdio_func[fn - 1] = func;
43 * Host is being removed. Free up the current card.
45 static void mmc_sdio_remove(struct mmc_host *host)
52 for (i = 0;i < host->card->sdio_funcs;i++) {
53 if (host->card->sdio_func[i]) {
54 sdio_remove_func(host->card->sdio_func[i]);
55 host->card->sdio_func[i] = NULL;
59 mmc_remove_card(host->card);
64 * Card detection callback from host.
66 static void mmc_sdio_detect(struct mmc_host *host)
76 * Just check if our card has been removed.
78 err = mmc_select_card(host->card);
80 mmc_release_host(host);
83 mmc_sdio_remove(host);
87 mmc_release_host(host);
92 static const struct mmc_bus_ops mmc_sdio_ops = {
93 .remove = mmc_sdio_remove,
94 .detect = mmc_sdio_detect,
99 * Starting point for SDIO card init.
101 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
105 struct mmc_card *card;
108 BUG_ON(!host->claimed);
110 mmc_attach_bus(host, &mmc_sdio_ops);
113 * Sanity check the voltages that the card claims to
117 printk(KERN_WARNING "%s: card claims to support voltages "
118 "below the defined range. These will be ignored.\n",
123 if (ocr & MMC_VDD_165_195) {
124 printk(KERN_WARNING "%s: SDIO card claims to support the "
125 "incompletely defined 'low voltage range'. This "
126 "will be ignored.\n", mmc_hostname(host));
127 ocr &= ~MMC_VDD_165_195;
130 host->ocr = mmc_select_voltage(host, ocr);
133 * Can we support the voltage(s) of the card(s)?
141 * Inform the card of the voltage
143 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
148 * The number of functions on the card is encoded inside
151 funcs = (ocr & 0x70000000) >> 28;
154 * Allocate card structure.
156 card = mmc_alloc_card(host);
162 card->type = MMC_TYPE_SDIO;
163 card->sdio_funcs = funcs;
170 err = mmc_send_relative_addr(host, &card->rca);
174 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
177 * Select card, as all following commands rely on that.
179 err = mmc_select_card(card);
184 * Initialize (but don't add) all present functions.
186 for (i = 0;i < funcs;i++) {
187 err = sdio_init_func(host->card, i + 1);
192 mmc_release_host(host);
195 * First add the card to the driver model...
197 err = mmc_add_card(host->card);
202 * ...then the SDIO functions.
204 for (i = 0;i < funcs;i++) {
205 err = sdio_add_func(host->card->sdio_func[i]);
214 /* Remove without lock if the device has been added. */
215 mmc_sdio_remove(host);
216 mmc_claim_host(host);
218 /* And with lock if it hasn't been added. */
220 mmc_sdio_remove(host);
222 mmc_detach_bus(host);
223 mmc_release_host(host);
225 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
226 mmc_hostname(host), err);