xref: /freebsd/sys/dev/sdio/sdio_subr.c (revision 67ca7330cf34a789afbbff9ae7e4cdc4a4917ae3)
1*67ca7330SBjoern A. Zeeb /*-
2*67ca7330SBjoern A. Zeeb  * Copyright (c) 2017 Ilya Bakulin.  All rights reserved.
3*67ca7330SBjoern A. Zeeb  * Copyright (c) 2018-2019 The FreeBSD Foundation
4*67ca7330SBjoern A. Zeeb  *
5*67ca7330SBjoern A. Zeeb  * Portions of this software were developed by Björn Zeeb
6*67ca7330SBjoern A. Zeeb  * under sponsorship from the FreeBSD Foundation.
7*67ca7330SBjoern A. Zeeb  *
8*67ca7330SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
9*67ca7330SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
10*67ca7330SBjoern A. Zeeb  * are met:
11*67ca7330SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
12*67ca7330SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
13*67ca7330SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
14*67ca7330SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
15*67ca7330SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
16*67ca7330SBjoern A. Zeeb  *
17*67ca7330SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*67ca7330SBjoern A. Zeeb  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*67ca7330SBjoern A. Zeeb  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*67ca7330SBjoern A. Zeeb  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*67ca7330SBjoern A. Zeeb  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*67ca7330SBjoern A. Zeeb  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*67ca7330SBjoern A. Zeeb  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*67ca7330SBjoern A. Zeeb  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*67ca7330SBjoern A. Zeeb  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*67ca7330SBjoern A. Zeeb  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*67ca7330SBjoern A. Zeeb  *
28*67ca7330SBjoern A. Zeeb  *
29*67ca7330SBjoern A. Zeeb  * Portions of this software may have been developed with reference to
30*67ca7330SBjoern A. Zeeb  * the SD Simplified Specification.  The following disclaimer may apply:
31*67ca7330SBjoern A. Zeeb  *
32*67ca7330SBjoern A. Zeeb  * The following conditions apply to the release of the simplified
33*67ca7330SBjoern A. Zeeb  * specification ("Simplified Specification") by the SD Card Association and
34*67ca7330SBjoern A. Zeeb  * the SD Group. The Simplified Specification is a subset of the complete SD
35*67ca7330SBjoern A. Zeeb  * Specification which is owned by the SD Card Association and the SD
36*67ca7330SBjoern A. Zeeb  * Group. This Simplified Specification is provided on a non-confidential
37*67ca7330SBjoern A. Zeeb  * basis subject to the disclaimers below. Any implementation of the
38*67ca7330SBjoern A. Zeeb  * Simplified Specification may require a license from the SD Card
39*67ca7330SBjoern A. Zeeb  * Association, SD Group, SD-3C LLC or other third parties.
40*67ca7330SBjoern A. Zeeb  *
41*67ca7330SBjoern A. Zeeb  * Disclaimers:
42*67ca7330SBjoern A. Zeeb  *
43*67ca7330SBjoern A. Zeeb  * The information contained in the Simplified Specification is presented only
44*67ca7330SBjoern A. Zeeb  * as a standard specification for SD Cards and SD Host/Ancillary products and
45*67ca7330SBjoern A. Zeeb  * is provided "AS-IS" without any representations or warranties of any
46*67ca7330SBjoern A. Zeeb  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
47*67ca7330SBjoern A. Zeeb  * Card Association for any damages, any infringements of patents or other
48*67ca7330SBjoern A. Zeeb  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
49*67ca7330SBjoern A. Zeeb  * parties, which may result from its use. No license is granted by
50*67ca7330SBjoern A. Zeeb  * implication, estoppel or otherwise under any patent or other rights of the
51*67ca7330SBjoern A. Zeeb  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
52*67ca7330SBjoern A. Zeeb  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
53*67ca7330SBjoern A. Zeeb  * or the SD Card Association to disclose or distribute any technical
54*67ca7330SBjoern A. Zeeb  * information, know-how or other confidential information to any third party.
55*67ca7330SBjoern A. Zeeb  */
56*67ca7330SBjoern A. Zeeb 
57*67ca7330SBjoern A. Zeeb #include <sys/cdefs.h>
58*67ca7330SBjoern A. Zeeb __FBSDID("$FreeBSD$");
59*67ca7330SBjoern A. Zeeb 
60*67ca7330SBjoern A. Zeeb #include <sys/types.h>
61*67ca7330SBjoern A. Zeeb #include <sys/param.h>
62*67ca7330SBjoern A. Zeeb #include <sys/kernel.h>
63*67ca7330SBjoern A. Zeeb #include <sys/systm.h>
64*67ca7330SBjoern A. Zeeb #include <sys/bus.h>
65*67ca7330SBjoern A. Zeeb #include <sys/endian.h>
66*67ca7330SBjoern A. Zeeb 
67*67ca7330SBjoern A. Zeeb #include <dev/mmc/mmcreg.h>
68*67ca7330SBjoern A. Zeeb 
69*67ca7330SBjoern A. Zeeb #include <dev/sdio/sdiob.h>
70*67ca7330SBjoern A. Zeeb #include <dev/sdio/sdio_subr.h>
71*67ca7330SBjoern A. Zeeb 
72*67ca7330SBjoern A. Zeeb #include "sdio_if.h"
73*67ca7330SBjoern A. Zeeb 
74*67ca7330SBjoern A. Zeeb /* Works on F0. */
75*67ca7330SBjoern A. Zeeb static int
76*67ca7330SBjoern A. Zeeb sdio_set_bool_for_func(device_t dev, uint32_t addr, uint8_t fn, bool enable)
77*67ca7330SBjoern A. Zeeb {
78*67ca7330SBjoern A. Zeeb 	device_t pdev;
79*67ca7330SBjoern A. Zeeb 	int error;
80*67ca7330SBjoern A. Zeeb 	uint8_t val;
81*67ca7330SBjoern A. Zeeb 	bool enabled;
82*67ca7330SBjoern A. Zeeb 
83*67ca7330SBjoern A. Zeeb 	pdev = device_get_parent(dev);
84*67ca7330SBjoern A. Zeeb 	error = SDIO_READ_DIRECT(pdev, 0, addr, &val);
85*67ca7330SBjoern A. Zeeb 	if (error != 0)
86*67ca7330SBjoern A. Zeeb 		return (error);
87*67ca7330SBjoern A. Zeeb 
88*67ca7330SBjoern A. Zeeb 	enabled = (val & (1 << fn)) ? true : false;
89*67ca7330SBjoern A. Zeeb 	if (enabled == enable)
90*67ca7330SBjoern A. Zeeb 		return (0);
91*67ca7330SBjoern A. Zeeb 
92*67ca7330SBjoern A. Zeeb 	if (enable)
93*67ca7330SBjoern A. Zeeb 		val |= (1 << fn);
94*67ca7330SBjoern A. Zeeb 	else
95*67ca7330SBjoern A. Zeeb 		val &= ~(1 << fn);
96*67ca7330SBjoern A. Zeeb 	error = SDIO_WRITE_DIRECT(pdev, 0, addr, val);
97*67ca7330SBjoern A. Zeeb 	return (error);
98*67ca7330SBjoern A. Zeeb }
99*67ca7330SBjoern A. Zeeb 
100*67ca7330SBjoern A. Zeeb int
101*67ca7330SBjoern A. Zeeb sdio_enable_func(struct sdio_func *f)
102*67ca7330SBjoern A. Zeeb {
103*67ca7330SBjoern A. Zeeb 
104*67ca7330SBjoern A. Zeeb 	return (sdio_set_bool_for_func(f->dev, SD_IO_CCCR_FN_ENABLE,
105*67ca7330SBjoern A. Zeeb 	    f->fn, true));
106*67ca7330SBjoern A. Zeeb }
107*67ca7330SBjoern A. Zeeb 
108*67ca7330SBjoern A. Zeeb int
109*67ca7330SBjoern A. Zeeb sdio_disable_func(struct sdio_func *f)
110*67ca7330SBjoern A. Zeeb {
111*67ca7330SBjoern A. Zeeb 
112*67ca7330SBjoern A. Zeeb 	return (sdio_set_bool_for_func(f->dev, SD_IO_CCCR_FN_ENABLE,
113*67ca7330SBjoern A. Zeeb 	    f->fn, false));
114*67ca7330SBjoern A. Zeeb }
115*67ca7330SBjoern A. Zeeb 
116*67ca7330SBjoern A. Zeeb int
117*67ca7330SBjoern A. Zeeb sdio_set_block_size(struct sdio_func *f, uint16_t bs)
118*67ca7330SBjoern A. Zeeb {
119*67ca7330SBjoern A. Zeeb 	device_t pdev;
120*67ca7330SBjoern A. Zeeb 	int error;
121*67ca7330SBjoern A. Zeeb 	uint32_t addr;
122*67ca7330SBjoern A. Zeeb 	uint16_t v;
123*67ca7330SBjoern A. Zeeb 
124*67ca7330SBjoern A. Zeeb 	if (!sdio_get_support_multiblk(f->dev))
125*67ca7330SBjoern A. Zeeb 		return (EOPNOTSUPP);
126*67ca7330SBjoern A. Zeeb 
127*67ca7330SBjoern A. Zeeb 	pdev = device_get_parent(f->dev);
128*67ca7330SBjoern A. Zeeb 	addr = SD_IO_FBR_START * f->fn + SD_IO_FBR_IOBLKSZ;
129*67ca7330SBjoern A. Zeeb 	v = htole16(bs);
130*67ca7330SBjoern A. Zeeb 	/* Always write through F0. */
131*67ca7330SBjoern A. Zeeb 	error = SDIO_WRITE_DIRECT(pdev, 0, addr, v & 0xff);
132*67ca7330SBjoern A. Zeeb 	if (error == 0)
133*67ca7330SBjoern A. Zeeb 		error = SDIO_WRITE_DIRECT(pdev, 0, addr + 1,
134*67ca7330SBjoern A. Zeeb 		    (v >> 8) & 0xff);
135*67ca7330SBjoern A. Zeeb 	if (error == 0)
136*67ca7330SBjoern A. Zeeb 		f->cur_blksize = bs;
137*67ca7330SBjoern A. Zeeb 
138*67ca7330SBjoern A. Zeeb 	return (error);
139*67ca7330SBjoern A. Zeeb }
140*67ca7330SBjoern A. Zeeb 
141*67ca7330SBjoern A. Zeeb uint8_t
142*67ca7330SBjoern A. Zeeb sdio_readb(struct sdio_func *f, uint32_t addr, int *err)
143*67ca7330SBjoern A. Zeeb {
144*67ca7330SBjoern A. Zeeb 	int error;
145*67ca7330SBjoern A. Zeeb 	uint8_t v;
146*67ca7330SBjoern A. Zeeb 
147*67ca7330SBjoern A. Zeeb 	error = SDIO_READ_DIRECT(device_get_parent(f->dev), f->fn, addr, &v);
148*67ca7330SBjoern A. Zeeb 	if (error) {
149*67ca7330SBjoern A. Zeeb 		if (err != NULL)
150*67ca7330SBjoern A. Zeeb 			*err = error;
151*67ca7330SBjoern A. Zeeb 		return (0xff);
152*67ca7330SBjoern A. Zeeb 	} else {
153*67ca7330SBjoern A. Zeeb 		if (err != NULL)
154*67ca7330SBjoern A. Zeeb 			*err = 0;
155*67ca7330SBjoern A. Zeeb 		return (v);
156*67ca7330SBjoern A. Zeeb 	}
157*67ca7330SBjoern A. Zeeb }
158*67ca7330SBjoern A. Zeeb 
159*67ca7330SBjoern A. Zeeb void
160*67ca7330SBjoern A. Zeeb sdio_writeb(struct sdio_func *f, uint8_t val, uint32_t addr, int *err)
161*67ca7330SBjoern A. Zeeb {
162*67ca7330SBjoern A. Zeeb 	int error;
163*67ca7330SBjoern A. Zeeb 
164*67ca7330SBjoern A. Zeeb 	error = SDIO_WRITE_DIRECT(device_get_parent(f->dev), f->fn, addr, val);
165*67ca7330SBjoern A. Zeeb 	if (err != NULL)
166*67ca7330SBjoern A. Zeeb 		*err = error;
167*67ca7330SBjoern A. Zeeb }
168*67ca7330SBjoern A. Zeeb 
169*67ca7330SBjoern A. Zeeb uint32_t
170*67ca7330SBjoern A. Zeeb sdio_readl(struct sdio_func *f, uint32_t addr, int *err)
171*67ca7330SBjoern A. Zeeb {
172*67ca7330SBjoern A. Zeeb 	int error;
173*67ca7330SBjoern A. Zeeb 	uint32_t v;
174*67ca7330SBjoern A. Zeeb 
175*67ca7330SBjoern A. Zeeb 	error = SDIO_READ_EXTENDED(device_get_parent(f->dev), f->fn, addr,
176*67ca7330SBjoern A. Zeeb 	    sizeof(v), (uint8_t *)&v, false);
177*67ca7330SBjoern A. Zeeb 	if (error) {
178*67ca7330SBjoern A. Zeeb 		if (err != NULL)
179*67ca7330SBjoern A. Zeeb 			*err = error;
180*67ca7330SBjoern A. Zeeb 		return (0xffffffff);
181*67ca7330SBjoern A. Zeeb 	} else {
182*67ca7330SBjoern A. Zeeb 		if (err != NULL)
183*67ca7330SBjoern A. Zeeb 			*err = 0;
184*67ca7330SBjoern A. Zeeb 		return (le32toh(v));
185*67ca7330SBjoern A. Zeeb 	}
186*67ca7330SBjoern A. Zeeb }
187*67ca7330SBjoern A. Zeeb 
188*67ca7330SBjoern A. Zeeb void
189*67ca7330SBjoern A. Zeeb sdio_writel(struct sdio_func *f, uint32_t val, uint32_t addr, int *err)
190*67ca7330SBjoern A. Zeeb {
191*67ca7330SBjoern A. Zeeb 	int error;
192*67ca7330SBjoern A. Zeeb 
193*67ca7330SBjoern A. Zeeb 	error = SDIO_WRITE_EXTENDED(device_get_parent(f->dev), f->fn, addr,
194*67ca7330SBjoern A. Zeeb 	    sizeof(val), (uint8_t *)&val, false);
195*67ca7330SBjoern A. Zeeb 	if (err != NULL)
196*67ca7330SBjoern A. Zeeb 		*err = error;
197*67ca7330SBjoern A. Zeeb }
198*67ca7330SBjoern A. Zeeb 
199*67ca7330SBjoern A. Zeeb uint8_t
200*67ca7330SBjoern A. Zeeb sdio_f0_readb(struct sdio_func *f, uint32_t addr, int *err)
201*67ca7330SBjoern A. Zeeb {
202*67ca7330SBjoern A. Zeeb 	int error;
203*67ca7330SBjoern A. Zeeb 	uint8_t v;
204*67ca7330SBjoern A. Zeeb 
205*67ca7330SBjoern A. Zeeb 	error = SDIO_READ_DIRECT(device_get_parent(f->dev), 0, addr, &v);
206*67ca7330SBjoern A. Zeeb 	if (error) {
207*67ca7330SBjoern A. Zeeb 		if (err != NULL)
208*67ca7330SBjoern A. Zeeb 			*err = error;
209*67ca7330SBjoern A. Zeeb 		return (0xff);
210*67ca7330SBjoern A. Zeeb 	} else {
211*67ca7330SBjoern A. Zeeb 		if (err != NULL)
212*67ca7330SBjoern A. Zeeb 			*err = 0;
213*67ca7330SBjoern A. Zeeb 		return (v);
214*67ca7330SBjoern A. Zeeb 	}
215*67ca7330SBjoern A. Zeeb }
216*67ca7330SBjoern A. Zeeb 
217*67ca7330SBjoern A. Zeeb void
218*67ca7330SBjoern A. Zeeb sdio_f0_writeb(struct sdio_func *f, uint8_t val, uint32_t addr, int *err)
219*67ca7330SBjoern A. Zeeb {
220*67ca7330SBjoern A. Zeeb 	int error;
221*67ca7330SBjoern A. Zeeb 
222*67ca7330SBjoern A. Zeeb 	error = SDIO_WRITE_DIRECT(device_get_parent(f->dev), 0, addr, val);
223*67ca7330SBjoern A. Zeeb 	if (err != NULL)
224*67ca7330SBjoern A. Zeeb 		*err = error;
225*67ca7330SBjoern A. Zeeb }
226*67ca7330SBjoern A. Zeeb 
227*67ca7330SBjoern A. Zeeb /* end */
228