summaryrefslogtreecommitdiff
path: root/drivers/mmc/f_sdh30.c
blob: 3d587a464d5057c9d609c437ef77fb951ee98e66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0+
/*
 * Socionext F_SDH30 eMMC driver
 * Copyright 2021 Linaro Ltd.
 * Copyright 2021 Socionext, Inc.
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <malloc.h>
#include <sdhci.h>

#define F_SDH30_ESD_CONTROL		0x124
#define F_SDH30_CMD_DAT_DELAY		BIT(9)

#define F_SDH30_TEST			0x158
#define F_SDH30_FORCE_CARD_INSERT	BIT(6)

struct f_sdh30_data {
	void (*init)(struct udevice *dev);
	u32 quirks;
};

struct f_sdh30_plat {
	struct mmc_config cfg;
	struct mmc mmc;

	bool enable_cmd_dat_delay;
	const struct f_sdh30_data *data;
};

DECLARE_GLOBAL_DATA_PTR;

static void f_sdh30_e51_init(struct udevice *dev)
{
	struct f_sdh30_plat *plat = dev_get_plat(dev);
	struct sdhci_host *host = dev_get_priv(dev);
	u32 val;

	val = sdhci_readl(host, F_SDH30_ESD_CONTROL);
	if (plat->enable_cmd_dat_delay)
		val |= F_SDH30_CMD_DAT_DELAY;
	else
		val &= ~F_SDH30_CMD_DAT_DELAY;
	sdhci_writel(host, val, F_SDH30_ESD_CONTROL);

	val = sdhci_readl(host, F_SDH30_TEST);
	if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
		val |= F_SDH30_FORCE_CARD_INSERT;
	else
		val &= ~F_SDH30_FORCE_CARD_INSERT;
	sdhci_writel(host, val, F_SDH30_TEST);
}

static int f_sdh30_sdhci_probe(struct udevice *dev)
{
	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
	struct f_sdh30_plat *plat = dev_get_plat(dev);
	struct sdhci_host *host = dev_get_priv(dev);
	int ret;

	plat->data = (const struct f_sdh30_data *)dev_get_driver_data(dev);

	ret = mmc_of_parse(dev, &plat->cfg);
	if (ret)
		return ret;

	host->mmc = &plat->mmc;
	host->mmc->dev = dev;
	host->mmc->priv = host;

	if (plat->data && plat->data->quirks)
		host->quirks = plat->data->quirks;

	ret = sdhci_setup_cfg(&plat->cfg, host, 200000000, 400000);
	if (ret)
		return ret;

	upriv->mmc = host->mmc;

	mmc_set_clock(host->mmc, host->mmc->cfg->f_min, MMC_CLK_ENABLE);

	ret = sdhci_probe(dev);
	if (ret)
		return ret;

	if (plat->data && plat->data->init)
		plat->data->init(dev);

	return 0;
}

static int f_sdh30_of_to_plat(struct udevice *dev)
{
	struct sdhci_host *host = dev_get_priv(dev);
	struct f_sdh30_plat *plat = dev_get_plat(dev);

	host->name = strdup(dev->name);
	host->ioaddr = dev_read_addr_ptr(dev);
	host->bus_width = dev_read_u32_default(dev, "bus-width", 4);
	host->index = dev_read_u32_default(dev, "index", 0);

	plat->enable_cmd_dat_delay =
		dev_read_bool(dev, "socionext,enable-cmd-dat-delay");

	return 0;
}

static int f_sdh30_bind(struct udevice *dev)
{
	struct f_sdh30_plat *plat = dev_get_plat(dev);

	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
}

static const struct f_sdh30_data f_sdh30_e51_data = {
	.init = f_sdh30_e51_init,
	.quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_SUPPORT_SINGLE,
};

static const struct udevice_id f_sdh30_mmc_ids[] = {
	{
		.compatible = "fujitsu,mb86s70-sdhci-3.0",
	},
	{
		.compatible = "socionext,f-sdh30-e51-mmc",
		.data = (ulong)&f_sdh30_e51_data,
	},
	{ }
};

U_BOOT_DRIVER(f_sdh30_drv) = {
	.name		= "f_sdh30_sdhci",
	.id		= UCLASS_MMC,
	.of_match	= f_sdh30_mmc_ids,
	.of_to_plat	= f_sdh30_of_to_plat,
	.ops		= &sdhci_ops,
	.bind		= f_sdh30_bind,
	.probe		= f_sdh30_sdhci_probe,
	.priv_auto	= sizeof(struct sdhci_host),
	.plat_auto	= sizeof(struct f_sdh30_plat),
};