2 * NAND flash driver for the MikroTik RouterBoard 4xx series
4 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * This file was based on the driver for Linux 2.6.22 published by
8 * MikroTik for their RouterBoard 4xx series devices.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/mtd/nand.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
25 #include <asm/mach-ar71xx/ar71xx.h>
27 #define DRV_NAME "rb4xx-nand"
28 #define DRV_VERSION "0.1.10"
29 #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
31 #define USE_FAST_READ 1
32 #define USE_FAST_WRITE 1
33 #undef RB4XX_NAND_DEBUG
35 #ifdef RB4XX_NAND_DEBUG
36 #define DBG(fmt, arg...) printk(KERN_DEBUG DRV_NAME ": " fmt, ## arg)
38 #define DBG(fmt, arg...) do {} while (0)
41 #define RB4XX_NAND_GPIO_RDY 5
42 #define RB4XX_FLASH_HZ 33333334
43 #define RB4XX_NAND_HZ 33333334
45 #define SPI_CTRL_FASTEST 0x40
46 #define SPI_CTRL_SAFE 0x43 /* 25 MHz for AHB 200 MHz */
47 #define SBIT_IOC_BASE SPI_IOC_CS1
48 #define SBIT_IOC_DO_SHIFT 0
49 #define SBIT_IOC_DO (1u << SBIT_IOC_DO_SHIFT)
50 #define SBIT_IOC_DO2_SHIFT 18
51 #define SBIT_IOC_DO2 (1u << SBIT_IOC_DO2_SHIFT)
53 #define CPLD_CMD_WRITE_MULT 0x08 /* send cmd, n x send data, read data */
54 #define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
55 #define CPLD_CMD_READ_MULT 0x0a /* send cmd, send idle, n x read data */
56 #define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
58 #define CFG_BIT_nCE 0x80
59 #define CFG_BIT_CLE 0x40
60 #define CFG_BIT_ALE 0x20
61 #define CFG_BIT_FAN 0x10
62 #define CFG_BIT_nLED4 0x08
63 #define CFG_BIT_nLED3 0x04
64 #define CFG_BIT_nLED2 0x02
65 #define CFG_BIT_nLED1 0x01
67 #define CFG_BIT_nLEDS \
68 (CFG_BIT_nLED1 | CFG_BIT_nLED2 | CFG_BIT_nLED3 | CFG_BIT_nLED4)
70 struct rb4xx_nand_info
{
71 struct nand_chip chip
;
76 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
77 * will not be able to find the kernel that we load.
79 static struct nand_ecclayout rb4xx_nand_ecclayout
= {
81 .eccpos
= { 8, 9, 10, 13, 14, 15 },
83 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
86 static struct mtd_partition rb4xx_nand_partitions
[] = {
91 .mask_flags
= MTD_WRITEABLE
,
95 .offset
= (256 * 1024),
96 .size
= (4 * 1024 * 1024) - (256 * 1024),
100 .offset
= MTDPART_OFS_NXTBLK
,
101 .size
= MTDPART_SIZ_FULL
,
106 #define SPI_NDATA_BASE 0x00800000
107 static unsigned spi_ctrl_fread
= SPI_CTRL_SAFE
;
108 static unsigned spi_ctrl_flash
= SPI_CTRL_SAFE
;
109 extern unsigned mips_hpt_frequency
;
112 static inline unsigned rb4xx_spi_rreg(unsigned r
)
114 return __raw_readl((void * __iomem
)(KSEG1ADDR(AR71XX_SPI_BASE
) + r
));
117 static inline void rb4xx_spi_wreg(unsigned r
, unsigned v
)
119 __raw_writel(v
, (void * __iomem
)(KSEG1ADDR(AR71XX_SPI_BASE
) + r
));
122 static inline void do_spi_clk(int bit
)
124 unsigned bval
= SBIT_IOC_BASE
| (bit
& 1);
126 rb4xx_spi_wreg(SPI_REG_IOC
, bval
);
127 rb4xx_spi_wreg(SPI_REG_IOC
, bval
| SPI_IOC_CLK
);
130 static void do_spi_byte(uint8_t byte
)
132 do_spi_clk(byte
>> 7);
133 do_spi_clk(byte
>> 6);
134 do_spi_clk(byte
>> 5);
135 do_spi_clk(byte
>> 4);
136 do_spi_clk(byte
>> 3);
137 do_spi_clk(byte
>> 2);
138 do_spi_clk(byte
>> 1);
141 DBG("spi_byte sent 0x%02x got 0x%x\n",
142 byte
, rb4xx_spi_rreg(SPI_REG_RDS
));
146 static inline void do_spi_clk_fast(int bit1
, int bit2
)
148 unsigned bval
= (SBIT_IOC_BASE
|
149 ((bit1
<< SBIT_IOC_DO_SHIFT
) & SBIT_IOC_DO
) |
150 ((bit2
<< SBIT_IOC_DO2_SHIFT
) & SBIT_IOC_DO2
));
152 rb4xx_spi_wreg(SPI_REG_IOC
, bval
);
153 rb4xx_spi_wreg(SPI_REG_IOC
, bval
| SPI_IOC_CLK
);
156 static inline void do_spi_byte_fast(uint8_t byte
)
158 do_spi_clk_fast(byte
>> 7, byte
>> 6);
159 do_spi_clk_fast(byte
>> 5, byte
>> 4);
160 do_spi_clk_fast(byte
>> 3, byte
>> 2);
161 do_spi_clk_fast(byte
>> 1, byte
>> 0);
163 DBG("spi_byte_fast sent 0x%02x got 0x%x\n",
164 byte
, rb4xx_spi_rreg(SPI_REG_RDS
));
167 static inline void do_spi_byte_fast(uint8_t byte
)
171 #endif /* USE_FAST_WRITE */
173 static int do_spi_cmd(unsigned cmd
, unsigned sendCnt
, const uint8_t *sendData
,
174 unsigned recvCnt
, uint8_t *recvData
,
175 const uint8_t *verifyData
, int fastWrite
)
179 DBG("SPI cmd 0x%x send %u recv %u\n", cmd
, sendCnt
, recvCnt
);
181 rb4xx_spi_wreg(SPI_REG_FS
, SPI_FS_GPIO
);
182 rb4xx_spi_wreg(SPI_REG_CTRL
, SPI_CTRL_FASTEST
);
186 if (cmd
== CPLD_CMD_READ_FAST
) {
192 for (i
= 0; i
< sendCnt
; ++i
) {
194 do_spi_byte_fast(sendData
[i
]);
196 do_spi_byte(sendData
[i
]);
199 for (i
= 0; i
< recvCnt
; ++i
) {
206 recvData
[i
] = rb4xx_spi_rreg(SPI_REG_RDS
) & 0xff;
207 } else if (verifyData
) {
208 if (verifyData
[i
] != (rb4xx_spi_rreg(SPI_REG_RDS
)
214 rb4xx_spi_wreg(SPI_REG_IOC
, SBIT_IOC_BASE
| SPI_IOC_CS0
);
215 rb4xx_spi_wreg(SPI_REG_CTRL
, spi_ctrl_flash
);
216 rb4xx_spi_wreg(SPI_REG_FS
, 0);
221 static int got_write
= 1;
223 static void rb4xx_nand_write_data(const uint8_t *byte
, unsigned cnt
)
225 do_spi_cmd(CPLD_CMD_WRITE_MULT
, cnt
, byte
, 1, NULL
, NULL
, 1);
229 static void rb4xx_nand_write_byte(uint8_t byte
)
231 rb4xx_nand_write_data(&byte
, 1);
235 static uint8_t *rb4xx_nand_read_getaddr(unsigned cnt
)
237 static unsigned nboffset
= 0x100000;
241 nboffset
= (nboffset
+ 31) & ~31;
242 if (nboffset
>= 0x100000) /* 1MB */
246 rb4xx_spi_wreg(SPI_REG_FS
, SPI_FS_GPIO
);
247 rb4xx_spi_wreg(SPI_REG_CTRL
, spi_ctrl_fread
);
248 rb4xx_spi_wreg(SPI_REG_FS
, 0);
251 addr
= KSEG1ADDR(AR71XX_SPI_BASE
+ SPI_NDATA_BASE
) + nboffset
;
252 DBG("rb4xx_nand_read_getaddr 0x%x cnt 0x%x\n", addr
, cnt
);
255 return (uint8_t *)addr
;
258 static void rb4xx_nand_read_data(uint8_t *buf
, unsigned cnt
)
260 unsigned size32
= cnt
& ~31;
261 unsigned remain
= cnt
& 31;
264 uint8_t *addr
= rb4xx_nand_read_getaddr(size32
);
265 memcpy(buf
, (void *)addr
, size32
);
269 do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, remain
,
270 buf
+ size32
, NULL
, 0);
274 static int rb4xx_nand_verify_data(const uint8_t *buf
, unsigned cnt
)
276 unsigned size32
= cnt
& ~31;
277 unsigned remain
= cnt
& 31;
280 uint8_t *addr
= rb4xx_nand_read_getaddr(size32
);
281 if (memcmp(buf
, (void *)addr
, size32
) != 0)
286 return do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, remain
,
287 NULL
, buf
+ size32
, 0);
291 #else /* USE_FAST_READ */
292 static void rb4xx_nand_read_data(uint8_t *buf
, unsigned cnt
)
294 do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, cnt
, buf
, NULL
, 0);
297 static int rb4xx_nand_verify_data(const uint8_t *buf
, unsigned cnt
)
299 return do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, cnt
, NULL
, buf
, 0);
301 #endif /* USE_FAST_READ */
303 static void rb4xx_nand_write_cfg(uint8_t byte
)
305 do_spi_cmd(CPLD_CMD_WRITE_CFG
, 1, &byte
, 0, NULL
, NULL
, 0);
309 static int rb4xx_nand_dev_ready(struct mtd_info
*mtd
)
311 return gpio_get_value(RB4XX_NAND_GPIO_RDY
);
314 static void rb4xx_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
317 if (ctrl
& NAND_CTRL_CHANGE
) {
318 uint8_t cfg
= CFG_BIT_nLEDS
;
320 cfg
|= (ctrl
& NAND_CLE
) ? CFG_BIT_CLE
: 0;
321 cfg
|= (ctrl
& NAND_ALE
) ? CFG_BIT_ALE
: 0;
322 cfg
|= (ctrl
& NAND_NCE
) ? 0 : CFG_BIT_nCE
;
324 rb4xx_nand_write_cfg(cfg
);
327 if (cmd
!= NAND_CMD_NONE
)
328 rb4xx_nand_write_byte(cmd
);
331 static uint8_t rb4xx_nand_read_byte(struct mtd_info
*mtd
)
335 rb4xx_nand_read_data(&byte
, 1);
339 static void rb4xx_nand_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
342 rb4xx_nand_write_data(buf
, len
);
345 static void rb4xx_nand_read_buf(struct mtd_info
*mtd
, uint8_t *buf
,
348 rb4xx_nand_read_data(buf
, len
);
351 static int rb4xx_nand_verify_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
354 if (!rb4xx_nand_verify_data(buf
, len
))
360 static unsigned get_spi_ctrl(unsigned hz_max
, const char *name
)
364 div
= (ar71xx_ahb_freq
- 1) / (2 * hz_max
);
366 * CPU has a bug at (div == 0) - first bit read is random
372 unsigned ahb_khz
= (ar71xx_ahb_freq
+ 500) / 1000;
373 unsigned div_real
= 2 * (div
+ 1);
374 printk(KERN_INFO
"%s SPI clock %u kHz (AHB %u kHz / %u)\n",
380 return SPI_CTRL_FASTEST
+ div
;
383 static int __init
rb4xx_nand_probe(struct platform_device
*pdev
)
385 struct rb4xx_nand_info
*info
;
388 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
390 ret
= gpio_request(RB4XX_NAND_GPIO_RDY
, "NAND RDY");
392 printk(KERN_ERR
"rb4xx-nand: gpio request failed\n");
396 ret
= gpio_direction_input(RB4XX_NAND_GPIO_RDY
);
398 printk(KERN_ERR
"rb4xx-nand: unable to set input mode "
399 "on gpio%d\n", RB4XX_NAND_GPIO_RDY
);
403 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
405 printk(KERN_ERR
"rb4xx-nand: no memory for private data\n");
411 spi_ctrl_fread
= get_spi_ctrl(RB4XX_NAND_HZ
, "NAND");
413 spi_ctrl_flash
= get_spi_ctrl(RB4XX_FLASH_HZ
, "FLASH");
415 rb4xx_nand_write_cfg(CFG_BIT_nLEDS
| CFG_BIT_nCE
);
417 info
->chip
.priv
= &info
;
418 info
->mtd
.priv
= &info
->chip
;
419 info
->mtd
.owner
= THIS_MODULE
;
421 info
->chip
.cmd_ctrl
= rb4xx_nand_cmd_ctrl
;
422 info
->chip
.dev_ready
= rb4xx_nand_dev_ready
;
423 info
->chip
.read_byte
= rb4xx_nand_read_byte
;
424 info
->chip
.write_buf
= rb4xx_nand_write_buf
;
425 info
->chip
.read_buf
= rb4xx_nand_read_buf
;
426 info
->chip
.verify_buf
= rb4xx_nand_verify_buf
;
428 info
->chip
.chip_delay
= 25;
429 info
->chip
.ecc
.mode
= NAND_ECC_SOFT
;
430 info
->chip
.options
|= NAND_NO_AUTOINCR
;
432 platform_set_drvdata(pdev
, info
);
434 ret
= nand_scan_ident(&info
->mtd
, 1);
440 if (info
->mtd
.writesize
== 512)
441 info
->chip
.ecc
.layout
= &rb4xx_nand_ecclayout
;
443 ret
= nand_scan_tail(&info
->mtd
);
446 goto err_set_drvdata
;
449 #ifdef CONFIG_MTD_PARTITIONS
450 ret
= add_mtd_partitions(&info
->mtd
, rb4xx_nand_partitions
,
451 ARRAY_SIZE(rb4xx_nand_partitions
));
453 ret
= add_mtd_device(&info
->mtd
);
456 goto err_release_nand
;
461 nand_release(&info
->mtd
);
463 platform_set_drvdata(pdev
, NULL
);
467 gpio_free(RB4XX_NAND_GPIO_RDY
);
471 static int __devexit
rb4xx_nand_remove(struct platform_device
*pdev
)
473 struct rb4xx_nand_info
*info
= platform_get_drvdata(pdev
);
475 nand_release(&info
->mtd
);
476 platform_set_drvdata(pdev
, NULL
);
482 static struct platform_driver rb4xx_nand_driver
= {
483 .probe
= rb4xx_nand_probe
,
484 .remove
= __devexit_p(rb4xx_nand_remove
),
487 .owner
= THIS_MODULE
,
491 static int __init
rb4xx_nand_init(void)
493 return platform_driver_register(&rb4xx_nand_driver
);
496 static void __exit
rb4xx_nand_exit(void)
498 platform_driver_unregister(&rb4xx_nand_driver
);
501 module_init(rb4xx_nand_init
);
502 module_exit(rb4xx_nand_exit
);
504 MODULE_DESCRIPTION(DRV_DESC
);
505 MODULE_VERSION(DRV_VERSION
);
506 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
507 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
508 MODULE_LICENSE("GPL v2");