xref: /freebsd/sys/arm/allwinner/aw_mmc.c (revision 0f7a6420fe692de6ef010294066bdf796829191d)
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;
120*0f7a6420SEmmanuel 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",
270ce0618beSEmmanuel Vadot 	    &sc->aw_reg_vmmc) == 0 && bootverbose)
271ce0618beSEmmanuel Vadot 		device_printf(dev, "vmmc-supply regulator found\n");
272ce0618beSEmmanuel Vadot 	if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply",
273ce0618beSEmmanuel Vadot 	    &sc->aw_reg_vqmmc) == 0 && bootverbose)
274ce0618beSEmmanuel Vadot 		device_printf(dev, "vqmmc-supply regulator found\n");
275ce0618beSEmmanuel Vadot 
276b5be541fSEmmanuel Vadot 	sc->aw_host.f_min = 400000;
277b5be541fSEmmanuel Vadot 	sc->aw_host.f_max = 52000000;
278b5be541fSEmmanuel Vadot 	sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
279ce0618beSEmmanuel Vadot 	sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 |
280ce0618beSEmmanuel Vadot 			   MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 |
281ce0618beSEmmanuel Vadot 			   MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52;
282ce0618beSEmmanuel Vadot 
283ce0618beSEmmanuel Vadot 	sc->aw_host.caps |= MMC_CAP_SIGNALING_330 /* | MMC_CAP_SIGNALING_180 */;
284ce0618beSEmmanuel Vadot 
285b5be541fSEmmanuel Vadot 	if (bus_width >= 4)
286b5be541fSEmmanuel Vadot 		sc->aw_host.caps |= MMC_CAP_4_BIT_DATA;
287b5be541fSEmmanuel Vadot 	if (bus_width >= 8)
288b5be541fSEmmanuel Vadot 		sc->aw_host.caps |= MMC_CAP_8_BIT_DATA;
289b5be541fSEmmanuel Vadot 
290b5be541fSEmmanuel Vadot 	child = device_add_child(dev, "mmc", -1);
291b5be541fSEmmanuel Vadot 	if (child == NULL) {
292b5be541fSEmmanuel Vadot 		device_printf(dev, "attaching MMC bus failed!\n");
293b5be541fSEmmanuel Vadot 		goto fail;
294b5be541fSEmmanuel Vadot 	}
295b5be541fSEmmanuel Vadot 	if (device_probe_and_attach(child) != 0) {
296b5be541fSEmmanuel Vadot 		device_printf(dev, "attaching MMC child failed!\n");
297b5be541fSEmmanuel Vadot 		device_delete_child(dev, child);
298b5be541fSEmmanuel Vadot 		goto fail;
299b5be541fSEmmanuel Vadot 	}
300b5be541fSEmmanuel Vadot 
301b5be541fSEmmanuel Vadot 	return (0);
302b5be541fSEmmanuel Vadot 
303b5be541fSEmmanuel Vadot fail:
304b5be541fSEmmanuel Vadot 	callout_drain(&sc->aw_timeoutc);
305b5be541fSEmmanuel Vadot 	mtx_destroy(&sc->aw_mtx);
306b5be541fSEmmanuel Vadot 	bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand);
307b5be541fSEmmanuel Vadot 	bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res);
308b5be541fSEmmanuel Vadot 
309b5be541fSEmmanuel Vadot 	return (ENXIO);
310b5be541fSEmmanuel Vadot }
311b5be541fSEmmanuel Vadot 
312b5be541fSEmmanuel Vadot static int
313b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev)
314b5be541fSEmmanuel Vadot {
315b5be541fSEmmanuel Vadot 
316b5be541fSEmmanuel Vadot 	return (EBUSY);
317b5be541fSEmmanuel Vadot }
318b5be541fSEmmanuel Vadot 
319b5be541fSEmmanuel Vadot static void
320b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
321b5be541fSEmmanuel Vadot {
322b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
323b5be541fSEmmanuel Vadot 
324b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
325b5be541fSEmmanuel Vadot 	if (err) {
326b5be541fSEmmanuel Vadot 		sc->aw_dma_map_err = err;
327b5be541fSEmmanuel Vadot 		return;
328b5be541fSEmmanuel Vadot 	}
329b5be541fSEmmanuel Vadot 	sc->aw_dma_desc_phys = segs[0].ds_addr;
330b5be541fSEmmanuel Vadot }
331b5be541fSEmmanuel Vadot 
332b5be541fSEmmanuel Vadot static int
333b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc)
334b5be541fSEmmanuel Vadot {
335b5be541fSEmmanuel Vadot 	int dma_desc_size, error;
336b5be541fSEmmanuel Vadot 
337b5be541fSEmmanuel Vadot 	/* Allocate the DMA descriptor memory. */
338b5be541fSEmmanuel Vadot 	dma_desc_size = sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS;
339b5be541fSEmmanuel Vadot 	error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev),
340b5be541fSEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,
341b5be541fSEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
342b5be541fSEmmanuel Vadot 	    dma_desc_size, 1, dma_desc_size, 0, NULL, NULL, &sc->aw_dma_tag);
343b5be541fSEmmanuel Vadot 	if (error)
344b5be541fSEmmanuel Vadot 		return (error);
345b5be541fSEmmanuel Vadot 	error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc,
346b5be541fSEmmanuel Vadot 	    BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->aw_dma_map);
347b5be541fSEmmanuel Vadot 	if (error)
348b5be541fSEmmanuel Vadot 		return (error);
349b5be541fSEmmanuel Vadot 
350b5be541fSEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_tag, sc->aw_dma_map,
351b5be541fSEmmanuel Vadot 	    sc->aw_dma_desc, dma_desc_size, aw_dma_desc_cb, sc, 0);
352b5be541fSEmmanuel Vadot 	if (error)
353b5be541fSEmmanuel Vadot 		return (error);
354b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
355b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
356b5be541fSEmmanuel Vadot 
357b5be541fSEmmanuel Vadot 	/* Create the DMA map for data transfers. */
358b5be541fSEmmanuel Vadot 	error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev),
359b5be541fSEmmanuel Vadot 	    AW_MMC_DMA_ALIGN, 0,
360b5be541fSEmmanuel Vadot 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
361ce0618beSEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS,
362ce0618beSEmmanuel Vadot 	    sc->aw_mmc_conf->dma_xferlen, BUS_DMA_ALLOCNOW, NULL, NULL,
363b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_tag);
364b5be541fSEmmanuel Vadot 	if (error)
365b5be541fSEmmanuel Vadot 		return (error);
366b5be541fSEmmanuel Vadot 	error = bus_dmamap_create(sc->aw_dma_buf_tag, 0,
367b5be541fSEmmanuel Vadot 	    &sc->aw_dma_buf_map);
368b5be541fSEmmanuel Vadot 	if (error)
369b5be541fSEmmanuel Vadot 		return (error);
370b5be541fSEmmanuel Vadot 
371b5be541fSEmmanuel Vadot 	return (0);
372b5be541fSEmmanuel Vadot }
373b5be541fSEmmanuel Vadot 
374b5be541fSEmmanuel Vadot static void
375b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err)
376b5be541fSEmmanuel Vadot {
377b5be541fSEmmanuel Vadot 	int i;
378b5be541fSEmmanuel Vadot 	struct aw_mmc_dma_desc *dma_desc;
379b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
380b5be541fSEmmanuel Vadot 
381b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
382b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = err;
383b5be541fSEmmanuel Vadot 
384b5be541fSEmmanuel Vadot 	if (err)
385b5be541fSEmmanuel Vadot 		return;
386b5be541fSEmmanuel Vadot 
387b5be541fSEmmanuel Vadot 	dma_desc = sc->aw_dma_desc;
388b5be541fSEmmanuel Vadot 	for (i = 0; i < nsegs; i++) {
389b5be541fSEmmanuel Vadot 		dma_desc[i].buf_size = segs[i].ds_len;
390b5be541fSEmmanuel Vadot 		dma_desc[i].buf_addr = segs[i].ds_addr;
391b5be541fSEmmanuel Vadot 		dma_desc[i].config = AW_MMC_DMA_CONFIG_CH |
392b5be541fSEmmanuel Vadot 		    AW_MMC_DMA_CONFIG_OWN;
393b5be541fSEmmanuel Vadot 		if (i == 0)
394b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_FD;
395b5be541fSEmmanuel Vadot 		if (i < (nsegs - 1)) {
396b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_DIC;
397b5be541fSEmmanuel Vadot 			dma_desc[i].next = sc->aw_dma_desc_phys +
398b5be541fSEmmanuel Vadot 			    ((i + 1) * sizeof(struct aw_mmc_dma_desc));
399b5be541fSEmmanuel Vadot 		} else {
400b5be541fSEmmanuel Vadot 			dma_desc[i].config |= AW_MMC_DMA_CONFIG_LD |
401b5be541fSEmmanuel Vadot 			    AW_MMC_DMA_CONFIG_ER;
402b5be541fSEmmanuel Vadot 			dma_desc[i].next = 0;
403b5be541fSEmmanuel Vadot 		}
404b5be541fSEmmanuel Vadot 	}
405b5be541fSEmmanuel Vadot }
406b5be541fSEmmanuel Vadot 
407b5be541fSEmmanuel Vadot static int
408b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc)
409b5be541fSEmmanuel Vadot {
410b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
411b5be541fSEmmanuel Vadot 	int error;
412b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
413b5be541fSEmmanuel Vadot 	uint32_t val;
414b5be541fSEmmanuel Vadot 
415b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
416ce0618beSEmmanuel Vadot 	if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS))
417b5be541fSEmmanuel Vadot 		return (EFBIG);
418b5be541fSEmmanuel Vadot 	error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
419b5be541fSEmmanuel Vadot 	    cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0);
420b5be541fSEmmanuel Vadot 	if (error)
421b5be541fSEmmanuel Vadot 		return (error);
422b5be541fSEmmanuel Vadot 	if (sc->aw_dma_map_err)
423b5be541fSEmmanuel Vadot 		return (sc->aw_dma_map_err);
424b5be541fSEmmanuel Vadot 
425b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
426b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREWRITE;
427b5be541fSEmmanuel Vadot 	else
428b5be541fSEmmanuel Vadot 		sync_op = BUS_DMASYNC_PREREAD;
429b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op);
430b5be541fSEmmanuel Vadot 	bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE);
431b5be541fSEmmanuel Vadot 
432b5be541fSEmmanuel Vadot 	/* Enable DMA */
433b5be541fSEmmanuel Vadot 	val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
434b5be541fSEmmanuel Vadot 	val &= ~AW_MMC_CTRL_FIFO_AC_MOD;
435b5be541fSEmmanuel Vadot 	val |= AW_MMC_CTRL_DMA_ENB;
436b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
437b5be541fSEmmanuel Vadot 
438b5be541fSEmmanuel Vadot 	/* Reset DMA */
439b5be541fSEmmanuel Vadot 	val |= AW_MMC_CTRL_DMA_RST;
440b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val);
441b5be541fSEmmanuel Vadot 
442b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST);
443b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DMAC,
444b5be541fSEmmanuel Vadot 	    AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST);
445b5be541fSEmmanuel Vadot 
446b5be541fSEmmanuel Vadot 	/* Enable RX or TX DMA interrupt */
447b5be541fSEmmanuel Vadot 	if (cmd->data->flags & MMC_DATA_WRITE)
448b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_TX_INT;
449b5be541fSEmmanuel Vadot 	else
450b5be541fSEmmanuel Vadot 		val |= AW_MMC_IDST_RX_INT;
451b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val);
452b5be541fSEmmanuel Vadot 
453b5be541fSEmmanuel Vadot 	/* Set DMA descritptor list address */
454b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys);
455b5be541fSEmmanuel Vadot 
456b5be541fSEmmanuel Vadot 	/* FIFO trigger level */
457b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL);
458b5be541fSEmmanuel Vadot 
459b5be541fSEmmanuel Vadot 	return (0);
460b5be541fSEmmanuel Vadot }
461b5be541fSEmmanuel Vadot 
462b5be541fSEmmanuel Vadot static int
463b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc)
464b5be541fSEmmanuel Vadot {
465b5be541fSEmmanuel Vadot 	int timeout;
466b5be541fSEmmanuel Vadot 
467b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, AW_MMC_RESET);
468b5be541fSEmmanuel Vadot 	timeout = 1000;
469b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
470b5be541fSEmmanuel Vadot 		if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_RESET) == 0)
471b5be541fSEmmanuel Vadot 			break;
472b5be541fSEmmanuel Vadot 		DELAY(100);
473b5be541fSEmmanuel Vadot 	}
474b5be541fSEmmanuel Vadot 	if (timeout == 0)
475b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
476b5be541fSEmmanuel Vadot 
477b5be541fSEmmanuel Vadot 	/* Set the timeout. */
478b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_TMOR,
479b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) |
480b5be541fSEmmanuel Vadot 	    AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK));
481b5be541fSEmmanuel Vadot 
482b5be541fSEmmanuel Vadot 	/* Clear pending interrupts. */
483b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
484b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff);
485b5be541fSEmmanuel Vadot 	/* Unmask interrupts. */
486b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IMKR,
487b5be541fSEmmanuel Vadot 	    AW_MMC_INT_CMD_DONE | AW_MMC_INT_ERR_BIT |
488b5be541fSEmmanuel Vadot 	    AW_MMC_INT_DATA_OVER | AW_MMC_INT_AUTO_STOP_DONE);
489b5be541fSEmmanuel Vadot 	/* Enable interrupts and AHB access. */
490b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL,
491b5be541fSEmmanuel Vadot 	    AW_MMC_READ_4(sc, AW_MMC_GCTL) | AW_MMC_CTRL_INT_ENB);
492b5be541fSEmmanuel Vadot 
493b5be541fSEmmanuel Vadot 	return (0);
494b5be541fSEmmanuel Vadot }
495b5be541fSEmmanuel Vadot 
496b5be541fSEmmanuel Vadot static void
497b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc)
498b5be541fSEmmanuel Vadot {
499b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
500b5be541fSEmmanuel Vadot 	struct mmc_request *req;
501b5be541fSEmmanuel Vadot 	uint32_t val, mask;
502b5be541fSEmmanuel Vadot 	int retry;
503b5be541fSEmmanuel Vadot 
504b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
505b5be541fSEmmanuel Vadot 	if (cmd->error != MMC_ERR_NONE) {
506b5be541fSEmmanuel Vadot 		/* Reset the FIFO and DMA engines. */
507b5be541fSEmmanuel Vadot 		mask = AW_MMC_CTRL_FIFO_RST | AW_MMC_CTRL_DMA_RST;
508b5be541fSEmmanuel Vadot 		val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
509b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask);
510b5be541fSEmmanuel Vadot 
511b5be541fSEmmanuel Vadot 		retry = AW_MMC_RESET_RETRY;
512b5be541fSEmmanuel Vadot 		while (--retry > 0) {
513b5be541fSEmmanuel Vadot 			val = AW_MMC_READ_4(sc, AW_MMC_GCTL);
514b5be541fSEmmanuel Vadot 			if ((val & mask) == 0)
515b5be541fSEmmanuel Vadot 				break;
516b5be541fSEmmanuel Vadot 			DELAY(10);
517b5be541fSEmmanuel Vadot 		}
518b5be541fSEmmanuel Vadot 		if (retry == 0)
519b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
520b5be541fSEmmanuel Vadot 			    "timeout resetting DMA/FIFO\n");
521b5be541fSEmmanuel Vadot 		aw_mmc_update_clock(sc, 1);
522b5be541fSEmmanuel Vadot 	}
523b5be541fSEmmanuel Vadot 
524b5be541fSEmmanuel Vadot 	req = sc->aw_req;
525b5be541fSEmmanuel Vadot 	callout_stop(&sc->aw_timeoutc);
526b5be541fSEmmanuel Vadot 	sc->aw_req = NULL;
527b5be541fSEmmanuel Vadot 	sc->aw_intr = 0;
528b5be541fSEmmanuel Vadot 	sc->aw_resid = 0;
529b5be541fSEmmanuel Vadot 	sc->aw_dma_map_err = 0;
530b5be541fSEmmanuel Vadot 	sc->aw_intr_wait = 0;
531b5be541fSEmmanuel Vadot 	req->done(req);
532b5be541fSEmmanuel Vadot }
533b5be541fSEmmanuel Vadot 
534b5be541fSEmmanuel Vadot static void
535b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc)
536b5be541fSEmmanuel Vadot {
537b5be541fSEmmanuel Vadot 	int timeout;
538b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
539b5be541fSEmmanuel Vadot 	uint32_t status;
540b5be541fSEmmanuel Vadot 
541b5be541fSEmmanuel Vadot 	timeout = 1000;
542b5be541fSEmmanuel Vadot 	while (--timeout > 0) {
543b5be541fSEmmanuel Vadot 		status = AW_MMC_READ_4(sc, AW_MMC_STAR);
544b5be541fSEmmanuel Vadot 		if ((status & AW_MMC_STAR_CARD_BUSY) == 0)
545b5be541fSEmmanuel Vadot 			break;
546b5be541fSEmmanuel Vadot 		DELAY(1000);
547b5be541fSEmmanuel Vadot 	}
548b5be541fSEmmanuel Vadot 	cmd = sc->aw_req->cmd;
549b5be541fSEmmanuel Vadot 	if (timeout == 0) {
550b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
551b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
552b5be541fSEmmanuel Vadot 		return;
553b5be541fSEmmanuel Vadot 	}
554b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT) {
555b5be541fSEmmanuel Vadot 		if (cmd->flags & MMC_RSP_136) {
556b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3);
557b5be541fSEmmanuel Vadot 			cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2);
558b5be541fSEmmanuel Vadot 			cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1);
559b5be541fSEmmanuel Vadot 			cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
560b5be541fSEmmanuel Vadot 		} else
561b5be541fSEmmanuel Vadot 			cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0);
562b5be541fSEmmanuel Vadot 	}
563b5be541fSEmmanuel Vadot 	/* All data has been transferred ? */
564b5be541fSEmmanuel Vadot 	if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len)
565b5be541fSEmmanuel Vadot 		cmd->error = MMC_ERR_FAILED;
566b5be541fSEmmanuel Vadot 	aw_mmc_req_done(sc);
567b5be541fSEmmanuel Vadot }
568b5be541fSEmmanuel Vadot 
569b5be541fSEmmanuel Vadot static void
570b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg)
571b5be541fSEmmanuel Vadot {
572b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
573b5be541fSEmmanuel Vadot 
574b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
575b5be541fSEmmanuel Vadot 	if (sc->aw_req != NULL) {
576b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "controller timeout\n");
577b5be541fSEmmanuel Vadot 		sc->aw_req->cmd->error = MMC_ERR_TIMEOUT;
578b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
579b5be541fSEmmanuel Vadot 	} else
580b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
581b5be541fSEmmanuel Vadot 		    "Spurious timeout - no active request\n");
582b5be541fSEmmanuel Vadot }
583b5be541fSEmmanuel Vadot 
584b5be541fSEmmanuel Vadot static void
585b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg)
586b5be541fSEmmanuel Vadot {
587b5be541fSEmmanuel Vadot 	bus_dmasync_op_t sync_op;
588b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
589b5be541fSEmmanuel Vadot 	struct mmc_data *data;
590b5be541fSEmmanuel Vadot 	uint32_t idst, imask, rint;
591b5be541fSEmmanuel Vadot 
592b5be541fSEmmanuel Vadot 	sc = (struct aw_mmc_softc *)arg;
593b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
594b5be541fSEmmanuel Vadot 	rint = AW_MMC_READ_4(sc, AW_MMC_RISR);
595b5be541fSEmmanuel Vadot 	idst = AW_MMC_READ_4(sc, AW_MMC_IDST);
596b5be541fSEmmanuel Vadot 	imask = AW_MMC_READ_4(sc, AW_MMC_IMKR);
597b5be541fSEmmanuel Vadot 	if (idst == 0 && imask == 0 && rint == 0) {
598b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
599b5be541fSEmmanuel Vadot 		return;
600b5be541fSEmmanuel Vadot 	}
601b5be541fSEmmanuel Vadot #ifdef DEBUG
602b5be541fSEmmanuel Vadot 	device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n",
603b5be541fSEmmanuel Vadot 	    idst, imask, rint);
604b5be541fSEmmanuel Vadot #endif
605b5be541fSEmmanuel Vadot 	if (sc->aw_req == NULL) {
606b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev,
607b5be541fSEmmanuel Vadot 		    "Spurious interrupt - no active request, rint: 0x%08X\n",
608b5be541fSEmmanuel Vadot 		    rint);
609b5be541fSEmmanuel Vadot 		goto end;
610b5be541fSEmmanuel Vadot 	}
611b5be541fSEmmanuel Vadot 	if (rint & AW_MMC_INT_ERR_BIT) {
612ce0618beSEmmanuel Vadot 		if (bootverbose)
613b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint);
614b5be541fSEmmanuel Vadot 		if (rint & AW_MMC_INT_RESP_TIMEOUT)
615b5be541fSEmmanuel Vadot 			sc->aw_req->cmd->error = MMC_ERR_TIMEOUT;
616b5be541fSEmmanuel Vadot 		else
617b5be541fSEmmanuel Vadot 			sc->aw_req->cmd->error = MMC_ERR_FAILED;
618b5be541fSEmmanuel Vadot 		aw_mmc_req_done(sc);
619b5be541fSEmmanuel Vadot 		goto end;
620b5be541fSEmmanuel Vadot 	}
621b5be541fSEmmanuel Vadot 	if (idst & AW_MMC_IDST_ERROR) {
622b5be541fSEmmanuel Vadot 		device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst);
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 
628b5be541fSEmmanuel Vadot 	sc->aw_intr |= rint;
629b5be541fSEmmanuel Vadot 	data = sc->aw_req->cmd->data;
630b5be541fSEmmanuel Vadot 	if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) {
631b5be541fSEmmanuel Vadot 		if (data->flags & MMC_DATA_WRITE)
632b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTWRITE;
633b5be541fSEmmanuel Vadot 		else
634b5be541fSEmmanuel Vadot 			sync_op = BUS_DMASYNC_POSTREAD;
635b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map,
636b5be541fSEmmanuel Vadot 		    sync_op);
637b5be541fSEmmanuel Vadot 		bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map,
638b5be541fSEmmanuel Vadot 		    BUS_DMASYNC_POSTWRITE);
639b5be541fSEmmanuel Vadot 		bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map);
640b5be541fSEmmanuel Vadot 		sc->aw_resid = data->len >> 2;
641b5be541fSEmmanuel Vadot 	}
642b5be541fSEmmanuel Vadot 	if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait)
643b5be541fSEmmanuel Vadot 		aw_mmc_req_ok(sc);
644b5be541fSEmmanuel Vadot 
645b5be541fSEmmanuel Vadot end:
646b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst);
647b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint);
648b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
649b5be541fSEmmanuel Vadot }
650b5be541fSEmmanuel Vadot 
651b5be541fSEmmanuel Vadot static int
652b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req)
653b5be541fSEmmanuel Vadot {
654b5be541fSEmmanuel Vadot 	int blksz;
655b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
656b5be541fSEmmanuel Vadot 	struct mmc_command *cmd;
657b5be541fSEmmanuel Vadot 	uint32_t cmdreg;
658b5be541fSEmmanuel Vadot 	int err;
659b5be541fSEmmanuel Vadot 
660b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
661b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
662b5be541fSEmmanuel Vadot 	if (sc->aw_req) {
663b5be541fSEmmanuel Vadot 		AW_MMC_UNLOCK(sc);
664b5be541fSEmmanuel Vadot 		return (EBUSY);
665b5be541fSEmmanuel Vadot 	}
666b5be541fSEmmanuel Vadot 	sc->aw_req = req;
667b5be541fSEmmanuel Vadot 	cmd = req->cmd;
668b5be541fSEmmanuel Vadot 	cmdreg = AW_MMC_CMDR_LOAD;
669b5be541fSEmmanuel Vadot 	if (cmd->opcode == MMC_GO_IDLE_STATE)
670b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ;
671b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_PRESENT)
672b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_RESP_RCV;
673b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_136)
674b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_LONG_RESP;
675b5be541fSEmmanuel Vadot 	if (cmd->flags & MMC_RSP_CRC)
676b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC;
677b5be541fSEmmanuel Vadot 
678b5be541fSEmmanuel Vadot 	sc->aw_intr = 0;
679b5be541fSEmmanuel Vadot 	sc->aw_resid = 0;
680b5be541fSEmmanuel Vadot 	sc->aw_intr_wait = AW_MMC_INT_CMD_DONE;
681b5be541fSEmmanuel Vadot 	cmd->error = MMC_ERR_NONE;
682b5be541fSEmmanuel Vadot 	if (cmd->data != NULL) {
683b5be541fSEmmanuel Vadot 		sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER;
684b5be541fSEmmanuel Vadot 		cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER;
685b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_MULTI) {
686b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG;
687b5be541fSEmmanuel Vadot 			sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE;
688b5be541fSEmmanuel Vadot 		}
689b5be541fSEmmanuel Vadot 		if (cmd->data->flags & MMC_DATA_WRITE)
690b5be541fSEmmanuel Vadot 			cmdreg |= AW_MMC_CMDR_DIR_WRITE;
691b5be541fSEmmanuel Vadot 		blksz = min(cmd->data->len, MMC_SECTOR_SIZE);
692b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz);
693b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len);
694b5be541fSEmmanuel Vadot 
695b5be541fSEmmanuel Vadot 		err = aw_mmc_prepare_dma(sc);
696b5be541fSEmmanuel Vadot 		if (err != 0)
697b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err);
698b5be541fSEmmanuel Vadot 	}
699b5be541fSEmmanuel Vadot 
700b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg);
701b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode);
702b5be541fSEmmanuel Vadot 	callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz,
703b5be541fSEmmanuel Vadot 	    aw_mmc_timeout, sc);
704b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
705b5be541fSEmmanuel Vadot 
706b5be541fSEmmanuel Vadot 	return (0);
707b5be541fSEmmanuel Vadot }
708b5be541fSEmmanuel Vadot 
709b5be541fSEmmanuel Vadot static int
710b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which,
711b5be541fSEmmanuel Vadot     uintptr_t *result)
712b5be541fSEmmanuel Vadot {
713b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
714b5be541fSEmmanuel Vadot 
715b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
716b5be541fSEmmanuel Vadot 	switch (which) {
717b5be541fSEmmanuel Vadot 	default:
718b5be541fSEmmanuel Vadot 		return (EINVAL);
719b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
720b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_mode;
721b5be541fSEmmanuel Vadot 		break;
722b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
723b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.bus_width;
724b5be541fSEmmanuel Vadot 		break;
725b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
726b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.chip_select;
727b5be541fSEmmanuel Vadot 		break;
728b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
729b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.clock;
730b5be541fSEmmanuel Vadot 		break;
731b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
732b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_min;
733b5be541fSEmmanuel Vadot 		break;
734b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
735b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.f_max;
736b5be541fSEmmanuel Vadot 		break;
737b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
738b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.host_ocr;
739b5be541fSEmmanuel Vadot 		break;
740b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
741b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.mode;
742b5be541fSEmmanuel Vadot 		break;
743b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
744b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ocr;
745b5be541fSEmmanuel Vadot 		break;
746b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
747b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.power_mode;
748b5be541fSEmmanuel Vadot 		break;
749b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
750b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.vdd;
751b5be541fSEmmanuel Vadot 		break;
752b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
753b5be541fSEmmanuel Vadot 		*(int *)result = sc->aw_host.caps;
754b5be541fSEmmanuel Vadot 		break;
755ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
756ce0618beSEmmanuel Vadot 		*(int *)result = sc->aw_host.ios.timing;
757ce0618beSEmmanuel Vadot 		break;
758b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
759b5be541fSEmmanuel Vadot 		*(int *)result = 65535;
760b5be541fSEmmanuel Vadot 		break;
761b5be541fSEmmanuel Vadot 	}
762b5be541fSEmmanuel Vadot 
763b5be541fSEmmanuel Vadot 	return (0);
764b5be541fSEmmanuel Vadot }
765b5be541fSEmmanuel Vadot 
766b5be541fSEmmanuel Vadot static int
767b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which,
768b5be541fSEmmanuel Vadot     uintptr_t value)
769b5be541fSEmmanuel Vadot {
770b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
771b5be541fSEmmanuel Vadot 
772b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
773b5be541fSEmmanuel Vadot 	switch (which) {
774b5be541fSEmmanuel Vadot 	default:
775b5be541fSEmmanuel Vadot 		return (EINVAL);
776b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_MODE:
777b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_mode = value;
778b5be541fSEmmanuel Vadot 		break;
779b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_BUS_WIDTH:
780b5be541fSEmmanuel Vadot 		sc->aw_host.ios.bus_width = value;
781b5be541fSEmmanuel Vadot 		break;
782b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CHIP_SELECT:
783b5be541fSEmmanuel Vadot 		sc->aw_host.ios.chip_select = value;
784b5be541fSEmmanuel Vadot 		break;
785b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CLOCK:
786b5be541fSEmmanuel Vadot 		sc->aw_host.ios.clock = value;
787b5be541fSEmmanuel Vadot 		break;
788b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MODE:
789b5be541fSEmmanuel Vadot 		sc->aw_host.mode = value;
790b5be541fSEmmanuel Vadot 		break;
791b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_OCR:
792b5be541fSEmmanuel Vadot 		sc->aw_host.ocr = value;
793b5be541fSEmmanuel Vadot 		break;
794b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_POWER_MODE:
795b5be541fSEmmanuel Vadot 		sc->aw_host.ios.power_mode = value;
796b5be541fSEmmanuel Vadot 		break;
797b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_VDD:
798b5be541fSEmmanuel Vadot 		sc->aw_host.ios.vdd = value;
799b5be541fSEmmanuel Vadot 		break;
800ce0618beSEmmanuel Vadot 	case MMCBR_IVAR_TIMING:
801ce0618beSEmmanuel Vadot 		sc->aw_host.ios.timing = value;
802ce0618beSEmmanuel Vadot 		break;
803b5be541fSEmmanuel Vadot 	/* These are read-only */
804b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_CAPS:
805b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_HOST_OCR:
806b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MIN:
807b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_F_MAX:
808b5be541fSEmmanuel Vadot 	case MMCBR_IVAR_MAX_DATA:
809b5be541fSEmmanuel Vadot 		return (EINVAL);
810b5be541fSEmmanuel Vadot 	}
811b5be541fSEmmanuel Vadot 
812b5be541fSEmmanuel Vadot 	return (0);
813b5be541fSEmmanuel Vadot }
814b5be541fSEmmanuel Vadot 
815b5be541fSEmmanuel Vadot static int
816b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon)
817b5be541fSEmmanuel Vadot {
818ce0618beSEmmanuel Vadot 	uint32_t reg;
819b5be541fSEmmanuel Vadot 	int retry;
820b5be541fSEmmanuel Vadot 
821ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
822ce0618beSEmmanuel Vadot 	reg &= ~(AW_MMC_CKCR_CCLK_ENB | AW_MMC_CKCR_CCLK_CTRL |
823ce0618beSEmmanuel Vadot 	    AW_MMC_CKCR_CCLK_MASK_DATA0);
824b5be541fSEmmanuel Vadot 
825b5be541fSEmmanuel Vadot 	if (clkon)
826ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CKCR_CCLK_ENB;
827ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0)
828ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CKCR_CCLK_MASK_DATA0;
829b5be541fSEmmanuel Vadot 
830ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
831b5be541fSEmmanuel Vadot 
832ce0618beSEmmanuel Vadot 	reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK |
833b5be541fSEmmanuel Vadot 	    AW_MMC_CMDR_WAIT_PRE_OVER;
834ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg);
835b5be541fSEmmanuel Vadot 	retry = 0xfffff;
836ce0618beSEmmanuel Vadot 
837ce0618beSEmmanuel Vadot 	while (reg & AW_MMC_CMDR_LOAD && --retry > 0) {
838ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CMDR);
839b5be541fSEmmanuel Vadot 		DELAY(10);
840b5be541fSEmmanuel Vadot 	}
841b5be541fSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff);
842b5be541fSEmmanuel Vadot 
843ce0618beSEmmanuel Vadot 	if (reg & AW_MMC_CMDR_LOAD) {
844ce0618beSEmmanuel Vadot 		device_printf(sc->aw_dev, "timeout updating clock\n");
845b5be541fSEmmanuel Vadot 		return (ETIMEDOUT);
846b5be541fSEmmanuel Vadot 	}
847b5be541fSEmmanuel Vadot 
848ce0618beSEmmanuel Vadot 	if (sc->aw_mmc_conf->mask_data0) {
849ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
850ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_CCLK_MASK_DATA0;
851ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
852ce0618beSEmmanuel Vadot 	}
853ce0618beSEmmanuel Vadot 
854ce0618beSEmmanuel Vadot 	return (0);
855ce0618beSEmmanuel Vadot }
856ce0618beSEmmanuel Vadot 
857ce0618beSEmmanuel Vadot static void
858ce0618beSEmmanuel Vadot aw_mmc_set_power(struct aw_mmc_softc *sc, int32_t vdd)
859ce0618beSEmmanuel Vadot {
860ce0618beSEmmanuel Vadot 	int min_uvolt, max_uvolt;
861ce0618beSEmmanuel Vadot 
862ce0618beSEmmanuel Vadot 	sc->aw_vdd = vdd;
863ce0618beSEmmanuel Vadot 
864ce0618beSEmmanuel Vadot 	if (sc->aw_reg_vmmc == NULL && sc->aw_reg_vqmmc == NULL)
865ce0618beSEmmanuel Vadot 		return;
866ce0618beSEmmanuel Vadot 
867ce0618beSEmmanuel Vadot 	switch (1 << vdd) {
868ce0618beSEmmanuel Vadot 	case MMC_OCR_LOW_VOLTAGE:
869ce0618beSEmmanuel Vadot 		min_uvolt = max_uvolt = 1800000;
870ce0618beSEmmanuel Vadot 		break;
871ce0618beSEmmanuel Vadot 	case MMC_OCR_320_330:
872ce0618beSEmmanuel Vadot 		min_uvolt = 3200000;
873ce0618beSEmmanuel Vadot 		max_uvolt = 3300000;
874ce0618beSEmmanuel Vadot 		break;
875ce0618beSEmmanuel Vadot 	case MMC_OCR_330_340:
876ce0618beSEmmanuel Vadot 		min_uvolt = 3300000;
877ce0618beSEmmanuel Vadot 		max_uvolt = 3400000;
878ce0618beSEmmanuel Vadot 		break;
879ce0618beSEmmanuel Vadot 	}
880ce0618beSEmmanuel Vadot 
881ce0618beSEmmanuel Vadot 	if (sc->aw_reg_vmmc)
882ce0618beSEmmanuel Vadot 		if (regulator_set_voltage(sc->aw_reg_vmmc,
883ce0618beSEmmanuel Vadot 		    min_uvolt, max_uvolt) != 0)
884ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev,
885ce0618beSEmmanuel Vadot 			    "Cannot set vmmc to %d<->%d\n",
886ce0618beSEmmanuel Vadot 			    min_uvolt,
887ce0618beSEmmanuel Vadot 			    max_uvolt);
888ce0618beSEmmanuel Vadot 	if (sc->aw_reg_vqmmc)
889ce0618beSEmmanuel Vadot 		if (regulator_set_voltage(sc->aw_reg_vqmmc,
890ce0618beSEmmanuel Vadot 		    min_uvolt, max_uvolt) != 0)
891ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev,
892ce0618beSEmmanuel Vadot 			    "Cannot set vqmmc to %d<->%d\n",
893ce0618beSEmmanuel Vadot 			    min_uvolt,
894ce0618beSEmmanuel Vadot 			    max_uvolt);
895ce0618beSEmmanuel Vadot }
896ce0618beSEmmanuel Vadot 
897b5be541fSEmmanuel Vadot static int
898b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child)
899b5be541fSEmmanuel Vadot {
900b5be541fSEmmanuel Vadot 	int error;
901b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
902b5be541fSEmmanuel Vadot 	struct mmc_ios *ios;
903ce0618beSEmmanuel Vadot 	unsigned int clock;
904ce0618beSEmmanuel Vadot 	uint32_t reg, div = 1;
905b5be541fSEmmanuel Vadot 
906b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
907b5be541fSEmmanuel Vadot 
908b5be541fSEmmanuel Vadot 	ios = &sc->aw_host.ios;
909b5be541fSEmmanuel Vadot 
910b5be541fSEmmanuel Vadot 	/* Set the bus width. */
911b5be541fSEmmanuel Vadot 	switch (ios->bus_width) {
912b5be541fSEmmanuel Vadot 	case bus_width_1:
913b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1);
914b5be541fSEmmanuel Vadot 		break;
915b5be541fSEmmanuel Vadot 	case bus_width_4:
916b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4);
917b5be541fSEmmanuel Vadot 		break;
918b5be541fSEmmanuel Vadot 	case bus_width_8:
919b5be541fSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8);
920b5be541fSEmmanuel Vadot 		break;
921b5be541fSEmmanuel Vadot 	}
922b5be541fSEmmanuel Vadot 
923ce0618beSEmmanuel Vadot 	/* Set the voltage */
924ce0618beSEmmanuel Vadot 	if (ios->power_mode == power_off) {
925ce0618beSEmmanuel Vadot 		if (bootverbose)
926ce0618beSEmmanuel Vadot 			device_printf(sc->aw_dev, "Powering down sd/mmc\n");
927ce0618beSEmmanuel Vadot 		if (sc->aw_reg_vmmc)
928ce0618beSEmmanuel Vadot 			regulator_disable(sc->aw_reg_vmmc);
929ce0618beSEmmanuel Vadot 		if (sc->aw_reg_vqmmc)
930ce0618beSEmmanuel Vadot 			regulator_disable(sc->aw_reg_vqmmc);
931ce0618beSEmmanuel Vadot 	} else if (sc->aw_vdd != ios->vdd)
932ce0618beSEmmanuel Vadot 		aw_mmc_set_power(sc, ios->vdd);
933ce0618beSEmmanuel Vadot 
934ce0618beSEmmanuel Vadot 	/* Enable ddr mode if needed */
935ce0618beSEmmanuel Vadot 	reg = AW_MMC_READ_4(sc, AW_MMC_GCTL);
936ce0618beSEmmanuel Vadot 	if (ios->timing == bus_timing_uhs_ddr50 ||
937ce0618beSEmmanuel Vadot 	  ios->timing == bus_timing_mmc_ddr52)
938ce0618beSEmmanuel Vadot 		reg |= AW_MMC_CTRL_DDR_MOD_SEL;
939ce0618beSEmmanuel Vadot 	else
940ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CTRL_DDR_MOD_SEL;
941ce0618beSEmmanuel Vadot 	AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg);
942ce0618beSEmmanuel Vadot 
943*0f7a6420SEmmanuel Vadot 	if (ios->clock && ios->clock != sc->aw_clock) {
944*0f7a6420SEmmanuel Vadot 		sc->aw_clock = clock = ios->clock;
945b5be541fSEmmanuel Vadot 
946b5be541fSEmmanuel Vadot 		/* Disable clock */
947b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 0);
948b5be541fSEmmanuel Vadot 		if (error != 0)
949b5be541fSEmmanuel Vadot 			return (error);
950b5be541fSEmmanuel Vadot 
951ce0618beSEmmanuel Vadot 		if (ios->timing == bus_timing_mmc_ddr52 &&
952ce0618beSEmmanuel Vadot 		    (sc->aw_mmc_conf->new_timing ||
953ce0618beSEmmanuel Vadot 		    ios->bus_width == bus_width_8)) {
954ce0618beSEmmanuel Vadot 			div = 2;
955ce0618beSEmmanuel Vadot 			clock <<= 1;
956ce0618beSEmmanuel Vadot 		}
957ce0618beSEmmanuel Vadot 
958b5be541fSEmmanuel Vadot 		/* Reset the divider. */
959ce0618beSEmmanuel Vadot 		reg = AW_MMC_READ_4(sc, AW_MMC_CKCR);
960ce0618beSEmmanuel Vadot 		reg &= ~AW_MMC_CKCR_CCLK_DIV;
961ce0618beSEmmanuel Vadot 		reg |= div - 1;
962ce0618beSEmmanuel Vadot 		AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg);
963ce0618beSEmmanuel Vadot 
964ce0618beSEmmanuel Vadot 		/* New timing mode if needed */
965ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->new_timing) {
966ce0618beSEmmanuel Vadot 			reg = AW_MMC_READ_4(sc, AW_MMC_NTSR);
967ce0618beSEmmanuel Vadot 			reg |= AW_MMC_NTSR_MODE_SELECT;
968ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg);
969ce0618beSEmmanuel Vadot 		}
970b5be541fSEmmanuel Vadot 
971b5be541fSEmmanuel Vadot 		/* Set the MMC clock. */
972ce0618beSEmmanuel Vadot 		error = clk_set_freq(sc->aw_clk_mmc, clock,
973b5be541fSEmmanuel Vadot 		    CLK_SET_ROUND_DOWN);
974b5be541fSEmmanuel Vadot 		if (error != 0) {
975b5be541fSEmmanuel Vadot 			device_printf(sc->aw_dev,
976b5be541fSEmmanuel Vadot 			    "failed to set frequency to %u Hz: %d\n",
977ce0618beSEmmanuel Vadot 			    clock, error);
978b5be541fSEmmanuel Vadot 			return (error);
979b5be541fSEmmanuel Vadot 		}
980b5be541fSEmmanuel Vadot 
981ce0618beSEmmanuel Vadot 		if (sc->aw_mmc_conf->can_calibrate)
982ce0618beSEmmanuel Vadot 			AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN);
983ce0618beSEmmanuel Vadot 
984b5be541fSEmmanuel Vadot 		/* Enable clock. */
985b5be541fSEmmanuel Vadot 		error = aw_mmc_update_clock(sc, 1);
986b5be541fSEmmanuel Vadot 		if (error != 0)
987b5be541fSEmmanuel Vadot 			return (error);
988b5be541fSEmmanuel Vadot 	}
989b5be541fSEmmanuel Vadot 
990b5be541fSEmmanuel Vadot 
991b5be541fSEmmanuel Vadot 	return (0);
992b5be541fSEmmanuel Vadot }
993b5be541fSEmmanuel Vadot 
994b5be541fSEmmanuel Vadot static int
995b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child)
996b5be541fSEmmanuel Vadot {
997b5be541fSEmmanuel Vadot 
998b5be541fSEmmanuel Vadot 	return (0);
999b5be541fSEmmanuel Vadot }
1000b5be541fSEmmanuel Vadot 
1001b5be541fSEmmanuel Vadot static int
1002b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child)
1003b5be541fSEmmanuel Vadot {
1004b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1005b5be541fSEmmanuel Vadot 	int error;
1006b5be541fSEmmanuel Vadot 
1007b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1008b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1009b5be541fSEmmanuel Vadot 	while (sc->aw_bus_busy) {
1010b5be541fSEmmanuel Vadot 		error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0);
1011b5be541fSEmmanuel Vadot 		if (error != 0) {
1012b5be541fSEmmanuel Vadot 			AW_MMC_UNLOCK(sc);
1013b5be541fSEmmanuel Vadot 			return (error);
1014b5be541fSEmmanuel Vadot 		}
1015b5be541fSEmmanuel Vadot 	}
1016b5be541fSEmmanuel Vadot 	sc->aw_bus_busy++;
1017b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1018b5be541fSEmmanuel Vadot 
1019b5be541fSEmmanuel Vadot 	return (0);
1020b5be541fSEmmanuel Vadot }
1021b5be541fSEmmanuel Vadot 
1022b5be541fSEmmanuel Vadot static int
1023b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child)
1024b5be541fSEmmanuel Vadot {
1025b5be541fSEmmanuel Vadot 	struct aw_mmc_softc *sc;
1026b5be541fSEmmanuel Vadot 
1027b5be541fSEmmanuel Vadot 	sc = device_get_softc(bus);
1028b5be541fSEmmanuel Vadot 	AW_MMC_LOCK(sc);
1029b5be541fSEmmanuel Vadot 	sc->aw_bus_busy--;
1030b5be541fSEmmanuel Vadot 	wakeup(sc);
1031b5be541fSEmmanuel Vadot 	AW_MMC_UNLOCK(sc);
1032b5be541fSEmmanuel Vadot 
1033b5be541fSEmmanuel Vadot 	return (0);
1034b5be541fSEmmanuel Vadot }
1035b5be541fSEmmanuel Vadot 
1036b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = {
1037b5be541fSEmmanuel Vadot 	/* Device interface */
1038b5be541fSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_mmc_probe),
1039b5be541fSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_mmc_attach),
1040b5be541fSEmmanuel Vadot 	DEVMETHOD(device_detach,	aw_mmc_detach),
1041b5be541fSEmmanuel Vadot 
1042b5be541fSEmmanuel Vadot 	/* Bus interface */
1043b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_read_ivar,	aw_mmc_read_ivar),
1044b5be541fSEmmanuel Vadot 	DEVMETHOD(bus_write_ivar,	aw_mmc_write_ivar),
1045b5be541fSEmmanuel Vadot 
1046b5be541fSEmmanuel Vadot 	/* MMC bridge interface */
1047b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_update_ios,	aw_mmc_update_ios),
1048b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_request,	aw_mmc_request),
1049b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_get_ro,		aw_mmc_get_ro),
1050b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_acquire_host,	aw_mmc_acquire_host),
1051b5be541fSEmmanuel Vadot 	DEVMETHOD(mmcbr_release_host,	aw_mmc_release_host),
1052b5be541fSEmmanuel Vadot 
1053b5be541fSEmmanuel Vadot 	DEVMETHOD_END
1054b5be541fSEmmanuel Vadot };
1055b5be541fSEmmanuel Vadot 
1056b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass;
1057b5be541fSEmmanuel Vadot 
1058b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = {
1059b5be541fSEmmanuel Vadot 	"aw_mmc",
1060b5be541fSEmmanuel Vadot 	aw_mmc_methods,
1061b5be541fSEmmanuel Vadot 	sizeof(struct aw_mmc_softc),
1062b5be541fSEmmanuel Vadot };
1063b5be541fSEmmanuel Vadot 
1064b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL,
1065b5be541fSEmmanuel Vadot     NULL);
1066b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc);
1067