| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* NAND Flash Controller Device Driver for DT |
| 4 |
* |
| 5 |
* Copyright © 2011, Picochip. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/clk.h> |
| 9 |
#include <linux/delay.h> |
| 10 |
#include <linux/err.h> |
| 11 |
#include <linux/io.h> |
| 12 |
#include <linux/ioport.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/of.h> |
| 16 |
#include <linux/platform_device.h> |
| 17 |
#include <linux/reset.h> |
| 18 |
|
| 19 |
#include "denali.h" |
| 20 |
|
| 21 |
struct denali_dt { |
| 22 |
struct denali_controller controller; |
| 23 |
struct clk *clk; /* core clock */ |
| 24 |
struct clk *clk_x; /* bus interface clock */ |
| 25 |
struct clk *clk_ecc; /* ECC circuit clock */ |
| 26 |
struct reset_control *rst; /* core reset */ |
| 27 |
struct reset_control *rst_reg; /* register reset */ |
| 28 |
}; |
| 29 |
|
| 30 |
struct denali_dt_data { |
| 31 |
unsigned int revision; |
| 32 |
unsigned int caps; |
| 33 |
unsigned int oob_skip_bytes; |
| 34 |
const struct nand_ecc_caps *ecc_caps; |
| 35 |
}; |
| 36 |
|
| 37 |
NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps, denali_calc_ecc_bytes, |
| 38 |
512, 8, 15); |
| 39 |
static const struct denali_dt_data denali_socfpga_data = { |
| 40 |
.caps = DENALI_CAP_HW_ECC_FIXUP, |
| 41 |
.oob_skip_bytes = 2, |
| 42 |
.ecc_caps = &denali_socfpga_ecc_caps, |
| 43 |
}; |
| 44 |
|
| 45 |
NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps, denali_calc_ecc_bytes, |
| 46 |
1024, 8, 16, 24); |
| 47 |
static const struct denali_dt_data denali_uniphier_v5a_data = { |
| 48 |
.caps = DENALI_CAP_HW_ECC_FIXUP | |
| 49 |
DENALI_CAP_DMA_64BIT, |
| 50 |
.oob_skip_bytes = 8, |
| 51 |
.ecc_caps = &denali_uniphier_v5a_ecc_caps, |
| 52 |
}; |
| 53 |
|
| 54 |
NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps, denali_calc_ecc_bytes, |
| 55 |
1024, 8, 16); |
| 56 |
static const struct denali_dt_data denali_uniphier_v5b_data = { |
| 57 |
.revision = 0x0501, |
| 58 |
.caps = DENALI_CAP_HW_ECC_FIXUP | |
| 59 |
DENALI_CAP_DMA_64BIT, |
| 60 |
.oob_skip_bytes = 8, |
| 61 |
.ecc_caps = &denali_uniphier_v5b_ecc_caps, |
| 62 |
}; |
| 63 |
|
| 64 |
static const struct of_device_id denali_nand_dt_ids[] = { |
| 65 |
{ |
| 66 |
.compatible = "altr,socfpga-denali-nand", |
| 67 |
.data = &denali_socfpga_data, |
| 68 |
}, |
| 69 |
{ |
| 70 |
.compatible = "socionext,uniphier-denali-nand-v5a", |
| 71 |
.data = &denali_uniphier_v5a_data, |
| 72 |
}, |
| 73 |
{ |
| 74 |
.compatible = "socionext,uniphier-denali-nand-v5b", |
| 75 |
.data = &denali_uniphier_v5b_data, |
| 76 |
}, |
| 77 |
{ /* sentinel */ } |
| 78 |
}; |
| 79 |
MODULE_DEVICE_TABLE(of, denali_nand_dt_ids); |
| 80 |
|
| 81 |
static int denali_dt_chip_init(struct denali_controller *denali, |
| 82 |
struct device_node *chip_np) |
| 83 |
{ |
| 84 |
struct denali_chip *dchip; |
| 85 |
u32 bank; |
| 86 |
int nsels, i, ret; |
| 87 |
|
| 88 |
nsels = of_property_count_u32_elems(chip_np, "reg"); |
| 89 |
if (nsels < 0) |
| 90 |
return nsels; |
| 91 |
|
| 92 |
dchip = devm_kzalloc(denali->dev, struct_size(dchip, sels, nsels), |
| 93 |
GFP_KERNEL); |
| 94 |
if (!dchip) |
| 95 |
return -ENOMEM; |
| 96 |
|
| 97 |
dchip->nsels = nsels; |
| 98 |
|
| 99 |
for (i = 0; i < nsels; i++) { |
| 100 |
ret = of_property_read_u32_index(chip_np, "reg", i, &bank); |
| 101 |
if (ret) |
| 102 |
return ret; |
| 103 |
|
| 104 |
dchip->sels[i].bank = bank; |
| 105 |
|
| 106 |
nand_set_flash_node(&dchip->chip, chip_np); |
| 107 |
} |
| 108 |
|
| 109 |
return denali_chip_init(denali, dchip); |
| 110 |
} |
| 111 |
|
| 112 |
static int denali_dt_probe(struct platform_device *pdev) |
| 113 |
{ |
| 114 |
struct device *dev = &pdev->dev; |
| 115 |
struct denali_dt *dt; |
| 116 |
const struct denali_dt_data *data; |
| 117 |
struct denali_controller *denali; |
| 118 |
int ret; |
| 119 |
|
| 120 |
dt = devm_kzalloc(dev, sizeof(*dt), GFP_KERNEL); |
| 121 |
if (!dt) |
| 122 |
return -ENOMEM; |
| 123 |
denali = &dt->controller; |
| 124 |
|
| 125 |
data = of_device_get_match_data(dev); |
| 126 |
if (WARN_ON(!data)) |
| 127 |
return -EINVAL; |
| 128 |
|
| 129 |
denali->revision = data->revision; |
| 130 |
denali->caps = data->caps; |
| 131 |
denali->oob_skip_bytes = data->oob_skip_bytes; |
| 132 |
denali->ecc_caps = data->ecc_caps; |
| 133 |
|
| 134 |
denali->dev = dev; |
| 135 |
denali->irq = platform_get_irq(pdev, 0); |
| 136 |
if (denali->irq < 0) |
| 137 |
return denali->irq; |
| 138 |
|
| 139 |
denali->reg = devm_platform_ioremap_resource_byname(pdev, "denali_reg"); |
| 140 |
if (IS_ERR(denali->reg)) |
| 141 |
return PTR_ERR(denali->reg); |
| 142 |
|
| 143 |
denali->host = devm_platform_ioremap_resource_byname(pdev, "nand_data"); |
| 144 |
if (IS_ERR(denali->host)) |
| 145 |
return PTR_ERR(denali->host); |
| 146 |
|
| 147 |
dt->clk = devm_clk_get_enabled(dev, "nand"); |
| 148 |
if (IS_ERR(dt->clk)) |
| 149 |
return PTR_ERR(dt->clk); |
| 150 |
|
| 151 |
dt->clk_x = devm_clk_get_enabled(dev, "nand_x"); |
| 152 |
if (IS_ERR(dt->clk_x)) |
| 153 |
return PTR_ERR(dt->clk_x); |
| 154 |
|
| 155 |
dt->clk_ecc = devm_clk_get_enabled(dev, "ecc"); |
| 156 |
if (IS_ERR(dt->clk_ecc)) |
| 157 |
return PTR_ERR(dt->clk_ecc); |
| 158 |
|
| 159 |
dt->rst = devm_reset_control_get_optional_shared(dev, "nand"); |
| 160 |
if (IS_ERR(dt->rst)) |
| 161 |
return PTR_ERR(dt->rst); |
| 162 |
|
| 163 |
dt->rst_reg = devm_reset_control_get_optional_shared(dev, "reg"); |
| 164 |
if (IS_ERR(dt->rst_reg)) |
| 165 |
return PTR_ERR(dt->rst_reg); |
| 166 |
|
| 167 |
denali->clk_rate = clk_get_rate(dt->clk); |
| 168 |
denali->clk_x_rate = clk_get_rate(dt->clk_x); |
| 169 |
|
| 170 |
/* |
| 171 |
* Deassert the register reset, and the core reset in this order. |
| 172 |
* Deasserting the core reset while the register reset is asserted |
| 173 |
* will cause unpredictable behavior in the controller. |
| 174 |
*/ |
| 175 |
ret = reset_control_deassert(dt->rst_reg); |
| 176 |
if (ret) |
| 177 |
return ret; |
| 178 |
|
| 179 |
ret = reset_control_deassert(dt->rst); |
| 180 |
if (ret) |
| 181 |
goto out_assert_rst_reg; |
| 182 |
|
| 183 |
/* |
| 184 |
* When the reset is deasserted, the initialization sequence is kicked |
| 185 |
* (bootstrap process). The driver must wait until it finished. |
| 186 |
* Otherwise, it will result in unpredictable behavior. |
| 187 |
*/ |
| 188 |
usleep_range(200, 1000); |
| 189 |
|
| 190 |
ret = denali_init(denali); |
| 191 |
if (ret) |
| 192 |
goto out_assert_rst; |
| 193 |
|
| 194 |
for_each_child_of_node_scoped(dev->of_node, np) { |
| 195 |
ret = denali_dt_chip_init(denali, np); |
| 196 |
if (ret) |
| 197 |
goto out_remove_denali; |
| 198 |
} |
| 199 |
|
| 200 |
platform_set_drvdata(pdev, dt); |
| 201 |
|
| 202 |
return 0; |
| 203 |
|
| 204 |
out_remove_denali: |
| 205 |
denali_remove(denali); |
| 206 |
out_assert_rst: |
| 207 |
reset_control_assert(dt->rst); |
| 208 |
out_assert_rst_reg: |
| 209 |
reset_control_assert(dt->rst_reg); |
| 210 |
|
| 211 |
return ret; |
| 212 |
} |
| 213 |
|
| 214 |
static void denali_dt_remove(struct platform_device *pdev) |
| 215 |
{ |
| 216 |
struct denali_dt *dt = platform_get_drvdata(pdev); |
| 217 |
|
| 218 |
denali_remove(&dt->controller); |
| 219 |
reset_control_assert(dt->rst); |
| 220 |
reset_control_assert(dt->rst_reg); |
| 221 |
} |
| 222 |
|
| 223 |
static struct platform_driver denali_dt_driver = { |
| 224 |
.probe = denali_dt_probe, |
| 225 |
.remove = denali_dt_remove, |
| 226 |
.driver = { |
| 227 |
.name = "denali-nand-dt", |
| 228 |
.of_match_table = denali_nand_dt_ids, |
| 229 |
}, |
| 230 |
}; |
| 231 |
module_platform_driver(denali_dt_driver); |
| 232 |
|
| 233 |
MODULE_LICENSE("GPL v2"); |
| 234 |
MODULE_AUTHOR("Jamie Iles"); |
| 235 |
MODULE_DESCRIPTION("DT driver for Denali NAND controller"); |