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