1 // SPDX-License-Identifier: GPL-2.0
3 * Ingenic JZ47xx NAND driver
5 * Copyright (c) 2015 Imagination Technologies
6 * Author: Alex Smith <alex.smith@imgtec.com>
9 #include <linux/delay.h>
10 #include <linux/init.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/rawnand.h>
22 #include <linux/mtd/partitions.h>
24 #include <linux/jz4780-nemc.h>
26 #include "ingenic_ecc.h"
28 #define DRV_NAME "ingenic-nand"
30 /* Command delay when there is no R/B pin. */
31 #define RB_DELAY_US 100
34 unsigned long data_offset
;
35 unsigned long addr_offset
;
36 unsigned long cmd_offset
;
37 const struct mtd_ooblayout_ops
*oob_layout
;
40 struct ingenic_nand_cs
{
47 struct ingenic_ecc
*ecc
;
48 const struct jz_soc_info
*soc_info
;
49 struct nand_controller controller
;
50 unsigned int num_banks
;
51 struct list_head chips
;
53 struct ingenic_nand_cs cs
[];
57 struct nand_chip chip
;
58 struct list_head chip_list
;
60 struct gpio_desc
*busy_gpio
;
61 struct gpio_desc
*wp_gpio
;
62 unsigned int reading
: 1;
65 static inline struct ingenic_nand
*to_ingenic_nand(struct mtd_info
*mtd
)
67 return container_of(mtd_to_nand(mtd
), struct ingenic_nand
, chip
);
70 static inline struct ingenic_nfc
*to_ingenic_nfc(struct nand_controller
*ctrl
)
72 return container_of(ctrl
, struct ingenic_nfc
, controller
);
75 static int qi_lb60_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
76 struct mtd_oob_region
*oobregion
)
78 struct nand_chip
*chip
= mtd_to_nand(mtd
);
79 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
81 if (section
|| !ecc
->total
)
84 oobregion
->length
= ecc
->total
;
85 oobregion
->offset
= 12;
90 static int qi_lb60_ooblayout_free(struct mtd_info
*mtd
, int section
,
91 struct mtd_oob_region
*oobregion
)
93 struct nand_chip
*chip
= mtd_to_nand(mtd
);
94 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
99 oobregion
->length
= mtd
->oobsize
- ecc
->total
- 12;
100 oobregion
->offset
= 12 + ecc
->total
;
105 const struct mtd_ooblayout_ops qi_lb60_ooblayout_ops
= {
106 .ecc
= qi_lb60_ooblayout_ecc
,
107 .free
= qi_lb60_ooblayout_free
,
110 static int jz4725b_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
111 struct mtd_oob_region
*oobregion
)
113 struct nand_chip
*chip
= mtd_to_nand(mtd
);
114 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
116 if (section
|| !ecc
->total
)
119 oobregion
->length
= ecc
->total
;
120 oobregion
->offset
= 3;
125 static int jz4725b_ooblayout_free(struct mtd_info
*mtd
, int section
,
126 struct mtd_oob_region
*oobregion
)
128 struct nand_chip
*chip
= mtd_to_nand(mtd
);
129 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
134 oobregion
->length
= mtd
->oobsize
- ecc
->total
- 3;
135 oobregion
->offset
= 3 + ecc
->total
;
140 static const struct mtd_ooblayout_ops jz4725b_ooblayout_ops
= {
141 .ecc
= jz4725b_ooblayout_ecc
,
142 .free
= jz4725b_ooblayout_free
,
145 static void ingenic_nand_select_chip(struct nand_chip
*chip
, int chipnr
)
147 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
148 struct ingenic_nfc
*nfc
= to_ingenic_nfc(nand
->chip
.controller
);
149 struct ingenic_nand_cs
*cs
;
151 /* Ensure the currently selected chip is deasserted. */
152 if (chipnr
== -1 && nfc
->selected
>= 0) {
153 cs
= &nfc
->cs
[nfc
->selected
];
154 jz4780_nemc_assert(nfc
->dev
, cs
->bank
, false);
157 nfc
->selected
= chipnr
;
160 static void ingenic_nand_cmd_ctrl(struct nand_chip
*chip
, int cmd
,
163 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
164 struct ingenic_nfc
*nfc
= to_ingenic_nfc(nand
->chip
.controller
);
165 struct ingenic_nand_cs
*cs
;
167 if (WARN_ON(nfc
->selected
< 0))
170 cs
= &nfc
->cs
[nfc
->selected
];
172 jz4780_nemc_assert(nfc
->dev
, cs
->bank
, ctrl
& NAND_NCE
);
174 if (cmd
== NAND_CMD_NONE
)
178 writeb(cmd
, cs
->base
+ nfc
->soc_info
->addr_offset
);
179 else if (ctrl
& NAND_CLE
)
180 writeb(cmd
, cs
->base
+ nfc
->soc_info
->cmd_offset
);
183 static int ingenic_nand_dev_ready(struct nand_chip
*chip
)
185 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
187 return !gpiod_get_value_cansleep(nand
->busy_gpio
);
190 static void ingenic_nand_ecc_hwctl(struct nand_chip
*chip
, int mode
)
192 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
194 nand
->reading
= (mode
== NAND_ECC_READ
);
197 static int ingenic_nand_ecc_calculate(struct nand_chip
*chip
, const u8
*dat
,
200 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
201 struct ingenic_nfc
*nfc
= to_ingenic_nfc(nand
->chip
.controller
);
202 struct ingenic_ecc_params params
;
205 * Don't need to generate the ECC when reading, the ECC engine does it
206 * for us as part of decoding/correction.
211 params
.size
= nand
->chip
.ecc
.size
;
212 params
.bytes
= nand
->chip
.ecc
.bytes
;
213 params
.strength
= nand
->chip
.ecc
.strength
;
215 return ingenic_ecc_calculate(nfc
->ecc
, ¶ms
, dat
, ecc_code
);
218 static int ingenic_nand_ecc_correct(struct nand_chip
*chip
, u8
*dat
,
219 u8
*read_ecc
, u8
*calc_ecc
)
221 struct ingenic_nand
*nand
= to_ingenic_nand(nand_to_mtd(chip
));
222 struct ingenic_nfc
*nfc
= to_ingenic_nfc(nand
->chip
.controller
);
223 struct ingenic_ecc_params params
;
225 params
.size
= nand
->chip
.ecc
.size
;
226 params
.bytes
= nand
->chip
.ecc
.bytes
;
227 params
.strength
= nand
->chip
.ecc
.strength
;
229 return ingenic_ecc_correct(nfc
->ecc
, ¶ms
, dat
, read_ecc
);
232 static int ingenic_nand_attach_chip(struct nand_chip
*chip
)
234 struct mtd_info
*mtd
= nand_to_mtd(chip
);
235 struct ingenic_nfc
*nfc
= to_ingenic_nfc(chip
->controller
);
238 if (chip
->ecc
.strength
== 4) {
239 /* JZ4740 uses 9 bytes of ECC to correct maximum 4 errors */
242 chip
->ecc
.bytes
= fls((1 + 8) * chip
->ecc
.size
) *
243 (chip
->ecc
.strength
/ 8);
246 switch (chip
->ecc
.mode
) {
249 dev_err(nfc
->dev
, "HW ECC selected, but ECC controller not found\n");
253 chip
->ecc
.hwctl
= ingenic_nand_ecc_hwctl
;
254 chip
->ecc
.calculate
= ingenic_nand_ecc_calculate
;
255 chip
->ecc
.correct
= ingenic_nand_ecc_correct
;
258 dev_info(nfc
->dev
, "using %s (strength %d, size %d, bytes %d)\n",
259 (nfc
->ecc
) ? "hardware ECC" : "software ECC",
260 chip
->ecc
.strength
, chip
->ecc
.size
, chip
->ecc
.bytes
);
263 dev_info(nfc
->dev
, "not using ECC\n");
266 dev_err(nfc
->dev
, "ECC mode %d not supported\n",
271 /* The NAND core will generate the ECC layout for SW ECC */
272 if (chip
->ecc
.mode
!= NAND_ECC_HW
)
275 /* Generate ECC layout. ECC codes are right aligned in the OOB area. */
276 eccbytes
= mtd
->writesize
/ chip
->ecc
.size
* chip
->ecc
.bytes
;
278 if (eccbytes
> mtd
->oobsize
- 2) {
280 "invalid ECC config: required %d ECC bytes, but only %d are available",
281 eccbytes
, mtd
->oobsize
- 2);
286 * The generic layout for BBT markers will most likely overlap with our
287 * ECC bytes in the OOB, so move the BBT markers outside the OOB area.
289 if (chip
->bbt_options
& NAND_BBT_USE_FLASH
)
290 chip
->bbt_options
|= NAND_BBT_NO_OOB
;
292 /* For legacy reasons we use a different layout on the qi,lb60 board. */
293 if (of_machine_is_compatible("qi,lb60"))
294 mtd_set_ooblayout(mtd
, &qi_lb60_ooblayout_ops
);
296 mtd_set_ooblayout(mtd
, nfc
->soc_info
->oob_layout
);
301 static const struct nand_controller_ops ingenic_nand_controller_ops
= {
302 .attach_chip
= ingenic_nand_attach_chip
,
305 static int ingenic_nand_init_chip(struct platform_device
*pdev
,
306 struct ingenic_nfc
*nfc
,
307 struct device_node
*np
,
310 struct device
*dev
= &pdev
->dev
;
311 struct ingenic_nand
*nand
;
312 struct ingenic_nand_cs
*cs
;
313 struct nand_chip
*chip
;
314 struct mtd_info
*mtd
;
318 cs
= &nfc
->cs
[chipnr
];
320 reg
= of_get_property(np
, "reg", NULL
);
324 cs
->bank
= be32_to_cpu(*reg
);
326 jz4780_nemc_set_type(nfc
->dev
, cs
->bank
, JZ4780_NEMC_BANK_NAND
);
328 cs
->base
= devm_platform_ioremap_resource(pdev
, chipnr
);
329 if (IS_ERR(cs
->base
))
330 return PTR_ERR(cs
->base
);
332 nand
= devm_kzalloc(dev
, sizeof(*nand
), GFP_KERNEL
);
336 nand
->busy_gpio
= devm_gpiod_get_optional(dev
, "rb", GPIOD_IN
);
338 if (IS_ERR(nand
->busy_gpio
)) {
339 ret
= PTR_ERR(nand
->busy_gpio
);
340 dev_err(dev
, "failed to request busy GPIO: %d\n", ret
);
342 } else if (nand
->busy_gpio
) {
343 nand
->chip
.legacy
.dev_ready
= ingenic_nand_dev_ready
;
346 nand
->wp_gpio
= devm_gpiod_get_optional(dev
, "wp", GPIOD_OUT_LOW
);
348 if (IS_ERR(nand
->wp_gpio
)) {
349 ret
= PTR_ERR(nand
->wp_gpio
);
350 dev_err(dev
, "failed to request WP GPIO: %d\n", ret
);
355 mtd
= nand_to_mtd(chip
);
356 mtd
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s.%d", dev_name(dev
),
360 mtd
->dev
.parent
= dev
;
362 chip
->legacy
.IO_ADDR_R
= cs
->base
+ nfc
->soc_info
->data_offset
;
363 chip
->legacy
.IO_ADDR_W
= cs
->base
+ nfc
->soc_info
->data_offset
;
364 chip
->legacy
.chip_delay
= RB_DELAY_US
;
365 chip
->options
= NAND_NO_SUBPAGE_WRITE
;
366 chip
->legacy
.select_chip
= ingenic_nand_select_chip
;
367 chip
->legacy
.cmd_ctrl
= ingenic_nand_cmd_ctrl
;
368 chip
->ecc
.mode
= NAND_ECC_HW
;
369 chip
->controller
= &nfc
->controller
;
370 nand_set_flash_node(chip
, np
);
372 chip
->controller
->ops
= &ingenic_nand_controller_ops
;
373 ret
= nand_scan(chip
, 1);
377 ret
= mtd_device_register(mtd
, NULL
, 0);
383 list_add_tail(&nand
->chip_list
, &nfc
->chips
);
388 static void ingenic_nand_cleanup_chips(struct ingenic_nfc
*nfc
)
390 struct ingenic_nand
*chip
;
392 while (!list_empty(&nfc
->chips
)) {
393 chip
= list_first_entry(&nfc
->chips
,
394 struct ingenic_nand
, chip_list
);
395 nand_release(&chip
->chip
);
396 list_del(&chip
->chip_list
);
400 static int ingenic_nand_init_chips(struct ingenic_nfc
*nfc
,
401 struct platform_device
*pdev
)
403 struct device
*dev
= &pdev
->dev
;
404 struct device_node
*np
;
407 int num_chips
= of_get_child_count(dev
->of_node
);
409 if (num_chips
> nfc
->num_banks
) {
410 dev_err(dev
, "found %d chips but only %d banks\n",
411 num_chips
, nfc
->num_banks
);
415 for_each_child_of_node(dev
->of_node
, np
) {
416 ret
= ingenic_nand_init_chip(pdev
, nfc
, np
, i
);
418 ingenic_nand_cleanup_chips(nfc
);
429 static int ingenic_nand_probe(struct platform_device
*pdev
)
431 struct device
*dev
= &pdev
->dev
;
432 unsigned int num_banks
;
433 struct ingenic_nfc
*nfc
;
436 num_banks
= jz4780_nemc_num_banks(dev
);
437 if (num_banks
== 0) {
438 dev_err(dev
, "no banks found\n");
442 nfc
= devm_kzalloc(dev
, struct_size(nfc
, cs
, num_banks
), GFP_KERNEL
);
446 nfc
->soc_info
= device_get_match_data(dev
);
451 * Check for ECC HW before we call nand_scan_ident, to prevent us from
452 * having to call it again if the ECC driver returns -EPROBE_DEFER.
454 nfc
->ecc
= of_ingenic_ecc_get(dev
->of_node
);
455 if (IS_ERR(nfc
->ecc
))
456 return PTR_ERR(nfc
->ecc
);
459 nfc
->num_banks
= num_banks
;
461 nand_controller_init(&nfc
->controller
);
462 INIT_LIST_HEAD(&nfc
->chips
);
464 ret
= ingenic_nand_init_chips(nfc
, pdev
);
467 ingenic_ecc_release(nfc
->ecc
);
471 platform_set_drvdata(pdev
, nfc
);
475 static int ingenic_nand_remove(struct platform_device
*pdev
)
477 struct ingenic_nfc
*nfc
= platform_get_drvdata(pdev
);
480 ingenic_ecc_release(nfc
->ecc
);
482 ingenic_nand_cleanup_chips(nfc
);
487 static const struct jz_soc_info jz4740_soc_info
= {
488 .data_offset
= 0x00000000,
489 .cmd_offset
= 0x00008000,
490 .addr_offset
= 0x00010000,
491 .oob_layout
= &nand_ooblayout_lp_ops
,
494 static const struct jz_soc_info jz4725b_soc_info
= {
495 .data_offset
= 0x00000000,
496 .cmd_offset
= 0x00008000,
497 .addr_offset
= 0x00010000,
498 .oob_layout
= &jz4725b_ooblayout_ops
,
501 static const struct jz_soc_info jz4780_soc_info
= {
502 .data_offset
= 0x00000000,
503 .cmd_offset
= 0x00400000,
504 .addr_offset
= 0x00800000,
505 .oob_layout
= &nand_ooblayout_lp_ops
,
508 static const struct of_device_id ingenic_nand_dt_match
[] = {
509 { .compatible
= "ingenic,jz4740-nand", .data
= &jz4740_soc_info
},
510 { .compatible
= "ingenic,jz4725b-nand", .data
= &jz4725b_soc_info
},
511 { .compatible
= "ingenic,jz4780-nand", .data
= &jz4780_soc_info
},
514 MODULE_DEVICE_TABLE(of
, ingenic_nand_dt_match
);
516 static struct platform_driver ingenic_nand_driver
= {
517 .probe
= ingenic_nand_probe
,
518 .remove
= ingenic_nand_remove
,
521 .of_match_table
= of_match_ptr(ingenic_nand_dt_match
),
524 module_platform_driver(ingenic_nand_driver
);
526 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
527 MODULE_AUTHOR("Harvey Hunt <harveyhuntnexus@gmail.com>");
528 MODULE_DESCRIPTION("Ingenic JZ47xx NAND driver");
529 MODULE_LICENSE("GPL v2");