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