// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2015 Toradex AG.
*
* Author: Sanchayan Maity <sanchayan.maity@toradex.com>
*
* Based on the barebox ocotp driver,
* Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>
* Orex Computed Radiography
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/nvmem-provider.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
/* OCOTP Register Offsets */
#define OCOTP_CTRL_REG 0 x00
#define OCOTP_CTRL_SET 0 x04
#define OCOTP_CTRL_CLR 0 x08
#define OCOTP_TIMING 0 x10
#define OCOTP_DATA 0 x20
#define OCOTP_READ_CTRL_REG 0 x30
#define OCOTP_READ_FUSE_DATA 0 x40
/* OCOTP Register bits and masks */
#define OCOTP_CTRL_WR_UNLOCK 16
#define OCOTP_CTRL_WR_UNLOCK_KEY 0 x3E77
#define OCOTP_CTRL_WR_UNLOCK_MASK GENMASK(31 , 16 )
#define OCOTP_CTRL_ADDR 0
#define OCOTP_CTRL_ADDR_MASK GENMASK(6 , 0 )
#define OCOTP_CTRL_RELOAD_SHADOWS BIT(10 )
#define OCOTP_CTRL_ERR BIT(9 )
#define OCOTP_CTRL_BUSY BIT(8 )
#define OCOTP_TIMING_STROBE_READ 16
#define OCOTP_TIMING_STROBE_READ_MASK GENMASK(21 , 16 )
#define OCOTP_TIMING_RELAX 12
#define OCOTP_TIMING_RELAX_MASK GENMASK(15 , 12 )
#define OCOTP_TIMING_STROBE_PROG 0
#define OCOTP_TIMING_STROBE_PROG_MASK GENMASK(11 , 0 )
#define OCOTP_READ_CTRL_READ_FUSE 0 x1
#define VF610_OCOTP_TIMEOUT 100000
#define BF(value, field) (((value) << field) & field## _MASK)
#define DEF_RELAX 20
static const int base_to_fuse_addr_mappings[][2 ] = {
{0 x400, 0 x00},
{0 x410, 0 x01},
{0 x420, 0 x02},
{0 x450, 0 x05},
{0 x4F0, 0 x0F},
{0 x600, 0 x20},
{0 x610, 0 x21},
{0 x620, 0 x22},
{0 x630, 0 x23},
{0 x640, 0 x24},
{0 x650, 0 x25},
{0 x660, 0 x26},
{0 x670, 0 x27},
{0 x6F0, 0 x2F},
{0 x880, 0 x38},
{0 x890, 0 x39},
{0 x8A0, 0 x3A},
{0 x8B0, 0 x3B},
{0 x8C0, 0 x3C},
{0 x8D0, 0 x3D},
{0 x8E0, 0 x3E},
{0 x8F0, 0 x3F},
{0 xC80, 0 x78},
{0 xC90, 0 x79},
{0 xCA0, 0 x7A},
{0 xCB0, 0 x7B},
{0 xCC0, 0 x7C},
{0 xCD0, 0 x7D},
{0 xCE0, 0 x7E},
{0 xCF0, 0 x7F},
};
struct vf610_ocotp {
void __iomem *base;
struct clk *clk;
struct device *dev;
struct nvmem_device *nvmem;
int timing;
};
static int vf610_ocotp_wait_busy(void __iomem *base)
{
int timeout = VF610_OCOTP_TIMEOUT;
while ((readl(base) & OCOTP_CTRL_BUSY) && --timeout)
udelay(10 );
if (!timeout) {
writel(OCOTP_CTRL_ERR, base + OCOTP_CTRL_CLR);
return -ETIMEDOUT;
}
udelay(10 );
return 0 ;
}
static int vf610_ocotp_calculate_timing(struct vf610_ocotp *ocotp_dev)
{
u32 clk_rate;
u32 relax, strobe_read, strobe_prog;
u32 timing;
clk_rate = clk_get_rate(ocotp_dev->clk);
/* Refer section OTP read/write timing parameters in TRM */
relax = clk_rate / (1000000000 / DEF_RELAX) - 1 ;
strobe_prog = clk_rate / (1000000000 / 10000 ) + 2 * (DEF_RELAX + 1 ) - 1 ;
strobe_read = clk_rate / (1000000000 / 40 ) + 2 * (DEF_RELAX + 1 ) - 1 ;
timing = BF(relax, OCOTP_TIMING_RELAX);
timing |= BF(strobe_read, OCOTP_TIMING_STROBE_READ);
timing |= BF(strobe_prog, OCOTP_TIMING_STROBE_PROG);
return timing;
}
static int vf610_get_fuse_address(int base_addr_offset)
{
int i;
for (i = 0 ; i < ARRAY_SIZE(base_to_fuse_addr_mappings); i++) {
if (base_to_fuse_addr_mappings[i][0 ] == base_addr_offset)
return base_to_fuse_addr_mappings[i][1 ];
}
return -EINVAL;
}
static int vf610_ocotp_read(void *context, unsigned int offset,
void *val, size_t bytes)
{
struct vf610_ocotp *ocotp = context;
void __iomem *base = ocotp->base;
u32 reg, *buf = val;
int fuse_addr;
int ret;
while (bytes > 0 ) {
fuse_addr = vf610_get_fuse_address(offset);
if (fuse_addr > 0 ) {
writel(ocotp->timing, base + OCOTP_TIMING);
ret = vf610_ocotp_wait_busy(base + OCOTP_CTRL_REG);
if (ret)
return ret;
reg = readl(base + OCOTP_CTRL_REG);
reg &= ~OCOTP_CTRL_ADDR_MASK;
reg &= ~OCOTP_CTRL_WR_UNLOCK_MASK;
reg |= BF(fuse_addr, OCOTP_CTRL_ADDR);
writel(reg, base + OCOTP_CTRL_REG);
writel(OCOTP_READ_CTRL_READ_FUSE,
base + OCOTP_READ_CTRL_REG);
ret = vf610_ocotp_wait_busy(base + OCOTP_CTRL_REG);
if (ret)
return ret;
if (readl(base) & OCOTP_CTRL_ERR) {
dev_dbg(ocotp->dev, "Error reading from fuse address %x\n" ,
fuse_addr);
writel(OCOTP_CTRL_ERR, base + OCOTP_CTRL_CLR);
}
/*
* In case of error, we do not abort and expect to read
* 0xBADABADA as mentioned by the TRM. We just read this
* value and return.
*/
*buf = readl(base + OCOTP_READ_FUSE_DATA);
} else {
*buf = 0 ;
}
buf++;
bytes -= 4 ;
offset += 4 ;
}
return 0 ;
}
static struct nvmem_config ocotp_config = {
.name = "ocotp" ,
.stride = 4 ,
.word_size = 4 ,
.reg_read = vf610_ocotp_read,
};
static const struct of_device_id ocotp_of_match[] = {
{ .compatible = "fsl,vf610-ocotp" , },
{/* sentinel */},
};
MODULE_DEVICE_TABLE(of, ocotp_of_match);
static int vf610_ocotp_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct resource *res;
struct vf610_ocotp *ocotp_dev;
ocotp_dev = devm_kzalloc(dev, sizeof (struct vf610_ocotp), GFP_KERNEL);
if (!ocotp_dev)
return -ENOMEM;
ocotp_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0 , &res);
if (IS_ERR(ocotp_dev->base))
return PTR_ERR(ocotp_dev->base);
ocotp_dev->clk = devm_clk_get(dev, NULL);
if (IS_ERR(ocotp_dev->clk)) {
dev_err(dev, "failed getting clock, err = %ld\n" ,
PTR_ERR(ocotp_dev->clk));
return PTR_ERR(ocotp_dev->clk);
}
ocotp_dev->dev = dev;
ocotp_dev->timing = vf610_ocotp_calculate_timing(ocotp_dev);
ocotp_config.size = resource_size(res);
ocotp_config.priv = ocotp_dev;
ocotp_config.dev = dev;
ocotp_dev->nvmem = devm_nvmem_register(dev, &ocotp_config);
return PTR_ERR_OR_ZERO(ocotp_dev->nvmem);
}
static struct platform_driver vf610_ocotp_driver = {
.probe = vf610_ocotp_probe,
.driver = {
.name = "vf610-ocotp" ,
.of_match_table = ocotp_of_match,
},
};
module_platform_driver(vf610_ocotp_driver);
MODULE_AUTHOR("Sanchayan Maity <sanchayan.maity@toradex.com>" );
MODULE_DESCRIPTION("Vybrid OCOTP driver" );
MODULE_LICENSE("GPL v2" );
Messung V0.5 in Prozent C=93 H=95 G=93
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-08)
¤
*© Formatika GbR, Deutschland