sh_flctl.c 29.1 KB
Newer Older
1 2 3
/*
 * SuperH FLCTL nand controller
 *
M
Magnus Damm 已提交
4 5
 * Copyright (c) 2008 Renesas Solutions Corp.
 * Copyright (c) 2008 Atom Create Engineering Co., Ltd.
6
 *
M
Magnus Damm 已提交
7
 * Based on fsl_elbc_nand.c, Copyright (c) 2006-2007 Freescale Semiconductor
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
B
Bastian Hecht 已提交
26
#include <linux/completion.h>
27
#include <linux/delay.h>
B
Bastian Hecht 已提交
28 29
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
30
#include <linux/interrupt.h>
31
#include <linux/io.h>
32 33 34
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_mtd.h>
35
#include <linux/platform_device.h>
36
#include <linux/pm_runtime.h>
B
Bastian Hecht 已提交
37
#include <linux/sh_dma.h>
38
#include <linux/slab.h>
39
#include <linux/string.h>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/sh_flctl.h>

static struct nand_ecclayout flctl_4secc_oob_16 = {
	.eccbytes = 10,
	.eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
	.oobfree = {
		{.offset = 12,
		. length = 4} },
};

static struct nand_ecclayout flctl_4secc_oob_64 = {
55 56 57 58 59 60
	.eccbytes = 4 * 10,
	.eccpos = {
		 6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
		22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
		38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
		54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
61
	.oobfree = {
62 63 64 65
		{.offset =  2, .length = 4},
		{.offset = 16, .length = 6},
		{.offset = 32, .length = 6},
		{.offset = 48, .length = 6} },
66 67 68 69 70 71 72 73 74 75 76 77
};

static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

static struct nand_bbt_descr flctl_4secc_smallpage = {
	.options = NAND_BBT_SCAN2NDPAGE,
	.offs = 11,
	.len = 1,
	.pattern = scan_ff_pattern,
};

static struct nand_bbt_descr flctl_4secc_largepage = {
78
	.options = NAND_BBT_SCAN2NDPAGE,
79
	.offs = 0,
80 81 82 83 84 85
	.len = 2,
	.pattern = scan_ff_pattern,
};

static void empty_fifo(struct sh_flctl *flctl)
{
86 87
	writel(flctl->flintdmacr_base | AC1CLR | AC0CLR, FLINTDMACR(flctl));
	writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
88 89 90 91 92 93 94
}

static void start_translation(struct sh_flctl *flctl)
{
	writeb(TRSTRT, FLTRCR(flctl));
}

M
Magnus Damm 已提交
95 96
static void timeout_error(struct sh_flctl *flctl, const char *str)
{
L
Lucas De Marchi 已提交
97
	dev_err(&flctl->pdev->dev, "Timeout occurred in %s\n", str);
M
Magnus Damm 已提交
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111
static void wait_completion(struct sh_flctl *flctl)
{
	uint32_t timeout = LOOP_TIMEOUT_MAX;

	while (timeout--) {
		if (readb(FLTRCR(flctl)) & TREND) {
			writeb(0x0, FLTRCR(flctl));
			return;
		}
		udelay(1);
	}

M
Magnus Damm 已提交
112
	timeout_error(flctl, __func__);
113 114 115
	writeb(0x0, FLTRCR(flctl));
}

B
Bastian Hecht 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 160 161 162 163 164 165 166 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
static void flctl_dma_complete(void *param)
{
	struct sh_flctl *flctl = param;

	complete(&flctl->dma_complete);
}

static void flctl_release_dma(struct sh_flctl *flctl)
{
	if (flctl->chan_fifo0_rx) {
		dma_release_channel(flctl->chan_fifo0_rx);
		flctl->chan_fifo0_rx = NULL;
	}
	if (flctl->chan_fifo0_tx) {
		dma_release_channel(flctl->chan_fifo0_tx);
		flctl->chan_fifo0_tx = NULL;
	}
}

static void flctl_setup_dma(struct sh_flctl *flctl)
{
	dma_cap_mask_t mask;
	struct dma_slave_config cfg;
	struct platform_device *pdev = flctl->pdev;
	struct sh_flctl_platform_data *pdata = pdev->dev.platform_data;
	int ret;

	if (!pdata)
		return;

	if (pdata->slave_id_fifo0_tx <= 0 || pdata->slave_id_fifo0_rx <= 0)
		return;

	/* We can only either use DMA for both Tx and Rx or not use it at all */
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

	flctl->chan_fifo0_tx = dma_request_channel(mask, shdma_chan_filter,
					    (void *)pdata->slave_id_fifo0_tx);
	dev_dbg(&pdev->dev, "%s: TX: got channel %p\n", __func__,
		flctl->chan_fifo0_tx);

	if (!flctl->chan_fifo0_tx)
		return;

	memset(&cfg, 0, sizeof(cfg));
	cfg.slave_id = pdata->slave_id_fifo0_tx;
	cfg.direction = DMA_MEM_TO_DEV;
	cfg.dst_addr = (dma_addr_t)FLDTFIFO(flctl);
	cfg.src_addr = 0;
	ret = dmaengine_slave_config(flctl->chan_fifo0_tx, &cfg);
	if (ret < 0)
		goto err;

	flctl->chan_fifo0_rx = dma_request_channel(mask, shdma_chan_filter,
					    (void *)pdata->slave_id_fifo0_rx);
	dev_dbg(&pdev->dev, "%s: RX: got channel %p\n", __func__,
		flctl->chan_fifo0_rx);

	if (!flctl->chan_fifo0_rx)
		goto err;

	cfg.slave_id = pdata->slave_id_fifo0_rx;
	cfg.direction = DMA_DEV_TO_MEM;
	cfg.dst_addr = 0;
	cfg.src_addr = (dma_addr_t)FLDTFIFO(flctl);
	ret = dmaengine_slave_config(flctl->chan_fifo0_rx, &cfg);
	if (ret < 0)
		goto err;

	init_completion(&flctl->dma_complete);

	return;

err:
	flctl_release_dma(flctl);
}

194 195 196 197 198 199 200 201 202
static void set_addr(struct mtd_info *mtd, int column, int page_addr)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	uint32_t addr = 0;

	if (column == -1) {
		addr = page_addr;	/* ERASE1 */
	} else if (page_addr != -1) {
		/* SEQIN, READ0, etc.. */
203 204
		if (flctl->chip.options & NAND_BUSWIDTH_16)
			column >>= 1;
205 206 207 208 209 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
		if (flctl->page_size) {
			addr = column & 0x0FFF;
			addr |= (page_addr & 0xff) << 16;
			addr |= ((page_addr >> 8) & 0xff) << 24;
			/* big than 128MB */
			if (flctl->rw_ADRCNT == ADRCNT2_E) {
				uint32_t 	addr2;
				addr2 = (page_addr >> 16) & 0xff;
				writel(addr2, FLADR2(flctl));
			}
		} else {
			addr = column;
			addr |= (page_addr & 0xff) << 8;
			addr |= ((page_addr >> 8) & 0xff) << 16;
			addr |= ((page_addr >> 16) & 0xff) << 24;
		}
	}
	writel(addr, FLADR(flctl));
}

static void wait_rfifo_ready(struct sh_flctl *flctl)
{
	uint32_t timeout = LOOP_TIMEOUT_MAX;

	while (timeout--) {
		uint32_t val;
		/* check FIFO */
		val = readl(FLDTCNTR(flctl)) >> 16;
		if (val & 0xFF)
			return;
		udelay(1);
	}
M
Magnus Damm 已提交
237
	timeout_error(flctl, __func__);
238 239 240 241 242 243 244 245 246 247 248 249 250
}

static void wait_wfifo_ready(struct sh_flctl *flctl)
{
	uint32_t len, timeout = LOOP_TIMEOUT_MAX;

	while (timeout--) {
		/* check FIFO */
		len = (readl(FLDTCNTR(flctl)) >> 16) & 0xFF;
		if (len >= 4)
			return;
		udelay(1);
	}
M
Magnus Damm 已提交
251
	timeout_error(flctl, __func__);
252 253
}

254 255
static enum flctl_ecc_res_t wait_recfifo_ready
		(struct sh_flctl *flctl, int sector_number)
256 257 258 259
{
	uint32_t timeout = LOOP_TIMEOUT_MAX;
	void __iomem *ecc_reg[4];
	int i;
260
	int state = FL_SUCCESS;
261 262
	uint32_t data, size;

263 264 265 266 267 268 269
	/*
	 * First this loops checks in FLDTCNTR if we are ready to read out the
	 * oob data. This is the case if either all went fine without errors or
	 * if the bottom part of the loop corrected the errors or marked them as
	 * uncorrectable and the controller is given time to push the data into
	 * the FIFO.
	 */
270
	while (timeout--) {
271
		/* check if all is ok and we can read out the OOB */
272
		size = readl(FLDTCNTR(flctl)) >> 24;
273 274 275 276 277 278 279 280 281 282 283 284
		if ((size & 0xFF) == 4)
			return state;

		/* check if a correction code has been calculated */
		if (!(readl(FL4ECCCR(flctl)) & _4ECCEND)) {
			/*
			 * either we wait for the fifo to be filled or a
			 * correction pattern is being generated
			 */
			udelay(1);
			continue;
		}
285

286 287 288 289 290 291 292 293 294
		/* check for an uncorrectable error */
		if (readl(FL4ECCCR(flctl)) & _4ECCFA) {
			/* check if we face a non-empty page */
			for (i = 0; i < 512; i++) {
				if (flctl->done_buff[i] != 0xff) {
					state = FL_ERROR; /* can't correct */
					break;
				}
			}
295

296 297 298 299 300 301
			if (state == FL_SUCCESS)
				dev_dbg(&flctl->pdev->dev,
				"reading empty sector %d, ecc error ignored\n",
				sector_number);

			writel(0, FL4ECCCR(flctl));
302
			continue;
303
		}
304 305 306 307 308 309 310 311

		/* start error correction */
		ecc_reg[0] = FL4ECCRESULT0(flctl);
		ecc_reg[1] = FL4ECCRESULT1(flctl);
		ecc_reg[2] = FL4ECCRESULT2(flctl);
		ecc_reg[3] = FL4ECCRESULT3(flctl);

		for (i = 0; i < 3; i++) {
312
			uint8_t org;
B
Bastian Hecht 已提交
313
			unsigned int index;
314

315 316
			data = readl(ecc_reg[i]);

317 318 319 320 321 322 323 324 325 326
			if (flctl->page_size)
				index = (512 * sector_number) +
					(data >> 16);
			else
				index = data >> 16;

			org = flctl->done_buff[index];
			flctl->done_buff[index] = org ^ (data & 0xFF);
		}
		state = FL_REPAIRABLE;
327 328 329
		writel(0, FL4ECCCR(flctl));
	}

M
Magnus Damm 已提交
330
	timeout_error(flctl, __func__);
331
	return FL_TIMEOUT;	/* timeout */
332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

static void wait_wecfifo_ready(struct sh_flctl *flctl)
{
	uint32_t timeout = LOOP_TIMEOUT_MAX;
	uint32_t len;

	while (timeout--) {
		/* check FLECFIFO */
		len = (readl(FLDTCNTR(flctl)) >> 24) & 0xFF;
		if (len >= 4)
			return;
		udelay(1);
	}
M
Magnus Damm 已提交
346
	timeout_error(flctl, __func__);
347 348
}

B
Bastian Hecht 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static int flctl_dma_fifo0_transfer(struct sh_flctl *flctl, unsigned long *buf,
					int len, enum dma_data_direction dir)
{
	struct dma_async_tx_descriptor *desc = NULL;
	struct dma_chan *chan;
	enum dma_transfer_direction tr_dir;
	dma_addr_t dma_addr;
	dma_cookie_t cookie = -EINVAL;
	uint32_t reg;
	int ret;

	if (dir == DMA_FROM_DEVICE) {
		chan = flctl->chan_fifo0_rx;
		tr_dir = DMA_DEV_TO_MEM;
	} else {
		chan = flctl->chan_fifo0_tx;
		tr_dir = DMA_MEM_TO_DEV;
	}

	dma_addr = dma_map_single(chan->device->dev, buf, len, dir);

	if (dma_addr)
		desc = dmaengine_prep_slave_single(chan, dma_addr, len,
			tr_dir, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);

	if (desc) {
		reg = readl(FLINTDMACR(flctl));
		reg |= DREQ0EN;
		writel(reg, FLINTDMACR(flctl));

		desc->callback = flctl_dma_complete;
		desc->callback_param = flctl;
		cookie = dmaengine_submit(desc);

		dma_async_issue_pending(chan);
	} else {
		/* DMA failed, fall back to PIO */
		flctl_release_dma(flctl);
		dev_warn(&flctl->pdev->dev,
			 "DMA failed, falling back to PIO\n");
		ret = -EIO;
		goto out;
	}

	ret =
	wait_for_completion_timeout(&flctl->dma_complete,
				msecs_to_jiffies(3000));

	if (ret <= 0) {
		chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
		dev_err(&flctl->pdev->dev, "wait_for_completion_timeout\n");
	}

out:
	reg = readl(FLINTDMACR(flctl));
	reg &= ~DREQ0EN;
	writel(reg, FLINTDMACR(flctl));

	dma_unmap_single(chan->device->dev, dma_addr, len, dir);

	/* ret > 0 is success */
	return ret;
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
static void read_datareg(struct sh_flctl *flctl, int offset)
{
	unsigned long data;
	unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];

	wait_completion(flctl);

	data = readl(FLDATAR(flctl));
	*buf = le32_to_cpu(data);
}

static void read_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
{
	int i, len_4align;
	unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];

	len_4align = (rlen + 3) / 4;

B
Bastian Hecht 已提交
431 432 433 434 435 436
	/* initiate DMA transfer */
	if (flctl->chan_fifo0_rx && rlen >= 32 &&
		flctl_dma_fifo0_transfer(flctl, buf, rlen, DMA_DEV_TO_MEM) > 0)
			goto convert;	/* DMA success */

	/* do polling transfer */
437 438
	for (i = 0; i < len_4align; i++) {
		wait_rfifo_ready(flctl);
439
		buf[i] = readl(FLDTFIFO(flctl));
440
	}
B
Bastian Hecht 已提交
441 442 443 444

convert:
	for (i = 0; i < len_4align; i++)
		buf[i] = be32_to_cpu(buf[i]);
445 446
}

447 448
static enum flctl_ecc_res_t read_ecfiforeg
		(struct sh_flctl *flctl, uint8_t *buff, int sector)
449 450
{
	int i;
451
	enum flctl_ecc_res_t res;
452 453
	unsigned long *ecc_buf = (unsigned long *)buff;

454 455 456 457 458 459 460
	res = wait_recfifo_ready(flctl , sector);

	if (res != FL_ERROR) {
		for (i = 0; i < 4; i++) {
			ecc_buf[i] = readl(FLECFIFO(flctl));
			ecc_buf[i] = be32_to_cpu(ecc_buf[i]);
		}
461 462
	}

463
	return res;
464 465
}

B
Bastian Hecht 已提交
466 467
static void write_fiforeg(struct sh_flctl *flctl, int rlen,
						unsigned int offset)
468 469
{
	int i, len_4align;
B
Bastian Hecht 已提交
470
	unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
471 472 473 474

	len_4align = (rlen + 3) / 4;
	for (i = 0; i < len_4align; i++) {
		wait_wfifo_ready(flctl);
B
Bastian Hecht 已提交
475
		writel(cpu_to_be32(buf[i]), FLDTFIFO(flctl));
476 477 478
	}
}

B
Bastian Hecht 已提交
479 480
static void write_ec_fiforeg(struct sh_flctl *flctl, int rlen,
						unsigned int offset)
481 482
{
	int i, len_4align;
B
Bastian Hecht 已提交
483
	unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
484 485

	len_4align = (rlen + 3) / 4;
B
Bastian Hecht 已提交
486 487 488 489 490 491 492 493 494 495

	for (i = 0; i < len_4align; i++)
		buf[i] = cpu_to_be32(buf[i]);

	/* initiate DMA transfer */
	if (flctl->chan_fifo0_tx && rlen >= 32 &&
		flctl_dma_fifo0_transfer(flctl, buf, rlen, DMA_MEM_TO_DEV) > 0)
			return;	/* DMA success */

	/* do polling transfer */
496 497
	for (i = 0; i < len_4align; i++) {
		wait_wecfifo_ready(flctl);
B
Bastian Hecht 已提交
498
		writel(buf[i], FLECFIFO(flctl));
499 500 501
	}
}

502 503 504
static void set_cmd_regs(struct mtd_info *mtd, uint32_t cmd, uint32_t flcmcdr_val)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
505
	uint32_t flcmncr_val = flctl->flcmncr_base & ~SEL_16BIT;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	uint32_t flcmdcr_val, addr_len_bytes = 0;

	/* Set SNAND bit if page size is 2048byte */
	if (flctl->page_size)
		flcmncr_val |= SNAND_E;
	else
		flcmncr_val &= ~SNAND_E;

	/* default FLCMDCR val */
	flcmdcr_val = DOCMD1_E | DOADR_E;

	/* Set for FLCMDCR */
	switch (cmd) {
	case NAND_CMD_ERASE1:
		addr_len_bytes = flctl->erase_ADRCNT;
		flcmdcr_val |= DOCMD2_E;
		break;
	case NAND_CMD_READ0:
	case NAND_CMD_READOOB:
525
	case NAND_CMD_RNDOUT:
526 527
		addr_len_bytes = flctl->rw_ADRCNT;
		flcmdcr_val |= CDSRC_E;
528 529
		if (flctl->chip.options & NAND_BUSWIDTH_16)
			flcmncr_val |= SEL_16BIT;
530 531 532 533 534 535 536
		break;
	case NAND_CMD_SEQIN:
		/* This case is that cmd is READ0 or READ1 or READ00 */
		flcmdcr_val &= ~DOADR_E;	/* ONLY execute 1st cmd */
		break;
	case NAND_CMD_PAGEPROG:
		addr_len_bytes = flctl->rw_ADRCNT;
537
		flcmdcr_val |= DOCMD2_E | CDSRC_E | SELRW;
538 539
		if (flctl->chip.options & NAND_BUSWIDTH_16)
			flcmncr_val |= SEL_16BIT;
540 541 542
		break;
	case NAND_CMD_READID:
		flcmncr_val &= ~SNAND_E;
543
		flcmdcr_val |= CDSRC_E;
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
		addr_len_bytes = ADRCNT_1;
		break;
	case NAND_CMD_STATUS:
	case NAND_CMD_RESET:
		flcmncr_val &= ~SNAND_E;
		flcmdcr_val &= ~(DOADR_E | DOSR_E);
		break;
	default:
		break;
	}

	/* Set address bytes parameter */
	flcmdcr_val |= addr_len_bytes;

	/* Now actually write */
	writel(flcmncr_val, FLCMNCR(flctl));
	writel(flcmdcr_val, FLCMDCR(flctl));
	writel(flcmcdr_val, FLCMCDR(flctl));
}

static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
565
				uint8_t *buf, int oob_required, int page)
566
{
567
	chip->read_buf(mtd, buf, mtd->writesize);
568 569
	if (oob_required)
		chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
570 571 572
	return 0;
}

573
static int flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
574
				   const uint8_t *buf, int oob_required)
575
{
576
	chip->write_buf(mtd, buf, mtd->writesize);
577
	chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
578
	return 0;
579 580 581 582 583 584
}

static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	int sector, page_sectors;
585
	enum flctl_ecc_res_t ecc_result;
586

587 588 589 590
	page_sectors = flctl->page_size ? 4 : 1;

	set_cmd_regs(mtd, NAND_CMD_READ0,
		(NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
591 592 593

	writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
		 FLCMNCR(flctl));
594 595
	writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
	writel(page_addr << 2, FLADR(flctl));
596

597 598
	empty_fifo(flctl);
	start_translation(flctl);
599 600 601 602

	for (sector = 0; sector < page_sectors; sector++) {
		read_fiforeg(flctl, 512, 512 * sector);

603
		ecc_result = read_ecfiforeg(flctl,
604 605
			&flctl->done_buff[mtd->writesize + 16 * sector],
			sector);
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		switch (ecc_result) {
		case FL_REPAIRABLE:
			dev_info(&flctl->pdev->dev,
				"applied ecc on page 0x%x", page_addr);
			flctl->mtd.ecc_stats.corrected++;
			break;
		case FL_ERROR:
			dev_warn(&flctl->pdev->dev,
				"page 0x%x contains corrupted data\n",
				page_addr);
			flctl->mtd.ecc_stats.failed++;
			break;
		default:
			;
		}
622
	}
623 624 625

	wait_completion(flctl);

626 627 628 629 630 631 632
	writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
			FLCMNCR(flctl));
}

static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
633 634
	int page_sectors = flctl->page_size ? 4 : 1;
	int i;
635 636 637 638 639 640

	set_cmd_regs(mtd, NAND_CMD_READ0,
		(NAND_CMD_READSTART << 8) | NAND_CMD_READ0);

	empty_fifo(flctl);

641 642
	for (i = 0; i < page_sectors; i++) {
		set_addr(mtd, (512 + 16) * i + 512 , page_addr);
643 644 645
		writel(16, FLDTCNTR(flctl));

		start_translation(flctl);
646
		read_fiforeg(flctl, 16, 16 * i);
647 648 649 650 651 652 653
		wait_completion(flctl);
	}
}

static void execmd_write_page_sector(struct mtd_info *mtd)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
654
	int page_addr = flctl->seqin_page_addr;
655 656
	int sector, page_sectors;

657
	page_sectors = flctl->page_size ? 4 : 1;
658 659 660 661

	set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
			(NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);

662 663 664 665 666
	empty_fifo(flctl);
	writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
	writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
	writel(page_addr << 2, FLADR(flctl));
	start_translation(flctl);
667

668
	for (sector = 0; sector < page_sectors; sector++) {
669
		write_fiforeg(flctl, 512, 512 * sector);
670
		write_ec_fiforeg(flctl, 16, mtd->writesize + 16 * sector);
671 672
	}

673
	wait_completion(flctl);
674 675 676 677 678 679 680 681 682
	writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
}

static void execmd_write_oob(struct mtd_info *mtd)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	int page_addr = flctl->seqin_page_addr;
	int sector, page_sectors;

683
	page_sectors = flctl->page_size ? 4 : 1;
684 685 686 687

	set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
			(NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);

688
	for (sector = 0; sector < page_sectors; sector++) {
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		empty_fifo(flctl);
		set_addr(mtd, sector * 528 + 512, page_addr);
		writel(16, FLDTCNTR(flctl));	/* set read size */

		start_translation(flctl);
		write_fiforeg(flctl, 16, 16 * sector);
		wait_completion(flctl);
	}
}

static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
			int column, int page_addr)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	uint32_t read_cmd = 0;

705 706
	pm_runtime_get_sync(&flctl->pdev->dev);

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	flctl->read_bytes = 0;
	if (command != NAND_CMD_PAGEPROG)
		flctl->index = 0;

	switch (command) {
	case NAND_CMD_READ1:
	case NAND_CMD_READ0:
		if (flctl->hwecc) {
			/* read page with hwecc */
			execmd_read_page_sector(mtd, page_addr);
			break;
		}
		if (flctl->page_size)
			set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
				| command);
		else
			set_cmd_regs(mtd, command, command);

		set_addr(mtd, 0, page_addr);

		flctl->read_bytes = mtd->writesize + mtd->oobsize;
728 729
		if (flctl->chip.options & NAND_BUSWIDTH_16)
			column >>= 1;
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
		flctl->index += column;
		goto read_normal_exit;

	case NAND_CMD_READOOB:
		if (flctl->hwecc) {
			/* read page with hwecc */
			execmd_read_oob(mtd, page_addr);
			break;
		}

		if (flctl->page_size) {
			set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
				| NAND_CMD_READ0);
			set_addr(mtd, mtd->writesize, page_addr);
		} else {
			set_cmd_regs(mtd, command, command);
			set_addr(mtd, 0, page_addr);
		}
		flctl->read_bytes = mtd->oobsize;
		goto read_normal_exit;

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	case NAND_CMD_RNDOUT:
		if (flctl->hwecc)
			break;

		if (flctl->page_size)
			set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
				| command);
		else
			set_cmd_regs(mtd, command, command);

		set_addr(mtd, column, 0);

		flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
		goto read_normal_exit;

766 767 768
	case NAND_CMD_READID:
		set_cmd_regs(mtd, command, command);

769 770 771 772 773 774
		/* READID is always performed using an 8-bit bus */
		if (flctl->chip.options & NAND_BUSWIDTH_16)
			column <<= 1;
		set_addr(mtd, column, 0);

		flctl->read_bytes = 8;
775
		writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
776
		empty_fifo(flctl);
777
		start_translation(flctl);
778 779
		read_fiforeg(flctl, flctl->read_bytes, 0);
		wait_completion(flctl);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 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 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
		break;

	case NAND_CMD_ERASE1:
		flctl->erase1_page_addr = page_addr;
		break;

	case NAND_CMD_ERASE2:
		set_cmd_regs(mtd, NAND_CMD_ERASE1,
			(command << 8) | NAND_CMD_ERASE1);
		set_addr(mtd, -1, flctl->erase1_page_addr);
		start_translation(flctl);
		wait_completion(flctl);
		break;

	case NAND_CMD_SEQIN:
		if (!flctl->page_size) {
			/* output read command */
			if (column >= mtd->writesize) {
				column -= mtd->writesize;
				read_cmd = NAND_CMD_READOOB;
			} else if (column < 256) {
				read_cmd = NAND_CMD_READ0;
			} else {
				column -= 256;
				read_cmd = NAND_CMD_READ1;
			}
		}
		flctl->seqin_column = column;
		flctl->seqin_page_addr = page_addr;
		flctl->seqin_read_cmd = read_cmd;
		break;

	case NAND_CMD_PAGEPROG:
		empty_fifo(flctl);
		if (!flctl->page_size) {
			set_cmd_regs(mtd, NAND_CMD_SEQIN,
					flctl->seqin_read_cmd);
			set_addr(mtd, -1, -1);
			writel(0, FLDTCNTR(flctl));	/* set 0 size */
			start_translation(flctl);
			wait_completion(flctl);
		}
		if (flctl->hwecc) {
			/* write page with hwecc */
			if (flctl->seqin_column == mtd->writesize)
				execmd_write_oob(mtd);
			else if (!flctl->seqin_column)
				execmd_write_page_sector(mtd);
			else
				printk(KERN_ERR "Invalid address !?\n");
			break;
		}
		set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
		set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
		writel(flctl->index, FLDTCNTR(flctl));	/* set write size */
		start_translation(flctl);
		write_fiforeg(flctl, flctl->index, 0);
		wait_completion(flctl);
		break;

	case NAND_CMD_STATUS:
		set_cmd_regs(mtd, command, command);
		set_addr(mtd, -1, -1);

		flctl->read_bytes = 1;
		writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
		start_translation(flctl);
		read_datareg(flctl, 0); /* read and end */
		break;

	case NAND_CMD_RESET:
		set_cmd_regs(mtd, command, command);
		set_addr(mtd, -1, -1);

		writel(0, FLDTCNTR(flctl));	/* set 0 size */
		start_translation(flctl);
		wait_completion(flctl);
		break;

	default:
		break;
	}
862
	goto runtime_exit;
863 864 865

read_normal_exit:
	writel(flctl->read_bytes, FLDTCNTR(flctl));	/* set read size */
866
	empty_fifo(flctl);
867 868 869
	start_translation(flctl);
	read_fiforeg(flctl, flctl->read_bytes, 0);
	wait_completion(flctl);
870 871
runtime_exit:
	pm_runtime_put_sync(&flctl->pdev->dev);
872 873 874 875 876 877
	return;
}

static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
878
	int ret;
879 880 881

	switch (chipnr) {
	case -1:
882
		flctl->flcmncr_base &= ~CE0_ENABLE;
883 884

		pm_runtime_get_sync(&flctl->pdev->dev);
885
		writel(flctl->flcmncr_base, FLCMNCR(flctl));
886 887 888 889 890 891 892

		if (flctl->qos_request) {
			dev_pm_qos_remove_request(&flctl->pm_qos);
			flctl->qos_request = 0;
		}

		pm_runtime_put_sync(&flctl->pdev->dev);
893 894
		break;
	case 0:
895
		flctl->flcmncr_base |= CE0_ENABLE;
896 897 898

		if (!flctl->qos_request) {
			ret = dev_pm_qos_add_request(&flctl->pdev->dev,
899 900 901
							&flctl->pm_qos,
							DEV_PM_QOS_LATENCY,
							100);
902 903 904 905 906 907 908 909
			if (ret < 0)
				dev_err(&flctl->pdev->dev,
					"PM QoS request failed: %d\n", ret);
			flctl->qos_request = 1;
		}

		if (flctl->holden) {
			pm_runtime_get_sync(&flctl->pdev->dev);
910
			writel(HOLDEN, FLHOLDCR(flctl));
911 912
			pm_runtime_put_sync(&flctl->pdev->dev);
		}
913 914 915 916 917 918 919 920 921 922
		break;
	default:
		BUG();
	}
}

static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);

B
Bastian Hecht 已提交
923
	memcpy(&flctl->done_buff[flctl->index], buf, len);
924 925 926 927 928 929 930 931
	flctl->index += len;
}

static uint8_t flctl_read_byte(struct mtd_info *mtd)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	uint8_t data;

B
Bastian Hecht 已提交
932
	data = flctl->done_buff[flctl->index];
933 934 935 936
	flctl->index++;
	return data;
}

937 938
static uint16_t flctl_read_word(struct mtd_info *mtd)
{
B
Bastian Hecht 已提交
939 940
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	uint16_t *buf = (uint16_t *)&flctl->done_buff[flctl->index];
941

B
Bastian Hecht 已提交
942 943
	flctl->index += 2;
	return *buf;
944 945
}

946 947
static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
{
948
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
949

B
Bastian Hecht 已提交
950
	memcpy(buf, &flctl->done_buff[flctl->index], len);
951
	flctl->index += len;
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 998 999
}

static int flctl_chip_init_tail(struct mtd_info *mtd)
{
	struct sh_flctl *flctl = mtd_to_flctl(mtd);
	struct nand_chip *chip = &flctl->chip;

	if (mtd->writesize == 512) {
		flctl->page_size = 0;
		if (chip->chipsize > (32 << 20)) {
			/* big than 32MB */
			flctl->rw_ADRCNT = ADRCNT_4;
			flctl->erase_ADRCNT = ADRCNT_3;
		} else if (chip->chipsize > (2 << 16)) {
			/* big than 128KB */
			flctl->rw_ADRCNT = ADRCNT_3;
			flctl->erase_ADRCNT = ADRCNT_2;
		} else {
			flctl->rw_ADRCNT = ADRCNT_2;
			flctl->erase_ADRCNT = ADRCNT_1;
		}
	} else {
		flctl->page_size = 1;
		if (chip->chipsize > (128 << 20)) {
			/* big than 128MB */
			flctl->rw_ADRCNT = ADRCNT2_E;
			flctl->erase_ADRCNT = ADRCNT_3;
		} else if (chip->chipsize > (8 << 16)) {
			/* big than 512KB */
			flctl->rw_ADRCNT = ADRCNT_4;
			flctl->erase_ADRCNT = ADRCNT_2;
		} else {
			flctl->rw_ADRCNT = ADRCNT_3;
			flctl->erase_ADRCNT = ADRCNT_1;
		}
	}

	if (flctl->hwecc) {
		if (mtd->writesize == 512) {
			chip->ecc.layout = &flctl_4secc_oob_16;
			chip->badblock_pattern = &flctl_4secc_smallpage;
		} else {
			chip->ecc.layout = &flctl_4secc_oob_64;
			chip->badblock_pattern = &flctl_4secc_largepage;
		}

		chip->ecc.size = 512;
		chip->ecc.bytes = 10;
M
Mike Dunn 已提交
1000
		chip->ecc.strength = 4;
1001 1002 1003 1004 1005
		chip->ecc.read_page = flctl_read_page_hwecc;
		chip->ecc.write_page = flctl_write_page_hwecc;
		chip->ecc.mode = NAND_ECC_HW;

		/* 4 symbols ECC enabled */
1006
		flctl->flcmncr_base |= _4ECCEN;
1007 1008 1009 1010 1011 1012 1013
	} else {
		chip->ecc.mode = NAND_ECC_SOFT;
	}

	return 0;
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
{
	struct sh_flctl *flctl = dev_id;

	dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
	writel(flctl->flintdmacr_base, FLINTDMACR(flctl));

	return IRQ_HANDLED;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
#ifdef CONFIG_OF
struct flctl_soc_config {
	unsigned long flcmncr_val;
	unsigned has_hwecc:1;
	unsigned use_holden:1;
};

static struct flctl_soc_config flctl_sh7372_config = {
	.flcmncr_val = CLK_16B_12L_4H | TYPESEL_SET | SHBUSSEL,
	.has_hwecc = 1,
	.use_holden = 1,
};

static const struct of_device_id of_flctl_match[] = {
	{ .compatible = "renesas,shmobile-flctl-sh7372",
				.data = &flctl_sh7372_config },
	{},
};
MODULE_DEVICE_TABLE(of, of_flctl_match);

static struct sh_flctl_platform_data *flctl_parse_dt(struct device *dev)
{
	const struct of_device_id *match;
	struct flctl_soc_config *config;
	struct sh_flctl_platform_data *pdata;
	struct device_node *dn = dev->of_node;
	int ret;

	match = of_match_device(of_flctl_match, dev);
	if (match)
		config = (struct flctl_soc_config *)match->data;
	else {
		dev_err(dev, "%s: no OF configuration attached\n", __func__);
		return NULL;
	}

	pdata = devm_kzalloc(dev, sizeof(struct sh_flctl_platform_data),
								GFP_KERNEL);
	if (!pdata) {
		dev_err(dev, "%s: failed to allocate config data\n", __func__);
		return NULL;
	}

	/* set SoC specific options */
	pdata->flcmncr_val = config->flcmncr_val;
	pdata->has_hwecc = config->has_hwecc;
	pdata->use_holden = config->use_holden;

	/* parse user defined options */
	ret = of_get_nand_bus_width(dn);
	if (ret == 16)
		pdata->flcmncr_val |= SEL_16BIT;
	else if (ret != 8) {
		dev_err(dev, "%s: invalid bus width\n", __func__);
		return NULL;
	}

	return pdata;
}
#else /* CONFIG_OF */
static struct sh_flctl_platform_data *flctl_parse_dt(struct device *dev)
{
	return NULL;
}
#endif /* CONFIG_OF */

B
Bill Pemberton 已提交
1090
static int flctl_probe(struct platform_device *pdev)
1091 1092 1093 1094 1095 1096
{
	struct resource *res;
	struct sh_flctl *flctl;
	struct mtd_info *flctl_mtd;
	struct nand_chip *nand;
	struct sh_flctl_platform_data *pdata;
M
Magnus Damm 已提交
1097
	int ret = -ENXIO;
1098
	int irq;
1099
	struct mtd_part_parser_data ppdata = {};
1100 1101 1102

	flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
	if (!flctl) {
M
Magnus Damm 已提交
1103
		dev_err(&pdev->dev, "failed to allocate driver data\n");
1104 1105 1106 1107 1108
		return -ENOMEM;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
M
Magnus Damm 已提交
1109
		dev_err(&pdev->dev, "failed to get I/O memory\n");
1110
		goto err_iomap;
1111 1112
	}

1113
	flctl->reg = ioremap(res->start, resource_size(res));
1114
	if (flctl->reg == NULL) {
M
Magnus Damm 已提交
1115
		dev_err(&pdev->dev, "failed to remap I/O memory\n");
1116
		goto err_iomap;
1117 1118
	}

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		dev_err(&pdev->dev, "failed to get flste irq data\n");
		goto err_flste;
	}

	ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
	if (ret) {
		dev_err(&pdev->dev, "request interrupt failed.\n");
		goto err_flste;
	}

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	if (pdev->dev.of_node)
		pdata = flctl_parse_dt(&pdev->dev);
	else
		pdata = pdev->dev.platform_data;

	if (!pdata) {
		dev_err(&pdev->dev, "no setup data defined\n");
		ret = -EINVAL;
		goto err_pdata;
	}

1142 1143 1144 1145
	platform_set_drvdata(pdev, flctl);
	flctl_mtd = &flctl->mtd;
	nand = &flctl->chip;
	flctl_mtd->priv = nand;
M
Magnus Damm 已提交
1146
	flctl->pdev = pdev;
1147
	flctl->hwecc = pdata->has_hwecc;
1148
	flctl->holden = pdata->use_holden;
1149 1150
	flctl->flcmncr_base = pdata->flcmncr_val;
	flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

	/* Set address of hardware control function */
	/* 20 us command delay time */
	nand->chip_delay = 20;

	nand->read_byte = flctl_read_byte;
	nand->write_buf = flctl_write_buf;
	nand->read_buf = flctl_read_buf;
	nand->select_chip = flctl_select_chip;
	nand->cmdfunc = flctl_cmdfunc;

1162 1163 1164 1165 1166
	if (pdata->flcmncr_val & SEL_16BIT) {
		nand->options |= NAND_BUSWIDTH_16;
		nand->read_word = flctl_read_word;
	}

1167 1168 1169
	pm_runtime_enable(&pdev->dev);
	pm_runtime_resume(&pdev->dev);

B
Bastian Hecht 已提交
1170 1171
	flctl_setup_dma(flctl);

1172
	ret = nand_scan_ident(flctl_mtd, 1, NULL);
1173
	if (ret)
1174
		goto err_chip;
1175 1176 1177

	ret = flctl_chip_init_tail(flctl_mtd);
	if (ret)
1178
		goto err_chip;
1179 1180 1181

	ret = nand_scan_tail(flctl_mtd);
	if (ret)
1182
		goto err_chip;
1183

1184 1185 1186
	ppdata.of_node = pdev->dev.of_node;
	ret = mtd_device_parse_register(flctl_mtd, NULL, &ppdata, pdata->parts,
			pdata->nr_parts);
1187 1188 1189

	return 0;

1190
err_chip:
B
Bastian Hecht 已提交
1191
	flctl_release_dma(flctl);
1192
	pm_runtime_disable(&pdev->dev);
1193
err_pdata:
1194 1195
	free_irq(irq, flctl);
err_flste:
1196
	iounmap(flctl->reg);
1197
err_iomap:
1198 1199 1200 1201
	kfree(flctl);
	return ret;
}

B
Bill Pemberton 已提交
1202
static int flctl_remove(struct platform_device *pdev)
1203 1204 1205
{
	struct sh_flctl *flctl = platform_get_drvdata(pdev);

B
Bastian Hecht 已提交
1206
	flctl_release_dma(flctl);
1207
	nand_release(&flctl->mtd);
1208
	pm_runtime_disable(&pdev->dev);
1209
	free_irq(platform_get_irq(pdev, 0), flctl);
1210
	iounmap(flctl->reg);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	kfree(flctl);

	return 0;
}

static struct platform_driver flctl_driver = {
	.remove		= flctl_remove,
	.driver = {
		.name	= "sh_flctl",
		.owner	= THIS_MODULE,
1221
		.of_match_table = of_match_ptr(of_flctl_match),
1222 1223 1224
	},
};

1225
module_platform_driver_probe(flctl_driver, flctl_probe);
1226 1227 1228 1229 1230

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Yoshihiro Shimoda");
MODULE_DESCRIPTION("SuperH FLCTL driver");
MODULE_ALIAS("platform:sh_flctl");