1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
8 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <dm/device-internal.h>
14 #include <i2c_eeprom.h>
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 */
23 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
25 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
30 return ops->read(dev, offset, buf, size);
33 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
35 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
40 return ops->write(dev, offset, buf, size);
43 int i2c_eeprom_size(struct udevice *dev)
45 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
50 return ops->size(dev);
53 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
56 return dm_i2c_read(dev, offset, buf, size);
59 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
60 const uint8_t *buf, int size)
62 struct i2c_eeprom *priv = dev_get_priv(dev);
66 int write_size = min_t(int, size, priv->pagesize);
68 ret = dm_i2c_write(dev, offset, buf, write_size);
82 static int i2c_eeprom_std_size(struct udevice *dev)
84 struct i2c_eeprom *priv = dev_get_priv(dev);
89 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
90 .read = i2c_eeprom_std_read,
91 .write = i2c_eeprom_std_write,
92 .size = i2c_eeprom_std_size,
95 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
97 struct i2c_eeprom *priv = dev_get_priv(dev);
98 struct i2c_eeprom_drv_data *data =
99 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
103 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
104 priv->pagesize = pagesize;
106 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
107 priv->pagesize = data->pagesize;
109 if (dev_read_u32(dev, "size", &size) == 0)
112 priv->size = data->size;
117 static int i2c_eeprom_std_bind(struct udevice *dev)
119 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
123 if (!ofnode_valid(partitions))
125 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
128 ofnode_for_each_subnode(partition, partitions) {
129 name = ofnode_get_name(partition);
133 device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
134 name, NULL, partition, NULL);
140 static int i2c_eeprom_std_probe(struct udevice *dev)
144 struct i2c_eeprom_drv_data *data =
145 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
147 i2c_set_chip_offset_len(dev, data->offset_len);
148 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
150 /* Verify that the chip is functional */
151 ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
158 static const struct i2c_eeprom_drv_data eeprom_data = {
161 .addr_offset_mask = 0,
165 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
168 .addr_offset_mask = 0,
172 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
175 .addr_offset_mask = 0,
179 static const struct i2c_eeprom_drv_data atmel24c02_data = {
182 .addr_offset_mask = 0,
186 static const struct i2c_eeprom_drv_data atmel24c04_data = {
189 .addr_offset_mask = 0x1,
193 static const struct i2c_eeprom_drv_data atmel24c08_data = {
196 .addr_offset_mask = 0x3,
200 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
203 .addr_offset_mask = 0x3,
207 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
210 .addr_offset_mask = 0x7,
214 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
217 .addr_offset_mask = 0,
221 static const struct i2c_eeprom_drv_data atmel24c32_data = {
224 .addr_offset_mask = 0,
228 static const struct i2c_eeprom_drv_data atmel24c64_data = {
231 .addr_offset_mask = 0,
235 static const struct i2c_eeprom_drv_data atmel24c128_data = {
238 .addr_offset_mask = 0,
242 static const struct i2c_eeprom_drv_data atmel24c256_data = {
245 .addr_offset_mask = 0,
249 static const struct i2c_eeprom_drv_data atmel24c512_data = {
252 .addr_offset_mask = 0,
256 static const struct udevice_id i2c_eeprom_std_ids[] = {
257 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
258 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
259 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
260 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
261 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
262 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
263 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
264 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
265 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
266 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
267 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
268 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
269 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
270 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
274 U_BOOT_DRIVER(i2c_eeprom_std) = {
275 .name = "i2c_eeprom",
276 .id = UCLASS_I2C_EEPROM,
277 .of_match = i2c_eeprom_std_ids,
278 .bind = i2c_eeprom_std_bind,
279 .probe = i2c_eeprom_std_probe,
280 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
281 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
282 .ops = &i2c_eeprom_std_ops,
285 struct i2c_eeprom_partition {
290 static int i2c_eeprom_partition_probe(struct udevice *dev)
295 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
297 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
301 ret = dev_read_u32(dev, "offset", &offset);
305 ret = dev_read_u32(dev, "size", &size);
309 priv->offset = offset;
315 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
318 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
319 struct udevice *parent = dev_get_parent(dev);
323 if (offset + size > priv->size)
326 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
329 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
330 const u8 *buf, int size)
332 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
333 struct udevice *parent = dev_get_parent(dev);
337 if (offset + size > priv->size)
340 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
344 static int i2c_eeprom_partition_size(struct udevice *dev)
346 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
351 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
352 .read = i2c_eeprom_partition_read,
353 .write = i2c_eeprom_partition_write,
354 .size = i2c_eeprom_partition_size,
357 U_BOOT_DRIVER(i2c_eeprom_partition) = {
358 .name = "i2c_eeprom_partition",
359 .id = UCLASS_I2C_EEPROM,
360 .probe = i2c_eeprom_partition_probe,
361 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
362 .priv_auto_alloc_size = sizeof(struct i2c_eeprom_partition),
363 .ops = &i2c_eeprom_partition_ops,
366 UCLASS_DRIVER(i2c_eeprom) = {
367 .id = UCLASS_I2C_EEPROM,
368 .name = "i2c_eeprom",