2 * NAND flash driver for the MikroTik RouterBOARD 750
4 * Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mtd/nand.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include <asm/mach-ath79/ar71xx_regs.h>
21 #include <asm/mach-ath79/ath79.h>
22 #include <asm/mach-ath79/mach-rb750.h>
24 #define DRV_NAME "rb750-nand"
25 #define DRV_VERSION "0.1.0"
26 #define DRV_DESC "NAND flash driver for the RouterBOARD 750"
28 #define RB750_NAND_IO0 BIT(RB750_GPIO_NAND_IO0)
29 #define RB750_NAND_ALE BIT(RB750_GPIO_NAND_ALE)
30 #define RB750_NAND_CLE BIT(RB750_GPIO_NAND_CLE)
31 #define RB750_NAND_NRE BIT(RB750_GPIO_NAND_NRE)
32 #define RB750_NAND_NWE BIT(RB750_GPIO_NAND_NWE)
33 #define RB750_NAND_RDY BIT(RB750_GPIO_NAND_RDY)
34 #define RB750_NAND_NCE BIT(RB750_GPIO_NAND_NCE)
36 #define RB750_NAND_DATA_SHIFT 1
37 #define RB750_NAND_DATA_BITS (0xff << RB750_NAND_DATA_SHIFT)
38 #define RB750_NAND_INPUT_BITS (RB750_NAND_DATA_BITS | RB750_NAND_RDY)
39 #define RB750_NAND_OUTPUT_BITS (RB750_NAND_ALE | RB750_NAND_CLE | \
40 RB750_NAND_NRE | RB750_NAND_NWE | \
43 struct rb750_nand_info
{
44 struct nand_chip chip
;
49 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
50 * will not be able to find the kernel that we load.
52 static struct nand_ecclayout rb750_nand_ecclayout
= {
54 .eccpos
= { 8, 9, 10, 13, 14, 15 },
56 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
59 static struct mtd_partition rb750_nand_partitions
[] = {
64 .mask_flags
= MTD_WRITEABLE
,
67 .offset
= (256 * 1024),
68 .size
= (4 * 1024 * 1024) - (256 * 1024),
71 .offset
= MTDPART_OFS_NXTBLK
,
72 .size
= MTDPART_SIZ_FULL
,
76 static void rb750_nand_write(const u8
*buf
, unsigned len
)
78 void __iomem
*base
= ath79_gpio_base
;
83 /* set data lines to output mode */
84 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
85 __raw_writel(t
| RB750_NAND_DATA_BITS
, base
+ AR71XX_GPIO_REG_OE
);
87 out
= __raw_readl(base
+ AR71XX_GPIO_REG_OUT
);
88 out
&= ~(RB750_NAND_DATA_BITS
| RB750_NAND_NWE
);
89 for (i
= 0; i
!= len
; i
++) {
93 data
<<= RB750_NAND_DATA_SHIFT
;
95 __raw_writel(data
, base
+ AR71XX_GPIO_REG_OUT
);
97 __raw_writel(data
| RB750_NAND_NWE
, base
+ AR71XX_GPIO_REG_OUT
);
99 __raw_readl(base
+ AR71XX_GPIO_REG_OUT
);
102 /* set data lines to input mode */
103 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
104 __raw_writel(t
& ~RB750_NAND_DATA_BITS
, base
+ AR71XX_GPIO_REG_OE
);
106 __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
109 static int rb750_nand_read_verify(u8
*read_buf
, unsigned len
,
110 const u8
*verify_buf
)
112 void __iomem
*base
= ath79_gpio_base
;
115 for (i
= 0; i
< len
; i
++) {
118 /* activate RE line */
119 __raw_writel(RB750_NAND_NRE
, base
+ AR71XX_GPIO_REG_CLEAR
);
121 __raw_readl(base
+ AR71XX_GPIO_REG_CLEAR
);
123 /* read input lines */
124 data
= __raw_readl(base
+ AR71XX_GPIO_REG_IN
) >>
125 RB750_NAND_DATA_SHIFT
;
127 /* deactivate RE line */
128 __raw_writel(RB750_NAND_NRE
, base
+ AR71XX_GPIO_REG_SET
);
132 else if (verify_buf
&& verify_buf
[i
] != data
)
139 static void rb750_nand_select_chip(struct mtd_info
*mtd
, int chip
)
141 void __iomem
*base
= ath79_gpio_base
;
145 func
= __raw_readl(base
+ AR71XX_GPIO_REG_FUNC
);
148 rb750_latch_change(RB750_LVC573_LE
, 0);
150 rb750_nand_pins_enable();
152 /* set input mode for data lines */
153 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
154 __raw_writel(t
& ~RB750_NAND_INPUT_BITS
,
155 base
+ AR71XX_GPIO_REG_OE
);
157 /* deactivate RE and WE lines */
158 __raw_writel(RB750_NAND_NRE
| RB750_NAND_NWE
,
159 base
+ AR71XX_GPIO_REG_SET
);
161 (void) __raw_readl(base
+ AR71XX_GPIO_REG_SET
);
163 /* activate CE line */
164 __raw_writel(RB750_NAND_NCE
, base
+ AR71XX_GPIO_REG_CLEAR
);
166 /* deactivate CE line */
167 __raw_writel(RB750_NAND_NCE
, base
+ AR71XX_GPIO_REG_SET
);
169 (void) __raw_readl(base
+ AR71XX_GPIO_REG_SET
);
171 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
172 __raw_writel(t
| RB750_NAND_IO0
| RB750_NAND_RDY
,
173 base
+ AR71XX_GPIO_REG_OE
);
175 rb750_nand_pins_disable();
178 rb750_latch_change(0, RB750_LVC573_LE
);
182 static int rb750_nand_dev_ready(struct mtd_info
*mtd
)
184 void __iomem
*base
= ath79_gpio_base
;
186 return !!(__raw_readl(base
+ AR71XX_GPIO_REG_IN
) & RB750_NAND_RDY
);
189 static void rb750_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
192 if (ctrl
& NAND_CTRL_CHANGE
) {
193 void __iomem
*base
= ath79_gpio_base
;
196 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OUT
);
198 t
&= ~(RB750_NAND_CLE
| RB750_NAND_ALE
);
199 t
|= (ctrl
& NAND_CLE
) ? RB750_NAND_CLE
: 0;
200 t
|= (ctrl
& NAND_ALE
) ? RB750_NAND_ALE
: 0;
202 __raw_writel(t
, base
+ AR71XX_GPIO_REG_OUT
);
204 __raw_readl(base
+ AR71XX_GPIO_REG_OUT
);
207 if (cmd
!= NAND_CMD_NONE
) {
209 rb750_nand_write(&t
, 1);
213 static u8
rb750_nand_read_byte(struct mtd_info
*mtd
)
216 rb750_nand_read_verify(&data
, 1, NULL
);
220 static void rb750_nand_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
222 rb750_nand_read_verify(buf
, len
, NULL
);
225 static void rb750_nand_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
227 rb750_nand_write(buf
, len
);
230 static int rb750_nand_verify_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
232 return rb750_nand_read_verify(NULL
, len
, buf
);
235 static void __init
rb750_nand_gpio_init(void)
237 void __iomem
*base
= ath79_gpio_base
;
241 out
= __raw_readl(base
+ AR71XX_GPIO_REG_OUT
);
243 /* setup output levels */
244 __raw_writel(RB750_NAND_NCE
| RB750_NAND_NRE
| RB750_NAND_NWE
,
245 base
+ AR71XX_GPIO_REG_SET
);
247 __raw_writel(RB750_NAND_ALE
| RB750_NAND_CLE
,
248 base
+ AR71XX_GPIO_REG_CLEAR
);
250 /* setup input lines */
251 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
252 __raw_writel(t
& ~(RB750_NAND_INPUT_BITS
), base
+ AR71XX_GPIO_REG_OE
);
254 /* setup output lines */
255 t
= __raw_readl(base
+ AR71XX_GPIO_REG_OE
);
256 __raw_writel(t
| RB750_NAND_OUTPUT_BITS
, base
+ AR71XX_GPIO_REG_OE
);
258 rb750_latch_change(~out
& RB750_NAND_IO0
, out
& RB750_NAND_IO0
);
261 static int __devinit
rb750_nand_probe(struct platform_device
*pdev
)
263 struct rb750_nand_info
*info
;
266 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
268 rb750_nand_gpio_init();
270 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
274 info
->chip
.priv
= &info
;
275 info
->mtd
.priv
= &info
->chip
;
276 info
->mtd
.owner
= THIS_MODULE
;
278 info
->chip
.select_chip
= rb750_nand_select_chip
;
279 info
->chip
.cmd_ctrl
= rb750_nand_cmd_ctrl
;
280 info
->chip
.dev_ready
= rb750_nand_dev_ready
;
281 info
->chip
.read_byte
= rb750_nand_read_byte
;
282 info
->chip
.write_buf
= rb750_nand_write_buf
;
283 info
->chip
.read_buf
= rb750_nand_read_buf
;
284 info
->chip
.verify_buf
= rb750_nand_verify_buf
;
286 info
->chip
.chip_delay
= 25;
287 info
->chip
.ecc
.mode
= NAND_ECC_SOFT
;
288 info
->chip
.options
|= NAND_NO_AUTOINCR
;
290 platform_set_drvdata(pdev
, info
);
292 ret
= nand_scan_ident(&info
->mtd
, 1, NULL
);
298 if (info
->mtd
.writesize
== 512)
299 info
->chip
.ecc
.layout
= &rb750_nand_ecclayout
;
301 ret
= nand_scan_tail(&info
->mtd
);
304 goto err_set_drvdata
;
307 ret
= mtd_device_register(&info
->mtd
, rb750_nand_partitions
,
308 ARRAY_SIZE(rb750_nand_partitions
));
310 goto err_release_nand
;
315 nand_release(&info
->mtd
);
317 platform_set_drvdata(pdev
, NULL
);
323 static int __devexit
rb750_nand_remove(struct platform_device
*pdev
)
325 struct rb750_nand_info
*info
= platform_get_drvdata(pdev
);
327 nand_release(&info
->mtd
);
328 platform_set_drvdata(pdev
, NULL
);
334 static struct platform_driver rb750_nand_driver
= {
335 .probe
= rb750_nand_probe
,
336 .remove
= __devexit_p(rb750_nand_remove
),
339 .owner
= THIS_MODULE
,
343 static int __init
rb750_nand_init(void)
345 return platform_driver_register(&rb750_nand_driver
);
348 static void __exit
rb750_nand_exit(void)
350 platform_driver_unregister(&rb750_nand_driver
);
353 module_init(rb750_nand_init
);
354 module_exit(rb750_nand_exit
);
356 MODULE_DESCRIPTION(DRV_DESC
);
357 MODULE_VERSION(DRV_VERSION
);
358 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
359 MODULE_LICENSE("GPL v2");