Commit 7f7e4129c23f0419257184dff6fec89d2d5a8964

Authored by Ulf Hansson
Committed by Chris Ball
1 parent 46cbd56ad4

mmc: core: Fix hangs related to insert/remove of cards

During a rescan operation mmc_attach(sd|mmc|sdio) functions are
called. The error handling in these function can trigger a detach
of the bus, which also meant a power off. This is not notified by
the rescan operation which then continues to the next attach function.

If a power off has been done, the framework must never send any
new commands to the host driver, without first doing a new power up.
This will most likely trigger any host driver to hang.

Moving power off out of detach and instead handle power off
separately when it is actually needed, solves the issue.

Signed-off-by: Ulf Hansson <ulf.hansson@stericsson.com>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
Cc: <stable@kernel.org>
Signed-off-by: Chris Ball <cjb@laptop.org>

Showing 5 changed files with 9 additions and 5 deletions Inline Diff

drivers/mmc/core/core.c
1 /* 1 /*
2 * linux/drivers/mmc/core/core.c 2 * linux/drivers/mmc/core/core.c
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. 6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as 10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. 11 * published by the Free Software Foundation.
12 */ 12 */
13 #include <linux/module.h> 13 #include <linux/module.h>
14 #include <linux/init.h> 14 #include <linux/init.h>
15 #include <linux/interrupt.h> 15 #include <linux/interrupt.h>
16 #include <linux/completion.h> 16 #include <linux/completion.h>
17 #include <linux/device.h> 17 #include <linux/device.h>
18 #include <linux/delay.h> 18 #include <linux/delay.h>
19 #include <linux/pagemap.h> 19 #include <linux/pagemap.h>
20 #include <linux/err.h> 20 #include <linux/err.h>
21 #include <linux/leds.h> 21 #include <linux/leds.h>
22 #include <linux/scatterlist.h> 22 #include <linux/scatterlist.h>
23 #include <linux/log2.h> 23 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h> 24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_runtime.h> 25 #include <linux/pm_runtime.h>
26 #include <linux/suspend.h> 26 #include <linux/suspend.h>
27 #include <linux/fault-inject.h> 27 #include <linux/fault-inject.h>
28 #include <linux/random.h> 28 #include <linux/random.h>
29 29
30 #include <linux/mmc/card.h> 30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h> 31 #include <linux/mmc/host.h>
32 #include <linux/mmc/mmc.h> 32 #include <linux/mmc/mmc.h>
33 #include <linux/mmc/sd.h> 33 #include <linux/mmc/sd.h>
34 34
35 #include "core.h" 35 #include "core.h"
36 #include "bus.h" 36 #include "bus.h"
37 #include "host.h" 37 #include "host.h"
38 #include "sdio_bus.h" 38 #include "sdio_bus.h"
39 39
40 #include "mmc_ops.h" 40 #include "mmc_ops.h"
41 #include "sd_ops.h" 41 #include "sd_ops.h"
42 #include "sdio_ops.h" 42 #include "sdio_ops.h"
43 43
44 static struct workqueue_struct *workqueue; 44 static struct workqueue_struct *workqueue;
45 45
46 /* 46 /*
47 * Enabling software CRCs on the data blocks can be a significant (30%) 47 * Enabling software CRCs on the data blocks can be a significant (30%)
48 * performance cost, and for other reasons may not always be desired. 48 * performance cost, and for other reasons may not always be desired.
49 * So we allow it it to be disabled. 49 * So we allow it it to be disabled.
50 */ 50 */
51 int use_spi_crc = 1; 51 int use_spi_crc = 1;
52 module_param(use_spi_crc, bool, 0); 52 module_param(use_spi_crc, bool, 0);
53 53
54 /* 54 /*
55 * We normally treat cards as removed during suspend if they are not 55 * We normally treat cards as removed during suspend if they are not
56 * known to be on a non-removable bus, to avoid the risk of writing 56 * known to be on a non-removable bus, to avoid the risk of writing
57 * back data to a different card after resume. Allow this to be 57 * back data to a different card after resume. Allow this to be
58 * overridden if necessary. 58 * overridden if necessary.
59 */ 59 */
60 #ifdef CONFIG_MMC_UNSAFE_RESUME 60 #ifdef CONFIG_MMC_UNSAFE_RESUME
61 int mmc_assume_removable; 61 int mmc_assume_removable;
62 #else 62 #else
63 int mmc_assume_removable = 1; 63 int mmc_assume_removable = 1;
64 #endif 64 #endif
65 EXPORT_SYMBOL(mmc_assume_removable); 65 EXPORT_SYMBOL(mmc_assume_removable);
66 module_param_named(removable, mmc_assume_removable, bool, 0644); 66 module_param_named(removable, mmc_assume_removable, bool, 0644);
67 MODULE_PARM_DESC( 67 MODULE_PARM_DESC(
68 removable, 68 removable,
69 "MMC/SD cards are removable and may be removed during suspend"); 69 "MMC/SD cards are removable and may be removed during suspend");
70 70
71 /* 71 /*
72 * Internal function. Schedule delayed work in the MMC work queue. 72 * Internal function. Schedule delayed work in the MMC work queue.
73 */ 73 */
74 static int mmc_schedule_delayed_work(struct delayed_work *work, 74 static int mmc_schedule_delayed_work(struct delayed_work *work,
75 unsigned long delay) 75 unsigned long delay)
76 { 76 {
77 return queue_delayed_work(workqueue, work, delay); 77 return queue_delayed_work(workqueue, work, delay);
78 } 78 }
79 79
80 /* 80 /*
81 * Internal function. Flush all scheduled work from the MMC work queue. 81 * Internal function. Flush all scheduled work from the MMC work queue.
82 */ 82 */
83 static void mmc_flush_scheduled_work(void) 83 static void mmc_flush_scheduled_work(void)
84 { 84 {
85 flush_workqueue(workqueue); 85 flush_workqueue(workqueue);
86 } 86 }
87 87
88 #ifdef CONFIG_FAIL_MMC_REQUEST 88 #ifdef CONFIG_FAIL_MMC_REQUEST
89 89
90 /* 90 /*
91 * Internal function. Inject random data errors. 91 * Internal function. Inject random data errors.
92 * If mmc_data is NULL no errors are injected. 92 * If mmc_data is NULL no errors are injected.
93 */ 93 */
94 static void mmc_should_fail_request(struct mmc_host *host, 94 static void mmc_should_fail_request(struct mmc_host *host,
95 struct mmc_request *mrq) 95 struct mmc_request *mrq)
96 { 96 {
97 struct mmc_command *cmd = mrq->cmd; 97 struct mmc_command *cmd = mrq->cmd;
98 struct mmc_data *data = mrq->data; 98 struct mmc_data *data = mrq->data;
99 static const int data_errors[] = { 99 static const int data_errors[] = {
100 -ETIMEDOUT, 100 -ETIMEDOUT,
101 -EILSEQ, 101 -EILSEQ,
102 -EIO, 102 -EIO,
103 }; 103 };
104 104
105 if (!data) 105 if (!data)
106 return; 106 return;
107 107
108 if (cmd->error || data->error || 108 if (cmd->error || data->error ||
109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) 109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
110 return; 110 return;
111 111
112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)]; 112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9; 113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
114 } 114 }
115 115
116 #else /* CONFIG_FAIL_MMC_REQUEST */ 116 #else /* CONFIG_FAIL_MMC_REQUEST */
117 117
118 static inline void mmc_should_fail_request(struct mmc_host *host, 118 static inline void mmc_should_fail_request(struct mmc_host *host,
119 struct mmc_request *mrq) 119 struct mmc_request *mrq)
120 { 120 {
121 } 121 }
122 122
123 #endif /* CONFIG_FAIL_MMC_REQUEST */ 123 #endif /* CONFIG_FAIL_MMC_REQUEST */
124 124
125 /** 125 /**
126 * mmc_request_done - finish processing an MMC request 126 * mmc_request_done - finish processing an MMC request
127 * @host: MMC host which completed request 127 * @host: MMC host which completed request
128 * @mrq: MMC request which request 128 * @mrq: MMC request which request
129 * 129 *
130 * MMC drivers should call this function when they have completed 130 * MMC drivers should call this function when they have completed
131 * their processing of a request. 131 * their processing of a request.
132 */ 132 */
133 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) 133 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
134 { 134 {
135 struct mmc_command *cmd = mrq->cmd; 135 struct mmc_command *cmd = mrq->cmd;
136 int err = cmd->error; 136 int err = cmd->error;
137 137
138 if (err && cmd->retries && mmc_host_is_spi(host)) { 138 if (err && cmd->retries && mmc_host_is_spi(host)) {
139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) 139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
140 cmd->retries = 0; 140 cmd->retries = 0;
141 } 141 }
142 142
143 if (err && cmd->retries) { 143 if (err && cmd->retries) {
144 pr_debug("%s: req failed (CMD%u): %d, retrying...\n", 144 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
145 mmc_hostname(host), cmd->opcode, err); 145 mmc_hostname(host), cmd->opcode, err);
146 146
147 cmd->retries--; 147 cmd->retries--;
148 cmd->error = 0; 148 cmd->error = 0;
149 host->ops->request(host, mrq); 149 host->ops->request(host, mrq);
150 } else { 150 } else {
151 mmc_should_fail_request(host, mrq); 151 mmc_should_fail_request(host, mrq);
152 152
153 led_trigger_event(host->led, LED_OFF); 153 led_trigger_event(host->led, LED_OFF);
154 154
155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", 155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
156 mmc_hostname(host), cmd->opcode, err, 156 mmc_hostname(host), cmd->opcode, err,
157 cmd->resp[0], cmd->resp[1], 157 cmd->resp[0], cmd->resp[1],
158 cmd->resp[2], cmd->resp[3]); 158 cmd->resp[2], cmd->resp[3]);
159 159
160 if (mrq->data) { 160 if (mrq->data) {
161 pr_debug("%s: %d bytes transferred: %d\n", 161 pr_debug("%s: %d bytes transferred: %d\n",
162 mmc_hostname(host), 162 mmc_hostname(host),
163 mrq->data->bytes_xfered, mrq->data->error); 163 mrq->data->bytes_xfered, mrq->data->error);
164 } 164 }
165 165
166 if (mrq->stop) { 166 if (mrq->stop) {
167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", 167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), mrq->stop->opcode, 168 mmc_hostname(host), mrq->stop->opcode,
169 mrq->stop->error, 169 mrq->stop->error,
170 mrq->stop->resp[0], mrq->stop->resp[1], 170 mrq->stop->resp[0], mrq->stop->resp[1],
171 mrq->stop->resp[2], mrq->stop->resp[3]); 171 mrq->stop->resp[2], mrq->stop->resp[3]);
172 } 172 }
173 173
174 if (mrq->done) 174 if (mrq->done)
175 mrq->done(mrq); 175 mrq->done(mrq);
176 176
177 mmc_host_clk_release(host); 177 mmc_host_clk_release(host);
178 } 178 }
179 } 179 }
180 180
181 EXPORT_SYMBOL(mmc_request_done); 181 EXPORT_SYMBOL(mmc_request_done);
182 182
183 static void 183 static void
184 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) 184 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
185 { 185 {
186 #ifdef CONFIG_MMC_DEBUG 186 #ifdef CONFIG_MMC_DEBUG
187 unsigned int i, sz; 187 unsigned int i, sz;
188 struct scatterlist *sg; 188 struct scatterlist *sg;
189 #endif 189 #endif
190 190
191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n", 191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
192 mmc_hostname(host), mrq->cmd->opcode, 192 mmc_hostname(host), mrq->cmd->opcode,
193 mrq->cmd->arg, mrq->cmd->flags); 193 mrq->cmd->arg, mrq->cmd->flags);
194 194
195 if (mrq->data) { 195 if (mrq->data) {
196 pr_debug("%s: blksz %d blocks %d flags %08x " 196 pr_debug("%s: blksz %d blocks %d flags %08x "
197 "tsac %d ms nsac %d\n", 197 "tsac %d ms nsac %d\n",
198 mmc_hostname(host), mrq->data->blksz, 198 mmc_hostname(host), mrq->data->blksz,
199 mrq->data->blocks, mrq->data->flags, 199 mrq->data->blocks, mrq->data->flags,
200 mrq->data->timeout_ns / 1000000, 200 mrq->data->timeout_ns / 1000000,
201 mrq->data->timeout_clks); 201 mrq->data->timeout_clks);
202 } 202 }
203 203
204 if (mrq->stop) { 204 if (mrq->stop) {
205 pr_debug("%s: CMD%u arg %08x flags %08x\n", 205 pr_debug("%s: CMD%u arg %08x flags %08x\n",
206 mmc_hostname(host), mrq->stop->opcode, 206 mmc_hostname(host), mrq->stop->opcode,
207 mrq->stop->arg, mrq->stop->flags); 207 mrq->stop->arg, mrq->stop->flags);
208 } 208 }
209 209
210 WARN_ON(!host->claimed); 210 WARN_ON(!host->claimed);
211 211
212 mrq->cmd->error = 0; 212 mrq->cmd->error = 0;
213 mrq->cmd->mrq = mrq; 213 mrq->cmd->mrq = mrq;
214 if (mrq->data) { 214 if (mrq->data) {
215 BUG_ON(mrq->data->blksz > host->max_blk_size); 215 BUG_ON(mrq->data->blksz > host->max_blk_size);
216 BUG_ON(mrq->data->blocks > host->max_blk_count); 216 BUG_ON(mrq->data->blocks > host->max_blk_count);
217 BUG_ON(mrq->data->blocks * mrq->data->blksz > 217 BUG_ON(mrq->data->blocks * mrq->data->blksz >
218 host->max_req_size); 218 host->max_req_size);
219 219
220 #ifdef CONFIG_MMC_DEBUG 220 #ifdef CONFIG_MMC_DEBUG
221 sz = 0; 221 sz = 0;
222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) 222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
223 sz += sg->length; 223 sz += sg->length;
224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); 224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
225 #endif 225 #endif
226 226
227 mrq->cmd->data = mrq->data; 227 mrq->cmd->data = mrq->data;
228 mrq->data->error = 0; 228 mrq->data->error = 0;
229 mrq->data->mrq = mrq; 229 mrq->data->mrq = mrq;
230 if (mrq->stop) { 230 if (mrq->stop) {
231 mrq->data->stop = mrq->stop; 231 mrq->data->stop = mrq->stop;
232 mrq->stop->error = 0; 232 mrq->stop->error = 0;
233 mrq->stop->mrq = mrq; 233 mrq->stop->mrq = mrq;
234 } 234 }
235 } 235 }
236 mmc_host_clk_hold(host); 236 mmc_host_clk_hold(host);
237 led_trigger_event(host->led, LED_FULL); 237 led_trigger_event(host->led, LED_FULL);
238 host->ops->request(host, mrq); 238 host->ops->request(host, mrq);
239 } 239 }
240 240
241 static void mmc_wait_done(struct mmc_request *mrq) 241 static void mmc_wait_done(struct mmc_request *mrq)
242 { 242 {
243 complete(&mrq->completion); 243 complete(&mrq->completion);
244 } 244 }
245 245
246 static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) 246 static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
247 { 247 {
248 init_completion(&mrq->completion); 248 init_completion(&mrq->completion);
249 mrq->done = mmc_wait_done; 249 mrq->done = mmc_wait_done;
250 mmc_start_request(host, mrq); 250 mmc_start_request(host, mrq);
251 } 251 }
252 252
253 static void mmc_wait_for_req_done(struct mmc_host *host, 253 static void mmc_wait_for_req_done(struct mmc_host *host,
254 struct mmc_request *mrq) 254 struct mmc_request *mrq)
255 { 255 {
256 wait_for_completion(&mrq->completion); 256 wait_for_completion(&mrq->completion);
257 } 257 }
258 258
259 /** 259 /**
260 * mmc_pre_req - Prepare for a new request 260 * mmc_pre_req - Prepare for a new request
261 * @host: MMC host to prepare command 261 * @host: MMC host to prepare command
262 * @mrq: MMC request to prepare for 262 * @mrq: MMC request to prepare for
263 * @is_first_req: true if there is no previous started request 263 * @is_first_req: true if there is no previous started request
264 * that may run in parellel to this call, otherwise false 264 * that may run in parellel to this call, otherwise false
265 * 265 *
266 * mmc_pre_req() is called in prior to mmc_start_req() to let 266 * mmc_pre_req() is called in prior to mmc_start_req() to let
267 * host prepare for the new request. Preparation of a request may be 267 * host prepare for the new request. Preparation of a request may be
268 * performed while another request is running on the host. 268 * performed while another request is running on the host.
269 */ 269 */
270 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq, 270 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
271 bool is_first_req) 271 bool is_first_req)
272 { 272 {
273 if (host->ops->pre_req) 273 if (host->ops->pre_req)
274 host->ops->pre_req(host, mrq, is_first_req); 274 host->ops->pre_req(host, mrq, is_first_req);
275 } 275 }
276 276
277 /** 277 /**
278 * mmc_post_req - Post process a completed request 278 * mmc_post_req - Post process a completed request
279 * @host: MMC host to post process command 279 * @host: MMC host to post process command
280 * @mrq: MMC request to post process for 280 * @mrq: MMC request to post process for
281 * @err: Error, if non zero, clean up any resources made in pre_req 281 * @err: Error, if non zero, clean up any resources made in pre_req
282 * 282 *
283 * Let the host post process a completed request. Post processing of 283 * Let the host post process a completed request. Post processing of
284 * a request may be performed while another reuqest is running. 284 * a request may be performed while another reuqest is running.
285 */ 285 */
286 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq, 286 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
287 int err) 287 int err)
288 { 288 {
289 if (host->ops->post_req) 289 if (host->ops->post_req)
290 host->ops->post_req(host, mrq, err); 290 host->ops->post_req(host, mrq, err);
291 } 291 }
292 292
293 /** 293 /**
294 * mmc_start_req - start a non-blocking request 294 * mmc_start_req - start a non-blocking request
295 * @host: MMC host to start command 295 * @host: MMC host to start command
296 * @areq: async request to start 296 * @areq: async request to start
297 * @error: out parameter returns 0 for success, otherwise non zero 297 * @error: out parameter returns 0 for success, otherwise non zero
298 * 298 *
299 * Start a new MMC custom command request for a host. 299 * Start a new MMC custom command request for a host.
300 * If there is on ongoing async request wait for completion 300 * If there is on ongoing async request wait for completion
301 * of that request and start the new one and return. 301 * of that request and start the new one and return.
302 * Does not wait for the new request to complete. 302 * Does not wait for the new request to complete.
303 * 303 *
304 * Returns the completed request, NULL in case of none completed. 304 * Returns the completed request, NULL in case of none completed.
305 * Wait for the an ongoing request (previoulsy started) to complete and 305 * Wait for the an ongoing request (previoulsy started) to complete and
306 * return the completed request. If there is no ongoing request, NULL 306 * return the completed request. If there is no ongoing request, NULL
307 * is returned without waiting. NULL is not an error condition. 307 * is returned without waiting. NULL is not an error condition.
308 */ 308 */
309 struct mmc_async_req *mmc_start_req(struct mmc_host *host, 309 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
310 struct mmc_async_req *areq, int *error) 310 struct mmc_async_req *areq, int *error)
311 { 311 {
312 int err = 0; 312 int err = 0;
313 struct mmc_async_req *data = host->areq; 313 struct mmc_async_req *data = host->areq;
314 314
315 /* Prepare a new request */ 315 /* Prepare a new request */
316 if (areq) 316 if (areq)
317 mmc_pre_req(host, areq->mrq, !host->areq); 317 mmc_pre_req(host, areq->mrq, !host->areq);
318 318
319 if (host->areq) { 319 if (host->areq) {
320 mmc_wait_for_req_done(host, host->areq->mrq); 320 mmc_wait_for_req_done(host, host->areq->mrq);
321 err = host->areq->err_check(host->card, host->areq); 321 err = host->areq->err_check(host->card, host->areq);
322 if (err) { 322 if (err) {
323 mmc_post_req(host, host->areq->mrq, 0); 323 mmc_post_req(host, host->areq->mrq, 0);
324 if (areq) 324 if (areq)
325 mmc_post_req(host, areq->mrq, -EINVAL); 325 mmc_post_req(host, areq->mrq, -EINVAL);
326 326
327 host->areq = NULL; 327 host->areq = NULL;
328 goto out; 328 goto out;
329 } 329 }
330 } 330 }
331 331
332 if (areq) 332 if (areq)
333 __mmc_start_req(host, areq->mrq); 333 __mmc_start_req(host, areq->mrq);
334 334
335 if (host->areq) 335 if (host->areq)
336 mmc_post_req(host, host->areq->mrq, 0); 336 mmc_post_req(host, host->areq->mrq, 0);
337 337
338 host->areq = areq; 338 host->areq = areq;
339 out: 339 out:
340 if (error) 340 if (error)
341 *error = err; 341 *error = err;
342 return data; 342 return data;
343 } 343 }
344 EXPORT_SYMBOL(mmc_start_req); 344 EXPORT_SYMBOL(mmc_start_req);
345 345
346 /** 346 /**
347 * mmc_wait_for_req - start a request and wait for completion 347 * mmc_wait_for_req - start a request and wait for completion
348 * @host: MMC host to start command 348 * @host: MMC host to start command
349 * @mrq: MMC request to start 349 * @mrq: MMC request to start
350 * 350 *
351 * Start a new MMC custom command request for a host, and wait 351 * Start a new MMC custom command request for a host, and wait
352 * for the command to complete. Does not attempt to parse the 352 * for the command to complete. Does not attempt to parse the
353 * response. 353 * response.
354 */ 354 */
355 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) 355 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
356 { 356 {
357 __mmc_start_req(host, mrq); 357 __mmc_start_req(host, mrq);
358 mmc_wait_for_req_done(host, mrq); 358 mmc_wait_for_req_done(host, mrq);
359 } 359 }
360 EXPORT_SYMBOL(mmc_wait_for_req); 360 EXPORT_SYMBOL(mmc_wait_for_req);
361 361
362 /** 362 /**
363 * mmc_wait_for_cmd - start a command and wait for completion 363 * mmc_wait_for_cmd - start a command and wait for completion
364 * @host: MMC host to start command 364 * @host: MMC host to start command
365 * @cmd: MMC command to start 365 * @cmd: MMC command to start
366 * @retries: maximum number of retries 366 * @retries: maximum number of retries
367 * 367 *
368 * Start a new MMC command for a host, and wait for the command 368 * Start a new MMC command for a host, and wait for the command
369 * to complete. Return any error that occurred while the command 369 * to complete. Return any error that occurred while the command
370 * was executing. Do not attempt to parse the response. 370 * was executing. Do not attempt to parse the response.
371 */ 371 */
372 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) 372 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
373 { 373 {
374 struct mmc_request mrq = {NULL}; 374 struct mmc_request mrq = {NULL};
375 375
376 WARN_ON(!host->claimed); 376 WARN_ON(!host->claimed);
377 377
378 memset(cmd->resp, 0, sizeof(cmd->resp)); 378 memset(cmd->resp, 0, sizeof(cmd->resp));
379 cmd->retries = retries; 379 cmd->retries = retries;
380 380
381 mrq.cmd = cmd; 381 mrq.cmd = cmd;
382 cmd->data = NULL; 382 cmd->data = NULL;
383 383
384 mmc_wait_for_req(host, &mrq); 384 mmc_wait_for_req(host, &mrq);
385 385
386 return cmd->error; 386 return cmd->error;
387 } 387 }
388 388
389 EXPORT_SYMBOL(mmc_wait_for_cmd); 389 EXPORT_SYMBOL(mmc_wait_for_cmd);
390 390
391 /** 391 /**
392 * mmc_set_data_timeout - set the timeout for a data command 392 * mmc_set_data_timeout - set the timeout for a data command
393 * @data: data phase for command 393 * @data: data phase for command
394 * @card: the MMC card associated with the data transfer 394 * @card: the MMC card associated with the data transfer
395 * 395 *
396 * Computes the data timeout parameters according to the 396 * Computes the data timeout parameters according to the
397 * correct algorithm given the card type. 397 * correct algorithm given the card type.
398 */ 398 */
399 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) 399 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
400 { 400 {
401 unsigned int mult; 401 unsigned int mult;
402 402
403 /* 403 /*
404 * SDIO cards only define an upper 1 s limit on access. 404 * SDIO cards only define an upper 1 s limit on access.
405 */ 405 */
406 if (mmc_card_sdio(card)) { 406 if (mmc_card_sdio(card)) {
407 data->timeout_ns = 1000000000; 407 data->timeout_ns = 1000000000;
408 data->timeout_clks = 0; 408 data->timeout_clks = 0;
409 return; 409 return;
410 } 410 }
411 411
412 /* 412 /*
413 * SD cards use a 100 multiplier rather than 10 413 * SD cards use a 100 multiplier rather than 10
414 */ 414 */
415 mult = mmc_card_sd(card) ? 100 : 10; 415 mult = mmc_card_sd(card) ? 100 : 10;
416 416
417 /* 417 /*
418 * Scale up the multiplier (and therefore the timeout) by 418 * Scale up the multiplier (and therefore the timeout) by
419 * the r2w factor for writes. 419 * the r2w factor for writes.
420 */ 420 */
421 if (data->flags & MMC_DATA_WRITE) 421 if (data->flags & MMC_DATA_WRITE)
422 mult <<= card->csd.r2w_factor; 422 mult <<= card->csd.r2w_factor;
423 423
424 data->timeout_ns = card->csd.tacc_ns * mult; 424 data->timeout_ns = card->csd.tacc_ns * mult;
425 data->timeout_clks = card->csd.tacc_clks * mult; 425 data->timeout_clks = card->csd.tacc_clks * mult;
426 426
427 /* 427 /*
428 * SD cards also have an upper limit on the timeout. 428 * SD cards also have an upper limit on the timeout.
429 */ 429 */
430 if (mmc_card_sd(card)) { 430 if (mmc_card_sd(card)) {
431 unsigned int timeout_us, limit_us; 431 unsigned int timeout_us, limit_us;
432 432
433 timeout_us = data->timeout_ns / 1000; 433 timeout_us = data->timeout_ns / 1000;
434 if (mmc_host_clk_rate(card->host)) 434 if (mmc_host_clk_rate(card->host))
435 timeout_us += data->timeout_clks * 1000 / 435 timeout_us += data->timeout_clks * 1000 /
436 (mmc_host_clk_rate(card->host) / 1000); 436 (mmc_host_clk_rate(card->host) / 1000);
437 437
438 if (data->flags & MMC_DATA_WRITE) 438 if (data->flags & MMC_DATA_WRITE)
439 /* 439 /*
440 * The limit is really 250 ms, but that is 440 * The limit is really 250 ms, but that is
441 * insufficient for some crappy cards. 441 * insufficient for some crappy cards.
442 */ 442 */
443 limit_us = 300000; 443 limit_us = 300000;
444 else 444 else
445 limit_us = 100000; 445 limit_us = 100000;
446 446
447 /* 447 /*
448 * SDHC cards always use these fixed values. 448 * SDHC cards always use these fixed values.
449 */ 449 */
450 if (timeout_us > limit_us || mmc_card_blockaddr(card)) { 450 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
451 data->timeout_ns = limit_us * 1000; 451 data->timeout_ns = limit_us * 1000;
452 data->timeout_clks = 0; 452 data->timeout_clks = 0;
453 } 453 }
454 } 454 }
455 /* 455 /*
456 * Some cards need very high timeouts if driven in SPI mode. 456 * Some cards need very high timeouts if driven in SPI mode.
457 * The worst observed timeout was 900ms after writing a 457 * The worst observed timeout was 900ms after writing a
458 * continuous stream of data until the internal logic 458 * continuous stream of data until the internal logic
459 * overflowed. 459 * overflowed.
460 */ 460 */
461 if (mmc_host_is_spi(card->host)) { 461 if (mmc_host_is_spi(card->host)) {
462 if (data->flags & MMC_DATA_WRITE) { 462 if (data->flags & MMC_DATA_WRITE) {
463 if (data->timeout_ns < 1000000000) 463 if (data->timeout_ns < 1000000000)
464 data->timeout_ns = 1000000000; /* 1s */ 464 data->timeout_ns = 1000000000; /* 1s */
465 } else { 465 } else {
466 if (data->timeout_ns < 100000000) 466 if (data->timeout_ns < 100000000)
467 data->timeout_ns = 100000000; /* 100ms */ 467 data->timeout_ns = 100000000; /* 100ms */
468 } 468 }
469 } 469 }
470 } 470 }
471 EXPORT_SYMBOL(mmc_set_data_timeout); 471 EXPORT_SYMBOL(mmc_set_data_timeout);
472 472
473 /** 473 /**
474 * mmc_align_data_size - pads a transfer size to a more optimal value 474 * mmc_align_data_size - pads a transfer size to a more optimal value
475 * @card: the MMC card associated with the data transfer 475 * @card: the MMC card associated with the data transfer
476 * @sz: original transfer size 476 * @sz: original transfer size
477 * 477 *
478 * Pads the original data size with a number of extra bytes in 478 * Pads the original data size with a number of extra bytes in
479 * order to avoid controller bugs and/or performance hits 479 * order to avoid controller bugs and/or performance hits
480 * (e.g. some controllers revert to PIO for certain sizes). 480 * (e.g. some controllers revert to PIO for certain sizes).
481 * 481 *
482 * Returns the improved size, which might be unmodified. 482 * Returns the improved size, which might be unmodified.
483 * 483 *
484 * Note that this function is only relevant when issuing a 484 * Note that this function is only relevant when issuing a
485 * single scatter gather entry. 485 * single scatter gather entry.
486 */ 486 */
487 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) 487 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
488 { 488 {
489 /* 489 /*
490 * FIXME: We don't have a system for the controller to tell 490 * FIXME: We don't have a system for the controller to tell
491 * the core about its problems yet, so for now we just 32-bit 491 * the core about its problems yet, so for now we just 32-bit
492 * align the size. 492 * align the size.
493 */ 493 */
494 sz = ((sz + 3) / 4) * 4; 494 sz = ((sz + 3) / 4) * 4;
495 495
496 return sz; 496 return sz;
497 } 497 }
498 EXPORT_SYMBOL(mmc_align_data_size); 498 EXPORT_SYMBOL(mmc_align_data_size);
499 499
500 /** 500 /**
501 * mmc_host_enable - enable a host. 501 * mmc_host_enable - enable a host.
502 * @host: mmc host to enable 502 * @host: mmc host to enable
503 * 503 *
504 * Hosts that support power saving can use the 'enable' and 'disable' 504 * Hosts that support power saving can use the 'enable' and 'disable'
505 * methods to exit and enter power saving states. For more information 505 * methods to exit and enter power saving states. For more information
506 * see comments for struct mmc_host_ops. 506 * see comments for struct mmc_host_ops.
507 */ 507 */
508 int mmc_host_enable(struct mmc_host *host) 508 int mmc_host_enable(struct mmc_host *host)
509 { 509 {
510 if (!(host->caps & MMC_CAP_DISABLE)) 510 if (!(host->caps & MMC_CAP_DISABLE))
511 return 0; 511 return 0;
512 512
513 if (host->en_dis_recurs) 513 if (host->en_dis_recurs)
514 return 0; 514 return 0;
515 515
516 if (host->nesting_cnt++) 516 if (host->nesting_cnt++)
517 return 0; 517 return 0;
518 518
519 cancel_delayed_work_sync(&host->disable); 519 cancel_delayed_work_sync(&host->disable);
520 520
521 if (host->enabled) 521 if (host->enabled)
522 return 0; 522 return 0;
523 523
524 if (host->ops->enable) { 524 if (host->ops->enable) {
525 int err; 525 int err;
526 526
527 host->en_dis_recurs = 1; 527 host->en_dis_recurs = 1;
528 err = host->ops->enable(host); 528 err = host->ops->enable(host);
529 host->en_dis_recurs = 0; 529 host->en_dis_recurs = 0;
530 530
531 if (err) { 531 if (err) {
532 pr_debug("%s: enable error %d\n", 532 pr_debug("%s: enable error %d\n",
533 mmc_hostname(host), err); 533 mmc_hostname(host), err);
534 return err; 534 return err;
535 } 535 }
536 } 536 }
537 host->enabled = 1; 537 host->enabled = 1;
538 return 0; 538 return 0;
539 } 539 }
540 EXPORT_SYMBOL(mmc_host_enable); 540 EXPORT_SYMBOL(mmc_host_enable);
541 541
542 static int mmc_host_do_disable(struct mmc_host *host, int lazy) 542 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
543 { 543 {
544 if (host->ops->disable) { 544 if (host->ops->disable) {
545 int err; 545 int err;
546 546
547 host->en_dis_recurs = 1; 547 host->en_dis_recurs = 1;
548 err = host->ops->disable(host, lazy); 548 err = host->ops->disable(host, lazy);
549 host->en_dis_recurs = 0; 549 host->en_dis_recurs = 0;
550 550
551 if (err < 0) { 551 if (err < 0) {
552 pr_debug("%s: disable error %d\n", 552 pr_debug("%s: disable error %d\n",
553 mmc_hostname(host), err); 553 mmc_hostname(host), err);
554 return err; 554 return err;
555 } 555 }
556 if (err > 0) { 556 if (err > 0) {
557 unsigned long delay = msecs_to_jiffies(err); 557 unsigned long delay = msecs_to_jiffies(err);
558 558
559 mmc_schedule_delayed_work(&host->disable, delay); 559 mmc_schedule_delayed_work(&host->disable, delay);
560 } 560 }
561 } 561 }
562 host->enabled = 0; 562 host->enabled = 0;
563 return 0; 563 return 0;
564 } 564 }
565 565
566 /** 566 /**
567 * mmc_host_disable - disable a host. 567 * mmc_host_disable - disable a host.
568 * @host: mmc host to disable 568 * @host: mmc host to disable
569 * 569 *
570 * Hosts that support power saving can use the 'enable' and 'disable' 570 * Hosts that support power saving can use the 'enable' and 'disable'
571 * methods to exit and enter power saving states. For more information 571 * methods to exit and enter power saving states. For more information
572 * see comments for struct mmc_host_ops. 572 * see comments for struct mmc_host_ops.
573 */ 573 */
574 int mmc_host_disable(struct mmc_host *host) 574 int mmc_host_disable(struct mmc_host *host)
575 { 575 {
576 int err; 576 int err;
577 577
578 if (!(host->caps & MMC_CAP_DISABLE)) 578 if (!(host->caps & MMC_CAP_DISABLE))
579 return 0; 579 return 0;
580 580
581 if (host->en_dis_recurs) 581 if (host->en_dis_recurs)
582 return 0; 582 return 0;
583 583
584 if (--host->nesting_cnt) 584 if (--host->nesting_cnt)
585 return 0; 585 return 0;
586 586
587 if (!host->enabled) 587 if (!host->enabled)
588 return 0; 588 return 0;
589 589
590 err = mmc_host_do_disable(host, 0); 590 err = mmc_host_do_disable(host, 0);
591 return err; 591 return err;
592 } 592 }
593 EXPORT_SYMBOL(mmc_host_disable); 593 EXPORT_SYMBOL(mmc_host_disable);
594 594
595 /** 595 /**
596 * __mmc_claim_host - exclusively claim a host 596 * __mmc_claim_host - exclusively claim a host
597 * @host: mmc host to claim 597 * @host: mmc host to claim
598 * @abort: whether or not the operation should be aborted 598 * @abort: whether or not the operation should be aborted
599 * 599 *
600 * Claim a host for a set of operations. If @abort is non null and 600 * Claim a host for a set of operations. If @abort is non null and
601 * dereference a non-zero value then this will return prematurely with 601 * dereference a non-zero value then this will return prematurely with
602 * that non-zero value without acquiring the lock. Returns zero 602 * that non-zero value without acquiring the lock. Returns zero
603 * with the lock held otherwise. 603 * with the lock held otherwise.
604 */ 604 */
605 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) 605 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
606 { 606 {
607 DECLARE_WAITQUEUE(wait, current); 607 DECLARE_WAITQUEUE(wait, current);
608 unsigned long flags; 608 unsigned long flags;
609 int stop; 609 int stop;
610 610
611 might_sleep(); 611 might_sleep();
612 612
613 add_wait_queue(&host->wq, &wait); 613 add_wait_queue(&host->wq, &wait);
614 spin_lock_irqsave(&host->lock, flags); 614 spin_lock_irqsave(&host->lock, flags);
615 while (1) { 615 while (1) {
616 set_current_state(TASK_UNINTERRUPTIBLE); 616 set_current_state(TASK_UNINTERRUPTIBLE);
617 stop = abort ? atomic_read(abort) : 0; 617 stop = abort ? atomic_read(abort) : 0;
618 if (stop || !host->claimed || host->claimer == current) 618 if (stop || !host->claimed || host->claimer == current)
619 break; 619 break;
620 spin_unlock_irqrestore(&host->lock, flags); 620 spin_unlock_irqrestore(&host->lock, flags);
621 schedule(); 621 schedule();
622 spin_lock_irqsave(&host->lock, flags); 622 spin_lock_irqsave(&host->lock, flags);
623 } 623 }
624 set_current_state(TASK_RUNNING); 624 set_current_state(TASK_RUNNING);
625 if (!stop) { 625 if (!stop) {
626 host->claimed = 1; 626 host->claimed = 1;
627 host->claimer = current; 627 host->claimer = current;
628 host->claim_cnt += 1; 628 host->claim_cnt += 1;
629 } else 629 } else
630 wake_up(&host->wq); 630 wake_up(&host->wq);
631 spin_unlock_irqrestore(&host->lock, flags); 631 spin_unlock_irqrestore(&host->lock, flags);
632 remove_wait_queue(&host->wq, &wait); 632 remove_wait_queue(&host->wq, &wait);
633 if (!stop) 633 if (!stop)
634 mmc_host_enable(host); 634 mmc_host_enable(host);
635 return stop; 635 return stop;
636 } 636 }
637 637
638 EXPORT_SYMBOL(__mmc_claim_host); 638 EXPORT_SYMBOL(__mmc_claim_host);
639 639
640 /** 640 /**
641 * mmc_try_claim_host - try exclusively to claim a host 641 * mmc_try_claim_host - try exclusively to claim a host
642 * @host: mmc host to claim 642 * @host: mmc host to claim
643 * 643 *
644 * Returns %1 if the host is claimed, %0 otherwise. 644 * Returns %1 if the host is claimed, %0 otherwise.
645 */ 645 */
646 int mmc_try_claim_host(struct mmc_host *host) 646 int mmc_try_claim_host(struct mmc_host *host)
647 { 647 {
648 int claimed_host = 0; 648 int claimed_host = 0;
649 unsigned long flags; 649 unsigned long flags;
650 650
651 spin_lock_irqsave(&host->lock, flags); 651 spin_lock_irqsave(&host->lock, flags);
652 if (!host->claimed || host->claimer == current) { 652 if (!host->claimed || host->claimer == current) {
653 host->claimed = 1; 653 host->claimed = 1;
654 host->claimer = current; 654 host->claimer = current;
655 host->claim_cnt += 1; 655 host->claim_cnt += 1;
656 claimed_host = 1; 656 claimed_host = 1;
657 } 657 }
658 spin_unlock_irqrestore(&host->lock, flags); 658 spin_unlock_irqrestore(&host->lock, flags);
659 return claimed_host; 659 return claimed_host;
660 } 660 }
661 EXPORT_SYMBOL(mmc_try_claim_host); 661 EXPORT_SYMBOL(mmc_try_claim_host);
662 662
663 /** 663 /**
664 * mmc_do_release_host - release a claimed host 664 * mmc_do_release_host - release a claimed host
665 * @host: mmc host to release 665 * @host: mmc host to release
666 * 666 *
667 * If you successfully claimed a host, this function will 667 * If you successfully claimed a host, this function will
668 * release it again. 668 * release it again.
669 */ 669 */
670 void mmc_do_release_host(struct mmc_host *host) 670 void mmc_do_release_host(struct mmc_host *host)
671 { 671 {
672 unsigned long flags; 672 unsigned long flags;
673 673
674 spin_lock_irqsave(&host->lock, flags); 674 spin_lock_irqsave(&host->lock, flags);
675 if (--host->claim_cnt) { 675 if (--host->claim_cnt) {
676 /* Release for nested claim */ 676 /* Release for nested claim */
677 spin_unlock_irqrestore(&host->lock, flags); 677 spin_unlock_irqrestore(&host->lock, flags);
678 } else { 678 } else {
679 host->claimed = 0; 679 host->claimed = 0;
680 host->claimer = NULL; 680 host->claimer = NULL;
681 spin_unlock_irqrestore(&host->lock, flags); 681 spin_unlock_irqrestore(&host->lock, flags);
682 wake_up(&host->wq); 682 wake_up(&host->wq);
683 } 683 }
684 } 684 }
685 EXPORT_SYMBOL(mmc_do_release_host); 685 EXPORT_SYMBOL(mmc_do_release_host);
686 686
687 void mmc_host_deeper_disable(struct work_struct *work) 687 void mmc_host_deeper_disable(struct work_struct *work)
688 { 688 {
689 struct mmc_host *host = 689 struct mmc_host *host =
690 container_of(work, struct mmc_host, disable.work); 690 container_of(work, struct mmc_host, disable.work);
691 691
692 /* If the host is claimed then we do not want to disable it anymore */ 692 /* If the host is claimed then we do not want to disable it anymore */
693 if (!mmc_try_claim_host(host)) 693 if (!mmc_try_claim_host(host))
694 return; 694 return;
695 mmc_host_do_disable(host, 1); 695 mmc_host_do_disable(host, 1);
696 mmc_do_release_host(host); 696 mmc_do_release_host(host);
697 } 697 }
698 698
699 /** 699 /**
700 * mmc_host_lazy_disable - lazily disable a host. 700 * mmc_host_lazy_disable - lazily disable a host.
701 * @host: mmc host to disable 701 * @host: mmc host to disable
702 * 702 *
703 * Hosts that support power saving can use the 'enable' and 'disable' 703 * Hosts that support power saving can use the 'enable' and 'disable'
704 * methods to exit and enter power saving states. For more information 704 * methods to exit and enter power saving states. For more information
705 * see comments for struct mmc_host_ops. 705 * see comments for struct mmc_host_ops.
706 */ 706 */
707 int mmc_host_lazy_disable(struct mmc_host *host) 707 int mmc_host_lazy_disable(struct mmc_host *host)
708 { 708 {
709 if (!(host->caps & MMC_CAP_DISABLE)) 709 if (!(host->caps & MMC_CAP_DISABLE))
710 return 0; 710 return 0;
711 711
712 if (host->en_dis_recurs) 712 if (host->en_dis_recurs)
713 return 0; 713 return 0;
714 714
715 if (--host->nesting_cnt) 715 if (--host->nesting_cnt)
716 return 0; 716 return 0;
717 717
718 if (!host->enabled) 718 if (!host->enabled)
719 return 0; 719 return 0;
720 720
721 if (host->disable_delay) { 721 if (host->disable_delay) {
722 mmc_schedule_delayed_work(&host->disable, 722 mmc_schedule_delayed_work(&host->disable,
723 msecs_to_jiffies(host->disable_delay)); 723 msecs_to_jiffies(host->disable_delay));
724 return 0; 724 return 0;
725 } else 725 } else
726 return mmc_host_do_disable(host, 1); 726 return mmc_host_do_disable(host, 1);
727 } 727 }
728 EXPORT_SYMBOL(mmc_host_lazy_disable); 728 EXPORT_SYMBOL(mmc_host_lazy_disable);
729 729
730 /** 730 /**
731 * mmc_release_host - release a host 731 * mmc_release_host - release a host
732 * @host: mmc host to release 732 * @host: mmc host to release
733 * 733 *
734 * Release a MMC host, allowing others to claim the host 734 * Release a MMC host, allowing others to claim the host
735 * for their operations. 735 * for their operations.
736 */ 736 */
737 void mmc_release_host(struct mmc_host *host) 737 void mmc_release_host(struct mmc_host *host)
738 { 738 {
739 WARN_ON(!host->claimed); 739 WARN_ON(!host->claimed);
740 740
741 mmc_host_lazy_disable(host); 741 mmc_host_lazy_disable(host);
742 742
743 mmc_do_release_host(host); 743 mmc_do_release_host(host);
744 } 744 }
745 745
746 EXPORT_SYMBOL(mmc_release_host); 746 EXPORT_SYMBOL(mmc_release_host);
747 747
748 /* 748 /*
749 * Internal function that does the actual ios call to the host driver, 749 * Internal function that does the actual ios call to the host driver,
750 * optionally printing some debug output. 750 * optionally printing some debug output.
751 */ 751 */
752 static inline void mmc_set_ios(struct mmc_host *host) 752 static inline void mmc_set_ios(struct mmc_host *host)
753 { 753 {
754 struct mmc_ios *ios = &host->ios; 754 struct mmc_ios *ios = &host->ios;
755 755
756 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " 756 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
757 "width %u timing %u\n", 757 "width %u timing %u\n",
758 mmc_hostname(host), ios->clock, ios->bus_mode, 758 mmc_hostname(host), ios->clock, ios->bus_mode,
759 ios->power_mode, ios->chip_select, ios->vdd, 759 ios->power_mode, ios->chip_select, ios->vdd,
760 ios->bus_width, ios->timing); 760 ios->bus_width, ios->timing);
761 761
762 if (ios->clock > 0) 762 if (ios->clock > 0)
763 mmc_set_ungated(host); 763 mmc_set_ungated(host);
764 host->ops->set_ios(host, ios); 764 host->ops->set_ios(host, ios);
765 } 765 }
766 766
767 /* 767 /*
768 * Control chip select pin on a host. 768 * Control chip select pin on a host.
769 */ 769 */
770 void mmc_set_chip_select(struct mmc_host *host, int mode) 770 void mmc_set_chip_select(struct mmc_host *host, int mode)
771 { 771 {
772 mmc_host_clk_hold(host); 772 mmc_host_clk_hold(host);
773 host->ios.chip_select = mode; 773 host->ios.chip_select = mode;
774 mmc_set_ios(host); 774 mmc_set_ios(host);
775 mmc_host_clk_release(host); 775 mmc_host_clk_release(host);
776 } 776 }
777 777
778 /* 778 /*
779 * Sets the host clock to the highest possible frequency that 779 * Sets the host clock to the highest possible frequency that
780 * is below "hz". 780 * is below "hz".
781 */ 781 */
782 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) 782 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
783 { 783 {
784 WARN_ON(hz < host->f_min); 784 WARN_ON(hz < host->f_min);
785 785
786 if (hz > host->f_max) 786 if (hz > host->f_max)
787 hz = host->f_max; 787 hz = host->f_max;
788 788
789 host->ios.clock = hz; 789 host->ios.clock = hz;
790 mmc_set_ios(host); 790 mmc_set_ios(host);
791 } 791 }
792 792
793 void mmc_set_clock(struct mmc_host *host, unsigned int hz) 793 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
794 { 794 {
795 mmc_host_clk_hold(host); 795 mmc_host_clk_hold(host);
796 __mmc_set_clock(host, hz); 796 __mmc_set_clock(host, hz);
797 mmc_host_clk_release(host); 797 mmc_host_clk_release(host);
798 } 798 }
799 799
800 #ifdef CONFIG_MMC_CLKGATE 800 #ifdef CONFIG_MMC_CLKGATE
801 /* 801 /*
802 * This gates the clock by setting it to 0 Hz. 802 * This gates the clock by setting it to 0 Hz.
803 */ 803 */
804 void mmc_gate_clock(struct mmc_host *host) 804 void mmc_gate_clock(struct mmc_host *host)
805 { 805 {
806 unsigned long flags; 806 unsigned long flags;
807 807
808 spin_lock_irqsave(&host->clk_lock, flags); 808 spin_lock_irqsave(&host->clk_lock, flags);
809 host->clk_old = host->ios.clock; 809 host->clk_old = host->ios.clock;
810 host->ios.clock = 0; 810 host->ios.clock = 0;
811 host->clk_gated = true; 811 host->clk_gated = true;
812 spin_unlock_irqrestore(&host->clk_lock, flags); 812 spin_unlock_irqrestore(&host->clk_lock, flags);
813 mmc_set_ios(host); 813 mmc_set_ios(host);
814 } 814 }
815 815
816 /* 816 /*
817 * This restores the clock from gating by using the cached 817 * This restores the clock from gating by using the cached
818 * clock value. 818 * clock value.
819 */ 819 */
820 void mmc_ungate_clock(struct mmc_host *host) 820 void mmc_ungate_clock(struct mmc_host *host)
821 { 821 {
822 /* 822 /*
823 * We should previously have gated the clock, so the clock shall 823 * We should previously have gated the clock, so the clock shall
824 * be 0 here! The clock may however be 0 during initialization, 824 * be 0 here! The clock may however be 0 during initialization,
825 * when some request operations are performed before setting 825 * when some request operations are performed before setting
826 * the frequency. When ungate is requested in that situation 826 * the frequency. When ungate is requested in that situation
827 * we just ignore the call. 827 * we just ignore the call.
828 */ 828 */
829 if (host->clk_old) { 829 if (host->clk_old) {
830 BUG_ON(host->ios.clock); 830 BUG_ON(host->ios.clock);
831 /* This call will also set host->clk_gated to false */ 831 /* This call will also set host->clk_gated to false */
832 __mmc_set_clock(host, host->clk_old); 832 __mmc_set_clock(host, host->clk_old);
833 } 833 }
834 } 834 }
835 835
836 void mmc_set_ungated(struct mmc_host *host) 836 void mmc_set_ungated(struct mmc_host *host)
837 { 837 {
838 unsigned long flags; 838 unsigned long flags;
839 839
840 /* 840 /*
841 * We've been given a new frequency while the clock is gated, 841 * We've been given a new frequency while the clock is gated,
842 * so make sure we regard this as ungating it. 842 * so make sure we regard this as ungating it.
843 */ 843 */
844 spin_lock_irqsave(&host->clk_lock, flags); 844 spin_lock_irqsave(&host->clk_lock, flags);
845 host->clk_gated = false; 845 host->clk_gated = false;
846 spin_unlock_irqrestore(&host->clk_lock, flags); 846 spin_unlock_irqrestore(&host->clk_lock, flags);
847 } 847 }
848 848
849 #else 849 #else
850 void mmc_set_ungated(struct mmc_host *host) 850 void mmc_set_ungated(struct mmc_host *host)
851 { 851 {
852 } 852 }
853 #endif 853 #endif
854 854
855 /* 855 /*
856 * Change the bus mode (open drain/push-pull) of a host. 856 * Change the bus mode (open drain/push-pull) of a host.
857 */ 857 */
858 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) 858 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
859 { 859 {
860 mmc_host_clk_hold(host); 860 mmc_host_clk_hold(host);
861 host->ios.bus_mode = mode; 861 host->ios.bus_mode = mode;
862 mmc_set_ios(host); 862 mmc_set_ios(host);
863 mmc_host_clk_release(host); 863 mmc_host_clk_release(host);
864 } 864 }
865 865
866 /* 866 /*
867 * Change data bus width of a host. 867 * Change data bus width of a host.
868 */ 868 */
869 void mmc_set_bus_width(struct mmc_host *host, unsigned int width) 869 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
870 { 870 {
871 mmc_host_clk_hold(host); 871 mmc_host_clk_hold(host);
872 host->ios.bus_width = width; 872 host->ios.bus_width = width;
873 mmc_set_ios(host); 873 mmc_set_ios(host);
874 mmc_host_clk_release(host); 874 mmc_host_clk_release(host);
875 } 875 }
876 876
877 /** 877 /**
878 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number 878 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
879 * @vdd: voltage (mV) 879 * @vdd: voltage (mV)
880 * @low_bits: prefer low bits in boundary cases 880 * @low_bits: prefer low bits in boundary cases
881 * 881 *
882 * This function returns the OCR bit number according to the provided @vdd 882 * This function returns the OCR bit number according to the provided @vdd
883 * value. If conversion is not possible a negative errno value returned. 883 * value. If conversion is not possible a negative errno value returned.
884 * 884 *
885 * Depending on the @low_bits flag the function prefers low or high OCR bits 885 * Depending on the @low_bits flag the function prefers low or high OCR bits
886 * on boundary voltages. For example, 886 * on boundary voltages. For example,
887 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); 887 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
888 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); 888 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
889 * 889 *
890 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). 890 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
891 */ 891 */
892 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) 892 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
893 { 893 {
894 const int max_bit = ilog2(MMC_VDD_35_36); 894 const int max_bit = ilog2(MMC_VDD_35_36);
895 int bit; 895 int bit;
896 896
897 if (vdd < 1650 || vdd > 3600) 897 if (vdd < 1650 || vdd > 3600)
898 return -EINVAL; 898 return -EINVAL;
899 899
900 if (vdd >= 1650 && vdd <= 1950) 900 if (vdd >= 1650 && vdd <= 1950)
901 return ilog2(MMC_VDD_165_195); 901 return ilog2(MMC_VDD_165_195);
902 902
903 if (low_bits) 903 if (low_bits)
904 vdd -= 1; 904 vdd -= 1;
905 905
906 /* Base 2000 mV, step 100 mV, bit's base 8. */ 906 /* Base 2000 mV, step 100 mV, bit's base 8. */
907 bit = (vdd - 2000) / 100 + 8; 907 bit = (vdd - 2000) / 100 + 8;
908 if (bit > max_bit) 908 if (bit > max_bit)
909 return max_bit; 909 return max_bit;
910 return bit; 910 return bit;
911 } 911 }
912 912
913 /** 913 /**
914 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask 914 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
915 * @vdd_min: minimum voltage value (mV) 915 * @vdd_min: minimum voltage value (mV)
916 * @vdd_max: maximum voltage value (mV) 916 * @vdd_max: maximum voltage value (mV)
917 * 917 *
918 * This function returns the OCR mask bits according to the provided @vdd_min 918 * This function returns the OCR mask bits according to the provided @vdd_min
919 * and @vdd_max values. If conversion is not possible the function returns 0. 919 * and @vdd_max values. If conversion is not possible the function returns 0.
920 * 920 *
921 * Notes wrt boundary cases: 921 * Notes wrt boundary cases:
922 * This function sets the OCR bits for all boundary voltages, for example 922 * This function sets the OCR bits for all boundary voltages, for example
923 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | 923 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
924 * MMC_VDD_34_35 mask. 924 * MMC_VDD_34_35 mask.
925 */ 925 */
926 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) 926 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
927 { 927 {
928 u32 mask = 0; 928 u32 mask = 0;
929 929
930 if (vdd_max < vdd_min) 930 if (vdd_max < vdd_min)
931 return 0; 931 return 0;
932 932
933 /* Prefer high bits for the boundary vdd_max values. */ 933 /* Prefer high bits for the boundary vdd_max values. */
934 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); 934 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
935 if (vdd_max < 0) 935 if (vdd_max < 0)
936 return 0; 936 return 0;
937 937
938 /* Prefer low bits for the boundary vdd_min values. */ 938 /* Prefer low bits for the boundary vdd_min values. */
939 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); 939 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
940 if (vdd_min < 0) 940 if (vdd_min < 0)
941 return 0; 941 return 0;
942 942
943 /* Fill the mask, from max bit to min bit. */ 943 /* Fill the mask, from max bit to min bit. */
944 while (vdd_max >= vdd_min) 944 while (vdd_max >= vdd_min)
945 mask |= 1 << vdd_max--; 945 mask |= 1 << vdd_max--;
946 946
947 return mask; 947 return mask;
948 } 948 }
949 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); 949 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
950 950
951 #ifdef CONFIG_REGULATOR 951 #ifdef CONFIG_REGULATOR
952 952
953 /** 953 /**
954 * mmc_regulator_get_ocrmask - return mask of supported voltages 954 * mmc_regulator_get_ocrmask - return mask of supported voltages
955 * @supply: regulator to use 955 * @supply: regulator to use
956 * 956 *
957 * This returns either a negative errno, or a mask of voltages that 957 * This returns either a negative errno, or a mask of voltages that
958 * can be provided to MMC/SD/SDIO devices using the specified voltage 958 * can be provided to MMC/SD/SDIO devices using the specified voltage
959 * regulator. This would normally be called before registering the 959 * regulator. This would normally be called before registering the
960 * MMC host adapter. 960 * MMC host adapter.
961 */ 961 */
962 int mmc_regulator_get_ocrmask(struct regulator *supply) 962 int mmc_regulator_get_ocrmask(struct regulator *supply)
963 { 963 {
964 int result = 0; 964 int result = 0;
965 int count; 965 int count;
966 int i; 966 int i;
967 967
968 count = regulator_count_voltages(supply); 968 count = regulator_count_voltages(supply);
969 if (count < 0) 969 if (count < 0)
970 return count; 970 return count;
971 971
972 for (i = 0; i < count; i++) { 972 for (i = 0; i < count; i++) {
973 int vdd_uV; 973 int vdd_uV;
974 int vdd_mV; 974 int vdd_mV;
975 975
976 vdd_uV = regulator_list_voltage(supply, i); 976 vdd_uV = regulator_list_voltage(supply, i);
977 if (vdd_uV <= 0) 977 if (vdd_uV <= 0)
978 continue; 978 continue;
979 979
980 vdd_mV = vdd_uV / 1000; 980 vdd_mV = vdd_uV / 1000;
981 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); 981 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
982 } 982 }
983 983
984 return result; 984 return result;
985 } 985 }
986 EXPORT_SYMBOL(mmc_regulator_get_ocrmask); 986 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
987 987
988 /** 988 /**
989 * mmc_regulator_set_ocr - set regulator to match host->ios voltage 989 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
990 * @mmc: the host to regulate 990 * @mmc: the host to regulate
991 * @supply: regulator to use 991 * @supply: regulator to use
992 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) 992 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
993 * 993 *
994 * Returns zero on success, else negative errno. 994 * Returns zero on success, else negative errno.
995 * 995 *
996 * MMC host drivers may use this to enable or disable a regulator using 996 * MMC host drivers may use this to enable or disable a regulator using
997 * a particular supply voltage. This would normally be called from the 997 * a particular supply voltage. This would normally be called from the
998 * set_ios() method. 998 * set_ios() method.
999 */ 999 */
1000 int mmc_regulator_set_ocr(struct mmc_host *mmc, 1000 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1001 struct regulator *supply, 1001 struct regulator *supply,
1002 unsigned short vdd_bit) 1002 unsigned short vdd_bit)
1003 { 1003 {
1004 int result = 0; 1004 int result = 0;
1005 int min_uV, max_uV; 1005 int min_uV, max_uV;
1006 1006
1007 if (vdd_bit) { 1007 if (vdd_bit) {
1008 int tmp; 1008 int tmp;
1009 int voltage; 1009 int voltage;
1010 1010
1011 /* REVISIT mmc_vddrange_to_ocrmask() may have set some 1011 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1012 * bits this regulator doesn't quite support ... don't 1012 * bits this regulator doesn't quite support ... don't
1013 * be too picky, most cards and regulators are OK with 1013 * be too picky, most cards and regulators are OK with
1014 * a 0.1V range goof (it's a small error percentage). 1014 * a 0.1V range goof (it's a small error percentage).
1015 */ 1015 */
1016 tmp = vdd_bit - ilog2(MMC_VDD_165_195); 1016 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1017 if (tmp == 0) { 1017 if (tmp == 0) {
1018 min_uV = 1650 * 1000; 1018 min_uV = 1650 * 1000;
1019 max_uV = 1950 * 1000; 1019 max_uV = 1950 * 1000;
1020 } else { 1020 } else {
1021 min_uV = 1900 * 1000 + tmp * 100 * 1000; 1021 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1022 max_uV = min_uV + 100 * 1000; 1022 max_uV = min_uV + 100 * 1000;
1023 } 1023 }
1024 1024
1025 /* avoid needless changes to this voltage; the regulator 1025 /* avoid needless changes to this voltage; the regulator
1026 * might not allow this operation 1026 * might not allow this operation
1027 */ 1027 */
1028 voltage = regulator_get_voltage(supply); 1028 voltage = regulator_get_voltage(supply);
1029 if (voltage < 0) 1029 if (voltage < 0)
1030 result = voltage; 1030 result = voltage;
1031 else if (voltage < min_uV || voltage > max_uV) 1031 else if (voltage < min_uV || voltage > max_uV)
1032 result = regulator_set_voltage(supply, min_uV, max_uV); 1032 result = regulator_set_voltage(supply, min_uV, max_uV);
1033 else 1033 else
1034 result = 0; 1034 result = 0;
1035 1035
1036 if (result == 0 && !mmc->regulator_enabled) { 1036 if (result == 0 && !mmc->regulator_enabled) {
1037 result = regulator_enable(supply); 1037 result = regulator_enable(supply);
1038 if (!result) 1038 if (!result)
1039 mmc->regulator_enabled = true; 1039 mmc->regulator_enabled = true;
1040 } 1040 }
1041 } else if (mmc->regulator_enabled) { 1041 } else if (mmc->regulator_enabled) {
1042 result = regulator_disable(supply); 1042 result = regulator_disable(supply);
1043 if (result == 0) 1043 if (result == 0)
1044 mmc->regulator_enabled = false; 1044 mmc->regulator_enabled = false;
1045 } 1045 }
1046 1046
1047 if (result) 1047 if (result)
1048 dev_err(mmc_dev(mmc), 1048 dev_err(mmc_dev(mmc),
1049 "could not set regulator OCR (%d)\n", result); 1049 "could not set regulator OCR (%d)\n", result);
1050 return result; 1050 return result;
1051 } 1051 }
1052 EXPORT_SYMBOL(mmc_regulator_set_ocr); 1052 EXPORT_SYMBOL(mmc_regulator_set_ocr);
1053 1053
1054 #endif /* CONFIG_REGULATOR */ 1054 #endif /* CONFIG_REGULATOR */
1055 1055
1056 /* 1056 /*
1057 * Mask off any voltages we don't support and select 1057 * Mask off any voltages we don't support and select
1058 * the lowest voltage 1058 * the lowest voltage
1059 */ 1059 */
1060 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 1060 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1061 { 1061 {
1062 int bit; 1062 int bit;
1063 1063
1064 ocr &= host->ocr_avail; 1064 ocr &= host->ocr_avail;
1065 1065
1066 bit = ffs(ocr); 1066 bit = ffs(ocr);
1067 if (bit) { 1067 if (bit) {
1068 bit -= 1; 1068 bit -= 1;
1069 1069
1070 ocr &= 3 << bit; 1070 ocr &= 3 << bit;
1071 1071
1072 mmc_host_clk_hold(host); 1072 mmc_host_clk_hold(host);
1073 host->ios.vdd = bit; 1073 host->ios.vdd = bit;
1074 mmc_set_ios(host); 1074 mmc_set_ios(host);
1075 mmc_host_clk_release(host); 1075 mmc_host_clk_release(host);
1076 } else { 1076 } else {
1077 pr_warning("%s: host doesn't support card's voltages\n", 1077 pr_warning("%s: host doesn't support card's voltages\n",
1078 mmc_hostname(host)); 1078 mmc_hostname(host));
1079 ocr = 0; 1079 ocr = 0;
1080 } 1080 }
1081 1081
1082 return ocr; 1082 return ocr;
1083 } 1083 }
1084 1084
1085 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11) 1085 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
1086 { 1086 {
1087 struct mmc_command cmd = {0}; 1087 struct mmc_command cmd = {0};
1088 int err = 0; 1088 int err = 0;
1089 1089
1090 BUG_ON(!host); 1090 BUG_ON(!host);
1091 1091
1092 /* 1092 /*
1093 * Send CMD11 only if the request is to switch the card to 1093 * Send CMD11 only if the request is to switch the card to
1094 * 1.8V signalling. 1094 * 1.8V signalling.
1095 */ 1095 */
1096 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) { 1096 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1097 cmd.opcode = SD_SWITCH_VOLTAGE; 1097 cmd.opcode = SD_SWITCH_VOLTAGE;
1098 cmd.arg = 0; 1098 cmd.arg = 0;
1099 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1099 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1100 1100
1101 err = mmc_wait_for_cmd(host, &cmd, 0); 1101 err = mmc_wait_for_cmd(host, &cmd, 0);
1102 if (err) 1102 if (err)
1103 return err; 1103 return err;
1104 1104
1105 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) 1105 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1106 return -EIO; 1106 return -EIO;
1107 } 1107 }
1108 1108
1109 host->ios.signal_voltage = signal_voltage; 1109 host->ios.signal_voltage = signal_voltage;
1110 1110
1111 if (host->ops->start_signal_voltage_switch) 1111 if (host->ops->start_signal_voltage_switch)
1112 err = host->ops->start_signal_voltage_switch(host, &host->ios); 1112 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1113 1113
1114 return err; 1114 return err;
1115 } 1115 }
1116 1116
1117 /* 1117 /*
1118 * Select timing parameters for host. 1118 * Select timing parameters for host.
1119 */ 1119 */
1120 void mmc_set_timing(struct mmc_host *host, unsigned int timing) 1120 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1121 { 1121 {
1122 mmc_host_clk_hold(host); 1122 mmc_host_clk_hold(host);
1123 host->ios.timing = timing; 1123 host->ios.timing = timing;
1124 mmc_set_ios(host); 1124 mmc_set_ios(host);
1125 mmc_host_clk_release(host); 1125 mmc_host_clk_release(host);
1126 } 1126 }
1127 1127
1128 /* 1128 /*
1129 * Select appropriate driver type for host. 1129 * Select appropriate driver type for host.
1130 */ 1130 */
1131 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) 1131 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1132 { 1132 {
1133 mmc_host_clk_hold(host); 1133 mmc_host_clk_hold(host);
1134 host->ios.drv_type = drv_type; 1134 host->ios.drv_type = drv_type;
1135 mmc_set_ios(host); 1135 mmc_set_ios(host);
1136 mmc_host_clk_release(host); 1136 mmc_host_clk_release(host);
1137 } 1137 }
1138 1138
1139 /* 1139 /*
1140 * Apply power to the MMC stack. This is a two-stage process. 1140 * Apply power to the MMC stack. This is a two-stage process.
1141 * First, we enable power to the card without the clock running. 1141 * First, we enable power to the card without the clock running.
1142 * We then wait a bit for the power to stabilise. Finally, 1142 * We then wait a bit for the power to stabilise. Finally,
1143 * enable the bus drivers and clock to the card. 1143 * enable the bus drivers and clock to the card.
1144 * 1144 *
1145 * We must _NOT_ enable the clock prior to power stablising. 1145 * We must _NOT_ enable the clock prior to power stablising.
1146 * 1146 *
1147 * If a host does all the power sequencing itself, ignore the 1147 * If a host does all the power sequencing itself, ignore the
1148 * initial MMC_POWER_UP stage. 1148 * initial MMC_POWER_UP stage.
1149 */ 1149 */
1150 static void mmc_power_up(struct mmc_host *host) 1150 static void mmc_power_up(struct mmc_host *host)
1151 { 1151 {
1152 int bit; 1152 int bit;
1153 1153
1154 mmc_host_clk_hold(host); 1154 mmc_host_clk_hold(host);
1155 1155
1156 /* If ocr is set, we use it */ 1156 /* If ocr is set, we use it */
1157 if (host->ocr) 1157 if (host->ocr)
1158 bit = ffs(host->ocr) - 1; 1158 bit = ffs(host->ocr) - 1;
1159 else 1159 else
1160 bit = fls(host->ocr_avail) - 1; 1160 bit = fls(host->ocr_avail) - 1;
1161 1161
1162 host->ios.vdd = bit; 1162 host->ios.vdd = bit;
1163 if (mmc_host_is_spi(host)) { 1163 if (mmc_host_is_spi(host)) {
1164 host->ios.chip_select = MMC_CS_HIGH; 1164 host->ios.chip_select = MMC_CS_HIGH;
1165 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 1165 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1166 } else { 1166 } else {
1167 host->ios.chip_select = MMC_CS_DONTCARE; 1167 host->ios.chip_select = MMC_CS_DONTCARE;
1168 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 1168 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1169 } 1169 }
1170 host->ios.power_mode = MMC_POWER_UP; 1170 host->ios.power_mode = MMC_POWER_UP;
1171 host->ios.bus_width = MMC_BUS_WIDTH_1; 1171 host->ios.bus_width = MMC_BUS_WIDTH_1;
1172 host->ios.timing = MMC_TIMING_LEGACY; 1172 host->ios.timing = MMC_TIMING_LEGACY;
1173 mmc_set_ios(host); 1173 mmc_set_ios(host);
1174 1174
1175 /* 1175 /*
1176 * This delay should be sufficient to allow the power supply 1176 * This delay should be sufficient to allow the power supply
1177 * to reach the minimum voltage. 1177 * to reach the minimum voltage.
1178 */ 1178 */
1179 mmc_delay(10); 1179 mmc_delay(10);
1180 1180
1181 host->ios.clock = host->f_init; 1181 host->ios.clock = host->f_init;
1182 1182
1183 host->ios.power_mode = MMC_POWER_ON; 1183 host->ios.power_mode = MMC_POWER_ON;
1184 mmc_set_ios(host); 1184 mmc_set_ios(host);
1185 1185
1186 /* 1186 /*
1187 * This delay must be at least 74 clock sizes, or 1 ms, or the 1187 * This delay must be at least 74 clock sizes, or 1 ms, or the
1188 * time required to reach a stable voltage. 1188 * time required to reach a stable voltage.
1189 */ 1189 */
1190 mmc_delay(10); 1190 mmc_delay(10);
1191 1191
1192 mmc_host_clk_release(host); 1192 mmc_host_clk_release(host);
1193 } 1193 }
1194 1194
1195 static void mmc_power_off(struct mmc_host *host) 1195 void mmc_power_off(struct mmc_host *host)
1196 { 1196 {
1197 mmc_host_clk_hold(host); 1197 mmc_host_clk_hold(host);
1198 1198
1199 host->ios.clock = 0; 1199 host->ios.clock = 0;
1200 host->ios.vdd = 0; 1200 host->ios.vdd = 0;
1201 1201
1202 /* 1202 /*
1203 * Reset ocr mask to be the highest possible voltage supported for 1203 * Reset ocr mask to be the highest possible voltage supported for
1204 * this mmc host. This value will be used at next power up. 1204 * this mmc host. This value will be used at next power up.
1205 */ 1205 */
1206 host->ocr = 1 << (fls(host->ocr_avail) - 1); 1206 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1207 1207
1208 if (!mmc_host_is_spi(host)) { 1208 if (!mmc_host_is_spi(host)) {
1209 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 1209 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1210 host->ios.chip_select = MMC_CS_DONTCARE; 1210 host->ios.chip_select = MMC_CS_DONTCARE;
1211 } 1211 }
1212 host->ios.power_mode = MMC_POWER_OFF; 1212 host->ios.power_mode = MMC_POWER_OFF;
1213 host->ios.bus_width = MMC_BUS_WIDTH_1; 1213 host->ios.bus_width = MMC_BUS_WIDTH_1;
1214 host->ios.timing = MMC_TIMING_LEGACY; 1214 host->ios.timing = MMC_TIMING_LEGACY;
1215 mmc_set_ios(host); 1215 mmc_set_ios(host);
1216 1216
1217 /* 1217 /*
1218 * Some configurations, such as the 802.11 SDIO card in the OLPC 1218 * Some configurations, such as the 802.11 SDIO card in the OLPC
1219 * XO-1.5, require a short delay after poweroff before the card 1219 * XO-1.5, require a short delay after poweroff before the card
1220 * can be successfully turned on again. 1220 * can be successfully turned on again.
1221 */ 1221 */
1222 mmc_delay(1); 1222 mmc_delay(1);
1223 1223
1224 mmc_host_clk_release(host); 1224 mmc_host_clk_release(host);
1225 } 1225 }
1226 1226
1227 /* 1227 /*
1228 * Cleanup when the last reference to the bus operator is dropped. 1228 * Cleanup when the last reference to the bus operator is dropped.
1229 */ 1229 */
1230 static void __mmc_release_bus(struct mmc_host *host) 1230 static void __mmc_release_bus(struct mmc_host *host)
1231 { 1231 {
1232 BUG_ON(!host); 1232 BUG_ON(!host);
1233 BUG_ON(host->bus_refs); 1233 BUG_ON(host->bus_refs);
1234 BUG_ON(!host->bus_dead); 1234 BUG_ON(!host->bus_dead);
1235 1235
1236 host->bus_ops = NULL; 1236 host->bus_ops = NULL;
1237 } 1237 }
1238 1238
1239 /* 1239 /*
1240 * Increase reference count of bus operator 1240 * Increase reference count of bus operator
1241 */ 1241 */
1242 static inline void mmc_bus_get(struct mmc_host *host) 1242 static inline void mmc_bus_get(struct mmc_host *host)
1243 { 1243 {
1244 unsigned long flags; 1244 unsigned long flags;
1245 1245
1246 spin_lock_irqsave(&host->lock, flags); 1246 spin_lock_irqsave(&host->lock, flags);
1247 host->bus_refs++; 1247 host->bus_refs++;
1248 spin_unlock_irqrestore(&host->lock, flags); 1248 spin_unlock_irqrestore(&host->lock, flags);
1249 } 1249 }
1250 1250
1251 /* 1251 /*
1252 * Decrease reference count of bus operator and free it if 1252 * Decrease reference count of bus operator and free it if
1253 * it is the last reference. 1253 * it is the last reference.
1254 */ 1254 */
1255 static inline void mmc_bus_put(struct mmc_host *host) 1255 static inline void mmc_bus_put(struct mmc_host *host)
1256 { 1256 {
1257 unsigned long flags; 1257 unsigned long flags;
1258 1258
1259 spin_lock_irqsave(&host->lock, flags); 1259 spin_lock_irqsave(&host->lock, flags);
1260 host->bus_refs--; 1260 host->bus_refs--;
1261 if ((host->bus_refs == 0) && host->bus_ops) 1261 if ((host->bus_refs == 0) && host->bus_ops)
1262 __mmc_release_bus(host); 1262 __mmc_release_bus(host);
1263 spin_unlock_irqrestore(&host->lock, flags); 1263 spin_unlock_irqrestore(&host->lock, flags);
1264 } 1264 }
1265 1265
1266 /* 1266 /*
1267 * Assign a mmc bus handler to a host. Only one bus handler may control a 1267 * Assign a mmc bus handler to a host. Only one bus handler may control a
1268 * host at any given time. 1268 * host at any given time.
1269 */ 1269 */
1270 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) 1270 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1271 { 1271 {
1272 unsigned long flags; 1272 unsigned long flags;
1273 1273
1274 BUG_ON(!host); 1274 BUG_ON(!host);
1275 BUG_ON(!ops); 1275 BUG_ON(!ops);
1276 1276
1277 WARN_ON(!host->claimed); 1277 WARN_ON(!host->claimed);
1278 1278
1279 spin_lock_irqsave(&host->lock, flags); 1279 spin_lock_irqsave(&host->lock, flags);
1280 1280
1281 BUG_ON(host->bus_ops); 1281 BUG_ON(host->bus_ops);
1282 BUG_ON(host->bus_refs); 1282 BUG_ON(host->bus_refs);
1283 1283
1284 host->bus_ops = ops; 1284 host->bus_ops = ops;
1285 host->bus_refs = 1; 1285 host->bus_refs = 1;
1286 host->bus_dead = 0; 1286 host->bus_dead = 0;
1287 1287
1288 spin_unlock_irqrestore(&host->lock, flags); 1288 spin_unlock_irqrestore(&host->lock, flags);
1289 } 1289 }
1290 1290
1291 /* 1291 /*
1292 * Remove the current bus handler from a host. Assumes that there are 1292 * Remove the current bus handler from a host.
1293 * no interesting cards left, so the bus is powered down.
1294 */ 1293 */
1295 void mmc_detach_bus(struct mmc_host *host) 1294 void mmc_detach_bus(struct mmc_host *host)
1296 { 1295 {
1297 unsigned long flags; 1296 unsigned long flags;
1298 1297
1299 BUG_ON(!host); 1298 BUG_ON(!host);
1300 1299
1301 WARN_ON(!host->claimed); 1300 WARN_ON(!host->claimed);
1302 WARN_ON(!host->bus_ops); 1301 WARN_ON(!host->bus_ops);
1303 1302
1304 spin_lock_irqsave(&host->lock, flags); 1303 spin_lock_irqsave(&host->lock, flags);
1305 1304
1306 host->bus_dead = 1; 1305 host->bus_dead = 1;
1307 1306
1308 spin_unlock_irqrestore(&host->lock, flags); 1307 spin_unlock_irqrestore(&host->lock, flags);
1309 1308
1310 mmc_power_off(host);
1311
1312 mmc_bus_put(host); 1309 mmc_bus_put(host);
1313 } 1310 }
1314 1311
1315 /** 1312 /**
1316 * mmc_detect_change - process change of state on a MMC socket 1313 * mmc_detect_change - process change of state on a MMC socket
1317 * @host: host which changed state. 1314 * @host: host which changed state.
1318 * @delay: optional delay to wait before detection (jiffies) 1315 * @delay: optional delay to wait before detection (jiffies)
1319 * 1316 *
1320 * MMC drivers should call this when they detect a card has been 1317 * MMC drivers should call this when they detect a card has been
1321 * inserted or removed. The MMC layer will confirm that any 1318 * inserted or removed. The MMC layer will confirm that any
1322 * present card is still functional, and initialize any newly 1319 * present card is still functional, and initialize any newly
1323 * inserted. 1320 * inserted.
1324 */ 1321 */
1325 void mmc_detect_change(struct mmc_host *host, unsigned long delay) 1322 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1326 { 1323 {
1327 #ifdef CONFIG_MMC_DEBUG 1324 #ifdef CONFIG_MMC_DEBUG
1328 unsigned long flags; 1325 unsigned long flags;
1329 spin_lock_irqsave(&host->lock, flags); 1326 spin_lock_irqsave(&host->lock, flags);
1330 WARN_ON(host->removed); 1327 WARN_ON(host->removed);
1331 spin_unlock_irqrestore(&host->lock, flags); 1328 spin_unlock_irqrestore(&host->lock, flags);
1332 #endif 1329 #endif
1333 1330
1334 mmc_schedule_delayed_work(&host->detect, delay); 1331 mmc_schedule_delayed_work(&host->detect, delay);
1335 } 1332 }
1336 1333
1337 EXPORT_SYMBOL(mmc_detect_change); 1334 EXPORT_SYMBOL(mmc_detect_change);
1338 1335
1339 void mmc_init_erase(struct mmc_card *card) 1336 void mmc_init_erase(struct mmc_card *card)
1340 { 1337 {
1341 unsigned int sz; 1338 unsigned int sz;
1342 1339
1343 if (is_power_of_2(card->erase_size)) 1340 if (is_power_of_2(card->erase_size))
1344 card->erase_shift = ffs(card->erase_size) - 1; 1341 card->erase_shift = ffs(card->erase_size) - 1;
1345 else 1342 else
1346 card->erase_shift = 0; 1343 card->erase_shift = 0;
1347 1344
1348 /* 1345 /*
1349 * It is possible to erase an arbitrarily large area of an SD or MMC 1346 * It is possible to erase an arbitrarily large area of an SD or MMC
1350 * card. That is not desirable because it can take a long time 1347 * card. That is not desirable because it can take a long time
1351 * (minutes) potentially delaying more important I/O, and also the 1348 * (minutes) potentially delaying more important I/O, and also the
1352 * timeout calculations become increasingly hugely over-estimated. 1349 * timeout calculations become increasingly hugely over-estimated.
1353 * Consequently, 'pref_erase' is defined as a guide to limit erases 1350 * Consequently, 'pref_erase' is defined as a guide to limit erases
1354 * to that size and alignment. 1351 * to that size and alignment.
1355 * 1352 *
1356 * For SD cards that define Allocation Unit size, limit erases to one 1353 * For SD cards that define Allocation Unit size, limit erases to one
1357 * Allocation Unit at a time. For MMC cards that define High Capacity 1354 * Allocation Unit at a time. For MMC cards that define High Capacity
1358 * Erase Size, whether it is switched on or not, limit to that size. 1355 * Erase Size, whether it is switched on or not, limit to that size.
1359 * Otherwise just have a stab at a good value. For modern cards it 1356 * Otherwise just have a stab at a good value. For modern cards it
1360 * will end up being 4MiB. Note that if the value is too small, it 1357 * will end up being 4MiB. Note that if the value is too small, it
1361 * can end up taking longer to erase. 1358 * can end up taking longer to erase.
1362 */ 1359 */
1363 if (mmc_card_sd(card) && card->ssr.au) { 1360 if (mmc_card_sd(card) && card->ssr.au) {
1364 card->pref_erase = card->ssr.au; 1361 card->pref_erase = card->ssr.au;
1365 card->erase_shift = ffs(card->ssr.au) - 1; 1362 card->erase_shift = ffs(card->ssr.au) - 1;
1366 } else if (card->ext_csd.hc_erase_size) { 1363 } else if (card->ext_csd.hc_erase_size) {
1367 card->pref_erase = card->ext_csd.hc_erase_size; 1364 card->pref_erase = card->ext_csd.hc_erase_size;
1368 } else { 1365 } else {
1369 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; 1366 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1370 if (sz < 128) 1367 if (sz < 128)
1371 card->pref_erase = 512 * 1024 / 512; 1368 card->pref_erase = 512 * 1024 / 512;
1372 else if (sz < 512) 1369 else if (sz < 512)
1373 card->pref_erase = 1024 * 1024 / 512; 1370 card->pref_erase = 1024 * 1024 / 512;
1374 else if (sz < 1024) 1371 else if (sz < 1024)
1375 card->pref_erase = 2 * 1024 * 1024 / 512; 1372 card->pref_erase = 2 * 1024 * 1024 / 512;
1376 else 1373 else
1377 card->pref_erase = 4 * 1024 * 1024 / 512; 1374 card->pref_erase = 4 * 1024 * 1024 / 512;
1378 if (card->pref_erase < card->erase_size) 1375 if (card->pref_erase < card->erase_size)
1379 card->pref_erase = card->erase_size; 1376 card->pref_erase = card->erase_size;
1380 else { 1377 else {
1381 sz = card->pref_erase % card->erase_size; 1378 sz = card->pref_erase % card->erase_size;
1382 if (sz) 1379 if (sz)
1383 card->pref_erase += card->erase_size - sz; 1380 card->pref_erase += card->erase_size - sz;
1384 } 1381 }
1385 } 1382 }
1386 } 1383 }
1387 1384
1388 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, 1385 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1389 unsigned int arg, unsigned int qty) 1386 unsigned int arg, unsigned int qty)
1390 { 1387 {
1391 unsigned int erase_timeout; 1388 unsigned int erase_timeout;
1392 1389
1393 if (card->ext_csd.erase_group_def & 1) { 1390 if (card->ext_csd.erase_group_def & 1) {
1394 /* High Capacity Erase Group Size uses HC timeouts */ 1391 /* High Capacity Erase Group Size uses HC timeouts */
1395 if (arg == MMC_TRIM_ARG) 1392 if (arg == MMC_TRIM_ARG)
1396 erase_timeout = card->ext_csd.trim_timeout; 1393 erase_timeout = card->ext_csd.trim_timeout;
1397 else 1394 else
1398 erase_timeout = card->ext_csd.hc_erase_timeout; 1395 erase_timeout = card->ext_csd.hc_erase_timeout;
1399 } else { 1396 } else {
1400 /* CSD Erase Group Size uses write timeout */ 1397 /* CSD Erase Group Size uses write timeout */
1401 unsigned int mult = (10 << card->csd.r2w_factor); 1398 unsigned int mult = (10 << card->csd.r2w_factor);
1402 unsigned int timeout_clks = card->csd.tacc_clks * mult; 1399 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1403 unsigned int timeout_us; 1400 unsigned int timeout_us;
1404 1401
1405 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ 1402 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1406 if (card->csd.tacc_ns < 1000000) 1403 if (card->csd.tacc_ns < 1000000)
1407 timeout_us = (card->csd.tacc_ns * mult) / 1000; 1404 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1408 else 1405 else
1409 timeout_us = (card->csd.tacc_ns / 1000) * mult; 1406 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1410 1407
1411 /* 1408 /*
1412 * ios.clock is only a target. The real clock rate might be 1409 * ios.clock is only a target. The real clock rate might be
1413 * less but not that much less, so fudge it by multiplying by 2. 1410 * less but not that much less, so fudge it by multiplying by 2.
1414 */ 1411 */
1415 timeout_clks <<= 1; 1412 timeout_clks <<= 1;
1416 timeout_us += (timeout_clks * 1000) / 1413 timeout_us += (timeout_clks * 1000) /
1417 (mmc_host_clk_rate(card->host) / 1000); 1414 (mmc_host_clk_rate(card->host) / 1000);
1418 1415
1419 erase_timeout = timeout_us / 1000; 1416 erase_timeout = timeout_us / 1000;
1420 1417
1421 /* 1418 /*
1422 * Theoretically, the calculation could underflow so round up 1419 * Theoretically, the calculation could underflow so round up
1423 * to 1ms in that case. 1420 * to 1ms in that case.
1424 */ 1421 */
1425 if (!erase_timeout) 1422 if (!erase_timeout)
1426 erase_timeout = 1; 1423 erase_timeout = 1;
1427 } 1424 }
1428 1425
1429 /* Multiplier for secure operations */ 1426 /* Multiplier for secure operations */
1430 if (arg & MMC_SECURE_ARGS) { 1427 if (arg & MMC_SECURE_ARGS) {
1431 if (arg == MMC_SECURE_ERASE_ARG) 1428 if (arg == MMC_SECURE_ERASE_ARG)
1432 erase_timeout *= card->ext_csd.sec_erase_mult; 1429 erase_timeout *= card->ext_csd.sec_erase_mult;
1433 else 1430 else
1434 erase_timeout *= card->ext_csd.sec_trim_mult; 1431 erase_timeout *= card->ext_csd.sec_trim_mult;
1435 } 1432 }
1436 1433
1437 erase_timeout *= qty; 1434 erase_timeout *= qty;
1438 1435
1439 /* 1436 /*
1440 * Ensure at least a 1 second timeout for SPI as per 1437 * Ensure at least a 1 second timeout for SPI as per
1441 * 'mmc_set_data_timeout()' 1438 * 'mmc_set_data_timeout()'
1442 */ 1439 */
1443 if (mmc_host_is_spi(card->host) && erase_timeout < 1000) 1440 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1444 erase_timeout = 1000; 1441 erase_timeout = 1000;
1445 1442
1446 return erase_timeout; 1443 return erase_timeout;
1447 } 1444 }
1448 1445
1449 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, 1446 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1450 unsigned int arg, 1447 unsigned int arg,
1451 unsigned int qty) 1448 unsigned int qty)
1452 { 1449 {
1453 unsigned int erase_timeout; 1450 unsigned int erase_timeout;
1454 1451
1455 if (card->ssr.erase_timeout) { 1452 if (card->ssr.erase_timeout) {
1456 /* Erase timeout specified in SD Status Register (SSR) */ 1453 /* Erase timeout specified in SD Status Register (SSR) */
1457 erase_timeout = card->ssr.erase_timeout * qty + 1454 erase_timeout = card->ssr.erase_timeout * qty +
1458 card->ssr.erase_offset; 1455 card->ssr.erase_offset;
1459 } else { 1456 } else {
1460 /* 1457 /*
1461 * Erase timeout not specified in SD Status Register (SSR) so 1458 * Erase timeout not specified in SD Status Register (SSR) so
1462 * use 250ms per write block. 1459 * use 250ms per write block.
1463 */ 1460 */
1464 erase_timeout = 250 * qty; 1461 erase_timeout = 250 * qty;
1465 } 1462 }
1466 1463
1467 /* Must not be less than 1 second */ 1464 /* Must not be less than 1 second */
1468 if (erase_timeout < 1000) 1465 if (erase_timeout < 1000)
1469 erase_timeout = 1000; 1466 erase_timeout = 1000;
1470 1467
1471 return erase_timeout; 1468 return erase_timeout;
1472 } 1469 }
1473 1470
1474 static unsigned int mmc_erase_timeout(struct mmc_card *card, 1471 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1475 unsigned int arg, 1472 unsigned int arg,
1476 unsigned int qty) 1473 unsigned int qty)
1477 { 1474 {
1478 if (mmc_card_sd(card)) 1475 if (mmc_card_sd(card))
1479 return mmc_sd_erase_timeout(card, arg, qty); 1476 return mmc_sd_erase_timeout(card, arg, qty);
1480 else 1477 else
1481 return mmc_mmc_erase_timeout(card, arg, qty); 1478 return mmc_mmc_erase_timeout(card, arg, qty);
1482 } 1479 }
1483 1480
1484 static int mmc_do_erase(struct mmc_card *card, unsigned int from, 1481 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1485 unsigned int to, unsigned int arg) 1482 unsigned int to, unsigned int arg)
1486 { 1483 {
1487 struct mmc_command cmd = {0}; 1484 struct mmc_command cmd = {0};
1488 unsigned int qty = 0; 1485 unsigned int qty = 0;
1489 int err; 1486 int err;
1490 1487
1491 /* 1488 /*
1492 * qty is used to calculate the erase timeout which depends on how many 1489 * qty is used to calculate the erase timeout which depends on how many
1493 * erase groups (or allocation units in SD terminology) are affected. 1490 * erase groups (or allocation units in SD terminology) are affected.
1494 * We count erasing part of an erase group as one erase group. 1491 * We count erasing part of an erase group as one erase group.
1495 * For SD, the allocation units are always a power of 2. For MMC, the 1492 * For SD, the allocation units are always a power of 2. For MMC, the
1496 * erase group size is almost certainly also power of 2, but it does not 1493 * erase group size is almost certainly also power of 2, but it does not
1497 * seem to insist on that in the JEDEC standard, so we fall back to 1494 * seem to insist on that in the JEDEC standard, so we fall back to
1498 * division in that case. SD may not specify an allocation unit size, 1495 * division in that case. SD may not specify an allocation unit size,
1499 * in which case the timeout is based on the number of write blocks. 1496 * in which case the timeout is based on the number of write blocks.
1500 * 1497 *
1501 * Note that the timeout for secure trim 2 will only be correct if the 1498 * Note that the timeout for secure trim 2 will only be correct if the
1502 * number of erase groups specified is the same as the total of all 1499 * number of erase groups specified is the same as the total of all
1503 * preceding secure trim 1 commands. Since the power may have been 1500 * preceding secure trim 1 commands. Since the power may have been
1504 * lost since the secure trim 1 commands occurred, it is generally 1501 * lost since the secure trim 1 commands occurred, it is generally
1505 * impossible to calculate the secure trim 2 timeout correctly. 1502 * impossible to calculate the secure trim 2 timeout correctly.
1506 */ 1503 */
1507 if (card->erase_shift) 1504 if (card->erase_shift)
1508 qty += ((to >> card->erase_shift) - 1505 qty += ((to >> card->erase_shift) -
1509 (from >> card->erase_shift)) + 1; 1506 (from >> card->erase_shift)) + 1;
1510 else if (mmc_card_sd(card)) 1507 else if (mmc_card_sd(card))
1511 qty += to - from + 1; 1508 qty += to - from + 1;
1512 else 1509 else
1513 qty += ((to / card->erase_size) - 1510 qty += ((to / card->erase_size) -
1514 (from / card->erase_size)) + 1; 1511 (from / card->erase_size)) + 1;
1515 1512
1516 if (!mmc_card_blockaddr(card)) { 1513 if (!mmc_card_blockaddr(card)) {
1517 from <<= 9; 1514 from <<= 9;
1518 to <<= 9; 1515 to <<= 9;
1519 } 1516 }
1520 1517
1521 if (mmc_card_sd(card)) 1518 if (mmc_card_sd(card))
1522 cmd.opcode = SD_ERASE_WR_BLK_START; 1519 cmd.opcode = SD_ERASE_WR_BLK_START;
1523 else 1520 else
1524 cmd.opcode = MMC_ERASE_GROUP_START; 1521 cmd.opcode = MMC_ERASE_GROUP_START;
1525 cmd.arg = from; 1522 cmd.arg = from;
1526 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1523 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1527 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1524 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1528 if (err) { 1525 if (err) {
1529 printk(KERN_ERR "mmc_erase: group start error %d, " 1526 printk(KERN_ERR "mmc_erase: group start error %d, "
1530 "status %#x\n", err, cmd.resp[0]); 1527 "status %#x\n", err, cmd.resp[0]);
1531 err = -EINVAL; 1528 err = -EINVAL;
1532 goto out; 1529 goto out;
1533 } 1530 }
1534 1531
1535 memset(&cmd, 0, sizeof(struct mmc_command)); 1532 memset(&cmd, 0, sizeof(struct mmc_command));
1536 if (mmc_card_sd(card)) 1533 if (mmc_card_sd(card))
1537 cmd.opcode = SD_ERASE_WR_BLK_END; 1534 cmd.opcode = SD_ERASE_WR_BLK_END;
1538 else 1535 else
1539 cmd.opcode = MMC_ERASE_GROUP_END; 1536 cmd.opcode = MMC_ERASE_GROUP_END;
1540 cmd.arg = to; 1537 cmd.arg = to;
1541 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1538 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1542 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1539 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1543 if (err) { 1540 if (err) {
1544 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", 1541 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1545 err, cmd.resp[0]); 1542 err, cmd.resp[0]);
1546 err = -EINVAL; 1543 err = -EINVAL;
1547 goto out; 1544 goto out;
1548 } 1545 }
1549 1546
1550 memset(&cmd, 0, sizeof(struct mmc_command)); 1547 memset(&cmd, 0, sizeof(struct mmc_command));
1551 cmd.opcode = MMC_ERASE; 1548 cmd.opcode = MMC_ERASE;
1552 cmd.arg = arg; 1549 cmd.arg = arg;
1553 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1550 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1554 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); 1551 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1555 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1552 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1556 if (err) { 1553 if (err) {
1557 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", 1554 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1558 err, cmd.resp[0]); 1555 err, cmd.resp[0]);
1559 err = -EIO; 1556 err = -EIO;
1560 goto out; 1557 goto out;
1561 } 1558 }
1562 1559
1563 if (mmc_host_is_spi(card->host)) 1560 if (mmc_host_is_spi(card->host))
1564 goto out; 1561 goto out;
1565 1562
1566 do { 1563 do {
1567 memset(&cmd, 0, sizeof(struct mmc_command)); 1564 memset(&cmd, 0, sizeof(struct mmc_command));
1568 cmd.opcode = MMC_SEND_STATUS; 1565 cmd.opcode = MMC_SEND_STATUS;
1569 cmd.arg = card->rca << 16; 1566 cmd.arg = card->rca << 16;
1570 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1567 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1571 /* Do not retry else we can't see errors */ 1568 /* Do not retry else we can't see errors */
1572 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1569 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1573 if (err || (cmd.resp[0] & 0xFDF92000)) { 1570 if (err || (cmd.resp[0] & 0xFDF92000)) {
1574 printk(KERN_ERR "error %d requesting status %#x\n", 1571 printk(KERN_ERR "error %d requesting status %#x\n",
1575 err, cmd.resp[0]); 1572 err, cmd.resp[0]);
1576 err = -EIO; 1573 err = -EIO;
1577 goto out; 1574 goto out;
1578 } 1575 }
1579 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || 1576 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1580 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG); 1577 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
1581 out: 1578 out:
1582 return err; 1579 return err;
1583 } 1580 }
1584 1581
1585 /** 1582 /**
1586 * mmc_erase - erase sectors. 1583 * mmc_erase - erase sectors.
1587 * @card: card to erase 1584 * @card: card to erase
1588 * @from: first sector to erase 1585 * @from: first sector to erase
1589 * @nr: number of sectors to erase 1586 * @nr: number of sectors to erase
1590 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) 1587 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1591 * 1588 *
1592 * Caller must claim host before calling this function. 1589 * Caller must claim host before calling this function.
1593 */ 1590 */
1594 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, 1591 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1595 unsigned int arg) 1592 unsigned int arg)
1596 { 1593 {
1597 unsigned int rem, to = from + nr; 1594 unsigned int rem, to = from + nr;
1598 1595
1599 if (!(card->host->caps & MMC_CAP_ERASE) || 1596 if (!(card->host->caps & MMC_CAP_ERASE) ||
1600 !(card->csd.cmdclass & CCC_ERASE)) 1597 !(card->csd.cmdclass & CCC_ERASE))
1601 return -EOPNOTSUPP; 1598 return -EOPNOTSUPP;
1602 1599
1603 if (!card->erase_size) 1600 if (!card->erase_size)
1604 return -EOPNOTSUPP; 1601 return -EOPNOTSUPP;
1605 1602
1606 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) 1603 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1607 return -EOPNOTSUPP; 1604 return -EOPNOTSUPP;
1608 1605
1609 if ((arg & MMC_SECURE_ARGS) && 1606 if ((arg & MMC_SECURE_ARGS) &&
1610 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) 1607 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1611 return -EOPNOTSUPP; 1608 return -EOPNOTSUPP;
1612 1609
1613 if ((arg & MMC_TRIM_ARGS) && 1610 if ((arg & MMC_TRIM_ARGS) &&
1614 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) 1611 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1615 return -EOPNOTSUPP; 1612 return -EOPNOTSUPP;
1616 1613
1617 if (arg == MMC_SECURE_ERASE_ARG) { 1614 if (arg == MMC_SECURE_ERASE_ARG) {
1618 if (from % card->erase_size || nr % card->erase_size) 1615 if (from % card->erase_size || nr % card->erase_size)
1619 return -EINVAL; 1616 return -EINVAL;
1620 } 1617 }
1621 1618
1622 if (arg == MMC_ERASE_ARG) { 1619 if (arg == MMC_ERASE_ARG) {
1623 rem = from % card->erase_size; 1620 rem = from % card->erase_size;
1624 if (rem) { 1621 if (rem) {
1625 rem = card->erase_size - rem; 1622 rem = card->erase_size - rem;
1626 from += rem; 1623 from += rem;
1627 if (nr > rem) 1624 if (nr > rem)
1628 nr -= rem; 1625 nr -= rem;
1629 else 1626 else
1630 return 0; 1627 return 0;
1631 } 1628 }
1632 rem = nr % card->erase_size; 1629 rem = nr % card->erase_size;
1633 if (rem) 1630 if (rem)
1634 nr -= rem; 1631 nr -= rem;
1635 } 1632 }
1636 1633
1637 if (nr == 0) 1634 if (nr == 0)
1638 return 0; 1635 return 0;
1639 1636
1640 to = from + nr; 1637 to = from + nr;
1641 1638
1642 if (to <= from) 1639 if (to <= from)
1643 return -EINVAL; 1640 return -EINVAL;
1644 1641
1645 /* 'from' and 'to' are inclusive */ 1642 /* 'from' and 'to' are inclusive */
1646 to -= 1; 1643 to -= 1;
1647 1644
1648 return mmc_do_erase(card, from, to, arg); 1645 return mmc_do_erase(card, from, to, arg);
1649 } 1646 }
1650 EXPORT_SYMBOL(mmc_erase); 1647 EXPORT_SYMBOL(mmc_erase);
1651 1648
1652 int mmc_can_erase(struct mmc_card *card) 1649 int mmc_can_erase(struct mmc_card *card)
1653 { 1650 {
1654 if ((card->host->caps & MMC_CAP_ERASE) && 1651 if ((card->host->caps & MMC_CAP_ERASE) &&
1655 (card->csd.cmdclass & CCC_ERASE) && card->erase_size) 1652 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1656 return 1; 1653 return 1;
1657 return 0; 1654 return 0;
1658 } 1655 }
1659 EXPORT_SYMBOL(mmc_can_erase); 1656 EXPORT_SYMBOL(mmc_can_erase);
1660 1657
1661 int mmc_can_trim(struct mmc_card *card) 1658 int mmc_can_trim(struct mmc_card *card)
1662 { 1659 {
1663 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) 1660 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1664 return 1; 1661 return 1;
1665 return 0; 1662 return 0;
1666 } 1663 }
1667 EXPORT_SYMBOL(mmc_can_trim); 1664 EXPORT_SYMBOL(mmc_can_trim);
1668 1665
1669 int mmc_can_secure_erase_trim(struct mmc_card *card) 1666 int mmc_can_secure_erase_trim(struct mmc_card *card)
1670 { 1667 {
1671 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) 1668 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1672 return 1; 1669 return 1;
1673 return 0; 1670 return 0;
1674 } 1671 }
1675 EXPORT_SYMBOL(mmc_can_secure_erase_trim); 1672 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1676 1673
1677 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, 1674 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1678 unsigned int nr) 1675 unsigned int nr)
1679 { 1676 {
1680 if (!card->erase_size) 1677 if (!card->erase_size)
1681 return 0; 1678 return 0;
1682 if (from % card->erase_size || nr % card->erase_size) 1679 if (from % card->erase_size || nr % card->erase_size)
1683 return 0; 1680 return 0;
1684 return 1; 1681 return 1;
1685 } 1682 }
1686 EXPORT_SYMBOL(mmc_erase_group_aligned); 1683 EXPORT_SYMBOL(mmc_erase_group_aligned);
1687 1684
1688 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, 1685 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1689 unsigned int arg) 1686 unsigned int arg)
1690 { 1687 {
1691 struct mmc_host *host = card->host; 1688 struct mmc_host *host = card->host;
1692 unsigned int max_discard, x, y, qty = 0, max_qty, timeout; 1689 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1693 unsigned int last_timeout = 0; 1690 unsigned int last_timeout = 0;
1694 1691
1695 if (card->erase_shift) 1692 if (card->erase_shift)
1696 max_qty = UINT_MAX >> card->erase_shift; 1693 max_qty = UINT_MAX >> card->erase_shift;
1697 else if (mmc_card_sd(card)) 1694 else if (mmc_card_sd(card))
1698 max_qty = UINT_MAX; 1695 max_qty = UINT_MAX;
1699 else 1696 else
1700 max_qty = UINT_MAX / card->erase_size; 1697 max_qty = UINT_MAX / card->erase_size;
1701 1698
1702 /* Find the largest qty with an OK timeout */ 1699 /* Find the largest qty with an OK timeout */
1703 do { 1700 do {
1704 y = 0; 1701 y = 0;
1705 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { 1702 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1706 timeout = mmc_erase_timeout(card, arg, qty + x); 1703 timeout = mmc_erase_timeout(card, arg, qty + x);
1707 if (timeout > host->max_discard_to) 1704 if (timeout > host->max_discard_to)
1708 break; 1705 break;
1709 if (timeout < last_timeout) 1706 if (timeout < last_timeout)
1710 break; 1707 break;
1711 last_timeout = timeout; 1708 last_timeout = timeout;
1712 y = x; 1709 y = x;
1713 } 1710 }
1714 qty += y; 1711 qty += y;
1715 } while (y); 1712 } while (y);
1716 1713
1717 if (!qty) 1714 if (!qty)
1718 return 0; 1715 return 0;
1719 1716
1720 if (qty == 1) 1717 if (qty == 1)
1721 return 1; 1718 return 1;
1722 1719
1723 /* Convert qty to sectors */ 1720 /* Convert qty to sectors */
1724 if (card->erase_shift) 1721 if (card->erase_shift)
1725 max_discard = --qty << card->erase_shift; 1722 max_discard = --qty << card->erase_shift;
1726 else if (mmc_card_sd(card)) 1723 else if (mmc_card_sd(card))
1727 max_discard = qty; 1724 max_discard = qty;
1728 else 1725 else
1729 max_discard = --qty * card->erase_size; 1726 max_discard = --qty * card->erase_size;
1730 1727
1731 return max_discard; 1728 return max_discard;
1732 } 1729 }
1733 1730
1734 unsigned int mmc_calc_max_discard(struct mmc_card *card) 1731 unsigned int mmc_calc_max_discard(struct mmc_card *card)
1735 { 1732 {
1736 struct mmc_host *host = card->host; 1733 struct mmc_host *host = card->host;
1737 unsigned int max_discard, max_trim; 1734 unsigned int max_discard, max_trim;
1738 1735
1739 if (!host->max_discard_to) 1736 if (!host->max_discard_to)
1740 return UINT_MAX; 1737 return UINT_MAX;
1741 1738
1742 /* 1739 /*
1743 * Without erase_group_def set, MMC erase timeout depends on clock 1740 * Without erase_group_def set, MMC erase timeout depends on clock
1744 * frequence which can change. In that case, the best choice is 1741 * frequence which can change. In that case, the best choice is
1745 * just the preferred erase size. 1742 * just the preferred erase size.
1746 */ 1743 */
1747 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) 1744 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1748 return card->pref_erase; 1745 return card->pref_erase;
1749 1746
1750 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); 1747 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1751 if (mmc_can_trim(card)) { 1748 if (mmc_can_trim(card)) {
1752 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); 1749 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1753 if (max_trim < max_discard) 1750 if (max_trim < max_discard)
1754 max_discard = max_trim; 1751 max_discard = max_trim;
1755 } else if (max_discard < card->erase_size) { 1752 } else if (max_discard < card->erase_size) {
1756 max_discard = 0; 1753 max_discard = 0;
1757 } 1754 }
1758 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", 1755 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1759 mmc_hostname(host), max_discard, host->max_discard_to); 1756 mmc_hostname(host), max_discard, host->max_discard_to);
1760 return max_discard; 1757 return max_discard;
1761 } 1758 }
1762 EXPORT_SYMBOL(mmc_calc_max_discard); 1759 EXPORT_SYMBOL(mmc_calc_max_discard);
1763 1760
1764 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) 1761 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1765 { 1762 {
1766 struct mmc_command cmd = {0}; 1763 struct mmc_command cmd = {0};
1767 1764
1768 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card)) 1765 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1769 return 0; 1766 return 0;
1770 1767
1771 cmd.opcode = MMC_SET_BLOCKLEN; 1768 cmd.opcode = MMC_SET_BLOCKLEN;
1772 cmd.arg = blocklen; 1769 cmd.arg = blocklen;
1773 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1770 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1774 return mmc_wait_for_cmd(card->host, &cmd, 5); 1771 return mmc_wait_for_cmd(card->host, &cmd, 5);
1775 } 1772 }
1776 EXPORT_SYMBOL(mmc_set_blocklen); 1773 EXPORT_SYMBOL(mmc_set_blocklen);
1777 1774
1778 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) 1775 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1779 { 1776 {
1780 host->f_init = freq; 1777 host->f_init = freq;
1781 1778
1782 #ifdef CONFIG_MMC_DEBUG 1779 #ifdef CONFIG_MMC_DEBUG
1783 pr_info("%s: %s: trying to init card at %u Hz\n", 1780 pr_info("%s: %s: trying to init card at %u Hz\n",
1784 mmc_hostname(host), __func__, host->f_init); 1781 mmc_hostname(host), __func__, host->f_init);
1785 #endif 1782 #endif
1786 mmc_power_up(host); 1783 mmc_power_up(host);
1787 1784
1788 /* 1785 /*
1789 * sdio_reset sends CMD52 to reset card. Since we do not know 1786 * sdio_reset sends CMD52 to reset card. Since we do not know
1790 * if the card is being re-initialized, just send it. CMD52 1787 * if the card is being re-initialized, just send it. CMD52
1791 * should be ignored by SD/eMMC cards. 1788 * should be ignored by SD/eMMC cards.
1792 */ 1789 */
1793 sdio_reset(host); 1790 sdio_reset(host);
1794 mmc_go_idle(host); 1791 mmc_go_idle(host);
1795 1792
1796 mmc_send_if_cond(host, host->ocr_avail); 1793 mmc_send_if_cond(host, host->ocr_avail);
1797 1794
1798 /* Order's important: probe SDIO, then SD, then MMC */ 1795 /* Order's important: probe SDIO, then SD, then MMC */
1799 if (!mmc_attach_sdio(host)) 1796 if (!mmc_attach_sdio(host))
1800 return 0; 1797 return 0;
1801 if (!mmc_attach_sd(host)) 1798 if (!mmc_attach_sd(host))
1802 return 0; 1799 return 0;
1803 if (!mmc_attach_mmc(host)) 1800 if (!mmc_attach_mmc(host))
1804 return 0; 1801 return 0;
1805 1802
1806 mmc_power_off(host); 1803 mmc_power_off(host);
1807 return -EIO; 1804 return -EIO;
1808 } 1805 }
1809 1806
1810 void mmc_rescan(struct work_struct *work) 1807 void mmc_rescan(struct work_struct *work)
1811 { 1808 {
1812 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; 1809 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1813 struct mmc_host *host = 1810 struct mmc_host *host =
1814 container_of(work, struct mmc_host, detect.work); 1811 container_of(work, struct mmc_host, detect.work);
1815 int i; 1812 int i;
1816 1813
1817 if (host->rescan_disable) 1814 if (host->rescan_disable)
1818 return; 1815 return;
1819 1816
1820 mmc_bus_get(host); 1817 mmc_bus_get(host);
1821 1818
1822 /* 1819 /*
1823 * if there is a _removable_ card registered, check whether it is 1820 * if there is a _removable_ card registered, check whether it is
1824 * still present 1821 * still present
1825 */ 1822 */
1826 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead 1823 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1827 && !(host->caps & MMC_CAP_NONREMOVABLE)) 1824 && !(host->caps & MMC_CAP_NONREMOVABLE))
1828 host->bus_ops->detect(host); 1825 host->bus_ops->detect(host);
1829 1826
1830 /* 1827 /*
1831 * Let mmc_bus_put() free the bus/bus_ops if we've found that 1828 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1832 * the card is no longer present. 1829 * the card is no longer present.
1833 */ 1830 */
1834 mmc_bus_put(host); 1831 mmc_bus_put(host);
1835 mmc_bus_get(host); 1832 mmc_bus_get(host);
1836 1833
1837 /* if there still is a card present, stop here */ 1834 /* if there still is a card present, stop here */
1838 if (host->bus_ops != NULL) { 1835 if (host->bus_ops != NULL) {
1839 mmc_bus_put(host); 1836 mmc_bus_put(host);
1840 goto out; 1837 goto out;
1841 } 1838 }
1842 1839
1843 /* 1840 /*
1844 * Only we can add a new handler, so it's safe to 1841 * Only we can add a new handler, so it's safe to
1845 * release the lock here. 1842 * release the lock here.
1846 */ 1843 */
1847 mmc_bus_put(host); 1844 mmc_bus_put(host);
1848 1845
1849 if (host->ops->get_cd && host->ops->get_cd(host) == 0) 1846 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1850 goto out; 1847 goto out;
1851 1848
1852 mmc_claim_host(host); 1849 mmc_claim_host(host);
1853 for (i = 0; i < ARRAY_SIZE(freqs); i++) { 1850 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1854 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) 1851 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1855 break; 1852 break;
1856 if (freqs[i] <= host->f_min) 1853 if (freqs[i] <= host->f_min)
1857 break; 1854 break;
1858 } 1855 }
1859 mmc_release_host(host); 1856 mmc_release_host(host);
1860 1857
1861 out: 1858 out:
1862 if (host->caps & MMC_CAP_NEEDS_POLL) 1859 if (host->caps & MMC_CAP_NEEDS_POLL)
1863 mmc_schedule_delayed_work(&host->detect, HZ); 1860 mmc_schedule_delayed_work(&host->detect, HZ);
1864 } 1861 }
1865 1862
1866 void mmc_start_host(struct mmc_host *host) 1863 void mmc_start_host(struct mmc_host *host)
1867 { 1864 {
1868 mmc_power_off(host); 1865 mmc_power_off(host);
1869 mmc_detect_change(host, 0); 1866 mmc_detect_change(host, 0);
1870 } 1867 }
1871 1868
1872 void mmc_stop_host(struct mmc_host *host) 1869 void mmc_stop_host(struct mmc_host *host)
1873 { 1870 {
1874 #ifdef CONFIG_MMC_DEBUG 1871 #ifdef CONFIG_MMC_DEBUG
1875 unsigned long flags; 1872 unsigned long flags;
1876 spin_lock_irqsave(&host->lock, flags); 1873 spin_lock_irqsave(&host->lock, flags);
1877 host->removed = 1; 1874 host->removed = 1;
1878 spin_unlock_irqrestore(&host->lock, flags); 1875 spin_unlock_irqrestore(&host->lock, flags);
1879 #endif 1876 #endif
1880 1877
1881 if (host->caps & MMC_CAP_DISABLE) 1878 if (host->caps & MMC_CAP_DISABLE)
1882 cancel_delayed_work(&host->disable); 1879 cancel_delayed_work(&host->disable);
1883 cancel_delayed_work_sync(&host->detect); 1880 cancel_delayed_work_sync(&host->detect);
1884 mmc_flush_scheduled_work(); 1881 mmc_flush_scheduled_work();
1885 1882
1886 /* clear pm flags now and let card drivers set them as needed */ 1883 /* clear pm flags now and let card drivers set them as needed */
1887 host->pm_flags = 0; 1884 host->pm_flags = 0;
1888 1885
1889 mmc_bus_get(host); 1886 mmc_bus_get(host);
1890 if (host->bus_ops && !host->bus_dead) { 1887 if (host->bus_ops && !host->bus_dead) {
1891 if (host->bus_ops->remove) 1888 if (host->bus_ops->remove)
1892 host->bus_ops->remove(host); 1889 host->bus_ops->remove(host);
1893 1890
1894 mmc_claim_host(host); 1891 mmc_claim_host(host);
1895 mmc_detach_bus(host); 1892 mmc_detach_bus(host);
1893 mmc_power_off(host);
1896 mmc_release_host(host); 1894 mmc_release_host(host);
1897 mmc_bus_put(host); 1895 mmc_bus_put(host);
1898 return; 1896 return;
1899 } 1897 }
1900 mmc_bus_put(host); 1898 mmc_bus_put(host);
1901 1899
1902 BUG_ON(host->card); 1900 BUG_ON(host->card);
1903 1901
1904 mmc_power_off(host); 1902 mmc_power_off(host);
1905 } 1903 }
1906 1904
1907 int mmc_power_save_host(struct mmc_host *host) 1905 int mmc_power_save_host(struct mmc_host *host)
1908 { 1906 {
1909 int ret = 0; 1907 int ret = 0;
1910 1908
1911 #ifdef CONFIG_MMC_DEBUG 1909 #ifdef CONFIG_MMC_DEBUG
1912 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__); 1910 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
1913 #endif 1911 #endif
1914 1912
1915 mmc_bus_get(host); 1913 mmc_bus_get(host);
1916 1914
1917 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 1915 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1918 mmc_bus_put(host); 1916 mmc_bus_put(host);
1919 return -EINVAL; 1917 return -EINVAL;
1920 } 1918 }
1921 1919
1922 if (host->bus_ops->power_save) 1920 if (host->bus_ops->power_save)
1923 ret = host->bus_ops->power_save(host); 1921 ret = host->bus_ops->power_save(host);
1924 1922
1925 mmc_bus_put(host); 1923 mmc_bus_put(host);
1926 1924
1927 mmc_power_off(host); 1925 mmc_power_off(host);
1928 1926
1929 return ret; 1927 return ret;
1930 } 1928 }
1931 EXPORT_SYMBOL(mmc_power_save_host); 1929 EXPORT_SYMBOL(mmc_power_save_host);
1932 1930
1933 int mmc_power_restore_host(struct mmc_host *host) 1931 int mmc_power_restore_host(struct mmc_host *host)
1934 { 1932 {
1935 int ret; 1933 int ret;
1936 1934
1937 #ifdef CONFIG_MMC_DEBUG 1935 #ifdef CONFIG_MMC_DEBUG
1938 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__); 1936 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
1939 #endif 1937 #endif
1940 1938
1941 mmc_bus_get(host); 1939 mmc_bus_get(host);
1942 1940
1943 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 1941 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1944 mmc_bus_put(host); 1942 mmc_bus_put(host);
1945 return -EINVAL; 1943 return -EINVAL;
1946 } 1944 }
1947 1945
1948 mmc_power_up(host); 1946 mmc_power_up(host);
1949 ret = host->bus_ops->power_restore(host); 1947 ret = host->bus_ops->power_restore(host);
1950 1948
1951 mmc_bus_put(host); 1949 mmc_bus_put(host);
1952 1950
1953 return ret; 1951 return ret;
1954 } 1952 }
1955 EXPORT_SYMBOL(mmc_power_restore_host); 1953 EXPORT_SYMBOL(mmc_power_restore_host);
1956 1954
1957 int mmc_card_awake(struct mmc_host *host) 1955 int mmc_card_awake(struct mmc_host *host)
1958 { 1956 {
1959 int err = -ENOSYS; 1957 int err = -ENOSYS;
1960 1958
1961 mmc_bus_get(host); 1959 mmc_bus_get(host);
1962 1960
1963 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) 1961 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1964 err = host->bus_ops->awake(host); 1962 err = host->bus_ops->awake(host);
1965 1963
1966 mmc_bus_put(host); 1964 mmc_bus_put(host);
1967 1965
1968 return err; 1966 return err;
1969 } 1967 }
1970 EXPORT_SYMBOL(mmc_card_awake); 1968 EXPORT_SYMBOL(mmc_card_awake);
1971 1969
1972 int mmc_card_sleep(struct mmc_host *host) 1970 int mmc_card_sleep(struct mmc_host *host)
1973 { 1971 {
1974 int err = -ENOSYS; 1972 int err = -ENOSYS;
1975 1973
1976 mmc_bus_get(host); 1974 mmc_bus_get(host);
1977 1975
1978 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) 1976 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1979 err = host->bus_ops->sleep(host); 1977 err = host->bus_ops->sleep(host);
1980 1978
1981 mmc_bus_put(host); 1979 mmc_bus_put(host);
1982 1980
1983 return err; 1981 return err;
1984 } 1982 }
1985 EXPORT_SYMBOL(mmc_card_sleep); 1983 EXPORT_SYMBOL(mmc_card_sleep);
1986 1984
1987 int mmc_card_can_sleep(struct mmc_host *host) 1985 int mmc_card_can_sleep(struct mmc_host *host)
1988 { 1986 {
1989 struct mmc_card *card = host->card; 1987 struct mmc_card *card = host->card;
1990 1988
1991 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) 1989 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1992 return 1; 1990 return 1;
1993 return 0; 1991 return 0;
1994 } 1992 }
1995 EXPORT_SYMBOL(mmc_card_can_sleep); 1993 EXPORT_SYMBOL(mmc_card_can_sleep);
1996 1994
1997 #ifdef CONFIG_PM 1995 #ifdef CONFIG_PM
1998 1996
1999 /** 1997 /**
2000 * mmc_suspend_host - suspend a host 1998 * mmc_suspend_host - suspend a host
2001 * @host: mmc host 1999 * @host: mmc host
2002 */ 2000 */
2003 int mmc_suspend_host(struct mmc_host *host) 2001 int mmc_suspend_host(struct mmc_host *host)
2004 { 2002 {
2005 int err = 0; 2003 int err = 0;
2006 2004
2007 if (host->caps & MMC_CAP_DISABLE) 2005 if (host->caps & MMC_CAP_DISABLE)
2008 cancel_delayed_work(&host->disable); 2006 cancel_delayed_work(&host->disable);
2009 cancel_delayed_work(&host->detect); 2007 cancel_delayed_work(&host->detect);
2010 mmc_flush_scheduled_work(); 2008 mmc_flush_scheduled_work();
2011 2009
2012 mmc_bus_get(host); 2010 mmc_bus_get(host);
2013 if (host->bus_ops && !host->bus_dead) { 2011 if (host->bus_ops && !host->bus_dead) {
2014 if (host->bus_ops->suspend) 2012 if (host->bus_ops->suspend)
2015 err = host->bus_ops->suspend(host); 2013 err = host->bus_ops->suspend(host);
2016 if (err == -ENOSYS || !host->bus_ops->resume) { 2014 if (err == -ENOSYS || !host->bus_ops->resume) {
2017 /* 2015 /*
2018 * We simply "remove" the card in this case. 2016 * We simply "remove" the card in this case.
2019 * It will be redetected on resume. 2017 * It will be redetected on resume.
2020 */ 2018 */
2021 if (host->bus_ops->remove) 2019 if (host->bus_ops->remove)
2022 host->bus_ops->remove(host); 2020 host->bus_ops->remove(host);
2023 mmc_claim_host(host); 2021 mmc_claim_host(host);
2024 mmc_detach_bus(host); 2022 mmc_detach_bus(host);
2023 mmc_power_off(host);
2025 mmc_release_host(host); 2024 mmc_release_host(host);
2026 host->pm_flags = 0; 2025 host->pm_flags = 0;
2027 err = 0; 2026 err = 0;
2028 } 2027 }
2029 } 2028 }
2030 mmc_bus_put(host); 2029 mmc_bus_put(host);
2031 2030
2032 if (!err && !mmc_card_keep_power(host)) 2031 if (!err && !mmc_card_keep_power(host))
2033 mmc_power_off(host); 2032 mmc_power_off(host);
2034 2033
2035 return err; 2034 return err;
2036 } 2035 }
2037 2036
2038 EXPORT_SYMBOL(mmc_suspend_host); 2037 EXPORT_SYMBOL(mmc_suspend_host);
2039 2038
2040 /** 2039 /**
2041 * mmc_resume_host - resume a previously suspended host 2040 * mmc_resume_host - resume a previously suspended host
2042 * @host: mmc host 2041 * @host: mmc host
2043 */ 2042 */
2044 int mmc_resume_host(struct mmc_host *host) 2043 int mmc_resume_host(struct mmc_host *host)
2045 { 2044 {
2046 int err = 0; 2045 int err = 0;
2047 2046
2048 mmc_bus_get(host); 2047 mmc_bus_get(host);
2049 if (host->bus_ops && !host->bus_dead) { 2048 if (host->bus_ops && !host->bus_dead) {
2050 if (!mmc_card_keep_power(host)) { 2049 if (!mmc_card_keep_power(host)) {
2051 mmc_power_up(host); 2050 mmc_power_up(host);
2052 mmc_select_voltage(host, host->ocr); 2051 mmc_select_voltage(host, host->ocr);
2053 /* 2052 /*
2054 * Tell runtime PM core we just powered up the card, 2053 * Tell runtime PM core we just powered up the card,
2055 * since it still believes the card is powered off. 2054 * since it still believes the card is powered off.
2056 * Note that currently runtime PM is only enabled 2055 * Note that currently runtime PM is only enabled
2057 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD 2056 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2058 */ 2057 */
2059 if (mmc_card_sdio(host->card) && 2058 if (mmc_card_sdio(host->card) &&
2060 (host->caps & MMC_CAP_POWER_OFF_CARD)) { 2059 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2061 pm_runtime_disable(&host->card->dev); 2060 pm_runtime_disable(&host->card->dev);
2062 pm_runtime_set_active(&host->card->dev); 2061 pm_runtime_set_active(&host->card->dev);
2063 pm_runtime_enable(&host->card->dev); 2062 pm_runtime_enable(&host->card->dev);
2064 } 2063 }
2065 } 2064 }
2066 BUG_ON(!host->bus_ops->resume); 2065 BUG_ON(!host->bus_ops->resume);
2067 err = host->bus_ops->resume(host); 2066 err = host->bus_ops->resume(host);
2068 if (err) { 2067 if (err) {
2069 printk(KERN_WARNING "%s: error %d during resume " 2068 printk(KERN_WARNING "%s: error %d during resume "
2070 "(card was removed?)\n", 2069 "(card was removed?)\n",
2071 mmc_hostname(host), err); 2070 mmc_hostname(host), err);
2072 err = 0; 2071 err = 0;
2073 } 2072 }
2074 } 2073 }
2075 host->pm_flags &= ~MMC_PM_KEEP_POWER; 2074 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2076 mmc_bus_put(host); 2075 mmc_bus_put(host);
2077 2076
2078 return err; 2077 return err;
2079 } 2078 }
2080 EXPORT_SYMBOL(mmc_resume_host); 2079 EXPORT_SYMBOL(mmc_resume_host);
2081 2080
2082 /* Do the card removal on suspend if card is assumed removeable 2081 /* Do the card removal on suspend if card is assumed removeable
2083 * Do that in pm notifier while userspace isn't yet frozen, so we will be able 2082 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2084 to sync the card. 2083 to sync the card.
2085 */ 2084 */
2086 int mmc_pm_notify(struct notifier_block *notify_block, 2085 int mmc_pm_notify(struct notifier_block *notify_block,
2087 unsigned long mode, void *unused) 2086 unsigned long mode, void *unused)
2088 { 2087 {
2089 struct mmc_host *host = container_of( 2088 struct mmc_host *host = container_of(
2090 notify_block, struct mmc_host, pm_notify); 2089 notify_block, struct mmc_host, pm_notify);
2091 unsigned long flags; 2090 unsigned long flags;
2092 2091
2093 2092
2094 switch (mode) { 2093 switch (mode) {
2095 case PM_HIBERNATION_PREPARE: 2094 case PM_HIBERNATION_PREPARE:
2096 case PM_SUSPEND_PREPARE: 2095 case PM_SUSPEND_PREPARE:
2097 2096
2098 spin_lock_irqsave(&host->lock, flags); 2097 spin_lock_irqsave(&host->lock, flags);
2099 host->rescan_disable = 1; 2098 host->rescan_disable = 1;
2100 spin_unlock_irqrestore(&host->lock, flags); 2099 spin_unlock_irqrestore(&host->lock, flags);
2101 cancel_delayed_work_sync(&host->detect); 2100 cancel_delayed_work_sync(&host->detect);
2102 2101
2103 if (!host->bus_ops || host->bus_ops->suspend) 2102 if (!host->bus_ops || host->bus_ops->suspend)
2104 break; 2103 break;
2105 2104
2106 mmc_claim_host(host); 2105 mmc_claim_host(host);
2107 2106
2108 if (host->bus_ops->remove) 2107 if (host->bus_ops->remove)
2109 host->bus_ops->remove(host); 2108 host->bus_ops->remove(host);
2110 2109
2111 mmc_detach_bus(host); 2110 mmc_detach_bus(host);
2111 mmc_power_off(host);
2112 mmc_release_host(host); 2112 mmc_release_host(host);
2113 host->pm_flags = 0; 2113 host->pm_flags = 0;
2114 break; 2114 break;
2115 2115
2116 case PM_POST_SUSPEND: 2116 case PM_POST_SUSPEND:
2117 case PM_POST_HIBERNATION: 2117 case PM_POST_HIBERNATION:
2118 case PM_POST_RESTORE: 2118 case PM_POST_RESTORE:
2119 2119
2120 spin_lock_irqsave(&host->lock, flags); 2120 spin_lock_irqsave(&host->lock, flags);
2121 host->rescan_disable = 0; 2121 host->rescan_disable = 0;
2122 spin_unlock_irqrestore(&host->lock, flags); 2122 spin_unlock_irqrestore(&host->lock, flags);
2123 mmc_detect_change(host, 0); 2123 mmc_detect_change(host, 0);
2124 2124
2125 } 2125 }
2126 2126
2127 return 0; 2127 return 0;
2128 } 2128 }
2129 #endif 2129 #endif
2130 2130
2131 static int __init mmc_init(void) 2131 static int __init mmc_init(void)
2132 { 2132 {
2133 int ret; 2133 int ret;
2134 2134
2135 workqueue = alloc_ordered_workqueue("kmmcd", 0); 2135 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2136 if (!workqueue) 2136 if (!workqueue)
2137 return -ENOMEM; 2137 return -ENOMEM;
2138 2138
2139 ret = mmc_register_bus(); 2139 ret = mmc_register_bus();
2140 if (ret) 2140 if (ret)
2141 goto destroy_workqueue; 2141 goto destroy_workqueue;
2142 2142
2143 ret = mmc_register_host_class(); 2143 ret = mmc_register_host_class();
2144 if (ret) 2144 if (ret)
2145 goto unregister_bus; 2145 goto unregister_bus;
2146 2146
2147 ret = sdio_register_bus(); 2147 ret = sdio_register_bus();
2148 if (ret) 2148 if (ret)
2149 goto unregister_host_class; 2149 goto unregister_host_class;
2150 2150
2151 return 0; 2151 return 0;
2152 2152
2153 unregister_host_class: 2153 unregister_host_class:
2154 mmc_unregister_host_class(); 2154 mmc_unregister_host_class();
2155 unregister_bus: 2155 unregister_bus:
2156 mmc_unregister_bus(); 2156 mmc_unregister_bus();
2157 destroy_workqueue: 2157 destroy_workqueue:
2158 destroy_workqueue(workqueue); 2158 destroy_workqueue(workqueue);
2159 2159
2160 return ret; 2160 return ret;
2161 } 2161 }
2162 2162
2163 static void __exit mmc_exit(void) 2163 static void __exit mmc_exit(void)
2164 { 2164 {
2165 sdio_unregister_bus(); 2165 sdio_unregister_bus();
2166 mmc_unregister_host_class(); 2166 mmc_unregister_host_class();
2167 mmc_unregister_bus(); 2167 mmc_unregister_bus();
2168 destroy_workqueue(workqueue); 2168 destroy_workqueue(workqueue);
2169 } 2169 }
2170 2170
2171 subsys_initcall(mmc_init); 2171 subsys_initcall(mmc_init);
2172 module_exit(mmc_exit); 2172 module_exit(mmc_exit);
drivers/mmc/core/core.h
1 /* 1 /*
2 * linux/drivers/mmc/core/core.h 2 * linux/drivers/mmc/core/core.h
3 * 3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved. 4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2007 Pierre Ossman 5 * Copyright 2007 Pierre Ossman
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
10 */ 10 */
11 #ifndef _MMC_CORE_CORE_H 11 #ifndef _MMC_CORE_CORE_H
12 #define _MMC_CORE_CORE_H 12 #define _MMC_CORE_CORE_H
13 13
14 #include <linux/delay.h> 14 #include <linux/delay.h>
15 15
16 #define MMC_CMD_RETRIES 3 16 #define MMC_CMD_RETRIES 3
17 17
18 struct mmc_bus_ops { 18 struct mmc_bus_ops {
19 int (*awake)(struct mmc_host *); 19 int (*awake)(struct mmc_host *);
20 int (*sleep)(struct mmc_host *); 20 int (*sleep)(struct mmc_host *);
21 void (*remove)(struct mmc_host *); 21 void (*remove)(struct mmc_host *);
22 void (*detect)(struct mmc_host *); 22 void (*detect)(struct mmc_host *);
23 int (*suspend)(struct mmc_host *); 23 int (*suspend)(struct mmc_host *);
24 int (*resume)(struct mmc_host *); 24 int (*resume)(struct mmc_host *);
25 int (*power_save)(struct mmc_host *); 25 int (*power_save)(struct mmc_host *);
26 int (*power_restore)(struct mmc_host *); 26 int (*power_restore)(struct mmc_host *);
27 }; 27 };
28 28
29 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); 29 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
30 void mmc_detach_bus(struct mmc_host *host); 30 void mmc_detach_bus(struct mmc_host *host);
31 31
32 void mmc_init_erase(struct mmc_card *card); 32 void mmc_init_erase(struct mmc_card *card);
33 33
34 void mmc_set_chip_select(struct mmc_host *host, int mode); 34 void mmc_set_chip_select(struct mmc_host *host, int mode);
35 void mmc_set_clock(struct mmc_host *host, unsigned int hz); 35 void mmc_set_clock(struct mmc_host *host, unsigned int hz);
36 void mmc_gate_clock(struct mmc_host *host); 36 void mmc_gate_clock(struct mmc_host *host);
37 void mmc_ungate_clock(struct mmc_host *host); 37 void mmc_ungate_clock(struct mmc_host *host);
38 void mmc_set_ungated(struct mmc_host *host); 38 void mmc_set_ungated(struct mmc_host *host);
39 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode); 39 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
40 void mmc_set_bus_width(struct mmc_host *host, unsigned int width); 40 void mmc_set_bus_width(struct mmc_host *host, unsigned int width);
41 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr); 41 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr);
42 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, 42 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage,
43 bool cmd11); 43 bool cmd11);
44 void mmc_set_timing(struct mmc_host *host, unsigned int timing); 44 void mmc_set_timing(struct mmc_host *host, unsigned int timing);
45 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type); 45 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type);
46 void mmc_power_off(struct mmc_host *host);
46 47
47 static inline void mmc_delay(unsigned int ms) 48 static inline void mmc_delay(unsigned int ms)
48 { 49 {
49 if (ms < 1000 / HZ) { 50 if (ms < 1000 / HZ) {
50 cond_resched(); 51 cond_resched();
51 mdelay(ms); 52 mdelay(ms);
52 } else { 53 } else {
53 msleep(ms); 54 msleep(ms);
54 } 55 }
55 } 56 }
56 57
57 void mmc_rescan(struct work_struct *work); 58 void mmc_rescan(struct work_struct *work);
58 void mmc_start_host(struct mmc_host *host); 59 void mmc_start_host(struct mmc_host *host);
59 void mmc_stop_host(struct mmc_host *host); 60 void mmc_stop_host(struct mmc_host *host);
60 61
61 int mmc_attach_mmc(struct mmc_host *host); 62 int mmc_attach_mmc(struct mmc_host *host);
62 int mmc_attach_sd(struct mmc_host *host); 63 int mmc_attach_sd(struct mmc_host *host);
63 int mmc_attach_sdio(struct mmc_host *host); 64 int mmc_attach_sdio(struct mmc_host *host);
64 65
65 /* Module parameters */ 66 /* Module parameters */
66 extern int use_spi_crc; 67 extern int use_spi_crc;
67 68
68 /* Debugfs information for hosts and cards */ 69 /* Debugfs information for hosts and cards */
69 void mmc_add_host_debugfs(struct mmc_host *host); 70 void mmc_add_host_debugfs(struct mmc_host *host);
70 void mmc_remove_host_debugfs(struct mmc_host *host); 71 void mmc_remove_host_debugfs(struct mmc_host *host);
71 72
72 void mmc_add_card_debugfs(struct mmc_card *card); 73 void mmc_add_card_debugfs(struct mmc_card *card);
73 void mmc_remove_card_debugfs(struct mmc_card *card); 74 void mmc_remove_card_debugfs(struct mmc_card *card);
74 75
75 #endif 76 #endif
76 77
77 78
drivers/mmc/core/mmc.c
1 /* 1 /*
2 * linux/drivers/mmc/core/mmc.c 2 * linux/drivers/mmc/core/mmc.c
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as 9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. 10 * published by the Free Software Foundation.
11 */ 11 */
12 12
13 #include <linux/err.h> 13 #include <linux/err.h>
14 #include <linux/slab.h> 14 #include <linux/slab.h>
15 15
16 #include <linux/mmc/host.h> 16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h> 17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h> 18 #include <linux/mmc/mmc.h>
19 19
20 #include "core.h" 20 #include "core.h"
21 #include "bus.h" 21 #include "bus.h"
22 #include "mmc_ops.h" 22 #include "mmc_ops.h"
23 #include "sd_ops.h" 23 #include "sd_ops.h"
24 24
25 static const unsigned int tran_exp[] = { 25 static const unsigned int tran_exp[] = {
26 10000, 100000, 1000000, 10000000, 26 10000, 100000, 1000000, 10000000,
27 0, 0, 0, 0 27 0, 0, 0, 0
28 }; 28 };
29 29
30 static const unsigned char tran_mant[] = { 30 static const unsigned char tran_mant[] = {
31 0, 10, 12, 13, 15, 20, 25, 30, 31 0, 10, 12, 13, 15, 20, 25, 30,
32 35, 40, 45, 50, 55, 60, 70, 80, 32 35, 40, 45, 50, 55, 60, 70, 80,
33 }; 33 };
34 34
35 static const unsigned int tacc_exp[] = { 35 static const unsigned int tacc_exp[] = {
36 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 36 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
37 }; 37 };
38 38
39 static const unsigned int tacc_mant[] = { 39 static const unsigned int tacc_mant[] = {
40 0, 10, 12, 13, 15, 20, 25, 30, 40 0, 10, 12, 13, 15, 20, 25, 30,
41 35, 40, 45, 50, 55, 60, 70, 80, 41 35, 40, 45, 50, 55, 60, 70, 80,
42 }; 42 };
43 43
44 #define UNSTUFF_BITS(resp,start,size) \ 44 #define UNSTUFF_BITS(resp,start,size) \
45 ({ \ 45 ({ \
46 const int __size = size; \ 46 const int __size = size; \
47 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 47 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
48 const int __off = 3 - ((start) / 32); \ 48 const int __off = 3 - ((start) / 32); \
49 const int __shft = (start) & 31; \ 49 const int __shft = (start) & 31; \
50 u32 __res; \ 50 u32 __res; \
51 \ 51 \
52 __res = resp[__off] >> __shft; \ 52 __res = resp[__off] >> __shft; \
53 if (__size + __shft > 32) \ 53 if (__size + __shft > 32) \
54 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 54 __res |= resp[__off-1] << ((32 - __shft) % 32); \
55 __res & __mask; \ 55 __res & __mask; \
56 }) 56 })
57 57
58 /* 58 /*
59 * Given the decoded CSD structure, decode the raw CID to our CID structure. 59 * Given the decoded CSD structure, decode the raw CID to our CID structure.
60 */ 60 */
61 static int mmc_decode_cid(struct mmc_card *card) 61 static int mmc_decode_cid(struct mmc_card *card)
62 { 62 {
63 u32 *resp = card->raw_cid; 63 u32 *resp = card->raw_cid;
64 64
65 /* 65 /*
66 * The selection of the format here is based upon published 66 * The selection of the format here is based upon published
67 * specs from sandisk and from what people have reported. 67 * specs from sandisk and from what people have reported.
68 */ 68 */
69 switch (card->csd.mmca_vsn) { 69 switch (card->csd.mmca_vsn) {
70 case 0: /* MMC v1.0 - v1.2 */ 70 case 0: /* MMC v1.0 - v1.2 */
71 case 1: /* MMC v1.4 */ 71 case 1: /* MMC v1.4 */
72 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); 72 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
73 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 73 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
74 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 74 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
75 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 75 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
76 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 76 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
77 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 77 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
78 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 78 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
79 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); 79 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); 80 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); 81 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 16, 24); 82 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
83 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 83 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
84 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 84 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
85 break; 85 break;
86 86
87 case 2: /* MMC v2.0 - v2.2 */ 87 case 2: /* MMC v2.0 - v2.2 */
88 case 3: /* MMC v3.1 - v3.3 */ 88 case 3: /* MMC v3.1 - v3.3 */
89 case 4: /* MMC v4 */ 89 case 4: /* MMC v4 */
90 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 90 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
91 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 91 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
92 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 92 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
93 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 93 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
94 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 94 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
95 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 95 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
96 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 96 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
97 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 97 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
98 card->cid.serial = UNSTUFF_BITS(resp, 16, 32); 98 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
99 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 99 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
100 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 100 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
101 break; 101 break;
102 102
103 default: 103 default:
104 printk(KERN_ERR "%s: card has unknown MMCA version %d\n", 104 printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
105 mmc_hostname(card->host), card->csd.mmca_vsn); 105 mmc_hostname(card->host), card->csd.mmca_vsn);
106 return -EINVAL; 106 return -EINVAL;
107 } 107 }
108 108
109 return 0; 109 return 0;
110 } 110 }
111 111
112 static void mmc_set_erase_size(struct mmc_card *card) 112 static void mmc_set_erase_size(struct mmc_card *card)
113 { 113 {
114 if (card->ext_csd.erase_group_def & 1) 114 if (card->ext_csd.erase_group_def & 1)
115 card->erase_size = card->ext_csd.hc_erase_size; 115 card->erase_size = card->ext_csd.hc_erase_size;
116 else 116 else
117 card->erase_size = card->csd.erase_size; 117 card->erase_size = card->csd.erase_size;
118 118
119 mmc_init_erase(card); 119 mmc_init_erase(card);
120 } 120 }
121 121
122 /* 122 /*
123 * Given a 128-bit response, decode to our card CSD structure. 123 * Given a 128-bit response, decode to our card CSD structure.
124 */ 124 */
125 static int mmc_decode_csd(struct mmc_card *card) 125 static int mmc_decode_csd(struct mmc_card *card)
126 { 126 {
127 struct mmc_csd *csd = &card->csd; 127 struct mmc_csd *csd = &card->csd;
128 unsigned int e, m, a, b; 128 unsigned int e, m, a, b;
129 u32 *resp = card->raw_csd; 129 u32 *resp = card->raw_csd;
130 130
131 /* 131 /*
132 * We only understand CSD structure v1.1 and v1.2. 132 * We only understand CSD structure v1.1 and v1.2.
133 * v1.2 has extra information in bits 15, 11 and 10. 133 * v1.2 has extra information in bits 15, 11 and 10.
134 * We also support eMMC v4.4 & v4.41. 134 * We also support eMMC v4.4 & v4.41.
135 */ 135 */
136 csd->structure = UNSTUFF_BITS(resp, 126, 2); 136 csd->structure = UNSTUFF_BITS(resp, 126, 2);
137 if (csd->structure == 0) { 137 if (csd->structure == 0) {
138 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 138 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
139 mmc_hostname(card->host), csd->structure); 139 mmc_hostname(card->host), csd->structure);
140 return -EINVAL; 140 return -EINVAL;
141 } 141 }
142 142
143 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 143 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
144 m = UNSTUFF_BITS(resp, 115, 4); 144 m = UNSTUFF_BITS(resp, 115, 4);
145 e = UNSTUFF_BITS(resp, 112, 3); 145 e = UNSTUFF_BITS(resp, 112, 3);
146 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 146 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
147 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 147 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
148 148
149 m = UNSTUFF_BITS(resp, 99, 4); 149 m = UNSTUFF_BITS(resp, 99, 4);
150 e = UNSTUFF_BITS(resp, 96, 3); 150 e = UNSTUFF_BITS(resp, 96, 3);
151 csd->max_dtr = tran_exp[e] * tran_mant[m]; 151 csd->max_dtr = tran_exp[e] * tran_mant[m];
152 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 152 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
153 153
154 e = UNSTUFF_BITS(resp, 47, 3); 154 e = UNSTUFF_BITS(resp, 47, 3);
155 m = UNSTUFF_BITS(resp, 62, 12); 155 m = UNSTUFF_BITS(resp, 62, 12);
156 csd->capacity = (1 + m) << (e + 2); 156 csd->capacity = (1 + m) << (e + 2);
157 157
158 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 158 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
159 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 159 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
160 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 160 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
161 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 161 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
162 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 162 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
163 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 163 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
164 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 164 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
165 165
166 if (csd->write_blkbits >= 9) { 166 if (csd->write_blkbits >= 9) {
167 a = UNSTUFF_BITS(resp, 42, 5); 167 a = UNSTUFF_BITS(resp, 42, 5);
168 b = UNSTUFF_BITS(resp, 37, 5); 168 b = UNSTUFF_BITS(resp, 37, 5);
169 csd->erase_size = (a + 1) * (b + 1); 169 csd->erase_size = (a + 1) * (b + 1);
170 csd->erase_size <<= csd->write_blkbits - 9; 170 csd->erase_size <<= csd->write_blkbits - 9;
171 } 171 }
172 172
173 return 0; 173 return 0;
174 } 174 }
175 175
176 /* 176 /*
177 * Read extended CSD. 177 * Read extended CSD.
178 */ 178 */
179 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd) 179 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
180 { 180 {
181 int err; 181 int err;
182 u8 *ext_csd; 182 u8 *ext_csd;
183 183
184 BUG_ON(!card); 184 BUG_ON(!card);
185 BUG_ON(!new_ext_csd); 185 BUG_ON(!new_ext_csd);
186 186
187 *new_ext_csd = NULL; 187 *new_ext_csd = NULL;
188 188
189 if (card->csd.mmca_vsn < CSD_SPEC_VER_4) 189 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
190 return 0; 190 return 0;
191 191
192 /* 192 /*
193 * As the ext_csd is so large and mostly unused, we don't store the 193 * As the ext_csd is so large and mostly unused, we don't store the
194 * raw block in mmc_card. 194 * raw block in mmc_card.
195 */ 195 */
196 ext_csd = kmalloc(512, GFP_KERNEL); 196 ext_csd = kmalloc(512, GFP_KERNEL);
197 if (!ext_csd) { 197 if (!ext_csd) {
198 printk(KERN_ERR "%s: could not allocate a buffer to " 198 printk(KERN_ERR "%s: could not allocate a buffer to "
199 "receive the ext_csd.\n", mmc_hostname(card->host)); 199 "receive the ext_csd.\n", mmc_hostname(card->host));
200 return -ENOMEM; 200 return -ENOMEM;
201 } 201 }
202 202
203 err = mmc_send_ext_csd(card, ext_csd); 203 err = mmc_send_ext_csd(card, ext_csd);
204 if (err) { 204 if (err) {
205 kfree(ext_csd); 205 kfree(ext_csd);
206 *new_ext_csd = NULL; 206 *new_ext_csd = NULL;
207 207
208 /* If the host or the card can't do the switch, 208 /* If the host or the card can't do the switch,
209 * fail more gracefully. */ 209 * fail more gracefully. */
210 if ((err != -EINVAL) 210 if ((err != -EINVAL)
211 && (err != -ENOSYS) 211 && (err != -ENOSYS)
212 && (err != -EFAULT)) 212 && (err != -EFAULT))
213 return err; 213 return err;
214 214
215 /* 215 /*
216 * High capacity cards should have this "magic" size 216 * High capacity cards should have this "magic" size
217 * stored in their CSD. 217 * stored in their CSD.
218 */ 218 */
219 if (card->csd.capacity == (4096 * 512)) { 219 if (card->csd.capacity == (4096 * 512)) {
220 printk(KERN_ERR "%s: unable to read EXT_CSD " 220 printk(KERN_ERR "%s: unable to read EXT_CSD "
221 "on a possible high capacity card. " 221 "on a possible high capacity card. "
222 "Card will be ignored.\n", 222 "Card will be ignored.\n",
223 mmc_hostname(card->host)); 223 mmc_hostname(card->host));
224 } else { 224 } else {
225 printk(KERN_WARNING "%s: unable to read " 225 printk(KERN_WARNING "%s: unable to read "
226 "EXT_CSD, performance might " 226 "EXT_CSD, performance might "
227 "suffer.\n", 227 "suffer.\n",
228 mmc_hostname(card->host)); 228 mmc_hostname(card->host));
229 err = 0; 229 err = 0;
230 } 230 }
231 } else 231 } else
232 *new_ext_csd = ext_csd; 232 *new_ext_csd = ext_csd;
233 233
234 return err; 234 return err;
235 } 235 }
236 236
237 /* 237 /*
238 * Decode extended CSD. 238 * Decode extended CSD.
239 */ 239 */
240 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd) 240 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
241 { 241 {
242 int err = 0; 242 int err = 0;
243 243
244 BUG_ON(!card); 244 BUG_ON(!card);
245 245
246 if (!ext_csd) 246 if (!ext_csd)
247 return 0; 247 return 0;
248 248
249 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ 249 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
250 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE]; 250 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
251 if (card->csd.structure == 3) { 251 if (card->csd.structure == 3) {
252 if (card->ext_csd.raw_ext_csd_structure > 2) { 252 if (card->ext_csd.raw_ext_csd_structure > 2) {
253 printk(KERN_ERR "%s: unrecognised EXT_CSD structure " 253 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
254 "version %d\n", mmc_hostname(card->host), 254 "version %d\n", mmc_hostname(card->host),
255 card->ext_csd.raw_ext_csd_structure); 255 card->ext_csd.raw_ext_csd_structure);
256 err = -EINVAL; 256 err = -EINVAL;
257 goto out; 257 goto out;
258 } 258 }
259 } 259 }
260 260
261 card->ext_csd.rev = ext_csd[EXT_CSD_REV]; 261 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
262 if (card->ext_csd.rev > 6) { 262 if (card->ext_csd.rev > 6) {
263 printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n", 263 printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
264 mmc_hostname(card->host), card->ext_csd.rev); 264 mmc_hostname(card->host), card->ext_csd.rev);
265 err = -EINVAL; 265 err = -EINVAL;
266 goto out; 266 goto out;
267 } 267 }
268 268
269 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0]; 269 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
270 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1]; 270 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
271 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2]; 271 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
272 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3]; 272 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
273 if (card->ext_csd.rev >= 2) { 273 if (card->ext_csd.rev >= 2) {
274 card->ext_csd.sectors = 274 card->ext_csd.sectors =
275 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 275 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
276 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 276 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
277 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 277 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
278 ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 278 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
279 279
280 /* Cards with density > 2GiB are sector addressed */ 280 /* Cards with density > 2GiB are sector addressed */
281 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) 281 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
282 mmc_card_set_blockaddr(card); 282 mmc_card_set_blockaddr(card);
283 } 283 }
284 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE]; 284 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
285 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) { 285 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
286 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 | 286 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
287 EXT_CSD_CARD_TYPE_26: 287 EXT_CSD_CARD_TYPE_26:
288 card->ext_csd.hs_max_dtr = 52000000; 288 card->ext_csd.hs_max_dtr = 52000000;
289 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52; 289 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
290 break; 290 break;
291 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 | 291 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
292 EXT_CSD_CARD_TYPE_26: 292 EXT_CSD_CARD_TYPE_26:
293 card->ext_csd.hs_max_dtr = 52000000; 293 card->ext_csd.hs_max_dtr = 52000000;
294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V; 294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
295 break; 295 break;
296 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 | 296 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
297 EXT_CSD_CARD_TYPE_26: 297 EXT_CSD_CARD_TYPE_26:
298 card->ext_csd.hs_max_dtr = 52000000; 298 card->ext_csd.hs_max_dtr = 52000000;
299 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V; 299 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
300 break; 300 break;
301 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 301 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
302 card->ext_csd.hs_max_dtr = 52000000; 302 card->ext_csd.hs_max_dtr = 52000000;
303 break; 303 break;
304 case EXT_CSD_CARD_TYPE_26: 304 case EXT_CSD_CARD_TYPE_26:
305 card->ext_csd.hs_max_dtr = 26000000; 305 card->ext_csd.hs_max_dtr = 26000000;
306 break; 306 break;
307 default: 307 default:
308 /* MMC v4 spec says this cannot happen */ 308 /* MMC v4 spec says this cannot happen */
309 printk(KERN_WARNING "%s: card is mmc v4 but doesn't " 309 printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
310 "support any high-speed modes.\n", 310 "support any high-speed modes.\n",
311 mmc_hostname(card->host)); 311 mmc_hostname(card->host));
312 } 312 }
313 313
314 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT]; 314 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
315 card->ext_csd.raw_erase_timeout_mult = 315 card->ext_csd.raw_erase_timeout_mult =
316 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; 316 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
317 card->ext_csd.raw_hc_erase_grp_size = 317 card->ext_csd.raw_hc_erase_grp_size =
318 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 318 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
319 if (card->ext_csd.rev >= 3) { 319 if (card->ext_csd.rev >= 3) {
320 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; 320 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
321 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG]; 321 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
322 322
323 /* EXT_CSD value is in units of 10ms, but we store in ms */ 323 /* EXT_CSD value is in units of 10ms, but we store in ms */
324 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME]; 324 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
325 325
326 /* Sleep / awake timeout in 100ns units */ 326 /* Sleep / awake timeout in 100ns units */
327 if (sa_shift > 0 && sa_shift <= 0x17) 327 if (sa_shift > 0 && sa_shift <= 0x17)
328 card->ext_csd.sa_timeout = 328 card->ext_csd.sa_timeout =
329 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; 329 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
330 card->ext_csd.erase_group_def = 330 card->ext_csd.erase_group_def =
331 ext_csd[EXT_CSD_ERASE_GROUP_DEF]; 331 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
332 card->ext_csd.hc_erase_timeout = 300 * 332 card->ext_csd.hc_erase_timeout = 300 *
333 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; 333 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
334 card->ext_csd.hc_erase_size = 334 card->ext_csd.hc_erase_size =
335 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; 335 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
336 336
337 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C]; 337 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
338 338
339 /* 339 /*
340 * There are two boot regions of equal size, defined in 340 * There are two boot regions of equal size, defined in
341 * multiples of 128K. 341 * multiples of 128K.
342 */ 342 */
343 card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17; 343 card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
344 } 344 }
345 345
346 card->ext_csd.raw_hc_erase_gap_size = 346 card->ext_csd.raw_hc_erase_gap_size =
347 ext_csd[EXT_CSD_PARTITION_ATTRIBUTE]; 347 ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
348 card->ext_csd.raw_sec_trim_mult = 348 card->ext_csd.raw_sec_trim_mult =
349 ext_csd[EXT_CSD_SEC_TRIM_MULT]; 349 ext_csd[EXT_CSD_SEC_TRIM_MULT];
350 card->ext_csd.raw_sec_erase_mult = 350 card->ext_csd.raw_sec_erase_mult =
351 ext_csd[EXT_CSD_SEC_ERASE_MULT]; 351 ext_csd[EXT_CSD_SEC_ERASE_MULT];
352 card->ext_csd.raw_sec_feature_support = 352 card->ext_csd.raw_sec_feature_support =
353 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; 353 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
354 card->ext_csd.raw_trim_mult = 354 card->ext_csd.raw_trim_mult =
355 ext_csd[EXT_CSD_TRIM_MULT]; 355 ext_csd[EXT_CSD_TRIM_MULT];
356 if (card->ext_csd.rev >= 4) { 356 if (card->ext_csd.rev >= 4) {
357 /* 357 /*
358 * Enhanced area feature support -- check whether the eMMC 358 * Enhanced area feature support -- check whether the eMMC
359 * card has the Enhanced area enabled. If so, export enhanced 359 * card has the Enhanced area enabled. If so, export enhanced
360 * area offset and size to user by adding sysfs interface. 360 * area offset and size to user by adding sysfs interface.
361 */ 361 */
362 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) && 362 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
363 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) { 363 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
364 u8 hc_erase_grp_sz = 364 u8 hc_erase_grp_sz =
365 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 365 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
366 u8 hc_wp_grp_sz = 366 u8 hc_wp_grp_sz =
367 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 367 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
368 368
369 card->ext_csd.enhanced_area_en = 1; 369 card->ext_csd.enhanced_area_en = 1;
370 /* 370 /*
371 * calculate the enhanced data area offset, in bytes 371 * calculate the enhanced data area offset, in bytes
372 */ 372 */
373 card->ext_csd.enhanced_area_offset = 373 card->ext_csd.enhanced_area_offset =
374 (ext_csd[139] << 24) + (ext_csd[138] << 16) + 374 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
375 (ext_csd[137] << 8) + ext_csd[136]; 375 (ext_csd[137] << 8) + ext_csd[136];
376 if (mmc_card_blockaddr(card)) 376 if (mmc_card_blockaddr(card))
377 card->ext_csd.enhanced_area_offset <<= 9; 377 card->ext_csd.enhanced_area_offset <<= 9;
378 /* 378 /*
379 * calculate the enhanced data area size, in kilobytes 379 * calculate the enhanced data area size, in kilobytes
380 */ 380 */
381 card->ext_csd.enhanced_area_size = 381 card->ext_csd.enhanced_area_size =
382 (ext_csd[142] << 16) + (ext_csd[141] << 8) + 382 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
383 ext_csd[140]; 383 ext_csd[140];
384 card->ext_csd.enhanced_area_size *= 384 card->ext_csd.enhanced_area_size *=
385 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz); 385 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
386 card->ext_csd.enhanced_area_size <<= 9; 386 card->ext_csd.enhanced_area_size <<= 9;
387 } else { 387 } else {
388 /* 388 /*
389 * If the enhanced area is not enabled, disable these 389 * If the enhanced area is not enabled, disable these
390 * device attributes. 390 * device attributes.
391 */ 391 */
392 card->ext_csd.enhanced_area_offset = -EINVAL; 392 card->ext_csd.enhanced_area_offset = -EINVAL;
393 card->ext_csd.enhanced_area_size = -EINVAL; 393 card->ext_csd.enhanced_area_size = -EINVAL;
394 } 394 }
395 card->ext_csd.sec_trim_mult = 395 card->ext_csd.sec_trim_mult =
396 ext_csd[EXT_CSD_SEC_TRIM_MULT]; 396 ext_csd[EXT_CSD_SEC_TRIM_MULT];
397 card->ext_csd.sec_erase_mult = 397 card->ext_csd.sec_erase_mult =
398 ext_csd[EXT_CSD_SEC_ERASE_MULT]; 398 ext_csd[EXT_CSD_SEC_ERASE_MULT];
399 card->ext_csd.sec_feature_support = 399 card->ext_csd.sec_feature_support =
400 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; 400 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
401 card->ext_csd.trim_timeout = 300 * 401 card->ext_csd.trim_timeout = 300 *
402 ext_csd[EXT_CSD_TRIM_MULT]; 402 ext_csd[EXT_CSD_TRIM_MULT];
403 } 403 }
404 404
405 if (card->ext_csd.rev >= 5) 405 if (card->ext_csd.rev >= 5)
406 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM]; 406 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
407 407
408 if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) 408 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
409 card->erased_byte = 0xFF; 409 card->erased_byte = 0xFF;
410 else 410 else
411 card->erased_byte = 0x0; 411 card->erased_byte = 0x0;
412 412
413 out: 413 out:
414 return err; 414 return err;
415 } 415 }
416 416
417 static inline void mmc_free_ext_csd(u8 *ext_csd) 417 static inline void mmc_free_ext_csd(u8 *ext_csd)
418 { 418 {
419 kfree(ext_csd); 419 kfree(ext_csd);
420 } 420 }
421 421
422 422
423 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width) 423 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
424 { 424 {
425 u8 *bw_ext_csd; 425 u8 *bw_ext_csd;
426 int err; 426 int err;
427 427
428 if (bus_width == MMC_BUS_WIDTH_1) 428 if (bus_width == MMC_BUS_WIDTH_1)
429 return 0; 429 return 0;
430 430
431 err = mmc_get_ext_csd(card, &bw_ext_csd); 431 err = mmc_get_ext_csd(card, &bw_ext_csd);
432 432
433 if (err || bw_ext_csd == NULL) { 433 if (err || bw_ext_csd == NULL) {
434 if (bus_width != MMC_BUS_WIDTH_1) 434 if (bus_width != MMC_BUS_WIDTH_1)
435 err = -EINVAL; 435 err = -EINVAL;
436 goto out; 436 goto out;
437 } 437 }
438 438
439 if (bus_width == MMC_BUS_WIDTH_1) 439 if (bus_width == MMC_BUS_WIDTH_1)
440 goto out; 440 goto out;
441 441
442 /* only compare read only fields */ 442 /* only compare read only fields */
443 err = (!(card->ext_csd.raw_partition_support == 443 err = (!(card->ext_csd.raw_partition_support ==
444 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) && 444 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
445 (card->ext_csd.raw_erased_mem_count == 445 (card->ext_csd.raw_erased_mem_count ==
446 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) && 446 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
447 (card->ext_csd.rev == 447 (card->ext_csd.rev ==
448 bw_ext_csd[EXT_CSD_REV]) && 448 bw_ext_csd[EXT_CSD_REV]) &&
449 (card->ext_csd.raw_ext_csd_structure == 449 (card->ext_csd.raw_ext_csd_structure ==
450 bw_ext_csd[EXT_CSD_STRUCTURE]) && 450 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
451 (card->ext_csd.raw_card_type == 451 (card->ext_csd.raw_card_type ==
452 bw_ext_csd[EXT_CSD_CARD_TYPE]) && 452 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
453 (card->ext_csd.raw_s_a_timeout == 453 (card->ext_csd.raw_s_a_timeout ==
454 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) && 454 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
455 (card->ext_csd.raw_hc_erase_gap_size == 455 (card->ext_csd.raw_hc_erase_gap_size ==
456 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) && 456 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
457 (card->ext_csd.raw_erase_timeout_mult == 457 (card->ext_csd.raw_erase_timeout_mult ==
458 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) && 458 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
459 (card->ext_csd.raw_hc_erase_grp_size == 459 (card->ext_csd.raw_hc_erase_grp_size ==
460 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) && 460 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
461 (card->ext_csd.raw_sec_trim_mult == 461 (card->ext_csd.raw_sec_trim_mult ==
462 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) && 462 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
463 (card->ext_csd.raw_sec_erase_mult == 463 (card->ext_csd.raw_sec_erase_mult ==
464 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) && 464 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
465 (card->ext_csd.raw_sec_feature_support == 465 (card->ext_csd.raw_sec_feature_support ==
466 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) && 466 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
467 (card->ext_csd.raw_trim_mult == 467 (card->ext_csd.raw_trim_mult ==
468 bw_ext_csd[EXT_CSD_TRIM_MULT]) && 468 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
469 (card->ext_csd.raw_sectors[0] == 469 (card->ext_csd.raw_sectors[0] ==
470 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) && 470 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
471 (card->ext_csd.raw_sectors[1] == 471 (card->ext_csd.raw_sectors[1] ==
472 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) && 472 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
473 (card->ext_csd.raw_sectors[2] == 473 (card->ext_csd.raw_sectors[2] ==
474 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) && 474 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
475 (card->ext_csd.raw_sectors[3] == 475 (card->ext_csd.raw_sectors[3] ==
476 bw_ext_csd[EXT_CSD_SEC_CNT + 3])); 476 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
477 if (err) 477 if (err)
478 err = -EINVAL; 478 err = -EINVAL;
479 479
480 out: 480 out:
481 mmc_free_ext_csd(bw_ext_csd); 481 mmc_free_ext_csd(bw_ext_csd);
482 return err; 482 return err;
483 } 483 }
484 484
485 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 485 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
486 card->raw_cid[2], card->raw_cid[3]); 486 card->raw_cid[2], card->raw_cid[3]);
487 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 487 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
488 card->raw_csd[2], card->raw_csd[3]); 488 card->raw_csd[2], card->raw_csd[3]);
489 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 489 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
490 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 490 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
491 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 491 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
492 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 492 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
493 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 493 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
494 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 494 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
495 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 495 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
496 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 496 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
497 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 497 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
498 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n", 498 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
499 card->ext_csd.enhanced_area_offset); 499 card->ext_csd.enhanced_area_offset);
500 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size); 500 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
501 501
502 static struct attribute *mmc_std_attrs[] = { 502 static struct attribute *mmc_std_attrs[] = {
503 &dev_attr_cid.attr, 503 &dev_attr_cid.attr,
504 &dev_attr_csd.attr, 504 &dev_attr_csd.attr,
505 &dev_attr_date.attr, 505 &dev_attr_date.attr,
506 &dev_attr_erase_size.attr, 506 &dev_attr_erase_size.attr,
507 &dev_attr_preferred_erase_size.attr, 507 &dev_attr_preferred_erase_size.attr,
508 &dev_attr_fwrev.attr, 508 &dev_attr_fwrev.attr,
509 &dev_attr_hwrev.attr, 509 &dev_attr_hwrev.attr,
510 &dev_attr_manfid.attr, 510 &dev_attr_manfid.attr,
511 &dev_attr_name.attr, 511 &dev_attr_name.attr,
512 &dev_attr_oemid.attr, 512 &dev_attr_oemid.attr,
513 &dev_attr_serial.attr, 513 &dev_attr_serial.attr,
514 &dev_attr_enhanced_area_offset.attr, 514 &dev_attr_enhanced_area_offset.attr,
515 &dev_attr_enhanced_area_size.attr, 515 &dev_attr_enhanced_area_size.attr,
516 NULL, 516 NULL,
517 }; 517 };
518 518
519 static struct attribute_group mmc_std_attr_group = { 519 static struct attribute_group mmc_std_attr_group = {
520 .attrs = mmc_std_attrs, 520 .attrs = mmc_std_attrs,
521 }; 521 };
522 522
523 static const struct attribute_group *mmc_attr_groups[] = { 523 static const struct attribute_group *mmc_attr_groups[] = {
524 &mmc_std_attr_group, 524 &mmc_std_attr_group,
525 NULL, 525 NULL,
526 }; 526 };
527 527
528 static struct device_type mmc_type = { 528 static struct device_type mmc_type = {
529 .groups = mmc_attr_groups, 529 .groups = mmc_attr_groups,
530 }; 530 };
531 531
532 /* 532 /*
533 * Handle the detection and initialisation of a card. 533 * Handle the detection and initialisation of a card.
534 * 534 *
535 * In the case of a resume, "oldcard" will contain the card 535 * In the case of a resume, "oldcard" will contain the card
536 * we're trying to reinitialise. 536 * we're trying to reinitialise.
537 */ 537 */
538 static int mmc_init_card(struct mmc_host *host, u32 ocr, 538 static int mmc_init_card(struct mmc_host *host, u32 ocr,
539 struct mmc_card *oldcard) 539 struct mmc_card *oldcard)
540 { 540 {
541 struct mmc_card *card; 541 struct mmc_card *card;
542 int err, ddr = 0; 542 int err, ddr = 0;
543 u32 cid[4]; 543 u32 cid[4];
544 unsigned int max_dtr; 544 unsigned int max_dtr;
545 u32 rocr; 545 u32 rocr;
546 u8 *ext_csd = NULL; 546 u8 *ext_csd = NULL;
547 547
548 BUG_ON(!host); 548 BUG_ON(!host);
549 WARN_ON(!host->claimed); 549 WARN_ON(!host->claimed);
550 550
551 /* 551 /*
552 * Since we're changing the OCR value, we seem to 552 * Since we're changing the OCR value, we seem to
553 * need to tell some cards to go back to the idle 553 * need to tell some cards to go back to the idle
554 * state. We wait 1ms to give cards time to 554 * state. We wait 1ms to give cards time to
555 * respond. 555 * respond.
556 * mmc_go_idle is needed for eMMC that are asleep 556 * mmc_go_idle is needed for eMMC that are asleep
557 */ 557 */
558 mmc_go_idle(host); 558 mmc_go_idle(host);
559 559
560 /* The extra bit indicates that we support high capacity */ 560 /* The extra bit indicates that we support high capacity */
561 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr); 561 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
562 if (err) 562 if (err)
563 goto err; 563 goto err;
564 564
565 /* 565 /*
566 * For SPI, enable CRC as appropriate. 566 * For SPI, enable CRC as appropriate.
567 */ 567 */
568 if (mmc_host_is_spi(host)) { 568 if (mmc_host_is_spi(host)) {
569 err = mmc_spi_set_crc(host, use_spi_crc); 569 err = mmc_spi_set_crc(host, use_spi_crc);
570 if (err) 570 if (err)
571 goto err; 571 goto err;
572 } 572 }
573 573
574 /* 574 /*
575 * Fetch CID from card. 575 * Fetch CID from card.
576 */ 576 */
577 if (mmc_host_is_spi(host)) 577 if (mmc_host_is_spi(host))
578 err = mmc_send_cid(host, cid); 578 err = mmc_send_cid(host, cid);
579 else 579 else
580 err = mmc_all_send_cid(host, cid); 580 err = mmc_all_send_cid(host, cid);
581 if (err) 581 if (err)
582 goto err; 582 goto err;
583 583
584 if (oldcard) { 584 if (oldcard) {
585 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { 585 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
586 err = -ENOENT; 586 err = -ENOENT;
587 goto err; 587 goto err;
588 } 588 }
589 589
590 card = oldcard; 590 card = oldcard;
591 } else { 591 } else {
592 /* 592 /*
593 * Allocate card structure. 593 * Allocate card structure.
594 */ 594 */
595 card = mmc_alloc_card(host, &mmc_type); 595 card = mmc_alloc_card(host, &mmc_type);
596 if (IS_ERR(card)) { 596 if (IS_ERR(card)) {
597 err = PTR_ERR(card); 597 err = PTR_ERR(card);
598 goto err; 598 goto err;
599 } 599 }
600 600
601 card->type = MMC_TYPE_MMC; 601 card->type = MMC_TYPE_MMC;
602 card->rca = 1; 602 card->rca = 1;
603 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 603 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
604 } 604 }
605 605
606 /* 606 /*
607 * For native busses: set card RCA and quit open drain mode. 607 * For native busses: set card RCA and quit open drain mode.
608 */ 608 */
609 if (!mmc_host_is_spi(host)) { 609 if (!mmc_host_is_spi(host)) {
610 err = mmc_set_relative_addr(card); 610 err = mmc_set_relative_addr(card);
611 if (err) 611 if (err)
612 goto free_card; 612 goto free_card;
613 613
614 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 614 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
615 } 615 }
616 616
617 if (!oldcard) { 617 if (!oldcard) {
618 /* 618 /*
619 * Fetch CSD from card. 619 * Fetch CSD from card.
620 */ 620 */
621 err = mmc_send_csd(card, card->raw_csd); 621 err = mmc_send_csd(card, card->raw_csd);
622 if (err) 622 if (err)
623 goto free_card; 623 goto free_card;
624 624
625 err = mmc_decode_csd(card); 625 err = mmc_decode_csd(card);
626 if (err) 626 if (err)
627 goto free_card; 627 goto free_card;
628 err = mmc_decode_cid(card); 628 err = mmc_decode_cid(card);
629 if (err) 629 if (err)
630 goto free_card; 630 goto free_card;
631 } 631 }
632 632
633 /* 633 /*
634 * Select card, as all following commands rely on that. 634 * Select card, as all following commands rely on that.
635 */ 635 */
636 if (!mmc_host_is_spi(host)) { 636 if (!mmc_host_is_spi(host)) {
637 err = mmc_select_card(card); 637 err = mmc_select_card(card);
638 if (err) 638 if (err)
639 goto free_card; 639 goto free_card;
640 } 640 }
641 641
642 if (!oldcard) { 642 if (!oldcard) {
643 /* 643 /*
644 * Fetch and process extended CSD. 644 * Fetch and process extended CSD.
645 */ 645 */
646 646
647 err = mmc_get_ext_csd(card, &ext_csd); 647 err = mmc_get_ext_csd(card, &ext_csd);
648 if (err) 648 if (err)
649 goto free_card; 649 goto free_card;
650 err = mmc_read_ext_csd(card, ext_csd); 650 err = mmc_read_ext_csd(card, ext_csd);
651 if (err) 651 if (err)
652 goto free_card; 652 goto free_card;
653 653
654 /* If doing byte addressing, check if required to do sector 654 /* If doing byte addressing, check if required to do sector
655 * addressing. Handle the case of <2GB cards needing sector 655 * addressing. Handle the case of <2GB cards needing sector
656 * addressing. See section 8.1 JEDEC Standard JED84-A441; 656 * addressing. See section 8.1 JEDEC Standard JED84-A441;
657 * ocr register has bit 30 set for sector addressing. 657 * ocr register has bit 30 set for sector addressing.
658 */ 658 */
659 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30))) 659 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
660 mmc_card_set_blockaddr(card); 660 mmc_card_set_blockaddr(card);
661 661
662 /* Erase size depends on CSD and Extended CSD */ 662 /* Erase size depends on CSD and Extended CSD */
663 mmc_set_erase_size(card); 663 mmc_set_erase_size(card);
664 } 664 }
665 665
666 /* 666 /*
667 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF 667 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
668 * bit. This bit will be lost every time after a reset or power off. 668 * bit. This bit will be lost every time after a reset or power off.
669 */ 669 */
670 if (card->ext_csd.enhanced_area_en) { 670 if (card->ext_csd.enhanced_area_en) {
671 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 671 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
672 EXT_CSD_ERASE_GROUP_DEF, 1, 0); 672 EXT_CSD_ERASE_GROUP_DEF, 1, 0);
673 673
674 if (err && err != -EBADMSG) 674 if (err && err != -EBADMSG)
675 goto free_card; 675 goto free_card;
676 676
677 if (err) { 677 if (err) {
678 err = 0; 678 err = 0;
679 /* 679 /*
680 * Just disable enhanced area off & sz 680 * Just disable enhanced area off & sz
681 * will try to enable ERASE_GROUP_DEF 681 * will try to enable ERASE_GROUP_DEF
682 * during next time reinit 682 * during next time reinit
683 */ 683 */
684 card->ext_csd.enhanced_area_offset = -EINVAL; 684 card->ext_csd.enhanced_area_offset = -EINVAL;
685 card->ext_csd.enhanced_area_size = -EINVAL; 685 card->ext_csd.enhanced_area_size = -EINVAL;
686 } else { 686 } else {
687 card->ext_csd.erase_group_def = 1; 687 card->ext_csd.erase_group_def = 1;
688 /* 688 /*
689 * enable ERASE_GRP_DEF successfully. 689 * enable ERASE_GRP_DEF successfully.
690 * This will affect the erase size, so 690 * This will affect the erase size, so
691 * here need to reset erase size 691 * here need to reset erase size
692 */ 692 */
693 mmc_set_erase_size(card); 693 mmc_set_erase_size(card);
694 } 694 }
695 } 695 }
696 696
697 /* 697 /*
698 * Ensure eMMC user default partition is enabled 698 * Ensure eMMC user default partition is enabled
699 */ 699 */
700 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) { 700 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
701 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; 701 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
702 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG, 702 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
703 card->ext_csd.part_config, 703 card->ext_csd.part_config,
704 card->ext_csd.part_time); 704 card->ext_csd.part_time);
705 if (err && err != -EBADMSG) 705 if (err && err != -EBADMSG)
706 goto free_card; 706 goto free_card;
707 } 707 }
708 708
709 /* 709 /*
710 * Activate high speed (if supported) 710 * Activate high speed (if supported)
711 */ 711 */
712 if ((card->ext_csd.hs_max_dtr != 0) && 712 if ((card->ext_csd.hs_max_dtr != 0) &&
713 (host->caps & MMC_CAP_MMC_HIGHSPEED)) { 713 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
714 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 714 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
715 EXT_CSD_HS_TIMING, 1, 0); 715 EXT_CSD_HS_TIMING, 1, 0);
716 if (err && err != -EBADMSG) 716 if (err && err != -EBADMSG)
717 goto free_card; 717 goto free_card;
718 718
719 if (err) { 719 if (err) {
720 printk(KERN_WARNING "%s: switch to highspeed failed\n", 720 printk(KERN_WARNING "%s: switch to highspeed failed\n",
721 mmc_hostname(card->host)); 721 mmc_hostname(card->host));
722 err = 0; 722 err = 0;
723 } else { 723 } else {
724 mmc_card_set_highspeed(card); 724 mmc_card_set_highspeed(card);
725 mmc_set_timing(card->host, MMC_TIMING_MMC_HS); 725 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
726 } 726 }
727 } 727 }
728 728
729 /* 729 /*
730 * Compute bus speed. 730 * Compute bus speed.
731 */ 731 */
732 max_dtr = (unsigned int)-1; 732 max_dtr = (unsigned int)-1;
733 733
734 if (mmc_card_highspeed(card)) { 734 if (mmc_card_highspeed(card)) {
735 if (max_dtr > card->ext_csd.hs_max_dtr) 735 if (max_dtr > card->ext_csd.hs_max_dtr)
736 max_dtr = card->ext_csd.hs_max_dtr; 736 max_dtr = card->ext_csd.hs_max_dtr;
737 } else if (max_dtr > card->csd.max_dtr) { 737 } else if (max_dtr > card->csd.max_dtr) {
738 max_dtr = card->csd.max_dtr; 738 max_dtr = card->csd.max_dtr;
739 } 739 }
740 740
741 mmc_set_clock(host, max_dtr); 741 mmc_set_clock(host, max_dtr);
742 742
743 /* 743 /*
744 * Indicate DDR mode (if supported). 744 * Indicate DDR mode (if supported).
745 */ 745 */
746 if (mmc_card_highspeed(card)) { 746 if (mmc_card_highspeed(card)) {
747 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) 747 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
748 && ((host->caps & (MMC_CAP_1_8V_DDR | 748 && ((host->caps & (MMC_CAP_1_8V_DDR |
749 MMC_CAP_UHS_DDR50)) 749 MMC_CAP_UHS_DDR50))
750 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50))) 750 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
751 ddr = MMC_1_8V_DDR_MODE; 751 ddr = MMC_1_8V_DDR_MODE;
752 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) 752 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
753 && ((host->caps & (MMC_CAP_1_2V_DDR | 753 && ((host->caps & (MMC_CAP_1_2V_DDR |
754 MMC_CAP_UHS_DDR50)) 754 MMC_CAP_UHS_DDR50))
755 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50))) 755 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
756 ddr = MMC_1_2V_DDR_MODE; 756 ddr = MMC_1_2V_DDR_MODE;
757 } 757 }
758 758
759 /* 759 /*
760 * Activate wide bus and DDR (if supported). 760 * Activate wide bus and DDR (if supported).
761 */ 761 */
762 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) && 762 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
763 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { 763 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
764 static unsigned ext_csd_bits[][2] = { 764 static unsigned ext_csd_bits[][2] = {
765 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 }, 765 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
766 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 }, 766 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
767 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 }, 767 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
768 }; 768 };
769 static unsigned bus_widths[] = { 769 static unsigned bus_widths[] = {
770 MMC_BUS_WIDTH_8, 770 MMC_BUS_WIDTH_8,
771 MMC_BUS_WIDTH_4, 771 MMC_BUS_WIDTH_4,
772 MMC_BUS_WIDTH_1 772 MMC_BUS_WIDTH_1
773 }; 773 };
774 unsigned idx, bus_width = 0; 774 unsigned idx, bus_width = 0;
775 775
776 if (host->caps & MMC_CAP_8_BIT_DATA) 776 if (host->caps & MMC_CAP_8_BIT_DATA)
777 idx = 0; 777 idx = 0;
778 else 778 else
779 idx = 1; 779 idx = 1;
780 for (; idx < ARRAY_SIZE(bus_widths); idx++) { 780 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
781 bus_width = bus_widths[idx]; 781 bus_width = bus_widths[idx];
782 if (bus_width == MMC_BUS_WIDTH_1) 782 if (bus_width == MMC_BUS_WIDTH_1)
783 ddr = 0; /* no DDR for 1-bit width */ 783 ddr = 0; /* no DDR for 1-bit width */
784 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 784 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
785 EXT_CSD_BUS_WIDTH, 785 EXT_CSD_BUS_WIDTH,
786 ext_csd_bits[idx][0], 786 ext_csd_bits[idx][0],
787 0); 787 0);
788 if (!err) { 788 if (!err) {
789 mmc_set_bus_width(card->host, bus_width); 789 mmc_set_bus_width(card->host, bus_width);
790 790
791 /* 791 /*
792 * If controller can't handle bus width test, 792 * If controller can't handle bus width test,
793 * compare ext_csd previously read in 1 bit mode 793 * compare ext_csd previously read in 1 bit mode
794 * against ext_csd at new bus width 794 * against ext_csd at new bus width
795 */ 795 */
796 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST)) 796 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
797 err = mmc_compare_ext_csds(card, 797 err = mmc_compare_ext_csds(card,
798 bus_width); 798 bus_width);
799 else 799 else
800 err = mmc_bus_test(card, bus_width); 800 err = mmc_bus_test(card, bus_width);
801 if (!err) 801 if (!err)
802 break; 802 break;
803 } 803 }
804 } 804 }
805 805
806 if (!err && ddr) { 806 if (!err && ddr) {
807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
808 EXT_CSD_BUS_WIDTH, 808 EXT_CSD_BUS_WIDTH,
809 ext_csd_bits[idx][1], 809 ext_csd_bits[idx][1],
810 0); 810 0);
811 } 811 }
812 if (err) { 812 if (err) {
813 printk(KERN_WARNING "%s: switch to bus width %d ddr %d " 813 printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
814 "failed\n", mmc_hostname(card->host), 814 "failed\n", mmc_hostname(card->host),
815 1 << bus_width, ddr); 815 1 << bus_width, ddr);
816 goto free_card; 816 goto free_card;
817 } else if (ddr) { 817 } else if (ddr) {
818 /* 818 /*
819 * eMMC cards can support 3.3V to 1.2V i/o (vccq) 819 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
820 * signaling. 820 * signaling.
821 * 821 *
822 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq. 822 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
823 * 823 *
824 * 1.8V vccq at 3.3V core voltage (vcc) is not required 824 * 1.8V vccq at 3.3V core voltage (vcc) is not required
825 * in the JEDEC spec for DDR. 825 * in the JEDEC spec for DDR.
826 * 826 *
827 * Do not force change in vccq since we are obviously 827 * Do not force change in vccq since we are obviously
828 * working and no change to vccq is needed. 828 * working and no change to vccq is needed.
829 * 829 *
830 * WARNING: eMMC rules are NOT the same as SD DDR 830 * WARNING: eMMC rules are NOT the same as SD DDR
831 */ 831 */
832 if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) { 832 if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
833 err = mmc_set_signal_voltage(host, 833 err = mmc_set_signal_voltage(host,
834 MMC_SIGNAL_VOLTAGE_120, 0); 834 MMC_SIGNAL_VOLTAGE_120, 0);
835 if (err) 835 if (err)
836 goto err; 836 goto err;
837 } 837 }
838 mmc_card_set_ddr_mode(card); 838 mmc_card_set_ddr_mode(card);
839 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50); 839 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
840 mmc_set_bus_width(card->host, bus_width); 840 mmc_set_bus_width(card->host, bus_width);
841 } 841 }
842 } 842 }
843 843
844 if (!oldcard) 844 if (!oldcard)
845 host->card = card; 845 host->card = card;
846 846
847 mmc_free_ext_csd(ext_csd); 847 mmc_free_ext_csd(ext_csd);
848 return 0; 848 return 0;
849 849
850 free_card: 850 free_card:
851 if (!oldcard) 851 if (!oldcard)
852 mmc_remove_card(card); 852 mmc_remove_card(card);
853 err: 853 err:
854 mmc_free_ext_csd(ext_csd); 854 mmc_free_ext_csd(ext_csd);
855 855
856 return err; 856 return err;
857 } 857 }
858 858
859 /* 859 /*
860 * Host is being removed. Free up the current card. 860 * Host is being removed. Free up the current card.
861 */ 861 */
862 static void mmc_remove(struct mmc_host *host) 862 static void mmc_remove(struct mmc_host *host)
863 { 863 {
864 BUG_ON(!host); 864 BUG_ON(!host);
865 BUG_ON(!host->card); 865 BUG_ON(!host->card);
866 866
867 mmc_remove_card(host->card); 867 mmc_remove_card(host->card);
868 host->card = NULL; 868 host->card = NULL;
869 } 869 }
870 870
871 /* 871 /*
872 * Card detection callback from host. 872 * Card detection callback from host.
873 */ 873 */
874 static void mmc_detect(struct mmc_host *host) 874 static void mmc_detect(struct mmc_host *host)
875 { 875 {
876 int err; 876 int err;
877 877
878 BUG_ON(!host); 878 BUG_ON(!host);
879 BUG_ON(!host->card); 879 BUG_ON(!host->card);
880 880
881 mmc_claim_host(host); 881 mmc_claim_host(host);
882 882
883 /* 883 /*
884 * Just check if our card has been removed. 884 * Just check if our card has been removed.
885 */ 885 */
886 err = mmc_send_status(host->card, NULL); 886 err = mmc_send_status(host->card, NULL);
887 887
888 mmc_release_host(host); 888 mmc_release_host(host);
889 889
890 if (err) { 890 if (err) {
891 mmc_remove(host); 891 mmc_remove(host);
892 892
893 mmc_claim_host(host); 893 mmc_claim_host(host);
894 mmc_detach_bus(host); 894 mmc_detach_bus(host);
895 mmc_power_off(host);
895 mmc_release_host(host); 896 mmc_release_host(host);
896 } 897 }
897 } 898 }
898 899
899 /* 900 /*
900 * Suspend callback from host. 901 * Suspend callback from host.
901 */ 902 */
902 static int mmc_suspend(struct mmc_host *host) 903 static int mmc_suspend(struct mmc_host *host)
903 { 904 {
904 int err = 0; 905 int err = 0;
905 906
906 BUG_ON(!host); 907 BUG_ON(!host);
907 BUG_ON(!host->card); 908 BUG_ON(!host->card);
908 909
909 mmc_claim_host(host); 910 mmc_claim_host(host);
910 if (mmc_card_can_sleep(host)) 911 if (mmc_card_can_sleep(host))
911 err = mmc_card_sleep(host); 912 err = mmc_card_sleep(host);
912 else if (!mmc_host_is_spi(host)) 913 else if (!mmc_host_is_spi(host))
913 mmc_deselect_cards(host); 914 mmc_deselect_cards(host);
914 host->card->state &= ~MMC_STATE_HIGHSPEED; 915 host->card->state &= ~MMC_STATE_HIGHSPEED;
915 mmc_release_host(host); 916 mmc_release_host(host);
916 917
917 return err; 918 return err;
918 } 919 }
919 920
920 /* 921 /*
921 * Resume callback from host. 922 * Resume callback from host.
922 * 923 *
923 * This function tries to determine if the same card is still present 924 * This function tries to determine if the same card is still present
924 * and, if so, restore all state to it. 925 * and, if so, restore all state to it.
925 */ 926 */
926 static int mmc_resume(struct mmc_host *host) 927 static int mmc_resume(struct mmc_host *host)
927 { 928 {
928 int err; 929 int err;
929 930
930 BUG_ON(!host); 931 BUG_ON(!host);
931 BUG_ON(!host->card); 932 BUG_ON(!host->card);
932 933
933 mmc_claim_host(host); 934 mmc_claim_host(host);
934 err = mmc_init_card(host, host->ocr, host->card); 935 err = mmc_init_card(host, host->ocr, host->card);
935 mmc_release_host(host); 936 mmc_release_host(host);
936 937
937 return err; 938 return err;
938 } 939 }
939 940
940 static int mmc_power_restore(struct mmc_host *host) 941 static int mmc_power_restore(struct mmc_host *host)
941 { 942 {
942 int ret; 943 int ret;
943 944
944 host->card->state &= ~MMC_STATE_HIGHSPEED; 945 host->card->state &= ~MMC_STATE_HIGHSPEED;
945 mmc_claim_host(host); 946 mmc_claim_host(host);
946 ret = mmc_init_card(host, host->ocr, host->card); 947 ret = mmc_init_card(host, host->ocr, host->card);
947 mmc_release_host(host); 948 mmc_release_host(host);
948 949
949 return ret; 950 return ret;
950 } 951 }
951 952
952 static int mmc_sleep(struct mmc_host *host) 953 static int mmc_sleep(struct mmc_host *host)
953 { 954 {
954 struct mmc_card *card = host->card; 955 struct mmc_card *card = host->card;
955 int err = -ENOSYS; 956 int err = -ENOSYS;
956 957
957 if (card && card->ext_csd.rev >= 3) { 958 if (card && card->ext_csd.rev >= 3) {
958 err = mmc_card_sleepawake(host, 1); 959 err = mmc_card_sleepawake(host, 1);
959 if (err < 0) 960 if (err < 0)
960 pr_debug("%s: Error %d while putting card into sleep", 961 pr_debug("%s: Error %d while putting card into sleep",
961 mmc_hostname(host), err); 962 mmc_hostname(host), err);
962 } 963 }
963 964
964 return err; 965 return err;
965 } 966 }
966 967
967 static int mmc_awake(struct mmc_host *host) 968 static int mmc_awake(struct mmc_host *host)
968 { 969 {
969 struct mmc_card *card = host->card; 970 struct mmc_card *card = host->card;
970 int err = -ENOSYS; 971 int err = -ENOSYS;
971 972
972 if (card && card->ext_csd.rev >= 3) { 973 if (card && card->ext_csd.rev >= 3) {
973 err = mmc_card_sleepawake(host, 0); 974 err = mmc_card_sleepawake(host, 0);
974 if (err < 0) 975 if (err < 0)
975 pr_debug("%s: Error %d while awaking sleeping card", 976 pr_debug("%s: Error %d while awaking sleeping card",
976 mmc_hostname(host), err); 977 mmc_hostname(host), err);
977 } 978 }
978 979
979 return err; 980 return err;
980 } 981 }
981 982
982 static const struct mmc_bus_ops mmc_ops = { 983 static const struct mmc_bus_ops mmc_ops = {
983 .awake = mmc_awake, 984 .awake = mmc_awake,
984 .sleep = mmc_sleep, 985 .sleep = mmc_sleep,
985 .remove = mmc_remove, 986 .remove = mmc_remove,
986 .detect = mmc_detect, 987 .detect = mmc_detect,
987 .suspend = NULL, 988 .suspend = NULL,
988 .resume = NULL, 989 .resume = NULL,
989 .power_restore = mmc_power_restore, 990 .power_restore = mmc_power_restore,
990 }; 991 };
991 992
992 static const struct mmc_bus_ops mmc_ops_unsafe = { 993 static const struct mmc_bus_ops mmc_ops_unsafe = {
993 .awake = mmc_awake, 994 .awake = mmc_awake,
994 .sleep = mmc_sleep, 995 .sleep = mmc_sleep,
995 .remove = mmc_remove, 996 .remove = mmc_remove,
996 .detect = mmc_detect, 997 .detect = mmc_detect,
997 .suspend = mmc_suspend, 998 .suspend = mmc_suspend,
998 .resume = mmc_resume, 999 .resume = mmc_resume,
999 .power_restore = mmc_power_restore, 1000 .power_restore = mmc_power_restore,
1000 }; 1001 };
1001 1002
1002 static void mmc_attach_bus_ops(struct mmc_host *host) 1003 static void mmc_attach_bus_ops(struct mmc_host *host)
1003 { 1004 {
1004 const struct mmc_bus_ops *bus_ops; 1005 const struct mmc_bus_ops *bus_ops;
1005 1006
1006 if (!mmc_card_is_removable(host)) 1007 if (!mmc_card_is_removable(host))
1007 bus_ops = &mmc_ops_unsafe; 1008 bus_ops = &mmc_ops_unsafe;
1008 else 1009 else
1009 bus_ops = &mmc_ops; 1010 bus_ops = &mmc_ops;
1010 mmc_attach_bus(host, bus_ops); 1011 mmc_attach_bus(host, bus_ops);
1011 } 1012 }
1012 1013
1013 /* 1014 /*
1014 * Starting point for MMC card init. 1015 * Starting point for MMC card init.
1015 */ 1016 */
1016 int mmc_attach_mmc(struct mmc_host *host) 1017 int mmc_attach_mmc(struct mmc_host *host)
1017 { 1018 {
1018 int err; 1019 int err;
1019 u32 ocr; 1020 u32 ocr;
1020 1021
1021 BUG_ON(!host); 1022 BUG_ON(!host);
1022 WARN_ON(!host->claimed); 1023 WARN_ON(!host->claimed);
1023 1024
1024 err = mmc_send_op_cond(host, 0, &ocr); 1025 err = mmc_send_op_cond(host, 0, &ocr);
1025 if (err) 1026 if (err)
1026 return err; 1027 return err;
1027 1028
1028 mmc_attach_bus_ops(host); 1029 mmc_attach_bus_ops(host);
1029 if (host->ocr_avail_mmc) 1030 if (host->ocr_avail_mmc)
1030 host->ocr_avail = host->ocr_avail_mmc; 1031 host->ocr_avail = host->ocr_avail_mmc;
1031 1032
1032 /* 1033 /*
1033 * We need to get OCR a different way for SPI. 1034 * We need to get OCR a different way for SPI.
1034 */ 1035 */
1035 if (mmc_host_is_spi(host)) { 1036 if (mmc_host_is_spi(host)) {
1036 err = mmc_spi_read_ocr(host, 1, &ocr); 1037 err = mmc_spi_read_ocr(host, 1, &ocr);
1037 if (err) 1038 if (err)
1038 goto err; 1039 goto err;
1039 } 1040 }
1040 1041
1041 /* 1042 /*
1042 * Sanity check the voltages that the card claims to 1043 * Sanity check the voltages that the card claims to
1043 * support. 1044 * support.
1044 */ 1045 */
1045 if (ocr & 0x7F) { 1046 if (ocr & 0x7F) {
1046 printk(KERN_WARNING "%s: card claims to support voltages " 1047 printk(KERN_WARNING "%s: card claims to support voltages "
1047 "below the defined range. These will be ignored.\n", 1048 "below the defined range. These will be ignored.\n",
1048 mmc_hostname(host)); 1049 mmc_hostname(host));
1049 ocr &= ~0x7F; 1050 ocr &= ~0x7F;
1050 } 1051 }
1051 1052
1052 host->ocr = mmc_select_voltage(host, ocr); 1053 host->ocr = mmc_select_voltage(host, ocr);
1053 1054
1054 /* 1055 /*
1055 * Can we support the voltage of the card? 1056 * Can we support the voltage of the card?
1056 */ 1057 */
1057 if (!host->ocr) { 1058 if (!host->ocr) {
1058 err = -EINVAL; 1059 err = -EINVAL;
1059 goto err; 1060 goto err;
1060 } 1061 }
1061 1062
1062 /* 1063 /*
1063 * Detect and init the card. 1064 * Detect and init the card.
1064 */ 1065 */
1065 err = mmc_init_card(host, host->ocr, NULL); 1066 err = mmc_init_card(host, host->ocr, NULL);
1066 if (err) 1067 if (err)
1067 goto err; 1068 goto err;
1068 1069
1069 mmc_release_host(host); 1070 mmc_release_host(host);
1070 err = mmc_add_card(host->card); 1071 err = mmc_add_card(host->card);
1071 mmc_claim_host(host); 1072 mmc_claim_host(host);
1072 if (err) 1073 if (err)
1073 goto remove_card; 1074 goto remove_card;
1074 1075
1075 return 0; 1076 return 0;
1076 1077
1077 remove_card: 1078 remove_card:
1078 mmc_release_host(host); 1079 mmc_release_host(host);
1079 mmc_remove_card(host->card); 1080 mmc_remove_card(host->card);
1080 mmc_claim_host(host); 1081 mmc_claim_host(host);
1081 host->card = NULL; 1082 host->card = NULL;
1082 err: 1083 err:
1083 mmc_detach_bus(host); 1084 mmc_detach_bus(host);
1084 1085
1085 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n", 1086 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
1086 mmc_hostname(host), err); 1087 mmc_hostname(host), err);
1087 1088
1088 return err; 1089 return err;
1089 } 1090 }
1090 1091
drivers/mmc/core/sd.c
1 /* 1 /*
2 * linux/drivers/mmc/core/sd.c 2 * linux/drivers/mmc/core/sd.c
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as 9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. 10 * published by the Free Software Foundation.
11 */ 11 */
12 12
13 #include <linux/err.h> 13 #include <linux/err.h>
14 #include <linux/slab.h> 14 #include <linux/slab.h>
15 15
16 #include <linux/mmc/host.h> 16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h> 17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h> 18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h> 19 #include <linux/mmc/sd.h>
20 20
21 #include "core.h" 21 #include "core.h"
22 #include "bus.h" 22 #include "bus.h"
23 #include "mmc_ops.h" 23 #include "mmc_ops.h"
24 #include "sd.h" 24 #include "sd.h"
25 #include "sd_ops.h" 25 #include "sd_ops.h"
26 26
27 static const unsigned int tran_exp[] = { 27 static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000, 28 10000, 100000, 1000000, 10000000,
29 0, 0, 0, 0 29 0, 0, 0, 0
30 }; 30 };
31 31
32 static const unsigned char tran_mant[] = { 32 static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30, 33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80, 34 35, 40, 45, 50, 55, 60, 70, 80,
35 }; 35 };
36 36
37 static const unsigned int tacc_exp[] = { 37 static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
39 }; 39 };
40 40
41 static const unsigned int tacc_mant[] = { 41 static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30, 42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80, 43 35, 40, 45, 50, 55, 60, 70, 80,
44 }; 44 };
45 45
46 #define UNSTUFF_BITS(resp,start,size) \ 46 #define UNSTUFF_BITS(resp,start,size) \
47 ({ \ 47 ({ \
48 const int __size = size; \ 48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \ 50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \ 51 const int __shft = (start) & 31; \
52 u32 __res; \ 52 u32 __res; \
53 \ 53 \
54 __res = resp[__off] >> __shft; \ 54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \ 55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
57 __res & __mask; \ 57 __res & __mask; \
58 }) 58 })
59 59
60 /* 60 /*
61 * Given the decoded CSD structure, decode the raw CID to our CID structure. 61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
62 */ 62 */
63 void mmc_decode_cid(struct mmc_card *card) 63 void mmc_decode_cid(struct mmc_card *card)
64 { 64 {
65 u32 *resp = card->raw_cid; 65 u32 *resp = card->raw_cid;
66 66
67 memset(&card->cid, 0, sizeof(struct mmc_cid)); 67 memset(&card->cid, 0, sizeof(struct mmc_cid));
68 68
69 /* 69 /*
70 * SD doesn't currently have a version field so we will 70 * SD doesn't currently have a version field so we will
71 * have to assume we can parse this. 71 * have to assume we can parse this.
72 */ 72 */
73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 80 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
83 card->cid.year = UNSTUFF_BITS(resp, 12, 8); 83 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
84 card->cid.month = UNSTUFF_BITS(resp, 8, 4); 84 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
85 85
86 card->cid.year += 2000; /* SD cards year offset */ 86 card->cid.year += 2000; /* SD cards year offset */
87 } 87 }
88 88
89 /* 89 /*
90 * Given a 128-bit response, decode to our card CSD structure. 90 * Given a 128-bit response, decode to our card CSD structure.
91 */ 91 */
92 static int mmc_decode_csd(struct mmc_card *card) 92 static int mmc_decode_csd(struct mmc_card *card)
93 { 93 {
94 struct mmc_csd *csd = &card->csd; 94 struct mmc_csd *csd = &card->csd;
95 unsigned int e, m, csd_struct; 95 unsigned int e, m, csd_struct;
96 u32 *resp = card->raw_csd; 96 u32 *resp = card->raw_csd;
97 97
98 csd_struct = UNSTUFF_BITS(resp, 126, 2); 98 csd_struct = UNSTUFF_BITS(resp, 126, 2);
99 99
100 switch (csd_struct) { 100 switch (csd_struct) {
101 case 0: 101 case 0:
102 m = UNSTUFF_BITS(resp, 115, 4); 102 m = UNSTUFF_BITS(resp, 115, 4);
103 e = UNSTUFF_BITS(resp, 112, 3); 103 e = UNSTUFF_BITS(resp, 112, 3);
104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
106 106
107 m = UNSTUFF_BITS(resp, 99, 4); 107 m = UNSTUFF_BITS(resp, 99, 4);
108 e = UNSTUFF_BITS(resp, 96, 3); 108 e = UNSTUFF_BITS(resp, 96, 3);
109 csd->max_dtr = tran_exp[e] * tran_mant[m]; 109 csd->max_dtr = tran_exp[e] * tran_mant[m];
110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
111 111
112 e = UNSTUFF_BITS(resp, 47, 3); 112 e = UNSTUFF_BITS(resp, 47, 3);
113 m = UNSTUFF_BITS(resp, 62, 12); 113 m = UNSTUFF_BITS(resp, 62, 12);
114 csd->capacity = (1 + m) << (e + 2); 114 csd->capacity = (1 + m) << (e + 2);
115 115
116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
123 123
124 if (UNSTUFF_BITS(resp, 46, 1)) { 124 if (UNSTUFF_BITS(resp, 46, 1)) {
125 csd->erase_size = 1; 125 csd->erase_size = 1;
126 } else if (csd->write_blkbits >= 9) { 126 } else if (csd->write_blkbits >= 9) {
127 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; 127 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
128 csd->erase_size <<= csd->write_blkbits - 9; 128 csd->erase_size <<= csd->write_blkbits - 9;
129 } 129 }
130 break; 130 break;
131 case 1: 131 case 1:
132 /* 132 /*
133 * This is a block-addressed SDHC or SDXC card. Most 133 * This is a block-addressed SDHC or SDXC card. Most
134 * interesting fields are unused and have fixed 134 * interesting fields are unused and have fixed
135 * values. To avoid getting tripped by buggy cards, 135 * values. To avoid getting tripped by buggy cards,
136 * we assume those fixed values ourselves. 136 * we assume those fixed values ourselves.
137 */ 137 */
138 mmc_card_set_blockaddr(card); 138 mmc_card_set_blockaddr(card);
139 139
140 csd->tacc_ns = 0; /* Unused */ 140 csd->tacc_ns = 0; /* Unused */
141 csd->tacc_clks = 0; /* Unused */ 141 csd->tacc_clks = 0; /* Unused */
142 142
143 m = UNSTUFF_BITS(resp, 99, 4); 143 m = UNSTUFF_BITS(resp, 99, 4);
144 e = UNSTUFF_BITS(resp, 96, 3); 144 e = UNSTUFF_BITS(resp, 96, 3);
145 csd->max_dtr = tran_exp[e] * tran_mant[m]; 145 csd->max_dtr = tran_exp[e] * tran_mant[m];
146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
147 csd->c_size = UNSTUFF_BITS(resp, 48, 22); 147 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
148 148
149 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */ 149 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
150 if (csd->c_size >= 0xFFFF) 150 if (csd->c_size >= 0xFFFF)
151 mmc_card_set_ext_capacity(card); 151 mmc_card_set_ext_capacity(card);
152 152
153 m = UNSTUFF_BITS(resp, 48, 22); 153 m = UNSTUFF_BITS(resp, 48, 22);
154 csd->capacity = (1 + m) << 10; 154 csd->capacity = (1 + m) << 10;
155 155
156 csd->read_blkbits = 9; 156 csd->read_blkbits = 9;
157 csd->read_partial = 0; 157 csd->read_partial = 0;
158 csd->write_misalign = 0; 158 csd->write_misalign = 0;
159 csd->read_misalign = 0; 159 csd->read_misalign = 0;
160 csd->r2w_factor = 4; /* Unused */ 160 csd->r2w_factor = 4; /* Unused */
161 csd->write_blkbits = 9; 161 csd->write_blkbits = 9;
162 csd->write_partial = 0; 162 csd->write_partial = 0;
163 csd->erase_size = 1; 163 csd->erase_size = 1;
164 break; 164 break;
165 default: 165 default:
166 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 166 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
167 mmc_hostname(card->host), csd_struct); 167 mmc_hostname(card->host), csd_struct);
168 return -EINVAL; 168 return -EINVAL;
169 } 169 }
170 170
171 card->erase_size = csd->erase_size; 171 card->erase_size = csd->erase_size;
172 172
173 return 0; 173 return 0;
174 } 174 }
175 175
176 /* 176 /*
177 * Given a 64-bit response, decode to our card SCR structure. 177 * Given a 64-bit response, decode to our card SCR structure.
178 */ 178 */
179 static int mmc_decode_scr(struct mmc_card *card) 179 static int mmc_decode_scr(struct mmc_card *card)
180 { 180 {
181 struct sd_scr *scr = &card->scr; 181 struct sd_scr *scr = &card->scr;
182 unsigned int scr_struct; 182 unsigned int scr_struct;
183 u32 resp[4]; 183 u32 resp[4];
184 184
185 resp[3] = card->raw_scr[1]; 185 resp[3] = card->raw_scr[1];
186 resp[2] = card->raw_scr[0]; 186 resp[2] = card->raw_scr[0];
187 187
188 scr_struct = UNSTUFF_BITS(resp, 60, 4); 188 scr_struct = UNSTUFF_BITS(resp, 60, 4);
189 if (scr_struct != 0) { 189 if (scr_struct != 0) {
190 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n", 190 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
191 mmc_hostname(card->host), scr_struct); 191 mmc_hostname(card->host), scr_struct);
192 return -EINVAL; 192 return -EINVAL;
193 } 193 }
194 194
195 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 195 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
196 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 196 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
197 if (scr->sda_vsn == SCR_SPEC_VER_2) 197 if (scr->sda_vsn == SCR_SPEC_VER_2)
198 /* Check if Physical Layer Spec v3.0 is supported */ 198 /* Check if Physical Layer Spec v3.0 is supported */
199 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1); 199 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
200 200
201 if (UNSTUFF_BITS(resp, 55, 1)) 201 if (UNSTUFF_BITS(resp, 55, 1))
202 card->erased_byte = 0xFF; 202 card->erased_byte = 0xFF;
203 else 203 else
204 card->erased_byte = 0x0; 204 card->erased_byte = 0x0;
205 205
206 if (scr->sda_spec3) 206 if (scr->sda_spec3)
207 scr->cmds = UNSTUFF_BITS(resp, 32, 2); 207 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
208 return 0; 208 return 0;
209 } 209 }
210 210
211 /* 211 /*
212 * Fetch and process SD Status register. 212 * Fetch and process SD Status register.
213 */ 213 */
214 static int mmc_read_ssr(struct mmc_card *card) 214 static int mmc_read_ssr(struct mmc_card *card)
215 { 215 {
216 unsigned int au, es, et, eo; 216 unsigned int au, es, et, eo;
217 int err, i; 217 int err, i;
218 u32 *ssr; 218 u32 *ssr;
219 219
220 if (!(card->csd.cmdclass & CCC_APP_SPEC)) { 220 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
221 printk(KERN_WARNING "%s: card lacks mandatory SD Status " 221 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
222 "function.\n", mmc_hostname(card->host)); 222 "function.\n", mmc_hostname(card->host));
223 return 0; 223 return 0;
224 } 224 }
225 225
226 ssr = kmalloc(64, GFP_KERNEL); 226 ssr = kmalloc(64, GFP_KERNEL);
227 if (!ssr) 227 if (!ssr)
228 return -ENOMEM; 228 return -ENOMEM;
229 229
230 err = mmc_app_sd_status(card, ssr); 230 err = mmc_app_sd_status(card, ssr);
231 if (err) { 231 if (err) {
232 printk(KERN_WARNING "%s: problem reading SD Status " 232 printk(KERN_WARNING "%s: problem reading SD Status "
233 "register.\n", mmc_hostname(card->host)); 233 "register.\n", mmc_hostname(card->host));
234 err = 0; 234 err = 0;
235 goto out; 235 goto out;
236 } 236 }
237 237
238 for (i = 0; i < 16; i++) 238 for (i = 0; i < 16; i++)
239 ssr[i] = be32_to_cpu(ssr[i]); 239 ssr[i] = be32_to_cpu(ssr[i]);
240 240
241 /* 241 /*
242 * UNSTUFF_BITS only works with four u32s so we have to offset the 242 * UNSTUFF_BITS only works with four u32s so we have to offset the
243 * bitfield positions accordingly. 243 * bitfield positions accordingly.
244 */ 244 */
245 au = UNSTUFF_BITS(ssr, 428 - 384, 4); 245 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
246 if (au > 0 || au <= 9) { 246 if (au > 0 || au <= 9) {
247 card->ssr.au = 1 << (au + 4); 247 card->ssr.au = 1 << (au + 4);
248 es = UNSTUFF_BITS(ssr, 408 - 384, 16); 248 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
249 et = UNSTUFF_BITS(ssr, 402 - 384, 6); 249 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
250 eo = UNSTUFF_BITS(ssr, 400 - 384, 2); 250 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
251 if (es && et) { 251 if (es && et) {
252 card->ssr.erase_timeout = (et * 1000) / es; 252 card->ssr.erase_timeout = (et * 1000) / es;
253 card->ssr.erase_offset = eo * 1000; 253 card->ssr.erase_offset = eo * 1000;
254 } 254 }
255 } else { 255 } else {
256 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit " 256 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
257 "size.\n", mmc_hostname(card->host)); 257 "size.\n", mmc_hostname(card->host));
258 } 258 }
259 out: 259 out:
260 kfree(ssr); 260 kfree(ssr);
261 return err; 261 return err;
262 } 262 }
263 263
264 /* 264 /*
265 * Fetches and decodes switch information 265 * Fetches and decodes switch information
266 */ 266 */
267 static int mmc_read_switch(struct mmc_card *card) 267 static int mmc_read_switch(struct mmc_card *card)
268 { 268 {
269 int err; 269 int err;
270 u8 *status; 270 u8 *status;
271 271
272 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 272 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
273 return 0; 273 return 0;
274 274
275 if (!(card->csd.cmdclass & CCC_SWITCH)) { 275 if (!(card->csd.cmdclass & CCC_SWITCH)) {
276 printk(KERN_WARNING "%s: card lacks mandatory switch " 276 printk(KERN_WARNING "%s: card lacks mandatory switch "
277 "function, performance might suffer.\n", 277 "function, performance might suffer.\n",
278 mmc_hostname(card->host)); 278 mmc_hostname(card->host));
279 return 0; 279 return 0;
280 } 280 }
281 281
282 err = -EIO; 282 err = -EIO;
283 283
284 status = kmalloc(64, GFP_KERNEL); 284 status = kmalloc(64, GFP_KERNEL);
285 if (!status) { 285 if (!status) {
286 printk(KERN_ERR "%s: could not allocate a buffer for " 286 printk(KERN_ERR "%s: could not allocate a buffer for "
287 "switch capabilities.\n", 287 "switch capabilities.\n",
288 mmc_hostname(card->host)); 288 mmc_hostname(card->host));
289 return -ENOMEM; 289 return -ENOMEM;
290 } 290 }
291 291
292 /* Find out the supported Bus Speed Modes. */ 292 /* Find out the supported Bus Speed Modes. */
293 err = mmc_sd_switch(card, 0, 0, 1, status); 293 err = mmc_sd_switch(card, 0, 0, 1, status);
294 if (err) { 294 if (err) {
295 /* 295 /*
296 * If the host or the card can't do the switch, 296 * If the host or the card can't do the switch,
297 * fail more gracefully. 297 * fail more gracefully.
298 */ 298 */
299 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) 299 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
300 goto out; 300 goto out;
301 301
302 printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n", 302 printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n",
303 mmc_hostname(card->host)); 303 mmc_hostname(card->host));
304 err = 0; 304 err = 0;
305 305
306 goto out; 306 goto out;
307 } 307 }
308 308
309 if (card->scr.sda_spec3) { 309 if (card->scr.sda_spec3) {
310 card->sw_caps.sd3_bus_mode = status[13]; 310 card->sw_caps.sd3_bus_mode = status[13];
311 311
312 /* Find out Driver Strengths supported by the card */ 312 /* Find out Driver Strengths supported by the card */
313 err = mmc_sd_switch(card, 0, 2, 1, status); 313 err = mmc_sd_switch(card, 0, 2, 1, status);
314 if (err) { 314 if (err) {
315 /* 315 /*
316 * If the host or the card can't do the switch, 316 * If the host or the card can't do the switch,
317 * fail more gracefully. 317 * fail more gracefully.
318 */ 318 */
319 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) 319 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
320 goto out; 320 goto out;
321 321
322 printk(KERN_WARNING "%s: problem reading " 322 printk(KERN_WARNING "%s: problem reading "
323 "Driver Strength.\n", 323 "Driver Strength.\n",
324 mmc_hostname(card->host)); 324 mmc_hostname(card->host));
325 err = 0; 325 err = 0;
326 326
327 goto out; 327 goto out;
328 } 328 }
329 329
330 card->sw_caps.sd3_drv_type = status[9]; 330 card->sw_caps.sd3_drv_type = status[9];
331 331
332 /* Find out Current Limits supported by the card */ 332 /* Find out Current Limits supported by the card */
333 err = mmc_sd_switch(card, 0, 3, 1, status); 333 err = mmc_sd_switch(card, 0, 3, 1, status);
334 if (err) { 334 if (err) {
335 /* 335 /*
336 * If the host or the card can't do the switch, 336 * If the host or the card can't do the switch,
337 * fail more gracefully. 337 * fail more gracefully.
338 */ 338 */
339 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) 339 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
340 goto out; 340 goto out;
341 341
342 printk(KERN_WARNING "%s: problem reading " 342 printk(KERN_WARNING "%s: problem reading "
343 "Current Limit.\n", 343 "Current Limit.\n",
344 mmc_hostname(card->host)); 344 mmc_hostname(card->host));
345 err = 0; 345 err = 0;
346 346
347 goto out; 347 goto out;
348 } 348 }
349 349
350 card->sw_caps.sd3_curr_limit = status[7]; 350 card->sw_caps.sd3_curr_limit = status[7];
351 } else { 351 } else {
352 if (status[13] & 0x02) 352 if (status[13] & 0x02)
353 card->sw_caps.hs_max_dtr = 50000000; 353 card->sw_caps.hs_max_dtr = 50000000;
354 } 354 }
355 355
356 out: 356 out:
357 kfree(status); 357 kfree(status);
358 358
359 return err; 359 return err;
360 } 360 }
361 361
362 /* 362 /*
363 * Test if the card supports high-speed mode and, if so, switch to it. 363 * Test if the card supports high-speed mode and, if so, switch to it.
364 */ 364 */
365 int mmc_sd_switch_hs(struct mmc_card *card) 365 int mmc_sd_switch_hs(struct mmc_card *card)
366 { 366 {
367 int err; 367 int err;
368 u8 *status; 368 u8 *status;
369 369
370 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 370 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
371 return 0; 371 return 0;
372 372
373 if (!(card->csd.cmdclass & CCC_SWITCH)) 373 if (!(card->csd.cmdclass & CCC_SWITCH))
374 return 0; 374 return 0;
375 375
376 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 376 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
377 return 0; 377 return 0;
378 378
379 if (card->sw_caps.hs_max_dtr == 0) 379 if (card->sw_caps.hs_max_dtr == 0)
380 return 0; 380 return 0;
381 381
382 err = -EIO; 382 err = -EIO;
383 383
384 status = kmalloc(64, GFP_KERNEL); 384 status = kmalloc(64, GFP_KERNEL);
385 if (!status) { 385 if (!status) {
386 printk(KERN_ERR "%s: could not allocate a buffer for " 386 printk(KERN_ERR "%s: could not allocate a buffer for "
387 "switch capabilities.\n", mmc_hostname(card->host)); 387 "switch capabilities.\n", mmc_hostname(card->host));
388 return -ENOMEM; 388 return -ENOMEM;
389 } 389 }
390 390
391 err = mmc_sd_switch(card, 1, 0, 1, status); 391 err = mmc_sd_switch(card, 1, 0, 1, status);
392 if (err) 392 if (err)
393 goto out; 393 goto out;
394 394
395 if ((status[16] & 0xF) != 1) { 395 if ((status[16] & 0xF) != 1) {
396 printk(KERN_WARNING "%s: Problem switching card " 396 printk(KERN_WARNING "%s: Problem switching card "
397 "into high-speed mode!\n", 397 "into high-speed mode!\n",
398 mmc_hostname(card->host)); 398 mmc_hostname(card->host));
399 err = 0; 399 err = 0;
400 } else { 400 } else {
401 err = 1; 401 err = 1;
402 } 402 }
403 403
404 out: 404 out:
405 kfree(status); 405 kfree(status);
406 406
407 return err; 407 return err;
408 } 408 }
409 409
410 static int sd_select_driver_type(struct mmc_card *card, u8 *status) 410 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
411 { 411 {
412 int host_drv_type = SD_DRIVER_TYPE_B; 412 int host_drv_type = SD_DRIVER_TYPE_B;
413 int card_drv_type = SD_DRIVER_TYPE_B; 413 int card_drv_type = SD_DRIVER_TYPE_B;
414 int drive_strength; 414 int drive_strength;
415 int err; 415 int err;
416 416
417 /* 417 /*
418 * If the host doesn't support any of the Driver Types A,C or D, 418 * If the host doesn't support any of the Driver Types A,C or D,
419 * or there is no board specific handler then default Driver 419 * or there is no board specific handler then default Driver
420 * Type B is used. 420 * Type B is used.
421 */ 421 */
422 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C 422 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
423 | MMC_CAP_DRIVER_TYPE_D))) 423 | MMC_CAP_DRIVER_TYPE_D)))
424 return 0; 424 return 0;
425 425
426 if (!card->host->ops->select_drive_strength) 426 if (!card->host->ops->select_drive_strength)
427 return 0; 427 return 0;
428 428
429 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) 429 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
430 host_drv_type |= SD_DRIVER_TYPE_A; 430 host_drv_type |= SD_DRIVER_TYPE_A;
431 431
432 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) 432 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
433 host_drv_type |= SD_DRIVER_TYPE_C; 433 host_drv_type |= SD_DRIVER_TYPE_C;
434 434
435 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D) 435 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
436 host_drv_type |= SD_DRIVER_TYPE_D; 436 host_drv_type |= SD_DRIVER_TYPE_D;
437 437
438 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A) 438 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
439 card_drv_type |= SD_DRIVER_TYPE_A; 439 card_drv_type |= SD_DRIVER_TYPE_A;
440 440
441 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C) 441 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
442 card_drv_type |= SD_DRIVER_TYPE_C; 442 card_drv_type |= SD_DRIVER_TYPE_C;
443 443
444 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D) 444 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
445 card_drv_type |= SD_DRIVER_TYPE_D; 445 card_drv_type |= SD_DRIVER_TYPE_D;
446 446
447 /* 447 /*
448 * The drive strength that the hardware can support 448 * The drive strength that the hardware can support
449 * depends on the board design. Pass the appropriate 449 * depends on the board design. Pass the appropriate
450 * information and let the hardware specific code 450 * information and let the hardware specific code
451 * return what is possible given the options 451 * return what is possible given the options
452 */ 452 */
453 drive_strength = card->host->ops->select_drive_strength( 453 drive_strength = card->host->ops->select_drive_strength(
454 card->sw_caps.uhs_max_dtr, 454 card->sw_caps.uhs_max_dtr,
455 host_drv_type, card_drv_type); 455 host_drv_type, card_drv_type);
456 456
457 err = mmc_sd_switch(card, 1, 2, drive_strength, status); 457 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
458 if (err) 458 if (err)
459 return err; 459 return err;
460 460
461 if ((status[15] & 0xF) != drive_strength) { 461 if ((status[15] & 0xF) != drive_strength) {
462 printk(KERN_WARNING "%s: Problem setting drive strength!\n", 462 printk(KERN_WARNING "%s: Problem setting drive strength!\n",
463 mmc_hostname(card->host)); 463 mmc_hostname(card->host));
464 return 0; 464 return 0;
465 } 465 }
466 466
467 mmc_set_driver_type(card->host, drive_strength); 467 mmc_set_driver_type(card->host, drive_strength);
468 468
469 return 0; 469 return 0;
470 } 470 }
471 471
472 static void sd_update_bus_speed_mode(struct mmc_card *card) 472 static void sd_update_bus_speed_mode(struct mmc_card *card)
473 { 473 {
474 /* 474 /*
475 * If the host doesn't support any of the UHS-I modes, fallback on 475 * If the host doesn't support any of the UHS-I modes, fallback on
476 * default speed. 476 * default speed.
477 */ 477 */
478 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 478 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
479 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) { 479 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
480 card->sd_bus_speed = 0; 480 card->sd_bus_speed = 0;
481 return; 481 return;
482 } 482 }
483 483
484 if ((card->host->caps & MMC_CAP_UHS_SDR104) && 484 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
485 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { 485 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
486 card->sd_bus_speed = UHS_SDR104_BUS_SPEED; 486 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
487 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && 487 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
488 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { 488 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
489 card->sd_bus_speed = UHS_DDR50_BUS_SPEED; 489 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
491 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & 491 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
492 SD_MODE_UHS_SDR50)) { 492 SD_MODE_UHS_SDR50)) {
493 card->sd_bus_speed = UHS_SDR50_BUS_SPEED; 493 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
494 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 494 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
495 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && 495 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
496 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { 496 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
497 card->sd_bus_speed = UHS_SDR25_BUS_SPEED; 497 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
498 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 498 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
499 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | 499 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
500 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & 500 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
501 SD_MODE_UHS_SDR12)) { 501 SD_MODE_UHS_SDR12)) {
502 card->sd_bus_speed = UHS_SDR12_BUS_SPEED; 502 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
503 } 503 }
504 } 504 }
505 505
506 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status) 506 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
507 { 507 {
508 int err; 508 int err;
509 unsigned int timing = 0; 509 unsigned int timing = 0;
510 510
511 switch (card->sd_bus_speed) { 511 switch (card->sd_bus_speed) {
512 case UHS_SDR104_BUS_SPEED: 512 case UHS_SDR104_BUS_SPEED:
513 timing = MMC_TIMING_UHS_SDR104; 513 timing = MMC_TIMING_UHS_SDR104;
514 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; 514 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
515 break; 515 break;
516 case UHS_DDR50_BUS_SPEED: 516 case UHS_DDR50_BUS_SPEED:
517 timing = MMC_TIMING_UHS_DDR50; 517 timing = MMC_TIMING_UHS_DDR50;
518 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; 518 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
519 break; 519 break;
520 case UHS_SDR50_BUS_SPEED: 520 case UHS_SDR50_BUS_SPEED:
521 timing = MMC_TIMING_UHS_SDR50; 521 timing = MMC_TIMING_UHS_SDR50;
522 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; 522 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
523 break; 523 break;
524 case UHS_SDR25_BUS_SPEED: 524 case UHS_SDR25_BUS_SPEED:
525 timing = MMC_TIMING_UHS_SDR25; 525 timing = MMC_TIMING_UHS_SDR25;
526 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; 526 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
527 break; 527 break;
528 case UHS_SDR12_BUS_SPEED: 528 case UHS_SDR12_BUS_SPEED:
529 timing = MMC_TIMING_UHS_SDR12; 529 timing = MMC_TIMING_UHS_SDR12;
530 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; 530 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
531 break; 531 break;
532 default: 532 default:
533 return 0; 533 return 0;
534 } 534 }
535 535
536 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status); 536 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
537 if (err) 537 if (err)
538 return err; 538 return err;
539 539
540 if ((status[16] & 0xF) != card->sd_bus_speed) 540 if ((status[16] & 0xF) != card->sd_bus_speed)
541 printk(KERN_WARNING "%s: Problem setting bus speed mode!\n", 541 printk(KERN_WARNING "%s: Problem setting bus speed mode!\n",
542 mmc_hostname(card->host)); 542 mmc_hostname(card->host));
543 else { 543 else {
544 mmc_set_timing(card->host, timing); 544 mmc_set_timing(card->host, timing);
545 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); 545 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
546 } 546 }
547 547
548 return 0; 548 return 0;
549 } 549 }
550 550
551 static int sd_set_current_limit(struct mmc_card *card, u8 *status) 551 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
552 { 552 {
553 int current_limit = 0; 553 int current_limit = 0;
554 int err; 554 int err;
555 555
556 /* 556 /*
557 * Current limit switch is only defined for SDR50, SDR104, and DDR50 557 * Current limit switch is only defined for SDR50, SDR104, and DDR50
558 * bus speed modes. For other bus speed modes, we set the default 558 * bus speed modes. For other bus speed modes, we set the default
559 * current limit of 200mA. 559 * current limit of 200mA.
560 */ 560 */
561 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) || 561 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
562 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) || 562 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
563 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) { 563 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
564 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) { 564 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
565 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800) 565 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
566 current_limit = SD_SET_CURRENT_LIMIT_800; 566 current_limit = SD_SET_CURRENT_LIMIT_800;
567 else if (card->sw_caps.sd3_curr_limit & 567 else if (card->sw_caps.sd3_curr_limit &
568 SD_MAX_CURRENT_600) 568 SD_MAX_CURRENT_600)
569 current_limit = SD_SET_CURRENT_LIMIT_600; 569 current_limit = SD_SET_CURRENT_LIMIT_600;
570 else if (card->sw_caps.sd3_curr_limit & 570 else if (card->sw_caps.sd3_curr_limit &
571 SD_MAX_CURRENT_400) 571 SD_MAX_CURRENT_400)
572 current_limit = SD_SET_CURRENT_LIMIT_400; 572 current_limit = SD_SET_CURRENT_LIMIT_400;
573 else if (card->sw_caps.sd3_curr_limit & 573 else if (card->sw_caps.sd3_curr_limit &
574 SD_MAX_CURRENT_200) 574 SD_MAX_CURRENT_200)
575 current_limit = SD_SET_CURRENT_LIMIT_200; 575 current_limit = SD_SET_CURRENT_LIMIT_200;
576 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) { 576 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
577 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600) 577 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
578 current_limit = SD_SET_CURRENT_LIMIT_600; 578 current_limit = SD_SET_CURRENT_LIMIT_600;
579 else if (card->sw_caps.sd3_curr_limit & 579 else if (card->sw_caps.sd3_curr_limit &
580 SD_MAX_CURRENT_400) 580 SD_MAX_CURRENT_400)
581 current_limit = SD_SET_CURRENT_LIMIT_400; 581 current_limit = SD_SET_CURRENT_LIMIT_400;
582 else if (card->sw_caps.sd3_curr_limit & 582 else if (card->sw_caps.sd3_curr_limit &
583 SD_MAX_CURRENT_200) 583 SD_MAX_CURRENT_200)
584 current_limit = SD_SET_CURRENT_LIMIT_200; 584 current_limit = SD_SET_CURRENT_LIMIT_200;
585 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) { 585 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
586 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400) 586 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
587 current_limit = SD_SET_CURRENT_LIMIT_400; 587 current_limit = SD_SET_CURRENT_LIMIT_400;
588 else if (card->sw_caps.sd3_curr_limit & 588 else if (card->sw_caps.sd3_curr_limit &
589 SD_MAX_CURRENT_200) 589 SD_MAX_CURRENT_200)
590 current_limit = SD_SET_CURRENT_LIMIT_200; 590 current_limit = SD_SET_CURRENT_LIMIT_200;
591 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) { 591 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
592 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200) 592 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
593 current_limit = SD_SET_CURRENT_LIMIT_200; 593 current_limit = SD_SET_CURRENT_LIMIT_200;
594 } 594 }
595 } else 595 } else
596 current_limit = SD_SET_CURRENT_LIMIT_200; 596 current_limit = SD_SET_CURRENT_LIMIT_200;
597 597
598 err = mmc_sd_switch(card, 1, 3, current_limit, status); 598 err = mmc_sd_switch(card, 1, 3, current_limit, status);
599 if (err) 599 if (err)
600 return err; 600 return err;
601 601
602 if (((status[15] >> 4) & 0x0F) != current_limit) 602 if (((status[15] >> 4) & 0x0F) != current_limit)
603 printk(KERN_WARNING "%s: Problem setting current limit!\n", 603 printk(KERN_WARNING "%s: Problem setting current limit!\n",
604 mmc_hostname(card->host)); 604 mmc_hostname(card->host));
605 605
606 return 0; 606 return 0;
607 } 607 }
608 608
609 /* 609 /*
610 * UHS-I specific initialization procedure 610 * UHS-I specific initialization procedure
611 */ 611 */
612 static int mmc_sd_init_uhs_card(struct mmc_card *card) 612 static int mmc_sd_init_uhs_card(struct mmc_card *card)
613 { 613 {
614 int err; 614 int err;
615 u8 *status; 615 u8 *status;
616 616
617 if (!card->scr.sda_spec3) 617 if (!card->scr.sda_spec3)
618 return 0; 618 return 0;
619 619
620 if (!(card->csd.cmdclass & CCC_SWITCH)) 620 if (!(card->csd.cmdclass & CCC_SWITCH))
621 return 0; 621 return 0;
622 622
623 status = kmalloc(64, GFP_KERNEL); 623 status = kmalloc(64, GFP_KERNEL);
624 if (!status) { 624 if (!status) {
625 printk(KERN_ERR "%s: could not allocate a buffer for " 625 printk(KERN_ERR "%s: could not allocate a buffer for "
626 "switch capabilities.\n", mmc_hostname(card->host)); 626 "switch capabilities.\n", mmc_hostname(card->host));
627 return -ENOMEM; 627 return -ENOMEM;
628 } 628 }
629 629
630 /* Set 4-bit bus width */ 630 /* Set 4-bit bus width */
631 if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 631 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
632 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 632 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
633 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 633 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
634 if (err) 634 if (err)
635 goto out; 635 goto out;
636 636
637 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 637 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
638 } 638 }
639 639
640 /* 640 /*
641 * Select the bus speed mode depending on host 641 * Select the bus speed mode depending on host
642 * and card capability. 642 * and card capability.
643 */ 643 */
644 sd_update_bus_speed_mode(card); 644 sd_update_bus_speed_mode(card);
645 645
646 /* Set the driver strength for the card */ 646 /* Set the driver strength for the card */
647 err = sd_select_driver_type(card, status); 647 err = sd_select_driver_type(card, status);
648 if (err) 648 if (err)
649 goto out; 649 goto out;
650 650
651 /* Set current limit for the card */ 651 /* Set current limit for the card */
652 err = sd_set_current_limit(card, status); 652 err = sd_set_current_limit(card, status);
653 if (err) 653 if (err)
654 goto out; 654 goto out;
655 655
656 /* Set bus speed mode of the card */ 656 /* Set bus speed mode of the card */
657 err = sd_set_bus_speed_mode(card, status); 657 err = sd_set_bus_speed_mode(card, status);
658 if (err) 658 if (err)
659 goto out; 659 goto out;
660 660
661 /* SPI mode doesn't define CMD19 */ 661 /* SPI mode doesn't define CMD19 */
662 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) 662 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
663 err = card->host->ops->execute_tuning(card->host); 663 err = card->host->ops->execute_tuning(card->host);
664 664
665 out: 665 out:
666 kfree(status); 666 kfree(status);
667 667
668 return err; 668 return err;
669 } 669 }
670 670
671 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 671 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
672 card->raw_cid[2], card->raw_cid[3]); 672 card->raw_cid[2], card->raw_cid[3]);
673 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 673 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
674 card->raw_csd[2], card->raw_csd[3]); 674 card->raw_csd[2], card->raw_csd[3]);
675 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 675 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
676 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 676 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
677 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 677 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
678 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 678 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
679 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 679 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
680 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 680 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
681 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 681 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
682 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); 682 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
683 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); 683 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
684 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); 684 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
685 685
686 686
687 static struct attribute *sd_std_attrs[] = { 687 static struct attribute *sd_std_attrs[] = {
688 &dev_attr_cid.attr, 688 &dev_attr_cid.attr,
689 &dev_attr_csd.attr, 689 &dev_attr_csd.attr,
690 &dev_attr_scr.attr, 690 &dev_attr_scr.attr,
691 &dev_attr_date.attr, 691 &dev_attr_date.attr,
692 &dev_attr_erase_size.attr, 692 &dev_attr_erase_size.attr,
693 &dev_attr_preferred_erase_size.attr, 693 &dev_attr_preferred_erase_size.attr,
694 &dev_attr_fwrev.attr, 694 &dev_attr_fwrev.attr,
695 &dev_attr_hwrev.attr, 695 &dev_attr_hwrev.attr,
696 &dev_attr_manfid.attr, 696 &dev_attr_manfid.attr,
697 &dev_attr_name.attr, 697 &dev_attr_name.attr,
698 &dev_attr_oemid.attr, 698 &dev_attr_oemid.attr,
699 &dev_attr_serial.attr, 699 &dev_attr_serial.attr,
700 NULL, 700 NULL,
701 }; 701 };
702 702
703 static struct attribute_group sd_std_attr_group = { 703 static struct attribute_group sd_std_attr_group = {
704 .attrs = sd_std_attrs, 704 .attrs = sd_std_attrs,
705 }; 705 };
706 706
707 static const struct attribute_group *sd_attr_groups[] = { 707 static const struct attribute_group *sd_attr_groups[] = {
708 &sd_std_attr_group, 708 &sd_std_attr_group,
709 NULL, 709 NULL,
710 }; 710 };
711 711
712 struct device_type sd_type = { 712 struct device_type sd_type = {
713 .groups = sd_attr_groups, 713 .groups = sd_attr_groups,
714 }; 714 };
715 715
716 /* 716 /*
717 * Fetch CID from card. 717 * Fetch CID from card.
718 */ 718 */
719 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr) 719 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
720 { 720 {
721 int err; 721 int err;
722 722
723 /* 723 /*
724 * Since we're changing the OCR value, we seem to 724 * Since we're changing the OCR value, we seem to
725 * need to tell some cards to go back to the idle 725 * need to tell some cards to go back to the idle
726 * state. We wait 1ms to give cards time to 726 * state. We wait 1ms to give cards time to
727 * respond. 727 * respond.
728 */ 728 */
729 mmc_go_idle(host); 729 mmc_go_idle(host);
730 730
731 /* 731 /*
732 * If SD_SEND_IF_COND indicates an SD 2.0 732 * If SD_SEND_IF_COND indicates an SD 2.0
733 * compliant card and we should set bit 30 733 * compliant card and we should set bit 30
734 * of the ocr to indicate that we can handle 734 * of the ocr to indicate that we can handle
735 * block-addressed SDHC cards. 735 * block-addressed SDHC cards.
736 */ 736 */
737 err = mmc_send_if_cond(host, ocr); 737 err = mmc_send_if_cond(host, ocr);
738 if (!err) 738 if (!err)
739 ocr |= SD_OCR_CCS; 739 ocr |= SD_OCR_CCS;
740 740
741 /* 741 /*
742 * If the host supports one of UHS-I modes, request the card 742 * If the host supports one of UHS-I modes, request the card
743 * to switch to 1.8V signaling level. 743 * to switch to 1.8V signaling level.
744 */ 744 */
745 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 745 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
746 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)) 746 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
747 ocr |= SD_OCR_S18R; 747 ocr |= SD_OCR_S18R;
748 748
749 /* If the host can supply more than 150mA, XPC should be set to 1. */ 749 /* If the host can supply more than 150mA, XPC should be set to 1. */
750 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 | 750 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
751 MMC_CAP_SET_XPC_180)) 751 MMC_CAP_SET_XPC_180))
752 ocr |= SD_OCR_XPC; 752 ocr |= SD_OCR_XPC;
753 753
754 try_again: 754 try_again:
755 err = mmc_send_app_op_cond(host, ocr, rocr); 755 err = mmc_send_app_op_cond(host, ocr, rocr);
756 if (err) 756 if (err)
757 return err; 757 return err;
758 758
759 /* 759 /*
760 * In case CCS and S18A in the response is set, start Signal Voltage 760 * In case CCS and S18A in the response is set, start Signal Voltage
761 * Switch procedure. SPI mode doesn't support CMD11. 761 * Switch procedure. SPI mode doesn't support CMD11.
762 */ 762 */
763 if (!mmc_host_is_spi(host) && rocr && 763 if (!mmc_host_is_spi(host) && rocr &&
764 ((*rocr & 0x41000000) == 0x41000000)) { 764 ((*rocr & 0x41000000) == 0x41000000)) {
765 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true); 765 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
766 if (err) { 766 if (err) {
767 ocr &= ~SD_OCR_S18R; 767 ocr &= ~SD_OCR_S18R;
768 goto try_again; 768 goto try_again;
769 } 769 }
770 } 770 }
771 771
772 if (mmc_host_is_spi(host)) 772 if (mmc_host_is_spi(host))
773 err = mmc_send_cid(host, cid); 773 err = mmc_send_cid(host, cid);
774 else 774 else
775 err = mmc_all_send_cid(host, cid); 775 err = mmc_all_send_cid(host, cid);
776 776
777 return err; 777 return err;
778 } 778 }
779 779
780 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card) 780 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
781 { 781 {
782 int err; 782 int err;
783 783
784 /* 784 /*
785 * Fetch CSD from card. 785 * Fetch CSD from card.
786 */ 786 */
787 err = mmc_send_csd(card, card->raw_csd); 787 err = mmc_send_csd(card, card->raw_csd);
788 if (err) 788 if (err)
789 return err; 789 return err;
790 790
791 err = mmc_decode_csd(card); 791 err = mmc_decode_csd(card);
792 if (err) 792 if (err)
793 return err; 793 return err;
794 794
795 return 0; 795 return 0;
796 } 796 }
797 797
798 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, 798 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
799 bool reinit) 799 bool reinit)
800 { 800 {
801 int err; 801 int err;
802 802
803 if (!reinit) { 803 if (!reinit) {
804 /* 804 /*
805 * Fetch SCR from card. 805 * Fetch SCR from card.
806 */ 806 */
807 err = mmc_app_send_scr(card, card->raw_scr); 807 err = mmc_app_send_scr(card, card->raw_scr);
808 if (err) 808 if (err)
809 return err; 809 return err;
810 810
811 err = mmc_decode_scr(card); 811 err = mmc_decode_scr(card);
812 if (err) 812 if (err)
813 return err; 813 return err;
814 814
815 /* 815 /*
816 * Fetch and process SD Status register. 816 * Fetch and process SD Status register.
817 */ 817 */
818 err = mmc_read_ssr(card); 818 err = mmc_read_ssr(card);
819 if (err) 819 if (err)
820 return err; 820 return err;
821 821
822 /* Erase init depends on CSD and SSR */ 822 /* Erase init depends on CSD and SSR */
823 mmc_init_erase(card); 823 mmc_init_erase(card);
824 824
825 /* 825 /*
826 * Fetch switch information from card. 826 * Fetch switch information from card.
827 */ 827 */
828 err = mmc_read_switch(card); 828 err = mmc_read_switch(card);
829 if (err) 829 if (err)
830 return err; 830 return err;
831 } 831 }
832 832
833 /* 833 /*
834 * For SPI, enable CRC as appropriate. 834 * For SPI, enable CRC as appropriate.
835 * This CRC enable is located AFTER the reading of the 835 * This CRC enable is located AFTER the reading of the
836 * card registers because some SDHC cards are not able 836 * card registers because some SDHC cards are not able
837 * to provide valid CRCs for non-512-byte blocks. 837 * to provide valid CRCs for non-512-byte blocks.
838 */ 838 */
839 if (mmc_host_is_spi(host)) { 839 if (mmc_host_is_spi(host)) {
840 err = mmc_spi_set_crc(host, use_spi_crc); 840 err = mmc_spi_set_crc(host, use_spi_crc);
841 if (err) 841 if (err)
842 return err; 842 return err;
843 } 843 }
844 844
845 /* 845 /*
846 * Check if read-only switch is active. 846 * Check if read-only switch is active.
847 */ 847 */
848 if (!reinit) { 848 if (!reinit) {
849 int ro = -1; 849 int ro = -1;
850 850
851 if (host->ops->get_ro) 851 if (host->ops->get_ro)
852 ro = host->ops->get_ro(host); 852 ro = host->ops->get_ro(host);
853 853
854 if (ro < 0) { 854 if (ro < 0) {
855 printk(KERN_WARNING "%s: host does not " 855 printk(KERN_WARNING "%s: host does not "
856 "support reading read-only " 856 "support reading read-only "
857 "switch. assuming write-enable.\n", 857 "switch. assuming write-enable.\n",
858 mmc_hostname(host)); 858 mmc_hostname(host));
859 } else if (ro > 0) { 859 } else if (ro > 0) {
860 mmc_card_set_readonly(card); 860 mmc_card_set_readonly(card);
861 } 861 }
862 } 862 }
863 863
864 return 0; 864 return 0;
865 } 865 }
866 866
867 unsigned mmc_sd_get_max_clock(struct mmc_card *card) 867 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
868 { 868 {
869 unsigned max_dtr = (unsigned int)-1; 869 unsigned max_dtr = (unsigned int)-1;
870 870
871 if (mmc_card_highspeed(card)) { 871 if (mmc_card_highspeed(card)) {
872 if (max_dtr > card->sw_caps.hs_max_dtr) 872 if (max_dtr > card->sw_caps.hs_max_dtr)
873 max_dtr = card->sw_caps.hs_max_dtr; 873 max_dtr = card->sw_caps.hs_max_dtr;
874 } else if (max_dtr > card->csd.max_dtr) { 874 } else if (max_dtr > card->csd.max_dtr) {
875 max_dtr = card->csd.max_dtr; 875 max_dtr = card->csd.max_dtr;
876 } 876 }
877 877
878 return max_dtr; 878 return max_dtr;
879 } 879 }
880 880
881 void mmc_sd_go_highspeed(struct mmc_card *card) 881 void mmc_sd_go_highspeed(struct mmc_card *card)
882 { 882 {
883 mmc_card_set_highspeed(card); 883 mmc_card_set_highspeed(card);
884 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 884 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
885 } 885 }
886 886
887 /* 887 /*
888 * Handle the detection and initialisation of a card. 888 * Handle the detection and initialisation of a card.
889 * 889 *
890 * In the case of a resume, "oldcard" will contain the card 890 * In the case of a resume, "oldcard" will contain the card
891 * we're trying to reinitialise. 891 * we're trying to reinitialise.
892 */ 892 */
893 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 893 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
894 struct mmc_card *oldcard) 894 struct mmc_card *oldcard)
895 { 895 {
896 struct mmc_card *card; 896 struct mmc_card *card;
897 int err; 897 int err;
898 u32 cid[4]; 898 u32 cid[4];
899 u32 rocr = 0; 899 u32 rocr = 0;
900 900
901 BUG_ON(!host); 901 BUG_ON(!host);
902 WARN_ON(!host->claimed); 902 WARN_ON(!host->claimed);
903 903
904 err = mmc_sd_get_cid(host, ocr, cid, &rocr); 904 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
905 if (err) 905 if (err)
906 return err; 906 return err;
907 907
908 if (oldcard) { 908 if (oldcard) {
909 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 909 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
910 return -ENOENT; 910 return -ENOENT;
911 911
912 card = oldcard; 912 card = oldcard;
913 } else { 913 } else {
914 /* 914 /*
915 * Allocate card structure. 915 * Allocate card structure.
916 */ 916 */
917 card = mmc_alloc_card(host, &sd_type); 917 card = mmc_alloc_card(host, &sd_type);
918 if (IS_ERR(card)) 918 if (IS_ERR(card))
919 return PTR_ERR(card); 919 return PTR_ERR(card);
920 920
921 card->type = MMC_TYPE_SD; 921 card->type = MMC_TYPE_SD;
922 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 922 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
923 } 923 }
924 924
925 /* 925 /*
926 * For native busses: get card RCA and quit open drain mode. 926 * For native busses: get card RCA and quit open drain mode.
927 */ 927 */
928 if (!mmc_host_is_spi(host)) { 928 if (!mmc_host_is_spi(host)) {
929 err = mmc_send_relative_addr(host, &card->rca); 929 err = mmc_send_relative_addr(host, &card->rca);
930 if (err) 930 if (err)
931 return err; 931 return err;
932 932
933 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 933 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
934 } 934 }
935 935
936 if (!oldcard) { 936 if (!oldcard) {
937 err = mmc_sd_get_csd(host, card); 937 err = mmc_sd_get_csd(host, card);
938 if (err) 938 if (err)
939 return err; 939 return err;
940 940
941 mmc_decode_cid(card); 941 mmc_decode_cid(card);
942 } 942 }
943 943
944 /* 944 /*
945 * Select card, as all following commands rely on that. 945 * Select card, as all following commands rely on that.
946 */ 946 */
947 if (!mmc_host_is_spi(host)) { 947 if (!mmc_host_is_spi(host)) {
948 err = mmc_select_card(card); 948 err = mmc_select_card(card);
949 if (err) 949 if (err)
950 return err; 950 return err;
951 } 951 }
952 952
953 err = mmc_sd_setup_card(host, card, oldcard != NULL); 953 err = mmc_sd_setup_card(host, card, oldcard != NULL);
954 if (err) 954 if (err)
955 goto free_card; 955 goto free_card;
956 956
957 /* Initialization sequence for UHS-I cards */ 957 /* Initialization sequence for UHS-I cards */
958 if (rocr & SD_ROCR_S18A) { 958 if (rocr & SD_ROCR_S18A) {
959 err = mmc_sd_init_uhs_card(card); 959 err = mmc_sd_init_uhs_card(card);
960 if (err) 960 if (err)
961 goto free_card; 961 goto free_card;
962 962
963 /* Card is an ultra-high-speed card */ 963 /* Card is an ultra-high-speed card */
964 mmc_sd_card_set_uhs(card); 964 mmc_sd_card_set_uhs(card);
965 965
966 /* 966 /*
967 * Since initialization is now complete, enable preset 967 * Since initialization is now complete, enable preset
968 * value registers for UHS-I cards. 968 * value registers for UHS-I cards.
969 */ 969 */
970 if (host->ops->enable_preset_value) 970 if (host->ops->enable_preset_value)
971 host->ops->enable_preset_value(host, true); 971 host->ops->enable_preset_value(host, true);
972 } else { 972 } else {
973 /* 973 /*
974 * Attempt to change to high-speed (if supported) 974 * Attempt to change to high-speed (if supported)
975 */ 975 */
976 err = mmc_sd_switch_hs(card); 976 err = mmc_sd_switch_hs(card);
977 if (err > 0) 977 if (err > 0)
978 mmc_sd_go_highspeed(card); 978 mmc_sd_go_highspeed(card);
979 else if (err) 979 else if (err)
980 goto free_card; 980 goto free_card;
981 981
982 /* 982 /*
983 * Set bus speed. 983 * Set bus speed.
984 */ 984 */
985 mmc_set_clock(host, mmc_sd_get_max_clock(card)); 985 mmc_set_clock(host, mmc_sd_get_max_clock(card));
986 986
987 /* 987 /*
988 * Switch to wider bus (if supported). 988 * Switch to wider bus (if supported).
989 */ 989 */
990 if ((host->caps & MMC_CAP_4_BIT_DATA) && 990 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
991 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 991 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
992 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 992 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
993 if (err) 993 if (err)
994 goto free_card; 994 goto free_card;
995 995
996 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 996 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
997 } 997 }
998 } 998 }
999 999
1000 host->card = card; 1000 host->card = card;
1001 return 0; 1001 return 0;
1002 1002
1003 free_card: 1003 free_card:
1004 if (!oldcard) 1004 if (!oldcard)
1005 mmc_remove_card(card); 1005 mmc_remove_card(card);
1006 1006
1007 return err; 1007 return err;
1008 } 1008 }
1009 1009
1010 /* 1010 /*
1011 * Host is being removed. Free up the current card. 1011 * Host is being removed. Free up the current card.
1012 */ 1012 */
1013 static void mmc_sd_remove(struct mmc_host *host) 1013 static void mmc_sd_remove(struct mmc_host *host)
1014 { 1014 {
1015 BUG_ON(!host); 1015 BUG_ON(!host);
1016 BUG_ON(!host->card); 1016 BUG_ON(!host->card);
1017 1017
1018 mmc_remove_card(host->card); 1018 mmc_remove_card(host->card);
1019 host->card = NULL; 1019 host->card = NULL;
1020 } 1020 }
1021 1021
1022 /* 1022 /*
1023 * Card detection callback from host. 1023 * Card detection callback from host.
1024 */ 1024 */
1025 static void mmc_sd_detect(struct mmc_host *host) 1025 static void mmc_sd_detect(struct mmc_host *host)
1026 { 1026 {
1027 int err; 1027 int err;
1028 1028
1029 BUG_ON(!host); 1029 BUG_ON(!host);
1030 BUG_ON(!host->card); 1030 BUG_ON(!host->card);
1031 1031
1032 mmc_claim_host(host); 1032 mmc_claim_host(host);
1033 1033
1034 /* 1034 /*
1035 * Just check if our card has been removed. 1035 * Just check if our card has been removed.
1036 */ 1036 */
1037 err = mmc_send_status(host->card, NULL); 1037 err = mmc_send_status(host->card, NULL);
1038 1038
1039 mmc_release_host(host); 1039 mmc_release_host(host);
1040 1040
1041 if (err) { 1041 if (err) {
1042 mmc_sd_remove(host); 1042 mmc_sd_remove(host);
1043 1043
1044 mmc_claim_host(host); 1044 mmc_claim_host(host);
1045 mmc_detach_bus(host); 1045 mmc_detach_bus(host);
1046 mmc_power_off(host);
1046 mmc_release_host(host); 1047 mmc_release_host(host);
1047 } 1048 }
1048 } 1049 }
1049 1050
1050 /* 1051 /*
1051 * Suspend callback from host. 1052 * Suspend callback from host.
1052 */ 1053 */
1053 static int mmc_sd_suspend(struct mmc_host *host) 1054 static int mmc_sd_suspend(struct mmc_host *host)
1054 { 1055 {
1055 BUG_ON(!host); 1056 BUG_ON(!host);
1056 BUG_ON(!host->card); 1057 BUG_ON(!host->card);
1057 1058
1058 mmc_claim_host(host); 1059 mmc_claim_host(host);
1059 if (!mmc_host_is_spi(host)) 1060 if (!mmc_host_is_spi(host))
1060 mmc_deselect_cards(host); 1061 mmc_deselect_cards(host);
1061 host->card->state &= ~MMC_STATE_HIGHSPEED; 1062 host->card->state &= ~MMC_STATE_HIGHSPEED;
1062 mmc_release_host(host); 1063 mmc_release_host(host);
1063 1064
1064 return 0; 1065 return 0;
1065 } 1066 }
1066 1067
1067 /* 1068 /*
1068 * Resume callback from host. 1069 * Resume callback from host.
1069 * 1070 *
1070 * This function tries to determine if the same card is still present 1071 * This function tries to determine if the same card is still present
1071 * and, if so, restore all state to it. 1072 * and, if so, restore all state to it.
1072 */ 1073 */
1073 static int mmc_sd_resume(struct mmc_host *host) 1074 static int mmc_sd_resume(struct mmc_host *host)
1074 { 1075 {
1075 int err; 1076 int err;
1076 1077
1077 BUG_ON(!host); 1078 BUG_ON(!host);
1078 BUG_ON(!host->card); 1079 BUG_ON(!host->card);
1079 1080
1080 mmc_claim_host(host); 1081 mmc_claim_host(host);
1081 err = mmc_sd_init_card(host, host->ocr, host->card); 1082 err = mmc_sd_init_card(host, host->ocr, host->card);
1082 mmc_release_host(host); 1083 mmc_release_host(host);
1083 1084
1084 return err; 1085 return err;
1085 } 1086 }
1086 1087
1087 static int mmc_sd_power_restore(struct mmc_host *host) 1088 static int mmc_sd_power_restore(struct mmc_host *host)
1088 { 1089 {
1089 int ret; 1090 int ret;
1090 1091
1091 host->card->state &= ~MMC_STATE_HIGHSPEED; 1092 host->card->state &= ~MMC_STATE_HIGHSPEED;
1092 mmc_claim_host(host); 1093 mmc_claim_host(host);
1093 ret = mmc_sd_init_card(host, host->ocr, host->card); 1094 ret = mmc_sd_init_card(host, host->ocr, host->card);
1094 mmc_release_host(host); 1095 mmc_release_host(host);
1095 1096
1096 return ret; 1097 return ret;
1097 } 1098 }
1098 1099
1099 static const struct mmc_bus_ops mmc_sd_ops = { 1100 static const struct mmc_bus_ops mmc_sd_ops = {
1100 .remove = mmc_sd_remove, 1101 .remove = mmc_sd_remove,
1101 .detect = mmc_sd_detect, 1102 .detect = mmc_sd_detect,
1102 .suspend = NULL, 1103 .suspend = NULL,
1103 .resume = NULL, 1104 .resume = NULL,
1104 .power_restore = mmc_sd_power_restore, 1105 .power_restore = mmc_sd_power_restore,
1105 }; 1106 };
1106 1107
1107 static const struct mmc_bus_ops mmc_sd_ops_unsafe = { 1108 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1108 .remove = mmc_sd_remove, 1109 .remove = mmc_sd_remove,
1109 .detect = mmc_sd_detect, 1110 .detect = mmc_sd_detect,
1110 .suspend = mmc_sd_suspend, 1111 .suspend = mmc_sd_suspend,
1111 .resume = mmc_sd_resume, 1112 .resume = mmc_sd_resume,
1112 .power_restore = mmc_sd_power_restore, 1113 .power_restore = mmc_sd_power_restore,
1113 }; 1114 };
1114 1115
1115 static void mmc_sd_attach_bus_ops(struct mmc_host *host) 1116 static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1116 { 1117 {
1117 const struct mmc_bus_ops *bus_ops; 1118 const struct mmc_bus_ops *bus_ops;
1118 1119
1119 if (!mmc_card_is_removable(host)) 1120 if (!mmc_card_is_removable(host))
1120 bus_ops = &mmc_sd_ops_unsafe; 1121 bus_ops = &mmc_sd_ops_unsafe;
1121 else 1122 else
1122 bus_ops = &mmc_sd_ops; 1123 bus_ops = &mmc_sd_ops;
1123 mmc_attach_bus(host, bus_ops); 1124 mmc_attach_bus(host, bus_ops);
1124 } 1125 }
1125 1126
1126 /* 1127 /*
1127 * Starting point for SD card init. 1128 * Starting point for SD card init.
1128 */ 1129 */
1129 int mmc_attach_sd(struct mmc_host *host) 1130 int mmc_attach_sd(struct mmc_host *host)
1130 { 1131 {
1131 int err; 1132 int err;
1132 u32 ocr; 1133 u32 ocr;
1133 1134
1134 BUG_ON(!host); 1135 BUG_ON(!host);
1135 WARN_ON(!host->claimed); 1136 WARN_ON(!host->claimed);
1136 1137
1137 /* Make sure we are at 3.3V signalling voltage */ 1138 /* Make sure we are at 3.3V signalling voltage */
1138 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false); 1139 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false);
1139 if (err) 1140 if (err)
1140 return err; 1141 return err;
1141 1142
1142 /* Disable preset value enable if already set since last time */ 1143 /* Disable preset value enable if already set since last time */
1143 if (host->ops->enable_preset_value) 1144 if (host->ops->enable_preset_value)
1144 host->ops->enable_preset_value(host, false); 1145 host->ops->enable_preset_value(host, false);
1145 1146
1146 err = mmc_send_app_op_cond(host, 0, &ocr); 1147 err = mmc_send_app_op_cond(host, 0, &ocr);
1147 if (err) 1148 if (err)
1148 return err; 1149 return err;
1149 1150
1150 mmc_sd_attach_bus_ops(host); 1151 mmc_sd_attach_bus_ops(host);
1151 if (host->ocr_avail_sd) 1152 if (host->ocr_avail_sd)
1152 host->ocr_avail = host->ocr_avail_sd; 1153 host->ocr_avail = host->ocr_avail_sd;
1153 1154
1154 /* 1155 /*
1155 * We need to get OCR a different way for SPI. 1156 * We need to get OCR a different way for SPI.
1156 */ 1157 */
1157 if (mmc_host_is_spi(host)) { 1158 if (mmc_host_is_spi(host)) {
1158 mmc_go_idle(host); 1159 mmc_go_idle(host);
1159 1160
1160 err = mmc_spi_read_ocr(host, 0, &ocr); 1161 err = mmc_spi_read_ocr(host, 0, &ocr);
1161 if (err) 1162 if (err)
1162 goto err; 1163 goto err;
1163 } 1164 }
1164 1165
1165 /* 1166 /*
1166 * Sanity check the voltages that the card claims to 1167 * Sanity check the voltages that the card claims to
1167 * support. 1168 * support.
1168 */ 1169 */
1169 if (ocr & 0x7F) { 1170 if (ocr & 0x7F) {
1170 printk(KERN_WARNING "%s: card claims to support voltages " 1171 printk(KERN_WARNING "%s: card claims to support voltages "
1171 "below the defined range. These will be ignored.\n", 1172 "below the defined range. These will be ignored.\n",
1172 mmc_hostname(host)); 1173 mmc_hostname(host));
1173 ocr &= ~0x7F; 1174 ocr &= ~0x7F;
1174 } 1175 }
1175 1176
1176 if ((ocr & MMC_VDD_165_195) && 1177 if ((ocr & MMC_VDD_165_195) &&
1177 !(host->ocr_avail_sd & MMC_VDD_165_195)) { 1178 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
1178 printk(KERN_WARNING "%s: SD card claims to support the " 1179 printk(KERN_WARNING "%s: SD card claims to support the "
1179 "incompletely defined 'low voltage range'. This " 1180 "incompletely defined 'low voltage range'. This "
1180 "will be ignored.\n", mmc_hostname(host)); 1181 "will be ignored.\n", mmc_hostname(host));
1181 ocr &= ~MMC_VDD_165_195; 1182 ocr &= ~MMC_VDD_165_195;
1182 } 1183 }
1183 1184
1184 host->ocr = mmc_select_voltage(host, ocr); 1185 host->ocr = mmc_select_voltage(host, ocr);
1185 1186
1186 /* 1187 /*
1187 * Can we support the voltage(s) of the card(s)? 1188 * Can we support the voltage(s) of the card(s)?
1188 */ 1189 */
1189 if (!host->ocr) { 1190 if (!host->ocr) {
1190 err = -EINVAL; 1191 err = -EINVAL;
1191 goto err; 1192 goto err;
1192 } 1193 }
1193 1194
1194 /* 1195 /*
1195 * Detect and init the card. 1196 * Detect and init the card.
1196 */ 1197 */
1197 err = mmc_sd_init_card(host, host->ocr, NULL); 1198 err = mmc_sd_init_card(host, host->ocr, NULL);
1198 if (err) 1199 if (err)
1199 goto err; 1200 goto err;
1200 1201
1201 mmc_release_host(host); 1202 mmc_release_host(host);
1202 err = mmc_add_card(host->card); 1203 err = mmc_add_card(host->card);
1203 mmc_claim_host(host); 1204 mmc_claim_host(host);
1204 if (err) 1205 if (err)
1205 goto remove_card; 1206 goto remove_card;
1206 1207
1207 return 0; 1208 return 0;
1208 1209
1209 remove_card: 1210 remove_card:
1210 mmc_release_host(host); 1211 mmc_release_host(host);
1211 mmc_remove_card(host->card); 1212 mmc_remove_card(host->card);
1212 host->card = NULL; 1213 host->card = NULL;
1213 mmc_claim_host(host); 1214 mmc_claim_host(host);
1214 err: 1215 err:
1215 mmc_detach_bus(host); 1216 mmc_detach_bus(host);
1216 1217
1217 printk(KERN_ERR "%s: error %d whilst initialising SD card\n", 1218 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
1218 mmc_hostname(host), err); 1219 mmc_hostname(host), err);
1219 1220
1220 return err; 1221 return err;
1221 } 1222 }
1222 1223
1223 1224
drivers/mmc/core/sdio.c
1 /* 1 /*
2 * linux/drivers/mmc/sdio.c 2 * linux/drivers/mmc/sdio.c
3 * 3 *
4 * Copyright 2006-2007 Pierre Ossman 4 * Copyright 2006-2007 Pierre Ossman
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 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 8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version. 9 * your option) any later version.
10 */ 10 */
11 11
12 #include <linux/err.h> 12 #include <linux/err.h>
13 #include <linux/pm_runtime.h> 13 #include <linux/pm_runtime.h>
14 14
15 #include <linux/mmc/host.h> 15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h> 16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h> 17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h> 18 #include <linux/mmc/sdio_func.h>
19 #include <linux/mmc/sdio_ids.h> 19 #include <linux/mmc/sdio_ids.h>
20 20
21 #include "core.h" 21 #include "core.h"
22 #include "bus.h" 22 #include "bus.h"
23 #include "sd.h" 23 #include "sd.h"
24 #include "sdio_bus.h" 24 #include "sdio_bus.h"
25 #include "mmc_ops.h" 25 #include "mmc_ops.h"
26 #include "sd_ops.h" 26 #include "sd_ops.h"
27 #include "sdio_ops.h" 27 #include "sdio_ops.h"
28 #include "sdio_cis.h" 28 #include "sdio_cis.h"
29 29
30 static int sdio_read_fbr(struct sdio_func *func) 30 static int sdio_read_fbr(struct sdio_func *func)
31 { 31 {
32 int ret; 32 int ret;
33 unsigned char data; 33 unsigned char data;
34 34
35 if (mmc_card_nonstd_func_interface(func->card)) { 35 if (mmc_card_nonstd_func_interface(func->card)) {
36 func->class = SDIO_CLASS_NONE; 36 func->class = SDIO_CLASS_NONE;
37 return 0; 37 return 0;
38 } 38 }
39 39
40 ret = mmc_io_rw_direct(func->card, 0, 0, 40 ret = mmc_io_rw_direct(func->card, 0, 0,
41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); 41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
42 if (ret) 42 if (ret)
43 goto out; 43 goto out;
44 44
45 data &= 0x0f; 45 data &= 0x0f;
46 46
47 if (data == 0x0f) { 47 if (data == 0x0f) {
48 ret = mmc_io_rw_direct(func->card, 0, 0, 48 ret = mmc_io_rw_direct(func->card, 0, 0,
49 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); 49 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
50 if (ret) 50 if (ret)
51 goto out; 51 goto out;
52 } 52 }
53 53
54 func->class = data; 54 func->class = data;
55 55
56 out: 56 out:
57 return ret; 57 return ret;
58 } 58 }
59 59
60 static int sdio_init_func(struct mmc_card *card, unsigned int fn) 60 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
61 { 61 {
62 int ret; 62 int ret;
63 struct sdio_func *func; 63 struct sdio_func *func;
64 64
65 BUG_ON(fn > SDIO_MAX_FUNCS); 65 BUG_ON(fn > SDIO_MAX_FUNCS);
66 66
67 func = sdio_alloc_func(card); 67 func = sdio_alloc_func(card);
68 if (IS_ERR(func)) 68 if (IS_ERR(func))
69 return PTR_ERR(func); 69 return PTR_ERR(func);
70 70
71 func->num = fn; 71 func->num = fn;
72 72
73 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) { 73 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
74 ret = sdio_read_fbr(func); 74 ret = sdio_read_fbr(func);
75 if (ret) 75 if (ret)
76 goto fail; 76 goto fail;
77 77
78 ret = sdio_read_func_cis(func); 78 ret = sdio_read_func_cis(func);
79 if (ret) 79 if (ret)
80 goto fail; 80 goto fail;
81 } else { 81 } else {
82 func->vendor = func->card->cis.vendor; 82 func->vendor = func->card->cis.vendor;
83 func->device = func->card->cis.device; 83 func->device = func->card->cis.device;
84 func->max_blksize = func->card->cis.blksize; 84 func->max_blksize = func->card->cis.blksize;
85 } 85 }
86 86
87 card->sdio_func[fn - 1] = func; 87 card->sdio_func[fn - 1] = func;
88 88
89 return 0; 89 return 0;
90 90
91 fail: 91 fail:
92 /* 92 /*
93 * It is okay to remove the function here even though we hold 93 * It is okay to remove the function here even though we hold
94 * the host lock as we haven't registered the device yet. 94 * the host lock as we haven't registered the device yet.
95 */ 95 */
96 sdio_remove_func(func); 96 sdio_remove_func(func);
97 return ret; 97 return ret;
98 } 98 }
99 99
100 static int sdio_read_cccr(struct mmc_card *card) 100 static int sdio_read_cccr(struct mmc_card *card)
101 { 101 {
102 int ret; 102 int ret;
103 int cccr_vsn; 103 int cccr_vsn;
104 unsigned char data; 104 unsigned char data;
105 105
106 memset(&card->cccr, 0, sizeof(struct sdio_cccr)); 106 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
107 107
108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data); 108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
109 if (ret) 109 if (ret)
110 goto out; 110 goto out;
111 111
112 cccr_vsn = data & 0x0f; 112 cccr_vsn = data & 0x0f;
113 113
114 if (cccr_vsn > SDIO_CCCR_REV_1_20) { 114 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
115 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n", 115 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
116 mmc_hostname(card->host), cccr_vsn); 116 mmc_hostname(card->host), cccr_vsn);
117 return -EINVAL; 117 return -EINVAL;
118 } 118 }
119 119
120 card->cccr.sdio_vsn = (data & 0xf0) >> 4; 120 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
121 121
122 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data); 122 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
123 if (ret) 123 if (ret)
124 goto out; 124 goto out;
125 125
126 if (data & SDIO_CCCR_CAP_SMB) 126 if (data & SDIO_CCCR_CAP_SMB)
127 card->cccr.multi_block = 1; 127 card->cccr.multi_block = 1;
128 if (data & SDIO_CCCR_CAP_LSC) 128 if (data & SDIO_CCCR_CAP_LSC)
129 card->cccr.low_speed = 1; 129 card->cccr.low_speed = 1;
130 if (data & SDIO_CCCR_CAP_4BLS) 130 if (data & SDIO_CCCR_CAP_4BLS)
131 card->cccr.wide_bus = 1; 131 card->cccr.wide_bus = 1;
132 132
133 if (cccr_vsn >= SDIO_CCCR_REV_1_10) { 133 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
134 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data); 134 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
135 if (ret) 135 if (ret)
136 goto out; 136 goto out;
137 137
138 if (data & SDIO_POWER_SMPC) 138 if (data & SDIO_POWER_SMPC)
139 card->cccr.high_power = 1; 139 card->cccr.high_power = 1;
140 } 140 }
141 141
142 if (cccr_vsn >= SDIO_CCCR_REV_1_20) { 142 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
143 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data); 143 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
144 if (ret) 144 if (ret)
145 goto out; 145 goto out;
146 146
147 if (data & SDIO_SPEED_SHS) 147 if (data & SDIO_SPEED_SHS)
148 card->cccr.high_speed = 1; 148 card->cccr.high_speed = 1;
149 } 149 }
150 150
151 out: 151 out:
152 return ret; 152 return ret;
153 } 153 }
154 154
155 static int sdio_enable_wide(struct mmc_card *card) 155 static int sdio_enable_wide(struct mmc_card *card)
156 { 156 {
157 int ret; 157 int ret;
158 u8 ctrl; 158 u8 ctrl;
159 159
160 if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 160 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
161 return 0; 161 return 0;
162 162
163 if (card->cccr.low_speed && !card->cccr.wide_bus) 163 if (card->cccr.low_speed && !card->cccr.wide_bus)
164 return 0; 164 return 0;
165 165
166 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 166 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
167 if (ret) 167 if (ret)
168 return ret; 168 return ret;
169 169
170 ctrl |= SDIO_BUS_WIDTH_4BIT; 170 ctrl |= SDIO_BUS_WIDTH_4BIT;
171 171
172 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 172 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
173 if (ret) 173 if (ret)
174 return ret; 174 return ret;
175 175
176 return 1; 176 return 1;
177 } 177 }
178 178
179 /* 179 /*
180 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) 180 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
181 * of the card. This may be required on certain setups of boards, 181 * of the card. This may be required on certain setups of boards,
182 * controllers and embedded sdio device which do not need the card's 182 * controllers and embedded sdio device which do not need the card's
183 * pull-up. As a result, card detection is disabled and power is saved. 183 * pull-up. As a result, card detection is disabled and power is saved.
184 */ 184 */
185 static int sdio_disable_cd(struct mmc_card *card) 185 static int sdio_disable_cd(struct mmc_card *card)
186 { 186 {
187 int ret; 187 int ret;
188 u8 ctrl; 188 u8 ctrl;
189 189
190 if (!mmc_card_disable_cd(card)) 190 if (!mmc_card_disable_cd(card))
191 return 0; 191 return 0;
192 192
193 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 193 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
194 if (ret) 194 if (ret)
195 return ret; 195 return ret;
196 196
197 ctrl |= SDIO_BUS_CD_DISABLE; 197 ctrl |= SDIO_BUS_CD_DISABLE;
198 198
199 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 199 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
200 } 200 }
201 201
202 /* 202 /*
203 * Devices that remain active during a system suspend are 203 * Devices that remain active during a system suspend are
204 * put back into 1-bit mode. 204 * put back into 1-bit mode.
205 */ 205 */
206 static int sdio_disable_wide(struct mmc_card *card) 206 static int sdio_disable_wide(struct mmc_card *card)
207 { 207 {
208 int ret; 208 int ret;
209 u8 ctrl; 209 u8 ctrl;
210 210
211 if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 211 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
212 return 0; 212 return 0;
213 213
214 if (card->cccr.low_speed && !card->cccr.wide_bus) 214 if (card->cccr.low_speed && !card->cccr.wide_bus)
215 return 0; 215 return 0;
216 216
217 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 217 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
218 if (ret) 218 if (ret)
219 return ret; 219 return ret;
220 220
221 if (!(ctrl & SDIO_BUS_WIDTH_4BIT)) 221 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
222 return 0; 222 return 0;
223 223
224 ctrl &= ~SDIO_BUS_WIDTH_4BIT; 224 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
225 ctrl |= SDIO_BUS_ASYNC_INT; 225 ctrl |= SDIO_BUS_ASYNC_INT;
226 226
227 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 227 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
228 if (ret) 228 if (ret)
229 return ret; 229 return ret;
230 230
231 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1); 231 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
232 232
233 return 0; 233 return 0;
234 } 234 }
235 235
236 236
237 static int sdio_enable_4bit_bus(struct mmc_card *card) 237 static int sdio_enable_4bit_bus(struct mmc_card *card)
238 { 238 {
239 int err; 239 int err;
240 240
241 if (card->type == MMC_TYPE_SDIO) 241 if (card->type == MMC_TYPE_SDIO)
242 return sdio_enable_wide(card); 242 return sdio_enable_wide(card);
243 243
244 if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 244 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
245 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 245 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
246 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 246 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
247 if (err) 247 if (err)
248 return err; 248 return err;
249 } else 249 } else
250 return 0; 250 return 0;
251 251
252 err = sdio_enable_wide(card); 252 err = sdio_enable_wide(card);
253 if (err <= 0) 253 if (err <= 0)
254 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1); 254 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
255 255
256 return err; 256 return err;
257 } 257 }
258 258
259 259
260 /* 260 /*
261 * Test if the card supports high-speed mode and, if so, switch to it. 261 * Test if the card supports high-speed mode and, if so, switch to it.
262 */ 262 */
263 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable) 263 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
264 { 264 {
265 int ret; 265 int ret;
266 u8 speed; 266 u8 speed;
267 267
268 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 268 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
269 return 0; 269 return 0;
270 270
271 if (!card->cccr.high_speed) 271 if (!card->cccr.high_speed)
272 return 0; 272 return 0;
273 273
274 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 274 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
275 if (ret) 275 if (ret)
276 return ret; 276 return ret;
277 277
278 if (enable) 278 if (enable)
279 speed |= SDIO_SPEED_EHS; 279 speed |= SDIO_SPEED_EHS;
280 else 280 else
281 speed &= ~SDIO_SPEED_EHS; 281 speed &= ~SDIO_SPEED_EHS;
282 282
283 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 283 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
284 if (ret) 284 if (ret)
285 return ret; 285 return ret;
286 286
287 return 1; 287 return 1;
288 } 288 }
289 289
290 /* 290 /*
291 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported. 291 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
292 */ 292 */
293 static int sdio_enable_hs(struct mmc_card *card) 293 static int sdio_enable_hs(struct mmc_card *card)
294 { 294 {
295 int ret; 295 int ret;
296 296
297 ret = mmc_sdio_switch_hs(card, true); 297 ret = mmc_sdio_switch_hs(card, true);
298 if (ret <= 0 || card->type == MMC_TYPE_SDIO) 298 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
299 return ret; 299 return ret;
300 300
301 ret = mmc_sd_switch_hs(card); 301 ret = mmc_sd_switch_hs(card);
302 if (ret <= 0) 302 if (ret <= 0)
303 mmc_sdio_switch_hs(card, false); 303 mmc_sdio_switch_hs(card, false);
304 304
305 return ret; 305 return ret;
306 } 306 }
307 307
308 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card) 308 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
309 { 309 {
310 unsigned max_dtr; 310 unsigned max_dtr;
311 311
312 if (mmc_card_highspeed(card)) { 312 if (mmc_card_highspeed(card)) {
313 /* 313 /*
314 * The SDIO specification doesn't mention how 314 * The SDIO specification doesn't mention how
315 * the CIS transfer speed register relates to 315 * the CIS transfer speed register relates to
316 * high-speed, but it seems that 50 MHz is 316 * high-speed, but it seems that 50 MHz is
317 * mandatory. 317 * mandatory.
318 */ 318 */
319 max_dtr = 50000000; 319 max_dtr = 50000000;
320 } else { 320 } else {
321 max_dtr = card->cis.max_dtr; 321 max_dtr = card->cis.max_dtr;
322 } 322 }
323 323
324 if (card->type == MMC_TYPE_SD_COMBO) 324 if (card->type == MMC_TYPE_SD_COMBO)
325 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card)); 325 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
326 326
327 return max_dtr; 327 return max_dtr;
328 } 328 }
329 329
330 /* 330 /*
331 * Handle the detection and initialisation of a card. 331 * Handle the detection and initialisation of a card.
332 * 332 *
333 * In the case of a resume, "oldcard" will contain the card 333 * In the case of a resume, "oldcard" will contain the card
334 * we're trying to reinitialise. 334 * we're trying to reinitialise.
335 */ 335 */
336 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, 336 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
337 struct mmc_card *oldcard, int powered_resume) 337 struct mmc_card *oldcard, int powered_resume)
338 { 338 {
339 struct mmc_card *card; 339 struct mmc_card *card;
340 int err; 340 int err;
341 341
342 BUG_ON(!host); 342 BUG_ON(!host);
343 WARN_ON(!host->claimed); 343 WARN_ON(!host->claimed);
344 344
345 /* 345 /*
346 * Inform the card of the voltage 346 * Inform the card of the voltage
347 */ 347 */
348 if (!powered_resume) { 348 if (!powered_resume) {
349 err = mmc_send_io_op_cond(host, host->ocr, &ocr); 349 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
350 if (err) 350 if (err)
351 goto err; 351 goto err;
352 } 352 }
353 353
354 /* 354 /*
355 * For SPI, enable CRC as appropriate. 355 * For SPI, enable CRC as appropriate.
356 */ 356 */
357 if (mmc_host_is_spi(host)) { 357 if (mmc_host_is_spi(host)) {
358 err = mmc_spi_set_crc(host, use_spi_crc); 358 err = mmc_spi_set_crc(host, use_spi_crc);
359 if (err) 359 if (err)
360 goto err; 360 goto err;
361 } 361 }
362 362
363 /* 363 /*
364 * Allocate card structure. 364 * Allocate card structure.
365 */ 365 */
366 card = mmc_alloc_card(host, NULL); 366 card = mmc_alloc_card(host, NULL);
367 if (IS_ERR(card)) { 367 if (IS_ERR(card)) {
368 err = PTR_ERR(card); 368 err = PTR_ERR(card);
369 goto err; 369 goto err;
370 } 370 }
371 371
372 if ((ocr & R4_MEMORY_PRESENT) && 372 if ((ocr & R4_MEMORY_PRESENT) &&
373 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) { 373 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
374 card->type = MMC_TYPE_SD_COMBO; 374 card->type = MMC_TYPE_SD_COMBO;
375 375
376 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 376 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
377 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { 377 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
378 mmc_remove_card(card); 378 mmc_remove_card(card);
379 return -ENOENT; 379 return -ENOENT;
380 } 380 }
381 } else { 381 } else {
382 card->type = MMC_TYPE_SDIO; 382 card->type = MMC_TYPE_SDIO;
383 383
384 if (oldcard && oldcard->type != MMC_TYPE_SDIO) { 384 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
385 mmc_remove_card(card); 385 mmc_remove_card(card);
386 return -ENOENT; 386 return -ENOENT;
387 } 387 }
388 } 388 }
389 389
390 /* 390 /*
391 * Call the optional HC's init_card function to handle quirks. 391 * Call the optional HC's init_card function to handle quirks.
392 */ 392 */
393 if (host->ops->init_card) 393 if (host->ops->init_card)
394 host->ops->init_card(host, card); 394 host->ops->init_card(host, card);
395 395
396 /* 396 /*
397 * For native busses: set card RCA and quit open drain mode. 397 * For native busses: set card RCA and quit open drain mode.
398 */ 398 */
399 if (!powered_resume && !mmc_host_is_spi(host)) { 399 if (!powered_resume && !mmc_host_is_spi(host)) {
400 err = mmc_send_relative_addr(host, &card->rca); 400 err = mmc_send_relative_addr(host, &card->rca);
401 if (err) 401 if (err)
402 goto remove; 402 goto remove;
403 403
404 /* 404 /*
405 * Update oldcard with the new RCA received from the SDIO 405 * Update oldcard with the new RCA received from the SDIO
406 * device -- we're doing this so that it's updated in the 406 * device -- we're doing this so that it's updated in the
407 * "card" struct when oldcard overwrites that later. 407 * "card" struct when oldcard overwrites that later.
408 */ 408 */
409 if (oldcard) 409 if (oldcard)
410 oldcard->rca = card->rca; 410 oldcard->rca = card->rca;
411 411
412 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 412 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
413 } 413 }
414 414
415 /* 415 /*
416 * Read CSD, before selecting the card 416 * Read CSD, before selecting the card
417 */ 417 */
418 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { 418 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
419 err = mmc_sd_get_csd(host, card); 419 err = mmc_sd_get_csd(host, card);
420 if (err) 420 if (err)
421 return err; 421 return err;
422 422
423 mmc_decode_cid(card); 423 mmc_decode_cid(card);
424 } 424 }
425 425
426 /* 426 /*
427 * Select card, as all following commands rely on that. 427 * Select card, as all following commands rely on that.
428 */ 428 */
429 if (!powered_resume && !mmc_host_is_spi(host)) { 429 if (!powered_resume && !mmc_host_is_spi(host)) {
430 err = mmc_select_card(card); 430 err = mmc_select_card(card);
431 if (err) 431 if (err)
432 goto remove; 432 goto remove;
433 } 433 }
434 434
435 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) { 435 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
436 /* 436 /*
437 * This is non-standard SDIO device, meaning it doesn't 437 * This is non-standard SDIO device, meaning it doesn't
438 * have any CIA (Common I/O area) registers present. 438 * have any CIA (Common I/O area) registers present.
439 * It's host's responsibility to fill cccr and cis 439 * It's host's responsibility to fill cccr and cis
440 * structures in init_card(). 440 * structures in init_card().
441 */ 441 */
442 mmc_set_clock(host, card->cis.max_dtr); 442 mmc_set_clock(host, card->cis.max_dtr);
443 443
444 if (card->cccr.high_speed) { 444 if (card->cccr.high_speed) {
445 mmc_card_set_highspeed(card); 445 mmc_card_set_highspeed(card);
446 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 446 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
447 } 447 }
448 448
449 goto finish; 449 goto finish;
450 } 450 }
451 451
452 /* 452 /*
453 * Read the common registers. 453 * Read the common registers.
454 */ 454 */
455 err = sdio_read_cccr(card); 455 err = sdio_read_cccr(card);
456 if (err) 456 if (err)
457 goto remove; 457 goto remove;
458 458
459 /* 459 /*
460 * Read the common CIS tuples. 460 * Read the common CIS tuples.
461 */ 461 */
462 err = sdio_read_common_cis(card); 462 err = sdio_read_common_cis(card);
463 if (err) 463 if (err)
464 goto remove; 464 goto remove;
465 465
466 if (oldcard) { 466 if (oldcard) {
467 int same = (card->cis.vendor == oldcard->cis.vendor && 467 int same = (card->cis.vendor == oldcard->cis.vendor &&
468 card->cis.device == oldcard->cis.device); 468 card->cis.device == oldcard->cis.device);
469 mmc_remove_card(card); 469 mmc_remove_card(card);
470 if (!same) 470 if (!same)
471 return -ENOENT; 471 return -ENOENT;
472 472
473 card = oldcard; 473 card = oldcard;
474 } 474 }
475 mmc_fixup_device(card, NULL); 475 mmc_fixup_device(card, NULL);
476 476
477 if (card->type == MMC_TYPE_SD_COMBO) { 477 if (card->type == MMC_TYPE_SD_COMBO) {
478 err = mmc_sd_setup_card(host, card, oldcard != NULL); 478 err = mmc_sd_setup_card(host, card, oldcard != NULL);
479 /* handle as SDIO-only card if memory init failed */ 479 /* handle as SDIO-only card if memory init failed */
480 if (err) { 480 if (err) {
481 mmc_go_idle(host); 481 mmc_go_idle(host);
482 if (mmc_host_is_spi(host)) 482 if (mmc_host_is_spi(host))
483 /* should not fail, as it worked previously */ 483 /* should not fail, as it worked previously */
484 mmc_spi_set_crc(host, use_spi_crc); 484 mmc_spi_set_crc(host, use_spi_crc);
485 card->type = MMC_TYPE_SDIO; 485 card->type = MMC_TYPE_SDIO;
486 } else 486 } else
487 card->dev.type = &sd_type; 487 card->dev.type = &sd_type;
488 } 488 }
489 489
490 /* 490 /*
491 * If needed, disconnect card detection pull-up resistor. 491 * If needed, disconnect card detection pull-up resistor.
492 */ 492 */
493 err = sdio_disable_cd(card); 493 err = sdio_disable_cd(card);
494 if (err) 494 if (err)
495 goto remove; 495 goto remove;
496 496
497 /* 497 /*
498 * Switch to high-speed (if supported). 498 * Switch to high-speed (if supported).
499 */ 499 */
500 err = sdio_enable_hs(card); 500 err = sdio_enable_hs(card);
501 if (err > 0) 501 if (err > 0)
502 mmc_sd_go_highspeed(card); 502 mmc_sd_go_highspeed(card);
503 else if (err) 503 else if (err)
504 goto remove; 504 goto remove;
505 505
506 /* 506 /*
507 * Change to the card's maximum speed. 507 * Change to the card's maximum speed.
508 */ 508 */
509 mmc_set_clock(host, mmc_sdio_get_max_clock(card)); 509 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
510 510
511 /* 511 /*
512 * Switch to wider bus (if supported). 512 * Switch to wider bus (if supported).
513 */ 513 */
514 err = sdio_enable_4bit_bus(card); 514 err = sdio_enable_4bit_bus(card);
515 if (err > 0) 515 if (err > 0)
516 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 516 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
517 else if (err) 517 else if (err)
518 goto remove; 518 goto remove;
519 519
520 finish: 520 finish:
521 if (!oldcard) 521 if (!oldcard)
522 host->card = card; 522 host->card = card;
523 return 0; 523 return 0;
524 524
525 remove: 525 remove:
526 if (!oldcard) 526 if (!oldcard)
527 mmc_remove_card(card); 527 mmc_remove_card(card);
528 528
529 err: 529 err:
530 return err; 530 return err;
531 } 531 }
532 532
533 /* 533 /*
534 * Host is being removed. Free up the current card. 534 * Host is being removed. Free up the current card.
535 */ 535 */
536 static void mmc_sdio_remove(struct mmc_host *host) 536 static void mmc_sdio_remove(struct mmc_host *host)
537 { 537 {
538 int i; 538 int i;
539 539
540 BUG_ON(!host); 540 BUG_ON(!host);
541 BUG_ON(!host->card); 541 BUG_ON(!host->card);
542 542
543 for (i = 0;i < host->card->sdio_funcs;i++) { 543 for (i = 0;i < host->card->sdio_funcs;i++) {
544 if (host->card->sdio_func[i]) { 544 if (host->card->sdio_func[i]) {
545 sdio_remove_func(host->card->sdio_func[i]); 545 sdio_remove_func(host->card->sdio_func[i]);
546 host->card->sdio_func[i] = NULL; 546 host->card->sdio_func[i] = NULL;
547 } 547 }
548 } 548 }
549 549
550 mmc_remove_card(host->card); 550 mmc_remove_card(host->card);
551 host->card = NULL; 551 host->card = NULL;
552 } 552 }
553 553
554 /* 554 /*
555 * Card detection callback from host. 555 * Card detection callback from host.
556 */ 556 */
557 static void mmc_sdio_detect(struct mmc_host *host) 557 static void mmc_sdio_detect(struct mmc_host *host)
558 { 558 {
559 int err; 559 int err;
560 560
561 BUG_ON(!host); 561 BUG_ON(!host);
562 BUG_ON(!host->card); 562 BUG_ON(!host->card);
563 563
564 /* Make sure card is powered before detecting it */ 564 /* Make sure card is powered before detecting it */
565 if (host->caps & MMC_CAP_POWER_OFF_CARD) { 565 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
566 err = pm_runtime_get_sync(&host->card->dev); 566 err = pm_runtime_get_sync(&host->card->dev);
567 if (err < 0) 567 if (err < 0)
568 goto out; 568 goto out;
569 } 569 }
570 570
571 mmc_claim_host(host); 571 mmc_claim_host(host);
572 572
573 /* 573 /*
574 * Just check if our card has been removed. 574 * Just check if our card has been removed.
575 */ 575 */
576 err = mmc_select_card(host->card); 576 err = mmc_select_card(host->card);
577 577
578 mmc_release_host(host); 578 mmc_release_host(host);
579 579
580 /* 580 /*
581 * Tell PM core it's OK to power off the card now. 581 * Tell PM core it's OK to power off the card now.
582 * 582 *
583 * The _sync variant is used in order to ensure that the card 583 * The _sync variant is used in order to ensure that the card
584 * is left powered off in case an error occurred, and the card 584 * is left powered off in case an error occurred, and the card
585 * is going to be removed. 585 * is going to be removed.
586 * 586 *
587 * Since there is no specific reason to believe a new user 587 * Since there is no specific reason to believe a new user
588 * is about to show up at this point, the _sync variant is 588 * is about to show up at this point, the _sync variant is
589 * desirable anyway. 589 * desirable anyway.
590 */ 590 */
591 if (host->caps & MMC_CAP_POWER_OFF_CARD) 591 if (host->caps & MMC_CAP_POWER_OFF_CARD)
592 pm_runtime_put_sync(&host->card->dev); 592 pm_runtime_put_sync(&host->card->dev);
593 593
594 out: 594 out:
595 if (err) { 595 if (err) {
596 mmc_sdio_remove(host); 596 mmc_sdio_remove(host);
597 597
598 mmc_claim_host(host); 598 mmc_claim_host(host);
599 mmc_detach_bus(host); 599 mmc_detach_bus(host);
600 mmc_power_off(host);
600 mmc_release_host(host); 601 mmc_release_host(host);
601 } 602 }
602 } 603 }
603 604
604 /* 605 /*
605 * SDIO suspend. We need to suspend all functions separately. 606 * SDIO suspend. We need to suspend all functions separately.
606 * Therefore all registered functions must have drivers with suspend 607 * Therefore all registered functions must have drivers with suspend
607 * and resume methods. Failing that we simply remove the whole card. 608 * and resume methods. Failing that we simply remove the whole card.
608 */ 609 */
609 static int mmc_sdio_suspend(struct mmc_host *host) 610 static int mmc_sdio_suspend(struct mmc_host *host)
610 { 611 {
611 int i, err = 0; 612 int i, err = 0;
612 613
613 for (i = 0; i < host->card->sdio_funcs; i++) { 614 for (i = 0; i < host->card->sdio_funcs; i++) {
614 struct sdio_func *func = host->card->sdio_func[i]; 615 struct sdio_func *func = host->card->sdio_func[i];
615 if (func && sdio_func_present(func) && func->dev.driver) { 616 if (func && sdio_func_present(func) && func->dev.driver) {
616 const struct dev_pm_ops *pmops = func->dev.driver->pm; 617 const struct dev_pm_ops *pmops = func->dev.driver->pm;
617 if (!pmops || !pmops->suspend || !pmops->resume) { 618 if (!pmops || !pmops->suspend || !pmops->resume) {
618 /* force removal of entire card in that case */ 619 /* force removal of entire card in that case */
619 err = -ENOSYS; 620 err = -ENOSYS;
620 } else 621 } else
621 err = pmops->suspend(&func->dev); 622 err = pmops->suspend(&func->dev);
622 if (err) 623 if (err)
623 break; 624 break;
624 } 625 }
625 } 626 }
626 while (err && --i >= 0) { 627 while (err && --i >= 0) {
627 struct sdio_func *func = host->card->sdio_func[i]; 628 struct sdio_func *func = host->card->sdio_func[i];
628 if (func && sdio_func_present(func) && func->dev.driver) { 629 if (func && sdio_func_present(func) && func->dev.driver) {
629 const struct dev_pm_ops *pmops = func->dev.driver->pm; 630 const struct dev_pm_ops *pmops = func->dev.driver->pm;
630 pmops->resume(&func->dev); 631 pmops->resume(&func->dev);
631 } 632 }
632 } 633 }
633 634
634 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 635 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
635 mmc_claim_host(host); 636 mmc_claim_host(host);
636 sdio_disable_wide(host->card); 637 sdio_disable_wide(host->card);
637 mmc_release_host(host); 638 mmc_release_host(host);
638 } 639 }
639 640
640 return err; 641 return err;
641 } 642 }
642 643
643 static int mmc_sdio_resume(struct mmc_host *host) 644 static int mmc_sdio_resume(struct mmc_host *host)
644 { 645 {
645 int i, err = 0; 646 int i, err = 0;
646 647
647 BUG_ON(!host); 648 BUG_ON(!host);
648 BUG_ON(!host->card); 649 BUG_ON(!host->card);
649 650
650 /* Basic card reinitialization. */ 651 /* Basic card reinitialization. */
651 mmc_claim_host(host); 652 mmc_claim_host(host);
652 653
653 /* No need to reinitialize powered-resumed nonremovable cards */ 654 /* No need to reinitialize powered-resumed nonremovable cards */
654 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) 655 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
655 err = mmc_sdio_init_card(host, host->ocr, host->card, 656 err = mmc_sdio_init_card(host, host->ocr, host->card,
656 mmc_card_keep_power(host)); 657 mmc_card_keep_power(host));
657 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 658 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
658 /* We may have switched to 1-bit mode during suspend */ 659 /* We may have switched to 1-bit mode during suspend */
659 err = sdio_enable_4bit_bus(host->card); 660 err = sdio_enable_4bit_bus(host->card);
660 if (err > 0) { 661 if (err > 0) {
661 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 662 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
662 err = 0; 663 err = 0;
663 } 664 }
664 } 665 }
665 666
666 if (!err && host->sdio_irqs) 667 if (!err && host->sdio_irqs)
667 mmc_signal_sdio_irq(host); 668 mmc_signal_sdio_irq(host);
668 mmc_release_host(host); 669 mmc_release_host(host);
669 670
670 /* 671 /*
671 * If the card looked to be the same as before suspending, then 672 * If the card looked to be the same as before suspending, then
672 * we proceed to resume all card functions. If one of them returns 673 * we proceed to resume all card functions. If one of them returns
673 * an error then we simply return that error to the core and the 674 * an error then we simply return that error to the core and the
674 * card will be redetected as new. It is the responsibility of 675 * card will be redetected as new. It is the responsibility of
675 * the function driver to perform further tests with the extra 676 * the function driver to perform further tests with the extra
676 * knowledge it has of the card to confirm the card is indeed the 677 * knowledge it has of the card to confirm the card is indeed the
677 * same as before suspending (same MAC address for network cards, 678 * same as before suspending (same MAC address for network cards,
678 * etc.) and return an error otherwise. 679 * etc.) and return an error otherwise.
679 */ 680 */
680 for (i = 0; !err && i < host->card->sdio_funcs; i++) { 681 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
681 struct sdio_func *func = host->card->sdio_func[i]; 682 struct sdio_func *func = host->card->sdio_func[i];
682 if (func && sdio_func_present(func) && func->dev.driver) { 683 if (func && sdio_func_present(func) && func->dev.driver) {
683 const struct dev_pm_ops *pmops = func->dev.driver->pm; 684 const struct dev_pm_ops *pmops = func->dev.driver->pm;
684 err = pmops->resume(&func->dev); 685 err = pmops->resume(&func->dev);
685 } 686 }
686 } 687 }
687 688
688 return err; 689 return err;
689 } 690 }
690 691
691 static int mmc_sdio_power_restore(struct mmc_host *host) 692 static int mmc_sdio_power_restore(struct mmc_host *host)
692 { 693 {
693 int ret; 694 int ret;
694 u32 ocr; 695 u32 ocr;
695 696
696 BUG_ON(!host); 697 BUG_ON(!host);
697 BUG_ON(!host->card); 698 BUG_ON(!host->card);
698 699
699 mmc_claim_host(host); 700 mmc_claim_host(host);
700 701
701 /* 702 /*
702 * Reset the card by performing the same steps that are taken by 703 * Reset the card by performing the same steps that are taken by
703 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe. 704 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
704 * 705 *
705 * sdio_reset() is technically not needed. Having just powered up the 706 * sdio_reset() is technically not needed. Having just powered up the
706 * hardware, it should already be in reset state. However, some 707 * hardware, it should already be in reset state. However, some
707 * platforms (such as SD8686 on OLPC) do not instantly cut power, 708 * platforms (such as SD8686 on OLPC) do not instantly cut power,
708 * meaning that a reset is required when restoring power soon after 709 * meaning that a reset is required when restoring power soon after
709 * powering off. It is harmless in other cases. 710 * powering off. It is harmless in other cases.
710 * 711 *
711 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec, 712 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
712 * is not necessary for non-removable cards. However, it is required 713 * is not necessary for non-removable cards. However, it is required
713 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and 714 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
714 * harmless in other situations. 715 * harmless in other situations.
715 * 716 *
716 * With these steps taken, mmc_select_voltage() is also required to 717 * With these steps taken, mmc_select_voltage() is also required to
717 * restore the correct voltage setting of the card. 718 * restore the correct voltage setting of the card.
718 */ 719 */
719 sdio_reset(host); 720 sdio_reset(host);
720 mmc_go_idle(host); 721 mmc_go_idle(host);
721 mmc_send_if_cond(host, host->ocr_avail); 722 mmc_send_if_cond(host, host->ocr_avail);
722 723
723 ret = mmc_send_io_op_cond(host, 0, &ocr); 724 ret = mmc_send_io_op_cond(host, 0, &ocr);
724 if (ret) 725 if (ret)
725 goto out; 726 goto out;
726 727
727 if (host->ocr_avail_sdio) 728 if (host->ocr_avail_sdio)
728 host->ocr_avail = host->ocr_avail_sdio; 729 host->ocr_avail = host->ocr_avail_sdio;
729 730
730 host->ocr = mmc_select_voltage(host, ocr & ~0x7F); 731 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
731 if (!host->ocr) { 732 if (!host->ocr) {
732 ret = -EINVAL; 733 ret = -EINVAL;
733 goto out; 734 goto out;
734 } 735 }
735 736
736 ret = mmc_sdio_init_card(host, host->ocr, host->card, 737 ret = mmc_sdio_init_card(host, host->ocr, host->card,
737 mmc_card_keep_power(host)); 738 mmc_card_keep_power(host));
738 if (!ret && host->sdio_irqs) 739 if (!ret && host->sdio_irqs)
739 mmc_signal_sdio_irq(host); 740 mmc_signal_sdio_irq(host);
740 741
741 out: 742 out:
742 mmc_release_host(host); 743 mmc_release_host(host);
743 744
744 return ret; 745 return ret;
745 } 746 }
746 747
747 static const struct mmc_bus_ops mmc_sdio_ops = { 748 static const struct mmc_bus_ops mmc_sdio_ops = {
748 .remove = mmc_sdio_remove, 749 .remove = mmc_sdio_remove,
749 .detect = mmc_sdio_detect, 750 .detect = mmc_sdio_detect,
750 .suspend = mmc_sdio_suspend, 751 .suspend = mmc_sdio_suspend,
751 .resume = mmc_sdio_resume, 752 .resume = mmc_sdio_resume,
752 .power_restore = mmc_sdio_power_restore, 753 .power_restore = mmc_sdio_power_restore,
753 }; 754 };
754 755
755 756
756 /* 757 /*
757 * Starting point for SDIO card init. 758 * Starting point for SDIO card init.
758 */ 759 */
759 int mmc_attach_sdio(struct mmc_host *host) 760 int mmc_attach_sdio(struct mmc_host *host)
760 { 761 {
761 int err, i, funcs; 762 int err, i, funcs;
762 u32 ocr; 763 u32 ocr;
763 struct mmc_card *card; 764 struct mmc_card *card;
764 765
765 BUG_ON(!host); 766 BUG_ON(!host);
766 WARN_ON(!host->claimed); 767 WARN_ON(!host->claimed);
767 768
768 err = mmc_send_io_op_cond(host, 0, &ocr); 769 err = mmc_send_io_op_cond(host, 0, &ocr);
769 if (err) 770 if (err)
770 return err; 771 return err;
771 772
772 mmc_attach_bus(host, &mmc_sdio_ops); 773 mmc_attach_bus(host, &mmc_sdio_ops);
773 if (host->ocr_avail_sdio) 774 if (host->ocr_avail_sdio)
774 host->ocr_avail = host->ocr_avail_sdio; 775 host->ocr_avail = host->ocr_avail_sdio;
775 776
776 /* 777 /*
777 * Sanity check the voltages that the card claims to 778 * Sanity check the voltages that the card claims to
778 * support. 779 * support.
779 */ 780 */
780 if (ocr & 0x7F) { 781 if (ocr & 0x7F) {
781 printk(KERN_WARNING "%s: card claims to support voltages " 782 printk(KERN_WARNING "%s: card claims to support voltages "
782 "below the defined range. These will be ignored.\n", 783 "below the defined range. These will be ignored.\n",
783 mmc_hostname(host)); 784 mmc_hostname(host));
784 ocr &= ~0x7F; 785 ocr &= ~0x7F;
785 } 786 }
786 787
787 host->ocr = mmc_select_voltage(host, ocr); 788 host->ocr = mmc_select_voltage(host, ocr);
788 789
789 /* 790 /*
790 * Can we support the voltage(s) of the card(s)? 791 * Can we support the voltage(s) of the card(s)?
791 */ 792 */
792 if (!host->ocr) { 793 if (!host->ocr) {
793 err = -EINVAL; 794 err = -EINVAL;
794 goto err; 795 goto err;
795 } 796 }
796 797
797 /* 798 /*
798 * Detect and init the card. 799 * Detect and init the card.
799 */ 800 */
800 err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 801 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
801 if (err) 802 if (err)
802 goto err; 803 goto err;
803 card = host->card; 804 card = host->card;
804 805
805 /* 806 /*
806 * Enable runtime PM only if supported by host+card+board 807 * Enable runtime PM only if supported by host+card+board
807 */ 808 */
808 if (host->caps & MMC_CAP_POWER_OFF_CARD) { 809 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
809 /* 810 /*
810 * Let runtime PM core know our card is active 811 * Let runtime PM core know our card is active
811 */ 812 */
812 err = pm_runtime_set_active(&card->dev); 813 err = pm_runtime_set_active(&card->dev);
813 if (err) 814 if (err)
814 goto remove; 815 goto remove;
815 816
816 /* 817 /*
817 * Enable runtime PM for this card 818 * Enable runtime PM for this card
818 */ 819 */
819 pm_runtime_enable(&card->dev); 820 pm_runtime_enable(&card->dev);
820 } 821 }
821 822
822 /* 823 /*
823 * The number of functions on the card is encoded inside 824 * The number of functions on the card is encoded inside
824 * the ocr. 825 * the ocr.
825 */ 826 */
826 funcs = (ocr & 0x70000000) >> 28; 827 funcs = (ocr & 0x70000000) >> 28;
827 card->sdio_funcs = 0; 828 card->sdio_funcs = 0;
828 829
829 /* 830 /*
830 * Initialize (but don't add) all present functions. 831 * Initialize (but don't add) all present functions.
831 */ 832 */
832 for (i = 0; i < funcs; i++, card->sdio_funcs++) { 833 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
833 err = sdio_init_func(host->card, i + 1); 834 err = sdio_init_func(host->card, i + 1);
834 if (err) 835 if (err)
835 goto remove; 836 goto remove;
836 837
837 /* 838 /*
838 * Enable Runtime PM for this func (if supported) 839 * Enable Runtime PM for this func (if supported)
839 */ 840 */
840 if (host->caps & MMC_CAP_POWER_OFF_CARD) 841 if (host->caps & MMC_CAP_POWER_OFF_CARD)
841 pm_runtime_enable(&card->sdio_func[i]->dev); 842 pm_runtime_enable(&card->sdio_func[i]->dev);
842 } 843 }
843 844
844 /* 845 /*
845 * First add the card to the driver model... 846 * First add the card to the driver model...
846 */ 847 */
847 mmc_release_host(host); 848 mmc_release_host(host);
848 err = mmc_add_card(host->card); 849 err = mmc_add_card(host->card);
849 if (err) 850 if (err)
850 goto remove_added; 851 goto remove_added;
851 852
852 /* 853 /*
853 * ...then the SDIO functions. 854 * ...then the SDIO functions.
854 */ 855 */
855 for (i = 0;i < funcs;i++) { 856 for (i = 0;i < funcs;i++) {
856 err = sdio_add_func(host->card->sdio_func[i]); 857 err = sdio_add_func(host->card->sdio_func[i]);
857 if (err) 858 if (err)
858 goto remove_added; 859 goto remove_added;
859 } 860 }
860 861
861 mmc_claim_host(host); 862 mmc_claim_host(host);
862 return 0; 863 return 0;
863 864
864 865
865 remove_added: 866 remove_added:
866 /* Remove without lock if the device has been added. */ 867 /* Remove without lock if the device has been added. */
867 mmc_sdio_remove(host); 868 mmc_sdio_remove(host);
868 mmc_claim_host(host); 869 mmc_claim_host(host);
869 remove: 870 remove:
870 /* And with lock if it hasn't been added. */ 871 /* And with lock if it hasn't been added. */
871 mmc_release_host(host); 872 mmc_release_host(host);
872 if (host->card) 873 if (host->card)
873 mmc_sdio_remove(host); 874 mmc_sdio_remove(host);
874 mmc_claim_host(host); 875 mmc_claim_host(host);
875 err: 876 err:
876 mmc_detach_bus(host); 877 mmc_detach_bus(host);
877 878
878 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", 879 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
879 mmc_hostname(host), err); 880 mmc_hostname(host), err);
880 881
881 return err; 882 return err;
882 } 883 }
883 884
884 885