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>
24 #include <asm/mach-ar71xx/ar71xx.h>
26 #define DRV_NAME "rb4xx-nand"
27 #define DRV_VERSION "0.1.9"
28 #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
30 #define USE_FAST_READ 1
31 #define USE_FAST_WRITE 1
32 #undef RB4XX_NAND_DEBUG
34 #ifdef RB4XX_NAND_DEBUG
35 #define DBG(fmt, arg...) printk(KERN_DEBUG DRV_NAME ": " fmt, ## arg)
37 #define DBG(fmt, arg...) do {} while (0)
40 #define RB4XX_NAND_GPIO_RDY 5
41 #define RB4XX_FLASH_HZ 33333334
42 #define RB4XX_NAND_HZ 33333334
44 #define SPI_CTRL_FASTEST 0x40
45 #define SPI_CTRL_SAFE 0x43 /* 25 MHz for AHB 200 MHz */
46 #define SBIT_IOC_BASE SPI_IOC_CS1
47 #define SBIT_IOC_DO_SHIFT 0
48 #define SBIT_IOC_DO (1u << SBIT_IOC_DO_SHIFT)
49 #define SBIT_IOC_DO2_SHIFT 18
50 #define SBIT_IOC_DO2 (1u << SBIT_IOC_DO2_SHIFT)
52 #define CPLD_CMD_WRITE_MULT 0x08 /* send cmd, n x send data, read data */
53 #define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
54 #define CPLD_CMD_READ_MULT 0x0a /* send cmd, send idle, n x read data */
55 #define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
57 #define CFG_BIT_nCE 0x80
58 #define CFG_BIT_CLE 0x40
59 #define CFG_BIT_ALE 0x20
60 #define CFG_BIT_FAN 0x10
61 #define CFG_BIT_nLED4 0x08
62 #define CFG_BIT_nLED3 0x04
63 #define CFG_BIT_nLED2 0x02
64 #define CFG_BIT_nLED1 0x01
66 #define CFG_BIT_nLEDS \
67 (CFG_BIT_nLED1 | CFG_BIT_nLED2 | CFG_BIT_nLED3 | CFG_BIT_nLED4)
69 struct rb4xx_nand_info
{
70 struct nand_chip chip
;
75 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
76 * will not be able to find the kernel that we load.
78 static struct nand_ecclayout rb4xx_nand_ecclayout
= {
80 .eccpos
= { 8, 9, 10, 13, 14, 15 },
82 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
85 static struct mtd_partition rb4xx_nand_partitions
[] = {
88 .offset
= (256 * 1024),
89 .size
= (4 * 1024 * 1024) - (256 * 1024),
93 .offset
= MTDPART_OFS_NXTBLK
,
94 .size
= MTDPART_SIZ_FULL
,
100 .mask_flags
= MTD_WRITEABLE
,
105 #define SPI_NDATA_BASE 0x00800000
106 static unsigned spi_ctrl_fread
= SPI_CTRL_SAFE
;
107 static unsigned spi_ctrl_flash
= SPI_CTRL_SAFE
;
108 extern unsigned mips_hpt_frequency
;
111 static inline unsigned rb4xx_spi_rreg(unsigned r
)
113 return __raw_readl((void * __iomem
)(KSEG1ADDR(AR71XX_SPI_BASE
) + r
));
116 static inline void rb4xx_spi_wreg(unsigned r
, unsigned v
)
118 __raw_writel(v
, (void * __iomem
)(KSEG1ADDR(AR71XX_SPI_BASE
) + r
));
121 static inline void do_spi_clk(int bit
)
123 unsigned bval
= SBIT_IOC_BASE
| (bit
& 1);
125 rb4xx_spi_wreg(SPI_REG_IOC
, bval
);
126 rb4xx_spi_wreg(SPI_REG_IOC
, bval
| SPI_IOC_CLK
);
129 static void do_spi_byte(uint8_t byte
)
131 do_spi_clk(byte
>> 7);
132 do_spi_clk(byte
>> 6);
133 do_spi_clk(byte
>> 5);
134 do_spi_clk(byte
>> 4);
135 do_spi_clk(byte
>> 3);
136 do_spi_clk(byte
>> 2);
137 do_spi_clk(byte
>> 1);
140 DBG("spi_byte sent 0x%02x got 0x%x\n",
141 byte
, rb4xx_spi_rreg(SPI_REG_RDS
));
145 static inline void do_spi_clk_fast(int bit1
, int bit2
)
147 unsigned bval
= (SBIT_IOC_BASE
|
148 ((bit1
<< SBIT_IOC_DO_SHIFT
) & SBIT_IOC_DO
) |
149 ((bit2
<< SBIT_IOC_DO2_SHIFT
) & SBIT_IOC_DO2
));
151 rb4xx_spi_wreg(SPI_REG_IOC
, bval
);
152 rb4xx_spi_wreg(SPI_REG_IOC
, bval
| SPI_IOC_CLK
);
155 static inline void do_spi_byte_fast(uint8_t byte
)
157 do_spi_clk_fast(byte
>> 7, byte
>> 6);
158 do_spi_clk_fast(byte
>> 5, byte
>> 4);
159 do_spi_clk_fast(byte
>> 3, byte
>> 2);
160 do_spi_clk_fast(byte
>> 1, byte
>> 0);
162 DBG("spi_byte_fast sent 0x%02x got 0x%x\n",
163 byte
, rb4xx_spi_rreg(SPI_REG_RDS
));
166 static inline void do_spi_byte_fast(uint8_t byte
)
170 #endif /* USE_FAST_WRITE */
172 static int do_spi_cmd(unsigned cmd
, unsigned sendCnt
, const uint8_t *sendData
,
173 unsigned recvCnt
, uint8_t *recvData
,
174 const uint8_t *verifyData
, int fastWrite
)
178 DBG("SPI cmd 0x%x send %u recv %u\n", cmd
, sendCnt
, recvCnt
);
180 rb4xx_spi_wreg(SPI_REG_FS
, SPI_FS_GPIO
);
181 rb4xx_spi_wreg(SPI_REG_CTRL
, SPI_CTRL_FASTEST
);
185 if (cmd
== CPLD_CMD_READ_FAST
) {
191 for (i
= 0; i
< sendCnt
; ++i
) {
193 do_spi_byte_fast(sendData
[i
]);
195 do_spi_byte(sendData
[i
]);
198 for (i
= 0; i
< recvCnt
; ++i
) {
205 recvData
[i
] = rb4xx_spi_rreg(SPI_REG_RDS
) & 0xff;
206 } else if (verifyData
) {
207 if (verifyData
[i
] != (rb4xx_spi_rreg(SPI_REG_RDS
)
213 rb4xx_spi_wreg(SPI_REG_IOC
, SBIT_IOC_BASE
| SPI_IOC_CS0
);
214 rb4xx_spi_wreg(SPI_REG_CTRL
, spi_ctrl_flash
);
215 rb4xx_spi_wreg(SPI_REG_FS
, 0);
220 static int got_write
= 1;
222 static void rb4xx_nand_write_data(const uint8_t *byte
, unsigned cnt
)
224 do_spi_cmd(CPLD_CMD_WRITE_MULT
, cnt
, byte
, 1, NULL
, NULL
, 1);
228 static void rb4xx_nand_write_byte(uint8_t byte
)
230 rb4xx_nand_write_data(&byte
, 1);
234 static uint8_t *rb4xx_nand_read_getaddr(unsigned cnt
)
236 static unsigned nboffset
= 0x100000;
240 nboffset
= (nboffset
+ 31) & ~31;
241 if (nboffset
>= 0x100000) /* 1MB */
245 rb4xx_spi_wreg(SPI_REG_FS
, SPI_FS_GPIO
);
246 rb4xx_spi_wreg(SPI_REG_CTRL
, spi_ctrl_fread
);
247 rb4xx_spi_wreg(SPI_REG_FS
, 0);
250 addr
= KSEG1ADDR(AR71XX_SPI_BASE
+ SPI_NDATA_BASE
) + nboffset
;
251 DBG("rb4xx_nand_read_getaddr 0x%x cnt 0x%x\n", addr
, cnt
);
254 return (uint8_t *)addr
;
257 static void rb4xx_nand_read_data(uint8_t *buf
, unsigned cnt
)
259 unsigned size32
= cnt
& ~31;
260 unsigned remain
= cnt
& 31;
263 uint8_t *addr
= rb4xx_nand_read_getaddr(size32
);
264 memcpy(buf
, (void *)addr
, size32
);
268 do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, remain
,
269 buf
+ size32
, NULL
, 0);
273 static int rb4xx_nand_verify_data(const uint8_t *buf
, unsigned cnt
)
275 unsigned size32
= cnt
& ~31;
276 unsigned remain
= cnt
& 31;
279 uint8_t *addr
= rb4xx_nand_read_getaddr(size32
);
280 if (memcmp(buf
, (void *)addr
, size32
) != 0)
285 return do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, remain
,
286 NULL
, buf
+ size32
, 0);
290 #else /* USE_FAST_READ */
291 static void rb4xx_nand_read_data(uint8_t *buf
, unsigned cnt
)
293 do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, cnt
, buf
, NULL
, 0);
296 static int rb4xx_nand_verify_data(const uint8_t *buf
, unsigned cnt
)
298 return do_spi_cmd(CPLD_CMD_READ_MULT
, 1, buf
, cnt
, NULL
, buf
, 0);
300 #endif /* USE_FAST_READ */
302 static void rb4xx_nand_write_cfg(uint8_t byte
)
304 do_spi_cmd(CPLD_CMD_WRITE_CFG
, 1, &byte
, 0, NULL
, NULL
, 0);
308 static int rb4xx_nand_dev_ready(struct mtd_info
*mtd
)
310 return gpio_get_value(RB4XX_NAND_GPIO_RDY
);
313 static void rb4xx_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
316 if (ctrl
& NAND_CTRL_CHANGE
) {
317 uint8_t cfg
= CFG_BIT_nLEDS
;
319 cfg
|= (ctrl
& NAND_CLE
) ? CFG_BIT_CLE
: 0;
320 cfg
|= (ctrl
& NAND_ALE
) ? CFG_BIT_ALE
: 0;
321 cfg
|= (ctrl
& NAND_NCE
) ? 0 : CFG_BIT_nCE
;
323 rb4xx_nand_write_cfg(cfg
);
326 if (cmd
!= NAND_CMD_NONE
)
327 rb4xx_nand_write_byte(cmd
);
330 static uint8_t rb4xx_nand_read_byte(struct mtd_info
*mtd
)
334 rb4xx_nand_read_data(&byte
, 1);
338 static void rb4xx_nand_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
341 rb4xx_nand_write_data(buf
, len
);
344 static void rb4xx_nand_read_buf(struct mtd_info
*mtd
, uint8_t *buf
,
347 rb4xx_nand_read_data(buf
, len
);
350 static int rb4xx_nand_verify_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
353 if (!rb4xx_nand_verify_data(buf
, len
))
359 static unsigned get_spi_ctrl(unsigned hz_max
, const char *name
)
363 div
= (ar71xx_ahb_freq
- 1) / (2 * hz_max
);
365 * CPU has a bug at (div == 0) - first bit read is random
371 unsigned ahb_khz
= (ar71xx_ahb_freq
+ 500) / 1000;
372 unsigned div_real
= 2 * (div
+ 1);
373 printk(KERN_INFO
"%s SPI clock %u kHz (AHB %u kHz / %u)\n",
379 return SPI_CTRL_FASTEST
+ div
;
382 static int __init
rb4xx_nand_probe(struct platform_device
*pdev
)
384 struct rb4xx_nand_info
*info
;
387 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
389 ret
= gpio_request(RB4XX_NAND_GPIO_RDY
, "NAND RDY");
391 printk(KERN_ERR
"rb4xx-nand: gpio request failed\n");
395 ret
= gpio_direction_input(RB4XX_NAND_GPIO_RDY
);
397 printk(KERN_ERR
"rb4xx-nand: unable to set input mode "
398 "on gpio%d\n", RB4XX_NAND_GPIO_RDY
);
402 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
404 printk(KERN_ERR
"rb4xx-nand: no memory for private data\n");
410 spi_ctrl_fread
= get_spi_ctrl(RB4XX_NAND_HZ
, "NAND");
412 spi_ctrl_flash
= get_spi_ctrl(RB4XX_FLASH_HZ
, "FLASH");
414 rb4xx_nand_write_cfg(CFG_BIT_nLEDS
| CFG_BIT_nCE
);
416 info
->chip
.priv
= &info
;
417 info
->mtd
.priv
= &info
->chip
;
418 info
->mtd
.owner
= THIS_MODULE
;
420 info
->chip
.cmd_ctrl
= rb4xx_nand_cmd_ctrl
;
421 info
->chip
.dev_ready
= rb4xx_nand_dev_ready
;
422 info
->chip
.read_byte
= rb4xx_nand_read_byte
;
423 info
->chip
.write_buf
= rb4xx_nand_write_buf
;
424 info
->chip
.read_buf
= rb4xx_nand_read_buf
;
425 info
->chip
.verify_buf
= rb4xx_nand_verify_buf
;
427 info
->chip
.chip_delay
= 25;
428 info
->chip
.ecc
.mode
= NAND_ECC_SOFT
;
429 info
->chip
.options
|= NAND_NO_AUTOINCR
;
431 platform_set_drvdata(pdev
, info
);
433 ret
= nand_scan_ident(&info
->mtd
, 1);
439 if (info
->mtd
.writesize
== 512)
440 info
->chip
.ecc
.layout
= &rb4xx_nand_ecclayout
;
442 ret
= nand_scan_tail(&info
->mtd
);
445 goto err_set_drvdata
;
448 #ifdef CONFIG_MTD_PARTITIONS
449 ret
= add_mtd_partitions(&info
->mtd
, rb4xx_nand_partitions
,
450 ARRAY_SIZE(rb4xx_nand_partitions
));
452 ret
= add_mtd_device(&info
->mtd
);
455 goto err_release_nand
;
460 nand_release(&info
->mtd
);
462 platform_set_drvdata(pdev
, NULL
);
466 gpio_free(RB4XX_NAND_GPIO_RDY
);
470 static int __devexit
rb4xx_nand_remove(struct platform_device
*pdev
)
472 struct rb4xx_nand_info
*info
= platform_get_drvdata(pdev
);
474 nand_release(&info
->mtd
);
475 platform_set_drvdata(pdev
, NULL
);
481 static struct platform_driver rb4xx_nand_driver
= {
482 .probe
= rb4xx_nand_probe
,
483 .remove
= __devexit_p(rb4xx_nand_remove
),
486 .owner
= THIS_MODULE
,
490 static int __init
rb4xx_nand_init(void)
492 return platform_driver_register(&rb4xx_nand_driver
);
495 static void __exit
rb4xx_nand_exit(void)
497 platform_driver_unregister(&rb4xx_nand_driver
);
500 module_init(rb4xx_nand_init
);
501 module_exit(rb4xx_nand_exit
);
503 MODULE_DESCRIPTION(DRV_DESC
);
504 MODULE_VERSION(DRV_VERSION
);
505 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
506 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
507 MODULE_LICENSE("GPL v2");