1
2
3
4
5
6
7
8
9
10
11
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/idr.h>
15#include <linux/of.h>
16#include <linux/of_gpio.h>
17#include <linux/pagemap.h>
18#include <linux/pm_wakeup.h>
19#include <linux/export.h>
20#include <linux/leds.h>
21#include <linux/slab.h>
22
23#include <linux/mmc/host.h>
24#include <linux/mmc/card.h>
25#include <linux/mmc/slot-gpio.h>
26
27#include "core.h"
28#include "crypto.h"
29#include "host.h"
30#include "slot-gpio.h"
31#include "pwrseq.h"
32#include "sdio_ops.h"
33
34#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
35
36static DEFINE_IDA(mmc_host_ida);
37
38#ifdef CONFIG_PM_SLEEP
39static int mmc_host_class_prepare(struct device *dev)
40{
41 struct mmc_host *host = cls_dev_to_mmc_host(dev);
42
43
44
45
46
47 if (!host->bus_ops)
48 return 0;
49
50
51 if (host->bus_ops->pre_suspend)
52 return host->bus_ops->pre_suspend(host);
53
54 return 0;
55}
56
57static void mmc_host_class_complete(struct device *dev)
58{
59 struct mmc_host *host = cls_dev_to_mmc_host(dev);
60
61 _mmc_detect_change(host, 0, false);
62}
63
64static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
65 .prepare = mmc_host_class_prepare,
66 .complete = mmc_host_class_complete,
67};
68
69#define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
70#else
71#define MMC_HOST_CLASS_DEV_PM_OPS NULL
72#endif
73
74static void mmc_host_classdev_release(struct device *dev)
75{
76 struct mmc_host *host = cls_dev_to_mmc_host(dev);
77 wakeup_source_unregister(host->ws);
78 if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
79 ida_simple_remove(&mmc_host_ida, host->index);
80 kfree(host);
81}
82
83static struct class mmc_host_class = {
84 .name = "mmc_host",
85 .dev_release = mmc_host_classdev_release,
86 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
87};
88
89int mmc_register_host_class(void)
90{
91 return class_register(&mmc_host_class);
92}
93
94void mmc_unregister_host_class(void)
95{
96 class_unregister(&mmc_host_class);
97}
98
99
100
101
102
103void mmc_retune_enable(struct mmc_host *host)
104{
105 host->can_retune = 1;
106 if (host->retune_period)
107 mod_timer(&host->retune_timer,
108 jiffies + host->retune_period * HZ);
109}
110
111
112
113
114
115void mmc_retune_pause(struct mmc_host *host)
116{
117 if (!host->retune_paused) {
118 host->retune_paused = 1;
119 mmc_retune_needed(host);
120 mmc_retune_hold(host);
121 }
122}
123EXPORT_SYMBOL(mmc_retune_pause);
124
125void mmc_retune_unpause(struct mmc_host *host)
126{
127 if (host->retune_paused) {
128 host->retune_paused = 0;
129 mmc_retune_release(host);
130 }
131}
132EXPORT_SYMBOL(mmc_retune_unpause);
133
134
135
136
137
138
139
140void mmc_retune_disable(struct mmc_host *host)
141{
142 mmc_retune_unpause(host);
143 host->can_retune = 0;
144 del_timer_sync(&host->retune_timer);
145 mmc_retune_clear(host);
146}
147
148void mmc_retune_timer_stop(struct mmc_host *host)
149{
150 del_timer_sync(&host->retune_timer);
151}
152EXPORT_SYMBOL(mmc_retune_timer_stop);
153
154void mmc_retune_hold(struct mmc_host *host)
155{
156 if (!host->hold_retune)
157 host->retune_now = 1;
158 host->hold_retune += 1;
159}
160
161void mmc_retune_release(struct mmc_host *host)
162{
163 if (host->hold_retune)
164 host->hold_retune -= 1;
165 else
166 WARN_ON(1);
167}
168EXPORT_SYMBOL(mmc_retune_release);
169
170int mmc_retune(struct mmc_host *host)
171{
172 bool return_to_hs400 = false;
173 int err;
174
175 if (host->retune_now)
176 host->retune_now = 0;
177 else
178 return 0;
179
180 if (!host->need_retune || host->doing_retune || !host->card)
181 return 0;
182
183 host->need_retune = 0;
184
185 host->doing_retune = 1;
186
187 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
188 err = mmc_hs400_to_hs200(host->card);
189 if (err)
190 goto out;
191
192 return_to_hs400 = true;
193 }
194
195 err = mmc_execute_tuning(host->card);
196 if (err)
197 goto out;
198
199 if (return_to_hs400)
200 err = mmc_hs200_to_hs400(host->card);
201out:
202 host->doing_retune = 0;
203
204 return err;
205}
206
207static void mmc_retune_timer(struct timer_list *t)
208{
209 struct mmc_host *host = from_timer(host, t, retune_timer);
210
211 mmc_retune_needed(host);
212}
213
214static void mmc_of_parse_timing_phase(struct device *dev, const char *prop,
215 struct mmc_clk_phase *phase)
216{
217 int degrees[2] = {0};
218 int rc;
219
220 rc = device_property_read_u32_array(dev, prop, degrees, 2);
221 phase->valid = !rc;
222 if (phase->valid) {
223 phase->in_deg = degrees[0];
224 phase->out_deg = degrees[1];
225 }
226}
227
228void
229mmc_of_parse_clk_phase(struct mmc_host *host, struct mmc_clk_phase_map *map)
230{
231 struct device *dev = host->parent;
232
233 mmc_of_parse_timing_phase(dev, "clk-phase-legacy",
234 &map->phase[MMC_TIMING_LEGACY]);
235 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs",
236 &map->phase[MMC_TIMING_MMC_HS]);
237 mmc_of_parse_timing_phase(dev, "clk-phase-sd-hs",
238 &map->phase[MMC_TIMING_SD_HS]);
239 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr12",
240 &map->phase[MMC_TIMING_UHS_SDR12]);
241 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr25",
242 &map->phase[MMC_TIMING_UHS_SDR25]);
243 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr50",
244 &map->phase[MMC_TIMING_UHS_SDR50]);
245 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr104",
246 &map->phase[MMC_TIMING_UHS_SDR104]);
247 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-ddr50",
248 &map->phase[MMC_TIMING_UHS_DDR50]);
249 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-ddr52",
250 &map->phase[MMC_TIMING_MMC_DDR52]);
251 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs200",
252 &map->phase[MMC_TIMING_MMC_HS200]);
253 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs400",
254 &map->phase[MMC_TIMING_MMC_HS400]);
255}
256EXPORT_SYMBOL(mmc_of_parse_clk_phase);
257
258
259
260
261
262
263
264
265
266
267int mmc_of_parse(struct mmc_host *host)
268{
269 struct device *dev = host->parent;
270 u32 bus_width, drv_type, cd_debounce_delay_ms;
271 int ret;
272
273 if (!dev || !dev_fwnode(dev))
274 return 0;
275
276
277 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
278 dev_dbg(host->parent,
279 "\"bus-width\" property is missing, assuming 1 bit.\n");
280 bus_width = 1;
281 }
282
283 switch (bus_width) {
284 case 8:
285 host->caps |= MMC_CAP_8_BIT_DATA;
286 fallthrough;
287 case 4:
288 host->caps |= MMC_CAP_4_BIT_DATA;
289 break;
290 case 1:
291 break;
292 default:
293 dev_err(host->parent,
294 "Invalid \"bus-width\" value %u!\n", bus_width);
295 return -EINVAL;
296 }
297
298
299 device_property_read_u32(dev, "max-frequency", &host->f_max);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 if (device_property_read_bool(dev, "non-removable")) {
316 host->caps |= MMC_CAP_NONREMOVABLE;
317 } else {
318 if (device_property_read_bool(dev, "cd-inverted"))
319 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
320
321 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
322 &cd_debounce_delay_ms))
323 cd_debounce_delay_ms = 200;
324
325 if (device_property_read_bool(dev, "broken-cd"))
326 host->caps |= MMC_CAP_NEEDS_POLL;
327
328 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
329 cd_debounce_delay_ms * 1000);
330 if (!ret)
331 dev_info(host->parent, "Got CD GPIO\n");
332 else if (ret != -ENOENT && ret != -ENOSYS)
333 return ret;
334 }
335
336
337
338 if (device_property_read_bool(dev, "wp-inverted"))
339 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
340
341 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
342 if (!ret)
343 dev_info(host->parent, "Got WP GPIO\n");
344 else if (ret != -ENOENT && ret != -ENOSYS)
345 return ret;
346
347 if (device_property_read_bool(dev, "disable-wp"))
348 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
349
350 if (device_property_read_bool(dev, "cap-sd-highspeed"))
351 host->caps |= MMC_CAP_SD_HIGHSPEED;
352 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
353 host->caps |= MMC_CAP_MMC_HIGHSPEED;
354 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
355 host->caps |= MMC_CAP_UHS_SDR12;
356 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
357 host->caps |= MMC_CAP_UHS_SDR25;
358 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
359 host->caps |= MMC_CAP_UHS_SDR50;
360 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
361 host->caps |= MMC_CAP_UHS_SDR104;
362 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
363 host->caps |= MMC_CAP_UHS_DDR50;
364 if (device_property_read_bool(dev, "cap-power-off-card"))
365 host->caps |= MMC_CAP_POWER_OFF_CARD;
366 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
367 host->caps |= MMC_CAP_HW_RESET;
368 if (device_property_read_bool(dev, "cap-sdio-irq"))
369 host->caps |= MMC_CAP_SDIO_IRQ;
370 if (device_property_read_bool(dev, "full-pwr-cycle"))
371 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
372 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
373 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
374 if (device_property_read_bool(dev, "keep-power-in-suspend"))
375 host->pm_caps |= MMC_PM_KEEP_POWER;
376 if (device_property_read_bool(dev, "wakeup-source") ||
377 device_property_read_bool(dev, "enable-sdio-wakeup"))
378 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
379 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
380 host->caps |= MMC_CAP_3_3V_DDR;
381 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
382 host->caps |= MMC_CAP_1_8V_DDR;
383 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
384 host->caps |= MMC_CAP_1_2V_DDR;
385 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
386 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
387 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
388 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
389 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
390 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
391 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
392 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
393 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
394 host->caps2 |= MMC_CAP2_HS400_ES;
395 if (device_property_read_bool(dev, "no-sdio"))
396 host->caps2 |= MMC_CAP2_NO_SDIO;
397 if (device_property_read_bool(dev, "no-sd"))
398 host->caps2 |= MMC_CAP2_NO_SD;
399 if (device_property_read_bool(dev, "no-mmc"))
400 host->caps2 |= MMC_CAP2_NO_MMC;
401 if (device_property_read_bool(dev, "no-mmc-hs400"))
402 host->caps2 &= ~(MMC_CAP2_HS400_1_8V | MMC_CAP2_HS400_1_2V |
403 MMC_CAP2_HS400_ES);
404
405
406 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
407 if (host->caps & MMC_CAP_NONREMOVABLE)
408 host->fixed_drv_type = drv_type;
409 else
410 dev_err(host->parent,
411 "can't use fixed driver type, media is removable\n");
412 }
413
414 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
415 if (host->dsr_req && (host->dsr & ~0xffff)) {
416 dev_err(host->parent,
417 "device tree specified broken value for DSR: 0x%x, ignoring\n",
418 host->dsr);
419 host->dsr_req = 0;
420 }
421
422 device_property_read_u32(dev, "post-power-on-delay-ms",
423 &host->ios.power_delay_ms);
424
425 return mmc_pwrseq_alloc(host);
426}
427
428EXPORT_SYMBOL(mmc_of_parse);
429
430
431
432
433
434
435
436
437
438
439int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask)
440{
441 const char *prop = "voltage-ranges";
442 struct device *dev = host->parent;
443 u32 *voltage_ranges;
444 int num_ranges, i;
445 int ret;
446
447 if (!device_property_present(dev, prop)) {
448 dev_dbg(dev, "%s unspecified\n", prop);
449 return 0;
450 }
451
452 ret = device_property_count_u32(dev, prop);
453 if (ret < 0)
454 return ret;
455
456 num_ranges = ret / 2;
457 if (!num_ranges) {
458 dev_err(dev, "%s empty\n", prop);
459 return -EINVAL;
460 }
461
462 voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL);
463 if (!voltage_ranges)
464 return -ENOMEM;
465
466 ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges);
467 if (ret) {
468 kfree(voltage_ranges);
469 return ret;
470 }
471
472 for (i = 0; i < num_ranges; i++) {
473 const int j = i * 2;
474 u32 ocr_mask;
475
476 ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0],
477 voltage_ranges[j + 1]);
478 if (!ocr_mask) {
479 dev_err(dev, "range #%d in %s is invalid\n", i, prop);
480 kfree(voltage_ranges);
481 return -EINVAL;
482 }
483 *mask |= ocr_mask;
484 }
485
486 kfree(voltage_ranges);
487
488 return 1;
489}
490EXPORT_SYMBOL(mmc_of_parse_voltage);
491
492
493
494
495static int mmc_first_nonreserved_index(void)
496{
497 int max;
498
499 max = of_alias_get_highest_id("mmc");
500 if (max < 0)
501 return 0;
502
503 return max + 1;
504}
505
506
507
508
509
510
511
512
513struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
514{
515 int index;
516 struct mmc_host *host;
517 int alias_id, min_idx, max_idx;
518
519 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
520 if (!host)
521 return NULL;
522
523
524 host->rescan_disable = 1;
525
526 alias_id = of_alias_get_id(dev->of_node, "mmc");
527 if (alias_id >= 0) {
528 index = alias_id;
529 } else {
530 min_idx = mmc_first_nonreserved_index();
531 max_idx = 0;
532
533 index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
534 if (index < 0) {
535 kfree(host);
536 return NULL;
537 }
538 }
539
540 host->index = index;
541
542 dev_set_name(&host->class_dev, "mmc%d", host->index);
543 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
544
545 host->parent = dev;
546 host->class_dev.parent = dev;
547 host->class_dev.class = &mmc_host_class;
548 device_initialize(&host->class_dev);
549 device_enable_async_suspend(&host->class_dev);
550
551 if (mmc_gpio_alloc(host)) {
552 put_device(&host->class_dev);
553 return NULL;
554 }
555
556 spin_lock_init(&host->lock);
557 init_waitqueue_head(&host->wq);
558 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
559 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
560 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
561
562
563
564
565
566 host->max_segs = 1;
567 host->max_seg_size = PAGE_SIZE;
568
569 host->max_req_size = PAGE_SIZE;
570 host->max_blk_size = 512;
571 host->max_blk_count = PAGE_SIZE / 512;
572
573 host->fixed_drv_type = -EINVAL;
574 host->ios.power_delay_ms = 10;
575 host->ios.power_mode = MMC_POWER_UNDEFINED;
576
577 return host;
578}
579
580EXPORT_SYMBOL(mmc_alloc_host);
581
582
583
584
585
586
587
588
589
590int mmc_add_host(struct mmc_host *host)
591{
592 int err;
593
594 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
595 !host->ops->enable_sdio_irq);
596
597 err = device_add(&host->class_dev);
598 if (err)
599 return err;
600
601 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
602
603#ifdef CONFIG_DEBUG_FS
604 mmc_add_host_debugfs(host);
605#endif
606
607 mmc_start_host(host);
608 return 0;
609}
610
611EXPORT_SYMBOL(mmc_add_host);
612
613
614
615
616
617
618
619
620
621void mmc_remove_host(struct mmc_host *host)
622{
623 mmc_stop_host(host);
624
625#ifdef CONFIG_DEBUG_FS
626 mmc_remove_host_debugfs(host);
627#endif
628
629 device_del(&host->class_dev);
630
631 led_trigger_unregister_simple(host->led);
632}
633
634EXPORT_SYMBOL(mmc_remove_host);
635
636
637
638
639
640
641
642void mmc_free_host(struct mmc_host *host)
643{
644 mmc_pwrseq_free(host);
645 put_device(&host->class_dev);
646}
647
648EXPORT_SYMBOL(mmc_free_host);
649