blob: a0fa9bb2bda5bed7e952cd7639374c040d858fbc [file] [log] [blame]
Juha Yrjolaaa62e902009-05-28 13:23:52 -07001/*
2 * linux/arch/arm/mach-omap2/gpmc-onenand.c
3 *
4 * Copyright (C) 2006 - 2009 Nokia Corporation
5 * Contacts: Juha Yrjola
6 * Tony Lindgren
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Paul Gortmakerd44b28c2011-07-31 10:52:44 -040013#include <linux/string.h>
Juha Yrjolaaa62e902009-05-28 13:23:52 -070014#include <linux/kernel.h>
15#include <linux/platform_device.h>
16#include <linux/mtd/onenand_regs.h>
17#include <linux/io.h>
18
19#include <asm/mach/flash.h>
20
Tony Lindgren2c799ce2012-02-24 10:34:35 -080021#include <plat/cpu.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070022#include <plat/onenand.h>
23#include <plat/board.h>
24#include <plat/gpmc.h>
Juha Yrjolaaa62e902009-05-28 13:23:52 -070025
26static struct omap_onenand_platform_data *gpmc_onenand_data;
27
28static struct platform_device gpmc_onenand_device = {
29 .name = "omap2-onenand",
30 .id = -1,
31};
32
33static int omap2_onenand_set_async_mode(int cs, void __iomem *onenand_base)
34{
35 struct gpmc_timings t;
Adrian Hunter6d453e82009-06-23 13:30:24 +030036 u32 reg;
37 int err;
Juha Yrjolaaa62e902009-05-28 13:23:52 -070038
39 const int t_cer = 15;
40 const int t_avdp = 12;
41 const int t_aavdh = 7;
42 const int t_ce = 76;
43 const int t_aa = 76;
44 const int t_oe = 20;
45 const int t_cez = 20; /* max of t_cez, t_oez */
46 const int t_ds = 30;
47 const int t_wpl = 40;
48 const int t_wph = 30;
49
Adrian Hunter6d453e82009-06-23 13:30:24 +030050 /* Ensure sync read and sync write are disabled */
51 reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
52 reg &= ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE;
53 writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
54
Juha Yrjolaaa62e902009-05-28 13:23:52 -070055 memset(&t, 0, sizeof(t));
56 t.sync_clk = 0;
57 t.cs_on = 0;
58 t.adv_on = 0;
59
60 /* Read */
61 t.adv_rd_off = gpmc_round_ns_to_ticks(max_t(int, t_avdp, t_cer));
62 t.oe_on = t.adv_rd_off + gpmc_round_ns_to_ticks(t_aavdh);
63 t.access = t.adv_on + gpmc_round_ns_to_ticks(t_aa);
64 t.access = max_t(int, t.access, t.cs_on + gpmc_round_ns_to_ticks(t_ce));
65 t.access = max_t(int, t.access, t.oe_on + gpmc_round_ns_to_ticks(t_oe));
66 t.oe_off = t.access + gpmc_round_ns_to_ticks(1);
67 t.cs_rd_off = t.oe_off;
68 t.rd_cycle = t.cs_rd_off + gpmc_round_ns_to_ticks(t_cez);
69
70 /* Write */
71 t.adv_wr_off = t.adv_rd_off;
72 t.we_on = t.oe_on;
73 if (cpu_is_omap34xx()) {
74 t.wr_data_mux_bus = t.we_on;
75 t.wr_access = t.we_on + gpmc_round_ns_to_ticks(t_ds);
76 }
77 t.we_off = t.we_on + gpmc_round_ns_to_ticks(t_wpl);
78 t.cs_wr_off = t.we_off + gpmc_round_ns_to_ticks(t_wph);
79 t.wr_cycle = t.cs_wr_off + gpmc_round_ns_to_ticks(t_cez);
80
81 /* Configure GPMC for asynchronous read */
82 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
83 GPMC_CONFIG1_DEVICESIZE_16 |
84 GPMC_CONFIG1_MUXADDDATA);
85
Adrian Hunter6d453e82009-06-23 13:30:24 +030086 err = gpmc_cs_set_timings(cs, &t);
87 if (err)
88 return err;
89
90 /* Ensure sync read and sync write are disabled */
91 reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
92 reg &= ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE;
93 writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
94
95 return 0;
Juha Yrjolaaa62e902009-05-28 13:23:52 -070096}
97
98static void set_onenand_cfg(void __iomem *onenand_base, int latency,
Adrian Hunter1435ca02011-02-07 10:46:58 +020099 int sync_read, int sync_write, int hf, int vhf)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700100{
101 u32 reg;
102
103 reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
104 reg &= ~((0x7 << ONENAND_SYS_CFG1_BRL_SHIFT) | (0x7 << 9));
105 reg |= (latency << ONENAND_SYS_CFG1_BRL_SHIFT) |
106 ONENAND_SYS_CFG1_BL_16;
107 if (sync_read)
108 reg |= ONENAND_SYS_CFG1_SYNC_READ;
109 else
110 reg &= ~ONENAND_SYS_CFG1_SYNC_READ;
111 if (sync_write)
112 reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
113 else
114 reg &= ~ONENAND_SYS_CFG1_SYNC_WRITE;
115 if (hf)
116 reg |= ONENAND_SYS_CFG1_HF;
117 else
118 reg &= ~ONENAND_SYS_CFG1_HF;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200119 if (vhf)
120 reg |= ONENAND_SYS_CFG1_VHF;
121 else
122 reg &= ~ONENAND_SYS_CFG1_VHF;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700123 writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
124}
125
Adrian Hunter5714b7e2011-02-07 10:47:00 +0200126static int omap2_onenand_get_freq(struct omap_onenand_platform_data *cfg,
127 void __iomem *onenand_base, bool *clk_dep)
128{
129 u16 ver = readw(onenand_base + ONENAND_REG_VERSION_ID);
130 int freq = 0;
131
132 if (cfg->get_freq) {
133 struct onenand_freq_info fi;
134
135 fi.maf_id = readw(onenand_base + ONENAND_REG_MANUFACTURER_ID);
136 fi.dev_id = readw(onenand_base + ONENAND_REG_DEVICE_ID);
137 fi.ver_id = ver;
138 freq = cfg->get_freq(&fi, clk_dep);
139 if (freq)
140 return freq;
141 }
142
143 switch ((ver >> 4) & 0xf) {
144 case 0:
145 freq = 40;
146 break;
147 case 1:
148 freq = 54;
149 break;
150 case 2:
151 freq = 66;
152 break;
153 case 3:
154 freq = 83;
155 break;
156 case 4:
157 freq = 104;
158 break;
159 default:
160 freq = 54;
161 break;
162 }
163
164 return freq;
165}
166
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700167static int omap2_onenand_set_sync_mode(struct omap_onenand_platform_data *cfg,
168 void __iomem *onenand_base,
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200169 int *freq_ptr)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700170{
171 struct gpmc_timings t;
172 const int t_cer = 15;
173 const int t_avdp = 12;
174 const int t_cez = 20; /* max of t_cez, t_oez */
175 const int t_ds = 30;
176 const int t_wpl = 40;
177 const int t_wph = 30;
178 int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
Paul Walmsleyeeb37112012-04-13 06:34:32 -0600179 int div, fclk_offset_ns, fclk_offset, gpmc_clk_ns, latency;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200180 int first_time = 0, hf = 0, vhf = 0, sync_read = 0, sync_write = 0;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700181 int err, ticks_cez;
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200182 int cs = cfg->cs, freq = *freq_ptr;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700183 u32 reg;
Adrian Hunter5714b7e2011-02-07 10:47:00 +0200184 bool clk_dep = false;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700185
186 if (cfg->flags & ONENAND_SYNC_READ) {
187 sync_read = 1;
188 } else if (cfg->flags & ONENAND_SYNC_READWRITE) {
189 sync_read = 1;
190 sync_write = 1;
Adrian Hunter6d453e82009-06-23 13:30:24 +0300191 } else
192 return omap2_onenand_set_async_mode(cs, onenand_base);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700193
194 if (!freq) {
195 /* Very first call freq is not known */
196 err = omap2_onenand_set_async_mode(cs, onenand_base);
197 if (err)
198 return err;
Adrian Hunter5714b7e2011-02-07 10:47:00 +0200199 freq = omap2_onenand_get_freq(cfg, onenand_base, &clk_dep);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700200 first_time = 1;
201 }
202
203 switch (freq) {
Adrian Hunter49314452010-12-09 11:22:50 +0200204 case 104:
205 min_gpmc_clk_period = 9600; /* 104 MHz */
206 t_ces = 3;
207 t_avds = 4;
208 t_avdh = 2;
209 t_ach = 3;
210 t_aavdh = 6;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200211 t_rdyo = 6;
Adrian Hunter49314452010-12-09 11:22:50 +0200212 break;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700213 case 83:
Adrian Huntera3551f52010-12-09 10:48:27 +0200214 min_gpmc_clk_period = 12000; /* 83 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700215 t_ces = 5;
216 t_avds = 4;
217 t_avdh = 2;
218 t_ach = 6;
219 t_aavdh = 6;
220 t_rdyo = 9;
221 break;
222 case 66:
Adrian Huntera3551f52010-12-09 10:48:27 +0200223 min_gpmc_clk_period = 15000; /* 66 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700224 t_ces = 6;
225 t_avds = 5;
226 t_avdh = 2;
227 t_ach = 6;
228 t_aavdh = 6;
229 t_rdyo = 11;
230 break;
231 default:
Adrian Huntera3551f52010-12-09 10:48:27 +0200232 min_gpmc_clk_period = 18500; /* 54 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700233 t_ces = 7;
234 t_avds = 7;
235 t_avdh = 7;
236 t_ach = 9;
237 t_aavdh = 7;
238 t_rdyo = 15;
239 sync_write = 0;
240 break;
241 }
242
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700243 div = gpmc_cs_calc_divider(cs, min_gpmc_clk_period);
244 gpmc_clk_ns = gpmc_ticks_to_ns(div);
245 if (gpmc_clk_ns < 15) /* >66Mhz */
246 hf = 1;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200247 if (gpmc_clk_ns < 12) /* >83Mhz */
248 vhf = 1;
249 if (vhf)
250 latency = 8;
251 else if (hf)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700252 latency = 6;
253 else if (gpmc_clk_ns >= 25) /* 40 MHz*/
254 latency = 3;
255 else
256 latency = 4;
257
Adrian Hunter5714b7e2011-02-07 10:47:00 +0200258 if (clk_dep) {
259 if (gpmc_clk_ns < 12) { /* >83Mhz */
260 t_ces = 3;
261 t_avds = 4;
262 } else if (gpmc_clk_ns < 15) { /* >66Mhz */
263 t_ces = 5;
264 t_avds = 4;
265 } else if (gpmc_clk_ns < 25) { /* >40Mhz */
266 t_ces = 6;
267 t_avds = 5;
268 } else {
269 t_ces = 7;
270 t_avds = 7;
271 }
272 }
273
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700274 if (first_time)
275 set_onenand_cfg(onenand_base, latency,
Adrian Hunter1435ca02011-02-07 10:46:58 +0200276 sync_read, sync_write, hf, vhf);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700277
278 if (div == 1) {
279 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
280 reg |= (1 << 7);
281 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
282 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
283 reg |= (1 << 7);
284 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
285 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
286 reg |= (1 << 7);
287 reg |= (1 << 23);
288 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
289 } else {
290 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
291 reg &= ~(1 << 7);
292 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
293 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
294 reg &= ~(1 << 7);
295 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
296 reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
297 reg &= ~(1 << 7);
298 reg &= ~(1 << 23);
299 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
300 }
301
302 /* Set synchronous read timings */
303 memset(&t, 0, sizeof(t));
304 t.sync_clk = min_gpmc_clk_period;
305 t.cs_on = 0;
306 t.adv_on = 0;
307 fclk_offset_ns = gpmc_round_ns_to_ticks(max_t(int, t_ces, t_avds));
308 fclk_offset = gpmc_ns_to_ticks(fclk_offset_ns);
309 t.page_burst_access = gpmc_clk_ns;
310
311 /* Read */
312 t.adv_rd_off = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_avdh));
313 t.oe_on = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_ach));
Adrian Hunter1435ca02011-02-07 10:46:58 +0200314 /* Force at least 1 clk between AVD High to OE Low */
315 if (t.oe_on <= t.adv_rd_off)
316 t.oe_on = t.adv_rd_off + gpmc_round_ns_to_ticks(1);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700317 t.access = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div);
318 t.oe_off = t.access + gpmc_round_ns_to_ticks(1);
319 t.cs_rd_off = t.oe_off;
320 ticks_cez = ((gpmc_ns_to_ticks(t_cez) + div - 1) / div) * div;
321 t.rd_cycle = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div +
322 ticks_cez);
323
324 /* Write */
325 if (sync_write) {
326 t.adv_wr_off = t.adv_rd_off;
327 t.we_on = 0;
328 t.we_off = t.cs_rd_off;
329 t.cs_wr_off = t.cs_rd_off;
330 t.wr_cycle = t.rd_cycle;
331 if (cpu_is_omap34xx()) {
332 t.wr_data_mux_bus = gpmc_ticks_to_ns(fclk_offset +
Adrian Huntera3551f52010-12-09 10:48:27 +0200333 gpmc_ps_to_ticks(min_gpmc_clk_period +
334 t_rdyo * 1000));
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700335 t.wr_access = t.access;
336 }
337 } else {
338 t.adv_wr_off = gpmc_round_ns_to_ticks(max_t(int,
339 t_avdp, t_cer));
340 t.we_on = t.adv_wr_off + gpmc_round_ns_to_ticks(t_aavdh);
341 t.we_off = t.we_on + gpmc_round_ns_to_ticks(t_wpl);
342 t.cs_wr_off = t.we_off + gpmc_round_ns_to_ticks(t_wph);
343 t.wr_cycle = t.cs_wr_off + gpmc_round_ns_to_ticks(t_cez);
344 if (cpu_is_omap34xx()) {
345 t.wr_data_mux_bus = t.we_on;
346 t.wr_access = t.we_on + gpmc_round_ns_to_ticks(t_ds);
347 }
348 }
349
350 /* Configure GPMC for synchronous read */
351 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
352 GPMC_CONFIG1_WRAPBURST_SUPP |
353 GPMC_CONFIG1_READMULTIPLE_SUPP |
354 (sync_read ? GPMC_CONFIG1_READTYPE_SYNC : 0) |
355 (sync_write ? GPMC_CONFIG1_WRITEMULTIPLE_SUPP : 0) |
356 (sync_write ? GPMC_CONFIG1_WRITETYPE_SYNC : 0) |
357 GPMC_CONFIG1_CLKACTIVATIONTIME(fclk_offset) |
358 GPMC_CONFIG1_PAGE_LEN(2) |
359 (cpu_is_omap34xx() ? 0 :
360 (GPMC_CONFIG1_WAIT_READ_MON |
361 GPMC_CONFIG1_WAIT_PIN_SEL(0))) |
362 GPMC_CONFIG1_DEVICESIZE_16 |
363 GPMC_CONFIG1_DEVICETYPE_NOR |
364 GPMC_CONFIG1_MUXADDDATA);
365
366 err = gpmc_cs_set_timings(cs, &t);
367 if (err)
368 return err;
369
Adrian Hunter1435ca02011-02-07 10:46:58 +0200370 set_onenand_cfg(onenand_base, latency, sync_read, sync_write, hf, vhf);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700371
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200372 *freq_ptr = freq;
373
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700374 return 0;
375}
376
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200377static int gpmc_onenand_setup(void __iomem *onenand_base, int *freq_ptr)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700378{
379 struct device *dev = &gpmc_onenand_device.dev;
380
381 /* Set sync timings in GPMC */
382 if (omap2_onenand_set_sync_mode(gpmc_onenand_data, onenand_base,
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200383 freq_ptr) < 0) {
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700384 dev_err(dev, "Unable to set synchronous mode\n");
385 return -EINVAL;
386 }
387
388 return 0;
389}
390
391void __init gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
392{
393 gpmc_onenand_data = _onenand_data;
394 gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
395 gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;
396
397 if (cpu_is_omap24xx() &&
398 (gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
399 printk(KERN_ERR "Onenand using only SYNC_READ on 24xx\n");
400 gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
401 gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
402 }
403
404 if (platform_device_register(&gpmc_onenand_device) < 0) {
405 printk(KERN_ERR "Unable to register OneNAND device\n");
406 return;
407 }
408}