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