public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Benoît Thébaudeau" <benoit.thebaudeau@advansee.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 4/5] Add fsl_iim driver
Date: Tue, 27 Nov 2012 14:31:39 +0100 (CET)	[thread overview]
Message-ID: <569176563.2224213.1354023099932.JavaMail.root@advansee.com> (raw)
In-Reply-To: <867526288.2224095.1354023015881.JavaMail.root@advansee.com>

Add a fsl_iim driver common to i.MX and MPC.

Signed-off-by: Beno?t Th?baudeau <benoit.thebaudeau@advansee.com>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Stefano Babic <sbabic@denx.de>
---
This patch supersedes http://patchwork.ozlabs.org/patch/177245/ .
Changes for v2:
 - Rebase against latest master.

 .../drivers/misc/Makefile                          |    1 +
 /dev/null => u-boot-178d0cc/drivers/misc/fsl_iim.c |  318 ++++++++++++++++++++
 2 files changed, 319 insertions(+)
 create mode 100644 u-boot-178d0cc/drivers/misc/fsl_iim.c

diff --git u-boot-178d0cc.orig/drivers/misc/Makefile u-boot-178d0cc/drivers/misc/Makefile
index cdec88b..0833d78 100644
--- u-boot-178d0cc.orig/drivers/misc/Makefile
+++ u-boot-178d0cc/drivers/misc/Makefile
@@ -27,6 +27,7 @@ LIB	:= $(obj)libmisc.o
 
 COBJS-$(CONFIG_ALI152X) += ali512x.o
 COBJS-$(CONFIG_DS4510)  += ds4510.o
+COBJS-$(CONFIG_FSL_IIM) += fsl_iim.o
 COBJS-$(CONFIG_FSL_LAW) += fsl_law.o
 COBJS-$(CONFIG_GPIO_LED) += gpio_led.o
 COBJS-$(CONFIG_FSL_MC9SDZ60) += mc9sdz60.o
diff --git u-boot-178d0cc/drivers/misc/fsl_iim.c u-boot-178d0cc/drivers/misc/fsl_iim.c
new file mode 100644
index 0000000..1a3d5fc
--- /dev/null
+++ u-boot-178d0cc/drivers/misc/fsl_iim.c
@@ -0,0 +1,318 @@
+/*
+ * (C) Copyright 2009-2012 ADVANSEE
+ * Beno?t Th?baudeau <benoit.thebaudeau@advansee.com>
+ *
+ * Based on the mpc512x iim code:
+ * Copyright 2008 Silicon Turnkey Express, Inc.
+ * Martha Marx <mmarx@silicontkx.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <config.h>
+#include <common.h>
+#include <hwconfig.h>
+#include <fuse.h>
+#include <asm/errno.h>
+#include <asm/byteorder.h>
+#include <asm/io.h>
+
+/* FSL IIM-specific constants */
+#define STAT_BUSY		0x80
+#define STAT_PRGD		0x02
+#define STAT_SNSD		0x01
+
+#define STATM_PRGD_M		0x02
+#define STATM_SNSD_M		0x01
+
+#define ERR_PRGE		0x80
+#define ERR_WPE			0x40
+#define ERR_OPE			0x20
+#define ERR_RPE			0x10
+#define ERR_WLRE		0x08
+#define ERR_SNSE		0x04
+#define ERR_PARITYE		0x02
+
+#define EMASK_PRGE_M		0x80
+#define EMASK_WPE_M		0x40
+#define EMASK_OPE_M		0x20
+#define EMASK_RPE_M		0x10
+#define EMASK_WLRE_M		0x08
+#define EMASK_SNSE_M		0x04
+#define EMASK_PARITYE_M		0x02
+
+#define FCTL_DPC		0x80
+#define FCTL_PRG_LENGTH_MASK	0x70
+#define FCTL_ESNS_N		0x08
+#define FCTL_ESNS_0		0x04
+#define FCTL_ESNS_1		0x02
+#define FCTL_PRG		0x01
+
+#define UA_A_BANK_MASK		0x38
+#define UA_A_ROWH_MASK		0x07
+
+#define LA_A_ROWL_MASK		0xf8
+#define LA_A_BIT_MASK		0x07
+
+#define PREV_PROD_REV_MASK	0xf8
+#define PREV_PROD_VT_MASK	0x07
+
+/* Select the correct accessors depending on endianness */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define iim_read32		in_le32
+#define iim_write32		out_le32
+#define iim_clrsetbits32	clrsetbits_le32
+#define iim_clrbits32		clrbits_le32
+#define iim_setbits32		setbits_le32
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define iim_read32		in_be32
+#define iim_write32		out_be32
+#define iim_clrsetbits32	clrsetbits_be32
+#define iim_clrbits32		clrbits_be32
+#define iim_setbits32		setbits_be32
+#else
+#error "Endianess is not defined: please fix to continue"
+#endif
+
+/* IIM control registers */
+struct fsl_iim {
+	u32 stat;
+	u32 statm;
+	u32 err;
+	u32 emask;
+	u32 fctl;
+	u32 ua;
+	u32 la;
+	u32 sdat;
+	u32 prev;
+	u32 srev;
+	u32 prg_p;
+	u32 scs[0x1f5];
+	struct {
+		u32 row[0x100];
+	} bank[8];
+};
+
+int fuse_read_bit(u32 bank, u32 row, u32 bit, u32 *val)
+{
+	int ret;
+
+	if (bit >= 8) {
+		puts("fsl_iim fuse read: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	ret = fuse_read_row(bank, row, val);
+	if (ret)
+		return ret;
+
+	*val = !!(*val & 1 << bit);
+	return 0;
+}
+
+int fuse_read_row(u32 bank, u32 row, u32 *val)
+{
+	volatile struct fsl_iim *regs =
+			(struct fsl_iim *)CONFIG_SYS_FSL_IIM_ADDR;
+	u32 err;
+
+	if (bank >= ARRAY_SIZE(regs->bank) ||
+			row >= ARRAY_SIZE(regs->bank[0].row) ||
+			val == NULL) {
+		puts("fsl_iim fuse read: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	iim_write32(&regs->err, iim_read32(&regs->err));
+	*val = iim_read32(&regs->bank[bank].row[row]);
+	err = iim_read32(&regs->err);
+	iim_write32(&regs->err, iim_read32(&regs->err));
+
+	if (err & ERR_RPE) {
+		puts("fsl_iim fuse read: Read protect error\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int fuse_sense_bit(u32 bank, u32 row, u32 bit, u32 *val)
+{
+	int ret;
+
+	if (bit >= 8) {
+		puts("fsl_iim fuse sense: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	ret = fuse_sense_row(bank, row, val);
+	if (ret)
+		return ret;
+
+	*val = !!(*val & 1 << bit);
+	return 0;
+}
+
+int fuse_sense_row(u32 bank, u32 row, u32 *val)
+{
+	volatile struct fsl_iim *regs =
+			(struct fsl_iim *)CONFIG_SYS_FSL_IIM_ADDR;
+	u32 stat, err;
+
+	if (bank >= ARRAY_SIZE(regs->bank) ||
+			row >= ARRAY_SIZE(regs->bank[0].row) ||
+			val == NULL) {
+		puts("fsl_iim fuse sense: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	iim_write32(&regs->stat, iim_read32(&regs->stat));
+	iim_write32(&regs->err, iim_read32(&regs->err));
+	iim_write32(&regs->ua, bank << 3 | row >> 5);
+	iim_write32(&regs->la, row << 3 & 0xff);
+	iim_write32(&regs->fctl, iim_read32(&regs->fctl) | FCTL_ESNS_N);
+	while (iim_read32(&regs->stat) & STAT_BUSY)
+		udelay(20);
+	stat = iim_read32(&regs->stat);
+	err = iim_read32(&regs->err);
+	iim_write32(&regs->stat, iim_read32(&regs->stat));
+	iim_write32(&regs->err, iim_read32(&regs->err));
+
+	if (err & ERR_SNSE) {
+		puts("fsl_iim fuse sense: Explicit sense cycle error\n");
+		return -EIO;
+	}
+
+	if (!(stat & STAT_SNSD)) {
+		puts("fsl_iim fuse sense: Explicit sense cycle "
+				"did not complete\n");
+		return -EIO;
+	}
+
+	*val = iim_read32(&regs->sdat);
+	return 0;
+}
+
+int fuse_prog_bit(u32 bank, u32 row, u32 bit)
+{
+	volatile struct fsl_iim *regs =
+			(struct fsl_iim *)CONFIG_SYS_FSL_IIM_ADDR;
+	u32 stat, err;
+
+	if (bank >= ARRAY_SIZE(regs->bank) ||
+			row >= ARRAY_SIZE(regs->bank[0].row) ||
+			bit >= 8) {
+		puts("fsl_iim fuse program: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	iim_write32(&regs->stat, iim_read32(&regs->stat));
+	iim_write32(&regs->err, iim_read32(&regs->err));
+	iim_write32(&regs->ua, bank << 3 | row >> 5);
+	iim_write32(&regs->la, (row << 3 | bit) & 0xff);
+	iim_write32(&regs->prg_p, 0xaa);
+	iim_write32(&regs->fctl, iim_read32(&regs->fctl) | FCTL_PRG);
+	while (iim_read32(&regs->stat) & STAT_BUSY)
+		udelay(20);
+	stat = iim_read32(&regs->stat);
+	err = iim_read32(&regs->err);
+	iim_write32(&regs->stat, iim_read32(&regs->stat));
+	iim_write32(&regs->err, iim_read32(&regs->err));
+	iim_write32(&regs->prg_p, 0x00);
+
+	if (err & ERR_PRGE) {
+		puts("fsl_iim fuse program: Program error\n");
+		return -EIO;
+	}
+
+	if (err & ERR_WPE) {
+		puts("fsl_iim fuse program: Write protect error\n");
+		return -EIO;
+	}
+
+	if (!(stat & STAT_PRGD)) {
+		puts("fsl_iim fuse program: Program did not complete\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int fuse_prog_row(u32 bank, u32 row, u32 val)
+{
+	int bit, ret;
+
+	if (val & ~0xff) {
+		puts("fsl_iim fuse program: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	for (bit = 0; val; bit++, val >>= 1)
+		if (val & 0x01) {
+			ret = fuse_prog_bit(bank, row, bit);
+			if (ret)
+				return ret;
+		}
+
+	return 0;
+}
+
+int fuse_override_bit(u32 bank, u32 row, u32 bit, u32 val)
+{
+	u32 row_val;
+	int ret;
+
+	if (bit >= 8 || val > 1) {
+		puts("fsl_iim fuse override: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	ret = fuse_read_row(bank, row, &row_val);
+	if (ret)
+		return ret;
+
+	return fuse_override_row(bank, row,
+			(row_val & ~(1 << bit)) | val << bit);
+}
+
+int fuse_override_row(u32 bank, u32 row, u32 val)
+{
+	volatile struct fsl_iim *regs =
+			(struct fsl_iim *)CONFIG_SYS_FSL_IIM_ADDR;
+	u32 err;
+
+	if (bank >= ARRAY_SIZE(regs->bank) ||
+			row >= ARRAY_SIZE(regs->bank[0].row) ||
+			val & ~0xff) {
+		puts("fsl_iim fuse override: Invalid argument\n");
+		return -EINVAL;
+	}
+
+	iim_write32(&regs->err, iim_read32(&regs->err));
+	iim_write32(&regs->bank[bank].row[row], val);
+	err = iim_read32(&regs->err);
+	iim_write32(&regs->err, iim_read32(&regs->err));
+
+	if (err & ERR_OPE) {
+		puts("fsl_iim fuse override: Override protect error\n");
+		return -EIO;
+	}
+
+	return 0;
+}

  parent reply	other threads:[~2012-11-27 13:31 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-14 12:51 [U-Boot] [PATCH 0/5] Make iim support common to mpc and imx Benoît Thébaudeau
2012-08-14 12:52 ` [U-Boot] [PATCH 1/5] imx iim: Homogenize and fix register definitions Benoît Thébaudeau
2012-11-27 13:30   ` [U-Boot] [PATCH v2 " Benoît Thébaudeau
2012-11-27 13:30     ` [U-Boot] [PATCH v2 2/5] imx iim: Add useful fuse definitions Benoît Thébaudeau
2012-11-27 13:31     ` [U-Boot] [PATCH v2 3/5] Add fuse API and commands Benoît Thébaudeau
2012-11-27 13:31     ` Benoît Thébaudeau [this message]
2012-11-27 13:32     ` [U-Boot] [PATCH v2 5/5] mpc iim: Switch to common fsl_iim Benoît Thébaudeau
2013-03-18 12:07     ` [U-Boot] [PATCH v2 1/5] imx iim: Homogenize and fix register definitions Stefano Babic
2013-03-18 12:08       ` Benoît Thébaudeau
2013-03-18 12:13         ` Benoît Thébaudeau
2012-08-14 12:52 ` [U-Boot] [PATCH 2/5] imx iim: Add useful fuse definitions Benoît Thébaudeau
2012-08-14 12:52 ` [U-Boot] [PATCH 3/5] Add fuse API and commands Benoît Thébaudeau
2012-08-21  8:11   ` Stefano Babic
2012-08-21 10:14     ` Benoît Thébaudeau
2012-08-22 10:43   ` Dirk Behme
2012-08-22 11:11     ` Benoît Thébaudeau
2012-08-22 16:25       ` Dirk Behme
2012-08-22 16:53         ` Benoît Thébaudeau
2012-08-23 10:31     ` Stefano Babic
2012-08-23 13:23       ` Eric Nelson
2012-11-26 16:03         ` Benoît Thébaudeau
2012-11-27  7:19           ` Dirk Behme
2012-11-27 16:58             ` Eric Nelson
2012-11-27 18:27               ` Benoît Thébaudeau
2012-11-27 18:36                 ` Eric Nelson
2012-11-27 19:23                   ` Benoît Thébaudeau
2012-11-27 19:54                     ` Eric Nelson
2012-12-03  9:03   ` Stefano Babic
2012-12-03 11:25     ` Benoît Thébaudeau
2012-12-03 11:41       ` Stefano Babic
2012-08-14 12:52 ` [U-Boot] [PATCH 4/5] Add fsl_iim driver Benoît Thébaudeau
2012-08-21  8:13   ` Stefano Babic
2012-08-21 12:56     ` Benoît Thébaudeau
2012-08-21 13:41       ` Stefano Babic
2012-08-14 12:53 ` [U-Boot] [PATCH 5/5] mpc iim: Switch to common fsl_iim Benoît Thébaudeau
2012-08-15 14:30   ` Benoît Thébaudeau
2012-08-21  8:15   ` Stefano Babic
2012-11-05 20:05     ` Benoît Thébaudeau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=569176563.2224213.1354023099932.JavaMail.root@advansee.com \
    --to=benoit.thebaudeau@advansee.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox