| 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/pagemap.h> |
| 17 |
#include <linux/export.h> |
| 18 |
#include <linux/leds.h> |
| 19 |
#include <linux/slab.h> |
| 20 |
|
| 21 |
#include <linux/mmc/host.h> |
| 22 |
#include <linux/mmc/card.h> |
| 23 |
#include <linux/mmc/slot-gpio.h> |
| 24 |
|
| 25 |
#include "core.h" |
| 26 |
#include "crypto.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 |
wakeup_source_unregister(host->ws); |
| 76 |
if (of_alias_get_id(host->parent->of_node, "mmc") < 0) |
| 77 |
ida_free(&mmc_host_ida, host->index); |
| 78 |
kfree(host); |
| 79 |
} |
| 80 |
|
| 81 |
static int mmc_host_classdev_shutdown(struct device *dev) |
| 82 |
{ |
| 83 |
struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 84 |
|
| 85 |
__mmc_stop_host(host); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static const struct class mmc_host_class = { |
| 90 |
.name = "mmc_host", |
| 91 |
.dev_release = mmc_host_classdev_release, |
| 92 |
.shutdown_pre = mmc_host_classdev_shutdown, |
| 93 |
.pm = MMC_HOST_CLASS_DEV_PM_OPS, |
| 94 |
}; |
| 95 |
|
| 96 |
int mmc_register_host_class(void) |
| 97 |
{ |
| 98 |
return class_register(&mmc_host_class); |
| 99 |
} |
| 100 |
|
| 101 |
void mmc_unregister_host_class(void) |
| 102 |
{ |
| 103 |
class_unregister(&mmc_host_class); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* mmc_retune_enable() - enter a transfer mode that requires retuning |
| 108 |
* @host: host which should retune now |
| 109 |
*/ |
| 110 |
void mmc_retune_enable(struct mmc_host *host) |
| 111 |
{ |
| 112 |
host->can_retune = 1; |
| 113 |
if (host->retune_period) |
| 114 |
mod_timer(&host->retune_timer, |
| 115 |
jiffies + host->retune_period * HZ); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* Pause re-tuning for a small set of operations. The pause begins after the |
| 120 |
* next command. |
| 121 |
*/ |
| 122 |
void mmc_retune_pause(struct mmc_host *host) |
| 123 |
{ |
| 124 |
if (!host->retune_paused) { |
| 125 |
host->retune_paused = 1; |
| 126 |
mmc_retune_hold(host); |
| 127 |
} |
| 128 |
} |
| 129 |
EXPORT_SYMBOL(mmc_retune_pause); |
| 130 |
|
| 131 |
void mmc_retune_unpause(struct mmc_host *host) |
| 132 |
{ |
| 133 |
if (host->retune_paused) { |
| 134 |
host->retune_paused = 0; |
| 135 |
mmc_retune_release(host); |
| 136 |
} |
| 137 |
} |
| 138 |
EXPORT_SYMBOL(mmc_retune_unpause); |
| 139 |
|
| 140 |
/** |
| 141 |
* mmc_retune_disable() - exit a transfer mode that requires retuning |
| 142 |
* @host: host which should not retune anymore |
| 143 |
* |
| 144 |
* It is not meant for temporarily preventing retuning! |
| 145 |
*/ |
| 146 |
void mmc_retune_disable(struct mmc_host *host) |
| 147 |
{ |
| 148 |
mmc_retune_unpause(host); |
| 149 |
host->can_retune = 0; |
| 150 |
timer_delete_sync(&host->retune_timer); |
| 151 |
mmc_retune_clear(host); |
| 152 |
} |
| 153 |
|
| 154 |
void mmc_retune_timer_stop(struct mmc_host *host) |
| 155 |
{ |
| 156 |
timer_delete_sync(&host->retune_timer); |
| 157 |
} |
| 158 |
EXPORT_SYMBOL(mmc_retune_timer_stop); |
| 159 |
|
| 160 |
void mmc_retune_hold(struct mmc_host *host) |
| 161 |
{ |
| 162 |
if (!host->hold_retune) |
| 163 |
host->retune_now = 1; |
| 164 |
host->hold_retune += 1; |
| 165 |
} |
| 166 |
|
| 167 |
void mmc_retune_release(struct mmc_host *host) |
| 168 |
{ |
| 169 |
if (host->hold_retune) |
| 170 |
host->hold_retune -= 1; |
| 171 |
else |
| 172 |
WARN_ON(1); |
| 173 |
} |
| 174 |
EXPORT_SYMBOL(mmc_retune_release); |
| 175 |
|
| 176 |
int mmc_retune(struct mmc_host *host) |
| 177 |
{ |
| 178 |
bool return_to_hs400 = false; |
| 179 |
int err; |
| 180 |
|
| 181 |
if (host->retune_now) |
| 182 |
host->retune_now = 0; |
| 183 |
else |
| 184 |
return 0; |
| 185 |
|
| 186 |
if (!host->need_retune || host->doing_retune || !host->card) |
| 187 |
return 0; |
| 188 |
|
| 189 |
host->need_retune = 0; |
| 190 |
|
| 191 |
host->doing_retune = 1; |
| 192 |
|
| 193 |
if (host->ios.timing == MMC_TIMING_MMC_HS400) { |
| 194 |
err = mmc_hs400_to_hs200(host->card); |
| 195 |
if (err) |
| 196 |
goto out; |
| 197 |
|
| 198 |
return_to_hs400 = true; |
| 199 |
} |
| 200 |
|
| 201 |
err = mmc_execute_tuning(host->card); |
| 202 |
if (err) |
| 203 |
goto out; |
| 204 |
|
| 205 |
if (return_to_hs400) |
| 206 |
err = mmc_hs200_to_hs400(host->card); |
| 207 |
out: |
| 208 |
host->doing_retune = 0; |
| 209 |
|
| 210 |
return err; |
| 211 |
} |
| 212 |
|
| 213 |
static void mmc_retune_timer(struct timer_list *t) |
| 214 |
{ |
| 215 |
struct mmc_host *host = timer_container_of(host, t, retune_timer); |
| 216 |
|
| 217 |
mmc_retune_needed(host); |
| 218 |
} |
| 219 |
|
| 220 |
static void mmc_of_parse_timing_phase(struct device *dev, const char *prop, |
| 221 |
struct mmc_clk_phase *phase) |
| 222 |
{ |
| 223 |
int degrees[2] = {0}; |
| 224 |
int rc; |
| 225 |
|
| 226 |
rc = device_property_read_u32_array(dev, prop, degrees, 2); |
| 227 |
phase->valid = !rc; |
| 228 |
if (phase->valid) { |
| 229 |
phase->in_deg = degrees[0]; |
| 230 |
phase->out_deg = degrees[1]; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
mmc_of_parse_clk_phase(struct device *dev, struct mmc_clk_phase_map *map) |
| 236 |
{ |
| 237 |
mmc_of_parse_timing_phase(dev, "clk-phase-legacy", |
| 238 |
&map->phase[MMC_TIMING_LEGACY]); |
| 239 |
mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs", |
| 240 |
&map->phase[MMC_TIMING_MMC_HS]); |
| 241 |
mmc_of_parse_timing_phase(dev, "clk-phase-sd-hs", |
| 242 |
&map->phase[MMC_TIMING_SD_HS]); |
| 243 |
mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr12", |
| 244 |
&map->phase[MMC_TIMING_UHS_SDR12]); |
| 245 |
mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr25", |
| 246 |
&map->phase[MMC_TIMING_UHS_SDR25]); |
| 247 |
mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr50", |
| 248 |
&map->phase[MMC_TIMING_UHS_SDR50]); |
| 249 |
mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr104", |
| 250 |
&map->phase[MMC_TIMING_UHS_SDR104]); |
| 251 |
mmc_of_parse_timing_phase(dev, "clk-phase-uhs-ddr50", |
| 252 |
&map->phase[MMC_TIMING_UHS_DDR50]); |
| 253 |
mmc_of_parse_timing_phase(dev, "clk-phase-mmc-ddr52", |
| 254 |
&map->phase[MMC_TIMING_MMC_DDR52]); |
| 255 |
mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs200", |
| 256 |
&map->phase[MMC_TIMING_MMC_HS200]); |
| 257 |
mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs400", |
| 258 |
&map->phase[MMC_TIMING_MMC_HS400]); |
| 259 |
} |
| 260 |
EXPORT_SYMBOL(mmc_of_parse_clk_phase); |
| 261 |
|
| 262 |
/** |
| 263 |
* mmc_of_parse() - parse host's device properties |
| 264 |
* @host: host whose properties should be parsed. |
| 265 |
* |
| 266 |
* To keep the rest of the MMC subsystem unaware of whether DT has been |
| 267 |
* used to instantiate and configure this host instance or not, we |
| 268 |
* parse the properties and set respective generic mmc-host flags and |
| 269 |
* parameters. |
| 270 |
*/ |
| 271 |
int mmc_of_parse(struct mmc_host *host) |
| 272 |
{ |
| 273 |
struct device *dev = host->parent; |
| 274 |
u32 bus_width, drv_type, cd_debounce_delay_ms; |
| 275 |
int ret; |
| 276 |
|
| 277 |
if (!dev || !dev_fwnode(dev)) |
| 278 |
return 0; |
| 279 |
|
| 280 |
/* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */ |
| 281 |
if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) { |
| 282 |
dev_dbg(host->parent, |
| 283 |
"\"bus-width\" property is missing, assuming 1 bit.\n"); |
| 284 |
bus_width = 1; |
| 285 |
} |
| 286 |
|
| 287 |
switch (bus_width) { |
| 288 |
case 8: |
| 289 |
host->caps |= MMC_CAP_8_BIT_DATA; |
| 290 |
fallthrough; /* Hosts capable of 8-bit can also do 4 bits */ |
| 291 |
case 4: |
| 292 |
host->caps |= MMC_CAP_4_BIT_DATA; |
| 293 |
break; |
| 294 |
case 1: |
| 295 |
break; |
| 296 |
default: |
| 297 |
dev_err(host->parent, |
| 298 |
"Invalid \"bus-width\" value %u!\n", bus_width); |
| 299 |
return -EINVAL; |
| 300 |
} |
| 301 |
|
| 302 |
/* f_max is obtained from the optional "max-frequency" property */ |
| 303 |
device_property_read_u32(dev, "max-frequency", &host->f_max); |
| 304 |
|
| 305 |
device_property_read_u32(dev, "max-sd-hs-hz", &host->max_sd_hs_hz); |
| 306 |
|
| 307 |
/* |
| 308 |
* Configure CD and WP pins. They are both by default active low to |
| 309 |
* match the SDHCI spec. If GPIOs are provided for CD and / or WP, the |
| 310 |
* mmc-gpio helpers are used to attach, configure and use them. If |
| 311 |
* polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH |
| 312 |
* and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the |
| 313 |
* "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability |
| 314 |
* is set. If the "non-removable" property is found, the |
| 315 |
* MMC_CAP_NONREMOVABLE capability is set and no card-detection |
| 316 |
* configuration is performed. |
| 317 |
*/ |
| 318 |
|
| 319 |
/* Parse Card Detection */ |
| 320 |
|
| 321 |
if (device_property_read_bool(dev, "non-removable")) { |
| 322 |
host->caps |= MMC_CAP_NONREMOVABLE; |
| 323 |
} else { |
| 324 |
if (device_property_read_bool(dev, "cd-inverted")) |
| 325 |
host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH; |
| 326 |
|
| 327 |
if (device_property_read_u32(dev, "cd-debounce-delay-ms", |
| 328 |
&cd_debounce_delay_ms)) |
| 329 |
cd_debounce_delay_ms = 200; |
| 330 |
|
| 331 |
if (device_property_read_bool(dev, "broken-cd")) |
| 332 |
host->caps |= MMC_CAP_NEEDS_POLL; |
| 333 |
|
| 334 |
ret = mmc_gpiod_request_cd(host, "cd", 0, false, |
| 335 |
cd_debounce_delay_ms * 1000); |
| 336 |
if (!ret) |
| 337 |
dev_info(host->parent, "Got CD GPIO\n"); |
| 338 |
else if (ret != -ENOENT && ret != -ENOSYS) |
| 339 |
return ret; |
| 340 |
} |
| 341 |
|
| 342 |
/* Parse Write Protection */ |
| 343 |
|
| 344 |
if (device_property_read_bool(dev, "wp-inverted")) |
| 345 |
host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH; |
| 346 |
|
| 347 |
ret = mmc_gpiod_request_ro(host, "wp", 0, 0); |
| 348 |
if (!ret) |
| 349 |
dev_info(host->parent, "Got WP GPIO\n"); |
| 350 |
else if (ret != -ENOENT && ret != -ENOSYS) |
| 351 |
return ret; |
| 352 |
|
| 353 |
if (device_property_read_bool(dev, "disable-wp")) |
| 354 |
host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT; |
| 355 |
|
| 356 |
if (device_property_read_bool(dev, "cap-sd-highspeed")) |
| 357 |
host->caps |= MMC_CAP_SD_HIGHSPEED; |
| 358 |
if (device_property_read_bool(dev, "cap-mmc-highspeed")) |
| 359 |
host->caps |= MMC_CAP_MMC_HIGHSPEED; |
| 360 |
if (device_property_read_bool(dev, "sd-uhs-sdr12")) |
| 361 |
host->caps |= MMC_CAP_UHS_SDR12; |
| 362 |
if (device_property_read_bool(dev, "sd-uhs-sdr25")) |
| 363 |
host->caps |= MMC_CAP_UHS_SDR25; |
| 364 |
if (device_property_read_bool(dev, "sd-uhs-sdr50")) |
| 365 |
host->caps |= MMC_CAP_UHS_SDR50; |
| 366 |
if (device_property_read_bool(dev, "sd-uhs-sdr104")) |
| 367 |
host->caps |= MMC_CAP_UHS_SDR104; |
| 368 |
if (device_property_read_bool(dev, "sd-uhs-ddr50")) |
| 369 |
host->caps |= MMC_CAP_UHS_DDR50; |
| 370 |
if (device_property_read_bool(dev, "cap-power-off-card")) |
| 371 |
host->caps |= MMC_CAP_POWER_OFF_CARD; |
| 372 |
if (device_property_read_bool(dev, "cap-mmc-hw-reset")) |
| 373 |
host->caps |= MMC_CAP_HW_RESET; |
| 374 |
if (device_property_read_bool(dev, "cap-sdio-irq")) |
| 375 |
host->caps |= MMC_CAP_SDIO_IRQ; |
| 376 |
if (device_property_read_bool(dev, "full-pwr-cycle")) |
| 377 |
host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE; |
| 378 |
if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend")) |
| 379 |
host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND; |
| 380 |
if (device_property_read_bool(dev, "keep-power-in-suspend")) |
| 381 |
host->pm_caps |= MMC_PM_KEEP_POWER; |
| 382 |
if (device_property_read_bool(dev, "wakeup-source") || |
| 383 |
device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */ |
| 384 |
host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ; |
| 385 |
if (device_property_read_bool(dev, "mmc-ddr-3_3v")) |
| 386 |
host->caps |= MMC_CAP_3_3V_DDR; |
| 387 |
if (device_property_read_bool(dev, "mmc-ddr-1_8v")) |
| 388 |
host->caps |= MMC_CAP_1_8V_DDR; |
| 389 |
if (device_property_read_bool(dev, "mmc-ddr-1_2v")) |
| 390 |
host->caps |= MMC_CAP_1_2V_DDR; |
| 391 |
if (device_property_read_bool(dev, "mmc-hs200-1_8v")) |
| 392 |
host->caps2 |= MMC_CAP2_HS200_1_8V_SDR; |
| 393 |
if (device_property_read_bool(dev, "mmc-hs200-1_2v")) |
| 394 |
host->caps2 |= MMC_CAP2_HS200_1_2V_SDR; |
| 395 |
if (device_property_read_bool(dev, "mmc-hs400-1_8v")) |
| 396 |
host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR; |
| 397 |
if (device_property_read_bool(dev, "mmc-hs400-1_2v")) |
| 398 |
host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR; |
| 399 |
if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe")) |
| 400 |
host->caps2 |= MMC_CAP2_HS400_ES; |
| 401 |
if (device_property_read_bool(dev, "no-sdio")) |
| 402 |
host->caps2 |= MMC_CAP2_NO_SDIO; |
| 403 |
if (device_property_read_bool(dev, "no-sd")) |
| 404 |
host->caps2 |= MMC_CAP2_NO_SD; |
| 405 |
if (device_property_read_bool(dev, "no-mmc")) |
| 406 |
host->caps2 |= MMC_CAP2_NO_MMC; |
| 407 |
if (device_property_read_bool(dev, "no-mmc-hs400")) |
| 408 |
host->caps2 &= ~(MMC_CAP2_HS400_1_8V | MMC_CAP2_HS400_1_2V | |
| 409 |
MMC_CAP2_HS400_ES); |
| 410 |
|
| 411 |
/* Must be after "non-removable" check */ |
| 412 |
if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) { |
| 413 |
if (host->caps & MMC_CAP_NONREMOVABLE) |
| 414 |
host->fixed_drv_type = drv_type; |
| 415 |
else |
| 416 |
dev_err(host->parent, |
| 417 |
"can't use fixed driver type, media is removable\n"); |
| 418 |
} |
| 419 |
|
| 420 |
host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr); |
| 421 |
if (host->dsr_req && (host->dsr & ~0xffff)) { |
| 422 |
dev_err(host->parent, |
| 423 |
"device tree specified broken value for DSR: 0x%x, ignoring\n", |
| 424 |
host->dsr); |
| 425 |
host->dsr_req = 0; |
| 426 |
} |
| 427 |
|
| 428 |
device_property_read_u32(dev, "post-power-on-delay-ms", |
| 429 |
&host->ios.power_delay_ms); |
| 430 |
|
| 431 |
return mmc_pwrseq_alloc(host); |
| 432 |
} |
| 433 |
|
| 434 |
EXPORT_SYMBOL(mmc_of_parse); |
| 435 |
|
| 436 |
/** |
| 437 |
* mmc_of_parse_voltage - return mask of supported voltages |
| 438 |
* @host: host whose properties should be parsed. |
| 439 |
* @mask: mask of voltages available for MMC/SD/SDIO |
| 440 |
* |
| 441 |
* Parse the "voltage-ranges" property, returning zero if it is not |
| 442 |
* found, negative errno if the voltage-range specification is invalid, |
| 443 |
* or one if the voltage-range is specified and successfully parsed. |
| 444 |
*/ |
| 445 |
int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask) |
| 446 |
{ |
| 447 |
const char *prop = "voltage-ranges"; |
| 448 |
struct device *dev = host->parent; |
| 449 |
u32 *voltage_ranges; |
| 450 |
int num_ranges, i; |
| 451 |
int ret; |
| 452 |
|
| 453 |
if (!device_property_present(dev, prop)) { |
| 454 |
dev_dbg(dev, "%s unspecified\n", prop); |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
ret = device_property_count_u32(dev, prop); |
| 459 |
if (ret < 0) |
| 460 |
return ret; |
| 461 |
|
| 462 |
num_ranges = ret / 2; |
| 463 |
if (!num_ranges) { |
| 464 |
dev_err(dev, "%s empty\n", prop); |
| 465 |
return -EINVAL; |
| 466 |
} |
| 467 |
|
| 468 |
voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL); |
| 469 |
if (!voltage_ranges) |
| 470 |
return -ENOMEM; |
| 471 |
|
| 472 |
ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges); |
| 473 |
if (ret) { |
| 474 |
kfree(voltage_ranges); |
| 475 |
return ret; |
| 476 |
} |
| 477 |
|
| 478 |
for (i = 0; i < num_ranges; i++) { |
| 479 |
const int j = i * 2; |
| 480 |
u32 ocr_mask; |
| 481 |
|
| 482 |
ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0], |
| 483 |
voltage_ranges[j + 1]); |
| 484 |
if (!ocr_mask) { |
| 485 |
dev_err(dev, "range #%d in %s is invalid\n", i, prop); |
| 486 |
kfree(voltage_ranges); |
| 487 |
return -EINVAL; |
| 488 |
} |
| 489 |
*mask |= ocr_mask; |
| 490 |
} |
| 491 |
|
| 492 |
kfree(voltage_ranges); |
| 493 |
|
| 494 |
return 1; |
| 495 |
} |
| 496 |
EXPORT_SYMBOL(mmc_of_parse_voltage); |
| 497 |
|
| 498 |
/** |
| 499 |
* mmc_first_nonreserved_index() - get the first index that is not reserved |
| 500 |
*/ |
| 501 |
static int mmc_first_nonreserved_index(void) |
| 502 |
{ |
| 503 |
int max; |
| 504 |
|
| 505 |
max = of_alias_get_highest_id("mmc"); |
| 506 |
if (max < 0) |
| 507 |
return 0; |
| 508 |
|
| 509 |
return max + 1; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* mmc_alloc_host - initialise the per-host structure. |
| 514 |
* @extra: sizeof private data structure |
| 515 |
* @dev: pointer to host device model structure |
| 516 |
* |
| 517 |
* Initialise the per-host structure. |
| 518 |
*/ |
| 519 |
struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 520 |
{ |
| 521 |
int index; |
| 522 |
struct mmc_host *host; |
| 523 |
int alias_id, min_idx, max_idx; |
| 524 |
|
| 525 |
host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); |
| 526 |
if (!host) |
| 527 |
return NULL; |
| 528 |
|
| 529 |
/* scanning will be enabled when we're ready */ |
| 530 |
host->rescan_disable = 1; |
| 531 |
|
| 532 |
alias_id = of_alias_get_id(dev->of_node, "mmc"); |
| 533 |
if (alias_id >= 0) { |
| 534 |
index = alias_id; |
| 535 |
} else { |
| 536 |
min_idx = mmc_first_nonreserved_index(); |
| 537 |
max_idx = 0; |
| 538 |
|
| 539 |
index = ida_alloc_range(&mmc_host_ida, min_idx, max_idx - 1, |
| 540 |
GFP_KERNEL); |
| 541 |
if (index < 0) { |
| 542 |
kfree(host); |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
host->index = index; |
| 548 |
|
| 549 |
dev_set_name(&host->class_dev, "mmc%d", host->index); |
| 550 |
host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev)); |
| 551 |
|
| 552 |
host->parent = dev; |
| 553 |
host->class_dev.parent = dev; |
| 554 |
host->class_dev.class = &mmc_host_class; |
| 555 |
device_initialize(&host->class_dev); |
| 556 |
device_enable_async_suspend(&host->class_dev); |
| 557 |
|
| 558 |
if (mmc_gpio_alloc(host)) { |
| 559 |
put_device(&host->class_dev); |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
|
| 563 |
spin_lock_init(&host->lock); |
| 564 |
init_waitqueue_head(&host->wq); |
| 565 |
INIT_DELAYED_WORK(&host->detect, mmc_rescan); |
| 566 |
INIT_WORK(&host->sdio_irq_work, sdio_irq_work); |
| 567 |
timer_setup(&host->retune_timer, mmc_retune_timer, 0); |
| 568 |
|
| 569 |
INIT_WORK(&host->supply.uv_work, mmc_undervoltage_workfn); |
| 570 |
|
| 571 |
/* |
| 572 |
* By default, hosts do not support SGIO or large requests. |
| 573 |
* They have to set these according to their abilities. |
| 574 |
*/ |
| 575 |
host->max_segs = 1; |
| 576 |
host->max_seg_size = PAGE_SIZE; |
| 577 |
|
| 578 |
host->max_req_size = PAGE_SIZE; |
| 579 |
host->max_blk_size = 512; |
| 580 |
host->max_blk_count = PAGE_SIZE / 512; |
| 581 |
|
| 582 |
host->fixed_drv_type = -EINVAL; |
| 583 |
host->ios.power_delay_ms = 10; |
| 584 |
host->ios.power_mode = MMC_POWER_UNDEFINED; |
| 585 |
|
| 586 |
return host; |
| 587 |
} |
| 588 |
|
| 589 |
EXPORT_SYMBOL(mmc_alloc_host); |
| 590 |
|
| 591 |
static void devm_mmc_host_release(struct device *dev, void *res) |
| 592 |
{ |
| 593 |
mmc_free_host(*(struct mmc_host **)res); |
| 594 |
} |
| 595 |
|
| 596 |
struct mmc_host *devm_mmc_alloc_host(struct device *dev, int extra) |
| 597 |
{ |
| 598 |
struct mmc_host **dr, *host; |
| 599 |
|
| 600 |
dr = devres_alloc(devm_mmc_host_release, sizeof(*dr), GFP_KERNEL); |
| 601 |
if (!dr) |
| 602 |
return NULL; |
| 603 |
|
| 604 |
host = mmc_alloc_host(extra, dev); |
| 605 |
if (!host) { |
| 606 |
devres_free(dr); |
| 607 |
return NULL; |
| 608 |
} |
| 609 |
|
| 610 |
*dr = host; |
| 611 |
devres_add(dev, dr); |
| 612 |
|
| 613 |
return host; |
| 614 |
} |
| 615 |
EXPORT_SYMBOL(devm_mmc_alloc_host); |
| 616 |
|
| 617 |
static int mmc_validate_host_caps(struct mmc_host *host) |
| 618 |
{ |
| 619 |
struct device *dev = host->parent; |
| 620 |
u32 caps = host->caps, caps2 = host->caps2; |
| 621 |
|
| 622 |
if (caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) { |
| 623 |
dev_warn(dev, "missing ->enable_sdio_irq() ops\n"); |
| 624 |
return -EINVAL; |
| 625 |
} |
| 626 |
|
| 627 |
if (caps2 & (MMC_CAP2_HS400_ES | MMC_CAP2_HS400) && |
| 628 |
!(caps & MMC_CAP_8_BIT_DATA) && !(caps2 & MMC_CAP2_NO_MMC)) { |
| 629 |
dev_warn(dev, "drop HS400 support since no 8-bit bus\n"); |
| 630 |
host->caps2 = caps2 & ~MMC_CAP2_HS400_ES & ~MMC_CAP2_HS400; |
| 631 |
} |
| 632 |
|
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* mmc_add_host - initialise host hardware |
| 638 |
* @host: mmc host |
| 639 |
* |
| 640 |
* Register the host with the driver model. The host must be |
| 641 |
* prepared to start servicing requests before this function |
| 642 |
* completes. |
| 643 |
*/ |
| 644 |
int mmc_add_host(struct mmc_host *host) |
| 645 |
{ |
| 646 |
int err; |
| 647 |
|
| 648 |
err = mmc_validate_host_caps(host); |
| 649 |
if (err) |
| 650 |
return err; |
| 651 |
|
| 652 |
err = device_add(&host->class_dev); |
| 653 |
if (err) |
| 654 |
return err; |
| 655 |
|
| 656 |
led_trigger_register_simple(dev_name(&host->class_dev), &host->led); |
| 657 |
|
| 658 |
mmc_add_host_debugfs(host); |
| 659 |
|
| 660 |
mmc_start_host(host); |
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
EXPORT_SYMBOL(mmc_add_host); |
| 665 |
|
| 666 |
/** |
| 667 |
* mmc_remove_host - remove host hardware |
| 668 |
* @host: mmc host |
| 669 |
* |
| 670 |
* Unregister and remove all cards associated with this host, |
| 671 |
* and power down the MMC bus. No new requests will be issued |
| 672 |
* after this function has returned. |
| 673 |
*/ |
| 674 |
void mmc_remove_host(struct mmc_host *host) |
| 675 |
{ |
| 676 |
mmc_stop_host(host); |
| 677 |
|
| 678 |
mmc_remove_host_debugfs(host); |
| 679 |
|
| 680 |
device_del(&host->class_dev); |
| 681 |
|
| 682 |
led_trigger_unregister_simple(host->led); |
| 683 |
} |
| 684 |
|
| 685 |
EXPORT_SYMBOL(mmc_remove_host); |
| 686 |
|
| 687 |
/** |
| 688 |
* mmc_free_host - free the host structure |
| 689 |
* @host: mmc host |
| 690 |
* |
| 691 |
* Free the host once all references to it have been dropped. |
| 692 |
*/ |
| 693 |
void mmc_free_host(struct mmc_host *host) |
| 694 |
{ |
| 695 |
cancel_delayed_work_sync(&host->detect); |
| 696 |
mmc_pwrseq_free(host); |
| 697 |
put_device(&host->class_dev); |
| 698 |
} |
| 699 |
|
| 700 |
EXPORT_SYMBOL(mmc_free_host); |