2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 SoC NAND controller driver
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/ioport.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/nand.h>
24 #include <linux/mtd/partitions.h>
26 #include <linux/gpio.h>
28 #include <asm/mach-jz4740/gpio.h>
29 #include <asm/mach-jz4740/jz4740_nand.h>
31 #define JZ_REG_NAND_CTRL 0x50
32 #define JZ_REG_NAND_ECC_CTRL 0x100
33 #define JZ_REG_NAND_DATA 0x104
34 #define JZ_REG_NAND_PAR0 0x108
35 #define JZ_REG_NAND_PAR1 0x10C
36 #define JZ_REG_NAND_PAR2 0x110
37 #define JZ_REG_NAND_IRQ_STAT 0x114
38 #define JZ_REG_NAND_IRQ_CTRL 0x118
39 #define JZ_REG_NAND_ERR(x) (0x11C + ((x) << 2))
41 #define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
42 #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
43 #define JZ_NAND_ECC_CTRL_RS BIT(2)
44 #define JZ_NAND_ECC_CTRL_RESET BIT(1)
45 #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
47 #define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
48 #define JZ_NAND_STATUS_PAD_FINISH BIT(4)
49 #define JZ_NAND_STATUS_DEC_FINISH BIT(3)
50 #define JZ_NAND_STATUS_ENC_FINISH BIT(2)
51 #define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
52 #define JZ_NAND_STATUS_ERROR BIT(0)
54 #define JZ_NAND_CTRL_ENABLE_CHIP(x) BIT((x) << 1)
55 #define JZ_NAND_CTRL_ASSERT_CHIP(x) BIT(((x) << 1) + 1)
56 #define JZ_NAND_CTRL_ASSERT_CHIP_MASK 0xaa
58 #define JZ_NAND_MEM_CMD_OFFSET 0x08000
59 #define JZ_NAND_MEM_ADDR_OFFSET 0x10000
63 struct nand_chip chip
;
67 unsigned char banks
[JZ_NAND_NUM_BANKS
];
68 void __iomem
*bank_base
[JZ_NAND_NUM_BANKS
];
69 struct resource
*bank_mem
[JZ_NAND_NUM_BANKS
];
73 struct gpio_desc
*busy_gpio
;
77 static inline struct jz_nand
*mtd_to_jz_nand(struct mtd_info
*mtd
)
79 return container_of(mtd
, struct jz_nand
, mtd
);
82 static void jz_nand_select_chip(struct mtd_info
*mtd
, int chipnr
)
84 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
85 struct nand_chip
*chip
= mtd
->priv
;
89 ctrl
= readl(nand
->base
+ JZ_REG_NAND_CTRL
);
90 ctrl
&= ~JZ_NAND_CTRL_ASSERT_CHIP_MASK
;
95 banknr
= nand
->banks
[chipnr
] - 1;
96 chip
->IO_ADDR_R
= nand
->bank_base
[banknr
];
97 chip
->IO_ADDR_W
= nand
->bank_base
[banknr
];
99 writel(ctrl
, nand
->base
+ JZ_REG_NAND_CTRL
);
101 nand
->selected_bank
= banknr
;
104 static void jz_nand_cmd_ctrl(struct mtd_info
*mtd
, int dat
, unsigned int ctrl
)
106 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
107 struct nand_chip
*chip
= mtd
->priv
;
109 void __iomem
*bank_base
= nand
->bank_base
[nand
->selected_bank
];
111 BUG_ON(nand
->selected_bank
< 0);
113 if (ctrl
& NAND_CTRL_CHANGE
) {
114 BUG_ON((ctrl
& NAND_ALE
) && (ctrl
& NAND_CLE
));
116 bank_base
+= JZ_NAND_MEM_ADDR_OFFSET
;
117 else if (ctrl
& NAND_CLE
)
118 bank_base
+= JZ_NAND_MEM_CMD_OFFSET
;
119 chip
->IO_ADDR_W
= bank_base
;
121 reg
= readl(nand
->base
+ JZ_REG_NAND_CTRL
);
123 reg
|= JZ_NAND_CTRL_ASSERT_CHIP(nand
->selected_bank
);
125 reg
&= ~JZ_NAND_CTRL_ASSERT_CHIP(nand
->selected_bank
);
126 writel(reg
, nand
->base
+ JZ_REG_NAND_CTRL
);
128 if (dat
!= NAND_CMD_NONE
)
129 writeb(dat
, chip
->IO_ADDR_W
);
132 static int jz_nand_dev_ready(struct mtd_info
*mtd
)
134 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
135 return gpiod_get_value_cansleep(nand
->busy_gpio
);
138 static void jz_nand_hwctl(struct mtd_info
*mtd
, int mode
)
140 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
143 writel(0, nand
->base
+ JZ_REG_NAND_IRQ_STAT
);
144 reg
= readl(nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
146 reg
|= JZ_NAND_ECC_CTRL_RESET
;
147 reg
|= JZ_NAND_ECC_CTRL_ENABLE
;
148 reg
|= JZ_NAND_ECC_CTRL_RS
;
152 reg
&= ~JZ_NAND_ECC_CTRL_ENCODING
;
153 nand
->is_reading
= true;
156 reg
|= JZ_NAND_ECC_CTRL_ENCODING
;
157 nand
->is_reading
= false;
163 writel(reg
, nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
166 static int jz_nand_calculate_ecc_rs(struct mtd_info
*mtd
, const uint8_t *dat
,
169 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
170 uint32_t reg
, status
;
172 unsigned int timeout
= 1000;
173 static uint8_t empty_block_ecc
[] = {0xcd, 0x9d, 0x90, 0x58, 0xf4,
174 0x8b, 0xff, 0xb7, 0x6f};
176 if (nand
->is_reading
)
180 status
= readl(nand
->base
+ JZ_REG_NAND_IRQ_STAT
);
181 } while (!(status
& JZ_NAND_STATUS_ENC_FINISH
) && --timeout
);
186 reg
= readl(nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
187 reg
&= ~JZ_NAND_ECC_CTRL_ENABLE
;
188 writel(reg
, nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
190 for (i
= 0; i
< 9; ++i
)
191 ecc_code
[i
] = readb(nand
->base
+ JZ_REG_NAND_PAR0
+ i
);
193 /* If the written data is completly 0xff, we also want to write 0xff as
194 * ecc, otherwise we will get in trouble when doing subpage writes. */
195 if (memcmp(ecc_code
, empty_block_ecc
, 9) == 0)
196 memset(ecc_code
, 0xff, 9);
201 static void jz_nand_correct_data(uint8_t *dat
, int index
, int mask
)
203 int offset
= index
& 0x7;
206 index
+= (index
>> 3);
209 data
|= dat
[index
+1] << 8;
211 mask
^= (data
>> offset
) & 0x1ff;
212 data
&= ~(0x1ff << offset
);
213 data
|= (mask
<< offset
);
215 dat
[index
] = data
& 0xff;
216 dat
[index
+1] = (data
>> 8) & 0xff;
219 static int jz_nand_correct_ecc_rs(struct mtd_info
*mtd
, uint8_t *dat
,
220 uint8_t *read_ecc
, uint8_t *calc_ecc
)
222 struct jz_nand
*nand
= mtd_to_jz_nand(mtd
);
223 int i
, error_count
, index
;
224 uint32_t reg
, status
, error
;
226 unsigned int timeout
= 1000;
231 for (i
= 1; i
< 9; ++i
)
235 t
&= dat
[nand
->chip
.ecc
.size
/ 2];
236 t
&= dat
[nand
->chip
.ecc
.size
- 1];
239 for (i
= 1; i
< nand
->chip
.ecc
.size
- 1; ++i
)
246 for (i
= 0; i
< 9; ++i
)
247 writeb(read_ecc
[i
], nand
->base
+ JZ_REG_NAND_PAR0
+ i
);
249 reg
= readl(nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
250 reg
|= JZ_NAND_ECC_CTRL_PAR_READY
;
251 writel(reg
, nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
254 status
= readl(nand
->base
+ JZ_REG_NAND_IRQ_STAT
);
255 } while (!(status
& JZ_NAND_STATUS_DEC_FINISH
) && --timeout
);
260 reg
= readl(nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
261 reg
&= ~JZ_NAND_ECC_CTRL_ENABLE
;
262 writel(reg
, nand
->base
+ JZ_REG_NAND_ECC_CTRL
);
264 if (status
& JZ_NAND_STATUS_ERROR
) {
265 if (status
& JZ_NAND_STATUS_UNCOR_ERROR
)
268 error_count
= (status
& JZ_NAND_STATUS_ERR_COUNT
) >> 29;
270 for (i
= 0; i
< error_count
; ++i
) {
271 error
= readl(nand
->base
+ JZ_REG_NAND_ERR(i
));
272 index
= ((error
>> 16) & 0x1ff) - 1;
273 if (index
>= 0 && index
< 512)
274 jz_nand_correct_data(dat
, index
, error
& 0x1ff);
283 static int jz_nand_ioremap_resource(struct platform_device
*pdev
,
284 const char *name
, struct resource
**res
, void *__iomem
*base
)
288 *res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
290 dev_err(&pdev
->dev
, "Failed to get platform %s memory\n", name
);
295 *res
= request_mem_region((*res
)->start
, resource_size(*res
),
298 dev_err(&pdev
->dev
, "Failed to request %s memory region\n", name
);
303 *base
= ioremap((*res
)->start
, resource_size(*res
));
305 dev_err(&pdev
->dev
, "Failed to ioremap %s memory region\n", name
);
307 goto err_release_mem
;
313 release_mem_region((*res
)->start
, resource_size(*res
));
320 static inline void jz_nand_iounmap_resource(struct resource
*res
,
324 release_mem_region(res
->start
, resource_size(res
));
327 static int jz_nand_detect_bank(struct platform_device
*pdev
,
328 struct jz_nand
*nand
, unsigned char bank
,
329 size_t chipnr
, uint8_t *nand_maf_id
,
330 uint8_t *nand_dev_id
)
337 struct mtd_info
*mtd
= &nand
->mtd
;
338 struct nand_chip
*chip
= &nand
->chip
;
340 /* Request GPIO port. */
341 gpio
= JZ_GPIO_MEM_CS0
+ bank
- 1;
342 sprintf(gpio_name
, "NAND CS%d", bank
);
343 ret
= gpio_request(gpio
, gpio_name
);
346 "Failed to request %s gpio %d: %d\n",
347 gpio_name
, gpio
, ret
);
351 /* Request I/O resource. */
352 sprintf(res_name
, "bank%d", bank
);
353 ret
= jz_nand_ioremap_resource(pdev
, res_name
,
354 &nand
->bank_mem
[bank
- 1],
355 &nand
->bank_base
[bank
- 1]);
357 goto notfound_resource
;
359 /* Enable chip in bank. */
360 jz_gpio_set_function(gpio
, JZ_GPIO_FUNC_MEM_CS0
);
361 ctrl
= readl(nand
->base
+ JZ_REG_NAND_CTRL
);
362 ctrl
|= JZ_NAND_CTRL_ENABLE_CHIP(bank
- 1);
363 writel(ctrl
, nand
->base
+ JZ_REG_NAND_CTRL
);
366 /* Detect first chip. */
367 ret
= nand_scan_ident(mtd
, 1, NULL
);
371 /* Retrieve the IDs from the first chip. */
372 chip
->select_chip(mtd
, 0);
373 chip
->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
374 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x00, -1);
375 *nand_maf_id
= chip
->read_byte(mtd
);
376 *nand_dev_id
= chip
->read_byte(mtd
);
378 /* Detect additional chip. */
379 chip
->select_chip(mtd
, chipnr
);
380 chip
->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
381 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x00, -1);
382 if (*nand_maf_id
!= chip
->read_byte(mtd
)
383 || *nand_dev_id
!= chip
->read_byte(mtd
)) {
388 /* Update size of the MTD. */
390 mtd
->size
+= chip
->chipsize
;
393 dev_info(&pdev
->dev
, "Found chip %i on bank %i\n", chipnr
, bank
);
397 dev_info(&pdev
->dev
, "No chip found on bank %i\n", bank
);
398 ctrl
&= ~(JZ_NAND_CTRL_ENABLE_CHIP(bank
- 1));
399 writel(ctrl
, nand
->base
+ JZ_REG_NAND_CTRL
);
400 jz_gpio_set_function(gpio
, JZ_GPIO_FUNC_NONE
);
401 jz_nand_iounmap_resource(nand
->bank_mem
[bank
- 1],
402 nand
->bank_base
[bank
- 1]);
409 static int jz_nand_probe(struct platform_device
*pdev
)
412 struct jz_nand
*nand
;
413 struct nand_chip
*chip
;
414 struct mtd_info
*mtd
;
415 struct jz_nand_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
416 size_t chipnr
, bank_idx
;
417 uint8_t nand_maf_id
= 0, nand_dev_id
= 0;
419 nand
= kzalloc(sizeof(*nand
), GFP_KERNEL
);
423 ret
= jz_nand_ioremap_resource(pdev
, "mmio", &nand
->mem
, &nand
->base
);
427 nand
->busy_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "busy", GPIOD_IN
);
428 if (IS_ERR(nand
->busy_gpio
)) {
429 ret
= PTR_ERR(nand
->busy_gpio
);
430 dev_err(&pdev
->dev
, "Failed to request busy gpio %d\n",
432 goto err_iounmap_mmio
;
438 mtd
->dev
.parent
= &pdev
->dev
;
439 mtd
->name
= "jz4740-nand";
441 chip
->ecc
.hwctl
= jz_nand_hwctl
;
442 chip
->ecc
.calculate
= jz_nand_calculate_ecc_rs
;
443 chip
->ecc
.correct
= jz_nand_correct_ecc_rs
;
444 chip
->ecc
.mode
= NAND_ECC_HW_OOB_FIRST
;
445 chip
->ecc
.size
= 512;
447 chip
->ecc
.strength
= 4;
450 chip
->ecc
.layout
= pdata
->ecc_layout
;
452 chip
->chip_delay
= 50;
453 chip
->cmd_ctrl
= jz_nand_cmd_ctrl
;
454 chip
->select_chip
= jz_nand_select_chip
;
457 chip
->dev_ready
= jz_nand_dev_ready
;
459 platform_set_drvdata(pdev
, nand
);
461 /* We are going to autodetect NAND chips in the banks specified in the
462 * platform data. Although nand_scan_ident() can detect multiple chips,
463 * it requires those chips to be numbered consecuitively, which is not
464 * always the case for external memory banks. And a fixed chip-to-bank
465 * mapping is not practical either, since for example Dingoo units
466 * produced at different times have NAND chips in different banks.
469 for (bank_idx
= 0; bank_idx
< JZ_NAND_NUM_BANKS
; bank_idx
++) {
472 /* If there is no platform data, look for NAND in bank 1,
473 * which is the most likely bank since it is the only one
474 * that can be booted from.
476 bank
= pdata
? pdata
->banks
[bank_idx
] : bank_idx
^ 1;
479 if (bank
> JZ_NAND_NUM_BANKS
) {
481 "Skipping non-existing bank: %d\n", bank
);
484 /* The detection routine will directly or indirectly call
485 * jz_nand_select_chip(), so nand->banks has to contain the
486 * bank we're checking.
488 nand
->banks
[chipnr
] = bank
;
489 if (jz_nand_detect_bank(pdev
, nand
, bank
, chipnr
,
490 &nand_maf_id
, &nand_dev_id
) == 0)
493 nand
->banks
[chipnr
] = 0;
496 dev_err(&pdev
->dev
, "No NAND chips found\n");
497 goto err_iounmap_mmio
;
500 if (pdata
&& pdata
->ident_callback
) {
501 pdata
->ident_callback(pdev
, chip
, &pdata
->partitions
,
502 &pdata
->num_partitions
);
505 ret
= nand_scan_tail(mtd
);
507 dev_err(&pdev
->dev
, "Failed to scan NAND\n");
508 goto err_unclaim_banks
;
511 ret
= mtd_device_parse_register(mtd
, NULL
, NULL
,
512 pdata
? pdata
->partitions
: NULL
,
513 pdata
? pdata
->num_partitions
: 0);
516 dev_err(&pdev
->dev
, "Failed to add mtd device\n");
517 goto err_nand_release
;
520 dev_info(&pdev
->dev
, "Successfully registered JZ4740 NAND driver\n");
528 unsigned char bank
= nand
->banks
[chipnr
];
529 gpio_free(JZ_GPIO_MEM_CS0
+ bank
- 1);
530 jz_nand_iounmap_resource(nand
->bank_mem
[bank
- 1],
531 nand
->bank_base
[bank
- 1]);
533 writel(0, nand
->base
+ JZ_REG_NAND_CTRL
);
535 jz_nand_iounmap_resource(nand
->mem
, nand
->base
);
541 static int jz_nand_remove(struct platform_device
*pdev
)
543 struct jz_nand
*nand
= platform_get_drvdata(pdev
);
546 nand_release(&nand
->mtd
);
548 /* Deassert and disable all chips */
549 writel(0, nand
->base
+ JZ_REG_NAND_CTRL
);
551 for (i
= 0; i
< JZ_NAND_NUM_BANKS
; ++i
) {
552 unsigned char bank
= nand
->banks
[i
];
554 jz_nand_iounmap_resource(nand
->bank_mem
[bank
- 1],
555 nand
->bank_base
[bank
- 1]);
556 gpio_free(JZ_GPIO_MEM_CS0
+ bank
- 1);
560 jz_nand_iounmap_resource(nand
->mem
, nand
->base
);
567 static struct platform_driver jz_nand_driver
= {
568 .probe
= jz_nand_probe
,
569 .remove
= jz_nand_remove
,
571 .name
= "jz4740-nand",
575 module_platform_driver(jz_nand_driver
);
577 MODULE_LICENSE("GPL");
578 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
579 MODULE_DESCRIPTION("NAND controller driver for JZ4740 SoC");
580 MODULE_ALIAS("platform:jz4740-nand");