pasemi_nand.c 5.1 KB
Newer Older
E
Egor Martovetsky 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (C) 2006-2007 PA Semi, Inc
 *
 * Author: Egor Martovetsky <egor@pasemi.com>
 * Maintained by: Olof Johansson <olof@lixom.net>
 *
 * Driver for the PWRficient onchip NAND flash interface
 *
 * 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.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#undef DEBUG

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
28
#include <linux/mtd/rawnand.h>
E
Egor Martovetsky 已提交
29
#include <linux/mtd/nand_ecc.h>
30 31
#include <linux/of_address.h>
#include <linux/of_irq.h>
E
Egor Martovetsky 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/pci.h>

#include <asm/io.h>

#define LBICTRL_LPCCTL_NR		0x00004000
#define CLE_PIN_CTL			15
#define ALE_PIN_CTL			14

static unsigned int lpcctl;
static struct mtd_info *pasemi_nand_mtd;
static const char driver_name[] = "pasemi-nand";

46
static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
E
Egor Martovetsky 已提交
47 48
{
	while (len > 0x800) {
49
		memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
E
Egor Martovetsky 已提交
50 51 52
		buf += 0x800;
		len -= 0x800;
	}
53
	memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
E
Egor Martovetsky 已提交
54 55
}

56 57
static void pasemi_write_buf(struct nand_chip *chip, const u_char *buf,
			     int len)
E
Egor Martovetsky 已提交
58 59
{
	while (len > 0x800) {
60
		memcpy_toio(chip->legacy.IO_ADDR_R, buf, 0x800);
E
Egor Martovetsky 已提交
61 62 63
		buf += 0x800;
		len -= 0x800;
	}
64
	memcpy_toio(chip->legacy.IO_ADDR_R, buf, len);
E
Egor Martovetsky 已提交
65 66
}

67
static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
E
Egor Martovetsky 已提交
68 69 70 71 72 73
			     unsigned int ctrl)
{
	if (cmd == NAND_CMD_NONE)
		return;

	if (ctrl & NAND_CLE)
74
		out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
E
Egor Martovetsky 已提交
75
	else
76
		out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
E
Egor Martovetsky 已提交
77 78 79 80 81 82

	/* Push out posted writes */
	eieio();
	inl(lpcctl);
}

83
int pasemi_device_ready(struct nand_chip *chip)
E
Egor Martovetsky 已提交
84 85 86 87
{
	return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
}

B
Bill Pemberton 已提交
88
static int pasemi_nand_probe(struct platform_device *ofdev)
E
Egor Martovetsky 已提交
89
{
90
	struct device *dev = &ofdev->dev;
E
Egor Martovetsky 已提交
91
	struct pci_dev *pdev;
92
	struct device_node *np = dev->of_node;
E
Egor Martovetsky 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
	struct resource res;
	struct nand_chip *chip;
	int err = 0;

	err = of_address_to_resource(np, 0, &res);

	if (err)
		return -EINVAL;

	/* We only support one device at the moment */
	if (pasemi_nand_mtd)
		return -ENODEV;

106
	dev_dbg(dev, "pasemi_nand at %pR\n", &res);
E
Egor Martovetsky 已提交
107 108

	/* Allocate memory for MTD device structure and private data */
109 110
	chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
	if (!chip) {
E
Egor Martovetsky 已提交
111 112 113 114
		err = -ENOMEM;
		goto out;
	}

115
	pasemi_nand_mtd = nand_to_mtd(chip);
E
Egor Martovetsky 已提交
116 117

	/* Link the private data with the MTD structure */
118
	pasemi_nand_mtd->dev.parent = dev;
E
Egor Martovetsky 已提交
119

120 121
	chip->legacy.IO_ADDR_R = of_iomap(np, 0);
	chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
E
Egor Martovetsky 已提交
122

123
	if (!chip->legacy.IO_ADDR_R) {
E
Egor Martovetsky 已提交
124 125 126 127 128 129 130 131 132 133 134
		err = -EIO;
		goto out_mtd;
	}

	pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
	if (!pdev) {
		err = -ENODEV;
		goto out_ior;
	}

	lpcctl = pci_resource_start(pdev, 0);
135
	pci_dev_put(pdev);
E
Egor Martovetsky 已提交
136 137 138 139 140 141 142 143 144 145 146 147

	if (!request_region(lpcctl, 4, driver_name)) {
		err = -EBUSY;
		goto out_ior;
	}

	chip->cmd_ctrl = pasemi_hwcontrol;
	chip->dev_ready = pasemi_device_ready;
	chip->read_buf = pasemi_read_buf;
	chip->write_buf = pasemi_write_buf;
	chip->chip_delay = 0;
	chip->ecc.mode = NAND_ECC_SOFT;
148
	chip->ecc.algo = NAND_ECC_HAMMING;
E
Egor Martovetsky 已提交
149 150

	/* Enable the following for a flash based bad block table */
151
	chip->bbt_options = NAND_BBT_USE_FLASH;
E
Egor Martovetsky 已提交
152

L
Lucas De Marchi 已提交
153
	/* Scan to find existence of the device */
154
	err = nand_scan(chip, 1);
155
	if (err)
E
Egor Martovetsky 已提交
156 157
		goto out_lpc;

158
	if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
159
		dev_err(dev, "Unable to register MTD device\n");
E
Egor Martovetsky 已提交
160 161 162 163
		err = -ENODEV;
		goto out_lpc;
	}

164 165
	dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
		 lpcctl);
E
Egor Martovetsky 已提交
166 167 168 169 170 171

	return 0;

 out_lpc:
	release_region(lpcctl, 4);
 out_ior:
172
	iounmap(chip->legacy.IO_ADDR_R);
E
Egor Martovetsky 已提交
173
 out_mtd:
174
	kfree(chip);
E
Egor Martovetsky 已提交
175 176 177 178
 out:
	return err;
}

B
Bill Pemberton 已提交
179
static int pasemi_nand_remove(struct platform_device *ofdev)
E
Egor Martovetsky 已提交
180 181 182 183 184 185
{
	struct nand_chip *chip;

	if (!pasemi_nand_mtd)
		return 0;

186
	chip = mtd_to_nand(pasemi_nand_mtd);
E
Egor Martovetsky 已提交
187 188

	/* Release resources, unregister device */
189
	nand_release(chip);
E
Egor Martovetsky 已提交
190 191 192

	release_region(lpcctl, 4);

193
	iounmap(chip->legacy.IO_ADDR_R);
E
Egor Martovetsky 已提交
194 195

	/* Free the MTD device structure */
196
	kfree(chip);
E
Egor Martovetsky 已提交
197 198 199 200 201 202

	pasemi_nand_mtd = NULL;

	return 0;
}

203
static const struct of_device_id pasemi_nand_match[] =
E
Egor Martovetsky 已提交
204 205 206 207 208 209 210 211 212
{
	{
		.compatible   = "pasemi,localbus-nand",
	},
	{},
};

MODULE_DEVICE_TABLE(of, pasemi_nand_match);

213
static struct platform_driver pasemi_nand_driver =
E
Egor Martovetsky 已提交
214
{
215
	.driver = {
216
		.name = driver_name,
217 218
		.of_match_table = pasemi_nand_match,
	},
E
Egor Martovetsky 已提交
219 220 221 222
	.probe		= pasemi_nand_probe,
	.remove		= pasemi_nand_remove,
};

223
module_platform_driver(pasemi_nand_driver);
E
Egor Martovetsky 已提交
224 225 226 227

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");