xref: /freebsd/sys/dev/mmc/mmc_helpers.c (revision 5d3e7166)
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 	uint32_t bus_width, max_freq;
93 
94 	bus_width = 0;
95 	if (device_get_property(dev, "bus-width", &bus_width,
96 	    sizeof(bus_width), DEVICE_PROP_UINT32) <= 0)
97 		bus_width = 1;
98 
99 	if (bus_width >= 4)
100 		host->caps |= MMC_CAP_4_BIT_DATA;
101 	if (bus_width >= 8)
102 		host->caps |= MMC_CAP_8_BIT_DATA;
103 
104 	/*
105 	 * max-frequency is optional, drivers should tweak this value
106 	 * if it's not present based on the clock that the mmc controller
107 	 * operates on
108 	 */
109 	if (device_get_property(dev, "max-frequency", &max_freq,
110 	    sizeof(max_freq), DEVICE_PROP_UINT32) > 0)
111 		host->f_max = max_freq;
112 
113 	if (device_has_property(dev, "broken-cd"))
114 		helper->props |= MMC_PROP_BROKEN_CD;
115 	if (device_has_property(dev, "non-removable"))
116 		helper->props |= MMC_PROP_NON_REMOVABLE;
117 	if (device_has_property(dev, "wp-inverted"))
118 		helper->props |= MMC_PROP_WP_INVERTED;
119 	if (device_has_property(dev, "cd-inverted"))
120 		helper->props |= MMC_PROP_CD_INVERTED;
121 	if (device_has_property(dev, "no-sdio"))
122 		helper->props |= MMC_PROP_NO_SDIO;
123 	if (device_has_property(dev, "no-sd"))
124 		helper->props |= MMC_PROP_NO_SD;
125 	if (device_has_property(dev, "no-mmc"))
126 		helper->props |= MMC_PROP_NO_MMC;
127 
128 	if (!(helper->props & MMC_PROP_NO_SD))
129 		mmc_parse_sd_speed(dev, host);
130 
131 	if (!(helper->props & MMC_PROP_NO_MMC))
132 		mmc_parse_mmc_speed(dev, host);
133 
134 	return (0);
135 }
136