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 */
21 u32 start_offset; /* valid start offset inside memory, by default 0 */
24 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
26 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
31 return ops->read(dev, offset, buf, size);
34 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
36 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
41 return ops->write(dev, offset, buf, size);
44 int i2c_eeprom_size(struct udevice *dev)
46 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
51 return ops->size(dev);
54 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
57 return dm_i2c_read(dev, offset, buf, size);
60 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
61 const uint8_t *buf, int size)
63 struct i2c_eeprom *priv = dev_get_priv(dev);
67 int write_size = min_t(int, size, priv->pagesize);
69 ret = dm_i2c_write(dev, offset, buf, write_size);
83 static int i2c_eeprom_std_size(struct udevice *dev)
85 struct i2c_eeprom *priv = dev_get_priv(dev);
90 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
91 .read = i2c_eeprom_std_read,
92 .write = i2c_eeprom_std_write,
93 .size = i2c_eeprom_std_size,
96 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
98 struct i2c_eeprom *priv = dev_get_priv(dev);
99 struct i2c_eeprom_drv_data *data =
100 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
104 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
105 priv->pagesize = pagesize;
107 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
108 priv->pagesize = data->pagesize;
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(dev, DM_GET_DRIVER(i2c_eeprom_partition), name,
135 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 */
153 * Not all eeproms start from offset 0. Valid offset is available
154 * in the platform data struct.
156 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
163 static const struct i2c_eeprom_drv_data eeprom_data = {
166 .addr_offset_mask = 0,
170 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
173 .addr_offset_mask = 0,
177 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
180 .addr_offset_mask = 0,
184 static const struct i2c_eeprom_drv_data atmel24c02_data = {
187 .addr_offset_mask = 0,
191 static const struct i2c_eeprom_drv_data atmel24c04_data = {
194 .addr_offset_mask = 0x1,
198 static const struct i2c_eeprom_drv_data atmel24c08_data = {
201 .addr_offset_mask = 0x3,
205 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
208 .addr_offset_mask = 0x3,
212 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
215 .addr_offset_mask = 0x7,
219 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
222 .addr_offset_mask = 0,
224 .start_offset = 0x80,
227 static const struct i2c_eeprom_drv_data atmel24c32_data = {
230 .addr_offset_mask = 0,
234 static const struct i2c_eeprom_drv_data atmel24c64_data = {
237 .addr_offset_mask = 0,
241 static const struct i2c_eeprom_drv_data atmel24c128_data = {
244 .addr_offset_mask = 0,
248 static const struct i2c_eeprom_drv_data atmel24c256_data = {
251 .addr_offset_mask = 0,
255 static const struct i2c_eeprom_drv_data atmel24c512_data = {
258 .addr_offset_mask = 0,
262 static const struct udevice_id i2c_eeprom_std_ids[] = {
263 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
264 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
265 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
266 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
267 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
268 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
269 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
270 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
271 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
272 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
273 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
274 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
275 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
276 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
280 U_BOOT_DRIVER(i2c_eeprom_std) = {
281 .name = "i2c_eeprom",
282 .id = UCLASS_I2C_EEPROM,
283 .of_match = i2c_eeprom_std_ids,
284 .bind = i2c_eeprom_std_bind,
285 .probe = i2c_eeprom_std_probe,
286 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
287 .priv_auto = sizeof(struct i2c_eeprom),
288 .ops = &i2c_eeprom_std_ops,
291 struct i2c_eeprom_partition {
296 static int i2c_eeprom_partition_probe(struct udevice *dev)
301 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
303 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
307 ret = dev_read_u32_array(dev, "reg", reg, 2);
314 priv->offset = reg[0];
317 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
322 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
325 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
326 struct udevice *parent = dev_get_parent(dev);
330 if (offset + size > priv->size)
333 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
336 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
337 const u8 *buf, int size)
339 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
340 struct udevice *parent = dev_get_parent(dev);
344 if (offset + size > priv->size)
347 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
351 static int i2c_eeprom_partition_size(struct udevice *dev)
353 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
358 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
359 .read = i2c_eeprom_partition_read,
360 .write = i2c_eeprom_partition_write,
361 .size = i2c_eeprom_partition_size,
364 U_BOOT_DRIVER(i2c_eeprom_partition) = {
365 .name = "i2c_eeprom_partition",
366 .id = UCLASS_I2C_EEPROM,
367 .probe = i2c_eeprom_partition_probe,
368 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
369 .priv_auto = sizeof(struct i2c_eeprom_partition),
370 .ops = &i2c_eeprom_partition_ops,
373 UCLASS_DRIVER(i2c_eeprom) = {
374 .id = UCLASS_I2C_EEPROM,
375 .name = "i2c_eeprom",