blob: bc18b2ebb7b0fc875a299b9e2e5c64c3d6a8e2b3 [file] [log] [blame]
Tom Rinia023c192022-07-14 08:07:40 -04001.. SPDX-License-Identifier: GPL-2.0+:
2
3U-Boot Coding Style
4===================
5
6The following Coding Style requirements shall be mandatory for all code contributed to
7the U-Boot project.
8
9Exceptions are only allowed if code from other projects is integrated with no
10or only minimal changes.
11
12The following rules apply:
13
14* All contributions to U-Boot should conform to the `Linux kernel
15 coding style <https://www.kernel.org/doc/html/latest/process/coding-style.html>`_
16 and the `Lindent script <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/Lindent>`_.
17 * The exception for net files to the `multi-line comment
18 <https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting>`_
19 applies only to Linux, not to U-Boot. Only large hunks which are copied
20 unchanged from Linux may retain that comment format.
21
Heinrich Schuchardtb7f35092023-04-19 08:45:34 +020022* Python code shall conform to `PEP8 (Style Guide for Python Code)
23 <https://peps.python.org/pep-0008/>`_. Use `pylint
24 <https://github.com/pylint-dev/pylint>`_ for checking the code.
25
Tom Rinia023c192022-07-14 08:07:40 -040026* Use patman to send your patches (``tools/patman/patman -H`` for full
27 instructions). With a few tags in your commits this will check your patches
28 and take care of emailing them.
29
30* If you don't use patman, make sure to run ``scripts/checkpatch.pl``. For
31 more information, read :doc:`checkpatch`. Note that this should be done
32 *before* posting on the mailing list!
33
34* Source files originating from different projects (for example the MTD
35 subsystem or the hush shell code from the BusyBox project) may, after
36 careful consideration, be exempted from these rules. For such files, the
37 original coding style may be kept to ease subsequent migration to newer
38 versions of those sources.
39
Tom Rinia023c192022-07-14 08:07:40 -040040* Please also stick to the following formatting rules:
41
42 * Remove any trailing white space
43
44 * Use TAB characters for indentation and vertical alignment, not spaces
45
46 * The exception here is Python which requires 4 spaces instead.
47
Heinrich Schuchardtf910cbf2022-07-16 06:19:08 +020048 * All source files need to be in "Unix" and not "DOS" or "Windows" format,
Tom Rinia023c192022-07-14 08:07:40 -040049 with respect to line ends.
50
51 * Do not add more than 2 consecutive empty lines to source files
52
53 * Do not add trailing empty lines to source files
54
55 * Using the option ``git config --global color.diff auto`` will help to
56 visually see whitespace problems in ``diff`` output from ``git``.
57
58 * In Emacs one can use ``=M-x whitespace-global-mode=`` to get visual
59 feedback on the nasty details. ``=M-x whitespace-cleanup=`` does The Right
60 Thing (tm)
61
62Submissions of new code or patches that do not conform to these requirements
63shall be rejected with a request to reformat the changes.
64
65U-Boot Code Documentation
66-------------------------
67
68U-Boot adopted the kernel-doc annotation style, this is the only exception from
69multi-line comment rule of Coding Style. While not mandatory, adding
70documentation is strongly advised. The Linux kernel `kernel-doc
71<https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html>`_
72documentation applies with no changes.
73
Heinrich Schuchardtb7f35092023-04-19 08:45:34 +020074Our Python code documentation follows `PEP257 (Docstring Conventions)
75<https://peps.python.org/pep-0257/>`_.
76
Tom Rinia023c192022-07-14 08:07:40 -040077Use structures for I/O access
78-----------------------------
79
80U-Boot typically uses a C structure to map out the registers in an I/O region,
81rather than offsets. The reasons for this are:
82
83* It dissociates the register location (offset) from the register type, which
84 means the developer has to make sure the type is right for each access,
85 whereas with the struct method, this is checked by the compiler;
86
87* It avoids actually writing all offsets, which is (more) error-prone;
88
89* It allows for better compile time sanity-checking of values we write to registers.
90
91Some reasons why you might not use C structures:
92
93* Where the registers appear at different offsets in different hardware
94 revisions supported by the same driver
95
96* Where the driver only uses a small subset of registers and it is not worth
97 defining a struct to cover them all, with large empty regions
98
99* Where the offset of a register might be hard to figure out when buried a long
100 way down a structure, possibly with embedded sub-structures
101
102* This may need to change to the kernel model if we allow for more run-time
103 detection of what drivers are appropriate for what we're running on.
104
105Please use the check_member() macro to verify that your structure is the
106expected size, or that particular members appear at the right offset.
107
108Include files
109-------------
110
Tom Rini533f58b2024-02-09 09:35:20 -0500111You should follow this ordering in U-Boot. In all cases, they should be listed
112in alphabetical order. First comes headers which are located directly in our
Tom Rinidec7ea02024-05-20 13:35:03 -0600113top-level include diretory. Second are headers within subdirectories, Finally
114directory-local includes should be listed. See this example:
Tom Rinia023c192022-07-14 08:07:40 -0400115
116.. code-block:: C
117
Tom Rinia023c192022-07-14 08:07:40 -0400118 #include <bootstage.h>
119 #include <dm.h>
120 #include <others.h>
121 #include <asm/...>
Tom Rini533f58b2024-02-09 09:35:20 -0500122 #include <asm/arch/...>
Heinrich Schuchardt216a25d2023-07-24 10:53:59 +0200123 #include <dm/device_compat.h>
Tom Rinia023c192022-07-14 08:07:40 -0400124 #include <linux/...>
125 #include "local.h"
126
Tom Rinia023c192022-07-14 08:07:40 -0400127For files that need to be compiled for the host (e.g. tools), you need to use
Tom Rini533f58b2024-02-09 09:35:20 -0500128``#ifndef USE_HOSTCC`` to avoid including U-Boot specific include files. See
129common/image.c for an example.
130
Tom Rinia023c192022-07-14 08:07:40 -0400131If your file uses driver model, include <dm.h> in the C file. Do not include
132dm.h in a header file. Try to use forward declarations (e.g. ``struct
133udevice``) instead.
134
135Filenames
136---------
137
138For .c and .h files try to use underscore rather than hyphen unless you want
139the file to stand out (e.g. driver-model uclasses should be named xxx-uclass.h.
140Avoid upper case and keep the names fairly short.
141
142Function and struct comments
143----------------------------
144
145Non-trivial functions should have a comment which describes what they do. If it
146is an exported function, put the comment in the header file so the API is in
147one place. If it is a static function, put it in the C file.
148
149If the function returns errors, mention that and list the different errors that
150are returned. If it is merely passing errors back from a function it calls,
151then you can skip that.
152
153See `here
154<https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html#function-documentation>`_
155for style.
156
Tom Rinie245f372025-04-01 16:55:24 -0600157Conditional Compilation
158-----------------------
159
160Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
161files; doing so makes code harder to read and logic harder to follow. Instead,
162use such conditionals in a header file defining functions for use in those .c
163files, providing no-op stub versions in the #else case, and then call those
164functions unconditionally from .c files. The compiler will avoid generating
165any code for the stub calls, producing identical results, but the logic will
166remain easy to follow.
167
168Prefer to compile out entire functions, rather than portions of functions or
169portions of expressions. Rather than putting an ifdef in an expression, factor
170out part or all of the expression into a separate helper function and apply the
171conditional to that function.
172
173If you have a function or variable which may potentially go unused in a
174particular configuration, and the compiler would warn about its definition
175going unused, mark the definition as __maybe_unused rather than wrapping it in
176a preprocessor conditional. (However, if a function or variable *always* goes
177unused, delete it.)
178
179Within code, where possible, use the IS_ENABLED macro to convert a Kconfig
180symbol into a C boolean expression, and use it in a normal C conditional:
181
182.. code-block:: c
183
184 if (IS_ENABLED(CONFIG_SOMETHING)) {
185 ...
186 }
187
188The compiler will constant-fold the conditional away, and include or exclude
189the block of code just as with an #ifdef, so this will not add any runtime
190overhead. However, this approach still allows the C compiler to see the code
191inside the block, and check it for correctness (syntax, types, symbol
192references, etc). Thus, you still have to use an #ifdef if the code inside the
193block references symbols that will not exist if the condition is not met.
194
Tom Rinicca5ba92025-04-01 16:55:25 -0600195When working with xPL (see :doc:`spl` for more information) we need to take
196further care to use the right macro. In the case where a symbol may be
197referenced with an xPL-specific Kconfig symbol, use the CONFIG_IS_ENABLED macro
198instead, in a similar manner:
199
200.. code-block:: c
201
202 if (CONIG_IS_ENABLED(SOMETHING)) {
203 ...
204 }
205
206When dealing with a Kconfig symbol that has both a normal name and one or more
207xPL-prefixed names, the Makefile needs special consideration as well. The
208PHASE\_ macro helps us in this situation thusly:
209
210.. code-block:: make
211
212 obj-$(CONFIG_$(PHASE_)SOMETHING) += something.o
213
Tom Rinie245f372025-04-01 16:55:24 -0600214At the end of any non-trivial #if or #ifdef block (more than a few lines),
215place a comment after the #endif on the same line, noting the conditional
216expression used. For instance:
217
218.. code-block:: c
219
220 #ifdef CONFIG_SOMETHING
221 ...
222 #endif /* CONFIG_SOMETHING */
223
Tom Rinia023c192022-07-14 08:07:40 -0400224Driver model
225------------
226
227When declaring a device, try to use ``struct udevice *dev``, i.e. ``dev`` as the name:
228
229.. code-block:: C
230
231 struct udevice *dev;
232
233Use ``ret`` as the return value:
234
235.. code-block:: C
236
237 struct udevice *dev;
238 int ret;
239
240 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &dev);
241 if (ret)
242 return log_msg_ret("pmc", dev);
243
244Consider using log_ret() or log_msg_ret() to return a value (see above).
245
246Add a ``p`` suffix on return arguments:
247
248.. code-block:: C
249
250 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
251 {
252 ...
253 *devp = dev;
254
255 return 0;
256 }
257
258There are standard variable names that you should use in drivers:
259
260* ``struct xxx_priv`` and ``priv`` for dev_get_priv()
261
262* ``struct xxx_plat`` and ``plat`` for dev_get_platdata()
263
264For example:
265
266.. code-block:: C
267
268 struct simple_bus_plat {
269 u32 base;
270 u32 size;
271 u32 target;
272 };
273
274 /* Davinci MMC board definitions */
275 struct davinci_mmc_priv {
276 struct davinci_mmc_regs *reg_base; /* Register base address */
277 uint input_clk; /* Input clock to MMC controller */
278 struct gpio_desc cd_gpio; /* Card Detect GPIO */
279 struct gpio_desc wp_gpio; /* Write Protect GPIO */
280 };
281
282 struct rcar_gpio_priv *priv = dev_get_priv(dev);
283
284 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
285
286Other
287-----
288
289Some minor things:
290
291* Put a blank line before the last ``return`` in a function unless it is the only line:
292
293.. code-block:: C
294
295 struct udevice *pci_get_controller(struct udevice *dev)
296 {
297 while (device_is_on_pci_bus(dev))
298 dev = dev->parent;
299
300 return dev;
301 }
302
303Tests
304-----
305
306Please add tests when you add code. Please change or expand tests when you change code.
307
308Run the tests with::
309
310 make check
311 make qcheck (skips some tests)
312
313Python tests are in test/py/tests - see the docs in test/py for info.
314
315Try to write your tests in C if you can. For example, tests to check a command
316will be much faster (10-100x or more) if they can directly call run_command()
317and ut_check_console_line() instead of using Python to send commands over a
318pipe to U-Boot.
319
Simon Glass29ea2aa2022-08-07 07:24:52 -0600320Tests run all supported CI systems (GitLab, Azure) using scripts in the root of
321the U-Boot tree.