pasemi_nand.c 4.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
E
Egor Martovetsky 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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
 */

#undef DEBUG

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
16
#include <linux/mtd/rawnand.h>
E
Egor Martovetsky 已提交
17
#include <linux/mtd/nand_ecc.h>
18 19
#include <linux/of_address.h>
#include <linux/of_irq.h>
E
Egor Martovetsky 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
#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";

34
static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
E
Egor Martovetsky 已提交
35 36
{
	while (len > 0x800) {
37
		memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
E
Egor Martovetsky 已提交
38 39 40
		buf += 0x800;
		len -= 0x800;
	}
41
	memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
E
Egor Martovetsky 已提交
42 43
}

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

55
static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
E
Egor Martovetsky 已提交
56 57 58 59 60 61
			     unsigned int ctrl)
{
	if (cmd == NAND_CMD_NONE)
		return;

	if (ctrl & NAND_CLE)
62
		out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
E
Egor Martovetsky 已提交
63
	else
64
		out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
E
Egor Martovetsky 已提交
65 66 67 68 69 70

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

71
int pasemi_device_ready(struct nand_chip *chip)
E
Egor Martovetsky 已提交
72 73 74 75
{
	return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
}

B
Bill Pemberton 已提交
76
static int pasemi_nand_probe(struct platform_device *ofdev)
E
Egor Martovetsky 已提交
77
{
78
	struct device *dev = &ofdev->dev;
E
Egor Martovetsky 已提交
79
	struct pci_dev *pdev;
80
	struct device_node *np = dev->of_node;
E
Egor Martovetsky 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
	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;

94
	dev_dbg(dev, "pasemi_nand at %pR\n", &res);
E
Egor Martovetsky 已提交
95 96

	/* Allocate memory for MTD device structure and private data */
97 98
	chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
	if (!chip) {
E
Egor Martovetsky 已提交
99 100 101 102
		err = -ENOMEM;
		goto out;
	}

103
	pasemi_nand_mtd = nand_to_mtd(chip);
E
Egor Martovetsky 已提交
104 105

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

108 109
	chip->legacy.IO_ADDR_R = of_iomap(np, 0);
	chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
E
Egor Martovetsky 已提交
110

111
	if (!chip->legacy.IO_ADDR_R) {
E
Egor Martovetsky 已提交
112 113 114 115 116 117 118 119 120 121 122
		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);
123
	pci_dev_put(pdev);
E
Egor Martovetsky 已提交
124 125 126 127 128 129

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

130
	chip->legacy.cmd_ctrl = pasemi_hwcontrol;
131
	chip->legacy.dev_ready = pasemi_device_ready;
132 133
	chip->legacy.read_buf = pasemi_read_buf;
	chip->legacy.write_buf = pasemi_write_buf;
134
	chip->legacy.chip_delay = 0;
135
	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
136
	chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
E
Egor Martovetsky 已提交
137 138

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

L
Lucas De Marchi 已提交
141
	/* Scan to find existence of the device */
142
	err = nand_scan(chip, 1);
143
	if (err)
E
Egor Martovetsky 已提交
144 145
		goto out_lpc;

146
	if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
147
		dev_err(dev, "Unable to register MTD device\n");
E
Egor Martovetsky 已提交
148
		err = -ENODEV;
149
		goto out_cleanup_nand;
E
Egor Martovetsky 已提交
150 151
	}

152 153
	dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
		 lpcctl);
E
Egor Martovetsky 已提交
154 155 156

	return 0;

157 158
 out_cleanup_nand:
	nand_cleanup(chip);
E
Egor Martovetsky 已提交
159 160 161
 out_lpc:
	release_region(lpcctl, 4);
 out_ior:
162
	iounmap(chip->legacy.IO_ADDR_R);
E
Egor Martovetsky 已提交
163
 out_mtd:
164
	kfree(chip);
E
Egor Martovetsky 已提交
165 166 167 168
 out:
	return err;
}

B
Bill Pemberton 已提交
169
static int pasemi_nand_remove(struct platform_device *ofdev)
E
Egor Martovetsky 已提交
170 171
{
	struct nand_chip *chip;
172
	int ret;
E
Egor Martovetsky 已提交
173 174 175 176

	if (!pasemi_nand_mtd)
		return 0;

177
	chip = mtd_to_nand(pasemi_nand_mtd);
E
Egor Martovetsky 已提交
178 179

	/* Release resources, unregister device */
180 181 182
	ret = mtd_device_unregister(pasemi_nand_mtd);
	WARN_ON(ret);
	nand_cleanup(chip);
E
Egor Martovetsky 已提交
183 184 185

	release_region(lpcctl, 4);

186
	iounmap(chip->legacy.IO_ADDR_R);
E
Egor Martovetsky 已提交
187 188

	/* Free the MTD device structure */
189
	kfree(chip);
E
Egor Martovetsky 已提交
190 191 192 193 194 195

	pasemi_nand_mtd = NULL;

	return 0;
}

196
static const struct of_device_id pasemi_nand_match[] =
E
Egor Martovetsky 已提交
197 198 199 200 201 202 203 204 205
{
	{
		.compatible   = "pasemi,localbus-nand",
	},
	{},
};

MODULE_DEVICE_TABLE(of, pasemi_nand_match);

206
static struct platform_driver pasemi_nand_driver =
E
Egor Martovetsky 已提交
207
{
208
	.driver = {
209
		.name = driver_name,
210 211
		.of_match_table = pasemi_nand_match,
	},
E
Egor Martovetsky 已提交
212 213 214 215
	.probe		= pasemi_nand_probe,
	.remove		= pasemi_nand_remove,
};

216
module_platform_driver(pasemi_nand_driver);
E
Egor Martovetsky 已提交
217 218 219 220

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