blob: e175ceb0dc0541cd80de178e0a5c86ee21fd47a0 [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>
Arnd Bergmann22037472012-08-24 15:21:06 +020018#include <linux/platform_data/mtd-onenand-omap2.h>
Afzal Mohammed46376882012-06-05 10:11:48 +053019#include <linux/err.h>
Juha Yrjolaaa62e902009-05-28 13:23:52 -070020
21#include <asm/mach/flash.h>
22
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053023#include "gpmc.h"
Tony Lindgrendbc04162012-08-31 10:59:07 -070024#include "soc.h"
Afzal Mohammedb6ab13e2012-09-29 10:32:42 +053025#include "gpmc-onenand.h"
Tony Lindgrendbc04162012-08-31 10:59:07 -070026
Afzal Mohammed681988b2012-08-30 12:53:23 -070027#define ONENAND_IO_SIZE SZ_128K
28
Afzal Mohammed46376882012-06-05 10:11:48 +053029#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)
33
34static unsigned onenand_flags;
35static unsigned latency;
Afzal Mohammed46376882012-06-05 10:11:48 +053036
Juha Yrjolaaa62e902009-05-28 13:23:52 -070037static struct omap_onenand_platform_data *gpmc_onenand_data;
38
Afzal Mohammed681988b2012-08-30 12:53:23 -070039static struct resource gpmc_onenand_resource = {
40 .flags = IORESOURCE_MEM,
41};
42
Juha Yrjolaaa62e902009-05-28 13:23:52 -070043static struct platform_device gpmc_onenand_device = {
44 .name = "omap2-onenand",
45 .id = -1,
Afzal Mohammed681988b2012-08-30 12:53:23 -070046 .num_resources = 1,
47 .resource = &gpmc_onenand_resource,
Juha Yrjolaaa62e902009-05-28 13:23:52 -070048};
49
Jon Hunterc3be5b42013-02-21 13:46:22 -060050static struct gpmc_settings onenand_async = {
51 .mux_add_data = GPMC_MUX_AD,
52};
53
54static struct gpmc_settings onenand_sync = {
55 .burst_read = true,
56 .mux_add_data = GPMC_MUX_AD,
57};
58
Jon Hunterbe9f10c2013-02-21 15:20:53 -060059static void omap2_onenand_calc_async_timings(struct gpmc_timings *t)
Juha Yrjolaaa62e902009-05-28 13:23:52 -070060{
Afzal Mohammed4f4426f2012-11-09 18:05:17 +053061 struct gpmc_device_timings dev_t;
Juha Yrjolaaa62e902009-05-28 13:23:52 -070062
63 const int t_cer = 15;
64 const int t_avdp = 12;
65 const int t_aavdh = 7;
66 const int t_ce = 76;
67 const int t_aa = 76;
68 const int t_oe = 20;
69 const int t_cez = 20; /* max of t_cez, t_oez */
Juha Yrjolaaa62e902009-05-28 13:23:52 -070070 const int t_wpl = 40;
71 const int t_wph = 30;
72
Afzal Mohammed4f4426f2012-11-09 18:05:17 +053073 memset(&dev_t, 0, sizeof(dev_t));
Juha Yrjolaaa62e902009-05-28 13:23:52 -070074
Afzal Mohammed4f4426f2012-11-09 18:05:17 +053075 dev_t.t_avdp_r = max_t(int, t_avdp, t_cer) * 1000;
76 dev_t.t_avdp_w = dev_t.t_avdp_r;
77 dev_t.t_aavdh = t_aavdh * 1000;
78 dev_t.t_aa = t_aa * 1000;
79 dev_t.t_ce = t_ce * 1000;
80 dev_t.t_oe = t_oe * 1000;
81 dev_t.t_cez_r = t_cez * 1000;
82 dev_t.t_cez_w = dev_t.t_cez_r;
83 dev_t.t_wpl = t_wpl * 1000;
84 dev_t.t_wph = t_wph * 1000;
Juha Yrjolaaa62e902009-05-28 13:23:52 -070085
Jon Hunterc3be5b42013-02-21 13:46:22 -060086 gpmc_calc_timings(t, &onenand_async, &dev_t);
Afzal Mohammed46376882012-06-05 10:11:48 +053087}
88
89static int gpmc_set_async_mode(int cs, struct gpmc_timings *t)
90{
Juha Yrjolaaa62e902009-05-28 13:23:52 -070091 /* Configure GPMC for asynchronous read */
92 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
93 GPMC_CONFIG1_DEVICESIZE_16 |
94 GPMC_CONFIG1_MUXADDDATA);
95
Afzal Mohammed46376882012-06-05 10:11:48 +053096 return gpmc_cs_set_timings(cs, t);
97}
98
99static void omap2_onenand_set_async_mode(void __iomem *onenand_base)
100{
101 u32 reg;
Adrian Hunter6d453e82009-06-23 13:30:24 +0300102
103 /* Ensure sync read and sync write are disabled */
104 reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
105 reg &= ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE;
106 writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700107}
108
Afzal Mohammed46376882012-06-05 10:11:48 +0530109static void set_onenand_cfg(void __iomem *onenand_base)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700110{
111 u32 reg;
112
113 reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
114 reg &= ~((0x7 << ONENAND_SYS_CFG1_BRL_SHIFT) | (0x7 << 9));
115 reg |= (latency << ONENAND_SYS_CFG1_BRL_SHIFT) |
116 ONENAND_SYS_CFG1_BL_16;
Afzal Mohammed46376882012-06-05 10:11:48 +0530117 if (onenand_flags & ONENAND_FLAG_SYNCREAD)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700118 reg |= ONENAND_SYS_CFG1_SYNC_READ;
119 else
120 reg &= ~ONENAND_SYS_CFG1_SYNC_READ;
Afzal Mohammed46376882012-06-05 10:11:48 +0530121 if (onenand_flags & ONENAND_FLAG_SYNCWRITE)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700122 reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
123 else
124 reg &= ~ONENAND_SYS_CFG1_SYNC_WRITE;
Afzal Mohammed46376882012-06-05 10:11:48 +0530125 if (onenand_flags & ONENAND_FLAG_HF)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700126 reg |= ONENAND_SYS_CFG1_HF;
127 else
128 reg &= ~ONENAND_SYS_CFG1_HF;
Afzal Mohammed46376882012-06-05 10:11:48 +0530129 if (onenand_flags & ONENAND_FLAG_VHF)
Adrian Hunter1435ca02011-02-07 10:46:58 +0200130 reg |= ONENAND_SYS_CFG1_VHF;
131 else
132 reg &= ~ONENAND_SYS_CFG1_VHF;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700133 writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
134}
135
Adrian Hunter5714b7ed2011-02-07 10:47:00 +0200136static int omap2_onenand_get_freq(struct omap_onenand_platform_data *cfg,
Jon Hunter757ef792012-06-28 13:41:29 -0500137 void __iomem *onenand_base)
Adrian Hunter5714b7ed2011-02-07 10:47:00 +0200138{
139 u16 ver = readw(onenand_base + ONENAND_REG_VERSION_ID);
Jon Hunter757ef792012-06-28 13:41:29 -0500140 int freq;
Adrian Hunter5714b7ed2011-02-07 10:47:00 +0200141
142 switch ((ver >> 4) & 0xf) {
143 case 0:
144 freq = 40;
145 break;
146 case 1:
147 freq = 54;
148 break;
149 case 2:
150 freq = 66;
151 break;
152 case 3:
153 freq = 83;
154 break;
155 case 4:
156 freq = 104;
157 break;
158 default:
159 freq = 54;
160 break;
161 }
162
163 return freq;
164}
165
Jon Hunterbe9f10c2013-02-21 15:20:53 -0600166static void omap2_onenand_calc_sync_timings(struct gpmc_timings *t,
167 unsigned int flags,
168 int freq)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700169{
Afzal Mohammed4f4426f2012-11-09 18:05:17 +0530170 struct gpmc_device_timings dev_t;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700171 const int t_cer = 15;
172 const int t_avdp = 12;
173 const int t_cez = 20; /* max of t_cez, t_oez */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700174 const int t_wpl = 40;
175 const int t_wph = 30;
176 int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
Afzal Mohammed4f4426f2012-11-09 18:05:17 +0530177 int div, gpmc_clk_ns;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700178
Jon Hunterbe9f10c2013-02-21 15:20:53 -0600179 if (flags & ONENAND_SYNC_READ)
Afzal Mohammed46376882012-06-05 10:11:48 +0530180 onenand_flags = ONENAND_FLAG_SYNCREAD;
Jon Hunterbe9f10c2013-02-21 15:20:53 -0600181 else if (flags & ONENAND_SYNC_READWRITE)
Afzal Mohammed46376882012-06-05 10:11:48 +0530182 onenand_flags = ONENAND_FLAG_SYNCREAD | ONENAND_FLAG_SYNCWRITE;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700183
184 switch (freq) {
Adrian Hunter49314452010-12-09 11:22:50 +0200185 case 104:
186 min_gpmc_clk_period = 9600; /* 104 MHz */
187 t_ces = 3;
188 t_avds = 4;
189 t_avdh = 2;
190 t_ach = 3;
191 t_aavdh = 6;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200192 t_rdyo = 6;
Adrian Hunter49314452010-12-09 11:22:50 +0200193 break;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700194 case 83:
Adrian Huntera3551f52010-12-09 10:48:27 +0200195 min_gpmc_clk_period = 12000; /* 83 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700196 t_ces = 5;
197 t_avds = 4;
198 t_avdh = 2;
199 t_ach = 6;
200 t_aavdh = 6;
201 t_rdyo = 9;
202 break;
203 case 66:
Adrian Huntera3551f52010-12-09 10:48:27 +0200204 min_gpmc_clk_period = 15000; /* 66 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700205 t_ces = 6;
206 t_avds = 5;
207 t_avdh = 2;
208 t_ach = 6;
209 t_aavdh = 6;
210 t_rdyo = 11;
211 break;
212 default:
Adrian Huntera3551f52010-12-09 10:48:27 +0200213 min_gpmc_clk_period = 18500; /* 54 MHz */
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700214 t_ces = 7;
215 t_avds = 7;
216 t_avdh = 7;
217 t_ach = 9;
218 t_aavdh = 7;
219 t_rdyo = 15;
Afzal Mohammed46376882012-06-05 10:11:48 +0530220 onenand_flags &= ~ONENAND_FLAG_SYNCWRITE;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700221 break;
222 }
223
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530224 div = gpmc_calc_divider(min_gpmc_clk_period);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700225 gpmc_clk_ns = gpmc_ticks_to_ns(div);
226 if (gpmc_clk_ns < 15) /* >66Mhz */
Afzal Mohammed46376882012-06-05 10:11:48 +0530227 onenand_flags |= ONENAND_FLAG_HF;
228 else
229 onenand_flags &= ~ONENAND_FLAG_HF;
Adrian Hunter1435ca02011-02-07 10:46:58 +0200230 if (gpmc_clk_ns < 12) /* >83Mhz */
Afzal Mohammed46376882012-06-05 10:11:48 +0530231 onenand_flags |= ONENAND_FLAG_VHF;
232 else
233 onenand_flags &= ~ONENAND_FLAG_VHF;
234 if (onenand_flags & ONENAND_FLAG_VHF)
Adrian Hunter1435ca02011-02-07 10:46:58 +0200235 latency = 8;
Afzal Mohammed46376882012-06-05 10:11:48 +0530236 else if (onenand_flags & ONENAND_FLAG_HF)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700237 latency = 6;
238 else if (gpmc_clk_ns >= 25) /* 40 MHz*/
239 latency = 3;
240 else
241 latency = 4;
242
Afzal Mohammed46376882012-06-05 10:11:48 +0530243 /* Set synchronous read timings */
Afzal Mohammed4f4426f2012-11-09 18:05:17 +0530244 memset(&dev_t, 0, sizeof(dev_t));
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700245
Afzal Mohammed46376882012-06-05 10:11:48 +0530246 if (onenand_flags & ONENAND_FLAG_SYNCWRITE) {
Jon Hunterc3be5b42013-02-21 13:46:22 -0600247 onenand_sync.sync_write = true;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700248 } else {
Afzal Mohammed4f4426f2012-11-09 18:05:17 +0530249 dev_t.t_avdp_w = max(t_avdp, t_cer) * 1000;
250 dev_t.t_wpl = t_wpl * 1000;
251 dev_t.t_wph = t_wph * 1000;
252 dev_t.t_aavdh = t_aavdh * 1000;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700253 }
Afzal Mohammed4f4426f2012-11-09 18:05:17 +0530254 dev_t.ce_xdelay = true;
255 dev_t.avd_xdelay = true;
256 dev_t.oe_xdelay = true;
257 dev_t.we_xdelay = true;
258 dev_t.clk = min_gpmc_clk_period;
259 dev_t.t_bacc = dev_t.clk;
260 dev_t.t_ces = t_ces * 1000;
261 dev_t.t_avds = t_avds * 1000;
262 dev_t.t_avdh = t_avdh * 1000;
263 dev_t.t_ach = t_ach * 1000;
264 dev_t.cyc_iaa = (latency + 1);
265 dev_t.t_cez_r = t_cez * 1000;
266 dev_t.t_cez_w = dev_t.t_cez_r;
267 dev_t.cyc_aavdh_oe = 1;
268 dev_t.t_rdyo = t_rdyo * 1000 + min_gpmc_clk_period;
269
Jon Hunterc3be5b42013-02-21 13:46:22 -0600270 gpmc_calc_timings(t, &onenand_sync, &dev_t);
Afzal Mohammed46376882012-06-05 10:11:48 +0530271}
272
273static int gpmc_set_sync_mode(int cs, struct gpmc_timings *t)
274{
275 unsigned sync_read = onenand_flags & ONENAND_FLAG_SYNCREAD;
276 unsigned sync_write = onenand_flags & ONENAND_FLAG_SYNCWRITE;
277
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700278 /* Configure GPMC for synchronous read */
279 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
280 GPMC_CONFIG1_WRAPBURST_SUPP |
281 GPMC_CONFIG1_READMULTIPLE_SUPP |
282 (sync_read ? GPMC_CONFIG1_READTYPE_SYNC : 0) |
283 (sync_write ? GPMC_CONFIG1_WRITEMULTIPLE_SUPP : 0) |
284 (sync_write ? GPMC_CONFIG1_WRITETYPE_SYNC : 0) |
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700285 GPMC_CONFIG1_PAGE_LEN(2) |
286 (cpu_is_omap34xx() ? 0 :
287 (GPMC_CONFIG1_WAIT_READ_MON |
288 GPMC_CONFIG1_WAIT_PIN_SEL(0))) |
289 GPMC_CONFIG1_DEVICESIZE_16 |
290 GPMC_CONFIG1_DEVICETYPE_NOR |
291 GPMC_CONFIG1_MUXADDDATA);
292
Afzal Mohammed46376882012-06-05 10:11:48 +0530293 return gpmc_cs_set_timings(cs, t);
294}
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700295
Afzal Mohammed46376882012-06-05 10:11:48 +0530296static int omap2_onenand_setup_async(void __iomem *onenand_base)
297{
298 struct gpmc_timings t;
299 int ret;
300
301 omap2_onenand_set_async_mode(onenand_base);
302
Jon Hunterbe9f10c2013-02-21 15:20:53 -0600303 omap2_onenand_calc_async_timings(&t);
Afzal Mohammed46376882012-06-05 10:11:48 +0530304
305 ret = gpmc_set_async_mode(gpmc_onenand_data->cs, &t);
Russell King71856842013-03-13 20:44:21 +0000306 if (ret < 0)
Afzal Mohammed46376882012-06-05 10:11:48 +0530307 return ret;
308
309 omap2_onenand_set_async_mode(onenand_base);
310
311 return 0;
312}
313
314static int omap2_onenand_setup_sync(void __iomem *onenand_base, int *freq_ptr)
315{
316 int ret, freq = *freq_ptr;
317 struct gpmc_timings t;
Afzal Mohammed46376882012-06-05 10:11:48 +0530318
319 if (!freq) {
320 /* Very first call freq is not known */
Jon Hunter757ef792012-06-28 13:41:29 -0500321 freq = omap2_onenand_get_freq(gpmc_onenand_data, onenand_base);
Afzal Mohammed46376882012-06-05 10:11:48 +0530322 set_onenand_cfg(onenand_base);
323 }
324
Jon Hunterbe9f10c2013-02-21 15:20:53 -0600325 omap2_onenand_calc_sync_timings(&t, gpmc_onenand_data->flags, freq);
Afzal Mohammed46376882012-06-05 10:11:48 +0530326
327 ret = gpmc_set_sync_mode(gpmc_onenand_data->cs, &t);
Russell King71856842013-03-13 20:44:21 +0000328 if (ret < 0)
Afzal Mohammed46376882012-06-05 10:11:48 +0530329 return ret;
330
331 set_onenand_cfg(onenand_base);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700332
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200333 *freq_ptr = freq;
334
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700335 return 0;
336}
337
Adrian Hunter3ad2d862011-02-07 10:46:59 +0200338static int gpmc_onenand_setup(void __iomem *onenand_base, int *freq_ptr)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700339{
340 struct device *dev = &gpmc_onenand_device.dev;
Afzal Mohammed46376882012-06-05 10:11:48 +0530341 unsigned l = ONENAND_SYNC_READ | ONENAND_SYNC_READWRITE;
342 int ret;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700343
Afzal Mohammed46376882012-06-05 10:11:48 +0530344 ret = omap2_onenand_setup_async(onenand_base);
345 if (ret) {
346 dev_err(dev, "unable to set to async mode\n");
347 return ret;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700348 }
349
Afzal Mohammed46376882012-06-05 10:11:48 +0530350 if (!(gpmc_onenand_data->flags & l))
351 return 0;
352
353 ret = omap2_onenand_setup_sync(onenand_base, freq_ptr);
354 if (ret)
355 dev_err(dev, "unable to set to sync mode\n");
356 return ret;
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700357}
358
Ezequiel Garciafaf5d2f2013-01-25 09:23:10 -0300359void gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700360{
Afzal Mohammed681988b2012-08-30 12:53:23 -0700361 int err;
Ezequiel Garciadf25cb42013-02-12 16:22:22 -0300362 struct device *dev = &gpmc_onenand_device.dev;
Afzal Mohammed681988b2012-08-30 12:53:23 -0700363
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700364 gpmc_onenand_data = _onenand_data;
365 gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
366 gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;
367
368 if (cpu_is_omap24xx() &&
369 (gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
Ezequiel Garcia7ab91592013-02-12 16:22:23 -0300370 dev_warn(dev, "OneNAND using only SYNC_READ on 24xx\n");
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700371 gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
372 gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
373 }
374
Afzal Mohammedeb77b6a2012-10-05 11:39:54 +0530375 if (cpu_is_omap34xx())
376 gpmc_onenand_data->flags |= ONENAND_IN_OMAP34XX;
377 else
378 gpmc_onenand_data->flags &= ~ONENAND_IN_OMAP34XX;
379
Afzal Mohammed681988b2012-08-30 12:53:23 -0700380 err = gpmc_cs_request(gpmc_onenand_data->cs, ONENAND_IO_SIZE,
381 (unsigned long *)&gpmc_onenand_resource.start);
382 if (err < 0) {
Ezequiel Garciadf25cb42013-02-12 16:22:22 -0300383 dev_err(dev, "Cannot request GPMC CS %d, error %d\n",
384 gpmc_onenand_data->cs, err);
Afzal Mohammed681988b2012-08-30 12:53:23 -0700385 return;
386 }
387
388 gpmc_onenand_resource.end = gpmc_onenand_resource.start +
389 ONENAND_IO_SIZE - 1;
390
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700391 if (platform_device_register(&gpmc_onenand_device) < 0) {
Ezequiel Garciadf25cb42013-02-12 16:22:22 -0300392 dev_err(dev, "Unable to register OneNAND device\n");
Afzal Mohammed681988b2012-08-30 12:53:23 -0700393 gpmc_cs_free(gpmc_onenand_data->cs);
Juha Yrjolaaa62e902009-05-28 13:23:52 -0700394 return;
395 }
396}