blob: 444328581cec1a2a7c0987abb3d0534e0386ecc1 [file] [log] [blame]
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001/*
2 * linux/drivers/mmc/sdio.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
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.
10 */
11
12#include <linux/err.h>
13
14#include <linux/mmc/host.h>
15#include <linux/mmc/card.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020016#include <linux/mmc/sdio_func.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020017
18#include "core.h"
19#include "bus.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020020#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020021#include "mmc_ops.h"
22#include "sd_ops.h"
23#include "sdio_ops.h"
24
Pierre Ossmane29a7d72007-05-26 13:48:18 +020025static int sdio_init_func(struct mmc_card *card, unsigned int fn)
26{
27 struct sdio_func *func;
28
29 BUG_ON(fn > SDIO_MAX_FUNCS);
30
31 func = sdio_alloc_func(card);
32 if (IS_ERR(func))
33 return PTR_ERR(func);
34
35 func->num = fn;
36
37 card->sdio_func[fn - 1] = func;
38
39 return 0;
40}
41
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020042/*
43 * Host is being removed. Free up the current card.
44 */
45static void mmc_sdio_remove(struct mmc_host *host)
46{
Pierre Ossmane29a7d72007-05-26 13:48:18 +020047 int i;
48
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020049 BUG_ON(!host);
50 BUG_ON(!host->card);
51
Pierre Ossmane29a7d72007-05-26 13:48:18 +020052 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;
56 }
57 }
58
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020059 mmc_remove_card(host->card);
60 host->card = NULL;
61}
62
63/*
64 * Card detection callback from host.
65 */
66static void mmc_sdio_detect(struct mmc_host *host)
67{
68 int err;
69
70 BUG_ON(!host);
71 BUG_ON(!host->card);
72
73 mmc_claim_host(host);
74
75 /*
76 * Just check if our card has been removed.
77 */
78 err = mmc_select_card(host->card);
79
80 mmc_release_host(host);
81
82 if (err) {
83 mmc_sdio_remove(host);
84
85 mmc_claim_host(host);
86 mmc_detach_bus(host);
87 mmc_release_host(host);
88 }
89}
90
91
92static const struct mmc_bus_ops mmc_sdio_ops = {
93 .remove = mmc_sdio_remove,
94 .detect = mmc_sdio_detect,
95};
96
97
98/*
99 * Starting point for SDIO card init.
100 */
101int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
102{
103 int err;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200104 int i, funcs;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200105 struct mmc_card *card;
106
107 BUG_ON(!host);
108 BUG_ON(!host->claimed);
109
110 mmc_attach_bus(host, &mmc_sdio_ops);
111
112 /*
113 * Sanity check the voltages that the card claims to
114 * support.
115 */
116 if (ocr & 0x7F) {
117 printk(KERN_WARNING "%s: card claims to support voltages "
118 "below the defined range. These will be ignored.\n",
119 mmc_hostname(host));
120 ocr &= ~0x7F;
121 }
122
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;
128 }
129
130 host->ocr = mmc_select_voltage(host, ocr);
131
132 /*
133 * Can we support the voltage(s) of the card(s)?
134 */
135 if (!host->ocr) {
136 err = -EINVAL;
137 goto err;
138 }
139
140 /*
141 * Inform the card of the voltage
142 */
143 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
144 if (err)
145 goto err;
146
147 /*
148 * The number of functions on the card is encoded inside
149 * the ocr.
150 */
151 funcs = (ocr & 0x70000000) >> 28;
152
153 /*
154 * Allocate card structure.
155 */
156 card = mmc_alloc_card(host);
157 if (IS_ERR(card)) {
158 err = PTR_ERR(card);
159 goto err;
160 }
161
162 card->type = MMC_TYPE_SDIO;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200163 card->sdio_funcs = funcs;
164
165 host->card = card;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200166
167 /*
168 * Set card RCA.
169 */
170 err = mmc_send_relative_addr(host, &card->rca);
171 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200172 goto remove;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200173
174 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
175
176 /*
177 * Select card, as all following commands rely on that.
178 */
179 err = mmc_select_card(card);
180 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200181 goto remove;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200182
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200183 /*
184 * Initialize (but don't add) all present functions.
185 */
186 for (i = 0;i < funcs;i++) {
187 err = sdio_init_func(host->card, i + 1);
188 if (err)
189 goto remove;
190 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200191
192 mmc_release_host(host);
193
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200194 /*
195 * First add the card to the driver model...
196 */
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200197 err = mmc_add_card(host->card);
198 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200199 goto remove_added;
200
201 /*
202 * ...then the SDIO functions.
203 */
204 for (i = 0;i < funcs;i++) {
205 err = sdio_add_func(host->card->sdio_func[i]);
206 if (err)
207 goto remove_added;
208 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200209
210 return 0;
211
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200212
213remove_added:
214 /* Remove without lock if the device has been added. */
215 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200216 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200217remove:
218 /* And with lock if it hasn't been added. */
219 if (host->card)
220 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200221err:
222 mmc_detach_bus(host);
223 mmc_release_host(host);
224
225 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
226 mmc_hostname(host), err);
227
228 return err;
229}
230