README.sunxi64 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. Allwinner 64-bit boards README
  2. ==============================
  3. Newer Allwinner SoCs feature ARMv8 cores (ARM Cortex-A53) with support for
  4. both the 64-bit AArch64 mode and the ARMv7 compatible 32-bit AArch32 mode.
  5. Examples are the Allwinner A64 (used for instance on the Pine64 board) or
  6. the Allwinner H5 SoC (as used on the OrangePi PC 2).
  7. These SoCs are wired to start in AArch32 mode on reset and execute 32-bit
  8. code from the Boot ROM (BROM). As this has some implications on U-Boot, this
  9. file describes how to make full use of the 64-bit capabilities.
  10. Quick Start / Overview
  11. ======================
  12. - Build the ARM Trusted Firmware binary (see "ARM Trusted Firmware (ATF)" below)
  13. $ cd /src/arm-trusted-firmware
  14. $ make PLAT=sun50i_a64 DEBUG=1 bl31
  15. - Build the SCP firmware binary (see "SCP firmware (Crust)" below)
  16. $ cd /src/crust
  17. $ make pine64_plus_defconfig && make -j5 scp
  18. - Build U-Boot (see "SPL/U-Boot" below)
  19. $ export BL31=/path/to/bl31.bin
  20. $ export SCP=/src/crust/build/scp/scp.bin
  21. $ make pine64_plus_defconfig && make -j5
  22. - Transfer to an uSD card (see "microSD card" below)
  23. $ dd if=u-boot-sunxi-with-spl.bin of=/dev/sdx bs=8k seek=1
  24. - Boot and enjoy!
  25. Building the firmware
  26. =====================
  27. The Allwinner A64/H5/H6 firmware consists of several parts: U-Boot's SPL,
  28. ARM Trusted Firmware (ATF), optional System Control Processor (SCP) firmware
  29. (e.g. Crust), and the U-Boot proper.
  30. The SPL will load all of the other firmware binaries into RAM, along with the
  31. right device tree blob (.dtb), and will pass execution to ATF (in EL3). If SCP
  32. firmware was loaded, ATF will power on the SCP and wait for it to boot.
  33. ATF will then drop into U-Boot proper (in EL2).
  34. As the ATF binary and SCP firmware will become part of the U-Boot image file,
  35. you will need to build them first.
  36. ARM Trusted Firmware (ATF)
  37. ----------------------------
  38. Checkout the latest master branch from the official ATF repository [1] and
  39. build it:
  40. $ export CROSS_COMPILE=aarch64-linux-gnu-
  41. $ make PLAT=sun50i_a64 DEBUG=1 bl31
  42. The resulting binary is build/sun50i_a64/debug/bl31.bin. Either put the
  43. location of this file into the BL31 environment variable or copy this to
  44. the root of your U-Boot build directory (or create a symbolic link).
  45. $ export BL31=/src/arm-trusted-firmware/build/sun50i_a64/debug/bl31.bin
  46. (adjust the actual path accordingly)
  47. The platform target "sun50i_a64" covers all boards with either an Allwinner
  48. A64 or H5 SoC (since they are very similar). For boards with an Allwinner H6
  49. SoC use "sun50i_h6".
  50. If you run into size issues with the resulting U-Boot image file, it might
  51. help to use a release build, by using "DEBUG=0" when building bl31.bin.
  52. As sometimes the ATF build process is a bit picky about the toolchain used,
  53. or if you can't be bothered with building ATF, there are known working
  54. binaries in the firmware repository[3], purely for convenience reasons.
  55. SCP firmware (Crust)
  56. ----------------------
  57. SCP firmware is responsible for implementing system suspend/resume, and (on
  58. boards without a PMIC) soft poweroff/on. ATF contains fallback code for CPU
  59. power control, so SCP firmware is optional if you don't need either of these
  60. features. It runs on the AR100, with is an or1k CPU, not ARM, so it needs a
  61. different cross toolchain.
  62. There is one SCP firmware implementation currently available, Crust:
  63. $ git clone https://github.com/crust-firmware/crust
  64. $ cd crust
  65. $ export CROSS_COMPILE=or1k-linux-musl-
  66. $ make pine64_plus_defconfig
  67. $ make scp
  68. The same configuration generally works on any board with the same SoC (A64, H5,
  69. or H6), so if there is no config for your board, use one for a similar board.
  70. Like for ATF, U-Boot finds the SCP firmware binary via an environment variable:
  71. $ export SCP=/src/crust/build/scp/scp.bin
  72. If you do not want to use SCP firmware, you can silence the warning from binman
  73. by pointing it to an empty file:
  74. $ export SCP=/dev/null
  75. SPL/U-Boot
  76. ------------
  77. Both U-Boot proper and the SPL are using the 64-bit mode. As the boot ROM
  78. enters the SPL still in AArch32 secure SVC mode, there is some shim code to
  79. enter AArch64 very early. The rest of the SPL runs in AArch64 EL3.
  80. U-Boot proper runs in EL2 and can load any AArch64 code (using the "go"
  81. command), EFI applications (with "bootefi") or arm64 Linux kernel images
  82. (often named "Image"), using the "booti" command.
  83. $ make clean
  84. $ export CROSS_COMPILE=aarch64-linux-gnu-
  85. $ make pine64_plus_defconfig
  86. $ make
  87. This will build the SPL in spl/sunxi-spl.bin and a FIT image called u-boot.itb,
  88. which contains the rest of the firmware. u-boot-sunxi-with-spl.bin joins those
  89. two components in one convenient image file.
  90. Boot process
  91. ============
  92. The on-die BROM code will try several methods to load and execute the firmware.
  93. On a typical board like the Pine64 this will result in the following boot order:
  94. 1) Reading 32KB from sector 16 (@8K) of the microSD card to SRAM A1. If the
  95. BROM finds the magic "eGON" header in the first bytes, it will execute that
  96. code. If not (no SD card at all or invalid magic), it will:
  97. 2) Try to read 32KB from sector 16 (@8K) of memory connected to the MMC2
  98. controller, typically an on-board eMMC chip. If there is no eMMC or it does
  99. not contain a valid boot header, it will:
  100. 3) Initialize the SPI0 controller and try to access a NOR flash connected to
  101. it (using the CS0 pin). If a flash chip is found, the BROM will load the
  102. first 32KB (from offset 0) into SRAM A1. Now it checks for the magic eGON
  103. header and checksum and will execute the code upon finding it. If not, it will:
  104. 4) Initialize the USB OTG controller and will wait for a host to connect to
  105. it, speaking the Allwinner proprietary (but deciphered) "FEL" USB protocol.
  106. To boot the Pine64 board, you can use U-Boot and any of the described methods.
  107. FEL boot (USB OTG)
  108. ------------------
  109. FEL is the name of the Allwinner defined USB boot protocol built in the
  110. mask ROM of most Allwinner SoCs. It allows to bootstrap a board solely
  111. by using the USB-OTG interface and a host port on another computer.
  112. As the FEL mode is controlled by the boot ROM, it expects to be running in
  113. AArch32. For now the AArch64 SPL cannot properly return into FEL mode, so the
  114. feature is disabled in the configuration at the moment.
  115. The repository in [3] contains FEL capable SPL binaries, built using an
  116. off-tree branch to generate 32-bit ARM code (along with instructions
  117. how to re-create them).
  118. microSD card
  119. ------------
  120. Transfer the SPL and the U-Boot FIT image directly to an uSD card:
  121. # dd if=spl/sunxi-spl.bin of=/dev/sdx bs=8k seek=1
  122. # dd if=u-boot.itb of=/dev/sdx bs=8k seek=5
  123. # sync
  124. (replace /dev/sdx with you SD card device file name, which could be
  125. /dev/mmcblk[x] as well).
  126. Alternatively you can use the SPL and the U-Boot FIT image combined into a
  127. single file and transfer that instead:
  128. # dd if=u-boot-sunxi-with-spl.bin of=/dev/sdx bs=8k seek=1
  129. You can partition the microSD card, but leave the first MB unallocated (most
  130. partitioning tools will do this anyway).
  131. NOR flash
  132. ---------
  133. Some boards (like the SoPine, Pinebook or the OrangePi PC2) come with a
  134. soldered SPI NOR flash chip. On other boards like the Pine64 such a chip
  135. can be connected to the SPI0/CS0 pins on the PI-2 headers.
  136. Create the SPL and FIT image like described above for the SD card.
  137. Now connect either an "A to A" USB cable to the upper USB port on the Pine64
  138. or get an adaptor and use a regular A-microB cable connected to it. Other
  139. boards often have a proper micro-B USB socket connected to the USB OTB port.
  140. Remove a microSD card from the slot and power on the board.
  141. On your host computer download and build the sunxi-tools package[2], then
  142. use "sunxi-fel" to access the board:
  143. $ ./sunxi-fel ver -v -p
  144. This should give you an output starting with: AWUSBFEX soc=00001689(A64) ...
  145. Now use the sunxi-fel tool to write to the NOR flash:
  146. $ ./sunxi-fel spiflash-write 0 spl/sunxi-spl.bin
  147. $ ./sunxi-fel spiflash-write 32768 u-boot.itb
  148. Now boot the board without an SD card inserted and you should see the
  149. U-Boot prompt on the serial console.
  150. (Legacy) boot0 method
  151. ---------------------
  152. boot0 is Allwinner's secondary program loader and it can be used as some kind
  153. of SPL replacement to get U-Boot up and running from an microSD card.
  154. For some time using boot0 was the only option to get the Pine64 booted.
  155. With working DRAM init code in U-Boot's SPL this is no longer necessary,
  156. but this method is described here for the sake of completeness.
  157. Please note that this method works only with the boot0 files shipped with
  158. A64 based boards, the H5 uses an incompatible layout which is not supported
  159. by this method.
  160. The boot0 binary is a 32 KByte blob and contained in the official Pine64 images
  161. distributed by Pine64 or Allwinner. It can be easily extracted from a micro
  162. SD card or an image file:
  163. # dd if=/dev/sd<x> of=boot0.bin bs=8k skip=1 count=4
  164. where /dev/sd<x> is the device name of the uSD card or the name of the image
  165. file. Apparently Allwinner allows re-distribution of this proprietary code
  166. "as-is".
  167. This boot0 blob takes care of DRAM initialisation and loads the remaining
  168. firmware parts, then switches the core into AArch64 mode.
  169. The original boot0 code looks for U-Boot at a certain place on an uSD card
  170. (at 19096 KB), also it expects a header with magic bytes and a checksum.
  171. There is a tool called boot0img[3] which takes a boot0.bin image and a compiled
  172. U-Boot binary (plus other binaries) and will populate that header accordingly.
  173. To make space for the magic header, the pine64_plus_defconfig will make sure
  174. there is sufficient space at the beginning of the U-Boot binary.
  175. boot0img will also take care of putting the different binaries at the right
  176. places on the uSD card and works around unused, but mandatory parts by using
  177. trampoline code. See the output of "boot0img -h" for more information.
  178. boot0img can also patch boot0 to avoid loading U-Boot from 19MB, instead
  179. fetching it from just behind the boot0 binary (-B option).
  180. $ ./boot0img -o firmware.img -B boot0.img -u u-boot-dtb.bin -e -s bl31.bin \
  181. -a 0x44008 -d trampoline64:0x44000
  182. Then write this image to a microSD card, replacing /dev/sdx with the right
  183. device file (see above):
  184. $ dd if=firmware.img of=/dev/sdx bs=8k seek=1
  185. [1] https://github.com/ARM-software/arm-trusted-firmware.git
  186. [2] git://github.com/linux-sunxi/sunxi-tools.git
  187. [3] https://github.com/apritzel/pine64/