1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 Richard Purdie
4 * Copyright (C) 2008 Dmitry Baryshkov
6 * Based on Sharp's NAND driver sharp_sl.c
9 #include <linux/genhd.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/rawnand.h>
15 #include <linux/mtd/nand_ecc.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/mtd/sharpsl.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
23 struct nand_controller controller;
24 struct nand_chip chip;
29 static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
31 return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
35 #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
36 #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
37 #define ECCCP 0x08 /* column parity 5 - 0 bit */
38 #define ECCCNTR 0x0C /* ECC byte counter */
39 #define ECCCLRR 0x10 /* cleare ECC */
40 #define FLASHIO 0x14 /* Flash I/O */
41 #define FLASHCTL 0x18 /* Flash Control */
43 /* Flash control bit */
44 #define FLRYBY (1 << 5)
45 #define FLCE1 (1 << 4)
47 #define FLALE (1 << 2)
48 #define FLCLE (1 << 1)
49 #define FLCE0 (1 << 0)
52 * hardware specific access to control-lines
54 * NAND_CNE: bit 0 -> ! bit 0 & 4
55 * NAND_CLE: bit 1 -> bit 1
56 * NAND_ALE: bit 2 -> bit 2
59 static void sharpsl_nand_hwcontrol(struct nand_chip *chip, int cmd,
62 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
64 if (ctrl & NAND_CTRL_CHANGE) {
65 unsigned char bits = ctrl & 0x07;
67 bits |= (ctrl & 0x01) << 4;
71 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
74 if (cmd != NAND_CMD_NONE)
75 writeb(cmd, chip->legacy.IO_ADDR_W);
78 static int sharpsl_nand_dev_ready(struct nand_chip *chip)
80 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
81 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
84 static void sharpsl_nand_enable_hwecc(struct nand_chip *chip, int mode)
86 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
87 writeb(0, sharpsl->io + ECCCLRR);
90 static int sharpsl_nand_calculate_ecc(struct nand_chip *chip,
91 const u_char * dat, u_char * ecc_code)
93 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(nand_to_mtd(chip));
94 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
95 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
96 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
97 return readb(sharpsl->io + ECCCNTR) != 0;
100 static int sharpsl_attach_chip(struct nand_chip *chip)
102 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
105 chip->ecc.size = 256;
107 chip->ecc.strength = 1;
108 chip->ecc.hwctl = sharpsl_nand_enable_hwecc;
109 chip->ecc.calculate = sharpsl_nand_calculate_ecc;
110 chip->ecc.correct = nand_correct_data;
115 static const struct nand_controller_ops sharpsl_ops = {
116 .attach_chip = sharpsl_attach_chip,
120 * Main initialization routine
122 static int sharpsl_nand_probe(struct platform_device *pdev)
124 struct nand_chip *this;
125 struct mtd_info *mtd;
128 struct sharpsl_nand *sharpsl;
129 struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
132 dev_err(&pdev->dev, "no platform data!\n");
136 /* Allocate memory for MTD device structure and private data */
137 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
141 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 dev_err(&pdev->dev, "no io memory resource defined!\n");
148 /* map physical address */
149 sharpsl->io = ioremap(r->start, resource_size(r));
151 dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
156 /* Get pointer to private data */
157 this = (struct nand_chip *)(&sharpsl->chip);
159 nand_controller_init(&sharpsl->controller);
160 sharpsl->controller.ops = &sharpsl_ops;
161 this->controller = &sharpsl->controller;
163 /* Link the private data with the MTD structure */
164 mtd = nand_to_mtd(this);
165 mtd->dev.parent = &pdev->dev;
166 mtd_set_ooblayout(mtd, data->ecc_layout);
168 platform_set_drvdata(pdev, sharpsl);
173 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
175 /* Set address of NAND IO lines */
176 this->legacy.IO_ADDR_R = sharpsl->io + FLASHIO;
177 this->legacy.IO_ADDR_W = sharpsl->io + FLASHIO;
178 /* Set address of hardware control function */
179 this->legacy.cmd_ctrl = sharpsl_nand_hwcontrol;
180 this->legacy.dev_ready = sharpsl_nand_dev_ready;
181 /* 15 us command delay time */
182 this->legacy.chip_delay = 15;
183 this->badblock_pattern = data->badblock_pattern;
185 /* Scan to find existence of the device */
186 err = nand_scan(this, 1);
190 /* Register the partitions */
191 mtd->name = "sharpsl-nand";
193 err = mtd_device_parse_register(mtd, data->part_parsers, NULL,
194 data->partitions, data->nr_partitions);
205 iounmap(sharpsl->io);
215 static int sharpsl_nand_remove(struct platform_device *pdev)
217 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
218 struct nand_chip *chip = &sharpsl->chip;
221 /* Unregister device */
222 ret = mtd_device_unregister(nand_to_mtd(chip));
225 /* Release resources */
228 iounmap(sharpsl->io);
230 /* Free the driver's structure */
236 static struct platform_driver sharpsl_nand_driver = {
238 .name = "sharpsl-nand",
240 .probe = sharpsl_nand_probe,
241 .remove = sharpsl_nand_remove,
244 module_platform_driver(sharpsl_nand_driver);
246 MODULE_LICENSE("GPL");
247 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
248 MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");