1
2
3
4
5
6
7
8
9
10
11
12
13
14
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>
26
27#include <asm/io.h>
28#include <mach/hardware.h>
29#include <asm/mach-types.h>
30
31struct sharpsl_nand {
32 struct mtd_info mtd;
33 struct nand_chip chip;
34
35 void __iomem *io;
36};
37
38#define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
39
40
41#define ECCLPLB 0x00
42#define ECCLPUB 0x04
43#define ECCCP 0x08
44#define ECCCNTR 0x0C
45#define ECCCLRR 0x10
46#define FLASHIO 0x14
47#define FLASHCTL 0x18
48
49
50#define FLRYBY (1 << 5)
51#define FLCE1 (1 << 4)
52#define FLWP (1 << 3)
53#define FLALE (1 << 2)
54#define FLCLE (1 << 1)
55#define FLCE0 (1 << 0)
56
57
58
59
60
61
62
63
64
65static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
66 unsigned int ctrl)
67{
68 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
69 struct nand_chip *chip = mtd->priv;
70
71 if (ctrl & NAND_CTRL_CHANGE) {
72 unsigned char bits = ctrl & 0x07;
73
74 bits |= (ctrl & 0x01) << 4;
75
76 bits ^= 0x11;
77
78 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
79 }
80
81 if (cmd != NAND_CMD_NONE)
82 writeb(cmd, chip->IO_ADDR_W);
83}
84
85static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
86{
87 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
88 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
89}
90
91static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
92{
93 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
94 writeb(0, sharpsl->io + ECCCLRR);
95}
96
97static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
98{
99 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
100 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
101 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
102 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
103 return readb(sharpsl->io + ECCCNTR) != 0;
104}
105
106
107
108
109static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
110{
111 struct nand_chip *this;
112 struct resource *r;
113 int err = 0;
114 struct sharpsl_nand *sharpsl;
115 struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;
116
117 if (!data) {
118 dev_err(&pdev->dev, "no platform data!\n");
119 return -EINVAL;
120 }
121
122
123 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
124 if (!sharpsl) {
125 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
126 return -ENOMEM;
127 }
128
129 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 if (!r) {
131 dev_err(&pdev->dev, "no io memory resource defined!\n");
132 err = -ENODEV;
133 goto err_get_res;
134 }
135
136
137 sharpsl->io = ioremap(r->start, resource_size(r));
138 if (!sharpsl->io) {
139 printk("ioremap to access Sharp SL NAND chip failed\n");
140 err = -EIO;
141 goto err_ioremap;
142 }
143
144
145 this = (struct nand_chip *)(&sharpsl->chip);
146
147
148 sharpsl->mtd.priv = this;
149 sharpsl->mtd.owner = THIS_MODULE;
150
151 platform_set_drvdata(pdev, sharpsl);
152
153
154
155
156 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
157
158
159 this->IO_ADDR_R = sharpsl->io + FLASHIO;
160 this->IO_ADDR_W = sharpsl->io + FLASHIO;
161
162 this->cmd_ctrl = sharpsl_nand_hwcontrol;
163 this->dev_ready = sharpsl_nand_dev_ready;
164
165 this->chip_delay = 15;
166
167 this->ecc.mode = NAND_ECC_HW;
168 this->ecc.size = 256;
169 this->ecc.bytes = 3;
170 this->badblock_pattern = data->badblock_pattern;
171 this->ecc.layout = data->ecc_layout;
172 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
173 this->ecc.calculate = sharpsl_nand_calculate_ecc;
174 this->ecc.correct = nand_correct_data;
175
176
177 err = nand_scan(&sharpsl->mtd, 1);
178 if (err)
179 goto err_scan;
180
181
182 sharpsl->mtd.name = "sharpsl-nand";
183
184 err = mtd_device_parse_register(&sharpsl->mtd, NULL, 0,
185 data->partitions, data->nr_partitions);
186 if (err)
187 goto err_add;
188
189
190 return 0;
191
192err_add:
193 nand_release(&sharpsl->mtd);
194
195err_scan:
196 platform_set_drvdata(pdev, NULL);
197 iounmap(sharpsl->io);
198err_ioremap:
199err_get_res:
200 kfree(sharpsl);
201 return err;
202}
203
204
205
206
207static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
208{
209 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
210
211
212 nand_release(&sharpsl->mtd);
213
214 platform_set_drvdata(pdev, NULL);
215
216 iounmap(sharpsl->io);
217
218
219 kfree(sharpsl);
220
221 return 0;
222}
223
224static struct platform_driver sharpsl_nand_driver = {
225 .driver = {
226 .name = "sharpsl-nand",
227 .owner = THIS_MODULE,
228 },
229 .probe = sharpsl_nand_probe,
230 .remove = __devexit_p(sharpsl_nand_remove),
231};
232
233module_platform_driver(sharpsl_nand_driver);
234
235MODULE_LICENSE("GPL");
236MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
237MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");
238