drivers/nvmem/lan9662-otpc.c

Source file repositories/reference/linux-study-clean/drivers/nvmem/lan9662-otpc.c

File Facts

System
Linux kernel
Corpus path
drivers/nvmem/lan9662-otpc.c
Extension
.c
Size
5527 bytes
Lines
223
Domain
Driver Families
Bucket
drivers/nvmem
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct lan9662_otp {
	struct device *dev;
	void __iomem *base;
};

static int lan9662_otp_wait_flag_clear(void __iomem *reg, u32 flag)
{
	u32 val;

	return readl_poll_timeout(reg, val, !(val & flag),
				  OTP_SLEEP_US, OTP_TIMEOUT_US);
}

static int lan9662_otp_power(struct lan9662_otp *otp, bool up)
{
	void __iomem *pwrdn = OTP_OTP_PWR_DN(otp->base);

	if (up) {
		writel(readl(pwrdn) & ~OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
		if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
						OTP_OTP_STATUS_OTP_CPUMPEN))
			return -ETIMEDOUT;
	} else {
		writel(readl(pwrdn) | OTP_OTP_PWR_DN_OTP_PWRDN_N, pwrdn);
	}

	return 0;
}

static int lan9662_otp_execute(struct lan9662_otp *otp)
{
	if (lan9662_otp_wait_flag_clear(OTP_OTP_CMD_GO(otp->base),
					OTP_OTP_CMD_GO_OTP_GO))
		return -ETIMEDOUT;

	if (lan9662_otp_wait_flag_clear(OTP_OTP_STATUS(otp->base),
					OTP_OTP_STATUS_OTP_BUSY))
		return -ETIMEDOUT;

	return 0;
}

static void lan9662_otp_set_address(struct lan9662_otp *otp, u32 offset)
{
	writel(0xff & (offset >> 8), OTP_OTP_ADDR_HI(otp->base));
	writel(0xff & offset, OTP_OTP_ADDR_LO(otp->base));
}

static int lan9662_otp_read_byte(struct lan9662_otp *otp, u32 offset, u8 *dst)
{
	u32 pass;
	int rc;

	lan9662_otp_set_address(otp, offset);
	writel(OTP_OTP_FUNC_CMD_OTP_READ, OTP_OTP_FUNC_CMD(otp->base));
	writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));
	rc = lan9662_otp_execute(otp);
	if (!rc) {
		pass = readl(OTP_OTP_PASS_FAIL(otp->base));
		if (pass & OTP_OTP_PASS_FAIL_OTP_READ_PROHIBITED)
			return -EACCES;
		*dst = (u8) readl(OTP_OTP_RD_DATA(otp->base));
	}
	return rc;
}

static int lan9662_otp_write_byte(struct lan9662_otp *otp, u32 offset, u8 data)
{
	u32 pass;
	int rc;

	lan9662_otp_set_address(otp, offset);
	writel(OTP_OTP_PRGM_MODE_OTP_PGM_MODE_BYTE, OTP_OTP_PRGM_MODE(otp->base));
	writel(data, OTP_OTP_PRGM_DATA(otp->base));
	writel(OTP_OTP_FUNC_CMD_OTP_PROGRAM, OTP_OTP_FUNC_CMD(otp->base));
	writel(OTP_OTP_CMD_GO_OTP_GO, OTP_OTP_CMD_GO(otp->base));

	rc = lan9662_otp_execute(otp);
	if (!rc) {
		pass = readl(OTP_OTP_PASS_FAIL(otp->base));
		if (pass & OTP_OTP_PASS_FAIL_OTP_WRITE_PROHIBITED)
			return -EACCES;
		if (pass & OTP_OTP_PASS_FAIL_OTP_FAIL)
			return -EIO;
	}
	return rc;
}

static int lan9662_otp_read(void *context, unsigned int offset,
			    void *_val, size_t bytes)

Annotation

Implementation Notes