blob: 2728e8098faa517476497f4b42e3394359b439ea [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>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glass1e268642020-05-10 11:39:55 -060010#include <linux/bug.h>
11#include <linux/delay.h>
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090012#include <linux/io.h>
13#include <linux/ioport.h>
Masahiro Yamada2cf4eba2017-11-30 13:45:26 +090014#include <linux/printk.h>
Marek Vasut54f81072020-01-21 20:03:11 +010015#include <reset.h>
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090016
17#include "denali.h"
18
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090019struct denali_dt_data {
20 unsigned int revision;
21 unsigned int caps;
Masahiro Yamada6be38732020-01-30 00:55:55 +090022 unsigned int oob_skip_bytes;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090023 const struct nand_ecc_caps *ecc_caps;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090024};
25
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090026NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps, denali_calc_ecc_bytes,
27 512, 8, 15);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090028static const struct denali_dt_data denali_socfpga_data = {
29 .caps = DENALI_CAP_HW_ECC_FIXUP,
Masahiro Yamada6be38732020-01-30 00:55:55 +090030 .oob_skip_bytes = 2,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090031 .ecc_caps = &denali_socfpga_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090032};
33
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090034NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps, denali_calc_ecc_bytes,
35 1024, 8, 16, 24);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090036static const struct denali_dt_data denali_uniphier_v5a_data = {
37 .caps = DENALI_CAP_HW_ECC_FIXUP |
38 DENALI_CAP_DMA_64BIT,
Masahiro Yamada6be38732020-01-30 00:55:55 +090039 .oob_skip_bytes = 8,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090040 .ecc_caps = &denali_uniphier_v5a_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090041};
42
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090043NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps, denali_calc_ecc_bytes,
44 1024, 8, 16);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090045static 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 Yamada6be38732020-01-30 00:55:55 +090049 .oob_skip_bytes = 8,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090050 .ecc_caps = &denali_uniphier_v5b_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090051};
52
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090053static const struct udevice_id denali_nand_dt_ids[] = {
54 {
55 .compatible = "altr,socfpga-denali-nand",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090056 .data = (unsigned long)&denali_socfpga_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090057 },
58 {
59 .compatible = "socionext,uniphier-denali-nand-v5a",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090060 .data = (unsigned long)&denali_uniphier_v5a_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090061 },
62 {
63 .compatible = "socionext,uniphier-denali-nand-v5b",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090064 .data = (unsigned long)&denali_uniphier_v5b_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090065 },
66 { /* sentinel */ }
67};
68
69static int denali_dt_probe(struct udevice *dev)
70{
71 struct denali_nand_info *denali = dev_get_priv(dev);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090072 const struct denali_dt_data *data;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090073 struct clk clk, clk_x, clk_ecc;
Marek Vasut54f81072020-01-21 20:03:11 +010074 struct reset_ctl_bulk resets;
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090075 struct resource res;
76 int ret;
77
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090078 data = (void *)dev_get_driver_data(dev);
Masahiro Yamada6be38732020-01-30 00:55:55 +090079 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 Yamada54fde8e2017-09-15 21:43:19 +090086
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090087 denali->dev = dev;
88
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090089 ret = dev_read_resource_byname(dev, "denali_reg", &res);
90 if (ret)
91 return ret;
92
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090093 denali->reg = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090094
95 ret = dev_read_resource_byname(dev, "nand_data", &res);
96 if (ret)
97 return ret;
98
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090099 denali->host = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900100
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900101 ret = clk_get_by_name(dev, "nand", &clk);
102 if (ret)
103 ret = clk_get_by_index(dev, 0, &clk);
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900104 if (ret)
Masahiro Yamadabfee37c2020-01-21 20:03:10 +0100105 clk.dev = NULL;
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900106
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900107 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 Yamadabfee37c2020-01-21 20:03:10 +0100115 if (clk.dev) {
116 ret = clk_enable(&clk);
117 if (ret)
118 return ret;
119 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900120
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900121 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 Yamada2d1fbc82018-12-19 20:03:18 +0900134 denali->clk_rate = clk_get_rate(&clk);
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900135 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 Yamada2d1fbc82018-12-19 20:03:18 +0900143 denali->clk_rate = 50000000;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900144 denali->clk_x_rate = 200000000;
145 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900146
Marek Vasut54f81072020-01-21 20:03:11 +0100147 ret = reset_get_bulk(dev, &resets);
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900148 if (ret) {
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100149 dev_warn(dev, "Can't get reset: %d\n", ret);
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900150 } else {
Marek Vasut54f81072020-01-21 20:03:11 +0100151 reset_deassert_bulk(&resets);
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100152
Masahiro Yamada6b982ad2020-01-30 00:55:54 +0900153 /*
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 Yamada9c5a5dd2017-08-26 01:12:31 +0900161 return denali_init(denali);
162}
163
164U_BOOT_DRIVER(denali_nand_dt) = {
165 .name = "denali-nand-dt",
Masahiro Yamada8fc53822020-01-30 22:07:59 +0900166 .id = UCLASS_MTD,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900167 .of_match = denali_nand_dt_ids,
168 .probe = denali_dt_probe,
169 .priv_auto_alloc_size = sizeof(struct denali_nand_info),
170};
171
172void board_nand_init(void)
173{
174 struct udevice *dev;
175 int ret;
176
Masahiro Yamada8fc53822020-01-30 22:07:59 +0900177 ret = uclass_get_device_by_driver(UCLASS_MTD,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900178 DM_GET_DRIVER(denali_nand_dt),
179 &dev);
180 if (ret && ret != -ENODEV)
Masahiro Yamada2cf4eba2017-11-30 13:45:26 +0900181 pr_err("Failed to initialize Denali NAND controller. (error %d)\n",
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900182 ret);
183}