blob: 09687defaa1c4827004dad42eb01db190b309bf7 [file] [log] [blame]
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
* Copyright (c) 2019 Amlogic, Inc. All rights reserved.
*/
#include <config.h>
#include <common.h>
#include <command.h>
#include <asm/arch/io.h>
#include <asm/arch/efuse.h>
char efuse_buf[EFUSE_BYTES] = {0};
ssize_t efuse_read(char *buf, size_t count, loff_t *ppos)
{
unsigned int pos = *ppos;
struct efuse_hal_api_arg arg;
unsigned int retcnt;
int ret;
arg.cmd = EFUSE_HAL_API_READ;
arg.offset = pos;
arg.size = count;
arg.buffer_phy = (unsigned long)buf;
arg.retcnt_phy = (unsigned long)&retcnt;
ret = meson_trustzone_efuse(&arg);
if (ret == 0) {
*ppos += retcnt;
return retcnt;
} else{
return ret;
}
}
ssize_t efuse_write(const char *buf, size_t count, loff_t *ppos)
{
unsigned int pos = *ppos;
if ((pos & 0xffff) >= EFUSE_BYTES)
return 0; /* Past EOF */
if (count > EFUSE_BYTES - pos)
count = EFUSE_BYTES - pos;
if (count > EFUSE_BYTES)
return -1;
struct efuse_hal_api_arg arg;
unsigned int retcnt;
arg.cmd = EFUSE_HAL_API_WRITE;
arg.offset = pos;
arg.size = count;
arg.buffer_phy = (unsigned long)buf;
arg.retcnt_phy = (unsigned long)&retcnt;
int ret;
ret = meson_trustzone_efuse(&arg);
if (ret == 0) {
*ppos = retcnt;
return retcnt;
} else{
return ret;
}
}
int efuse_read_usr(char *buf, size_t count, loff_t *ppos)
{
char data[EFUSE_BYTES];
char *pdata = NULL;
int ret;
loff_t pos;
memset(data, 0, count);
pdata = data;
pos = *ppos;
ret = efuse_read(pdata, count, (loff_t *)&pos);
memcpy(buf, data, count);
return ret;
}
int efuse_write_usr(char *buf, size_t count, loff_t *ppos)
{
char data[EFUSE_BYTES];
char *pdata = NULL;
char *penc = NULL;
int ret;
loff_t pos;
if (count == 0) {
printf("data length: 0 is error!\n");
return -1;
}
memset(data, 0, EFUSE_BYTES);
memset(efuse_buf, 0, EFUSE_BYTES);
memcpy(data, buf, count);
pdata = data;
penc = efuse_buf;
memcpy(penc, pdata, count);
pos = *ppos;
ret = efuse_write(efuse_buf, count, (loff_t *)&pos);
return ret;
}
uint32_t efuse_get_max(void)
{
struct efuse_hal_api_arg arg;
int ret;
arg.cmd = EFUSE_HAL_API_USER_MAX;
ret = meson_trustzone_efuse_get_max(&arg);
if (ret == 0) {
printf("ERROR: can not get efuse user max bytes!!!\n");
return -1;
} else{
return ret;
}
}