18a8166e5SBartlomiej Grzesik /*
28a8166e5SBartlomiej Grzesik * Copyright 2019 Emmanuel Vadot <manu@freebsd.org>
38a8166e5SBartlomiej Grzesik * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved.
48a8166e5SBartlomiej Grzesik *
58a8166e5SBartlomiej Grzesik * Redistribution and use in source and binary forms, with or without
68a8166e5SBartlomiej Grzesik * modification, are permitted provided that the following conditions are
78a8166e5SBartlomiej Grzesik * met:
88a8166e5SBartlomiej Grzesik *
98a8166e5SBartlomiej Grzesik * 1. Redistributions of source code must retain the above copyright
108a8166e5SBartlomiej Grzesik * notice, this list of conditions and the following disclaimer.
118a8166e5SBartlomiej Grzesik * 2. Redistributions in binary form must reproduce the above copyright
128a8166e5SBartlomiej Grzesik * notice, this list of conditions and the following disclaimer in the
138a8166e5SBartlomiej Grzesik * documentation and/or other materials provided with the distribution.
148a8166e5SBartlomiej Grzesik *
158a8166e5SBartlomiej Grzesik * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168a8166e5SBartlomiej Grzesik * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178a8166e5SBartlomiej Grzesik * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
188a8166e5SBartlomiej Grzesik * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
198a8166e5SBartlomiej Grzesik * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
208a8166e5SBartlomiej Grzesik * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
218a8166e5SBartlomiej Grzesik * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
228a8166e5SBartlomiej Grzesik * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
238a8166e5SBartlomiej Grzesik * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
248a8166e5SBartlomiej Grzesik * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
258a8166e5SBartlomiej Grzesik * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268a8166e5SBartlomiej Grzesik */
278a8166e5SBartlomiej Grzesik
288a8166e5SBartlomiej Grzesik #include <sys/param.h>
298a8166e5SBartlomiej Grzesik #include <sys/bus.h>
308a8166e5SBartlomiej Grzesik #include <sys/kernel.h>
318a8166e5SBartlomiej Grzesik #include <sys/gpio.h>
328a8166e5SBartlomiej Grzesik #include <sys/taskqueue.h>
338a8166e5SBartlomiej Grzesik
348a8166e5SBartlomiej Grzesik #include <dev/mmc/bridge.h>
358a8166e5SBartlomiej Grzesik #include <dev/mmc/mmc_helpers.h>
368a8166e5SBartlomiej Grzesik
378a8166e5SBartlomiej Grzesik static inline void
mmc_parse_sd_speed(device_t dev,struct mmc_host * host)388a8166e5SBartlomiej Grzesik mmc_parse_sd_speed(device_t dev, struct mmc_host *host)
398a8166e5SBartlomiej Grzesik {
408a8166e5SBartlomiej Grzesik bool no_18v = false;
418a8166e5SBartlomiej Grzesik
428a8166e5SBartlomiej Grzesik /*
438a8166e5SBartlomiej Grzesik * Parse SD supported modes
448a8166e5SBartlomiej Grzesik * All UHS-I modes requires 1.8V signaling.
458a8166e5SBartlomiej Grzesik */
468a8166e5SBartlomiej Grzesik if (device_has_property(dev, "no-1-8-v"))
478a8166e5SBartlomiej Grzesik no_18v = true;
488a8166e5SBartlomiej Grzesik if (device_has_property(dev, "cap-sd-highspeed"))
498a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_HSPEED;
508a8166e5SBartlomiej Grzesik if (device_has_property(dev, "sd-uhs-sdr12") && !no_18v)
518a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
528a8166e5SBartlomiej Grzesik if (device_has_property(dev, "sd-uhs-sdr25") && !no_18v)
538a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
548a8166e5SBartlomiej Grzesik if (device_has_property(dev, "sd-uhs-sdr50") && !no_18v)
558a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
568a8166e5SBartlomiej Grzesik if (device_has_property(dev, "sd-uhs-sdr104") && !no_18v)
578a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
588a8166e5SBartlomiej Grzesik if (device_has_property(dev, "sd-uhs-ddr50") && !no_18v)
598a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
608a8166e5SBartlomiej Grzesik }
618a8166e5SBartlomiej Grzesik
628a8166e5SBartlomiej Grzesik static inline void
mmc_parse_mmc_speed(device_t dev,struct mmc_host * host)638a8166e5SBartlomiej Grzesik mmc_parse_mmc_speed(device_t dev, struct mmc_host *host)
648a8166e5SBartlomiej Grzesik {
658a8166e5SBartlomiej Grzesik /* Parse eMMC supported modes */
668a8166e5SBartlomiej Grzesik if (device_has_property(dev, "cap-mmc-highspeed"))
678a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_HSPEED;
688a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-ddr-1_2v"))
698a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
708a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-ddr-1_8v"))
718a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
728a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-ddr-3_3v"))
738a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_SIGNALING_330;
748a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-hs200-1_2v"))
758a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
768a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-hs200-1_8v"))
778a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
788a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-hs400-1_2v"))
798a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
808a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-hs400-1_8v"))
818a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
828a8166e5SBartlomiej Grzesik if (device_has_property(dev, "mmc-hs400-enhanced-strobe"))
838a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_MMC_ENH_STROBE;
848a8166e5SBartlomiej Grzesik }
858a8166e5SBartlomiej Grzesik
868a8166e5SBartlomiej Grzesik int
mmc_parse(device_t dev,struct mmc_helper * helper,struct mmc_host * host)878a8166e5SBartlomiej Grzesik mmc_parse(device_t dev, struct mmc_helper *helper, struct mmc_host *host)
888a8166e5SBartlomiej Grzesik {
89*b344de4dSKornel Duleba uint32_t bus_width, max_freq;
908a8166e5SBartlomiej Grzesik
918a8166e5SBartlomiej Grzesik bus_width = 0;
92*b344de4dSKornel Duleba if (device_get_property(dev, "bus-width", &bus_width,
93*b344de4dSKornel Duleba sizeof(bus_width), DEVICE_PROP_UINT32) <= 0)
948a8166e5SBartlomiej Grzesik bus_width = 1;
958a8166e5SBartlomiej Grzesik
968a8166e5SBartlomiej Grzesik if (bus_width >= 4)
978a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_4_BIT_DATA;
988a8166e5SBartlomiej Grzesik if (bus_width >= 8)
998a8166e5SBartlomiej Grzesik host->caps |= MMC_CAP_8_BIT_DATA;
1008a8166e5SBartlomiej Grzesik
1018a8166e5SBartlomiej Grzesik /*
1028a8166e5SBartlomiej Grzesik * max-frequency is optional, drivers should tweak this value
1038a8166e5SBartlomiej Grzesik * if it's not present based on the clock that the mmc controller
1048a8166e5SBartlomiej Grzesik * operates on
1058a8166e5SBartlomiej Grzesik */
1064a331971SJessica Clarke if (device_get_property(dev, "max-frequency", &max_freq,
107*b344de4dSKornel Duleba sizeof(max_freq), DEVICE_PROP_UINT32) > 0)
1088a8166e5SBartlomiej Grzesik host->f_max = max_freq;
1098a8166e5SBartlomiej Grzesik
1108a8166e5SBartlomiej Grzesik if (device_has_property(dev, "broken-cd"))
1118a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_BROKEN_CD;
1128a8166e5SBartlomiej Grzesik if (device_has_property(dev, "non-removable"))
1138a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_NON_REMOVABLE;
1148a8166e5SBartlomiej Grzesik if (device_has_property(dev, "wp-inverted"))
1158a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_WP_INVERTED;
1168a8166e5SBartlomiej Grzesik if (device_has_property(dev, "cd-inverted"))
1178a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_CD_INVERTED;
1188a8166e5SBartlomiej Grzesik if (device_has_property(dev, "no-sdio"))
1198a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_NO_SDIO;
1208a8166e5SBartlomiej Grzesik if (device_has_property(dev, "no-sd"))
1218a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_NO_SD;
1228a8166e5SBartlomiej Grzesik if (device_has_property(dev, "no-mmc"))
1238a8166e5SBartlomiej Grzesik helper->props |= MMC_PROP_NO_MMC;
1248a8166e5SBartlomiej Grzesik
1258a8166e5SBartlomiej Grzesik if (!(helper->props & MMC_PROP_NO_SD))
1268a8166e5SBartlomiej Grzesik mmc_parse_sd_speed(dev, host);
1278a8166e5SBartlomiej Grzesik
1288a8166e5SBartlomiej Grzesik if (!(helper->props & MMC_PROP_NO_MMC))
1298a8166e5SBartlomiej Grzesik mmc_parse_mmc_speed(dev, host);
1308a8166e5SBartlomiej Grzesik
1318a8166e5SBartlomiej Grzesik return (0);
1328a8166e5SBartlomiej Grzesik }
133