xref: /openbmc/u-boot/drivers/mmc/uniphier-sd.c (revision 57efeb04)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Socionext Inc.
4  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5  */
6 
7 #include <common.h>
8 #include <clk.h>
9 #include <fdtdec.h>
10 #include <mmc.h>
11 #include <dm.h>
12 #include <linux/compat.h>
13 #include <linux/dma-direction.h>
14 #include <linux/io.h>
15 #include <linux/sizes.h>
16 #include <power/regulator.h>
17 #include <asm/unaligned.h>
18 
19 #include "tmio-common.h"
20 
21 static const struct dm_mmc_ops uniphier_sd_ops = {
22 	.send_cmd = tmio_sd_send_cmd,
23 	.set_ios = tmio_sd_set_ios,
24 	.get_cd = tmio_sd_get_cd,
25 };
26 
27 static const struct udevice_id uniphier_sd_match[] = {
28 	{ .compatible = "socionext,uniphier-sd-v2.91" },
29 	{ .compatible = "socionext,uniphier-sd-v3.1" },
30 	{ .compatible = "socionext,uniphier-sd-v3.1.1" },
31 	{ /* sentinel */ }
32 };
33 
34 static ulong uniphier_sd_clk_get_rate(struct tmio_sd_priv *priv)
35 {
36 #if CONFIG_IS_ENABLED(CLK)
37 	return clk_get_rate(&priv->clk);
38 #elif CONFIG_SPL_BUILD
39 	return 100000000;
40 #else
41 	return 0;
42 #endif
43 }
44 
45 static int uniphier_sd_probe(struct udevice *dev)
46 {
47 	struct tmio_sd_priv *priv = dev_get_priv(dev);
48 
49 	priv->clk_get_rate = uniphier_sd_clk_get_rate;
50 	priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
51 
52 #ifndef CONFIG_SPL_BUILD
53 	int ret;
54 
55 	ret = clk_get_by_index(dev, 0, &priv->clk);
56 	if (ret < 0) {
57 		dev_err(dev, "failed to get host clock\n");
58 		return ret;
59 	}
60 
61 	/* set to max rate */
62 	ret = clk_set_rate(&priv->clk, ULONG_MAX);
63 	if (ret < 0) {
64 		dev_err(dev, "failed to set rate for host clock\n");
65 		clk_free(&priv->clk);
66 		return ret;
67 	}
68 
69 	ret = clk_enable(&priv->clk);
70 	if (ret) {
71 		dev_err(dev, "failed to enable host clock\n");
72 		return ret;
73 	}
74 #endif
75 
76 	return tmio_sd_probe(dev, 0);
77 }
78 
79 U_BOOT_DRIVER(uniphier_mmc) = {
80 	.name = "uniphier-mmc",
81 	.id = UCLASS_MMC,
82 	.of_match = uniphier_sd_match,
83 	.bind = tmio_sd_bind,
84 	.probe = uniphier_sd_probe,
85 	.priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
86 	.platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
87 	.ops = &uniphier_sd_ops,
88 };
89