Você está na página 1de 17

PortingtheLinuxkerneltoanARMboard

PortingtheLinux
kernel
toanARMboard
ThomasPetazzoni
FreeElectrons
Copyright20092010,FreeElectrons.
CreativeCommonsBYSA3.0license
Latestupdate:Jul30,2016,
Documentsources,updatesandtranslations:
http://freeelectrons.com/docs/kernelporting
Corrections,suggestions,contributionsandtranslationsarewelcome!

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

PortingtheLinuxkernel
TheLinuxkernelsupportsalotofdifferentCPUarchitectures
Eachofthemismaintainedbyadifferentgroupofcontributors
SeetheMAINTAINERSfilefordetails

Theorganizationofthesourcecodeandthemethodstoportthe
Linuxkerneltoanewboardarethereforeveryarchitecture
dependent
Forexample,PowerPCandARMareverydifferent
PowerPCreliesondevicetreestodescribehardwaredetails
ARMreliesonsourcecodeonly

ThispresentationisfocusedontheARMarchitectureonly

2
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Architecture,CPUandmachine
Inthesourcetree,eacharchitecturehasitsowndirectory
arch/armfortheARMarchitecture
ThisdirectorycontainsgenericARMcode
boot,common,configs,kernel,lib,mm,nwfpe,
vfp,oprofile,tools

AndmanydirectoriesfordifferentCPUfamilies
mach*directories:machpxaforPXACPUs,machimxfor
FreescaleiMXCPUs,etc.
Eachofthesedirectoriescontain
SupportfortheCPU
SupportforseveralboardsusingthisCPU

SomeCPUtypessharesomecode,inanentitycalledaplatform
platomapcontainscommoncodefrommachomap1andmach
omap2

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

SourcecodeforCalaoUSBA9263
TakingthecaseoftheCalaoUSBA9263board,whichusesa
AT91SAM9263CPU.
arch/
arm/
machat91/
AT91genericcode
clock.c,leds.c,irq.c,pm.c
CPUspecificcodefortheAT91SAM9263
at91sam9263.c,
at91sam926x_time.c,
at91sam9263_devices.c
Boardspecificcode
boardusba9263.c

Fortherestofthispresentation,wewillfocusonboardsupport
only
4
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Configuration
Aconfigurationoptionmustbedefinedfortheboard,in
arch/arm/machat91/Kconfig
configMACH_USB_A9263
bool"CALAOUSBA9263"
dependsonARCH_AT91SAM9263
help
SelectthisifyouareusingaCalaoSystemsUSBA9263.
<http://www.calaosystems.com>

ThisoptionmustdependontheCPUtypeoptioncorresponding
totheCPUusedintheboard
HeretheoptionisARCH_AT91SAM9263,definedinthesamefile

Adefaultconfigurationfilefortheboardcanoptionallybestored
inarch/arm/configs/.Forourboard,it'susb
a9263_defconfig
5
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Compilation
Thesourcefilescorrespondingtotheboardsupportmustbe
associatedwiththeconfigurationoptionoftheboard
Thisisdoneinarch/arm/machat91/Makefile
obj$(CONFIG_MACH_USB_A9263)+=boardusba9263.o

TheMakefilealsotellswhichfilesarecompiledforeveryAT91
CPU
objy:=irq.ogpio.o
obj$(CONFIG_AT91_PMC_UNIT)+=clock.o
objy+=leds.o
obj$(CONFIG_PM)+=pm.o
obj$(CONFIG_AT91_SLOW_CLOCK)+=pm_slowclock.o

AndwhichfilesforourparticularCPU,theAT91SAM9263
obj$(CONFIG_ARCH_AT91SAM9263)+=at91sam9263.oat91sam926x_time.o
at91sam9263_devices.osam9_smc.o
6
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Machinestructure
Eachboardisdefinedbyamachinestructure
Thewordmachineisquiteconfusingsinceeverymach*
directorycontainsseveralmachinedefinitions,oneforeachboard
usingagivenCPUtype

FortheCalaoboard,attheendofarch/arm/mach
at91/boardusba9263.c
MACHINE_START(USB_A9263,"CALAOUSB_A9263")
/*Maintainer:calaosystems*/
.phys_io=AT91_BASE_SYS,
.io_pg_offst=(AT91_VA_BASE_SYS>>18)&0xfffc,
.boot_params=AT91_SDRAM_BASE+0x100,
.timer=&at91sam926x_timer,
.map_io=ek_map_io,
.init_irq=ek_init_irq,
.init_machine=ek_board_init,
MACHINE_END

7
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Machinestructuremacros
MACHINE_STARTandMACHINE_END
Macrosdefinedinarch/arm/include/asm/mach/arch.h
Theyarehelperstodefineastructmachine_descstructure
storedinaspecificELFsection
Severalmachine_descstructurescanbedefinedinakernel,
whichmeansthatthekernelcansupportseveralboards.
Therightstructureischosenatboottime

8
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Machinetypenumber
IntheARMarchitecture,eachboardtypeisidentifiedbya
machinetypenumber
Thelatestmachinetypenumberslistcanbefoundat
http://www.arm.linux.org.uk/developer/machines/download.php
Acopyofitexistsinthekerneltreeinarch/arm/tools/mach
types
FortheCalaoboard
usb_a9263MACH_USB_A9263USB_A92631710

Atcompiletime,thisfileisprocessedtogenerateaheaderfile,
include/asmarm/machtypes.h
FortheCalaoboard
#defineMACH_TYPE_USB_A92631710
Andafewothermacrosinthesamefile
9
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Machinetypenumber
ThemachinetypenumberissetintheMACHINE_START()
definition
MACHINE_START(USB_A9263,"CALAOUSB_A9263")
Atruntime,themachinetypenumberoftheboardonwhichthe
kernelisrunningispassedbythebootloaderinregisterr1
Veryearlyinthebootprocess(arch/arm/kernel/head.S),
thekernelcalls__lookup_machine_typein
arch/arm/kernel/headcommon.S
__lookup_machine_typelooksatallthemachine_desc
structuresofthespecialELFsection
Ifitdoesn'tfindtherequestednumber,printsamessageandstops
Iffound,itknowsthemachinedescriptionsandcontinuestheboot
process
10
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Earlydebuggingandbootparameters
Earlydebugging
phys_ioisthephysicaladdressoftheI/Ospace
io_pg_offsetistheoffsetinthepagetabletoremaptheI/O
space
TheseareusedwhenCONFIG_DEBUG_LLisenabledtoprovide
veryearlydebuggingmessagesontheserialport

Bootparameters
boot_paramsisthelocationwherethebootloaderhaslefttheboot
parameters(thekernelcommandline)
Thebootloadercanoverridethisaddressinregisterr2
SeealsoDocumentation/arm/Bootingforthedetailsofthe
environmentexpectedbythekernelwhenbooted
11
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Systemtimer
Thetimerfieldpointtoastructsys_timerstructure,that
describesthesystemtimer
UsedtogeneratetheperiodictickatHZfrequencytocallthe
schedulerperiodically

OntheCalaoboard,thesystemtimerisdefinedbythe
at91sam926x_timerstructureinat91sam926x_time.c
ItcontainstheinterrupthandlercalledatHZfrequency
Itisintegratedwiththeclockeventsandtheclocksource
infrastructures
Seeinclude/linux/clocksource.hand
include/linux/clockchips.hfordetails

12
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

map_io()
Themap_io()functionpointstoek_map_io(),which
InitializestheCPUusingat91sam9263_initialize()
MapI/Ospace
Registerandinitializetheclocks

Configuresthedebugserialportandsettheconsoletobeonthis
serialport
CalledattheverybeginningoftheCcodeexecution
init/main.c:start_kernel()
arch/arm/kernel/setup.c:setup_arch()
arch/arm/mm/mmu.c:paging_init()
arch/arm/mm/mmu.c:devicemaps_init()
mdesc>map_io()
13
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

init_irq()
init_irq()toinitializetheIRQhardwarespecificdetails
Implementedbyek_init_irq(),whichcalls
at91sam9263_init_interrupts()inat91sam9263.c,
whichmainlycallsat91_aic_init()inirq.c
Initializetheinterruptcontroller,assignthepriorities
RegistertheIRQchip(irq_chipstructure)tothekernelgeneric
IRQinfrastructure,sothatthekernelknowshowtoack,mask,
unmasktheIRQs

Calledalittlebitlaterthanmap_io()
init/main.c:start_kernel()
arch/arm/kernel/irq.c:init_IRQ()
init_arch_irq()(equaltomdesc>init_irq)
14
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

init_machine()
init_machine()completestheinitializationoftheboardby
registeringallplatformdevices
Calledbycustomize_machines()in
arch/arm/kernel/setup.c
Thisfunctionisanarch_initcall(listoffunctionswhose
addressisstoredinaspecificELFsection,bylevels)
Attheendofkernelinitialization,justbeforerunningthefirst
userspaceprograminit:
init/main.c:kernel_init()
init/main.c:do_basic_setup()
init/main.c:do_initcalls()
Callsallinitcalls,levelbylevel
15
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

init_machine()forCalao
FortheCalaoboard,implementinek_board_init()
Registersserialports,USBhost,USBdevice,SPI,Ethernet,NAND
flash,2IC,buttonsandLEDs
Usesat91_add_device_*()helpers,definedin
at91sam9263_devices.c
Thesehelperscallplatform_device_register()toregister
thedifferentplatform_devicestructuresdefinedinthesamefile
Forsomedevices,theboardspecificcodedoestheregistration
itself(buttons)orpassesboardspecificdatatotheregistration
helper(USBhostanddevice,NAND,Ethernet,etc.)

16
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Drivers
Theat91sam9263_devices.cfiledoesn'timplementthe
driversfortheplatformdevices
Thedriversareimplementedatdifferentplacesofthekerneltree
FortheCalaoboard
USBhost,driverat91_ohci,drivers/usb/host/ohciat91.c
USBdevice,driverat91_udc,drivers/usb/gadget/at91_udc.c
Ethernet,drivermacb,drivers/net/macb.c
NAND,driveratmel_nand,drivers/mtd/nand/atmel_nand.c
I2ConGPIO,driveri2cgpio,drivers/i2c/busses/i2cgpio.c
SPI,driveratmel_spi,drivers/spi/atmel_spi.c
Buttons,drivergpiokeys,drivers/input/keyboard/gpio_keys.c

Allthesedriversareselectedbythereadymadeconfigurationfile
17
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Você também pode gostar