Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Masahiro Yamada | b1c7273 | 2017-10-14 02:21:18 +0900 | [diff] [blame] | 7 | #include <clk.h> |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/ioport.h> |
Masahiro Yamada | 2cf4eba | 2017-11-30 13:45:26 +0900 | [diff] [blame] | 11 | #include <linux/printk.h> |
Marek Vasut | 54f8107 | 2020-01-21 20:03:11 +0100 | [diff] [blame] | 12 | #include <reset.h> |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 13 | |
| 14 | #include "denali.h" |
| 15 | |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 16 | struct denali_dt_data { |
| 17 | unsigned int revision; |
| 18 | unsigned int caps; |
Masahiro Yamada | 6be3873 | 2020-01-30 00:55:55 +0900 | [diff] [blame] | 19 | unsigned int oob_skip_bytes; |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 20 | const struct nand_ecc_caps *ecc_caps; |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 21 | }; |
| 22 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 23 | NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps, denali_calc_ecc_bytes, |
| 24 | 512, 8, 15); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 25 | static const struct denali_dt_data denali_socfpga_data = { |
| 26 | .caps = DENALI_CAP_HW_ECC_FIXUP, |
Masahiro Yamada | 6be3873 | 2020-01-30 00:55:55 +0900 | [diff] [blame] | 27 | .oob_skip_bytes = 2, |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 28 | .ecc_caps = &denali_socfpga_ecc_caps, |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 29 | }; |
| 30 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 31 | NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps, denali_calc_ecc_bytes, |
| 32 | 1024, 8, 16, 24); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 33 | static const struct denali_dt_data denali_uniphier_v5a_data = { |
| 34 | .caps = DENALI_CAP_HW_ECC_FIXUP | |
| 35 | DENALI_CAP_DMA_64BIT, |
Masahiro Yamada | 6be3873 | 2020-01-30 00:55:55 +0900 | [diff] [blame] | 36 | .oob_skip_bytes = 8, |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 37 | .ecc_caps = &denali_uniphier_v5a_ecc_caps, |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 38 | }; |
| 39 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 40 | NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps, denali_calc_ecc_bytes, |
| 41 | 1024, 8, 16); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 42 | static const struct denali_dt_data denali_uniphier_v5b_data = { |
| 43 | .revision = 0x0501, |
| 44 | .caps = DENALI_CAP_HW_ECC_FIXUP | |
| 45 | DENALI_CAP_DMA_64BIT, |
Masahiro Yamada | 6be3873 | 2020-01-30 00:55:55 +0900 | [diff] [blame] | 46 | .oob_skip_bytes = 8, |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 47 | .ecc_caps = &denali_uniphier_v5b_ecc_caps, |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 50 | static const struct udevice_id denali_nand_dt_ids[] = { |
| 51 | { |
| 52 | .compatible = "altr,socfpga-denali-nand", |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 53 | .data = (unsigned long)&denali_socfpga_data, |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 54 | }, |
| 55 | { |
| 56 | .compatible = "socionext,uniphier-denali-nand-v5a", |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 57 | .data = (unsigned long)&denali_uniphier_v5a_data, |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 58 | }, |
| 59 | { |
| 60 | .compatible = "socionext,uniphier-denali-nand-v5b", |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 61 | .data = (unsigned long)&denali_uniphier_v5b_data, |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 62 | }, |
| 63 | { /* sentinel */ } |
| 64 | }; |
| 65 | |
| 66 | static int denali_dt_probe(struct udevice *dev) |
| 67 | { |
| 68 | struct denali_nand_info *denali = dev_get_priv(dev); |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 69 | const struct denali_dt_data *data; |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 70 | struct clk clk, clk_x, clk_ecc; |
Marek Vasut | 54f8107 | 2020-01-21 20:03:11 +0100 | [diff] [blame] | 71 | struct reset_ctl_bulk resets; |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 72 | struct resource res; |
| 73 | int ret; |
| 74 | |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 75 | data = (void *)dev_get_driver_data(dev); |
Masahiro Yamada | 6be3873 | 2020-01-30 00:55:55 +0900 | [diff] [blame] | 76 | if (WARN_ON(!data)) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | denali->revision = data->revision; |
| 80 | denali->caps = data->caps; |
| 81 | denali->oob_skip_bytes = data->oob_skip_bytes; |
| 82 | denali->ecc_caps = data->ecc_caps; |
Masahiro Yamada | 54fde8e | 2017-09-15 21:43:19 +0900 | [diff] [blame] | 83 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 84 | denali->dev = dev; |
| 85 | |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 86 | ret = dev_read_resource_byname(dev, "denali_reg", &res); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 90 | denali->reg = devm_ioremap(dev, res.start, resource_size(&res)); |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 91 | |
| 92 | ret = dev_read_resource_byname(dev, "nand_data", &res); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | |
Masahiro Yamada | 8b0c16f | 2017-11-22 02:38:32 +0900 | [diff] [blame] | 96 | denali->host = devm_ioremap(dev, res.start, resource_size(&res)); |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 97 | |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 98 | ret = clk_get_by_name(dev, "nand", &clk); |
| 99 | if (ret) |
| 100 | ret = clk_get_by_index(dev, 0, &clk); |
Masahiro Yamada | b1c7273 | 2017-10-14 02:21:18 +0900 | [diff] [blame] | 101 | if (ret) |
Masahiro Yamada | bfee37c | 2020-01-21 20:03:10 +0100 | [diff] [blame] | 102 | clk.dev = NULL; |
Masahiro Yamada | b1c7273 | 2017-10-14 02:21:18 +0900 | [diff] [blame] | 103 | |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 104 | ret = clk_get_by_name(dev, "nand_x", &clk_x); |
| 105 | if (ret) |
| 106 | clk_x.dev = NULL; |
| 107 | |
| 108 | ret = clk_get_by_name(dev, "ecc", &clk_ecc); |
| 109 | if (ret) |
| 110 | clk_ecc.dev = NULL; |
| 111 | |
Masahiro Yamada | bfee37c | 2020-01-21 20:03:10 +0100 | [diff] [blame] | 112 | if (clk.dev) { |
| 113 | ret = clk_enable(&clk); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | } |
Masahiro Yamada | b1c7273 | 2017-10-14 02:21:18 +0900 | [diff] [blame] | 117 | |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 118 | if (clk_x.dev) { |
| 119 | ret = clk_enable(&clk_x); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | if (clk_ecc.dev) { |
| 125 | ret = clk_enable(&clk_ecc); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | if (clk_x.dev) { |
Masahiro Yamada | 2d1fbc8 | 2018-12-19 20:03:18 +0900 | [diff] [blame] | 131 | denali->clk_rate = clk_get_rate(&clk); |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 132 | denali->clk_x_rate = clk_get_rate(&clk_x); |
| 133 | } else { |
| 134 | /* |
| 135 | * Hardcode the clock rates for the backward compatibility. |
| 136 | * This works for both SOCFPGA and UniPhier. |
| 137 | */ |
| 138 | dev_notice(dev, |
| 139 | "necessary clock is missing. default clock rates are used.\n"); |
Masahiro Yamada | 2d1fbc8 | 2018-12-19 20:03:18 +0900 | [diff] [blame] | 140 | denali->clk_rate = 50000000; |
Masahiro Yamada | 8b86f5e | 2018-12-19 20:03:17 +0900 | [diff] [blame] | 141 | denali->clk_x_rate = 200000000; |
| 142 | } |
Masahiro Yamada | b1c7273 | 2017-10-14 02:21:18 +0900 | [diff] [blame] | 143 | |
Marek Vasut | 54f8107 | 2020-01-21 20:03:11 +0100 | [diff] [blame] | 144 | ret = reset_get_bulk(dev, &resets); |
Masahiro Yamada | 6b982ad | 2020-01-30 00:55:54 +0900 | [diff] [blame] | 145 | if (ret) { |
Simon Goldschmidt | cedfa4e | 2019-03-01 20:12:34 +0100 | [diff] [blame] | 146 | dev_warn(dev, "Can't get reset: %d\n", ret); |
Masahiro Yamada | 6b982ad | 2020-01-30 00:55:54 +0900 | [diff] [blame] | 147 | } else { |
Marek Vasut | 54f8107 | 2020-01-21 20:03:11 +0100 | [diff] [blame] | 148 | reset_deassert_bulk(&resets); |
Simon Goldschmidt | cedfa4e | 2019-03-01 20:12:34 +0100 | [diff] [blame] | 149 | |
Masahiro Yamada | 6b982ad | 2020-01-30 00:55:54 +0900 | [diff] [blame] | 150 | /* |
| 151 | * When the reset is deasserted, the initialization sequence is |
| 152 | * kicked (bootstrap process). The driver must wait until it is |
| 153 | * finished. Otherwise, it will result in unpredictable behavior. |
| 154 | */ |
| 155 | udelay(200); |
| 156 | } |
| 157 | |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 158 | return denali_init(denali); |
| 159 | } |
| 160 | |
| 161 | U_BOOT_DRIVER(denali_nand_dt) = { |
| 162 | .name = "denali-nand-dt", |
Masahiro Yamada | 8fc5382 | 2020-01-30 22:07:59 +0900 | [diff] [blame] | 163 | .id = UCLASS_MTD, |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 164 | .of_match = denali_nand_dt_ids, |
| 165 | .probe = denali_dt_probe, |
| 166 | .priv_auto_alloc_size = sizeof(struct denali_nand_info), |
| 167 | }; |
| 168 | |
| 169 | void board_nand_init(void) |
| 170 | { |
| 171 | struct udevice *dev; |
| 172 | int ret; |
| 173 | |
Masahiro Yamada | 8fc5382 | 2020-01-30 22:07:59 +0900 | [diff] [blame] | 174 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 175 | DM_GET_DRIVER(denali_nand_dt), |
| 176 | &dev); |
| 177 | if (ret && ret != -ENODEV) |
Masahiro Yamada | 2cf4eba | 2017-11-30 13:45:26 +0900 | [diff] [blame] | 178 | pr_err("Failed to initialize Denali NAND controller. (error %d)\n", |
Masahiro Yamada | 9c5a5dd | 2017-08-26 01:12:31 +0900 | [diff] [blame] | 179 | ret); |
| 180 | } |