Mauro Carvalho Chehab | baa293e | 2019-06-27 15:39:22 -0300 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 2 | |
| 3 | =============== |
| 4 | NVMEM Subsystem |
| 5 | =============== |
| 6 | |
| 7 | Srinivas Kandagatla <srinivas.kandagatla@linaro.org> |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 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. This |
| 30 | framework is based on regmap, so that most of the abstraction available in |
| 31 | regmap can be reused, across multiple types of buses. |
| 32 | |
| 33 | NVMEM Providers |
| 34 | +++++++++++++++ |
| 35 | |
| 36 | NVMEM provider refers to an entity that implements methods to initialize, read |
| 37 | and write the non-volatile memory. |
| 38 | |
| 39 | 2. Registering/Unregistering the NVMEM provider |
| 40 | =============================================== |
| 41 | |
| 42 | A NVMEM provider can register with NVMEM core by supplying relevant |
| 43 | nvmem configuration to nvmem_register(), on success core would return a valid |
| 44 | nvmem_device pointer. |
| 45 | |
| 46 | nvmem_unregister(nvmem) is used to unregister a previously registered provider. |
| 47 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 48 | For example, a simple qfprom case:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 49 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 50 | static struct nvmem_config econfig = { |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 51 | .name = "qfprom", |
| 52 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 53 | }; |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 54 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 55 | static int qfprom_probe(struct platform_device *pdev) |
| 56 | { |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 57 | ... |
| 58 | econfig.dev = &pdev->dev; |
| 59 | nvmem = nvmem_register(&econfig); |
| 60 | ... |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 61 | } |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 62 | |
| 63 | It is mandatory that the NVMEM provider has a regmap associated with its |
| 64 | struct device. Failure to do would return error code from nvmem_register(). |
| 65 | |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 66 | Users of board files can define and register nvmem cells using the |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 67 | nvmem_cell_table struct:: |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 68 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 69 | static struct nvmem_cell_info foo_nvmem_cells[] = { |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 70 | { |
| 71 | .name = "macaddr", |
| 72 | .offset = 0x7f00, |
| 73 | .bytes = ETH_ALEN, |
| 74 | } |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 75 | }; |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 76 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 77 | static struct nvmem_cell_table foo_nvmem_cell_table = { |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 78 | .nvmem_name = "i2c-eeprom", |
| 79 | .cells = foo_nvmem_cells, |
| 80 | .ncells = ARRAY_SIZE(foo_nvmem_cells), |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 81 | }; |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 82 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 83 | nvmem_add_cell_table(&foo_nvmem_cell_table); |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 84 | |
| 85 | Additionally it is possible to create nvmem cell lookup entries and register |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 86 | them with the nvmem framework from machine code as shown in the example below:: |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 87 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 88 | static struct nvmem_cell_lookup foo_nvmem_lookup = { |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 89 | .nvmem_name = "i2c-eeprom", |
| 90 | .cell_name = "macaddr", |
| 91 | .dev_id = "foo_mac.0", |
| 92 | .con_id = "mac-address", |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 93 | }; |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 94 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 95 | nvmem_add_cell_lookups(&foo_nvmem_lookup, 1); |
Bartosz Golaszewski | 4903d19 | 2018-09-21 06:40:18 -0700 | [diff] [blame] | 96 | |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 97 | NVMEM Consumers |
| 98 | +++++++++++++++ |
| 99 | |
| 100 | NVMEM consumers are the entities which make use of the NVMEM provider to |
| 101 | read from and to NVMEM. |
| 102 | |
| 103 | 3. NVMEM cell based consumer APIs |
| 104 | ================================= |
| 105 | |
| 106 | NVMEM cells are the data entries/fields in the NVMEM. |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 107 | The NVMEM framework provides 3 APIs to read/write NVMEM cells:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 108 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 109 | struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name); |
| 110 | struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name); |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 111 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 112 | void nvmem_cell_put(struct nvmem_cell *cell); |
| 113 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell); |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 114 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 115 | void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len); |
| 116 | int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len); |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 117 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 118 | `*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 119 | and nvmem_cell_read/write() can then read or write to the cell. |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 120 | Once the usage of the cell is finished the consumer should call |
| 121 | `*nvmem_cell_put()` to free all the allocation memory for the cell. |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 122 | |
| 123 | 4. Direct NVMEM device based consumer APIs |
| 124 | ========================================== |
| 125 | |
| 126 | In some instances it is necessary to directly read/write the NVMEM. |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 127 | To facilitate such consumers NVMEM framework provides below apis:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 128 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 129 | struct nvmem_device *nvmem_device_get(struct device *dev, const char *name); |
| 130 | struct nvmem_device *devm_nvmem_device_get(struct device *dev, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 131 | const char *name); |
Thomas Bogendoerfer | 8c2a2b8 | 2019-10-03 11:52:29 +0200 | [diff] [blame] | 132 | struct nvmem_device *nvmem_device_find(void *data, |
| 133 | int (*match)(struct device *dev, const void *data)); |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 134 | void nvmem_device_put(struct nvmem_device *nvmem); |
| 135 | int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 136 | size_t bytes, void *buf); |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 137 | int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 138 | size_t bytes, void *buf); |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 139 | int nvmem_device_cell_read(struct nvmem_device *nvmem, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 140 | struct nvmem_cell_info *info, void *buf); |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 141 | int nvmem_device_cell_write(struct nvmem_device *nvmem, |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 142 | struct nvmem_cell_info *info, void *buf); |
| 143 | |
| 144 | Before the consumers can read/write NVMEM directly, it should get hold |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 145 | of nvmem_controller from one of the `*nvmem_device_get()` api. |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 146 | |
| 147 | The difference between these apis and cell based apis is that these apis always |
| 148 | take nvmem_device as parameter. |
| 149 | |
| 150 | 5. Releasing a reference to the NVMEM |
| 151 | ===================================== |
| 152 | |
Naren | be629b4 | 2017-08-16 15:45:57 -0700 | [diff] [blame] | 153 | When a consumer no longer needs the NVMEM, it has to release the reference |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 154 | to the NVMEM it has obtained using the APIs mentioned in the above section. |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 155 | The NVMEM framework provides 2 APIs to release a reference to the NVMEM:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 156 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 157 | void nvmem_cell_put(struct nvmem_cell *cell); |
| 158 | void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell); |
| 159 | void nvmem_device_put(struct nvmem_device *nvmem); |
| 160 | void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem); |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 161 | |
| 162 | Both these APIs are used to release a reference to the NVMEM and |
| 163 | devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated |
| 164 | with this NVMEM. |
| 165 | |
| 166 | Userspace |
| 167 | +++++++++ |
| 168 | |
| 169 | 6. Userspace binary interface |
| 170 | ============================== |
| 171 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 172 | Userspace can read/write the raw NVMEM file located at:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 173 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 174 | /sys/bus/nvmem/devices/*/nvmem |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 175 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 176 | ex:: |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 177 | |
Mauro Carvalho Chehab | a278295 | 2019-04-15 19:25:27 -0300 | [diff] [blame] | 178 | hexdump /sys/bus/nvmem/devices/qfprom0/nvmem |
| 179 | |
| 180 | 0000000 0000 0000 0000 0000 0000 0000 0000 0000 |
| 181 | * |
| 182 | 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00 |
| 183 | 0000000 0000 0000 0000 0000 0000 0000 0000 0000 |
| 184 | ... |
| 185 | * |
| 186 | 0001000 |
Srinivas Kandagatla | 354ebb5 | 2015-07-27 12:14:14 +0100 | [diff] [blame] | 187 | |
| 188 | 7. DeviceTree Binding |
| 189 | ===================== |
| 190 | |
| 191 | See Documentation/devicetree/bindings/nvmem/nvmem.txt |