fsmc_nand.c 31.0 KB
Newer Older
L
Linus Walleij 已提交
1 2 3 4 5 6 7 8 9
/*
 * ST Microelectronics
 * Flexible Static Memory Controller (FSMC)
 * Driver for NAND portions
 *
 * Copyright © 2010 ST Microelectronics
 * Vipin Kumar <vipin.kumar@st.com>
 * Ashish Priyadarshi
 *
10
 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
11 12
 *  Copyright © 2007 STMicroelectronics Pvt. Ltd.
 *  Copyright © 2009 Alessandro Rubini
L
Linus Walleij 已提交
13 14 15 16 17 18 19
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/clk.h>
V
Vipin Kumar 已提交
20 21 22 23
#include <linux/completion.h>
#include <linux/dmaengine.h>
#include <linux/dma-direction.h>
#include <linux/dma-mapping.h>
L
Linus Walleij 已提交
24 25 26 27 28 29 30
#include <linux/err.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/resource.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/mtd/mtd.h>
31
#include <linux/mtd/rawnand.h>
L
Linus Walleij 已提交
32 33
#include <linux/mtd/nand_ecc.h>
#include <linux/platform_device.h>
34
#include <linux/of.h>
L
Linus Walleij 已提交
35 36 37
#include <linux/mtd/partitions.h>
#include <linux/io.h>
#include <linux/slab.h>
38
#include <linux/amba/bus.h>
L
Linus Walleij 已提交
39 40
#include <mtd/mtd-abi.h>

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/* fsmc controller registers for NOR flash */
#define CTRL			0x0
	/* ctrl register definitions */
	#define BANK_ENABLE		(1 << 0)
	#define MUXED			(1 << 1)
	#define NOR_DEV			(2 << 2)
	#define WIDTH_8			(0 << 4)
	#define WIDTH_16		(1 << 4)
	#define RSTPWRDWN		(1 << 6)
	#define WPROT			(1 << 7)
	#define WRT_ENABLE		(1 << 12)
	#define WAIT_ENB		(1 << 13)

#define CTRL_TIM		0x4
	/* ctrl_tim register definitions */

#define FSMC_NOR_BANK_SZ	0x8
#define FSMC_NOR_REG_SIZE	0x40

#define FSMC_NOR_REG(base, bank, reg)		(base + \
						FSMC_NOR_BANK_SZ * (bank) + \
						reg)

/* fsmc controller registers for NAND flash */
65
#define FSMC_PC			0x00
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	/* pc register definitions */
	#define FSMC_RESET		(1 << 0)
	#define FSMC_WAITON		(1 << 1)
	#define FSMC_ENABLE		(1 << 2)
	#define FSMC_DEVTYPE_NAND	(1 << 3)
	#define FSMC_DEVWID_8		(0 << 4)
	#define FSMC_DEVWID_16		(1 << 4)
	#define FSMC_ECCEN		(1 << 6)
	#define FSMC_ECCPLEN_512	(0 << 7)
	#define FSMC_ECCPLEN_256	(1 << 7)
	#define FSMC_TCLR_1		(1)
	#define FSMC_TCLR_SHIFT		(9)
	#define FSMC_TCLR_MASK		(0xF)
	#define FSMC_TAR_1		(1)
	#define FSMC_TAR_SHIFT		(13)
	#define FSMC_TAR_MASK		(0xF)
#define STS			0x04
	/* sts register definitions */
	#define FSMC_CODE_RDY		(1 << 15)
#define COMM			0x08
	/* comm register definitions */
	#define FSMC_TSET_0		0
	#define FSMC_TSET_SHIFT		0
	#define FSMC_TSET_MASK		0xFF
	#define FSMC_TWAIT_6		6
	#define FSMC_TWAIT_SHIFT	8
	#define FSMC_TWAIT_MASK		0xFF
	#define FSMC_THOLD_4		4
	#define FSMC_THOLD_SHIFT	16
	#define FSMC_THOLD_MASK		0xFF
	#define FSMC_THIZ_1		1
	#define FSMC_THIZ_SHIFT		24
	#define FSMC_THIZ_MASK		0xFF
#define ATTRIB			0x0C
#define IOATA			0x10
#define ECC1			0x14
#define ECC2			0x18
#define ECC3			0x1C
#define FSMC_NAND_BANK_SZ	0x20

#define FSMC_BUSY_WAIT_TIMEOUT	(1 * HZ)

struct fsmc_nand_timings {
	uint8_t tclr;
	uint8_t tar;
	uint8_t thiz;
	uint8_t thold;
	uint8_t twait;
	uint8_t tset;
};

enum access_mode {
	USE_DMA_ACCESS = 1,
	USE_WORD_ACCESS,
};

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * struct fsmc_nand_data - structure for FSMC NAND device state
 *
 * @pid:		Part ID on the AMBA PrimeCell format
 * @mtd:		MTD info for a NAND flash.
 * @nand:		Chip related info for a NAND flash.
 * @partitions:		Partition info for a NAND Flash.
 * @nr_partitions:	Total number of partition of a NAND flash.
 *
 * @bank:		Bank number for probed device.
 * @clk:		Clock structure for FSMC.
 *
 * @read_dma_chan:	DMA channel for read access
 * @write_dma_chan:	DMA channel for write access to NAND
 * @dma_access_complete: Completion structure
 *
 * @data_pa:		NAND Physical port for Data.
 * @data_va:		NAND port for Data.
 * @cmd_va:		NAND port for Command.
 * @addr_va:		NAND port for Address.
142
 * @regs_va:		Registers base address for a given bank.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
 */
struct fsmc_nand_data {
	u32			pid;
	struct nand_chip	nand;

	unsigned int		bank;
	struct device		*dev;
	enum access_mode	mode;
	struct clk		*clk;

	/* DMA related objects */
	struct dma_chan		*read_dma_chan;
	struct dma_chan		*write_dma_chan;
	struct completion	dma_access_complete;

	struct fsmc_nand_timings *dev_timings;

	dma_addr_t		data_pa;
	void __iomem		*data_va;
	void __iomem		*cmd_va;
	void __iomem		*addr_va;
	void __iomem		*regs_va;
};

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
				   struct mtd_oob_region *oobregion)
{
	struct nand_chip *chip = mtd_to_nand(mtd);

	if (section >= chip->ecc.steps)
		return -ERANGE;

	oobregion->offset = (section * 16) + 2;
	oobregion->length = 3;

	return 0;
}

static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
				    struct mtd_oob_region *oobregion)
{
	struct nand_chip *chip = mtd_to_nand(mtd);

	if (section >= chip->ecc.steps)
		return -ERANGE;

	oobregion->offset = (section * 16) + 8;

	if (section < chip->ecc.steps - 1)
		oobregion->length = 8;
	else
		oobregion->length = mtd->oobsize - oobregion->offset;

	return 0;
}

static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
	.ecc = fsmc_ecc1_ooblayout_ecc,
	.free = fsmc_ecc1_ooblayout_free,
};

204 205 206 207 208 209
/*
 * ECC placement definitions in oobfree type format.
 * There are 13 bytes of ecc for every 512 byte block and it has to be read
 * consecutively and immediately after the 512 byte data block for hardware to
 * generate the error bit offsets in 512 byte data.
 */
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
				   struct mtd_oob_region *oobregion)
{
	struct nand_chip *chip = mtd_to_nand(mtd);

	if (section >= chip->ecc.steps)
		return -ERANGE;

	oobregion->length = chip->ecc.bytes;

	if (!section && mtd->writesize <= 512)
		oobregion->offset = 0;
	else
		oobregion->offset = (section * 16) + 2;

	return 0;
}

static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
				    struct mtd_oob_region *oobregion)
{
	struct nand_chip *chip = mtd_to_nand(mtd);

	if (section >= chip->ecc.steps)
		return -ERANGE;

	oobregion->offset = (section * 16) + 15;

	if (section < chip->ecc.steps - 1)
		oobregion->length = 3;
	else
		oobregion->length = mtd->oobsize - oobregion->offset;

	return 0;
}

static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
	.ecc = fsmc_ecc4_ooblayout_ecc,
	.free = fsmc_ecc4_ooblayout_free,
};

251 252
static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
{
253
	return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
254 255
}

L
Linus Walleij 已提交
256 257 258 259 260 261
/*
 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
 *
 * This routine initializes timing parameters related to NAND memory access in
 * FSMC registers
 */
262
static void fsmc_nand_setup(struct fsmc_nand_data *host,
263
			    struct fsmc_nand_timings *tims)
L
Linus Walleij 已提交
264 265
{
	uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
266 267 268 269 270 271 272 273
	uint32_t tclr, tar, thiz, thold, twait, tset;

	tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
	tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
	thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
	thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
	twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
	tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
L
Linus Walleij 已提交
274

275
	if (host->nand.options & NAND_BUSWIDTH_16)
276 277
		writel_relaxed(value | FSMC_DEVWID_16,
			       host->regs_va + FSMC_PC);
L
Linus Walleij 已提交
278
	else
279
		writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + FSMC_PC);
280

281 282
	writel_relaxed(readl(host->regs_va + FSMC_PC) | tclr | tar,
		       host->regs_va + FSMC_PC);
283 284
	writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
	writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
L
Linus Walleij 已提交
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static int fsmc_calc_timings(struct fsmc_nand_data *host,
			     const struct nand_sdr_timings *sdrt,
			     struct fsmc_nand_timings *tims)
{
	unsigned long hclk = clk_get_rate(host->clk);
	unsigned long hclkn = NSEC_PER_SEC / hclk;
	uint32_t thiz, thold, twait, tset;

	if (sdrt->tRC_min < 30000)
		return -EOPNOTSUPP;

	tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
	if (tims->tar > FSMC_TAR_MASK)
		tims->tar = FSMC_TAR_MASK;
	tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
	if (tims->tclr > FSMC_TCLR_MASK)
		tims->tclr = FSMC_TCLR_MASK;

	thiz = sdrt->tCS_min - sdrt->tWP_min;
	tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);

	thold = sdrt->tDH_min;
	if (thold < sdrt->tCH_min)
		thold = sdrt->tCH_min;
	if (thold < sdrt->tCLH_min)
		thold = sdrt->tCLH_min;
	if (thold < sdrt->tWH_min)
		thold = sdrt->tWH_min;
	if (thold < sdrt->tALH_min)
		thold = sdrt->tALH_min;
	if (thold < sdrt->tREH_min)
		thold = sdrt->tREH_min;
	tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
	if (tims->thold == 0)
		tims->thold = 1;
	else if (tims->thold > FSMC_THOLD_MASK)
		tims->thold = FSMC_THOLD_MASK;

	twait = max(sdrt->tRP_min, sdrt->tWP_min);
	tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
	if (tims->twait == 0)
		tims->twait = 1;
	else if (tims->twait > FSMC_TWAIT_MASK)
		tims->twait = FSMC_TWAIT_MASK;

	tset = max(sdrt->tCS_min - sdrt->tWP_min,
		   sdrt->tCEA_max - sdrt->tREA_max);
	tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
	if (tims->tset == 0)
		tims->tset = 1;
	else if (tims->tset > FSMC_TSET_MASK)
		tims->tset = FSMC_TSET_MASK;

	return 0;
}

343 344
static int fsmc_setup_data_interface(struct mtd_info *mtd, int csline,
				     const struct nand_data_interface *conf)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
	struct nand_chip *nand = mtd_to_nand(mtd);
	struct fsmc_nand_data *host = nand_get_controller_data(nand);
	struct fsmc_nand_timings tims;
	const struct nand_sdr_timings *sdrt;
	int ret;

	sdrt = nand_get_sdr_timings(conf);
	if (IS_ERR(sdrt))
		return PTR_ERR(sdrt);

	ret = fsmc_calc_timings(host, sdrt, &tims);
	if (ret)
		return ret;

360
	if (csline == NAND_DATA_IFACE_CHECK_ONLY)
361 362 363 364 365 366 367
		return 0;

	fsmc_nand_setup(host, &tims);

	return 0;
}

L
Linus Walleij 已提交
368 369 370 371 372
/*
 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
 */
static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
{
373
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
374

375 376 377 378 379 380
	writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256,
		       host->regs_va + FSMC_PC);
	writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN,
		       host->regs_va + FSMC_PC);
	writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN,
		       host->regs_va + FSMC_PC);
L
Linus Walleij 已提交
381 382 383 384
}

/*
 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
L
Lucas De Marchi 已提交
385
 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
L
Linus Walleij 已提交
386 387 388 389 390
 * max of 8-bits)
 */
static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
				uint8_t *ecc)
{
391
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
L
Linus Walleij 已提交
392 393 394 395
	uint32_t ecc_tmp;
	unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;

	do {
396
		if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
L
Linus Walleij 已提交
397 398 399 400 401
			break;
		else
			cond_resched();
	} while (!time_after_eq(jiffies, deadline));

402 403 404 405 406
	if (time_after_eq(jiffies, deadline)) {
		dev_err(host->dev, "calculate ecc timed out\n");
		return -ETIMEDOUT;
	}

407
	ecc_tmp = readl_relaxed(host->regs_va + ECC1);
L
Linus Walleij 已提交
408 409 410 411 412
	ecc[0] = (uint8_t) (ecc_tmp >> 0);
	ecc[1] = (uint8_t) (ecc_tmp >> 8);
	ecc[2] = (uint8_t) (ecc_tmp >> 16);
	ecc[3] = (uint8_t) (ecc_tmp >> 24);

413
	ecc_tmp = readl_relaxed(host->regs_va + ECC2);
L
Linus Walleij 已提交
414 415 416 417 418
	ecc[4] = (uint8_t) (ecc_tmp >> 0);
	ecc[5] = (uint8_t) (ecc_tmp >> 8);
	ecc[6] = (uint8_t) (ecc_tmp >> 16);
	ecc[7] = (uint8_t) (ecc_tmp >> 24);

419
	ecc_tmp = readl_relaxed(host->regs_va + ECC3);
L
Linus Walleij 已提交
420 421 422 423 424
	ecc[8] = (uint8_t) (ecc_tmp >> 0);
	ecc[9] = (uint8_t) (ecc_tmp >> 8);
	ecc[10] = (uint8_t) (ecc_tmp >> 16);
	ecc[11] = (uint8_t) (ecc_tmp >> 24);

425
	ecc_tmp = readl_relaxed(host->regs_va + STS);
L
Linus Walleij 已提交
426 427 428 429 430 431 432
	ecc[12] = (uint8_t) (ecc_tmp >> 16);

	return 0;
}

/*
 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
L
Lucas De Marchi 已提交
433
 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
L
Linus Walleij 已提交
434 435 436 437 438
 * max of 1-bit)
 */
static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
				uint8_t *ecc)
{
439
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
L
Linus Walleij 已提交
440 441
	uint32_t ecc_tmp;

442
	ecc_tmp = readl_relaxed(host->regs_va + ECC1);
L
Linus Walleij 已提交
443 444 445 446 447 448 449
	ecc[0] = (uint8_t) (ecc_tmp >> 0);
	ecc[1] = (uint8_t) (ecc_tmp >> 8);
	ecc[2] = (uint8_t) (ecc_tmp >> 16);

	return 0;
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463
/* Count the number of 0's in buff upto a max of max_bits */
static int count_written_bits(uint8_t *buff, int size, int max_bits)
{
	int k, written_bits = 0;

	for (k = 0; k < size; k++) {
		written_bits += hweight8(~buff[k]);
		if (written_bits > max_bits)
			break;
	}

	return written_bits;
}

V
Vipin Kumar 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
static void dma_complete(void *param)
{
	struct fsmc_nand_data *host = param;

	complete(&host->dma_access_complete);
}

static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
		enum dma_data_direction direction)
{
	struct dma_chan *chan;
	struct dma_device *dma_dev;
	struct dma_async_tx_descriptor *tx;
	dma_addr_t dma_dst, dma_src, dma_addr;
	dma_cookie_t cookie;
	unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
	int ret;
481
	unsigned long time_left;
V
Vipin Kumar 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

	if (direction == DMA_TO_DEVICE)
		chan = host->write_dma_chan;
	else if (direction == DMA_FROM_DEVICE)
		chan = host->read_dma_chan;
	else
		return -EINVAL;

	dma_dev = chan->device;
	dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);

	if (direction == DMA_TO_DEVICE) {
		dma_src = dma_addr;
		dma_dst = host->data_pa;
	} else {
		dma_src = host->data_pa;
		dma_dst = dma_addr;
	}

	tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
			len, flags);
	if (!tx) {
		dev_err(host->dev, "device_prep_dma_memcpy error\n");
505 506
		ret = -EIO;
		goto unmap_dma;
V
Vipin Kumar 已提交
507 508 509 510 511 512 513 514 515
	}

	tx->callback = dma_complete;
	tx->callback_param = host;
	cookie = tx->tx_submit(tx);

	ret = dma_submit_error(cookie);
	if (ret) {
		dev_err(host->dev, "dma_submit_error %d\n", cookie);
516
		goto unmap_dma;
V
Vipin Kumar 已提交
517 518 519 520
	}

	dma_async_issue_pending(chan);

521
	time_left =
522
	wait_for_completion_timeout(&host->dma_access_complete,
V
Vipin Kumar 已提交
523
				msecs_to_jiffies(3000));
524
	if (time_left == 0) {
525
		dmaengine_terminate_all(chan);
V
Vipin Kumar 已提交
526
		dev_err(host->dev, "wait_for_completion_timeout\n");
527
		ret = -ETIMEDOUT;
528
		goto unmap_dma;
V
Vipin Kumar 已提交
529 530
	}

531 532 533 534 535 536
	ret = 0;

unmap_dma:
	dma_unmap_single(dma_dev->dev, dma_addr, len, direction);

	return ret;
V
Vipin Kumar 已提交
537 538
}

539 540 541 542 543 544 545 546
/*
 * fsmc_write_buf - write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 */
static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
{
547
	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
548 549
	int i;

550
	if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
551 552 553 554
			IS_ALIGNED(len, sizeof(uint32_t))) {
		uint32_t *p = (uint32_t *)buf;
		len = len >> 2;
		for (i = 0; i < len; i++)
555
			writel_relaxed(p[i], host->data_va);
556 557
	} else {
		for (i = 0; i < len; i++)
558
			writeb_relaxed(buf[i], host->data_va);
559 560 561 562 563 564 565 566 567 568 569
	}
}

/*
 * fsmc_read_buf - read chip data into buffer
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 */
static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
{
570
	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
571 572
	int i;

573
	if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) &&
574 575 576 577
			IS_ALIGNED(len, sizeof(uint32_t))) {
		uint32_t *p = (uint32_t *)buf;
		len = len >> 2;
		for (i = 0; i < len; i++)
578
			p[i] = readl_relaxed(host->data_va);
579 580
	} else {
		for (i = 0; i < len; i++)
581
			buf[i] = readb_relaxed(host->data_va);
582 583 584
	}
}

V
Vipin Kumar 已提交
585 586 587 588 589 590 591 592
/*
 * fsmc_read_buf_dma - read chip data into buffer
 * @mtd:	MTD device structure
 * @buf:	buffer to store date
 * @len:	number of bytes to read
 */
static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
{
593
	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
V
Vipin Kumar 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606

	dma_xfer(host, buf, len, DMA_FROM_DEVICE);
}

/*
 * fsmc_write_buf_dma - write buffer to chip
 * @mtd:	MTD device structure
 * @buf:	data buffer
 * @len:	number of bytes to write
 */
static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
		int len)
{
607
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
V
Vipin Kumar 已提交
608 609 610 611

	dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
}

M
Miquel Raynal 已提交
612 613 614 615 616 617 618 619 620 621
/* fsmc_select_chip - assert or deassert nCE */
static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
{
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
	u32 pc;

	/* Support only one CS */
	if (chipnr > 0)
		return;

622
	pc = readl(host->regs_va + FSMC_PC);
M
Miquel Raynal 已提交
623
	if (chipnr < 0)
624
		writel_relaxed(pc & ~FSMC_ENABLE, host->regs_va + FSMC_PC);
M
Miquel Raynal 已提交
625
	else
626
		writel_relaxed(pc | FSMC_ENABLE, host->regs_va + FSMC_PC);
M
Miquel Raynal 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

	/* nCE line must be asserted before starting any operation */
	mb();
}

/*
 * fsmc_exec_op - hook called by the core to execute NAND operations
 *
 * This controller is simple enough and thus does not need to use the parser
 * provided by the core, instead, handle every situation here.
 */
static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op,
			bool check_only)
{
	struct mtd_info *mtd = nand_to_mtd(chip);
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
	const struct nand_op_instr *instr = NULL;
	int ret = 0;
	unsigned int op_id;
	int i;

	pr_debug("Executing operation [%d instructions]:\n", op->ninstrs);
	for (op_id = 0; op_id < op->ninstrs; op_id++) {
		instr = &op->instrs[op_id];

		switch (instr->type) {
		case NAND_OP_CMD_INSTR:
			pr_debug("  ->CMD      [0x%02x]\n",
				 instr->ctx.cmd.opcode);

			writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va);
			break;

		case NAND_OP_ADDR_INSTR:
			pr_debug("  ->ADDR     [%d cyc]",
				 instr->ctx.addr.naddrs);

			for (i = 0; i < instr->ctx.addr.naddrs; i++)
				writeb_relaxed(instr->ctx.addr.addrs[i],
					       host->addr_va);
			break;

		case NAND_OP_DATA_IN_INSTR:
			pr_debug("  ->DATA_IN  [%d B%s]\n", instr->ctx.data.len,
				 instr->ctx.data.force_8bit ?
				 ", force 8-bit" : "");

			if (host->mode == USE_DMA_ACCESS)
				fsmc_read_buf_dma(mtd, instr->ctx.data.buf.in,
						  instr->ctx.data.len);
			else
				fsmc_read_buf(mtd, instr->ctx.data.buf.in,
					      instr->ctx.data.len);
			break;

		case NAND_OP_DATA_OUT_INSTR:
			pr_debug("  ->DATA_OUT [%d B%s]\n", instr->ctx.data.len,
				 instr->ctx.data.force_8bit ?
				 ", force 8-bit" : "");

			if (host->mode == USE_DMA_ACCESS)
				fsmc_write_buf_dma(mtd, instr->ctx.data.buf.out,
						   instr->ctx.data.len);
			else
				fsmc_write_buf(mtd, instr->ctx.data.buf.out,
					       instr->ctx.data.len);
			break;

		case NAND_OP_WAITRDY_INSTR:
			pr_debug("  ->WAITRDY  [max %d ms]\n",
				 instr->ctx.waitrdy.timeout_ms);

			ret = nand_soft_waitrdy(chip,
						instr->ctx.waitrdy.timeout_ms);
			break;
		}
	}

	return ret;
}

L
Linus Walleij 已提交
708 709 710 711 712
/*
 * fsmc_read_page_hwecc
 * @mtd:	mtd info structure
 * @chip:	nand chip info structure
 * @buf:	buffer to store read data
713
 * @oob_required:	caller expects OOB data read to chip->oob_poi
L
Linus Walleij 已提交
714 715
 * @page:	page number to read
 *
L
Lucas De Marchi 已提交
716
 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
L
Linus Walleij 已提交
717 718
 * performed in a strict sequence as follows:
 * data(512 byte) -> ecc(13 byte)
L
Lucas De Marchi 已提交
719
 * After this read, fsmc hardware generates and reports error data bits(up to a
L
Linus Walleij 已提交
720 721 722
 * max of 8 bits)
 */
static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
723
				 uint8_t *buf, int oob_required, int page)
L
Linus Walleij 已提交
724 725 726 727 728
{
	int i, j, s, stat, eccsize = chip->ecc.size;
	int eccbytes = chip->ecc.bytes;
	int eccsteps = chip->ecc.steps;
	uint8_t *p = buf;
729 730
	uint8_t *ecc_calc = chip->ecc.calc_buf;
	uint8_t *ecc_code = chip->ecc.code_buf;
L
Linus Walleij 已提交
731 732 733 734 735 736 737 738
	int off, len, group = 0;
	/*
	 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
	 * end up reading 14 bytes (7 words) from oob. The local array is
	 * to maintain word alignment
	 */
	uint16_t ecc_oob[7];
	uint8_t *oob = (uint8_t *)&ecc_oob[0];
739
	unsigned int max_bitflips = 0;
L
Linus Walleij 已提交
740 741

	for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
742
		nand_read_page_op(chip, page, s * eccsize, NULL, 0);
L
Linus Walleij 已提交
743
		chip->ecc.hwctl(mtd, NAND_ECC_READ);
744
		nand_read_data_op(chip, p, eccsize, false);
L
Linus Walleij 已提交
745 746

		for (j = 0; j < eccbytes;) {
747 748 749 750 751 752 753 754 755
			struct mtd_oob_region oobregion;
			int ret;

			ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
			if (ret)
				return ret;

			off = oobregion.offset;
			len = oobregion.length;
L
Linus Walleij 已提交
756 757

			/*
758 759 760 761
			 * length is intentionally kept a higher multiple of 2
			 * to read at least 13 bytes even in case of 16 bit NAND
			 * devices
			 */
762 763 764
			if (chip->options & NAND_BUSWIDTH_16)
				len = roundup(len, 2);

765
			nand_read_oob_op(chip, page, off, oob + j, len);
L
Linus Walleij 已提交
766 767 768
			j += len;
		}

769
		memcpy(&ecc_code[i], oob, chip->ecc.bytes);
L
Linus Walleij 已提交
770 771 772
		chip->ecc.calculate(mtd, p, &ecc_calc[i]);

		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
773
		if (stat < 0) {
L
Linus Walleij 已提交
774
			mtd->ecc_stats.failed++;
775
		} else {
L
Linus Walleij 已提交
776
			mtd->ecc_stats.corrected += stat;
777 778
			max_bitflips = max_t(unsigned int, max_bitflips, stat);
		}
L
Linus Walleij 已提交
779 780
	}

781
	return max_bitflips;
L
Linus Walleij 已提交
782 783 784
}

/*
785
 * fsmc_bch8_correct_data
L
Linus Walleij 已提交
786 787 788 789 790 791 792 793
 * @mtd:	mtd info structure
 * @dat:	buffer of read data
 * @read_ecc:	ecc read from device spare area
 * @calc_ecc:	ecc calculated from read data
 *
 * calc_ecc is a 104 bit information containing maximum of 8 error
 * offset informations of 13 bits each in 512 bytes of read data.
 */
794
static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
L
Linus Walleij 已提交
795 796
			     uint8_t *read_ecc, uint8_t *calc_ecc)
{
797
	struct nand_chip *chip = mtd_to_nand(mtd);
798
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
799
	uint32_t err_idx[8];
L
Linus Walleij 已提交
800
	uint32_t num_err, i;
801
	uint32_t ecc1, ecc2, ecc3, ecc4;
L
Linus Walleij 已提交
802

803
	num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

	/* no bit flipping */
	if (likely(num_err == 0))
		return 0;

	/* too many errors */
	if (unlikely(num_err > 8)) {
		/*
		 * This is a temporary erase check. A newly erased page read
		 * would result in an ecc error because the oob data is also
		 * erased to FF and the calculated ecc for an FF data is not
		 * FF..FF.
		 * This is a workaround to skip performing correction in case
		 * data is FF..FF
		 *
		 * Logic:
		 * For every page, each bit written as 0 is counted until these
		 * number of bits are greater than 8 (the maximum correction
		 * capability of FSMC for each 512 + 13 bytes)
		 */

		int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
		int bits_data = count_written_bits(dat, chip->ecc.size, 8);

		if ((bits_ecc + bits_data) <= 8) {
			if (bits_data)
				memset(dat, 0xff, chip->ecc.size);
			return bits_data;
		}

		return -EBADMSG;
	}

L
Linus Walleij 已提交
837 838 839 840 841 842 843 844 845
	/*
	 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
	 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
	 *
	 * calc_ecc is a 104 bit information containing maximum of 8 error
	 * offset informations of 13 bits each. calc_ecc is copied into a
	 * uint64_t array and error offset indexes are populated in err_idx
	 * array
	 */
846 847 848 849
	ecc1 = readl_relaxed(host->regs_va + ECC1);
	ecc2 = readl_relaxed(host->regs_va + ECC2);
	ecc3 = readl_relaxed(host->regs_va + ECC3);
	ecc4 = readl_relaxed(host->regs_va + STS);
850 851 852 853 854 855 856 857 858

	err_idx[0] = (ecc1 >> 0) & 0x1FFF;
	err_idx[1] = (ecc1 >> 13) & 0x1FFF;
	err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
	err_idx[3] = (ecc2 >> 7) & 0x1FFF;
	err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
	err_idx[5] = (ecc3 >> 1) & 0x1FFF;
	err_idx[6] = (ecc3 >> 14) & 0x1FFF;
	err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
L
Linus Walleij 已提交
859 860 861 862 863 864

	i = 0;
	while (num_err--) {
		change_bit(0, (unsigned long *)&err_idx[i]);
		change_bit(1, (unsigned long *)&err_idx[i]);

865
		if (err_idx[i] < chip->ecc.size * 8) {
L
Linus Walleij 已提交
866 867 868 869 870 871 872
			change_bit(err_idx[i], (unsigned long *)dat);
			i++;
		}
	}
	return i;
}

V
Vipin Kumar 已提交
873 874 875 876 877 878
static bool filter(struct dma_chan *chan, void *slave)
{
	chan->private = slave;
	return true;
}

B
Bill Pemberton 已提交
879
static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
880 881
				     struct fsmc_nand_data *host,
				     struct nand_chip *nand)
882
{
883
	struct device_node *np = pdev->dev.of_node;
884
	u32 val;
885
	int ret;
886

887
	nand->options = 0;
888

889 890
	if (!of_property_read_u32(np, "bank-width", &val)) {
		if (val == 2) {
891
			nand->options |= NAND_BUSWIDTH_16;
892 893 894 895 896
		} else if (val != 1) {
			dev_err(&pdev->dev, "invalid bank-width %u\n", val);
			return -EINVAL;
		}
	}
897

898
	if (of_get_property(np, "nand-skip-bbtscan", NULL))
899
		nand->options |= NAND_SKIP_BBTSCAN;
900

901 902 903
	host->dev_timings = devm_kzalloc(&pdev->dev,
				sizeof(*host->dev_timings), GFP_KERNEL);
	if (!host->dev_timings)
904
		return -ENOMEM;
905 906
	ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
						sizeof(*host->dev_timings));
907
	if (ret)
908
		host->dev_timings = NULL;
909 910

	/* Set default NAND bank to 0 */
911
	host->bank = 0;
912 913 914 915 916
	if (!of_property_read_u32(np, "bank", &val)) {
		if (val > 3) {
			dev_err(&pdev->dev, "invalid bank %u\n", val);
			return -EINVAL;
		}
917
		host->bank = val;
918
	}
919 920 921
	return 0;
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
static int fsmc_nand_attach_chip(struct nand_chip *nand)
{
	struct mtd_info *mtd = nand_to_mtd(nand);
	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);

	if (AMBA_REV_BITS(host->pid) >= 8) {
		switch (mtd->oobsize) {
		case 16:
		case 64:
		case 128:
		case 224:
		case 256:
			break;
		default:
			dev_warn(host->dev,
				 "No oob scheme defined for oobsize %d\n",
				 mtd->oobsize);
			return -EINVAL;
		}

		mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);

		return 0;
	}

	switch (nand->ecc.mode) {
	case NAND_ECC_HW:
		dev_info(host->dev, "Using 1-bit HW ECC scheme\n");
		nand->ecc.calculate = fsmc_read_hwecc_ecc1;
		nand->ecc.correct = nand_correct_data;
		nand->ecc.bytes = 3;
		nand->ecc.strength = 1;
		break;

	case NAND_ECC_SOFT:
		if (nand->ecc.algo == NAND_ECC_BCH) {
			dev_info(host->dev,
				 "Using 4-bit SW BCH ECC scheme\n");
			break;
		}

	case NAND_ECC_ON_DIE:
		break;

	default:
		dev_err(host->dev, "Unsupported ECC mode!\n");
		return -ENOTSUPP;
	}

	/*
	 * Don't set layout for BCH4 SW ECC. This will be
	 * generated later in nand_bch_init() later.
	 */
	if (nand->ecc.mode == NAND_ECC_HW) {
		switch (mtd->oobsize) {
		case 16:
		case 64:
		case 128:
			mtd_set_ooblayout(mtd,
					  &fsmc_ecc1_ooblayout_ops);
			break;
		default:
			dev_warn(host->dev,
				 "No oob scheme defined for oobsize %d\n",
				 mtd->oobsize);
			return -EINVAL;
		}
	}

	return 0;
}

static const struct nand_controller_ops fsmc_nand_controller_ops = {
	.attach_chip = fsmc_nand_attach_chip,
};

L
Linus Walleij 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007
/*
 * fsmc_nand_probe - Probe function
 * @pdev:       platform device structure
 */
static int __init fsmc_nand_probe(struct platform_device *pdev)
{
	struct fsmc_nand_data *host;
	struct mtd_info *mtd;
	struct nand_chip *nand;
	struct resource *res;
1008
	void __iomem *base;
V
Vipin Kumar 已提交
1009
	dma_cap_mask_t mask;
1010
	int ret = 0;
1011 1012
	u32 pid;
	int i;
L
Linus Walleij 已提交
1013 1014

	/* Allocate memory for the device structure (and zero it) */
V
Vipin Kumar 已提交
1015
	host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
1016
	if (!host)
L
Linus Walleij 已提交
1017 1018
		return -ENOMEM;

1019 1020 1021 1022 1023 1024
	nand = &host->nand;

	ret = fsmc_nand_probe_config_dt(pdev, host, nand);
	if (ret)
		return ret;

L
Linus Walleij 已提交
1025
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
1026 1027 1028
	host->data_va = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(host->data_va))
		return PTR_ERR(host->data_va);
1029

1030
	host->data_pa = (dma_addr_t)res->start;
L
Linus Walleij 已提交
1031

1032
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
1033 1034 1035
	host->addr_va = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(host->addr_va))
		return PTR_ERR(host->addr_va);
L
Linus Walleij 已提交
1036

1037
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
1038 1039 1040
	host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(host->cmd_va))
		return PTR_ERR(host->cmd_va);
L
Linus Walleij 已提交
1041 1042

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
1043 1044 1045 1046 1047 1048
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);

	host->regs_va = base + FSMC_NOR_REG_SIZE +
		(host->bank * FSMC_NAND_BANK_SZ);
L
Linus Walleij 已提交
1049

1050
	host->clk = devm_clk_get(&pdev->dev, NULL);
L
Linus Walleij 已提交
1051 1052
	if (IS_ERR(host->clk)) {
		dev_err(&pdev->dev, "failed to fetch block clock\n");
V
Vipin Kumar 已提交
1053
		return PTR_ERR(host->clk);
L
Linus Walleij 已提交
1054 1055
	}

1056
	ret = clk_prepare_enable(host->clk);
L
Linus Walleij 已提交
1057
	if (ret)
1058
		return ret;
L
Linus Walleij 已提交
1059

1060 1061 1062 1063 1064
	/*
	 * This device ID is actually a common AMBA ID as used on the
	 * AMBA PrimeCell bus. However it is not a PrimeCell.
	 */
	for (pid = 0, i = 0; i < 4; i++)
1065
		pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
1066 1067 1068 1069 1070 1071
	host->pid = pid;
	dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
		 "revision %02x, config %02x\n",
		 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
		 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));

1072
	host->dev = &pdev->dev;
V
Vipin Kumar 已提交
1073 1074 1075 1076

	if (host->mode == USE_DMA_ACCESS)
		init_completion(&host->dma_access_complete);

L
Linus Walleij 已提交
1077
	/* Link all private pointers */
1078
	mtd = nand_to_mtd(&host->nand);
1079
	nand_set_controller_data(nand, host);
1080
	nand_set_flash_node(nand, pdev->dev.of_node);
L
Linus Walleij 已提交
1081

1082
	mtd->dev.parent = &pdev->dev;
M
Miquel Raynal 已提交
1083 1084
	nand->exec_op = fsmc_exec_op;
	nand->select_chip = fsmc_select_chip;
L
Linus Walleij 已提交
1085 1086
	nand->chip_delay = 30;

1087 1088 1089 1090
	/*
	 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
	 * can overwrite this value if the DT provides a different value.
	 */
L
Linus Walleij 已提交
1091 1092 1093
	nand->ecc.mode = NAND_ECC_HW;
	nand->ecc.hwctl = fsmc_enable_hwecc;
	nand->ecc.size = 512;
1094
	nand->badblockbits = 7;
L
Linus Walleij 已提交
1095

M
Miquel Raynal 已提交
1096
	if (host->mode == USE_DMA_ACCESS) {
V
Vipin Kumar 已提交
1097 1098
		dma_cap_zero(mask);
		dma_cap_set(DMA_MEMCPY, mask);
1099
		host->read_dma_chan = dma_request_channel(mask, filter, NULL);
V
Vipin Kumar 已提交
1100 1101
		if (!host->read_dma_chan) {
			dev_err(&pdev->dev, "Unable to get read dma channel\n");
1102
			goto disable_clk;
V
Vipin Kumar 已提交
1103
		}
1104
		host->write_dma_chan = dma_request_channel(mask, filter, NULL);
V
Vipin Kumar 已提交
1105 1106
		if (!host->write_dma_chan) {
			dev_err(&pdev->dev, "Unable to get write dma channel\n");
1107
			goto release_dma_read_chan;
V
Vipin Kumar 已提交
1108
		}
1109 1110
	}

1111 1112 1113 1114
	if (host->dev_timings)
		fsmc_nand_setup(host, host->dev_timings);
	else
		nand->setup_data_interface = fsmc_setup_data_interface;
L
Linus Walleij 已提交
1115

1116
	if (AMBA_REV_BITS(host->pid) >= 8) {
L
Linus Walleij 已提交
1117 1118
		nand->ecc.read_page = fsmc_read_page_hwecc;
		nand->ecc.calculate = fsmc_read_hwecc_ecc4;
1119
		nand->ecc.correct = fsmc_bch8_correct_data;
L
Linus Walleij 已提交
1120
		nand->ecc.bytes = 13;
M
Mike Dunn 已提交
1121
		nand->ecc.strength = 8;
L
Linus Walleij 已提交
1122 1123 1124
	}

	/*
L
Lucas De Marchi 已提交
1125
	 * Scan to find existence of the device
L
Linus Walleij 已提交
1126
	 */
1127
	nand->dummy_controller.ops = &fsmc_nand_controller_ops;
1128
	ret = nand_scan(nand, 1);
1129
	if (ret)
1130
		goto release_dma_write_chan;
L
Linus Walleij 已提交
1131

1132
	mtd->name = "nand";
1133
	ret = mtd_device_register(mtd, NULL, 0);
1134
	if (ret)
1135
		goto cleanup_nand;
L
Linus Walleij 已提交
1136 1137 1138

	platform_set_drvdata(pdev, host);
	dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1139

L
Linus Walleij 已提交
1140 1141
	return 0;

1142 1143
cleanup_nand:
	nand_cleanup(nand);
1144
release_dma_write_chan:
V
Vipin Kumar 已提交
1145 1146
	if (host->mode == USE_DMA_ACCESS)
		dma_release_channel(host->write_dma_chan);
1147
release_dma_read_chan:
V
Vipin Kumar 已提交
1148 1149
	if (host->mode == USE_DMA_ACCESS)
		dma_release_channel(host->read_dma_chan);
1150
disable_clk:
1151
	clk_disable_unprepare(host->clk);
1152

L
Linus Walleij 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	return ret;
}

/*
 * Clean up routine
 */
static int fsmc_nand_remove(struct platform_device *pdev)
{
	struct fsmc_nand_data *host = platform_get_drvdata(pdev);

	if (host) {
1164
		nand_release(&host->nand);
V
Vipin Kumar 已提交
1165 1166 1167 1168 1169

		if (host->mode == USE_DMA_ACCESS) {
			dma_release_channel(host->write_dma_chan);
			dma_release_channel(host->read_dma_chan);
		}
1170
		clk_disable_unprepare(host->clk);
L
Linus Walleij 已提交
1171
	}
V
Vipin Kumar 已提交
1172

L
Linus Walleij 已提交
1173 1174 1175
	return 0;
}

1176
#ifdef CONFIG_PM_SLEEP
L
Linus Walleij 已提交
1177 1178 1179 1180
static int fsmc_nand_suspend(struct device *dev)
{
	struct fsmc_nand_data *host = dev_get_drvdata(dev);
	if (host)
1181
		clk_disable_unprepare(host->clk);
L
Linus Walleij 已提交
1182 1183 1184 1185 1186 1187
	return 0;
}

static int fsmc_nand_resume(struct device *dev)
{
	struct fsmc_nand_data *host = dev_get_drvdata(dev);
1188
	if (host) {
1189
		clk_prepare_enable(host->clk);
1190 1191
		if (host->dev_timings)
			fsmc_nand_setup(host, host->dev_timings);
1192
	}
L
Linus Walleij 已提交
1193 1194
	return 0;
}
1195
#endif
L
Linus Walleij 已提交
1196

1197
static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
L
Linus Walleij 已提交
1198

1199 1200
static const struct of_device_id fsmc_nand_id_table[] = {
	{ .compatible = "st,spear600-fsmc-nand" },
L
Linus Walleij 已提交
1201
	{ .compatible = "stericsson,fsmc-nand" },
1202 1203 1204 1205
	{}
};
MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);

L
Linus Walleij 已提交
1206 1207 1208 1209
static struct platform_driver fsmc_nand_driver = {
	.remove = fsmc_nand_remove,
	.driver = {
		.name = "fsmc-nand",
1210
		.of_match_table = fsmc_nand_id_table,
L
Linus Walleij 已提交
1211 1212 1213 1214
		.pm = &fsmc_nand_pm_ops,
	},
};

1215
module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
L
Linus Walleij 已提交
1216 1217 1218 1219

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");