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