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_len(int offset, int len, int pagesize)
65 int page_offset = offset & (pagesize - 1);
66 int maxlen = pagesize - page_offset;
74 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
75 const uint8_t *buf, int size)
77 struct i2c_eeprom *priv = dev_get_priv(dev);
81 int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
83 ret = dm_i2c_write(dev, offset, buf, write_size);
97 static int i2c_eeprom_std_size(struct udevice *dev)
99 struct i2c_eeprom *priv = dev_get_priv(dev);
104 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
105 .read = i2c_eeprom_std_read,
106 .write = i2c_eeprom_std_write,
107 .size = i2c_eeprom_std_size,
110 static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
112 struct i2c_eeprom *priv = dev_get_priv(dev);
113 struct i2c_eeprom_drv_data *data =
114 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
118 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
119 priv->pagesize = pagesize;
121 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
122 priv->pagesize = data->pagesize;
124 if (dev_read_u32(dev, "size", &size) == 0)
127 priv->size = data->size;
132 static int i2c_eeprom_std_bind(struct udevice *dev)
134 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
138 if (!ofnode_valid(partitions))
140 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
143 ofnode_for_each_subnode(partition, partitions) {
144 name = ofnode_get_name(partition);
148 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
149 NULL, partition, NULL);
155 static int i2c_eeprom_std_probe(struct udevice *dev)
159 struct i2c_eeprom_drv_data *data =
160 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
162 i2c_set_chip_offset_len(dev, data->offset_len);
163 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
165 /* Verify that the chip is functional */
167 * Not all eeproms start from offset 0. Valid offset is available
168 * in the platform data struct.
170 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
177 static const struct i2c_eeprom_drv_data eeprom_data = {
180 .addr_offset_mask = 0,
184 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
187 .addr_offset_mask = 0,
191 static const struct i2c_eeprom_drv_data atmel24c02_data = {
194 .addr_offset_mask = 0,
198 static const struct i2c_eeprom_drv_data atmel24c04_data = {
201 .addr_offset_mask = 0x1,
205 static const struct i2c_eeprom_drv_data atmel24c08_data = {
208 .addr_offset_mask = 0x3,
212 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
215 .addr_offset_mask = 0x3,
219 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
222 .addr_offset_mask = 0x7,
226 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
229 .addr_offset_mask = 0,
231 .start_offset = 0x80,
234 static const struct i2c_eeprom_drv_data atmel24c32_data = {
237 .addr_offset_mask = 0,
241 static const struct i2c_eeprom_drv_data atmel24c32d_wlp_data = {
244 .addr_offset_mask = 0,
248 static const struct i2c_eeprom_drv_data atmel24c64_data = {
251 .addr_offset_mask = 0,
255 static const struct i2c_eeprom_drv_data atmel24c128_data = {
258 .addr_offset_mask = 0,
262 static const struct i2c_eeprom_drv_data atmel24c256_data = {
265 .addr_offset_mask = 0,
269 static const struct i2c_eeprom_drv_data atmel24c512_data = {
272 .addr_offset_mask = 0,
276 static const struct udevice_id i2c_eeprom_std_ids[] = {
277 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
278 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
279 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
280 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
281 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
282 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
283 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
284 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
285 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
286 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
287 { .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data },
288 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
289 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
290 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
291 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
295 U_BOOT_DRIVER(i2c_eeprom_std) = {
296 .name = "i2c_eeprom",
297 .id = UCLASS_I2C_EEPROM,
298 .of_match = i2c_eeprom_std_ids,
299 .bind = i2c_eeprom_std_bind,
300 .probe = i2c_eeprom_std_probe,
301 .of_to_plat = i2c_eeprom_std_of_to_plat,
302 .priv_auto = sizeof(struct i2c_eeprom),
303 .ops = &i2c_eeprom_std_ops,
306 struct i2c_eeprom_partition {
311 static int i2c_eeprom_partition_probe(struct udevice *dev)
316 static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
318 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
322 ret = dev_read_u32_array(dev, "reg", reg, 2);
329 priv->offset = reg[0];
332 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
337 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
340 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
341 struct udevice *parent = dev_get_parent(dev);
345 if (offset + size > priv->size)
348 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
351 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
352 const u8 *buf, int size)
354 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
355 struct udevice *parent = dev_get_parent(dev);
359 if (offset + size > priv->size)
362 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
366 static int i2c_eeprom_partition_size(struct udevice *dev)
368 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
373 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
374 .read = i2c_eeprom_partition_read,
375 .write = i2c_eeprom_partition_write,
376 .size = i2c_eeprom_partition_size,
379 U_BOOT_DRIVER(i2c_eeprom_partition) = {
380 .name = "i2c_eeprom_partition",
381 .id = UCLASS_I2C_EEPROM,
382 .probe = i2c_eeprom_partition_probe,
383 .of_to_plat = i2c_eeprom_partition_of_to_plat,
384 .priv_auto = sizeof(struct i2c_eeprom_partition),
385 .ops = &i2c_eeprom_partition_ops,
388 UCLASS_DRIVER(i2c_eeprom) = {
389 .id = UCLASS_I2C_EEPROM,
390 .name = "i2c_eeprom",