Blame view

drivers/mmc/exynos_dw_mmc.c 7.49 KB
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
1
2
3
4
  /*
   * (C) Copyright 2012 SAMSUNG Electronics
   * Jaehoon Chung <jh80.chung@samsung.com>
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
6
7
8
   */
  
  #include <common.h>
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
9
  #include <dwmmc.h>
a082a2dde   Amar   EXYNOS5: DWMMC: A...
10
  #include <fdtdec.h>
b08c8c487   Masahiro Yamada   libfdt: move head...
11
  #include <linux/libfdt.h>
a082a2dde   Amar   EXYNOS5: DWMMC: A...
12
  #include <malloc.h>
ccd60a852   Jaehoon Chung   mmc: dw_mmc: remo...
13
  #include <errno.h>
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
14
15
  #include <asm/arch/dwmmc.h>
  #include <asm/arch/clk.h>
a082a2dde   Amar   EXYNOS5: DWMMC: A...
16
  #include <asm/arch/pinmux.h>
64029f7ae   Przemyslaw Marczak   mmc: exynos dwmmc...
17
  #include <asm/arch/power.h>
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
18
  #include <asm/gpio.h>
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
19

a082a2dde   Amar   EXYNOS5: DWMMC: A...
20
21
22
  #define	DWMMC_MAX_CH_NUM		4
  #define	DWMMC_MAX_FREQ			52000000
  #define	DWMMC_MIN_FREQ			400000
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
23
24
  #define	DWMMC_MMC0_SDR_TIMING_VAL	0x03030001
  #define	DWMMC_MMC2_SDR_TIMING_VAL	0x03020001
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
25
26
27
28
29
30
31
32
33
  #ifdef CONFIG_DM_MMC
  #include <dm.h>
  DECLARE_GLOBAL_DATA_PTR;
  
  struct exynos_mmc_plat {
  	struct mmc_config cfg;
  	struct mmc mmc;
  };
  #endif
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
34
35
  /* Exynos implmentation specific drver private data */
  struct dwmci_exynos_priv_data {
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
36
37
38
  #ifdef CONFIG_DM_MMC
  	struct dwmci_host host;
  #endif
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
39
40
  	u32 sdr_timing;
  };
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
41

a082a2dde   Amar   EXYNOS5: DWMMC: A...
42
43
44
45
  /*
   * Function used as callback function to initialise the
   * CLKSEL register for every mmc channel.
   */
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
46
47
  static void exynos_dwmci_clksel(struct dwmci_host *host)
  {
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
48
49
50
  	struct dwmci_exynos_priv_data *priv = host->priv;
  
  	dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
51
  }
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
52

e3563f2ec   Simon Glass   mmc: Support bypa...
53
  unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
a082a2dde   Amar   EXYNOS5: DWMMC: A...
54
  {
d3e016cc2   Rajeshwari S Shinde   MMC: DWMMC: Corre...
55
56
57
58
59
60
61
62
63
64
65
66
  	unsigned long sclk;
  	int8_t clk_div;
  
  	/*
  	 * Since SDCLKIN is divided inside controller by the DIVRATIO
  	 * value set in the CLKSEL register, we need to use the same output
  	 * clock value to calculate the CLKDIV value.
  	 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
  	 */
  	clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
  			& DWMCI_DIVRATIO_MASK) + 1;
  	sclk = get_mmc_clk(host->dev_index);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
67
68
69
70
71
  	/*
  	 * Assume to know divider value.
  	 * When clock unit is broken, need to set "host->div"
  	 */
  	return sclk / clk_div / (host->div + 1);
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
72
  }
18ab67559   Jaehoon Chung   mmc: dw_mmc: remo...
73
74
  static void exynos_dwmci_board_init(struct dwmci_host *host)
  {
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
75
  	struct dwmci_exynos_priv_data *priv = host->priv;
18ab67559   Jaehoon Chung   mmc: dw_mmc: remo...
76
77
78
79
80
81
82
83
84
  	if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
  		dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
  		dwmci_writel(host, EMMCP_SEND0, 0);
  		dwmci_writel(host, EMMCP_CTRL0,
  			     MPSCTRL_SECURE_READ_BIT |
  			     MPSCTRL_SECURE_WRITE_BIT |
  			     MPSCTRL_NON_SECURE_READ_BIT |
  			     MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
  	}
3a33bb187   Jaehoon Chung   mmc: exynos_dw_mm...
85

5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
86
87
  	/* Set to timing value at initial time */
  	if (priv->sdr_timing)
3a33bb187   Jaehoon Chung   mmc: exynos_dw_mm...
88
  		exynos_dwmci_clksel(host);
18ab67559   Jaehoon Chung   mmc: dw_mmc: remo...
89
  }
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
90
  static int exynos_dwmci_core_init(struct dwmci_host *host)
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
91
  {
a082a2dde   Amar   EXYNOS5: DWMMC: A...
92
93
  	unsigned int div;
  	unsigned long freq, sclk;
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
94
95
96
97
98
  
  	if (host->bus_hz)
  		freq = host->bus_hz;
  	else
  		freq = DWMMC_MAX_FREQ;
a082a2dde   Amar   EXYNOS5: DWMMC: A...
99
  	/* request mmc clock vlaue of 52MHz.  */
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
100
  	sclk = get_mmc_clk(host->dev_index);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
101
102
  	div = DIV_ROUND_UP(sclk, freq);
  	/* set the clock divisor for mmc */
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
103
  	set_mmc_clk(host->dev_index, div);
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
104

a082a2dde   Amar   EXYNOS5: DWMMC: A...
105
  	host->name = "EXYNOS DWMMC";
6f0b7caa6   Rajeshwari Shinde   DWMMC: SMDK5420: ...
106
107
108
  #ifdef CONFIG_EXYNOS5420
  	host->quirks = DWMCI_QUIRK_DISABLE_SMU;
  #endif
18ab67559   Jaehoon Chung   mmc: dw_mmc: remo...
109
  	host->board_init = exynos_dwmci_board_init;
a082a2dde   Amar   EXYNOS5: DWMMC: A...
110

e09bd8532   Jaehoon Chung   mmc: exynos_dw_mm...
111
  	host->caps = MMC_MODE_DDR_52MHz;
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
112
  	host->clksel = exynos_dwmci_clksel;
b44fe83a5   Jaehoon Chung   mmc: dw_mmc: chan...
113
  	host->get_mmc_clk = exynos_dwmci_get_clk;
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
114
115
  
  #ifndef CONFIG_DM_MMC
a082a2dde   Amar   EXYNOS5: DWMMC: A...
116
117
  	/* Add the mmc channel to be registered with mmc core */
  	if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
118
119
  		printf("DWMMC%d registration failed
  ", host->dev_index);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
120
121
  		return -1;
  	}
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
122
  #endif
a082a2dde   Amar   EXYNOS5: DWMMC: A...
123
124
  	return 0;
  }
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
125
126
127
  static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
  
  static int do_dwmci_init(struct dwmci_host *host)
a082a2dde   Amar   EXYNOS5: DWMMC: A...
128
  {
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
129
  	int flag, err;
a082a2dde   Amar   EXYNOS5: DWMMC: A...
130

959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
131
132
133
  	flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  	err = exynos_pinmux_config(host->dev_id, flag);
  	if (err) {
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
134
135
  		printf("DWMMC%d not configure
  ", host->dev_index);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
136
137
  		return err;
  	}
a082a2dde   Amar   EXYNOS5: DWMMC: A...
138

d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
139
  	return exynos_dwmci_core_init(host);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
140
  }
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
141

959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
142
143
144
145
  static int exynos_dwmci_get_config(const void *blob, int node,
  					struct dwmci_host *host)
  {
  	int err = 0;
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
146
147
148
149
150
  	u32 base, timing[3];
  	struct dwmci_exynos_priv_data *priv;
  
  	priv = malloc(sizeof(struct dwmci_exynos_priv_data));
  	if (!priv) {
9b643e312   Masahiro Yamada   treewide: replace...
151
152
  		pr_err("dwmci_exynos_priv_data malloc fail!
  ");
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
153
154
  		return -ENOMEM;
  	}
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
155

959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
156
157
  	/* Extract device id for each mmc channel */
  	host->dev_id = pinmux_decode_periph_id(blob, node);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
158

dfcb683a3   Jaehoon Chung   mmc: exynos_dw-mm...
159
160
161
  	host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
  	if (host->dev_index == host->dev_id)
  		host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
ce757b18f   Jaehoon Chung   mmc: exynos_dw_mm...
162
163
164
  	if (host->dev_index > 4) {
  		printf("DWMMC%d: Can't get the dev index
  ", host->dev_index);
0e1746aca   Suniel Mahesh   drivers: mmc: Avo...
165
  		free(priv);
ce757b18f   Jaehoon Chung   mmc: exynos_dw_mm...
166
167
  		return -EINVAL;
  	}
70f6d3943   Jaehoon Chung   mmc: exynos_dw_mm...
168
169
  	/* Get the bus width from the device node (Default is 4bit buswidth) */
  	host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 4);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
170

959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
171
172
173
  	/* Set the base address from the device node */
  	base = fdtdec_get_addr(blob, node, "reg");
  	if (!base) {
dfcb683a3   Jaehoon Chung   mmc: exynos_dw-mm...
174
175
  		printf("DWMMC%d: Can't get base address
  ", host->dev_index);
0e1746aca   Suniel Mahesh   drivers: mmc: Avo...
176
  		free(priv);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
177
178
179
180
181
182
183
  		return -EINVAL;
  	}
  	host->ioaddr = (void *)base;
  
  	/* Extract the timing info from the node */
  	err =  fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
  	if (err) {
dfcb683a3   Jaehoon Chung   mmc: exynos_dw-mm...
184
185
186
  		printf("DWMMC%d: Can't get sdr-timings for devider
  ",
  				host->dev_index);
0e1746aca   Suniel Mahesh   drivers: mmc: Avo...
187
  		free(priv);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
188
189
  		return -EINVAL;
  	}
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
190
  	priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
191
192
  			DWMCI_SET_DRV_CLK(timing[1]) |
  			DWMCI_SET_DIV_RATIO(timing[2]));
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
193
194
195
196
197
198
199
200
  
  	/* sdr_timing didn't assigned anything, use the default value */
  	if (!priv->sdr_timing) {
  		if (host->dev_index == 0)
  			priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
  		else if (host->dev_index == 2)
  			priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
  	}
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
201
202
203
204
  
  	host->fifoth_val = fdtdec_get_int(blob, node, "fifoth_val", 0);
  	host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
  	host->div = fdtdec_get_int(blob, node, "div", 0);
5dab81cea   Jaehoon Chung   mmc: exynos_dw_mm...
205
  	host->priv = priv;
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	return 0;
  }
  
  static int exynos_dwmci_process_node(const void *blob,
  					int node_list[], int count)
  {
  	struct dwmci_host *host;
  	int i, node, err;
  
  	for (i = 0; i < count; i++) {
  		node = node_list[i];
  		if (node <= 0)
  			continue;
  		host = &dwmci_host[i];
  		err = exynos_dwmci_get_config(blob, node, host);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
221
  		if (err) {
dfcb683a3   Jaehoon Chung   mmc: exynos_dw-mm...
222
223
  			printf("%s: failed to decode dev %d
  ", __func__, i);
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
224
  			return err;
a082a2dde   Amar   EXYNOS5: DWMMC: A...
225
  		}
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
226
  		do_dwmci_init(host);
a082a2dde   Amar   EXYNOS5: DWMMC: A...
227
  	}
d0ebbb8df   Jaehoon Chung   EXYNOS: mmc: supp...
228
229
  	return 0;
  }
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
230
231
232
  
  int exynos_dwmmc_init(const void *blob)
  {
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
233
  	int node_list[DWMMC_MAX_CH_NUM];
64029f7ae   Przemyslaw Marczak   mmc: exynos dwmmc...
234
  	int boot_dev_node;
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
235
  	int err = 0, count;
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
236
  	count = fdtdec_find_aliases_for_id(blob, "mmc",
d956a67ed   Jaehoon Chung   mmc: exynos_dw_mm...
237
238
  			COMPAT_SAMSUNG_EXYNOS_DWMMC, node_list,
  			DWMMC_MAX_CH_NUM);
64029f7ae   Przemyslaw Marczak   mmc: exynos dwmmc...
239
240
241
242
243
244
245
  
  	/* For DWMMC always set boot device as mmc 0 */
  	if (count >= 3 && get_boot_mode() == BOOT_MODE_SD) {
  		boot_dev_node = node_list[2];
  		node_list[2] = node_list[0];
  		node_list[0] = boot_dev_node;
  	}
959198f7c   Jaehoon Chung   mmc: exynos_dw_mm...
246
247
248
249
  	err = exynos_dwmci_process_node(blob, node_list, count);
  
  	return err;
  }
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
250
251
252
253
254
255
256
257
258
  
  #ifdef CONFIG_DM_MMC
  static int exynos_dwmmc_probe(struct udevice *dev)
  {
  	struct exynos_mmc_plat *plat = dev_get_platdata(dev);
  	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  	struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
  	struct dwmci_host *host = &priv->host;
  	int err;
e160f7d43   Simon Glass   dm: core: Replace...
259
  	err = exynos_dwmci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
260
261
262
263
264
  	if (err)
  		return err;
  	err = do_dwmci_init(host);
  	if (err)
  		return err;
e5113c333   Jaehoon Chung   mmc: dw_mmc: remo...
265
  	dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
266
267
268
269
270
271
272
273
274
275
276
  	host->mmc = &plat->mmc;
  	host->mmc->priv = &priv->host;
  	host->priv = dev;
  	upriv->mmc = host->mmc;
  
  	return dwmci_probe(dev);
  }
  
  static int exynos_dwmmc_bind(struct udevice *dev)
  {
  	struct exynos_mmc_plat *plat = dev_get_platdata(dev);
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
277

24f5aec36   Masahiro Yamada   mmc: squash lines...
278
  	return dwmci_bind(dev, &plat->mmc, &plat->cfg);
3537ee879   Jaehoon Chung   mmc: exynos_dw_mm...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  }
  
  static const struct udevice_id exynos_dwmmc_ids[] = {
  	{ .compatible = "samsung,exynos4412-dw-mshc" },
  	{ }
  };
  
  U_BOOT_DRIVER(exynos_dwmmc_drv) = {
  	.name		= "exynos_dwmmc",
  	.id		= UCLASS_MMC,
  	.of_match	= exynos_dwmmc_ids,
  	.bind		= exynos_dwmmc_bind,
  	.ops		= &dm_dwmci_ops,
  	.probe		= exynos_dwmmc_probe,
  	.priv_auto_alloc_size	= sizeof(struct dwmci_exynos_priv_data),
  	.platdata_auto_alloc_size = sizeof(struct exynos_mmc_plat),
  };
  #endif