blob: 89a450d0f8d6f83683e88e1af1a96ff20fd8270b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass20142012014-12-10 08:55:54 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass20142012014-12-10 08:55:54 -07004 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_I2C_EEPROM
7
Simon Glass20142012014-12-10 08:55:54 -07008#include <common.h>
Simon Glasscb3ef682019-11-14 12:57:50 -07009#include <eeprom.h>
Simon Glassc05ed002020-05-10 11:40:11 -060010#include <linux/delay.h>
Masahiro Yamadaee5ee872014-12-18 20:00:26 +090011#include <linux/err.h>
Baruch Siach84c80c62019-04-07 12:38:49 +030012#include <linux/kernel.h>
Simon Glass20142012014-12-10 08:55:54 -070013#include <dm.h>
Robert Beckett1a59cb42019-10-28 18:29:05 +000014#include <dm/device-internal.h>
Simon Glass20142012014-12-10 08:55:54 -070015#include <i2c.h>
16#include <i2c_eeprom.h>
17
Robert Beckett033e18b2019-10-28 18:29:06 +000018struct i2c_eeprom_drv_data {
19 u32 size; /* size in bytes */
Masahiro Yamada0c17bb12020-02-28 22:04:14 +090020 u32 pagesize; /* page size in bytes */
Robert Beckett821c9822020-01-31 15:07:52 +020021 u32 addr_offset_mask; /* bits in addr used for offset overflow */
22 u32 offset_len; /* size in bytes of offset */
Eugen Hristevb24dc832020-05-07 11:53:18 +030023 u32 start_offset; /* valid start offset inside memory, by default 0 */
Robert Beckett033e18b2019-10-28 18:29:06 +000024};
25
Jonas Karlman8880efb2017-04-22 08:57:41 +000026int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
27{
28 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
29
30 if (!ops->read)
31 return -ENOSYS;
32
33 return ops->read(dev, offset, buf, size);
34}
35
36int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
37{
38 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
39
40 if (!ops->write)
41 return -ENOSYS;
42
43 return ops->write(dev, offset, buf, size);
44}
45
Robert Beckett033e18b2019-10-28 18:29:06 +000046int i2c_eeprom_size(struct udevice *dev)
47{
48 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
49
50 if (!ops->size)
51 return -ENOSYS;
52
53 return ops->size(dev);
54}
55
Jonas Karlman8880efb2017-04-22 08:57:41 +000056static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
57 int size)
Simon Glass20142012014-12-10 08:55:54 -070058{
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +020059 return dm_i2c_read(dev, offset, buf, size);
Simon Glass20142012014-12-10 08:55:54 -070060}
61
Jonas Karlman8880efb2017-04-22 08:57:41 +000062static int i2c_eeprom_std_write(struct udevice *dev, int offset,
63 const uint8_t *buf, int size)
Simon Glass20142012014-12-10 08:55:54 -070064{
Baruch Siach84c80c62019-04-07 12:38:49 +030065 struct i2c_eeprom *priv = dev_get_priv(dev);
66 int ret;
67
68 while (size > 0) {
69 int write_size = min_t(int, size, priv->pagesize);
70
71 ret = dm_i2c_write(dev, offset, buf, write_size);
72 if (ret)
73 return ret;
74
75 offset += write_size;
76 buf += write_size;
77 size -= write_size;
78
79 udelay(10000);
80 }
81
82 return 0;
Simon Glass20142012014-12-10 08:55:54 -070083}
84
Robert Beckett033e18b2019-10-28 18:29:06 +000085static int i2c_eeprom_std_size(struct udevice *dev)
86{
87 struct i2c_eeprom *priv = dev_get_priv(dev);
88
89 return priv->size;
90}
91
Masahiro Yamada5e75ea12017-06-22 16:51:22 +090092static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8880efb2017-04-22 08:57:41 +000093 .read = i2c_eeprom_std_read,
94 .write = i2c_eeprom_std_write,
Robert Beckett033e18b2019-10-28 18:29:06 +000095 .size = i2c_eeprom_std_size,
Simon Glass20142012014-12-10 08:55:54 -070096};
97
Simon Glassd1998a92020-12-03 16:55:21 -070098static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +020099{
100 struct i2c_eeprom *priv = dev_get_priv(dev);
Robert Beckett033e18b2019-10-28 18:29:06 +0000101 struct i2c_eeprom_drv_data *data =
102 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
Baruch Siacha29034d2019-04-07 12:38:48 +0300103 u32 pagesize;
Robert Beckett033e18b2019-10-28 18:29:06 +0000104 u32 size;
Baruch Siacha29034d2019-04-07 12:38:48 +0300105
Masahiro Yamadaed6dd4e2020-02-28 22:04:13 +0900106 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
Baruch Siacha29034d2019-04-07 12:38:48 +0300107 priv->pagesize = pagesize;
Masahiro Yamadaed6dd4e2020-02-28 22:04:13 +0900108 else
Robert Beckett033e18b2019-10-28 18:29:06 +0000109 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900110 priv->pagesize = data->pagesize;
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +0200111
Robert Beckett033e18b2019-10-28 18:29:06 +0000112 if (dev_read_u32(dev, "size", &size) == 0)
113 priv->size = size;
114 else
115 priv->size = data->size;
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +0200116
117 return 0;
118}
119
Robert Beckett1a59cb42019-10-28 18:29:05 +0000120static int i2c_eeprom_std_bind(struct udevice *dev)
121{
122 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
123 ofnode partition;
124 const char *name;
125
126 if (!ofnode_valid(partitions))
127 return 0;
128 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
129 return -ENOTSUPP;
130
131 ofnode_for_each_subnode(partition, partitions) {
132 name = ofnode_get_name(partition);
133 if (!name)
134 continue;
135
Simon Glass65e25be2020-12-28 20:34:56 -0700136 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
Simon Glass734206d2020-11-28 17:50:01 -0700137 NULL, partition, NULL);
Robert Beckett1a59cb42019-10-28 18:29:05 +0000138 }
139
140 return 0;
141}
142
Masahiro Yamada5e75ea12017-06-22 16:51:22 +0900143static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass20142012014-12-10 08:55:54 -0700144{
Baruch Siach5ae84862019-08-05 09:03:30 +0300145 u8 test_byte;
146 int ret;
Robert Beckett821c9822020-01-31 15:07:52 +0200147 struct i2c_eeprom_drv_data *data =
148 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
149
150 i2c_set_chip_offset_len(dev, data->offset_len);
151 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
Baruch Siach5ae84862019-08-05 09:03:30 +0300152
153 /* Verify that the chip is functional */
Eugen Hristevb24dc832020-05-07 11:53:18 +0300154 /*
155 * Not all eeproms start from offset 0. Valid offset is available
156 * in the platform data struct.
157 */
158 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
Baruch Siach5ae84862019-08-05 09:03:30 +0300159 if (ret)
160 return -ENODEV;
161
Simon Glass20142012014-12-10 08:55:54 -0700162 return 0;
163}
164
Robert Beckett033e18b2019-10-28 18:29:06 +0000165static const struct i2c_eeprom_drv_data eeprom_data = {
166 .size = 0,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900167 .pagesize = 1,
Robert Beckett821c9822020-01-31 15:07:52 +0200168 .addr_offset_mask = 0,
169 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000170};
171
172static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
173 .size = 256,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900174 .pagesize = 8,
Robert Beckett821c9822020-01-31 15:07:52 +0200175 .addr_offset_mask = 0,
176 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000177};
178
179static const struct i2c_eeprom_drv_data atmel24c01a_data = {
180 .size = 128,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900181 .pagesize = 8,
Robert Beckett821c9822020-01-31 15:07:52 +0200182 .addr_offset_mask = 0,
183 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000184};
185
186static const struct i2c_eeprom_drv_data atmel24c02_data = {
187 .size = 256,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900188 .pagesize = 8,
Robert Beckett821c9822020-01-31 15:07:52 +0200189 .addr_offset_mask = 0,
190 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000191};
192
193static const struct i2c_eeprom_drv_data atmel24c04_data = {
194 .size = 512,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900195 .pagesize = 16,
Robert Beckett821c9822020-01-31 15:07:52 +0200196 .addr_offset_mask = 0x1,
197 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000198};
199
200static const struct i2c_eeprom_drv_data atmel24c08_data = {
201 .size = 1024,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900202 .pagesize = 16,
Robert Beckett821c9822020-01-31 15:07:52 +0200203 .addr_offset_mask = 0x3,
204 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000205};
206
207static const struct i2c_eeprom_drv_data atmel24c08a_data = {
208 .size = 1024,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900209 .pagesize = 16,
Robert Beckett821c9822020-01-31 15:07:52 +0200210 .addr_offset_mask = 0x3,
211 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000212};
213
214static const struct i2c_eeprom_drv_data atmel24c16a_data = {
215 .size = 2048,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900216 .pagesize = 16,
Robert Beckett821c9822020-01-31 15:07:52 +0200217 .addr_offset_mask = 0x7,
218 .offset_len = 1,
Robert Beckett033e18b2019-10-28 18:29:06 +0000219};
220
221static const struct i2c_eeprom_drv_data atmel24mac402_data = {
222 .size = 256,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900223 .pagesize = 16,
Robert Beckett821c9822020-01-31 15:07:52 +0200224 .addr_offset_mask = 0,
225 .offset_len = 1,
Eugen Hristevb24dc832020-05-07 11:53:18 +0300226 .start_offset = 0x80,
Robert Beckett033e18b2019-10-28 18:29:06 +0000227};
228
229static const struct i2c_eeprom_drv_data atmel24c32_data = {
230 .size = 4096,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900231 .pagesize = 32,
Robert Beckett821c9822020-01-31 15:07:52 +0200232 .addr_offset_mask = 0,
233 .offset_len = 2,
Robert Beckett033e18b2019-10-28 18:29:06 +0000234};
235
236static const struct i2c_eeprom_drv_data atmel24c64_data = {
237 .size = 8192,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900238 .pagesize = 32,
Robert Beckett821c9822020-01-31 15:07:52 +0200239 .addr_offset_mask = 0,
240 .offset_len = 2,
Robert Beckett033e18b2019-10-28 18:29:06 +0000241};
242
243static const struct i2c_eeprom_drv_data atmel24c128_data = {
244 .size = 16384,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900245 .pagesize = 64,
Robert Beckett821c9822020-01-31 15:07:52 +0200246 .addr_offset_mask = 0,
247 .offset_len = 2,
Robert Beckett033e18b2019-10-28 18:29:06 +0000248};
249
250static const struct i2c_eeprom_drv_data atmel24c256_data = {
251 .size = 32768,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900252 .pagesize = 64,
Robert Beckett821c9822020-01-31 15:07:52 +0200253 .addr_offset_mask = 0,
254 .offset_len = 2,
Robert Beckett033e18b2019-10-28 18:29:06 +0000255};
256
257static const struct i2c_eeprom_drv_data atmel24c512_data = {
258 .size = 65536,
Masahiro Yamada0c17bb12020-02-28 22:04:14 +0900259 .pagesize = 64,
Robert Beckett821c9822020-01-31 15:07:52 +0200260 .addr_offset_mask = 0,
261 .offset_len = 2,
Robert Beckett033e18b2019-10-28 18:29:06 +0000262};
263
Simon Glass20142012014-12-10 08:55:54 -0700264static const struct udevice_id i2c_eeprom_std_ids[] = {
Robert Beckett033e18b2019-10-28 18:29:06 +0000265 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
266 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
Marek Vasutfc3292c2021-07-04 21:31:18 +0200267 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
Robert Beckett033e18b2019-10-28 18:29:06 +0000268 { .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 },
Simon Glass20142012014-12-10 08:55:54 -0700280 { }
281};
282
283U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +0200284 .name = "i2c_eeprom",
285 .id = UCLASS_I2C_EEPROM,
286 .of_match = i2c_eeprom_std_ids,
Robert Beckett1a59cb42019-10-28 18:29:05 +0000287 .bind = i2c_eeprom_std_bind,
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +0200288 .probe = i2c_eeprom_std_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700289 .of_to_plat = i2c_eeprom_std_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700290 .priv_auto = sizeof(struct i2c_eeprom),
mario.six@gdsys.ccd7e28912016-06-22 15:14:16 +0200291 .ops = &i2c_eeprom_std_ops,
Simon Glass20142012014-12-10 08:55:54 -0700292};
293
Robert Beckett1a59cb42019-10-28 18:29:05 +0000294struct i2c_eeprom_partition {
295 u32 offset;
296 u32 size;
297};
298
299static int i2c_eeprom_partition_probe(struct udevice *dev)
300{
301 return 0;
302}
303
Simon Glassd1998a92020-12-03 16:55:21 -0700304static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
Robert Beckett1a59cb42019-10-28 18:29:05 +0000305{
306 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
Michal Simekf692b472020-05-28 11:48:55 +0200307 u32 reg[2];
Robert Beckett1a59cb42019-10-28 18:29:05 +0000308 int ret;
309
Michal Simekf692b472020-05-28 11:48:55 +0200310 ret = dev_read_u32_array(dev, "reg", reg, 2);
Robert Beckett1a59cb42019-10-28 18:29:05 +0000311 if (ret)
312 return ret;
313
Michal Simekf692b472020-05-28 11:48:55 +0200314 if (!reg[1])
315 return -EINVAL;
Robert Beckett1a59cb42019-10-28 18:29:05 +0000316
Michal Simekf692b472020-05-28 11:48:55 +0200317 priv->offset = reg[0];
318 priv->size = reg[1];
319
320 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
Robert Beckett1a59cb42019-10-28 18:29:05 +0000321
322 return 0;
323}
324
325static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
326 u8 *buf, int size)
327{
328 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
329 struct udevice *parent = dev_get_parent(dev);
330
331 if (!parent)
332 return -ENODEV;
333 if (offset + size > priv->size)
334 return -EINVAL;
335
336 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
337}
338
339static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
340 const u8 *buf, int size)
341{
342 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
343 struct udevice *parent = dev_get_parent(dev);
344
345 if (!parent)
346 return -ENODEV;
347 if (offset + size > priv->size)
348 return -EINVAL;
349
350 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
351 size);
352}
353
Robert Beckett033e18b2019-10-28 18:29:06 +0000354static int i2c_eeprom_partition_size(struct udevice *dev)
355{
356 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
357
358 return priv->size;
359}
360
Robert Beckett1a59cb42019-10-28 18:29:05 +0000361static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
362 .read = i2c_eeprom_partition_read,
363 .write = i2c_eeprom_partition_write,
Robert Beckett033e18b2019-10-28 18:29:06 +0000364 .size = i2c_eeprom_partition_size,
Robert Beckett1a59cb42019-10-28 18:29:05 +0000365};
366
367U_BOOT_DRIVER(i2c_eeprom_partition) = {
368 .name = "i2c_eeprom_partition",
369 .id = UCLASS_I2C_EEPROM,
370 .probe = i2c_eeprom_partition_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700371 .of_to_plat = i2c_eeprom_partition_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700372 .priv_auto = sizeof(struct i2c_eeprom_partition),
Robert Beckett1a59cb42019-10-28 18:29:05 +0000373 .ops = &i2c_eeprom_partition_ops,
374};
375
Simon Glass20142012014-12-10 08:55:54 -0700376UCLASS_DRIVER(i2c_eeprom) = {
377 .id = UCLASS_I2C_EEPROM,
378 .name = "i2c_eeprom",
379};