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 | |
| 6 | #include <common.h> |
Simon Glass | cb3ef68 | 2019-11-14 12:57:50 -0700 | [diff] [blame] | 7 | #include <eeprom.h> |
Masahiro Yamada | ee5ee87 | 2014-12-18 20:00:26 +0900 | [diff] [blame] | 8 | #include <linux/err.h> |
Baruch Siach | 84c80c6 | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <i2c.h> |
| 12 | #include <i2c_eeprom.h> |
| 13 | |
Jonas Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 14 | int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size) |
| 15 | { |
| 16 | const struct i2c_eeprom_ops *ops = device_get_ops(dev); |
| 17 | |
| 18 | if (!ops->read) |
| 19 | return -ENOSYS; |
| 20 | |
| 21 | return ops->read(dev, offset, buf, size); |
| 22 | } |
| 23 | |
| 24 | int i2c_eeprom_write(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->write) |
| 29 | return -ENOSYS; |
| 30 | |
| 31 | return ops->write(dev, offset, buf, size); |
| 32 | } |
| 33 | |
| 34 | static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf, |
| 35 | int size) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 36 | { |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 37 | return dm_i2c_read(dev, offset, buf, size); |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Jonas Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 40 | static int i2c_eeprom_std_write(struct udevice *dev, int offset, |
| 41 | const uint8_t *buf, int size) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 42 | { |
Baruch Siach | 84c80c6 | 2019-04-07 12:38:49 +0300 | [diff] [blame] | 43 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 44 | int ret; |
| 45 | |
| 46 | while (size > 0) { |
| 47 | int write_size = min_t(int, size, priv->pagesize); |
| 48 | |
| 49 | ret = dm_i2c_write(dev, offset, buf, write_size); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | offset += write_size; |
| 54 | buf += write_size; |
| 55 | size -= write_size; |
| 56 | |
| 57 | udelay(10000); |
| 58 | } |
| 59 | |
| 60 | return 0; |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Masahiro Yamada | 5e75ea1 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 63 | static const struct i2c_eeprom_ops i2c_eeprom_std_ops = { |
Jonas Karlman | 8880efb | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 64 | .read = i2c_eeprom_std_read, |
| 65 | .write = i2c_eeprom_std_write, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 68 | static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev) |
| 69 | { |
| 70 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 71 | u64 data = dev_get_driver_data(dev); |
Baruch Siach | a29034d | 2019-04-07 12:38:48 +0300 | [diff] [blame] | 72 | u32 pagesize; |
| 73 | |
| 74 | if (dev_read_u32(dev, "pagesize", &pagesize) == 0) { |
| 75 | priv->pagesize = pagesize; |
| 76 | return 0; |
| 77 | } |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 78 | |
| 79 | /* 6 bit -> page size of up to 2^63 (should be sufficient) */ |
| 80 | priv->pagewidth = data & 0x3F; |
| 81 | priv->pagesize = (1 << priv->pagewidth); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Masahiro Yamada | 5e75ea1 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 86 | static int i2c_eeprom_std_probe(struct udevice *dev) |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 87 | { |
Baruch Siach | 5ae8486 | 2019-08-05 09:03:30 +0300 | [diff] [blame] | 88 | u8 test_byte; |
| 89 | int ret; |
| 90 | |
| 91 | /* Verify that the chip is functional */ |
| 92 | ret = i2c_eeprom_read(dev, 0, &test_byte, 1); |
| 93 | if (ret) |
| 94 | return -ENODEV; |
| 95 | |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const struct udevice_id i2c_eeprom_std_ids[] = { |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 100 | { .compatible = "i2c-eeprom", .data = 0 }, |
Wenyou Yang | 7264066 | 2017-07-31 11:25:30 +0800 | [diff] [blame] | 101 | { .compatible = "microchip,24aa02e48", .data = 3 }, |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 102 | { .compatible = "atmel,24c01a", .data = 3 }, |
| 103 | { .compatible = "atmel,24c02", .data = 3 }, |
| 104 | { .compatible = "atmel,24c04", .data = 4 }, |
Michal Simek | e38cef9 | 2019-01-21 13:54:57 +0100 | [diff] [blame] | 105 | { .compatible = "atmel,24c08", .data = 4 }, |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 106 | { .compatible = "atmel,24c08a", .data = 4 }, |
| 107 | { .compatible = "atmel,24c16a", .data = 4 }, |
Wenyou Yang | de65675 | 2017-07-31 11:25:31 +0800 | [diff] [blame] | 108 | { .compatible = "atmel,24mac402", .data = 4 }, |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 109 | { .compatible = "atmel,24c32", .data = 5 }, |
| 110 | { .compatible = "atmel,24c64", .data = 5 }, |
| 111 | { .compatible = "atmel,24c128", .data = 6 }, |
| 112 | { .compatible = "atmel,24c256", .data = 6 }, |
| 113 | { .compatible = "atmel,24c512", .data = 6 }, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 114 | { } |
| 115 | }; |
| 116 | |
| 117 | U_BOOT_DRIVER(i2c_eeprom_std) = { |
mario.six@gdsys.cc | d7e2891 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 118 | .name = "i2c_eeprom", |
| 119 | .id = UCLASS_I2C_EEPROM, |
| 120 | .of_match = i2c_eeprom_std_ids, |
| 121 | .probe = i2c_eeprom_std_probe, |
| 122 | .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata, |
| 123 | .priv_auto_alloc_size = sizeof(struct i2c_eeprom), |
| 124 | .ops = &i2c_eeprom_std_ops, |
Simon Glass | 2014201 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | UCLASS_DRIVER(i2c_eeprom) = { |
| 128 | .id = UCLASS_I2C_EEPROM, |
| 129 | .name = "i2c_eeprom", |
| 130 | }; |