1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OpenFirmware helpers for memory drivers
5 * Copyright (C) 2012 Texas Instruments, Inc.
6 * Copyright (C) 2019 Samsung Electronics Co., Ltd.
7 * Copyright (C) 2020 Krzysztof Kozlowski <krzk@kernel.org>
10 #include <linux/device.h>
12 #include <linux/gfp.h>
13 #include <linux/export.h>
15 #include "jedec_ddr.h"
16 #include "of_memory.h"
19 * of_get_min_tck() - extract min timing values for ddr
20 * @np: pointer to ddr device tree node
21 * @dev: device requesting for min timing values
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.
28 const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
32 struct lpddr2_min_tck *min;
34 min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
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);
58 dev_warn(dev, "Using default min-tck values\n");
59 return &lpddr2_jedec_min_tck;
61 EXPORT_SYMBOL(of_get_min_tck);
63 static int of_do_get_timings(struct device_node *np,
64 struct lpddr2_timings *tim)
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",
86 &tim->tDQSCK_max_derated);
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)
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.
104 const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
109 struct lpddr2_timings *timings = NULL;
110 u32 arr_sz = 0, i = 0;
111 struct device_node *np_tim;
112 char *tim_compat = NULL;
114 switch (device_type) {
115 case DDR_TYPE_LPDDR2_S2:
116 case DDR_TYPE_LPDDR2_S4:
117 tim_compat = "jedec,lpddr2-timings";
120 dev_warn(dev, "Unsupported memory type\n");
123 for_each_child_of_node(np_ddr, np_tim)
124 if (of_device_is_compatible(np_tim, tim_compat))
128 timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
132 goto default_timings;
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;
144 *nr_frequencies = arr_sz;
149 dev_warn(dev, "Using default memory timings\n");
150 *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
151 return lpddr2_jedec_timings;
153 EXPORT_SYMBOL(of_get_ddr_timings);
156 * of_lpddr3_get_min_tck() - extract min timing values for lpddr3
157 * @np: pointer to ddr device tree node
158 * @dev: device requesting for min timing values
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.
164 const struct lpddr3_min_tck *of_lpddr3_get_min_tck(struct device_node *np,
168 struct lpddr3_min_tck *min;
170 min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
172 goto default_min_tck;
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);
197 dev_warn(dev, "Errors while parsing min-tck values\n");
198 devm_kfree(dev, min);
199 goto default_min_tck;
205 dev_warn(dev, "Using default min-tck values\n");
208 EXPORT_SYMBOL(of_lpddr3_get_min_tck);
210 static int of_lpddr3_do_get_timings(struct device_node *np,
211 struct lpddr3_timings *tim)
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);
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)
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.
253 const struct lpddr3_timings
254 *of_lpddr3_get_ddr_timings(struct device_node *np_ddr, struct device *dev,
255 u32 device_type, u32 *nr_frequencies)
257 struct lpddr3_timings *timings = NULL;
258 u32 arr_sz = 0, i = 0;
259 struct device_node *np_tim;
260 char *tim_compat = NULL;
262 switch (device_type) {
263 case DDR_TYPE_LPDDR3:
264 tim_compat = "jedec,lpddr3-timings";
267 dev_warn(dev, "Unsupported memory type\n");
270 for_each_child_of_node(np_ddr, np_tim)
271 if (of_device_is_compatible(np_tim, tim_compat))
275 timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
279 goto default_timings;
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;
291 *nr_frequencies = arr_sz;
296 dev_warn(dev, "Failed to get timings\n");
300 EXPORT_SYMBOL(of_lpddr3_get_ddr_timings);