Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Support of SDHCI for Microchip PIC32 SoC. |
| 4 | * |
| 5 | * Copyright (C) 2015 Microchip Technology Inc. |
| 6 | * Andrei Pistirica <andrei.pistirica@microchip.com> |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 51a3ec3 | 2017-05-17 17:18:07 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 10 | #include <sdhci.h> |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 11 | #include <clk.h> |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 12 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 13 | struct pic32_sdhci_plat { |
| 14 | struct mmc_config cfg; |
| 15 | struct mmc mmc; |
Jaehoon Chung | 9174eb4 | 2016-12-30 15:30:14 +0900 | [diff] [blame] | 16 | }; |
| 17 | |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 18 | static int pic32_sdhci_probe(struct udevice *dev) |
| 19 | { |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 20 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame^] | 21 | struct pic32_sdhci_plat *plat = dev_get_plat(dev); |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 22 | struct sdhci_host *host = dev_get_priv(dev); |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 23 | |
| 24 | struct clk clk; |
| 25 | ulong clk_rate; |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 26 | int ret; |
| 27 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 28 | ret = clk_get_by_name(dev, "base_clk", &clk); |
| 29 | if (ret) |
| 30 | return ret; |
| 31 | |
| 32 | clk_rate = clk_get_rate(&clk); |
| 33 | clk_free(&clk); |
| 34 | |
| 35 | if (IS_ERR_VALUE(clk_rate)) |
| 36 | return clk_rate; |
| 37 | |
| 38 | host->ioaddr = dev_remap_addr(dev); |
| 39 | |
| 40 | if (!host->ioaddr) |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | |
Masahiro Yamada | a440561 | 2016-04-22 20:59:31 +0900 | [diff] [blame] | 43 | host->name = dev->name; |
Jaehoon Chung | 730a595 | 2016-12-30 15:30:15 +0900 | [diff] [blame] | 44 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT; |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 45 | host->bus_width = dev_read_u32_default(dev, "bus-width", 4); |
| 46 | host->max_clk = clk_rate; |
| 47 | |
| 48 | host->mmc = &plat->mmc; |
| 49 | host->mmc->dev = dev; |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 50 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 51 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 52 | if (ret) |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 53 | return ret; |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 54 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 55 | host->mmc->priv = host; |
| 56 | upriv->mmc = host->mmc; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 57 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 58 | ret = sdhci_probe(dev); |
Simon Glass | 77ca42b | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 59 | if (ret) |
| 60 | return ret; |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 61 | |
| 62 | if (!dev_read_bool(dev, "microchip,use-sdcd")) { |
| 63 | // Use workaround 1 for erratum #15 by default |
| 64 | u8 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 65 | ctrl = (ctrl & ~SDHCI_CTRL_CD_TEST_INS) | SDHCI_CTRL_CD_TEST; |
| 66 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 67 | } |
Simon Glass | 77ca42b | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 68 | |
| 69 | return 0; |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 70 | } |
| 71 | |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 72 | static int pic32_sdhci_bind(struct udevice *dev) |
| 73 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame^] | 74 | struct pic32_sdhci_plat *plat = dev_get_plat(dev); |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 75 | |
| 76 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 77 | } |
| 78 | |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 79 | static const struct udevice_id pic32_sdhci_ids[] = { |
| 80 | { .compatible = "microchip,pic32mzda-sdhci" }, |
| 81 | { } |
| 82 | }; |
| 83 | |
| 84 | U_BOOT_DRIVER(pic32_sdhci_drv) = { |
| 85 | .name = "pic32_sdhci", |
| 86 | .id = UCLASS_MMC, |
| 87 | .of_match = pic32_sdhci_ids, |
John Robertson | f9286d4 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 88 | .ops = &sdhci_ops, |
| 89 | .bind = pic32_sdhci_bind, |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 90 | .probe = pic32_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 91 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 92 | .plat_auto = sizeof(struct pic32_sdhci_plat) |
Andrei Pistirica | be03317 | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 93 | }; |