blob: 759ad40e517871e4b543ea33a37d49d4865e07b0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09002/*
3 * Copyright (C) 2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09005 */
6
Masahiro Yamadab1c72732017-10-14 02:21:18 +09007#include <clk.h>
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09008#include <dm.h>
9#include <linux/io.h>
10#include <linux/ioport.h>
Masahiro Yamada2cf4eba2017-11-30 13:45:26 +090011#include <linux/printk.h>
Marek Vasut54f81072020-01-21 20:03:11 +010012#include <reset.h>
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090013
14#include "denali.h"
15
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090016struct denali_dt_data {
17 unsigned int revision;
18 unsigned int caps;
Masahiro Yamada6be38732020-01-30 00:55:55 +090019 unsigned int oob_skip_bytes;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090020 const struct nand_ecc_caps *ecc_caps;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090021};
22
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090023NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps, denali_calc_ecc_bytes,
24 512, 8, 15);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090025static const struct denali_dt_data denali_socfpga_data = {
26 .caps = DENALI_CAP_HW_ECC_FIXUP,
Masahiro Yamada6be38732020-01-30 00:55:55 +090027 .oob_skip_bytes = 2,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090028 .ecc_caps = &denali_socfpga_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090029};
30
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090031NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps, denali_calc_ecc_bytes,
32 1024, 8, 16, 24);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090033static const struct denali_dt_data denali_uniphier_v5a_data = {
34 .caps = DENALI_CAP_HW_ECC_FIXUP |
35 DENALI_CAP_DMA_64BIT,
Masahiro Yamada6be38732020-01-30 00:55:55 +090036 .oob_skip_bytes = 8,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090037 .ecc_caps = &denali_uniphier_v5a_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090038};
39
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090040NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps, denali_calc_ecc_bytes,
41 1024, 8, 16);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090042static 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 Yamada6be38732020-01-30 00:55:55 +090046 .oob_skip_bytes = 8,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090047 .ecc_caps = &denali_uniphier_v5b_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090048};
49
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090050static const struct udevice_id denali_nand_dt_ids[] = {
51 {
52 .compatible = "altr,socfpga-denali-nand",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090053 .data = (unsigned long)&denali_socfpga_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090054 },
55 {
56 .compatible = "socionext,uniphier-denali-nand-v5a",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090057 .data = (unsigned long)&denali_uniphier_v5a_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090058 },
59 {
60 .compatible = "socionext,uniphier-denali-nand-v5b",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090061 .data = (unsigned long)&denali_uniphier_v5b_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090062 },
63 { /* sentinel */ }
64};
65
66static int denali_dt_probe(struct udevice *dev)
67{
68 struct denali_nand_info *denali = dev_get_priv(dev);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090069 const struct denali_dt_data *data;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090070 struct clk clk, clk_x, clk_ecc;
Marek Vasut54f81072020-01-21 20:03:11 +010071 struct reset_ctl_bulk resets;
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090072 struct resource res;
73 int ret;
74
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090075 data = (void *)dev_get_driver_data(dev);
Masahiro Yamada6be38732020-01-30 00:55:55 +090076 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 Yamada54fde8e2017-09-15 21:43:19 +090083
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090084 denali->dev = dev;
85
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090086 ret = dev_read_resource_byname(dev, "denali_reg", &res);
87 if (ret)
88 return ret;
89
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090090 denali->reg = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090091
92 ret = dev_read_resource_byname(dev, "nand_data", &res);
93 if (ret)
94 return ret;
95
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090096 denali->host = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090097
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090098 ret = clk_get_by_name(dev, "nand", &clk);
99 if (ret)
100 ret = clk_get_by_index(dev, 0, &clk);
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900101 if (ret)
Masahiro Yamadabfee37c2020-01-21 20:03:10 +0100102 clk.dev = NULL;
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900103
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900104 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 Yamadabfee37c2020-01-21 20:03:10 +0100112 if (clk.dev) {
113 ret = clk_enable(&clk);
114 if (ret)
115 return ret;
116 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900117
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900118 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 Yamada2d1fbc82018-12-19 20:03:18 +0900131 denali->clk_rate = clk_get_rate(&clk);
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900132 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 Yamada2d1fbc82018-12-19 20:03:18 +0900140 denali->clk_rate = 50000000;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900141 denali->clk_x_rate = 200000000;
142 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900143
Marek Vasut54f81072020-01-21 20:03:11 +0100144 ret = reset_get_bulk(dev, &resets);
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900145 if (ret) {
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100146 dev_warn(dev, "Can't get reset: %d\n", ret);
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900147 } else {
Marek Vasut54f81072020-01-21 20:03:11 +0100148 reset_deassert_bulk(&resets);
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100149
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900150 /*
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 Yamada9c5a5dd2017-08-26 01:12:31 +0900158 return denali_init(denali);
159}
160
161U_BOOT_DRIVER(denali_nand_dt) = {
162 .name = "denali-nand-dt",
Masahiro Yamada8fc53822020-01-30 22:07:59 +0900163 .id = UCLASS_MTD,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900164 .of_match = denali_nand_dt_ids,
165 .probe = denali_dt_probe,
166 .priv_auto_alloc_size = sizeof(struct denali_nand_info),
167};
168
169void board_nand_init(void)
170{
171 struct udevice *dev;
172 int ret;
173
Masahiro Yamada8fc53822020-01-30 22:07:59 +0900174 ret = uclass_get_device_by_driver(UCLASS_MTD,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900175 DM_GET_DRIVER(denali_nand_dt),
176 &dev);
177 if (ret && ret != -ENODEV)
Masahiro Yamada2cf4eba2017-11-30 13:45:26 +0900178 pr_err("Failed to initialize Denali NAND controller. (error %d)\n",
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900179 ret);
180}