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