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/module.h>
14 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/rawnand.h>
23 #include <linux/mtd/partitions.h>
25 #define REG_FMICSR 0x00
26 #define REG_SMCSR 0xa0
27 #define REG_SMISR 0xac
28 #define REG_SMCMD 0xb0
29 #define REG_SMADDR 0xb4
30 #define REG_SMDATA 0xb8
32 #define RESET_FMI 0x01
34 #define READYBUSY (0x01 << 18)
37 #define PSIZE (0x01 << 3)
38 #define DMARWEN (0x03 << 1)
39 #define BUSWID (0x01 << 4)
40 #define ECC4EN (0x01 << 5)
41 #define WP (0x01 << 24)
42 #define NANDCS (0x01 << 25)
43 #define ENDADDR (0x01 << 31)
45 #define read_data_reg(dev) \
46 __raw_readl((dev)->reg + REG_SMDATA)
48 #define write_data_reg(dev, val) \
49 __raw_writel((val), (dev)->reg + REG_SMDATA)
51 #define write_cmd_reg(dev, val) \
52 __raw_writel((val), (dev)->reg + REG_SMCMD)
54 #define write_addr_reg(dev, val) \
55 __raw_writel((val), (dev)->reg + REG_SMADDR)
58 struct nand_chip chip
;
64 static inline struct nuc900_nand
*mtd_to_nuc900(struct mtd_info
*mtd
)
66 return container_of(mtd_to_nand(mtd
), struct nuc900_nand
, chip
);
69 static const struct mtd_partition partitions
[] = {
73 .size
= 8 * 1024 * 1024
77 .offset
= MTDPART_OFS_APPEND
,
78 .size
= MTDPART_SIZ_FULL
82 static unsigned char nuc900_nand_read_byte(struct mtd_info
*mtd
)
85 struct nuc900_nand
*nand
= mtd_to_nuc900(mtd
);
87 ret
= (unsigned char)read_data_reg(nand
);
92 static void nuc900_nand_read_buf(struct mtd_info
*mtd
,
93 unsigned char *buf
, int len
)
96 struct nuc900_nand
*nand
= mtd_to_nuc900(mtd
);
98 for (i
= 0; i
< len
; i
++)
99 buf
[i
] = (unsigned char)read_data_reg(nand
);
102 static void nuc900_nand_write_buf(struct mtd_info
*mtd
,
103 const unsigned char *buf
, int len
)
106 struct nuc900_nand
*nand
= mtd_to_nuc900(mtd
);
108 for (i
= 0; i
< len
; i
++)
109 write_data_reg(nand
, buf
[i
]);
112 static int nuc900_check_rb(struct nuc900_nand
*nand
)
115 spin_lock(&nand
->lock
);
116 val
= __raw_readl(nand
->reg
+ REG_SMISR
);
118 spin_unlock(&nand
->lock
);
123 static int nuc900_nand_devready(struct mtd_info
*mtd
)
125 struct nuc900_nand
*nand
= mtd_to_nuc900(mtd
);
128 ready
= (nuc900_check_rb(nand
)) ? 1 : 0;
132 static void nuc900_nand_command_lp(struct mtd_info
*mtd
, unsigned int command
,
133 int column
, int page_addr
)
135 register struct nand_chip
*chip
= mtd_to_nand(mtd
);
136 struct nuc900_nand
*nand
= mtd_to_nuc900(mtd
);
138 if (command
== NAND_CMD_READOOB
) {
139 column
+= mtd
->writesize
;
140 command
= NAND_CMD_READ0
;
143 write_cmd_reg(nand
, command
& 0xff);
145 if (column
!= -1 || page_addr
!= -1) {
148 if (chip
->options
& NAND_BUSWIDTH_16
&&
149 !nand_opcode_8bits(command
))
151 write_addr_reg(nand
, column
);
152 write_addr_reg(nand
, column
>> 8 | ENDADDR
);
154 if (page_addr
!= -1) {
155 write_addr_reg(nand
, page_addr
);
157 if (chip
->options
& NAND_ROW_ADDR_3
) {
158 write_addr_reg(nand
, page_addr
>> 8);
159 write_addr_reg(nand
, page_addr
>> 16 | ENDADDR
);
161 write_addr_reg(nand
, page_addr
>> 8 | ENDADDR
);
167 case NAND_CMD_CACHEDPROG
:
168 case NAND_CMD_PAGEPROG
:
169 case NAND_CMD_ERASE1
:
170 case NAND_CMD_ERASE2
:
173 case NAND_CMD_STATUS
:
179 udelay(chip
->chip_delay
);
181 write_cmd_reg(nand
, NAND_CMD_STATUS
);
182 write_cmd_reg(nand
, command
);
184 while (!nuc900_check_rb(nand
))
189 case NAND_CMD_RNDOUT
:
190 write_cmd_reg(nand
, NAND_CMD_RNDOUTSTART
);
195 write_cmd_reg(nand
, NAND_CMD_READSTART
);
198 if (!chip
->dev_ready
) {
199 udelay(chip
->chip_delay
);
204 /* Apply this short delay always to ensure that we do wait tWB in
205 * any case on any machine. */
208 while (!chip
->dev_ready(mtd
))
213 static void nuc900_nand_enable(struct nuc900_nand
*nand
)
216 spin_lock(&nand
->lock
);
217 __raw_writel(RESET_FMI
, (nand
->reg
+ REG_FMICSR
));
219 val
= __raw_readl(nand
->reg
+ REG_FMICSR
);
221 if (!(val
& NAND_EN
))
222 __raw_writel(val
| NAND_EN
, nand
->reg
+ REG_FMICSR
);
224 val
= __raw_readl(nand
->reg
+ REG_SMCSR
);
226 val
&= ~(SWRST
|PSIZE
|DMARWEN
|BUSWID
|ECC4EN
|NANDCS
);
229 __raw_writel(val
, nand
->reg
+ REG_SMCSR
);
231 spin_unlock(&nand
->lock
);
234 static int nuc900_nand_probe(struct platform_device
*pdev
)
236 struct nuc900_nand
*nuc900_nand
;
237 struct nand_chip
*chip
;
238 struct mtd_info
*mtd
;
239 struct resource
*res
;
241 nuc900_nand
= devm_kzalloc(&pdev
->dev
, sizeof(struct nuc900_nand
),
245 chip
= &(nuc900_nand
->chip
);
246 mtd
= nand_to_mtd(chip
);
248 mtd
->dev
.parent
= &pdev
->dev
;
249 spin_lock_init(&nuc900_nand
->lock
);
251 nuc900_nand
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
252 if (IS_ERR(nuc900_nand
->clk
))
254 clk_enable(nuc900_nand
->clk
);
256 chip
->cmdfunc
= nuc900_nand_command_lp
;
257 chip
->dev_ready
= nuc900_nand_devready
;
258 chip
->read_byte
= nuc900_nand_read_byte
;
259 chip
->write_buf
= nuc900_nand_write_buf
;
260 chip
->read_buf
= nuc900_nand_read_buf
;
261 chip
->chip_delay
= 50;
263 chip
->ecc
.mode
= NAND_ECC_SOFT
;
264 chip
->ecc
.algo
= NAND_ECC_HAMMING
;
266 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
267 nuc900_nand
->reg
= devm_ioremap_resource(&pdev
->dev
, res
);
268 if (IS_ERR(nuc900_nand
->reg
))
269 return PTR_ERR(nuc900_nand
->reg
);
271 nuc900_nand_enable(nuc900_nand
);
273 if (nand_scan(chip
, 1))
276 mtd_device_register(mtd
, partitions
, ARRAY_SIZE(partitions
));
278 platform_set_drvdata(pdev
, nuc900_nand
);
283 static int nuc900_nand_remove(struct platform_device
*pdev
)
285 struct nuc900_nand
*nuc900_nand
= platform_get_drvdata(pdev
);
287 nand_release(&nuc900_nand
->chip
);
288 clk_disable(nuc900_nand
->clk
);
293 static struct platform_driver nuc900_nand_driver
= {
294 .probe
= nuc900_nand_probe
,
295 .remove
= nuc900_nand_remove
,
297 .name
= "nuc900-fmi",
301 module_platform_driver(nuc900_nand_driver
);
303 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
304 MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
305 MODULE_LICENSE("GPL");
306 MODULE_ALIAS("platform:nuc900-fmi");