2 * linux/arch/arm/mach-omap2/gpmc-onenand.c
4 * Copyright (C) 2006 - 2009 Nokia Corporation
5 * Contacts: Juha Yrjola
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.
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/mtd/onenand_regs.h>
18 #include <linux/platform_data/mtd-onenand-omap2.h>
19 #include <linux/err.h>
21 #include <asm/mach/flash.h>
25 #include "gpmc-onenand.h"
27 #define ONENAND_IO_SIZE SZ_128K
29 #define ONENAND_FLAG_SYNCREAD (1 << 0)
30 #define ONENAND_FLAG_SYNCWRITE (1 << 1)
31 #define ONENAND_FLAG_HF (1 << 2)
32 #define ONENAND_FLAG_VHF (1 << 3)
34 static unsigned onenand_flags
;
35 static unsigned latency
;
37 static struct omap_onenand_platform_data
*gpmc_onenand_data
;
39 static struct resource gpmc_onenand_resource
= {
40 .flags
= IORESOURCE_MEM
,
43 static struct platform_device gpmc_onenand_device
= {
44 .name
= "omap2-onenand",
47 .resource
= &gpmc_onenand_resource
,
50 static struct gpmc_settings onenand_async
= {
51 .device_width
= GPMC_DEVWIDTH_16BIT
,
52 .mux_add_data
= GPMC_MUX_AD
,
55 static struct gpmc_settings onenand_sync
= {
58 .burst_len
= GPMC_BURST_16
,
59 .device_width
= GPMC_DEVWIDTH_16BIT
,
60 .mux_add_data
= GPMC_MUX_AD
,
64 static void omap2_onenand_calc_async_timings(struct gpmc_timings
*t
)
66 struct gpmc_device_timings dev_t
;
68 const int t_avdp
= 12;
69 const int t_aavdh
= 7;
73 const int t_cez
= 20; /* max of t_cez, t_oez */
77 memset(&dev_t
, 0, sizeof(dev_t
));
79 dev_t
.t_avdp_r
= max_t(int, t_avdp
, t_cer
) * 1000;
80 dev_t
.t_avdp_w
= dev_t
.t_avdp_r
;
81 dev_t
.t_aavdh
= t_aavdh
* 1000;
82 dev_t
.t_aa
= t_aa
* 1000;
83 dev_t
.t_ce
= t_ce
* 1000;
84 dev_t
.t_oe
= t_oe
* 1000;
85 dev_t
.t_cez_r
= t_cez
* 1000;
86 dev_t
.t_cez_w
= dev_t
.t_cez_r
;
87 dev_t
.t_wpl
= t_wpl
* 1000;
88 dev_t
.t_wph
= t_wph
* 1000;
90 gpmc_calc_timings(t
, &onenand_async
, &dev_t
);
93 static void omap2_onenand_set_async_mode(void __iomem
*onenand_base
)
97 /* Ensure sync read and sync write are disabled */
98 reg
= readw(onenand_base
+ ONENAND_REG_SYS_CFG1
);
99 reg
&= ~ONENAND_SYS_CFG1_SYNC_READ
& ~ONENAND_SYS_CFG1_SYNC_WRITE
;
100 writew(reg
, onenand_base
+ ONENAND_REG_SYS_CFG1
);
103 static void set_onenand_cfg(void __iomem
*onenand_base
)
107 reg
= readw(onenand_base
+ ONENAND_REG_SYS_CFG1
);
108 reg
&= ~((0x7 << ONENAND_SYS_CFG1_BRL_SHIFT
) | (0x7 << 9));
109 reg
|= (latency
<< ONENAND_SYS_CFG1_BRL_SHIFT
) |
110 ONENAND_SYS_CFG1_BL_16
;
111 if (onenand_flags
& ONENAND_FLAG_SYNCREAD
)
112 reg
|= ONENAND_SYS_CFG1_SYNC_READ
;
114 reg
&= ~ONENAND_SYS_CFG1_SYNC_READ
;
115 if (onenand_flags
& ONENAND_FLAG_SYNCWRITE
)
116 reg
|= ONENAND_SYS_CFG1_SYNC_WRITE
;
118 reg
&= ~ONENAND_SYS_CFG1_SYNC_WRITE
;
119 if (onenand_flags
& ONENAND_FLAG_HF
)
120 reg
|= ONENAND_SYS_CFG1_HF
;
122 reg
&= ~ONENAND_SYS_CFG1_HF
;
123 if (onenand_flags
& ONENAND_FLAG_VHF
)
124 reg
|= ONENAND_SYS_CFG1_VHF
;
126 reg
&= ~ONENAND_SYS_CFG1_VHF
;
127 writew(reg
, onenand_base
+ ONENAND_REG_SYS_CFG1
);
130 static int omap2_onenand_get_freq(struct omap_onenand_platform_data
*cfg
,
131 void __iomem
*onenand_base
)
133 u16 ver
= readw(onenand_base
+ ONENAND_REG_VERSION_ID
);
136 switch ((ver
>> 4) & 0xf) {
160 static void omap2_onenand_calc_sync_timings(struct gpmc_timings
*t
,
164 struct gpmc_device_timings dev_t
;
165 const int t_cer
= 15;
166 const int t_avdp
= 12;
167 const int t_cez
= 20; /* max of t_cez, t_oez */
168 const int t_wpl
= 40;
169 const int t_wph
= 30;
170 int min_gpmc_clk_period
, t_ces
, t_avds
, t_avdh
, t_ach
, t_aavdh
, t_rdyo
;
171 int div
, gpmc_clk_ns
;
173 if (flags
& ONENAND_SYNC_READ
)
174 onenand_flags
= ONENAND_FLAG_SYNCREAD
;
175 else if (flags
& ONENAND_SYNC_READWRITE
)
176 onenand_flags
= ONENAND_FLAG_SYNCREAD
| ONENAND_FLAG_SYNCWRITE
;
180 min_gpmc_clk_period
= 9600; /* 104 MHz */
189 min_gpmc_clk_period
= 12000; /* 83 MHz */
198 min_gpmc_clk_period
= 15000; /* 66 MHz */
207 min_gpmc_clk_period
= 18500; /* 54 MHz */
214 onenand_flags
&= ~ONENAND_FLAG_SYNCWRITE
;
218 div
= gpmc_calc_divider(min_gpmc_clk_period
);
219 gpmc_clk_ns
= gpmc_ticks_to_ns(div
);
220 if (gpmc_clk_ns
< 15) /* >66Mhz */
221 onenand_flags
|= ONENAND_FLAG_HF
;
223 onenand_flags
&= ~ONENAND_FLAG_HF
;
224 if (gpmc_clk_ns
< 12) /* >83Mhz */
225 onenand_flags
|= ONENAND_FLAG_VHF
;
227 onenand_flags
&= ~ONENAND_FLAG_VHF
;
228 if (onenand_flags
& ONENAND_FLAG_VHF
)
230 else if (onenand_flags
& ONENAND_FLAG_HF
)
232 else if (gpmc_clk_ns
>= 25) /* 40 MHz*/
237 /* Set synchronous read timings */
238 memset(&dev_t
, 0, sizeof(dev_t
));
240 if (onenand_flags
& ONENAND_FLAG_SYNCREAD
)
241 onenand_sync
.sync_read
= true;
242 if (onenand_flags
& ONENAND_FLAG_SYNCWRITE
) {
243 onenand_sync
.sync_write
= true;
244 onenand_sync
.burst_write
= true;
246 dev_t
.t_avdp_w
= max(t_avdp
, t_cer
) * 1000;
247 dev_t
.t_wpl
= t_wpl
* 1000;
248 dev_t
.t_wph
= t_wph
* 1000;
249 dev_t
.t_aavdh
= t_aavdh
* 1000;
251 dev_t
.ce_xdelay
= true;
252 dev_t
.avd_xdelay
= true;
253 dev_t
.oe_xdelay
= true;
254 dev_t
.we_xdelay
= true;
255 dev_t
.clk
= min_gpmc_clk_period
;
256 dev_t
.t_bacc
= dev_t
.clk
;
257 dev_t
.t_ces
= t_ces
* 1000;
258 dev_t
.t_avds
= t_avds
* 1000;
259 dev_t
.t_avdh
= t_avdh
* 1000;
260 dev_t
.t_ach
= t_ach
* 1000;
261 dev_t
.cyc_iaa
= (latency
+ 1);
262 dev_t
.t_cez_r
= t_cez
* 1000;
263 dev_t
.t_cez_w
= dev_t
.t_cez_r
;
264 dev_t
.cyc_aavdh_oe
= 1;
265 dev_t
.t_rdyo
= t_rdyo
* 1000 + min_gpmc_clk_period
;
267 gpmc_calc_timings(t
, &onenand_sync
, &dev_t
);
270 static int omap2_onenand_setup_async(void __iomem
*onenand_base
)
272 struct gpmc_timings t
;
275 if (gpmc_onenand_data
->of_node
) {
276 gpmc_read_settings_dt(gpmc_onenand_data
->of_node
,
278 if (onenand_async
.sync_read
|| onenand_async
.sync_write
) {
279 if (onenand_async
.sync_write
)
280 gpmc_onenand_data
->flags
|=
281 ONENAND_SYNC_READWRITE
;
283 gpmc_onenand_data
->flags
|= ONENAND_SYNC_READ
;
284 onenand_async
.sync_read
= false;
285 onenand_async
.sync_write
= false;
289 omap2_onenand_set_async_mode(onenand_base
);
291 omap2_onenand_calc_async_timings(&t
);
293 ret
= gpmc_cs_program_settings(gpmc_onenand_data
->cs
, &onenand_async
);
297 ret
= gpmc_cs_set_timings(gpmc_onenand_data
->cs
, &t
);
301 omap2_onenand_set_async_mode(onenand_base
);
306 static int omap2_onenand_setup_sync(void __iomem
*onenand_base
, int *freq_ptr
)
308 int ret
, freq
= *freq_ptr
;
309 struct gpmc_timings t
;
312 /* Very first call freq is not known */
313 freq
= omap2_onenand_get_freq(gpmc_onenand_data
, onenand_base
);
314 set_onenand_cfg(onenand_base
);
317 if (gpmc_onenand_data
->of_node
) {
318 gpmc_read_settings_dt(gpmc_onenand_data
->of_node
,
322 * FIXME: Appears to be legacy code from initial ONENAND commit.
323 * Unclear what boards this is for and if this can be removed.
325 if (!cpu_is_omap34xx())
326 onenand_sync
.wait_on_read
= true;
329 omap2_onenand_calc_sync_timings(&t
, gpmc_onenand_data
->flags
, freq
);
331 ret
= gpmc_cs_program_settings(gpmc_onenand_data
->cs
, &onenand_sync
);
335 ret
= gpmc_cs_set_timings(gpmc_onenand_data
->cs
, &t
);
339 set_onenand_cfg(onenand_base
);
346 static int gpmc_onenand_setup(void __iomem
*onenand_base
, int *freq_ptr
)
348 struct device
*dev
= &gpmc_onenand_device
.dev
;
349 unsigned l
= ONENAND_SYNC_READ
| ONENAND_SYNC_READWRITE
;
352 ret
= omap2_onenand_setup_async(onenand_base
);
354 dev_err(dev
, "unable to set to async mode\n");
358 if (!(gpmc_onenand_data
->flags
& l
))
361 ret
= omap2_onenand_setup_sync(onenand_base
, freq_ptr
);
363 dev_err(dev
, "unable to set to sync mode\n");
367 void gpmc_onenand_init(struct omap_onenand_platform_data
*_onenand_data
)
370 struct device
*dev
= &gpmc_onenand_device
.dev
;
372 gpmc_onenand_data
= _onenand_data
;
373 gpmc_onenand_data
->onenand_setup
= gpmc_onenand_setup
;
374 gpmc_onenand_device
.dev
.platform_data
= gpmc_onenand_data
;
376 if (cpu_is_omap24xx() &&
377 (gpmc_onenand_data
->flags
& ONENAND_SYNC_READWRITE
)) {
378 dev_warn(dev
, "OneNAND using only SYNC_READ on 24xx\n");
379 gpmc_onenand_data
->flags
&= ~ONENAND_SYNC_READWRITE
;
380 gpmc_onenand_data
->flags
|= ONENAND_SYNC_READ
;
383 if (cpu_is_omap34xx())
384 gpmc_onenand_data
->flags
|= ONENAND_IN_OMAP34XX
;
386 gpmc_onenand_data
->flags
&= ~ONENAND_IN_OMAP34XX
;
388 err
= gpmc_cs_request(gpmc_onenand_data
->cs
, ONENAND_IO_SIZE
,
389 (unsigned long *)&gpmc_onenand_resource
.start
);
391 dev_err(dev
, "Cannot request GPMC CS %d, error %d\n",
392 gpmc_onenand_data
->cs
, err
);
396 gpmc_onenand_resource
.end
= gpmc_onenand_resource
.start
+
399 if (platform_device_register(&gpmc_onenand_device
) < 0) {
400 dev_err(dev
, "Unable to register OneNAND device\n");
401 gpmc_cs_free(gpmc_onenand_data
->cs
);