183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a111bfbfSMasahiro Yamada /*
34e3d8406SMasahiro Yamada * Copyright (C) 2016 Socionext Inc.
44e3d8406SMasahiro Yamada * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5a111bfbfSMasahiro Yamada */
6a111bfbfSMasahiro Yamada
7a111bfbfSMasahiro Yamada #include <common.h>
8a111bfbfSMasahiro Yamada #include <clk.h>
9a111bfbfSMasahiro Yamada #include <fdtdec.h>
10a111bfbfSMasahiro Yamada #include <mmc.h>
119d922450SSimon Glass #include <dm.h>
12a111bfbfSMasahiro Yamada #include <linux/compat.h>
13b27af399SMasahiro Yamada #include <linux/dma-direction.h>
14a111bfbfSMasahiro Yamada #include <linux/io.h>
154f80501bSMasahiro Yamada #include <linux/sizes.h>
169f13021fSMarek Vasut #include <power/regulator.h>
17a111bfbfSMasahiro Yamada #include <asm/unaligned.h>
18a111bfbfSMasahiro Yamada
19cb0b6b03SMarek Vasut #include "tmio-common.h"
204eb00846SMasahiro Yamada
214eb00846SMasahiro Yamada static const struct dm_mmc_ops uniphier_sd_ops = {
22cb0b6b03SMarek Vasut .send_cmd = tmio_sd_send_cmd,
23cb0b6b03SMarek Vasut .set_ios = tmio_sd_set_ios,
24cb0b6b03SMarek Vasut .get_cd = tmio_sd_get_cd,
254eb00846SMasahiro Yamada };
264eb00846SMasahiro Yamada
27a111bfbfSMasahiro Yamada static const struct udevice_id uniphier_sd_match[] = {
28c3ab1e11SMasahiro Yamada { .compatible = "socionext,uniphier-sd-v2.91" },
29c3ab1e11SMasahiro Yamada { .compatible = "socionext,uniphier-sd-v3.1" },
30c3ab1e11SMasahiro Yamada { .compatible = "socionext,uniphier-sd-v3.1.1" },
31a111bfbfSMasahiro Yamada { /* sentinel */ }
32a111bfbfSMasahiro Yamada };
33a111bfbfSMasahiro Yamada
uniphier_sd_clk_get_rate(struct tmio_sd_priv * priv)348ec6a04bSMarek Vasut static ulong uniphier_sd_clk_get_rate(struct tmio_sd_priv *priv)
358ec6a04bSMarek Vasut {
368ec6a04bSMarek Vasut #if CONFIG_IS_ENABLED(CLK)
378ec6a04bSMarek Vasut return clk_get_rate(&priv->clk);
388ec6a04bSMarek Vasut #elif CONFIG_SPL_BUILD
398ec6a04bSMarek Vasut return 100000000;
408ec6a04bSMarek Vasut #else
418ec6a04bSMarek Vasut return 0;
428ec6a04bSMarek Vasut #endif
438ec6a04bSMarek Vasut }
448ec6a04bSMarek Vasut
uniphier_sd_probe(struct udevice * dev)45c769e609SMarek Vasut static int uniphier_sd_probe(struct udevice *dev)
46c769e609SMarek Vasut {
4730b5d9aaSMasahiro Yamada struct tmio_sd_priv *priv = dev_get_priv(dev);
488ec6a04bSMarek Vasut
498ec6a04bSMarek Vasut priv->clk_get_rate = uniphier_sd_clk_get_rate;
50*992bcf4fSMarek Vasut priv->read_poll_flag = TMIO_SD_DMA_INFO1_END_RD2;
518ec6a04bSMarek Vasut
52fc2d0302SMasahiro Yamada #ifndef CONFIG_SPL_BUILD
5330b5d9aaSMasahiro Yamada int ret;
5430b5d9aaSMasahiro Yamada
558ec6a04bSMarek Vasut ret = clk_get_by_index(dev, 0, &priv->clk);
5630b5d9aaSMasahiro Yamada if (ret < 0) {
5730b5d9aaSMasahiro Yamada dev_err(dev, "failed to get host clock\n");
5830b5d9aaSMasahiro Yamada return ret;
5930b5d9aaSMasahiro Yamada }
6030b5d9aaSMasahiro Yamada
6130b5d9aaSMasahiro Yamada /* set to max rate */
628ec6a04bSMarek Vasut ret = clk_set_rate(&priv->clk, ULONG_MAX);
638ec6a04bSMarek Vasut if (ret < 0) {
6430b5d9aaSMasahiro Yamada dev_err(dev, "failed to set rate for host clock\n");
658ec6a04bSMarek Vasut clk_free(&priv->clk);
668ec6a04bSMarek Vasut return ret;
6730b5d9aaSMasahiro Yamada }
6830b5d9aaSMasahiro Yamada
698ec6a04bSMarek Vasut ret = clk_enable(&priv->clk);
7030b5d9aaSMasahiro Yamada if (ret) {
7130b5d9aaSMasahiro Yamada dev_err(dev, "failed to enable host clock\n");
7230b5d9aaSMasahiro Yamada return ret;
7330b5d9aaSMasahiro Yamada }
74fc2d0302SMasahiro Yamada #endif
7530b5d9aaSMasahiro Yamada
76cb0b6b03SMarek Vasut return tmio_sd_probe(dev, 0);
77c769e609SMarek Vasut }
78c769e609SMarek Vasut
79a111bfbfSMasahiro Yamada U_BOOT_DRIVER(uniphier_mmc) = {
80a111bfbfSMasahiro Yamada .name = "uniphier-mmc",
81a111bfbfSMasahiro Yamada .id = UCLASS_MMC,
82a111bfbfSMasahiro Yamada .of_match = uniphier_sd_match,
83cb0b6b03SMarek Vasut .bind = tmio_sd_bind,
84c769e609SMarek Vasut .probe = uniphier_sd_probe,
85cb0b6b03SMarek Vasut .priv_auto_alloc_size = sizeof(struct tmio_sd_priv),
86cb0b6b03SMarek Vasut .platdata_auto_alloc_size = sizeof(struct tmio_sd_plat),
873937404fSMasahiro Yamada .ops = &uniphier_sd_ops,
88a111bfbfSMasahiro Yamada };
89