2 * TXx9 NAND flash memory controller driver
3 * Based on RBTX49xx patch from CELF patch archive.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * (C) Copyright TOSHIBA CORPORATION 2004-2007
10 * All Rights Reserved.
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/nand.h>
19 #include <linux/mtd/nand_ecc.h>
20 #include <linux/mtd/partitions.h>
22 #include <asm/txx9/ndfmc.h>
24 /* TXX9 NDFMC Registers */
25 #define TXX9_NDFDTR 0x00
26 #define TXX9_NDFMCR 0x04
27 #define TXX9_NDFSR 0x08
28 #define TXX9_NDFISR 0x0c
29 #define TXX9_NDFIMR 0x10
30 #define TXX9_NDFSPR 0x14
31 #define TXX9_NDFRSTR 0x18 /* not TX4939 */
33 /* NDFMCR : NDFMC Mode Control */
34 #define TXX9_NDFMCR_WE 0x80
35 #define TXX9_NDFMCR_ECC_ALL 0x60
36 #define TXX9_NDFMCR_ECC_RESET 0x60
37 #define TXX9_NDFMCR_ECC_READ 0x40
38 #define TXX9_NDFMCR_ECC_ON 0x20
39 #define TXX9_NDFMCR_ECC_OFF 0x00
40 #define TXX9_NDFMCR_CE 0x10
41 #define TXX9_NDFMCR_BSPRT 0x04 /* TX4925/TX4926 only */
42 #define TXX9_NDFMCR_ALE 0x02
43 #define TXX9_NDFMCR_CLE 0x01
45 #define TXX9_NDFMCR_X16 0x0400
46 #define TXX9_NDFMCR_DMAREQ_MASK 0x0300
47 #define TXX9_NDFMCR_DMAREQ_NODMA 0x0000
48 #define TXX9_NDFMCR_DMAREQ_128 0x0100
49 #define TXX9_NDFMCR_DMAREQ_256 0x0200
50 #define TXX9_NDFMCR_DMAREQ_512 0x0300
51 #define TXX9_NDFMCR_CS_MASK 0x0c
52 #define TXX9_NDFMCR_CS(ch) ((ch) << 2)
54 /* NDFMCR : NDFMC Status */
55 #define TXX9_NDFSR_BUSY 0x80
57 #define TXX9_NDFSR_DMARUN 0x40
59 /* NDFMCR : NDFMC Reset */
60 #define TXX9_NDFRSTR_RST 0x01
62 struct txx9ndfmc_priv
{
63 struct platform_device
*dev
;
64 struct nand_chip chip
;
70 #define MAX_TXX9NDFMC_DEV 4
71 struct txx9ndfmc_drvdata
{
72 struct mtd_info
*mtds
[MAX_TXX9NDFMC_DEV
];
74 unsigned char hold
; /* in gbusclock */
75 unsigned char spw
; /* in gbusclock */
76 struct nand_hw_control hw_control
;
77 #ifdef CONFIG_MTD_PARTITIONS
78 struct mtd_partition
*parts
[MAX_TXX9NDFMC_DEV
];
82 static struct platform_device
*mtd_to_platdev(struct mtd_info
*mtd
)
84 struct nand_chip
*chip
= mtd
->priv
;
85 struct txx9ndfmc_priv
*txx9_priv
= chip
->priv
;
86 return txx9_priv
->dev
;
89 static void __iomem
*ndregaddr(struct platform_device
*dev
, unsigned int reg
)
91 struct txx9ndfmc_drvdata
*drvdata
= platform_get_drvdata(dev
);
92 struct txx9ndfmc_platform_data
*plat
= dev
->dev
.platform_data
;
94 return drvdata
->base
+ (reg
<< plat
->shift
);
97 static u32
txx9ndfmc_read(struct platform_device
*dev
, unsigned int reg
)
99 return __raw_readl(ndregaddr(dev
, reg
));
102 static void txx9ndfmc_write(struct platform_device
*dev
,
103 u32 val
, unsigned int reg
)
105 __raw_writel(val
, ndregaddr(dev
, reg
));
108 static uint8_t txx9ndfmc_read_byte(struct mtd_info
*mtd
)
110 struct platform_device
*dev
= mtd_to_platdev(mtd
);
112 return txx9ndfmc_read(dev
, TXX9_NDFDTR
);
115 static void txx9ndfmc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
118 struct platform_device
*dev
= mtd_to_platdev(mtd
);
119 void __iomem
*ndfdtr
= ndregaddr(dev
, TXX9_NDFDTR
);
120 u32 mcr
= txx9ndfmc_read(dev
, TXX9_NDFMCR
);
122 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_WE
, TXX9_NDFMCR
);
124 __raw_writel(*buf
++, ndfdtr
);
125 txx9ndfmc_write(dev
, mcr
, TXX9_NDFMCR
);
128 static void txx9ndfmc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
130 struct platform_device
*dev
= mtd_to_platdev(mtd
);
131 void __iomem
*ndfdtr
= ndregaddr(dev
, TXX9_NDFDTR
);
134 *buf
++ = __raw_readl(ndfdtr
);
137 static int txx9ndfmc_verify_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
140 struct platform_device
*dev
= mtd_to_platdev(mtd
);
141 void __iomem
*ndfdtr
= ndregaddr(dev
, TXX9_NDFDTR
);
144 if (*buf
++ != (uint8_t)__raw_readl(ndfdtr
))
149 static void txx9ndfmc_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
152 struct nand_chip
*chip
= mtd
->priv
;
153 struct txx9ndfmc_priv
*txx9_priv
= chip
->priv
;
154 struct platform_device
*dev
= txx9_priv
->dev
;
155 struct txx9ndfmc_platform_data
*plat
= dev
->dev
.platform_data
;
157 if (ctrl
& NAND_CTRL_CHANGE
) {
158 u32 mcr
= txx9ndfmc_read(dev
, TXX9_NDFMCR
);
160 mcr
&= ~(TXX9_NDFMCR_CLE
| TXX9_NDFMCR_ALE
| TXX9_NDFMCR_CE
);
161 mcr
|= ctrl
& NAND_CLE
? TXX9_NDFMCR_CLE
: 0;
162 mcr
|= ctrl
& NAND_ALE
? TXX9_NDFMCR_ALE
: 0;
163 /* TXX9_NDFMCR_CE bit is 0:high 1:low */
164 mcr
|= ctrl
& NAND_NCE
? TXX9_NDFMCR_CE
: 0;
165 if (txx9_priv
->cs
>= 0 && (ctrl
& NAND_NCE
)) {
166 mcr
&= ~TXX9_NDFMCR_CS_MASK
;
167 mcr
|= TXX9_NDFMCR_CS(txx9_priv
->cs
);
169 txx9ndfmc_write(dev
, mcr
, TXX9_NDFMCR
);
171 if (cmd
!= NAND_CMD_NONE
)
172 txx9ndfmc_write(dev
, cmd
& 0xff, TXX9_NDFDTR
);
173 if (plat
->flags
& NDFMC_PLAT_FLAG_DUMMYWRITE
) {
174 /* dummy write to update external latch */
175 if ((ctrl
& NAND_CTRL_CHANGE
) && cmd
== NAND_CMD_NONE
)
176 txx9ndfmc_write(dev
, 0, TXX9_NDFDTR
);
181 static int txx9ndfmc_dev_ready(struct mtd_info
*mtd
)
183 struct platform_device
*dev
= mtd_to_platdev(mtd
);
185 return !(txx9ndfmc_read(dev
, TXX9_NDFSR
) & TXX9_NDFSR_BUSY
);
188 static int txx9ndfmc_calculate_ecc(struct mtd_info
*mtd
, const uint8_t *dat
,
191 struct platform_device
*dev
= mtd_to_platdev(mtd
);
192 struct nand_chip
*chip
= mtd
->priv
;
194 u32 mcr
= txx9ndfmc_read(dev
, TXX9_NDFMCR
);
196 mcr
&= ~TXX9_NDFMCR_ECC_ALL
;
197 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_OFF
, TXX9_NDFMCR
);
198 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_READ
, TXX9_NDFMCR
);
199 for (eccbytes
= chip
->ecc
.bytes
; eccbytes
> 0; eccbytes
-= 3) {
200 ecc_code
[1] = txx9ndfmc_read(dev
, TXX9_NDFDTR
);
201 ecc_code
[0] = txx9ndfmc_read(dev
, TXX9_NDFDTR
);
202 ecc_code
[2] = txx9ndfmc_read(dev
, TXX9_NDFDTR
);
205 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_OFF
, TXX9_NDFMCR
);
209 static int txx9ndfmc_correct_data(struct mtd_info
*mtd
, unsigned char *buf
,
210 unsigned char *read_ecc
, unsigned char *calc_ecc
)
212 struct nand_chip
*chip
= mtd
->priv
;
217 for (eccsize
= chip
->ecc
.size
; eccsize
> 0; eccsize
-= 256) {
218 stat
= __nand_correct_data(buf
, read_ecc
, calc_ecc
, 256);
229 static void txx9ndfmc_enable_hwecc(struct mtd_info
*mtd
, int mode
)
231 struct platform_device
*dev
= mtd_to_platdev(mtd
);
232 u32 mcr
= txx9ndfmc_read(dev
, TXX9_NDFMCR
);
234 mcr
&= ~TXX9_NDFMCR_ECC_ALL
;
235 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_RESET
, TXX9_NDFMCR
);
236 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_OFF
, TXX9_NDFMCR
);
237 txx9ndfmc_write(dev
, mcr
| TXX9_NDFMCR_ECC_ON
, TXX9_NDFMCR
);
240 static void txx9ndfmc_initialize(struct platform_device
*dev
)
242 struct txx9ndfmc_platform_data
*plat
= dev
->dev
.platform_data
;
243 struct txx9ndfmc_drvdata
*drvdata
= platform_get_drvdata(dev
);
246 if (plat
->flags
& NDFMC_PLAT_FLAG_NO_RSTR
)
247 ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
251 txx9ndfmc_read(dev
, TXX9_NDFRSTR
) |
254 while (txx9ndfmc_read(dev
, TXX9_NDFRSTR
) & TXX9_NDFRSTR_RST
) {
256 dev_err(&dev
->dev
, "reset failed.\n");
262 /* setup Hold Time, Strobe Pulse Width */
263 txx9ndfmc_write(dev
, (drvdata
->hold
<< 4) | drvdata
->spw
, TXX9_NDFSPR
);
265 (plat
->flags
& NDFMC_PLAT_FLAG_USE_BSPRT
) ?
266 TXX9_NDFMCR_BSPRT
: 0, TXX9_NDFMCR
);
269 #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
270 DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
272 static int txx9ndfmc_nand_scan(struct mtd_info
*mtd
)
274 struct nand_chip
*chip
= mtd
->priv
;
277 ret
= nand_scan_ident(mtd
, 1, NULL
);
279 if (mtd
->writesize
>= 512) {
280 /* Hardware ECC 6 byte ECC per 512 Byte data */
281 chip
->ecc
.size
= 512;
284 ret
= nand_scan_tail(mtd
);
289 static int __init
txx9ndfmc_probe(struct platform_device
*dev
)
291 struct txx9ndfmc_platform_data
*plat
= dev
->dev
.platform_data
;
292 #ifdef CONFIG_MTD_PARTITIONS
293 static const char *probes
[] = { "cmdlinepart", NULL
};
297 struct txx9ndfmc_drvdata
*drvdata
;
298 unsigned long gbusclk
= plat
->gbus_clock
;
299 struct resource
*res
;
301 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
304 drvdata
= devm_kzalloc(&dev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
307 if (!devm_request_mem_region(&dev
->dev
, res
->start
,
308 resource_size(res
), dev_name(&dev
->dev
)))
310 drvdata
->base
= devm_ioremap(&dev
->dev
, res
->start
,
315 hold
= plat
->hold
?: 20; /* tDH */
316 spw
= plat
->spw
?: 90; /* max(tREADID, tWP, tRP) */
318 hold
= TXX9NDFMC_NS_TO_CYC(gbusclk
, hold
);
319 spw
= TXX9NDFMC_NS_TO_CYC(gbusclk
, spw
);
320 if (plat
->flags
& NDFMC_PLAT_FLAG_HOLDADD
)
321 hold
-= 2; /* actual hold time : (HOLD + 2) BUSCLK */
322 spw
-= 1; /* actual wait time : (SPW + 1) BUSCLK */
323 hold
= clamp(hold
, 1, 15);
324 drvdata
->hold
= hold
;
325 spw
= clamp(spw
, 1, 15);
327 dev_info(&dev
->dev
, "CLK:%ldMHz HOLD:%d SPW:%d\n",
328 (gbusclk
+ 500000) / 1000000, hold
, spw
);
330 spin_lock_init(&drvdata
->hw_control
.lock
);
331 init_waitqueue_head(&drvdata
->hw_control
.wq
);
333 platform_set_drvdata(dev
, drvdata
);
334 txx9ndfmc_initialize(dev
);
336 for (i
= 0; i
< MAX_TXX9NDFMC_DEV
; i
++) {
337 struct txx9ndfmc_priv
*txx9_priv
;
338 struct nand_chip
*chip
;
339 struct mtd_info
*mtd
;
340 #ifdef CONFIG_MTD_PARTITIONS
344 if (!(plat
->ch_mask
& (1 << i
)))
346 txx9_priv
= kzalloc(sizeof(struct txx9ndfmc_priv
),
349 dev_err(&dev
->dev
, "Unable to allocate "
350 "TXx9 NDFMC MTD device structure.\n");
353 chip
= &txx9_priv
->chip
;
354 mtd
= &txx9_priv
->mtd
;
355 mtd
->owner
= THIS_MODULE
;
359 chip
->read_byte
= txx9ndfmc_read_byte
;
360 chip
->read_buf
= txx9ndfmc_read_buf
;
361 chip
->write_buf
= txx9ndfmc_write_buf
;
362 chip
->verify_buf
= txx9ndfmc_verify_buf
;
363 chip
->cmd_ctrl
= txx9ndfmc_cmd_ctrl
;
364 chip
->dev_ready
= txx9ndfmc_dev_ready
;
365 chip
->ecc
.calculate
= txx9ndfmc_calculate_ecc
;
366 chip
->ecc
.correct
= txx9ndfmc_correct_data
;
367 chip
->ecc
.hwctl
= txx9ndfmc_enable_hwecc
;
368 chip
->ecc
.mode
= NAND_ECC_HW
;
369 /* txx9ndfmc_nand_scan will overwrite ecc.size and ecc.bytes */
370 chip
->ecc
.size
= 256;
372 chip
->chip_delay
= 100;
373 chip
->controller
= &drvdata
->hw_control
;
375 chip
->priv
= txx9_priv
;
376 txx9_priv
->dev
= dev
;
378 if (plat
->ch_mask
!= 1) {
380 txx9_priv
->mtdname
= kasprintf(GFP_KERNEL
, "%s.%u",
381 dev_name(&dev
->dev
), i
);
384 txx9_priv
->mtdname
= kstrdup(dev_name(&dev
->dev
),
387 if (!txx9_priv
->mtdname
) {
389 dev_err(&dev
->dev
, "Unable to allocate MTD name.\n");
392 if (plat
->wide_mask
& (1 << i
))
393 chip
->options
|= NAND_BUSWIDTH_16
;
395 if (txx9ndfmc_nand_scan(mtd
)) {
396 kfree(txx9_priv
->mtdname
);
400 mtd
->name
= txx9_priv
->mtdname
;
402 #ifdef CONFIG_MTD_PARTITIONS
403 nr_parts
= parse_mtd_partitions(mtd
, probes
,
404 &drvdata
->parts
[i
], 0);
406 add_mtd_partitions(mtd
, drvdata
->parts
[i
], nr_parts
);
409 drvdata
->mtds
[i
] = mtd
;
415 static int __exit
txx9ndfmc_remove(struct platform_device
*dev
)
417 struct txx9ndfmc_drvdata
*drvdata
= platform_get_drvdata(dev
);
420 platform_set_drvdata(dev
, NULL
);
423 for (i
= 0; i
< MAX_TXX9NDFMC_DEV
; i
++) {
424 struct mtd_info
*mtd
= drvdata
->mtds
[i
];
425 struct nand_chip
*chip
;
426 struct txx9ndfmc_priv
*txx9_priv
;
431 txx9_priv
= chip
->priv
;
434 #ifdef CONFIG_MTD_PARTITIONS
435 kfree(drvdata
->parts
[i
]);
437 kfree(txx9_priv
->mtdname
);
444 static int txx9ndfmc_resume(struct platform_device
*dev
)
446 if (platform_get_drvdata(dev
))
447 txx9ndfmc_initialize(dev
);
451 #define txx9ndfmc_resume NULL
454 static struct platform_driver txx9ndfmc_driver
= {
455 .remove
= __exit_p(txx9ndfmc_remove
),
456 .resume
= txx9ndfmc_resume
,
459 .owner
= THIS_MODULE
,
463 static int __init
txx9ndfmc_init(void)
465 return platform_driver_probe(&txx9ndfmc_driver
, txx9ndfmc_probe
);
468 static void __exit
txx9ndfmc_exit(void)
470 platform_driver_unregister(&txx9ndfmc_driver
);
473 module_init(txx9ndfmc_init
);
474 module_exit(txx9ndfmc_exit
);
476 MODULE_LICENSE("GPL");
477 MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
478 MODULE_ALIAS("platform:txx9ndfmc");