nvmem.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.
  23. NVMEM Providers
  24. +++++++++++++++
  25. NVMEM provider refers to an entity that implements methods to initialize, read
  26. and write the non-volatile memory.
  27. 2. Registering/Unregistering the NVMEM provider
  28. ===============================================
  29. A NVMEM provider can register with NVMEM core by supplying relevant
  30. nvmem configuration to nvmem_register(), on success core would return a valid
  31. nvmem_device pointer.
  32. nvmem_unregister() is used to unregister a previously registered provider.
  33. For example, a simple nvram case::
  34. static int brcm_nvram_probe(struct platform_device *pdev)
  35. {
  36. struct nvmem_config config = {
  37. .name = "brcm-nvram",
  38. .reg_read = brcm_nvram_read,
  39. };
  40. ...
  41. config.dev = &pdev->dev;
  42. config.priv = priv;
  43. config.size = resource_size(res);
  44. devm_nvmem_register(&config);
  45. }
  46. Device drivers can define and register an nvmem cell using the nvmem_cell_info
  47. struct::
  48. static const struct nvmem_cell_info foo_nvmem_cell = {
  49. {
  50. .name = "macaddr",
  51. .offset = 0x7f00,
  52. .bytes = ETH_ALEN,
  53. }
  54. };
  55. int nvmem_add_one_cell(nvmem, &foo_nvmem_cell);
  56. Additionally it is possible to create nvmem cell lookup entries and register
  57. them with the nvmem framework from machine code as shown in the example below::
  58. static struct nvmem_cell_lookup foo_nvmem_lookup = {
  59. .nvmem_name = "i2c-eeprom",
  60. .cell_name = "macaddr",
  61. .dev_id = "foo_mac.0",
  62. .con_id = "mac-address",
  63. };
  64. nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);
  65. NVMEM Consumers
  66. +++++++++++++++
  67. NVMEM consumers are the entities which make use of the NVMEM provider to
  68. read from and to NVMEM.
  69. 3. NVMEM cell based consumer APIs
  70. =================================
  71. NVMEM cells are the data entries/fields in the NVMEM.
  72. The NVMEM framework provides 3 APIs to read/write NVMEM cells::
  73. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
  74. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
  75. void nvmem_cell_put(struct nvmem_cell *cell);
  76. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  77. void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
  78. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
  79. `*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id,
  80. and nvmem_cell_read/write() can then read or write to the cell.
  81. Once the usage of the cell is finished the consumer should call
  82. `*nvmem_cell_put()` to free all the allocation memory for the cell.
  83. 4. Direct NVMEM device based consumer APIs
  84. ==========================================
  85. In some instances it is necessary to directly read/write the NVMEM.
  86. To facilitate such consumers NVMEM framework provides below apis::
  87. struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
  88. struct nvmem_device *devm_nvmem_device_get(struct device *dev,
  89. const char *name);
  90. struct nvmem_device *nvmem_device_find(void *data,
  91. int (*match)(struct device *dev, const void *data));
  92. void nvmem_device_put(struct nvmem_device *nvmem);
  93. int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
  94. size_t bytes, void *buf);
  95. int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
  96. size_t bytes, void *buf);
  97. int nvmem_device_cell_read(struct nvmem_device *nvmem,
  98. struct nvmem_cell_info *info, void *buf);
  99. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  100. struct nvmem_cell_info *info, void *buf);
  101. Before the consumers can read/write NVMEM directly, it should get hold
  102. of nvmem_controller from one of the `*nvmem_device_get()` api.
  103. The difference between these apis and cell based apis is that these apis always
  104. take nvmem_device as parameter.
  105. 5. Releasing a reference to the NVMEM
  106. =====================================
  107. When a consumer no longer needs the NVMEM, it has to release the reference
  108. to the NVMEM it has obtained using the APIs mentioned in the above section.
  109. The NVMEM framework provides 2 APIs to release a reference to the NVMEM::
  110. void nvmem_cell_put(struct nvmem_cell *cell);
  111. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  112. void nvmem_device_put(struct nvmem_device *nvmem);
  113. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
  114. Both these APIs are used to release a reference to the NVMEM and
  115. devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
  116. with this NVMEM.
  117. Userspace
  118. +++++++++
  119. 6. Userspace binary interface
  120. ==============================
  121. Userspace can read/write the raw NVMEM file located at::
  122. /sys/bus/nvmem/devices/*/nvmem
  123. ex::
  124. hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
  125. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  126. *
  127. 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
  128. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  129. ...
  130. *
  131. 0001000
  132. 7. DeviceTree Binding
  133. =====================
  134. See Documentation/devicetree/bindings/nvmem/nvmem.txt
  135. 8. NVMEM layouts
  136. ================
  137. NVMEM layouts are yet another mechanism to create cells. With the device
  138. tree binding it is possible to specify simple cells by using an offset
  139. and a length. Sometimes, the cells doesn't have a static offset, but
  140. the content is still well defined, e.g. tag-length-values. In this case,
  141. the NVMEM device content has to be first parsed and the cells need to
  142. be added accordingly. Layouts let you read the content of the NVMEM device
  143. and let you add cells dynamically.
  144. Another use case for layouts is the post processing of cells. With layouts,
  145. it is possible to associate a custom post processing hook to a cell. It
  146. even possible to add this hook to cells not created by the layout itself.
  147. 9. Internal kernel API
  148. ======================
  149. .. kernel-doc:: drivers/nvmem/core.c
  150. :export: