1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_I2C_EEPROM
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
14 #include <dm/device-internal.h>
16 #include <i2c_eeprom.h>
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 */
26 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
28 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
33 return ops->read(dev, offset, buf, size);
36 int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
39 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
44 return ops->write(dev, offset, buf, size);
47 int i2c_eeprom_size(struct udevice *dev)
49 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
54 return ops->size(dev);
57 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
60 return dm_i2c_read(dev, offset, buf, size);
63 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
64 const uint8_t *buf, int size)
66 struct i2c_eeprom *priv = dev_get_priv(dev);
70 int write_size = min_t(int, size, priv->pagesize);
72 ret = dm_i2c_write(dev, offset, buf, write_size);
86 static int i2c_eeprom_std_size(struct udevice *dev)
88 struct i2c_eeprom *priv = dev_get_priv(dev);
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,
99 static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
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);
107 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
108 priv->pagesize = pagesize;
110 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
111 priv->pagesize = data->pagesize;
113 if (dev_read_u32(dev, "size", &size) == 0)
116 priv->size = data->size;
121 static int i2c_eeprom_std_bind(struct udevice *dev)
123 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
127 if (!ofnode_valid(partitions))
129 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
132 ofnode_for_each_subnode(partition, partitions) {
133 name = ofnode_get_name(partition);
137 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
138 NULL, partition, NULL);
144 static int i2c_eeprom_std_probe(struct udevice *dev)
148 struct i2c_eeprom_drv_data *data =
149 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
151 i2c_set_chip_offset_len(dev, data->offset_len);
152 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
154 /* Verify that the chip is functional */
156 * Not all eeproms start from offset 0. Valid offset is available
157 * in the platform data struct.
159 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
166 static const struct i2c_eeprom_drv_data eeprom_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,
220 .start_offset = 0x80,
223 static const struct i2c_eeprom_drv_data atmel24c32_data = {
226 .addr_offset_mask = 0,
230 static const struct i2c_eeprom_drv_data atmel24c64_data = {
233 .addr_offset_mask = 0,
237 static const struct i2c_eeprom_drv_data atmel24c128_data = {
240 .addr_offset_mask = 0,
244 static const struct i2c_eeprom_drv_data atmel24c256_data = {
247 .addr_offset_mask = 0,
251 static const struct i2c_eeprom_drv_data atmel24c512_data = {
254 .addr_offset_mask = 0,
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 },
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,
287 struct i2c_eeprom_partition {
292 static int i2c_eeprom_partition_probe(struct udevice *dev)
297 static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
299 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
303 ret = dev_read_u32_array(dev, "reg", reg, 2);
310 priv->offset = reg[0];
313 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
318 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
321 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
322 struct udevice *parent = dev_get_parent(dev);
326 if (offset + size > priv->size)
329 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
332 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
333 const u8 *buf, int size)
335 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
336 struct udevice *parent = dev_get_parent(dev);
340 if (offset + size > priv->size)
343 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
347 static int i2c_eeprom_partition_size(struct udevice *dev)
349 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
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,
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,
369 UCLASS_DRIVER(i2c_eeprom) = {
370 .id = UCLASS_I2C_EEPROM,
371 .name = "i2c_eeprom",