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