blob: 91d0f20aaeb073304c361a6ce80b27c969e2fafc [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 Yamada8b0c16f2017-11-22 02:38:32 +090019 const struct nand_ecc_caps *ecc_caps;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090020};
21
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090022NAND_ECC_CAPS_SINGLE(denali_socfpga_ecc_caps, denali_calc_ecc_bytes,
23 512, 8, 15);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090024static const struct denali_dt_data denali_socfpga_data = {
25 .caps = DENALI_CAP_HW_ECC_FIXUP,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090026 .ecc_caps = &denali_socfpga_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090027};
28
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090029NAND_ECC_CAPS_SINGLE(denali_uniphier_v5a_ecc_caps, denali_calc_ecc_bytes,
30 1024, 8, 16, 24);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090031static const struct denali_dt_data denali_uniphier_v5a_data = {
32 .caps = DENALI_CAP_HW_ECC_FIXUP |
33 DENALI_CAP_DMA_64BIT,
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090034 .ecc_caps = &denali_uniphier_v5a_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090035};
36
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090037NAND_ECC_CAPS_SINGLE(denali_uniphier_v5b_ecc_caps, denali_calc_ecc_bytes,
38 1024, 8, 16);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090039static 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 Yamada8b0c16f2017-11-22 02:38:32 +090043 .ecc_caps = &denali_uniphier_v5b_ecc_caps,
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090044};
45
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090046static const struct udevice_id denali_nand_dt_ids[] = {
47 {
48 .compatible = "altr,socfpga-denali-nand",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090049 .data = (unsigned long)&denali_socfpga_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090050 },
51 {
52 .compatible = "socionext,uniphier-denali-nand-v5a",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090053 .data = (unsigned long)&denali_uniphier_v5a_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090054 },
55 {
56 .compatible = "socionext,uniphier-denali-nand-v5b",
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090057 .data = (unsigned long)&denali_uniphier_v5b_data,
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090058 },
59 { /* sentinel */ }
60};
61
62static int denali_dt_probe(struct udevice *dev)
63{
64 struct denali_nand_info *denali = dev_get_priv(dev);
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090065 const struct denali_dt_data *data;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090066 struct clk clk, clk_x, clk_ecc;
Marek Vasut54f81072020-01-21 20:03:11 +010067 struct reset_ctl_bulk resets;
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090068 struct resource res;
69 int ret;
70
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090071 data = (void *)dev_get_driver_data(dev);
72 if (data) {
73 denali->revision = data->revision;
74 denali->caps = data->caps;
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090075 denali->ecc_caps = data->ecc_caps;
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090076 }
77
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090078 denali->dev = dev;
79
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090080 ret = dev_read_resource_byname(dev, "denali_reg", &res);
81 if (ret)
82 return ret;
83
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090084 denali->reg = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090085
86 ret = dev_read_resource_byname(dev, "nand_data", &res);
87 if (ret)
88 return ret;
89
Masahiro Yamada8b0c16f2017-11-22 02:38:32 +090090 denali->host = devm_ioremap(dev, res.start, resource_size(&res));
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +090091
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090092 ret = clk_get_by_name(dev, "nand", &clk);
93 if (ret)
94 ret = clk_get_by_index(dev, 0, &clk);
Masahiro Yamadab1c72732017-10-14 02:21:18 +090095 if (ret)
Masahiro Yamadabfee37c2020-01-21 20:03:10 +010096 clk.dev = NULL;
Masahiro Yamadab1c72732017-10-14 02:21:18 +090097
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +090098 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 Yamadabfee37c2020-01-21 20:03:10 +0100106 if (clk.dev) {
107 ret = clk_enable(&clk);
108 if (ret)
109 return ret;
110 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900111
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900112 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 Yamada2d1fbc82018-12-19 20:03:18 +0900125 denali->clk_rate = clk_get_rate(&clk);
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900126 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 Yamada2d1fbc82018-12-19 20:03:18 +0900134 denali->clk_rate = 50000000;
Masahiro Yamada8b86f5e2018-12-19 20:03:17 +0900135 denali->clk_x_rate = 200000000;
136 }
Masahiro Yamadab1c72732017-10-14 02:21:18 +0900137
Marek Vasut54f81072020-01-21 20:03:11 +0100138 ret = reset_get_bulk(dev, &resets);
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100139 if (ret)
140 dev_warn(dev, "Can't get reset: %d\n", ret);
141 else
Marek Vasut54f81072020-01-21 20:03:11 +0100142 reset_deassert_bulk(&resets);
Simon Goldschmidtcedfa4e2019-03-01 20:12:34 +0100143
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900144 return denali_init(denali);
145}
146
147U_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
155void 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 Yamada2cf4eba2017-11-30 13:45:26 +0900164 pr_err("Failed to initialize Denali NAND controller. (error %d)\n",
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +0900165 ret);
166}