| 1 | |
| 2 |
|
| 3 | ===============
|
| 4 | NVMEM Subsystem
|
| 5 | ===============
|
| 6 |
|
| 7 | Srinivas Kandagatla <[email protected]>
|
| 8 |
|
| 9 | This document explains the NVMEM Framework along with the APIs provided,
|
| 10 | and how to use it.
|
| 11 |
|
| 12 | 1. Introduction
|
| 13 | ===============
|
| 14 | *NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
|
| 15 | retrieve configuration of SOC or Device specific data from non volatile
|
| 16 | memories like eeprom, efuses and so on.
|
| 17 |
|
| 18 | Before this framework existed, NVMEM drivers like eeprom were stored in
|
| 19 | drivers/misc, where they all had to duplicate pretty much the same code to
|
| 20 | register a sysfs file, allow in-kernel users to access the content of the
|
| 21 | devices they were driving, etc.
|
| 22 |
|
| 23 | This was also a problem as far as other in-kernel users were involved, since
|
| 24 | the solutions used were pretty much different from one driver to another, there
|
| 25 | was a rather big abstraction leak.
|
| 26 |
|
| 27 | This framework aims at solve these problems. It also introduces DT
|
| 28 | representation for consumer devices to go get the data they require (MAC
|
| 29 | Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs.
|
| 30 |
|
| 31 | NVMEM Providers
|
| 32 | +++++++++++++++
|
| 33 |
|
| 34 | NVMEM provider refers to an entity that implements methods to initialize, read
|
| 35 | and write the non-volatile memory.
|
| 36 |
|
| 37 | 2. Registering/Unregistering the NVMEM provider
|
| 38 | ===============================================
|
| 39 |
|
| 40 | A NVMEM provider can register with NVMEM core by supplying relevant
|
| 41 | nvmem configuration to nvmem_register(), on success core would return a valid
|
| 42 | nvmem_device pointer.
|
| 43 |
|
| 44 | nvmem_unregister() is used to unregister a previously registered provider.
|
| 45 |
|
| 46 | For example, a simple nvram case::
|
| 47 |
|
| 48 | static int brcm_nvram_probe(struct platform_device *pdev)
|
| 49 | {
|
| 50 | struct nvmem_config config = {
|
| 51 | .name = "brcm-nvram",
|
| 52 | .reg_read = brcm_nvram_read,
|
| 53 | };
|
| 54 | ...
|
| 55 | config.dev = &pdev->dev;
|
| 56 | config.priv = priv;
|
| 57 | config.size = resource_size(res);
|
| 58 |
|
| 59 | devm_nvmem_register(&config);
|
| 60 | }
|
| 61 |
|
| 62 | Device drivers can define and register an nvmem cell using the nvmem_cell_info
|
| 63 | struct::
|
| 64 |
|
| 65 | static const struct nvmem_cell_info foo_nvmem_cell = {
|
| 66 | {
|
| 67 | .name = "macaddr",
|
| 68 | .offset = 0x7f00,
|
| 69 | .bytes = ETH_ALEN,
|
| 70 | }
|
| 71 | };
|
| 72 |
|
| 73 | int nvmem_add_one_cell(nvmem, &foo_nvmem_cell);
|
| 74 |
|
| 75 | Additionally it is possible to create nvmem cell lookup entries and register
|
| 76 | them with the nvmem framework from machine code as shown in the example below::
|
| 77 |
|
| 78 | static struct nvmem_cell_lookup foo_nvmem_lookup = {
|
| 79 | .nvmem_name = "i2c-eeprom",
|
| 80 | .cell_name = "macaddr",
|
| 81 | .dev_id = "foo_mac.0",
|
| 82 | .con_id = "mac-address",
|
| 83 | };
|
| 84 |
|
| 85 | nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);
|
| 86 |
|
| 87 | NVMEM Consumers
|
| 88 | +++++++++++++++
|
| 89 |
|
| 90 | NVMEM consumers are the entities which make use of the NVMEM provider to
|
| 91 | read from and to NVMEM.
|
| 92 |
|
| 93 | 3. NVMEM cell based consumer APIs
|
| 94 | =================================
|
| 95 |
|
| 96 | NVMEM cells are the data entries/fields in the NVMEM.
|
| 97 | The NVMEM framework provides 3 APIs to read/write NVMEM cells::
|
| 98 |
|
| 99 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
|
| 100 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
|
| 101 |
|
| 102 | void nvmem_cell_put(struct nvmem_cell *cell);
|
| 103 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
|
| 104 |
|
| 105 | void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
|
| 106 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
|
| 107 |
|
| 108 | `*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id,
|
| 109 | and nvmem_cell_read/write() can then read or write to the cell.
|
| 110 | Once the usage of the cell is finished the consumer should call
|
| 111 | `*nvmem_cell_put()` to free all the allocation memory for the cell.
|
| 112 |
|
| 113 | 4. Direct NVMEM device based consumer APIs
|
| 114 | ==========================================
|
| 115 |
|
| 116 | In some instances it is necessary to directly read/write the NVMEM.
|
| 117 | To facilitate such consumers NVMEM framework provides below apis::
|
| 118 |
|
| 119 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
|
| 120 | struct nvmem_device *devm_nvmem_device_get(struct device *dev,
|
| 121 | const char *name);
|
| 122 | struct nvmem_device *nvmem_device_find(void *data,
|
| 123 | int (*match)(struct device *dev, const void *data));
|
| 124 | void nvmem_device_put(struct nvmem_device *nvmem);
|
| 125 | int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
|
| 126 | size_t bytes, void *buf);
|
| 127 | int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
|
| 128 | size_t bytes, void *buf);
|
| 129 | int nvmem_device_cell_read(struct nvmem_device *nvmem,
|
| 130 | struct nvmem_cell_info *info, void *buf);
|
| 131 | int nvmem_device_cell_write(struct nvmem_device *nvmem,
|
| 132 | struct nvmem_cell_info *info, void *buf);
|
| 133 |
|
| 134 | Before the consumers can read/write NVMEM directly, it should get hold
|
| 135 | of nvmem_controller from one of the `*nvmem_device_get()` api.
|
| 136 |
|
| 137 | The difference between these apis and cell based apis is that these apis always
|
| 138 | take nvmem_device as parameter.
|
| 139 |
|
| 140 | 5. Releasing a reference to the NVMEM
|
| 141 | =====================================
|
| 142 |
|
| 143 | When a consumer no longer needs the NVMEM, it has to release the reference
|
| 144 | to the NVMEM it has obtained using the APIs mentioned in the above section.
|
| 145 | The NVMEM framework provides 2 APIs to release a reference to the NVMEM::
|
| 146 |
|
| 147 | void nvmem_cell_put(struct nvmem_cell *cell);
|
| 148 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
|
| 149 | void nvmem_device_put(struct nvmem_device *nvmem);
|
| 150 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
|
| 151 |
|
| 152 | Both these APIs are used to release a reference to the NVMEM and
|
| 153 | devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
|
| 154 | with this NVMEM.
|
| 155 |
|
| 156 | Userspace
|
| 157 | +++++++++
|
| 158 |
|
| 159 | 6. Userspace binary interface
|
| 160 | ==============================
|
| 161 |
|
| 162 | Userspace can read/write the raw NVMEM file located at::
|
| 163 |
|
| 164 | /sys/bus/nvmem/devices/*/nvmem
|
| 165 |
|
| 166 | ex::
|
| 167 |
|
| 168 | hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
|
| 169 |
|
| 170 | 0000000 0000 0000 0000 0000 0000 0000 0000 0000
|
| 171 | *
|
| 172 | 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
|
| 173 | 0000000 0000 0000 0000 0000 0000 0000 0000 0000
|
| 174 | ...
|
| 175 | *
|
| 176 | 0001000
|
| 177 |
|
| 178 | 7. DeviceTree Binding
|
| 179 | =====================
|
| 180 |
|
| 181 | See Documentation/devicetree/bindings/nvmem/nvmem.txt
|
| 182 |
|
| 183 | 8. NVMEM layouts
|
| 184 | ================
|
| 185 |
|
| 186 | NVMEM layouts are yet another mechanism to create cells. With the device
|
| 187 | tree binding it is possible to specify simple cells by using an offset
|
| 188 | and a length. Sometimes, the cells doesn't have a static offset, but
|
| 189 | the content is still well defined, e.g. tag-length-values. In this case,
|
| 190 | the NVMEM device content has to be first parsed and the cells need to
|
| 191 | be added accordingly. Layouts let you read the content of the NVMEM device
|
| 192 | and let you add cells dynamically.
|
| 193 |
|
| 194 | Another use case for layouts is the post processing of cells. With layouts,
|
| 195 | it is possible to associate a custom post processing hook to a cell. It
|
| 196 | even possible to add this hook to cells not created by the layout itself.
|
| 197 |
|
| 198 | 9. Internal kernel API
|
| 199 | ======================
|
| 200 |
|
| 201 | .. kernel-doc:: drivers/nvmem/core.c
|
| 202 | :export:
|