mesongx_mmc.c revision 1.4 1 1.4 jmcneill /* $NetBSD: mesongx_mmc.c,v 1.4 2019/03/16 12:52:47 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: mesongx_mmc.c,v 1.4 2019/03/16 12:52:47 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/bitops.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
42 1.1 jmcneill #include <dev/sdmmc/sdmmcchip.h>
43 1.1 jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #define SD_EMMC_CLOCK 0x00
48 1.1 jmcneill #define CLOCK_CFG_IRQ_SDIO_SLEEP __BIT(25)
49 1.1 jmcneill #define CLOCK_CFG_ALWAYS_ON __BIT(24)
50 1.1 jmcneill #define CLOCK_CFG_RX_DELAY __BITS(23,20)
51 1.1 jmcneill #define CLOCK_CFG_TX_DELAY __BITS(19,16)
52 1.1 jmcneill #define CLOCK_CFG_SRAM_PD __BITS(15,14)
53 1.1 jmcneill #define CLOCK_CFG_RX_PHASE __BITS(13,12)
54 1.1 jmcneill #define CLOCK_CFG_TX_PHASE __BITS(11,10)
55 1.1 jmcneill #define CLOCK_CFG_CO_PHASE __BITS(9,8)
56 1.1 jmcneill #define CLOCK_CFG_SRC __BITS(7,6)
57 1.1 jmcneill #define CLOCK_CFG_DIV __BITS(5,0)
58 1.1 jmcneill #define SD_EMMC_DELAY 0x04
59 1.1 jmcneill #define SD_EMMC_ADJUST 0x08
60 1.1 jmcneill #define ADJUST_ADJ_DELAY __BITS(21,16)
61 1.1 jmcneill #define ADJUST_CALI_RISE __BIT(14)
62 1.1 jmcneill #define ADJUST_ADJ_ENABLE __BIT(13)
63 1.1 jmcneill #define ADJUST_CALI_ENABLE __BIT(12)
64 1.1 jmcneill #define ADJUST_CALI_SEL __BITS(11,8)
65 1.1 jmcneill #define SD_EMMC_CALOUT 0x10
66 1.1 jmcneill #define CALOUT_CALI_SETUP __BITS(15,8)
67 1.1 jmcneill #define CALOUT_CALI_VLD __BIT(7)
68 1.1 jmcneill #define CALOUT_CALI_IDX __BITS(5,0)
69 1.1 jmcneill #define SD_EMMC_START 0x40
70 1.1 jmcneill #define START_DESC_ADDR __BITS(31,2)
71 1.1 jmcneill #define START_DESC_BUSY __BIT(1)
72 1.1 jmcneill #define START_DESC_INT __BIT(0)
73 1.1 jmcneill #define SD_EMMC_CFG 0x44
74 1.1 jmcneill #define CFG_IP_TXD_ADJ __BITS(31,28)
75 1.1 jmcneill #define CFG_ERR_ABORT __BIT(27)
76 1.1 jmcneill #define CFG_IRQ_DS __BIT(26)
77 1.1 jmcneill #define CFG_TXD_RETRY __BIT(25)
78 1.1 jmcneill #define CFG_TXD_ADD_ERR __BIT(24)
79 1.1 jmcneill #define CFG_AUTO_CLK __BIT(23)
80 1.1 jmcneill #define CFG_STOP_CLK __BIT(22)
81 1.1 jmcneill #define CFG_CMD_LOW __BIT(21)
82 1.1 jmcneill #define CFG_CHK_DS __BIT(20)
83 1.1 jmcneill #define CFG_IGNORE_OWNER __BIT(19)
84 1.1 jmcneill #define CFG_SDCLK_ALWAYS_ON __BIT(18)
85 1.1 jmcneill #define CFG_BLK_GAP_IP __BIT(17)
86 1.1 jmcneill #define CFG_OUT_FALL __BIT(16)
87 1.1 jmcneill #define CFG_RC_CC __BITS(15,12)
88 1.1 jmcneill #define CFG_RESP_TIMEOUT __BIT(11,8)
89 1.1 jmcneill #define CFG_BL_LEN __BITS(7,4)
90 1.1 jmcneill #define CFG_DC_UGT __BIT(3)
91 1.1 jmcneill #define CFG_DDR __BIT(2)
92 1.1 jmcneill #define CFG_BUS_WIDTH __BITS(1,0)
93 1.1 jmcneill #define CFG_BUS_WIDTH_1 0
94 1.1 jmcneill #define CFG_BUS_WIDTH_4 1
95 1.1 jmcneill #define CFG_BUS_WIDTH_8 2
96 1.1 jmcneill #define SD_EMMC_STATUS 0x48
97 1.1 jmcneill #define STATUS_CORE_BUSY __BIT(31)
98 1.1 jmcneill #define STATUS_DESC_BUSY __BIT(30)
99 1.1 jmcneill #define STATUS_BUS_FSM __BIT(29,26)
100 1.1 jmcneill #define STATUS_DS __BIT(25)
101 1.1 jmcneill #define STATUS_CMD_I __BIT(24)
102 1.1 jmcneill #define STATUS_DAT_I __BITS(23,16)
103 1.1 jmcneill #define STATUS_IRQ_SDIO __BIT(15)
104 1.1 jmcneill #define STATUS_RESP_STATUS __BIT(14)
105 1.1 jmcneill #define STATUS_END_OF_CHAIN __BIT(13)
106 1.1 jmcneill #define STATUS_DESC_TIMEOUT __BIT(12)
107 1.1 jmcneill #define STATUS_RESP_TIMEOUT __BIT(11)
108 1.1 jmcneill #define STATUS_RESP_ERR __BIT(10)
109 1.1 jmcneill #define STATUS_DESC_ERR __BIT(9)
110 1.1 jmcneill #define STATUS_TXD_ERR __BIT(8)
111 1.1 jmcneill #define STATUS_RXD_ERR __BITS(7,0)
112 1.1 jmcneill #define STATUS_TIMEOUT (STATUS_DESC_TIMEOUT | STATUS_RESP_TIMEOUT)
113 1.1 jmcneill #define STATUS_ERROR (STATUS_RESP_ERR | STATUS_DESC_ERR | STATUS_RXD_ERR | STATUS_TXD_ERR)
114 1.1 jmcneill #define SD_EMMC_IRQ_EN 0x4c
115 1.1 jmcneill #define IRQ_EN_CFG_SECURE __BIT(16)
116 1.1 jmcneill #define IRQ_EN_IRQ_SDIO __BIT(15)
117 1.1 jmcneill #define IRQ_EN_RESP_STATUS __BIT(14)
118 1.1 jmcneill #define IRQ_EN_END_OF_CHAIN __BIT(13)
119 1.1 jmcneill #define IRQ_EN_DESC_TIMEOUT __BIT(12)
120 1.1 jmcneill #define IRQ_EN_RESP_TIMEOUT __BIT(11)
121 1.1 jmcneill #define IRQ_EN_RESP_ERR __BIT(10)
122 1.1 jmcneill #define IRQ_EN_DESC_ERR __BIT(9)
123 1.1 jmcneill #define IRQ_EN_TXD_ERR __BIT(8)
124 1.1 jmcneill #define IRQ_EN_RXD_ERR __BITS(7,0)
125 1.1 jmcneill #define SD_EMMC_CMD_CFG 0x50
126 1.1 jmcneill #define SD_EMMC_CMD_ARG 0x54
127 1.1 jmcneill #define SD_EMMC_CMD_DAT 0x58
128 1.1 jmcneill #define SD_EMMC_CMD_RSP 0x5c
129 1.1 jmcneill #define SD_EMMC_CMD_RSP1 0x60
130 1.1 jmcneill #define SD_EMMC_CMD_RSP2 0x64
131 1.1 jmcneill #define SD_EMMC_CMD_RSP3 0x68
132 1.1 jmcneill
133 1.1 jmcneill struct mesongx_mmc_desc {
134 1.1 jmcneill uint32_t flags;
135 1.1 jmcneill #define MESONGX_MMC_FLAGS_OWNER __BIT(31)
136 1.1 jmcneill #define MESONGX_MMC_FLAGS_ERROR __BIT(30)
137 1.1 jmcneill #define MESONGX_MMC_FLAGS_CMD_INDEX __BITS(29,24)
138 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_NUM __BIT(23)
139 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_NUM __BIT(22)
140 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_128 __BIT(21)
141 1.1 jmcneill #define MESONGX_MMC_FLAGS_RESP_NOCRC __BIT(20)
142 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_WR __BIT(19)
143 1.1 jmcneill #define MESONGX_MMC_FLAGS_DATA_IO __BIT(18)
144 1.1 jmcneill #define MESONGX_MMC_FLAGS_NO_CMD __BIT(17)
145 1.1 jmcneill #define MESONGX_MMC_FLAGS_NO_RESP __BIT(16)
146 1.1 jmcneill #define MESONGX_MMC_FLAGS_TIMEOUT __BITS(15,12)
147 1.1 jmcneill #define MESONGX_MMC_FLAGS_END_OF_CHAIN __BIT(11)
148 1.1 jmcneill #define MESONGX_MMC_FLAGS_R1B __BIT(10)
149 1.1 jmcneill #define MESONGX_MMC_FLAGS_BLOCK_MODE __BIT(9)
150 1.1 jmcneill #define MESONGX_MMC_FLAGS_LENGTH __BITS(8,0)
151 1.1 jmcneill uint32_t arg;
152 1.1 jmcneill uint32_t data;
153 1.1 jmcneill #define MESONGX_MMC_DATA_BIG_ENDIAN __BIT(1)
154 1.1 jmcneill #define MESONGX_MMC_DATA_SRAM __BIT(0)
155 1.1 jmcneill uint32_t resp;
156 1.1 jmcneill #define MESONGX_MMC_RESP_SRAM __BIT(0)
157 1.1 jmcneill } __packed;
158 1.1 jmcneill
159 1.1 jmcneill #define MESONGX_MMC_NDESC 256
160 1.1 jmcneill
161 1.1 jmcneill struct mesongx_mmc_softc;
162 1.1 jmcneill
163 1.1 jmcneill static int mesongx_mmc_match(device_t, cfdata_t, void *);
164 1.1 jmcneill static void mesongx_mmc_attach(device_t, device_t, void *);
165 1.1 jmcneill static void mesongx_mmc_attach_i(device_t);
166 1.1 jmcneill
167 1.1 jmcneill static int mesongx_mmc_intr(void *);
168 1.1 jmcneill static int mesongx_mmc_dma_setup(struct mesongx_mmc_softc *);
169 1.1 jmcneill static int mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *);
170 1.1 jmcneill
171 1.1 jmcneill static int mesongx_mmc_host_reset(sdmmc_chipset_handle_t);
172 1.1 jmcneill static uint32_t mesongx_mmc_host_ocr(sdmmc_chipset_handle_t);
173 1.1 jmcneill static int mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t);
174 1.1 jmcneill static int mesongx_mmc_card_detect(sdmmc_chipset_handle_t);
175 1.1 jmcneill static int mesongx_mmc_write_protect(sdmmc_chipset_handle_t);
176 1.1 jmcneill static int mesongx_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
177 1.1 jmcneill static int mesongx_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
178 1.1 jmcneill static int mesongx_mmc_bus_width(sdmmc_chipset_handle_t, int);
179 1.1 jmcneill static int mesongx_mmc_bus_rod(sdmmc_chipset_handle_t, int);
180 1.1 jmcneill static int mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
181 1.1 jmcneill static int mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t, int);
182 1.1 jmcneill static void mesongx_mmc_exec_command(sdmmc_chipset_handle_t,
183 1.1 jmcneill struct sdmmc_command *);
184 1.1 jmcneill static void mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
185 1.1 jmcneill static void mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t);
186 1.1 jmcneill
187 1.1 jmcneill static struct sdmmc_chip_functions mesongx_mmc_chip_functions = {
188 1.1 jmcneill .host_reset = mesongx_mmc_host_reset,
189 1.1 jmcneill .host_ocr = mesongx_mmc_host_ocr,
190 1.1 jmcneill .host_maxblklen = mesongx_mmc_host_maxblklen,
191 1.1 jmcneill .card_detect = mesongx_mmc_card_detect,
192 1.1 jmcneill .write_protect = mesongx_mmc_write_protect,
193 1.1 jmcneill .bus_power = mesongx_mmc_bus_power,
194 1.1 jmcneill .bus_clock_ddr = mesongx_mmc_bus_clock,
195 1.1 jmcneill .bus_width = mesongx_mmc_bus_width,
196 1.1 jmcneill .bus_rod = mesongx_mmc_bus_rod,
197 1.1 jmcneill .signal_voltage = mesongx_mmc_signal_voltage,
198 1.1 jmcneill .execute_tuning = mesongx_mmc_execute_tuning,
199 1.1 jmcneill .exec_command = mesongx_mmc_exec_command,
200 1.1 jmcneill .card_enable_intr = mesongx_mmc_card_enable_intr,
201 1.1 jmcneill .card_intr_ack = mesongx_mmc_card_intr_ack,
202 1.1 jmcneill };
203 1.1 jmcneill
204 1.1 jmcneill struct mesongx_mmc_softc {
205 1.1 jmcneill device_t sc_dev;
206 1.1 jmcneill bus_space_tag_t sc_bst;
207 1.1 jmcneill bus_space_handle_t sc_bsh;
208 1.1 jmcneill bus_dma_tag_t sc_dmat;
209 1.1 jmcneill int sc_phandle;
210 1.1 jmcneill
211 1.1 jmcneill void *sc_ih;
212 1.1 jmcneill kmutex_t sc_intr_lock;
213 1.1 jmcneill kcondvar_t sc_intr_cv;
214 1.1 jmcneill
215 1.1 jmcneill device_t sc_sdmmc_dev;
216 1.1 jmcneill uint32_t sc_host_ocr;
217 1.1 jmcneill
218 1.1 jmcneill struct sdmmc_command *sc_cmd;
219 1.1 jmcneill
220 1.1 jmcneill bus_dma_segment_t sc_desc_segs[1];
221 1.1 jmcneill int sc_desc_nsegs;
222 1.1 jmcneill bus_size_t sc_desc_size;
223 1.1 jmcneill bus_dmamap_t sc_desc_map;
224 1.1 jmcneill int sc_desc_ndesc;
225 1.1 jmcneill void *sc_desc_desc;
226 1.1 jmcneill
227 1.1 jmcneill bus_dmamap_t sc_dmabounce_map;
228 1.1 jmcneill void *sc_dmabounce_buf;
229 1.1 jmcneill size_t sc_dmabounce_buflen;
230 1.1 jmcneill
231 1.1 jmcneill struct clk *sc_clk_core;
232 1.1 jmcneill struct clk *sc_clk_clkin[2];
233 1.1 jmcneill
234 1.1 jmcneill struct fdtbus_reset *sc_rst;
235 1.1 jmcneill
236 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_cd;
237 1.1 jmcneill int sc_gpio_cd_inverted;
238 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_wp;
239 1.1 jmcneill int sc_gpio_wp_inverted;
240 1.1 jmcneill
241 1.1 jmcneill struct fdtbus_regulator *sc_reg_vmmc;
242 1.1 jmcneill struct fdtbus_regulator *sc_reg_vqmmc;
243 1.1 jmcneill
244 1.1 jmcneill struct fdtbus_mmc_pwrseq *sc_pwrseq;
245 1.1 jmcneill
246 1.1 jmcneill u_int sc_max_frequency;
247 1.1 jmcneill bool sc_non_removable;
248 1.1 jmcneill bool sc_broken_cd;
249 1.1 jmcneill };
250 1.1 jmcneill
251 1.1 jmcneill CFATTACH_DECL_NEW(mesongx_mmc, sizeof(struct mesongx_mmc_softc),
252 1.1 jmcneill mesongx_mmc_match, mesongx_mmc_attach, NULL, NULL);
253 1.1 jmcneill
254 1.1 jmcneill #define MMC_WRITE(sc, reg, val) \
255 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
256 1.1 jmcneill #define MMC_READ(sc, reg) \
257 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
258 1.1 jmcneill
259 1.1 jmcneill static const struct of_compat_data compat_data[] = {
260 1.1 jmcneill { "amlogic,meson-gx-mmc", 1 },
261 1.1 jmcneill { "amlogic,meson-gxbb-mmc", 1 },
262 1.1 jmcneill { NULL }
263 1.1 jmcneill };
264 1.1 jmcneill
265 1.1 jmcneill static int
266 1.1 jmcneill mesongx_mmc_match(device_t parent, cfdata_t cf, void *aux)
267 1.1 jmcneill {
268 1.1 jmcneill struct fdt_attach_args * const faa = aux;
269 1.1 jmcneill
270 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static void
274 1.1 jmcneill mesongx_mmc_attach(device_t parent, device_t self, void *aux)
275 1.1 jmcneill {
276 1.1 jmcneill struct mesongx_mmc_softc * const sc = device_private(self);
277 1.1 jmcneill struct fdt_attach_args * const faa = aux;
278 1.1 jmcneill const int phandle = faa->faa_phandle;
279 1.1 jmcneill char intrstr[128];
280 1.1 jmcneill bus_addr_t addr;
281 1.1 jmcneill bus_size_t size;
282 1.1 jmcneill
283 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
284 1.1 jmcneill aprint_error(": couldn't get registers\n");
285 1.1 jmcneill return;
286 1.1 jmcneill }
287 1.1 jmcneill
288 1.1 jmcneill sc->sc_clk_core = fdtbus_clock_get(phandle, "core");
289 1.1 jmcneill sc->sc_clk_clkin[0] = fdtbus_clock_get(phandle, "clkin0");
290 1.1 jmcneill sc->sc_clk_clkin[1] = fdtbus_clock_get(phandle, "clkin1");
291 1.1 jmcneill
292 1.1 jmcneill if (sc->sc_clk_core == NULL || sc->sc_clk_clkin[0] == NULL ||
293 1.1 jmcneill sc->sc_clk_clkin[1] == NULL) {
294 1.1 jmcneill aprint_error(": couldn't get clocks\n");
295 1.1 jmcneill return;
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill sc->sc_rst = fdtbus_reset_get_index(phandle, 0);
299 1.1 jmcneill if (sc->sc_rst == NULL) {
300 1.1 jmcneill aprint_error(": couldn't get reset\n");
301 1.1 jmcneill return;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
305 1.1 jmcneill
306 1.1 jmcneill if (clk_enable(sc->sc_clk_core) != 0) {
307 1.1 jmcneill aprint_error(": couldn't enable core clock\n");
308 1.1 jmcneill return;
309 1.1 jmcneill }
310 1.1 jmcneill if (clk_enable(sc->sc_clk_clkin[0]) != 0 ||
311 1.1 jmcneill clk_enable(sc->sc_clk_clkin[1]) != 0) {
312 1.1 jmcneill aprint_error(": couldn't enable clkin clocks\n");
313 1.1 jmcneill return;
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
317 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
318 1.1 jmcneill return;
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill sc->sc_dev = self;
322 1.1 jmcneill sc->sc_phandle = phandle;
323 1.1 jmcneill sc->sc_bst = faa->faa_bst;
324 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
325 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
326 1.1 jmcneill cv_init(&sc->sc_intr_cv, "gxmmcirq");
327 1.1 jmcneill
328 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
329 1.1 jmcneill aprint_error(": couldn't map registers\n");
330 1.1 jmcneill return;
331 1.1 jmcneill }
332 1.1 jmcneill
333 1.1 jmcneill aprint_naive("\n");
334 1.1 jmcneill aprint_normal(": eMMC/SD/SDIO controller\n");
335 1.1 jmcneill
336 1.1 jmcneill sc->sc_reg_vmmc = fdtbus_regulator_acquire(phandle, "vmmc-supply");
337 1.1 jmcneill sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
338 1.1 jmcneill
339 1.1 jmcneill sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
340 1.1 jmcneill GPIO_PIN_INPUT);
341 1.1 jmcneill sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
342 1.1 jmcneill GPIO_PIN_INPUT);
343 1.1 jmcneill
344 1.1 jmcneill sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 1 : 0;
345 1.1 jmcneill sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 1 : 0;
346 1.1 jmcneill
347 1.1 jmcneill sc->sc_non_removable = of_hasprop(phandle, "non-removable");
348 1.1 jmcneill sc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
349 1.1 jmcneill
350 1.1 jmcneill if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_frequency))
351 1.1 jmcneill sc->sc_max_frequency = 52000000;
352 1.1 jmcneill
353 1.1 jmcneill if (mesongx_mmc_dma_setup(sc) != 0 ||
354 1.1 jmcneill mesongx_mmc_dmabounce_setup(sc) != 0) {
355 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
356 1.1 jmcneill return;
357 1.1 jmcneill }
358 1.1 jmcneill
359 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
360 1.1 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
361 1.1 jmcneill return;
362 1.1 jmcneill }
363 1.1 jmcneill
364 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, FDT_INTR_MPSAFE,
365 1.1 jmcneill mesongx_mmc_intr, sc);
366 1.1 jmcneill if (sc->sc_ih == NULL) {
367 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
368 1.1 jmcneill intrstr);
369 1.1 jmcneill return;
370 1.1 jmcneill }
371 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
372 1.1 jmcneill
373 1.2 jmcneill if (sc->sc_pwrseq)
374 1.2 jmcneill fdtbus_mmc_pwrseq_reset(sc->sc_pwrseq);
375 1.2 jmcneill
376 1.1 jmcneill config_interrupts(self, mesongx_mmc_attach_i);
377 1.1 jmcneill }
378 1.1 jmcneill
379 1.1 jmcneill static int
380 1.1 jmcneill mesongx_mmc_dma_setup(struct mesongx_mmc_softc *sc)
381 1.1 jmcneill {
382 1.1 jmcneill int error;
383 1.1 jmcneill
384 1.1 jmcneill sc->sc_desc_ndesc = MESONGX_MMC_NDESC;
385 1.1 jmcneill sc->sc_desc_size = sizeof(struct mesongx_mmc_desc) *
386 1.1 jmcneill sc->sc_desc_ndesc;
387 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_size,
388 1.1 jmcneill sizeof(struct mesongx_mmc_desc),
389 1.1 jmcneill sc->sc_desc_size, sc->sc_desc_segs, 1,
390 1.1 jmcneill &sc->sc_desc_nsegs, BUS_DMA_WAITOK);
391 1.1 jmcneill if (error)
392 1.1 jmcneill return error;
393 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_desc_segs,
394 1.1 jmcneill sc->sc_desc_nsegs, sc->sc_desc_size,
395 1.1 jmcneill &sc->sc_desc_desc, BUS_DMA_WAITOK);
396 1.1 jmcneill if (error)
397 1.1 jmcneill goto free;
398 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_size, 1,
399 1.1 jmcneill sc->sc_desc_size, 0, BUS_DMA_WAITOK, &sc->sc_desc_map);
400 1.1 jmcneill if (error)
401 1.1 jmcneill goto unmap;
402 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_map,
403 1.1 jmcneill sc->sc_desc_desc, sc->sc_desc_size, NULL, BUS_DMA_WAITOK);
404 1.1 jmcneill if (error)
405 1.1 jmcneill goto destroy;
406 1.1 jmcneill return 0;
407 1.1 jmcneill
408 1.1 jmcneill destroy:
409 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_map);
410 1.1 jmcneill unmap:
411 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_desc_desc, sc->sc_desc_size);
412 1.1 jmcneill free:
413 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, sc->sc_desc_segs, sc->sc_desc_nsegs);
414 1.1 jmcneill return error;
415 1.1 jmcneill }
416 1.1 jmcneill
417 1.1 jmcneill static int
418 1.1 jmcneill mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *sc)
419 1.1 jmcneill {
420 1.1 jmcneill bus_dma_segment_t ds[1];
421 1.1 jmcneill int error, rseg;
422 1.1 jmcneill
423 1.1 jmcneill sc->sc_dmabounce_buflen = MAXPHYS;
424 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmabounce_buflen, 0,
425 1.1 jmcneill sc->sc_dmabounce_buflen, ds, 1, &rseg, BUS_DMA_WAITOK);
426 1.1 jmcneill if (error)
427 1.1 jmcneill return error;
428 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, ds, 1, sc->sc_dmabounce_buflen,
429 1.1 jmcneill &sc->sc_dmabounce_buf, BUS_DMA_WAITOK);
430 1.1 jmcneill if (error)
431 1.1 jmcneill goto free;
432 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmabounce_buflen, 1,
433 1.1 jmcneill sc->sc_dmabounce_buflen, 0, BUS_DMA_WAITOK, &sc->sc_dmabounce_map);
434 1.1 jmcneill if (error)
435 1.1 jmcneill goto unmap;
436 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmabounce_map,
437 1.1 jmcneill sc->sc_dmabounce_buf, sc->sc_dmabounce_buflen, NULL,
438 1.1 jmcneill BUS_DMA_WAITOK);
439 1.1 jmcneill if (error)
440 1.1 jmcneill goto destroy;
441 1.1 jmcneill return 0;
442 1.1 jmcneill
443 1.1 jmcneill destroy:
444 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmabounce_map);
445 1.1 jmcneill unmap:
446 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmabounce_buf,
447 1.1 jmcneill sc->sc_dmabounce_buflen);
448 1.1 jmcneill free:
449 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, ds, rseg);
450 1.1 jmcneill return error;
451 1.1 jmcneill }
452 1.1 jmcneill
453 1.1 jmcneill static int
454 1.1 jmcneill mesongx_mmc_set_clock(struct mesongx_mmc_softc *sc, u_int freq, bool ddr)
455 1.1 jmcneill {
456 1.1 jmcneill int best_diff, best_sel, best_div, sel, div;
457 1.1 jmcneill uint32_t val;
458 1.1 jmcneill
459 1.1 jmcneill if (freq == 0)
460 1.1 jmcneill freq = SDMMC_SDCLK_400K;
461 1.1 jmcneill
462 1.1 jmcneill best_diff = INT_MAX;
463 1.1 jmcneill best_sel = 0;
464 1.1 jmcneill best_div = 0;
465 1.1 jmcneill
466 1.1 jmcneill const u_int target_rate = (freq * 1000) >> ddr;
467 1.1 jmcneill for (sel = 0; sel <= 1; sel++) {
468 1.1 jmcneill const u_int parent_rate = clk_get_rate(sc->sc_clk_clkin[sel]);
469 1.1 jmcneill for (div = 1; div <= 63; div++) {
470 1.1 jmcneill const u_int rate = parent_rate / div;
471 1.1 jmcneill if (rate > target_rate)
472 1.1 jmcneill continue;
473 1.1 jmcneill const int diff = target_rate - rate;
474 1.1 jmcneill if (diff < best_diff) {
475 1.1 jmcneill best_diff = diff;
476 1.1 jmcneill best_sel = sel;
477 1.1 jmcneill best_div = div;
478 1.1 jmcneill }
479 1.1 jmcneill }
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill if (best_diff == INT_MAX)
483 1.1 jmcneill return ERANGE;
484 1.1 jmcneill
485 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CLOCK);
486 1.1 jmcneill val |= CLOCK_CFG_ALWAYS_ON;
487 1.1 jmcneill val &= ~CLOCK_CFG_RX_PHASE;
488 1.1 jmcneill val |= __SHIFTIN(0, CLOCK_CFG_RX_PHASE);
489 1.1 jmcneill val &= ~CLOCK_CFG_TX_PHASE;
490 1.1 jmcneill val |= __SHIFTIN(2, CLOCK_CFG_TX_PHASE);
491 1.1 jmcneill val &= ~CLOCK_CFG_CO_PHASE;
492 1.1 jmcneill val |= __SHIFTIN(3, CLOCK_CFG_CO_PHASE);
493 1.1 jmcneill val &= ~CLOCK_CFG_SRC;
494 1.1 jmcneill val |= __SHIFTIN(best_sel, CLOCK_CFG_SRC);
495 1.1 jmcneill val &= ~CLOCK_CFG_DIV;
496 1.1 jmcneill val |= __SHIFTIN(best_div, CLOCK_CFG_DIV);
497 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CLOCK, val);
498 1.1 jmcneill
499 1.1 jmcneill return 0;
500 1.1 jmcneill }
501 1.1 jmcneill
502 1.1 jmcneill static void
503 1.1 jmcneill mesongx_mmc_attach_i(device_t self)
504 1.1 jmcneill {
505 1.1 jmcneill struct mesongx_mmc_softc * const sc = device_private(self);
506 1.1 jmcneill struct sdmmcbus_attach_args saa;
507 1.1 jmcneill uint32_t width;
508 1.1 jmcneill
509 1.1 jmcneill if (sc->sc_pwrseq)
510 1.1 jmcneill fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
511 1.1 jmcneill
512 1.1 jmcneill mesongx_mmc_bus_clock(sc, SDMMC_SDCLK_400K, false);
513 1.1 jmcneill mesongx_mmc_host_reset(sc);
514 1.1 jmcneill mesongx_mmc_bus_width(sc, 1);
515 1.1 jmcneill
516 1.1 jmcneill if (sc->sc_pwrseq)
517 1.1 jmcneill fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
518 1.1 jmcneill
519 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
520 1.1 jmcneill width = 4;
521 1.1 jmcneill
522 1.1 jmcneill memset(&saa, 0, sizeof(saa));
523 1.1 jmcneill saa.saa_busname = "sdmmc";
524 1.1 jmcneill saa.saa_sct = &mesongx_mmc_chip_functions;
525 1.1 jmcneill saa.saa_sch = sc;
526 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
527 1.1 jmcneill saa.saa_clkmin = SDMMC_SDCLK_400K;
528 1.1 jmcneill saa.saa_clkmax = sc->sc_max_frequency / 1000;
529 1.4 jmcneill saa.saa_caps = SMC_CAPS_DMA;
530 1.4 jmcneill #if notyet
531 1.4 jmcneill /* XXX causes init to die when using root on eMMC with ODROID-C2 */
532 1.4 jmcneill saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
533 1.4 jmcneill #endif
534 1.1 jmcneill
535 1.1 jmcneill sc->sc_host_ocr = MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
536 1.1 jmcneill
537 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "cap-sd-highspeed")) {
538 1.1 jmcneill saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
539 1.1 jmcneill sc->sc_host_ocr |= MMC_OCR_HCS;
540 1.1 jmcneill }
541 1.1 jmcneill if (of_getprop_bool(sc->sc_phandle, "cap-mmc-highspeed"))
542 1.1 jmcneill saa.saa_caps |= SMC_CAPS_MMC_HIGHSPEED;
543 1.1 jmcneill
544 1.2 jmcneill if (of_getprop_bool(sc->sc_phandle, "mmc-ddr-1_8v")) {
545 1.2 jmcneill saa.saa_caps |= SMC_CAPS_MMC_DDR52;
546 1.2 jmcneill sc->sc_host_ocr |= MMC_OCR_1_65V_1_95V;
547 1.2 jmcneill }
548 1.2 jmcneill if (of_getprop_bool(sc->sc_phandle, "mmc-hs200-1_8v")) {
549 1.2 jmcneill saa.saa_caps |= SMC_CAPS_MMC_HS200;
550 1.2 jmcneill sc->sc_host_ocr |= MMC_OCR_1_65V_1_95V;
551 1.1 jmcneill }
552 1.1 jmcneill
553 1.1 jmcneill if (width == 4)
554 1.1 jmcneill saa.saa_caps |= SMC_CAPS_4BIT_MODE;
555 1.1 jmcneill if (width == 8)
556 1.1 jmcneill saa.saa_caps |= SMC_CAPS_8BIT_MODE;
557 1.1 jmcneill
558 1.1 jmcneill if (sc->sc_gpio_cd)
559 1.1 jmcneill saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
560 1.1 jmcneill
561 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
562 1.1 jmcneill }
563 1.1 jmcneill
564 1.1 jmcneill static int
565 1.1 jmcneill mesongx_mmc_intr(void *priv)
566 1.1 jmcneill {
567 1.1 jmcneill struct mesongx_mmc_softc * const sc = priv;
568 1.1 jmcneill struct sdmmc_command *cmd;
569 1.1 jmcneill int rv = 0;
570 1.1 jmcneill
571 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
572 1.1 jmcneill
573 1.1 jmcneill const uint32_t irq_en = MMC_READ(sc, SD_EMMC_IRQ_EN);
574 1.1 jmcneill const uint32_t status = MMC_READ(sc, SD_EMMC_STATUS) & irq_en;
575 1.1 jmcneill
576 1.1 jmcneill if ((status & STATUS_IRQ_SDIO) != 0) {
577 1.1 jmcneill rv = 1;
578 1.1 jmcneill sdmmc_card_intr(sc->sc_sdmmc_dev);
579 1.1 jmcneill }
580 1.1 jmcneill
581 1.1 jmcneill cmd = sc->sc_cmd;
582 1.1 jmcneill if (cmd == NULL) {
583 1.1 jmcneill device_printf(sc->sc_dev, "WARNING: IRQ with no active command, status %#x\n", status);
584 1.1 jmcneill goto done;
585 1.1 jmcneill }
586 1.1 jmcneill
587 1.1 jmcneill if ((status & STATUS_TIMEOUT) != 0) {
588 1.1 jmcneill rv = 1;
589 1.1 jmcneill cmd->c_error = ETIMEDOUT;
590 1.1 jmcneill goto done;
591 1.1 jmcneill }
592 1.1 jmcneill
593 1.1 jmcneill if ((status & STATUS_ERROR) != 0) {
594 1.1 jmcneill rv = 1;
595 1.1 jmcneill cmd->c_error = EIO;
596 1.1 jmcneill goto done;
597 1.1 jmcneill }
598 1.1 jmcneill
599 1.1 jmcneill if ((status & STATUS_END_OF_CHAIN) != 0 && (cmd->c_flags & SCF_ITSDONE) == 0) {
600 1.1 jmcneill rv = 1;
601 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_PRESENT) != 0) {
602 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136) {
603 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
604 1.1 jmcneill cmd->c_resp[1] = MMC_READ(sc, SD_EMMC_CMD_RSP1);
605 1.1 jmcneill cmd->c_resp[2] = MMC_READ(sc, SD_EMMC_CMD_RSP2);
606 1.1 jmcneill cmd->c_resp[3] = MMC_READ(sc, SD_EMMC_CMD_RSP3);
607 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC) {
608 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
609 1.1 jmcneill (cmd->c_resp[1] << 24);
610 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
611 1.1 jmcneill (cmd->c_resp[2] << 24);
612 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
613 1.1 jmcneill (cmd->c_resp[3] << 24);
614 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
615 1.1 jmcneill }
616 1.1 jmcneill } else {
617 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
618 1.1 jmcneill }
619 1.1 jmcneill }
620 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
621 1.1 jmcneill cmd->c_error = 0;
622 1.1 jmcneill goto done;
623 1.1 jmcneill }
624 1.1 jmcneill
625 1.1 jmcneill done:
626 1.1 jmcneill if (rv) {
627 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
628 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, irq_en);
629 1.1 jmcneill }
630 1.1 jmcneill
631 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
632 1.1 jmcneill
633 1.1 jmcneill return rv;
634 1.1 jmcneill }
635 1.1 jmcneill
636 1.1 jmcneill static int
637 1.1 jmcneill mesongx_mmc_host_reset(sdmmc_chipset_handle_t sch)
638 1.1 jmcneill {
639 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
640 1.1 jmcneill uint32_t val;
641 1.1 jmcneill
642 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
643 1.1 jmcneill
644 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
645 1.1 jmcneill val &= ~CFG_RC_CC;
646 1.1 jmcneill val |= __SHIFTIN(ilog2(16), CFG_RC_CC);
647 1.1 jmcneill val |= CFG_SDCLK_ALWAYS_ON;
648 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
649 1.1 jmcneill
650 1.1 jmcneill return 0;
651 1.1 jmcneill }
652 1.1 jmcneill
653 1.1 jmcneill static uint32_t
654 1.1 jmcneill mesongx_mmc_host_ocr(sdmmc_chipset_handle_t sch)
655 1.1 jmcneill {
656 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
657 1.1 jmcneill
658 1.1 jmcneill return sc->sc_host_ocr;
659 1.1 jmcneill }
660 1.1 jmcneill
661 1.1 jmcneill static int
662 1.1 jmcneill mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
663 1.1 jmcneill {
664 1.1 jmcneill return 512;
665 1.1 jmcneill }
666 1.1 jmcneill
667 1.1 jmcneill static int
668 1.1 jmcneill mesongx_mmc_card_detect(sdmmc_chipset_handle_t sch)
669 1.1 jmcneill {
670 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
671 1.1 jmcneill int val;
672 1.1 jmcneill
673 1.1 jmcneill if (sc->sc_non_removable || sc->sc_broken_cd) {
674 1.1 jmcneill /*
675 1.1 jmcneill * Non-removable or broken card detect flag set in
676 1.1 jmcneill * DT, assume always present
677 1.1 jmcneill */
678 1.1 jmcneill return 1;
679 1.1 jmcneill } else if (sc->sc_gpio_cd != NULL) {
680 1.1 jmcneill val = fdtbus_gpio_read(sc->sc_gpio_cd);
681 1.1 jmcneill if (sc->sc_gpio_cd_inverted)
682 1.1 jmcneill val = !val;
683 1.1 jmcneill return val;
684 1.1 jmcneill } else {
685 1.1 jmcneill return 1;
686 1.1 jmcneill }
687 1.1 jmcneill }
688 1.1 jmcneill
689 1.1 jmcneill static int
690 1.1 jmcneill mesongx_mmc_write_protect(sdmmc_chipset_handle_t sch)
691 1.1 jmcneill {
692 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
693 1.1 jmcneill int val;
694 1.1 jmcneill
695 1.1 jmcneill if (sc->sc_gpio_wp != NULL) {
696 1.1 jmcneill val = fdtbus_gpio_read(sc->sc_gpio_wp);
697 1.1 jmcneill if (sc->sc_gpio_wp_inverted)
698 1.1 jmcneill val = !val;
699 1.1 jmcneill return val;
700 1.1 jmcneill }
701 1.1 jmcneill
702 1.1 jmcneill return 0;
703 1.1 jmcneill }
704 1.1 jmcneill
705 1.1 jmcneill static int
706 1.1 jmcneill mesongx_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
707 1.1 jmcneill {
708 1.1 jmcneill return 0;
709 1.1 jmcneill }
710 1.1 jmcneill
711 1.1 jmcneill static int
712 1.1 jmcneill mesongx_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
713 1.1 jmcneill {
714 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
715 1.1 jmcneill uint32_t val;
716 1.1 jmcneill int error;
717 1.1 jmcneill
718 1.1 jmcneill error = mesongx_mmc_set_clock(sc, freq, ddr);
719 1.1 jmcneill if (error != 0)
720 1.1 jmcneill return error;
721 1.1 jmcneill
722 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
723 1.1 jmcneill if (ddr)
724 1.1 jmcneill val |= CFG_DDR;
725 1.1 jmcneill else
726 1.1 jmcneill val &= ~CFG_DDR;
727 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
728 1.1 jmcneill
729 1.1 jmcneill return 0;
730 1.1 jmcneill }
731 1.1 jmcneill
732 1.1 jmcneill static int
733 1.1 jmcneill mesongx_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
734 1.1 jmcneill {
735 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
736 1.1 jmcneill uint32_t val;
737 1.1 jmcneill
738 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
739 1.1 jmcneill val &= ~CFG_BUS_WIDTH;
740 1.1 jmcneill
741 1.1 jmcneill switch (width) {
742 1.1 jmcneill case 1:
743 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_1, CFG_BUS_WIDTH);
744 1.1 jmcneill break;
745 1.1 jmcneill case 4:
746 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_4, CFG_BUS_WIDTH);
747 1.1 jmcneill break;
748 1.1 jmcneill case 8:
749 1.1 jmcneill val |= __SHIFTIN(CFG_BUS_WIDTH_8, CFG_BUS_WIDTH);
750 1.1 jmcneill break;
751 1.1 jmcneill default:
752 1.1 jmcneill return EINVAL;
753 1.1 jmcneill }
754 1.1 jmcneill
755 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
756 1.1 jmcneill
757 1.1 jmcneill return 0;
758 1.1 jmcneill }
759 1.1 jmcneill
760 1.1 jmcneill static int
761 1.1 jmcneill mesongx_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
762 1.1 jmcneill {
763 1.1 jmcneill return -1;
764 1.1 jmcneill }
765 1.1 jmcneill
766 1.1 jmcneill static int
767 1.1 jmcneill mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
768 1.1 jmcneill {
769 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
770 1.1 jmcneill u_int uvol;
771 1.1 jmcneill int error;
772 1.1 jmcneill
773 1.1 jmcneill if (sc->sc_reg_vqmmc == NULL)
774 1.1 jmcneill return 0;
775 1.1 jmcneill
776 1.1 jmcneill switch (signal_voltage) {
777 1.1 jmcneill case SDMMC_SIGNAL_VOLTAGE_330:
778 1.1 jmcneill uvol = 3300000;
779 1.1 jmcneill break;
780 1.1 jmcneill case SDMMC_SIGNAL_VOLTAGE_180:
781 1.1 jmcneill uvol = 1800000;
782 1.1 jmcneill break;
783 1.1 jmcneill default:
784 1.1 jmcneill return EINVAL;
785 1.1 jmcneill }
786 1.1 jmcneill
787 1.1 jmcneill error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
788 1.1 jmcneill if (error != 0)
789 1.1 jmcneill return 0;
790 1.1 jmcneill
791 1.1 jmcneill error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
792 1.1 jmcneill if (error != 0)
793 1.1 jmcneill return error;
794 1.1 jmcneill
795 1.1 jmcneill return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
796 1.1 jmcneill }
797 1.1 jmcneill
798 1.1 jmcneill static int
799 1.1 jmcneill mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
800 1.1 jmcneill {
801 1.1 jmcneill switch (timing) {
802 1.1 jmcneill case SDMMC_TIMING_MMC_HS200:
803 1.1 jmcneill break;
804 1.1 jmcneill default:
805 1.1 jmcneill return EINVAL;
806 1.1 jmcneill }
807 1.1 jmcneill
808 1.1 jmcneill return 0;
809 1.1 jmcneill }
810 1.1 jmcneill
811 1.1 jmcneill static int
812 1.1 jmcneill mesongx_mmc_dma_prepare(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd, uint32_t cmdflags)
813 1.1 jmcneill {
814 1.1 jmcneill struct mesongx_mmc_desc *dma = sc->sc_desc_desc;
815 1.1 jmcneill bus_dmamap_t map = cmd->c_dmamap;
816 1.1 jmcneill u_int xferlen, blen, resid;
817 1.1 jmcneill bus_size_t off;
818 1.1 jmcneill uint32_t flags;
819 1.1 jmcneill int desc, seg;
820 1.1 jmcneill
821 1.1 jmcneill if (cmd->c_blklen > 512) {
822 1.1 jmcneill device_printf(sc->sc_dev, "block length %d not supported\n", cmd->c_blklen);
823 1.1 jmcneill return EINVAL;
824 1.1 jmcneill }
825 1.1 jmcneill
826 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) {
827 1.1 jmcneill if (map->dm_segs[seg].ds_len % cmd->c_blklen != 0) {
828 1.1 jmcneill /* Force DMA bounce for unaligned transfers */
829 1.1 jmcneill map = NULL;
830 1.1 jmcneill break;
831 1.1 jmcneill }
832 1.1 jmcneill }
833 1.1 jmcneill
834 1.1 jmcneill if (map == NULL) {
835 1.1 jmcneill map = sc->sc_dmabounce_map;
836 1.1 jmcneill cmd->c_flags |= SCF_NEED_BOUNCE;
837 1.1 jmcneill
838 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) != 0) {
839 1.1 jmcneill memset(sc->sc_dmabounce_buf, 0, cmd->c_datalen);
840 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
841 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREREAD);
842 1.1 jmcneill } else {
843 1.1 jmcneill memcpy(sc->sc_dmabounce_buf, cmd->c_data, cmd->c_datalen);
844 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
845 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREWRITE);
846 1.1 jmcneill }
847 1.1 jmcneill }
848 1.1 jmcneill
849 1.1 jmcneill desc = 0;
850 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) {
851 1.1 jmcneill bus_addr_t paddr = map->dm_segs[seg].ds_addr;
852 1.1 jmcneill bus_size_t len = map->dm_segs[seg].ds_len;
853 1.1 jmcneill resid = uimin(len, cmd->c_resid);
854 1.1 jmcneill off = 0;
855 1.1 jmcneill while (resid > 0) {
856 1.1 jmcneill if (desc == sc->sc_desc_ndesc)
857 1.1 jmcneill break;
858 1.1 jmcneill
859 1.1 jmcneill flags = cmdflags;
860 1.1 jmcneill
861 1.1 jmcneill if (resid >= cmd->c_blklen) {
862 1.1 jmcneill xferlen = (resid / cmd->c_blklen) * cmd->c_blklen;
863 1.1 jmcneill blen = xferlen / cmd->c_blklen;
864 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_BLOCK_MODE;
865 1.1 jmcneill } else {
866 1.1 jmcneill blen = xferlen = resid;
867 1.1 jmcneill }
868 1.1 jmcneill KASSERT(xferlen > 0);
869 1.1 jmcneill KASSERT(blen <= 512);
870 1.1 jmcneill
871 1.1 jmcneill flags |= __SHIFTIN(blen % 512, MESONGX_MMC_FLAGS_LENGTH);
872 1.1 jmcneill if (desc > 0)
873 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_NO_CMD;
874 1.1 jmcneill if (cmd->c_resid == xferlen)
875 1.1 jmcneill flags |= MESONGX_MMC_FLAGS_END_OF_CHAIN;
876 1.1 jmcneill
877 1.1 jmcneill dma[desc].flags = htole32(flags);
878 1.1 jmcneill dma[desc].arg = htole32(cmd->c_arg);
879 1.1 jmcneill dma[desc].data = htole32(paddr + off);
880 1.1 jmcneill dma[desc].resp = 0;
881 1.1 jmcneill
882 1.1 jmcneill cmd->c_resid -= xferlen;
883 1.1 jmcneill resid -= xferlen;
884 1.1 jmcneill off += xferlen;
885 1.1 jmcneill
886 1.1 jmcneill if (cmd->c_resid == 0)
887 1.1 jmcneill break;
888 1.1 jmcneill
889 1.1 jmcneill ++desc;
890 1.1 jmcneill }
891 1.1 jmcneill }
892 1.1 jmcneill if (desc == sc->sc_desc_ndesc) {
893 1.1 jmcneill device_printf(sc->sc_dev,
894 1.1 jmcneill "not enough descriptors for %d byte transfer (%d segs)!\n",
895 1.1 jmcneill cmd->c_datalen, map->dm_nsegs);
896 1.1 jmcneill return EIO;
897 1.1 jmcneill }
898 1.1 jmcneill
899 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
900 1.1 jmcneill sc->sc_desc_size, BUS_DMASYNC_PREWRITE);
901 1.1 jmcneill
902 1.1 jmcneill return 0;
903 1.1 jmcneill }
904 1.1 jmcneill
905 1.1 jmcneill static void
906 1.1 jmcneill mesongx_mmc_dma_complete(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd)
907 1.1 jmcneill {
908 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
909 1.1 jmcneill sc->sc_desc_size, BUS_DMASYNC_POSTWRITE);
910 1.1 jmcneill
911 1.1 jmcneill if ((cmd->c_flags & SCF_NEED_BOUNCE) != 0) {
912 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) != 0) {
913 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
914 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTREAD);
915 1.1 jmcneill memcpy(cmd->c_data, sc->sc_dmabounce_buf, cmd->c_datalen);
916 1.1 jmcneill } else {
917 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
918 1.1 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTWRITE);
919 1.1 jmcneill }
920 1.1 jmcneill }
921 1.1 jmcneill }
922 1.1 jmcneill
923 1.1 jmcneill static void
924 1.1 jmcneill mesongx_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
925 1.1 jmcneill {
926 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
927 1.1 jmcneill uint32_t cmdflags, val;
928 1.1 jmcneill int error;
929 1.1 jmcneill
930 1.1 jmcneill const uint32_t irq_mask = IRQ_EN_RESP_STATUS |
931 1.1 jmcneill IRQ_EN_END_OF_CHAIN |
932 1.1 jmcneill IRQ_EN_DESC_TIMEOUT |
933 1.1 jmcneill IRQ_EN_RESP_TIMEOUT |
934 1.1 jmcneill IRQ_EN_RESP_ERR |
935 1.1 jmcneill IRQ_EN_DESC_ERR |
936 1.1 jmcneill IRQ_EN_TXD_ERR |
937 1.1 jmcneill IRQ_EN_RXD_ERR;
938 1.1 jmcneill
939 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
940 1.1 jmcneill
941 1.1 jmcneill while (sc->sc_cmd != NULL)
942 1.1 jmcneill cv_wait(&sc->sc_intr_cv, &sc->sc_intr_lock);
943 1.1 jmcneill sc->sc_cmd = cmd;
944 1.1 jmcneill
945 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
946 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, MMC_READ(sc, SD_EMMC_STATUS));
947 1.1 jmcneill
948 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
949 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | irq_mask);
950 1.1 jmcneill
951 1.1 jmcneill cmdflags = MESONGX_MMC_FLAGS_OWNER;
952 1.1 jmcneill cmdflags |= __SHIFTIN(12, MESONGX_MMC_FLAGS_TIMEOUT); /* 2^12 = 4096 ms timeout */
953 1.1 jmcneill cmdflags |= __SHIFTIN(cmd->c_opcode, MESONGX_MMC_FLAGS_CMD_INDEX);
954 1.1 jmcneill
955 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_PRESENT) == 0) {
956 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_NO_RESP;
957 1.1 jmcneill } else {
958 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_NUM;
959 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_136) != 0)
960 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_128;
961 1.1 jmcneill if ((cmd->c_flags & SCF_RSP_CRC) == 0)
962 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_RESP_NOCRC;
963 1.3 jmcneill if ((cmd->c_flags & SCF_RSP_MASK) == SCF_RSP_R1B)
964 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_R1B;
965 1.1 jmcneill }
966 1.1 jmcneill
967 1.1 jmcneill if (cmd->c_datalen > 0) {
968 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_DATA_IO;
969 1.1 jmcneill if ((cmd->c_flags & SCF_CMD_READ) == 0)
970 1.1 jmcneill cmdflags |= MESONGX_MMC_FLAGS_DATA_WR;
971 1.1 jmcneill
972 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_CFG);
973 1.1 jmcneill val &= ~CFG_BL_LEN;
974 1.1 jmcneill val |= __SHIFTIN(ilog2(cmd->c_blklen), CFG_BL_LEN);
975 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CFG, val);
976 1.1 jmcneill
977 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
978 1.1 jmcneill cmd->c_error = mesongx_mmc_dma_prepare(sc, cmd, cmdflags);
979 1.1 jmcneill if (cmd->c_error != 0)
980 1.1 jmcneill goto done;
981 1.1 jmcneill
982 1.1 jmcneill const bus_addr_t desc_paddr = sc->sc_desc_map->dm_segs[0].ds_addr;
983 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, desc_paddr | START_DESC_BUSY); /* starts transfer */
984 1.1 jmcneill } else {
985 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_CFG, cmdflags | MESONGX_MMC_FLAGS_END_OF_CHAIN);
986 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_DAT, 0);
987 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_CMD_ARG, cmd->c_arg); /* starts transfer */
988 1.1 jmcneill }
989 1.1 jmcneill
990 1.1 jmcneill struct bintime timeout = { .sec = 5, .frac = 0 };
991 1.1 jmcneill const struct bintime epsilon = { .sec = 1, .frac = 0 };
992 1.1 jmcneill
993 1.1 jmcneill while ((cmd->c_flags & SCF_ITSDONE) == 0 && cmd->c_error == 0) {
994 1.1 jmcneill error = cv_timedwaitbt(&sc->sc_intr_cv, &sc->sc_intr_lock, &timeout, &epsilon);
995 1.1 jmcneill if (error != 0) {
996 1.1 jmcneill cmd->c_error = error;
997 1.1 jmcneill goto done;
998 1.1 jmcneill }
999 1.1 jmcneill }
1000 1.1 jmcneill
1001 1.1 jmcneill if (cmd->c_error == 0 && cmd->c_datalen > 0)
1002 1.1 jmcneill mesongx_mmc_dma_complete(sc, cmd);
1003 1.1 jmcneill
1004 1.1 jmcneill done:
1005 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_START, 0);
1006 1.1 jmcneill
1007 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
1008 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val & ~irq_mask);
1009 1.1 jmcneill
1010 1.1 jmcneill sc->sc_cmd = NULL;
1011 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
1012 1.1 jmcneill
1013 1.1 jmcneill #ifdef MESONGX_MMC_DEBUG
1014 1.1 jmcneill if (cmd->c_error != 0) {
1015 1.1 jmcneill for (u_int reg = 0x00; reg < 0x100; reg += 0x10) {
1016 1.1 jmcneill device_printf(sc->sc_dev, " %02x: %08x %08x %08x %08x\n", reg,
1017 1.1 jmcneill MMC_READ(sc, reg + 0),
1018 1.1 jmcneill MMC_READ(sc, reg + 4),
1019 1.1 jmcneill MMC_READ(sc, reg + 8),
1020 1.1 jmcneill MMC_READ(sc, reg + 12));
1021 1.1 jmcneill }
1022 1.1 jmcneill }
1023 1.1 jmcneill #endif
1024 1.1 jmcneill
1025 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1026 1.1 jmcneill }
1027 1.1 jmcneill
1028 1.1 jmcneill static void
1029 1.1 jmcneill mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1030 1.1 jmcneill {
1031 1.1 jmcneill struct mesongx_mmc_softc * const sc = sch;
1032 1.1 jmcneill uint32_t val;
1033 1.1 jmcneill
1034 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
1035 1.1 jmcneill
1036 1.1 jmcneill val = MMC_READ(sc, SD_EMMC_IRQ_EN);
1037 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | IRQ_EN_IRQ_SDIO);
1038 1.1 jmcneill
1039 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1040 1.1 jmcneill }
1041 1.1 jmcneill
1042 1.1 jmcneill static void
1043 1.1 jmcneill mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
1044 1.1 jmcneill {
1045 1.1 jmcneill struct mesongx_mmc_softc *sc = sch;
1046 1.1 jmcneill
1047 1.1 jmcneill MMC_WRITE(sc, SD_EMMC_STATUS, STATUS_IRQ_SDIO);
1048 1.1 jmcneill }
1049