2019-09-25 17:57:49 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/delay.h>
|
2023-02-22 22:44:38 +00:00
|
|
|
#include <linux/iopoll.h>
|
2019-09-25 17:57:49 +02:00
|
|
|
#include <misc.h>
|
|
|
|
|
|
|
|
/* OTP Register Offsets */
|
|
|
|
#define OTPC_SBPI_CTRL 0x0020
|
|
|
|
#define OTPC_SBPI_CMD_VALID_PRE 0x0024
|
|
|
|
#define OTPC_SBPI_CS_VALID_PRE 0x0028
|
|
|
|
#define OTPC_SBPI_STATUS 0x002C
|
|
|
|
#define OTPC_USER_CTRL 0x0100
|
|
|
|
#define OTPC_USER_ADDR 0x0104
|
|
|
|
#define OTPC_USER_ENABLE 0x0108
|
|
|
|
#define OTPC_USER_QP 0x0120
|
|
|
|
#define OTPC_USER_Q 0x0124
|
|
|
|
#define OTPC_INT_STATUS 0x0304
|
|
|
|
#define OTPC_SBPI_CMD0_OFFSET 0x1000
|
|
|
|
#define OTPC_SBPI_CMD1_OFFSET 0x1004
|
|
|
|
|
|
|
|
/* OTP Register bits and masks */
|
|
|
|
#define OTPC_USER_ADDR_MASK GENMASK(31, 16)
|
|
|
|
#define OTPC_USE_USER BIT(0)
|
|
|
|
#define OTPC_USE_USER_MASK GENMASK(16, 16)
|
|
|
|
#define OTPC_USER_FSM_ENABLE BIT(0)
|
|
|
|
#define OTPC_USER_FSM_ENABLE_MASK GENMASK(16, 16)
|
|
|
|
#define OTPC_SBPI_DONE BIT(1)
|
|
|
|
#define OTPC_USER_DONE BIT(2)
|
|
|
|
|
|
|
|
#define SBPI_DAP_ADDR 0x02
|
|
|
|
#define SBPI_DAP_ADDR_SHIFT 8
|
|
|
|
#define SBPI_DAP_ADDR_MASK GENMASK(31, 24)
|
|
|
|
#define SBPI_CMD_VALID_MASK GENMASK(31, 16)
|
|
|
|
#define SBPI_DAP_CMD_WRF 0xC0
|
|
|
|
#define SBPI_DAP_REG_ECC 0x3A
|
|
|
|
#define SBPI_ECC_ENABLE 0x00
|
|
|
|
#define SBPI_ECC_DISABLE 0x09
|
|
|
|
#define SBPI_ENABLE BIT(0)
|
|
|
|
#define SBPI_ENABLE_MASK GENMASK(16, 16)
|
|
|
|
|
|
|
|
#define OTPC_TIMEOUT 10000
|
|
|
|
|
2020-12-03 16:55:23 -07:00
|
|
|
struct rockchip_otp_plat {
|
2019-09-25 17:57:49 +02:00
|
|
|
void __iomem *base;
|
|
|
|
};
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
struct rockchip_otp_data {
|
|
|
|
int (*read)(struct udevice *dev, int offset, void *buf, int size);
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rockchip_otp_poll_timeout(struct rockchip_otp_plat *otp, u32 flag)
|
2019-09-25 17:57:49 +02:00
|
|
|
{
|
2023-02-22 22:44:38 +00:00
|
|
|
u32 status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = readl_poll_sleep_timeout(otp->base + OTPC_INT_STATUS, status,
|
|
|
|
(status & flag), 1, OTPC_TIMEOUT);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-25 17:57:49 +02:00
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
/* Clear int flag */
|
2019-09-25 17:57:49 +02:00
|
|
|
writel(flag, otp->base + OTPC_INT_STATUS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
static int rockchip_otp_ecc_enable(struct rockchip_otp_plat *otp, bool enable)
|
2019-09-25 17:57:49 +02:00
|
|
|
{
|
|
|
|
writel(SBPI_DAP_ADDR_MASK | (SBPI_DAP_ADDR << SBPI_DAP_ADDR_SHIFT),
|
|
|
|
otp->base + OTPC_SBPI_CTRL);
|
|
|
|
|
|
|
|
writel(SBPI_CMD_VALID_MASK | 0x1, otp->base + OTPC_SBPI_CMD_VALID_PRE);
|
|
|
|
writel(SBPI_DAP_CMD_WRF | SBPI_DAP_REG_ECC,
|
|
|
|
otp->base + OTPC_SBPI_CMD0_OFFSET);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
writel(SBPI_ECC_ENABLE, otp->base + OTPC_SBPI_CMD1_OFFSET);
|
|
|
|
else
|
|
|
|
writel(SBPI_ECC_DISABLE, otp->base + OTPC_SBPI_CMD1_OFFSET);
|
|
|
|
|
|
|
|
writel(SBPI_ENABLE_MASK | SBPI_ENABLE, otp->base + OTPC_SBPI_CTRL);
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
return rockchip_otp_poll_timeout(otp, OTPC_SBPI_DONE);
|
2019-09-25 17:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_px30_otp_read(struct udevice *dev, int offset,
|
|
|
|
void *buf, int size)
|
|
|
|
{
|
2020-12-03 16:55:23 -07:00
|
|
|
struct rockchip_otp_plat *otp = dev_get_plat(dev);
|
2019-09-25 17:57:49 +02:00
|
|
|
u8 *buffer = buf;
|
2023-02-22 22:44:38 +00:00
|
|
|
int ret;
|
2019-09-25 17:57:49 +02:00
|
|
|
|
|
|
|
ret = rockchip_otp_ecc_enable(otp, false);
|
2023-02-22 22:44:38 +00:00
|
|
|
if (ret)
|
2019-09-25 17:57:49 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
writel(OTPC_USE_USER | OTPC_USE_USER_MASK, otp->base + OTPC_USER_CTRL);
|
|
|
|
udelay(5);
|
2023-02-22 22:44:38 +00:00
|
|
|
|
2019-09-25 17:57:49 +02:00
|
|
|
while (size--) {
|
|
|
|
writel(offset++ | OTPC_USER_ADDR_MASK,
|
|
|
|
otp->base + OTPC_USER_ADDR);
|
|
|
|
writel(OTPC_USER_FSM_ENABLE | OTPC_USER_FSM_ENABLE_MASK,
|
|
|
|
otp->base + OTPC_USER_ENABLE);
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
ret = rockchip_otp_poll_timeout(otp, OTPC_USER_DONE);
|
|
|
|
if (ret)
|
2019-09-25 17:57:49 +02:00
|
|
|
goto read_end;
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
*buffer++ = (u8)(readl(otp->base + OTPC_USER_Q) & 0xFF);
|
2019-09-25 17:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_end:
|
|
|
|
writel(0x0 | OTPC_USE_USER_MASK, otp->base + OTPC_USER_CTRL);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_otp_read(struct udevice *dev, int offset,
|
|
|
|
void *buf, int size)
|
|
|
|
{
|
2023-02-22 22:44:38 +00:00
|
|
|
const struct rockchip_otp_data *data =
|
|
|
|
(void *)dev_get_driver_data(dev);
|
|
|
|
|
|
|
|
if (offset < 0 || !buf || size <= 0 || offset + size > data->size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!data->read)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return data->read(dev, offset, buf, size);
|
2019-09-25 17:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct misc_ops rockchip_otp_ops = {
|
|
|
|
.read = rockchip_otp_read,
|
|
|
|
};
|
|
|
|
|
2020-12-03 16:55:21 -07:00
|
|
|
static int rockchip_otp_of_to_plat(struct udevice *dev)
|
2019-09-25 17:57:49 +02:00
|
|
|
{
|
2023-02-22 22:44:38 +00:00
|
|
|
struct rockchip_otp_plat *plat = dev_get_plat(dev);
|
2019-09-25 17:57:49 +02:00
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
plat->base = dev_read_addr_ptr(dev);
|
2019-09-25 17:57:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-22 22:44:38 +00:00
|
|
|
static const struct rockchip_otp_data px30_data = {
|
|
|
|
.read = rockchip_px30_otp_read,
|
|
|
|
.size = 0x40,
|
|
|
|
};
|
|
|
|
|
2019-09-25 17:57:49 +02:00
|
|
|
static const struct udevice_id rockchip_otp_ids[] = {
|
|
|
|
{
|
|
|
|
.compatible = "rockchip,px30-otp",
|
2023-02-22 22:44:38 +00:00
|
|
|
.data = (ulong)&px30_data,
|
2019-09-25 17:57:49 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "rockchip,rk3308-otp",
|
2023-02-22 22:44:38 +00:00
|
|
|
.data = (ulong)&px30_data,
|
2019-09-25 17:57:49 +02:00
|
|
|
},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(rockchip_otp) = {
|
|
|
|
.name = "rockchip_otp",
|
|
|
|
.id = UCLASS_MISC,
|
|
|
|
.of_match = rockchip_otp_ids,
|
2020-12-03 16:55:21 -07:00
|
|
|
.of_to_plat = rockchip_otp_of_to_plat,
|
2023-02-22 22:44:38 +00:00
|
|
|
.plat_auto = sizeof(struct rockchip_otp_plat),
|
|
|
|
.ops = &rockchip_otp_ops,
|
2019-09-25 17:57:49 +02:00
|
|
|
};
|