]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/misc/mxc_ocotp.c
mx6: ocotp: add timeout to busy wait loop
[karo-tx-uboot.git] / drivers / misc / mxc_ocotp.c
index 0095b471bdc81e9b064855ed949d258abb9cf3cf..f468d9d5839fdf0ec68418f890235468f26b1378 100644 (file)
@@ -9,23 +9,7 @@
  * 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
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #define BF(value, field)               (((value) << BO_##field) & BM_##field)
 
 #define WRITE_POSTAMBLE_US             2
+#define MXC_OTP_BUSY_TIMEOUT           1000
 
-static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
+static bool wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
 {
-       while (readl(&regs->ctrl) & BM_CTRL_BUSY)
+       unsigned long start;
+       u32 reg;
+
+       start = get_timer_masked();
+       while ((reg = readl(&regs->ctrl)) & BM_CTRL_BUSY) {
                udelay(delay_us);
+               if (get_timer(start) > MXC_OTP_BUSY_TIMEOUT)
+                       break;
+       }
+       if (!(reg & BM_CTRL_BUSY))
+               return 1;
+       return !(readl(&regs->ctrl) & BM_CTRL_BUSY);
 }
 
 static void clear_error(struct ocotp_regs *regs)
@@ -76,7 +71,7 @@ static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
        *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
 
        if (bank >= ARRAY_SIZE((*regs)->bank) ||
-                       word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
+                       word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) ||
                        !assert) {
                printf("mxc_ocotp %s(): Invalid argument\n", caller);
                return -EINVAL;
@@ -84,8 +79,10 @@ static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
 
        enable_ocotp_clk(1);
 
-       wait_busy(*regs, 1);
-       clear_error(*regs);
+       if (wait_busy(*regs, 1))
+               clear_error(*regs);
+       else
+               return -ETIMEDOUT;
 
        return 0;
 }
@@ -96,7 +93,6 @@ static int finish_access(struct ocotp_regs *regs, const char *caller)
 
        err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
        clear_error(regs);
-
        enable_ocotp_clk(0);
 
        if (err) {
@@ -138,8 +134,8 @@ static void set_timing(struct ocotp_regs *regs)
        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;
+       strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US,
+                                               1000000) + 2 * (relax + 1) - 1;
 
        timing = BF(strobe_read, TIMING_STROBE_READ) |
                        BF(relax, TIMING_RELAX) |
@@ -172,8 +168,10 @@ int fuse_sense(u32 bank, u32 word, u32 *val)
 
        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);
+       if (wait_busy(regs, 1))
+               *val = readl(&regs->read_fuse_data);
+       else
+               *val = ~0;
 
        return finish_access(regs, __func__);
 }