blob: e9f74a29439469f5d55ac30ec47f4db8599394c4 [file] [log] [blame]
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001/*
2 * linux/drivers/mmc/core/host.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossmanff3112f2008-03-08 23:43:19 +01005 * Copyright (C) 2007-2008 Pierre Ossman
Linus Walleij04566832010-11-08 21:36:50 -05006 * Copyright (C) 2010 Linus Walleij
Pierre Ossmanb93931a2007-05-19 14:06:24 +02007 *
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
10 * published by the Free Software Foundation.
11 *
12 * MMC host class device management
13 */
14
15#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/idr.h>
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +010018#include <linux/of.h>
19#include <linux/of_gpio.h>
Pierre Ossmanb93931a2007-05-19 14:06:24 +020020#include <linux/pagemap.h>
Paul Gortmaker3ef77af2011-07-10 12:42:00 -040021#include <linux/export.h>
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020022#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Pierre Ossmanb93931a2007-05-19 14:06:24 +020024
25#include <linux/mmc/host.h>
Linus Walleij04566832010-11-08 21:36:50 -050026#include <linux/mmc/card.h>
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +010027#include <linux/mmc/slot-gpio.h>
Pierre Ossmanb93931a2007-05-19 14:06:24 +020028
29#include "core.h"
30#include "host.h"
Ulf Hanssondf8aca12014-12-18 15:44:36 +010031#include "slot-gpio.h"
Ulf Hansson3aa87932014-11-28 14:38:36 +010032#include "pwrseq.h"
Pierre Ossmanb93931a2007-05-19 14:06:24 +020033
Ulf Hansson5674a9b2016-04-07 11:40:59 +020034static DEFINE_IDA(mmc_host_ida);
Ulf Hanssone2d19262014-12-18 15:44:35 +010035static DEFINE_SPINLOCK(mmc_host_lock);
36
Pierre Ossmanb93931a2007-05-19 14:06:24 +020037static void mmc_host_classdev_release(struct device *dev)
38{
39 struct mmc_host *host = cls_dev_to_mmc_host(dev);
Ulf Hanssone2d19262014-12-18 15:44:35 +010040 spin_lock(&mmc_host_lock);
Ulf Hansson5674a9b2016-04-07 11:40:59 +020041 ida_remove(&mmc_host_ida, host->index);
Ulf Hanssone2d19262014-12-18 15:44:35 +010042 spin_unlock(&mmc_host_lock);
Pierre Ossmanb93931a2007-05-19 14:06:24 +020043 kfree(host);
44}
45
46static struct class mmc_host_class = {
47 .name = "mmc_host",
48 .dev_release = mmc_host_classdev_release,
49};
50
51int mmc_register_host_class(void)
52{
53 return class_register(&mmc_host_class);
54}
55
56void mmc_unregister_host_class(void)
57{
58 class_unregister(&mmc_host_class);
59}
60
Subhash Jadavanidff31262016-04-19 16:21:16 -070061#ifdef CONFIG_MMC_CLKGATE
62static ssize_t clkgate_delay_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
64{
65 struct mmc_host *host = cls_dev_to_mmc_host(dev);
66 return snprintf(buf, PAGE_SIZE, "%lu\n", host->clkgate_delay);
67}
68
69static ssize_t clkgate_delay_store(struct device *dev,
70 struct device_attribute *attr, const char *buf, size_t count)
71{
72 struct mmc_host *host = cls_dev_to_mmc_host(dev);
73 unsigned long flags, value;
74
75 if (kstrtoul(buf, 0, &value))
76 return -EINVAL;
77
78 spin_lock_irqsave(&host->clk_lock, flags);
79 host->clkgate_delay = value;
80 spin_unlock_irqrestore(&host->clk_lock, flags);
81 return count;
82}
83
84/*
85 * Enabling clock gating will make the core call out to the host
86 * once up and once down when it performs a request or card operation
87 * intermingled in any fashion. The driver will see this through
88 * set_ios() operations with ios.clock field set to 0 to gate (disable)
89 * the block clock, and to the old frequency to enable it again.
90 */
91static void mmc_host_clk_gate_delayed(struct mmc_host *host)
92{
93 unsigned long tick_ns;
94 unsigned long freq = host->ios.clock;
95 unsigned long flags;
96
97 if (!freq) {
98 pr_debug("%s: frequency set to 0 in disable function, "
99 "this means the clock is already disabled.\n",
100 mmc_hostname(host));
101 return;
102 }
103 /*
104 * New requests may have appeared while we were scheduling,
105 * then there is no reason to delay the check before
106 * clk_disable().
107 */
108 spin_lock_irqsave(&host->clk_lock, flags);
109
110 /*
111 * Delay n bus cycles (at least 8 from MMC spec) before attempting
112 * to disable the MCI block clock. The reference count may have
113 * gone up again after this delay due to rescheduling!
114 */
115 if (!host->clk_requests) {
116 spin_unlock_irqrestore(&host->clk_lock, flags);
117 tick_ns = DIV_ROUND_UP(1000000000, freq);
118 ndelay(host->clk_delay * tick_ns);
119 } else {
120 /* New users appeared while waiting for this work */
121 spin_unlock_irqrestore(&host->clk_lock, flags);
122 return;
123 }
124 mutex_lock(&host->clk_gate_mutex);
125 spin_lock_irqsave(&host->clk_lock, flags);
126 if (!host->clk_requests) {
127 spin_unlock_irqrestore(&host->clk_lock, flags);
128 /* This will set host->ios.clock to 0 */
129 mmc_gate_clock(host);
130 spin_lock_irqsave(&host->clk_lock, flags);
131 pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
132 }
133 spin_unlock_irqrestore(&host->clk_lock, flags);
134 mutex_unlock(&host->clk_gate_mutex);
135}
136
137/*
138 * Internal work. Work to disable the clock at some later point.
139 */
140static void mmc_host_clk_gate_work(struct work_struct *work)
141{
142 struct mmc_host *host = container_of(work, struct mmc_host,
143 clk_gate_work.work);
144
145 mmc_host_clk_gate_delayed(host);
146}
147
148/**
149 * mmc_host_clk_hold - ungate hardware MCI clocks
150 * @host: host to ungate.
151 *
152 * Makes sure the host ios.clock is restored to a non-zero value
153 * past this call. Increase clock reference count and ungate clock
154 * if we're the first user.
155 */
156void mmc_host_clk_hold(struct mmc_host *host)
157{
158 unsigned long flags;
159
160 /* cancel any clock gating work scheduled by mmc_host_clk_release() */
161 cancel_delayed_work_sync(&host->clk_gate_work);
162 mutex_lock(&host->clk_gate_mutex);
163 spin_lock_irqsave(&host->clk_lock, flags);
164 if (host->clk_gated) {
165 spin_unlock_irqrestore(&host->clk_lock, flags);
166 mmc_ungate_clock(host);
167 spin_lock_irqsave(&host->clk_lock, flags);
168 pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
169 }
170 host->clk_requests++;
171 spin_unlock_irqrestore(&host->clk_lock, flags);
172 mutex_unlock(&host->clk_gate_mutex);
173}
174
175/**
176 * mmc_host_may_gate_card - check if this card may be gated
177 * @card: card to check.
178 */
179static bool mmc_host_may_gate_card(struct mmc_card *card)
180{
181 /* If there is no card we may gate it */
182 if (!card)
183 return true;
184 /*
185 * Don't gate SDIO cards! These need to be clocked at all times
186 * since they may be independent systems generating interrupts
187 * and other events. The clock requests counter from the core will
188 * go down to zero since the core does not need it, but we will not
189 * gate the clock, because there is somebody out there that may still
190 * be using it.
191 */
192 return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
193}
194
195/**
196 * mmc_host_clk_release - gate off hardware MCI clocks
197 * @host: host to gate.
198 *
199 * Calls the host driver with ios.clock set to zero as often as possible
200 * in order to gate off hardware MCI clocks. Decrease clock reference
201 * count and schedule disabling of clock.
202 */
203void mmc_host_clk_release(struct mmc_host *host)
204{
205 unsigned long flags;
206
207 spin_lock_irqsave(&host->clk_lock, flags);
208 host->clk_requests--;
209 if (mmc_host_may_gate_card(host->card) &&
210 !host->clk_requests)
211 schedule_delayed_work(&host->clk_gate_work,
212 msecs_to_jiffies(host->clkgate_delay));
213 spin_unlock_irqrestore(&host->clk_lock, flags);
214}
215
216/**
217 * mmc_host_clk_rate - get current clock frequency setting
218 * @host: host to get the clock frequency for.
219 *
220 * Returns current clock frequency regardless of gating.
221 */
222unsigned int mmc_host_clk_rate(struct mmc_host *host)
223{
224 unsigned long freq;
225 unsigned long flags;
226
227 spin_lock_irqsave(&host->clk_lock, flags);
228 if (host->clk_gated)
229 freq = host->clk_old;
230 else
231 freq = host->ios.clock;
232 spin_unlock_irqrestore(&host->clk_lock, flags);
233 return freq;
234}
235
236/**
237 * mmc_host_clk_init - set up clock gating code
238 * @host: host with potential clock to control
239 */
240static inline void mmc_host_clk_init(struct mmc_host *host)
241{
242 host->clk_requests = 0;
243 /* Hold MCI clock for 8 cycles by default */
244 host->clk_delay = 8;
245 /*
246 * Default clock gating delay is 0ms to avoid wasting power.
247 * This value can be tuned by writing into sysfs entry.
248 */
249 host->clkgate_delay = 0;
250 host->clk_gated = false;
251 INIT_DELAYED_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
252 spin_lock_init(&host->clk_lock);
253 mutex_init(&host->clk_gate_mutex);
254}
255
256/**
257 * mmc_host_clk_exit - shut down clock gating code
258 * @host: host with potential clock to control
259 */
260static inline void mmc_host_clk_exit(struct mmc_host *host)
261{
262 /*
263 * Wait for any outstanding gate and then make sure we're
264 * ungated before exiting.
265 */
266 if (cancel_delayed_work_sync(&host->clk_gate_work))
267 mmc_host_clk_gate_delayed(host);
268 if (host->clk_gated)
269 mmc_host_clk_hold(host);
270 /* There should be only one user now */
271 WARN_ON(host->clk_requests > 1);
272}
273
274static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
275{
276 host->clkgate_delay_attr.show = clkgate_delay_show;
277 host->clkgate_delay_attr.store = clkgate_delay_store;
278 sysfs_attr_init(&host->clkgate_delay_attr.attr);
279 host->clkgate_delay_attr.attr.name = "clkgate_delay";
280 host->clkgate_delay_attr.attr.mode = S_IRUGO | S_IWUSR;
281 if (device_create_file(&host->class_dev, &host->clkgate_delay_attr))
282 pr_err("%s: Failed to create clkgate_delay sysfs entry\n",
283 mmc_hostname(host));
284}
285#else
286
287static inline void mmc_host_clk_init(struct mmc_host *host)
288{
289}
290
291static inline void mmc_host_clk_exit(struct mmc_host *host)
292{
293}
294
295static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
296{
297}
298
Sahitya Tummala043744a2013-06-24 09:55:33 +0530299bool mmc_host_may_gate_card(struct mmc_card *card)
300{
301 return false;
302}
Subhash Jadavanidff31262016-04-19 16:21:16 -0700303#endif
304
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300305void mmc_retune_enable(struct mmc_host *host)
306{
307 host->can_retune = 1;
308 if (host->retune_period)
309 mod_timer(&host->retune_timer,
310 jiffies + host->retune_period * HZ);
311}
312
Adrian Hunter7ff27602016-05-16 15:35:24 +0300313/*
314 * Pause re-tuning for a small set of operations. The pause begins after the
315 * next command and after first doing re-tuning.
316 */
317void mmc_retune_pause(struct mmc_host *host)
318{
319 if (!host->retune_paused) {
320 host->retune_paused = 1;
321 mmc_retune_needed(host);
322 mmc_retune_hold(host);
323 }
324}
325EXPORT_SYMBOL(mmc_retune_pause);
326
327void mmc_retune_unpause(struct mmc_host *host)
328{
329 if (host->retune_paused) {
330 host->retune_paused = 0;
331 mmc_retune_release(host);
332 }
333}
334EXPORT_SYMBOL(mmc_retune_unpause);
335
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300336void mmc_retune_disable(struct mmc_host *host)
337{
Adrian Hunter7ff27602016-05-16 15:35:24 +0300338 mmc_retune_unpause(host);
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300339 host->can_retune = 0;
340 del_timer_sync(&host->retune_timer);
341 host->retune_now = 0;
342 host->need_retune = 0;
343}
344
345void mmc_retune_timer_stop(struct mmc_host *host)
346{
347 del_timer_sync(&host->retune_timer);
348}
349EXPORT_SYMBOL(mmc_retune_timer_stop);
350
351void mmc_retune_hold(struct mmc_host *host)
352{
353 if (!host->hold_retune)
354 host->retune_now = 1;
355 host->hold_retune += 1;
356}
357
358void mmc_retune_release(struct mmc_host *host)
359{
360 if (host->hold_retune)
361 host->hold_retune -= 1;
362 else
363 WARN_ON(1);
364}
365
366int mmc_retune(struct mmc_host *host)
367{
Adrian Hunter6376f692015-05-07 13:10:20 +0300368 bool return_to_hs400 = false;
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300369 int err;
370
371 if (host->retune_now)
372 host->retune_now = 0;
373 else
374 return 0;
375
376 if (!host->need_retune || host->doing_retune || !host->card)
377 return 0;
378
379 host->need_retune = 0;
380
381 host->doing_retune = 1;
382
Adrian Hunter6376f692015-05-07 13:10:20 +0300383 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
384 err = mmc_hs400_to_hs200(host->card);
385 if (err)
386 goto out;
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300387
Adrian Hunter6376f692015-05-07 13:10:20 +0300388 return_to_hs400 = true;
389
390 if (host->ops->prepare_hs400_tuning)
391 host->ops->prepare_hs400_tuning(host, &host->ios);
392 }
393
394 err = mmc_execute_tuning(host->card);
395 if (err)
396 goto out;
397
398 if (return_to_hs400)
399 err = mmc_hs200_to_hs400(host->card);
400out:
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300401 host->doing_retune = 0;
402
403 return err;
404}
405
406static void mmc_retune_timer(unsigned long data)
407{
408 struct mmc_host *host = (struct mmc_host *)data;
409
410 mmc_retune_needed(host);
411}
412
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200413/**
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100414 * mmc_of_parse() - parse host's device-tree node
415 * @host: host whose node should be parsed.
416 *
417 * To keep the rest of the MMC subsystem unaware of whether DT has been
418 * used to to instantiate and configure this host instance or not, we
419 * parse the properties and set respective generic mmc-host flags and
420 * parameters.
421 */
Simon Baatzec0a7512013-06-09 22:14:11 +0200422int mmc_of_parse(struct mmc_host *host)
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100423{
424 struct device_node *np;
425 u32 bus_width;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300426 int ret;
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200427 bool cd_cap_invert, cd_gpio_invert = false;
428 bool ro_cap_invert, ro_gpio_invert = false;
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100429
430 if (!host->parent || !host->parent->of_node)
Simon Baatzec0a7512013-06-09 22:14:11 +0200431 return 0;
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100432
433 np = host->parent->of_node;
434
435 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
436 if (of_property_read_u32(np, "bus-width", &bus_width) < 0) {
437 dev_dbg(host->parent,
438 "\"bus-width\" property is missing, assuming 1 bit.\n");
439 bus_width = 1;
440 }
441
442 switch (bus_width) {
443 case 8:
444 host->caps |= MMC_CAP_8_BIT_DATA;
445 /* Hosts capable of 8-bit transfers can also do 4 bits */
446 case 4:
447 host->caps |= MMC_CAP_4_BIT_DATA;
448 break;
449 case 1:
450 break;
451 default:
452 dev_err(host->parent,
Alexander Shiyan1c279f42014-02-15 15:40:58 +0400453 "Invalid \"bus-width\" value %u!\n", bus_width);
Simon Baatzec0a7512013-06-09 22:14:11 +0200454 return -EINVAL;
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100455 }
456
457 /* f_max is obtained from the optional "max-frequency" property */
458 of_property_read_u32(np, "max-frequency", &host->f_max);
459
460 /*
461 * Configure CD and WP pins. They are both by default active low to
462 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
463 * mmc-gpio helpers are used to attach, configure and use them. If
464 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
465 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
466 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
467 * is set. If the "non-removable" property is found, the
468 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
469 * configuration is performed.
470 */
471
472 /* Parse Card Detection */
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300473 if (of_property_read_bool(np, "non-removable")) {
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100474 host->caps |= MMC_CAP_NONREMOVABLE;
475 } else {
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200476 cd_cap_invert = of_property_read_bool(np, "cd-inverted");
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100477
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300478 if (of_property_read_bool(np, "broken-cd"))
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100479 host->caps |= MMC_CAP_NEEDS_POLL;
480
Linus Walleij89168b42014-10-02 09:08:46 +0200481 ret = mmc_gpiod_request_cd(host, "cd", 0, true,
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200482 0, &cd_gpio_invert);
Ulf Hansson91167522014-12-18 10:41:44 +0100483 if (!ret)
Linus Walleij98e90de2014-08-27 13:00:52 +0200484 dev_info(host->parent, "Got CD GPIO\n");
Ulf Hansson43934ec2015-09-14 12:18:55 +0200485 else if (ret != -ENOENT && ret != -ENOSYS)
Ulf Hansson91167522014-12-18 10:41:44 +0100486 return ret;
Linus Walleij89168b42014-10-02 09:08:46 +0200487
488 /*
489 * There are two ways to flag that the CD line is inverted:
490 * through the cd-inverted flag and by the GPIO line itself
491 * being inverted from the GPIO subsystem. This is a leftover
492 * from the times when the GPIO subsystem did not make it
493 * possible to flag a line as inverted.
494 *
495 * If the capability on the host AND the GPIO line are
496 * both inverted, the end result is that the CD line is
497 * not inverted.
498 */
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200499 if (cd_cap_invert ^ cd_gpio_invert)
Linus Walleij89168b42014-10-02 09:08:46 +0200500 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100501 }
502
503 /* Parse Write Protection */
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200504 ro_cap_invert = of_property_read_bool(np, "wp-inverted");
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100505
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200506 ret = mmc_gpiod_request_ro(host, "wp", 0, false, 0, &ro_gpio_invert);
Ulf Hansson91167522014-12-18 10:41:44 +0100507 if (!ret)
Linus Walleij98e90de2014-08-27 13:00:52 +0200508 dev_info(host->parent, "Got WP GPIO\n");
Ulf Hansson43934ec2015-09-14 12:18:55 +0200509 else if (ret != -ENOENT && ret != -ENOSYS)
Ulf Hansson91167522014-12-18 10:41:44 +0100510 return ret;
Linus Walleij98e90de2014-08-27 13:00:52 +0200511
Lars-Peter Clausen19f44242015-05-06 20:31:20 +0200512 if (of_property_read_bool(np, "disable-wp"))
513 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
514
Linus Walleij89168b42014-10-02 09:08:46 +0200515 /* See the comment on CD inversion above */
Kristina Martsenkoa31b0c62014-11-05 02:22:41 +0200516 if (ro_cap_invert ^ ro_gpio_invert)
Linus Walleij89168b42014-10-02 09:08:46 +0200517 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
518
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300519 if (of_property_read_bool(np, "cap-sd-highspeed"))
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100520 host->caps |= MMC_CAP_SD_HIGHSPEED;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300521 if (of_property_read_bool(np, "cap-mmc-highspeed"))
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100522 host->caps |= MMC_CAP_MMC_HIGHSPEED;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300523 if (of_property_read_bool(np, "sd-uhs-sdr12"))
Ulf Hanssonb66bd0e2014-02-14 13:27:07 +0100524 host->caps |= MMC_CAP_UHS_SDR12;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300525 if (of_property_read_bool(np, "sd-uhs-sdr25"))
Ulf Hanssonb66bd0e2014-02-14 13:27:07 +0100526 host->caps |= MMC_CAP_UHS_SDR25;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300527 if (of_property_read_bool(np, "sd-uhs-sdr50"))
Ulf Hanssonb66bd0e2014-02-14 13:27:07 +0100528 host->caps |= MMC_CAP_UHS_SDR50;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300529 if (of_property_read_bool(np, "sd-uhs-sdr104"))
Ulf Hanssonb66bd0e2014-02-14 13:27:07 +0100530 host->caps |= MMC_CAP_UHS_SDR104;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300531 if (of_property_read_bool(np, "sd-uhs-ddr50"))
Ulf Hanssonb66bd0e2014-02-14 13:27:07 +0100532 host->caps |= MMC_CAP_UHS_DDR50;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300533 if (of_property_read_bool(np, "cap-power-off-card"))
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100534 host->caps |= MMC_CAP_POWER_OFF_CARD;
Chaotian Jing794f1572015-10-27 14:24:21 +0800535 if (of_property_read_bool(np, "cap-mmc-hw-reset"))
536 host->caps |= MMC_CAP_HW_RESET;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300537 if (of_property_read_bool(np, "cap-sdio-irq"))
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100538 host->caps |= MMC_CAP_SDIO_IRQ;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300539 if (of_property_read_bool(np, "full-pwr-cycle"))
Ulf Hansson5a36d6b2013-06-10 17:03:47 +0200540 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300541 if (of_property_read_bool(np, "keep-power-in-suspend"))
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100542 host->pm_caps |= MMC_PM_KEEP_POWER;
Sudeep Holla0dbcdc02015-10-21 11:10:02 +0100543 if (of_property_read_bool(np, "wakeup-source") ||
544 of_property_read_bool(np, "enable-sdio-wakeup")) /* legacy */
Guennadi Liakhovetski2fdb6e22013-02-15 16:14:01 +0100545 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300546 if (of_property_read_bool(np, "mmc-ddr-1_8v"))
Ulf Hanssonc0baf842014-02-14 13:27:08 +0100547 host->caps |= MMC_CAP_1_8V_DDR;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300548 if (of_property_read_bool(np, "mmc-ddr-1_2v"))
Ulf Hanssonc0baf842014-02-14 13:27:08 +0100549 host->caps |= MMC_CAP_1_2V_DDR;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300550 if (of_property_read_bool(np, "mmc-hs200-1_8v"))
Jaehoon Chung321bd412014-02-14 13:27:09 +0100551 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300552 if (of_property_read_bool(np, "mmc-hs200-1_2v"))
Jaehoon Chung321bd412014-02-14 13:27:09 +0100553 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300554 if (of_property_read_bool(np, "mmc-hs400-1_8v"))
Seungwon Jeonc373eb42014-04-23 17:15:08 +0900555 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
Sergei Shtylyov90614cd2015-08-07 01:06:48 +0300556 if (of_property_read_bool(np, "mmc-hs400-1_2v"))
Seungwon Jeonc373eb42014-04-23 17:15:08 +0900557 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
Shawn Linef29c0e2016-05-26 09:56:12 +0800558 if (of_property_read_bool(np, "mmc-hs400-enhanced-strobe"))
559 host->caps2 |= MMC_CAP2_HS400_ES;
Shawn Lin6ae3e532016-07-14 16:26:04 +0800560 if (of_property_read_bool(np, "no-sdio"))
561 host->caps2 |= MMC_CAP2_NO_SDIO;
562 if (of_property_read_bool(np, "no-sd"))
563 host->caps2 |= MMC_CAP2_NO_SD;
564 if (of_property_read_bool(np, "no-mmc"))
565 host->caps2 |= MMC_CAP2_NO_MMC;
Simon Baatzec0a7512013-06-09 22:14:11 +0200566
Sascha Hauer3d705d12014-08-19 10:45:51 +0200567 host->dsr_req = !of_property_read_u32(np, "dsr", &host->dsr);
568 if (host->dsr_req && (host->dsr & ~0xffff)) {
569 dev_err(host->parent,
570 "device tree specified broken value for DSR: 0x%x, ignoring\n",
571 host->dsr);
572 host->dsr_req = 0;
573 }
574
Ulf Hansson3aa87932014-11-28 14:38:36 +0100575 return mmc_pwrseq_alloc(host);
Guennadi Liakhovetski6c56e7a2013-02-16 16:21:16 +0100576}
577
578EXPORT_SYMBOL(mmc_of_parse);
579
580/**
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200581 * mmc_alloc_host - initialise the per-host structure.
582 * @extra: sizeof private data structure
583 * @dev: pointer to host device model structure
584 *
585 * Initialise the per-host structure.
586 */
587struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
588{
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100589 int err;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200590 struct mmc_host *host;
591
Mariusz Kozlowskibe760a92007-08-10 14:00:50 -0700592 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200593 if (!host)
594 return NULL;
595
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +0200596 /* scanning will be enabled when we're ready */
597 host->rescan_disable = 1;
Ulf Hansson5674a9b2016-04-07 11:40:59 +0200598
599again:
600 if (!ida_pre_get(&mmc_host_ida, GFP_KERNEL)) {
601 kfree(host);
602 return NULL;
603 }
604
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100605 spin_lock(&mmc_host_lock);
Ulf Hansson5674a9b2016-04-07 11:40:59 +0200606 err = ida_get_new(&mmc_host_ida, &host->index);
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100607 spin_unlock(&mmc_host_lock);
Ulf Hansson5674a9b2016-04-07 11:40:59 +0200608
609 if (err == -EAGAIN) {
610 goto again;
611 } else if (err) {
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100612 kfree(host);
613 return NULL;
614 }
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100615
Kay Sieversd1b26862008-11-08 21:37:46 +0100616 dev_set_name(&host->class_dev, "mmc%d", host->index);
Pierre Ossmanff3112f2008-03-08 23:43:19 +0100617
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200618 host->parent = dev;
619 host->class_dev.parent = dev;
620 host->class_dev.class = &mmc_host_class;
621 device_initialize(&host->class_dev);
Fu, Zhonghuiccf7bfd2016-01-22 11:32:18 +0800622 device_enable_async_suspend(&host->class_dev);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200623
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100624 if (mmc_gpio_alloc(host)) {
625 put_device(&host->class_dev);
626 return NULL;
627 }
Linus Walleij04566832010-11-08 21:36:50 -0500628
Subhash Jadavanidff31262016-04-19 16:21:16 -0700629 mmc_host_clk_init(host);
630
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200631 spin_lock_init(&host->lock);
632 init_waitqueue_head(&host->wq);
633 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
Adrian Hunterdfa13eb2015-05-07 13:10:12 +0300634 setup_timer(&host->retune_timer, mmc_retune_timer, (unsigned long)host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200635
636 /*
637 * By default, hosts do not support SGIO or large requests.
638 * They have to set these according to their abilities.
639 */
Martin K. Petersena36274e2010-09-10 01:33:59 -0400640 host->max_segs = 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300641 host->max_seg_size = PAGE_SIZE;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200642
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300643 host->max_req_size = PAGE_SIZE;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200644 host->max_blk_size = 512;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300645 host->max_blk_count = PAGE_SIZE / 512;
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200646
647 return host;
648}
649
650EXPORT_SYMBOL(mmc_alloc_host);
651
652/**
653 * mmc_add_host - initialise host hardware
654 * @host: mmc host
Pierre Ossman67a61c42007-07-11 20:22:11 +0200655 *
656 * Register the host with the driver model. The host must be
657 * prepared to start servicing requests before this function
658 * completes.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200659 */
660int mmc_add_host(struct mmc_host *host)
661{
662 int err;
663
Nicolas Pitre17b759a2007-07-24 02:09:39 -0400664 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
665 !host->ops->enable_sdio_irq);
666
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200667 err = device_add(&host->class_dev);
668 if (err)
669 return err;
670
Wolfram Sangf317dfe2011-04-11 06:11:29 +0200671 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
672
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200673#ifdef CONFIG_DEBUG_FS
674 mmc_add_host_debugfs(host);
675#endif
Subhash Jadavanidff31262016-04-19 16:21:16 -0700676 mmc_host_clk_sysfs_init(host);
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200677
Mohan Srinivasane2d88782016-12-14 15:55:36 -0800678#ifdef CONFIG_BLOCK
679 mmc_latency_hist_sysfs_init(host);
680#endif
681
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200682 mmc_start_host(host);
Dmitry Shmidt6fc8bec2010-10-07 14:39:16 -0700683 if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
684 mmc_register_pm_notifier(host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200685
686 return 0;
687}
688
689EXPORT_SYMBOL(mmc_add_host);
690
691/**
692 * mmc_remove_host - remove host hardware
693 * @host: mmc host
694 *
695 * Unregister and remove all cards associated with this host,
Pierre Ossman67a61c42007-07-11 20:22:11 +0200696 * and power down the MMC bus. No new requests will be issued
697 * after this function has returned.
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200698 */
699void mmc_remove_host(struct mmc_host *host)
700{
Dmitry Shmidt6fc8bec2010-10-07 14:39:16 -0700701 if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
702 mmc_unregister_pm_notifier(host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200703 mmc_stop_host(host);
704
Haavard Skinnemoen6edd8ee2008-07-24 14:18:57 +0200705#ifdef CONFIG_DEBUG_FS
706 mmc_remove_host_debugfs(host);
707#endif
708
Mohan Srinivasane2d88782016-12-14 15:55:36 -0800709#ifdef CONFIG_BLOCK
710 mmc_latency_hist_sysfs_exit(host);
711#endif
712
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200713 device_del(&host->class_dev);
714
Pierre Ossman77f1fd62007-10-12 22:48:46 +0200715 led_trigger_unregister_simple(host->led);
Subhash Jadavanidff31262016-04-19 16:21:16 -0700716
717 mmc_host_clk_exit(host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200718}
719
720EXPORT_SYMBOL(mmc_remove_host);
721
722/**
723 * mmc_free_host - free the host structure
724 * @host: mmc host
725 *
726 * Free the host once all references to it have been dropped.
727 */
728void mmc_free_host(struct mmc_host *host)
729{
Ulf Hansson3aa87932014-11-28 14:38:36 +0100730 mmc_pwrseq_free(host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200731 put_device(&host->class_dev);
732}
733
734EXPORT_SYMBOL(mmc_free_host);