blob: 743f355465989ecf25f075aebaafe163ba0e173f [file] [log] [blame]
Mario Six2161e552018-07-31 11:44:11 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7/*
8 * This uclass encapsulates hardware methods to gather information about a
Simon Glass458b66a2020-11-05 06:32:05 -07009 * sysinfo or a specific device such as hard-wired GPIOs on GPIO expanders,
Mario Six2161e552018-07-31 11:44:11 +020010 * read-only data in flash ICs, or similar.
11 *
12 * The interface offers functions to read the usual standard data types (bool,
13 * int, string) from the device, each of which is identified by a static
14 * numeric ID (which will usually be defined as a enum in a header file).
15 *
Simon Glass458b66a2020-11-05 06:32:05 -070016 * If for example the sysinfo had a read-only serial number flash IC, we could
Mario Six2161e552018-07-31 11:44:11 +020017 * call
18 *
Simon Glass458b66a2020-11-05 06:32:05 -070019 * ret = sysinfo_detect(dev);
Mario Six2161e552018-07-31 11:44:11 +020020 * if (ret) {
Simon Glass458b66a2020-11-05 06:32:05 -070021 * debug("sysinfo device not found.");
Mario Six2161e552018-07-31 11:44:11 +020022 * return ret;
23 * }
24 *
Simon Glass458b66a2020-11-05 06:32:05 -070025 * ret = sysinfo_get_int(dev, ID_SERIAL_NUMBER, &serial);
Mario Six2161e552018-07-31 11:44:11 +020026 * if (ret) {
27 * debug("Error when reading serial number from device.");
28 * return ret;
29 * }
30 *
31 * to read the serial number.
32 */
33
Simon Glasscf67d6d2021-02-04 21:17:23 -070034/** enum sysinfo_id - Standard IDs defined by U-Boot */
35enum sysinfo_id {
36 SYSINFO_ID_NONE,
37
38 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
39 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
40
41 /* First value available for downstream/board used */
42 SYSINFO_ID_USER = 0x1000,
43};
44
Simon Glass458b66a2020-11-05 06:32:05 -070045struct sysinfo_ops {
Mario Six2161e552018-07-31 11:44:11 +020046 /**
47 * detect() - Run the hardware info detection procedure for this
48 * device.
49 * @dev: The device containing the information
50 *
51 * This operation might take a long time (e.g. read from EEPROM,
52 * check the presence of a device on a bus etc.), hence this is not
53 * done in the probe() method, but later during operation in this
54 * dedicated method.
55 *
56 * Return: 0 if OK, -ve on error.
57 */
58 int (*detect)(struct udevice *dev);
59
60 /**
61 * get_bool() - Read a specific bool data value that describes the
62 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -070063 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +020064 * @id: A unique identifier for the bool value to be read.
65 * @val: Pointer to a buffer that receives the value read.
66 *
67 * Return: 0 if OK, -ve on error.
68 */
69 int (*get_bool)(struct udevice *dev, int id, bool *val);
70
71 /**
72 * get_int() - Read a specific int data value that describes the
73 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -070074 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +020075 * @id: A unique identifier for the int value to be read.
76 * @val: Pointer to a buffer that receives the value read.
77 *
78 * Return: 0 if OK, -ve on error.
79 */
80 int (*get_int)(struct udevice *dev, int id, int *val);
81
82 /**
83 * get_str() - Read a specific string data value that describes the
84 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -070085 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +020086 * @id: A unique identifier for the string value to be read.
87 * @size: The size of the buffer to receive the string data.
88 * @val: Pointer to a buffer that receives the value read.
89 *
90 * Return: 0 if OK, -ve on error.
91 */
92 int (*get_str)(struct udevice *dev, int id, size_t size, char *val);
Jean-Jacques Hiblot7815fc12019-10-22 16:39:19 +020093
94 /**
95 * get_fit_loadable - Get the name of an image to load from FIT
96 * This function can be used to provide the image names based on runtime
97 * detection. A classic use-case would when DTBOs are used to describe
98 * additionnal daughter cards.
99 *
Simon Glass458b66a2020-11-05 06:32:05 -0700100 * @dev: The sysinfo instance to gather the data.
Jean-Jacques Hiblot7815fc12019-10-22 16:39:19 +0200101 * @index: Index of the image. Starts at 0 and gets incremented
102 * after each call to this function.
103 * @type: The type of image. For example, "fdt" for DTBs
104 * @strp: A pointer to string. Untouched if the function fails
105 *
106 * Return: 0 if OK, -ENOENT if no loadable is available else -ve on
107 * error.
108 */
109 int (*get_fit_loadable)(struct udevice *dev, int index,
110 const char *type, const char **strp);
Mario Six2161e552018-07-31 11:44:11 +0200111};
112
Simon Glass458b66a2020-11-05 06:32:05 -0700113#define sysinfo_get_ops(dev) ((struct sysinfo_ops *)(dev)->driver->ops)
Mario Six2161e552018-07-31 11:44:11 +0200114
Simon Glass10e6f792021-02-04 21:17:21 -0700115#if CONFIG_IS_ENABLED(SYSINFO)
Mario Six2161e552018-07-31 11:44:11 +0200116/**
Simon Glass458b66a2020-11-05 06:32:05 -0700117 * sysinfo_detect() - Run the hardware info detection procedure for this device.
Mario Six2161e552018-07-31 11:44:11 +0200118 *
119 * @dev: The device containing the information
120 *
121 * Return: 0 if OK, -ve on error.
122 */
Simon Glass458b66a2020-11-05 06:32:05 -0700123int sysinfo_detect(struct udevice *dev);
Mario Six2161e552018-07-31 11:44:11 +0200124
125/**
Simon Glass458b66a2020-11-05 06:32:05 -0700126 * sysinfo_get_bool() - Read a specific bool data value that describes the
Mario Six2161e552018-07-31 11:44:11 +0200127 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -0700128 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +0200129 * @id: A unique identifier for the bool value to be read.
130 * @val: Pointer to a buffer that receives the value read.
131 *
132 * Return: 0 if OK, -ve on error.
133 */
Simon Glass458b66a2020-11-05 06:32:05 -0700134int sysinfo_get_bool(struct udevice *dev, int id, bool *val);
Mario Six2161e552018-07-31 11:44:11 +0200135
136/**
Simon Glass458b66a2020-11-05 06:32:05 -0700137 * sysinfo_get_int() - Read a specific int data value that describes the
Mario Six2161e552018-07-31 11:44:11 +0200138 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -0700139 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +0200140 * @id: A unique identifier for the int value to be read.
141 * @val: Pointer to a buffer that receives the value read.
142 *
143 * Return: 0 if OK, -ve on error.
144 */
Simon Glass458b66a2020-11-05 06:32:05 -0700145int sysinfo_get_int(struct udevice *dev, int id, int *val);
Mario Six2161e552018-07-31 11:44:11 +0200146
147/**
Simon Glass458b66a2020-11-05 06:32:05 -0700148 * sysinfo_get_str() - Read a specific string data value that describes the
Mario Six2161e552018-07-31 11:44:11 +0200149 * hardware setup.
Simon Glass458b66a2020-11-05 06:32:05 -0700150 * @dev: The sysinfo instance to gather the data.
Mario Six2161e552018-07-31 11:44:11 +0200151 * @id: A unique identifier for the string value to be read.
152 * @size: The size of the buffer to receive the string data.
153 * @val: Pointer to a buffer that receives the value read.
154 *
155 * Return: 0 if OK, -ve on error.
156 */
Simon Glass458b66a2020-11-05 06:32:05 -0700157int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val);
Mario Six2161e552018-07-31 11:44:11 +0200158
159/**
Simon Glass458b66a2020-11-05 06:32:05 -0700160 * sysinfo_get() - Return the sysinfo device for the sysinfo in question.
161 * @devp: Pointer to structure to receive the sysinfo device.
Mario Six2161e552018-07-31 11:44:11 +0200162 *
Simon Glass458b66a2020-11-05 06:32:05 -0700163 * Since there can only be at most one sysinfo instance, the API can supply a
Mario Six2161e552018-07-31 11:44:11 +0200164 * function that returns the unique device. This is especially useful for use
Simon Glass458b66a2020-11-05 06:32:05 -0700165 * in sysinfo files.
Mario Six2161e552018-07-31 11:44:11 +0200166 *
167 * Return: 0 if OK, -ve on error.
168 */
Simon Glass458b66a2020-11-05 06:32:05 -0700169int sysinfo_get(struct udevice **devp);
Jean-Jacques Hiblot7815fc12019-10-22 16:39:19 +0200170
171/**
Simon Glass458b66a2020-11-05 06:32:05 -0700172 * sysinfo_get_fit_loadable - Get the name of an image to load from FIT
Jean-Jacques Hiblot7815fc12019-10-22 16:39:19 +0200173 * This function can be used to provide the image names based on runtime
174 * detection. A classic use-case would when DTBOs are used to describe
175 * additionnal daughter cards.
176 *
Simon Glass458b66a2020-11-05 06:32:05 -0700177 * @dev: The sysinfo instance to gather the data.
Jean-Jacques Hiblot7815fc12019-10-22 16:39:19 +0200178 * @index: Index of the image. Starts at 0 and gets incremented
179 * after each call to this function.
180 * @type: The type of image. For example, "fdt" for DTBs
181 * @strp: A pointer to string. Untouched if the function fails
182 *
183 *
184 * Return: 0 if OK, -ENOENT if no loadable is available else -ve on
185 * error.
186 */
Simon Glass458b66a2020-11-05 06:32:05 -0700187int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
188 const char **strp);
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200189
190#else
191
Simon Glass458b66a2020-11-05 06:32:05 -0700192static inline int sysinfo_detect(struct udevice *dev)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200193{
194 return -ENOSYS;
195}
196
Simon Glass458b66a2020-11-05 06:32:05 -0700197static inline int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200198{
199 return -ENOSYS;
200}
201
Simon Glass458b66a2020-11-05 06:32:05 -0700202static inline int sysinfo_get_int(struct udevice *dev, int id, int *val)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200203{
204 return -ENOSYS;
205}
206
Simon Glass458b66a2020-11-05 06:32:05 -0700207static inline int sysinfo_get_str(struct udevice *dev, int id, size_t size,
208 char *val)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200209{
210 return -ENOSYS;
211}
212
Simon Glass458b66a2020-11-05 06:32:05 -0700213static inline int sysinfo_get(struct udevice **devp)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200214{
215 return -ENOSYS;
216}
217
Simon Glass458b66a2020-11-05 06:32:05 -0700218static inline int sysinfo_get_fit_loadable(struct udevice *dev, int index,
219 const char *type, const char **strp)
Jean-Jacques Hiblot98823aa2019-10-22 16:39:20 +0200220{
221 return -ENOSYS;
222}
223
224#endif