xref: /freebsd/sys/dev/mmc/mmc_helpers.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/gpio.h>
32 #include <sys/taskqueue.h>
33 
34 #include <dev/mmc/bridge.h>
35 #include <dev/mmc/mmc_helpers.h>
36 
37 static inline void
38 mmc_parse_sd_speed(device_t dev, struct mmc_host *host)
39 {
40 	bool no_18v = false;
41 
42 	/*
43 	 * Parse SD supported modes
44 	 * All UHS-I modes requires 1.8V signaling.
45 	 */
46 	if (device_has_property(dev, "no-1-8-v"))
47 		no_18v = true;
48 	if (device_has_property(dev, "cap-sd-highspeed"))
49 		host->caps |= MMC_CAP_HSPEED;
50 	if (device_has_property(dev, "sd-uhs-sdr12") && !no_18v)
51 		host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180;
52 	if (device_has_property(dev, "sd-uhs-sdr25") && !no_18v)
53 		host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180;
54 	if (device_has_property(dev, "sd-uhs-sdr50") && !no_18v)
55 		host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180;
56 	if (device_has_property(dev, "sd-uhs-sdr104") && !no_18v)
57 		host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180;
58 	if (device_has_property(dev, "sd-uhs-ddr50") && !no_18v)
59 		host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180;
60 }
61 
62 static inline void
63 mmc_parse_mmc_speed(device_t dev, struct mmc_host *host)
64 {
65 	/* Parse eMMC supported modes */
66 	if (device_has_property(dev, "cap-mmc-highspeed"))
67 		host->caps |= MMC_CAP_HSPEED;
68 	if (device_has_property(dev, "mmc-ddr-1_2v"))
69 		host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120;
70 	if (device_has_property(dev, "mmc-ddr-1_8v"))
71 		host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180;
72 	if (device_has_property(dev, "mmc-ddr-3_3v"))
73 		host->caps |= MMC_CAP_SIGNALING_330;
74 	if (device_has_property(dev, "mmc-hs200-1_2v"))
75 		host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120;
76 	if (device_has_property(dev, "mmc-hs200-1_8v"))
77 		host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180;
78 	if (device_has_property(dev, "mmc-hs400-1_2v"))
79 		host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120;
80 	if (device_has_property(dev, "mmc-hs400-1_8v"))
81 		host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180;
82 	if (device_has_property(dev, "mmc-hs400-enhanced-strobe"))
83 		host->caps |= MMC_CAP_MMC_ENH_STROBE;
84 }
85 
86 int
87 mmc_parse(device_t dev, struct mmc_helper *helper, struct mmc_host *host)
88 {
89 	uint32_t bus_width, max_freq;
90 
91 	bus_width = 0;
92 	if (device_get_property(dev, "bus-width", &bus_width,
93 	    sizeof(bus_width), DEVICE_PROP_UINT32) <= 0)
94 		bus_width = 1;
95 
96 	if (bus_width >= 4)
97 		host->caps |= MMC_CAP_4_BIT_DATA;
98 	if (bus_width >= 8)
99 		host->caps |= MMC_CAP_8_BIT_DATA;
100 
101 	/*
102 	 * max-frequency is optional, drivers should tweak this value
103 	 * if it's not present based on the clock that the mmc controller
104 	 * operates on
105 	 */
106 	if (device_get_property(dev, "max-frequency", &max_freq,
107 	    sizeof(max_freq), DEVICE_PROP_UINT32) > 0)
108 		host->f_max = max_freq;
109 
110 	if (device_has_property(dev, "broken-cd"))
111 		helper->props |= MMC_PROP_BROKEN_CD;
112 	if (device_has_property(dev, "non-removable"))
113 		helper->props |= MMC_PROP_NON_REMOVABLE;
114 	if (device_has_property(dev, "wp-inverted"))
115 		helper->props |= MMC_PROP_WP_INVERTED;
116 	if (device_has_property(dev, "cd-inverted"))
117 		helper->props |= MMC_PROP_CD_INVERTED;
118 	if (device_has_property(dev, "no-sdio"))
119 		helper->props |= MMC_PROP_NO_SDIO;
120 	if (device_has_property(dev, "no-sd"))
121 		helper->props |= MMC_PROP_NO_SD;
122 	if (device_has_property(dev, "no-mmc"))
123 		helper->props |= MMC_PROP_NO_MMC;
124 
125 	if (!(helper->props & MMC_PROP_NO_SD))
126 		mmc_parse_sd_speed(dev, host);
127 
128 	if (!(helper->props & MMC_PROP_NO_MMC))
129 		mmc_parse_mmc_speed(dev, host);
130 
131 	return (0);
132 }
133