Commit e7747475b61fdc2a4a412475a9d64d8c309916e3

Authored by Ulf Hansson
Committed by Chris Ball
1 parent 8f7f6b7e46

mmc: core: Fixup suspend/resume issues for UHS-I cards

Even if cards supports 1.8V I/O voltage those should anyway be
initialized at 3.3V I/O according to (e)MMC, SD and SDIO specs.
Some eMMC and embedded SDIO devices are able to be initialized
at 1.8V as well, but it is better to be safe.

Do note that initialization in this context means that the card
has been completely powered off, otherwise the card will remain
at the last I/O voltage level that were negotitiated.

Due to the above being taken care of the suspend/resume issues
for UHS-I SD-cards has been fixed.

Signed-off-by: Ulf Hansson <ulf.hansson@stericsson.com>
Acked-by: Philip Rakity <prakity@marvell.com>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
Tested-by: Subhash Jadavani <subhashj@codeaurora.org>
Signed-off-by: Chris Ball <cjb@laptop.org>

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