Skip to contentSkip to conversation

imx6: ocotp: Add On-Chip OTP registers write support

PATCH In review Uladzimir Bely 2 participants 8 messages Apr 9, 2014 – Apr 24, 2014 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 imx6: ocotp: Add On-Chip OTP registers write support

In reply to a message not in the archive

OCOTP imx6 driver updated and ocotptool command added. MAC address can be written to PROM. "0" (unprogrammed) bits of MAC can be replaced with "1" (but not vice versa) only once.

Tested on phytec-phyflex-imx6 board.

---
 arch/arm/mach-imx/Kconfig |   7 ++
 arch/arm/mach-imx/ocotp.c | 304 ++++++++++++++++++++++++++++++++++++++++++++++
 commands/Kconfig          |  10 ++
 commands/Makefile         |   1 +
 commands/ocotptool.c      | 123 +++++++++++++++++++
 5 files changed, 445 insertions(+)
 create mode 100644 commands/ocotptool.c
arch/arm/mach-imx/Kconfig +7 -0
index 81d16e1..0c81560 100644
@@ -593,6 +593,13 @@ config IMX_OCOTP
 	  only supported functionality is reading the MAC address and assigning
 	  it to an ethernet device.
 
+config IMX_OCOTP_WRITE
+	bool
+	prompt "Enable write support of i.MX6 CPUs OTP fuses"
+	depends on IMX_OCOTP
+	help
+	  This adds support for writing to On-Chip OTP registers.
+
 endmenu
 
 endif
arch/arm/mach-imx/ocotp.c +304 -0

328 lines not on this page · raw message

commands/Kconfig +10 -0
index 1e07b5b..560e426 100644
@@ -733,6 +733,16 @@ config CMD_MIITOOL
 	  detailed MII status information, such as MII capabilities,
 	  current advertising mode, and link partner capabilities.
 
+config CMD_OCOTPTOOL
+	bool
+	depends on IMX_OCOTP
+	prompt "ocotptool"
+	help
+	  The ocotptool command allows to read and write to i.MX6 On-Chip
+	  OTP registers from barebox shell. Usage:
+		"ocotptool read mac" to read MAC
+		"ocotptool blow mac xx:xx:xx:xx:xx:xx" to write MAC
+
 config CMD_CLK
 	tristate
 	depends on COMMON_CLK
commands/Makefile +1 -0
index 58d27fa..01de356 100644
@@ -93,3 +93,4 @@ obj-$(CONFIG_CMD_MIITOOL)	+= miitool.o
 obj-$(CONFIG_CMD_DETECT)	+= detect.o
 obj-$(CONFIG_CMD_BOOT)		+= boot.o
 obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
+obj-$(CONFIG_CMD_OCOTPTOOL)	+= ocotptool.o
commands/ocotptool.c new file +123 -0

123 lines not on this page · raw message

Signature · 7 lines
1.8.3.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Hello, Sasha.

It's a new version of patch, considering most of your remarks. But I still have some questions.

arch/arm/mach-imx/ocotp.c line 97

Quoted 9 lines
arch/arm/mach-imx/ocotp.c +9 -0
+
+	/* Get clock */
+	clk_rate = clk_get_rate(clk_lookup("ipg"));
+	if (clk_rate == -1) {
+		printf("ERROR: imx6_get_gptclk failed\n");
+		return -1;
+	}
+
+	relax = clk_rate / (1000000000 / DEF_RELAX) - 1;

I tried use clk_get(), as you proposed, but didn't understand, how to get clk rate value in this case.
clkdev_add_physbase() just creates structure clk_lookup, but how to get pointer to it, to be able to use its ->clk?

Signature · 8 lines
With regards,
Uladzimir Bely.
09.04.2014 15:11, Uladzimir Bely пишет:

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#3 lore raw
In reply to Uladzimir Bely

Hello Uladzimir,

Quoted 14 lines
arch/arm/mach-imx/ocotp.c +14 -0
+	struct cdev cdev;
+	void __iomem *base;
+};
+
+struct ocotp_regs {
+	u32 ctrl;
+	u32 ctrl_set;
+	u32 ctrl_clr;
+	u32 ctrl_tog;
+	u32 timing;
+	u32 rsvd0[3];
+	u32 data;
+	u32 rsvd1[3];
+	u32 read_ctrl;

arch/arm/mach-imx/ocotp.c line 88

Quoted 10 lines
arch/arm/mach-imx/ocotp.c +10 -0
+	u32 rsvd2[3];
+	u32 fuse_data;
+};
+
+static int imx6_ocotp_set_timing(void)
+{
+	u32 clk_rate;
+	u32 relax, strobe_read, strobe_prog;
+	u32 timing;
+	struct ocotp_regs *otp = (struct ocotp_regs *)MX6_OCOTP_BASE_ADDR;

You're still using MX6_OCOTP_BASE_ADDR. All functions in this driver need a struct ocotp_priv * as argument. Then use priv->base for getting the ocotp base address.

arch/arm/mach-imx/ocotp.c line 121

Quoted 12 lines
arch/arm/mach-imx/ocotp.c +12 -0
+static int imx6_ocotp_wait_busy(u32 flags)
+{
+	int count;
+	u32 cf;
+	struct ocotp_regs *otp = (struct ocotp_regs *)MX6_OCOTP_BASE_ADDR;
+
+	for (count = 10000; count >= 0; count--) {
+		cf = (1 << OCOTP_CTRL_BUSY) | (1 << OCOTP_CTRL_ERROR) | flags;
+		cf &= readl(&otp->ctrl);
+		if (!cf)
+			break;
+	}

No counting loop please. grep for is_timeout in the barebox code for examples how to program timeout loops.

arch/arm/mach-imx/ocotp.c line 128

Quoted 7 lines
arch/arm/mach-imx/ocotp.c +7 -0
+
+	if (count < 0) {
+		printf("ERROR: otp_wait_busy timeout. 0x%X\n", cf);
+		/* Clear ERROR bit, BUSY bit will be cleared by controller */
+		writel(1 << OCOTP_CTRL_ERROR, &otp->ctrl_clr);
+		return -EBUSY;
+	}

count will never be smaller than 0.

arch/arm/mach-imx/ocotp.c line 177

Quoted 8 lines
arch/arm/mach-imx/ocotp.c +8 -0
+int imx6_ocotp_read_one_u32(u32 index, u32 *pdata)
+{
+	int ret;
+	struct ocotp_regs *otp = (struct ocotp_regs *)MX6_OCOTP_BASE_ADDR;
+
+	ret = imx6_ocotp_prepare();
+	if (ret) {
+		printf("ERROR: read preparation failed\n");

Driver should always use dev_err and friends for printing messages. Also, it often helps printing the actual error number aswell, because that's the next thing people want to know when they get an error.

arch/arm/mach-imx/ocotp.c static int imx_ocotp_probe(struct device_d *dev) · line 366

Quoted 8 lines
arch/arm/mach-imx/ocotp.c +4 -0
@@ -74,12 +360,30 @@ static int imx_ocotp_probe(struct device_d *dev)
 {
 	void __iomem *base;
 
+	struct ocotp_priv *priv;
+	struct cdev *cdev;
+	int ret = 0;
+

Add the following line to arch/arm/mach-imx/clk-imx6.c:

clkdev_add_physbase(clks[ipg], MX6_OCOTP_BASE_ADDR, NULL);

And then in this function do a:

	priv->clk = clk_get(dev, NULL);

ocotp_priv needs a struct clk *clk member. You have to pass a pointer to this around to the function using it.

Quoted 9 lines
arch/arm/mach-imx/ocotp.c +3 -0
 	base = dev_request_mem_region(dev, 0);
 	if (!base)
 		return -EBUSY;
 
 	imx_ocotp_init_dt(dev, base);
 
+	priv = xzalloc(sizeof(*priv));
+
+	priv->base	= base;

arch/arm/mach-imx/ocotp.c static int imx_ocotp_probe(struct device_d *dev) · line 385

Quoted 10 lines
arch/arm/mach-imx/ocotp.c +10 -0
+	cdev		= &priv->cdev;
+	cdev->dev	= dev;
+	cdev->ops	= &imx6_ocotp_ops;
+	cdev->priv	= priv;
+	cdev->size	= 32;
+	cdev->name	= asprintf("imx-ocotp");
+	if (cdev->name == NULL)
+		return -ENOMEM;
+
+	ret = devfs_create(cdev);

return ret.

Quoted 11 lines
arch/arm/mach-imx/ocotp.c +1 -0
+
 	return 0;
 }
 
commands/Kconfig +0 -0
index 1e07b5b..560e426 100644
@@ -733,6 +733,16 @@ config CMD_MIITOOL
 	  detailed MII status information, such as MII capabilities,
 	  current advertising mode, and link partner capabilities.

commands/Kconfig config CMD_MIITOOL · line 744

Quoted 10 lines
commands/Kconfig +9 -0
 
+config CMD_OCOTPTOOL
+	bool
+	depends on IMX_OCOTP
+	prompt "ocotptool"
+	help
+	  The ocotptool command allows to read and write to i.MX6 On-Chip
+	  OTP registers from barebox shell. Usage:
+		"ocotptool read mac" to read MAC
+		"ocotptool blow mac xx:xx:xx:xx:xx:xx" to write MAC

As suggested in my last mail:

Do we need this tool at all? We can add a .macaddr parameter to the ocotp device using dev_add_param_mac() (This function is new and currently only in the -next branch)

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

Hello, Sasha.

It seems I've fixed all according your remarks instead of one question.

10.04.2014 09:16, Sascha Hauer пишет:

Quoted 7 lines

As suggested in my last mail:

Do we need this tool at all? We can add a .macaddr parameter to the ocotp device using dev_add_param_mac() (This function is new and currently only in the -next branch)

How can we to use such parameters (even not macaddr, just bool)?

For example, I'm trying just to add bool parameter in imx_ocotp_probe(), for additional FUSE write protection:

Quoted 4 lines
if (IS_ENABLED(CONFIG_IMX_OCOTP_WRITE)) {
	dev_add_param_bool(dev, "permanent_write_enable",
		NULL, NULL, &priv->write_enable, NULL);
}

and check priv->write_enable instead of CONFIG_IMX_OCOTP_WRITE in write function.

But how to set permanent_write_enable to 1 in barebox shell?
As I understand, it should be something like ocotp0.permanent_write_enable=1, but I don't see any similar.

If I add

Quoted 2 lines

add_generic_device("ocotp", 0, NULL, MX6_OCOTP_BASE_ADDR, 0x2000,

		   IORESOURCE_MEM, NULL);

to board.c, I see something like 21bc000.ocotp.write_enable= available, but 21bc000.ocotp.write_enable=1 or 21bc000.ocotp.write_enable=0 doesn't work, saying "No such file or directory".

Also, as I understand, it's an incorrect in field of using devicetree.

One more question: is there any way in barebox shell to write to character device with offset? For example, ocotptool just writes (reads) 8 bytes to ocotp cdev with offset 0x22*4 (MAC offset). How to do it from shell?

Signature · 8 lines
With regards,
Uladzimir Bely.


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#5 lore raw
Quoted 31 lines · Uladzimir Bely

Hello, Sasha.

It seems I've fixed all according your remarks instead of one question.

10.04.2014 09:16, Sascha Hauer пишет:

Quoted 7 lines

As suggested in my last mail:

Do we need this tool at all? We can add a .macaddr parameter to the ocotp device using dev_add_param_mac() (This function is new and currently only in the -next branch)

How can we to use such parameters (even not macaddr, just bool)?

For example, I'm trying just to add bool parameter in imx_ocotp_probe(), for additional FUSE write protection:

Quoted 4 lines
if (IS_ENABLED(CONFIG_IMX_OCOTP_WRITE)) {
	dev_add_param_bool(dev, "permanent_write_enable",
		NULL, NULL, &priv->write_enable, NULL);
}

and check priv->write_enable instead of CONFIG_IMX_OCOTP_WRITE in write function.

But how to set permanent_write_enable to 1 in barebox shell?
As I understand, it should be something like ocotp0.permanent_write_enable=1, but I don't see any similar.

If I add

Quoted 8 lines
Quoted 2 lines

add_generic_device("ocotp", 0, NULL, MX6_OCOTP_BASE_ADDR, 0x2000,

		   IORESOURCE_MEM, NULL);

to board.c, I see something like 21bc000.ocotp.write_enable= available, but 21bc000.ocotp.write_enable=1 or 21bc000.ocotp.write_enable=0 doesn't work, saying "No such file or directory".

Also, as I understand, it's an incorrect in field of using devicetree.

Sorry, I didn't remember this. Normally this is correct. We use devname.parametername=value. With devicetree probing the devicename itself has a dot in it and barebox doesn't handle this propertly. I'm unsure if we can fix this. What you can do is register a logical device in the probe function:

struct ocotp_priv {
	...
	struct device dev;
	...
};

int ocotp_probe(struct device_d *dev)

{
	...
	strcpy(priv->dev.name, "ocotp");
	priv->dev.parent = dev;
	register_device(&priv->dev);
}

Then register the parameters on the new device and you can access them with ocotp.write_enable=1.

Quoted 4 lines

One more question: is there any way in barebox shell to write to character device with offset? For example, ocotptool just writes (reads) 8 bytes to ocotp cdev with offset 0x22*4 (MAC offset). How to do it from shell?

You can use md/mw to access offsets. Reading at an offset is:

md -s /dev/ocotp -b 0x10+1

reads one byte at offset 0x10 and:

mw -d /dev/ocotp -b 0x10 0xde

writes 0xde at offset 0x10. See 'help md' or 'help mw' for more information.

What you can't do is reading bytes into variables.

That said, of cause it's cumbersome to handle MAC addresses with these commands.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

PATCH 1/1 imx6: ocotp: Add On-Chip OTP registers write support

In reply to Sascha Hauer

For example, writing MAC 12:34:56:78:9A:BC can be performed as

    mw -l -d /dev/imx-ocotp 0x8c 0x00001234
    mw -l -d /dev/imx-ocotp 0x88 0x56789ABC
and reading as
    md -l -s /dev/imx-ocotp 0x88+8
    00000088: 56789ABC 00001234

, where 0x88 (0x22*4) and 0x8C (0x23*4) are offsets of MAC OTP registers.

Notice: FUSEs are PROM, so "0" (unprogrammed) bits can be replaced with "1" (but not vice versa) only once.

---
 arch/arm/mach-imx/Kconfig    |  10 ++
 arch/arm/mach-imx/clk-imx6.c |   1 +
 arch/arm/mach-imx/ocotp.c    | 285 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 296 insertions(+)
arch/arm/mach-imx/Kconfig +10 -0
index 81d16e1..556c7dc 100644
@@ -593,6 +593,16 @@ config IMX_OCOTP
 	  only supported functionality is reading the MAC address and assigning
 	  it to an ethernet device.
 
+config IMX_OCOTP_WRITE
+	bool
+	  prompt "Enable write support of i.MX6 CPUs OTP fuses"
+	  depends on IMX_OCOTP
+	help
+	  This adds write support to IMX6 On-Chip OTP registers.
+	  Example of set MAC to 12:34:56:78:9A:BC (2 words with offset 0x22 * 4):
+		mw -l -d /dev/imx-ocotp 0x8C 0x00001234
+		mw -l -d /dev/imx-ocotp 0x88 0x56789ABC
+
 endmenu
 
 endif
arch/arm/mach-imx/clk-imx6.c +1 -0
index c32b6cc..5d2d9d0 100644
@@ -294,6 +294,7 @@ static int imx6_ccm_probe(struct device_d *dev)
 	clkdev_add_physbase(clks[ecspi_root], MX6_ECSPI5_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[ipg_per], MX6_GPT_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[ipg], MX6_ENET_BASE_ADDR, NULL);
+	clkdev_add_physbase(clks[ipg], MX6_OCOTP_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc1_podf], MX6_USDHC1_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc2_podf], MX6_USDHC2_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc3_podf], MX6_USDHC3_BASE_ADDR, NULL);
arch/arm/mach-imx/ocotp.c +285 -0

309 lines not on this page · raw message

Signature · 7 lines
1.8.3.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH 1/1 imx6: ocotp: Add On-Chip OTP registers write support

In reply to Sascha Hauer

FUSEs (OTP registers) can be written via /dev/imx-ocotp character device.

For example, writing MAC 12:34:56:78:9A:BC can be performed as

Quoted 2 lines

mw -l -d /dev/imx-ocotp 0x8c 0x00001234
mw -l -d /dev/imx-ocotp 0x88 0x56789ABC

and reading as

Quoted 1 line

md -l -s /dev/imx-ocotp 0x88+8

    00000088: 56789ABC 00001234

, where 0x88 (0x22*4) and 0x8C (0x23*4) are offsets of MAC OTP registers.

Notice: FUSEs are PROM, so "0" (unprogrammed) bits can be replaced with "1" (but not vice versa) only once.

Also, for MAC there are convinient parameters:

Quoted 2 lines

ocotp0.permanent_write_enable=1
ocotp0.mac_addr=12:34:56:78:9A:BC

imx_ocotp 21bc000.ocotp: reloading shadow registers... imx_ocotp 21bc000.ocotp: reloading shadow registers... > echo $ocotp0.mac_addr
12:34:56:78:9A:BC

---
 arch/arm/mach-imx/Kconfig    |  10 ++
 arch/arm/mach-imx/clk-imx6.c |   1 +
 arch/arm/mach-imx/ocotp.c    | 332 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 343 insertions(+)
arch/arm/mach-imx/Kconfig +10 -0
index d40c944..c2613bf 100644
@@ -621,6 +621,16 @@ config IMX_OCOTP
 	  only supported functionality is reading the MAC address and assigning
 	  it to an ethernet device.
 
+config IMX_OCOTP_WRITE
+	bool
+	  prompt "Enable write support of i.MX6 CPUs OTP fuses"
+	  depends on IMX_OCOTP
+	help
+	  This adds write support to IMX6 On-Chip OTP registers.
+	  Example of set MAC to 12:34:56:78:9A:BC (2 words with offset 0x22 * 4):
+		mw -l -d /dev/imx-ocotp 0x8C 0x00001234
+		mw -l -d /dev/imx-ocotp 0x88 0x56789ABC
+
 endmenu
 
 endif
arch/arm/mach-imx/clk-imx6.c +1 -0
index 518fc00..4598a62 100644
@@ -433,6 +433,7 @@ static int imx6_ccm_probe(struct device_d *dev)
 	clkdev_add_physbase(clks[ecspi_root], MX6_ECSPI5_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[ipg_per], MX6_GPT_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[ipg], MX6_ENET_BASE_ADDR, NULL);
+	clkdev_add_physbase(clks[ipg], MX6_OCOTP_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc1_podf], MX6_USDHC1_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc2_podf], MX6_USDHC2_BASE_ADDR, NULL);
 	clkdev_add_physbase(clks[usdhc3_podf], MX6_USDHC3_BASE_ADDR, NULL);
arch/arm/mach-imx/ocotp.c +332 -0

360 lines not on this page · raw message

Signature · 7 lines
1.8.3.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#7 lore raw

Hi Uladzimir,

Sorry for the delay, I was on holidays.

Quoted 13 lines · Uladzimir Bely

FUSEs (OTP registers) can be written via /dev/imx-ocotp character device.

For example, writing MAC 12:34:56:78:9A:BC can be performed as

Quoted 2 lines

mw -l -d /dev/imx-ocotp 0x8c 0x00001234
mw -l -d /dev/imx-ocotp 0x88 0x56789ABC

and reading as

Quoted 1 line

md -l -s /dev/imx-ocotp 0x88+8

    00000088: 56789ABC 00001234

, where 0x88 (0x22*4) and 0x8C (0x23*4) are offsets of MAC OTP registers.

Notice: FUSEs are PROM, so "0" (unprogrammed) bits can be replaced with "1" (but not vice versa) only once.

Quoted 8 lines

Also, for MAC there are convinient parameters:

Quoted 2 lines

ocotp0.permanent_write_enable=1
ocotp0.mac_addr=12:34:56:78:9A:BC

imx_ocotp 21bc000.ocotp: reloading shadow registers... imx_ocotp 21bc000.ocotp: reloading shadow registers... > echo $ocotp0.mac_addr
12:34:56:78:9A:BC

This version looks mostly fine. I'd like to include the following into your patch. This changes the behaviour for the shadow fuse registers. It introduces a 'sense_enable' boolean parameter. If it is false the driver will read from the shadow registers, otherwise it explicitly senses the fuses. Also, writing is always allowed. If permanent_write_enable is false, we will write to the shadow registers instead of the fuses. This allows for some operations without burning the precious fuses.

Sascha

From be9d6004d0bfd2dd403c94b33ba00186b173b250 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <[email protected]>
Date: Thu, 24 Apr 2014 12:58:23 +0200
Subject: [PATCH] fixup! imx6: ocotp: Add On-Chip OTP registers write support

---
 arch/arm/mach-imx/ocotp.c | 110 ++++++++++++++++++++++++++++------------------
 1 file changed, 67 insertions(+), 43 deletions(-)
arch/arm/mach-imx/ocotp.c +67 -43
index 0ad5b46..476b376 100644
@@ -75,7 +75,8 @@ struct ocotp_priv {
 	void __iomem *base;
 	struct clk *clk;
 	struct device_d dev;
-	int write_enable;
+	int permanent_write_enable;
+	int sense_enable;
 	char ethaddr[6];
 };
 
@@ -102,8 +103,8 @@ static int imx6_ocotp_set_timing(struct ocotp_priv *priv)
 
 static int imx6_ocotp_wait_busy(u32 flags, struct ocotp_priv *priv)
 {
-
 	uint64_t start = get_time_ns();
+
 	while ((OCOTP_CTRL_BUSY | OCOTP_CTRL_ERROR | flags) &
 			readl(priv->base)) {
 		if (is_timeout(start, MSECOND)) {
@@ -158,43 +159,53 @@ int imx6_ocotp_read_one_u32(u32 index, u32 *pdata, struct ocotp_priv *priv)
 
 	ret = imx6_ocotp_prepare(priv);
 	if (ret) {
-		dev_err(priv->cdev.dev, "prepare to read failed\n");
+		dev_err(priv->cdev.dev, "failed to prepare read fuse 0x%08x\n",
+				index);
 		return ret;
 	}
 
 	ret = fuse_read_addr(index, pdata, priv);
 	if (ret) {
-		dev_err(priv->cdev.dev, "read failed\n");
+		dev_err(priv->cdev.dev, "failed to read fuse 0x%08x\n", index);
 		return ret;
 	}
 
 	if (readl(priv->base) & OCOTP_CTRL_ERROR) {
-		dev_err(priv->cdev.dev, "bad read status\n");
+		dev_err(priv->cdev.dev, "bad read status at fuse 0x%08x\n", index);
 		return -EFAULT;
 	}
+
 	return 0;
 }
 
 static ssize_t imx6_ocotp_cdev_read(struct cdev *cdev, void *buf,
-		size_t count, loff_t offset, ulong flags)
+		size_t count, loff_t offset, unsigned long flags)
 {
-	ulong i;
 	u32 index;
-	size_t read_count = 0;
+	ssize_t read_count = 0;
+	int ret, i;
+	struct ocotp_priv *priv = container_of(cdev, struct ocotp_priv, cdev);
 
 	index = offset >> 2;
 	count >>= 2;
+
 	if (count > (FUSE_REGS_COUNT - index))
 		count = FUSE_REGS_COUNT - index - 1;
 
 	for (i = index; i < (index + count); i++) {
-		imx6_ocotp_read_one_u32(i, buf, cdev->priv);
+		if (priv->sense_enable) {
+			ret = imx6_ocotp_read_one_u32(i, buf, priv);
+			if (ret)
+				return ret;
+		} else {
+			*(u32 *)buf = readl(priv->base + 0x400 + i * 0x10);
+		}
+
 		buf += 4;
 		read_count++;
 	}
-	read_count <<= 2;
 
-	return read_count;
+	return read_count << 2;
 }
 
 static int fuse_blow_addr(u32 addr, u32 value, struct ocotp_priv *priv)
@@ -245,12 +256,6 @@ int imx6_ocotp_blow_one_u32(u32 index, u32 data, u32 *pfused_value,
 		return ret;
 	}
 
-	ret = imx6_ocotp_reload_shadow(priv);
-	if (ret) {
-		dev_err(priv->cdev.dev, "reload shadow registers failed\n");
-		return ret;
-	}
-
 	if (readl(priv->base) & OCOTP_CTRL_ERROR) {
 		dev_err(priv->cdev.dev, "bad write status\n");
 		return -EFAULT;
@@ -262,39 +267,56 @@ int imx6_ocotp_blow_one_u32(u32 index, u32 data, u32 *pfused_value,
 }
 
 static ssize_t imx6_ocotp_cdev_write(struct cdev *cdev, const void *buf,
-		size_t count, loff_t offset, ulong flags)
+		size_t count, loff_t offset, unsigned long flags)
 {
-	ulong size, index, i;
-	u32 data, pfuse;
-
 	struct ocotp_priv *priv = cdev->priv;
+	int index, i;
+	ssize_t write_count = 0;
+	const u32 *data;
+	u32 pfuse;
+	int ret;
+
+	/* We could do better, but currently this is what's implemented */
+	if (offset & 0x3 || count & 0x3) {
+		dev_err(cdev->dev, "only u32 aligned writes allowed\n");
+		return -EINVAL;
+	}
 
 	index = offset >> 2;
-	size = count >> 2;
+	count >>= 2;
 
-	if (!priv->write_enable) {
-		dev_err(cdev->dev, "operation not permitted\n");
-		return -EPERM;
-	}
+	if (count > (FUSE_REGS_COUNT - index))
+		count = FUSE_REGS_COUNT - index - 1;
 
-	if (size > (FUSE_REGS_COUNT - index))
-		size = FUSE_REGS_COUNT - index - 1;
+	data = buf;
 
-	for (i = 0; i < size; i++) {
-		int ret;
+	for (i = index; i < (index + count); i++) {
+		if (priv->permanent_write_enable) {
+			ret = imx6_ocotp_blow_one_u32(i, *data,
+					&pfuse, priv);
+			if (ret < 0) {
+				goto out;
+			}
+		} else {
+			writel(*data, priv->base + 0x400 + i * 0x10);
+		}
 
-		memcpy(&data, buf + i * 4, 4);
-		ret = imx6_ocotp_blow_one_u32(index + i, data,
-				&pfuse, cdev->priv);
-		if (ret < 0)
-			return ret;
+		data++;
+		write_count++;
 	}
 
-	return count;
+	ret = 0;
+
+out:
+	if (priv->permanent_write_enable)
+		imx6_ocotp_reload_shadow(priv);
+
+	return ret < 0 ? ret : (write_count << 2);
 }
 
 static struct file_operations imx6_ocotp_ops = {
 	.read	= imx6_ocotp_cdev_read,
+	.write	= imx6_ocotp_cdev_write,
 	.lseek	= dev_lseek_default,
 };
 
@@ -354,13 +376,15 @@ static int imx_ocotp_set_mac(struct param_d *param, void *priv)
 {
 	struct ocotp_priv *ocotp_priv = priv;
 	char buf[8];
-	int i;
+	int i, ret;
 
 	for (i = 0; i < 6; i++)
 		buf[5 - i] = ocotp_priv->ethaddr[i];
 	buf[6] = 0; buf[7] = 0;
 
-	imx6_ocotp_cdev_write(&ocotp_priv->cdev, buf, MAC_BYTES, MAC_OFFSET, 0);
+	ret = imx6_ocotp_cdev_write(&ocotp_priv->cdev, buf, MAC_BYTES, MAC_OFFSET, 0);
+	if (ret < 0)
+		return ret;
 
 	return 0;
 }
@@ -368,7 +392,6 @@ static int imx_ocotp_set_mac(struct param_d *param, void *priv)
 static int imx_ocotp_probe(struct device_d *dev)
 {
 	void __iomem *base;
-
 	struct ocotp_priv *priv;
 	struct cdev *cdev;
 	int ret = 0;
@@ -405,13 +428,14 @@ static int imx_ocotp_probe(struct device_d *dev)
 	register_device(&priv->dev);
 
 	if (IS_ENABLED(CONFIG_IMX_OCOTP_WRITE)) {
-		imx6_ocotp_ops.write = imx6_ocotp_cdev_write;
 		dev_add_param_bool(&(priv->dev), "permanent_write_enable",
-				NULL, NULL, &priv->write_enable, NULL);
-		dev_add_param_mac(&(priv->dev), "mac_addr", imx_ocotp_set_mac,
-				imx_ocotp_get_mac, priv->ethaddr, priv);
+				NULL, NULL, &priv->permanent_write_enable, NULL);
 	}
 
+	dev_add_param_mac(&(priv->dev), "mac_addr", imx_ocotp_set_mac,
+			imx_ocotp_get_mac, priv->ethaddr, priv);
+	dev_add_param_bool(&(priv->dev), "sense_enable", NULL, NULL, &priv->sense_enable, priv);
+
 	return 0;
 }
 
Signature · 12 lines
1.9.1

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox