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