blob: 7b9e133ca7b753b3ed050cb656b8182854bd2fd4 [file] [log] [blame]
Pierre Ossman4101c162007-05-19 13:39:01 +02001/*
2 * linux/drivers/mmc/core/bus.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright (C) 2007 Pierre Ossman
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * MMC card bus driver model
12 */
13
Paul Gortmaker3ef77af2011-07-10 12:42:00 -040014#include <linux/export.h>
Pierre Ossman4101c162007-05-19 13:39:01 +020015#include <linux/device.h>
16#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040018#include <linux/stat.h>
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +020019#include <linux/pm_runtime.h>
Pierre Ossman4101c162007-05-19 13:39:01 +020020
21#include <linux/mmc/card.h>
22#include <linux/mmc/host.h>
23
Pierre Ossman4101c162007-05-19 13:39:01 +020024#include "core.h"
Pierre Ossman1a632f82007-07-30 15:15:30 +020025#include "sdio_cis.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020026#include "bus.h"
27
Pierre Ossman4101c162007-05-19 13:39:01 +020028#define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
Asutosh Dasbbc84782013-02-11 15:31:35 +053029#define RUNTIME_SUSPEND_DELAY_MS 10000
Pierre Ossman4101c162007-05-19 13:39:01 +020030
31static ssize_t mmc_type_show(struct device *dev,
32 struct device_attribute *attr, char *buf)
33{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040034 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +020035
36 switch (card->type) {
37 case MMC_TYPE_MMC:
38 return sprintf(buf, "MMC\n");
39 case MMC_TYPE_SD:
40 return sprintf(buf, "SD\n");
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020041 case MMC_TYPE_SDIO:
42 return sprintf(buf, "SDIO\n");
Michal Miroslaw7310ece2010-08-10 18:01:40 -070043 case MMC_TYPE_SD_COMBO:
44 return sprintf(buf, "SDcombo\n");
Pierre Ossman4101c162007-05-19 13:39:01 +020045 default:
46 return -EFAULT;
47 }
48}
49
50static struct device_attribute mmc_dev_attrs[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +010051 __ATTR(type, S_IRUGO, mmc_type_show, NULL),
Pierre Ossman4101c162007-05-19 13:39:01 +020052 __ATTR_NULL,
53};
54
55/*
56 * This currently matches any MMC driver to any MMC card - drivers
57 * themselves make the decision whether to drive this card in their
58 * probe method.
59 */
60static int mmc_bus_match(struct device *dev, struct device_driver *drv)
61{
62 return 1;
63}
64
65static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020066mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
Pierre Ossman4101c162007-05-19 13:39:01 +020067{
Andy Shevchenko265cdc92010-09-17 20:32:25 -040068 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman9eb3a942007-06-17 11:18:46 +020069 const char *type;
Kay Sievers7eff2e72007-08-14 15:15:12 +020070 int retval = 0;
Pierre Ossman4101c162007-05-19 13:39:01 +020071
72 switch (card->type) {
73 case MMC_TYPE_MMC:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020074 type = "MMC";
Pierre Ossman4101c162007-05-19 13:39:01 +020075 break;
76 case MMC_TYPE_SD:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020077 type = "SD";
Pierre Ossman4101c162007-05-19 13:39:01 +020078 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020079 case MMC_TYPE_SDIO:
Pierre Ossman9eb3a942007-06-17 11:18:46 +020080 type = "SDIO";
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020081 break;
Michal Miroslaw7310ece2010-08-10 18:01:40 -070082 case MMC_TYPE_SD_COMBO:
83 type = "SDcombo";
84 break;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020085 default:
86 type = NULL;
Pierre Ossman4101c162007-05-19 13:39:01 +020087 }
88
Pierre Ossman9eb3a942007-06-17 11:18:46 +020089 if (type) {
Kay Sievers7eff2e72007-08-14 15:15:12 +020090 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
91 if (retval)
92 return retval;
Pierre Ossman9eb3a942007-06-17 11:18:46 +020093 }
Pierre Ossman4101c162007-05-19 13:39:01 +020094
Kay Sievers7eff2e72007-08-14 15:15:12 +020095 retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000096 if (retval)
97 return retval;
98
99 /*
100 * Request the mmc_block device. Note: that this is a direct request
101 * for the module it carries no information as to what is inserted.
102 */
103 retval = add_uevent_var(env, "MODALIAS=mmc:block");
Pierre Ossman4101c162007-05-19 13:39:01 +0200104
Kay Sievers7eff2e72007-08-14 15:15:12 +0200105 return retval;
Pierre Ossman4101c162007-05-19 13:39:01 +0200106}
107
108static int mmc_bus_probe(struct device *dev)
109{
110 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400111 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200112
113 return drv->probe(card);
114}
115
116static int mmc_bus_remove(struct device *dev)
117{
118 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400119 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200120
121 drv->remove(card);
122
123 return 0;
124}
125
Rohit Vaswanid4e3d522012-03-28 16:03:47 -0700126#ifdef CONFIG_PM_SLEEP
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800127static int mmc_bus_suspend(struct device *dev)
Pierre Ossman4101c162007-05-19 13:39:01 +0200128{
129 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400130 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200131 int ret = 0;
132
133 if (dev->driver && drv->suspend)
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800134 ret = drv->suspend(card);
Pierre Ossman4101c162007-05-19 13:39:01 +0200135 return ret;
136}
137
138static int mmc_bus_resume(struct device *dev)
139{
140 struct mmc_driver *drv = to_mmc_driver(dev->driver);
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400141 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200142 int ret = 0;
143
144 if (dev->driver && drv->resume)
145 ret = drv->resume(card);
146 return ret;
147}
Rohit Vaswanid4e3d522012-03-28 16:03:47 -0700148#endif
Pierre Ossman4101c162007-05-19 13:39:01 +0200149
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200150#ifdef CONFIG_PM_RUNTIME
151
152static int mmc_runtime_suspend(struct device *dev)
153{
154 struct mmc_card *card = mmc_dev_to_card(dev);
155
Asutosh Dasbbc84782013-02-11 15:31:35 +0530156 if (mmc_use_core_runtime_pm(card->host))
157 return 0;
158 else
159 return mmc_power_save_host(card->host);
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200160}
161
162static int mmc_runtime_resume(struct device *dev)
163{
164 struct mmc_card *card = mmc_dev_to_card(dev);
165
Asutosh Dasbbc84782013-02-11 15:31:35 +0530166 if (mmc_use_core_runtime_pm(card->host))
167 return 0;
168 else
169 return mmc_power_restore_host(card->host);
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200170}
171
172static int mmc_runtime_idle(struct device *dev)
173{
Asutosh Dasbbc84782013-02-11 15:31:35 +0530174 struct mmc_card *card = mmc_dev_to_card(dev);
175 struct mmc_host *host = card->host;
176 int ret = 0;
177
178 if (mmc_use_core_runtime_pm(card->host)) {
179 ret = pm_schedule_suspend(dev, card->idle_timeout);
180 if (ret) {
181 pr_err("%s: %s: pm_schedule_suspend failed: err: %d\n",
182 mmc_hostname(host), __func__, ret);
183 return ret;
184 }
185 }
186
187 return ret;
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200188}
189
Ohad Ben-Cohen516d5cc2010-10-02 13:54:08 +0200190#endif /* !CONFIG_PM_RUNTIME */
191
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800192static const struct dev_pm_ops mmc_bus_pm_ops = {
193 SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume,
194 mmc_runtime_idle)
195 SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
196};
197
Asutosh Dasbbc84782013-02-11 15:31:35 +0530198static ssize_t show_rpm_delay(struct device *dev, struct device_attribute *attr,
199 char *buf)
200{
201 struct mmc_card *card = mmc_dev_to_card(dev);
202
203 if (!card) {
204 pr_err("%s: %s: card is NULL\n", dev_name(dev), __func__);
205 return -EINVAL;
206 }
207
208 return snprintf(buf, PAGE_SIZE, "%u\n", card->idle_timeout);
209}
210
211static ssize_t store_rpm_delay(struct device *dev, struct device_attribute
212 *attr, const char *buf, size_t count)
213{
214 struct mmc_card *card = mmc_dev_to_card(dev);
215 unsigned int delay;
216
217 if (!card) {
218 pr_err("%s: %s: card is NULL\n", dev_name(dev), __func__);
219 return -EINVAL;
220 }
221
222 if (!kstrtou32(buf, 0, &delay)) {
223 if (delay < 2000) {
224 pr_err("%s: %s: less than 2 sec delay is unsupported\n",
225 mmc_hostname(card->host), __func__);
226 return -EINVAL;
227 }
228 card->idle_timeout = delay;
229 }
230
231 return count;
232}
233
Pierre Ossman4101c162007-05-19 13:39:01 +0200234static struct bus_type mmc_bus_type = {
235 .name = "mmc",
236 .dev_attrs = mmc_dev_attrs,
237 .match = mmc_bus_match,
238 .uevent = mmc_bus_uevent,
239 .probe = mmc_bus_probe,
240 .remove = mmc_bus_remove,
Chuanxiao Dong32d317c2012-04-11 19:54:38 +0800241 .pm = &mmc_bus_pm_ops,
Pierre Ossman4101c162007-05-19 13:39:01 +0200242};
243
244int mmc_register_bus(void)
245{
246 return bus_register(&mmc_bus_type);
247}
248
249void mmc_unregister_bus(void)
250{
251 bus_unregister(&mmc_bus_type);
252}
253
254/**
255 * mmc_register_driver - register a media driver
256 * @drv: MMC media driver
257 */
258int mmc_register_driver(struct mmc_driver *drv)
259{
260 drv->drv.bus = &mmc_bus_type;
261 return driver_register(&drv->drv);
262}
263
264EXPORT_SYMBOL(mmc_register_driver);
265
266/**
267 * mmc_unregister_driver - unregister a media driver
268 * @drv: MMC media driver
269 */
270void mmc_unregister_driver(struct mmc_driver *drv)
271{
272 drv->drv.bus = &mmc_bus_type;
273 driver_unregister(&drv->drv);
274}
275
276EXPORT_SYMBOL(mmc_unregister_driver);
277
278static void mmc_release_card(struct device *dev)
279{
Andy Shevchenko265cdc92010-09-17 20:32:25 -0400280 struct mmc_card *card = mmc_dev_to_card(dev);
Pierre Ossman4101c162007-05-19 13:39:01 +0200281
Pierre Ossman1a632f82007-07-30 15:15:30 +0200282 sdio_free_common_cis(card);
283
Pierre Ossman759bdc72007-09-19 18:42:16 +0200284 if (card->info)
285 kfree(card->info);
286
Pierre Ossman4101c162007-05-19 13:39:01 +0200287 kfree(card);
288}
289
290/*
291 * Allocate and initialise a new MMC card structure.
292 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100293struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
Pierre Ossman4101c162007-05-19 13:39:01 +0200294{
295 struct mmc_card *card;
296
Mariusz Kozlowski733cb1e2007-08-10 14:00:47 -0700297 card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
Pierre Ossman4101c162007-05-19 13:39:01 +0200298 if (!card)
299 return ERR_PTR(-ENOMEM);
300
Pierre Ossman4101c162007-05-19 13:39:01 +0200301 card->host = host;
302
303 device_initialize(&card->dev);
304
305 card->dev.parent = mmc_classdev(host);
306 card->dev.bus = &mmc_bus_type;
307 card->dev.release = mmc_release_card;
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100308 card->dev.type = type;
Pierre Ossman4101c162007-05-19 13:39:01 +0200309
Yaniv Gardi4c4383f2012-10-11 18:29:28 +0200310 spin_lock_init(&card->bkops_info.bkops_stats.lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +0200311 spin_lock_init(&card->wr_pack_stats.lock);
312
Pierre Ossman4101c162007-05-19 13:39:01 +0200313 return card;
314}
315
316/*
317 * Register a new MMC card with the driver model.
318 */
319int mmc_add_card(struct mmc_card *card)
320{
321 int ret;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200322 const char *type;
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530323 const char *uhs_bus_speed_mode = "";
324 static const char *const uhs_speeds[] = {
325 [UHS_SDR12_BUS_SPEED] = "SDR12 ",
326 [UHS_SDR25_BUS_SPEED] = "SDR25 ",
327 [UHS_SDR50_BUS_SPEED] = "SDR50 ",
328 [UHS_SDR104_BUS_SPEED] = "SDR104 ",
329 [UHS_DDR50_BUS_SPEED] = "DDR50 ",
330 };
331
Pierre Ossman4101c162007-05-19 13:39:01 +0200332
Kay Sieversd1b26862008-11-08 21:37:46 +0100333 dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
Pierre Ossman4101c162007-05-19 13:39:01 +0200334
Pierre Ossman109b5be2007-07-23 00:12:10 +0200335 switch (card->type) {
336 case MMC_TYPE_MMC:
337 type = "MMC";
338 break;
339 case MMC_TYPE_SD:
340 type = "SD";
Arindam Nath3a303512011-05-05 12:19:03 +0530341 if (mmc_card_blockaddr(card)) {
342 if (mmc_card_ext_capacity(card))
343 type = "SDXC";
344 else
345 type = "SDHC";
346 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200347 break;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200348 case MMC_TYPE_SDIO:
349 type = "SDIO";
350 break;
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700351 case MMC_TYPE_SD_COMBO:
352 type = "SD-combo";
353 if (mmc_card_blockaddr(card))
354 type = "SDHC-combo";
Michał Mirosław9bc21842011-04-09 08:16:47 +0200355 break;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200356 default:
357 type = "?";
358 break;
359 }
360
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530361 if (mmc_sd_card_uhs(card) &&
362 (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
363 uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
364
David Brownellaf517152007-08-08 09:11:32 -0700365 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530366 pr_info("%s: new %s%s%s card on SPI\n",
David Brownellaf517152007-08-08 09:11:32 -0700367 mmc_hostname(card->host),
368 mmc_card_highspeed(card) ? "high speed " : "",
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300369 mmc_card_ddr_mode(card) ? "DDR " : "",
David Brownellaf517152007-08-08 09:11:32 -0700370 type);
371 } else {
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530372 pr_info("%s: new %s%s%s%s%s card at address %04x\n",
David Brownellaf517152007-08-08 09:11:32 -0700373 mmc_hostname(card->host),
Philip Rakitya303c532011-11-14 19:14:38 -0800374 mmc_card_uhs(card) ? "ultra high speed " :
Arindam Nath3a303512011-05-05 12:19:03 +0530375 (mmc_card_highspeed(card) ? "high speed " : ""),
Girish K Sa4924c72012-01-11 14:04:52 -0500376 (mmc_card_hs200(card) ? "HS200 " : ""),
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300377 mmc_card_ddr_mode(card) ? "DDR " : "",
Subhash Jadavani6500c8e2012-03-30 12:10:18 +0530378 uhs_bus_speed_mode, type, card->rca);
David Brownellaf517152007-08-08 09:11:32 -0700379 }
Pierre Ossman109b5be2007-07-23 00:12:10 +0200380
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200381#ifdef CONFIG_DEBUG_FS
382 mmc_add_card_debugfs(card);
383#endif
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200384 mmc_init_context_info(card->host);
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200385
Asutosh Dasbbc84782013-02-11 15:31:35 +0530386 if (mmc_use_core_runtime_pm(card->host)) {
387 ret = pm_runtime_set_active(&card->dev);
388 if (ret)
389 pr_err("%s: %s: failed setting runtime active: ret: %d\n",
390 mmc_hostname(card->host), __func__, ret);
391 else
392 pm_runtime_enable(&card->dev);
393 }
394
Viresh Kumar1a2727e2011-01-04 12:55:14 -0500395 ret = device_add(&card->dev);
396 if (ret)
397 return ret;
398
Asutosh Dasbbc84782013-02-11 15:31:35 +0530399 if (mmc_use_core_runtime_pm(card->host)) {
400 card->rpm_attrib.show = show_rpm_delay;
401 card->rpm_attrib.store = store_rpm_delay;
402 sysfs_attr_init(&card->rpm_attrib.attr);
403 card->rpm_attrib.attr.name = "runtime_pm_timeout";
404 card->rpm_attrib.attr.mode = S_IRUGO | S_IWUSR;
405
406 ret = device_create_file(&card->dev, &card->rpm_attrib);
407 if (ret)
408 pr_err("%s: %s: creating runtime pm sysfs entry: failed: %d\n",
409 mmc_hostname(card->host), __func__, ret);
410 /* Default timeout is 10 seconds */
411 card->idle_timeout = RUNTIME_SUSPEND_DELAY_MS;
412 }
413
Pierre Ossman4101c162007-05-19 13:39:01 +0200414 mmc_card_set_present(card);
415
416 return 0;
417}
418
419/*
420 * Unregister a new MMC card with the driver model, and
421 * (eventually) free it.
422 */
423void mmc_remove_card(struct mmc_card *card)
424{
Haavard Skinnemoenf4b7f922008-07-24 14:18:58 +0200425#ifdef CONFIG_DEBUG_FS
426 mmc_remove_card_debugfs(card);
427#endif
428
Pierre Ossman4101c162007-05-19 13:39:01 +0200429 if (mmc_card_present(card)) {
David Brownellaf517152007-08-08 09:11:32 -0700430 if (mmc_host_is_spi(card->host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530431 pr_info("%s: SPI card removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700432 mmc_hostname(card->host));
433 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530434 pr_info("%s: card %04x removed\n",
David Brownellaf517152007-08-08 09:11:32 -0700435 mmc_hostname(card->host), card->rca);
436 }
Pierre Ossman4101c162007-05-19 13:39:01 +0200437 device_del(&card->dev);
438 }
439
Tatyana Brokhman464fbe12012-10-07 10:33:13 +0200440 kfree(card->wr_pack_stats.packing_events);
441
Pierre Ossman4101c162007-05-19 13:39:01 +0200442 put_device(&card->dev);
443}
444