blob: b94408954d85cca3c4cb082c535156b41dc0cbe4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Aneesh Ve6b42eb2012-08-17 14:05:15 +05302/*
3 * OpenFirmware helpers for memory drivers
4 *
5 * Copyright (C) 2012 Texas Instruments, Inc.
Lukasz Luba976897d2019-08-21 12:42:58 +02006 * Copyright (C) 2019 Samsung Electronics Co., Ltd.
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +02007 * Copyright (C) 2020 Krzysztof Kozlowski <krzk@kernel.org>
Aneesh Ve6b42eb2012-08-17 14:05:15 +05308 */
9
10#include <linux/device.h>
Aneesh Ve6b42eb2012-08-17 14:05:15 +053011#include <linux/of.h>
12#include <linux/gfp.h>
Aneesh Ve6b42eb2012-08-17 14:05:15 +053013#include <linux/export.h>
Masahiro Yamada5ec47cd2019-06-03 17:12:33 +090014
15#include "jedec_ddr.h"
Baoyou Xieaeb83d72016-08-28 01:31:28 +080016#include "of_memory.h"
Aneesh Ve6b42eb2012-08-17 14:05:15 +053017
18/**
19 * of_get_min_tck() - extract min timing values for ddr
20 * @np: pointer to ddr device tree node
Krzysztof Kozlowski46c71112020-07-24 20:23:25 +020021 * @dev: device requesting for min timing values
Aneesh Ve6b42eb2012-08-17 14:05:15 +053022 *
23 * Populates the lpddr2_min_tck structure by extracting data
24 * from device tree node. Returns a pointer to the populated
25 * structure. If any error in populating the structure, returns
26 * default min timings provided by JEDEC.
27 */
28const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
Krzysztof Kozlowski98250952020-07-24 09:40:18 +020029 struct device *dev)
Aneesh Ve6b42eb2012-08-17 14:05:15 +053030{
31 int ret = 0;
32 struct lpddr2_min_tck *min;
33
34 min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
35 if (!min)
36 goto default_min_tck;
37
38 ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
39 ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
40 ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
41 ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
42 ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
43 ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
44 ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
45 ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
46 ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
47 ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
48 ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
49
50 if (ret) {
51 devm_kfree(dev, min);
52 goto default_min_tck;
53 }
54
55 return min;
56
57default_min_tck:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +020058 dev_warn(dev, "Using default min-tck values\n");
Aneesh Ve6b42eb2012-08-17 14:05:15 +053059 return &lpddr2_jedec_min_tck;
60}
61EXPORT_SYMBOL(of_get_min_tck);
62
63static int of_do_get_timings(struct device_node *np,
Krzysztof Kozlowski98250952020-07-24 09:40:18 +020064 struct lpddr2_timings *tim)
Aneesh Ve6b42eb2012-08-17 14:05:15 +053065{
66 int ret;
67
68 ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
69 ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
70 ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
71 ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
72 ret |= of_property_read_u32(np, "tWR", &tim->tWR);
73 ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
74 ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
75 ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
76 ret |= of_property_read_u32(np, "tXP", &tim->tXP);
77 ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
78 ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
79 ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
80 ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
81 ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
82 ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
83 ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
84 ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
85 ret |= of_property_read_u32(np, "tDQSCK-max-derated",
Krzysztof Kozlowski98250952020-07-24 09:40:18 +020086 &tim->tDQSCK_max_derated);
Aneesh Ve6b42eb2012-08-17 14:05:15 +053087
88 return ret;
89}
90
91/**
92 * of_get_ddr_timings() - extracts the ddr timings and updates no of
93 * frequencies available.
94 * @np_ddr: Pointer to ddr device tree node
95 * @dev: Device requesting for ddr timings
96 * @device_type: Type of ddr(LPDDR2 S2/S4)
97 * @nr_frequencies: No of frequencies available for ddr
98 * (updated by this function)
99 *
100 * Populates lpddr2_timings structure by extracting data from device
101 * tree node. Returns pointer to populated structure. If any error
102 * while populating, returns default timings provided by JEDEC.
103 */
104const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
Krzysztof Kozlowski98250952020-07-24 09:40:18 +0200105 struct device *dev,
106 u32 device_type,
107 u32 *nr_frequencies)
Aneesh Ve6b42eb2012-08-17 14:05:15 +0530108{
109 struct lpddr2_timings *timings = NULL;
110 u32 arr_sz = 0, i = 0;
111 struct device_node *np_tim;
Dan Carpenterae53e372016-04-15 17:50:32 +0300112 char *tim_compat = NULL;
Aneesh Ve6b42eb2012-08-17 14:05:15 +0530113
114 switch (device_type) {
115 case DDR_TYPE_LPDDR2_S2:
116 case DDR_TYPE_LPDDR2_S4:
117 tim_compat = "jedec,lpddr2-timings";
118 break;
119 default:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200120 dev_warn(dev, "Unsupported memory type\n");
Aneesh Ve6b42eb2012-08-17 14:05:15 +0530121 }
122
123 for_each_child_of_node(np_ddr, np_tim)
124 if (of_device_is_compatible(np_tim, tim_compat))
125 arr_sz++;
126
127 if (arr_sz)
Kees Cooka86854d2018-06-12 14:07:58 -0700128 timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
129 GFP_KERNEL);
Aneesh Ve6b42eb2012-08-17 14:05:15 +0530130
131 if (!timings)
132 goto default_timings;
133
134 for_each_child_of_node(np_ddr, np_tim) {
135 if (of_device_is_compatible(np_tim, tim_compat)) {
136 if (of_do_get_timings(np_tim, &timings[i])) {
137 devm_kfree(dev, timings);
138 goto default_timings;
139 }
140 i++;
141 }
142 }
143
144 *nr_frequencies = arr_sz;
145
146 return timings;
147
148default_timings:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200149 dev_warn(dev, "Using default memory timings\n");
Aneesh Ve6b42eb2012-08-17 14:05:15 +0530150 *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
151 return lpddr2_jedec_timings;
152}
153EXPORT_SYMBOL(of_get_ddr_timings);
Lukasz Luba976897d2019-08-21 12:42:58 +0200154
155/**
156 * of_lpddr3_get_min_tck() - extract min timing values for lpddr3
157 * @np: pointer to ddr device tree node
Krzysztof Kozlowski46c71112020-07-24 20:23:25 +0200158 * @dev: device requesting for min timing values
Lukasz Luba976897d2019-08-21 12:42:58 +0200159 *
160 * Populates the lpddr3_min_tck structure by extracting data
161 * from device tree node. Returns a pointer to the populated
162 * structure. If any error in populating the structure, returns NULL.
163 */
164const struct lpddr3_min_tck *of_lpddr3_get_min_tck(struct device_node *np,
165 struct device *dev)
166{
167 int ret = 0;
168 struct lpddr3_min_tck *min;
169
170 min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
171 if (!min)
172 goto default_min_tck;
173
174 ret |= of_property_read_u32(np, "tRFC-min-tck", &min->tRFC);
175 ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
176 ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
177 ret |= of_property_read_u32(np, "tRPpb-min-tck", &min->tRPpb);
178 ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
179 ret |= of_property_read_u32(np, "tRC-min-tck", &min->tRC);
180 ret |= of_property_read_u32(np, "tRAS-min-tck", &min->tRAS);
181 ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
182 ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
183 ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
184 ret |= of_property_read_u32(np, "tW2W-C2C-min-tck", &min->tW2W_C2C);
185 ret |= of_property_read_u32(np, "tR2R-C2C-min-tck", &min->tR2R_C2C);
186 ret |= of_property_read_u32(np, "tWL-min-tck", &min->tWL);
187 ret |= of_property_read_u32(np, "tDQSCK-min-tck", &min->tDQSCK);
188 ret |= of_property_read_u32(np, "tRL-min-tck", &min->tRL);
189 ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
190 ret |= of_property_read_u32(np, "tXSR-min-tck", &min->tXSR);
191 ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
192 ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
193 ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
194 ret |= of_property_read_u32(np, "tMRD-min-tck", &min->tMRD);
195
196 if (ret) {
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200197 dev_warn(dev, "Errors while parsing min-tck values\n");
Lukasz Luba976897d2019-08-21 12:42:58 +0200198 devm_kfree(dev, min);
199 goto default_min_tck;
200 }
201
202 return min;
203
204default_min_tck:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200205 dev_warn(dev, "Using default min-tck values\n");
Lukasz Luba976897d2019-08-21 12:42:58 +0200206 return NULL;
207}
208EXPORT_SYMBOL(of_lpddr3_get_min_tck);
209
210static int of_lpddr3_do_get_timings(struct device_node *np,
211 struct lpddr3_timings *tim)
212{
213 int ret;
214
215 /* The 'reg' param required since DT has changed, used as 'max-freq' */
216 ret = of_property_read_u32(np, "reg", &tim->max_freq);
217 ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
218 ret |= of_property_read_u32(np, "tRFC", &tim->tRFC);
219 ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
220 ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
221 ret |= of_property_read_u32(np, "tRPpb", &tim->tRPpb);
222 ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
223 ret |= of_property_read_u32(np, "tRC", &tim->tRC);
224 ret |= of_property_read_u32(np, "tRAS", &tim->tRAS);
225 ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
226 ret |= of_property_read_u32(np, "tWR", &tim->tWR);
227 ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
228 ret |= of_property_read_u32(np, "tW2W-C2C", &tim->tW2W_C2C);
229 ret |= of_property_read_u32(np, "tR2R-C2C", &tim->tR2R_C2C);
230 ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
231 ret |= of_property_read_u32(np, "tXSR", &tim->tXSR);
232 ret |= of_property_read_u32(np, "tXP", &tim->tXP);
233 ret |= of_property_read_u32(np, "tCKE", &tim->tCKE);
234 ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
235 ret |= of_property_read_u32(np, "tMRD", &tim->tMRD);
236
237 return ret;
238}
239
240/**
241 * of_lpddr3_get_ddr_timings() - extracts the lpddr3 timings and updates no of
242 * frequencies available.
243 * @np_ddr: Pointer to ddr device tree node
244 * @dev: Device requesting for ddr timings
245 * @device_type: Type of ddr
246 * @nr_frequencies: No of frequencies available for ddr
247 * (updated by this function)
248 *
249 * Populates lpddr3_timings structure by extracting data from device
250 * tree node. Returns pointer to populated structure. If any error
251 * while populating, returns NULL.
252 */
253const struct lpddr3_timings
254*of_lpddr3_get_ddr_timings(struct device_node *np_ddr, struct device *dev,
255 u32 device_type, u32 *nr_frequencies)
256{
257 struct lpddr3_timings *timings = NULL;
258 u32 arr_sz = 0, i = 0;
259 struct device_node *np_tim;
260 char *tim_compat = NULL;
261
262 switch (device_type) {
263 case DDR_TYPE_LPDDR3:
264 tim_compat = "jedec,lpddr3-timings";
265 break;
266 default:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200267 dev_warn(dev, "Unsupported memory type\n");
Lukasz Luba976897d2019-08-21 12:42:58 +0200268 }
269
270 for_each_child_of_node(np_ddr, np_tim)
271 if (of_device_is_compatible(np_tim, tim_compat))
272 arr_sz++;
273
274 if (arr_sz)
275 timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
276 GFP_KERNEL);
277
278 if (!timings)
279 goto default_timings;
280
281 for_each_child_of_node(np_ddr, np_tim) {
282 if (of_device_is_compatible(np_tim, tim_compat)) {
283 if (of_lpddr3_do_get_timings(np_tim, &timings[i])) {
284 devm_kfree(dev, timings);
285 goto default_timings;
286 }
287 i++;
288 }
289 }
290
291 *nr_frequencies = arr_sz;
292
293 return timings;
294
295default_timings:
Krzysztof Kozlowskiefc46462020-07-24 09:40:17 +0200296 dev_warn(dev, "Failed to get timings\n");
Lukasz Luba976897d2019-08-21 12:42:58 +0200297 *nr_frequencies = 0;
298 return NULL;
299}
300EXPORT_SYMBOL(of_lpddr3_get_ddr_timings);
Dmitry Osipenko38322cf2021-10-07 01:46:58 +0300301
302/**
303 * of_lpddr2_get_info() - extracts information about the lpddr2 chip.
304 * @np: Pointer to device tree node containing lpddr2 info
305 * @dev: Device requesting info
306 *
307 * Populates lpddr2_info structure by extracting data from device
308 * tree node. Returns pointer to populated structure. If error
309 * happened while populating, returns NULL. If property is missing
310 * in a device-tree, then the corresponding value is set to -ENOENT.
311 */
312const struct lpddr2_info
313*of_lpddr2_get_info(struct device_node *np, struct device *dev)
314{
315 struct lpddr2_info *ret_info, info = {};
316 struct property *prop;
317 const char *cp;
318 int err;
319
320 err = of_property_read_u32(np, "revision-id1", &info.revision_id1);
321 if (err)
322 info.revision_id1 = -ENOENT;
323
324 err = of_property_read_u32(np, "revision-id2", &info.revision_id2);
325 if (err)
326 info.revision_id2 = -ENOENT;
327
328 err = of_property_read_u32(np, "io-width", &info.io_width);
329 if (err)
330 return NULL;
331
332 info.io_width = 32 / info.io_width - 1;
333
334 err = of_property_read_u32(np, "density", &info.density);
335 if (err)
336 return NULL;
337
338 info.density = ffs(info.density) - 7;
339
340 if (of_device_is_compatible(np, "jedec,lpddr2-s4"))
341 info.arch_type = LPDDR2_TYPE_S4;
342 else if (of_device_is_compatible(np, "jedec,lpddr2-s2"))
343 info.arch_type = LPDDR2_TYPE_S2;
344 else if (of_device_is_compatible(np, "jedec,lpddr2-nvm"))
345 info.arch_type = LPDDR2_TYPE_NVM;
346 else
347 return NULL;
348
349 prop = of_find_property(np, "compatible", NULL);
350 for (cp = of_prop_next_string(prop, NULL); cp;
351 cp = of_prop_next_string(prop, cp)) {
352
353#define OF_LPDDR2_VENDOR_CMP(compat, ID) \
354 if (!of_compat_cmp(cp, compat ",", strlen(compat ","))) { \
355 info.manufacturer_id = LPDDR2_MANID_##ID; \
356 break; \
357 }
358
359 OF_LPDDR2_VENDOR_CMP("samsung", SAMSUNG)
360 OF_LPDDR2_VENDOR_CMP("qimonda", QIMONDA)
361 OF_LPDDR2_VENDOR_CMP("elpida", ELPIDA)
362 OF_LPDDR2_VENDOR_CMP("etron", ETRON)
363 OF_LPDDR2_VENDOR_CMP("nanya", NANYA)
364 OF_LPDDR2_VENDOR_CMP("hynix", HYNIX)
365 OF_LPDDR2_VENDOR_CMP("mosel", MOSEL)
366 OF_LPDDR2_VENDOR_CMP("winbond", WINBOND)
367 OF_LPDDR2_VENDOR_CMP("esmt", ESMT)
368 OF_LPDDR2_VENDOR_CMP("spansion", SPANSION)
369 OF_LPDDR2_VENDOR_CMP("sst", SST)
370 OF_LPDDR2_VENDOR_CMP("zmos", ZMOS)
371 OF_LPDDR2_VENDOR_CMP("intel", INTEL)
372 OF_LPDDR2_VENDOR_CMP("numonyx", NUMONYX)
373 OF_LPDDR2_VENDOR_CMP("micron", MICRON)
374
375#undef OF_LPDDR2_VENDOR_CMP
376 }
377
378 if (!info.manufacturer_id)
379 info.manufacturer_id = -ENOENT;
380
381 ret_info = devm_kzalloc(dev, sizeof(*ret_info), GFP_KERNEL);
382 if (ret_info)
383 *ret_info = info;
384
385 return ret_info;
386}
387EXPORT_SYMBOL(of_lpddr2_get_info);