ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / Documentation / sh / new-machine.txt
1
2                 Adding a new board to LinuxSH
3                ================================
4
5                Paul Mundt <lethal@linux-sh.org>
6
7 This document attempts to outline what steps are necessary to add support
8 for new boards to the LinuxSH port under the new 2.5 and 2.6 kernels. This
9 also attempts to outline some of the noticeable changes between the 2.4
10 and the 2.5/2.6 SH backend.
11
12 1. New Directory Structure
13 ==========================
14
15 The first thing to note is the new directory structure. Under 2.4, most
16 of the board-specific code (with the exception of stboards) ended up
17 in arch/sh/kernel/ directly, with board-specific headers ending up in
18 include/asm-sh/. For the new kernel, things are broken out by board type,
19 companion chip type, and CPU type. Looking at a tree view of this directory
20 heirarchy looks like the following:
21
22 Board-specific code:
23
24 .
25 |-- arch
26 |   `-- sh
27 |       `-- boards
28 |           |-- adx
29 |           |   `-- board-specific files
30 |           |-- bigsur
31 |           |   `-- board-specific files
32 |           |
33 |           ... more boards here ...
34 |
35 `-- include
36     `-- asm-sh
37         |-- adx
38         |   `-- board-specific headers
39         |-- bigsur
40         |   `-- board-specific headers
41         |
42         .. more boards here ...
43
44 It should also be noted that each board is required to have some certain
45 headers. At the time of this writing, io.h is the only thing that needs
46 to be provided for each board, and can generally just reference generic
47 functions (with the exception of isa_port2addr).
48
49 Next, for companion chips:
50 .
51 `-- arch
52     `-- sh
53         `-- cchips
54             `-- hd6446x
55                 |-- hd64461
56                 |   `-- cchip-specific files
57                 `-- hd64465
58                     `-- cchip-specific files
59
60 ... and so on. Headers for the companion chips are treated the same way as
61 board-specific headers. Thus, include/asm-sh/hd64461 is home to all of the
62 hd64461-specific headers.
63
64 Finally, CPU family support is also abstracted:
65 .
66 |-- arch
67 |   `-- sh
68 |       |-- kernel
69 |       |   `-- cpu
70 |       |       |-- sh2
71 |       |       |   `-- SH-2 generic files
72 |       |       |-- sh3
73 |       |       |   `-- SH-3 generic files
74 |       |       `-- sh4
75 |       |           `-- SH-4 generic files
76 |       `-- mm
77 |           `-- This is also broken out per CPU family, so each family can
78 |               have their own set of cache/tlb functions.
79 |
80 `-- include
81     `-- asm-sh
82         |-- cpu-sh2
83         |   `-- SH-2 specific headers
84         |-- cpu-sh3
85         |   `-- SH-3 specific headers
86         `-- cpu-sh4
87             `-- SH-4 specific headers
88
89 It should be noted that CPU subtypes are _not_ abstracted. Thus, these still
90 need to be dealt with by the CPU family specific code.
91
92 2. Adding a New Board
93 =====================
94
95 The first thing to determine is whether the board you are adding will be
96 isolated, or whether it will be part of a family of boards that can mostly
97 share the same board-specific code with minor differences.
98
99 In the first case, this is just a matter of making a directory for your
100 board in arch/sh/boards/ and adding rules to hook your board in with the
101 build system (more on this in the next section). However, for board families
102 it makes more sense to have a common top-level arch/sh/boards/ directory
103 and then populate that with sub-directories for each member of the family.
104 Both the Solution Engine and the hp6xx boards are an example of this.
105
106 After you have setup your new arch/sh/boards/ directory, remember that you
107 also must add a directory in include/asm-sh for headers localized to this
108 board. In order to interoperate seamlessly with the build system, it's best
109 to have this directory the same as the arch/sh/boards/ directory name,
110 though if your board is again part of a family, the build system has ways
111 of dealing with this, and you can feel free to name the directory after
112 the family member itself.
113
114 There are a few things that each board is required to have, both in the
115 arch/sh/boards and the include/asm-sh/ heirarchy. In order to better
116 explain this, we use some examples for adding an imaginary board. For
117 setup code, we're required at the very least to provide definitions for
118 get_system_type() and platform_setup(). For our imaginary board, this
119 might look something like:
120
121 /*
122  * arch/sh/boards/vapor/setup.c - Setup code for imaginary board
123  */
124 #include <linux/init.h>
125
126 const char *get_system_type(void)
127 {
128         return "FooTech Vaporboard";
129 }
130
131 int __init platform_setup(void)
132 {
133         /*
134          * If our hardware actually existed, we would do real
135          * setup here. Though it's also sane to leave this empty
136          * if there's no real init work that has to be done for
137          * this board.
138          */
139
140         /* 
141          * Presume all FooTech boards have the same broken timer,
142          * and also presume that we've defined foo_timer_init to
143          * do something useful.
144          */
145         board_time_init = foo_timer_init;
146
147         /* Start-up imaginary PCI ... */
148
149         /* And whatever else ... */
150
151         return 0;
152 }
153
154 Our new imaginary board will also have to tie into the machvec in order for it
155 to be of any use. Currently the machvec is slowly on its way out, but is still
156 required for the time being. As such, let us take a look at what needs to be
157 done for the machvec assignment.
158
159 machvec functions fall into a number of categories:
160
161  - I/O functions to IO memory (inb etc) and PCI/main memory (readb etc).
162  - I/O remapping functions (ioremap etc)
163  - some initialisation functions
164  - a 'heartbeat' function
165  - some miscellaneous flags
166
167 The tree can be built in two ways:
168  - as a fully generic build. All drivers are linked in, and all functions
169    go through the machvec
170  - as a machine specific build. In this case only the required drivers
171    will be linked in, and some macros may be redefined to not go through
172    the machvec where performance is important (in particular IO functions).
173
174 There are three ways in which IO can be performed:
175  - none at all. This is really only useful for the 'unknown' machine type,
176    which us designed to run on a machine about which we know nothing, and
177    so all all IO instructions do nothing.
178  - fully custom. In this case all IO functions go to a machine specific
179    set of functions which can do what they like
180  - a generic set of functions. These will cope with most situations,
181    and rely on a single function, mv_port2addr, which is called through the
182    machine vector, and converts an IO address into a memory address, which
183    can be read from/written to directly.
184
185 Thus adding a new machine involves the following steps (I will assume I am
186 adding a machine called vapor):
187
188  - add a new file include/asm-sh/vapor/io.h which contains prototypes for
189    any machine specific IO functions prefixed with the machine name, for
190    example vapor_inb. These will be needed when filling out the machine
191    vector. In addition, a section is required which defines what to do when
192    building a machine specific version. For example:
193
194      #ifdef __WANT_IO_DEF
195      #define inb  vapor_inb
196      ...
197      #endif
198
199    This is the minimum that is required, however there are ample
200    opportunities to optimise this. In particular, by making the prototypes
201    inline function definitions, it is possible to inline the function when
202    building machine specific versions. Note that the machine vector
203    functions will still be needed, so that a module built for a generic
204    setup can be loaded.
205
206  - add a new file arch/sh/boards/vapor/mach.c. This contains the definition
207    of the machine vector. When building the machine specific version, this
208    will be the real machine vector (via an alias), while in the generic
209    version is used to initialise the machine vector, and then freed, by
210    making it initdata. This should be defined as:
211
212      struct sh_machine_vector mv_vapor __initmv = {
213        .mv_name = "vapor",
214      }
215      ALIAS_MV(vapor)
216
217  - finally add a file arch/sh/boards/vapor/io.c, which contains
218    definitions of the machine specific io functions.
219
220 A note about initialisation functions. Three initialisation functions are
221 provided in the machine vector:
222  - mv_arch_init - called very early on from setup_arch
223  - mv_init_irq - called from init_IRQ, after the generic SH interrupt
224    initialisation
225  - mv_init_pci - currently not used
226
227 Any other remaining functions which need to be called at start up can be
228 added to the list using the __initcalls macro (or module_init if the code
229 can be built as a module). Many generic drivers probe to see if the device
230 they are targeting is present, however this may not always be appropriate,
231 so a flag can be added to the machine vector which will be set on those
232 machines which have the hardware in question, reducing the probe to a
233 single conditional.
234
235 3. Hooking into the Build System
236 ================================
237
238 Now that we have the corresponding directories setup, and all of the
239 board-specific code is in place, it's time to look at how to get the
240 whole mess to fit into the build system.
241
242 Large portions of the build system are now entirely dynamic, and merely
243 require the proper entry here and there in order to get things done.
244
245 The first thing to do is to add an entry to arch/sh/Kconfig, under the
246 "System type" menu:
247
248 config SH_VAPOR
249         bool "Vapor"
250         help
251           select Vapor if configuring for a FooTech Vaporboard.
252
253 next, this has to be added into arch/sh/Makefile. All boards require a
254 machdir-y entry in order to be built. This entry needs to be the name of
255 the board directory as it appears in arch/sh/boards, even if it is in a
256 sub-directory (in which case, all parent directories below arch/sh/boards/
257 need to be listed). For our new board, this entry can look like:
258
259 machdir-$(CONFIG_SH_VAPOR)      += vapor
260
261 provided that we've placed everything in the arch/sh/boards/vapor/ directory.
262
263 Next, the build system assumes that your include/asm-sh directory will also
264 be named the same. If this is not the case (as is the case with multiple
265 boards belonging to a common family), then the directory name needs to be
266 implicitly appended to incdir-y. The existing code manages this for the
267 Solution Engine and hp6xx boards, so see these for an example.
268
269 Once that is taken care of, it's time to add an entry for the mach type.
270 This is done by adding an entry to the end of the arch/sh/tools/mach-types
271 list. The method for doing this is self explanatory, and so we won't waste
272 space restating it here. After this is done, you will be able to use
273 implicit checks for your board if you need this somewhere throughout the
274 common code, such as:
275
276         /* Make sure we're on the FooTech Vaporboard */
277         if (!mach_is_vapor())
278                 return -ENODEV;
279
280 also note that the mach_is_boardname() check will be implicitly forced to
281 lowercase, regardless of the fact that the mach-types entries are all
282 uppercase. You can read the script if you really care, but it's pretty ugly,
283 so you probably don't want to do that.
284
285 Now all that's left to do is providing a defconfig for your new board. This
286 way, other people who end up with this board can simply use this config
287 for reference instead of trying to guess what settings are supposed to be
288 used on it.
289
290 Also, as soon as you have copied over a sample .config for your new board
291 (assume arch/sh/configs/vapor_defconfig), you can also use this directly as a
292 build target, and it will be implicitly listed as such in the help text.
293
294 Looking at the 'make help' output, you should now see something like:
295
296 Architecture specific targets (sh):
297   zImage                  - Compressed kernel image (arch/sh/boot/zImage)
298   adx_defconfig           - Build for adx
299   cqreek_defconfig        - Build for cqreek
300   dreamcast_defconfig     - Build for dreamcast
301 ...
302   vapor_defconfig         - Build for vapor
303
304 which then allows you to do:
305
306 $ make ARCH=sh CROSS_COMPILE=sh4-linux- vapor_defconfig vmlinux
307
308 which will in turn copy the defconfig for this board, run it through
309 oldconfig (prompting you for any new options since the time of creation),
310 and start you on your way to having a functional kernel for your new
311 board.
312