xref: /openbmc/linux/drivers/mmc/core/bus.c (revision 034f90b3)
1 /*
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 
14 #include <linux/export.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 #include <linux/of.h>
20 #include <linux/pm_runtime.h>
21 
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/host.h>
24 
25 #include "core.h"
26 #include "sdio_cis.h"
27 #include "bus.h"
28 
29 static ssize_t type_show(struct device *dev,
30 	struct device_attribute *attr, char *buf)
31 {
32 	struct mmc_card *card = mmc_dev_to_card(dev);
33 
34 	switch (card->type) {
35 	case MMC_TYPE_MMC:
36 		return sprintf(buf, "MMC\n");
37 	case MMC_TYPE_SD:
38 		return sprintf(buf, "SD\n");
39 	case MMC_TYPE_SDIO:
40 		return sprintf(buf, "SDIO\n");
41 	case MMC_TYPE_SD_COMBO:
42 		return sprintf(buf, "SDcombo\n");
43 	default:
44 		return -EFAULT;
45 	}
46 }
47 static DEVICE_ATTR_RO(type);
48 
49 static struct attribute *mmc_dev_attrs[] = {
50 	&dev_attr_type.attr,
51 	NULL,
52 };
53 ATTRIBUTE_GROUPS(mmc_dev);
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  */
60 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
61 {
62 	return 1;
63 }
64 
65 static int
66 mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
67 {
68 	struct mmc_card *card = mmc_dev_to_card(dev);
69 	const char *type;
70 	int retval = 0;
71 
72 	switch (card->type) {
73 	case MMC_TYPE_MMC:
74 		type = "MMC";
75 		break;
76 	case MMC_TYPE_SD:
77 		type = "SD";
78 		break;
79 	case MMC_TYPE_SDIO:
80 		type = "SDIO";
81 		break;
82 	case MMC_TYPE_SD_COMBO:
83 		type = "SDcombo";
84 		break;
85 	default:
86 		type = NULL;
87 	}
88 
89 	if (type) {
90 		retval = add_uevent_var(env, "MMC_TYPE=%s", type);
91 		if (retval)
92 			return retval;
93 	}
94 
95 	retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
96 	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");
104 
105 	return retval;
106 }
107 
108 static void mmc_bus_shutdown(struct device *dev)
109 {
110 	struct mmc_card *card = mmc_dev_to_card(dev);
111 	struct mmc_host *host = card->host;
112 	int ret;
113 
114 	if (dev->driver && dev->driver->shutdown)
115 		dev->driver->shutdown(dev);
116 
117 	if (host->bus_ops->shutdown) {
118 		ret = host->bus_ops->shutdown(host);
119 		if (ret)
120 			pr_warn("%s: error %d during shutdown\n",
121 				mmc_hostname(host), ret);
122 	}
123 }
124 
125 #ifdef CONFIG_PM_SLEEP
126 static int mmc_bus_suspend(struct device *dev)
127 {
128 	struct mmc_card *card = mmc_dev_to_card(dev);
129 	struct mmc_host *host = card->host;
130 	int ret;
131 
132 	ret = pm_generic_suspend(dev);
133 	if (ret)
134 		return ret;
135 
136 	ret = host->bus_ops->suspend(host);
137 	return ret;
138 }
139 
140 static int mmc_bus_resume(struct device *dev)
141 {
142 	struct mmc_card *card = mmc_dev_to_card(dev);
143 	struct mmc_host *host = card->host;
144 	int ret;
145 
146 	ret = host->bus_ops->resume(host);
147 	if (ret)
148 		pr_warn("%s: error %d during resume (card was removed?)\n",
149 			mmc_hostname(host), ret);
150 
151 	ret = pm_generic_resume(dev);
152 	return ret;
153 }
154 #endif
155 
156 #ifdef CONFIG_PM
157 static int mmc_runtime_suspend(struct device *dev)
158 {
159 	struct mmc_card *card = mmc_dev_to_card(dev);
160 	struct mmc_host *host = card->host;
161 
162 	return host->bus_ops->runtime_suspend(host);
163 }
164 
165 static int mmc_runtime_resume(struct device *dev)
166 {
167 	struct mmc_card *card = mmc_dev_to_card(dev);
168 	struct mmc_host *host = card->host;
169 
170 	return host->bus_ops->runtime_resume(host);
171 }
172 #endif /* !CONFIG_PM */
173 
174 static const struct dev_pm_ops mmc_bus_pm_ops = {
175 	SET_RUNTIME_PM_OPS(mmc_runtime_suspend, mmc_runtime_resume, NULL)
176 	SET_SYSTEM_SLEEP_PM_OPS(mmc_bus_suspend, mmc_bus_resume)
177 };
178 
179 static struct bus_type mmc_bus_type = {
180 	.name		= "mmc",
181 	.dev_groups	= mmc_dev_groups,
182 	.match		= mmc_bus_match,
183 	.uevent		= mmc_bus_uevent,
184 	.shutdown	= mmc_bus_shutdown,
185 	.pm		= &mmc_bus_pm_ops,
186 };
187 
188 int mmc_register_bus(void)
189 {
190 	return bus_register(&mmc_bus_type);
191 }
192 
193 void mmc_unregister_bus(void)
194 {
195 	bus_unregister(&mmc_bus_type);
196 }
197 
198 /**
199  *	mmc_register_driver - register a media driver
200  *	@drv: MMC media driver
201  */
202 int mmc_register_driver(struct device_driver *drv)
203 {
204 	drv->bus = &mmc_bus_type;
205 	return driver_register(drv);
206 }
207 EXPORT_SYMBOL(mmc_register_driver);
208 
209 /**
210  *	mmc_unregister_driver - unregister a media driver
211  *	@drv: MMC media driver
212  */
213 void mmc_unregister_driver(struct device_driver *drv)
214 {
215 	drv->bus = &mmc_bus_type;
216 	driver_unregister(drv);
217 }
218 EXPORT_SYMBOL(mmc_unregister_driver);
219 
220 static void mmc_release_card(struct device *dev)
221 {
222 	struct mmc_card *card = mmc_dev_to_card(dev);
223 
224 	sdio_free_common_cis(card);
225 
226 	kfree(card->info);
227 
228 	kfree(card);
229 }
230 
231 /*
232  * Allocate and initialise a new MMC card structure.
233  */
234 struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
235 {
236 	struct mmc_card *card;
237 
238 	card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
239 	if (!card)
240 		return ERR_PTR(-ENOMEM);
241 
242 	card->host = host;
243 
244 	device_initialize(&card->dev);
245 
246 	card->dev.parent = mmc_classdev(host);
247 	card->dev.bus = &mmc_bus_type;
248 	card->dev.release = mmc_release_card;
249 	card->dev.type = type;
250 
251 	return card;
252 }
253 
254 /*
255  * Register a new MMC card with the driver model.
256  */
257 int mmc_add_card(struct mmc_card *card)
258 {
259 	int ret;
260 	const char *type;
261 	const char *uhs_bus_speed_mode = "";
262 	static const char *const uhs_speeds[] = {
263 		[UHS_SDR12_BUS_SPEED] = "SDR12 ",
264 		[UHS_SDR25_BUS_SPEED] = "SDR25 ",
265 		[UHS_SDR50_BUS_SPEED] = "SDR50 ",
266 		[UHS_SDR104_BUS_SPEED] = "SDR104 ",
267 		[UHS_DDR50_BUS_SPEED] = "DDR50 ",
268 	};
269 
270 
271 	dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
272 
273 	switch (card->type) {
274 	case MMC_TYPE_MMC:
275 		type = "MMC";
276 		break;
277 	case MMC_TYPE_SD:
278 		type = "SD";
279 		if (mmc_card_blockaddr(card)) {
280 			if (mmc_card_ext_capacity(card))
281 				type = "SDXC";
282 			else
283 				type = "SDHC";
284 		}
285 		break;
286 	case MMC_TYPE_SDIO:
287 		type = "SDIO";
288 		break;
289 	case MMC_TYPE_SD_COMBO:
290 		type = "SD-combo";
291 		if (mmc_card_blockaddr(card))
292 			type = "SDHC-combo";
293 		break;
294 	default:
295 		type = "?";
296 		break;
297 	}
298 
299 	if (mmc_card_uhs(card) &&
300 		(card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
301 		uhs_bus_speed_mode = uhs_speeds[card->sd_bus_speed];
302 
303 	if (mmc_host_is_spi(card->host)) {
304 		pr_info("%s: new %s%s%s card on SPI\n",
305 			mmc_hostname(card->host),
306 			mmc_card_hs(card) ? "high speed " : "",
307 			mmc_card_ddr52(card) ? "DDR " : "",
308 			type);
309 	} else {
310 		pr_info("%s: new %s%s%s%s%s card at address %04x\n",
311 			mmc_hostname(card->host),
312 			mmc_card_uhs(card) ? "ultra high speed " :
313 			(mmc_card_hs(card) ? "high speed " : ""),
314 			mmc_card_hs400(card) ? "HS400 " :
315 			(mmc_card_hs200(card) ? "HS200 " : ""),
316 			mmc_card_ddr52(card) ? "DDR " : "",
317 			uhs_bus_speed_mode, type, card->rca);
318 	}
319 
320 #ifdef CONFIG_DEBUG_FS
321 	mmc_add_card_debugfs(card);
322 #endif
323 	mmc_init_context_info(card->host);
324 
325 	card->dev.of_node = mmc_of_find_child_device(card->host, 0);
326 
327 	ret = device_add(&card->dev);
328 	if (ret)
329 		return ret;
330 
331 	mmc_card_set_present(card);
332 
333 	return 0;
334 }
335 
336 /*
337  * Unregister a new MMC card with the driver model, and
338  * (eventually) free it.
339  */
340 void mmc_remove_card(struct mmc_card *card)
341 {
342 #ifdef CONFIG_DEBUG_FS
343 	mmc_remove_card_debugfs(card);
344 #endif
345 
346 	if (mmc_card_present(card)) {
347 		if (mmc_host_is_spi(card->host)) {
348 			pr_info("%s: SPI card removed\n",
349 				mmc_hostname(card->host));
350 		} else {
351 			pr_info("%s: card %04x removed\n",
352 				mmc_hostname(card->host), card->rca);
353 		}
354 		device_del(&card->dev);
355 		of_node_put(card->dev.of_node);
356 	}
357 
358 	put_device(&card->dev);
359 }
360 
361