1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/io.h>
13#include <linux/err.h>
14#include <linux/clk.h>
15#include <linux/module.h>
16#include <linux/time.h>
17
18#include <linux/platform_data/mtd-davinci-aemif.h>
19#include <linux/platform_data/mtd-davinci.h>
20
21
22
23#define TA(x) ((x) << 2)
24#define RHOLD(x) ((x) << 4)
25#define RSTROBE(x) ((x) << 7)
26#define RSETUP(x) ((x) << 13)
27#define WHOLD(x) ((x) << 17)
28#define WSTROBE(x) ((x) << 20)
29#define WSETUP(x) ((x) << 26)
30
31#define TA_MAX 0x3
32#define RHOLD_MAX 0x7
33#define RSTROBE_MAX 0x3f
34#define RSETUP_MAX 0xf
35#define WHOLD_MAX 0x7
36#define WSTROBE_MAX 0x3f
37#define WSETUP_MAX 0xf
38
39#define TIMING_MASK (TA(TA_MAX) | \
40 RHOLD(RHOLD_MAX) | \
41 RSTROBE(RSTROBE_MAX) | \
42 RSETUP(RSETUP_MAX) | \
43 WHOLD(WHOLD_MAX) | \
44 WSTROBE(WSTROBE_MAX) | \
45 WSETUP(WSETUP_MAX))
46
47static inline unsigned int davinci_aemif_readl(void __iomem *base, int offset)
48{
49 return readl_relaxed(base + offset);
50}
51
52static inline void davinci_aemif_writel(void __iomem *base,
53 int offset, unsigned long value)
54{
55 writel_relaxed(value, base + offset);
56}
57
58
59
60
61
62
63
64
65
66
67static int aemif_calc_rate(int wanted, unsigned long clk, int max)
68{
69 int result;
70
71 result = DIV_ROUND_UP((wanted * clk), NSEC_PER_MSEC) - 1;
72
73 pr_debug("%s: result %d from %ld, %d\n", __func__, result, clk, wanted);
74
75
76 if (result < 0)
77 result = 0;
78
79
80 else if (result > max)
81 result = -EINVAL;
82
83 return result;
84}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static int davinci_aemif_setup_timing(struct davinci_aemif_timing *t,
103 void __iomem *base, unsigned cs,
104 unsigned long clkrate)
105{
106 unsigned set, val;
107 int ta, rhold, rstrobe, rsetup, whold, wstrobe, wsetup;
108 unsigned offset = A1CR_OFFSET + cs * 4;
109
110 if (!t)
111 return 0;
112
113 clkrate /= 1000;
114
115 ta = aemif_calc_rate(t->ta, clkrate, TA_MAX);
116 rhold = aemif_calc_rate(t->rhold, clkrate, RHOLD_MAX);
117 rstrobe = aemif_calc_rate(t->rstrobe, clkrate, RSTROBE_MAX);
118 rsetup = aemif_calc_rate(t->rsetup, clkrate, RSETUP_MAX);
119 whold = aemif_calc_rate(t->whold, clkrate, WHOLD_MAX);
120 wstrobe = aemif_calc_rate(t->wstrobe, clkrate, WSTROBE_MAX);
121 wsetup = aemif_calc_rate(t->wsetup, clkrate, WSETUP_MAX);
122
123 if (ta < 0 || rhold < 0 || rstrobe < 0 || rsetup < 0 ||
124 whold < 0 || wstrobe < 0 || wsetup < 0) {
125 pr_err("%s: cannot get suitable timings\n", __func__);
126 return -EINVAL;
127 }
128
129 set = TA(ta) | RHOLD(rhold) | RSTROBE(rstrobe) | RSETUP(rsetup) |
130 WHOLD(whold) | WSTROBE(wstrobe) | WSETUP(wsetup);
131
132 val = __raw_readl(base + offset);
133 val &= ~TIMING_MASK;
134 val |= set;
135 __raw_writel(val, base + offset);
136
137 return 0;
138}
139
140
141
142
143
144
145
146
147
148
149
150int davinci_aemif_setup(struct platform_device *pdev)
151{
152 struct davinci_nand_pdata *pdata = dev_get_platdata(&pdev->dev);
153 uint32_t val;
154 unsigned long clkrate;
155 struct resource *res;
156 void __iomem *base;
157 struct clk *clk;
158 int ret = 0;
159
160 clk = clk_get(&pdev->dev, "aemif");
161 if (IS_ERR(clk)) {
162 ret = PTR_ERR(clk);
163 dev_dbg(&pdev->dev, "unable to get AEMIF clock, err %d\n", ret);
164 return ret;
165 }
166
167 ret = clk_prepare_enable(clk);
168 if (ret < 0) {
169 dev_dbg(&pdev->dev, "unable to enable AEMIF clock, err %d\n",
170 ret);
171 goto err_put;
172 }
173
174 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
175 if (!res) {
176 dev_err(&pdev->dev, "cannot get IORESOURCE_MEM\n");
177 ret = -ENOMEM;
178 goto err;
179 }
180
181 base = ioremap(res->start, resource_size(res));
182 if (!base) {
183 dev_err(&pdev->dev, "ioremap failed for resource %pR\n", res);
184 ret = -ENOMEM;
185 goto err;
186 }
187
188
189
190
191
192 val = davinci_aemif_readl(base, A1CR_OFFSET + pdev->id * 4);
193
194
195
196
197 val &= ~(ACR_ASIZE_MASK | ACR_EW_MASK | ACR_SS_MASK);
198 if (pdata->options & NAND_BUSWIDTH_16)
199 val |= 0x1;
200
201 davinci_aemif_writel(base, A1CR_OFFSET + pdev->id * 4, val);
202
203 clkrate = clk_get_rate(clk);
204
205 if (pdata->timing)
206 ret = davinci_aemif_setup_timing(pdata->timing, base, pdev->id,
207 clkrate);
208
209 if (ret < 0)
210 dev_dbg(&pdev->dev, "NAND timing values setup fail\n");
211
212 iounmap(base);
213err:
214 clk_disable_unprepare(clk);
215err_put:
216 clk_put(clk);
217 return ret;
218}
219