4 * Copyright (c) 2015 Imagination Technologies
5 * Author: Alex Smith <alex.smith@imgtec.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/of_mtd.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/partitions.h>
27 #include <linux/jz4780-nemc.h>
29 #include "jz4780_bch.h"
31 #define DRV_NAME "jz4780-nand"
33 #define OFFSET_DATA 0x00000000
34 #define OFFSET_CMD 0x00400000
35 #define OFFSET_ADDR 0x00800000
37 /* Command delay when there is no R/B pin. */
38 #define RB_DELAY_US 100
40 struct jz4780_nand_cs
{
45 struct jz4780_nand_controller
{
47 struct jz4780_bch
*bch
;
48 struct nand_hw_control controller
;
49 unsigned int num_banks
;
50 struct list_head chips
;
52 struct jz4780_nand_cs cs
[];
55 struct jz4780_nand_chip
{
56 struct nand_chip chip
;
57 struct list_head chip_list
;
59 struct nand_ecclayout ecclayout
;
61 struct gpio_desc
*busy_gpio
;
62 struct gpio_desc
*wp_gpio
;
63 unsigned int reading
: 1;
66 static inline struct jz4780_nand_chip
*to_jz4780_nand_chip(struct mtd_info
*mtd
)
68 return container_of(mtd_to_nand(mtd
), struct jz4780_nand_chip
, chip
);
71 static inline struct jz4780_nand_controller
*to_jz4780_nand_controller(struct nand_hw_control
*ctrl
)
73 return container_of(ctrl
, struct jz4780_nand_controller
, controller
);
76 static void jz4780_nand_select_chip(struct mtd_info
*mtd
, int chipnr
)
78 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
79 struct jz4780_nand_controller
*nfc
= to_jz4780_nand_controller(nand
->chip
.controller
);
80 struct jz4780_nand_cs
*cs
;
82 /* Ensure the currently selected chip is deasserted. */
83 if (chipnr
== -1 && nfc
->selected
>= 0) {
84 cs
= &nfc
->cs
[nfc
->selected
];
85 jz4780_nemc_assert(nfc
->dev
, cs
->bank
, false);
88 nfc
->selected
= chipnr
;
91 static void jz4780_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
94 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
95 struct jz4780_nand_controller
*nfc
= to_jz4780_nand_controller(nand
->chip
.controller
);
96 struct jz4780_nand_cs
*cs
;
98 if (WARN_ON(nfc
->selected
< 0))
101 cs
= &nfc
->cs
[nfc
->selected
];
103 jz4780_nemc_assert(nfc
->dev
, cs
->bank
, ctrl
& NAND_NCE
);
105 if (cmd
== NAND_CMD_NONE
)
109 writeb(cmd
, cs
->base
+ OFFSET_ADDR
);
110 else if (ctrl
& NAND_CLE
)
111 writeb(cmd
, cs
->base
+ OFFSET_CMD
);
114 static int jz4780_nand_dev_ready(struct mtd_info
*mtd
)
116 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
118 return !gpiod_get_value_cansleep(nand
->busy_gpio
);
121 static void jz4780_nand_ecc_hwctl(struct mtd_info
*mtd
, int mode
)
123 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
125 nand
->reading
= (mode
== NAND_ECC_READ
);
128 static int jz4780_nand_ecc_calculate(struct mtd_info
*mtd
, const u8
*dat
,
131 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
132 struct jz4780_nand_controller
*nfc
= to_jz4780_nand_controller(nand
->chip
.controller
);
133 struct jz4780_bch_params params
;
136 * Don't need to generate the ECC when reading, BCH does it for us as
137 * part of decoding/correction.
142 params
.size
= nand
->chip
.ecc
.size
;
143 params
.bytes
= nand
->chip
.ecc
.bytes
;
144 params
.strength
= nand
->chip
.ecc
.strength
;
146 return jz4780_bch_calculate(nfc
->bch
, ¶ms
, dat
, ecc_code
);
149 static int jz4780_nand_ecc_correct(struct mtd_info
*mtd
, u8
*dat
,
150 u8
*read_ecc
, u8
*calc_ecc
)
152 struct jz4780_nand_chip
*nand
= to_jz4780_nand_chip(mtd
);
153 struct jz4780_nand_controller
*nfc
= to_jz4780_nand_controller(nand
->chip
.controller
);
154 struct jz4780_bch_params params
;
156 params
.size
= nand
->chip
.ecc
.size
;
157 params
.bytes
= nand
->chip
.ecc
.bytes
;
158 params
.strength
= nand
->chip
.ecc
.strength
;
160 return jz4780_bch_correct(nfc
->bch
, ¶ms
, dat
, read_ecc
);
163 static int jz4780_nand_init_ecc(struct jz4780_nand_chip
*nand
, struct device
*dev
)
165 struct nand_chip
*chip
= &nand
->chip
;
166 struct mtd_info
*mtd
= nand_to_mtd(chip
);
167 struct jz4780_nand_controller
*nfc
= to_jz4780_nand_controller(chip
->controller
);
168 struct nand_ecclayout
*layout
= &nand
->ecclayout
;
171 chip
->ecc
.bytes
= fls((1 + 8) * chip
->ecc
.size
) *
172 (chip
->ecc
.strength
/ 8);
174 switch (chip
->ecc
.mode
) {
177 dev_err(dev
, "HW BCH selected, but BCH controller not found\n");
181 chip
->ecc
.hwctl
= jz4780_nand_ecc_hwctl
;
182 chip
->ecc
.calculate
= jz4780_nand_ecc_calculate
;
183 chip
->ecc
.correct
= jz4780_nand_ecc_correct
;
186 case NAND_ECC_SOFT_BCH
:
187 dev_info(dev
, "using %s (strength %d, size %d, bytes %d)\n",
188 (nfc
->bch
) ? "hardware BCH" : "software ECC",
189 chip
->ecc
.strength
, chip
->ecc
.size
, chip
->ecc
.bytes
);
192 dev_info(dev
, "not using ECC\n");
195 dev_err(dev
, "ECC mode %d not supported\n", chip
->ecc
.mode
);
199 /* The NAND core will generate the ECC layout for SW ECC */
200 if (chip
->ecc
.mode
!= NAND_ECC_HW
)
203 /* Generate ECC layout. ECC codes are right aligned in the OOB area. */
204 layout
->eccbytes
= mtd
->writesize
/ chip
->ecc
.size
* chip
->ecc
.bytes
;
206 if (layout
->eccbytes
> mtd
->oobsize
- 2) {
208 "invalid ECC config: required %d ECC bytes, but only %d are available",
209 layout
->eccbytes
, mtd
->oobsize
- 2);
213 start
= mtd
->oobsize
- layout
->eccbytes
;
214 for (i
= 0; i
< layout
->eccbytes
; i
++)
215 layout
->eccpos
[i
] = start
+ i
;
217 layout
->oobfree
[0].offset
= 2;
218 layout
->oobfree
[0].length
= mtd
->oobsize
- layout
->eccbytes
- 2;
220 chip
->ecc
.layout
= layout
;
224 static int jz4780_nand_init_chip(struct platform_device
*pdev
,
225 struct jz4780_nand_controller
*nfc
,
226 struct device_node
*np
,
229 struct device
*dev
= &pdev
->dev
;
230 struct jz4780_nand_chip
*nand
;
231 struct jz4780_nand_cs
*cs
;
232 struct resource
*res
;
233 struct nand_chip
*chip
;
234 struct mtd_info
*mtd
;
238 cs
= &nfc
->cs
[chipnr
];
240 reg
= of_get_property(np
, "reg", NULL
);
244 cs
->bank
= be32_to_cpu(*reg
);
246 jz4780_nemc_set_type(nfc
->dev
, cs
->bank
, JZ4780_NEMC_BANK_NAND
);
248 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, chipnr
);
249 cs
->base
= devm_ioremap_resource(dev
, res
);
250 if (IS_ERR(cs
->base
))
251 return PTR_ERR(cs
->base
);
253 nand
= devm_kzalloc(dev
, sizeof(*nand
), GFP_KERNEL
);
257 nand
->busy_gpio
= devm_gpiod_get_optional(dev
, "rb", GPIOD_IN
);
259 if (IS_ERR(nand
->busy_gpio
)) {
260 ret
= PTR_ERR(nand
->busy_gpio
);
261 dev_err(dev
, "failed to request busy GPIO: %d\n", ret
);
263 } else if (nand
->busy_gpio
) {
264 nand
->chip
.dev_ready
= jz4780_nand_dev_ready
;
267 nand
->wp_gpio
= devm_gpiod_get_optional(dev
, "wp", GPIOD_OUT_LOW
);
269 if (IS_ERR(nand
->wp_gpio
)) {
270 ret
= PTR_ERR(nand
->wp_gpio
);
271 dev_err(dev
, "failed to request WP GPIO: %d\n", ret
);
276 mtd
= nand_to_mtd(chip
);
277 mtd
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s.%d", dev_name(dev
),
281 mtd
->dev
.parent
= dev
;
283 chip
->IO_ADDR_R
= cs
->base
+ OFFSET_DATA
;
284 chip
->IO_ADDR_W
= cs
->base
+ OFFSET_DATA
;
285 chip
->chip_delay
= RB_DELAY_US
;
286 chip
->options
= NAND_NO_SUBPAGE_WRITE
;
287 chip
->select_chip
= jz4780_nand_select_chip
;
288 chip
->cmd_ctrl
= jz4780_nand_cmd_ctrl
;
289 chip
->ecc
.mode
= NAND_ECC_HW
;
290 chip
->controller
= &nfc
->controller
;
291 nand_set_flash_node(chip
, np
);
293 ret
= nand_scan_ident(mtd
, 1, NULL
);
297 ret
= jz4780_nand_init_ecc(nand
, dev
);
301 ret
= nand_scan_tail(mtd
);
305 ret
= mtd_device_register(mtd
, NULL
, 0);
311 list_add_tail(&nand
->chip_list
, &nfc
->chips
);
316 static void jz4780_nand_cleanup_chips(struct jz4780_nand_controller
*nfc
)
318 struct jz4780_nand_chip
*chip
;
320 while (!list_empty(&nfc
->chips
)) {
321 chip
= list_first_entry(&nfc
->chips
, struct jz4780_nand_chip
, chip_list
);
322 nand_release(nand_to_mtd(&chip
->chip
));
323 list_del(&chip
->chip_list
);
327 static int jz4780_nand_init_chips(struct jz4780_nand_controller
*nfc
,
328 struct platform_device
*pdev
)
330 struct device
*dev
= &pdev
->dev
;
331 struct device_node
*np
;
334 int num_chips
= of_get_child_count(dev
->of_node
);
336 if (num_chips
> nfc
->num_banks
) {
337 dev_err(dev
, "found %d chips but only %d banks\n", num_chips
, nfc
->num_banks
);
341 for_each_child_of_node(dev
->of_node
, np
) {
342 ret
= jz4780_nand_init_chip(pdev
, nfc
, np
, i
);
344 jz4780_nand_cleanup_chips(nfc
);
354 static int jz4780_nand_probe(struct platform_device
*pdev
)
356 struct device
*dev
= &pdev
->dev
;
357 unsigned int num_banks
;
358 struct jz4780_nand_controller
*nfc
;
361 num_banks
= jz4780_nemc_num_banks(dev
);
362 if (num_banks
== 0) {
363 dev_err(dev
, "no banks found\n");
367 nfc
= devm_kzalloc(dev
, sizeof(*nfc
) + (sizeof(nfc
->cs
[0]) * num_banks
), GFP_KERNEL
);
372 * Check for BCH HW before we call nand_scan_ident, to prevent us from
373 * having to call it again if the BCH driver returns -EPROBE_DEFER.
375 nfc
->bch
= of_jz4780_bch_get(dev
->of_node
);
376 if (IS_ERR(nfc
->bch
))
377 return PTR_ERR(nfc
->bch
);
380 nfc
->num_banks
= num_banks
;
382 spin_lock_init(&nfc
->controller
.lock
);
383 INIT_LIST_HEAD(&nfc
->chips
);
384 init_waitqueue_head(&nfc
->controller
.wq
);
386 ret
= jz4780_nand_init_chips(nfc
, pdev
);
389 jz4780_bch_release(nfc
->bch
);
393 platform_set_drvdata(pdev
, nfc
);
397 static int jz4780_nand_remove(struct platform_device
*pdev
)
399 struct jz4780_nand_controller
*nfc
= platform_get_drvdata(pdev
);
402 jz4780_bch_release(nfc
->bch
);
404 jz4780_nand_cleanup_chips(nfc
);
409 static const struct of_device_id jz4780_nand_dt_match
[] = {
410 { .compatible
= "ingenic,jz4780-nand" },
413 MODULE_DEVICE_TABLE(of
, jz4780_nand_dt_match
);
415 static struct platform_driver jz4780_nand_driver
= {
416 .probe
= jz4780_nand_probe
,
417 .remove
= jz4780_nand_remove
,
420 .of_match_table
= of_match_ptr(jz4780_nand_dt_match
),
423 module_platform_driver(jz4780_nand_driver
);
425 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
426 MODULE_AUTHOR("Harvey Hunt <harvey.hunt@imgtec.com>");
427 MODULE_DESCRIPTION("Ingenic JZ4780 NAND driver");
428 MODULE_LICENSE("GPL v2");