From LinuxMIPS
Revision as of 00:03, 9 November 2004 by Ralf (Talk | contribs)

Jump to: navigation, search

This document reflects what I have learned through porting several MIPS machines and other related Linux work. Hopefully it will help beginners to get started and give the experienced a reference point.

This document goes through all the major steps to port Linux to a MIPS machine. The focus can perhaps be called "MIPS machine abstraction layer", i.e., the interface between machine-specific code and, mostly, MIPS common code. Another useful document focuses on "Linux hardware abstraction layer", i.e., the interface between Linux common code and architecture-specific code. The document is written by C Hanish Menon (www.hanishkvc.com).

There are some notations used in this document.


   Reminder for incomplete part


   Really need your help on this


   Here is my opinion. What do you think?


   Thanks to the person who pointed this out


   Additional note added later, but the original comment may be still useful

An overview


  • Know C programming.
  • Have some knowledge of OS concepts, such as interrupt handling, system calls, memory management.
  • Know how to configure and make Linux kernel. You can find many helps on this if you are not very comfortable.
  • Have some knowledge of MIPS CPU. More than likely you will need to deal with CP0 registers, enable or disable interrupts, etc..
  • You don't have to be an expert in MIPS assembly, but total ignorance of it might make you handicapped in some situations.
  • Obviously, you need a MIPS hardware to play with.
  • Finally but most importantly, you need a willing-to-learn heart and perhaps many restless debugging hours. :-)

It is also highly recommanded to read through the Linux MIPS HOWTO by Ralf Bächle, ralf@gnu.org. By the way, as part of the pre-requisite, you should also remember Ralf's name. :-)

Kernel source trees

The common MIPS tree is the CVS tree at linux-mips.org. See the instructions in "Anonymous CVS servers" section in Linux/MIPS HOWTO. The current kernel version as of 2004/01/26 is 2.6.1. You can always check out earlier stable revisions by using "linux_2_4" or even "linux_2_2" branch tag.

Kernel patches

For various reasons, a kernel tree may leave bugs there for a quite long time before a suitable fix is checked in. There are various places to get patches. Here are some of the more common ones:

   Jun Sun patches
   Linux/MIPS FTP archive
   Maciej W. Rozyki patches
   Brad LaRonde's patches

Cross-compilation and toolchains

More than likely your MIPS box does not run Linux yet (why would you bother otherwise?). Therefore you will need another machine to build the kernel image. Once the image is built, you need to download this image to your MIPS machine and let it run your MIPS kernel. This is called cross-development. Your MIPS box is often called the target machine and the machine used to build the kernel image is called the host machine.

Cross-development is common for developing on embedded targets, because usually embedded targets do not have enough power or the peripherals to develop natively.

The most common host machine is probably Linux on i386/PCs

You need to have cross-development tools setup on your host before you can start. Several years of experience have show that hile you can find instructions to build cross-compilation tools, your best bet is probably to get some ready-made ones. Another reason is that a full toolchain build has become quite time consuming so you probably want to avoid it.

MontaVista used to offer for free Journeyman edition, which includes a full featured toolchain. Unfortunately, it does not offer that anymore. Instead you can download the preview kit, which includes a "slim" version of toolchain. You can get the kit from http://www.mvista.com/previewkit/index.html

Dan Kegel has a set of scripts that build cross-compiling tools. You can check it out here.

The following are links to pre-build toolchains, instructions to build your own toolchain and finally pre-compiled distributions for MIPS boards:

  • Brad LaRonde's cross toolchain for Linux
  • Steve Hill's toolchains for glibc and uClibc
  • Toolchains from MIPS Technologies
  • Distributions for MIPS

Overall porting steps

Depending on your specific cases, some of the following steps can be skipped.

  1. Hello World! - Get board setup, serial porting working, and print out "Hello, world!" through the serial port.
  2. Add your own code.
  3. Get early printk working - Make the first MIPS image and see the printk output from kernel.
  4. Serial Driver and Console - Get the real printk working with the serial console.
  5. KGDB can be enormously helpful in your development. It is highly recommended and it is not that difficult to set up.
  6. CPU support - If your MIPS CPU is not currently supported, you need to add new code that supports it.
  7. Board specific support - Create your board-specific directory. Setup interrupt routing/handling and kernel timer services.
  8. PCI subsystem - If your machine has PCI, you need to get the PCI subsystem working before you can use PCI devices.
  9. Ethernet drivers - You should already have the serial port working before attempting this. Usually the next driver you want is the ethernet driver. With ethernet driver working, you can set up a NFS root file system which gives you a fully working Linux userland.
  10. ROMFS root file system - Alternatively you can create a userland file system as a ROMFS image stored in a ramdisk.
  11. Some words on Debugging.

Hello, world!

In cross development, the serial port is usually the most important interface: That is where you can see anything happening! It might be worthwhile to make sure you get serial porting work before you even start playing with Linux. You can find the sample code or gzipped tar ball of a stand-alone program that can do printf. Such a program can even be useful in later debugging staging, e.g., printing out hardware register values.

Before you rush to type 'make', check and modify the following configurations:

  1. The sample code assumes R4K style CP0 structure. It should apply to most CPUs named above number 4000 and the recent MIPS32/MIPS64.
  2. Check if you have 1MB RAM size. (You really should have at 1MB to run Linux at all.) It is recommanded you have 8MB RAM or more.
  3. Is your serial port a standard UART type? If yes, modify the serial code and parameters. If not, you will have to supply your own functions to utilize the UART.
  4. What is your cross-tool name and path? Modify the Makefile accordingly.

Now, fire your "make" command.

Depending on your downloader on your MIPS box, you may need to generate ELF image, binary image or a SREC image.

Download the barebone image to your target and give it a run! Connect the serial port to your host machine. Start minicom and hopefully you can see the "Hello, world!" message.

Trouble shooting:

  • Make sure your bootloader downloads the image to uncached KSEG1 segment. If your bootloader downloads to the cached KSEG0 area, you will want to run the image from the KSEG0 area too.
  • If your bootloader has already initialized the serial port, you may want to skip your own initialization.
  • Did you set up minicom correctly? Test it with other machines.
  • Hopefully it is not the toolchain problem...

Next page: Add Your Own Code