blob: 10f0173d80500d026514f022fed150c0f2ed57a5 [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 Glassdbd79542020-05-10 11:40:11 -06008#include <linux/delay.h>
Masahiro Yamadaf9d52cc2014-12-18 20:00:26 +09009#include <linux/err.h>
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030010#include <linux/kernel.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070011#include <dm.h>
Robert Beckettf030d6f2019-10-28 18:29:05 +000012#include <dm/device-internal.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070013#include <i2c.h>
14#include <i2c_eeprom.h>
15
Robert Beckett069ddc72019-10-28 18:29:06 +000016struct i2c_eeprom_drv_data {
17 u32 size; /* size in bytes */
Masahiro Yamada2a579372020-02-28 22:04:14 +090018 u32 pagesize; /* page size in bytes */
Robert Beckett44cf4612020-01-31 15:07:52 +020019 u32 addr_offset_mask; /* bits in addr used for offset overflow */
20 u32 offset_len; /* size in bytes of offset */
Eugen Hristev24d8b752020-05-07 11:53:18 +030021 u32 start_offset; /* valid start offset inside memory, by default 0 */
Robert Beckett069ddc72019-10-28 18:29:06 +000022};
23
Jonas Karlman8427a242017-04-22 08:57:41 +000024int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
25{
26 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
27
28 if (!ops->read)
29 return -ENOSYS;
30
31 return ops->read(dev, offset, buf, size);
32}
33
Sean Anderson4a55f9d2022-05-05 13:11:37 -040034int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
35 int size)
Jonas Karlman8427a242017-04-22 08:57:41 +000036{
37 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
38
39 if (!ops->write)
40 return -ENOSYS;
41
42 return ops->write(dev, offset, buf, size);
43}
44
Robert Beckett069ddc72019-10-28 18:29:06 +000045int i2c_eeprom_size(struct udevice *dev)
46{
47 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
48
49 if (!ops->size)
50 return -ENOSYS;
51
52 return ops->size(dev);
53}
54
Jonas Karlman8427a242017-04-22 08:57:41 +000055static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
56 int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070057{
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020058 return dm_i2c_read(dev, offset, buf, size);
Simon Glass6ca4ba02014-12-10 08:55:54 -070059}
60
Michel Alex69f207f2023-10-26 05:47:41 +000061static int i2c_eeprom_len(int offset, int len, int pagesize)
62{
63 int page_offset = offset & (pagesize - 1);
64 int maxlen = pagesize - page_offset;
65
66 if (len > maxlen)
67 len = maxlen;
68
69 return len;
70}
71
Jonas Karlman8427a242017-04-22 08:57:41 +000072static int i2c_eeprom_std_write(struct udevice *dev, int offset,
73 const uint8_t *buf, int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070074{
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030075 struct i2c_eeprom *priv = dev_get_priv(dev);
76 int ret;
77
78 while (size > 0) {
Michel Alex69f207f2023-10-26 05:47:41 +000079 int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030080
81 ret = dm_i2c_write(dev, offset, buf, write_size);
82 if (ret)
83 return ret;
84
85 offset += write_size;
86 buf += write_size;
87 size -= write_size;
88
89 udelay(10000);
90 }
91
92 return 0;
Simon Glass6ca4ba02014-12-10 08:55:54 -070093}
94
Robert Beckett069ddc72019-10-28 18:29:06 +000095static int i2c_eeprom_std_size(struct udevice *dev)
96{
97 struct i2c_eeprom *priv = dev_get_priv(dev);
98
99 return priv->size;
100}
101
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900102static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8427a242017-04-22 08:57:41 +0000103 .read = i2c_eeprom_std_read,
104 .write = i2c_eeprom_std_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000105 .size = i2c_eeprom_std_size,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700106};
107
Simon Glassaad29ae2020-12-03 16:55:21 -0700108static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200109{
110 struct i2c_eeprom *priv = dev_get_priv(dev);
Robert Beckett069ddc72019-10-28 18:29:06 +0000111 struct i2c_eeprom_drv_data *data =
112 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
Baruch Siach263b5b02019-04-07 12:38:48 +0300113 u32 pagesize;
Robert Beckett069ddc72019-10-28 18:29:06 +0000114 u32 size;
Baruch Siach263b5b02019-04-07 12:38:48 +0300115
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900116 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
Baruch Siach263b5b02019-04-07 12:38:48 +0300117 priv->pagesize = pagesize;
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900118 else
Robert Beckett069ddc72019-10-28 18:29:06 +0000119 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
Masahiro Yamada2a579372020-02-28 22:04:14 +0900120 priv->pagesize = data->pagesize;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200121
Robert Beckett069ddc72019-10-28 18:29:06 +0000122 if (dev_read_u32(dev, "size", &size) == 0)
123 priv->size = size;
124 else
125 priv->size = data->size;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200126
127 return 0;
128}
129
Robert Beckettf030d6f2019-10-28 18:29:05 +0000130static int i2c_eeprom_std_bind(struct udevice *dev)
131{
132 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
133 ofnode partition;
134 const char *name;
135
136 if (!ofnode_valid(partitions))
137 return 0;
138 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
139 return -ENOTSUPP;
140
141 ofnode_for_each_subnode(partition, partitions) {
142 name = ofnode_get_name(partition);
143 if (!name)
144 continue;
145
Simon Glass65130cd2020-12-28 20:34:56 -0700146 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
Simon Glass884870f2020-11-28 17:50:01 -0700147 NULL, partition, NULL);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000148 }
149
150 return 0;
151}
152
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900153static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass6ca4ba02014-12-10 08:55:54 -0700154{
Baruch Siach38793e22019-08-05 09:03:30 +0300155 u8 test_byte;
156 int ret;
Robert Beckett44cf4612020-01-31 15:07:52 +0200157 struct i2c_eeprom_drv_data *data =
158 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
159
160 i2c_set_chip_offset_len(dev, data->offset_len);
161 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
Baruch Siach38793e22019-08-05 09:03:30 +0300162
163 /* Verify that the chip is functional */
Eugen Hristev24d8b752020-05-07 11:53:18 +0300164 /*
165 * Not all eeproms start from offset 0. Valid offset is available
166 * in the platform data struct.
167 */
168 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
Baruch Siach38793e22019-08-05 09:03:30 +0300169 if (ret)
170 return -ENODEV;
171
Simon Glass6ca4ba02014-12-10 08:55:54 -0700172 return 0;
173}
174
Robert Beckett069ddc72019-10-28 18:29:06 +0000175static const struct i2c_eeprom_drv_data eeprom_data = {
176 .size = 0,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900177 .pagesize = 1,
Robert Beckett44cf4612020-01-31 15:07:52 +0200178 .addr_offset_mask = 0,
179 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000180};
181
Robert Beckett069ddc72019-10-28 18:29:06 +0000182static const struct i2c_eeprom_drv_data atmel24c01a_data = {
183 .size = 128,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900184 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200185 .addr_offset_mask = 0,
186 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000187};
188
189static const struct i2c_eeprom_drv_data atmel24c02_data = {
190 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900191 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200192 .addr_offset_mask = 0,
193 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000194};
195
196static const struct i2c_eeprom_drv_data atmel24c04_data = {
197 .size = 512,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900198 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200199 .addr_offset_mask = 0x1,
200 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000201};
202
203static const struct i2c_eeprom_drv_data atmel24c08_data = {
204 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900205 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200206 .addr_offset_mask = 0x3,
207 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000208};
209
210static const struct i2c_eeprom_drv_data atmel24c08a_data = {
211 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900212 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200213 .addr_offset_mask = 0x3,
214 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000215};
216
217static const struct i2c_eeprom_drv_data atmel24c16a_data = {
218 .size = 2048,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900219 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200220 .addr_offset_mask = 0x7,
221 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000222};
223
224static const struct i2c_eeprom_drv_data atmel24mac402_data = {
225 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900226 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200227 .addr_offset_mask = 0,
228 .offset_len = 1,
Eugen Hristev24d8b752020-05-07 11:53:18 +0300229 .start_offset = 0x80,
Robert Beckett069ddc72019-10-28 18:29:06 +0000230};
231
232static const struct i2c_eeprom_drv_data atmel24c32_data = {
233 .size = 4096,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900234 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200235 .addr_offset_mask = 0,
236 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000237};
238
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200239static const struct i2c_eeprom_drv_data atmel24c32d_wlp_data = {
240 .size = 32,
241 .pagesize = 32,
242 .addr_offset_mask = 0,
243 .offset_len = 2,
244};
245
Robert Beckett069ddc72019-10-28 18:29:06 +0000246static const struct i2c_eeprom_drv_data atmel24c64_data = {
247 .size = 8192,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900248 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200249 .addr_offset_mask = 0,
250 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000251};
252
253static const struct i2c_eeprom_drv_data atmel24c128_data = {
254 .size = 16384,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900255 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200256 .addr_offset_mask = 0,
257 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000258};
259
260static const struct i2c_eeprom_drv_data atmel24c256_data = {
261 .size = 32768,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900262 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200263 .addr_offset_mask = 0,
264 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000265};
266
267static const struct i2c_eeprom_drv_data atmel24c512_data = {
268 .size = 65536,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900269 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200270 .addr_offset_mask = 0,
271 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000272};
273
Simon Glass6ca4ba02014-12-10 08:55:54 -0700274static const struct udevice_id i2c_eeprom_std_ids[] = {
Robert Beckett069ddc72019-10-28 18:29:06 +0000275 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
Marek Vasut8df0def2021-07-04 21:31:18 +0200276 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000277 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
278 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
279 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
280 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
281 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
282 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
283 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
284 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200285 { .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000286 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
287 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
288 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
289 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
Simon Glass6ca4ba02014-12-10 08:55:54 -0700290 { }
291};
292
293U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200294 .name = "i2c_eeprom",
295 .id = UCLASS_I2C_EEPROM,
296 .of_match = i2c_eeprom_std_ids,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000297 .bind = i2c_eeprom_std_bind,
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200298 .probe = i2c_eeprom_std_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700299 .of_to_plat = i2c_eeprom_std_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700300 .priv_auto = sizeof(struct i2c_eeprom),
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200301 .ops = &i2c_eeprom_std_ops,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700302};
303
Robert Beckettf030d6f2019-10-28 18:29:05 +0000304struct i2c_eeprom_partition {
305 u32 offset;
306 u32 size;
307};
308
309static int i2c_eeprom_partition_probe(struct udevice *dev)
310{
311 return 0;
312}
313
Simon Glassaad29ae2020-12-03 16:55:21 -0700314static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
Robert Beckettf030d6f2019-10-28 18:29:05 +0000315{
316 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
Michal Simek4f18f922020-05-28 11:48:55 +0200317 u32 reg[2];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000318 int ret;
319
Michal Simek4f18f922020-05-28 11:48:55 +0200320 ret = dev_read_u32_array(dev, "reg", reg, 2);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000321 if (ret)
322 return ret;
323
Michal Simek4f18f922020-05-28 11:48:55 +0200324 if (!reg[1])
325 return -EINVAL;
326
327 priv->offset = reg[0];
328 priv->size = reg[1];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000329
Michal Simek4f18f922020-05-28 11:48:55 +0200330 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000331
332 return 0;
333}
334
335static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
336 u8 *buf, int size)
337{
338 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
339 struct udevice *parent = dev_get_parent(dev);
340
341 if (!parent)
342 return -ENODEV;
343 if (offset + size > priv->size)
344 return -EINVAL;
345
346 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
347}
348
349static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
350 const u8 *buf, int size)
351{
352 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
353 struct udevice *parent = dev_get_parent(dev);
354
355 if (!parent)
356 return -ENODEV;
357 if (offset + size > priv->size)
358 return -EINVAL;
359
360 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
361 size);
362}
363
Robert Beckett069ddc72019-10-28 18:29:06 +0000364static int i2c_eeprom_partition_size(struct udevice *dev)
365{
366 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
367
368 return priv->size;
369}
370
Robert Beckettf030d6f2019-10-28 18:29:05 +0000371static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
372 .read = i2c_eeprom_partition_read,
373 .write = i2c_eeprom_partition_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000374 .size = i2c_eeprom_partition_size,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000375};
376
377U_BOOT_DRIVER(i2c_eeprom_partition) = {
378 .name = "i2c_eeprom_partition",
379 .id = UCLASS_I2C_EEPROM,
380 .probe = i2c_eeprom_partition_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700381 .of_to_plat = i2c_eeprom_partition_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700382 .priv_auto = sizeof(struct i2c_eeprom_partition),
Robert Beckettf030d6f2019-10-28 18:29:05 +0000383 .ops = &i2c_eeprom_partition_ops,
384};
385
Simon Glass6ca4ba02014-12-10 08:55:54 -0700386UCLASS_DRIVER(i2c_eeprom) = {
387 .id = UCLASS_I2C_EEPROM,
388 .name = "i2c_eeprom",
389};