PinePhone: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
No edit summary
Line 12: Line 12:
* https://linux-sunxi.org/File:Allwinner_A64_User_Manual_V1.1.pdf - includes memory map; full register documentation for several basic peripherals including GPIOs, DMA, timers...
* https://linux-sunxi.org/File:Allwinner_A64_User_Manual_V1.1.pdf - includes memory map; full register documentation for several basic peripherals including GPIOs, DMA, timers...
* https://wiki.pine64.org/index.php?title=PinePhone#PinePhone_board_information,_schematics_and_certifications
* https://wiki.pine64.org/index.php?title=PinePhone#PinePhone_board_information,_schematics_and_certifications

The CPU is an [https://linux-sunxi.org/A64 Allwinner A64] (a.k.a. "sun50i"), which is also used in some of Pine64's single-board computers. Although Allwinner does not release full documentation, significant reverse-engineered information is available through the [https://linux-sunxi.org/ linux-sunxi project]. This is presumably one reason Pine64 chose this CPU.


== Requirements ==
== Requirements ==
Line 21: Line 23:
Since the OS controls all signs of whether the phone is powered on, it's probably a good idea to remove the battery, so that you can be sure whether the phone is on or off. Also, some aspects of battery charging are controlled by the OS, so this removes any chance of accidentally damaging it by applying incorrect settings (not sure if this is possible/likely). The cellular modem is powered directly from the battery and won't work if the battery is not inserted - but this won't be a problem until your OS is quite well developed. The CPU seems to work just fine while the battery is removed.
Since the OS controls all signs of whether the phone is powered on, it's probably a good idea to remove the battery, so that you can be sure whether the phone is on or off. Also, some aspects of battery charging are controlled by the OS, so this removes any chance of accidentally damaging it by applying incorrect settings (not sure if this is possible/likely). The cellular modem is powered directly from the battery and won't work if the battery is not inserted - but this won't be a problem until your OS is quite well developed. The CPU seems to work just fine while the battery is removed.


== Boot order ==
== Booting ==

The CPU is an [https://linux-sunxi.org/A64 Allwinner A64] (a.k.a. "sun50i"), which is also used in some of Pine64's single-board computers. Although Allwinner does not release full documentation, significant reverse-engineered information is available through the [https://linux-sunxi.org/ linux-sunxi project]. This is presumably one reason Pine64 chose this CPU.


The CPU first boots from an integrated boot ROM, which then tries to boot from one of these sources:[https://linux-sunxi.org/Boot_Process]
The CPU first boots from an integrated boot ROM, which then tries to boot from one of these sources:[https://linux-sunxi.org/Boot_Process]
Line 30: Line 30:
# The internal eMMC storage, if boot signature (eGON header) is found
# The internal eMMC storage, if boot signature (eGON header) is found
# The FEL mode ROM - a USB slave mode where the host computer can read, write and execute memory.
# The FEL mode ROM - a USB slave mode where the host computer can read, write and execute memory.



There is also an FEL button that can theoretically be pressed to force boot in FEL mode, but on the PinePhone it is only connected to a test pad, not to an actual button.
There is also an FEL button that can theoretically be pressed to force boot in FEL mode, but on the PinePhone it is only connected to a test pad, not to an actual button.



The normal way to prepare an OS for the PinePhone is to flash a microSD card and insert it. The OS may then offer an option to install itself onto the eMMC. However, when getting started with osdev, FEL mode (which allows loading code via USB) should allow for a much faster testing cycle.
The normal way to prepare an OS for the PinePhone is to flash a microSD card and insert it. The OS may then offer an option to install itself onto the eMMC. However, when getting started with osdev, FEL mode (which allows loading code via USB) should allow for a much faster testing cycle.

For your OS, you can choose to boot via U-boot [TODO] or directly into your own "bootloader".


=== Loading code via microSD/eMMC ===
=== Loading code via microSD/eMMC ===
Line 78: Line 78:
To see if your toolchain works you can try to run [[User:Immibis/PinePhone Blinky]].
To see if your toolchain works you can try to run [[User:Immibis/PinePhone Blinky]].


== Bootloader stuff ==
== Custom bootloader stuff ==


Skip this section if you are running your OS from U-Boot.
Skip this section if you are running your OS from U-Boot.

Revision as of 20:06, 12 August 2022

This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.

The PinePhone is an open hardware smartphone from Pine64. It features all the stuff you'd expect from a modern smartphone, although don't expect the latest and greatest variants of each. Full schematics are available on the Pine64 wiki. Unfortunately, open hardware does not mean that full datasheets for all chips are available.

Beware the slight but incompatible changes between different hardware revisions - such as different GPIO pins being used. This page is written about the Community Edition with the v1.2b mainboard, the latest as of writing.

The PinePhone Pro is a different device with similar but incompatible hardware! (such as an entirely different CPU chip from Rockchip)

Resources

The CPU is an Allwinner A64 (a.k.a. "sun50i"), which is also used in some of Pine64's single-board computers. Although Allwinner does not release full documentation, significant reverse-engineered information is available through the linux-sunxi project. This is presumably one reason Pine64 chose this CPU.

Requirements

To write an SD/eMMC image you need the mksunxiboot tool from the u-boot-tools package (may vary by distribution). [TODO: or you could just write the header yourself]

To use FEL you also need the sunxi-fel tool from the sunxi-tools package.

Since the OS controls all signs of whether the phone is powered on, it's probably a good idea to remove the battery, so that you can be sure whether the phone is on or off. Also, some aspects of battery charging are controlled by the OS, so this removes any chance of accidentally damaging it by applying incorrect settings (not sure if this is possible/likely). The cellular modem is powered directly from the battery and won't work if the battery is not inserted - but this won't be a problem until your OS is quite well developed. The CPU seems to work just fine while the battery is removed.

Booting

The CPU first boots from an integrated boot ROM, which then tries to boot from one of these sources:[1]

  1. The microSD card, if present and boot signature (eGON header) is found
  2. The internal eMMC storage, if boot signature (eGON header) is found
  3. The FEL mode ROM - a USB slave mode where the host computer can read, write and execute memory.

There is also an FEL button that can theoretically be pressed to force boot in FEL mode, but on the PinePhone it is only connected to a test pad, not to an actual button.

The normal way to prepare an OS for the PinePhone is to flash a microSD card and insert it. The OS may then offer an option to install itself onto the eMMC. However, when getting started with osdev, FEL mode (which allows loading code via USB) should allow for a much faster testing cycle.

For your OS, you can choose to boot via U-boot [TODO] or directly into your own "bootloader".

Loading code via microSD/eMMC

Section TODO

Boot signature / eGON header starts at address 8192 and occupies (theoretically) up to 32KB. This region is often left unallocated; if you're low on microSD cards, you might be able to share one with a Raspberry Pi project for example, without constantly re-flashing it.

Loading code via FEL

Accessing FEL

FEL is executed if:

  • You have no valid bootloader installed (eGON header not found) on microSD card nor eMMC
  • You press the FEL key (not applicable to Pinephone)
  • You call the FEL boot code directly

Third option is possible by writing a tiny stub to a microSD card. This is available in sunxi-tools by the name of fel-sdboot. Run make fel-sdboot.sunxi. It makes an 8kB file but only the first 124 bytes are used. Write the file to offset 8192 on any microSD card:

   sudo dd if=fel-sdboot.sunxi of=/dev/NAME_OF_SD_CARD_DEVICE bs=8192 seek=1 count=1

and you can insert this microSD card to trigger FEL mode.

NOTE: When in FEL mode, the phone provides absolutely no visible sign that it is turned on. You can check by running lsusb on the host computer - you should see a USB device like this one:

   Bus 001 Device 012: ID 1f3a:efe8 Allwinner Technology sunxi SoC OTG connector in FEL/flashing mode

and you can verify it by running sunxi-fel version:

   sunxi-tools$ ./sunxi-fel version
   AWUSBFEX soc=00001689(A64) 00000001 ver=0001 44 08 scratchpad=00007e00 00000000 00000000

Loading code example

   sunxi-fel write 0x00010000 code.bin
   sunxi-fel execute 0x00010000

Why 0x00010000? According to the memory map this region 0x00010000-0x00017FFF is where the boot ROM normally loads user code, which means it shouldn't conflict with anything used by the boot ROM (e.g. to support the USB interface).

Your code can execute BX LR to return back to the boot loader.

To see if your toolchain works you can try to run User:Immibis/PinePhone Blinky.

Custom bootloader stuff

Skip this section if you are running your OS from U-Boot.

You might notice accessing the 2GB/3GB (depending on model) DRAM area at address 0x40000000 upwards causes the processor to hang, so you only have the SRAM areas (a few hundred kB) to play with. That's because the boot ROM doesn't initialize the DRAM controller. It only uses ROM and SRAM. On a PC, the BIOS would have initialized the memory for you, but this is an embedded system, not a PC. The bootloader is the BIOS, and you are the bootloader.

See PinePhone/DRAM initialization