xref: /openbmc/linux/drivers/memory/of_memory.c (revision ba61bb17)
1 /*
2  * OpenFirmware helpers for memory drivers
3  *
4  * 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.
10  */
11 
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/list.h>
15 #include <linux/of.h>
16 #include <linux/gfp.h>
17 #include <memory/jedec_ddr.h>
18 #include <linux/export.h>
19 #include "of_memory.h"
20 
21 /**
22  * of_get_min_tck() - extract min timing values for ddr
23  * @np: pointer to ddr device tree node
24  * @device: device requesting for min timing values
25  *
26  * Populates the lpddr2_min_tck structure by extracting data
27  * from device tree node. Returns a pointer to the populated
28  * structure. If any error in populating the structure, returns
29  * default min timings provided by JEDEC.
30  */
31 const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
32 		struct device *dev)
33 {
34 	int			ret = 0;
35 	struct lpddr2_min_tck	*min;
36 
37 	min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
38 	if (!min)
39 		goto default_min_tck;
40 
41 	ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
42 	ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
43 	ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
44 	ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
45 	ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
46 	ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
47 	ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
48 	ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
49 	ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
50 	ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
51 	ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
52 
53 	if (ret) {
54 		devm_kfree(dev, min);
55 		goto default_min_tck;
56 	}
57 
58 	return min;
59 
60 default_min_tck:
61 	dev_warn(dev, "%s: using default min-tck values\n", __func__);
62 	return &lpddr2_jedec_min_tck;
63 }
64 EXPORT_SYMBOL(of_get_min_tck);
65 
66 static int of_do_get_timings(struct device_node *np,
67 		struct lpddr2_timings *tim)
68 {
69 	int ret;
70 
71 	ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
72 	ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
73 	ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
74 	ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
75 	ret |= of_property_read_u32(np, "tWR", &tim->tWR);
76 	ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
77 	ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
78 	ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
79 	ret |= of_property_read_u32(np, "tXP", &tim->tXP);
80 	ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
81 	ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
82 	ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
83 	ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
84 	ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
85 	ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
86 	ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
87 	ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
88 	ret |= of_property_read_u32(np, "tDQSCK-max-derated",
89 		&tim->tDQSCK_max_derated);
90 
91 	return ret;
92 }
93 
94 /**
95  * of_get_ddr_timings() - extracts the ddr timings and updates no of
96  * frequencies available.
97  * @np_ddr: Pointer to ddr device tree node
98  * @dev: Device requesting for ddr timings
99  * @device_type: Type of ddr(LPDDR2 S2/S4)
100  * @nr_frequencies: No of frequencies available for ddr
101  * (updated by this function)
102  *
103  * Populates lpddr2_timings structure by extracting data from device
104  * tree node. Returns pointer to populated structure. If any error
105  * while populating, returns default timings provided by JEDEC.
106  */
107 const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
108 		struct device *dev, u32 device_type, u32 *nr_frequencies)
109 {
110 	struct lpddr2_timings	*timings = NULL;
111 	u32			arr_sz = 0, i = 0;
112 	struct device_node	*np_tim;
113 	char			*tim_compat = NULL;
114 
115 	switch (device_type) {
116 	case DDR_TYPE_LPDDR2_S2:
117 	case DDR_TYPE_LPDDR2_S4:
118 		tim_compat = "jedec,lpddr2-timings";
119 		break;
120 	default:
121 		dev_warn(dev, "%s: un-supported memory type\n", __func__);
122 	}
123 
124 	for_each_child_of_node(np_ddr, np_tim)
125 		if (of_device_is_compatible(np_tim, tim_compat))
126 			arr_sz++;
127 
128 	if (arr_sz)
129 		timings = devm_kcalloc(dev, arr_sz, sizeof(*timings),
130 				       GFP_KERNEL);
131 
132 	if (!timings)
133 		goto default_timings;
134 
135 	for_each_child_of_node(np_ddr, np_tim) {
136 		if (of_device_is_compatible(np_tim, tim_compat)) {
137 			if (of_do_get_timings(np_tim, &timings[i])) {
138 				devm_kfree(dev, timings);
139 				goto default_timings;
140 			}
141 			i++;
142 		}
143 	}
144 
145 	*nr_frequencies = arr_sz;
146 
147 	return timings;
148 
149 default_timings:
150 	dev_warn(dev, "%s: using default timings\n", __func__);
151 	*nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
152 	return lpddr2_jedec_timings;
153 }
154 EXPORT_SYMBOL(of_get_ddr_timings);
155