1 // SPDX-License-Identifier: GPL-2.0
3 * NAND Flash Controller Device Driver for DT
5 * Copyright © 2011, Picochip.
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include <linux/ioport.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/reset.h>
23 struct denali_controller controller
;
24 struct clk
*clk
; /* core clock */
25 struct clk
*clk_x
; /* bus interface clock */
26 struct clk
*clk_ecc
; /* ECC circuit clock */
27 struct reset_control
*rst
; /* core reset */
28 struct reset_control
*rst_reg
; /* register reset */
31 struct denali_dt_data
{
32 unsigned int revision
;
34 unsigned int oob_skip_bytes
;
35 const struct nand_ecc_caps
*ecc_caps
;
38 NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps
, denali_calc_ecc_bytes
,
40 static const struct denali_dt_data denali_socfpga_data
= {
41 .caps
= DENALI_CAP_HW_ECC_FIXUP
,
43 .ecc_caps
= &denali_socfpga_ecc_caps
,
46 NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps
, denali_calc_ecc_bytes
,
48 static const struct denali_dt_data denali_uniphier_v5a_data
= {
49 .caps
= DENALI_CAP_HW_ECC_FIXUP
|
52 .ecc_caps
= &denali_uniphier_v5a_ecc_caps
,
55 NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps
, denali_calc_ecc_bytes
,
57 static const struct denali_dt_data denali_uniphier_v5b_data
= {
59 .caps
= DENALI_CAP_HW_ECC_FIXUP
|
62 .ecc_caps
= &denali_uniphier_v5b_ecc_caps
,
65 static const struct of_device_id denali_nand_dt_ids
[] = {
67 .compatible
= "altr,socfpga-denali-nand",
68 .data
= &denali_socfpga_data
,
71 .compatible
= "socionext,uniphier-denali-nand-v5a",
72 .data
= &denali_uniphier_v5a_data
,
75 .compatible
= "socionext,uniphier-denali-nand-v5b",
76 .data
= &denali_uniphier_v5b_data
,
80 MODULE_DEVICE_TABLE(of
, denali_nand_dt_ids
);
82 static int denali_dt_chip_init(struct denali_controller
*denali
,
83 struct device_node
*chip_np
)
85 struct denali_chip
*dchip
;
89 nsels
= of_property_count_u32_elems(chip_np
, "reg");
93 dchip
= devm_kzalloc(denali
->dev
, struct_size(dchip
, sels
, nsels
),
100 for (i
= 0; i
< nsels
; i
++) {
101 ret
= of_property_read_u32_index(chip_np
, "reg", i
, &bank
);
105 dchip
->sels
[i
].bank
= bank
;
107 nand_set_flash_node(&dchip
->chip
, chip_np
);
110 return denali_chip_init(denali
, dchip
);
113 static int denali_dt_probe(struct platform_device
*pdev
)
115 struct device
*dev
= &pdev
->dev
;
116 struct resource
*res
;
117 struct denali_dt
*dt
;
118 const struct denali_dt_data
*data
;
119 struct denali_controller
*denali
;
120 struct device_node
*np
;
123 dt
= devm_kzalloc(dev
, sizeof(*dt
), GFP_KERNEL
);
126 denali
= &dt
->controller
;
128 data
= of_device_get_match_data(dev
);
132 denali
->revision
= data
->revision
;
133 denali
->caps
= data
->caps
;
134 denali
->oob_skip_bytes
= data
->oob_skip_bytes
;
135 denali
->ecc_caps
= data
->ecc_caps
;
138 denali
->irq
= platform_get_irq(pdev
, 0);
142 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "denali_reg");
143 denali
->reg
= devm_ioremap_resource(dev
, res
);
144 if (IS_ERR(denali
->reg
))
145 return PTR_ERR(denali
->reg
);
147 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "nand_data");
148 denali
->host
= devm_ioremap_resource(dev
, res
);
149 if (IS_ERR(denali
->host
))
150 return PTR_ERR(denali
->host
);
152 dt
->clk
= devm_clk_get(dev
, "nand");
154 return PTR_ERR(dt
->clk
);
156 dt
->clk_x
= devm_clk_get(dev
, "nand_x");
157 if (IS_ERR(dt
->clk_x
))
158 return PTR_ERR(dt
->clk_x
);
160 dt
->clk_ecc
= devm_clk_get(dev
, "ecc");
161 if (IS_ERR(dt
->clk_ecc
))
162 return PTR_ERR(dt
->clk_ecc
);
164 dt
->rst
= devm_reset_control_get_optional_shared(dev
, "nand");
166 return PTR_ERR(dt
->rst
);
168 dt
->rst_reg
= devm_reset_control_get_optional_shared(dev
, "reg");
169 if (IS_ERR(dt
->rst_reg
))
170 return PTR_ERR(dt
->rst_reg
);
172 ret
= clk_prepare_enable(dt
->clk
);
176 ret
= clk_prepare_enable(dt
->clk_x
);
178 goto out_disable_clk
;
180 ret
= clk_prepare_enable(dt
->clk_ecc
);
182 goto out_disable_clk_x
;
184 denali
->clk_rate
= clk_get_rate(dt
->clk
);
185 denali
->clk_x_rate
= clk_get_rate(dt
->clk_x
);
188 * Deassert the register reset, and the core reset in this order.
189 * Deasserting the core reset while the register reset is asserted
190 * will cause unpredictable behavior in the controller.
192 ret
= reset_control_deassert(dt
->rst_reg
);
194 goto out_disable_clk_ecc
;
196 ret
= reset_control_deassert(dt
->rst
);
198 goto out_assert_rst_reg
;
201 * When the reset is deasserted, the initialization sequence is kicked
202 * (bootstrap process). The driver must wait until it finished.
203 * Otherwise, it will result in unpredictable behavior.
205 usleep_range(200, 1000);
207 ret
= denali_init(denali
);
211 for_each_child_of_node(dev
->of_node
, np
) {
212 ret
= denali_dt_chip_init(denali
, np
);
215 goto out_remove_denali
;
219 platform_set_drvdata(pdev
, dt
);
224 denali_remove(denali
);
226 reset_control_assert(dt
->rst
);
228 reset_control_assert(dt
->rst_reg
);
230 clk_disable_unprepare(dt
->clk_ecc
);
232 clk_disable_unprepare(dt
->clk_x
);
234 clk_disable_unprepare(dt
->clk
);
239 static int denali_dt_remove(struct platform_device
*pdev
)
241 struct denali_dt
*dt
= platform_get_drvdata(pdev
);
243 denali_remove(&dt
->controller
);
244 reset_control_assert(dt
->rst
);
245 reset_control_assert(dt
->rst_reg
);
246 clk_disable_unprepare(dt
->clk_ecc
);
247 clk_disable_unprepare(dt
->clk_x
);
248 clk_disable_unprepare(dt
->clk
);
253 static struct platform_driver denali_dt_driver
= {
254 .probe
= denali_dt_probe
,
255 .remove
= denali_dt_remove
,
257 .name
= "denali-nand-dt",
258 .of_match_table
= denali_nand_dt_ids
,
261 module_platform_driver(denali_dt_driver
);
263 MODULE_LICENSE("GPL v2");
264 MODULE_AUTHOR("Jamie Iles");
265 MODULE_DESCRIPTION("DT driver for Denali NAND controller");