blob: 934f82074d5dbc59427ab36eea9a0197901400e1 [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
6#include <common.h>
Simon Glasseba6b8d2019-11-14 12:57:50 -07007#include <eeprom.h>
Masahiro Yamadaf9d52cc2014-12-18 20:00:26 +09008#include <linux/err.h>
Baruch Siach4e4c5ed2019-04-07 12:38:49 +03009#include <linux/kernel.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070010#include <dm.h>
Robert Beckettf030d6f2019-10-28 18:29:05 +000011#include <dm/device-internal.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -070012#include <i2c.h>
13#include <i2c_eeprom.h>
14
Robert Beckett069ddc72019-10-28 18:29:06 +000015struct i2c_eeprom_drv_data {
16 u32 size; /* size in bytes */
17 u32 pagewidth; /* pagesize = 2^pagewidth */
18};
19
Jonas Karlman8427a242017-04-22 08:57:41 +000020int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
21{
22 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
23
24 if (!ops->read)
25 return -ENOSYS;
26
27 return ops->read(dev, offset, buf, size);
28}
29
30int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
31{
32 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
33
34 if (!ops->write)
35 return -ENOSYS;
36
37 return ops->write(dev, offset, buf, size);
38}
39
Robert Beckett069ddc72019-10-28 18:29:06 +000040int i2c_eeprom_size(struct udevice *dev)
41{
42 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
43
44 if (!ops->size)
45 return -ENOSYS;
46
47 return ops->size(dev);
48}
49
Jonas Karlman8427a242017-04-22 08:57:41 +000050static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
51 int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070052{
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020053 return dm_i2c_read(dev, offset, buf, size);
Simon Glass6ca4ba02014-12-10 08:55:54 -070054}
55
Jonas Karlman8427a242017-04-22 08:57:41 +000056static int i2c_eeprom_std_write(struct udevice *dev, int offset,
57 const uint8_t *buf, int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070058{
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030059 struct i2c_eeprom *priv = dev_get_priv(dev);
60 int ret;
61
62 while (size > 0) {
63 int write_size = min_t(int, size, priv->pagesize);
64
65 ret = dm_i2c_write(dev, offset, buf, write_size);
66 if (ret)
67 return ret;
68
69 offset += write_size;
70 buf += write_size;
71 size -= write_size;
72
73 udelay(10000);
74 }
75
76 return 0;
Simon Glass6ca4ba02014-12-10 08:55:54 -070077}
78
Robert Beckett069ddc72019-10-28 18:29:06 +000079static int i2c_eeprom_std_size(struct udevice *dev)
80{
81 struct i2c_eeprom *priv = dev_get_priv(dev);
82
83 return priv->size;
84}
85
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +090086static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8427a242017-04-22 08:57:41 +000087 .read = i2c_eeprom_std_read,
88 .write = i2c_eeprom_std_write,
Robert Beckett069ddc72019-10-28 18:29:06 +000089 .size = i2c_eeprom_std_size,
Simon Glass6ca4ba02014-12-10 08:55:54 -070090};
91
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020092static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
93{
94 struct i2c_eeprom *priv = dev_get_priv(dev);
Robert Beckett069ddc72019-10-28 18:29:06 +000095 struct i2c_eeprom_drv_data *data =
96 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
Baruch Siach263b5b02019-04-07 12:38:48 +030097 u32 pagesize;
Robert Beckett069ddc72019-10-28 18:29:06 +000098 u32 size;
Baruch Siach263b5b02019-04-07 12:38:48 +030099
100 if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
101 priv->pagesize = pagesize;
Robert Beckett069ddc72019-10-28 18:29:06 +0000102 } else {
103 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
104 priv->pagewidth = data->pagewidth;
105 priv->pagesize = (1 << priv->pagewidth);
Baruch Siach263b5b02019-04-07 12:38:48 +0300106 }
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200107
Robert Beckett069ddc72019-10-28 18:29:06 +0000108 if (dev_read_u32(dev, "size", &size) == 0)
109 priv->size = size;
110 else
111 priv->size = data->size;
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200112
113 return 0;
114}
115
Robert Beckettf030d6f2019-10-28 18:29:05 +0000116static int i2c_eeprom_std_bind(struct udevice *dev)
117{
118 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
119 ofnode partition;
120 const char *name;
121
122 if (!ofnode_valid(partitions))
123 return 0;
124 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
125 return -ENOTSUPP;
126
127 ofnode_for_each_subnode(partition, partitions) {
128 name = ofnode_get_name(partition);
129 if (!name)
130 continue;
131
132 device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
133 name, NULL, partition, NULL);
134 }
135
136 return 0;
137}
138
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +0900139static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass6ca4ba02014-12-10 08:55:54 -0700140{
Baruch Siach38793e22019-08-05 09:03:30 +0300141 u8 test_byte;
142 int ret;
143
144 /* Verify that the chip is functional */
145 ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
146 if (ret)
147 return -ENODEV;
148
Simon Glass6ca4ba02014-12-10 08:55:54 -0700149 return 0;
150}
151
Robert Beckett069ddc72019-10-28 18:29:06 +0000152static const struct i2c_eeprom_drv_data eeprom_data = {
153 .size = 0,
154 .pagewidth = 0,
155};
156
157static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
158 .size = 256,
159 .pagewidth = 3,
160};
161
162static const struct i2c_eeprom_drv_data atmel24c01a_data = {
163 .size = 128,
164 .pagewidth = 3,
165};
166
167static const struct i2c_eeprom_drv_data atmel24c02_data = {
168 .size = 256,
169 .pagewidth = 3,
170};
171
172static const struct i2c_eeprom_drv_data atmel24c04_data = {
173 .size = 512,
174 .pagewidth = 4,
175};
176
177static const struct i2c_eeprom_drv_data atmel24c08_data = {
178 .size = 1024,
179 .pagewidth = 4,
180};
181
182static const struct i2c_eeprom_drv_data atmel24c08a_data = {
183 .size = 1024,
184 .pagewidth = 4,
185};
186
187static const struct i2c_eeprom_drv_data atmel24c16a_data = {
188 .size = 2048,
189 .pagewidth = 4,
190};
191
192static const struct i2c_eeprom_drv_data atmel24mac402_data = {
193 .size = 256,
194 .pagewidth = 4,
195};
196
197static const struct i2c_eeprom_drv_data atmel24c32_data = {
198 .size = 4096,
199 .pagewidth = 5,
200};
201
202static const struct i2c_eeprom_drv_data atmel24c64_data = {
203 .size = 8192,
204 .pagewidth = 5,
205};
206
207static const struct i2c_eeprom_drv_data atmel24c128_data = {
208 .size = 16384,
209 .pagewidth = 6,
210};
211
212static const struct i2c_eeprom_drv_data atmel24c256_data = {
213 .size = 32768,
214 .pagewidth = 6,
215};
216
217static const struct i2c_eeprom_drv_data atmel24c512_data = {
218 .size = 65536,
219 .pagewidth = 6,
220};
221
Simon Glass6ca4ba02014-12-10 08:55:54 -0700222static const struct udevice_id i2c_eeprom_std_ids[] = {
Robert Beckett069ddc72019-10-28 18:29:06 +0000223 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
224 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
225 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
226 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
227 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
228 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
229 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
230 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
231 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
232 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
233 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
234 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
235 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
236 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
Simon Glass6ca4ba02014-12-10 08:55:54 -0700237 { }
238};
239
240U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200241 .name = "i2c_eeprom",
242 .id = UCLASS_I2C_EEPROM,
243 .of_match = i2c_eeprom_std_ids,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000244 .bind = i2c_eeprom_std_bind,
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200245 .probe = i2c_eeprom_std_probe,
246 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
247 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
248 .ops = &i2c_eeprom_std_ops,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700249};
250
Robert Beckettf030d6f2019-10-28 18:29:05 +0000251struct i2c_eeprom_partition {
252 u32 offset;
253 u32 size;
254};
255
256static int i2c_eeprom_partition_probe(struct udevice *dev)
257{
258 return 0;
259}
260
261static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
262{
263 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
264 u32 offset, size;
265 int ret;
266
267 ret = dev_read_u32(dev, "offset", &offset);
268 if (ret)
269 return ret;
270
271 ret = dev_read_u32(dev, "size", &size);
272 if (ret)
273 return ret;
274
275 priv->offset = offset;
276 priv->size = size;
277
278 return 0;
279}
280
281static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
282 u8 *buf, int size)
283{
284 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
285 struct udevice *parent = dev_get_parent(dev);
286
287 if (!parent)
288 return -ENODEV;
289 if (offset + size > priv->size)
290 return -EINVAL;
291
292 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
293}
294
295static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
296 const u8 *buf, int size)
297{
298 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
299 struct udevice *parent = dev_get_parent(dev);
300
301 if (!parent)
302 return -ENODEV;
303 if (offset + size > priv->size)
304 return -EINVAL;
305
306 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
307 size);
308}
309
Robert Beckett069ddc72019-10-28 18:29:06 +0000310static int i2c_eeprom_partition_size(struct udevice *dev)
311{
312 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
313
314 return priv->size;
315}
316
Robert Beckettf030d6f2019-10-28 18:29:05 +0000317static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
318 .read = i2c_eeprom_partition_read,
319 .write = i2c_eeprom_partition_write,
Robert Beckett069ddc72019-10-28 18:29:06 +0000320 .size = i2c_eeprom_partition_size,
Robert Beckettf030d6f2019-10-28 18:29:05 +0000321};
322
323U_BOOT_DRIVER(i2c_eeprom_partition) = {
324 .name = "i2c_eeprom_partition",
325 .id = UCLASS_I2C_EEPROM,
326 .probe = i2c_eeprom_partition_probe,
327 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
328 .priv_auto_alloc_size = sizeof(struct i2c_eeprom_partition),
329 .ops = &i2c_eeprom_partition_ops,
330};
331
Simon Glass6ca4ba02014-12-10 08:55:54 -0700332UCLASS_DRIVER(i2c_eeprom) = {
333 .id = UCLASS_I2C_EEPROM,
334 .name = "i2c_eeprom",
335};