blob: fe555106a1d36722a6968cb2bf121d86ea74ab28 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andrei Pistirica102142c2016-01-28 15:30:18 +05302/*
3 * Support of SDHCI for Microchip PIC32 SoC.
4 *
5 * Copyright (C) 2015 Microchip Technology Inc.
6 * Andrei Pistirica <andrei.pistirica@microchip.com>
Andrei Pistirica102142c2016-01-28 15:30:18 +05307 */
8
Simon Glass4af0d7e2017-05-17 17:18:07 -06009#include <dm.h>
Andrei Pistirica102142c2016-01-28 15:30:18 +053010#include <sdhci.h>
John Robertson6978db32020-09-01 02:55:14 +000011#include <clk.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <linux/errno.h>
13#include <mach/pic32.h>
Andrei Pistirica102142c2016-01-28 15:30:18 +053014
John Robertson6978db32020-09-01 02:55:14 +000015struct pic32_sdhci_plat {
16 struct mmc_config cfg;
17 struct mmc mmc;
Jaehoon Chung5e962172016-12-30 15:30:14 +090018};
19
Andrei Pistirica102142c2016-01-28 15:30:18 +053020static int pic32_sdhci_probe(struct udevice *dev)
21{
John Robertson6978db32020-09-01 02:55:14 +000022 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -070023 struct pic32_sdhci_plat *plat = dev_get_plat(dev);
Andrei Pistirica102142c2016-01-28 15:30:18 +053024 struct sdhci_host *host = dev_get_priv(dev);
John Robertson6978db32020-09-01 02:55:14 +000025
26 struct clk clk;
27 ulong clk_rate;
Andrei Pistirica102142c2016-01-28 15:30:18 +053028 int ret;
29
John Robertson6978db32020-09-01 02:55:14 +000030 ret = clk_get_by_name(dev, "base_clk", &clk);
Simon Glasscffe5d82016-05-01 13:52:34 -060031 if (ret)
32 return ret;
John Robertson6978db32020-09-01 02:55:14 +000033
34 clk_rate = clk_get_rate(&clk);
35 clk_free(&clk);
36
37 if (IS_ERR_VALUE(clk_rate))
38 return clk_rate;
39
40 host->ioaddr = dev_remap_addr(dev);
41
42 if (!host->ioaddr)
43 return -EINVAL;
44
45 host->name = dev->name;
46 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT;
47 host->bus_width = dev_read_u32_default(dev, "bus-width", 4);
48 host->max_clk = clk_rate;
49
50 host->mmc = &plat->mmc;
Simon Glasscffe5d82016-05-01 13:52:34 -060051 host->mmc->dev = dev;
52
John Robertson6978db32020-09-01 02:55:14 +000053 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
54 if (ret)
55 return ret;
56
57 host->mmc->priv = host;
58 upriv->mmc = host->mmc;
59
60 ret = sdhci_probe(dev);
61 if (ret)
62 return ret;
63
64 if (!dev_read_bool(dev, "microchip,use-sdcd")) {
65 // Use workaround 1 for erratum #15 by default
66 u8 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
67 ctrl = (ctrl & ~SDHCI_CTRL_CD_TEST_INS) | SDHCI_CTRL_CD_TEST;
68 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
69 }
70
Simon Glasscffe5d82016-05-01 13:52:34 -060071 return 0;
Andrei Pistirica102142c2016-01-28 15:30:18 +053072}
73
John Robertson6978db32020-09-01 02:55:14 +000074static int pic32_sdhci_bind(struct udevice *dev)
75{
Simon Glassc69cda22020-12-03 16:55:20 -070076 struct pic32_sdhci_plat *plat = dev_get_plat(dev);
John Robertson6978db32020-09-01 02:55:14 +000077
78 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
79}
80
Andrei Pistirica102142c2016-01-28 15:30:18 +053081static const struct udevice_id pic32_sdhci_ids[] = {
82 { .compatible = "microchip,pic32mzda-sdhci" },
83 { }
84};
85
86U_BOOT_DRIVER(pic32_sdhci_drv) = {
87 .name = "pic32_sdhci",
88 .id = UCLASS_MMC,
89 .of_match = pic32_sdhci_ids,
John Robertson6978db32020-09-01 02:55:14 +000090 .ops = &sdhci_ops,
91 .bind = pic32_sdhci_bind,
Andrei Pistirica102142c2016-01-28 15:30:18 +053092 .probe = pic32_sdhci_probe,
Simon Glass41575d82020-12-03 16:55:17 -070093 .priv_auto = sizeof(struct sdhci_host),
Simon Glasscaa4daa2020-12-03 16:55:18 -070094 .plat_auto = sizeof(struct pic32_sdhci_plat)
Andrei Pistirica102142c2016-01-28 15:30:18 +053095};