Linux Custom Porting Guide
Linux Custom Porting Guide
Rafi Yanai my partner in the porting process Avi Rubenbach without whom this wouldn't be possible Revision History Revision 2.1 20030308 Modified code example per author Revision 2.0 20020613 Added GFDL per author Revision 1.0 20020513 Initial release
Table of Contents
Chapter 1. Introduction ......................................................................................................................................1 1.1. Who needs to read this ?...................................................................................................................1 1.2. What do I need to know (why so much) ?........................................................................................1 1.3. The tools...........................................................................................................................................1 1.4. The hardware....................................................................................................................................2 1.5. Copyright & License.........................................................................................................................2 Chapter 2. Bootcamp: How To Begin ?............................................................................................................3 2.1. Creating a development environment...............................................................................................3 2.2. Compiling the first kernel.................................................................................................................3 2.3. Booting the machine.........................................................................................................................4 Chapter 3. Booting In The Dark ........................................................................................................................5 3.1. Debugging with print_str()...............................................................................................................5 3.2. Modifying code using compiler flags...............................................................................................5 3.3. Getting the console to work..............................................................................................................6 3.3.1. Forcing the kernel to boot ourway........................................................................................6 3.3.2. Nonstandard hardware just say no! ....................................................................................6 3.3.3. Let there be light: calculating baud rate..................................................................................6 Chapter 4. Linux Still Isn't Booting..................................................................................................................8 4.1. Memory probing, RTC and decrementors........................................................................................8 4.2. Biglittle endian (we should have known).......................................................................................8 4.2.1. Probing the CPC700................................................................................................................8 4.2.2. Making CPC700 speak littleendian .......................................................................................8 4.3. Ethernet: our first PCI device...........................................................................................................9 4.4. Some Miscellaneous Issues .............................................................................................................10 Chapter 5. Linux Is Booting ... What Now ?..................................................................................................11 5.1. The 64 bit barrier............................................................................................................................11 5.2. Booting from flash..........................................................................................................................12 Appendix A. GNU Free Documentation License...........................................................................................13 A.1. PREAMBLE..................................................................................................................................13 A.2. APPLICABILITY AND DEFINITIONS......................................................................................13 A.3. VERBATIM COPYING................................................................................................................14 A.4. COPYING IN QUANTITY...........................................................................................................14 A.5. MODIFICATIONS........................................................................................................................15 A.6. COMBINING DOCUMENTS .......................................................................................................16 A.7. COLLECTIONS OF DOCUMENTS............................................................................................16 A.8. AGGREGATION WITH INDEPENDENT WORKS...................................................................17 A.9. TRANSLATION ............................................................................................................................17 A.10. TERMINATION..........................................................................................................................17 A.11. FUTURE REVISIONS OF THIS LICENSE...............................................................................17 A.12. How to use this License for your documents...............................................................................18
Table of Contents
Appendix B. Trademarks.................................................................................................................................19
ii
Chapter 1. Introduction
1.1. Who needs to read this ?
This guide describes a work in progress, to port Linux to a custom PowerPCbased board. This means making the operating system work on unfamiliar hardware. Anyone who is on the same track might benefit from reading this paper, as it highlights the pitfalls and problematic points along the way.
Custom Linux: A Porting Guide CVS: A version control system, allows you to keep multiple versions of the code. Other than backing up the code, it allows diffing between different version, and reverting to older version, when needed. A terminal program, like HyperTerminal or ProCOMM for Windows", or minicom for Linux.
Chapter 1. Introduction
The Linux kernel is modular, and allows you to configure it and choose which "blocks" should be compiled with the kernel. In order to do this, first cd /usr/src/linux (assuming your kernel source code is installed at /usr/src/linux). Once there, type make xconfig.After saving your options, you should make vmlinux to create a kernel image suitably for using with VisionICE. We will not go into more details here, as it's outside the scope of this document. For more information, try http://www.tldp.org/HOWTO/KernelHOWTO.html
void print_char (char ch) { volatile unsigned char status = 0; /* wait until txempty */ while ((status & LSR_THREMPTY ) == 0) status = *((volatile unsigned char *)(COM1_ADDRESS + LSR_REG)); *((volatile unsigned char *)(COM1_ADDRESS + THR_REG)) = ch; }
There's a better code for printing directly to the serial port, however, it's a bit more complicated. You can find it in arch/ppc/boot/common/misccommon.c, using puts() or putc().
To "activate" our code, we added the new flag to the kernel configuration file .config by adding CONFIG_TESTMACH=y to it. In the first stage, this solution allows you a quick way to find the code you changed, but later the flag you chose will allow you to add your code into the kernel tree and into the configuration program (make xconfig).
The first (naive) thing we tried, was to configure the console the way we wanted. Of course, this didn't help us much ;) Disappointed but not discouraged, we remembered that we didn't have a bootloader yet, and that we didn't really know if any option was being passed on to the kernel. "Maybe the kernel gets some garbage for command line?" we (again, naively) thought. So we tried to stop the kernel from parsing commandline options, and manually inserted our command line. This didn't help us much ;)
Ofcourse, this didn't help us much :( The lesson learned here was check, check, check your hardware!. Custom boards might not be standard, and the porting will go a lot quicker if you know about it.
Custom Linux: A Porting Guide BAUDBASE, which is later used for everything regarding serial ports. It was computed using the board's local bus frequency, bus clock to system clock ratio etc. This seemed wrong, so we checked out what the base baud was in a vxWorks system we had running on the board, and changed it to:
/* * system clock = 33Mhz, serial clock = system clock / 4 * the following must hold: (divisor * BaudRate) == (System clock / 64) */ #define BASE_BAUD (33000000 / 4 / 16)
A quick compilation, and a reboot later we had a booting kernel visible through our serial port. Success!
A short compilation later, PCI probing was working! We got some beer and partied ;)
The function maps IRQs according to IDselects, which means in the order on the PCI bus by which the devices are set. This structure is a bit tricky: min_idsel denotes the topleft corner of the array, and max_idsel is the bottomleft corner. irqs_per_slot is the number of IRQs per line. The structure is as follows:
each cell contains (IDSEL, SLOT#, IRQ) ++ | (3,0,22) | (3,1,0) | (3,2,0) | (3,3,0) | ++ | (4,0,0) | (4,1,0) | (4,2,0) | (4,3,0) | ++ .......... .......... ++ | (9,0,0) | (9,1,0) | (9,2,0) | (9,3,0) | ++
Custom Linux: A Porting Guide As you can see, our i8559er needs IRQ 22, and is seated in IDselect 3. Of course, we didn't know that at the start, so we wrote a small piece of code that read all the vendor IDs in all the IDselects. Once done we compiled, but the ethernet device still didn't work. The next problem was that the module couldn't decide on a MAC address for the device. The MAC address should be written on an EEPROM chip (connected to the device), but we discovered that the hardware guys decided that i82559 doesn't need the EEPROM, so they removed it. After hardcoding a MAC address inside eepro100.c, the ethernet device finally worked. The final solution was to make the module read the MAC address from NVRAM memory, and if no other choice was available, to fall back to a default MAC address. The next step was to mount a NFS root filesystem. For details see the documentation in Documentation/nfsroot.txt
As you can see, we don't take the return value of ioremap(). We don't need it, since at this stage the kernel maps the addresses so that virtual address == physical address.
10
The function adds a floating point to the PowerPC MSR register, and makes sure that no exceptions will be generated as a result of doing FP. Once done, it uses an assembly code, described below in the sysOut64() to do the actual floatingpoint operation. Note that the function turns off interrupts, but this is acceptable here, since we use the function on rare occasion.
_GLOBAL(sysOut64) stwu r1, DEPTH(r1) mflr r0 stw r31, FP_LOC(r1) stw r0, LR_LOC(r1) mr r31, r1 stfd fr0, FPR_SAVE(r31) lfd fr0,0(r4) stfd fr0,0(r3) eieio lfd lwz lwz mtlr fr0, FPR_SAVE(r31) r4, 0(r1) r0, 4(r4) r0 /* restore floating point value */ /* now restore the stack frame */
11
12
Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 021111307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
A.1. PREAMBLE
The purpose of this License is to make a manual, textbook, or other written document "free" in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others. This License is a kind of "copyleft", which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference.
13
Custom Linux: A Porting Guide A "Transparent" copy of the Document means a machinereadable copy, represented in a format whose specification is available to the general public, whose contents can be viewed and edited directly and straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup has been designed to thwart or discourage subsequent modification by readers is not Transparent. A copy that is not "Transparent" is called "Opaque". Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standardconforming simple HTML designed for human modification. Opaque formats include PostScript, PDF, proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machinegenerated HTML produced by some word processors for output purposes only. The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, "Title Page" means the text near the most prominent appearance of the work's title, preceding the beginning of the body of the text.
Custom Linux: A Porting Guide take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document.
A.5. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version: A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has less than five). C. State on the Title page the name of the publisher of the Modified Version, as the publisher. D. Preserve all the copyright notices of the Document. E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices. F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below. G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document's license notice. H. Include an unaltered copy of this License. I. Preserve the section entitled "History", and its title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section entitled "History" in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the "History" section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. K. In any section entitled "Acknowledgements" or "Dedications", preserve the section's title, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. M. Delete any section entitled "Endorsements". Such a section may not be included in the Modified Version. N. Do not retitle any existing section as "Endorsements" or to conflict in title with any Invariant Section. If the Modified Version includes new frontmatter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these Appendix A. GNU Free Documentation License 15
Custom Linux: A Porting Guide sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version's license notice. These titles must be distinct from any other section titles. You may add a section entitled "Endorsements", provided it contains nothing but endorsements of your Modified Version by various partiesfor example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard. You may add a passage of up to five words as a FrontCover Text, and a passage of up to 25 words as a BackCover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of FrontCover Text and one of BackCover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version.
16
A.9. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License provided that you also include the original English version of this License. In case of a disagreement between the translation and the original English version of this License, the original English version will prevail.
A.10. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
17
Copyright (c) YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being LIST THEIR TITLES, with the FrontCover Texts being LIST, and with the BackCover Texts being LIST. A copy of the license is included in the section entitled "GNU Free Documentation License". If you have no Invariant Sections, write "with no Invariant Sections" instead of saying which ones are invariant. If you have no FrontCover Texts, write "no FrontCover Texts" instead of "FrontCover Texts being LIST"; likewise for BackCover Texts. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software.
18
Appendix B. Trademarks
Linux is a registered trademark of Linus Torvalds. MontaVista" is a trademark of MontaVista Software Inc. PowerPC is a registered trademark of IBM Corporation. Windows is a registered trademark of Microsoft Corporation. vxWorks" and Vision ICE" are trademarks of Wind River Systems Inc. ProCOMM" is a trademark of Symantec Corporation.
Appendix B. Trademarks
19