kernel: Remove unused *.h files from SRCS in kernel module Makefiles.
[dragonfly.git] / sys / bus / mmc / mmc_subr.c
1 /*-
2  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3  * Copyright (c) 2006 M. Warner Losh.  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
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Portions of this software may have been developed with reference to
26  * the SD Simplified Specification.  The following disclaimer may apply:
27  *
28  * The following conditions apply to the release of the simplified
29  * specification ("Simplified Specification") by the SD Card Association and
30  * the SD Group. The Simplified Specification is a subset of the complete SD
31  * Specification which is owned by the SD Card Association and the SD
32  * Group. This Simplified Specification is provided on a non-confidential
33  * basis subject to the disclaimers below. Any implementation of the
34  * Simplified Specification may require a license from the SD Card
35  * Association, SD Group, SD-3C LLC or other third parties.
36  *
37  * Disclaimers:
38  *
39  * The information contained in the Simplified Specification is presented only
40  * as a standard specification for SD Cards and SD Host/Ancillary products and
41  * is provided "AS-IS" without any representations or warranties of any
42  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43  * Card Association for any damages, any infringements of patents or other
44  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45  * parties, which may result from its use. No license is granted by
46  * implication, estoppel or otherwise under any patent or other rights of the
47  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49  * or the SD Card Association to disclose or distribute any technical
50  * information, know-how or other confidential information to any third party.
51  *
52  * $FreeBSD: head/sys/dev/mmc/mmc_subr.c 315430 2017-03-16 22:23:04Z marius $
53  */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/lock.h>
59 #include <sys/time.h>
60 #include <sys/bus.h>
61
62 #include <bus/mmc/bridge.h>
63 #include <bus/mmc/mmc_private.h>
64 #include <bus/mmc/mmc_subr.h>
65 #include <bus/mmc/mmcreg.h>
66 #include <bus/mmc/mmcbrvar.h>
67
68 #include "mmcbus_if.h"
69
70 #define CMD_RETRIES     3
71 #define LOG_PPS         5 /* Log no more than 5 errors per second. */
72
73 int
74 mmc_wait_for_cmd(device_t brdev, device_t reqdev, struct mmc_command *cmd,
75     int retries)
76 {
77         struct mmc_request mreq;
78         struct mmc_softc *sc;
79         int err;
80
81         do {
82                 memset(&mreq, 0, sizeof(mreq));
83                 memset(cmd->resp, 0, sizeof(cmd->resp));
84                 cmd->retries = 0; /* Retries done here, not in hardware. */
85                 cmd->mrq = &mreq;
86                 if (cmd->data != NULL)
87                         cmd->data->mrq = &mreq;
88                 mreq.cmd = cmd;
89                 if (MMCBUS_WAIT_FOR_REQUEST(brdev, reqdev, &mreq) != 0)
90                         err = MMC_ERR_FAILED;
91                 else
92                         err = cmd->error;
93         } while (err != MMC_ERR_NONE && retries-- > 0);
94
95         if (err != MMC_ERR_NONE && brdev == reqdev) {
96                 sc = device_get_softc(brdev);
97                 if (sc->squelched == 0 && ppsratecheck(&sc->log_time,
98                     &sc->log_count, LOG_PPS)) {
99                         device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
100                             cmd->opcode, err);
101                 }
102         }
103
104         return (err);
105 }
106
107 int
108 mmc_wait_for_app_cmd(device_t brdev, device_t reqdev, uint16_t rca,
109     struct mmc_command *cmd, int retries)
110 {
111         struct mmc_command appcmd;
112         struct mmc_softc *sc;
113         int err;
114
115         sc = device_get_softc(brdev);
116
117         /* Squelch error reporting at lower levels, we report below. */
118         sc->squelched++;
119         do {
120                 memset(&appcmd, 0, sizeof(appcmd));
121                 appcmd.opcode = MMC_APP_CMD;
122                 appcmd.arg = (uint32_t)rca << 16;
123                 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
124                 if (mmc_wait_for_cmd(brdev, reqdev, &appcmd, 0) != 0)
125                         err = MMC_ERR_FAILED;
126                 else
127                         err = appcmd.error;
128                 if (err == MMC_ERR_NONE) {
129                         if (!(appcmd.resp[0] & R1_APP_CMD))
130                                 err = MMC_ERR_FAILED;
131                         else if (mmc_wait_for_cmd(brdev, reqdev, cmd, 0) != 0)
132                                 err = MMC_ERR_FAILED;
133                         else
134                                 err = cmd->error;
135                 }
136         } while (err != MMC_ERR_NONE && retries-- > 0);
137         sc->squelched--;
138
139         if (err != MMC_ERR_NONE && brdev == reqdev) {
140                 sc = device_get_softc(brdev);
141                 if (sc->squelched == 0 && ppsratecheck(&sc->log_time,
142                     &sc->log_count, LOG_PPS)) {
143                         device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
144                             cmd->opcode, err);
145                 }
146         }
147
148         return (err);
149 }
150
151 int
152 mmc_switch(device_t brdev, device_t reqdev, uint16_t rca, uint8_t set,
153     uint8_t index, uint8_t value, u_int timeout, bool status)
154 {
155         struct mmc_command cmd;
156         int err;
157
158         KASSERT(timeout != 0, ("%s: no timeout", __func__));
159
160         memset(&cmd, 0, sizeof(cmd));
161         cmd.opcode = MMC_SWITCH_FUNC;
162         cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | (index << 16) | (value << 8) |
163             set;
164         /*
165          * If the hardware supports busy detection but the switch timeout
166          * exceeds the maximum host timeout, use a R1 instead of a R1B
167          * response in order to keep the hardware from timing out.
168          */
169         if (mmcbr_get_caps(brdev) & MMC_CAP_WAIT_WHILE_BUSY &&
170             timeout > mmcbr_get_max_busy_timeout(brdev))
171                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
172         else
173                 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
174         err = mmc_wait_for_cmd(brdev, reqdev, &cmd, CMD_RETRIES);
175         if (err != MMC_ERR_NONE || status == false)
176                 return (err);
177         return (mmc_switch_status(brdev, reqdev, rca, timeout));
178 }
179
180 int
181 mmc_switch_status(device_t brdev, device_t reqdev, uint16_t rca, u_int timeout)
182 {
183         struct timeval cur, end;
184         int err;
185         uint32_t status;
186
187         KASSERT(timeout != 0, ("%s: no timeout", __func__));
188
189         /*
190          * Note that when using a R1B response in mmc_switch(), bridges of
191          * type MMC_CAP_WAIT_WHILE_BUSY will issue mmc_send_status() only
192          * once and then exit the loop.
193          */
194         for (;;) {
195                 err = mmc_send_status(brdev, reqdev, rca, &status);
196                 if (err != MMC_ERR_NONE)
197                         break;
198                 if (R1_CURRENT_STATE(status) == R1_STATE_TRAN)
199                         break;
200                 getmicrouptime(&cur);
201                 if (end.tv_sec == 0 && end.tv_usec == 0) {
202                         end.tv_usec = timeout;
203                         timevaladd(&end, &cur);
204                 }
205                 if (timevalcmp(&cur, &end, >)) {
206                         err = MMC_ERR_TIMEOUT;
207                         break;
208                 }
209         }
210         if (err == MMC_ERR_NONE && R1_CURRENT_STATE(status) == R1_SWITCH_ERROR)
211                 return (MMC_ERR_FAILED);
212         return (err);
213 }
214
215 int
216 mmc_send_ext_csd(device_t brdev, device_t reqdev, uint8_t *rawextcsd)
217 {
218         struct mmc_command cmd;
219         struct mmc_data data;
220         int err;
221
222         memset(&cmd, 0, sizeof(cmd));
223         memset(&data, 0, sizeof(data));
224
225         memset(rawextcsd, 0, MMC_EXTCSD_SIZE);
226         cmd.opcode = MMC_SEND_EXT_CSD;
227         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
228         cmd.data = &data;
229
230         data.data = rawextcsd;
231         data.len = MMC_EXTCSD_SIZE;
232         data.flags = MMC_DATA_READ;
233
234         err = mmc_wait_for_cmd(brdev, reqdev, &cmd, CMD_RETRIES);
235         return (err);
236 }
237
238 int
239 mmc_send_status(device_t brdev, device_t reqdev, uint16_t rca, uint32_t *status)
240 {
241         struct mmc_command cmd;
242         int err;
243
244         memset(&cmd, 0, sizeof(cmd));
245         cmd.opcode = MMC_SEND_STATUS;
246         cmd.arg = (uint32_t)rca << 16;
247         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
248         err = mmc_wait_for_cmd(brdev, reqdev, &cmd, CMD_RETRIES);
249         *status = cmd.resp[0];
250         return (err);
251 }