net: smc91x: ACPI Enable lan91x adapters
[linux/fpc-iii.git] / drivers / mtd / nand / sharpsl.c
blob064ca1757589ac8d8cba8b536f72ff30632efcfe
1 /*
2 * drivers/mtd/nand/sharpsl.c
4 * Copyright (C) 2004 Richard Purdie
5 * Copyright (C) 2008 Dmitry Baryshkov
7 * Based on Sharp's NAND driver sharp_sl.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/genhd.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/nand_ecc.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/sharpsl.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
27 #include <asm/io.h>
28 #include <mach/hardware.h>
29 #include <asm/mach-types.h>
31 struct sharpsl_nand {
32 struct nand_chip chip;
34 void __iomem *io;
37 static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
39 return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
42 /* register offset */
43 #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
44 #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
45 #define ECCCP 0x08 /* column parity 5 - 0 bit */
46 #define ECCCNTR 0x0C /* ECC byte counter */
47 #define ECCCLRR 0x10 /* cleare ECC */
48 #define FLASHIO 0x14 /* Flash I/O */
49 #define FLASHCTL 0x18 /* Flash Control */
51 /* Flash control bit */
52 #define FLRYBY (1 << 5)
53 #define FLCE1 (1 << 4)
54 #define FLWP (1 << 3)
55 #define FLALE (1 << 2)
56 #define FLCLE (1 << 1)
57 #define FLCE0 (1 << 0)
60 * hardware specific access to control-lines
61 * ctrl:
62 * NAND_CNE: bit 0 -> ! bit 0 & 4
63 * NAND_CLE: bit 1 -> bit 1
64 * NAND_ALE: bit 2 -> bit 2
67 static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
68 unsigned int ctrl)
70 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
71 struct nand_chip *chip = mtd_to_nand(mtd);
73 if (ctrl & NAND_CTRL_CHANGE) {
74 unsigned char bits = ctrl & 0x07;
76 bits |= (ctrl & 0x01) << 4;
78 bits ^= 0x11;
80 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
83 if (cmd != NAND_CMD_NONE)
84 writeb(cmd, chip->IO_ADDR_W);
87 static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
89 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
90 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
93 static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
95 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
96 writeb(0, sharpsl->io + ECCCLRR);
99 static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
101 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
102 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
103 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
104 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
105 return readb(sharpsl->io + ECCCNTR) != 0;
109 * Main initialization routine
111 static int sharpsl_nand_probe(struct platform_device *pdev)
113 struct nand_chip *this;
114 struct mtd_info *mtd;
115 struct resource *r;
116 int err = 0;
117 struct sharpsl_nand *sharpsl;
118 struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
120 if (!data) {
121 dev_err(&pdev->dev, "no platform data!\n");
122 return -EINVAL;
125 /* Allocate memory for MTD device structure and private data */
126 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
127 if (!sharpsl)
128 return -ENOMEM;
130 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 if (!r) {
132 dev_err(&pdev->dev, "no io memory resource defined!\n");
133 err = -ENODEV;
134 goto err_get_res;
137 /* map physical address */
138 sharpsl->io = ioremap(r->start, resource_size(r));
139 if (!sharpsl->io) {
140 dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
141 err = -EIO;
142 goto err_ioremap;
145 /* Get pointer to private data */
146 this = (struct nand_chip *)(&sharpsl->chip);
148 /* Link the private data with the MTD structure */
149 mtd = nand_to_mtd(this);
150 mtd->dev.parent = &pdev->dev;
151 mtd_set_ooblayout(mtd, data->ecc_layout);
153 platform_set_drvdata(pdev, sharpsl);
156 * PXA initialize
158 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
160 /* Set address of NAND IO lines */
161 this->IO_ADDR_R = sharpsl->io + FLASHIO;
162 this->IO_ADDR_W = sharpsl->io + FLASHIO;
163 /* Set address of hardware control function */
164 this->cmd_ctrl = sharpsl_nand_hwcontrol;
165 this->dev_ready = sharpsl_nand_dev_ready;
166 /* 15 us command delay time */
167 this->chip_delay = 15;
168 /* set eccmode using hardware ECC */
169 this->ecc.mode = NAND_ECC_HW;
170 this->ecc.size = 256;
171 this->ecc.bytes = 3;
172 this->ecc.strength = 1;
173 this->badblock_pattern = data->badblock_pattern;
174 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
175 this->ecc.calculate = sharpsl_nand_calculate_ecc;
176 this->ecc.correct = nand_correct_data;
178 /* Scan to find existence of the device */
179 err = nand_scan(mtd, 1);
180 if (err)
181 goto err_scan;
183 /* Register the partitions */
184 mtd->name = "sharpsl-nand";
186 err = mtd_device_parse_register(mtd, NULL, NULL,
187 data->partitions, data->nr_partitions);
188 if (err)
189 goto err_add;
191 /* Return happy */
192 return 0;
194 err_add:
195 nand_release(mtd);
197 err_scan:
198 iounmap(sharpsl->io);
199 err_ioremap:
200 err_get_res:
201 kfree(sharpsl);
202 return err;
206 * Clean up routine
208 static int sharpsl_nand_remove(struct platform_device *pdev)
210 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
212 /* Release resources, unregister device */
213 nand_release(nand_to_mtd(&sharpsl->chip));
215 iounmap(sharpsl->io);
217 /* Free the MTD device structure */
218 kfree(sharpsl);
220 return 0;
223 static struct platform_driver sharpsl_nand_driver = {
224 .driver = {
225 .name = "sharpsl-nand",
227 .probe = sharpsl_nand_probe,
228 .remove = sharpsl_nand_remove,
231 module_platform_driver(sharpsl_nand_driver);
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
235 MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");