sharpsl.c 5.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2 3
/*
 *  Copyright (C) 2004 Richard Purdie
4
 *  Copyright (C) 2008 Dmitry Baryshkov
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13
 *
 *  Based on Sharp's NAND driver sharp_sl.c
 */

#include <linux/genhd.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/mtd/mtd.h>
14
#include <linux/mtd/rawnand.h>
L
Linus Torvalds 已提交
15 16
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/partitions.h>
17
#include <linux/mtd/sharpsl.h>
L
Linus Torvalds 已提交
18
#include <linux/interrupt.h>
19
#include <linux/platform_device.h>
20
#include <linux/io.h>
L
Linus Torvalds 已提交
21

22 23
struct sharpsl_nand {
	struct nand_chip	chip;
24 25

	void __iomem		*io;
26 27
};

28 29 30 31
static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
{
	return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
}
L
Linus Torvalds 已提交
32 33

/* register offset */
34 35 36 37 38 39 40
#define ECCLPLB		0x00	/* line parity 7 - 0 bit */
#define ECCLPUB		0x04	/* line parity 15 - 8 bit */
#define ECCCP		0x08	/* column parity 5 - 0 bit */
#define ECCCNTR		0x0C	/* ECC byte counter */
#define ECCCLRR		0x10	/* cleare ECC */
#define FLASHIO		0x14	/* Flash I/O */
#define FLASHCTL	0x18	/* Flash Control */
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49

/* Flash control bit */
#define FLRYBY		(1 << 5)
#define FLCE1		(1 << 4)
#define FLWP		(1 << 3)
#define FLALE		(1 << 2)
#define FLCLE		(1 << 1)
#define FLCE0		(1 << 0)

50
/*
L
Linus Torvalds 已提交
51
 *	hardware specific access to control-lines
52
 *	ctrl:
53
 *	NAND_CNE: bit 0 -> ! bit 0 & 4
54 55 56
 *	NAND_CLE: bit 1 -> bit 1
 *	NAND_ALE: bit 2 -> bit 2
 *
L
Linus Torvalds 已提交
57
 */
58
static void sharpsl_nand_hwcontrol(struct nand_chip *chip, int cmd,
59
				   unsigned int ctrl)
L
Linus Torvalds 已提交
60
{
61
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
62 63 64 65 66

	if (ctrl & NAND_CTRL_CHANGE) {
		unsigned char bits = ctrl & 0x07;

		bits |= (ctrl & 0x01) << 4;
67 68 69

		bits ^= 0x11;

70
		writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
L
Linus Torvalds 已提交
71
	}
72 73

	if (cmd != NAND_CMD_NONE)
74
		writeb(cmd, chip->legacy.IO_ADDR_W);
L
Linus Torvalds 已提交
75 76
}

77
static int sharpsl_nand_dev_ready(struct nand_chip *chip)
L
Linus Torvalds 已提交
78
{
79
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
80
	return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
L
Linus Torvalds 已提交
81 82
}

83
static void sharpsl_nand_enable_hwecc(struct nand_chip *chip, int mode)
L
Linus Torvalds 已提交
84
{
85
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
86
	writeb(0, sharpsl->io + ECCCLRR);
L
Linus Torvalds 已提交
87 88
}

89 90
static int sharpsl_nand_calculate_ecc(struct nand_chip *chip,
				      const u_char * dat, u_char * ecc_code)
L
Linus Torvalds 已提交
91
{
92
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
93 94 95 96
	ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
	ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
	ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
	return readb(sharpsl->io + ECCCNTR) != 0;
L
Linus Torvalds 已提交
97 98 99 100 101
}

/*
 * Main initialization routine
 */
B
Bill Pemberton 已提交
102
static int sharpsl_nand_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
103 104
{
	struct nand_chip *this;
105
	struct mtd_info *mtd;
106
	struct resource *r;
L
Linus Torvalds 已提交
107
	int err = 0;
108
	struct sharpsl_nand *sharpsl;
J
Jingoo Han 已提交
109
	struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
110 111 112 113 114

	if (!data) {
		dev_err(&pdev->dev, "no platform data!\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
115 116

	/* Allocate memory for MTD device structure and private data */
117
	sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
118
	if (!sharpsl)
L
Linus Torvalds 已提交
119
		return -ENOMEM;
120

121 122 123 124 125 126 127
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		dev_err(&pdev->dev, "no io memory resource defined!\n");
		err = -ENODEV;
		goto err_get_res;
	}

J
Joe Perches 已提交
128
	/* map physical address */
129 130
	sharpsl->io = ioremap(r->start, resource_size(r));
	if (!sharpsl->io) {
131
		dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
132 133
		err = -EIO;
		goto err_ioremap;
L
Linus Torvalds 已提交
134
	}
135

L
Linus Torvalds 已提交
136
	/* Get pointer to private data */
137
	this = (struct nand_chip *)(&sharpsl->chip);
L
Linus Torvalds 已提交
138 139

	/* Link the private data with the MTD structure */
140 141
	mtd = nand_to_mtd(this);
	mtd->dev.parent = &pdev->dev;
142
	mtd_set_ooblayout(mtd, data->ecc_layout);
143 144

	platform_set_drvdata(pdev, sharpsl);
L
Linus Torvalds 已提交
145 146 147 148

	/*
	 * PXA initialize
	 */
149
	writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
L
Linus Torvalds 已提交
150 151

	/* Set address of NAND IO lines */
152 153
	this->legacy.IO_ADDR_R = sharpsl->io + FLASHIO;
	this->legacy.IO_ADDR_W = sharpsl->io + FLASHIO;
L
Linus Torvalds 已提交
154
	/* Set address of hardware control function */
155
	this->legacy.cmd_ctrl = sharpsl_nand_hwcontrol;
156
	this->legacy.dev_ready = sharpsl_nand_dev_ready;
L
Linus Torvalds 已提交
157
	/* 15 us command delay time */
158
	this->legacy.chip_delay = 15;
L
Linus Torvalds 已提交
159
	/* set eccmode using hardware ECC */
T
Thomas Gleixner 已提交
160 161 162
	this->ecc.mode = NAND_ECC_HW;
	this->ecc.size = 256;
	this->ecc.bytes = 3;
M
Mike Dunn 已提交
163
	this->ecc.strength = 1;
164
	this->badblock_pattern = data->badblock_pattern;
T
Thomas Gleixner 已提交
165 166 167
	this->ecc.hwctl = sharpsl_nand_enable_hwecc;
	this->ecc.calculate = sharpsl_nand_calculate_ecc;
	this->ecc.correct = nand_correct_data;
L
Linus Torvalds 已提交
168 169

	/* Scan to find existence of the device */
170
	err = nand_scan(this, 1);
171 172
	if (err)
		goto err_scan;
L
Linus Torvalds 已提交
173 174

	/* Register the partitions */
175
	mtd->name = "sharpsl-nand";
L
Linus Torvalds 已提交
176

177
	err = mtd_device_parse_register(mtd, data->part_parsers, NULL,
178
					data->partitions, data->nr_partitions);
179 180
	if (err)
		goto err_add;
L
Linus Torvalds 已提交
181 182 183

	/* Return happy */
	return 0;
184

185
err_add:
186
	nand_release(this);
187

188 189
err_scan:
	iounmap(sharpsl->io);
190
err_ioremap:
191
err_get_res:
192
	kfree(sharpsl);
193 194
	return err;
}
L
Linus Torvalds 已提交
195 196 197 198

/*
 * Clean up routine
 */
B
Bill Pemberton 已提交
199
static int sharpsl_nand_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
200
{
201 202
	struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);

L
Linus Torvalds 已提交
203
	/* Release resources, unregister device */
204
	nand_release(&sharpsl->chip);
205

206
	iounmap(sharpsl->io);
L
Linus Torvalds 已提交
207 208

	/* Free the MTD device structure */
209
	kfree(sharpsl);
210 211 212 213 214 215 216 217 218

	return 0;
}

static struct platform_driver sharpsl_nand_driver = {
	.driver = {
		.name	= "sharpsl-nand",
	},
	.probe		= sharpsl_nand_probe,
B
Bill Pemberton 已提交
219
	.remove		= sharpsl_nand_remove,
220 221
};

222
module_platform_driver(sharpsl_nand_driver);
L
Linus Torvalds 已提交
223 224 225 226

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");