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