Merge branch 'for_next' into for_linus
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-mxs / ocotp.c
1 /*
2  * Copyright 2010 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18
19 #include <asm/processor.h>      /* for cpu_relax() */
20
21 #include <mach/mxs.h>
22 #include <mach/common.h>
23
24 #define OCOTP_WORD_OFFSET               0x20
25 #define OCOTP_WORD_COUNT                0x20
26
27 #define BM_OCOTP_CTRL_BUSY              (1 << 8)
28 #define BM_OCOTP_CTRL_ERROR             (1 << 9)
29 #define BM_OCOTP_CTRL_RD_BANK_OPEN      (1 << 12)
30
31 static DEFINE_MUTEX(ocotp_mutex);
32 static u32 ocotp_words[OCOTP_WORD_COUNT];
33
34 const u32 *mxs_get_ocotp(void)
35 {
36         void __iomem *ocotp_base = MXS_IO_ADDRESS(MXS_OCOTP_BASE_ADDR);
37         int timeout = 0x400;
38         size_t i;
39         static int once = 0;
40
41         if (once)
42                 return ocotp_words;
43
44         mutex_lock(&ocotp_mutex);
45
46         /*
47          * clk_enable(hbus_clk) for ocotp can be skipped
48          * as it must be on when system is running.
49          */
50
51         /* try to clear ERROR bit */
52         __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
53
54         /* check both BUSY and ERROR cleared */
55         while ((__raw_readl(ocotp_base) &
56                 (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
57                 cpu_relax();
58
59         if (unlikely(!timeout))
60                 goto error_unlock;
61
62         /* open OCOTP banks for read */
63         __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
64
65         /* approximately wait 32 hclk cycles */
66         udelay(1);
67
68         /* poll BUSY bit becoming cleared */
69         timeout = 0x400;
70         while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
71                 cpu_relax();
72
73         if (unlikely(!timeout))
74                 goto error_unlock;
75
76         for (i = 0; i < OCOTP_WORD_COUNT; i++)
77                 ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
78                                                 i * 0x10);
79
80         /* close banks for power saving */
81         __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
82
83         once = 1;
84
85         mutex_unlock(&ocotp_mutex);
86
87         return ocotp_words;
88
89 error_unlock:
90         mutex_unlock(&ocotp_mutex);
91         pr_err("%s: timeout in reading OCOTP\n", __func__);
92         return NULL;
93 }