nvmem.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
  4. */
  5. #include <common.h>
  6. #include <i2c_eeprom.h>
  7. #include <linker_lists.h>
  8. #include <misc.h>
  9. #include <nvmem.h>
  10. #include <rtc.h>
  11. #include <dm/device_compat.h>
  12. #include <dm/ofnode.h>
  13. #include <dm/read.h>
  14. #include <dm/uclass.h>
  15. int nvmem_cell_read(struct nvmem_cell *cell, void *buf, size_t size)
  16. {
  17. dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size);
  18. if (size != cell->size)
  19. return -EINVAL;
  20. switch (cell->nvmem->driver->id) {
  21. case UCLASS_I2C_EEPROM:
  22. return i2c_eeprom_read(cell->nvmem, cell->offset, buf, size);
  23. case UCLASS_MISC: {
  24. int ret = misc_read(cell->nvmem, cell->offset, buf, size);
  25. if (ret < 0)
  26. return ret;
  27. if (ret != size)
  28. return -EIO;
  29. return 0;
  30. }
  31. case UCLASS_RTC:
  32. return dm_rtc_read(cell->nvmem, cell->offset, buf, size);
  33. default:
  34. return -ENOSYS;
  35. }
  36. }
  37. int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, size_t size)
  38. {
  39. dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size);
  40. if (size != cell->size)
  41. return -EINVAL;
  42. switch (cell->nvmem->driver->id) {
  43. case UCLASS_I2C_EEPROM:
  44. return i2c_eeprom_write(cell->nvmem, cell->offset, buf, size);
  45. case UCLASS_MISC: {
  46. int ret = misc_write(cell->nvmem, cell->offset, buf, size);
  47. if (ret < 0)
  48. return ret;
  49. if (ret != size)
  50. return -EIO;
  51. return 0;
  52. }
  53. case UCLASS_RTC:
  54. return dm_rtc_write(cell->nvmem, cell->offset, buf, size);
  55. default:
  56. return -ENOSYS;
  57. }
  58. }
  59. /**
  60. * nvmem_get_device() - Get an nvmem device for a cell
  61. * @node: ofnode of the nvmem device
  62. * @cell: Cell to look up
  63. *
  64. * Try to find a nvmem-compatible device by going through the nvmem interfaces.
  65. *
  66. * Return:
  67. * * 0 on success
  68. * * -ENODEV if we didn't find anything
  69. * * A negative error if there was a problem looking up the device
  70. */
  71. static int nvmem_get_device(ofnode node, struct nvmem_cell *cell)
  72. {
  73. int i, ret;
  74. enum uclass_id ids[] = {
  75. UCLASS_I2C_EEPROM,
  76. UCLASS_MISC,
  77. UCLASS_RTC,
  78. };
  79. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  80. ret = uclass_get_device_by_ofnode(ids[i], node, &cell->nvmem);
  81. if (!ret)
  82. return 0;
  83. if (ret != -ENODEV && ret != -EPFNOSUPPORT)
  84. return ret;
  85. }
  86. return -ENODEV;
  87. }
  88. int nvmem_cell_get_by_index(struct udevice *dev, int index,
  89. struct nvmem_cell *cell)
  90. {
  91. fdt_addr_t offset;
  92. fdt_size_t size = FDT_SIZE_T_NONE;
  93. int ret;
  94. struct ofnode_phandle_args args;
  95. dev_dbg(dev, "%s: index=%d\n", __func__, index);
  96. ret = dev_read_phandle_with_args(dev, "nvmem-cells", NULL, 0, index,
  97. &args);
  98. if (ret)
  99. return ret;
  100. ret = nvmem_get_device(ofnode_get_parent(args.node), cell);
  101. if (ret)
  102. return ret;
  103. offset = ofnode_get_addr_size_index_notrans(args.node, 0, &size);
  104. if (offset == FDT_ADDR_T_NONE || size == FDT_SIZE_T_NONE) {
  105. dev_dbg(cell->nvmem, "missing address or size for %s\n",
  106. ofnode_get_name(args.node));
  107. return -EINVAL;
  108. }
  109. cell->offset = offset;
  110. cell->size = size;
  111. return 0;
  112. }
  113. int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
  114. struct nvmem_cell *cell)
  115. {
  116. int index;
  117. dev_dbg(dev, "%s, name=%s\n", __func__, name);
  118. index = dev_read_stringlist_search(dev, "nvmem-cell-names", name);
  119. if (index < 0)
  120. return index;
  121. return nvmem_cell_get_by_index(dev, index, cell);
  122. }