123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- // SPDX-License-Identifier: GPL-2.0+
- /*
- * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
- * Author: yanhong <yanhong.wang@starfivetech.com>
- *
- */
- #include <common.h>
- #include <clk.h>
- #include <dm/device.h>
- #include <dm/read.h>
- #include <linux/delay.h>
- #include <asm/io.h>
- #include <misc.h>
- /*otp param*/
- #define OTP_MEM_START 0x800
- #define OTP_MEM_SIZE 0x800
- #define OTP_DIV_1US 1000000
- /* program pulse time select. default value is 11*/
- #define OTP_TPW_TIME 11
- /* Read Data Access Time. max 50ns*/
- #define OTP_TCD_TIME 20
- #define OTP_PAOGRAM_DELAY 100
- #define OTP_TURN_ON_DELAY 200
- /*timing Register*/
- #define OTP_CFGR_DIV_1US_MASK 0xff
- #define OTP_CFGR_DIV_1US_SHIFT 8
- #define OTP_CFGR_RD_CYC_MASK 0x0f
- #define OTP_CFGR_RD_CYC_SHIFT 16
- /*status Register*/
- #define OTPC_SRR_BUSY (1<<31)
- /*otp operation mode select*/
- #define OTP_OPRR_OPR_MASK 0x7
- #define OTP_OPR_STANDBY 0x0 /* set otp to standby*/
- #define OTP_OPR_READ 0x1 /* set otp to read*/
- #define OTPC_TIMEOUT_CNT 10000
- #define BYTES_PER_INT 4
- struct starfive_otp_regs {
- u32 otp_cfg; /*timing Register*/
- u32 otpc_ie; /*interrupt Enable Register*/
- u32 otpc_sr; /*status Register*/
- u32 otp_opr; /*operation mode select Register*/
- u32 otpc_ctl; /*otp control port*/
- u32 otpc_addr; /*otp pa port*/
- u32 otpc_din; /*otp pdin port*/
- u32 otpc_dout; /*otp pdout*/
- };
- struct starfive_otp_platdata {
- struct starfive_otp_regs __iomem *regs;
- struct clk clk;
- u32 pclk_hz;
- };
- static int starfive_otp_regstatus(struct starfive_otp_regs *regs,
- u32 mask)
- {
- int delay = OTPC_TIMEOUT_CNT;
- while (readl(®s->otpc_sr) & mask) {
- udelay(OTP_PAOGRAM_DELAY);
- delay--;
- if (delay <= 0) {
- printf("%s: check otp status timeout\n", __func__);
- return -ETIMEDOUT;
- }
- }
- return 0;
- }
- static int starfive_otp_setmode(struct starfive_otp_regs *regs,
- u32 mode)
- {
- writel(mode & OTP_OPRR_OPR_MASK, ®s->otp_opr);
- starfive_otp_regstatus(regs, OTPC_SRR_BUSY);
- return 0;
- }
- static int starfive_otp_config(struct udevice *dev)
- {
- struct starfive_otp_platdata *plat = dev_get_plat(dev);
- struct starfive_otp_regs *regs = (struct starfive_otp_regs *)plat->regs;
- u32 div_1us;
- u32 rd_cyc;
- u32 val;
- div_1us = plat->pclk_hz / OTP_DIV_1US;
- rd_cyc = div_1us / OTP_TCD_TIME + 2;
- val = OTP_TPW_TIME;
- val |= (rd_cyc & OTP_CFGR_RD_CYC_MASK) << OTP_CFGR_RD_CYC_SHIFT;
- val |= (div_1us & OTP_CFGR_DIV_1US_MASK) << OTP_CFGR_DIV_1US_SHIFT;
- writel(val, ®s->otp_cfg);
- return 0;
- }
- static int starfive_otp_read(struct udevice *dev, int offset,
- void *buf, int size)
- {
- struct starfive_otp_platdata *plat = dev_get_plat(dev);
- void *base = (void *)plat->regs;
- u32 *databuf = (u32 *)buf;
- u32 data;
- int bytescnt;
- int i;
- if ((size % BYTES_PER_INT) || (offset % BYTES_PER_INT)) {
- printf("%s: size and offset must be multiple of 4.\n", __func__);
- return -EINVAL;
- }
- /* check bounds */
- if (!buf)
- return -EINVAL;
- if (offset >= OTP_MEM_SIZE)
- return -EINVAL;
- if ((offset + size) > OTP_MEM_SIZE)
- return -EINVAL;
- bytescnt = size / BYTES_PER_INT;
- for (i = 0; i < bytescnt; i++) {
- starfive_otp_setmode(plat->regs, OTP_OPR_READ);
- /* read the value */
- data = readl(base + OTP_MEM_START + offset);
- starfive_otp_regstatus(plat->regs, OTPC_SRR_BUSY);
- starfive_otp_setmode(plat->regs, OTP_OPR_STANDBY);
- databuf[i] = data;
- offset += 4;
- }
- return size;
- }
- static int starfive_otp_probe(struct udevice *dev)
- {
- starfive_otp_config(dev);
- udelay(OTP_TURN_ON_DELAY);
- return 0;
- }
- static int starfive_otp_ofdata_to_platdata(struct udevice *dev)
- {
- struct starfive_otp_platdata *plat = dev_get_plat(dev);
- int ret;
- plat->regs = dev_read_addr_ptr(dev);
- if (!plat->regs)
- goto err;
- ret = dev_read_u32(dev, "clock-frequency", &plat->pclk_hz);
- if (ret < 0)
- goto err;
- ret = clk_get_by_name(dev, "apb", &plat->clk);
- if (ret)
- goto err;
- ret = clk_enable(&plat->clk);
- if (ret < 0)
- goto err;
- return 0;
- err:
- printf("%s init fail.\n", __func__);
- return ret;
- }
- static const struct misc_ops starfive_otp_ops = {
- .read = starfive_otp_read,
- };
- static const struct udevice_id starfive_otp_ids[] = {
- { .compatible = "starfive,jh7110-otp" },
- {}
- };
- U_BOOT_DRIVER(starfive_otp) = {
- .name = "starfive_otp",
- .id = UCLASS_MISC,
- .of_match = starfive_otp_ids,
- .probe = starfive_otp_probe,
- .of_to_plat = starfive_otp_ofdata_to_platdata,
- .plat_auto = sizeof(struct starfive_otp_platdata),
- .ops = &starfive_otp_ops,
- };
|