xref: /openbmc/linux/drivers/memory/of_memory.c (revision 0e17c50f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * OpenFirmware helpers for memory drivers
4  *
5  * Copyright (C) 2012 Texas Instruments, Inc.
6  * Copyright (C) 2019 Samsung Electronics Co., Ltd.
7  */
8 
9 #include <linux/device.h>
10 #include <linux/platform_device.h>
11 #include <linux/list.h>
12 #include <linux/of.h>
13 #include <linux/gfp.h>
14 #include <linux/export.h>
15 
16 #include "jedec_ddr.h"
17 #include "of_memory.h"
18 
19 /**
20  * of_get_min_tck() - extract min timing values for ddr
21  * @np: pointer to ddr device tree node
22  * @device: device requesting for min timing values
23  *
24  * Populates the lpddr2_min_tck structure by extracting data
25  * from device tree node. Returns a pointer to the populated
26  * structure. If any error in populating the structure, returns
27  * default min timings provided by JEDEC.
28  */
29 const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
30 		struct device *dev)
31 {
32 	int			ret = 0;
33 	struct lpddr2_min_tck	*min;
34 
35 	min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
36 	if (!min)
37 		goto default_min_tck;
38 
39 	ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
40 	ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
41 	ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
42 	ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
43 	ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
44 	ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
45 	ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
46 	ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
47 	ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
48 	ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
49 	ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
50 
51 	if (ret) {
52 		devm_kfree(dev, min);
53 		goto default_min_tck;
54 	}
55 
56 	return min;
57 
58 default_min_tck:
59 	dev_warn(dev, "%s: using default min-tck values\n", __func__);
60 	return &lpddr2_jedec_min_tck;
61 }
62 EXPORT_SYMBOL(of_get_min_tck);
63 
64 static int of_do_get_timings(struct device_node *np,
65 		struct lpddr2_timings *tim)
66 {
67 	int ret;
68 
69 	ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
70 	ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
71 	ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
72 	ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
73 	ret |= of_property_read_u32(np, "tWR", &tim->tWR);
74 	ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
75 	ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
76 	ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
77 	ret |= of_property_read_u32(np, "tXP", &tim->tXP);
78 	ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
79 	ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
80 	ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
81 	ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
82 	ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
83 	ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
84 	ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
85 	ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
86 	ret |= of_property_read_u32(np, "tDQSCK-max-derated",
87 		&tim->tDQSCK_max_derated);
88 
89 	return ret;
90 }
91 
92 /**
93  * of_get_ddr_timings() - extracts the ddr timings and updates no of
94  * frequencies available.
95  * @np_ddr: Pointer to ddr device tree node
96  * @dev: Device requesting for ddr timings
97  * @device_type: Type of ddr(LPDDR2 S2/S4)
98  * @nr_frequencies: No of frequencies available for ddr
99  * (updated by this function)
100  *
101  * Populates lpddr2_timings structure by extracting data from device
102  * tree node. Returns pointer to populated structure. If any error
103  * while populating, returns default timings provided by JEDEC.
104  */
105 const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
106 		struct device *dev, u32 device_type, u32 *nr_frequencies)
107 {
108 	struct lpddr2_timings	*timings = NULL;
109 	u32			arr_sz = 0, i = 0;
110 	struct device_node	*np_tim;
111 	char			*tim_compat = NULL;
112 
113 	switch (device_type) {
114 	case DDR_TYPE_LPDDR2_S2:
115 	case DDR_TYPE_LPDDR2_S4:
116 		tim_compat = "jedec,lpddr2-timings";
117 		break;
118 	default:
119 		dev_warn(dev, "%s: un-supported memory type\n", __func__);
120 	}
121 
122 	for_each_child_of_node(np_ddr, np_tim)
123 		if (of_device_is_compatible(np_tim, tim_compat))
124 			arr_sz++;
125 
126 	if (arr_sz)
127 		timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
128 				       GFP_KERNEL);
129 
130 	if (!timings)
131 		goto default_timings;
132 
133 	for_each_child_of_node(np_ddr, np_tim) {
134 		if (of_device_is_compatible(np_tim, tim_compat)) {
135 			if (of_do_get_timings(np_tim, &timings[i])) {
136 				devm_kfree(dev, timings);
137 				goto default_timings;
138 			}
139 			i++;
140 		}
141 	}
142 
143 	*nr_frequencies = arr_sz;
144 
145 	return timings;
146 
147 default_timings:
148 	dev_warn(dev, "%s: using default timings\n", __func__);
149 	*nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
150 	return lpddr2_jedec_timings;
151 }
152 EXPORT_SYMBOL(of_get_ddr_timings);
153 
154 /**
155  * of_lpddr3_get_min_tck() - extract min timing values for lpddr3
156  * @np: pointer to ddr device tree node
157  * @device: device requesting for min timing values
158  *
159  * Populates the lpddr3_min_tck structure by extracting data
160  * from device tree node. Returns a pointer to the populated
161  * structure. If any error in populating the structure, returns NULL.
162  */
163 const struct lpddr3_min_tck *of_lpddr3_get_min_tck(struct device_node *np,
164 						   struct device *dev)
165 {
166 	int			ret = 0;
167 	struct lpddr3_min_tck	*min;
168 
169 	min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
170 	if (!min)
171 		goto default_min_tck;
172 
173 	ret |= of_property_read_u32(np, "tRFC-min-tck", &min->tRFC);
174 	ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
175 	ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
176 	ret |= of_property_read_u32(np, "tRPpb-min-tck", &min->tRPpb);
177 	ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
178 	ret |= of_property_read_u32(np, "tRC-min-tck", &min->tRC);
179 	ret |= of_property_read_u32(np, "tRAS-min-tck", &min->tRAS);
180 	ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
181 	ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
182 	ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
183 	ret |= of_property_read_u32(np, "tW2W-C2C-min-tck", &min->tW2W_C2C);
184 	ret |= of_property_read_u32(np, "tR2R-C2C-min-tck", &min->tR2R_C2C);
185 	ret |= of_property_read_u32(np, "tWL-min-tck", &min->tWL);
186 	ret |= of_property_read_u32(np, "tDQSCK-min-tck", &min->tDQSCK);
187 	ret |= of_property_read_u32(np, "tRL-min-tck", &min->tRL);
188 	ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
189 	ret |= of_property_read_u32(np, "tXSR-min-tck", &min->tXSR);
190 	ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
191 	ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
192 	ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
193 	ret |= of_property_read_u32(np, "tMRD-min-tck", &min->tMRD);
194 
195 	if (ret) {
196 		dev_warn(dev, "%s: errors while parsing min-tck values\n",
197 			 __func__);
198 		devm_kfree(dev, min);
199 		goto default_min_tck;
200 	}
201 
202 	return min;
203 
204 default_min_tck:
205 	dev_warn(dev, "%s: using default min-tck values\n", __func__);
206 	return NULL;
207 }
208 EXPORT_SYMBOL(of_lpddr3_get_min_tck);
209 
210 static 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  */
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)
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:
267 		dev_warn(dev, "%s: un-supported memory type\n", __func__);
268 	}
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 
295 default_timings:
296 	dev_warn(dev, "%s: failed to get timings\n", __func__);
297 	*nr_frequencies = 0;
298 	return NULL;
299 }
300 EXPORT_SYMBOL(of_lpddr3_get_ddr_timings);
301