import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / mmc / core / host.c
CommitLineData
b93931a6
PO
1/*
2 * linux/drivers/mmc/core/host.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
ff3112f5 5 * Copyright (C) 2007-2008 Pierre Ossman
04566831 6 * Copyright (C) 2010 Linus Walleij
b93931a6
PO
7 *
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>
6c56e7a0
GL
18#include <linux/of.h>
19#include <linux/of_gpio.h>
b93931a6 20#include <linux/pagemap.h>
3ef77af1 21#include <linux/export.h>
af8350c7 22#include <linux/leds.h>
5a0e3ad6 23#include <linux/slab.h>
4c2ef25f 24#include <linux/suspend.h>
b93931a6
PO
25
26#include <linux/mmc/host.h>
04566831 27#include <linux/mmc/card.h>
6c56e7a0 28#include <linux/mmc/slot-gpio.h>
b93931a6
PO
29
30#include "core.h"
31#include "host.h"
6fa3eb70
S
32#define CARD_INIT_TIMEOUT (HZ * 5) //5s
33#ifdef CONFIG_MTK_EMMC_CACHE
34/*
35 * A default time for checking the need for non urgent flush OPs once MMC thread is idle.
36 */
37#define MMC_IDLE_FLUSH_TIME_MS 1500
38#endif
b93931a6
PO
39#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
40
41static void mmc_host_classdev_release(struct device *dev)
42{
43 struct mmc_host *host = cls_dev_to_mmc_host(dev);
a7d1a1eb 44 mutex_destroy(&host->slot.lock);
b93931a6
PO
45 kfree(host);
46}
47
48static struct class mmc_host_class = {
49 .name = "mmc_host",
50 .dev_release = mmc_host_classdev_release,
51};
52
53int mmc_register_host_class(void)
54{
55 return class_register(&mmc_host_class);
56}
57
58void mmc_unregister_host_class(void)
59{
60 class_unregister(&mmc_host_class);
61}
62
63static DEFINE_IDR(mmc_host_idr);
64static DEFINE_SPINLOCK(mmc_host_lock);
65
04566831 66#ifdef CONFIG_MMC_CLKGATE
597dd9d7
SRT
67static ssize_t clkgate_delay_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
69{
70 struct mmc_host *host = cls_dev_to_mmc_host(dev);
4137e504 71 return snprintf(buf, PAGE_SIZE, "%lu\n", host->clkgate_delay);
597dd9d7
SRT
72}
73
74static ssize_t clkgate_delay_store(struct device *dev,
75 struct device_attribute *attr, const char *buf, size_t count)
76{
77 struct mmc_host *host = cls_dev_to_mmc_host(dev);
78 unsigned long flags, value;
79
80 if (kstrtoul(buf, 0, &value))
81 return -EINVAL;
82
83 spin_lock_irqsave(&host->clk_lock, flags);
84 host->clkgate_delay = value;
85 spin_unlock_irqrestore(&host->clk_lock, flags);
597dd9d7
SRT
86 return count;
87}
04566831
LW
88
89/*
90 * Enabling clock gating will make the core call out to the host
91 * once up and once down when it performs a request or card operation
92 * intermingled in any fashion. The driver will see this through
93 * set_ios() operations with ios.clock field set to 0 to gate (disable)
94 * the block clock, and to the old frequency to enable it again.
95 */
96static void mmc_host_clk_gate_delayed(struct mmc_host *host)
97{
98 unsigned long tick_ns;
99 unsigned long freq = host->ios.clock;
100 unsigned long flags;
101
102 if (!freq) {
103 pr_debug("%s: frequency set to 0 in disable function, "
104 "this means the clock is already disabled.\n",
105 mmc_hostname(host));
106 return;
107 }
108 /*
109 * New requests may have appeared while we were scheduling,
110 * then there is no reason to delay the check before
111 * clk_disable().
112 */
113 spin_lock_irqsave(&host->clk_lock, flags);
114
115 /*
116 * Delay n bus cycles (at least 8 from MMC spec) before attempting
117 * to disable the MCI block clock. The reference count may have
118 * gone up again after this delay due to rescheduling!
119 */
120 if (!host->clk_requests) {
121 spin_unlock_irqrestore(&host->clk_lock, flags);
122 tick_ns = DIV_ROUND_UP(1000000000, freq);
123 ndelay(host->clk_delay * tick_ns);
124 } else {
125 /* New users appeared while waiting for this work */
126 spin_unlock_irqrestore(&host->clk_lock, flags);
127 return;
128 }
86f315bb 129 mutex_lock(&host->clk_gate_mutex);
04566831
LW
130 spin_lock_irqsave(&host->clk_lock, flags);
131 if (!host->clk_requests) {
132 spin_unlock_irqrestore(&host->clk_lock, flags);
133 /* This will set host->ios.clock to 0 */
134 mmc_gate_clock(host);
135 spin_lock_irqsave(&host->clk_lock, flags);
136 pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
137 }
138 spin_unlock_irqrestore(&host->clk_lock, flags);
86f315bb 139 mutex_unlock(&host->clk_gate_mutex);
04566831
LW
140}
141
142/*
143 * Internal work. Work to disable the clock at some later point.
144 */
145static void mmc_host_clk_gate_work(struct work_struct *work)
146{
147 struct mmc_host *host = container_of(work, struct mmc_host,
597dd9d7 148 clk_gate_work.work);
04566831
LW
149
150 mmc_host_clk_gate_delayed(host);
151}
152
153/**
08c14071 154 * mmc_host_clk_hold - ungate hardware MCI clocks
04566831
LW
155 * @host: host to ungate.
156 *
157 * Makes sure the host ios.clock is restored to a non-zero value
158 * past this call. Increase clock reference count and ungate clock
159 * if we're the first user.
160 */
08c14071 161void mmc_host_clk_hold(struct mmc_host *host)
04566831
LW
162{
163 unsigned long flags;
164
597dd9d7
SRT
165 /* cancel any clock gating work scheduled by mmc_host_clk_release() */
166 cancel_delayed_work_sync(&host->clk_gate_work);
86f315bb 167 mutex_lock(&host->clk_gate_mutex);
04566831
LW
168 spin_lock_irqsave(&host->clk_lock, flags);
169 if (host->clk_gated) {
170 spin_unlock_irqrestore(&host->clk_lock, flags);
171 mmc_ungate_clock(host);
172 spin_lock_irqsave(&host->clk_lock, flags);
173 pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
174 }
175 host->clk_requests++;
176 spin_unlock_irqrestore(&host->clk_lock, flags);
86f315bb 177 mutex_unlock(&host->clk_gate_mutex);
04566831
LW
178}
179
180/**
181 * mmc_host_may_gate_card - check if this card may be gated
182 * @card: card to check.
183 */
184static bool mmc_host_may_gate_card(struct mmc_card *card)
185{
186 /* If there is no card we may gate it */
187 if (!card)
188 return true;
189 /*
190 * Don't gate SDIO cards! These need to be clocked at all times
191 * since they may be independent systems generating interrupts
192 * and other events. The clock requests counter from the core will
193 * go down to zero since the core does not need it, but we will not
194 * gate the clock, because there is somebody out there that may still
195 * be using it.
196 */
db993500 197 return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
04566831
LW
198}
199
200/**
08c14071 201 * mmc_host_clk_release - gate off hardware MCI clocks
04566831
LW
202 * @host: host to gate.
203 *
204 * Calls the host driver with ios.clock set to zero as often as possible
205 * in order to gate off hardware MCI clocks. Decrease clock reference
206 * count and schedule disabling of clock.
207 */
08c14071 208void mmc_host_clk_release(struct mmc_host *host)
04566831
LW
209{
210 unsigned long flags;
211
212 spin_lock_irqsave(&host->clk_lock, flags);
213 host->clk_requests--;
214 if (mmc_host_may_gate_card(host->card) &&
215 !host->clk_requests)
3b07e9ca
TH
216 schedule_delayed_work(&host->clk_gate_work,
217 msecs_to_jiffies(host->clkgate_delay));
04566831
LW
218 spin_unlock_irqrestore(&host->clk_lock, flags);
219}
220
221/**
222 * mmc_host_clk_rate - get current clock frequency setting
223 * @host: host to get the clock frequency for.
224 *
225 * Returns current clock frequency regardless of gating.
226 */
227unsigned int mmc_host_clk_rate(struct mmc_host *host)
228{
229 unsigned long freq;
230 unsigned long flags;
231
232 spin_lock_irqsave(&host->clk_lock, flags);
233 if (host->clk_gated)
234 freq = host->clk_old;
235 else
236 freq = host->ios.clock;
237 spin_unlock_irqrestore(&host->clk_lock, flags);
238 return freq;
239}
240
241/**
242 * mmc_host_clk_init - set up clock gating code
243 * @host: host with potential clock to control
244 */
245static inline void mmc_host_clk_init(struct mmc_host *host)
246{
247 host->clk_requests = 0;
248 /* Hold MCI clock for 8 cycles by default */
249 host->clk_delay = 8;
597dd9d7 250 /*
c84f15ae 251 * Default clock gating delay is 0ms to avoid wasting power.
597dd9d7
SRT
252 * This value can be tuned by writing into sysfs entry.
253 */
c84f15ae 254 host->clkgate_delay = 0;
04566831 255 host->clk_gated = false;
597dd9d7 256 INIT_DELAYED_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
04566831 257 spin_lock_init(&host->clk_lock);
86f315bb 258 mutex_init(&host->clk_gate_mutex);
04566831
LW
259}
260
261/**
262 * mmc_host_clk_exit - shut down clock gating code
263 * @host: host with potential clock to control
264 */
265static inline void mmc_host_clk_exit(struct mmc_host *host)
266{
267 /*
268 * Wait for any outstanding gate and then make sure we're
269 * ungated before exiting.
270 */
597dd9d7 271 if (cancel_delayed_work_sync(&host->clk_gate_work))
04566831
LW
272 mmc_host_clk_gate_delayed(host);
273 if (host->clk_gated)
08c14071 274 mmc_host_clk_hold(host);
c288b855
LW
275 /* There should be only one user now */
276 WARN_ON(host->clk_requests > 1);
04566831
LW
277}
278
597dd9d7
SRT
279static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
280{
281 host->clkgate_delay_attr.show = clkgate_delay_show;
282 host->clkgate_delay_attr.store = clkgate_delay_store;
283 sysfs_attr_init(&host->clkgate_delay_attr.attr);
284 host->clkgate_delay_attr.attr.name = "clkgate_delay";
285 host->clkgate_delay_attr.attr.mode = S_IRUGO | S_IWUSR;
286 if (device_create_file(&host->class_dev, &host->clkgate_delay_attr))
287 pr_err("%s: Failed to create clkgate_delay sysfs entry\n",
288 mmc_hostname(host));
289}
04566831
LW
290#else
291
292static inline void mmc_host_clk_init(struct mmc_host *host)
293{
294}
295
296static inline void mmc_host_clk_exit(struct mmc_host *host)
297{
298}
299
597dd9d7
SRT
300static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
301{
302}
303
04566831 304#endif
6fa3eb70
S
305static void mmc_card_init_wait(struct mmc_host *mmc)
306{
307 //if(mmc->card)
308 // return;
309 if(!wait_for_completion_timeout(&mmc->card_init_done, CARD_INIT_TIMEOUT))
310 {
311 kasprintf(GFP_KERNEL, "[%s]:card initiation is timeout\n", __func__);
312 }
313 return;
314}
315
316static void mmc_card_init_complete(struct mmc_host* mmc)
317{
318 complete(&mmc->card_init_done);
319 return;
320}
04566831 321
6c56e7a0
GL
322/**
323 * mmc_of_parse() - parse host's device-tree node
324 * @host: host whose node should be parsed.
325 *
326 * To keep the rest of the MMC subsystem unaware of whether DT has been
327 * used to to instantiate and configure this host instance or not, we
328 * parse the properties and set respective generic mmc-host flags and
329 * parameters.
330 */
331void mmc_of_parse(struct mmc_host *host)
332{
333 struct device_node *np;
334 u32 bus_width;
335 bool explicit_inv_wp, gpio_inv_wp = false;
336 enum of_gpio_flags flags;
337 int len, ret, gpio;
338
339 if (!host->parent || !host->parent->of_node)
340 return;
341
342 np = host->parent->of_node;
343
344 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
345 if (of_property_read_u32(np, "bus-width", &bus_width) < 0) {
346 dev_dbg(host->parent,
347 "\"bus-width\" property is missing, assuming 1 bit.\n");
348 bus_width = 1;
349 }
350
351 switch (bus_width) {
352 case 8:
353 host->caps |= MMC_CAP_8_BIT_DATA;
354 /* Hosts capable of 8-bit transfers can also do 4 bits */
355 case 4:
356 host->caps |= MMC_CAP_4_BIT_DATA;
357 break;
358 case 1:
359 break;
360 default:
361 dev_err(host->parent,
362 "Invalid \"bus-width\" value %ud!\n", bus_width);
363 }
364
365 /* f_max is obtained from the optional "max-frequency" property */
366 of_property_read_u32(np, "max-frequency", &host->f_max);
367
368 /*
369 * Configure CD and WP pins. They are both by default active low to
370 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
371 * mmc-gpio helpers are used to attach, configure and use them. If
372 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
373 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
374 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
375 * is set. If the "non-removable" property is found, the
376 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
377 * configuration is performed.
378 */
379
380 /* Parse Card Detection */
381 if (of_find_property(np, "non-removable", &len)) {
382 host->caps |= MMC_CAP_NONREMOVABLE;
383 } else {
384 bool explicit_inv_cd, gpio_inv_cd = false;
385
386 explicit_inv_cd = of_property_read_bool(np, "cd-inverted");
387
388 if (of_find_property(np, "broken-cd", &len))
389 host->caps |= MMC_CAP_NEEDS_POLL;
390
391 gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
392 if (gpio_is_valid(gpio)) {
393 if (!(flags & OF_GPIO_ACTIVE_LOW))
394 gpio_inv_cd = true;
395
396 ret = mmc_gpio_request_cd(host, gpio);
397 if (ret < 0)
398 dev_err(host->parent,
399 "Failed to request CD GPIO #%d: %d!\n",
400 gpio, ret);
401 else
402 dev_info(host->parent, "Got CD GPIO #%d.\n",
403 gpio);
404 }
405
406 if (explicit_inv_cd ^ gpio_inv_cd)
407 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
408 }
409
410 /* Parse Write Protection */
411 explicit_inv_wp = of_property_read_bool(np, "wp-inverted");
412
413 gpio = of_get_named_gpio_flags(np, "wp-gpios", 0, &flags);
414 if (gpio_is_valid(gpio)) {
415 if (!(flags & OF_GPIO_ACTIVE_LOW))
416 gpio_inv_wp = true;
417
418 ret = mmc_gpio_request_ro(host, gpio);
419 if (ret < 0)
420 dev_err(host->parent,
421 "Failed to request WP GPIO: %d!\n", ret);
422 }
423 if (explicit_inv_wp ^ gpio_inv_wp)
424 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
2fdb6e2d
GL
425
426 if (of_find_property(np, "cap-sd-highspeed", &len))
427 host->caps |= MMC_CAP_SD_HIGHSPEED;
428 if (of_find_property(np, "cap-mmc-highspeed", &len))
429 host->caps |= MMC_CAP_MMC_HIGHSPEED;
430 if (of_find_property(np, "cap-power-off-card", &len))
431 host->caps |= MMC_CAP_POWER_OFF_CARD;
432 if (of_find_property(np, "cap-sdio-irq", &len))
433 host->caps |= MMC_CAP_SDIO_IRQ;
434 if (of_find_property(np, "keep-power-in-suspend", &len))
435 host->pm_caps |= MMC_PM_KEEP_POWER;
436 if (of_find_property(np, "enable-sdio-wakeup", &len))
437 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
6c56e7a0
GL
438}
439
440EXPORT_SYMBOL(mmc_of_parse);
441
b93931a6
PO
442/**
443 * mmc_alloc_host - initialise the per-host structure.
444 * @extra: sizeof private data structure
445 * @dev: pointer to host device model structure
446 *
447 * Initialise the per-host structure.
448 */
449struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
450{
ff3112f5 451 int err;
b93931a6
PO
452 struct mmc_host *host;
453
be760a9d 454 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
b93931a6
PO
455 if (!host)
456 return NULL;
457
d9adcc12
GL
458 /* scanning will be enabled when we're ready */
459 host->rescan_disable = 1;
803d9e04 460 idr_preload(GFP_KERNEL);
ff3112f5 461 spin_lock(&mmc_host_lock);
803d9e04
TH
462 err = idr_alloc(&mmc_host_idr, host, 0, 0, GFP_NOWAIT);
463 if (err >= 0)
464 host->index = err;
ff3112f5 465 spin_unlock(&mmc_host_lock);
803d9e04
TH
466 idr_preload_end();
467 if (err < 0)
ff3112f5
PO
468 goto free;
469
d1b26863 470 dev_set_name(&host->class_dev, "mmc%d", host->index);
ff3112f5 471
b93931a6
PO
472 host->parent = dev;
473 host->class_dev.parent = dev;
474 host->class_dev.class = &mmc_host_class;
475 device_initialize(&host->class_dev);
476
04566831 477 mmc_host_clk_init(host);
6fa3eb70
S
478 init_completion(&host->card_init_done);
479 host->card_init_wait = mmc_card_init_wait;
480 host->card_init_complete = mmc_card_init_complete;
04566831 481
a7d1a1eb 482 mutex_init(&host->slot.lock);
27410ee7
GL
483 host->slot.cd_irq = -EINVAL;
484
b93931a6
PO
485 spin_lock_init(&host->lock);
486 init_waitqueue_head(&host->wq);
6fa3eb70
S
487 wake_lock_init(&host->detect_wake_lock, WAKE_LOCK_SUSPEND,
488 kasprintf(GFP_KERNEL, "%s_detect", mmc_hostname(host)));
b93931a6 489 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
6fa3eb70
S
490#ifdef MMC_ENABLED_EMPTY_QUEUE_FLUSH
491 host->flush_info.wq = create_singlethread_workqueue("flush_wq");
492 INIT_DELAYED_WORK(&host->flush_info.idle_time_dw, mmc_start_idle_time_flush);
493 host->flush_info.time_to_start_flush_ms = MMC_IDLE_FLUSH_TIME_MS;
494#endif
81ca03a0 495#ifdef CONFIG_PM
4c2ef25f 496 host->pm_notify.notifier_call = mmc_pm_notify;
81ca03a0 497#endif
b93931a6
PO
498
499 /*
500 * By default, hosts do not support SGIO or large requests.
501 * They have to set these according to their abilities.
502 */
a36274e0 503 host->max_segs = 1;
b93931a6
PO
504 host->max_seg_size = PAGE_CACHE_SIZE;
505
506 host->max_req_size = PAGE_CACHE_SIZE;
507 host->max_blk_size = 512;
508 host->max_blk_count = PAGE_CACHE_SIZE / 512;
509
510 return host;
ff3112f5
PO
511
512free:
513 kfree(host);
514 return NULL;
b93931a6
PO
515}
516
517EXPORT_SYMBOL(mmc_alloc_host);
518
519/**
520 * mmc_add_host - initialise host hardware
521 * @host: mmc host
67a61c48
PO
522 *
523 * Register the host with the driver model. The host must be
524 * prepared to start servicing requests before this function
525 * completes.
b93931a6
PO
526 */
527int mmc_add_host(struct mmc_host *host)
528{
529 int err;
530
17b759af
NP
531 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
532 !host->ops->enable_sdio_irq);
533
b93931a6
PO
534 err = device_add(&host->class_dev);
535 if (err)
536 return err;
537
f317dfeb
WS
538 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
539
6edd8ee6
HS
540#ifdef CONFIG_DEBUG_FS
541 mmc_add_host_debugfs(host);
542#endif
597dd9d7 543 mmc_host_clk_sysfs_init(host);
6edd8ee6 544
b93931a6 545 mmc_start_host(host);
6fa3eb70
S
546 if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
547 register_pm_notifier(&host->pm_notify);
b93931a6
PO
548
549 return 0;
550}
551
552EXPORT_SYMBOL(mmc_add_host);
553
554/**
555 * mmc_remove_host - remove host hardware
556 * @host: mmc host
557 *
558 * Unregister and remove all cards associated with this host,
67a61c48
PO
559 * and power down the MMC bus. No new requests will be issued
560 * after this function has returned.
b93931a6
PO
561 */
562void mmc_remove_host(struct mmc_host *host)
563{
6fa3eb70
S
564 if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
565 unregister_pm_notifier(&host->pm_notify);
566
b93931a6
PO
567 mmc_stop_host(host);
568
6edd8ee6
HS
569#ifdef CONFIG_DEBUG_FS
570 mmc_remove_host_debugfs(host);
571#endif
572
b93931a6
PO
573 device_del(&host->class_dev);
574
77f1fd6e 575 led_trigger_unregister_simple(host->led);
04566831
LW
576
577 mmc_host_clk_exit(host);
b93931a6
PO
578}
579
580EXPORT_SYMBOL(mmc_remove_host);
581
582/**
583 * mmc_free_host - free the host structure
584 * @host: mmc host
585 *
586 * Free the host once all references to it have been dropped.
587 */
588void mmc_free_host(struct mmc_host *host)
589{
ff3112f5
PO
590 spin_lock(&mmc_host_lock);
591 idr_remove(&mmc_host_idr, host->index);
592 spin_unlock(&mmc_host_lock);
6fa3eb70 593 wake_lock_destroy(&host->detect_wake_lock);
ff3112f5 594
b93931a6
PO
595 put_device(&host->class_dev);
596}
597
598EXPORT_SYMBOL(mmc_free_host);