blob: 9111bd724cbbfb097460e4bae87343254d60143a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6ca4ba02014-12-10 08:55:54 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass6ca4ba02014-12-10 08:55:54 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_I2C_EEPROM
7
Simon Glass6ca4ba02014-12-10 08:55:54 -07008#include <common.h>
Simon Glasseba6b8d2019-11-14 12:57:50 -07009#include <eeprom.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Masahiro Yamadaf9d52cc2014-12-18 20:00:26 +090011#include <linux/err.h>
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030012#include <linux/kernel.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070013#include <dm.h>
Robert Beckettf030d6f2019-10-28 18:29:05 +000014#include <dm/device-internal.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070015#include <i2c.h>
16#include <i2c_eeprom.h>
17
Robert Beckett069ddc72019-10-28 18:29:06 +000018struct i2c_eeprom_drv_data {
19 u32 size; /* size in bytes */
Masahiro Yamada2a579372020-02-28 22:04:14 +090020 u32 pagesize; /* page size in bytes */
Robert Beckett44cf4612020-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 Hristev24d8b752020-05-07 11:53:18 +030023 u32 start_offset; /* valid start offset inside memory, by default 0 */
Robert Beckett069ddc72019-10-28 18:29:06 +000024};
25
Jonas Karlman8427a242017-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
Sean Anderson4a55f9d2022-05-05 13:11:37 -040036int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
37 int size)
Jonas Karlman8427a242017-04-22 08:57:41 +000038{
39 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
40
41 if (!ops->write)
42 return -ENOSYS;
43
44 return ops->write(dev, offset, buf, size);
45}
46
Robert Beckett069ddc72019-10-28 18:29:06 +000047int i2c_eeprom_size(struct udevice *dev)
48{
49 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
50
51 if (!ops->size)
52 return -ENOSYS;
53
54 return ops->size(dev);
55}
56
Jonas Karlman8427a242017-04-22 08:57:41 +000057static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
58 int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070059{
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020060 return dm_i2c_read(dev, offset, buf, size);
Simon Glass6ca4ba02014-12-10 08:55:54 -070061}
62
Michel Alex69f207f2023-10-26 05:47:41 +000063static int i2c_eeprom_len(int offset, int len, int pagesize)
64{
65 int page_offset = offset & (pagesize - 1);
66 int maxlen = pagesize - page_offset;
67
68 if (len > maxlen)
69 len = maxlen;
70
71 return len;
72}
73
Jonas Karlman8427a242017-04-22 08:57:41 +000074static int i2c_eeprom_std_write(struct udevice *dev, int offset,
75 const uint8_t *buf, int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070076{
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030077 struct i2c_eeprom *priv = dev_get_priv(dev);
78 int ret;
79
80 while (size > 0) {
Michel Alex69f207f2023-10-26 05:47:41 +000081 int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030082
83 ret = dm_i2c_write(dev, offset, buf, write_size);
84 if (ret)
85 return ret;
86
87 offset += write_size;
88 buf += write_size;
89 size -= write_size;
90
91 udelay(10000);
92 }
93
94 return 0;
Simon Glass6ca4ba02014-12-10 08:55:54 -070095}
96
Robert Beckett069ddc72019-10-28 18:29:06 +000097static int i2c_eeprom_std_size(struct udevice *dev)
98{
99 struct i2c_eeprom *priv = dev_get_priv(dev);
100
101 return priv->size;
102}
103
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900104static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8427a242017-04-22 08:57:41 +0000105 .read = i2c_eeprom_std_read,
106 .write = i2c_eeprom_std_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000107 .size = i2c_eeprom_std_size,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700108};
109
Simon Glassaad29ae2020-12-03 16:55:21 -0700110static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200111{
112 struct i2c_eeprom *priv = dev_get_priv(dev);
Robert Beckett069ddc72019-10-28 18:29:06 +0000113 struct i2c_eeprom_drv_data *data =
114 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
Baruch Siach263b5b02019-04-07 12:38:48 +0300115 u32 pagesize;
Robert Beckett069ddc72019-10-28 18:29:06 +0000116 u32 size;
Baruch Siach263b5b02019-04-07 12:38:48 +0300117
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900118 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
Baruch Siach263b5b02019-04-07 12:38:48 +0300119 priv->pagesize = pagesize;
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900120 else
Robert Beckett069ddc72019-10-28 18:29:06 +0000121 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
Masahiro Yamada2a579372020-02-28 22:04:14 +0900122 priv->pagesize = data->pagesize;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200123
Robert Beckett069ddc72019-10-28 18:29:06 +0000124 if (dev_read_u32(dev, "size", &size) == 0)
125 priv->size = size;
126 else
127 priv->size = data->size;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200128
129 return 0;
130}
131
Robert Beckettf030d6f2019-10-28 18:29:05 +0000132static int i2c_eeprom_std_bind(struct udevice *dev)
133{
134 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
135 ofnode partition;
136 const char *name;
137
138 if (!ofnode_valid(partitions))
139 return 0;
140 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
141 return -ENOTSUPP;
142
143 ofnode_for_each_subnode(partition, partitions) {
144 name = ofnode_get_name(partition);
145 if (!name)
146 continue;
147
Simon Glass65130cd2020-12-28 20:34:56 -0700148 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
Simon Glass884870f2020-11-28 17:50:01 -0700149 NULL, partition, NULL);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000150 }
151
152 return 0;
153}
154
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900155static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass6ca4ba02014-12-10 08:55:54 -0700156{
Baruch Siach38793e22019-08-05 09:03:30 +0300157 u8 test_byte;
158 int ret;
Robert Beckett44cf4612020-01-31 15:07:52 +0200159 struct i2c_eeprom_drv_data *data =
160 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
161
162 i2c_set_chip_offset_len(dev, data->offset_len);
163 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
Baruch Siach38793e22019-08-05 09:03:30 +0300164
165 /* Verify that the chip is functional */
Eugen Hristev24d8b752020-05-07 11:53:18 +0300166 /*
167 * Not all eeproms start from offset 0. Valid offset is available
168 * in the platform data struct.
169 */
170 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
Baruch Siach38793e22019-08-05 09:03:30 +0300171 if (ret)
172 return -ENODEV;
173
Simon Glass6ca4ba02014-12-10 08:55:54 -0700174 return 0;
175}
176
Robert Beckett069ddc72019-10-28 18:29:06 +0000177static const struct i2c_eeprom_drv_data eeprom_data = {
178 .size = 0,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900179 .pagesize = 1,
Robert Beckett44cf4612020-01-31 15:07:52 +0200180 .addr_offset_mask = 0,
181 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000182};
183
Robert Beckett069ddc72019-10-28 18:29:06 +0000184static const struct i2c_eeprom_drv_data atmel24c01a_data = {
185 .size = 128,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900186 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200187 .addr_offset_mask = 0,
188 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000189};
190
191static const struct i2c_eeprom_drv_data atmel24c02_data = {
192 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900193 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200194 .addr_offset_mask = 0,
195 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000196};
197
198static const struct i2c_eeprom_drv_data atmel24c04_data = {
199 .size = 512,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900200 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200201 .addr_offset_mask = 0x1,
202 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000203};
204
205static const struct i2c_eeprom_drv_data atmel24c08_data = {
206 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900207 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200208 .addr_offset_mask = 0x3,
209 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000210};
211
212static const struct i2c_eeprom_drv_data atmel24c08a_data = {
213 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900214 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200215 .addr_offset_mask = 0x3,
216 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000217};
218
219static const struct i2c_eeprom_drv_data atmel24c16a_data = {
220 .size = 2048,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900221 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200222 .addr_offset_mask = 0x7,
223 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000224};
225
226static const struct i2c_eeprom_drv_data atmel24mac402_data = {
227 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900228 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200229 .addr_offset_mask = 0,
230 .offset_len = 1,
Eugen Hristev24d8b752020-05-07 11:53:18 +0300231 .start_offset = 0x80,
Robert Beckett069ddc72019-10-28 18:29:06 +0000232};
233
234static const struct i2c_eeprom_drv_data atmel24c32_data = {
235 .size = 4096,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900236 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200237 .addr_offset_mask = 0,
238 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000239};
240
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200241static const struct i2c_eeprom_drv_data atmel24c32d_wlp_data = {
242 .size = 32,
243 .pagesize = 32,
244 .addr_offset_mask = 0,
245 .offset_len = 2,
246};
247
Robert Beckett069ddc72019-10-28 18:29:06 +0000248static const struct i2c_eeprom_drv_data atmel24c64_data = {
249 .size = 8192,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900250 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200251 .addr_offset_mask = 0,
252 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000253};
254
255static const struct i2c_eeprom_drv_data atmel24c128_data = {
256 .size = 16384,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900257 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200258 .addr_offset_mask = 0,
259 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000260};
261
262static const struct i2c_eeprom_drv_data atmel24c256_data = {
263 .size = 32768,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900264 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200265 .addr_offset_mask = 0,
266 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000267};
268
269static const struct i2c_eeprom_drv_data atmel24c512_data = {
270 .size = 65536,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900271 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200272 .addr_offset_mask = 0,
273 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000274};
275
Simon Glass6ca4ba02014-12-10 08:55:54 -0700276static const struct udevice_id i2c_eeprom_std_ids[] = {
Robert Beckett069ddc72019-10-28 18:29:06 +0000277 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
Marek Vasut8df0def2021-07-04 21:31:18 +0200278 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000279 { .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 },
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200287 { .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000288 { .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 },
Simon Glass6ca4ba02014-12-10 08:55:54 -0700292 { }
293};
294
295U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200296 .name = "i2c_eeprom",
297 .id = UCLASS_I2C_EEPROM,
298 .of_match = i2c_eeprom_std_ids,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000299 .bind = i2c_eeprom_std_bind,
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200300 .probe = i2c_eeprom_std_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700301 .of_to_plat = i2c_eeprom_std_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700302 .priv_auto = sizeof(struct i2c_eeprom),
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200303 .ops = &i2c_eeprom_std_ops,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700304};
305
Robert Beckettf030d6f2019-10-28 18:29:05 +0000306struct i2c_eeprom_partition {
307 u32 offset;
308 u32 size;
309};
310
311static int i2c_eeprom_partition_probe(struct udevice *dev)
312{
313 return 0;
314}
315
Simon Glassaad29ae2020-12-03 16:55:21 -0700316static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
Robert Beckettf030d6f2019-10-28 18:29:05 +0000317{
318 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
Michal Simek4f18f922020-05-28 11:48:55 +0200319 u32 reg[2];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000320 int ret;
321
Michal Simek4f18f922020-05-28 11:48:55 +0200322 ret = dev_read_u32_array(dev, "reg", reg, 2);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000323 if (ret)
324 return ret;
325
Michal Simek4f18f922020-05-28 11:48:55 +0200326 if (!reg[1])
327 return -EINVAL;
328
329 priv->offset = reg[0];
330 priv->size = reg[1];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000331
Michal Simek4f18f922020-05-28 11:48:55 +0200332 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000333
334 return 0;
335}
336
337static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
338 u8 *buf, int size)
339{
340 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
341 struct udevice *parent = dev_get_parent(dev);
342
343 if (!parent)
344 return -ENODEV;
345 if (offset + size > priv->size)
346 return -EINVAL;
347
348 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
349}
350
351static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
352 const u8 *buf, int size)
353{
354 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
355 struct udevice *parent = dev_get_parent(dev);
356
357 if (!parent)
358 return -ENODEV;
359 if (offset + size > priv->size)
360 return -EINVAL;
361
362 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
363 size);
364}
365
Robert Beckett069ddc72019-10-28 18:29:06 +0000366static int i2c_eeprom_partition_size(struct udevice *dev)
367{
368 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
369
370 return priv->size;
371}
372
Robert Beckettf030d6f2019-10-28 18:29:05 +0000373static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
374 .read = i2c_eeprom_partition_read,
375 .write = i2c_eeprom_partition_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000376 .size = i2c_eeprom_partition_size,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000377};
378
379U_BOOT_DRIVER(i2c_eeprom_partition) = {
380 .name = "i2c_eeprom_partition",
381 .id = UCLASS_I2C_EEPROM,
382 .probe = i2c_eeprom_partition_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700383 .of_to_plat = i2c_eeprom_partition_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700384 .priv_auto = sizeof(struct i2c_eeprom_partition),
Robert Beckettf030d6f2019-10-28 18:29:05 +0000385 .ops = &i2c_eeprom_partition_ops,
386};
387
Simon Glass6ca4ba02014-12-10 08:55:54 -0700388UCLASS_DRIVER(i2c_eeprom) = {
389 .id = UCLASS_I2C_EEPROM,
390 .name = "i2c_eeprom",
391};