xref: /freebsd/share/man/man9/nvmem.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1cfe196fbSEmmanuel Vadot.\" Copyright (c) 2018 Emmanuel Vadot <manu@freebsd.org>
2cfe196fbSEmmanuel Vadot.\"
3cfe196fbSEmmanuel Vadot.\" Redistribution and use in source and binary forms, with or without
4cfe196fbSEmmanuel Vadot.\" modification, are permitted provided that the following conditions
5cfe196fbSEmmanuel Vadot.\" are met:
6cfe196fbSEmmanuel Vadot.\" 1. Redistributions of source code must retain the above copyright
7cfe196fbSEmmanuel Vadot.\"    notice, this list of conditions and the following disclaimer.
8cfe196fbSEmmanuel Vadot.\" 2. Redistributions in binary form must reproduce the above copyright
9cfe196fbSEmmanuel Vadot.\"    notice, this list of conditions and the following disclaimer in the
10cfe196fbSEmmanuel Vadot.\"    documentation and/or other materials provided with the distribution.
11cfe196fbSEmmanuel Vadot.\"
12cfe196fbSEmmanuel Vadot.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
13cfe196fbSEmmanuel Vadot.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14cfe196fbSEmmanuel Vadot.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15cfe196fbSEmmanuel Vadot.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
16cfe196fbSEmmanuel Vadot.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17cfe196fbSEmmanuel Vadot.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18cfe196fbSEmmanuel Vadot.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19cfe196fbSEmmanuel Vadot.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20cfe196fbSEmmanuel Vadot.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21cfe196fbSEmmanuel Vadot.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22cfe196fbSEmmanuel Vadot.\"
23cfe196fbSEmmanuel Vadot.Dd July 24, 2018
24cfe196fbSEmmanuel Vadot.Dt nvmem 9
25cfe196fbSEmmanuel Vadot.Os
26cfe196fbSEmmanuel Vadot.Sh NAME
27*32068667SChristian Brueffer.Nm nvmem ,
28cfe196fbSEmmanuel Vadot.Nm nvmem_get_cell_len ,
29cfe196fbSEmmanuel Vadot.Nm nvmem_read_cell_by_name ,
30cfe196fbSEmmanuel Vadot.Nm nvmem_read_cell_by_idx ,
31cfe196fbSEmmanuel Vadot.Nm nvmem_write_cell_by_name ,
32*32068667SChristian Brueffer.Nm nvmem_write_cell_by_idx
33cfe196fbSEmmanuel Vadot.Sh SYNOPSIS
34cfe196fbSEmmanuel Vadot.Cd "options FDT"
35cfe196fbSEmmanuel Vadot.Cd "device nvmem"
36cfe196fbSEmmanuel Vadot.In sys/extres/nvmem/nvmem.h
37cfe196fbSEmmanuel Vadot.Ft int
38cfe196fbSEmmanuel Vadot.Fn nvmem_get_cell_len "phandle_t node" "const char *name"
39cfe196fbSEmmanuel Vadot.Ft int
40cfe196fbSEmmanuel Vadot.Fn nvmem_read_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
41cfe196fbSEmmanuel Vadot.Ft int
42cfe196fbSEmmanuel Vadot.Fn nvmem_read_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
43cfe196fbSEmmanuel Vadot.Ft int
44cfe196fbSEmmanuel Vadot.Fn nvmem_write_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
45cfe196fbSEmmanuel Vadot.Ft int
46cfe196fbSEmmanuel Vadot.Fn nvmem_write_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
47cfe196fbSEmmanuel Vadot.Sh DESCRIPTION
48cfe196fbSEmmanuel VadotOn some embedded boards, the manufacturer stored some data on a NVMEM
49cfe196fbSEmmanuel Vadot(Non-Volatile Memory), this is generally stored in some eeprom or fuses.
50cfe196fbSEmmanuel Vadot.Pp
51cfe196fbSEmmanuel VadotThe
52cfe196fbSEmmanuel Vadot.Nm
53cfe196fbSEmmanuel VadotAPI consist of helpers functions for consumer and device methods for
54cfe196fbSEmmanuel Vadotproviders.
55cfe196fbSEmmanuel Vadot.Sh FUNCTIONS
56cfe196fbSEmmanuel Vadot.Bl -tag -width indent
57cfe196fbSEmmanuel Vadot.It Fn nvmem_get_cell_len  "phandle_t node" "const char *name"
58cfe196fbSEmmanuel VadotGet the size of the cell base on the reg property on the node.
59cfe196fbSEmmanuel VadotReturn the size or ENOENT if the cell name wasn't found
60cfe196fbSEmmanuel Vadot.It Fn nvmem_read_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
61cfe196fbSEmmanuel VadotGet the cell content based on the name.
62cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
63cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
64cfe196fbSEmmanuel Vadot.It Fn nvmem_read_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
65cfe196fbSEmmanuel VadotGet the cell content based on the id.
66cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
67cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
68cfe196fbSEmmanuel Vadot.It Fn nvmem_write_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
69cfe196fbSEmmanuel VadotWrite the cell content based on the name.
70cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
71cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
72cfe196fbSEmmanuel Vadot.It Fn nvmem_write_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
73cfe196fbSEmmanuel VadotWrite the cell content based on the id.
74cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
75cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
76cfe196fbSEmmanuel Vadot.El
77cfe196fbSEmmanuel Vadot.Sh DEVICE METHODS
78cfe196fbSEmmanuel Vadot.Bl -tag -width indent
79cfe196fbSEmmanuel Vadot.It Fn nvmem_read "device_t dev" "uint32_t offset" "uint32_t size" "uint8_t *buffer"
80cfe196fbSEmmanuel VadotProvider device method to read a cell content.
81cfe196fbSEmmanuel Vadot.It Fn nvmem_write "device_t dev" "uint32_t offset" "uint32_t size" "uint8_t *buffer"
82cfe196fbSEmmanuel VadotProvider device method to write a cell content.
83cfe196fbSEmmanuel Vadot.El
84cfe196fbSEmmanuel Vadot.Sh EXAMPLES
85cfe196fbSEmmanuel VadotConsider this DTS
86cfe196fbSEmmanuel Vadot.Bd -literal
87cfe196fbSEmmanuel Vadot/* Provider */
88cfe196fbSEmmanuel Vadoteeprom: eeprom@20000 {
89cfe196fbSEmmanuel Vadot	board_id: id@0 {
90cfe196fbSEmmanuel Vadot		reg = <0x0 0x4>;
91cfe196fbSEmmanuel Vadot	};
92cfe196fbSEmmanuel Vadot};
93cfe196fbSEmmanuel Vadot/* Consumer */
94cfe196fbSEmmanuel Vadotdevice@30000 {
95cfe196fbSEmmanuel Vadot	...
96cfe196fbSEmmanuel Vadot
97cfe196fbSEmmanuel Vadot	nvmem-cells = <&board_id>
98cfe196fbSEmmanuel Vadot	nvmem-cell-names = "boardid";
99cfe196fbSEmmanuel Vadot};
100cfe196fbSEmmanuel Vadot.Ed
101cfe196fbSEmmanuel Vadot.Pp
102cfe196fbSEmmanuel VadotThe device driver for eeprom@20000 needs to expose itself as a provider
103cfe196fbSEmmanuel Vadot.Bd -literal
104cfe196fbSEmmanuel Vadot#include "nvmem_if.h"
105cfe196fbSEmmanuel Vadot
106cfe196fbSEmmanuel Vadotint
107cfe196fbSEmmanuel Vadotfoo_nvmem_read(device_t dev, uint32_t offset, uint32_t size, uint8_t *buffer)
108cfe196fbSEmmanuel Vadot{
109cfe196fbSEmmanuel Vadot	/* Read the data */
110cfe196fbSEmmanuel Vadot}
111cfe196fbSEmmanuel Vadot
112cfe196fbSEmmanuel Vadotint
113cfe196fbSEmmanuel Vadotfoo_attach(device_t dev)
114cfe196fbSEmmanuel Vadot{
115cfe196fbSEmmanuel Vadot	phandle_t node;
116cfe196fbSEmmanuel Vadot
117cfe196fbSEmmanuel Vadot	node = ofw_bus_get_node(dev);
118cfe196fbSEmmanuel Vadot	...
119cfe196fbSEmmanuel Vadot	/* Registering the device so the consumers can find us */
120cfe196fbSEmmanuel Vadot	OF_device_register_xref(OF_xref_from_node(node), dev);
121cfe196fbSEmmanuel Vadot
122cfe196fbSEmmanuel Vadot	...
123cfe196fbSEmmanuel Vadot}
124cfe196fbSEmmanuel Vadot
125cfe196fbSEmmanuel Vadotstatic device_method_t foo_methods[] = {
126cfe196fbSEmmanuel Vadot	...
127cfe196fbSEmmanuel Vadot
128cfe196fbSEmmanuel Vadot	/* nvmem interface */
129cfe196fbSEmmanuel Vadot	DEVMETHOD(nvmem_read, foo_nvmem_read),
130cfe196fbSEmmanuel Vadot
131cfe196fbSEmmanuel Vadot	/* Terminate method list */
132cfe196fbSEmmanuel Vadot	DEVMETHOD_END
133cfe196fbSEmmanuel Vadot};
134cfe196fbSEmmanuel Vadot.Ed
135cfe196fbSEmmanuel Vadot.Pp
136cfe196fbSEmmanuel VadotThe consumer device driver for device@30000 can now read the nvmem data
137cfe196fbSEmmanuel Vadot.Bd -literal
138cfe196fbSEmmanuel Vadotint
139cfe196fbSEmmanuel Vadotbar_attach(device_t dev)
140cfe196fbSEmmanuel Vadot{
141cfe196fbSEmmanuel Vadot	phandle_t node;
142cfe196fbSEmmanuel Vadot	uint32_t boardid;
143cfe196fbSEmmanuel Vadot
144cfe196fbSEmmanuel Vadot	...
145cfe196fbSEmmanuel Vadot	node = ofw_bus_get_node(dev);
146cfe196fbSEmmanuel Vadot	nvmem_read_cell_by_name(node, "boardid", (void *)&boardid, sizeof(boardid));
147cfe196fbSEmmanuel Vadot	...
148cfe196fbSEmmanuel Vadot}
149cfe196fbSEmmanuel Vadot.Ed
150cfe196fbSEmmanuel Vadot.Sh HISTORY
151cfe196fbSEmmanuel VadotThe nvmem related function first appear in
152cfe196fbSEmmanuel Vadot.Fx 12.0 .
153cfe196fbSEmmanuel VadotThe nvmem interface and manual page was written by
154cfe196fbSEmmanuel Vadot.An Emmanuel Vadot Aq Mt manu@FreeBSD.org .
155