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>
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
35 #define READYBUSY (0x01 << 18)
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)
60 struct nand_chip chip
;
66 static const struct mtd_partition partitions
[] = {
70 .size
= 8 * 1024 * 1024
74 .offset
= MTDPART_OFS_APPEND
,
75 .size
= MTDPART_SIZ_FULL
79 static unsigned char nuc900_nand_read_byte(struct mtd_info
*mtd
)
82 struct nuc900_nand
*nand
;
84 nand
= container_of(mtd
, struct nuc900_nand
, mtd
);
86 ret
= (unsigned char)read_data_reg(nand
);
91 static void nuc900_nand_read_buf(struct mtd_info
*mtd
,
92 unsigned char *buf
, int len
)
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
)
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_verify_buf(struct mtd_info
*mtd
,
116 const unsigned char *buf
, int len
)
119 struct nuc900_nand
*nand
;
121 nand
= container_of(mtd
, struct nuc900_nand
, mtd
);
123 for (i
= 0; i
< len
; i
++) {
124 if (buf
[i
] != (unsigned char)read_data_reg(nand
))
131 static int nuc900_check_rb(struct nuc900_nand
*nand
)
134 spin_lock(&nand
->lock
);
135 val
= __raw_readl(REG_SMISR
);
137 spin_unlock(&nand
->lock
);
142 static int nuc900_nand_devready(struct mtd_info
*mtd
)
144 struct nuc900_nand
*nand
;
147 nand
= container_of(mtd
, struct nuc900_nand
, mtd
);
149 ready
= (nuc900_check_rb(nand
)) ? 1 : 0;
153 static void nuc900_nand_command_lp(struct mtd_info
*mtd
, unsigned int command
,
154 int column
, int page_addr
)
156 register struct nand_chip
*chip
= mtd
->priv
;
157 struct nuc900_nand
*nand
;
159 nand
= container_of(mtd
, struct nuc900_nand
, mtd
);
161 if (command
== NAND_CMD_READOOB
) {
162 column
+= mtd
->writesize
;
163 command
= NAND_CMD_READ0
;
166 write_cmd_reg(nand
, command
& 0xff);
168 if (column
!= -1 || page_addr
!= -1) {
171 if (chip
->options
& NAND_BUSWIDTH_16
)
173 write_addr_reg(nand
, column
);
174 write_addr_reg(nand
, column
>> 8 | ENDADDR
);
176 if (page_addr
!= -1) {
177 write_addr_reg(nand
, page_addr
);
179 if (chip
->chipsize
> (128 << 20)) {
180 write_addr_reg(nand
, page_addr
>> 8);
181 write_addr_reg(nand
, page_addr
>> 16 | ENDADDR
);
183 write_addr_reg(nand
, page_addr
>> 8 | ENDADDR
);
189 case NAND_CMD_CACHEDPROG
:
190 case NAND_CMD_PAGEPROG
:
191 case NAND_CMD_ERASE1
:
192 case NAND_CMD_ERASE2
:
195 case NAND_CMD_STATUS
:
196 case NAND_CMD_DEPLETE1
:
199 case NAND_CMD_STATUS_ERROR
:
200 case NAND_CMD_STATUS_ERROR0
:
201 case NAND_CMD_STATUS_ERROR1
:
202 case NAND_CMD_STATUS_ERROR2
:
203 case NAND_CMD_STATUS_ERROR3
:
204 udelay(chip
->chip_delay
);
210 udelay(chip
->chip_delay
);
212 write_cmd_reg(nand
, NAND_CMD_STATUS
);
213 write_cmd_reg(nand
, command
);
215 while (!nuc900_check_rb(nand
))
220 case NAND_CMD_RNDOUT
:
221 write_cmd_reg(nand
, NAND_CMD_RNDOUTSTART
);
226 write_cmd_reg(nand
, NAND_CMD_READSTART
);
229 if (!chip
->dev_ready
) {
230 udelay(chip
->chip_delay
);
235 /* Apply this short delay always to ensure that we do wait tWB in
236 * any case on any machine. */
239 while (!chip
->dev_ready(mtd
))
244 static void nuc900_nand_enable(struct nuc900_nand
*nand
)
247 spin_lock(&nand
->lock
);
248 __raw_writel(RESET_FMI
, (nand
->reg
+ REG_FMICSR
));
250 val
= __raw_readl(nand
->reg
+ REG_FMICSR
);
252 if (!(val
& NAND_EN
))
253 __raw_writel(val
| NAND_EN
, REG_FMICSR
);
255 val
= __raw_readl(nand
->reg
+ REG_SMCSR
);
257 val
&= ~(SWRST
|PSIZE
|DMARWEN
|BUSWID
|ECC4EN
|NANDCS
);
260 __raw_writel(val
, nand
->reg
+ REG_SMCSR
);
262 spin_unlock(&nand
->lock
);
265 static int __devinit
nuc900_nand_probe(struct platform_device
*pdev
)
267 struct nuc900_nand
*nuc900_nand
;
268 struct nand_chip
*chip
;
270 struct resource
*res
;
274 nuc900_nand
= kzalloc(sizeof(struct nuc900_nand
), GFP_KERNEL
);
277 chip
= &(nuc900_nand
->chip
);
279 nuc900_nand
->mtd
.priv
= chip
;
280 nuc900_nand
->mtd
.owner
= THIS_MODULE
;
281 spin_lock_init(&nuc900_nand
->lock
);
283 nuc900_nand
->clk
= clk_get(&pdev
->dev
, NULL
);
284 if (IS_ERR(nuc900_nand
->clk
)) {
288 clk_enable(nuc900_nand
->clk
);
290 chip
->cmdfunc
= nuc900_nand_command_lp
;
291 chip
->dev_ready
= nuc900_nand_devready
;
292 chip
->read_byte
= nuc900_nand_read_byte
;
293 chip
->write_buf
= nuc900_nand_write_buf
;
294 chip
->read_buf
= nuc900_nand_read_buf
;
295 chip
->verify_buf
= nuc900_verify_buf
;
296 chip
->chip_delay
= 50;
298 chip
->ecc
.mode
= NAND_ECC_SOFT
;
300 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
306 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
311 nuc900_nand
->reg
= ioremap(res
->start
, resource_size(res
));
312 if (!nuc900_nand
->reg
) {
317 nuc900_nand_enable(nuc900_nand
);
319 if (nand_scan(&(nuc900_nand
->mtd
), 1)) {
324 mtd_device_register(&(nuc900_nand
->mtd
), partitions
,
325 ARRAY_SIZE(partitions
));
327 platform_set_drvdata(pdev
, nuc900_nand
);
331 fail3
: iounmap(nuc900_nand
->reg
);
332 fail2
: release_mem_region(res
->start
, resource_size(res
));
333 fail1
: kfree(nuc900_nand
);
337 static int __devexit
nuc900_nand_remove(struct platform_device
*pdev
)
339 struct nuc900_nand
*nuc900_nand
= platform_get_drvdata(pdev
);
340 struct resource
*res
;
342 nand_release(&nuc900_nand
->mtd
);
343 iounmap(nuc900_nand
->reg
);
345 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
346 release_mem_region(res
->start
, resource_size(res
));
348 clk_disable(nuc900_nand
->clk
);
349 clk_put(nuc900_nand
->clk
);
353 platform_set_drvdata(pdev
, NULL
);
358 static struct platform_driver nuc900_nand_driver
= {
359 .probe
= nuc900_nand_probe
,
360 .remove
= __devexit_p(nuc900_nand_remove
),
362 .name
= "nuc900-fmi",
363 .owner
= THIS_MODULE
,
367 module_platform_driver(nuc900_nand_driver
);
369 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
370 MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS("platform:nuc900-fmi");