Add mxc_ocotp driver
authorBenoît Thébaudeau <benoit.thebaudeau@advansee.com>
Tue, 23 Apr 2013 10:17:44 +0000 (10:17 +0000)
committerStefano Babic <sbabic@denx.de>
Sun, 28 Apr 2013 09:07:43 +0000 (11:07 +0200)
Add an mxc_ocotp driver for i.MX6.

Signed-off-by: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
arch/arm/cpu/armv7/mx6/clock.c
arch/arm/include/asm/arch-mx6/clock.h
doc/README.mxc_ocotp [new file with mode: 0644]
drivers/misc/Makefile
drivers/misc/mxc_ocotp.c [new file with mode: 0644]

index 8cba4fd..3c0d908 100644 (file)
@@ -37,6 +37,20 @@ enum pll_clocks {
 
 struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
 
+#ifdef CONFIG_MXC_OCOTP
+void enable_ocotp_clk(unsigned char enable)
+{
+       u32 reg;
+
+       reg = __raw_readl(&imx_ccm->CCGR2);
+       if (enable)
+               reg |= MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
+       else
+               reg &= ~MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
+       __raw_writel(reg, &imx_ccm->CCGR2);
+}
+#endif
+
 void enable_usboh3_clk(unsigned char enable)
 {
        u32 reg;
index db377cc..cfd4edc 100644 (file)
@@ -61,6 +61,7 @@ enum mxc_clock {
 u32 imx_get_uartclk(void);
 u32 imx_get_fecclk(void);
 unsigned int mxc_get_clock(enum mxc_clock clk);
+void enable_ocotp_clk(unsigned char enable);
 void enable_usboh3_clk(unsigned char enable);
 int enable_sata_clock(void);
 int enable_i2c_clk(unsigned char enable, unsigned i2c_num);
diff --git a/doc/README.mxc_ocotp b/doc/README.mxc_ocotp
new file mode 100644 (file)
index 0000000..9a53311
--- /dev/null
@@ -0,0 +1,50 @@
+Driver implementing the fuse API for Freescale's On-Chip OTP Controller (OCOTP)
+on MXC
+
+This IP can be found on the following SoCs:
+ - i.MX6.
+
+Note that this IP is different from albeit similar to the IPs of the same name
+that can be found on the following SoCs:
+ - i.MX23,
+ - i.MX28,
+ - i.MX50.
+
+The section numbers in this file refer to the i.MX6 Reference Manual.
+
+A fuse word contains 32 fuse bit slots, as explained in 46.2.1.
+
+A bank contains 8 fuse word slots, as explained in 46.2.1 and shown by the
+memory map in 46.4.
+
+Some fuse bit or word slots may not have the corresponding fuses actually
+implemented in the fusebox.
+
+See the README files of the SoCs using this driver in order to know the
+conventions used by U-Boot to store some specific data in the fuses, e.g. MAC
+addresses.
+
+Fuse operations:
+
+   Read
+      Read operations are implemented as read accesses to the shadow registers,
+      using "Bankx Wordy" from the memory map in 46.4. This is explained in
+      detail by the first two paragraphs in 46.2.1.2.
+
+   Sense
+      Sense operations are implemented as the direct fusebox read explained by
+      the steps in 46.2.1.2.
+
+   Program
+      Program operations are implemented as explained by the steps in 46.2.1.3.
+      Following this operation, the shadow registers are not reloaded by the
+      hardware.
+
+   Override
+      Override operations are implemented as write accesses to the shadow
+      registers, as explained by the first paragraph in 46.2.1.3.
+
+Configuration:
+
+   CONFIG_MXC_OCOTP
+      Define this to enable the mxc_ocotp driver.
index c6dadd4..5d869b4 100644 (file)
@@ -31,6 +31,7 @@ COBJS-$(CONFIG_CBMEM_CONSOLE) += cbmem_console.o
 COBJS-$(CONFIG_FSL_IIM) += fsl_iim.o
 COBJS-$(CONFIG_GPIO_LED) += gpio_led.o
 COBJS-$(CONFIG_FSL_MC9SDZ60) += mc9sdz60.o
+COBJS-$(CONFIG_MXC_OCOTP) += mxc_ocotp.o
 COBJS-$(CONFIG_NS87308) += ns87308.o
 COBJS-$(CONFIG_PDSP188x) += pdsp188x.o
 COBJS-$(CONFIG_STATUS_LED) += status_led.o
diff --git a/drivers/misc/mxc_ocotp.c b/drivers/misc/mxc_ocotp.c
new file mode 100644 (file)
index 0000000..0095b47
--- /dev/null
@@ -0,0 +1,216 @@
+/*
+ * (C) Copyright 2013 ADVANSEE
+ * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
+ *
+ * Based on Dirk Behme's
+ * https://github.com/dirkbehme/u-boot-imx6/blob/28b17e9/drivers/misc/imx_otp.c,
+ * which is based on Freescale's
+ * http://git.freescale.com/git/cgit.cgi/imx/uboot-imx.git/tree/drivers/misc/imx_otp.c?h=imx_v2009.08_1.1.0&id=9aa74e6,
+ * which is:
+ * Copyright (C) 2011 Freescale Semiconductor, Inc.
+ *
+ * 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 <common.h>
+#include <fuse.h>
+#include <asm/errno.h>
+#include <asm/io.h>
+#include <asm/arch/clock.h>
+#include <asm/arch/imx-regs.h>
+
+#define BO_CTRL_WR_UNLOCK              16
+#define BM_CTRL_WR_UNLOCK              0xffff0000
+#define BV_CTRL_WR_UNLOCK_KEY          0x3e77
+#define BM_CTRL_ERROR                  0x00000200
+#define BM_CTRL_BUSY                   0x00000100
+#define BO_CTRL_ADDR                   0
+#define BM_CTRL_ADDR                   0x0000007f
+
+#define BO_TIMING_STROBE_READ          16
+#define BM_TIMING_STROBE_READ          0x003f0000
+#define BV_TIMING_STROBE_READ_NS       37
+#define BO_TIMING_RELAX                        12
+#define BM_TIMING_RELAX                        0x0000f000
+#define BV_TIMING_RELAX_NS             17
+#define BO_TIMING_STROBE_PROG          0
+#define BM_TIMING_STROBE_PROG          0x00000fff
+#define BV_TIMING_STROBE_PROG_US       10
+
+#define BM_READ_CTRL_READ_FUSE         0x00000001
+
+#define BF(value, field)               (((value) << BO_##field) & BM_##field)
+
+#define WRITE_POSTAMBLE_US             2
+
+static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
+{
+       while (readl(&regs->ctrl) & BM_CTRL_BUSY)
+               udelay(delay_us);
+}
+
+static void clear_error(struct ocotp_regs *regs)
+{
+       writel(BM_CTRL_ERROR, &regs->ctrl_clr);
+}
+
+static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
+                               int assert, const char *caller)
+{
+       *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
+
+       if (bank >= ARRAY_SIZE((*regs)->bank) ||
+                       word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
+                       !assert) {
+               printf("mxc_ocotp %s(): Invalid argument\n", caller);
+               return -EINVAL;
+       }
+
+       enable_ocotp_clk(1);
+
+       wait_busy(*regs, 1);
+       clear_error(*regs);
+
+       return 0;
+}
+
+static int finish_access(struct ocotp_regs *regs, const char *caller)
+{
+       u32 err;
+
+       err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
+       clear_error(regs);
+
+       enable_ocotp_clk(0);
+
+       if (err) {
+               printf("mxc_ocotp %s(): Access protect error\n", caller);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
+                       const char *caller)
+{
+       return prepare_access(regs, bank, word, val != NULL, caller);
+}
+
+int fuse_read(u32 bank, u32 word, u32 *val)
+{
+       struct ocotp_regs *regs;
+       int ret;
+
+       ret = prepare_read(&regs, bank, word, val, __func__);
+       if (ret)
+               return ret;
+
+       *val = readl(&regs->bank[bank].fuse_regs[word << 2]);
+
+       return finish_access(regs, __func__);
+}
+
+static void set_timing(struct ocotp_regs *regs)
+{
+       u32 ipg_clk;
+       u32 relax, strobe_read, strobe_prog;
+       u32 timing;
+
+       ipg_clk = mxc_get_clock(MXC_IPG_CLK);
+
+       relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
+       strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
+                                       1000000000) + 2 * (relax + 1) - 1;
+       strobe_prog = DIV_ROUND(ipg_clk * BV_TIMING_STROBE_PROG_US, 1000000) +
+                       2 * (relax + 1) - 1;
+
+       timing = BF(strobe_read, TIMING_STROBE_READ) |
+                       BF(relax, TIMING_RELAX) |
+                       BF(strobe_prog, TIMING_STROBE_PROG);
+
+       clrsetbits_le32(&regs->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
+                       BM_TIMING_STROBE_PROG, timing);
+}
+
+static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
+                               int write)
+{
+       u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
+       u32 addr = bank << 3 | word;
+
+       set_timing(regs);
+       clrsetbits_le32(&regs->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
+                       BF(wr_unlock, CTRL_WR_UNLOCK) |
+                       BF(addr, CTRL_ADDR));
+}
+
+int fuse_sense(u32 bank, u32 word, u32 *val)
+{
+       struct ocotp_regs *regs;
+       int ret;
+
+       ret = prepare_read(&regs, bank, word, val, __func__);
+       if (ret)
+               return ret;
+
+       setup_direct_access(regs, bank, word, false);
+       writel(BM_READ_CTRL_READ_FUSE, &regs->read_ctrl);
+       wait_busy(regs, 1);
+       *val = readl(&regs->read_fuse_data);
+
+       return finish_access(regs, __func__);
+}
+
+static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
+                               const char *caller)
+{
+       return prepare_access(regs, bank, word, true, caller);
+}
+
+int fuse_prog(u32 bank, u32 word, u32 val)
+{
+       struct ocotp_regs *regs;
+       int ret;
+
+       ret = prepare_write(&regs, bank, word, __func__);
+       if (ret)
+               return ret;
+
+       setup_direct_access(regs, bank, word, true);
+       writel(val, &regs->data);
+       wait_busy(regs, BV_TIMING_STROBE_PROG_US);
+       udelay(WRITE_POSTAMBLE_US);
+
+       return finish_access(regs, __func__);
+}
+
+int fuse_override(u32 bank, u32 word, u32 val)
+{
+       struct ocotp_regs *regs;
+       int ret;
+
+       ret = prepare_write(&regs, bank, word, __func__);
+       if (ret)
+               return ret;
+
+       writel(val, &regs->bank[bank].fuse_regs[word << 2]);
+
+       return finish_access(regs, __func__);
+}