1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
9 #include <linux/kernel.h>
11 #include <dm/device-internal.h>
13 #include <i2c_eeprom.h>
15 struct i2c_eeprom_drv_data {
16 u32 size; /* size in bytes */
17 u32 pagewidth; /* pagesize = 2^pagewidth */
18 u32 addr_offset_mask; /* bits in addr used for offset overflow */
19 u32 offset_len; /* size in bytes of offset */
22 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
24 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
29 return ops->read(dev, offset, buf, size);
32 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
34 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
39 return ops->write(dev, offset, buf, size);
42 int i2c_eeprom_size(struct udevice *dev)
44 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
49 return ops->size(dev);
52 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
55 return dm_i2c_read(dev, offset, buf, size);
58 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
59 const uint8_t *buf, int size)
61 struct i2c_eeprom *priv = dev_get_priv(dev);
65 int write_size = min_t(int, size, priv->pagesize);
67 ret = dm_i2c_write(dev, offset, buf, write_size);
81 static int i2c_eeprom_std_size(struct udevice *dev)
83 struct i2c_eeprom *priv = dev_get_priv(dev);
88 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
89 .read = i2c_eeprom_std_read,
90 .write = i2c_eeprom_std_write,
91 .size = i2c_eeprom_std_size,
94 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
96 struct i2c_eeprom *priv = dev_get_priv(dev);
97 struct i2c_eeprom_drv_data *data =
98 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
102 if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
103 priv->pagesize = pagesize;
105 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
106 priv->pagewidth = data->pagewidth;
107 priv->pagesize = (1 << priv->pagewidth);
110 if (dev_read_u32(dev, "size", &size) == 0)
113 priv->size = data->size;
118 static int i2c_eeprom_std_bind(struct udevice *dev)
120 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
124 if (!ofnode_valid(partitions))
126 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
129 ofnode_for_each_subnode(partition, partitions) {
130 name = ofnode_get_name(partition);
134 device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
135 name, NULL, partition, NULL);
141 static int i2c_eeprom_std_probe(struct udevice *dev)
145 struct i2c_eeprom_drv_data *data =
146 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
148 i2c_set_chip_offset_len(dev, data->offset_len);
149 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
151 /* Verify that the chip is functional */
152 ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
159 static const struct i2c_eeprom_drv_data eeprom_data = {
162 .addr_offset_mask = 0,
166 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
169 .addr_offset_mask = 0,
173 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
176 .addr_offset_mask = 0,
180 static const struct i2c_eeprom_drv_data atmel24c02_data = {
183 .addr_offset_mask = 0,
187 static const struct i2c_eeprom_drv_data atmel24c04_data = {
190 .addr_offset_mask = 0x1,
194 static const struct i2c_eeprom_drv_data atmel24c08_data = {
197 .addr_offset_mask = 0x3,
201 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
204 .addr_offset_mask = 0x3,
208 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
211 .addr_offset_mask = 0x7,
215 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
218 .addr_offset_mask = 0,
222 static const struct i2c_eeprom_drv_data atmel24c32_data = {
225 .addr_offset_mask = 0,
229 static const struct i2c_eeprom_drv_data atmel24c64_data = {
232 .addr_offset_mask = 0,
236 static const struct i2c_eeprom_drv_data atmel24c128_data = {
239 .addr_offset_mask = 0,
243 static const struct i2c_eeprom_drv_data atmel24c256_data = {
246 .addr_offset_mask = 0,
250 static const struct i2c_eeprom_drv_data atmel24c512_data = {
253 .addr_offset_mask = 0,
257 static const struct udevice_id i2c_eeprom_std_ids[] = {
258 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
259 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
260 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
261 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
262 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
263 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
264 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
265 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
266 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
267 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
268 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
269 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
270 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
271 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
275 U_BOOT_DRIVER(i2c_eeprom_std) = {
276 .name = "i2c_eeprom",
277 .id = UCLASS_I2C_EEPROM,
278 .of_match = i2c_eeprom_std_ids,
279 .bind = i2c_eeprom_std_bind,
280 .probe = i2c_eeprom_std_probe,
281 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
282 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
283 .ops = &i2c_eeprom_std_ops,
286 struct i2c_eeprom_partition {
291 static int i2c_eeprom_partition_probe(struct udevice *dev)
296 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
298 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
302 ret = dev_read_u32(dev, "offset", &offset);
306 ret = dev_read_u32(dev, "size", &size);
310 priv->offset = offset;
316 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
319 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
320 struct udevice *parent = dev_get_parent(dev);
324 if (offset + size > priv->size)
327 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
330 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
331 const u8 *buf, int size)
333 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
334 struct udevice *parent = dev_get_parent(dev);
338 if (offset + size > priv->size)
341 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
345 static int i2c_eeprom_partition_size(struct udevice *dev)
347 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
352 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
353 .read = i2c_eeprom_partition_read,
354 .write = i2c_eeprom_partition_write,
355 .size = i2c_eeprom_partition_size,
358 U_BOOT_DRIVER(i2c_eeprom_partition) = {
359 .name = "i2c_eeprom_partition",
360 .id = UCLASS_I2C_EEPROM,
361 .probe = i2c_eeprom_partition_probe,
362 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
363 .priv_auto_alloc_size = sizeof(struct i2c_eeprom_partition),
364 .ops = &i2c_eeprom_partition_ops,
367 UCLASS_DRIVER(i2c_eeprom) = {
368 .id = UCLASS_I2C_EEPROM,
369 .name = "i2c_eeprom",