forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/mmc/core/host.c
....@@ -1,13 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/drivers/mmc/core/host.c
34 *
45 * Copyright (C) 2003 Russell King, All Rights Reserved.
56 * Copyright (C) 2007-2008 Pierre Ossman
67 * Copyright (C) 2010 Linus Walleij
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.
118 *
129 * MMC host class device management
1310 */
....@@ -18,6 +15,7 @@
1815 #include <linux/of.h>
1916 #include <linux/of_gpio.h>
2017 #include <linux/pagemap.h>
18
+#include <linux/pm_wakeup.h>
2119 #include <linux/export.h>
2220 #include <linux/leds.h>
2321 #include <linux/slab.h>
....@@ -37,16 +35,64 @@
3735
3836 static DEFINE_IDA(mmc_host_ida);
3937
38
+#ifdef CONFIG_PM_SLEEP
39
+static int mmc_host_class_prepare(struct device *dev)
40
+{
41
+ struct mmc_host *host = cls_dev_to_mmc_host(dev);
42
+
43
+ /*
44
+ * It's safe to access the bus_ops pointer, as both userspace and the
45
+ * workqueue for detecting cards are frozen at this point.
46
+ */
47
+ if (!host->bus_ops)
48
+ return 0;
49
+
50
+ /* Validate conditions for system suspend. */
51
+ if (host->bus_ops->pre_suspend)
52
+ return host->bus_ops->pre_suspend(host);
53
+
54
+ return 0;
55
+}
56
+
57
+static 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
+
64
+static 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
+
4074 static void mmc_host_classdev_release(struct device *dev)
4175 {
4276 struct mmc_host *host = cls_dev_to_mmc_host(dev);
43
- ida_simple_remove(&mmc_host_ida, host->index);
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);
4480 kfree(host);
81
+}
82
+
83
+static int mmc_host_classdev_shutdown(struct device *dev)
84
+{
85
+ struct mmc_host *host = cls_dev_to_mmc_host(dev);
86
+
87
+ __mmc_stop_host(host);
88
+ return 0;
4589 }
4690
4791 static struct class mmc_host_class = {
4892 .name = "mmc_host",
4993 .dev_release = mmc_host_classdev_release,
94
+ .shutdown_pre = mmc_host_classdev_shutdown,
95
+ .pm = MMC_HOST_CLASS_DEV_PM_OPS,
5096 };
5197
5298 int mmc_register_host_class(void)
....@@ -66,7 +112,6 @@
66112 mod_timer(&host->retune_timer,
67113 jiffies + host->retune_period * HZ);
68114 }
69
-EXPORT_SYMBOL(mmc_retune_enable);
70115
71116 /*
72117 * Pause re-tuning for a small set of operations. The pause begins after the
....@@ -99,7 +144,6 @@
99144 host->retune_now = 0;
100145 host->need_retune = 0;
101146 }
102
-EXPORT_SYMBOL(mmc_retune_disable);
103147
104148 void mmc_retune_timer_stop(struct mmc_host *host)
105149 {
....@@ -181,8 +225,6 @@
181225 struct device *dev = host->parent;
182226 u32 bus_width, drv_type, cd_debounce_delay_ms;
183227 int ret;
184
- bool cd_cap_invert, cd_gpio_invert = false;
185
- bool ro_cap_invert, ro_gpio_invert = false;
186228
187229 if (!dev || !dev_fwnode(dev))
188230 return 0;
....@@ -197,7 +239,7 @@
197239 switch (bus_width) {
198240 case 8:
199241 host->caps |= MMC_CAP_8_BIT_DATA;
200
- /* Hosts capable of 8-bit transfers can also do 4 bits */
242
+ fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
201243 case 4:
202244 host->caps |= MMC_CAP_4_BIT_DATA;
203245 break;
....@@ -225,10 +267,12 @@
225267 */
226268
227269 /* Parse Card Detection */
270
+
228271 if (device_property_read_bool(dev, "non-removable")) {
229272 host->caps |= MMC_CAP_NONREMOVABLE;
230273 } else {
231
- cd_cap_invert = device_property_read_bool(dev, "cd-inverted");
274
+ if (device_property_read_bool(dev, "cd-inverted"))
275
+ host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
232276
233277 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
234278 &cd_debounce_delay_ms))
....@@ -237,33 +281,20 @@
237281 if (device_property_read_bool(dev, "broken-cd"))
238282 host->caps |= MMC_CAP_NEEDS_POLL;
239283
240
- ret = mmc_gpiod_request_cd(host, "cd", 0, true,
241
- cd_debounce_delay_ms * 1000,
242
- &cd_gpio_invert);
284
+ ret = mmc_gpiod_request_cd(host, "cd", 0, false,
285
+ cd_debounce_delay_ms * 1000);
243286 if (!ret)
244287 dev_info(host->parent, "Got CD GPIO\n");
245288 else if (ret != -ENOENT && ret != -ENOSYS)
246289 return ret;
247
-
248
- /*
249
- * There are two ways to flag that the CD line is inverted:
250
- * through the cd-inverted flag and by the GPIO line itself
251
- * being inverted from the GPIO subsystem. This is a leftover
252
- * from the times when the GPIO subsystem did not make it
253
- * possible to flag a line as inverted.
254
- *
255
- * If the capability on the host AND the GPIO line are
256
- * both inverted, the end result is that the CD line is
257
- * not inverted.
258
- */
259
- if (cd_cap_invert ^ cd_gpio_invert)
260
- host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
261290 }
262291
263292 /* Parse Write Protection */
264
- ro_cap_invert = device_property_read_bool(dev, "wp-inverted");
265293
266
- ret = mmc_gpiod_request_ro(host, "wp", 0, false, 0, &ro_gpio_invert);
294
+ if (device_property_read_bool(dev, "wp-inverted"))
295
+ host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
296
+
297
+ ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
267298 if (!ret)
268299 dev_info(host->parent, "Got WP GPIO\n");
269300 else if (ret != -ENOENT && ret != -ENOSYS)
....@@ -271,10 +302,6 @@
271302
272303 if (device_property_read_bool(dev, "disable-wp"))
273304 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
274
-
275
- /* See the comment on CD inversion above */
276
- if (ro_cap_invert ^ ro_gpio_invert)
277
- host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
278305
279306 if (device_property_read_bool(dev, "cap-sd-highspeed"))
280307 host->caps |= MMC_CAP_SD_HIGHSPEED;
....@@ -298,6 +325,8 @@
298325 host->caps |= MMC_CAP_SDIO_IRQ;
299326 if (device_property_read_bool(dev, "full-pwr-cycle"))
300327 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
328
+ if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
329
+ host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
301330 if (device_property_read_bool(dev, "keep-power-in-suspend"))
302331 host->pm_caps |= MMC_PM_KEEP_POWER;
303332 if (device_property_read_bool(dev, "wakeup-source") ||
....@@ -325,6 +354,8 @@
325354 host->caps2 |= MMC_CAP2_NO_SD;
326355 if (device_property_read_bool(dev, "no-mmc"))
327356 host->caps2 |= MMC_CAP2_NO_MMC;
357
+ if (device_property_read_bool(dev, "no-prescan-powerup"))
358
+ host->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
328359
329360 /* Must be after "non-removable" check */
330361 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
....@@ -334,20 +365,6 @@
334365 dev_err(host->parent,
335366 "can't use fixed driver type, media is removable\n");
336367 }
337
-
338
- if (device_property_read_bool(dev, "supports-sd"))
339
- host->restrict_caps |= RESTRICT_CARD_TYPE_SD;
340
- if (device_property_read_bool(dev, "supports-sdio"))
341
- host->restrict_caps |= RESTRICT_CARD_TYPE_SDIO;
342
- if (device_property_read_bool(dev, "supports-emmc"))
343
- host->restrict_caps |= RESTRICT_CARD_TYPE_EMMC;
344
-
345
-#ifdef CONFIG_SDIO_KEEPALIVE
346
- host->support_chip_alive =
347
- device_property_read_bool(dev, "supports-chip-alive");
348
- host->logic_remove_card =
349
- device_property_read_bool(dev, "logic-remove-card");
350
-#endif
351368
352369 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
353370 if (host->dsr_req && (host->dsr & ~0xffff)) {
....@@ -364,6 +381,50 @@
364381 }
365382
366383 EXPORT_SYMBOL(mmc_of_parse);
384
+
385
+/**
386
+ * mmc_of_parse_voltage - return mask of supported voltages
387
+ * @np: The device node need to be parsed.
388
+ * @mask: mask of voltages available for MMC/SD/SDIO
389
+ *
390
+ * Parse the "voltage-ranges" DT property, returning zero if it is not
391
+ * found, negative errno if the voltage-range specification is invalid,
392
+ * or one if the voltage-range is specified and successfully parsed.
393
+ */
394
+int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
395
+{
396
+ const u32 *voltage_ranges;
397
+ int num_ranges, i;
398
+
399
+ voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
400
+ if (!voltage_ranges) {
401
+ pr_debug("%pOF: voltage-ranges unspecified\n", np);
402
+ return 0;
403
+ }
404
+ num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
405
+ if (!num_ranges) {
406
+ pr_err("%pOF: voltage-ranges empty\n", np);
407
+ return -EINVAL;
408
+ }
409
+
410
+ for (i = 0; i < num_ranges; i++) {
411
+ const int j = i * 2;
412
+ u32 ocr_mask;
413
+
414
+ ocr_mask = mmc_vddrange_to_ocrmask(
415
+ be32_to_cpu(voltage_ranges[j]),
416
+ be32_to_cpu(voltage_ranges[j + 1]));
417
+ if (!ocr_mask) {
418
+ pr_err("%pOF: voltage-range #%d is invalid\n",
419
+ np, i);
420
+ return -EINVAL;
421
+ }
422
+ *mask |= ocr_mask;
423
+ }
424
+
425
+ return 1;
426
+}
427
+EXPORT_SYMBOL(mmc_of_parse_voltage);
367428
368429 /**
369430 * mmc_first_nonreserved_index() - get the first index that is not reserved
....@@ -388,7 +449,7 @@
388449 */
389450 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
390451 {
391
- int err;
452
+ int index;
392453 struct mmc_host *host;
393454 int alias_id, min_idx, max_idx;
394455
....@@ -401,22 +462,22 @@
401462
402463 alias_id = of_alias_get_id(dev->of_node, "mmc");
403464 if (alias_id >= 0) {
404
- min_idx = alias_id;
405
- max_idx = alias_id + 1;
465
+ index = alias_id;
406466 } else {
407467 min_idx = mmc_first_nonreserved_index();
408468 max_idx = 0;
469
+
470
+ index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
471
+ if (index < 0) {
472
+ kfree(host);
473
+ return NULL;
474
+ }
409475 }
410476
411
- err = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
412
- if (err < 0) {
413
- kfree(host);
414
- return NULL;
415
- }
416
-
417
- host->index = err;
477
+ host->index = index;
418478
419479 dev_set_name(&host->class_dev, "mmc%d", host->index);
480
+ host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
420481
421482 host->parent = dev;
422483 host->class_dev.parent = dev;
....@@ -448,14 +509,48 @@
448509
449510 host->fixed_drv_type = -EINVAL;
450511 host->ios.power_delay_ms = 10;
512
+ host->ios.power_mode = MMC_POWER_UNDEFINED;
451513
452514 return host;
453515 }
454516
455517 EXPORT_SYMBOL(mmc_alloc_host);
456518
457
-struct mmc_host *primary_sdio_host;
458
-EXPORT_SYMBOL(primary_sdio_host);
519
+static void devm_mmc_host_release(struct device *dev, void *res)
520
+{
521
+ mmc_free_host(*(struct mmc_host **)res);
522
+}
523
+
524
+struct mmc_host *devm_mmc_alloc_host(struct device *dev, int extra)
525
+{
526
+ struct mmc_host **dr, *host;
527
+
528
+ dr = devres_alloc(devm_mmc_host_release, sizeof(*dr), GFP_KERNEL);
529
+ if (!dr)
530
+ return ERR_PTR(-ENOMEM);
531
+
532
+ host = mmc_alloc_host(extra, dev);
533
+ if (IS_ERR(host)) {
534
+ devres_free(dr);
535
+ return host;
536
+ }
537
+
538
+ *dr = host;
539
+ devres_add(dev, dr);
540
+
541
+ return host;
542
+}
543
+EXPORT_SYMBOL(devm_mmc_alloc_host);
544
+
545
+static int mmc_validate_host_caps(struct mmc_host *host)
546
+{
547
+ if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
548
+ dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
549
+ return -EINVAL;
550
+ }
551
+
552
+ return 0;
553
+}
459554
460555 /**
461556 * mmc_add_host - initialise host hardware
....@@ -469,8 +564,9 @@
469564 {
470565 int err;
471566
472
- WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
473
- !host->ops->enable_sdio_irq);
567
+ err = mmc_validate_host_caps(host);
568
+ if (err)
569
+ return err;
474570
475571 err = device_add(&host->class_dev);
476572 if (err)
....@@ -483,12 +579,6 @@
483579 #endif
484580
485581 mmc_start_host(host);
486
- if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
487
- mmc_register_pm_notifier(host);
488
-
489
- if (host->restrict_caps & RESTRICT_CARD_TYPE_SDIO)
490
- primary_sdio_host = host;
491
-
492582 return 0;
493583 }
494584
....@@ -504,8 +594,6 @@
504594 */
505595 void mmc_remove_host(struct mmc_host *host)
506596 {
507
- if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
508
- mmc_unregister_pm_notifier(host);
509597 mmc_stop_host(host);
510598
511599 #ifdef CONFIG_DEBUG_FS
....@@ -527,51 +615,8 @@
527615 */
528616 void mmc_free_host(struct mmc_host *host)
529617 {
530
- mmc_crypto_free_host(host);
531618 mmc_pwrseq_free(host);
532619 put_device(&host->class_dev);
533620 }
534621
535622 EXPORT_SYMBOL(mmc_free_host);
536
-
537
-/**
538
- * mmc_host_rescan - triger software rescan flow
539
- * @host: mmc host
540
- *
541
- * rescan slot attach in the assigned host.
542
- * If @host is NULL, default rescan primary_sdio_host
543
- * saved by mmc_add_host().
544
- * OR, rescan host from argument.
545
- *
546
- */
547
-int mmc_host_rescan(struct mmc_host *host, int val, int is_cap_sdio_irq)
548
-{
549
- if (NULL != primary_sdio_host) {
550
- if (!host)
551
- host = primary_sdio_host;
552
- else
553
- pr_info("%s: mmc_host_rescan pass in host from argument!\n",
554
- mmc_hostname(host));
555
- } else {
556
- pr_err("sdio: host isn't initialization successfully.\n");
557
- return -ENOMEDIUM;
558
- }
559
-
560
- pr_info("%s:mmc host rescan start!\n", mmc_hostname(host));
561
-
562
- /* 0: oob 1:cap-sdio-irq */
563
- if (is_cap_sdio_irq == 1) {
564
- host->caps |= MMC_CAP_SDIO_IRQ;
565
- } else if (is_cap_sdio_irq == 0) {
566
- host->caps &= ~MMC_CAP_SDIO_IRQ;
567
- } else {
568
- dev_err(&host->class_dev, "sdio: host doesn't identify oob or sdio_irq mode!\n");
569
- return -ENOMEDIUM;
570
- }
571
-
572
- if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->set_sdio_status)
573
- host->ops->set_sdio_status(host, val);
574
-
575
- return 0;
576
-}
577
-EXPORT_SYMBOL(mmc_host_rescan);