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, uint8_t *buf, int size)
38 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
43 return ops->write(dev, offset, buf, size);
46 int i2c_eeprom_size(struct udevice *dev)
48 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
53 return ops->size(dev);
56 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
59 return dm_i2c_read(dev, offset, buf, size);
62 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
63 const uint8_t *buf, int size)
65 struct i2c_eeprom *priv = dev_get_priv(dev);
69 int write_size = min_t(int, size, priv->pagesize);
71 ret = dm_i2c_write(dev, offset, buf, write_size);
85 static int i2c_eeprom_std_size(struct udevice *dev)
87 struct i2c_eeprom *priv = dev_get_priv(dev);
92 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
93 .read = i2c_eeprom_std_read,
94 .write = i2c_eeprom_std_write,
95 .size = i2c_eeprom_std_size,
98 static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
100 struct i2c_eeprom *priv = dev_get_priv(dev);
101 struct i2c_eeprom_drv_data *data =
102 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
106 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
107 priv->pagesize = pagesize;
109 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
110 priv->pagesize = data->pagesize;
112 if (dev_read_u32(dev, "size", &size) == 0)
115 priv->size = data->size;
120 static int i2c_eeprom_std_bind(struct udevice *dev)
122 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
126 if (!ofnode_valid(partitions))
128 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
131 ofnode_for_each_subnode(partition, partitions) {
132 name = ofnode_get_name(partition);
136 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
137 NULL, partition, NULL);
143 static int i2c_eeprom_std_probe(struct udevice *dev)
147 struct i2c_eeprom_drv_data *data =
148 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
150 i2c_set_chip_offset_len(dev, data->offset_len);
151 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
153 /* Verify that the chip is functional */
155 * Not all eeproms start from offset 0. Valid offset is available
156 * in the platform data struct.
158 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
165 static const struct i2c_eeprom_drv_data eeprom_data = {
168 .addr_offset_mask = 0,
172 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
175 .addr_offset_mask = 0,
179 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
182 .addr_offset_mask = 0,
186 static const struct i2c_eeprom_drv_data atmel24c02_data = {
189 .addr_offset_mask = 0,
193 static const struct i2c_eeprom_drv_data atmel24c04_data = {
196 .addr_offset_mask = 0x1,
200 static const struct i2c_eeprom_drv_data atmel24c08_data = {
203 .addr_offset_mask = 0x3,
207 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
210 .addr_offset_mask = 0x3,
214 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
217 .addr_offset_mask = 0x7,
221 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
224 .addr_offset_mask = 0,
226 .start_offset = 0x80,
229 static const struct i2c_eeprom_drv_data atmel24c32_data = {
232 .addr_offset_mask = 0,
236 static const struct i2c_eeprom_drv_data atmel24c64_data = {
239 .addr_offset_mask = 0,
243 static const struct i2c_eeprom_drv_data atmel24c128_data = {
246 .addr_offset_mask = 0,
250 static const struct i2c_eeprom_drv_data atmel24c256_data = {
253 .addr_offset_mask = 0,
257 static const struct i2c_eeprom_drv_data atmel24c512_data = {
260 .addr_offset_mask = 0,
264 static const struct udevice_id i2c_eeprom_std_ids[] = {
265 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
266 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
267 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
268 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
269 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
270 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
271 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
272 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
273 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
274 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
275 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
276 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
277 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
278 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
279 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
283 U_BOOT_DRIVER(i2c_eeprom_std) = {
284 .name = "i2c_eeprom",
285 .id = UCLASS_I2C_EEPROM,
286 .of_match = i2c_eeprom_std_ids,
287 .bind = i2c_eeprom_std_bind,
288 .probe = i2c_eeprom_std_probe,
289 .of_to_plat = i2c_eeprom_std_of_to_plat,
290 .priv_auto = sizeof(struct i2c_eeprom),
291 .ops = &i2c_eeprom_std_ops,
294 struct i2c_eeprom_partition {
299 static int i2c_eeprom_partition_probe(struct udevice *dev)
304 static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
306 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
310 ret = dev_read_u32_array(dev, "reg", reg, 2);
317 priv->offset = reg[0];
320 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
325 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
328 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
329 struct udevice *parent = dev_get_parent(dev);
333 if (offset + size > priv->size)
336 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
339 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
340 const u8 *buf, int size)
342 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
343 struct udevice *parent = dev_get_parent(dev);
347 if (offset + size > priv->size)
350 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
354 static int i2c_eeprom_partition_size(struct udevice *dev)
356 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
361 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
362 .read = i2c_eeprom_partition_read,
363 .write = i2c_eeprom_partition_write,
364 .size = i2c_eeprom_partition_size,
367 U_BOOT_DRIVER(i2c_eeprom_partition) = {
368 .name = "i2c_eeprom_partition",
369 .id = UCLASS_I2C_EEPROM,
370 .probe = i2c_eeprom_partition_probe,
371 .of_to_plat = i2c_eeprom_partition_of_to_plat,
372 .priv_auto = sizeof(struct i2c_eeprom_partition),
373 .ops = &i2c_eeprom_partition_ops,
376 UCLASS_DRIVER(i2c_eeprom) = {
377 .id = UCLASS_I2C_EEPROM,
378 .name = "i2c_eeprom",