Skip to content

Commit

Permalink
samv7: add support for user signature area in embedded flash
Browse files Browse the repository at this point in the history
Embedded flash can have user signature area on SAMv7. This is a 512
bytes large page whose data are not erased by asserting ERASE pin or by
software ERASE command.

This commit adds arch to board interface for this area. It is possible
to perform read, write and erase operation. SAMV7_USER_SIGNATURE option
has to be set in the configuration.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
  • Loading branch information
michallenc committed May 28, 2024
1 parent bfa1799 commit 443254e
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 1 deletion.
15 changes: 15 additions & 0 deletions arch/arm/src/samv7/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ config SAMV7_HAVE_EBI
bool
default n

config SAMV7_EEFC
bool
default n

config SAMV7_EMAC
bool
default n
Expand Down Expand Up @@ -1786,6 +1790,7 @@ menuconfig SAMV7_PROGMEM
bool "FLASH program memory"
default n
select ARCH_HAVE_PROGMEM
select SAMV7_EEFC
---help---
Enable support FLASH interfaces as defined in include/nuttx/progmem.h

Expand All @@ -1804,6 +1809,16 @@ config SAMV7_PROGMEM_NSECTORS

endif # SAMV7_PROGMEM

config SAMV7_USER_SIGNATURE
bool "FLASH user signature"
default n
select SAMV7_EEFC
---help---
Each SAMv7 product contains a user signature area of 512 bytes in
embedded flash. It is possible to read, write and erase this area.
Data stored in this area are not erased by asserting ERASE pin or by
software ERASE command.

menu "SDRAM Configuration"
depends on SAMV7_SDRAMC

Expand Down
10 changes: 9 additions & 1 deletion arch/arm/src/samv7/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,16 @@ ifeq ($(CONFIG_SAMV7_TRNG),y)
CHIP_CSRCS += sam_trng.c
endif

ifeq ($(CONFIG_SAMV7_EEFC),y)
CHIP_CSRCS += sam_eefc.c
endif

ifeq ($(CONFIG_SAMV7_PROGMEM),y)
CHIP_CSRCS += sam_progmem.c sam_eefc.c
CHIP_CSRCS += sam_progmem.c
endif

ifeq ($(CONFIG_SAMV7_USER_SIGNATURE),y)
CHIP_CSRCS += sam_us.c
endif

ifeq ($(CONFIG_SAMV7_PWM),y)
Expand Down
172 changes: 172 additions & 0 deletions arch/arm/src/samv7/sam_us.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/****************************************************************************
* arch/arm/src/samv7/sam_us.c
*
* 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.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <errno.h>

#include <nuttx/arch.h>
#include <nuttx/mutex.h>
#include <arch/samv7/chip.h>

#include "hardware/sam_memorymap.h"

#include "sam_eefc.h"
#include "sam_us.h"

#ifdef CONFIG_SAMV7_USER_SIGNATURE

#define SAMV7_US_START (SAM_INTFLASH_BASE)
#define SAMV7_US_SIZE (512)
#define SAMV7_US_PAGE_WORDS (SAMV7_US_SIZE / sizeof(uint32_t))

/****************************************************************************
* Private Data
****************************************************************************/

static uint32_t g_page_buffer[SAMV7_US_PAGE_WORDS];
static mutex_t g_page_lock = NXMUTEX_INITIALIZER;

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: sam_erase_user_signature
*
* Description:
* Erases user signature page.
*
* Returned Value:
* Zero on success, negated errno value on error.
*
****************************************************************************/

int sam_erase_user_signature(void)
{
int ret;

ret = sam_eefc_command(EEFC_FCR_FCMD_EUS, 0);
if (ret < 0)
{
return ret;
}

return 0;
}

/****************************************************************************
* Name: sam_write_user_signature
*
* Description:
* Writes data to user signature page.
*
* Input Parameters:
* buffer - The buffer to be written to user signature.
* buflen - Number of bytes to be written.
*
* Returned Value:
* Number of written bytes on success, negated errno on error.
*
****************************************************************************/

int sam_write_user_signature(void *buffer, size_t buflen)
{
uint32_t *dest;
int ret;

if (buflen > SAMV7_US_SIZE)
{
return -ENOMEM;
}

ret = nxmutex_lock(&g_page_lock);
if (ret < 0)
{
return ret;
}

memcpy((uint8_t *)g_page_buffer, buffer, buflen);

dest = (uint32_t *)SAMV7_US_START;
for (int i = 0; i < SAMV7_US_PAGE_WORDS; i++)
{
*dest++ = g_page_buffer[i];

#ifdef CONFIG_ARMV7M_DCACHE_WRITETHROUGH
ARM_DMB();
#endif
}

/* Flush the data cache to memory */

up_clean_dcache(SAMV7_US_START, SAMV7_US_START + SAMV7_US_SIZE);

ret = sam_eefc_command(EEFC_FCR_FCMD_WUS, 0);
if (ret < 0)
{
nxmutex_unlock(&g_page_lock);
return ret;
}

nxmutex_unlock(&g_page_lock);
return buflen;
}

/****************************************************************************
* Name: sam_get_user_signature
*
* Description:
* Get bytes from user signature area.
*
* Input Parameters:
* buffer - The buffer to store user signature.
* buflen - Number of bytes to be read.
*
* Returned Value:
* NONE.
*
****************************************************************************/

int sam_read_user_signature(void *buffer, size_t buflen)
{
size_t nwords;
int ret;

ret = nxmutex_lock(&g_page_lock);
if (ret < 0)
{
return ret;
}

nwords = (buflen + sizeof(uint32_t) / sizeof(uint32_t));
sam_eefc_readsequence(FCMD_STUS, FCMD_SPUS, g_page_buffer, nwords);

memcpy(buffer, (uint8_t *)g_page_buffer, buflen);

nxmutex_unlock(&g_page_lock);
return buflen;
}

#endif /* CONFIG_SAMV7_USER_SIGNATURE */
81 changes: 81 additions & 0 deletions arch/arm/src/samv7/sam_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/****************************************************************************
* arch/arm/src/samv7/sam_us.h
*
* 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.
*
****************************************************************************/

#ifndef __ARCH_ARM_SRC_SAMV7_SAM_US_H
#define __ARCH_ARM_SRC_SAMV7_SAM_US_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <stdint.h>

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

/****************************************************************************
* Name: sam_erase_user_signature
*
* Description:
* Erases user signature page.
*
* Returned Value:
* Zero on success, negated errno value on error.
*
****************************************************************************/

int sam_erase_user_signature(void);

/****************************************************************************
* Name: sam_write_user_signature
*
* Description:
* Writes data to user signature page.
*
* Input Parameters:
* buffer - The buffer to be written to user signature.
* buflen - Number of bytes to be written.
*
* Returned Value:
* Number of written bytes on success, negated errno on error.
*
****************************************************************************/

int sam_write_user_signature(void *buffer, size_t buflen);

/****************************************************************************
* Name: sam_get_user_signature
*
* Description:
* Get bytes from user signature area.
*
* Input Parameters:
* buffer - The buffer to store user signature.
* buflen - Number of bytes to be read.
*
* Returned Value:
* NONE.
*
****************************************************************************/

int sam_read_user_signature(void *buffer, size_t buflen);

#endif /* __ARCH_ARM_SRC_SAMV7_SAM_US_H */

0 comments on commit 443254e

Please sign in to comment.