Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_I2C_EEPROM |
| 7 | |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 8 | #include <linux/delay.h> |
Masahiro Yamada | f9d52cc | 2014-12-18 20:00:26 +0900 | [diff] [blame] | 9 | #include <linux/err.h> |
Baruch Siach | 4e4c5ed | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 11 | #include <dm.h> |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 13 | #include <i2c.h> |
| 14 | #include <i2c_eeprom.h> |
| 15 | |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 16 | struct i2c_eeprom_drv_data { |
| 17 | u32 size; /* size in bytes */ |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 18 | u32 pagesize; /* page size in bytes */ |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 19 | u32 addr_offset_mask; /* bits in addr used for offset overflow */ |
| 20 | u32 offset_len; /* size in bytes of offset */ |
Eugen Hristev | 24d8b75 | 2020-05-07 11:53:18 +0300 | [diff] [blame] | 21 | u32 start_offset; /* valid start offset inside memory, by default 0 */ |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 24 | int 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 Anderson | 4a55f9d | 2022-05-05 13:11:37 -0400 | [diff] [blame] | 34 | int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf, |
| 35 | int size) |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 36 | { |
| 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 Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 45 | int 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 Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 55 | static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf, |
| 56 | int size) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 57 | { |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 58 | return dm_i2c_read(dev, offset, buf, size); |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Michel Alex | 69f207f | 2023-10-26 05:47:41 +0000 | [diff] [blame] | 61 | static 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 Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 72 | static int i2c_eeprom_std_write(struct udevice *dev, int offset, |
| 73 | const uint8_t *buf, int size) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 74 | { |
Baruch Siach | 4e4c5ed | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 75 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 76 | int ret; |
| 77 | |
| 78 | while (size > 0) { |
Michel Alex | 69f207f | 2023-10-26 05:47:41 +0000 | [diff] [blame] | 79 | int write_size = i2c_eeprom_len(offset, size, priv->pagesize); |
Baruch Siach | 4e4c5ed | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 80 | |
| 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 Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 95 | static 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 Yamada | a7cc93e8 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 102 | static const struct i2c_eeprom_ops i2c_eeprom_std_ops = { |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 103 | .read = i2c_eeprom_std_read, |
| 104 | .write = i2c_eeprom_std_write, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 105 | .size = i2c_eeprom_std_size, |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 108 | static int i2c_eeprom_std_of_to_plat(struct udevice *dev) |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 109 | { |
| 110 | struct i2c_eeprom *priv = dev_get_priv(dev); |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 111 | struct i2c_eeprom_drv_data *data = |
| 112 | (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev); |
Baruch Siach | 263b5b0 | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 113 | u32 pagesize; |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 114 | u32 size; |
Baruch Siach | 263b5b0 | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 115 | |
Masahiro Yamada | c8b5dc3 | 2020-02-28 22:04:13 +0900 | [diff] [blame] | 116 | if (dev_read_u32(dev, "pagesize", &pagesize) == 0) |
Baruch Siach | 263b5b0 | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 117 | priv->pagesize = pagesize; |
Masahiro Yamada | c8b5dc3 | 2020-02-28 22:04:13 +0900 | [diff] [blame] | 118 | else |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 119 | /* 6 bit -> page size of up to 2^63 (should be sufficient) */ |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 120 | priv->pagesize = data->pagesize; |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 121 | |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 122 | if (dev_read_u32(dev, "size", &size) == 0) |
| 123 | priv->size = size; |
| 124 | else |
| 125 | priv->size = data->size; |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 130 | static 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 Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 146 | device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name, |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 147 | NULL, partition, NULL); |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Masahiro Yamada | a7cc93e8 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 153 | static int i2c_eeprom_std_probe(struct udevice *dev) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 154 | { |
Baruch Siach | 38793e2 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 155 | u8 test_byte; |
| 156 | int ret; |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 157 | 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 Siach | 38793e2 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 162 | |
| 163 | /* Verify that the chip is functional */ |
Eugen Hristev | 24d8b75 | 2020-05-07 11:53:18 +0300 | [diff] [blame] | 164 | /* |
| 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 Siach | 38793e2 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 169 | if (ret) |
| 170 | return -ENODEV; |
| 171 | |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 175 | static const struct i2c_eeprom_drv_data eeprom_data = { |
| 176 | .size = 0, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 177 | .pagesize = 1, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 178 | .addr_offset_mask = 0, |
| 179 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 182 | static const struct i2c_eeprom_drv_data atmel24c01a_data = { |
| 183 | .size = 128, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 184 | .pagesize = 8, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 185 | .addr_offset_mask = 0, |
| 186 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | static const struct i2c_eeprom_drv_data atmel24c02_data = { |
| 190 | .size = 256, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 191 | .pagesize = 8, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 192 | .addr_offset_mask = 0, |
| 193 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | static const struct i2c_eeprom_drv_data atmel24c04_data = { |
| 197 | .size = 512, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 198 | .pagesize = 16, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 199 | .addr_offset_mask = 0x1, |
| 200 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | static const struct i2c_eeprom_drv_data atmel24c08_data = { |
| 204 | .size = 1024, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 205 | .pagesize = 16, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 206 | .addr_offset_mask = 0x3, |
| 207 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static const struct i2c_eeprom_drv_data atmel24c08a_data = { |
| 211 | .size = 1024, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 212 | .pagesize = 16, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 213 | .addr_offset_mask = 0x3, |
| 214 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static const struct i2c_eeprom_drv_data atmel24c16a_data = { |
| 218 | .size = 2048, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 219 | .pagesize = 16, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 220 | .addr_offset_mask = 0x7, |
| 221 | .offset_len = 1, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | static const struct i2c_eeprom_drv_data atmel24mac402_data = { |
| 225 | .size = 256, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 226 | .pagesize = 16, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 227 | .addr_offset_mask = 0, |
| 228 | .offset_len = 1, |
Eugen Hristev | 24d8b75 | 2020-05-07 11:53:18 +0300 | [diff] [blame] | 229 | .start_offset = 0x80, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | static const struct i2c_eeprom_drv_data atmel24c32_data = { |
| 233 | .size = 4096, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 234 | .pagesize = 32, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 235 | .addr_offset_mask = 0, |
| 236 | .offset_len = 2, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Marek Vasut | c48ea3e | 2023-10-16 02:04:33 +0200 | [diff] [blame] | 239 | static 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 Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 246 | static const struct i2c_eeprom_drv_data atmel24c64_data = { |
| 247 | .size = 8192, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 248 | .pagesize = 32, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 249 | .addr_offset_mask = 0, |
| 250 | .offset_len = 2, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | static const struct i2c_eeprom_drv_data atmel24c128_data = { |
| 254 | .size = 16384, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 255 | .pagesize = 64, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 256 | .addr_offset_mask = 0, |
| 257 | .offset_len = 2, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static const struct i2c_eeprom_drv_data atmel24c256_data = { |
| 261 | .size = 32768, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 262 | .pagesize = 64, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 263 | .addr_offset_mask = 0, |
| 264 | .offset_len = 2, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | static const struct i2c_eeprom_drv_data atmel24c512_data = { |
| 268 | .size = 65536, |
Masahiro Yamada | 2a57937 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 269 | .pagesize = 64, |
Robert Beckett | 44cf461 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 270 | .addr_offset_mask = 0, |
| 271 | .offset_len = 2, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 274 | static const struct udevice_id i2c_eeprom_std_ids[] = { |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 275 | { .compatible = "i2c-eeprom", (ulong)&eeprom_data }, |
Marek Vasut | 8df0def | 2021-07-04 21:31:18 +0200 | [diff] [blame] | 276 | { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data }, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 277 | { .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 Vasut | c48ea3e | 2023-10-16 02:04:33 +0200 | [diff] [blame] | 285 | { .compatible = "atmel,24c32d-wl", (ulong)&atmel24c32d_wlp_data }, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 286 | { .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 Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 290 | { } |
| 291 | }; |
| 292 | |
| 293 | U_BOOT_DRIVER(i2c_eeprom_std) = { |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 294 | .name = "i2c_eeprom", |
| 295 | .id = UCLASS_I2C_EEPROM, |
| 296 | .of_match = i2c_eeprom_std_ids, |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 297 | .bind = i2c_eeprom_std_bind, |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 298 | .probe = i2c_eeprom_std_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 299 | .of_to_plat = i2c_eeprom_std_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 300 | .priv_auto = sizeof(struct i2c_eeprom), |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 301 | .ops = &i2c_eeprom_std_ops, |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 304 | struct i2c_eeprom_partition { |
| 305 | u32 offset; |
| 306 | u32 size; |
| 307 | }; |
| 308 | |
| 309 | static int i2c_eeprom_partition_probe(struct udevice *dev) |
| 310 | { |
| 311 | return 0; |
| 312 | } |
| 313 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 314 | static int i2c_eeprom_partition_of_to_plat(struct udevice *dev) |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 315 | { |
| 316 | struct i2c_eeprom_partition *priv = dev_get_priv(dev); |
Michal Simek | 4f18f92 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 317 | u32 reg[2]; |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 318 | int ret; |
| 319 | |
Michal Simek | 4f18f92 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 320 | ret = dev_read_u32_array(dev, "reg", reg, 2); |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 321 | if (ret) |
| 322 | return ret; |
| 323 | |
Michal Simek | 4f18f92 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 324 | if (!reg[1]) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | priv->offset = reg[0]; |
| 328 | priv->size = reg[1]; |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 329 | |
Michal Simek | 4f18f92 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 330 | debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size); |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static 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 | |
| 349 | static 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 Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 364 | static 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 Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 371 | static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = { |
| 372 | .read = i2c_eeprom_partition_read, |
| 373 | .write = i2c_eeprom_partition_write, |
Robert Beckett | 069ddc7 | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 374 | .size = i2c_eeprom_partition_size, |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | U_BOOT_DRIVER(i2c_eeprom_partition) = { |
| 378 | .name = "i2c_eeprom_partition", |
| 379 | .id = UCLASS_I2C_EEPROM, |
| 380 | .probe = i2c_eeprom_partition_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 381 | .of_to_plat = i2c_eeprom_partition_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 382 | .priv_auto = sizeof(struct i2c_eeprom_partition), |
Robert Beckett | f030d6f | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 383 | .ops = &i2c_eeprom_partition_ops, |
| 384 | }; |
| 385 | |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 386 | UCLASS_DRIVER(i2c_eeprom) = { |
| 387 | .id = UCLASS_I2C_EEPROM, |
| 388 | .name = "i2c_eeprom", |
| 389 | }; |