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 | |
| 36 | int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size) |
| 37 | { |
| 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 Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 46 | int 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 Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 56 | static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf, |
| 57 | int size) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 58 | { |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 59 | return dm_i2c_read(dev, offset, buf, size); |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jonas Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 62 | static int i2c_eeprom_std_write(struct udevice *dev, int offset, |
| 63 | const uint8_t *buf, int size) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 64 | { |
Baruch Siach | 84c80c6 | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 65 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 66 | int ret; |
| 67 | |
| 68 | while (size > 0) { |
| 69 | int write_size = min_t(int, size, priv->pagesize); |
| 70 | |
| 71 | ret = dm_i2c_write(dev, offset, buf, write_size); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | offset += write_size; |
| 76 | buf += write_size; |
| 77 | size -= write_size; |
| 78 | |
| 79 | udelay(10000); |
| 80 | } |
| 81 | |
| 82 | return 0; |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 85 | static int i2c_eeprom_std_size(struct udevice *dev) |
| 86 | { |
| 87 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 88 | |
| 89 | return priv->size; |
| 90 | } |
| 91 | |
Masahiro Yamada | 5e75ea1 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 92 | static const struct i2c_eeprom_ops i2c_eeprom_std_ops = { |
Jonas Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 93 | .read = i2c_eeprom_std_read, |
| 94 | .write = i2c_eeprom_std_write, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 95 | .size = i2c_eeprom_std_size, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 98 | 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] | 99 | { |
| 100 | struct i2c_eeprom *priv = dev_get_priv(dev); |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 101 | struct i2c_eeprom_drv_data *data = |
| 102 | (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev); |
Baruch Siach | a29034d | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 103 | u32 pagesize; |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 104 | u32 size; |
Baruch Siach | a29034d | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 105 | |
Masahiro Yamada | ed6dd4e | 2020-02-28 22:04:13 +0900 | [diff] [blame] | 106 | if (dev_read_u32(dev, "pagesize", &pagesize) == 0) |
Baruch Siach | a29034d | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 107 | priv->pagesize = pagesize; |
Masahiro Yamada | ed6dd4e | 2020-02-28 22:04:13 +0900 | [diff] [blame] | 108 | else |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 109 | /* 6 bit -> page size of up to 2^63 (should be sufficient) */ |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 110 | priv->pagesize = data->pagesize; |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 111 | |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 112 | if (dev_read_u32(dev, "size", &size) == 0) |
| 113 | priv->size = size; |
| 114 | else |
| 115 | priv->size = data->size; |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 120 | static int i2c_eeprom_std_bind(struct udevice *dev) |
| 121 | { |
| 122 | ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions"); |
| 123 | ofnode partition; |
| 124 | const char *name; |
| 125 | |
| 126 | if (!ofnode_valid(partitions)) |
| 127 | return 0; |
| 128 | if (!ofnode_device_is_compatible(partitions, "fixed-partitions")) |
| 129 | return -ENOTSUPP; |
| 130 | |
| 131 | ofnode_for_each_subnode(partition, partitions) { |
| 132 | name = ofnode_get_name(partition); |
| 133 | if (!name) |
| 134 | continue; |
| 135 | |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 136 | device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name, |
Simon Glass | 734206d | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 137 | NULL, partition, NULL); |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Masahiro Yamada | 5e75ea1 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 143 | static int i2c_eeprom_std_probe(struct udevice *dev) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 144 | { |
Baruch Siach | 5ae8486 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 145 | u8 test_byte; |
| 146 | int ret; |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 147 | struct i2c_eeprom_drv_data *data = |
| 148 | (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev); |
| 149 | |
| 150 | i2c_set_chip_offset_len(dev, data->offset_len); |
| 151 | i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask); |
Baruch Siach | 5ae8486 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 152 | |
| 153 | /* Verify that the chip is functional */ |
Eugen Hristev | b24dc83 | 2020-05-07 11:53:18 +0300 | [diff] [blame] | 154 | /* |
| 155 | * Not all eeproms start from offset 0. Valid offset is available |
| 156 | * in the platform data struct. |
| 157 | */ |
| 158 | ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1); |
Baruch Siach | 5ae8486 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 159 | if (ret) |
| 160 | return -ENODEV; |
| 161 | |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 165 | static const struct i2c_eeprom_drv_data eeprom_data = { |
| 166 | .size = 0, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 167 | .pagesize = 1, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 168 | .addr_offset_mask = 0, |
| 169 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static const struct i2c_eeprom_drv_data mc24aa02e48_data = { |
| 173 | .size = 256, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 174 | .pagesize = 8, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 175 | .addr_offset_mask = 0, |
| 176 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static const struct i2c_eeprom_drv_data atmel24c01a_data = { |
| 180 | .size = 128, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 181 | .pagesize = 8, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 182 | .addr_offset_mask = 0, |
| 183 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | static const struct i2c_eeprom_drv_data atmel24c02_data = { |
| 187 | .size = 256, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 188 | .pagesize = 8, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 189 | .addr_offset_mask = 0, |
| 190 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static const struct i2c_eeprom_drv_data atmel24c04_data = { |
| 194 | .size = 512, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 195 | .pagesize = 16, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 196 | .addr_offset_mask = 0x1, |
| 197 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static const struct i2c_eeprom_drv_data atmel24c08_data = { |
| 201 | .size = 1024, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 202 | .pagesize = 16, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 203 | .addr_offset_mask = 0x3, |
| 204 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static const struct i2c_eeprom_drv_data atmel24c08a_data = { |
| 208 | .size = 1024, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 209 | .pagesize = 16, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 210 | .addr_offset_mask = 0x3, |
| 211 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static const struct i2c_eeprom_drv_data atmel24c16a_data = { |
| 215 | .size = 2048, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 216 | .pagesize = 16, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 217 | .addr_offset_mask = 0x7, |
| 218 | .offset_len = 1, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static const struct i2c_eeprom_drv_data atmel24mac402_data = { |
| 222 | .size = 256, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 223 | .pagesize = 16, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 224 | .addr_offset_mask = 0, |
| 225 | .offset_len = 1, |
Eugen Hristev | b24dc83 | 2020-05-07 11:53:18 +0300 | [diff] [blame] | 226 | .start_offset = 0x80, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | static const struct i2c_eeprom_drv_data atmel24c32_data = { |
| 230 | .size = 4096, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 231 | .pagesize = 32, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 232 | .addr_offset_mask = 0, |
| 233 | .offset_len = 2, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | static const struct i2c_eeprom_drv_data atmel24c64_data = { |
| 237 | .size = 8192, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 238 | .pagesize = 32, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 239 | .addr_offset_mask = 0, |
| 240 | .offset_len = 2, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | static const struct i2c_eeprom_drv_data atmel24c128_data = { |
| 244 | .size = 16384, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 245 | .pagesize = 64, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 246 | .addr_offset_mask = 0, |
| 247 | .offset_len = 2, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static const struct i2c_eeprom_drv_data atmel24c256_data = { |
| 251 | .size = 32768, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 252 | .pagesize = 64, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 253 | .addr_offset_mask = 0, |
| 254 | .offset_len = 2, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | static const struct i2c_eeprom_drv_data atmel24c512_data = { |
| 258 | .size = 65536, |
Masahiro Yamada | 0c17bb1 | 2020-02-28 22:04:14 +0900 | [diff] [blame] | 259 | .pagesize = 64, |
Robert Beckett | 821c982 | 2020-01-31 15:07:52 +0200 | [diff] [blame] | 260 | .addr_offset_mask = 0, |
| 261 | .offset_len = 2, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 264 | static const struct udevice_id i2c_eeprom_std_ids[] = { |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 265 | { .compatible = "i2c-eeprom", (ulong)&eeprom_data }, |
| 266 | { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data }, |
Marek Vasut | fc3292c | 2021-07-04 21:31:18 +0200 | [diff] [blame] | 267 | { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data }, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 268 | { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data }, |
| 269 | { .compatible = "atmel,24c02", (ulong)&atmel24c02_data }, |
| 270 | { .compatible = "atmel,24c04", (ulong)&atmel24c04_data }, |
| 271 | { .compatible = "atmel,24c08", (ulong)&atmel24c08_data }, |
| 272 | { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data }, |
| 273 | { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data }, |
| 274 | { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data }, |
| 275 | { .compatible = "atmel,24c32", (ulong)&atmel24c32_data }, |
| 276 | { .compatible = "atmel,24c64", (ulong)&atmel24c64_data }, |
| 277 | { .compatible = "atmel,24c128", (ulong)&atmel24c128_data }, |
| 278 | { .compatible = "atmel,24c256", (ulong)&atmel24c256_data }, |
| 279 | { .compatible = "atmel,24c512", (ulong)&atmel24c512_data }, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 280 | { } |
| 281 | }; |
| 282 | |
| 283 | U_BOOT_DRIVER(i2c_eeprom_std) = { |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 284 | .name = "i2c_eeprom", |
| 285 | .id = UCLASS_I2C_EEPROM, |
| 286 | .of_match = i2c_eeprom_std_ids, |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 287 | .bind = i2c_eeprom_std_bind, |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 288 | .probe = i2c_eeprom_std_probe, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 289 | .of_to_plat = i2c_eeprom_std_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 290 | .priv_auto = sizeof(struct i2c_eeprom), |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 291 | .ops = &i2c_eeprom_std_ops, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 294 | struct i2c_eeprom_partition { |
| 295 | u32 offset; |
| 296 | u32 size; |
| 297 | }; |
| 298 | |
| 299 | static int i2c_eeprom_partition_probe(struct udevice *dev) |
| 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 304 | static int i2c_eeprom_partition_of_to_plat(struct udevice *dev) |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 305 | { |
| 306 | struct i2c_eeprom_partition *priv = dev_get_priv(dev); |
Michal Simek | f692b47 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 307 | u32 reg[2]; |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 308 | int ret; |
| 309 | |
Michal Simek | f692b47 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 310 | ret = dev_read_u32_array(dev, "reg", reg, 2); |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 311 | if (ret) |
| 312 | return ret; |
| 313 | |
Michal Simek | f692b47 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 314 | if (!reg[1]) |
| 315 | return -EINVAL; |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 316 | |
Michal Simek | f692b47 | 2020-05-28 11:48:55 +0200 | [diff] [blame] | 317 | priv->offset = reg[0]; |
| 318 | priv->size = reg[1]; |
| 319 | |
| 320 | debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size); |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int i2c_eeprom_partition_read(struct udevice *dev, int offset, |
| 326 | u8 *buf, int size) |
| 327 | { |
| 328 | struct i2c_eeprom_partition *priv = dev_get_priv(dev); |
| 329 | struct udevice *parent = dev_get_parent(dev); |
| 330 | |
| 331 | if (!parent) |
| 332 | return -ENODEV; |
| 333 | if (offset + size > priv->size) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | return i2c_eeprom_read(parent, offset + priv->offset, buf, size); |
| 337 | } |
| 338 | |
| 339 | static int i2c_eeprom_partition_write(struct udevice *dev, int offset, |
| 340 | const u8 *buf, int size) |
| 341 | { |
| 342 | struct i2c_eeprom_partition *priv = dev_get_priv(dev); |
| 343 | struct udevice *parent = dev_get_parent(dev); |
| 344 | |
| 345 | if (!parent) |
| 346 | return -ENODEV; |
| 347 | if (offset + size > priv->size) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf, |
| 351 | size); |
| 352 | } |
| 353 | |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 354 | static int i2c_eeprom_partition_size(struct udevice *dev) |
| 355 | { |
| 356 | struct i2c_eeprom_partition *priv = dev_get_priv(dev); |
| 357 | |
| 358 | return priv->size; |
| 359 | } |
| 360 | |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 361 | static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = { |
| 362 | .read = i2c_eeprom_partition_read, |
| 363 | .write = i2c_eeprom_partition_write, |
Robert Beckett | 033e18b | 2019-10-28 18:29:06 +0000 | [diff] [blame] | 364 | .size = i2c_eeprom_partition_size, |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | U_BOOT_DRIVER(i2c_eeprom_partition) = { |
| 368 | .name = "i2c_eeprom_partition", |
| 369 | .id = UCLASS_I2C_EEPROM, |
| 370 | .probe = i2c_eeprom_partition_probe, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 371 | .of_to_plat = i2c_eeprom_partition_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 372 | .priv_auto = sizeof(struct i2c_eeprom_partition), |
Robert Beckett | 1a59cb4 | 2019-10-28 18:29:05 +0000 | [diff] [blame] | 373 | .ops = &i2c_eeprom_partition_ops, |
| 374 | }; |
| 375 | |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 376 | UCLASS_DRIVER(i2c_eeprom) = { |
| 377 | .id = UCLASS_I2C_EEPROM, |
| 378 | .name = "i2c_eeprom", |
| 379 | }; |