Tom Rini | a023c19 | 2022-07-14 08:07:40 -0400 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0+: |
| 2 | |
| 3 | U-Boot Coding Style |
| 4 | =================== |
| 5 | |
| 6 | The following Coding Style requirements shall be mandatory for all code contributed to |
| 7 | the U-Boot project. |
| 8 | |
| 9 | Exceptions are only allowed if code from other projects is integrated with no |
| 10 | or only minimal changes. |
| 11 | |
| 12 | The 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 Rini | a023c19 | 2022-07-14 08:07:40 -0400 | [diff] [blame] | 36 | * 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 Schuchardt | f910cbf | 2022-07-16 06:19:08 +0200 | [diff] [blame^] | 44 | * All source files need to be in "Unix" and not "DOS" or "Windows" format, |
Tom Rini | a023c19 | 2022-07-14 08:07:40 -0400 | [diff] [blame] | 45 | 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 | |
| 58 | Submissions of new code or patches that do not conform to these requirements |
| 59 | shall be rejected with a request to reformat the changes. |
| 60 | |
| 61 | U-Boot Code Documentation |
| 62 | ------------------------- |
| 63 | |
| 64 | U-Boot adopted the kernel-doc annotation style, this is the only exception from |
| 65 | multi-line comment rule of Coding Style. While not mandatory, adding |
| 66 | documentation is strongly advised. The Linux kernel `kernel-doc |
| 67 | <https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html>`_ |
| 68 | documentation applies with no changes. |
| 69 | |
| 70 | Use structures for I/O access |
| 71 | ----------------------------- |
| 72 | |
| 73 | U-Boot typically uses a C structure to map out the registers in an I/O region, |
| 74 | rather 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 | |
| 84 | Some 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 | |
| 98 | Please use the check_member() macro to verify that your structure is the |
| 99 | expected size, or that particular members appear at the right offset. |
| 100 | |
| 101 | Include files |
| 102 | ------------- |
| 103 | |
| 104 | You should follow this ordering in U-Boot. The common.h header (which is going |
| 105 | away at some point) should always be first, followed by other headers in order, |
| 106 | then 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 | |
| 120 | Within that order, sort your includes. |
| 121 | |
| 122 | It is important to include common.h first since it provides basic features used |
| 123 | by most files, e.g. CONFIG options. |
| 124 | |
| 125 | For 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 |
| 127 | internal U-Boot things. See common/image.c for an example. |
| 128 | |
| 129 | If your file uses driver model, include <dm.h> in the C file. Do not include |
| 130 | dm.h in a header file. Try to use forward declarations (e.g. ``struct |
| 131 | udevice``) instead. |
| 132 | |
| 133 | Filenames |
| 134 | --------- |
| 135 | |
| 136 | For .c and .h files try to use underscore rather than hyphen unless you want |
| 137 | the file to stand out (e.g. driver-model uclasses should be named xxx-uclass.h. |
| 138 | Avoid upper case and keep the names fairly short. |
| 139 | |
| 140 | Function and struct comments |
| 141 | ---------------------------- |
| 142 | |
| 143 | Non-trivial functions should have a comment which describes what they do. If it |
| 144 | is an exported function, put the comment in the header file so the API is in |
| 145 | one place. If it is a static function, put it in the C file. |
| 146 | |
| 147 | If the function returns errors, mention that and list the different errors that |
| 148 | are returned. If it is merely passing errors back from a function it calls, |
| 149 | then you can skip that. |
| 150 | |
| 151 | See `here |
| 152 | <https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html#function-documentation>`_ |
| 153 | for style. |
| 154 | |
| 155 | Driver model |
| 156 | ------------ |
| 157 | |
| 158 | When 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 | |
| 164 | Use ``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 | |
| 175 | Consider using log_ret() or log_msg_ret() to return a value (see above). |
| 176 | |
| 177 | Add 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 | |
| 189 | There 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 | |
| 195 | For 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 | |
| 217 | Other |
| 218 | ----- |
| 219 | |
| 220 | Some 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 | |
| 234 | Tests |
| 235 | ----- |
| 236 | |
| 237 | Please add tests when you add code. Please change or expand tests when you change code. |
| 238 | |
| 239 | Run the tests with:: |
| 240 | |
| 241 | make check |
| 242 | make qcheck (skips some tests) |
| 243 | |
| 244 | Python tests are in test/py/tests - see the docs in test/py for info. |
| 245 | |
| 246 | Try to write your tests in C if you can. For example, tests to check a command |
| 247 | will be much faster (10-100x or more) if they can directly call run_command() |
| 248 | and ut_check_console_line() instead of using Python to send commands over a |
| 249 | pipe to U-Boot. |
| 250 | |
| 251 | Tests run all supported CI systems (gitlab, travis, azure) using scripts in the |
| 252 | root of the U-Boot tree. |