.. SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
.. sectionauthor:: Anshul Dalal <anshuld@ti.com>

AM6254ATL SIP Platforms
=======================

Introduction:
-------------

The AM6254atl SiP (System In Package) provides existing :doc:`am62x_sk` with
512MiB of DDR integrated in a single packages.

More details can be found in the Technical Reference Manual:
https://www.ti.com/lit/pdf/spruiv7

Platform information:

* https://www.ti.com/tool/SK-AM62-SIP

Boot Flow:
----------
Below is the pictorial representation of boot flow:

.. image:: img/boot_diagram_am62.svg
  :alt: Boot flow diagram

- Here TIFS acts as master and provides all the critical services. R5/A53
  requests TIFS to get these services done as shown in the above diagram.

Sources:
--------

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_boot_sources
    :end-before: .. k3_rst_include_end_boot_sources

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_boot_firmwares
    :end-before: .. k3_rst_include_end_tifsstub

Build procedure:
----------------
0. Setup the environment variables:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_common_env_vars_desc
    :end-before: .. k3_rst_include_end_common_env_vars_desc

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_board_env_vars_desc
    :end-before: .. k3_rst_include_end_board_env_vars_desc

Set the variables corresponding to this platform:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_common_env_vars_defn
    :end-before: .. k3_rst_include_end_common_env_vars_defn
.. prompt:: bash $

  export UBOOT_CFG_CORTEXR=am6254atl_evm_r5_defconfig
  export UBOOT_CFG_CORTEXA=am6254atl_evm_a53_defconfig
  export TFA_BOARD=lite
  export TFA_EXTRA_ARGS="PRELOADED_BL33_BASE=0x82000000 BL32_BASE=0x80080000"
  export OPTEE_PLATFORM=k3-am62x
  export OPTEE_EXTRA_ARGS="CFG_TZDRAM_START=0x80080000"

1. Trusted Firmware-A:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_build_steps_tfa
    :end-before: .. k3_rst_include_end_build_steps_tfa


2. OP-TEE:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_build_steps_optee
    :end-before: .. k3_rst_include_end_build_steps_optee

3. U-Boot:

* 3.1 R5:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_build_steps_spl_r5
    :end-before: .. k3_rst_include_end_build_steps_spl_r5

* 3.1.1 Alternative build of R5 for DFU boot:

As the SPL size can get too big when building with support for booting both
from local storage *and* DFU an extra config fragment should be used to enable
DFU support (and disable storage support)

.. prompt:: bash $

  export UBOOT_CFG_CORTEXR="${UBOOT_CFG_CORTEXR} am62x_r5_usbdfu.config"

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_build_steps_spl_r5
    :end-before: .. k3_rst_include_end_build_steps_spl_r5

* 3.2 A53:

.. include::  ../ti/k3.rst
    :start-after: .. k3_rst_include_start_build_steps_uboot
    :end-before: .. k3_rst_include_end_build_steps_uboot

Target Images
-------------

In order to boot we need tiboot3.bin, tispl.bin and u-boot.img.  Each SoC
variant (HS-FS or HS-SE) requires a different source for these files.

 - HS-FS

        * tiboot3-am62x-hs-fs-evm.bin from step 3.1
        * tispl.bin, u-boot.img from step 3.2

 - HS-SE

        * tiboot3-am62x-hs-evm.bin from step 3.1
        * tispl.bin, u-boot.img from step 3.2

Image formats:
--------------

- tiboot3.bin

.. image:: img/multi_cert_tiboot3.bin.svg
  :alt: tiboot3.bin image format

- tispl.bin

.. image:: img/tifsstub_dm_tispl.bin.svg
  :alt: tispl.bin image format

OSPI:
-----
ROM supports booting from OSPI from offset 0x0.

Flashing images to OSPI:

Below commands can be used to download tiboot3.bin, tispl.bin, and u-boot.img,
over tftp and then flash those to OSPI at their respective addresses.

.. prompt:: bash =>

  sf probe
  tftp ${loadaddr} tiboot3.bin
  sf update $loadaddr 0x0 $filesize
  tftp ${loadaddr} tispl.bin
  sf update $loadaddr 0x80000 $filesize
  tftp ${loadaddr} u-boot.img
  sf update $loadaddr 0x280000 $filesize

Flash layout for OSPI:

.. image:: img/ospi_sysfw2.svg
  :alt: OSPI flash partition layout

A53 SPL DDR Memory Layout
-------------------------

This provides an overview memory usage in A53 SPL stage.

.. list-table::
   :widths: 16 16 16
   :header-rows: 1

   * - Region
     - Start Address
     - End Address

   * - ATF
     - 0x80000000
     - 0x80080000

   * - OPTEE
     - 0x80080000
     - 0x81880000

   * - TEXT BASE
     - 0x81880000
     - 0x818d8000

   * - EMPTY
     - 0x818d8000
     - 0x81a00000

   * - BMP IMAGE
     - 0x81a00000
     - 0x82377660

   * - STACK
     - 0x82377660
     - 0x82377e60

   * - GD
     - 0x82377e60
     - 0x80b78000

   * - MALLOC
     - 0x82378000
     - 0x82380000

   * - EMPTY
     - 0x82380000
     - 0x82480000

   * - BSS
     - 0x82480000
     - 0x82500000

   * - BLOBS
     - 0x82500000
     - 0x82500400

   * - EMPTY
     - 0x82500400
     - 0x82800000

Switch Setting for Boot Mode
----------------------------

Boot Mode pins provide means to select the boot mode and options before the
device is powered up. After every POR, they are the main source to populate
the Boot Parameter Tables.

The following table shows some common boot modes used on AM62 platform. More
details can be found in the Technical Reference Manual:
https://www.ti.com/lit/pdf/spruiv7 under the `Boot Mode Pins` section.

.. list-table:: Boot Modes
   :widths: 16 16 16
   :header-rows: 1

   * - Switch Label
     - SW2: 12345678
     - SW3: 12345678

   * - SD
     - 01000000
     - 11000010

   * - OSPI
     - 00000000
     - 11001110

   * - EMMC
     - 00000000
     - 11010010

   * - UART
     - 00000000
     - 11011100

   * - USB DFU
     - 00000000
     - 11001010

   * - Ethernet
     - 00110000
     - 11000100

For SW2 and SW1, the switch state in the "ON" position = 1.

DFU based boot
--------------

To boot the board over DFU, set the switches to DFU mode and connect to the
USB type C DRD port on the board. After power-on the build artifacts needs to be
uploaded one by one with a tool like dfu-util.

The initial ROM will have a DFU alt named `bootloader` for the initial R5 spl
upload. The next stages as exposed by U-Boot have target alts matching the name
of the artifacts, for these a USB reset has to be done after each upload.

When using dfu-util the following commands can be used to boot to a U-Boot shell:

.. prompt:: bash $

  dfu-util -a bootloader -D tiboot3.bin
  dfu-util -R -a tispl -D tispl.bin
  dfu-util -R -a u-boot.img -D u-boot.img

Debugging U-Boot
----------------

See :ref:`Common Debugging environment - OpenOCD<k3_rst_refer_openocd>`: for
detailed setup information.

.. warning::

  **OpenOCD support since**: v0.12.0

  If the default package version of OpenOCD in your development
  environment's distribution needs to be updated, it might be necessary to
  build OpenOCD from the source.

.. include::  k3.rst
    :start-after: .. k3_rst_include_start_openocd_connect_XDS110
    :end-before: .. k3_rst_include_end_openocd_connect_XDS110

To start OpenOCD and connect to the board

.. prompt:: bash $

  openocd -f board/ti_am625evm.cfg
