Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andrei Pistirica | 102142c | 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 | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 4af0d7e | 2017-05-17 17:18:07 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 10 | #include <sdhci.h> |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 11 | #include <clk.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <mach/pic32.h> |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 14 | |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 15 | struct pic32_sdhci_plat { |
| 16 | struct mmc_config cfg; |
| 17 | struct mmc mmc; |
Jaehoon Chung | 5e96217 | 2016-12-30 15:30:14 +0900 | [diff] [blame] | 18 | }; |
| 19 | |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 20 | static int pic32_sdhci_probe(struct udevice *dev) |
| 21 | { |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 22 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 23 | struct pic32_sdhci_plat *plat = dev_get_plat(dev); |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 24 | struct sdhci_host *host = dev_get_priv(dev); |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 25 | |
| 26 | struct clk clk; |
| 27 | ulong clk_rate; |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 28 | int ret; |
| 29 | |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 30 | ret = clk_get_by_name(dev, "base_clk", &clk); |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 31 | if (ret) |
| 32 | return ret; |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 33 | |
| 34 | clk_rate = clk_get_rate(&clk); |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 35 | |
| 36 | if (IS_ERR_VALUE(clk_rate)) |
| 37 | return clk_rate; |
| 38 | |
| 39 | host->ioaddr = dev_remap_addr(dev); |
| 40 | |
| 41 | if (!host->ioaddr) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | host->name = dev->name; |
| 45 | host->quirks = SDHCI_QUIRK_NO_HISPD_BIT; |
| 46 | host->bus_width = dev_read_u32_default(dev, "bus-width", 4); |
| 47 | host->max_clk = clk_rate; |
| 48 | |
| 49 | host->mmc = &plat->mmc; |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 50 | host->mmc->dev = dev; |
| 51 | |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 52 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 53 | if (ret) |
| 54 | return ret; |
| 55 | |
| 56 | host->mmc->priv = host; |
| 57 | upriv->mmc = host->mmc; |
| 58 | |
| 59 | ret = sdhci_probe(dev); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | if (!dev_read_bool(dev, "microchip,use-sdcd")) { |
| 64 | // Use workaround 1 for erratum #15 by default |
| 65 | u8 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 66 | ctrl = (ctrl & ~SDHCI_CTRL_CD_TEST_INS) | SDHCI_CTRL_CD_TEST; |
| 67 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 68 | } |
| 69 | |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 70 | return 0; |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 71 | } |
| 72 | |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 73 | static int pic32_sdhci_bind(struct udevice *dev) |
| 74 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 75 | struct pic32_sdhci_plat *plat = dev_get_plat(dev); |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 76 | |
| 77 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 78 | } |
| 79 | |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 80 | static const struct udevice_id pic32_sdhci_ids[] = { |
| 81 | { .compatible = "microchip,pic32mzda-sdhci" }, |
| 82 | { } |
| 83 | }; |
| 84 | |
| 85 | U_BOOT_DRIVER(pic32_sdhci_drv) = { |
| 86 | .name = "pic32_sdhci", |
| 87 | .id = UCLASS_MMC, |
| 88 | .of_match = pic32_sdhci_ids, |
John Robertson | 6978db3 | 2020-09-01 02:55:14 +0000 | [diff] [blame] | 89 | .ops = &sdhci_ops, |
| 90 | .bind = pic32_sdhci_bind, |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 91 | .probe = pic32_sdhci_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 92 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 93 | .plat_auto = sizeof(struct pic32_sdhci_plat) |
Andrei Pistirica | 102142c | 2016-01-28 15:30:18 +0530 | [diff] [blame] | 94 | }; |