hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/memory/of_memory.c
....@@ -1,27 +1,24 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * OpenFirmware helpers for memory drivers
34 *
45 * Copyright (C) 2012 Texas Instruments, Inc.
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
6
+ * Copyright (C) 2019 Samsung Electronics Co., Ltd.
7
+ * Copyright (C) 2020 Krzysztof Kozlowski <krzk@kernel.org>
108 */
119
1210 #include <linux/device.h>
13
-#include <linux/platform_device.h>
14
-#include <linux/list.h>
1511 #include <linux/of.h>
1612 #include <linux/gfp.h>
17
-#include <memory/jedec_ddr.h>
1813 #include <linux/export.h>
14
+
15
+#include "jedec_ddr.h"
1916 #include "of_memory.h"
2017
2118 /**
2219 * of_get_min_tck() - extract min timing values for ddr
2320 * @np: pointer to ddr device tree node
24
- * @device: device requesting for min timing values
21
+ * @dev: device requesting for min timing values
2522 *
2623 * Populates the lpddr2_min_tck structure by extracting data
2724 * from device tree node. Returns a pointer to the populated
....@@ -29,7 +26,7 @@
2926 * default min timings provided by JEDEC.
3027 */
3128 const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
32
- struct device *dev)
29
+ struct device *dev)
3330 {
3431 int ret = 0;
3532 struct lpddr2_min_tck *min;
....@@ -58,13 +55,13 @@
5855 return min;
5956
6057 default_min_tck:
61
- dev_warn(dev, "%s: using default min-tck values\n", __func__);
58
+ dev_warn(dev, "Using default min-tck values\n");
6259 return &lpddr2_jedec_min_tck;
6360 }
6461 EXPORT_SYMBOL(of_get_min_tck);
6562
6663 static int of_do_get_timings(struct device_node *np,
67
- struct lpddr2_timings *tim)
64
+ struct lpddr2_timings *tim)
6865 {
6966 int ret;
7067
....@@ -86,7 +83,7 @@
8683 ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
8784 ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
8885 ret |= of_property_read_u32(np, "tDQSCK-max-derated",
89
- &tim->tDQSCK_max_derated);
86
+ &tim->tDQSCK_max_derated);
9087
9188 return ret;
9289 }
....@@ -105,7 +102,9 @@
105102 * while populating, returns default timings provided by JEDEC.
106103 */
107104 const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
108
- struct device *dev, u32 device_type, u32 *nr_frequencies)
105
+ struct device *dev,
106
+ u32 device_type,
107
+ u32 *nr_frequencies)
109108 {
110109 struct lpddr2_timings *timings = NULL;
111110 u32 arr_sz = 0, i = 0;
....@@ -118,7 +117,7 @@
118117 tim_compat = "jedec,lpddr2-timings";
119118 break;
120119 default:
121
- dev_warn(dev, "%s: un-supported memory type\n", __func__);
120
+ dev_warn(dev, "Unsupported memory type\n");
122121 }
123122
124123 for_each_child_of_node(np_ddr, np_tim)
....@@ -135,6 +134,7 @@
135134 for_each_child_of_node(np_ddr, np_tim) {
136135 if (of_device_is_compatible(np_tim, tim_compat)) {
137136 if (of_do_get_timings(np_tim, &timings[i])) {
137
+ of_node_put(np_tim);
138138 devm_kfree(dev, timings);
139139 goto default_timings;
140140 }
....@@ -147,8 +147,156 @@
147147 return timings;
148148
149149 default_timings:
150
- dev_warn(dev, "%s: using default timings\n", __func__);
150
+ dev_warn(dev, "Using default memory timings\n");
151151 *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
152152 return lpddr2_jedec_timings;
153153 }
154154 EXPORT_SYMBOL(of_get_ddr_timings);
155
+
156
+/**
157
+ * of_lpddr3_get_min_tck() - extract min timing values for lpddr3
158
+ * @np: pointer to ddr device tree node
159
+ * @dev: device requesting for min timing values
160
+ *
161
+ * Populates the lpddr3_min_tck structure by extracting data
162
+ * from device tree node. Returns a pointer to the populated
163
+ * structure. If any error in populating the structure, returns NULL.
164
+ */
165
+const struct lpddr3_min_tck *of_lpddr3_get_min_tck(struct device_node *np,
166
+ struct device *dev)
167
+{
168
+ int ret = 0;
169
+ struct lpddr3_min_tck *min;
170
+
171
+ min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
172
+ if (!min)
173
+ goto default_min_tck;
174
+
175
+ ret |= of_property_read_u32(np, "tRFC-min-tck", &min->tRFC);
176
+ ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
177
+ ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
178
+ ret |= of_property_read_u32(np, "tRPpb-min-tck", &min->tRPpb);
179
+ ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
180
+ ret |= of_property_read_u32(np, "tRC-min-tck", &min->tRC);
181
+ ret |= of_property_read_u32(np, "tRAS-min-tck", &min->tRAS);
182
+ ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
183
+ ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
184
+ ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
185
+ ret |= of_property_read_u32(np, "tW2W-C2C-min-tck", &min->tW2W_C2C);
186
+ ret |= of_property_read_u32(np, "tR2R-C2C-min-tck", &min->tR2R_C2C);
187
+ ret |= of_property_read_u32(np, "tWL-min-tck", &min->tWL);
188
+ ret |= of_property_read_u32(np, "tDQSCK-min-tck", &min->tDQSCK);
189
+ ret |= of_property_read_u32(np, "tRL-min-tck", &min->tRL);
190
+ ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
191
+ ret |= of_property_read_u32(np, "tXSR-min-tck", &min->tXSR);
192
+ ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
193
+ ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
194
+ ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
195
+ ret |= of_property_read_u32(np, "tMRD-min-tck", &min->tMRD);
196
+
197
+ if (ret) {
198
+ dev_warn(dev, "Errors while parsing min-tck values\n");
199
+ devm_kfree(dev, min);
200
+ goto default_min_tck;
201
+ }
202
+
203
+ return min;
204
+
205
+default_min_tck:
206
+ dev_warn(dev, "Using default min-tck values\n");
207
+ return NULL;
208
+}
209
+EXPORT_SYMBOL(of_lpddr3_get_min_tck);
210
+
211
+static int of_lpddr3_do_get_timings(struct device_node *np,
212
+ struct lpddr3_timings *tim)
213
+{
214
+ int ret;
215
+
216
+ /* The 'reg' param required since DT has changed, used as 'max-freq' */
217
+ ret = of_property_read_u32(np, "reg", &tim->max_freq);
218
+ ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
219
+ ret |= of_property_read_u32(np, "tRFC", &tim->tRFC);
220
+ ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
221
+ ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
222
+ ret |= of_property_read_u32(np, "tRPpb", &tim->tRPpb);
223
+ ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
224
+ ret |= of_property_read_u32(np, "tRC", &tim->tRC);
225
+ ret |= of_property_read_u32(np, "tRAS", &tim->tRAS);
226
+ ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
227
+ ret |= of_property_read_u32(np, "tWR", &tim->tWR);
228
+ ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
229
+ ret |= of_property_read_u32(np, "tW2W-C2C", &tim->tW2W_C2C);
230
+ ret |= of_property_read_u32(np, "tR2R-C2C", &tim->tR2R_C2C);
231
+ ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
232
+ ret |= of_property_read_u32(np, "tXSR", &tim->tXSR);
233
+ ret |= of_property_read_u32(np, "tXP", &tim->tXP);
234
+ ret |= of_property_read_u32(np, "tCKE", &tim->tCKE);
235
+ ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
236
+ ret |= of_property_read_u32(np, "tMRD", &tim->tMRD);
237
+
238
+ return ret;
239
+}
240
+
241
+/**
242
+ * of_lpddr3_get_ddr_timings() - extracts the lpddr3 timings and updates no of
243
+ * frequencies available.
244
+ * @np_ddr: Pointer to ddr device tree node
245
+ * @dev: Device requesting for ddr timings
246
+ * @device_type: Type of ddr
247
+ * @nr_frequencies: No of frequencies available for ddr
248
+ * (updated by this function)
249
+ *
250
+ * Populates lpddr3_timings structure by extracting data from device
251
+ * tree node. Returns pointer to populated structure. If any error
252
+ * while populating, returns NULL.
253
+ */
254
+const struct lpddr3_timings
255
+*of_lpddr3_get_ddr_timings(struct device_node *np_ddr, struct device *dev,
256
+ u32 device_type, u32 *nr_frequencies)
257
+{
258
+ struct lpddr3_timings *timings = NULL;
259
+ u32 arr_sz = 0, i = 0;
260
+ struct device_node *np_tim;
261
+ char *tim_compat = NULL;
262
+
263
+ switch (device_type) {
264
+ case DDR_TYPE_LPDDR3:
265
+ tim_compat = "jedec,lpddr3-timings";
266
+ break;
267
+ default:
268
+ dev_warn(dev, "Unsupported memory type\n");
269
+ }
270
+
271
+ for_each_child_of_node(np_ddr, np_tim)
272
+ if (of_device_is_compatible(np_tim, tim_compat))
273
+ arr_sz++;
274
+
275
+ if (arr_sz)
276
+ timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
277
+ GFP_KERNEL);
278
+
279
+ if (!timings)
280
+ goto default_timings;
281
+
282
+ for_each_child_of_node(np_ddr, np_tim) {
283
+ if (of_device_is_compatible(np_tim, tim_compat)) {
284
+ if (of_lpddr3_do_get_timings(np_tim, &timings[i])) {
285
+ devm_kfree(dev, timings);
286
+ of_node_put(np_tim);
287
+ goto default_timings;
288
+ }
289
+ i++;
290
+ }
291
+ }
292
+
293
+ *nr_frequencies = arr_sz;
294
+
295
+ return timings;
296
+
297
+default_timings:
298
+ dev_warn(dev, "Failed to get timings\n");
299
+ *nr_frequencies = 0;
300
+ return NULL;
301
+}
302
+EXPORT_SYMBOL(of_lpddr3_get_ddr_timings);