blob: 1041d1c8d94642754efe39964fd7d853f74d4a20 [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
22* Use patman to send your patches (``tools/patman/patman -H`` for full
23 instructions). With a few tags in your commits this will check your patches
24 and take care of emailing them.
25
26* If you don't use patman, make sure to run ``scripts/checkpatch.pl``. For
27 more information, read :doc:`checkpatch`. Note that this should be done
28 *before* posting on the mailing list!
29
30* Source files originating from different projects (for example the MTD
31 subsystem or the hush shell code from the BusyBox project) may, after
32 careful consideration, be exempted from these rules. For such files, the
33 original coding style may be kept to ease subsequent migration to newer
34 versions of those sources.
35
36* Please note that U-Boot is implemented in C (and to some small parts in
37 Assembler); no C++ is used, so please do not use C++ style comments (//) in
38 your code.
39
40 * The sole exception here is for SPDX tags in some files (checkpatch.pl will warn you).
41
42* Please also stick to the following formatting rules:
43
44 * Remove any trailing white space
45
46 * Use TAB characters for indentation and vertical alignment, not spaces
47
48 * The exception here is Python which requires 4 spaces instead.
49
50 * All source files need to be in "Unix" and not "DOS" or "Windows" formatted,
51 with respect to line ends.
52
53 * Do not add more than 2 consecutive empty lines to source files
54
55 * Do not add trailing empty lines to source files
56
57 * Using the option ``git config --global color.diff auto`` will help to
58 visually see whitespace problems in ``diff`` output from ``git``.
59
60 * In Emacs one can use ``=M-x whitespace-global-mode=`` to get visual
61 feedback on the nasty details. ``=M-x whitespace-cleanup=`` does The Right
62 Thing (tm)
63
64Submissions of new code or patches that do not conform to these requirements
65shall be rejected with a request to reformat the changes.
66
67U-Boot Code Documentation
68-------------------------
69
70U-Boot adopted the kernel-doc annotation style, this is the only exception from
71multi-line comment rule of Coding Style. While not mandatory, adding
72documentation is strongly advised. The Linux kernel `kernel-doc
73<https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html>`_
74documentation applies with no changes.
75
76Use structures for I/O access
77-----------------------------
78
79U-Boot typically uses a C structure to map out the registers in an I/O region,
80rather than offsets. The reasons for this are:
81
82* It dissociates the register location (offset) from the register type, which
83 means the developer has to make sure the type is right for each access,
84 whereas with the struct method, this is checked by the compiler;
85
86* It avoids actually writing all offsets, which is (more) error-prone;
87
88* It allows for better compile time sanity-checking of values we write to registers.
89
90Some reasons why you might not use C structures:
91
92* Where the registers appear at different offsets in different hardware
93 revisions supported by the same driver
94
95* Where the driver only uses a small subset of registers and it is not worth
96 defining a struct to cover them all, with large empty regions
97
98* Where the offset of a register might be hard to figure out when buried a long
99 way down a structure, possibly with embedded sub-structures
100
101* This may need to change to the kernel model if we allow for more run-time
102 detection of what drivers are appropriate for what we're running on.
103
104Please use the check_member() macro to verify that your structure is the
105expected size, or that particular members appear at the right offset.
106
107Include files
108-------------
109
110You should follow this ordering in U-Boot. The common.h header (which is going
111away at some point) should always be first, followed by other headers in order,
112then headers with directories, then local files:
113
114.. code-block:: C
115
116 #include <common.h>
117 #include <bootstage.h>
118 #include <dm.h>
119 #include <others.h>
120 #include <asm/...>
121 #include <arm/arch/...>
122 #include <dm/device_compat/.h>
123 #include <linux/...>
124 #include "local.h"
125
126Within that order, sort your includes.
127
128It is important to include common.h first since it provides basic features used
129by most files, e.g. CONFIG options.
130
131For files that need to be compiled for the host (e.g. tools), you need to use
132``#ifndef USE_HOSTCC`` to avoid including common.h since it includes a lot of
133internal U-Boot things. See common/image.c for an example.
134
135If your file uses driver model, include <dm.h> in the C file. Do not include
136dm.h in a header file. Try to use forward declarations (e.g. ``struct
137udevice``) instead.
138
139Filenames
140---------
141
142For .c and .h files try to use underscore rather than hyphen unless you want
143the file to stand out (e.g. driver-model uclasses should be named xxx-uclass.h.
144Avoid upper case and keep the names fairly short.
145
146Function and struct comments
147----------------------------
148
149Non-trivial functions should have a comment which describes what they do. If it
150is an exported function, put the comment in the header file so the API is in
151one place. If it is a static function, put it in the C file.
152
153If the function returns errors, mention that and list the different errors that
154are returned. If it is merely passing errors back from a function it calls,
155then you can skip that.
156
157See `here
158<https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html#function-documentation>`_
159for style.
160
161Driver model
162------------
163
164When declaring a device, try to use ``struct udevice *dev``, i.e. ``dev`` as the name:
165
166.. code-block:: C
167
168 struct udevice *dev;
169
170Use ``ret`` as the return value:
171
172.. code-block:: C
173
174 struct udevice *dev;
175 int ret;
176
177 ret = uclass_first_device_err(UCLASS_ACPI_PMC, &dev);
178 if (ret)
179 return log_msg_ret("pmc", dev);
180
181Consider using log_ret() or log_msg_ret() to return a value (see above).
182
183Add a ``p`` suffix on return arguments:
184
185.. code-block:: C
186
187 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
188 {
189 ...
190 *devp = dev;
191
192 return 0;
193 }
194
195There are standard variable names that you should use in drivers:
196
197* ``struct xxx_priv`` and ``priv`` for dev_get_priv()
198
199* ``struct xxx_plat`` and ``plat`` for dev_get_platdata()
200
201For example:
202
203.. code-block:: C
204
205 struct simple_bus_plat {
206 u32 base;
207 u32 size;
208 u32 target;
209 };
210
211 /* Davinci MMC board definitions */
212 struct davinci_mmc_priv {
213 struct davinci_mmc_regs *reg_base; /* Register base address */
214 uint input_clk; /* Input clock to MMC controller */
215 struct gpio_desc cd_gpio; /* Card Detect GPIO */
216 struct gpio_desc wp_gpio; /* Write Protect GPIO */
217 };
218
219 struct rcar_gpio_priv *priv = dev_get_priv(dev);
220
221 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
222
223Other
224-----
225
226Some minor things:
227
228* Put a blank line before the last ``return`` in a function unless it is the only line:
229
230.. code-block:: C
231
232 struct udevice *pci_get_controller(struct udevice *dev)
233 {
234 while (device_is_on_pci_bus(dev))
235 dev = dev->parent;
236
237 return dev;
238 }
239
240Tests
241-----
242
243Please add tests when you add code. Please change or expand tests when you change code.
244
245Run the tests with::
246
247 make check
248 make qcheck (skips some tests)
249
250Python tests are in test/py/tests - see the docs in test/py for info.
251
252Try to write your tests in C if you can. For example, tests to check a command
253will be much faster (10-100x or more) if they can directly call run_command()
254and ut_check_console_line() instead of using Python to send commands over a
255pipe to U-Boot.
256
257Tests run all supported CI systems (gitlab, travis, azure) using scripts in the
258root of the U-Boot tree.