i2c_eeprom.c 8.6 KB

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