2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
14 #include "mmc_private.h"
16 DECLARE_GLOBAL_DATA_PTR
;
18 #if CONFIG_IS_ENABLED(DM_MMC_OPS)
19 int dm_mmc_send_cmd(struct udevice
*dev
, struct mmc_cmd
*cmd
,
20 struct mmc_data
*data
)
22 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
23 struct dm_mmc_ops
*ops
= mmc_get_ops(dev
);
26 mmmc_trace_before_send(mmc
, cmd
);
28 ret
= ops
->send_cmd(dev
, cmd
, data
);
31 mmmc_trace_after_send(mmc
, cmd
, ret
);
36 int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
38 return dm_mmc_send_cmd(mmc
->dev
, cmd
, data
);
41 int dm_mmc_set_ios(struct udevice
*dev
)
43 struct dm_mmc_ops
*ops
= mmc_get_ops(dev
);
47 return ops
->set_ios(dev
);
50 int mmc_set_ios(struct mmc
*mmc
)
52 return dm_mmc_set_ios(mmc
->dev
);
55 int dm_mmc_get_wp(struct udevice
*dev
)
57 struct dm_mmc_ops
*ops
= mmc_get_ops(dev
);
61 return ops
->get_wp(dev
);
64 int mmc_getwp(struct mmc
*mmc
)
66 return dm_mmc_get_wp(mmc
->dev
);
69 int dm_mmc_get_cd(struct udevice
*dev
)
71 struct dm_mmc_ops
*ops
= mmc_get_ops(dev
);
75 return ops
->get_cd(dev
);
78 int mmc_getcd(struct mmc
*mmc
)
80 return dm_mmc_get_cd(mmc
->dev
);
84 struct mmc
*mmc_get_mmc_dev(struct udevice
*dev
)
86 struct mmc_uclass_priv
*upriv
;
88 if (!device_active(dev
))
90 upriv
= dev_get_uclass_priv(dev
);
94 #if CONFIG_IS_ENABLED(BLK)
95 struct mmc
*find_mmc_device(int dev_num
)
97 struct udevice
*dev
, *mmc_dev
;
100 ret
= blk_find_device(IF_TYPE_MMC
, dev_num
, &dev
);
103 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
104 printf("MMC Device %d not found\n", dev_num
);
109 mmc_dev
= dev_get_parent(dev
);
111 struct mmc
*mmc
= mmc_get_mmc_dev(mmc_dev
);
116 int get_mmc_num(void)
118 return max((blk_find_max_devnum(IF_TYPE_MMC
) + 1), 0);
121 int mmc_get_next_devnum(void)
123 return blk_find_max_devnum(IF_TYPE_MMC
);
126 struct blk_desc
*mmc_get_blk_desc(struct mmc
*mmc
)
128 struct blk_desc
*desc
;
131 device_find_first_child(mmc
->dev
, &dev
);
134 desc
= dev_get_uclass_platdata(dev
);
139 void mmc_do_preinit(void)
145 ret
= uclass_get(UCLASS_MMC
, &uc
);
148 uclass_foreach_dev(dev
, uc
) {
149 struct mmc
*m
= mmc_get_mmc_dev(dev
);
153 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
154 mmc_set_preinit(m
, 1);
161 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
162 void print_mmc_devices(char separator
)
168 for (uclass_first_device(UCLASS_MMC
, &dev
);
170 uclass_next_device(&dev
), first
= false) {
171 struct mmc
*m
= mmc_get_mmc_dev(dev
);
174 printf("%c", separator
);
175 if (separator
!= '\n')
179 mmc_type
= IS_SD(m
) ? "SD" : "eMMC";
183 printf("%s: %d", m
->cfg
->name
, mmc_get_blk_desc(m
)->devnum
);
185 printf(" (%s)", mmc_type
);
192 void print_mmc_devices(char separator
) { }
195 int mmc_bind(struct udevice
*dev
, struct mmc
*mmc
, const struct mmc_config
*cfg
)
197 struct blk_desc
*bdesc
;
198 struct udevice
*bdev
;
199 int ret
, devnum
= -1;
201 #if CONFIG_IS_ENABLED(DM_MMC_OPS)
202 if (!mmc_get_ops(dev
))
205 #ifndef CONFIG_SPL_BUILD
206 /* Use the fixed index with aliase node's index */
207 ret
= dev_read_alias_seq(dev
, &devnum
);
208 debug("%s: alias ret=%d, devnum=%d\n", __func__
, ret
, devnum
);
211 ret
= blk_create_devicef(dev
, "mmc_blk", "blk", IF_TYPE_MMC
,
212 devnum
, 512, 0, &bdev
);
214 debug("Cannot create block device\n");
217 bdesc
= dev_get_uclass_platdata(bdev
);
221 /* the following chunk was from mmc_register() */
223 /* Setup dsr related values */
225 mmc
->dsr
= 0xffffffff;
226 /* Setup the universal parts of the block interface just once */
227 bdesc
->removable
= 1;
229 /* setup initial part type */
230 bdesc
->part_type
= cfg
->part_type
;
236 int mmc_unbind(struct udevice
*dev
)
238 struct udevice
*bdev
;
240 device_find_first_child(dev
, &bdev
);
242 device_remove(bdev
, DM_REMOVE_NORMAL
);
249 static int mmc_select_hwpart(struct udevice
*bdev
, int hwpart
)
251 struct udevice
*mmc_dev
= dev_get_parent(bdev
);
252 struct mmc
*mmc
= mmc_get_mmc_dev(mmc_dev
);
253 struct blk_desc
*desc
= dev_get_uclass_platdata(bdev
);
255 if (desc
->hwpart
== hwpart
)
258 if (mmc
->part_config
== MMCPART_NOAVAILABLE
)
261 return mmc_switch_part(mmc
, hwpart
);
264 static int mmc_blk_probe(struct udevice
*dev
)
266 struct udevice
*mmc_dev
= dev_get_parent(dev
);
267 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(mmc_dev
);
268 struct mmc
*mmc
= upriv
->mmc
;
273 debug("%s: mmc_init() failed (err=%d)\n", __func__
, ret
);
280 static const struct blk_ops mmc_blk_ops
= {
282 #ifndef CONFIG_SPL_BUILD
286 .select_hwpart
= mmc_select_hwpart
,
289 U_BOOT_DRIVER(mmc_blk
) = {
293 .probe
= mmc_blk_probe
,
295 #endif /* CONFIG_BLK */
297 U_BOOT_DRIVER(mmc
) = {
302 UCLASS_DRIVER(mmc
) = {
305 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
306 .per_device_auto_alloc_size
= sizeof(struct mmc_uclass_priv
),