xref: /freebsd/sys/arm/allwinner/aw_mmc.c (revision 3177f7cda13252910ee19249ccd3d400f288a774)
1b5be541fSEmmanuel Vadot /*-
2b5be541fSEmmanuel Vadot  * Copyright (c) 2013 Alexander Fedorov
3b5be541fSEmmanuel Vadot  * All rights reserved.
4b5be541fSEmmanuel Vadot  *
5b5be541fSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6b5be541fSEmmanuel Vadot  * modification, are permitted provided that the following conditions
7b5be541fSEmmanuel Vadot  * are met:
8b5be541fSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9b5be541fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10b5be541fSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11b5be541fSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12b5be541fSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13b5be541fSEmmanuel Vadot  *
14b5be541fSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15b5be541fSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b5be541fSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b5be541fSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18b5be541fSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b5be541fSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b5be541fSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b5be541fSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b5be541fSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b5be541fSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b5be541fSEmmanuel Vadot  * SUCH DAMAGE.
25b5be541fSEmmanuel Vadot  */
26b5be541fSEmmanuel Vadot 
27b5be541fSEmmanuel Vadot #include <sys/cdefs.h>
28b5be541fSEmmanuel Vadot __FBSDID("$FreeBSD$");
29b5be541fSEmmanuel Vadot 
30b5be541fSEmmanuel Vadot #include <sys/param.h>
31b5be541fSEmmanuel Vadot #include <sys/systm.h>
32b5be541fSEmmanuel Vadot #include <sys/bus.h>
33b5be541fSEmmanuel Vadot #include <sys/kernel.h>
34b5be541fSEmmanuel Vadot #include <sys/lock.h>
35b5be541fSEmmanuel Vadot #include <sys/malloc.h>
36b5be541fSEmmanuel Vadot #include <sys/module.h>
37b5be541fSEmmanuel Vadot #include <sys/mutex.h>
38b5be541fSEmmanuel Vadot #include <sys/resource.h>
39b5be541fSEmmanuel Vadot #include <sys/rman.h>
40b5be541fSEmmanuel Vadot #include <sys/sysctl.h>
41b5be541fSEmmanuel Vadot 
42b5be541fSEmmanuel Vadot #include <machine/bus.h>
43b5be541fSEmmanuel Vadot 
44b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
45b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
46b5be541fSEmmanuel Vadot 
47b5be541fSEmmanuel Vadot #include <dev/mmc/bridge.h>
48b5be541fSEmmanuel Vadot #include <dev/mmc/mmcbrvar.h>
49b5be541fSEmmanuel Vadot 
50b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h>
51b5be541fSEmmanuel Vadot #include <dev/extres/clk/clk.h>
52b5be541fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h>
53ce0618beSEmmanuel Vadot #include <dev/extres/regulator/regulator.h>
54b5be541fSEmmanuel Vadot 
55b5be541fSEmmanuel Vadot #define	AW_MMC_MEMRES		0
56b5be541fSEmmanuel Vadot #define	AW_MMC_IRQRES		1
57b5be541fSEmmanuel Vadot #define	AW_MMC_RESSZ		2
58b5be541fSEmmanuel Vadot #define	AW_MMC_DMA_SEGS		((MAXPHYS / PAGE_SIZE) + 1)
59b5be541fSEmmanuel Vadot #define	AW_MMC_DMA_FTRGLEVEL	0x20070008
60b5be541fSEmmanuel Vadot #define	AW_MMC_RESET_RETRY	1000
61b5be541fSEmmanuel Vadot 
62b5be541fSEmmanuel Vadot #define	CARD_ID_FREQUENCY	400000
63b5be541fSEmmanuel Vadot 
64ce0618beSEmmanuel Vadot struct aw_mmc_conf {
65ce0618beSEmmanuel Vadot 	uint32_t	dma_xferlen;
66ce0618beSEmmanuel Vadot 	bool		mask_data0;
67ce0618beSEmmanuel Vadot 	bool		can_calibrate;
68ce0618beSEmmanuel Vadot 	bool		new_timing;
69ce0618beSEmmanuel Vadot };
70ce0618beSEmmanuel Vadot 
71ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = {
72ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x2000,
73ce0618beSEmmanuel Vadot };
74ce0618beSEmmanuel Vadot 
75ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = {
76ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x10000,
77ce0618beSEmmanuel Vadot };
78ce0618beSEmmanuel Vadot 
79ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = {
80ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x10000,
81ce0618beSEmmanuel Vadot 	.mask_data0 = true,
82ce0618beSEmmanuel Vadot 	.can_calibrate = true,
83ce0618beSEmmanuel Vadot 	.new_timing = true,
84ce0618beSEmmanuel Vadot };
85ce0618beSEmmanuel Vadot 
86ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = {
87ce0618beSEmmanuel Vadot 	.dma_xferlen = 0x2000,
88ce0618beSEmmanuel Vadot 	.can_calibrate = true,
89ce0618beSEmmanuel Vadot };
90ce0618beSEmmanuel Vadot 
91b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
92ce0618beSEmmanuel Vadot 	{"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf},
93ce0618beSEmmanuel Vadot 	{"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf},
94ce0618beSEmmanuel Vadot 	{"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf},
95ce0618beSEmmanuel Vadot 	{"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf},
96ce0618beSEmmanuel Vadot 	{"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf},
97b5be541fSEmmanuel Vadot 	{NULL,             0}
98b5be541fSEmmanuel Vadot };
99b5be541fSEmmanuel Vadot 
100b5be541fSEmmanuel Vadot struct aw_mmc_softc {
101b5be541fSEmmanuel Vadot 	device_t		aw_dev;
102b5be541fSEmmanuel Vadot 	clk_t			aw_clk_ahb;
103b5be541fSEmmanuel Vadot 	clk_t			aw_clk_mmc;
104b5be541fSEmmanuel Vadot 	hwreset_t		aw_rst_ahb;
105b5be541fSEmmanuel Vadot 	int			aw_bus_busy;
106b5be541fSEmmanuel Vadot 	int			aw_resid;
107b5be541fSEmmanuel Vadot 	int			aw_timeout;
108b5be541fSEmmanuel Vadot 	struct callout		aw_timeoutc;
109b5be541fSEmmanuel Vadot 	struct mmc_host		aw_host;
110b5be541fSEmmanuel Vadot 	struct mmc_request *	aw_req;
111b5be541fSEmmanuel Vadot 	struct mtx		aw_mtx;
112b5be541fSEmmanuel Vadot 	struct resource *	aw_res[AW_MMC_RESSZ];
113ce0618beSEmmanuel Vadot 	struct aw_mmc_conf *	aw_mmc_conf;
114b5be541fSEmmanuel Vadot 	uint32_t		aw_intr;
115b5be541fSEmmanuel Vadot 	uint32_t		aw_intr_wait;
116b5be541fSEmmanuel Vadot 	void *			aw_intrhand;
117ce0618beSEmmanuel Vadot 	int32_t			aw_vdd;
118ce0618beSEmmanuel Vadot 	regulator_t		aw_reg_vmmc;
119ce0618beSEmmanuel Vadot 	regulator_t		aw_reg_vqmmc;
1200f7a6420SEmmanuel Vadot 	unsigned int		aw_clock;
121b5be541fSEmmanuel Vadot 
122b5be541fSEmmanuel Vadot 	/* Fields required for DMA access. */
123b5be541fSEmmanuel Vadot 	bus_addr_t	  	aw_dma_desc_phys;
124b5be541fSEmmanuel Vadot 	bus_dmamap_t		aw_dma_map;
125b5be541fSEmmanuel Vadot 	bus_dma_tag_t 		aw_dma_tag;
126b5be541fSEmmanuel Vadot 	void * 			aw_dma_desc;
127b5be541fSEmmanuel Vadot 	bus_dmamap_t		aw_dma_buf_map;
128b5be541fSEmmanuel Vadot 	bus_dma_tag_t		aw_dma_buf_tag;
129b5be541fSEmmanuel Vadot 	int			aw_dma_map_err;
130b5be541fSEmmanuel Vadot };
131b5be541fSEmmanuel Vadot 
132b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = {
133b5be541fSEmmanuel Vadot 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
134b5be541fSEmmanuel Vadot 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
135b5be541fSEmmanuel Vadot 	{ -1,			0,	0 }
136b5be541fSEmmanuel Vadot };
137b5be541fSEmmanuel Vadot 
138b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t);
139b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t);
140b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t);
141b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *);
142b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *);
143b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *);
144b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t);
145b5be541fSEmmanuel Vadot 
146b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t);
147b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *);
148b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t);
149b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t);
150b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t);
151b5be541fSEmmanuel Vadot 
152b5be541fSEmmanuel Vadot #define	AW_MMC_LOCK(_sc)	mtx_lock(&(_sc)->aw_mtx)
153b5be541fSEmmanuel Vadot #define	AW_MMC_UNLOCK(_sc)	mtx_unlock(&(_sc)->aw_mtx)
154b5be541fSEmmanuel Vadot #define	AW_MMC_READ_4(_sc, _reg)					\
155b5be541fSEmmanuel Vadot 	bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg)
156b5be541fSEmmanuel Vadot #define	AW_MMC_WRITE_4(_sc, _reg, _value)				\
157b5be541fSEmmanuel Vadot 	bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value)
158b5be541fSEmmanuel Vadot 
159b5be541fSEmmanuel Vadot static int
160b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev)
161b5be541fSEmmanuel Vadot {
162b5be541fSEmmanuel Vadot 
163b5be541fSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
164b5be541fSEmmanuel Vadot 		return (ENXIO);
165b5be541fSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
166b5be541fSEmmanuel Vadot 		return (ENXIO);
167b5be541fSEmmanuel Vadot 
168b5be541fSEmmanuel Vadot 	device_set_desc(dev, "Allwinner Integrated MMC/SD controller");
169b5be541fSEmmanuel Vadot 
170b5be541fSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
171b5be541fSEmmanuel Vadot }
172b5be541fSEmmanuel Vadot 
173b5be541fSEmmanuel Vadot static int
174b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev)
175b5be541fSEmmanuel Vadot {
176b5be541fSEmmanuel Vadot 	device_t child;
177b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
178b5be541fSEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
179b5be541fSEmmanuel Vadot 	struct sysctl_oid_list *tree;
180b5be541fSEmmanuel Vadot 	uint32_t bus_width;
181b5be541fSEmmanuel Vadot 	phandle_t node;
182b5be541fSEmmanuel Vadot 	int error;
183b5be541fSEmmanuel Vadot 
184b5be541fSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
185b5be541fSEmmanuel Vadot 	sc = device_get_softc(dev);
186b5be541fSEmmanuel Vadot 	sc->aw_dev = dev;
187ce0618beSEmmanuel Vadot 
188ce0618beSEmmanuel Vadot 	sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
189ce0618beSEmmanuel Vadot 
190b5be541fSEmmanuel Vadot 	sc->aw_req = NULL;
191b5be541fSEmmanuel Vadot 	if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) {
192b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot allocate device resources\n");
193b5be541fSEmmanuel Vadot 		return (ENXIO);
194b5be541fSEmmanuel Vadot 	}
195b5be541fSEmmanuel Vadot 	if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES],
196b5be541fSEmmanuel Vadot 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_mmc_intr, sc,
197b5be541fSEmmanuel Vadot 	    &sc->aw_intrhand)) {
198b5be541fSEmmanuel Vadot 		bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
199b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot setup interrupt handler\n");
200b5be541fSEmmanuel Vadot 		return (ENXIO);
201b5be541fSEmmanuel Vadot 	}
202b5be541fSEmmanuel Vadot 	mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc",
203b5be541fSEmmanuel Vadot 	    MTX_DEF);
204b5be541fSEmmanuel Vadot 	callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0);
205b5be541fSEmmanuel Vadot 
206b5be541fSEmmanuel Vadot 	/* De-assert reset */
207b5be541fSEmmanuel Vadot 	if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) {
208b5be541fSEmmanuel Vadot 		error = hwreset_deassert(sc->aw_rst_ahb);
209b5be541fSEmmanuel Vadot 		if (error != 0) {
210b5be541fSEmmanuel Vadot 			device_printf(dev, "cannot de-assert reset\n");
211b5be541fSEmmanuel Vadot 			goto fail;
212b5be541fSEmmanuel Vadot 		}
213b5be541fSEmmanuel Vadot 	}
214b5be541fSEmmanuel Vadot 
215b5be541fSEmmanuel Vadot 	/* Activate the module clock. */
216b5be541fSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb);
217b5be541fSEmmanuel Vadot 	if (error != 0) {
218b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot get ahb clock\n");
219b5be541fSEmmanuel Vadot 		goto fail;
220b5be541fSEmmanuel Vadot 	}
221b5be541fSEmmanuel Vadot 	error = clk_enable(sc->aw_clk_ahb);
222b5be541fSEmmanuel Vadot 	if (error != 0) {
223b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot enable ahb clock\n");
224b5be541fSEmmanuel Vadot 		goto fail;
225b5be541fSEmmanuel Vadot 	}
226b5be541fSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc);
227b5be541fSEmmanuel Vadot 	if (error != 0) {
228b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot get mmc clock\n");
229b5be541fSEmmanuel Vadot 		goto fail;
230b5be541fSEmmanuel Vadot 	}
231b5be541fSEmmanuel Vadot 	error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY,
232b5be541fSEmmanuel Vadot 	    CLK_SET_ROUND_DOWN);
233b5be541fSEmmanuel Vadot 	if (error != 0) {
234b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot init mmc clock\n");
235b5be541fSEmmanuel Vadot 		goto fail;
236b5be541fSEmmanuel Vadot 	}
237b5be541fSEmmanuel Vadot 	error = clk_enable(sc->aw_clk_mmc);
238b5be541fSEmmanuel Vadot 	if (error != 0) {
239b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot enable mmc clock\n");
240b5be541fSEmmanuel Vadot 		goto fail;
241b5be541fSEmmanuel Vadot 	}
242b5be541fSEmmanuel Vadot 
243b5be541fSEmmanuel Vadot 	sc->aw_timeout = 10;
244b5be541fSEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
245b5be541fSEmmanuel Vadot 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
246b5be541fSEmmanuel Vadot 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW,
247b5be541fSEmmanuel Vadot 	    &sc->aw_timeout, 0, "Request timeout in seconds");
248b5be541fSEmmanuel Vadot 
249b5be541fSEmmanuel Vadot 	/* Hardware reset */
250b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_HWRST, 1);
251b5be541fSEmmanuel Vadot 	DELAY(100);
252b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_HWRST, 0);
253b5be541fSEmmanuel Vadot 	DELAY(500);
254b5be541fSEmmanuel Vadot 
255b5be541fSEmmanuel Vadot 	/* Soft Reset controller. */
256b5be541fSEmmanuel Vadot 	if (aw_mmc_reset(sc) != 0) {
257b5be541fSEmmanuel Vadot 		device_printf(dev, "cannot reset the controller\n");
258b5be541fSEmmanuel Vadot 		goto fail;
259b5be541fSEmmanuel Vadot 	}
260b5be541fSEmmanuel Vadot 
261b5be541fSEmmanuel Vadot 	if (aw_mmc_setup_dma(sc) != 0) {
262b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "Couldn't setup DMA!\n");
263b5be541fSEmmanuel Vadot 		goto fail;
264b5be541fSEmmanuel Vadot 	}
265b5be541fSEmmanuel Vadot 
266b5be541fSEmmanuel Vadot 	if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0)
267b5be541fSEmmanuel Vadot 		bus_width = 4;
268b5be541fSEmmanuel Vadot 
269ce0618beSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply",
270*3177f7cdSEmmanuel Vadot 	    &sc->aw_reg_vmmc) == 0) {
271*3177f7cdSEmmanuel Vadot 		if (bootverbose)
272ce0618beSEmmanuel Vadot 			device_printf(dev, "vmmc-supply regulator found\n");
273*3177f7cdSEmmanuel Vadot 		regulator_enable(sc->aw_reg_vmmc);
274*3177f7cdSEmmanuel Vadot 	}
275ce0618beSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
276*3177f7cdSEmmanuel Vadot 	    &sc->aw_reg_vqmmc) == 0 && bootverbose) {
277*3177f7cdSEmmanuel Vadot 		if (bootverbose)
278ce0618beSEmmanuel Vadot 			device_printf(dev, "vqmmc-supply regulator found\n");
279*3177f7cdSEmmanuel Vadot 		regulator_enable(sc->aw_reg_vqmmc);
280*3177f7cdSEmmanuel Vadot 	}
281ce0618beSEmmanuel Vadot 
282b5be541fSEmmanuel Vadot 	sc->aw_host.f_min = 400000;
283b5be541fSEmmanuel Vadot 	sc->aw_host.f_max = 52000000;
284b5be541fSEmmanuel Vadot 	sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
285ce0618beSEmmanuel Vadot 	sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 |
286ce0618beSEmmanuel Vadot 			   MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 |
287ce0618beSEmmanuel Vadot 			   MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52;
288ce0618beSEmmanuel Vadot 
289ce0618beSEmmanuel Vadot 	sc->aw_host.caps |= MMC_CAP_SIGNALING_330 /* | MMC_CAP_SIGNALING_180 */;
290ce0618beSEmmanuel Vadot 
291b5be541fSEmmanuel Vadot 	if (bus_width >= 4)
292b5be541fSEmmanuel Vadot 		sc->aw_host.caps |= MMC_CAP_4_BIT_DATA;
293b5be541fSEmmanuel Vadot 	if (bus_width >= 8)
294b5be541fSEmmanuel Vadot 		sc->aw_host.caps |= MMC_CAP_8_BIT_DATA;
295b5be541fSEmmanuel Vadot 
296b5be541fSEmmanuel Vadot 	child = device_add_child(dev, "mmc", -1);
297b5be541fSEmmanuel Vadot 	if (child == NULL) {
298b5be541fSEmmanuel Vadot 		device_printf(dev, "attaching MMC bus failed!\n");
299b5be541fSEmmanuel Vadot 		goto fail;
300b5be541fSEmmanuel Vadot 	}
301b5be541fSEmmanuel Vadot 	if (device_probe_and_attach(child) != 0) {
302b5be541fSEmmanuel Vadot 		device_printf(dev, "attaching MMC child failed!\n");
303b5be541fSEmmanuel Vadot 		device_delete_child(dev, child);
304b5be541fSEmmanuel Vadot 		goto fail;
305b5be541fSEmmanuel Vadot 	}
306b5be541fSEmmanuel Vadot 
307b5be541fSEmmanuel Vadot 	return (0);
308b5be541fSEmmanuel Vadot 
309b5be541fSEmmanuel Vadot fail:
310b5be541fSEmmanuel Vadot 	callout_drain(&sc->aw_timeoutc);
311b5be541fSEmmanuel Vadot 	mtx_destroy(&sc->aw_mtx);
312b5be541fSEmmanuel Vadot 	bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand);
313b5be541fSEmmanuel Vadot 	bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
314b5be541fSEmmanuel Vadot 
315b5be541fSEmmanuel Vadot 	return (ENXIO);
316b5be541fSEmmanuel Vadot }
317b5be541fSEmmanuel Vadot 
318b5be541fSEmmanuel Vadot static int
319b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev)
320b5be541fSEmmanuel Vadot {
321b5be541fSEmmanuel Vadot 
322b5be541fSEmmanuel Vadot 	return (EBUSY);
323b5be541fSEmmanuel Vadot }
324b5be541fSEmmanuel Vadot 
325b5be541fSEmmanuel Vadot static void
326b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
327b5be541fSEmmanuel Vadot {
328b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
329b5be541fSEmmanuel Vadot 
330b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
331b5be541fSEmmanuel Vadot 	if (err) {
332b5be541fSEmmanuel Vadot 		sc->aw_dma_map_err = err;
333b5be541fSEmmanuel Vadot 		return;
334b5be541fSEmmanuel Vadot 	}
335b5be541fSEmmanuel Vadot 	sc->aw_dma_desc_phys = segs[0].ds_addr;
336b5be541fSEmmanuel Vadot }
337b5be541fSEmmanuel Vadot 
338b5be541fSEmmanuel Vadot static int
339b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc)
340b5be541fSEmmanuel Vadot {
341b5be541fSEmmanuel Vadot 	int dma_desc_size, error;
342b5be541fSEmmanuel Vadot 
343b5be541fSEmmanuel Vadot 	/* Allocate the DMA descriptor memory. */
344b5be541fSEmmanuel Vadot 	dma_desc_size = sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS;
345b5be541fSEmmanuel Vadot 	error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev),
346b5be541fSEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,
347b5be541fSEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
348b5be541fSEmmanuel Vadot 	    dma_desc_size, 1, dma_desc_size, 0, NULL, NULL, &sc->aw_dma_tag);
349b5be541fSEmmanuel Vadot 	if (error)
350b5be541fSEmmanuel Vadot 		return (error);
351b5be541fSEmmanuel Vadot 	error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc,
352b5be541fSEmmanuel Vadot 	    BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->aw_dma_map);
353b5be541fSEmmanuel Vadot 	if (error)
354b5be541fSEmmanuel Vadot 		return (error);
355b5be541fSEmmanuel Vadot 
356b5be541fSEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_tag, sc->aw_dma_map,
357b5be541fSEmmanuel Vadot 	    sc->aw_dma_desc, dma_desc_size, aw_dma_desc_cb, sc, 0);
358b5be541fSEmmanuel Vadot 	if (error)
359b5be541fSEmmanuel Vadot 		return (error);
360b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
361b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
362b5be541fSEmmanuel Vadot 
363b5be541fSEmmanuel Vadot 	/* Create the DMA map for data transfers. */
364b5be541fSEmmanuel Vadot 	error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev),
365b5be541fSEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,
366b5be541fSEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
367ce0618beSEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS,
368ce0618beSEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen, BUS_DMA_ALLOCNOW, NULL, NULL,
369b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_tag);
370b5be541fSEmmanuel Vadot 	if (error)
371b5be541fSEmmanuel Vadot 		return (error);
372b5be541fSEmmanuel Vadot 	error = bus_dmamap_create(sc->aw_dma_buf_tag, 0,
373b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_map);
374b5be541fSEmmanuel Vadot 	if (error)
375b5be541fSEmmanuel Vadot 		return (error);
376b5be541fSEmmanuel Vadot 
377b5be541fSEmmanuel Vadot 	return (0);
378b5be541fSEmmanuel Vadot }
379b5be541fSEmmanuel Vadot 
380b5be541fSEmmanuel Vadot static void
381b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
382b5be541fSEmmanuel Vadot {
383b5be541fSEmmanuel Vadot 	int i;
384b5be541fSEmmanuel Vadot 	struct aw_mmc_dma_desc *dma_desc;
385b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
386b5be541fSEmmanuel Vadot 
387b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
388b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = err;
389b5be541fSEmmanuel Vadot 
390b5be541fSEmmanuel Vadot 	if (err)
391b5be541fSEmmanuel Vadot 		return;
392b5be541fSEmmanuel Vadot 
393b5be541fSEmmanuel Vadot 	dma_desc = sc->aw_dma_desc;
394b5be541fSEmmanuel Vadot 	for (i = 0; i < nsegs; i++) {
395b5be541fSEmmanuel Vadot 		dma_desc[i].buf_size = segs[i].ds_len;
396b5be541fSEmmanuel Vadot 		dma_desc[i].buf_addr = segs[i].ds_addr;
397b5be541fSEmmanuel Vadot 		dma_desc[i].config = AW_MMC_DMA_CONFIG_CH |
398b5be541fSEmmanuel Vadot 		    AW_MMC_DMA_CONFIG_OWN;
399b5be541fSEmmanuel Vadot 		if (i == 0)
400b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_FD;
401b5be541fSEmmanuel Vadot 		if (i < (nsegs - 1)) {
402b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_DIC;
403b5be541fSEmmanuel Vadot 			dma_desc[i].next = sc->aw_dma_desc_phys +
404b5be541fSEmmanuel Vadot 			    ((i + 1) * sizeof(struct aw_mmc_dma_desc));
405b5be541fSEmmanuel Vadot 		} else {
406b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_LD |
407b5be541fSEmmanuel Vadot 			    AW_MMC_DMA_CONFIG_ER;
408b5be541fSEmmanuel Vadot 			dma_desc[i].next = 0;
409b5be541fSEmmanuel Vadot 		}
410b5be541fSEmmanuel Vadot 	}
411b5be541fSEmmanuel Vadot }
412b5be541fSEmmanuel Vadot 
413b5be541fSEmmanuel Vadot static int
414b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc)
415b5be541fSEmmanuel Vadot {
416b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
417b5be541fSEmmanuel Vadot 	int error;
418b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
419b5be541fSEmmanuel Vadot 	uint32_t val;
420b5be541fSEmmanuel Vadot 
421b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
422ce0618beSEmmanuel Vadot 	if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS))
423b5be541fSEmmanuel Vadot 		return (EFBIG);
424b5be541fSEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
425b5be541fSEmmanuel Vadot 	    cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0);
426b5be541fSEmmanuel Vadot 	if (error)
427b5be541fSEmmanuel Vadot 		return (error);
428b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
429b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
430b5be541fSEmmanuel Vadot 
431b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
432b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREWRITE;
433b5be541fSEmmanuel Vadot 	else
434b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREREAD;
435b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op);
436b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE);
437b5be541fSEmmanuel Vadot 
438b5be541fSEmmanuel Vadot 	/* Enable DMA */
439b5be541fSEmmanuel Vadot 	val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
440b5be541fSEmmanuel Vadot 	val &= ~AW_MMC_CTRL_FIFO_AC_MOD;
441b5be541fSEmmanuel Vadot 	val |= AW_MMC_CTRL_DMA_ENB;
442b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
443b5be541fSEmmanuel Vadot 
444b5be541fSEmmanuel Vadot 	/* Reset DMA */
445b5be541fSEmmanuel Vadot 	val |= AW_MMC_CTRL_DMA_RST;
446b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
447b5be541fSEmmanuel Vadot 
448b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST);
449b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC,
450b5be541fSEmmanuel Vadot 	    AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST);
451b5be541fSEmmanuel Vadot 
452b5be541fSEmmanuel Vadot 	/* Enable RX or TX DMA interrupt */
453b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
454b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_TX_INT;
455b5be541fSEmmanuel Vadot 	else
456b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_RX_INT;
457b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val);
458b5be541fSEmmanuel Vadot 
459b5be541fSEmmanuel Vadot 	/* Set DMA descritptor list address */
460b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys);
461b5be541fSEmmanuel Vadot 
462b5be541fSEmmanuel Vadot 	/* FIFO trigger level */
463b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL);
464b5be541fSEmmanuel Vadot 
465b5be541fSEmmanuel Vadot 	return (0);
466b5be541fSEmmanuel Vadot }
467b5be541fSEmmanuel Vadot 
468b5be541fSEmmanuel Vadot static int
469b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc)
470b5be541fSEmmanuel Vadot {
471b5be541fSEmmanuel Vadot 	int timeout;
472b5be541fSEmmanuel Vadot 
473b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, AW_MMC_RESET);
474b5be541fSEmmanuel Vadot 	timeout = 1000;
475b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
476b5be541fSEmmanuel Vadot 		if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_RESET) == 0)
477b5be541fSEmmanuel Vadot 			break;
478b5be541fSEmmanuel Vadot 		DELAY(100);
479b5be541fSEmmanuel Vadot 	}
480b5be541fSEmmanuel Vadot 	if (timeout == 0)
481b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
482b5be541fSEmmanuel Vadot 
483b5be541fSEmmanuel Vadot 	/* Set the timeout. */
484b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_TMOR,
485b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) |
486b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK));
487b5be541fSEmmanuel Vadot 
488b5be541fSEmmanuel Vadot 	/* Clear pending interrupts. */
489b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
490b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff);
491b5be541fSEmmanuel Vadot 	/* Unmask interrupts. */
492b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IMKR,
493b5be541fSEmmanuel Vadot 	    AW_MMC_INT_CMD_DONE | AW_MMC_INT_ERR_BIT |
494b5be541fSEmmanuel Vadot 	    AW_MMC_INT_DATA_OVER | AW_MMC_INT_AUTO_STOP_DONE);
495b5be541fSEmmanuel Vadot 	/* Enable interrupts and AHB access. */
496b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL,
497b5be541fSEmmanuel Vadot 	    AW_MMC_READ_4(sc, AW_MMC_GCTL) | AW_MMC_CTRL_INT_ENB);
498b5be541fSEmmanuel Vadot 
499b5be541fSEmmanuel Vadot 	return (0);
500b5be541fSEmmanuel Vadot }
501b5be541fSEmmanuel Vadot 
502b5be541fSEmmanuel Vadot static void
503b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc)
504b5be541fSEmmanuel Vadot {
505b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
506b5be541fSEmmanuel Vadot 	struct mmc_request *req;
507b5be541fSEmmanuel Vadot 	uint32_t val, mask;
508b5be541fSEmmanuel Vadot 	int retry;
509b5be541fSEmmanuel Vadot 
510b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
511b5be541fSEmmanuel Vadot 	if (cmd->error != MMC_ERR_NONE) {
512b5be541fSEmmanuel Vadot 		/* Reset the FIFO and DMA engines. */
513b5be541fSEmmanuel Vadot 		mask = AW_MMC_CTRL_FIFO_RST | AW_MMC_CTRL_DMA_RST;
514b5be541fSEmmanuel Vadot 		val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
515b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask);
516b5be541fSEmmanuel Vadot 
517b5be541fSEmmanuel Vadot 		retry = AW_MMC_RESET_RETRY;
518b5be541fSEmmanuel Vadot 		while (--retry > 0) {
519b5be541fSEmmanuel Vadot 			val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
520b5be541fSEmmanuel Vadot 			if ((val & mask) == 0)
521b5be541fSEmmanuel Vadot 				break;
522b5be541fSEmmanuel Vadot 			DELAY(10);
523b5be541fSEmmanuel Vadot 		}
524b5be541fSEmmanuel Vadot 		if (retry == 0)
525b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
526b5be541fSEmmanuel Vadot 			    "timeout resetting DMA/FIFO\n");
527b5be541fSEmmanuel Vadot 		aw_mmc_update_clock(sc, 1);
528b5be541fSEmmanuel Vadot 	}
529b5be541fSEmmanuel Vadot 
530b5be541fSEmmanuel Vadot 	req = sc->aw_req;
531b5be541fSEmmanuel Vadot 	callout_stop(&sc->aw_timeoutc);
532b5be541fSEmmanuel Vadot 	sc->aw_req = NULL;
533b5be541fSEmmanuel Vadot 	sc->aw_intr = 0;
534b5be541fSEmmanuel Vadot 	sc->aw_resid = 0;
535b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = 0;
536b5be541fSEmmanuel Vadot 	sc->aw_intr_wait = 0;
537b5be541fSEmmanuel Vadot 	req->done(req);
538b5be541fSEmmanuel Vadot }
539b5be541fSEmmanuel Vadot 
540b5be541fSEmmanuel Vadot static void
541b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc)
542b5be541fSEmmanuel Vadot {
543b5be541fSEmmanuel Vadot 	int timeout;
544b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
545b5be541fSEmmanuel Vadot 	uint32_t status;
546b5be541fSEmmanuel Vadot 
547b5be541fSEmmanuel Vadot 	timeout = 1000;
548b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
549b5be541fSEmmanuel Vadot 		status = AW_MMC_READ_4(sc, AW_MMC_STAR);
550b5be541fSEmmanuel Vadot 		if ((status & AW_MMC_STAR_CARD_BUSY) == 0)
551b5be541fSEmmanuel Vadot 			break;
552b5be541fSEmmanuel Vadot 		DELAY(1000);
553b5be541fSEmmanuel Vadot 	}
554b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
555b5be541fSEmmanuel Vadot 	if (timeout == 0) {
556b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
557b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
558b5be541fSEmmanuel Vadot 		return;
559b5be541fSEmmanuel Vadot 	}
560b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT) {
561b5be541fSEmmanuel Vadot 		if (cmd->flags & MMC_RSP_136) {
562b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3);
563b5be541fSEmmanuel Vadot 			cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2);
564b5be541fSEmmanuel Vadot 			cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1);
565b5be541fSEmmanuel Vadot 			cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
566b5be541fSEmmanuel Vadot 		} else
567b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
568b5be541fSEmmanuel Vadot 	}
569b5be541fSEmmanuel Vadot 	/* All data has been transferred ? */
570b5be541fSEmmanuel Vadot 	if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len)
571b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
572b5be541fSEmmanuel Vadot 	aw_mmc_req_done(sc);
573b5be541fSEmmanuel Vadot }
574b5be541fSEmmanuel Vadot 
575b5be541fSEmmanuel Vadot static void
576b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg)
577b5be541fSEmmanuel Vadot {
578b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
579b5be541fSEmmanuel Vadot 
580b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
581b5be541fSEmmanuel Vadot 	if (sc->aw_req != NULL) {
582b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "controller timeout\n");
583b5be541fSEmmanuel Vadot 		sc->aw_req->cmd->error = MMC_ERR_TIMEOUT;
584b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
585b5be541fSEmmanuel Vadot 	} else
586b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
587b5be541fSEmmanuel Vadot 		    "Spurious timeout - no active request\n");
588b5be541fSEmmanuel Vadot }
589b5be541fSEmmanuel Vadot 
590b5be541fSEmmanuel Vadot static void
591b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg)
592b5be541fSEmmanuel Vadot {
593b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
594b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
595b5be541fSEmmanuel Vadot 	struct mmc_data *data;
596b5be541fSEmmanuel Vadot 	uint32_t idst, imask, rint;
597b5be541fSEmmanuel Vadot 
598b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
599b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
600b5be541fSEmmanuel Vadot 	rint = AW_MMC_READ_4(sc, AW_MMC_RISR);
601b5be541fSEmmanuel Vadot 	idst = AW_MMC_READ_4(sc, AW_MMC_IDST);
602b5be541fSEmmanuel Vadot 	imask = AW_MMC_READ_4(sc, AW_MMC_IMKR);
603b5be541fSEmmanuel Vadot 	if (idst == 0 && imask == 0 && rint == 0) {
604b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
605b5be541fSEmmanuel Vadot 		return;
606b5be541fSEmmanuel Vadot 	}
607b5be541fSEmmanuel Vadot #ifdef DEBUG
608b5be541fSEmmanuel Vadot 	device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n",
609b5be541fSEmmanuel Vadot 	    idst, imask, rint);
610b5be541fSEmmanuel Vadot #endif
611b5be541fSEmmanuel Vadot 	if (sc->aw_req == NULL) {
612b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
613b5be541fSEmmanuel Vadot 		    "Spurious interrupt - no active request, rint: 0x%08X\n",
614b5be541fSEmmanuel Vadot 		    rint);
615b5be541fSEmmanuel Vadot 		goto end;
616b5be541fSEmmanuel Vadot 	}
617b5be541fSEmmanuel Vadot 	if (rint & AW_MMC_INT_ERR_BIT) {
618ce0618beSEmmanuel Vadot 		if (bootverbose)
619b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint);
620b5be541fSEmmanuel Vadot 		if (rint & AW_MMC_INT_RESP_TIMEOUT)
621b5be541fSEmmanuel Vadot 			sc->aw_req->cmd->error = MMC_ERR_TIMEOUT;
622b5be541fSEmmanuel Vadot 		else
623b5be541fSEmmanuel Vadot 			sc->aw_req->cmd->error = MMC_ERR_FAILED;
624b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
625b5be541fSEmmanuel Vadot 		goto end;
626b5be541fSEmmanuel Vadot 	}
627b5be541fSEmmanuel Vadot 	if (idst & AW_MMC_IDST_ERROR) {
628b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst);
629b5be541fSEmmanuel Vadot 		sc->aw_req->cmd->error = MMC_ERR_FAILED;
630b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
631b5be541fSEmmanuel Vadot 		goto end;
632b5be541fSEmmanuel Vadot 	}
633b5be541fSEmmanuel Vadot 
634b5be541fSEmmanuel Vadot 	sc->aw_intr |= rint;
635b5be541fSEmmanuel Vadot 	data = sc->aw_req->cmd->data;
636b5be541fSEmmanuel Vadot 	if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) {
637b5be541fSEmmanuel Vadot 		if (data->flags & MMC_DATA_WRITE)
638b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTWRITE;
639b5be541fSEmmanuel Vadot 		else
640b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTREAD;
641b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
642b5be541fSEmmanuel Vadot 		    sync_op);
643b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map,
644b5be541fSEmmanuel Vadot 		    BUS_DMASYNC_POSTWRITE);
645b5be541fSEmmanuel Vadot 		bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map);
646b5be541fSEmmanuel Vadot 		sc->aw_resid = data->len >> 2;
647b5be541fSEmmanuel Vadot 	}
648b5be541fSEmmanuel Vadot 	if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait)
649b5be541fSEmmanuel Vadot 		aw_mmc_req_ok(sc);
650b5be541fSEmmanuel Vadot 
651b5be541fSEmmanuel Vadot end:
652b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst);
653b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint);
654b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
655b5be541fSEmmanuel Vadot }
656b5be541fSEmmanuel Vadot 
657b5be541fSEmmanuel Vadot static int
658b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req)
659b5be541fSEmmanuel Vadot {
660b5be541fSEmmanuel Vadot 	int blksz;
661b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
662b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
663b5be541fSEmmanuel Vadot 	uint32_t cmdreg;
664b5be541fSEmmanuel Vadot 	int err;
665b5be541fSEmmanuel Vadot 
666b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
667b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
668b5be541fSEmmanuel Vadot 	if (sc->aw_req) {
669b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
670b5be541fSEmmanuel Vadot 		return (EBUSY);
671b5be541fSEmmanuel Vadot 	}
672b5be541fSEmmanuel Vadot 	sc->aw_req = req;
673b5be541fSEmmanuel Vadot 	cmd = req->cmd;
674b5be541fSEmmanuel Vadot 	cmdreg = AW_MMC_CMDR_LOAD;
675b5be541fSEmmanuel Vadot 	if (cmd->opcode == MMC_GO_IDLE_STATE)
676b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ;
677b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT)
678b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_RESP_RCV;
679b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_136)
680b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_LONG_RESP;
681b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_CRC)
682b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC;
683b5be541fSEmmanuel Vadot 
684b5be541fSEmmanuel Vadot 	sc->aw_intr = 0;
685b5be541fSEmmanuel Vadot 	sc->aw_resid = 0;
686b5be541fSEmmanuel Vadot 	sc->aw_intr_wait = AW_MMC_INT_CMD_DONE;
687b5be541fSEmmanuel Vadot 	cmd->error = MMC_ERR_NONE;
688b5be541fSEmmanuel Vadot 	if (cmd->data != NULL) {
689b5be541fSEmmanuel Vadot 		sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER;
690b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER;
691b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_MULTI) {
692b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG;
693b5be541fSEmmanuel Vadot 			sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE;
694b5be541fSEmmanuel Vadot 		}
695b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_WRITE)
696b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_DIR_WRITE;
697b5be541fSEmmanuel Vadot 		blksz = min(cmd->data->len, MMC_SECTOR_SIZE);
698b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz);
699b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len);
700b5be541fSEmmanuel Vadot 
701b5be541fSEmmanuel Vadot 		err = aw_mmc_prepare_dma(sc);
702b5be541fSEmmanuel Vadot 		if (err != 0)
703b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err);
704b5be541fSEmmanuel Vadot 	}
705b5be541fSEmmanuel Vadot 
706b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg);
707b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode);
708b5be541fSEmmanuel Vadot 	callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz,
709b5be541fSEmmanuel Vadot 	    aw_mmc_timeout, sc);
710b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
711b5be541fSEmmanuel Vadot 
712b5be541fSEmmanuel Vadot 	return (0);
713b5be541fSEmmanuel Vadot }
714b5be541fSEmmanuel Vadot 
715b5be541fSEmmanuel Vadot static int
716b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which,
717b5be541fSEmmanuel Vadot     uintptr_t *result)
718b5be541fSEmmanuel Vadot {
719b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
720b5be541fSEmmanuel Vadot 
721b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
722b5be541fSEmmanuel Vadot 	switch (which) {
723b5be541fSEmmanuel Vadot 	default:
724b5be541fSEmmanuel Vadot 		return (EINVAL);
725b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
726b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_mode;
727b5be541fSEmmanuel Vadot 		break;
728b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
729b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_width;
730b5be541fSEmmanuel Vadot 		break;
731b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
732b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.chip_select;
733b5be541fSEmmanuel Vadot 		break;
734b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
735b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.clock;
736b5be541fSEmmanuel Vadot 		break;
737b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
738b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_min;
739b5be541fSEmmanuel Vadot 		break;
740b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
741b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_max;
742b5be541fSEmmanuel Vadot 		break;
743b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
744b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.host_ocr;
745b5be541fSEmmanuel Vadot 		break;
746b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
747b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.mode;
748b5be541fSEmmanuel Vadot 		break;
749b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
750b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ocr;
751b5be541fSEmmanuel Vadot 		break;
752b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
753b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.power_mode;
754b5be541fSEmmanuel Vadot 		break;
755b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
756b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.vdd;
757b5be541fSEmmanuel Vadot 		break;
758b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
759b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.caps;
760b5be541fSEmmanuel Vadot 		break;
761ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
762ce0618beSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.timing;
763ce0618beSEmmanuel Vadot 		break;
764b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
765b5be541fSEmmanuel Vadot 		*(int *)result = 65535;
766b5be541fSEmmanuel Vadot 		break;
767b5be541fSEmmanuel Vadot 	}
768b5be541fSEmmanuel Vadot 
769b5be541fSEmmanuel Vadot 	return (0);
770b5be541fSEmmanuel Vadot }
771b5be541fSEmmanuel Vadot 
772b5be541fSEmmanuel Vadot static int
773b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which,
774b5be541fSEmmanuel Vadot     uintptr_t value)
775b5be541fSEmmanuel Vadot {
776b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
777b5be541fSEmmanuel Vadot 
778b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
779b5be541fSEmmanuel Vadot 	switch (which) {
780b5be541fSEmmanuel Vadot 	default:
781b5be541fSEmmanuel Vadot 		return (EINVAL);
782b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
783b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_mode = value;
784b5be541fSEmmanuel Vadot 		break;
785b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
786b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_width = value;
787b5be541fSEmmanuel Vadot 		break;
788b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
789b5be541fSEmmanuel Vadot 		sc->aw_host.ios.chip_select = value;
790b5be541fSEmmanuel Vadot 		break;
791b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
792b5be541fSEmmanuel Vadot 		sc->aw_host.ios.clock = value;
793b5be541fSEmmanuel Vadot 		break;
794b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
795b5be541fSEmmanuel Vadot 		sc->aw_host.mode = value;
796b5be541fSEmmanuel Vadot 		break;
797b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
798b5be541fSEmmanuel Vadot 		sc->aw_host.ocr = value;
799b5be541fSEmmanuel Vadot 		break;
800b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
801b5be541fSEmmanuel Vadot 		sc->aw_host.ios.power_mode = value;
802b5be541fSEmmanuel Vadot 		break;
803b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
804b5be541fSEmmanuel Vadot 		sc->aw_host.ios.vdd = value;
805b5be541fSEmmanuel Vadot 		break;
806ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
807ce0618beSEmmanuel Vadot 		sc->aw_host.ios.timing = value;
808ce0618beSEmmanuel Vadot 		break;
809b5be541fSEmmanuel Vadot 	/* These are read-only */
810b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
811b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
812b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
813b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
814b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
815b5be541fSEmmanuel Vadot 		return (EINVAL);
816b5be541fSEmmanuel Vadot 	}
817b5be541fSEmmanuel Vadot 
818b5be541fSEmmanuel Vadot 	return (0);
819b5be541fSEmmanuel Vadot }
820b5be541fSEmmanuel Vadot 
821b5be541fSEmmanuel Vadot static int
822b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon)
823b5be541fSEmmanuel Vadot {
824ce0618beSEmmanuel Vadot 	uint32_t reg;
825b5be541fSEmmanuel Vadot 	int retry;
826b5be541fSEmmanuel Vadot 
827ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
828ce0618beSEmmanuel Vadot 	reg &= ~(AW_MMC_CKCR_CCLK_ENB | AW_MMC_CKCR_CCLK_CTRL |
829ce0618beSEmmanuel Vadot 	    AW_MMC_CKCR_CCLK_MASK_DATA0);
830b5be541fSEmmanuel Vadot 
831b5be541fSEmmanuel Vadot 	if (clkon)
832ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CKCR_CCLK_ENB;
833ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0)
834ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CKCR_CCLK_MASK_DATA0;
835b5be541fSEmmanuel Vadot 
836ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
837b5be541fSEmmanuel Vadot 
838ce0618beSEmmanuel Vadot 	reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK |
839b5be541fSEmmanuel Vadot 	    AW_MMC_CMDR_WAIT_PRE_OVER;
840ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg);
841b5be541fSEmmanuel Vadot 	retry = 0xfffff;
842ce0618beSEmmanuel Vadot 
843ce0618beSEmmanuel Vadot 	while (reg & AW_MMC_CMDR_LOAD && --retry > 0) {
844ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CMDR);
845b5be541fSEmmanuel Vadot 		DELAY(10);
846b5be541fSEmmanuel Vadot 	}
847b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
848b5be541fSEmmanuel Vadot 
849ce0618beSEmmanuel Vadot 	if (reg & AW_MMC_CMDR_LOAD) {
850ce0618beSEmmanuel Vadot 		device_printf(sc->aw_dev, "timeout updating clock\n");
851b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
852b5be541fSEmmanuel Vadot 	}
853b5be541fSEmmanuel Vadot 
854ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0) {
855ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
856ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_CCLK_MASK_DATA0;
857ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
858ce0618beSEmmanuel Vadot 	}
859ce0618beSEmmanuel Vadot 
860ce0618beSEmmanuel Vadot 	return (0);
861ce0618beSEmmanuel Vadot }
862ce0618beSEmmanuel Vadot 
863ce0618beSEmmanuel Vadot static void
864ce0618beSEmmanuel Vadot aw_mmc_set_power(struct aw_mmc_softc *sc, int32_t vdd)
865ce0618beSEmmanuel Vadot {
866ce0618beSEmmanuel Vadot 	int min_uvolt, max_uvolt;
867ce0618beSEmmanuel Vadot 
868ce0618beSEmmanuel Vadot 	sc->aw_vdd = vdd;
869ce0618beSEmmanuel Vadot 
870*3177f7cdSEmmanuel Vadot 	if (sc->aw_reg_vqmmc == NULL)
871ce0618beSEmmanuel Vadot 		return;
872ce0618beSEmmanuel Vadot 
873ce0618beSEmmanuel Vadot 	switch (1 << vdd) {
874ce0618beSEmmanuel Vadot 	case MMC_OCR_LOW_VOLTAGE:
875ce0618beSEmmanuel Vadot 		min_uvolt = max_uvolt = 1800000;
876ce0618beSEmmanuel Vadot 		break;
877ce0618beSEmmanuel Vadot 	case MMC_OCR_320_330:
878ce0618beSEmmanuel Vadot 		min_uvolt = 3200000;
879ce0618beSEmmanuel Vadot 		max_uvolt = 3300000;
880ce0618beSEmmanuel Vadot 		break;
881ce0618beSEmmanuel Vadot 	case MMC_OCR_330_340:
882ce0618beSEmmanuel Vadot 		min_uvolt = 3300000;
883ce0618beSEmmanuel Vadot 		max_uvolt = 3400000;
884ce0618beSEmmanuel Vadot 		break;
885ce0618beSEmmanuel Vadot 	}
886ce0618beSEmmanuel Vadot 
887ce0618beSEmmanuel Vadot 	if (sc->aw_reg_vqmmc)
888ce0618beSEmmanuel Vadot 		if (regulator_set_voltage(sc->aw_reg_vqmmc,
889ce0618beSEmmanuel Vadot 		    min_uvolt, max_uvolt) != 0)
890ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev,
891ce0618beSEmmanuel Vadot 			    "Cannot set vqmmc to %d<->%d\n",
892ce0618beSEmmanuel Vadot 			    min_uvolt,
893ce0618beSEmmanuel Vadot 			    max_uvolt);
894ce0618beSEmmanuel Vadot }
895ce0618beSEmmanuel Vadot 
896b5be541fSEmmanuel Vadot static int
897b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child)
898b5be541fSEmmanuel Vadot {
899b5be541fSEmmanuel Vadot 	int error;
900b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
901b5be541fSEmmanuel Vadot 	struct mmc_ios *ios;
902ce0618beSEmmanuel Vadot 	unsigned int clock;
903ce0618beSEmmanuel Vadot 	uint32_t reg, div = 1;
904b5be541fSEmmanuel Vadot 
905b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
906b5be541fSEmmanuel Vadot 
907b5be541fSEmmanuel Vadot 	ios = &sc->aw_host.ios;
908b5be541fSEmmanuel Vadot 
909b5be541fSEmmanuel Vadot 	/* Set the bus width. */
910b5be541fSEmmanuel Vadot 	switch (ios->bus_width) {
911b5be541fSEmmanuel Vadot 	case bus_width_1:
912b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1);
913b5be541fSEmmanuel Vadot 		break;
914b5be541fSEmmanuel Vadot 	case bus_width_4:
915b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4);
916b5be541fSEmmanuel Vadot 		break;
917b5be541fSEmmanuel Vadot 	case bus_width_8:
918b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8);
919b5be541fSEmmanuel Vadot 		break;
920b5be541fSEmmanuel Vadot 	}
921b5be541fSEmmanuel Vadot 
922ce0618beSEmmanuel Vadot 	/* Set the voltage */
923ce0618beSEmmanuel Vadot 	if (ios->power_mode == power_off) {
924ce0618beSEmmanuel Vadot 		if (bootverbose)
925ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev, "Powering down sd/mmc\n");
926ce0618beSEmmanuel Vadot 		if (sc->aw_reg_vmmc)
927ce0618beSEmmanuel Vadot 			regulator_disable(sc->aw_reg_vmmc);
928ce0618beSEmmanuel Vadot 		if (sc->aw_reg_vqmmc)
929ce0618beSEmmanuel Vadot 			regulator_disable(sc->aw_reg_vqmmc);
930ce0618beSEmmanuel Vadot 	} else if (sc->aw_vdd != ios->vdd)
931ce0618beSEmmanuel Vadot 		aw_mmc_set_power(sc, ios->vdd);
932ce0618beSEmmanuel Vadot 
933ce0618beSEmmanuel Vadot 	/* Enable ddr mode if needed */
934ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_GCTL);
935ce0618beSEmmanuel Vadot 	if (ios->timing == bus_timing_uhs_ddr50 ||
936ce0618beSEmmanuel Vadot 	  ios->timing == bus_timing_mmc_ddr52)
937ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CTRL_DDR_MOD_SEL;
938ce0618beSEmmanuel Vadot 	else
939ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CTRL_DDR_MOD_SEL;
940ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg);
941ce0618beSEmmanuel Vadot 
9420f7a6420SEmmanuel Vadot 	if (ios->clock && ios->clock != sc->aw_clock) {
9430f7a6420SEmmanuel Vadot 		sc->aw_clock = clock = ios->clock;
944b5be541fSEmmanuel Vadot 
945b5be541fSEmmanuel Vadot 		/* Disable clock */
946b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 0);
947b5be541fSEmmanuel Vadot 		if (error != 0)
948b5be541fSEmmanuel Vadot 			return (error);
949b5be541fSEmmanuel Vadot 
950ce0618beSEmmanuel Vadot 		if (ios->timing == bus_timing_mmc_ddr52 &&
951ce0618beSEmmanuel Vadot 		    (sc->aw_mmc_conf->new_timing ||
952ce0618beSEmmanuel Vadot 		    ios->bus_width == bus_width_8)) {
953ce0618beSEmmanuel Vadot 			div = 2;
954ce0618beSEmmanuel Vadot 			clock <<= 1;
955ce0618beSEmmanuel Vadot 		}
956ce0618beSEmmanuel Vadot 
957b5be541fSEmmanuel Vadot 		/* Reset the divider. */
958ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
959ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_CCLK_DIV;
960ce0618beSEmmanuel Vadot 		reg |= div - 1;
961ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
962ce0618beSEmmanuel Vadot 
963ce0618beSEmmanuel Vadot 		/* New timing mode if needed */
964ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->new_timing) {
965ce0618beSEmmanuel Vadot 			reg = AW_MMC_READ_4(sc, AW_MMC_NTSR);
966ce0618beSEmmanuel Vadot 			reg |= AW_MMC_NTSR_MODE_SELECT;
967ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg);
968ce0618beSEmmanuel Vadot 		}
969b5be541fSEmmanuel Vadot 
970b5be541fSEmmanuel Vadot 		/* Set the MMC clock. */
971ce0618beSEmmanuel Vadot 		error = clk_set_freq(sc->aw_clk_mmc, clock,
972b5be541fSEmmanuel Vadot 		    CLK_SET_ROUND_DOWN);
973b5be541fSEmmanuel Vadot 		if (error != 0) {
974b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
975b5be541fSEmmanuel Vadot 			    "failed to set frequency to %u Hz: %d\n",
976ce0618beSEmmanuel Vadot 			    clock, error);
977b5be541fSEmmanuel Vadot 			return (error);
978b5be541fSEmmanuel Vadot 		}
979b5be541fSEmmanuel Vadot 
980ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->can_calibrate)
981ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN);
982ce0618beSEmmanuel Vadot 
983b5be541fSEmmanuel Vadot 		/* Enable clock. */
984b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 1);
985b5be541fSEmmanuel Vadot 		if (error != 0)
986b5be541fSEmmanuel Vadot 			return (error);
987b5be541fSEmmanuel Vadot 	}
988b5be541fSEmmanuel Vadot 
989b5be541fSEmmanuel Vadot 
990b5be541fSEmmanuel Vadot 	return (0);
991b5be541fSEmmanuel Vadot }
992b5be541fSEmmanuel Vadot 
993b5be541fSEmmanuel Vadot static int
994b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child)
995b5be541fSEmmanuel Vadot {
996b5be541fSEmmanuel Vadot 
997b5be541fSEmmanuel Vadot 	return (0);
998b5be541fSEmmanuel Vadot }
999b5be541fSEmmanuel Vadot 
1000b5be541fSEmmanuel Vadot static int
1001b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child)
1002b5be541fSEmmanuel Vadot {
1003b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1004b5be541fSEmmanuel Vadot 	int error;
1005b5be541fSEmmanuel Vadot 
1006b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1007b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1008b5be541fSEmmanuel Vadot 	while (sc->aw_bus_busy) {
1009b5be541fSEmmanuel Vadot 		error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0);
1010b5be541fSEmmanuel Vadot 		if (error != 0) {
1011b5be541fSEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
1012b5be541fSEmmanuel Vadot 			return (error);
1013b5be541fSEmmanuel Vadot 		}
1014b5be541fSEmmanuel Vadot 	}
1015b5be541fSEmmanuel Vadot 	sc->aw_bus_busy++;
1016b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1017b5be541fSEmmanuel Vadot 
1018b5be541fSEmmanuel Vadot 	return (0);
1019b5be541fSEmmanuel Vadot }
1020b5be541fSEmmanuel Vadot 
1021b5be541fSEmmanuel Vadot static int
1022b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child)
1023b5be541fSEmmanuel Vadot {
1024b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1025b5be541fSEmmanuel Vadot 
1026b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1027b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1028b5be541fSEmmanuel Vadot 	sc->aw_bus_busy--;
1029b5be541fSEmmanuel Vadot 	wakeup(sc);
1030b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1031b5be541fSEmmanuel Vadot 
1032b5be541fSEmmanuel Vadot 	return (0);
1033b5be541fSEmmanuel Vadot }
1034b5be541fSEmmanuel Vadot 
1035b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = {
1036b5be541fSEmmanuel Vadot 	/* Device interface */
1037b5be541fSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_mmc_probe),
1038b5be541fSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_mmc_attach),
1039b5be541fSEmmanuel Vadot 	DEVMETHOD(device_detach,	aw_mmc_detach),
1040b5be541fSEmmanuel Vadot 
1041b5be541fSEmmanuel Vadot 	/* Bus interface */
1042b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_read_ivar,	aw_mmc_read_ivar),
1043b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_write_ivar,	aw_mmc_write_ivar),
1044b5be541fSEmmanuel Vadot 
1045b5be541fSEmmanuel Vadot 	/* MMC bridge interface */
1046b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_update_ios,	aw_mmc_update_ios),
1047b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_request,	aw_mmc_request),
1048b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_get_ro,		aw_mmc_get_ro),
1049b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_acquire_host,	aw_mmc_acquire_host),
1050b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_release_host,	aw_mmc_release_host),
1051b5be541fSEmmanuel Vadot 
1052b5be541fSEmmanuel Vadot 	DEVMETHOD_END
1053b5be541fSEmmanuel Vadot };
1054b5be541fSEmmanuel Vadot 
1055b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass;
1056b5be541fSEmmanuel Vadot 
1057b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = {
1058b5be541fSEmmanuel Vadot 	"aw_mmc",
1059b5be541fSEmmanuel Vadot 	aw_mmc_methods,
1060b5be541fSEmmanuel Vadot 	sizeof(struct aw_mmc_softc),
1061b5be541fSEmmanuel Vadot };
1062b5be541fSEmmanuel Vadot 
1063b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL,
1064b5be541fSEmmanuel Vadot     NULL);
1065b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc);
1066