xref: /freebsd/share/man/man9/nvmem.9 (revision 320686674b3c8ce3ffe135c421a22d063917ee46)
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.\" $FreeBSD$
24cfe196fbSEmmanuel Vadot.\"
25cfe196fbSEmmanuel Vadot.Dd July 24, 2018
26cfe196fbSEmmanuel Vadot.Dt nvmem 9
27cfe196fbSEmmanuel Vadot.Os
28cfe196fbSEmmanuel Vadot.Sh NAME
29*32068667SChristian Brueffer.Nm nvmem ,
30cfe196fbSEmmanuel Vadot.Nm nvmem_get_cell_len ,
31cfe196fbSEmmanuel Vadot.Nm nvmem_read_cell_by_name ,
32cfe196fbSEmmanuel Vadot.Nm nvmem_read_cell_by_idx ,
33cfe196fbSEmmanuel Vadot.Nm nvmem_write_cell_by_name ,
34*32068667SChristian Brueffer.Nm nvmem_write_cell_by_idx
35cfe196fbSEmmanuel Vadot.Sh SYNOPSIS
36cfe196fbSEmmanuel Vadot.Cd "options FDT"
37cfe196fbSEmmanuel Vadot.Cd "device nvmem"
38cfe196fbSEmmanuel Vadot.In sys/extres/nvmem/nvmem.h
39cfe196fbSEmmanuel Vadot.Ft int
40cfe196fbSEmmanuel Vadot.Fn nvmem_get_cell_len "phandle_t node" "const char *name"
41cfe196fbSEmmanuel Vadot.Ft int
42cfe196fbSEmmanuel Vadot.Fn nvmem_read_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
43cfe196fbSEmmanuel Vadot.Ft int
44cfe196fbSEmmanuel Vadot.Fn nvmem_read_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
45cfe196fbSEmmanuel Vadot.Ft int
46cfe196fbSEmmanuel Vadot.Fn nvmem_write_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
47cfe196fbSEmmanuel Vadot.Ft int
48cfe196fbSEmmanuel Vadot.Fn nvmem_write_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
49cfe196fbSEmmanuel Vadot.Sh DESCRIPTION
50cfe196fbSEmmanuel VadotOn some embedded boards, the manufacturer stored some data on a NVMEM
51cfe196fbSEmmanuel Vadot(Non-Volatile Memory), this is generally stored in some eeprom or fuses.
52cfe196fbSEmmanuel Vadot.Pp
53cfe196fbSEmmanuel VadotThe
54cfe196fbSEmmanuel Vadot.Nm
55cfe196fbSEmmanuel VadotAPI consist of helpers functions for consumer and device methods for
56cfe196fbSEmmanuel Vadotproviders.
57cfe196fbSEmmanuel Vadot.Sh FUNCTIONS
58cfe196fbSEmmanuel Vadot.Bl -tag -width indent
59cfe196fbSEmmanuel Vadot.It Fn nvmem_get_cell_len  "phandle_t node" "const char *name"
60cfe196fbSEmmanuel VadotGet the size of the cell base on the reg property on the node.
61cfe196fbSEmmanuel VadotReturn the size or ENOENT if the cell name wasn't found
62cfe196fbSEmmanuel Vadot.It Fn nvmem_read_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
63cfe196fbSEmmanuel VadotGet the cell content based on the name.
64cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
65cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
66cfe196fbSEmmanuel Vadot.It Fn nvmem_read_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
67cfe196fbSEmmanuel VadotGet the cell content based on the id.
68cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
69cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
70cfe196fbSEmmanuel Vadot.It Fn nvmem_write_cell_by_name "phandle_t node" "const char *name" "void *cell" "size_t buflen"
71cfe196fbSEmmanuel VadotWrite the cell content based on the name.
72cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
73cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
74cfe196fbSEmmanuel Vadot.It Fn nvmem_write_cell_by_idx "phandle_t node" "int idx" "void *cell" "size_t buflen"
75cfe196fbSEmmanuel VadotWrite the cell content based on the id.
76cfe196fbSEmmanuel VadotReturn 0 on sucess or ENOENT if the cell doesn't exists, ENXIO if no provider device was found,
77cfe196fbSEmmanuel VadotEINVAL if the size isn't correct.
78cfe196fbSEmmanuel Vadot.El
79cfe196fbSEmmanuel Vadot.Sh DEVICE METHODS
80cfe196fbSEmmanuel Vadot.Bl -tag -width indent
81cfe196fbSEmmanuel Vadot.It Fn nvmem_read "device_t dev" "uint32_t offset" "uint32_t size" "uint8_t *buffer"
82cfe196fbSEmmanuel VadotProvider device method to read a cell content.
83cfe196fbSEmmanuel Vadot.It Fn nvmem_write "device_t dev" "uint32_t offset" "uint32_t size" "uint8_t *buffer"
84cfe196fbSEmmanuel VadotProvider device method to write a cell content.
85cfe196fbSEmmanuel Vadot.El
86cfe196fbSEmmanuel Vadot.Sh EXAMPLES
87cfe196fbSEmmanuel VadotConsider this DTS
88cfe196fbSEmmanuel Vadot.Bd -literal
89cfe196fbSEmmanuel Vadot/* Provider */
90cfe196fbSEmmanuel Vadoteeprom: eeprom@20000 {
91cfe196fbSEmmanuel Vadot	board_id: id@0 {
92cfe196fbSEmmanuel Vadot		reg = <0x0 0x4>;
93cfe196fbSEmmanuel Vadot	};
94cfe196fbSEmmanuel Vadot};
95cfe196fbSEmmanuel Vadot/* Consumer */
96cfe196fbSEmmanuel Vadotdevice@30000 {
97cfe196fbSEmmanuel Vadot	...
98cfe196fbSEmmanuel Vadot
99cfe196fbSEmmanuel Vadot	nvmem-cells = <&board_id>
100cfe196fbSEmmanuel Vadot	nvmem-cell-names = "boardid";
101cfe196fbSEmmanuel Vadot};
102cfe196fbSEmmanuel Vadot.Ed
103cfe196fbSEmmanuel Vadot.Pp
104cfe196fbSEmmanuel VadotThe device driver for eeprom@20000 needs to expose itself as a provider
105cfe196fbSEmmanuel Vadot.Bd -literal
106cfe196fbSEmmanuel Vadot#include "nvmem_if.h"
107cfe196fbSEmmanuel Vadot
108cfe196fbSEmmanuel Vadotint
109cfe196fbSEmmanuel Vadotfoo_nvmem_read(device_t dev, uint32_t offset, uint32_t size, uint8_t *buffer)
110cfe196fbSEmmanuel Vadot{
111cfe196fbSEmmanuel Vadot	/* Read the data */
112cfe196fbSEmmanuel Vadot}
113cfe196fbSEmmanuel Vadot
114cfe196fbSEmmanuel Vadotint
115cfe196fbSEmmanuel Vadotfoo_attach(device_t dev)
116cfe196fbSEmmanuel Vadot{
117cfe196fbSEmmanuel Vadot	phandle_t node;
118cfe196fbSEmmanuel Vadot
119cfe196fbSEmmanuel Vadot	node = ofw_bus_get_node(dev);
120cfe196fbSEmmanuel Vadot	...
121cfe196fbSEmmanuel Vadot	/* Registering the device so the consumers can find us */
122cfe196fbSEmmanuel Vadot	OF_device_register_xref(OF_xref_from_node(node), dev);
123cfe196fbSEmmanuel Vadot
124cfe196fbSEmmanuel Vadot	...
125cfe196fbSEmmanuel Vadot}
126cfe196fbSEmmanuel Vadot
127cfe196fbSEmmanuel Vadotstatic device_method_t foo_methods[] = {
128cfe196fbSEmmanuel Vadot	...
129cfe196fbSEmmanuel Vadot
130cfe196fbSEmmanuel Vadot	/* nvmem interface */
131cfe196fbSEmmanuel Vadot	DEVMETHOD(nvmem_read, foo_nvmem_read),
132cfe196fbSEmmanuel Vadot
133cfe196fbSEmmanuel Vadot	/* Terminate method list */
134cfe196fbSEmmanuel Vadot	DEVMETHOD_END
135cfe196fbSEmmanuel Vadot};
136cfe196fbSEmmanuel Vadot.Ed
137cfe196fbSEmmanuel Vadot.Pp
138cfe196fbSEmmanuel VadotThe consumer device driver for device@30000 can now read the nvmem data
139cfe196fbSEmmanuel Vadot.Bd -literal
140cfe196fbSEmmanuel Vadotint
141cfe196fbSEmmanuel Vadotbar_attach(device_t dev)
142cfe196fbSEmmanuel Vadot{
143cfe196fbSEmmanuel Vadot	phandle_t node;
144cfe196fbSEmmanuel Vadot	uint32_t boardid;
145cfe196fbSEmmanuel Vadot
146cfe196fbSEmmanuel Vadot	...
147cfe196fbSEmmanuel Vadot	node = ofw_bus_get_node(dev);
148cfe196fbSEmmanuel Vadot	nvmem_read_cell_by_name(node, "boardid", (void *)&boardid, sizeof(boardid));
149cfe196fbSEmmanuel Vadot	...
150cfe196fbSEmmanuel Vadot}
151cfe196fbSEmmanuel Vadot.Ed
152cfe196fbSEmmanuel Vadot.Sh HISTORY
153cfe196fbSEmmanuel VadotThe nvmem related function first appear in
154cfe196fbSEmmanuel Vadot.Fx 12.0 .
155cfe196fbSEmmanuel VadotThe nvmem interface and manual page was written by
156cfe196fbSEmmanuel Vadot.An Emmanuel Vadot Aq Mt manu@FreeBSD.org .
157