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