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