1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2007 PA Semi, Inc
8 * Driver for the PWRficient onchip NAND flash interface
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/rawnand.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pci.h>
25 #define LBICTRL_LPCCTL_NR 0x00004000
26 #define CLE_PIN_CTL 15
27 #define ALE_PIN_CTL 14
30 struct nand_chip chip;
32 struct nand_controller controller;
35 static const char driver_name[] = "pasemi-nand";
37 static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
40 memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
44 memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
47 static void pasemi_write_buf(struct nand_chip *chip, const u_char *buf,
51 memcpy_toio(chip->legacy.IO_ADDR_R, buf, 0x800);
55 memcpy_toio(chip->legacy.IO_ADDR_R, buf, len);
58 static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
61 struct pasemi_ddata *ddata = container_of(chip, struct pasemi_ddata, chip);
63 if (cmd == NAND_CMD_NONE)
67 out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
69 out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
71 /* Push out posted writes */
76 static int pasemi_device_ready(struct nand_chip *chip)
78 struct pasemi_ddata *ddata = container_of(chip, struct pasemi_ddata, chip);
80 return !!(inl(ddata->lpcctl) & LBICTRL_LPCCTL_NR);
83 static int pasemi_attach_chip(struct nand_chip *chip)
85 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
86 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
87 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
92 static const struct nand_controller_ops pasemi_ops = {
93 .attach_chip = pasemi_attach_chip,
96 static int pasemi_nand_probe(struct platform_device *ofdev)
98 struct device *dev = &ofdev->dev;
100 struct device_node *np = dev->of_node;
102 struct nand_chip *chip;
103 struct nand_controller *controller;
105 struct pasemi_ddata *ddata;
106 struct mtd_info *pasemi_nand_mtd;
108 err = of_address_to_resource(np, 0, &res);
113 dev_dbg(dev, "pasemi_nand at %pR\n", &res);
115 /* Allocate memory for MTD device structure and private data */
116 ddata = kzalloc(sizeof(*ddata), GFP_KERNEL);
121 platform_set_drvdata(ofdev, ddata);
123 controller = &ddata->controller;
125 controller->ops = &pasemi_ops;
126 nand_controller_init(controller);
127 chip->controller = controller;
129 pasemi_nand_mtd = nand_to_mtd(chip);
131 /* Link the private data with the MTD structure */
132 pasemi_nand_mtd->dev.parent = dev;
134 chip->legacy.IO_ADDR_R = of_iomap(np, 0);
135 chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
137 if (!chip->legacy.IO_ADDR_R) {
142 pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
148 ddata->lpcctl = pci_resource_start(pdev, 0);
151 if (!request_region(ddata->lpcctl, 4, driver_name)) {
156 chip->legacy.cmd_ctrl = pasemi_hwcontrol;
157 chip->legacy.dev_ready = pasemi_device_ready;
158 chip->legacy.read_buf = pasemi_read_buf;
159 chip->legacy.write_buf = pasemi_write_buf;
160 chip->legacy.chip_delay = 0;
162 /* Enable the following for a flash based bad block table */
163 chip->bbt_options = NAND_BBT_USE_FLASH;
166 * This driver assumes that the default ECC engine should be TYPE_SOFT.
167 * Set ->engine_type before registering the NAND devices in order to
168 * provide a driver specific default value.
170 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
172 /* Scan to find existence of the device */
173 err = nand_scan(chip, 1);
177 if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
178 dev_err(dev, "Unable to register MTD device\n");
180 goto out_cleanup_nand;
183 dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
191 release_region(ddata->lpcctl, 4);
193 iounmap(chip->legacy.IO_ADDR_R);
200 static void pasemi_nand_remove(struct platform_device *ofdev)
202 struct pasemi_ddata *ddata = platform_get_drvdata(ofdev);
203 struct mtd_info *pasemi_nand_mtd;
205 struct nand_chip *chip;
208 pasemi_nand_mtd = nand_to_mtd(chip);
210 /* Release resources, unregister device */
211 ret = mtd_device_unregister(pasemi_nand_mtd);
215 release_region(ddata->lpcctl, 4);
217 iounmap(chip->legacy.IO_ADDR_R);
219 /* Free the MTD device structure */
223 static const struct of_device_id pasemi_nand_match[] =
226 .compatible = "pasemi,localbus-nand",
231 MODULE_DEVICE_TABLE(of, pasemi_nand_match);
233 static struct platform_driver pasemi_nand_driver =
237 .of_match_table = pasemi_nand_match,
239 .probe = pasemi_nand_probe,
240 .remove = pasemi_nand_remove,
243 module_platform_driver(pasemi_nand_driver);
245 MODULE_LICENSE("GPL");
247 MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");