i2c_eeprom.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #define LOG_CATEGORY UCLASS_I2C_EEPROM
  6. #include <common.h>
  7. #include <eeprom.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <dm.h>
  12. #include <dm/device-internal.h>
  13. #include <i2c.h>
  14. #include <i2c_eeprom.h>
  15. struct i2c_eeprom_drv_data {
  16. u32 size; /* size in bytes */
  17. u32 pagesize; /* page size in bytes */
  18. u32 addr_offset_mask; /* bits in addr used for offset overflow */
  19. u32 offset_len; /* size in bytes of offset */
  20. u32 start_offset; /* valid start offset inside memory, by default 0 */
  21. };
  22. int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
  23. {
  24. const struct i2c_eeprom_ops *ops = device_get_ops(dev);
  25. if (!ops->read)
  26. return -ENOSYS;
  27. return ops->read(dev, offset, buf, size);
  28. }
  29. int i2c_eeprom_write(struct udevice *dev, int offset, const uint8_t *buf,
  30. int size)
  31. {
  32. const struct i2c_eeprom_ops *ops = device_get_ops(dev);
  33. if (!ops->write)
  34. return -ENOSYS;
  35. return ops->write(dev, offset, buf, size);
  36. }
  37. int i2c_eeprom_size(struct udevice *dev)
  38. {
  39. const struct i2c_eeprom_ops *ops = device_get_ops(dev);
  40. if (!ops->size)
  41. return -ENOSYS;
  42. return ops->size(dev);
  43. }
  44. static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
  45. int size)
  46. {
  47. return dm_i2c_read(dev, offset, buf, size);
  48. }
  49. static int i2c_eeprom_std_write(struct udevice *dev, int offset,
  50. const uint8_t *buf, int size)
  51. {
  52. struct i2c_eeprom *priv = dev_get_priv(dev);
  53. int ret;
  54. while (size > 0) {
  55. int write_size = min_t(int, size, priv->pagesize);
  56. ret = dm_i2c_write(dev, offset, buf, write_size);
  57. if (ret)
  58. return ret;
  59. offset += write_size;
  60. buf += write_size;
  61. size -= write_size;
  62. udelay(10000);
  63. }
  64. return 0;
  65. }
  66. static int i2c_eeprom_std_size(struct udevice *dev)
  67. {
  68. struct i2c_eeprom *priv = dev_get_priv(dev);
  69. return priv->size;
  70. }
  71. static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
  72. .read = i2c_eeprom_std_read,
  73. .write = i2c_eeprom_std_write,
  74. .size = i2c_eeprom_std_size,
  75. };
  76. static int i2c_eeprom_std_of_to_plat(struct udevice *dev)
  77. {
  78. struct i2c_eeprom *priv = dev_get_priv(dev);
  79. struct i2c_eeprom_drv_data *data =
  80. (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
  81. u32 pagesize;
  82. u32 size;
  83. if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
  84. priv->pagesize = pagesize;
  85. else
  86. /* 6 bit -> page size of up to 2^63 (should be sufficient) */
  87. priv->pagesize = data->pagesize;
  88. if (dev_read_u32(dev, "size", &size) == 0)
  89. priv->size = size;
  90. else
  91. priv->size = data->size;
  92. return 0;
  93. }
  94. static int i2c_eeprom_std_bind(struct udevice *dev)
  95. {
  96. ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
  97. ofnode partition;
  98. const char *name;
  99. if (!ofnode_valid(partitions))
  100. return 0;
  101. if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
  102. return -ENOTSUPP;
  103. ofnode_for_each_subnode(partition, partitions) {
  104. name = ofnode_get_name(partition);
  105. if (!name)
  106. continue;
  107. device_bind(dev, DM_DRIVER_GET(i2c_eeprom_partition), name,
  108. NULL, partition, NULL);
  109. }
  110. return 0;
  111. }
  112. static int i2c_eeprom_std_probe(struct udevice *dev)
  113. {
  114. u8 test_byte;
  115. int ret;
  116. struct i2c_eeprom_drv_data *data =
  117. (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
  118. i2c_set_chip_offset_len(dev, data->offset_len);
  119. i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
  120. /* Verify that the chip is functional */
  121. /*
  122. * Not all eeproms start from offset 0. Valid offset is available
  123. * in the platform data struct.
  124. */
  125. ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
  126. if (ret)
  127. return -ENODEV;
  128. return 0;
  129. }
  130. static const struct i2c_eeprom_drv_data eeprom_data = {
  131. .size = 0,
  132. .pagesize = 1,
  133. .addr_offset_mask = 0,
  134. .offset_len = 1,
  135. };
  136. static const struct i2c_eeprom_drv_data atmel24c01a_data = {
  137. .size = 128,
  138. .pagesize = 8,
  139. .addr_offset_mask = 0,
  140. .offset_len = 1,
  141. };
  142. static const struct i2c_eeprom_drv_data atmel24c02_data = {
  143. .size = 256,
  144. .pagesize = 8,
  145. .addr_offset_mask = 0,
  146. .offset_len = 1,
  147. };
  148. static const struct i2c_eeprom_drv_data atmel24c04_data = {
  149. .size = 512,
  150. .pagesize = 16,
  151. .addr_offset_mask = 0x1,
  152. .offset_len = 1,
  153. };
  154. static const struct i2c_eeprom_drv_data atmel24c08_data = {
  155. .size = 1024,
  156. .pagesize = 16,
  157. .addr_offset_mask = 0x3,
  158. .offset_len = 1,
  159. };
  160. static const struct i2c_eeprom_drv_data atmel24c08a_data = {
  161. .size = 1024,
  162. .pagesize = 16,
  163. .addr_offset_mask = 0x3,
  164. .offset_len = 1,
  165. };
  166. static const struct i2c_eeprom_drv_data atmel24c16a_data = {
  167. .size = 2048,
  168. .pagesize = 16,
  169. .addr_offset_mask = 0x7,
  170. .offset_len = 1,
  171. };
  172. static const struct i2c_eeprom_drv_data atmel24mac402_data = {
  173. .size = 256,
  174. .pagesize = 16,
  175. .addr_offset_mask = 0,
  176. .offset_len = 1,
  177. .start_offset = 0x80,
  178. };
  179. static const struct i2c_eeprom_drv_data atmel24c32_data = {
  180. .size = 4096,
  181. .pagesize = 32,
  182. .addr_offset_mask = 0,
  183. .offset_len = 2,
  184. };
  185. static const struct i2c_eeprom_drv_data atmel24c64_data = {
  186. .size = 8192,
  187. .pagesize = 32,
  188. .addr_offset_mask = 0,
  189. .offset_len = 2,
  190. };
  191. static const struct i2c_eeprom_drv_data atmel24c128_data = {
  192. .size = 16384,
  193. .pagesize = 64,
  194. .addr_offset_mask = 0,
  195. .offset_len = 2,
  196. };
  197. static const struct i2c_eeprom_drv_data atmel24c256_data = {
  198. .size = 32768,
  199. .pagesize = 64,
  200. .addr_offset_mask = 0,
  201. .offset_len = 2,
  202. };
  203. static const struct i2c_eeprom_drv_data atmel24c512_data = {
  204. .size = 65536,
  205. .pagesize = 64,
  206. .addr_offset_mask = 0,
  207. .offset_len = 2,
  208. };
  209. static const struct udevice_id i2c_eeprom_std_ids[] = {
  210. { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
  211. { .compatible = "atmel,24c01", (ulong)&atmel24c01a_data },
  212. { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
  213. { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
  214. { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
  215. { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
  216. { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
  217. { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
  218. { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
  219. { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
  220. { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
  221. { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
  222. { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
  223. { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
  224. { }
  225. };
  226. U_BOOT_DRIVER(i2c_eeprom_std) = {
  227. .name = "i2c_eeprom",
  228. .id = UCLASS_I2C_EEPROM,
  229. .of_match = i2c_eeprom_std_ids,
  230. .bind = i2c_eeprom_std_bind,
  231. .probe = i2c_eeprom_std_probe,
  232. .of_to_plat = i2c_eeprom_std_of_to_plat,
  233. .priv_auto = sizeof(struct i2c_eeprom),
  234. .ops = &i2c_eeprom_std_ops,
  235. };
  236. struct i2c_eeprom_partition {
  237. u32 offset;
  238. u32 size;
  239. };
  240. static int i2c_eeprom_partition_probe(struct udevice *dev)
  241. {
  242. return 0;
  243. }
  244. static int i2c_eeprom_partition_of_to_plat(struct udevice *dev)
  245. {
  246. struct i2c_eeprom_partition *priv = dev_get_priv(dev);
  247. u32 reg[2];
  248. int ret;
  249. ret = dev_read_u32_array(dev, "reg", reg, 2);
  250. if (ret)
  251. return ret;
  252. if (!reg[1])
  253. return -EINVAL;
  254. priv->offset = reg[0];
  255. priv->size = reg[1];
  256. debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
  257. return 0;
  258. }
  259. static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
  260. u8 *buf, int size)
  261. {
  262. struct i2c_eeprom_partition *priv = dev_get_priv(dev);
  263. struct udevice *parent = dev_get_parent(dev);
  264. if (!parent)
  265. return -ENODEV;
  266. if (offset + size > priv->size)
  267. return -EINVAL;
  268. return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
  269. }
  270. static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
  271. const u8 *buf, int size)
  272. {
  273. struct i2c_eeprom_partition *priv = dev_get_priv(dev);
  274. struct udevice *parent = dev_get_parent(dev);
  275. if (!parent)
  276. return -ENODEV;
  277. if (offset + size > priv->size)
  278. return -EINVAL;
  279. return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
  280. size);
  281. }
  282. static int i2c_eeprom_partition_size(struct udevice *dev)
  283. {
  284. struct i2c_eeprom_partition *priv = dev_get_priv(dev);
  285. return priv->size;
  286. }
  287. static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
  288. .read = i2c_eeprom_partition_read,
  289. .write = i2c_eeprom_partition_write,
  290. .size = i2c_eeprom_partition_size,
  291. };
  292. U_BOOT_DRIVER(i2c_eeprom_partition) = {
  293. .name = "i2c_eeprom_partition",
  294. .id = UCLASS_I2C_EEPROM,
  295. .probe = i2c_eeprom_partition_probe,
  296. .of_to_plat = i2c_eeprom_partition_of_to_plat,
  297. .priv_auto = sizeof(struct i2c_eeprom_partition),
  298. .ops = &i2c_eeprom_partition_ops,
  299. };
  300. UCLASS_DRIVER(i2c_eeprom) = {
  301. .id = UCLASS_I2C_EEPROM,
  302. .name = "i2c_eeprom",
  303. };