2018-05-17 13:24:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2018, STMicroelectronics - All Rights Reserved
|
|
|
|
*/
|
|
|
|
|
2020-11-06 18:01:31 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_MISC
|
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
#include <common.h>
|
2021-02-25 12:43:07 +00:00
|
|
|
#include <clk.h>
|
2018-05-17 13:24:05 +00:00
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2018-05-17 13:24:05 +00:00
|
|
|
#include <misc.h>
|
|
|
|
#include <asm/io.h>
|
2020-06-16 16:27:44 +00:00
|
|
|
#include <asm/arch/bsec.h>
|
2019-02-12 10:44:40 +00:00
|
|
|
#include <asm/arch/stm32mp1_smc.h>
|
2020-11-06 18:01:31 +00:00
|
|
|
#include <dm/device_compat.h>
|
2019-02-12 10:44:40 +00:00
|
|
|
#include <linux/arm-smccc.h>
|
2019-04-18 15:32:39 +00:00
|
|
|
#include <linux/iopoll.h>
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
#define BSEC_OTP_MAX_VALUE 95
|
|
|
|
#define BSEC_TIMEOUT_US 10000
|
|
|
|
|
|
|
|
/* BSEC REGISTER OFFSET (base relative) */
|
|
|
|
#define BSEC_OTP_CONF_OFF 0x000
|
|
|
|
#define BSEC_OTP_CTRL_OFF 0x004
|
|
|
|
#define BSEC_OTP_WRDATA_OFF 0x008
|
|
|
|
#define BSEC_OTP_STATUS_OFF 0x00C
|
|
|
|
#define BSEC_OTP_LOCK_OFF 0x010
|
2020-06-16 16:27:44 +00:00
|
|
|
#define BSEC_DENABLE_OFF 0x014
|
2018-05-17 13:24:05 +00:00
|
|
|
#define BSEC_DISTURBED_OFF 0x01C
|
|
|
|
#define BSEC_ERROR_OFF 0x034
|
2020-02-12 18:37:38 +00:00
|
|
|
#define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */
|
|
|
|
#define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */
|
|
|
|
#define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */
|
|
|
|
#define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */
|
2018-05-17 13:24:05 +00:00
|
|
|
#define BSEC_OTP_DATA_OFF 0x200
|
|
|
|
|
|
|
|
/* BSEC_CONFIGURATION Register MASK */
|
|
|
|
#define BSEC_CONF_POWER_UP 0x001
|
|
|
|
|
|
|
|
/* BSEC_CONTROL Register */
|
|
|
|
#define BSEC_READ 0x000
|
|
|
|
#define BSEC_WRITE 0x100
|
|
|
|
|
|
|
|
/* LOCK Register */
|
|
|
|
#define OTP_LOCK_MASK 0x1F
|
|
|
|
#define OTP_LOCK_BANK_SHIFT 0x05
|
|
|
|
#define OTP_LOCK_BIT_MASK 0x01
|
|
|
|
|
|
|
|
/* STATUS Register */
|
|
|
|
#define BSEC_MODE_BUSY_MASK 0x08
|
|
|
|
#define BSEC_MODE_PROGFAIL_MASK 0x10
|
|
|
|
#define BSEC_MODE_PWR_MASK 0x20
|
|
|
|
|
2020-06-16 16:27:44 +00:00
|
|
|
/* DENABLE Register */
|
|
|
|
#define BSEC_DENABLE_DBGSWENABLE BIT(10)
|
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
/*
|
|
|
|
* OTP Lock services definition
|
|
|
|
* Value must corresponding to the bit number in the register
|
|
|
|
*/
|
|
|
|
#define BSEC_LOCK_PROGRAM 0x04
|
|
|
|
|
|
|
|
/**
|
2020-02-12 18:37:38 +00:00
|
|
|
* bsec_lock() - manage lock for each type SR/SP/SW
|
|
|
|
* @address: address of bsec IP register
|
2018-05-17 13:24:05 +00:00
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
2020-02-12 18:37:38 +00:00
|
|
|
* Return: true if locked else false
|
2018-05-17 13:24:05 +00:00
|
|
|
*/
|
2020-02-12 18:37:38 +00:00
|
|
|
static bool bsec_read_lock(u32 address, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
u32 bit;
|
|
|
|
u32 bank;
|
|
|
|
|
|
|
|
bit = 1 << (otp & OTP_LOCK_MASK);
|
|
|
|
bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
return !!(readl(address + bank) & bit);
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-12 18:37:38 +00:00
|
|
|
* bsec_check_error() - Check status of one otp
|
|
|
|
* @base: base address of bsec IP
|
2018-05-17 13:24:05 +00:00
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
2020-02-12 18:37:38 +00:00
|
|
|
* Return: 0 if no error, -EAGAIN or -ENOTSUPP
|
2018-05-17 13:24:05 +00:00
|
|
|
*/
|
2020-02-12 18:37:38 +00:00
|
|
|
static u32 bsec_check_error(u32 base, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
u32 bit;
|
|
|
|
u32 bank;
|
|
|
|
|
|
|
|
bit = 1 << (otp & OTP_LOCK_MASK);
|
|
|
|
bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
|
|
|
|
return -EAGAIN;
|
|
|
|
else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
return 0;
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_read_SR_lock() - read SR lock (Shadowing)
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: true if locked else false
|
|
|
|
*/
|
|
|
|
static bool bsec_read_SR_lock(u32 base, u32 otp)
|
|
|
|
{
|
|
|
|
return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_read_SP_lock() - read SP lock (program Lock)
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: true if locked else false
|
|
|
|
*/
|
|
|
|
static bool bsec_read_SP_lock(u32 base, u32 otp)
|
|
|
|
{
|
|
|
|
return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_SW_lock() - manage SW lock (Write in Shadow)
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: true if locked else false
|
|
|
|
*/
|
|
|
|
static bool bsec_read_SW_lock(u32 base, u32 otp)
|
|
|
|
{
|
|
|
|
return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_power_safmem() - Activate or deactivate safmem power
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @power: true to power up , false to power down
|
|
|
|
* Return: 0 if succeed
|
|
|
|
*/
|
|
|
|
static int bsec_power_safmem(u32 base, bool power)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
u32 mask;
|
|
|
|
|
|
|
|
if (power) {
|
|
|
|
setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
|
|
|
|
mask = BSEC_MODE_PWR_MASK;
|
|
|
|
} else {
|
|
|
|
clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
|
|
|
|
mask = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* waiting loop */
|
|
|
|
return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
|
|
|
|
val, (val & BSEC_MODE_PWR_MASK) == mask,
|
|
|
|
BSEC_TIMEOUT_US);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_shadow_register() - copy safmen otp to bsec data
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: 0 if no error
|
|
|
|
*/
|
2020-11-06 18:01:31 +00:00
|
|
|
static int bsec_shadow_register(struct udevice *dev, u32 base, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
bool power_up = false;
|
|
|
|
|
|
|
|
/* check if shadowing of otp is locked */
|
|
|
|
if (bsec_read_SR_lock(base, otp))
|
2020-11-06 18:01:31 +00:00
|
|
|
dev_dbg(dev, "OTP %d is locked and refreshed with 0\n",
|
|
|
|
otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
/* check if safemem is power up */
|
|
|
|
val = readl(base + BSEC_OTP_STATUS_OFF);
|
|
|
|
if (!(val & BSEC_MODE_PWR_MASK)) {
|
|
|
|
ret = bsec_power_safmem(base, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-02-27 16:01:28 +00:00
|
|
|
power_up = true;
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
/* set BSEC_OTP_CTRL_OFF with the otp value*/
|
|
|
|
writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
|
|
|
|
|
|
|
|
/* check otp status*/
|
|
|
|
ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
|
|
|
|
val, (val & BSEC_MODE_BUSY_MASK) == 0,
|
|
|
|
BSEC_TIMEOUT_US);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = bsec_check_error(base, otp);
|
|
|
|
|
|
|
|
if (power_up)
|
|
|
|
bsec_power_safmem(base, false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_read_shadow() - read an otp data value from shadow
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @val: read value
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: 0 if no error
|
|
|
|
*/
|
2020-11-06 18:01:31 +00:00
|
|
|
static int bsec_read_shadow(struct udevice *dev, u32 base, u32 *val, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
*val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
|
|
|
|
|
|
|
|
return bsec_check_error(base, otp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_write_shadow() - write value in BSEC data register in shadow
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @val: value to write
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* Return: 0 if no error
|
|
|
|
*/
|
2020-11-06 18:01:31 +00:00
|
|
|
static int bsec_write_shadow(struct udevice *dev, u32 base, u32 val, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
/* check if programming of otp is locked */
|
|
|
|
if (bsec_read_SW_lock(base, otp))
|
2020-11-06 18:01:31 +00:00
|
|
|
dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
|
|
|
|
|
|
|
|
return bsec_check_error(base, otp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bsec_program_otp() - program a bit in SAFMEM
|
|
|
|
* @base: base address of bsec IP
|
|
|
|
* @val: value to program
|
|
|
|
* @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
|
|
|
|
* after the function the otp data is not refreshed in shadow
|
|
|
|
* Return: 0 if no error
|
|
|
|
*/
|
2020-11-06 18:01:31 +00:00
|
|
|
static int bsec_program_otp(struct udevice *dev, long base, u32 val, u32 otp)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
|
|
|
u32 ret;
|
|
|
|
bool power_up = false;
|
|
|
|
|
|
|
|
if (bsec_read_SP_lock(base, otp))
|
2020-11-06 18:01:31 +00:00
|
|
|
dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
|
2020-11-06 18:01:31 +00:00
|
|
|
dev_dbg(dev, "Global lock, prog will be ignore\n");
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
/* check if safemem is power up */
|
|
|
|
if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
|
|
|
|
ret = bsec_power_safmem(base, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
power_up = true;
|
|
|
|
}
|
|
|
|
/* set value in write register*/
|
|
|
|
writel(val, base + BSEC_OTP_WRDATA_OFF);
|
|
|
|
|
|
|
|
/* set BSEC_OTP_CTRL_OFF with the otp value */
|
|
|
|
writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
|
|
|
|
|
|
|
|
/* check otp status*/
|
|
|
|
ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
|
|
|
|
val, (val & BSEC_MODE_BUSY_MASK) == 0,
|
|
|
|
BSEC_TIMEOUT_US);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (val & BSEC_MODE_PROGFAIL_MASK)
|
|
|
|
ret = -EACCES;
|
|
|
|
else
|
|
|
|
ret = bsec_check_error(base, otp);
|
|
|
|
|
|
|
|
if (power_up)
|
|
|
|
bsec_power_safmem(base, false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BSEC MISC driver *******************************************************/
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat {
|
2018-05-17 13:24:05 +00:00
|
|
|
u32 base;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2018-05-17 13:24:05 +00:00
|
|
|
u32 tmp_data = 0;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-31 14:31:51 +00:00
|
|
|
if (IS_ENABLED(CONFIG_TFABOOT))
|
|
|
|
return stm32_smc(STM32_SMC_BSEC,
|
|
|
|
STM32_SMC_READ_OTP,
|
|
|
|
otp, 0, val);
|
|
|
|
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2020-07-31 14:31:51 +00:00
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
/* read current shadow value */
|
2020-11-06 18:01:31 +00:00
|
|
|
ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* copy otp in shadow */
|
2020-11-06 18:01:31 +00:00
|
|
|
ret = bsec_shadow_register(dev, plat->base, otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-11-06 18:01:31 +00:00
|
|
|
ret = bsec_read_shadow(dev, plat->base, val, otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* restore shadow value */
|
2020-11-06 18:01:31 +00:00
|
|
|
ret = bsec_write_shadow(dev, plat->base, tmp_data, otp);
|
2020-07-31 14:31:51 +00:00
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2020-07-31 14:31:51 +00:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_TFABOOT))
|
|
|
|
return stm32_smc(STM32_SMC_BSEC,
|
|
|
|
STM32_SMC_READ_SHADOW,
|
|
|
|
otp, 0, val);
|
|
|
|
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
2020-11-06 18:01:31 +00:00
|
|
|
return bsec_read_shadow(dev, plat->base, val, otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
|
2020-02-12 18:37:38 +00:00
|
|
|
|
|
|
|
/* return OTP permanent write lock status */
|
|
|
|
*val = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2020-07-31 14:31:51 +00:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_TFABOOT))
|
|
|
|
return stm32_smc_exec(STM32_SMC_BSEC,
|
|
|
|
STM32_SMC_PROG_OTP,
|
|
|
|
otp, val);
|
|
|
|
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
2020-11-06 18:01:31 +00:00
|
|
|
return bsec_program_otp(dev, plat->base, val, otp);
|
2020-07-31 14:31:51 +00:00
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2020-07-31 14:31:51 +00:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_TFABOOT))
|
|
|
|
return stm32_smc_exec(STM32_SMC_BSEC,
|
|
|
|
STM32_SMC_WRITE_SHADOW,
|
|
|
|
otp, val);
|
|
|
|
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
2020-11-06 18:01:31 +00:00
|
|
|
return bsec_write_shadow(dev, plat->base, val, otp);
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
|
|
|
|
{
|
2020-07-31 14:31:51 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_TFABOOT))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (val == 1)
|
|
|
|
return stm32_smc_exec(STM32_SMC_BSEC,
|
|
|
|
STM32_SMC_WRLOCK_OTP,
|
|
|
|
otp, 0);
|
|
|
|
if (val == 0)
|
|
|
|
return 0; /* nothing to do */
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
static int stm32mp_bsec_read(struct udevice *dev, int offset,
|
|
|
|
void *buf, int size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
2020-02-12 18:37:38 +00:00
|
|
|
bool shadow = true, lock = false;
|
2018-05-17 13:24:05 +00:00
|
|
|
int nb_otp = size / sizeof(u32);
|
|
|
|
int otp;
|
2019-06-21 13:26:43 +00:00
|
|
|
unsigned int offs = offset;
|
2018-05-17 13:24:05 +00:00
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (offs >= STM32_BSEC_LOCK_OFFSET) {
|
|
|
|
offs -= STM32_BSEC_LOCK_OFFSET;
|
|
|
|
lock = true;
|
|
|
|
} else if (offs >= STM32_BSEC_OTP_OFFSET) {
|
2019-06-21 13:26:43 +00:00
|
|
|
offs -= STM32_BSEC_OTP_OFFSET;
|
2018-05-17 13:24:05 +00:00
|
|
|
shadow = false;
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:37:37 +00:00
|
|
|
if ((offs % 4) || (size % 4))
|
2018-05-17 13:24:05 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-08-02 11:08:02 +00:00
|
|
|
otp = offs / sizeof(u32);
|
|
|
|
|
|
|
|
for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) {
|
2018-05-17 13:24:05 +00:00
|
|
|
u32 *addr = &((u32 *)buf)[i - otp];
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (lock)
|
|
|
|
ret = stm32mp_bsec_read_lock(dev, addr, i);
|
|
|
|
else if (shadow)
|
2018-05-17 13:24:05 +00:00
|
|
|
ret = stm32mp_bsec_read_shadow(dev, addr, i);
|
|
|
|
else
|
|
|
|
ret = stm32mp_bsec_read_otp(dev, addr, i);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
2019-08-02 11:08:02 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return (i - otp) * 4;
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32mp_bsec_write(struct udevice *dev, int offset,
|
|
|
|
const void *buf, int size)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
2020-02-12 18:37:38 +00:00
|
|
|
bool shadow = true, lock = false;
|
2018-05-17 13:24:05 +00:00
|
|
|
int nb_otp = size / sizeof(u32);
|
|
|
|
int otp;
|
2019-06-21 13:26:43 +00:00
|
|
|
unsigned int offs = offset;
|
2018-05-17 13:24:05 +00:00
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (offs >= STM32_BSEC_LOCK_OFFSET) {
|
|
|
|
offs -= STM32_BSEC_LOCK_OFFSET;
|
|
|
|
lock = true;
|
|
|
|
} else if (offs >= STM32_BSEC_OTP_OFFSET) {
|
2019-06-21 13:26:43 +00:00
|
|
|
offs -= STM32_BSEC_OTP_OFFSET;
|
2018-05-17 13:24:05 +00:00
|
|
|
shadow = false;
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:37:37 +00:00
|
|
|
if ((offs % 4) || (size % 4))
|
2018-05-17 13:24:05 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-08-02 11:08:02 +00:00
|
|
|
otp = offs / sizeof(u32);
|
|
|
|
|
|
|
|
for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) {
|
2018-05-17 13:24:05 +00:00
|
|
|
u32 *val = &((u32 *)buf)[i - otp];
|
|
|
|
|
2020-02-12 18:37:38 +00:00
|
|
|
if (lock)
|
|
|
|
ret = stm32mp_bsec_write_lock(dev, *val, i);
|
|
|
|
else if (shadow)
|
2018-05-17 13:24:05 +00:00
|
|
|
ret = stm32mp_bsec_write_shadow(dev, *val, i);
|
|
|
|
else
|
|
|
|
ret = stm32mp_bsec_write_otp(dev, *val, i);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
2019-08-02 11:08:02 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return (i - otp) * 4;
|
2018-05-17 13:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct misc_ops stm32mp_bsec_ops = {
|
|
|
|
.read = stm32mp_bsec_read,
|
|
|
|
.write = stm32mp_bsec_write,
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int stm32mp_bsec_of_to_plat(struct udevice *dev)
|
2018-05-17 13:24:05 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
|
2018-05-17 13:24:05 +00:00
|
|
|
|
|
|
|
plat->base = (u32)dev_read_addr_ptr(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-27 16:01:28 +00:00
|
|
|
static int stm32mp_bsec_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int otp;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2021-02-25 12:43:07 +00:00
|
|
|
struct clk_bulk clk_bulk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_get_bulk(dev, &clk_bulk);
|
|
|
|
if (!ret) {
|
|
|
|
ret = clk_enable_bulk(&clk_bulk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-27 16:01:28 +00:00
|
|
|
|
2020-05-25 10:19:41 +00:00
|
|
|
/*
|
|
|
|
* update unlocked shadow for OTP cleared by the rom code
|
|
|
|
* only executed in U-Boot proper when TF-A is not used
|
|
|
|
*/
|
2020-07-31 14:31:51 +00:00
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_TFABOOT) && !IS_ENABLED(CONFIG_SPL_BUILD)) {
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2020-07-31 14:31:51 +00:00
|
|
|
|
|
|
|
for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
|
|
|
|
if (!bsec_read_SR_lock(plat->base, otp))
|
2020-11-06 18:01:31 +00:00
|
|
|
bsec_shadow_register(dev, plat->base, otp);
|
2020-07-31 14:31:51 +00:00
|
|
|
}
|
2019-02-27 16:01:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:24:05 +00:00
|
|
|
static const struct udevice_id stm32mp_bsec_ids[] = {
|
2019-02-27 16:01:27 +00:00
|
|
|
{ .compatible = "st,stm32mp15-bsec" },
|
2018-05-17 13:24:05 +00:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(stm32mp_bsec) = {
|
|
|
|
.name = "stm32mp_bsec",
|
|
|
|
.id = UCLASS_MISC,
|
|
|
|
.of_match = stm32mp_bsec_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = stm32mp_bsec_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct stm32mp_bsec_plat),
|
2018-05-17 13:24:05 +00:00
|
|
|
.ops = &stm32mp_bsec_ops,
|
2019-02-27 16:01:28 +00:00
|
|
|
.probe = stm32mp_bsec_probe,
|
2018-05-17 13:24:05 +00:00
|
|
|
};
|
2020-06-16 16:27:44 +00:00
|
|
|
|
|
|
|
bool bsec_dbgswenable(void)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32mp_bsec_plat *plat;
|
2020-06-16 16:27:44 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_driver(UCLASS_MISC,
|
2020-12-29 03:34:56 +00:00
|
|
|
DM_DRIVER_GET(stm32mp_bsec), &dev);
|
2020-06-16 16:27:44 +00:00
|
|
|
if (ret || !dev) {
|
2020-11-06 18:01:31 +00:00
|
|
|
log_debug("bsec driver not available\n");
|
2020-06-16 16:27:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:20 +00:00
|
|
|
plat = dev_get_plat(dev);
|
2020-06-16 16:27:44 +00:00
|
|
|
if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|