xref: /freebsd/sys/dev/mmc/mmc_helpers.c (revision 10ff414c)
1 /*
2  * Copyright 2019 Emmanuel Vadot <manu@freebsd.org>
3  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/gpio.h>
35 #include <sys/taskqueue.h>
36 
37 #include <dev/mmc/bridge.h>
38 #include <dev/mmc/mmc_helpers.h>
39 
40 static inline void
41 mmc_parse_sd_speed(device_t dev, struct mmc_host *host)
42 {
43 	bool no_18v = false;
44 
45 	/*
46 	 * Parse SD supported modes
47 	 * All UHS-I modes requires 1.8V signaling.
48 	 */
49 	if (device_has_property(dev, "no-1-8-v"))
50 		no_18v = true;
51 	if (device_has_property(dev, "cap-sd-highspeed"))
52 		host->caps |= MMC_CAP_HSPEED;
53 	if (device_has_property(dev, "sd-uhs-sdr12") && !no_18v)
54 		host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
55 	if (device_has_property(dev, "sd-uhs-sdr25") && !no_18v)
56 		host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
57 	if (device_has_property(dev, "sd-uhs-sdr50") && !no_18v)
58 		host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
59 	if (device_has_property(dev, "sd-uhs-sdr104") && !no_18v)
60 		host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
61 	if (device_has_property(dev, "sd-uhs-ddr50") && !no_18v)
62 		host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
63 }
64 
65 static inline void
66 mmc_parse_mmc_speed(device_t dev, struct mmc_host *host)
67 {
68 	/* Parse eMMC supported modes */
69 	if (device_has_property(dev, "cap-mmc-highspeed"))
70 		host->caps |= MMC_CAP_HSPEED;
71 	if (device_has_property(dev, "mmc-ddr-1_2v"))
72 		host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
73 	if (device_has_property(dev, "mmc-ddr-1_8v"))
74 		host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
75 	if (device_has_property(dev, "mmc-ddr-3_3v"))
76 		host->caps |= MMC_CAP_SIGNALING_330;
77 	if (device_has_property(dev, "mmc-hs200-1_2v"))
78 		host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
79 	if (device_has_property(dev, "mmc-hs200-1_8v"))
80 		host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
81 	if (device_has_property(dev, "mmc-hs400-1_2v"))
82 		host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
83 	if (device_has_property(dev, "mmc-hs400-1_8v"))
84 		host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
85 	if (device_has_property(dev, "mmc-hs400-enhanced-strobe"))
86 		host->caps |= MMC_CAP_MMC_ENH_STROBE;
87 }
88 
89 int
90 mmc_parse(device_t dev, struct mmc_helper *helper, struct mmc_host *host)
91 {
92 	uint64_t bus_width, max_freq;
93 
94 	bus_width = 0;
95 	if (device_get_property(dev, "bus-width", &bus_width, sizeof(uint64_t)) <= 0)
96 		bus_width = 1;
97 
98 	if (bus_width >= 4)
99 		host->caps |= MMC_CAP_4_BIT_DATA;
100 	if (bus_width >= 8)
101 		host->caps |= MMC_CAP_8_BIT_DATA;
102 
103 	/*
104 	 * max-frequency is optional, drivers should tweak this value
105 	 * if it's not present based on the clock that the mmc controller
106 	 * operates on
107 	 */
108 	if (device_get_property(dev, "max-frequency", &max_freq,
109 	    sizeof(uint64_t)) > 0)
110 		host->f_max = max_freq;
111 
112 	if (device_has_property(dev, "broken-cd"))
113 		helper->props |= MMC_PROP_BROKEN_CD;
114 	if (device_has_property(dev, "non-removable"))
115 		helper->props |= MMC_PROP_NON_REMOVABLE;
116 	if (device_has_property(dev, "wp-inverted"))
117 		helper->props |= MMC_PROP_WP_INVERTED;
118 	if (device_has_property(dev, "cd-inverted"))
119 		helper->props |= MMC_PROP_CD_INVERTED;
120 	if (device_has_property(dev, "no-sdio"))
121 		helper->props |= MMC_PROP_NO_SDIO;
122 	if (device_has_property(dev, "no-sd"))
123 		helper->props |= MMC_PROP_NO_SD;
124 	if (device_has_property(dev, "no-mmc"))
125 		helper->props |= MMC_PROP_NO_MMC;
126 
127 	if (!(helper->props & MMC_PROP_NO_SD))
128 		mmc_parse_sd_speed(dev, host);
129 
130 	if (!(helper->props & MMC_PROP_NO_MMC))
131 		mmc_parse_mmc_speed(dev, host);
132 
133 	return (0);
134 }
135