Add Your Own Code

From LinuxMIPS
Jump to: navigation, search

Let us add some code to the tree and make a Linux image. For conveninence sake, let us say we are porting Linux to a MIPS board called Aloha.

Create the right directory for your board

Your code for a new board can be classified into board-support code (or board-specific code) and drivers. Driver code should be placed under the 'drivers' directory and board specific code should be placed under 'arch/mips' directory.

The easiest choice is to create a directory called 'arch/mips/aloha'.

However, a couple of other considerations might make it slightly complicated.

  • If Aloha uses a chipset or System on a Chip (SOC) that is already supported or belongs to a bigger family, such as NEC VR41xx and gt64120, it makes sense to put Aloha code under those sub-directories. You can re-use and share a lot of common code.
  • Similarly, if Aloha is the first board that uses a chipset or SOC which is expected to be used in many other boards, you may want to create similar directory structure. However, if you are not sure, just create your own board specific directory.

In the past people have created directories based on the board manufacturer's name, such as "mips-boards". This generally is not a good idea. It is almost certain that some of these boards do not share anything common at all.

To make things worse, sometimes boards made by different companies use the same chipset or SOC. Now what are you going to do? Are you going to duplicate the common code? Or are you going stick one company's board under another company's name?

For header files, you usually create similar directory or header files under include/asm-mips. [DEBATE] For board specific header files, I would encourage people to place them under the corresponding 'arch/mips' directory if possible.

In our exmaple, we will create 'arch/mips/aloha' directory.

Write the minimum Aloha code

Let us write some code for the Aloha board which can generate a complete Linux image without complaining about missing symbols.

Go to this directory to browse 'arch/mips/aloha' directory. Or download the gzipped file of the directory.

Obviously the code is not complete yet, but if you follow the following steps and everything is correct, you should be able to generate a Linux/MIPS kernel image of your very own!

Hook up your code with the Linux tree

Most of the steps are fairly straightforward:

  • include/asm-mip/bootinfo.h - Add your machine group ID, machine group name and Aloha machine ID.
  • arch/mips/kernel/setup.c - Add 'aloha_setup' function declaration and invocation code.
  • arch/mips/Makefile - Add a section that links your Aloha code in.
       # Hawaii Aloha board
       ifdef CONFIG_ALOHA
       SUBDIRS       += arch/mips/aloha
       LIBS          += arch/mips/aloha/aloha.o
       LOADADDR      += 0x80002000

LOADADDR is the starting address for your Linux image when it is loaded into RAM. Note that the first 0x200 bytes are used by the exception vectors on most CPUs. Some CPUs will requries a larger space, so modify the LOADADDR accordingly. Due to the linker's addressing limit, the start address is aligned on a 8KB boundary, so setting your LOADADDR to 0x80002000 should be reasonable.

  • arch/mips/ - Add necessary config information for Aloha board.
  1. Add the following to 'Machine selection'.
 dep_bool 'Support for Hawaii Aloha board  (EXPERIMENTAL)' CONFIG_ALOHA $CONFIG_EXPERIMENTAL
  1. Add a set of default configs for the board, which depends on the features and drivers that Linux port will supports. Here is a very simple example for our minimum Aloha board configurations.
           if [ "$CONFIG_ALOHA" = "y" ]; then
              define_bool CONFIG_CPU_R4X00 y
              define_bool CONFIG_CPU_LITTLE_ENDIAN y
              define_bool CONFIG_SERIAL y
              define_bool CONFIG_SERIAL_MANY_PORTS y
              define_bool CONFIG_NEW_IRQ y
              define_bool CONFIG_NEW_TIME_C y
              define_bool CONFIG_SCSI n

There are two kinds of configuration options here. The first kind are those you cannot select interactively during 'make config' or 'make menuconfig' or 'make xconfig'. Examples are CONFIG_NEW_IRQ and CONFIG_NEW_TIME_C. You must put them here, or else they will not get selected. The second kind of options are those you can select interactively, such as CONFIG_CPU_R4X00 and CONFIG_SERIAL. However you may also put them here if you know which selection is right for the board. This way people will make fewer mistakes when they configure for the board.

For instant gratification, you can find a complete patch for adding the Aloha board support to the Linux/MIPS CVS tree checked out on January 20, 2004.

Configure and build a kernel image

Now you are ready to run your favorite configuration tool. Since we do not have much code added yet, do not be too greedy with selecting options. Just pick a couple of simple options such as the serial and serial console.

  • If you denoted the Aloha board support to be EXPERIMENTAL, select 'Prompt for development and/or incomplete code/drivers' under 'Code maturity level options'.
  • Select 'Support for Hawaii Aloha board' and unselect all other machines under 'Machine selection'.
  • Select the right CPU. Under 'CPU selection' select your CPU. If there is no entry for the CPU on your board, you will need to add support for it. Most recent CPUs can generally run to some degree with CPU_R4X00.
  • Under 'Character devices', select 'Standard/generic (8250/16550 and compatible UARTs) serial support' and 'Support for console on serial port'. Unselect the 'Virtual terminal' option.
  • Under the 'Kernel hacking' option, select 'Are you using a crosscompiler'.
  • For other options either take the default or select 'no'.

Here is a sample minimum config for our Aloha board.

Before you type 'make', double-check the 'arch/mips/Makefile' and make sure the cross-toolchain program names are correct and in your execution path i.e. your PATH environment variable.

Now type 'make dep' and 'make'. Then wait for a miracle to happen!

Next page: Early printk