Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6.git] / drivers / mtd / nand / nuc900_nand.c
blob52115151e4a7f325491a3dbf6f16ccd87221b37a
1 /*
2 * Copyright © 2009 Nuvoton technology corporation.
4 * Wan ZongShun <mcuos.com@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;version 2 of the License.
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/nand.h>
24 #include <linux/mtd/partitions.h>
26 #define REG_FMICSR 0x00
27 #define REG_SMCSR 0xa0
28 #define REG_SMISR 0xac
29 #define REG_SMCMD 0xb0
30 #define REG_SMADDR 0xb4
31 #define REG_SMDATA 0xb8
33 #define RESET_FMI 0x01
34 #define NAND_EN 0x08
35 #define READYBUSY (0x01 << 18)
37 #define SWRST 0x01
38 #define PSIZE (0x01 << 3)
39 #define DMARWEN (0x03 << 1)
40 #define BUSWID (0x01 << 4)
41 #define ECC4EN (0x01 << 5)
42 #define WP (0x01 << 24)
43 #define NANDCS (0x01 << 25)
44 #define ENDADDR (0x01 << 31)
46 #define read_data_reg(dev) \
47 __raw_readl((dev)->reg + REG_SMDATA)
49 #define write_data_reg(dev, val) \
50 __raw_writel((val), (dev)->reg + REG_SMDATA)
52 #define write_cmd_reg(dev, val) \
53 __raw_writel((val), (dev)->reg + REG_SMCMD)
55 #define write_addr_reg(dev, val) \
56 __raw_writel((val), (dev)->reg + REG_SMADDR)
58 struct nuc900_nand {
59 struct mtd_info mtd;
60 struct nand_chip chip;
61 void __iomem *reg;
62 struct clk *clk;
63 spinlock_t lock;
66 static const struct mtd_partition partitions[] = {
68 .name = "NAND FS 0",
69 .offset = 0,
70 .size = 8 * 1024 * 1024
73 .name = "NAND FS 1",
74 .offset = MTDPART_OFS_APPEND,
75 .size = MTDPART_SIZ_FULL
79 static unsigned char nuc900_nand_read_byte(struct mtd_info *mtd)
81 unsigned char ret;
82 struct nuc900_nand *nand;
84 nand = container_of(mtd, struct nuc900_nand, mtd);
86 ret = (unsigned char)read_data_reg(nand);
88 return ret;
91 static void nuc900_nand_read_buf(struct mtd_info *mtd,
92 unsigned char *buf, int len)
94 int i;
95 struct nuc900_nand *nand;
97 nand = container_of(mtd, struct nuc900_nand, mtd);
99 for (i = 0; i < len; i++)
100 buf[i] = (unsigned char)read_data_reg(nand);
103 static void nuc900_nand_write_buf(struct mtd_info *mtd,
104 const unsigned char *buf, int len)
106 int i;
107 struct nuc900_nand *nand;
109 nand = container_of(mtd, struct nuc900_nand, mtd);
111 for (i = 0; i < len; i++)
112 write_data_reg(nand, buf[i]);
115 static int nuc900_check_rb(struct nuc900_nand *nand)
117 unsigned int val;
118 spin_lock(&nand->lock);
119 val = __raw_readl(REG_SMISR);
120 val &= READYBUSY;
121 spin_unlock(&nand->lock);
123 return val;
126 static int nuc900_nand_devready(struct mtd_info *mtd)
128 struct nuc900_nand *nand;
129 int ready;
131 nand = container_of(mtd, struct nuc900_nand, mtd);
133 ready = (nuc900_check_rb(nand)) ? 1 : 0;
134 return ready;
137 static void nuc900_nand_command_lp(struct mtd_info *mtd, unsigned int command,
138 int column, int page_addr)
140 register struct nand_chip *chip = mtd->priv;
141 struct nuc900_nand *nand;
143 nand = container_of(mtd, struct nuc900_nand, mtd);
145 if (command == NAND_CMD_READOOB) {
146 column += mtd->writesize;
147 command = NAND_CMD_READ0;
150 write_cmd_reg(nand, command & 0xff);
152 if (column != -1 || page_addr != -1) {
154 if (column != -1) {
155 if (chip->options & NAND_BUSWIDTH_16)
156 column >>= 1;
157 write_addr_reg(nand, column);
158 write_addr_reg(nand, column >> 8 | ENDADDR);
160 if (page_addr != -1) {
161 write_addr_reg(nand, page_addr);
163 if (chip->chipsize > (128 << 20)) {
164 write_addr_reg(nand, page_addr >> 8);
165 write_addr_reg(nand, page_addr >> 16 | ENDADDR);
166 } else {
167 write_addr_reg(nand, page_addr >> 8 | ENDADDR);
172 switch (command) {
173 case NAND_CMD_CACHEDPROG:
174 case NAND_CMD_PAGEPROG:
175 case NAND_CMD_ERASE1:
176 case NAND_CMD_ERASE2:
177 case NAND_CMD_SEQIN:
178 case NAND_CMD_RNDIN:
179 case NAND_CMD_STATUS:
180 return;
182 case NAND_CMD_RESET:
183 if (chip->dev_ready)
184 break;
185 udelay(chip->chip_delay);
187 write_cmd_reg(nand, NAND_CMD_STATUS);
188 write_cmd_reg(nand, command);
190 while (!nuc900_check_rb(nand))
193 return;
195 case NAND_CMD_RNDOUT:
196 write_cmd_reg(nand, NAND_CMD_RNDOUTSTART);
197 return;
199 case NAND_CMD_READ0:
201 write_cmd_reg(nand, NAND_CMD_READSTART);
202 default:
204 if (!chip->dev_ready) {
205 udelay(chip->chip_delay);
206 return;
210 /* Apply this short delay always to ensure that we do wait tWB in
211 * any case on any machine. */
212 ndelay(100);
214 while (!chip->dev_ready(mtd))
219 static void nuc900_nand_enable(struct nuc900_nand *nand)
221 unsigned int val;
222 spin_lock(&nand->lock);
223 __raw_writel(RESET_FMI, (nand->reg + REG_FMICSR));
225 val = __raw_readl(nand->reg + REG_FMICSR);
227 if (!(val & NAND_EN))
228 __raw_writel(val | NAND_EN, REG_FMICSR);
230 val = __raw_readl(nand->reg + REG_SMCSR);
232 val &= ~(SWRST|PSIZE|DMARWEN|BUSWID|ECC4EN|NANDCS);
233 val |= WP;
235 __raw_writel(val, nand->reg + REG_SMCSR);
237 spin_unlock(&nand->lock);
240 static int nuc900_nand_probe(struct platform_device *pdev)
242 struct nuc900_nand *nuc900_nand;
243 struct nand_chip *chip;
244 int retval;
245 struct resource *res;
247 retval = 0;
249 nuc900_nand = kzalloc(sizeof(struct nuc900_nand), GFP_KERNEL);
250 if (!nuc900_nand)
251 return -ENOMEM;
252 chip = &(nuc900_nand->chip);
254 nuc900_nand->mtd.priv = chip;
255 nuc900_nand->mtd.owner = THIS_MODULE;
256 spin_lock_init(&nuc900_nand->lock);
258 nuc900_nand->clk = clk_get(&pdev->dev, NULL);
259 if (IS_ERR(nuc900_nand->clk)) {
260 retval = -ENOENT;
261 goto fail1;
263 clk_enable(nuc900_nand->clk);
265 chip->cmdfunc = nuc900_nand_command_lp;
266 chip->dev_ready = nuc900_nand_devready;
267 chip->read_byte = nuc900_nand_read_byte;
268 chip->write_buf = nuc900_nand_write_buf;
269 chip->read_buf = nuc900_nand_read_buf;
270 chip->chip_delay = 50;
271 chip->options = 0;
272 chip->ecc.mode = NAND_ECC_SOFT;
274 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
275 if (!res) {
276 retval = -ENXIO;
277 goto fail1;
280 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
281 retval = -EBUSY;
282 goto fail1;
285 nuc900_nand->reg = ioremap(res->start, resource_size(res));
286 if (!nuc900_nand->reg) {
287 retval = -ENOMEM;
288 goto fail2;
291 nuc900_nand_enable(nuc900_nand);
293 if (nand_scan(&(nuc900_nand->mtd), 1)) {
294 retval = -ENXIO;
295 goto fail3;
298 mtd_device_register(&(nuc900_nand->mtd), partitions,
299 ARRAY_SIZE(partitions));
301 platform_set_drvdata(pdev, nuc900_nand);
303 return retval;
305 fail3: iounmap(nuc900_nand->reg);
306 fail2: release_mem_region(res->start, resource_size(res));
307 fail1: kfree(nuc900_nand);
308 return retval;
311 static int nuc900_nand_remove(struct platform_device *pdev)
313 struct nuc900_nand *nuc900_nand = platform_get_drvdata(pdev);
314 struct resource *res;
316 nand_release(&nuc900_nand->mtd);
317 iounmap(nuc900_nand->reg);
319 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
320 release_mem_region(res->start, resource_size(res));
322 clk_disable(nuc900_nand->clk);
323 clk_put(nuc900_nand->clk);
325 kfree(nuc900_nand);
327 return 0;
330 static struct platform_driver nuc900_nand_driver = {
331 .probe = nuc900_nand_probe,
332 .remove = nuc900_nand_remove,
333 .driver = {
334 .name = "nuc900-fmi",
335 .owner = THIS_MODULE,
339 module_platform_driver(nuc900_nand_driver);
341 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
342 MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
343 MODULE_LICENSE("GPL");
344 MODULE_ALIAS("platform:nuc900-fmi");