/****************************************************************************
 * boards/arm/samv7/common/scripts/memory.ld.template
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

#include <nuttx/config.h>

#define FLASH_START_ADDR        0x00400000

#ifdef CONFIG_ARMV7M_DTCM
#  define SRAM_START_ADDR       0x20000000
#else
#  define SRAM_START_ADDR       0x20400000
#endif

#define KFLASH_START_ADDR       FLASH_START_ADDR
#define KFLASH_SIZE             (CONFIG_ARCH_CHIP_SAMV7_MEM_FLASH / 2)

#define UFLASH_START_ADDR       (KFLASH_START_ADDR + KFLASH_SIZE)
#define UFLASH_SIZE             (CONFIG_ARCH_CHIP_SAMV7_MEM_FLASH - KFLASH_SIZE)

#define KSRAM_START_ADDR        SRAM_START_ADDR
#ifdef CONFIG_SAMV7_MEM_RAM_384
#  define KSRAM_SIZE            (CONFIG_ARCH_CHIP_SAMV7_MEM_RAM / 3)
#else
#  define KSRAM_SIZE            (CONFIG_ARCH_CHIP_SAMV7_MEM_RAM / 2)
#endif

#define USRAM_START_ADDR        (KSRAM_START_ADDR + KSRAM_SIZE)
#define USRAM_SIZE              ((CONFIG_ARCH_CHIP_SAMV7_MEM_RAM - KSRAM_SIZE) / 2)

#define XSRAM_START_ADDR        (USRAM_START_ADDR + USRAM_SIZE)
#define XSRAM_SIZE              (CONFIG_ARCH_CHIP_SAMV7_MEM_RAM - KSRAM_SIZE - USRAM_SIZE)

/* The SAMV7 can have up to 2048Kb of FLASH beginning at address 0x0040:0000
 * and up to 384Kb of SRAM beginining at 0x2040:0000 or 0x2000:0000
 *
 * When booting from FLASH, FLASH memory is aliased to address 0x0000:0000
 * where the code expects to begin execution by jumping to the entry point in
 * the 0x0400:0000 address range.
 *
 * The user space partition will be spanned with a single region of size
 * 2**n bytes.  The alignment of the user-space region must be the same.
 * As a consequence, as the user-space increases in size, the alignment
 * requirement also increases.  The sizes below give the largest possible
 * user address spaces (but leave far too much for the OS).
 *
 * The solution to this wasted memory is to (1) use more than one region to
 * span the user spaces, or (2) poke holes in a larger region to trim it
 * to fit better.
 *
 * A detailed memory map example for the 384KB SRAM region is as follows:
 *
 *   0x2040 0000:  Kernel .data region.  Typical size: 0.1KB
 *   ------ ----   Kernel .bss  region.  Typical size: 1.8KB
 *   0x2040 0800:  Kernel IDLE thread stack (approximate).  Size is
 *                 determined by CONFIG_IDLETHREAD_STACKSIZE and
 *                 adjustments for alignment.  Typical is 1KB.
 *   ------ ----   Padded to 4KB
 *   0x2042 0000:  User .data region.  Size is variable.
 *   ------- ----  User .bss region  Size is variable.
 *   0x2044 0000:  Beginning of kernel heap.  Size determined by
 *                 CONFIG_MM_KERNEL_HEAPSIZE.
 *   ------ ----   Beginning of user heap.  Can vary with other settings.
 *   0x2046 0000:  End+1 of mappable internal SRAM
 */

MEMORY
{
  /* Internal FLASH */

  kflash (rx)      : ORIGIN = KFLASH_START_ADDR, LENGTH = KFLASH_SIZE
  uflash (rx)      : ORIGIN = UFLASH_START_ADDR, LENGTH = UFLASH_SIZE

  /* Internal SRAM */

  ksram (rwx)      : ORIGIN = KSRAM_START_ADDR, LENGTH = KSRAM_SIZE
  usram (rwx)      : ORIGIN = USRAM_START_ADDR, LENGTH = USRAM_SIZE
  xsram (rwx)      : ORIGIN = XSRAM_START_ADDR, LENGTH = XSRAM_SIZE
}