blob: 9cb375a99ab84385371e0349aae48b9e0fc1e5f6 [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 Glassdbd79542020-05-10 11:40:11 -06009#include <linux/delay.h>
Masahiro Yamadaf9d52cc2014-12-18 20:00:26 +090010#include <linux/err.h>
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030011#include <linux/kernel.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070012#include <dm.h>
Robert Beckettf030d6f2019-10-28 18:29:05 +000013#include <dm/device-internal.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070014#include <i2c.h>
15#include <i2c_eeprom.h>
16
Robert Beckett069ddc72019-10-28 18:29:06 +000017struct i2c_eeprom_drv_data {
18 u32 size; /* size in bytes */
Masahiro Yamada2a579372020-02-28 22:04:14 +090019 u32 pagesize; /* page size in bytes */
Robert Beckett44cf4612020-01-31 15:07:52 +020020 u32 addr_offset_mask; /* bits in addr used for offset overflow */
21 u32 offset_len; /* size in bytes of offset */
Eugen Hristev24d8b752020-05-07 11:53:18 +030022 u32 start_offset; /* valid start offset inside memory, by default 0 */
Robert Beckett069ddc72019-10-28 18:29:06 +000023};
24
Jonas Karlman8427a242017-04-22 08:57:41 +000025int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
26{
27 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
28
29 if (!ops->read)
30 return -ENOSYS;
31
32 return ops->read(dev, offset, buf, size);
33}
34
Sean Anderson4a55f9d2022-05-05 13:11:37 -040035int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
36 int size)
Jonas Karlman8427a242017-04-22 08:57:41 +000037{
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 Beckett069ddc72019-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 Karlman8427a242017-04-22 08:57:41 +000056static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
57 int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070058{
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020059 return dm_i2c_read(dev, offset, buf, size);
Simon Glass6ca4ba02014-12-10 08:55:54 -070060}
61
Michel Alex69f207f2023-10-26 05:47:41 +000062static int i2c_eeprom_len(int offset, int len, int pagesize)
63{
64 int page_offset = offset & (pagesize - 1);
65 int maxlen = pagesize - page_offset;
66
67 if (len > maxlen)
68 len = maxlen;
69
70 return len;
71}
72
Jonas Karlman8427a242017-04-22 08:57:41 +000073static int i2c_eeprom_std_write(struct udevice *dev, int offset,
74 const uint8_t *buf, int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070075{
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030076 struct i2c_eeprom *priv = dev_get_priv(dev);
77 int ret;
78
79 while (size > 0) {
Michel Alex69f207f2023-10-26 05:47:41 +000080 int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030081
82 ret = dm_i2c_write(dev, offset, buf, write_size);
83 if (ret)
84 return ret;
85
86 offset += write_size;
87 buf += write_size;
88 size -= write_size;
89
90 udelay(10000);
91 }
92
93 return 0;
Simon Glass6ca4ba02014-12-10 08:55:54 -070094}
95
Robert Beckett069ddc72019-10-28 18:29:06 +000096static int i2c_eeprom_std_size(struct udevice *dev)
97{
98 struct i2c_eeprom *priv = dev_get_priv(dev);
99
100 return priv->size;
101}
102
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900103static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8427a242017-04-22 08:57:41 +0000104 .read = i2c_eeprom_std_read,
105 .write = i2c_eeprom_std_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000106 .size = i2c_eeprom_std_size,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700107};
108
Simon Glassaad29ae2020-12-03 16:55:21 -0700109static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200110{
111 struct i2c_eeprom *priv = dev_get_priv(dev);
Robert Beckett069ddc72019-10-28 18:29:06 +0000112 struct i2c_eeprom_drv_data *data =
113 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
Baruch Siach263b5b02019-04-07 12:38:48 +0300114 u32 pagesize;
Robert Beckett069ddc72019-10-28 18:29:06 +0000115 u32 size;
Baruch Siach263b5b02019-04-07 12:38:48 +0300116
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900117 if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
Baruch Siach263b5b02019-04-07 12:38:48 +0300118 priv->pagesize = pagesize;
Masahiro Yamadac8b5dc32020-02-28 22:04:13 +0900119 else
Robert Beckett069ddc72019-10-28 18:29:06 +0000120 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
Masahiro Yamada2a579372020-02-28 22:04:14 +0900121 priv->pagesize = data->pagesize;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200122
Robert Beckett069ddc72019-10-28 18:29:06 +0000123 if (dev_read_u32(dev, "size", &size) == 0)
124 priv->size = size;
125 else
126 priv->size = data->size;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200127
128 return 0;
129}
130
Robert Beckettf030d6f2019-10-28 18:29:05 +0000131static int i2c_eeprom_std_bind(struct udevice *dev)
132{
133 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
134 ofnode partition;
135 const char *name;
136
137 if (!ofnode_valid(partitions))
138 return 0;
139 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
140 return -ENOTSUPP;
141
142 ofnode_for_each_subnode(partition, partitions) {
143 name = ofnode_get_name(partition);
144 if (!name)
145 continue;
146
Simon Glass65130cd2020-12-28 20:34:56 -0700147 device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
Simon Glass884870f2020-11-28 17:50:01 -0700148 NULL, partition, NULL);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000149 }
150
151 return 0;
152}
153
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900154static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass6ca4ba02014-12-10 08:55:54 -0700155{
Baruch Siach38793e22019-08-05 09:03:30 +0300156 u8 test_byte;
157 int ret;
Robert Beckett44cf4612020-01-31 15:07:52 +0200158 struct i2c_eeprom_drv_data *data =
159 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
160
161 i2c_set_chip_offset_len(dev, data->offset_len);
162 i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
Baruch Siach38793e22019-08-05 09:03:30 +0300163
164 /* Verify that the chip is functional */
Eugen Hristev24d8b752020-05-07 11:53:18 +0300165 /*
166 * Not all eeproms start from offset 0. Valid offset is available
167 * in the platform data struct.
168 */
169 ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
Baruch Siach38793e22019-08-05 09:03:30 +0300170 if (ret)
171 return -ENODEV;
172
Simon Glass6ca4ba02014-12-10 08:55:54 -0700173 return 0;
174}
175
Robert Beckett069ddc72019-10-28 18:29:06 +0000176static const struct i2c_eeprom_drv_data eeprom_data = {
177 .size = 0,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900178 .pagesize = 1,
Robert Beckett44cf4612020-01-31 15:07:52 +0200179 .addr_offset_mask = 0,
180 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000181};
182
Robert Beckett069ddc72019-10-28 18:29:06 +0000183static const struct i2c_eeprom_drv_data atmel24c01a_data = {
184 .size = 128,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900185 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200186 .addr_offset_mask = 0,
187 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000188};
189
190static const struct i2c_eeprom_drv_data atmel24c02_data = {
191 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900192 .pagesize = 8,
Robert Beckett44cf4612020-01-31 15:07:52 +0200193 .addr_offset_mask = 0,
194 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000195};
196
197static const struct i2c_eeprom_drv_data atmel24c04_data = {
198 .size = 512,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900199 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200200 .addr_offset_mask = 0x1,
201 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000202};
203
204static const struct i2c_eeprom_drv_data atmel24c08_data = {
205 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900206 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200207 .addr_offset_mask = 0x3,
208 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000209};
210
211static const struct i2c_eeprom_drv_data atmel24c08a_data = {
212 .size = 1024,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900213 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200214 .addr_offset_mask = 0x3,
215 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000216};
217
218static const struct i2c_eeprom_drv_data atmel24c16a_data = {
219 .size = 2048,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900220 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200221 .addr_offset_mask = 0x7,
222 .offset_len = 1,
Robert Beckett069ddc72019-10-28 18:29:06 +0000223};
224
225static const struct i2c_eeprom_drv_data atmel24mac402_data = {
226 .size = 256,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900227 .pagesize = 16,
Robert Beckett44cf4612020-01-31 15:07:52 +0200228 .addr_offset_mask = 0,
229 .offset_len = 1,
Eugen Hristev24d8b752020-05-07 11:53:18 +0300230 .start_offset = 0x80,
Robert Beckett069ddc72019-10-28 18:29:06 +0000231};
232
233static const struct i2c_eeprom_drv_data atmel24c32_data = {
234 .size = 4096,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900235 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200236 .addr_offset_mask = 0,
237 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000238};
239
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200240static const struct i2c_eeprom_drv_data atmel24c32d_wlp_data = {
241 .size = 32,
242 .pagesize = 32,
243 .addr_offset_mask = 0,
244 .offset_len = 2,
245};
246
Robert Beckett069ddc72019-10-28 18:29:06 +0000247static const struct i2c_eeprom_drv_data atmel24c64_data = {
248 .size = 8192,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900249 .pagesize = 32,
Robert Beckett44cf4612020-01-31 15:07:52 +0200250 .addr_offset_mask = 0,
251 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000252};
253
254static const struct i2c_eeprom_drv_data atmel24c128_data = {
255 .size = 16384,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900256 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200257 .addr_offset_mask = 0,
258 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000259};
260
261static const struct i2c_eeprom_drv_data atmel24c256_data = {
262 .size = 32768,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900263 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200264 .addr_offset_mask = 0,
265 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000266};
267
268static const struct i2c_eeprom_drv_data atmel24c512_data = {
269 .size = 65536,
Masahiro Yamada2a579372020-02-28 22:04:14 +0900270 .pagesize = 64,
Robert Beckett44cf4612020-01-31 15:07:52 +0200271 .addr_offset_mask = 0,
272 .offset_len = 2,
Robert Beckett069ddc72019-10-28 18:29:06 +0000273};
274
Simon Glass6ca4ba02014-12-10 08:55:54 -0700275static const struct udevice_id i2c_eeprom_std_ids[] = {
Robert Beckett069ddc72019-10-28 18:29:06 +0000276 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
Marek Vasut8df0def2021-07-04 21:31:18 +0200277 { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000278 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
279 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
280 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
281 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
282 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
283 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
284 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
285 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
Marek Vasutc48ea3e2023-10-16 02:04:33 +0200286 { .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data },
Robert Beckett069ddc72019-10-28 18:29:06 +0000287 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
288 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
289 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
290 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
Simon Glass6ca4ba02014-12-10 08:55:54 -0700291 { }
292};
293
294U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200295 .name = "i2c_eeprom",
296 .id = UCLASS_I2C_EEPROM,
297 .of_match = i2c_eeprom_std_ids,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000298 .bind = i2c_eeprom_std_bind,
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200299 .probe = i2c_eeprom_std_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700300 .of_to_plat = i2c_eeprom_std_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700301 .priv_auto = sizeof(struct i2c_eeprom),
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200302 .ops = &i2c_eeprom_std_ops,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700303};
304
Robert Beckettf030d6f2019-10-28 18:29:05 +0000305struct i2c_eeprom_partition {
306 u32 offset;
307 u32 size;
308};
309
310static int i2c_eeprom_partition_probe(struct udevice *dev)
311{
312 return 0;
313}
314
Simon Glassaad29ae2020-12-03 16:55:21 -0700315static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
Robert Beckettf030d6f2019-10-28 18:29:05 +0000316{
317 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
Michal Simek4f18f922020-05-28 11:48:55 +0200318 u32 reg[2];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000319 int ret;
320
Michal Simek4f18f922020-05-28 11:48:55 +0200321 ret = dev_read_u32_array(dev, "reg", reg, 2);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000322 if (ret)
323 return ret;
324
Michal Simek4f18f922020-05-28 11:48:55 +0200325 if (!reg[1])
326 return -EINVAL;
327
328 priv->offset = reg[0];
329 priv->size = reg[1];
Robert Beckettf030d6f2019-10-28 18:29:05 +0000330
Michal Simek4f18f922020-05-28 11:48:55 +0200331 debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
Robert Beckettf030d6f2019-10-28 18:29:05 +0000332
333 return 0;
334}
335
336static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
337 u8 *buf, int size)
338{
339 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
340 struct udevice *parent = dev_get_parent(dev);
341
342 if (!parent)
343 return -ENODEV;
344 if (offset + size > priv->size)
345 return -EINVAL;
346
347 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
348}
349
350static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
351 const u8 *buf, int size)
352{
353 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
354 struct udevice *parent = dev_get_parent(dev);
355
356 if (!parent)
357 return -ENODEV;
358 if (offset + size > priv->size)
359 return -EINVAL;
360
361 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
362 size);
363}
364
Robert Beckett069ddc72019-10-28 18:29:06 +0000365static int i2c_eeprom_partition_size(struct udevice *dev)
366{
367 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
368
369 return priv->size;
370}
371
Robert Beckettf030d6f2019-10-28 18:29:05 +0000372static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
373 .read = i2c_eeprom_partition_read,
374 .write = i2c_eeprom_partition_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000375 .size = i2c_eeprom_partition_size,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000376};
377
378U_BOOT_DRIVER(i2c_eeprom_partition) = {
379 .name = "i2c_eeprom_partition",
380 .id = UCLASS_I2C_EEPROM,
381 .probe = i2c_eeprom_partition_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700382 .of_to_plat = i2c_eeprom_partition_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700383 .priv_auto = sizeof(struct i2c_eeprom_partition),
Robert Beckettf030d6f2019-10-28 18:29:05 +0000384 .ops = &i2c_eeprom_partition_ops,
385};
386
Simon Glass6ca4ba02014-12-10 08:55:54 -0700387UCLASS_DRIVER(i2c_eeprom) = {
388 .id = UCLASS_I2C_EEPROM,
389 .name = "i2c_eeprom",
390};