sharpsl.c 6.0 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
struct sharpsl_nand {
23
	struct nand_controller	controller;
24
	struct nand_chip	chip;
25 26

	void __iomem		*io;
27 28
};

29 30 31 32
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 已提交
33 34

/* register offset */
35 36 37 38 39 40 41
#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 已提交
42 43 44 45 46 47 48 49 50

/* 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)

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

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

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

		bits ^= 0x11;

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

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

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

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

90 91
static int sharpsl_nand_calculate_ecc(struct nand_chip *chip,
				      const u_char * dat, u_char * ecc_code)
L
Linus Torvalds 已提交
92
{
93
	struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
94 95 96 97
	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 已提交
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static int sharpsl_attach_chip(struct nand_chip *chip)
{
	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
		return 0;

	chip->ecc.size = 256;
	chip->ecc.bytes = 3;
	chip->ecc.strength = 1;
	chip->ecc.hwctl = sharpsl_nand_enable_hwecc;
	chip->ecc.calculate = sharpsl_nand_calculate_ecc;
	chip->ecc.correct = nand_correct_data;

	return 0;
}

static const struct nand_controller_ops sharpsl_ops = {
	.attach_chip = sharpsl_attach_chip,
};

L
Linus Torvalds 已提交
119 120 121
/*
 * Main initialization routine
 */
B
Bill Pemberton 已提交
122
static int sharpsl_nand_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
123 124
{
	struct nand_chip *this;
125
	struct mtd_info *mtd;
126
	struct resource *r;
L
Linus Torvalds 已提交
127
	int err = 0;
128
	struct sharpsl_nand *sharpsl;
J
Jingoo Han 已提交
129
	struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
130 131 132 133 134

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

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

141 142 143 144 145 146 147
	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 已提交
148
	/* map physical address */
149 150
	sharpsl->io = ioremap(r->start, resource_size(r));
	if (!sharpsl->io) {
151
		dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
152 153
		err = -EIO;
		goto err_ioremap;
L
Linus Torvalds 已提交
154
	}
155

L
Linus Torvalds 已提交
156
	/* Get pointer to private data */
157
	this = (struct nand_chip *)(&sharpsl->chip);
L
Linus Torvalds 已提交
158

159 160 161 162
	nand_controller_init(&sharpsl->controller);
	sharpsl->controller.ops = &sharpsl_ops;
	this->controller = &sharpsl->controller;

L
Linus Torvalds 已提交
163
	/* Link the private data with the MTD structure */
164 165
	mtd = nand_to_mtd(this);
	mtd->dev.parent = &pdev->dev;
166
	mtd_set_ooblayout(mtd, data->ecc_layout);
167 168

	platform_set_drvdata(pdev, sharpsl);
L
Linus Torvalds 已提交
169 170 171 172

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

	/* Set address of NAND IO lines */
176 177
	this->legacy.IO_ADDR_R = sharpsl->io + FLASHIO;
	this->legacy.IO_ADDR_W = sharpsl->io + FLASHIO;
L
Linus Torvalds 已提交
178
	/* Set address of hardware control function */
179
	this->legacy.cmd_ctrl = sharpsl_nand_hwcontrol;
180
	this->legacy.dev_ready = sharpsl_nand_dev_ready;
L
Linus Torvalds 已提交
181
	/* 15 us command delay time */
182
	this->legacy.chip_delay = 15;
183
	this->badblock_pattern = data->badblock_pattern;
L
Linus Torvalds 已提交
184 185

	/* Scan to find existence of the device */
186
	err = nand_scan(this, 1);
187 188
	if (err)
		goto err_scan;
L
Linus Torvalds 已提交
189 190

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

193
	err = mtd_device_parse_register(mtd, data->part_parsers, NULL,
194
					data->partitions, data->nr_partitions);
195 196
	if (err)
		goto err_add;
L
Linus Torvalds 已提交
197 198 199

	/* Return happy */
	return 0;
200

201
err_add:
202
	nand_cleanup(this);
203

204 205
err_scan:
	iounmap(sharpsl->io);
206
err_ioremap:
207
err_get_res:
208
	kfree(sharpsl);
209 210
	return err;
}
L
Linus Torvalds 已提交
211 212 213 214

/*
 * Clean up routine
 */
B
Bill Pemberton 已提交
215
static int sharpsl_nand_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
216
{
217
	struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
218 219
	struct nand_chip *chip = &sharpsl->chip;
	int ret;
220

221 222 223 224 225 226
	/* Unregister device */
	ret = mtd_device_unregister(nand_to_mtd(chip));
	WARN_ON(ret);

	/* Release resources */
	nand_cleanup(chip);
227

228
	iounmap(sharpsl->io);
L
Linus Torvalds 已提交
229

230
	/* Free the driver's structure */
231
	kfree(sharpsl);
232 233 234 235 236 237 238 239 240

	return 0;
}

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

244
module_platform_driver(sharpsl_nand_driver);
L
Linus Torvalds 已提交
245 246 247 248

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