1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  */
5 
6 #define LOG_CATEGORY UCLASS_I2C_EEPROM
7 
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/kernel.h>
11 #include <dm.h>
12 #include <dm/device-internal.h>
13 #include <i2c.h>
14 #include <i2c_eeprom.h>
15 
16 struct i2c_eeprom_drv_data {
17 	u32 size; /* size in bytes */
18 	u32 pagesize; /* page size in bytes */
19 	u32 addr_offset_mask; /* bits in addr used for offset overflow */
20 	u32 offset_len; /* size in bytes of offset */
21 	u32 start_offset; /* valid start offset inside memory, by default 0 */
22 };
23 
i2c_eeprom_read(struct udevice * dev,int offset,uint8_t * buf,int size)24 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
25 {
26 	const struct i2c_eeprom_ops *ops = device_get_ops(dev);
27 
28 	if (!ops->read)
29 		return -ENOSYS;
30 
31 	return ops->read(dev, offset, buf, size);
32 }
33 
i2c_eeprom_write(struct udevice * dev,int offset,const uint8_t * buf,int size)34 int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
35 		     int size)
36 {
37 	const struct i2c_eeprom_ops *ops = device_get_ops(dev);
38 
39 	if (!ops->write)
40 		return -ENOSYS;
41 
42 	return ops->write(dev, offset, buf, size);
43 }
44 
i2c_eeprom_size(struct udevice * dev)45 int i2c_eeprom_size(struct udevice *dev)
46 {
47 	const struct i2c_eeprom_ops *ops = device_get_ops(dev);
48 
49 	if (!ops->size)
50 		return -ENOSYS;
51 
52 	return ops->size(dev);
53 }
54 
i2c_eeprom_std_read(struct udevice * dev,int offset,uint8_t * buf,int size)55 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
56 			       int size)
57 {
58 	return dm_i2c_read(dev, offset, buf, size);
59 }
60 
i2c_eeprom_len(int offset,int len,int pagesize)61 static int i2c_eeprom_len(int offset, int len, int pagesize)
62 {
63 	int page_offset = offset & (pagesize - 1);
64 	int maxlen = pagesize - page_offset;
65 
66 	if (len > maxlen)
67 		len = maxlen;
68 
69 	return len;
70 }
71 
i2c_eeprom_std_write(struct udevice * dev,int offset,const uint8_t * buf,int size)72 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
73 				const uint8_t *buf, int size)
74 {
75 	struct i2c_eeprom *priv = dev_get_priv(dev);
76 	int ret;
77 
78 	while (size > 0) {
79 		int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
80 
81 		ret = dm_i2c_write(dev, offset, buf, write_size);
82 		if (ret)
83 			return ret;
84 
85 		offset += write_size;
86 		buf += write_size;
87 		size -= write_size;
88 
89 		udelay(10000);
90 	}
91 
92 	return 0;
93 }
94 
i2c_eeprom_std_size(struct udevice * dev)95 static int i2c_eeprom_std_size(struct udevice *dev)
96 {
97 	struct i2c_eeprom *priv = dev_get_priv(dev);
98 
99 	return priv->size;
100 }
101 
102 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
103 	.read	= i2c_eeprom_std_read,
104 	.write	= i2c_eeprom_std_write,
105 	.size	= i2c_eeprom_std_size,
106 };
107 
i2c_eeprom_std_of_to_plat(struct udevice * dev)108 static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
109 {
110 	struct i2c_eeprom *priv = dev_get_priv(dev);
111 	struct i2c_eeprom_drv_data *data =
112 		(struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
113 	u32 pagesize;
114 	u32 size;
115 
116 	if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
117 		priv->pagesize = pagesize;
118 	else
119 		/* 6 bit -> page size of up to 2^63 (should be sufficient) */
120 		priv->pagesize = data->pagesize;
121 
122 	if (dev_read_u32(dev, "size", &size) == 0)
123 		priv->size = size;
124 	else
125 		priv->size = data->size;
126 
127 	return 0;
128 }
129 
i2c_eeprom_std_bind(struct udevice * dev)130 static int i2c_eeprom_std_bind(struct udevice *dev)
131 {
132 	ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
133 	ofnode partition;
134 	const char *name;
135 
136 	if (!ofnode_valid(partitions))
137 		return 0;
138 	if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
139 		return -ENOTSUPP;
140 
141 	ofnode_for_each_subnode(partition, partitions) {
142 		name = ofnode_get_name(partition);
143 		if (!name)
144 			continue;
145 
146 		device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
147 			    NULL, partition, NULL);
148 	}
149 
150 	return 0;
151 }
152 
i2c_eeprom_std_probe(struct udevice * dev)153 static int i2c_eeprom_std_probe(struct udevice *dev)
154 {
155 	u8 test_byte;
156 	int ret;
157 	struct i2c_eeprom_drv_data *data =
158 		(struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
159 
160 	i2c_set_chip_offset_len(dev, data->offset_len);
161 	i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
162 
163 	/* Verify that the chip is functional */
164 	/*
165 	 * Not all eeproms start from offset 0. Valid offset is available
166 	 * in the platform data struct.
167 	 */
168 	ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
169 	if (ret)
170 		return -ENODEV;
171 
172 	return 0;
173 }
174 
175 static const struct i2c_eeprom_drv_data eeprom_data = {
176 	.size = 0,
177 	.pagesize = 1,
178 	.addr_offset_mask = 0,
179 	.offset_len = 1,
180 };
181 
182 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
183 	.size = 128,
184 	.pagesize = 8,
185 	.addr_offset_mask = 0,
186 	.offset_len = 1,
187 };
188 
189 static const struct i2c_eeprom_drv_data atmel24c02_data = {
190 	.size = 256,
191 	.pagesize = 8,
192 	.addr_offset_mask = 0,
193 	.offset_len = 1,
194 };
195 
196 static const struct i2c_eeprom_drv_data atmel24c04_data = {
197 	.size = 512,
198 	.pagesize = 16,
199 	.addr_offset_mask = 0x1,
200 	.offset_len = 1,
201 };
202 
203 static const struct i2c_eeprom_drv_data atmel24c08_data = {
204 	.size = 1024,
205 	.pagesize = 16,
206 	.addr_offset_mask = 0x3,
207 	.offset_len = 1,
208 };
209 
210 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
211 	.size = 1024,
212 	.pagesize = 16,
213 	.addr_offset_mask = 0x3,
214 	.offset_len = 1,
215 };
216 
217 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
218 	.size = 2048,
219 	.pagesize = 16,
220 	.addr_offset_mask = 0x7,
221 	.offset_len = 1,
222 };
223 
224 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
225 	.size = 256,
226 	.pagesize = 16,
227 	.addr_offset_mask = 0,
228 	.offset_len = 1,
229 	.start_offset = 0x80,
230 };
231 
232 static const struct i2c_eeprom_drv_data atmel24c32_data = {
233 	.size = 4096,
234 	.pagesize = 32,
235 	.addr_offset_mask = 0,
236 	.offset_len = 2,
237 };
238 
239 static const struct i2c_eeprom_drv_data atmel24c32d_wlp_data = {
240 	.size = 32,
241 	.pagesize = 32,
242 	.addr_offset_mask = 0,
243 	.offset_len = 2,
244 };
245 
246 static const struct i2c_eeprom_drv_data atmel24c64_data = {
247 	.size = 8192,
248 	.pagesize = 32,
249 	.addr_offset_mask = 0,
250 	.offset_len = 2,
251 };
252 
253 static const struct i2c_eeprom_drv_data atmel24c128_data = {
254 	.size = 16384,
255 	.pagesize = 64,
256 	.addr_offset_mask = 0,
257 	.offset_len = 2,
258 };
259 
260 static const struct i2c_eeprom_drv_data atmel24c256_data = {
261 	.size = 32768,
262 	.pagesize = 64,
263 	.addr_offset_mask = 0,
264 	.offset_len = 2,
265 };
266 
267 static const struct i2c_eeprom_drv_data st24256e_wlp_data = {
268 	.size = 64,
269 	.pagesize = 64,
270 	.addr_offset_mask = 0,
271 	.offset_len = 2,
272 };
273 
274 static const struct i2c_eeprom_drv_data atmel24c512_data = {
275 	.size = 65536,
276 	.pagesize = 64,
277 	.addr_offset_mask = 0,
278 	.offset_len = 2,
279 };
280 
281 static const struct udevice_id i2c_eeprom_std_ids[] = {
282 	{ .compatible = "i2c-eeprom", (ulong)&eeprom_data },
283 	{ .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
284 	{ .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
285 	{ .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
286 	{ .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
287 	{ .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
288 	{ .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
289 	{ .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
290 	{ .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
291 	{ .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
292 	{ .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data },
293 	{ .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
294 	{ .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
295 	{ .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
296 	{ .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
297 	{ .compatible = "st,24256e-wl", (ulong)&st24256e_wlp_data },
298 	{ }
299 };
300 
301 U_BOOT_DRIVER(i2c_eeprom_std) = {
302 	.name			= "i2c_eeprom",
303 	.id			= UCLASS_I2C_EEPROM,
304 	.of_match		= i2c_eeprom_std_ids,
305 	.bind			= i2c_eeprom_std_bind,
306 	.probe			= i2c_eeprom_std_probe,
307 	.of_to_plat	= i2c_eeprom_std_of_to_plat,
308 	.priv_auto	= sizeof(struct i2c_eeprom),
309 	.ops			= &i2c_eeprom_std_ops,
310 };
311 
312 struct i2c_eeprom_partition {
313 	u32 offset;
314 	u32 size;
315 };
316 
i2c_eeprom_partition_probe(struct udevice * dev)317 static int i2c_eeprom_partition_probe(struct udevice *dev)
318 {
319 	return 0;
320 }
321 
i2c_eeprom_partition_of_to_plat(struct udevice * dev)322 static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
323 {
324 	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
325 	u32 reg[2];
326 	int ret;
327 
328 	ret = dev_read_u32_array(dev, "reg", reg, 2);
329 	if (ret)
330 		return ret;
331 
332 	if (!reg[1])
333 		return -EINVAL;
334 
335 	priv->offset = reg[0];
336 	priv->size = reg[1];
337 
338 	debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
339 
340 	return 0;
341 }
342 
i2c_eeprom_partition_read(struct udevice * dev,int offset,u8 * buf,int size)343 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
344 				     u8 *buf, int size)
345 {
346 	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
347 	struct udevice *parent = dev_get_parent(dev);
348 
349 	if (!parent)
350 		return -ENODEV;
351 	if (offset + size > priv->size)
352 		return -EINVAL;
353 
354 	return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
355 }
356 
i2c_eeprom_partition_write(struct udevice * dev,int offset,const u8 * buf,int size)357 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
358 				      const u8 *buf, int size)
359 {
360 	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
361 	struct udevice *parent = dev_get_parent(dev);
362 
363 	if (!parent)
364 		return -ENODEV;
365 	if (offset + size > priv->size)
366 		return -EINVAL;
367 
368 	return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
369 				size);
370 }
371 
i2c_eeprom_partition_size(struct udevice * dev)372 static int i2c_eeprom_partition_size(struct udevice *dev)
373 {
374 	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
375 
376 	return priv->size;
377 }
378 
379 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
380 	.read	= i2c_eeprom_partition_read,
381 	.write	= i2c_eeprom_partition_write,
382 	.size	= i2c_eeprom_partition_size,
383 };
384 
385 U_BOOT_DRIVER(i2c_eeprom_partition) = {
386 	.name			= "i2c_eeprom_partition",
387 	.id			= UCLASS_I2C_EEPROM,
388 	.probe			= i2c_eeprom_partition_probe,
389 	.of_to_plat	= i2c_eeprom_partition_of_to_plat,
390 	.priv_auto	= sizeof(struct i2c_eeprom_partition),
391 	.ops			= &i2c_eeprom_partition_ops,
392 };
393 
394 UCLASS_DRIVER(i2c_eeprom) = {
395 	.id		= UCLASS_I2C_EEPROM,
396 	.name		= "i2c_eeprom",
397 };
398