GNU Linux-libre 5.4.200-gnu1
[releases.git] / drivers / mmc / core / host.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/mmc/core/host.c
4  *
5  *  Copyright (C) 2003 Russell King, All Rights Reserved.
6  *  Copyright (C) 2007-2008 Pierre Ossman
7  *  Copyright (C) 2010 Linus Walleij
8  *
9  *  MMC host class device management
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/export.h>
19 #include <linux/leds.h>
20 #include <linux/slab.h>
21
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/card.h>
24 #include <linux/mmc/slot-gpio.h>
25
26 #include "core.h"
27 #include "host.h"
28 #include "slot-gpio.h"
29 #include "pwrseq.h"
30 #include "sdio_ops.h"
31
32 #define cls_dev_to_mmc_host(d)  container_of(d, struct mmc_host, class_dev)
33
34 static DEFINE_IDA(mmc_host_ida);
35
36 #ifdef CONFIG_PM_SLEEP
37 static int mmc_host_class_prepare(struct device *dev)
38 {
39         struct mmc_host *host = cls_dev_to_mmc_host(dev);
40
41         /*
42          * It's safe to access the bus_ops pointer, as both userspace and the
43          * workqueue for detecting cards are frozen at this point.
44          */
45         if (!host->bus_ops)
46                 return 0;
47
48         /* Validate conditions for system suspend. */
49         if (host->bus_ops->pre_suspend)
50                 return host->bus_ops->pre_suspend(host);
51
52         return 0;
53 }
54
55 static void mmc_host_class_complete(struct device *dev)
56 {
57         struct mmc_host *host = cls_dev_to_mmc_host(dev);
58
59         _mmc_detect_change(host, 0, false);
60 }
61
62 static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
63         .prepare = mmc_host_class_prepare,
64         .complete = mmc_host_class_complete,
65 };
66
67 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
68 #else
69 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
70 #endif
71
72 static void mmc_host_classdev_release(struct device *dev)
73 {
74         struct mmc_host *host = cls_dev_to_mmc_host(dev);
75         ida_simple_remove(&mmc_host_ida, host->index);
76         kfree(host);
77 }
78
79 static int mmc_host_classdev_shutdown(struct device *dev)
80 {
81         struct mmc_host *host = cls_dev_to_mmc_host(dev);
82
83         __mmc_stop_host(host);
84         return 0;
85 }
86
87 static struct class mmc_host_class = {
88         .name           = "mmc_host",
89         .dev_release    = mmc_host_classdev_release,
90         .shutdown_pre   = mmc_host_classdev_shutdown,
91         .pm             = MMC_HOST_CLASS_DEV_PM_OPS,
92 };
93
94 int mmc_register_host_class(void)
95 {
96         return class_register(&mmc_host_class);
97 }
98
99 void mmc_unregister_host_class(void)
100 {
101         class_unregister(&mmc_host_class);
102 }
103
104 void mmc_retune_enable(struct mmc_host *host)
105 {
106         host->can_retune = 1;
107         if (host->retune_period)
108                 mod_timer(&host->retune_timer,
109                           jiffies + host->retune_period * HZ);
110 }
111
112 /*
113  * Pause re-tuning for a small set of operations.  The pause begins after the
114  * next command and after first doing re-tuning.
115  */
116 void mmc_retune_pause(struct mmc_host *host)
117 {
118         if (!host->retune_paused) {
119                 host->retune_paused = 1;
120                 mmc_retune_needed(host);
121                 mmc_retune_hold(host);
122         }
123 }
124 EXPORT_SYMBOL(mmc_retune_pause);
125
126 void mmc_retune_unpause(struct mmc_host *host)
127 {
128         if (host->retune_paused) {
129                 host->retune_paused = 0;
130                 mmc_retune_release(host);
131         }
132 }
133 EXPORT_SYMBOL(mmc_retune_unpause);
134
135 void mmc_retune_disable(struct mmc_host *host)
136 {
137         mmc_retune_unpause(host);
138         host->can_retune = 0;
139         del_timer_sync(&host->retune_timer);
140         host->retune_now = 0;
141         host->need_retune = 0;
142 }
143
144 void mmc_retune_timer_stop(struct mmc_host *host)
145 {
146         del_timer_sync(&host->retune_timer);
147 }
148 EXPORT_SYMBOL(mmc_retune_timer_stop);
149
150 void mmc_retune_hold(struct mmc_host *host)
151 {
152         if (!host->hold_retune)
153                 host->retune_now = 1;
154         host->hold_retune += 1;
155 }
156
157 void mmc_retune_release(struct mmc_host *host)
158 {
159         if (host->hold_retune)
160                 host->hold_retune -= 1;
161         else
162                 WARN_ON(1);
163 }
164 EXPORT_SYMBOL(mmc_retune_release);
165
166 int mmc_retune(struct mmc_host *host)
167 {
168         bool return_to_hs400 = false;
169         int err;
170
171         if (host->retune_now)
172                 host->retune_now = 0;
173         else
174                 return 0;
175
176         if (!host->need_retune || host->doing_retune || !host->card)
177                 return 0;
178
179         host->need_retune = 0;
180
181         host->doing_retune = 1;
182
183         if (host->ios.timing == MMC_TIMING_MMC_HS400) {
184                 err = mmc_hs400_to_hs200(host->card);
185                 if (err)
186                         goto out;
187
188                 return_to_hs400 = true;
189         }
190
191         err = mmc_execute_tuning(host->card);
192         if (err)
193                 goto out;
194
195         if (return_to_hs400)
196                 err = mmc_hs200_to_hs400(host->card);
197 out:
198         host->doing_retune = 0;
199
200         return err;
201 }
202
203 static void mmc_retune_timer(struct timer_list *t)
204 {
205         struct mmc_host *host = from_timer(host, t, retune_timer);
206
207         mmc_retune_needed(host);
208 }
209
210 /**
211  *      mmc_of_parse() - parse host's device-tree node
212  *      @host: host whose node should be parsed.
213  *
214  * To keep the rest of the MMC subsystem unaware of whether DT has been
215  * used to to instantiate and configure this host instance or not, we
216  * parse the properties and set respective generic mmc-host flags and
217  * parameters.
218  */
219 int mmc_of_parse(struct mmc_host *host)
220 {
221         struct device *dev = host->parent;
222         u32 bus_width, drv_type, cd_debounce_delay_ms;
223         int ret;
224         bool cd_cap_invert, cd_gpio_invert = false;
225
226         if (!dev || !dev_fwnode(dev))
227                 return 0;
228
229         /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
230         if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
231                 dev_dbg(host->parent,
232                         "\"bus-width\" property is missing, assuming 1 bit.\n");
233                 bus_width = 1;
234         }
235
236         switch (bus_width) {
237         case 8:
238                 host->caps |= MMC_CAP_8_BIT_DATA;
239                 /* fall through - Hosts capable of 8-bit can also do 4 bits */
240         case 4:
241                 host->caps |= MMC_CAP_4_BIT_DATA;
242                 break;
243         case 1:
244                 break;
245         default:
246                 dev_err(host->parent,
247                         "Invalid \"bus-width\" value %u!\n", bus_width);
248                 return -EINVAL;
249         }
250
251         /* f_max is obtained from the optional "max-frequency" property */
252         device_property_read_u32(dev, "max-frequency", &host->f_max);
253
254         /*
255          * Configure CD and WP pins. They are both by default active low to
256          * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
257          * mmc-gpio helpers are used to attach, configure and use them. If
258          * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
259          * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
260          * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
261          * is set. If the "non-removable" property is found, the
262          * MMC_CAP_NONREMOVABLE capability is set and no card-detection
263          * configuration is performed.
264          */
265
266         /* Parse Card Detection */
267         if (device_property_read_bool(dev, "non-removable")) {
268                 host->caps |= MMC_CAP_NONREMOVABLE;
269         } else {
270                 cd_cap_invert = device_property_read_bool(dev, "cd-inverted");
271
272                 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
273                                              &cd_debounce_delay_ms))
274                         cd_debounce_delay_ms = 200;
275
276                 if (device_property_read_bool(dev, "broken-cd"))
277                         host->caps |= MMC_CAP_NEEDS_POLL;
278
279                 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
280                                            cd_debounce_delay_ms * 1000,
281                                            &cd_gpio_invert);
282                 if (!ret)
283                         dev_info(host->parent, "Got CD GPIO\n");
284                 else if (ret != -ENOENT && ret != -ENOSYS)
285                         return ret;
286
287                 /*
288                  * There are two ways to flag that the CD line is inverted:
289                  * through the cd-inverted flag and by the GPIO line itself
290                  * being inverted from the GPIO subsystem. This is a leftover
291                  * from the times when the GPIO subsystem did not make it
292                  * possible to flag a line as inverted.
293                  *
294                  * If the capability on the host AND the GPIO line are
295                  * both inverted, the end result is that the CD line is
296                  * not inverted.
297                  */
298                 if (cd_cap_invert ^ cd_gpio_invert)
299                         host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
300         }
301
302         /* Parse Write Protection */
303
304         if (device_property_read_bool(dev, "wp-inverted"))
305                 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
306
307         ret = mmc_gpiod_request_ro(host, "wp", 0, 0, NULL);
308         if (!ret)
309                 dev_info(host->parent, "Got WP GPIO\n");
310         else if (ret != -ENOENT && ret != -ENOSYS)
311                 return ret;
312
313         if (device_property_read_bool(dev, "disable-wp"))
314                 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
315
316         if (device_property_read_bool(dev, "cap-sd-highspeed"))
317                 host->caps |= MMC_CAP_SD_HIGHSPEED;
318         if (device_property_read_bool(dev, "cap-mmc-highspeed"))
319                 host->caps |= MMC_CAP_MMC_HIGHSPEED;
320         if (device_property_read_bool(dev, "sd-uhs-sdr12"))
321                 host->caps |= MMC_CAP_UHS_SDR12;
322         if (device_property_read_bool(dev, "sd-uhs-sdr25"))
323                 host->caps |= MMC_CAP_UHS_SDR25;
324         if (device_property_read_bool(dev, "sd-uhs-sdr50"))
325                 host->caps |= MMC_CAP_UHS_SDR50;
326         if (device_property_read_bool(dev, "sd-uhs-sdr104"))
327                 host->caps |= MMC_CAP_UHS_SDR104;
328         if (device_property_read_bool(dev, "sd-uhs-ddr50"))
329                 host->caps |= MMC_CAP_UHS_DDR50;
330         if (device_property_read_bool(dev, "cap-power-off-card"))
331                 host->caps |= MMC_CAP_POWER_OFF_CARD;
332         if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
333                 host->caps |= MMC_CAP_HW_RESET;
334         if (device_property_read_bool(dev, "cap-sdio-irq"))
335                 host->caps |= MMC_CAP_SDIO_IRQ;
336         if (device_property_read_bool(dev, "full-pwr-cycle"))
337                 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
338         if (device_property_read_bool(dev, "keep-power-in-suspend"))
339                 host->pm_caps |= MMC_PM_KEEP_POWER;
340         if (device_property_read_bool(dev, "wakeup-source") ||
341             device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
342                 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
343         if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
344                 host->caps |= MMC_CAP_3_3V_DDR;
345         if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
346                 host->caps |= MMC_CAP_1_8V_DDR;
347         if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
348                 host->caps |= MMC_CAP_1_2V_DDR;
349         if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
350                 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
351         if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
352                 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
353         if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
354                 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
355         if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
356                 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
357         if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
358                 host->caps2 |= MMC_CAP2_HS400_ES;
359         if (device_property_read_bool(dev, "no-sdio"))
360                 host->caps2 |= MMC_CAP2_NO_SDIO;
361         if (device_property_read_bool(dev, "no-sd"))
362                 host->caps2 |= MMC_CAP2_NO_SD;
363         if (device_property_read_bool(dev, "no-mmc"))
364                 host->caps2 |= MMC_CAP2_NO_MMC;
365
366         /* Must be after "non-removable" check */
367         if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
368                 if (host->caps & MMC_CAP_NONREMOVABLE)
369                         host->fixed_drv_type = drv_type;
370                 else
371                         dev_err(host->parent,
372                                 "can't use fixed driver type, media is removable\n");
373         }
374
375         host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
376         if (host->dsr_req && (host->dsr & ~0xffff)) {
377                 dev_err(host->parent,
378                         "device tree specified broken value for DSR: 0x%x, ignoring\n",
379                         host->dsr);
380                 host->dsr_req = 0;
381         }
382
383         device_property_read_u32(dev, "post-power-on-delay-ms",
384                                  &host->ios.power_delay_ms);
385
386         return mmc_pwrseq_alloc(host);
387 }
388
389 EXPORT_SYMBOL(mmc_of_parse);
390
391 /**
392  * mmc_of_parse_voltage - return mask of supported voltages
393  * @np: The device node need to be parsed.
394  * @mask: mask of voltages available for MMC/SD/SDIO
395  *
396  * Parse the "voltage-ranges" DT property, returning zero if it is not
397  * found, negative errno if the voltage-range specification is invalid,
398  * or one if the voltage-range is specified and successfully parsed.
399  */
400 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
401 {
402         const u32 *voltage_ranges;
403         int num_ranges, i;
404
405         voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
406         if (!voltage_ranges) {
407                 pr_debug("%pOF: voltage-ranges unspecified\n", np);
408                 return 0;
409         }
410         num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
411         if (!num_ranges) {
412                 pr_err("%pOF: voltage-ranges empty\n", np);
413                 return -EINVAL;
414         }
415
416         for (i = 0; i < num_ranges; i++) {
417                 const int j = i * 2;
418                 u32 ocr_mask;
419
420                 ocr_mask = mmc_vddrange_to_ocrmask(
421                                 be32_to_cpu(voltage_ranges[j]),
422                                 be32_to_cpu(voltage_ranges[j + 1]));
423                 if (!ocr_mask) {
424                         pr_err("%pOF: voltage-range #%d is invalid\n",
425                                 np, i);
426                         return -EINVAL;
427                 }
428                 *mask |= ocr_mask;
429         }
430
431         return 1;
432 }
433 EXPORT_SYMBOL(mmc_of_parse_voltage);
434
435 /**
436  *      mmc_alloc_host - initialise the per-host structure.
437  *      @extra: sizeof private data structure
438  *      @dev: pointer to host device model structure
439  *
440  *      Initialise the per-host structure.
441  */
442 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
443 {
444         int err;
445         struct mmc_host *host;
446
447         host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
448         if (!host)
449                 return NULL;
450
451         /* scanning will be enabled when we're ready */
452         host->rescan_disable = 1;
453
454         err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
455         if (err < 0) {
456                 kfree(host);
457                 return NULL;
458         }
459
460         host->index = err;
461
462         dev_set_name(&host->class_dev, "mmc%d", host->index);
463
464         host->parent = dev;
465         host->class_dev.parent = dev;
466         host->class_dev.class = &mmc_host_class;
467         device_initialize(&host->class_dev);
468         device_enable_async_suspend(&host->class_dev);
469
470         if (mmc_gpio_alloc(host)) {
471                 put_device(&host->class_dev);
472                 return NULL;
473         }
474
475         spin_lock_init(&host->lock);
476         init_waitqueue_head(&host->wq);
477         INIT_DELAYED_WORK(&host->detect, mmc_rescan);
478         INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
479         timer_setup(&host->retune_timer, mmc_retune_timer, 0);
480
481         /*
482          * By default, hosts do not support SGIO or large requests.
483          * They have to set these according to their abilities.
484          */
485         host->max_segs = 1;
486         host->max_seg_size = PAGE_SIZE;
487
488         host->max_req_size = PAGE_SIZE;
489         host->max_blk_size = 512;
490         host->max_blk_count = PAGE_SIZE / 512;
491
492         host->fixed_drv_type = -EINVAL;
493         host->ios.power_delay_ms = 10;
494
495         return host;
496 }
497
498 EXPORT_SYMBOL(mmc_alloc_host);
499
500 static int mmc_validate_host_caps(struct mmc_host *host)
501 {
502         if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
503                 dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
504                 return -EINVAL;
505         }
506
507         return 0;
508 }
509
510 /**
511  *      mmc_add_host - initialise host hardware
512  *      @host: mmc host
513  *
514  *      Register the host with the driver model. The host must be
515  *      prepared to start servicing requests before this function
516  *      completes.
517  */
518 int mmc_add_host(struct mmc_host *host)
519 {
520         int err;
521
522         err = mmc_validate_host_caps(host);
523         if (err)
524                 return err;
525
526         err = device_add(&host->class_dev);
527         if (err)
528                 return err;
529
530         led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
531
532 #ifdef CONFIG_DEBUG_FS
533         mmc_add_host_debugfs(host);
534 #endif
535
536         mmc_start_host(host);
537         return 0;
538 }
539
540 EXPORT_SYMBOL(mmc_add_host);
541
542 /**
543  *      mmc_remove_host - remove host hardware
544  *      @host: mmc host
545  *
546  *      Unregister and remove all cards associated with this host,
547  *      and power down the MMC bus. No new requests will be issued
548  *      after this function has returned.
549  */
550 void mmc_remove_host(struct mmc_host *host)
551 {
552         mmc_stop_host(host);
553
554 #ifdef CONFIG_DEBUG_FS
555         mmc_remove_host_debugfs(host);
556 #endif
557
558         device_del(&host->class_dev);
559
560         led_trigger_unregister_simple(host->led);
561 }
562
563 EXPORT_SYMBOL(mmc_remove_host);
564
565 /**
566  *      mmc_free_host - free the host structure
567  *      @host: mmc host
568  *
569  *      Free the host once all references to it have been dropped.
570  */
571 void mmc_free_host(struct mmc_host *host)
572 {
573         mmc_pwrseq_free(host);
574         put_device(&host->class_dev);
575 }
576
577 EXPORT_SYMBOL(mmc_free_host);