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