gpmc-onenand.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * linux/arch/arm/mach-omap2/gpmc-onenand.c
 *
 * Copyright (C) 2006 - 2009 Nokia Corporation
 * Contacts:	Juha Yrjola
 *		Tony Lindgren
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

13
#include <linux/string.h>
14 15 16 17
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/mtd/onenand_regs.h>
#include <linux/io.h>
18
#include <linux/platform_data/mtd-onenand-omap2.h>
19
#include <linux/err.h>
20 21 22

#include <asm/mach/flash.h>

23
#include "gpmc.h"
24
#include "soc.h"
25
#include "gpmc-onenand.h"
26

27 28
#define	ONENAND_IO_SIZE	SZ_128K

29 30 31 32 33 34 35 36
#define	ONENAND_FLAG_SYNCREAD	(1 << 0)
#define	ONENAND_FLAG_SYNCWRITE	(1 << 1)
#define	ONENAND_FLAG_HF		(1 << 2)
#define	ONENAND_FLAG_VHF	(1 << 3)

static unsigned onenand_flags;
static unsigned latency;

37 38
static struct omap_onenand_platform_data *gpmc_onenand_data;

39 40 41 42
static struct resource gpmc_onenand_resource = {
	.flags		= IORESOURCE_MEM,
};

43 44 45
static struct platform_device gpmc_onenand_device = {
	.name		= "omap2-onenand",
	.id		= -1,
46 47
	.num_resources	= 1,
	.resource	= &gpmc_onenand_resource,
48 49
};

50
static struct gpmc_settings onenand_async = {
51
	.device_width	= GPMC_DEVWIDTH_16BIT,
52 53 54 55 56
	.mux_add_data	= GPMC_MUX_AD,
};

static struct gpmc_settings onenand_sync = {
	.burst_read	= true,
57 58 59
	.burst_wrap	= true,
	.burst_len	= GPMC_BURST_16,
	.device_width	= GPMC_DEVWIDTH_16BIT,
60
	.mux_add_data	= GPMC_MUX_AD,
61
	.wait_pin	= 0,
62 63
};

64
static void omap2_onenand_calc_async_timings(struct gpmc_timings *t)
65
{
66
	struct gpmc_device_timings dev_t;
67 68 69 70 71 72 73 74 75 76
	const int t_cer = 15;
	const int t_avdp = 12;
	const int t_aavdh = 7;
	const int t_ce = 76;
	const int t_aa = 76;
	const int t_oe = 20;
	const int t_cez = 20; /* max of t_cez, t_oez */
	const int t_wpl = 40;
	const int t_wph = 30;

77 78 79 80 81 82 83 84 85 86 87 88 89
	memset(&dev_t, 0, sizeof(dev_t));

	dev_t.t_avdp_r = max_t(int, t_avdp, t_cer) * 1000;
	dev_t.t_avdp_w = dev_t.t_avdp_r;
	dev_t.t_aavdh = t_aavdh * 1000;
	dev_t.t_aa = t_aa * 1000;
	dev_t.t_ce = t_ce * 1000;
	dev_t.t_oe = t_oe * 1000;
	dev_t.t_cez_r = t_cez * 1000;
	dev_t.t_cez_w = dev_t.t_cez_r;
	dev_t.t_wpl = t_wpl * 1000;
	dev_t.t_wph = t_wph * 1000;

90
	gpmc_calc_timings(t, &onenand_async, &dev_t);
91 92 93 94 95
}

static void omap2_onenand_set_async_mode(void __iomem *onenand_base)
{
	u32 reg;
96 97 98 99 100

	/* Ensure sync read and sync write are disabled */
	reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
	reg &= ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE;
	writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
101 102
}

103
static void set_onenand_cfg(void __iomem *onenand_base)
104 105 106 107 108 109 110
{
	u32 reg;

	reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
	reg &= ~((0x7 << ONENAND_SYS_CFG1_BRL_SHIFT) | (0x7 << 9));
	reg |=	(latency << ONENAND_SYS_CFG1_BRL_SHIFT) |
		ONENAND_SYS_CFG1_BL_16;
111
	if (onenand_flags & ONENAND_FLAG_SYNCREAD)
112 113 114
		reg |= ONENAND_SYS_CFG1_SYNC_READ;
	else
		reg &= ~ONENAND_SYS_CFG1_SYNC_READ;
115
	if (onenand_flags & ONENAND_FLAG_SYNCWRITE)
116 117 118
		reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
	else
		reg &= ~ONENAND_SYS_CFG1_SYNC_WRITE;
119
	if (onenand_flags & ONENAND_FLAG_HF)
120 121 122
		reg |= ONENAND_SYS_CFG1_HF;
	else
		reg &= ~ONENAND_SYS_CFG1_HF;
123
	if (onenand_flags & ONENAND_FLAG_VHF)
124 125 126
		reg |= ONENAND_SYS_CFG1_VHF;
	else
		reg &= ~ONENAND_SYS_CFG1_VHF;
127 128 129
	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)
132 133
{
	u16 ver = readw(onenand_base + ONENAND_REG_VERSION_ID);
134
	int freq;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	switch ((ver >> 4) & 0xf) {
	case 0:
		freq = 40;
		break;
	case 1:
		freq = 54;
		break;
	case 2:
		freq = 66;
		break;
	case 3:
		freq = 83;
		break;
	case 4:
		freq = 104;
		break;
	default:
		freq = 54;
		break;
	}

	return freq;
}

160 161 162
static void omap2_onenand_calc_sync_timings(struct gpmc_timings *t,
					    unsigned int flags,
					    int freq)
163
{
164
	struct gpmc_device_timings dev_t;
165 166 167 168 169 170
	const int t_cer  = 15;
	const int t_avdp = 12;
	const int t_cez  = 20; /* max of t_cez, t_oez */
	const int t_wpl  = 40;
	const int t_wph  = 30;
	int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
171
	int div, gpmc_clk_ns;
172

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;
177 178

	switch (freq) {
179 180 181 182 183 184 185
	case 104:
		min_gpmc_clk_period = 9600; /* 104 MHz */
		t_ces   = 3;
		t_avds  = 4;
		t_avdh  = 2;
		t_ach   = 3;
		t_aavdh = 6;
186
		t_rdyo  = 6;
187
		break;
188
	case 83:
189
		min_gpmc_clk_period = 12000; /* 83 MHz */
190 191 192 193 194 195 196 197
		t_ces   = 5;
		t_avds  = 4;
		t_avdh  = 2;
		t_ach   = 6;
		t_aavdh = 6;
		t_rdyo  = 9;
		break;
	case 66:
198
		min_gpmc_clk_period = 15000; /* 66 MHz */
199 200 201 202 203 204 205 206
		t_ces   = 6;
		t_avds  = 5;
		t_avdh  = 2;
		t_ach   = 6;
		t_aavdh = 6;
		t_rdyo  = 11;
		break;
	default:
207
		min_gpmc_clk_period = 18500; /* 54 MHz */
208 209 210 211 212 213
		t_ces   = 7;
		t_avds  = 7;
		t_avdh  = 7;
		t_ach   = 9;
		t_aavdh = 7;
		t_rdyo  = 15;
214
		onenand_flags &= ~ONENAND_FLAG_SYNCWRITE;
215 216 217
		break;
	}

218
	div = gpmc_calc_divider(min_gpmc_clk_period);
219 220
	gpmc_clk_ns = gpmc_ticks_to_ns(div);
	if (gpmc_clk_ns < 15) /* >66Mhz */
221 222 223
		onenand_flags |= ONENAND_FLAG_HF;
	else
		onenand_flags &= ~ONENAND_FLAG_HF;
224
	if (gpmc_clk_ns < 12) /* >83Mhz */
225 226 227 228
		onenand_flags |= ONENAND_FLAG_VHF;
	else
		onenand_flags &= ~ONENAND_FLAG_VHF;
	if (onenand_flags & ONENAND_FLAG_VHF)
229
		latency = 8;
230
	else if (onenand_flags & ONENAND_FLAG_HF)
231 232 233 234 235 236
		latency = 6;
	else if (gpmc_clk_ns >= 25) /* 40 MHz*/
		latency = 3;
	else
		latency = 4;

237
	/* Set synchronous read timings */
238
	memset(&dev_t, 0, sizeof(dev_t));
239

240 241
	if (onenand_flags & ONENAND_FLAG_SYNCREAD)
		onenand_sync.sync_read = true;
242
	if (onenand_flags & ONENAND_FLAG_SYNCWRITE) {
243
		onenand_sync.sync_write = true;
244
		onenand_sync.burst_write = true;
245
	} else {
246 247 248 249
		dev_t.t_avdp_w = max(t_avdp, t_cer) * 1000;
		dev_t.t_wpl = t_wpl * 1000;
		dev_t.t_wph = t_wph * 1000;
		dev_t.t_aavdh = t_aavdh * 1000;
250
	}
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	dev_t.ce_xdelay = true;
	dev_t.avd_xdelay = true;
	dev_t.oe_xdelay = true;
	dev_t.we_xdelay = true;
	dev_t.clk = min_gpmc_clk_period;
	dev_t.t_bacc = dev_t.clk;
	dev_t.t_ces = t_ces * 1000;
	dev_t.t_avds = t_avds * 1000;
	dev_t.t_avdh = t_avdh * 1000;
	dev_t.t_ach = t_ach * 1000;
	dev_t.cyc_iaa = (latency + 1);
	dev_t.t_cez_r = t_cez * 1000;
	dev_t.t_cez_w = dev_t.t_cez_r;
	dev_t.cyc_aavdh_oe = 1;
	dev_t.t_rdyo = t_rdyo * 1000 + min_gpmc_clk_period;

267
	gpmc_calc_timings(t, &onenand_sync, &dev_t);
268 269 270 271 272 273 274 275 276
}

static int omap2_onenand_setup_async(void __iomem *onenand_base)
{
	struct gpmc_timings t;
	int ret;

	omap2_onenand_set_async_mode(onenand_base);

277
	omap2_onenand_calc_async_timings(&t);
278

279 280 281 282 283
	ret = gpmc_cs_program_settings(gpmc_onenand_data->cs, &onenand_async);
	if (ret < 0)
		return ret;

	ret = gpmc_cs_set_timings(gpmc_onenand_data->cs, &t);
284
	if (ret < 0)
285 286 287 288 289 290 291 292 293 294 295 296 297 298
		return ret;

	omap2_onenand_set_async_mode(onenand_base);

	return 0;
}

static int omap2_onenand_setup_sync(void __iomem *onenand_base, int *freq_ptr)
{
	int ret, freq = *freq_ptr;
	struct gpmc_timings t;

	if (!freq) {
		/* Very first call freq is not known */
299
		freq = omap2_onenand_get_freq(gpmc_onenand_data, onenand_base);
300 301 302
		set_onenand_cfg(onenand_base);
	}

303 304 305 306 307 308 309
	/*
	 * FIXME: Appears to be legacy code from initial ONENAND commit.
	 * Unclear what boards this is for and if this can be removed.
	 */
	if (!cpu_is_omap34xx())
		onenand_sync.wait_on_read = true;

310
	omap2_onenand_calc_sync_timings(&t, gpmc_onenand_data->flags, freq);
311

312 313 314 315 316
	ret = gpmc_cs_program_settings(gpmc_onenand_data->cs, &onenand_sync);
	if (ret < 0)
		return ret;

	ret = gpmc_cs_set_timings(gpmc_onenand_data->cs, &t);
317
	if (ret < 0)
318 319 320
		return ret;

	set_onenand_cfg(onenand_base);
321

322 323
	*freq_ptr = freq;

324 325 326
	return 0;
}

327
static int gpmc_onenand_setup(void __iomem *onenand_base, int *freq_ptr)
328 329
{
	struct device *dev = &gpmc_onenand_device.dev;
330 331
	unsigned l = ONENAND_SYNC_READ | ONENAND_SYNC_READWRITE;
	int ret;
332

333 334 335 336
	ret = omap2_onenand_setup_async(onenand_base);
	if (ret) {
		dev_err(dev, "unable to set to async mode\n");
		return ret;
337 338
	}

339 340 341 342 343 344 345
	if (!(gpmc_onenand_data->flags & l))
		return 0;

	ret = omap2_onenand_setup_sync(onenand_base, freq_ptr);
	if (ret)
		dev_err(dev, "unable to set to sync mode\n");
	return ret;
346 347
}

348
void gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
349
{
350
	int err;
351
	struct device *dev = &gpmc_onenand_device.dev;
352

353 354 355 356 357 358
	gpmc_onenand_data = _onenand_data;
	gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
	gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;

	if (cpu_is_omap24xx() &&
			(gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
359
		dev_warn(dev, "OneNAND using only SYNC_READ on 24xx\n");
360 361 362 363
		gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
		gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
	}

364 365 366 367 368
	if (cpu_is_omap34xx())
		gpmc_onenand_data->flags |= ONENAND_IN_OMAP34XX;
	else
		gpmc_onenand_data->flags &= ~ONENAND_IN_OMAP34XX;

369 370 371
	err = gpmc_cs_request(gpmc_onenand_data->cs, ONENAND_IO_SIZE,
				(unsigned long *)&gpmc_onenand_resource.start);
	if (err < 0) {
372 373
		dev_err(dev, "Cannot request GPMC CS %d, error %d\n",
			gpmc_onenand_data->cs, err);
374 375 376 377 378 379
		return;
	}

	gpmc_onenand_resource.end = gpmc_onenand_resource.start +
							ONENAND_IO_SIZE - 1;

380
	if (platform_device_register(&gpmc_onenand_device) < 0) {
381
		dev_err(dev, "Unable to register OneNAND device\n");
382
		gpmc_cs_free(gpmc_onenand_data->cs);
383 384 385
		return;
	}
}