summaryrefslogtreecommitdiffstats
path: root/meta-rcar-gen3-adas/recipes-kernel/linux/linux-renesas/0124-RPC-Hyperflash-Add-devicetree-support.patch
blob: f347d4754254f5ac4352ddf77ba7b57965b99341 (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
From 466608ee2217ce02df6df977a35c4afa6ee1e350 Mon Sep 17 00:00:00 2001
From: Dmitry Shifrin <dmitry.shifrin@cogentembedded.com>
Date: Wed, 4 Apr 2018 12:29:47 +0300
Subject: [PATCH 1/2] RPC: Hyperflash: Add devicetree support

Signed-off-by: Dmitry Shifrin <dmitry.shifrin@cogentembedded.com>
---
 drivers/mtd/rpc_hyperflash.c | 142 +++++++++++++++++++++++--------------------
 1 file changed, 77 insertions(+), 65 deletions(-)

diff --git a/drivers/mtd/rpc_hyperflash.c b/drivers/mtd/rpc_hyperflash.c
index cf4d56e..c15e520 100644
--- a/drivers/mtd/rpc_hyperflash.c
+++ b/drivers/mtd/rpc_hyperflash.c
@@ -15,8 +15,10 @@
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/partitions.h>
 #include <linux/of.h>
+#include <linux/clk.h>
 #include <linux/rwsem.h>
 #include <linux/slab.h>
+#include <linux/platform_device.h>
 
 /* RPC */
 #define RPC_BASE		0xEE200000
@@ -156,10 +158,9 @@
 
 struct rpc_info {
 	struct rw_semaphore lock;
+	struct clk *clk;
 	void __iomem *rpc_base;
 	void __iomem *flash_base;
-	struct resource *rpc_res;
-	struct resource *flash_res;
 	u32 flash_id;
 	struct mtd_info mtd;
 };
@@ -243,8 +244,6 @@ enum rpc_hf_size {
 	RPC_HF_SIZE_64BIT = RPC_SMENR_SPIDE(0xF),
 };
 
-struct rpc_info *rpc_info;
-
 static void rpc_hf_mode_man(struct rpc_info *info)
 {
 	rpc_wait_tend(info);
@@ -861,8 +860,8 @@ static int rpc_hf_init_mtd(struct rpc_info *info)
 	rpc_hf_read_reg(info, 0x27 << 1, data, RPC_HF_SIZE_16BIT);
 	size = 1 << data[0];
 
-	if (size > resource_size(info->flash_res))
-		size = resource_size(info->flash_res);
+	if (size > RPC_FLASH_SIZE)
+		size = RPC_FLASH_SIZE;
 
 	if (size & (RPC_HF_ERASE_SIZE - 1)) {
 		retval = -EINVAL;
@@ -891,86 +890,99 @@ static int rpc_hf_init_mtd(struct rpc_info *info)
 	return retval;
 }
 
-static int rpc_flash_init(void)
+static int rpc_flash_probe(struct platform_device *pdev)
 {
-	struct rpc_info *info;
+	struct rpc_info *rpc;
 	struct resource *res;
-	void __iomem *base;
-	int retval = -ENODEV;
-
-	if (!of_machine_is_compatible("renesas,r8a7795"))
-		return -ENODEV;
+	int ret;
 
-	info = kzalloc(sizeof(*info), GFP_KERNEL);
-	if (!info)
+	rpc = devm_kzalloc(&pdev->dev, sizeof(*rpc), GFP_KERNEL);
+	if (!rpc)
 		return -ENOMEM;
 
-	res = request_mem_region(RPC_BASE, RPC_SIZE, "RPC");
-	if (!res)
-		goto out_info;
+	/* ...get memory */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	rpc->rpc_base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(rpc->rpc_base)) {
+		dev_err(&pdev->dev, "cannot get resources\n");
+		ret = PTR_ERR(rpc->rpc_base);
+		goto error;
+	}
 
-	info->rpc_res = res;
-	base = ioremap(res->start, resource_size(res));
-	if (!base)
-		goto out_rpc_res;
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	rpc->flash_base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(rpc->flash_base)) {
+		dev_err(&pdev->dev, "cannot get resources\n");
+		ret = PTR_ERR(rpc->flash_base);
+		goto error;
+	}
 
-	info->rpc_base = base;
-	res = request_mem_region(RPC_FLASH_BASE, RPC_FLASH_SIZE, "RPC-ext");
-	if (!res)
-		goto out_rpc_base;
+	/* ...get clk */
+	rpc->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(rpc->clk)) {
+		dev_err(&pdev->dev, "cannot get clock\n");
+		ret = PTR_ERR(rpc->clk);
+		goto error;
+	}
 
-	info->flash_res = res;
-	base = ioremap(res->start, resource_size(res));
-	if (!base)
-		goto out_flash_res;
+	/* ... enable clk */
+	ret = clk_prepare_enable(rpc->clk);
+	if (ret) {
+		dev_err(&pdev->dev, "cannot prepare clock\n");
+		goto error;
+	}
+
+	platform_set_drvdata(pdev, rpc);
 
-	info->flash_base = base;
-	retval = rpc_hf_init_mtd(info);
-	if (retval)
-		goto out_flash_base;
+	mtd_set_of_node(&rpc->mtd, pdev->dev.of_node);
+	ret = rpc_hf_init_mtd(rpc);
+	if (ret) {
+		dev_err(&pdev->dev, "mtd device register error.\n");
+		goto error_clk_disable;
+	}
 
-	pr_info("HyperFlash Id: %x\n", info->flash_id);
+	dev_info(&pdev->dev, "HyperFlash Id: %x\n", rpc->flash_id);
 
-	rpc_info = info;
 	return 0;
 
-out_flash_base:
-	iounmap(info->flash_base);
-out_flash_res:
-	release_mem_region(info->flash_res->start,
-			   resource_size(info->flash_res));
-out_rpc_base:
-	iounmap(info->rpc_base);
-out_rpc_res:
-	release_mem_region(info->rpc_res->start,
-			   resource_size(info->rpc_res));
-out_info:
-	kfree(info);
-	return retval;
+
+error_clk_disable:
+	clk_disable_unprepare(rpc->clk);
+error:
+	return ret;
 }
 
-static void rpc_flash_exit(void)
+static int rpc_flash_exit(struct platform_device *pdev)
 {
-	struct rpc_info *info = rpc_info;
+	struct rpc_info *rpc = platform_get_drvdata(pdev);
+
+	/* HW shutdown */
+	clk_disable_unprepare(rpc->clk);
+	mtd_device_unregister(&rpc->mtd);
+	return 0;
+}
 
-	if (!info)
-		return;
 
-	rpc_info = NULL;
+static const struct of_device_id rpc_flash_of_match[] = {
+	{ .compatible = "renesas,rpc-hyperflash-r8a7798" },
+	{ .compatible = "renesas,rpc-hyperflash-r8a7797" },
+	{ },
+};
 
-	mtd_device_unregister(&info->mtd);
+MODULE_DEVICE_TABLE(of, rpc_flash_of_match);
 
-	iounmap(info->flash_base);
-	release_mem_region(info->flash_res->start,
-			   resource_size(info->flash_res));
-	iounmap(info->rpc_base);
-	release_mem_region(info->rpc_res->start,
-			   resource_size(info->rpc_res));
-	kfree(info);
-}
+/* platform driver interface */
+static struct platform_driver rpc_flash_platform_driver = {
+	.probe		= rpc_flash_probe,
+	.remove		= rpc_flash_exit,
+	.driver		= {
+		.owner	= THIS_MODULE,
+		.name	= "rpc",
+		.of_match_table = of_match_ptr(rpc_flash_of_match),
+	},
+};
 
-module_init(rpc_flash_init);
-module_exit(rpc_flash_exit);
+module_platform_driver(rpc_flash_platform_driver);
 
 MODULE_LICENSE("GPL v2");
 MODULE_DESCRIPTION("Renesas RPC HyperFlash MTD driver");
-- 
2.7.4