nvmem.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. NVMEM Subsystem
  4. ===============
  5. Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  6. This document explains the NVMEM Framework along with the APIs provided,
  7. and how to use it.
  8. 1. Introduction
  9. ===============
  10. *NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
  11. retrieve configuration of SOC or Device specific data from non volatile
  12. memories like eeprom, efuses and so on.
  13. Before this framework existed, NVMEM drivers like eeprom were stored in
  14. drivers/misc, where they all had to duplicate pretty much the same code to
  15. register a sysfs file, allow in-kernel users to access the content of the
  16. devices they were driving, etc.
  17. This was also a problem as far as other in-kernel users were involved, since
  18. the solutions used were pretty much different from one driver to another, there
  19. was a rather big abstraction leak.
  20. This framework aims at solve these problems. It also introduces DT
  21. representation for consumer devices to go get the data they require (MAC
  22. Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs. This
  23. framework is based on regmap, so that most of the abstraction available in
  24. regmap can be reused, across multiple types of buses.
  25. NVMEM Providers
  26. +++++++++++++++
  27. NVMEM provider refers to an entity that implements methods to initialize, read
  28. and write the non-volatile memory.
  29. 2. Registering/Unregistering the NVMEM provider
  30. ===============================================
  31. A NVMEM provider can register with NVMEM core by supplying relevant
  32. nvmem configuration to nvmem_register(), on success core would return a valid
  33. nvmem_device pointer.
  34. nvmem_unregister(nvmem) is used to unregister a previously registered provider.
  35. For example, a simple qfprom case::
  36. static struct nvmem_config econfig = {
  37. .name = "qfprom",
  38. .owner = THIS_MODULE,
  39. };
  40. static int qfprom_probe(struct platform_device *pdev)
  41. {
  42. ...
  43. econfig.dev = &pdev->dev;
  44. nvmem = nvmem_register(&econfig);
  45. ...
  46. }
  47. It is mandatory that the NVMEM provider has a regmap associated with its
  48. struct device. Failure to do would return error code from nvmem_register().
  49. Users of board files can define and register nvmem cells using the
  50. nvmem_cell_table struct::
  51. static struct nvmem_cell_info foo_nvmem_cells[] = {
  52. {
  53. .name = "macaddr",
  54. .offset = 0x7f00,
  55. .bytes = ETH_ALEN,
  56. }
  57. };
  58. static struct nvmem_cell_table foo_nvmem_cell_table = {
  59. .nvmem_name = "i2c-eeprom",
  60. .cells = foo_nvmem_cells,
  61. .ncells = ARRAY_SIZE(foo_nvmem_cells),
  62. };
  63. nvmem_add_cell_table(&foo_nvmem_cell_table);
  64. Additionally it is possible to create nvmem cell lookup entries and register
  65. them with the nvmem framework from machine code as shown in the example below::
  66. static struct nvmem_cell_lookup foo_nvmem_lookup = {
  67. .nvmem_name = "i2c-eeprom",
  68. .cell_name = "macaddr",
  69. .dev_id = "foo_mac.0",
  70. .con_id = "mac-address",
  71. };
  72. nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);
  73. NVMEM Consumers
  74. +++++++++++++++
  75. NVMEM consumers are the entities which make use of the NVMEM provider to
  76. read from and to NVMEM.
  77. 3. NVMEM cell based consumer APIs
  78. =================================
  79. NVMEM cells are the data entries/fields in the NVMEM.
  80. The NVMEM framework provides 3 APIs to read/write NVMEM cells::
  81. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
  82. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
  83. void nvmem_cell_put(struct nvmem_cell *cell);
  84. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  85. void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
  86. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
  87. `*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id,
  88. and nvmem_cell_read/write() can then read or write to the cell.
  89. Once the usage of the cell is finished the consumer should call
  90. `*nvmem_cell_put()` to free all the allocation memory for the cell.
  91. 4. Direct NVMEM device based consumer APIs
  92. ==========================================
  93. In some instances it is necessary to directly read/write the NVMEM.
  94. To facilitate such consumers NVMEM framework provides below apis::
  95. struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
  96. struct nvmem_device *devm_nvmem_device_get(struct device *dev,
  97. const char *name);
  98. struct nvmem_device *nvmem_device_find(void *data,
  99. int (*match)(struct device *dev, const void *data));
  100. void nvmem_device_put(struct nvmem_device *nvmem);
  101. int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
  102. size_t bytes, void *buf);
  103. int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
  104. size_t bytes, void *buf);
  105. int nvmem_device_cell_read(struct nvmem_device *nvmem,
  106. struct nvmem_cell_info *info, void *buf);
  107. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  108. struct nvmem_cell_info *info, void *buf);
  109. Before the consumers can read/write NVMEM directly, it should get hold
  110. of nvmem_controller from one of the `*nvmem_device_get()` api.
  111. The difference between these apis and cell based apis is that these apis always
  112. take nvmem_device as parameter.
  113. 5. Releasing a reference to the NVMEM
  114. =====================================
  115. When a consumer no longer needs the NVMEM, it has to release the reference
  116. to the NVMEM it has obtained using the APIs mentioned in the above section.
  117. The NVMEM framework provides 2 APIs to release a reference to the NVMEM::
  118. void nvmem_cell_put(struct nvmem_cell *cell);
  119. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  120. void nvmem_device_put(struct nvmem_device *nvmem);
  121. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
  122. Both these APIs are used to release a reference to the NVMEM and
  123. devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
  124. with this NVMEM.
  125. Userspace
  126. +++++++++
  127. 6. Userspace binary interface
  128. ==============================
  129. Userspace can read/write the raw NVMEM file located at::
  130. /sys/bus/nvmem/devices/*/nvmem
  131. ex::
  132. hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
  133. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  134. *
  135. 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
  136. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  137. ...
  138. *
  139. 0001000
  140. 7. DeviceTree Binding
  141. =====================
  142. See Documentation/devicetree/bindings/nvmem/nvmem.txt