summaryrefslogtreecommitdiffstats
path: root/meta-agl-bsp/meta-renesas/recipes-kernel/linux/linux/hibernation/0008-Add-rcar-mmc-hibernation-code.patch
blob: 34b40a14715a16724024784b9da2a8f0f42de037 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
From 9d1d9be70ed3cf6670ae12a1caed337833f7bba8 Mon Sep 17 00:00:00 2001
From: Yuichi Kusakabe <yuichi.kusakabe@jp.fujitsu.com>
Date: Thu, 18 May 2017 17:38:11 +0900
Subject: [PATCH 08/15] Add rcar mmc hibernation code

Signed-off-by: Yuichi Kusakabe <yuichi.kusakabe@jp.fujitsu.com>
---
 drivers/mmc/host/sh_mmcif.c       |  65 +++++++++++++++++++++-
 drivers/mmc/host/sh_mobile_sdhi.c | 112 +++++++++++++++++++++++++++++++++++++-
 drivers/mmc/host/tmio_mmc.h       |   1 +
 drivers/mmc/host/tmio_mmc_pio.c   |  49 ++++++++++++-----
 4 files changed, 210 insertions(+), 17 deletions(-)

diff --git a/drivers/mmc/host/sh_mmcif.c b/drivers/mmc/host/sh_mmcif.c
index 7290e6e..4ecf62c 100644
--- a/drivers/mmc/host/sh_mmcif.c
+++ b/drivers/mmc/host/sh_mmcif.c
@@ -232,6 +232,7 @@ struct sh_mmcif_host {
 	struct platform_device *pd;
 	struct clk *hclk;
 	unsigned int clk;
+	int clkrate;
 	int bus_width;
 	unsigned char timing;
 	bool sd_error;
@@ -257,6 +258,8 @@ struct sh_mmcif_host {
 	struct dma_chan		*chan_tx;
 	struct completion	dma_complete;
 	bool			dma_active;
+#define N_REGS		10
+	u32			regs[N_REGS];
 };
 
 static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
@@ -1457,6 +1460,8 @@ static int sh_mmcif_probe(struct platform_device *pdev)
 		}
 	}
 
+	host->clkrate = clk_get_rate(host->hclk);
+
 	ret = sh_mmcif_clk_update(host);
 	if (ret < 0)
 		goto eclkupdate;
@@ -1503,6 +1508,7 @@ static int sh_mmcif_probe(struct platform_device *pdev)
 	dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
 	dev_dbg(&pdev->dev, "chip ver H'%04x\n",
 		sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
+	device_enable_async_suspend(&pdev->dev);
 	return ret;
 
 emmcaddh:
@@ -1574,15 +1580,68 @@ static int sh_mmcif_suspend(struct device *dev)
 	sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
 	pm_runtime_put(dev);
 
-	return 0;
+	return mmc_suspend_host(host->mmc);
 }
 
 static int sh_mmcif_resume(struct device *dev)
 {
-	return 0;
+	struct sh_mmcif_host *host = dev_get_drvdata(dev);
+	return mmc_resume_host(host->mmc);
+}
+#endif
+
+#ifdef CONFIG_PM
+static int sh_mmcif_restore(struct device *dev)
+{
+	struct sh_mmcif_host *host = dev_get_drvdata(dev);
+	int ret;
+	ret = clk_set_rate(host->hclk, host->clkrate);
+	if (ret < 0)
+		goto eclkupdate;
+	ret = sh_mmcif_clk_update(host);
+	if (ret < 0)
+		goto eclkupdate;
+	ret = pm_runtime_resume(dev);
+	if (ret < 0)
+		goto eresume;
+	sh_mmcif_sync_reset(host);
+#ifdef CONFIG_MACH_FTEN
+	sh_mmcif_writel(host->addr, 0x00000080, 0x00000100);
+#endif
+	sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
+	clk_disable_unprepare(host->hclk);
+	dev_info(dev, "restore: chip ver H'%04x\n",
+		sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
+	sh_mmcif_writel(host->addr, MMCIF_CE_CMD_CTRL, host->regs[0]);
+	sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, host->regs[1]);
+	sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL, host->regs[2]);
+	sh_mmcif_writel(host->addr, MMCIF_CE_BUF_ACC, host->regs[3]);
+	sh_mmcif_release_dma(host);
+	return mmc_resume_host(host->mmc);
+eclkupdate:
+	pr_info("Can't set clock\n");
+	return -EINVAL;
+eresume:
+	pr_info("Can't resume PM\n");
+	return -ENODEV;
 }
+
+static int sh_mmcif_freeze(struct device *dev)
+{
+	struct sh_mmcif_host *host = dev_get_drvdata(dev);
+	int ret = mmc_suspend_host(host->mmc);
+	host->regs[0] = sh_mmcif_readl(host->addr, MMCIF_CE_CMD_CTRL);
+	host->regs[1] = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET);
+	host->regs[2] = sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
+	host->regs[3] = sh_mmcif_readl(host->addr, MMCIF_CE_BUF_ACC);
+	return ret;
+}
+#else
+#define sh_mmcif_restore NULL
+#define sh_mmcif_freeze NULL
 #endif
 
+
 static const struct of_device_id mmcif_of_match[] = {
 	{ .compatible = "renesas,sh-mmcif" },
 	{ }
@@ -1591,6 +1650,8 @@ MODULE_DEVICE_TABLE(of, mmcif_of_match);
 
 static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume)
+	.restore = sh_mmcif_restore,
+	.freeze = sh_mmcif_freeze,
 };
 
 static struct platform_driver sh_mmcif_driver = {
diff --git a/drivers/mmc/host/sh_mobile_sdhi.c b/drivers/mmc/host/sh_mobile_sdhi.c
index 1b59cdf..c7f3abf 100644
--- a/drivers/mmc/host/sh_mobile_sdhi.c
+++ b/drivers/mmc/host/sh_mobile_sdhi.c
@@ -156,6 +156,8 @@ struct sh_mobile_sdhi {
 	struct tmio_mmc_dma dma_priv;
 	unsigned int type;
 	struct sh_mobile_sdhi_vlt vlt;
+	int wifi_xrst;
+	int save_clk_rate;
 };
 
 static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
@@ -647,6 +649,7 @@ static int sh_mobile_sdhi_probe(struct platform_device *pdev)
 			if (ret < 0)
 				dev_err(&pdev->dev,
 					"cannot set clock rate: %d\n", ret);
+			priv->save_clk_rate = clk_rate;
 
 			clk_disable_unprepare(priv->clk);
 		}
@@ -841,6 +844,7 @@ static int sh_mobile_sdhi_probe(struct platform_device *pdev)
 		}
 	}
 
+	device_enable_async_suspend(&pdev->dev);
 	dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
 		 mmc_hostname(host->mmc), (unsigned long)
 		 (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
@@ -865,17 +869,123 @@ static int sh_mobile_sdhi_remove(struct platform_device *pdev)
 	struct mmc_host *mmc = platform_get_drvdata(pdev);
 	struct tmio_mmc_host *host = mmc_priv(mmc);
 	struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
+#ifdef CONFIG_MACH_FTEN_DT
+	int ret;
+	struct sh_mobile_sdhi *priv = container_of(host->pdata,
+						   struct sh_mobile_sdhi,
+						   mmc_data);
+#endif
 
 	tmio_mmc_host_remove(host);
 
 	if (p && p->cleanup)
 		p->cleanup(pdev);
 
+#ifdef CONFIG_MACH_FTEN_DT
+	ret = gpio_request(priv->wifi_xrst, "sh_mobile_sdhi");
+	if (ret != 0) {
+		dev_err(&pdev->dev,
+			"gpio_request(%d) failed(%d) remove\n",
+			priv->wifi_xrst, ret);
+		goto skip_wifi;
+	}
+	ret = gpio_direction_output(priv->wifi_xrst, 0);
+	if (ret != 0) {
+		dev_err(&pdev->dev,
+			"gpio_direction_output(%d) failed(%d) remove\n",
+			priv->wifi_xrst, ret);
+	}
+	gpio_free(priv->wifi_xrst);
+skip_wifi:
+#endif
+
+	return 0;
+}
+
+static int sh_mobile_sdhi_restore_noirq(struct device *dev)
+{
+	struct mmc_host *mmc = dev_get_drvdata(dev);
+	struct tmio_mmc_host *host = mmc_priv(mmc);
+
+	sd_ctrl_write32(host, CTL_IRQ_MASK, 0x8b7f031d);
+	sd_ctrl_write32(host, CTL_STATUS, 0);
+#if 0
+	sh_mobile_sdhi_enable_sdbuf_acc32(host, false);
+	/* FIXME - should we set stop clock reg here */
+	sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
+	/* implicit BUG_ON(!res) */
+	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
+		sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
+	msleep(2);
+	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
+	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
+		sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
+	msleep(2);
+	sd_ctrl_write32(host, CTL_IRQ_MASK, 0x8b7f031d);
+	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0040);
+	sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80E0);
+	sd_ctrl_write16(host, CTL_DMA_ENABLE, 0x1002);
+#endif
+	return 0;
+}
+
+static int sh_mobile_sdhi_restore(struct device *dev)
+{
+	struct mmc_host *mmc = dev_get_drvdata(dev);
+	struct tmio_mmc_host *host = mmc_priv(mmc);
+	struct sh_mobile_sdhi *priv = container_of(host->pdata,
+						   struct sh_mobile_sdhi,
+						   mmc_data);
+#if defined(CONFIG_MACH_FTEN_DT) || defined(CONFIG_PM_SLEEP)
+	int ret;
+#endif
+	int dma_size;
+	host->restore = true;
+
+#ifdef CONFIG_MACH_FTEN_DT
+	/* priv->wifi_xrst is 0 or more. */
+	if (priv->wifi_xrst >= 0) {
+		ret = gpio_request(priv->wifi_xrst, "sh_mobile_sdhi");
+		if (ret != 0) {
+			dev_err(dev, "gpio_request(%d) failed(%d) restore\n",
+				priv->wifi_xrst, ret);
+			goto skip_wifi;
+		}
+		ret = gpio_direction_output(priv->wifi_xrst, 1);
+		if (ret != 0) {
+			dev_err(dev, "gpio_direction_output(%d) failed(%d) restore\n",
+				priv->wifi_xrst, ret);
+		}
+		gpio_free(priv->wifi_xrst);
+	}
+skip_wifi:
+#endif
+
+	dma_size = sh_mobile_sdhi_get_xmit_size(priv->type,
+				priv->dma_priv.alignment_shift);
+
+	sd_ctrl_write16(host, SD_DMACR(priv->type), dma_size);
+
+#ifdef CONFIG_PM_SLEEP
+	ret = tmio_mmc_host_resume(dev);
+	host->restore = false;
+	return ret;
+#else
+	host->restore = false;
 	return 0;
+#endif
 }
 
 static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
-	SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_host_suspend, tmio_mmc_host_resume)
+#ifdef CONFIG_PM_SLEEP
+	.suspend	= tmio_mmc_host_suspend,
+	.resume		= tmio_mmc_host_resume,
+	.freeze		= tmio_mmc_host_suspend,
+	.thaw		= tmio_mmc_host_resume,
+	.poweroff	= tmio_mmc_host_suspend,
+#endif
+	.restore	= sh_mobile_sdhi_restore,
+	.restore_noirq	= sh_mobile_sdhi_restore_noirq,
 	SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
 			tmio_mmc_host_runtime_resume,
 			NULL)
diff --git a/drivers/mmc/host/tmio_mmc.h b/drivers/mmc/host/tmio_mmc.h
index c5b12ad..3efe03d 100644
--- a/drivers/mmc/host/tmio_mmc.h
+++ b/drivers/mmc/host/tmio_mmc.h
@@ -104,6 +104,7 @@ struct tmio_mmc_host {
 	bool			resuming;
 	bool			done_tuning;
 	struct completion	completion;
+	bool			restore;
 };
 
 int tmio_mmc_host_probe(struct tmio_mmc_host **host,
diff --git a/drivers/mmc/host/tmio_mmc_pio.c b/drivers/mmc/host/tmio_mmc_pio.c
index 09c0c08..514af15 100644
--- a/drivers/mmc/host/tmio_mmc_pio.c
+++ b/drivers/mmc/host/tmio_mmc_pio.c
@@ -167,8 +167,20 @@ static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
 	if (host->set_clk_div)
 		host->set_clk_div(host->pdev, (clk>>22) & 1);
 
+#ifdef CONFIG_MACH_FTEN
+	clk |= sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL) & 0x0100;
+	if (host->pdata->flags & TMIO_MMC_SDCLK_AUTO_CONTROL &&
+	    new_clock > host->mmc->f_init)
+		clk |= SDCLKOFFEN;
+	dev_dbg(&host->pdev->dev,
+		"clock=%d, clk=%08x, new_clock=%d, f_init=%d\n",
+		clock, clk, new_clock, host->mmc->f_init);
+	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x3ff);
+#else
 	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
-	msleep(10);
+#endif
+	if (!host->restore)
+		msleep(2);
 }
 
 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
@@ -176,13 +188,15 @@ static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
 	/* implicit BUG_ON(!res) */
 	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
 		sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
-		if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP))
+		if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP)
+			&& !host->restore)
 			msleep(10);
 	}
 
 	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
 		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
-	if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP))
+	if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP)
+		&& !host->restore)
 		msleep(10);
 }
 
@@ -190,14 +204,16 @@ static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
 {
 	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
 		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
-	if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP))
-		msleep(10);
+	if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP)
+		&& !host->restore)
+		msleep(2);
 
 	/* implicit BUG_ON(!res) */
 	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
 		sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
-		if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP))
-			msleep(10);
+		if (!(host->pdata->flags & TMIO_MMC_CLK_NO_SLEEP)
+			&& !host->restore)
+			msleep(2);
 	}
 }
 
@@ -208,11 +224,11 @@ static void tmio_mmc_reset(struct tmio_mmc_host *host)
 	/* implicit BUG_ON(!res) */
 	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
 		sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
-	msleep(10);
+	msleep(2);
 	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
 	if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
 		sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
-	msleep(10);
+	msleep(2);
 }
 
 static void tmio_mmc_reset_work(struct work_struct *work)
@@ -1134,16 +1150,21 @@ static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 	 * is kept positive, so no suspending actually takes place.
 	 */
 	if (ios->power_mode == MMC_POWER_ON && ios->clock) {
+		int reset_needed = 0;
 		if (host->power != TMIO_MMC_ON_RUN) {
 			tmio_mmc_clk_update(mmc);
 			pm_runtime_get_sync(dev);
-			if (host->resuming) {
-				tmio_mmc_reset(host);
-				host->resuming = false;
-			}
+			if (host->resuming)
+				reset_needed = 1;
 		}
+
 		if (host->power == TMIO_MMC_OFF_STOP)
+			reset_needed = 1;
+		if (reset_needed) {
 			tmio_mmc_reset(host);
+			if (host->resuming)
+				host->resuming = false;
+		}
 		tmio_mmc_set_clock(host, ios->clock);
 		if (host->power == TMIO_MMC_OFF_STOP)
 			/* power up SD card and the bus */
@@ -1497,7 +1518,7 @@ int tmio_mmc_host_resume(struct device *dev)
 
 	/* The MMC core will perform the complete set up */
 	host->resuming = true;
-	return mmc_resume_host(mmc);
+	return  mmc_resume_host(mmc);
 }
 EXPORT_SYMBOL(tmio_mmc_host_resume);
 #endif
-- 
1.8.3.1